[OpaquePtrs] Remove some uses of type-less CreateLoad APIs (NFC)

Explicitly pass loaded type when creating loads, in preparation
for the deprecation of these APIs.

There are still a couple of uses left.

GitOrigin-RevId: 46354bac76f60e988537181b3609a8e3ac89f523
diff --git a/lib/CodeGen/IslNodeBuilder.cpp b/lib/CodeGen/IslNodeBuilder.cpp
index ba13969..cb176cf 100644
--- a/lib/CodeGen/IslNodeBuilder.cpp
+++ b/lib/CodeGen/IslNodeBuilder.cpp
@@ -1139,22 +1139,22 @@
                       Builder.getInt64(0), Builder.getInt32(2)};
   Value *endPtr = Builder.CreateInBoundsGEP(GlobalDescriptor, endIdx,
                                             ArrayName + "_end_ptr");
-  Value *end = Builder.CreateLoad(endPtr, ArrayName + "_end");
+  Type *type = cast<GEPOperator>(endPtr)->getResultElementType();
+  assert(isa<IntegerType>(type) && "expected type of end to be integral");
+
+  Value *end = Builder.CreateLoad(type, endPtr, ArrayName + "_end");
 
   Value *beginIdx[4] = {Builder.getInt64(0), Builder.getInt32(3),
                         Builder.getInt64(0), Builder.getInt32(1)};
   Value *beginPtr = Builder.CreateInBoundsGEP(GlobalDescriptor, beginIdx,
                                               ArrayName + "_begin_ptr");
-  Value *begin = Builder.CreateLoad(beginPtr, ArrayName + "_begin");
+  Value *begin = Builder.CreateLoad(type, beginPtr, ArrayName + "_begin");
 
   Value *size =
       Builder.CreateNSWSub(end, begin, ArrayName + "_end_begin_delta");
-  Type *endType = dyn_cast<IntegerType>(end->getType());
-  assert(endType && "expected type of end to be integral");
 
-  size = Builder.CreateNSWAdd(end,
-                              ConstantInt::get(endType, 1, /* signed = */ true),
-                              ArrayName + "_size");
+  size = Builder.CreateNSWAdd(
+      end, ConstantInt::get(type, 1, /* signed = */ true), ArrayName + "_size");
 
   return size;
 }
@@ -1211,7 +1211,7 @@
   auto Name = Ptr->getName();
   auto AS = Ptr->getType()->getPointerAddressSpace();
   Ptr = Builder.CreatePointerCast(Ptr, Ty->getPointerTo(AS), Name + ".cast");
-  PreloadVal = Builder.CreateLoad(Ptr, Name + ".load");
+  PreloadVal = Builder.CreateLoad(Ty, Ptr, Name + ".load");
   if (LoadInst *PreloadInst = dyn_cast<LoadInst>(PreloadVal))
     PreloadInst->setAlignment(cast<LoadInst>(AccInst)->getAlign());
 
diff --git a/lib/CodeGen/LoopGenerators.cpp b/lib/CodeGen/LoopGenerators.cpp
index fca800d..7025039 100644
--- a/lib/CodeGen/LoopGenerators.cpp
+++ b/lib/CodeGen/LoopGenerators.cpp
@@ -245,7 +245,8 @@
     SetVector<Value *> OldValues, Type *Ty, Value *Struct, ValueMapT &Map) {
   for (unsigned i = 0; i < OldValues.size(); i++) {
     Value *Address = Builder.CreateStructGEP(Ty, Struct, i);
-    Value *NewValue = Builder.CreateLoad(Address);
+    Type *ElemTy = cast<GetElementPtrInst>(Address)->getResultElementType();
+    Value *NewValue = Builder.CreateLoad(ElemTy, Address);
     NewValue->setName("polly.subfunc.arg." + OldValues[i]->getName());
     Map[OldValues[i]] = NewValue;
   }
diff --git a/lib/CodeGen/LoopGeneratorsGOMP.cpp b/lib/CodeGen/LoopGeneratorsGOMP.cpp
index 66ebd4d..294d91f 100644
--- a/lib/CodeGen/LoopGeneratorsGOMP.cpp
+++ b/lib/CodeGen/LoopGeneratorsGOMP.cpp
@@ -142,8 +142,8 @@
 
   // Add code to load the iv bounds for this set of iterations.
   Builder.SetInsertPoint(PreHeaderBB);
-  Value *LB = Builder.CreateLoad(LBPtr, "polly.par.LB");
-  Value *UB = Builder.CreateLoad(UBPtr, "polly.par.UB");
+  Value *LB = Builder.CreateLoad(LongType, LBPtr, "polly.par.LB");
+  Value *UB = Builder.CreateLoad(LongType, UBPtr, "polly.par.UB");
 
   // Subtract one as the upper bound provided by OpenMP is a < comparison
   // whereas the codegenForSequential function creates a <= comparison.
diff --git a/lib/CodeGen/LoopGeneratorsKMP.cpp b/lib/CodeGen/LoopGeneratorsKMP.cpp
index 1fa3f89..272f8ae 100644
--- a/lib/CodeGen/LoopGeneratorsKMP.cpp
+++ b/lib/CodeGen/LoopGeneratorsKMP.cpp
@@ -181,8 +181,8 @@
                           Map);
 
   const auto Alignment = llvm::Align(is64BitArch() ? 8 : 4);
