gollvm: sync with LLVM trunk at 288082d45d6124

Updates code to adapt to recent LLVM changes.
1, Construct AttrBuilder with LLVMContext.
2, CreateLoad function needs a LLVM::Type parameter.
3, INSTANTIATE_TEST_CASE_P is deprecated, replace it with
INSTANTIATE_TEST_SUITE_P.
4, Unit tests adjustments.

Change-Id: I78539c8d3dec1fa4d53e94b70e110c5dfc922f54
Reviewed-on: https://go-review.googlesource.com/c/gollvm/+/384394
Reviewed-by: Eric Fang <eric.fang@arm.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Trust: Cherry Mui <cherryyz@google.com>
diff --git a/bridge/go-llvm-bfunction.cpp b/bridge/go-llvm-bfunction.cpp
index fc80e99..20aed84 100644
--- a/bridge/go-llvm-bfunction.cpp
+++ b/bridge/go-llvm-bfunction.cpp
@@ -106,7 +106,7 @@
   if (abiOracle_->returnInfo().disp() == ParmIndirect) {
     std::string sretname(namegen("sret.formal"));
     arguments_[argIdx]->setName(sretname);
-    llvm::AttrBuilder SRETAttrs;
+    llvm::AttrBuilder SRETAttrs(function()->getContext());
     SRETAttrs.addStructRetAttr(fcnType_->resultType()->type());
     arguments_[argIdx]->addAttrs(SRETAttrs);
     rtnValueMem_ = arguments_[argIdx];
@@ -140,7 +140,7 @@
         paramValues_.push_back(arguments_[argIdx]);
         assert(paramInfo.numArgSlots() == 1);
         if (paramInfo.attr() == AttrByVal) {
-          llvm::AttrBuilder BVAttrs;
+          llvm::AttrBuilder BVAttrs(function()->getContext());
           BVAttrs.addByValAttr(paramTypes[idx]->type());
           arguments_[argIdx]->addAttrs(BVAttrs);
         }
@@ -550,7 +550,7 @@
   std::string castname(namegen("cast"));
   llvm::Value *bitcast = builder.CreateBitCast(toRet->value(), ptst, castname);
   std::string loadname(namegen("ld"));
-  llvm::Instruction *ldinst = builder.CreateLoad(bitcast, loadname);
+  llvm::Instruction *ldinst = builder.CreateLoad(llrt, bitcast, loadname);
   retInstrs->appendInstructions(builder.instructions());
   return ldinst;
 }
diff --git a/bridge/go-llvm-builtins.cpp b/bridge/go-llvm-builtins.cpp
index eabfcea..fcfea48 100644
--- a/bridge/go-llvm-builtins.cpp
+++ b/bridge/go-llvm-builtins.cpp
@@ -522,7 +522,7 @@
   assert(args.size() == 6);
   // GCC __atomic_compare_exchange_n takes a pointer to the old value.
   // We need to load it.
-  llvm::Value *old = builder->CreateLoad(args[1]);
+  llvm::Value *old = builder->CreateLoad(args[1]->getType()->getPointerElementType(), args[1]);
   // FIXME: see atomicLoadMaker, but default to SequentiallyConsistent
   // for success order, Monotonic (i.e. relaxed) for failed order,
   // and false for weak.
diff --git a/bridge/go-llvm-materialize.cpp b/bridge/go-llvm-materialize.cpp
index 3f04478..49aaad3 100644
--- a/bridge/go-llvm-materialize.cpp
+++ b/bridge/go-llvm-materialize.cpp
@@ -1249,7 +1249,7 @@
                                    val->getType()->getPointerAddressSpace());
         llvm::Value *bitcast = builder.CreateBitCast(val, ptv, castname);
         std::string ltag(namegen("ld"));
-        llvm::Value *ld = builder.CreateLoad(bitcast, ltag);
+        llvm::Value *ld = builder.CreateLoad(paramInfo.abiType(), bitcast, ltag);
         state.llargs.push_back(ld);
         continue;
       }
@@ -1310,7 +1310,7 @@
       llvm::Value *fieldgep =
           builder.CreateConstInBoundsGEP2_32(llst, bitcast, 0, i, ftag);
       std::string ltag(namegen("ld"));
-      llvm::Value *ld = builder.CreateLoad(fieldgep, ltag);
+      llvm::Value *ld = builder.CreateLoad(paramInfo.abiTypes()[i], fieldgep, ltag);
       state.llargs.push_back(ld);
     }
   }