-  Value *ID =
-      Builder.CreateAlignedLoad(IDPtr, Alignment, "polly.par.global_tid");
+  Value *ID = Builder.CreateAlignedLoad(Builder.getInt32Ty(), IDPtr, Alignment,
+                                        "polly.par.global_tid");
 
   Builder.CreateAlignedStore(LB, LBPtr, Alignment);
   Builder.CreateAlignedStore(UB, UBPtr, Alignment);
@@ -223,8 +223,10 @@
       Builder.CreateCondBr(HasIteration, PreHeaderBB, ExitBB);
 
       Builder.SetInsertPoint(PreHeaderBB);
-      LB = Builder.CreateAlignedLoad(LBPtr, Alignment, "polly.indvar.LB");
-      UB = Builder.CreateAlignedLoad(UBPtr, Alignment, "polly.indvar.UB");
+      LB = Builder.CreateAlignedLoad(LongType, LBPtr, Alignment,
+                                     "polly.indvar.LB");
+      UB = Builder.CreateAlignedLoad(LongType, UBPtr, Alignment,
+                                     "polly.indvar.UB");
     }
     break;
   case OMPGeneralSchedulingType::StaticChunked:
@@ -234,11 +236,13 @@
       Builder.CreateAlignedStore(AdjustedUB, UBPtr, Alignment);
       createCallStaticInit(ID, IsLastPtr, LBPtr, UBPtr, StridePtr, ChunkSize);
 
-      Value *ChunkedStride =
-          Builder.CreateAlignedLoad(StridePtr, Alignment, "polly.kmpc.stride");
+      Value *ChunkedStride = Builder.CreateAlignedLoad(
+          LongType, StridePtr, Alignment, "polly.kmpc.stride");
 