@@ -1319,7 +1319,7 @@
 void Llvm_backend::genCallAttributes(GenCallState &state, llvm::CallInst *call)
 {
   const llvm::AttributeList &callAttrList = call->getAttributes();
-  llvm::AttrBuilder retAttrs(callAttrList, llvm::AttributeList::ReturnIndex);
+  llvm::AttrBuilder retAttrs(context_, callAttrList.getRetAttrs());
   const std::vector<Btype *> &paramTypes = state.calleeFcnType->paramTypes();
   size_t na = state.oracle.getFunctionTypeForABI()->getNumParams();
   llvm::SmallVector<llvm::AttributeSet, 4> argAttrs(na);
@@ -1327,7 +1327,7 @@
   // Sret attribute if needed
   const CABIParamInfo &returnInfo = state.oracle.returnInfo();
   if (returnInfo.disp() == ParmIndirect) {
-    llvm::AttrBuilder ab;
+    llvm::AttrBuilder ab(context_);
     ab.addStructRetAttr(state.calleeFcnType->resultType()->type());
     ab.addAttribute(llvm::Attribute::get(call->getContext(), "go_sret"));
     argAttrs[0] = llvm::AttributeSet::get(context_, ab);
@@ -1336,7 +1336,7 @@
   // Nest attribute if needed
   const CABIParamInfo &chainInfo = state.oracle.chainInfo();
   if (chainInfo.disp() != ParmIgnore) {
-    llvm::AttrBuilder ab;
+    llvm::AttrBuilder ab(context_);
     ab.addAttribute(llvm::Attribute::Nest);
     argAttrs[chainInfo.sigOffset()] =
         llvm::AttributeSet::get(context_, ab);
@@ -1351,7 +1351,7 @@
     assert(paramInfo.attr() != AttrStructReturn);
     if (paramInfo.attr() != AttrNone) {
       unsigned off = paramInfo.sigOffset();
-      llvm::AttrBuilder ab;
+      llvm::AttrBuilder ab(context_);
       if (paramInfo.attr() == AttrByVal) {
         ab.addByValAttr(paramTypes[idx]->type());
       } else if (paramInfo.attr() == AttrZext) {
@@ -1470,7 +1470,7 @@
   if (be->triple().getArch() == llvm::Triple::aarch64)
     return makeGetgArm64(resType, builder, be);
   else
-    return builder->CreateLoad(g);
+    return builder->CreateLoad(resType->type(), g);
 }
 
 Bexpression *Llvm_backend::materializeCall(Bexpression *callExpr)
diff --git a/bridge/go-llvm.cpp b/bridge/go-llvm.cpp
index f78fd17..301a062 100644
--- a/bridge/go-llvm.cpp
+++ b/bridge/go-llvm.cpp
@@ -3686,7 +3686,7 @@
   llvm::BasicBlock *finRetBB =
       llvm::BasicBlock::Create(context_, be_->namegen("finret"), func);
   std::string lname(be_->namegen("fload"));
-  llvm::LoadInst *finvarload = builder.CreateLoad(finvar->value(), lname);
+  llvm::LoadInst *finvarload = builder.CreateLoad(finvar->btype()->type(), finvar->value(), lname);
   llvm::Value *tval = be_->boolean_constant_expression(true)->value();
   llvm::Value *cmp = builder.CreateICmp(llvm::CmpInst::Predicate::ICMP_EQ,
                                         finvarload, tval,
@@ -3705,7 +3705,7 @@
   // Populate resume block
   builder.SetInsertPoint(finResBB);
   std::string ename(be_->namegen("excv"));
-  llvm::LoadInst *exload = builder.CreateLoad(extmp, ename);
+  llvm::LoadInst *exload = builder.CreateLoad(extmp->getType()->getPointerElementType(), extmp, ename);
   builder.CreateResume(exload);
 
   return finRetBB;
diff --git a/passes/GoStatepoints.cpp b/passes/GoStatepoints.cpp
index c351706..e1279e9 100644
--- a/passes/GoStatepoints.cpp
+++ b/passes/GoStatepoints.cpp
@@ -1283,13 +1283,13 @@
     return AL;
 
     // Remove the readonly, readnone, and statepoint function attributes.
-  AttrBuilder FnAttrs = AL.getFnAttrs();
+  AttrBuilder FnAttrs(Ctx, AL.getFnAttrs());
   for (auto Attr : FnAttrsToStrip)
     FnAttrs.removeAttribute(Attr);
 
   for (Attribute A : AL.getFnAttrs()) {
     if (isStatepointDirectiveAttr(A))
-      FnAttrs.remove(A);
+      FnAttrs.removeAttribute(A);
   }
 
     // Just skip parameter and return attributes for now
@@ -1994,7 +1994,7 @@
 template <typename AttrHolder>
 static void RemoveNonValidAttrAtIndex(LLVMContext &Ctx, AttrHolder &AH,
                                       unsigned Index) {
-  AttrBuilder R;
+  AttrBuilder R(Ctx);
   AttributeSet AS = AH.getAttributes().getAttributes(Index);
   if (AS.getDereferenceableBytes())
     R.addAttribute(Attribute::get(Ctx, Attribute::Dereferenceable,
@@ -2006,8 +2006,9 @@
     if (AS.hasAttribute(Attr))
       R.addAttribute(Attr);
 
-  if (!R.empty())
-    AH.setAttributes(AH.getAttributes().removeAttributesAtIndex(Ctx, Index, R));
+  AttributeSet AS2 = AttributeSet::get(Ctx,R);
+  if (AS2.getNumAttributes() > 0)
+    AH.setAttributes(AH.getAttributes().removeAttributesAtIndex(Ctx, Index, AttributeMask(AS2)));
 }
 
 static void stripNonValidAttributesFromPrototype(Function &F) {
@@ -2983,9 +2984,10 @@
           // for now. The size is the first field. The optimizer should be
           // able to constant-fold it.
           Value *TD = CI->getArgOperand(1);
+          Type *etyp = TD->getType()->getPointerElementType();
           Value *GEP = Builder.CreateConstInBoundsGEP2_32(
-              TD->getType()->getPointerElementType(), TD, 0, 0);
-          Value *Siz = Builder.CreateLoad(GEP);
+              etyp, TD, 0, 0);
+          Value *Siz = Builder.CreateLoad(etyp, GEP);
           llvm::MaybeAlign malgn(0);
           Builder.CreateMemMove(Dst, malgn, Src, malgn, Siz);
           ToDel.insert(CI);
diff --git a/unittests/BackendCore/BackendArrayStruct.cpp b/unittests/BackendCore/BackendArrayStruct.cpp
index 5d10538..4f40d9c 100644
--- a/unittests/BackendCore/BackendArrayStruct.cpp
+++ b/unittests/BackendCore/BackendArrayStruct.cpp
@@ -20,7 +20,7 @@
 class BackendArrayStructTests
     : public testing::TestWithParam<llvm::CallingConv::ID> {};
 
-INSTANTIATE_TEST_CASE_P(
+INSTANTIATE_TEST_SUITE_P(
     UnitTest, BackendArrayStructTests,
     goBackendUnitTests::CConvs,
     [](const testing::TestParamInfo<BackendArrayStructTests::ParamType> &info) {
diff --git a/unittests/BackendCore/BackendCABIOracleTests.cpp b/unittests/BackendCore/BackendCABIOracleTests.cpp
index 87ae3ae..c1535ba 100644
--- a/unittests/BackendCore/BackendCABIOracleTests.cpp
+++ b/unittests/BackendCore/BackendCABIOracleTests.cpp
@@ -21,7 +21,7 @@
 class BackendCABIOracleTests
     : public testing::TestWithParam<llvm::CallingConv::ID> {};
 
-INSTANTIATE_TEST_CASE_P(
+INSTANTIATE_TEST_SUITE_P(
     UnitTest, BackendCABIOracleTests,
     goBackendUnitTests::CConvs,
     [](const testing::TestParamInfo<BackendCABIOracleTests::ParamType> &info) {
diff --git a/unittests/BackendCore/BackendCallTests.cpp b/unittests/BackendCore/BackendCallTests.cpp
index e20e24c..e835bde 100644
--- a/unittests/BackendCore/BackendCallTests.cpp
+++ b/unittests/BackendCore/BackendCallTests.cpp
@@ -20,7 +20,7 @@
 class BackendCallTests : public testing::TestWithParam<llvm::CallingConv::ID> {
 };
 
-INSTANTIATE_TEST_CASE_P(
+INSTANTIATE_TEST_SUITE_P(
     UnitTest, BackendCallTests,
     goBackendUnitTests::CConvs,
     [](const testing::TestParamInfo<BackendCallTests::ParamType> &info) {
diff --git a/unittests/BackendCore/BackendCoreTests.cpp b/unittests/BackendCore/BackendCoreTests.cpp
index cf132a5..dbed58b 100644
--- a/unittests/BackendCore/BackendCoreTests.cpp
+++ b/unittests/BackendCore/BackendCoreTests.cpp
@@ -18,7 +18,7 @@
 class BackendCoreTests : public testing::TestWithParam<llvm::CallingConv::ID> {
 };
 
-INSTANTIATE_TEST_CASE_P(
+INSTANTIATE_TEST_SUITE_P(
     UnitTest, BackendCoreTests,
     goBackendUnitTests::CConvs,
     [](const testing::TestParamInfo<BackendCoreTests::ParamType> &info) {
diff --git a/unittests/BackendCore/BackendDebugEmit.cpp b/unittests/BackendCore/BackendDebugEmit.cpp
index 9cd92ff..2875ba4 100644
--- a/unittests/BackendCore/BackendDebugEmit.cpp
+++ b/unittests/BackendCore/BackendDebugEmit.cpp
@@ -23,7 +23,7 @@
 class BackendDebugEmit : public testing::TestWithParam<llvm::CallingConv::ID> {
 };
 
-INSTANTIATE_TEST_CASE_P(
+INSTANTIATE_TEST_SUITE_P(
     UnitTest, BackendDebugEmit,
     goBackendUnitTests::CConvs,
     [](const testing::TestParamInfo<BackendDebugEmit::ParamType> &info) {
@@ -46,7 +46,7 @@
   entry:
     %x = alloca i32, align 4
     store i32 0, i32* %x, align 4
-    call void @llvm.dbg.declare(metadata i32* %x, metadata !5, metadata !DIExpression()), !dbg !12
+    call void @llvm.dbg.declare(metadata i32* %x, metadata !4, metadata !DIExpression()), !dbg !12
     ret void
   }
   )RAW_RESULT");
@@ -78,7 +78,7 @@
   DECLARE_EXPECTED_OUTPUT(exp, R"RAW_RESULT(
     define void @foo(i8* nest %nest.0, { i64, i64, i64 }* byval({ i64, i64, i64 }) %p0) #0 {
   entry:
-    call void @llvm.dbg.declare(metadata { i64, i64, i64 }* %p0, metadata !5, metadata !DIExpression()), !dbg !18
+    call void @llvm.dbg.declare(metadata { i64, i64, i64 }* %p0, metadata !4, metadata !DIExpression()), !dbg !18
     ret void
   }
   )RAW_RESULT");
@@ -105,7 +105,7 @@
   DECLARE_EXPECTED_OUTPUT(exp, R"RAW_RESULT(
     define void @foo(i8* nest %nest.0, { i64, i64, i64 }* %p0) #0 {
     entry:
-      call void @llvm.dbg.declare(metadata { i64, i64, i64 }* %p0, metadata !5,
+      call void @llvm.dbg.declare(metadata { i64, i64, i64 }* %p0, metadata !4,
                                   metadata !DIExpression()), !dbg !18
       ret void
     }
@@ -210,7 +210,7 @@
   h.mkLocal("x", bu32t);
 
   DECLARE_EXPECTED_OUTPUT(exp, R"RAW_RESULT(
-    define void @foo(i8* nest %nest.0) #0 !dbg !5 {
+    define void @foo(i8* nest %nest.0) #0 !dbg !4 {
   entry:
     %x = alloca i32, align 4
     ret void, !dbg !10
diff --git a/unittests/BackendCore/BackendExprTests.cpp b/unittests/BackendCore/BackendExprTests.cpp
index 5d7528f..eab503f 100644
--- a/unittests/BackendCore/BackendExprTests.cpp
+++ b/unittests/BackendCore/BackendExprTests.cpp
@@ -20,7 +20,7 @@
 class BackendExprTests : public testing::TestWithParam<llvm::CallingConv::ID> {
 };
 
-INSTANTIATE_TEST_CASE_P(
+INSTANTIATE_TEST_SUITE_P(
     UnitTest, BackendExprTests,
     goBackendUnitTests::CConvs,
     [](const testing::TestParamInfo<BackendExprTests::ParamType> &info) {
diff --git a/unittests/BackendCore/BackendFcnTests.cpp b/unittests/BackendCore/BackendFcnTests.cpp
index 79a3692..0cf95db 100644
--- a/unittests/BackendCore/BackendFcnTests.cpp
+++ b/unittests/BackendCore/BackendFcnTests.cpp
@@ -18,7 +18,7 @@
 
 class BackendFcnTests : public testing::TestWithParam<llvm::CallingConv::ID> {};
 
-INSTANTIATE_TEST_CASE_P(
+INSTANTIATE_TEST_SUITE_P(
     UnitTest, BackendFcnTests,
     goBackendUnitTests::CConvs,
     [](const testing::TestParamInfo<BackendFcnTests::ParamType> &info) {
diff --git a/unittests/BackendCore/BackendNodeTests.cpp b/unittests/BackendCore/BackendNodeTests.cpp
index 86396fc..34255bf 100644
--- a/unittests/BackendCore/BackendNodeTests.cpp
+++ b/unittests/BackendCore/BackendNodeTests.cpp
@@ -21,7 +21,7 @@
 class BackendNodeTests : public testing::TestWithParam<llvm::CallingConv::ID> {
 };
 
-INSTANTIATE_TEST_CASE_P(
+INSTANTIATE_TEST_SUITE_P(
     UnitTest, BackendNodeTests,
     goBackendUnitTests::CConvs,
     [](const testing::TestParamInfo<BackendNodeTests::ParamType> &info) {
diff --git a/unittests/BackendCore/BackendPointerExprTests.cpp b/unittests/BackendCore/BackendPointerExprTests.cpp
index 9311990..58978ec 100644
--- a/unittests/BackendCore/BackendPointerExprTests.cpp
+++ b/unittests/BackendCore/BackendPointerExprTests.cpp
@@ -20,7 +20,7 @@
 class BackendPointerExprTests
     : public testing::TestWithParam<llvm::CallingConv::ID> {};
 
-INSTANTIATE_TEST_CASE_P(
+INSTANTIATE_TEST_SUITE_P(
     UnitTest, BackendPointerExprTests,
     goBackendUnitTests::CConvs,
     [](const testing::TestParamInfo<BackendPointerExprTests::ParamType> &info) {
diff --git a/unittests/BackendCore/BackendStmtTests.cpp b/unittests/BackendCore/BackendStmtTests.cpp
index 5bd959e..866f3ed 100644
--- a/unittests/BackendCore/BackendStmtTests.cpp
+++ b/unittests/BackendCore/BackendStmtTests.cpp
@@ -18,7 +18,7 @@
 class BackendStmtTests : public testing::TestWithParam<llvm::CallingConv::ID> {
 };
 
-INSTANTIATE_TEST_CASE_P(
+INSTANTIATE_TEST_SUITE_P(
     UnitTest, BackendStmtTests,
     goBackendUnitTests::CConvs,
     [](const testing::TestParamInfo<BackendStmtTests::ParamType> &info) {
diff --git a/unittests/BackendCore/BackendTreeIntegrity.cpp b/unittests/BackendCore/BackendTreeIntegrity.cpp
index 6c56512..df6b32b 100644
--- a/unittests/BackendCore/BackendTreeIntegrity.cpp
+++ b/unittests/BackendCore/BackendTreeIntegrity.cpp
@@ -18,7 +18,7 @@
 class BackendTreeIntegrity
     : public testing::TestWithParam<llvm::CallingConv::ID> {};
 
-INSTANTIATE_TEST_CASE_P(
+INSTANTIATE_TEST_SUITE_P(
     UnitTest, BackendTreeIntegrity,
     goBackendUnitTests::CConvs,
     [](const testing::TestParamInfo<BackendTreeIntegrity::ParamType> &info) {
diff --git a/unittests/BackendCore/BackendVarTests.cpp b/unittests/BackendCore/BackendVarTests.cpp
index 4ae8bb6..763602e 100644
--- a/unittests/BackendCore/BackendVarTests.cpp
+++ b/unittests/BackendCore/BackendVarTests.cpp
@@ -22,7 +22,7 @@
 
 class BackendVarTests : public testing::TestWithParam<llvm::CallingConv::ID> {};
 
-INSTANTIATE_TEST_CASE_P(
+INSTANTIATE_TEST_SUITE_P(
     UnitTest, BackendVarTests,
     goBackendUnitTests::CConvs,
     [](const testing::TestParamInfo<BackendVarTests::ParamType> &info) {