-      LB = Builder.CreateAlignedLoad(LBPtr, Alignment, "polly.indvar.LB");
-      UB = Builder.CreateAlignedLoad(UBPtr, Alignment, "polly.indvar.UB.temp");
+      LB = Builder.CreateAlignedLoad(LongType, LBPtr, Alignment,
+                                     "polly.indvar.LB");
+      UB = Builder.CreateAlignedLoad(LongType, UBPtr, Alignment,
+                                     "polly.indvar.UB.temp");
 
       Value *UBInRange =
           Builder.CreateICmp(llvm::CmpInst::Predicate::ICMP_SLE, UB, AdjustedUB,
@@ -252,9 +256,9 @@
 
       if (Scheduling == OMPGeneralSchedulingType::StaticChunked) {
         Builder.SetInsertPoint(PreHeaderBB);
-        LB = Builder.CreateAlignedLoad(LBPtr, Alignment,
+        LB = Builder.CreateAlignedLoad(LongType, LBPtr, Alignment,
                                        "polly.indvar.LB.entry");
-        UB = Builder.CreateAlignedLoad(UBPtr, Alignment,
+        UB = Builder.CreateAlignedLoad(LongType, UBPtr, Alignment,
                                        "polly.indvar.UB.entry");
       }
 
diff --git a/lib/CodeGen/PerfMonitor.cpp b/lib/CodeGen/PerfMonitor.cpp
index b452dc3..f6efc53 100644
--- a/lib/CodeGen/PerfMonitor.cpp
+++ b/lib/CodeGen/PerfMonitor.cpp
@@ -138,11 +138,12 @@
   // Measure current cycles and compute final timings.
   Function *RDTSCPFn = getRDTSCP();
 
+  Type *Int64Ty = Builder.getInt64Ty();
   Value *CurrentCycles =
       Builder.CreateExtractValue(Builder.CreateCall(RDTSCPFn), {0});
-  Value *CyclesStart = Builder.CreateLoad(CyclesTotalStartPtr, true);
+  Value *CyclesStart = Builder.CreateLoad(Int64Ty, CyclesTotalStartPtr, true);
   Value *CyclesTotal = Builder.CreateSub(CurrentCycles, CyclesStart);
-  Value *CyclesInScops = Builder.CreateLoad(CyclesInScopsPtr, true);
+  Value *CyclesInScops = Builder.CreateLoad(Int64Ty, CyclesInScopsPtr, true);
 
   // Print the runtime information.
   RuntimeDebugBuilder::createCPUPrinter(Builder, "Polly runtime information\n");
@@ -175,11 +176,12 @@
   Builder.SetInsertPoint(FinalStartBB);
   ReturnFromFinal->eraseFromParent();
 
+  Type *Int64Ty = Builder.getInt64Ty();
   Value *CyclesInCurrentScop =
-      Builder.CreateLoad(this->CyclesInCurrentScopPtr, true);
+      Builder.CreateLoad(Int64Ty, this->CyclesInCurrentScopPtr, true);
 
   Value *TripCountForCurrentScop =
-      Builder.CreateLoad(this->TripCountForCurrentScopPtr, true);
+      Builder.CreateLoad(Int64Ty, this->TripCountForCurrentScopPtr, true);
 
   std::string EntryName, ExitName;
   std::tie(EntryName, ExitName) = S.getEntryExitStr();
@@ -231,7 +233,8 @@
   // multiple times. To avoid initializations being run multiple times (and
   // especially to avoid that atExitFn is called more than once), we bail
   // out if the initializer is run more than once.
-  Value *HasRunBefore = Builder.CreateLoad(AlreadyInitializedPtr);
+  Value *HasRunBefore =
+      Builder.CreateLoad(Builder.getInt1Ty(), AlreadyInitializedPtr);
   Builder.CreateCondBr(HasRunBefore, EarlyReturn, InitBB);
   Builder.SetInsertPoint(EarlyReturn);
   Builder.CreateRetVoid();
@@ -276,20 +279,23 @@
 
   Builder.SetInsertPoint(InsertBefore);
   Function *RDTSCPFn = getRDTSCP();
-  LoadInst *CyclesStart = Builder.CreateLoad(CyclesInScopStartPtr, true);
+  Type *Int64Ty = Builder.getInt64Ty();
+  LoadInst *CyclesStart =
+      Builder.CreateLoad(Int64Ty, CyclesInScopStartPtr, true);
   Value *CurrentCycles =
       Builder.CreateExtractValue(Builder.CreateCall(RDTSCPFn), {0});
   Value *CyclesInScop = Builder.CreateSub(CurrentCycles, CyclesStart);
-  Value *CyclesInScops = Builder.CreateLoad(CyclesInScopsPtr, true);
+  Value *CyclesInScops = Builder.CreateLoad(Int64Ty, CyclesInScopsPtr, true);
   CyclesInScops = Builder.CreateAdd(CyclesInScops, CyclesInScop);
   Builder.CreateStore(CyclesInScops, CyclesInScopsPtr, true);
 
-  Value *CyclesInCurrentScop = Builder.CreateLoad(CyclesInCurrentScopPtr, true);
+  Value *CyclesInCurrentScop =
+      Builder.CreateLoad(Int64Ty, CyclesInCurrentScopPtr, true);
   CyclesInCurrentScop = Builder.CreateAdd(CyclesInCurrentScop, CyclesInScop);
   Builder.CreateStore(CyclesInCurrentScop, CyclesInCurrentScopPtr, true);
 
   Value *TripCountForCurrentScop =
-      Builder.CreateLoad(TripCountForCurrentScopPtr, true);
+      Builder.CreateLoad(Int64Ty, TripCountForCurrentScopPtr, true);
   TripCountForCurrentScop =
       Builder.CreateAdd(TripCountForCurrentScop, Builder.getInt64(1));
   Builder.CreateStore(TripCountForCurrentScop, TripCountForCurrentScopPtr,