224224#include " SIDefines.h"
225225#include " llvm/ADT/SetOperations.h"
226226#include " llvm/ADT/SmallVector.h"
227- #include " llvm/Analysis/ConstantFolding .h"
227+ #include " llvm/Analysis/InstSimplifyFolder .h"
228228#include " llvm/Analysis/Utils/Local.h"
229229#include " llvm/CodeGen/TargetPassConfig.h"
230230#include " llvm/IR/AttributeMask.h"
@@ -445,7 +445,7 @@ class StoreFatPtrsAsIntsAndExpandMemcpyVisitor
445445
446446 ValueToValueMapTy ConvertedForStore;
447447
448- IRBuilder<> IRB;
448+ IRBuilder<InstSimplifyFolder > IRB;
449449
450450 const TargetMachine *TM;
451451
@@ -459,9 +459,10 @@ class StoreFatPtrsAsIntsAndExpandMemcpyVisitor
459459
460460public:
461461 StoreFatPtrsAsIntsAndExpandMemcpyVisitor (BufferFatPtrToIntTypeMap *TypeMap,
462+ const DataLayout &DL,
462463 LLVMContext &Ctx,
463464 const TargetMachine *TM)
464- : TypeMap(TypeMap), IRB(Ctx), TM(TM) {}
465+ : TypeMap(TypeMap), IRB(Ctx, InstSimplifyFolder(DL) ), TM(TM) {}
465466 bool processFunction (Function &F);
466467
467468 bool visitInstruction (Instruction &I) { return false ; }
@@ -683,7 +684,7 @@ class LegalizeBufferContentTypesVisitor
683684 : public InstVisitor<LegalizeBufferContentTypesVisitor, bool > {
684685 friend class InstVisitor <LegalizeBufferContentTypesVisitor, bool >;
685686
686- IRBuilder<> IRB;
687+ IRBuilder<InstSimplifyFolder > IRB;
687688
688689 const DataLayout &DL;
689690
@@ -743,7 +744,7 @@ class LegalizeBufferContentTypesVisitor
743744
744745public:
745746 LegalizeBufferContentTypesVisitor (const DataLayout &DL, LLVMContext &Ctx)
746- : IRB(Ctx), DL(DL) {}
747+ : IRB(Ctx, InstSimplifyFolder(DL) ), DL(DL) {}
747748 bool processFunction (Function &F);
748749};
749750} // namespace
@@ -1326,7 +1327,7 @@ class SplitPtrStructs : public InstVisitor<SplitPtrStructs, PtrParts> {
13261327 const TargetMachine *TM;
13271328 const GCNSubtarget *ST = nullptr ;
13281329
1329- IRBuilder<> IRB;
1330+ IRBuilder<InstSimplifyFolder > IRB;
13301331
13311332 // Copy metadata between instructions if applicable.
13321333 void copyMetadata (Value *Dest, Value *Src);
@@ -1363,8 +1364,9 @@ class SplitPtrStructs : public InstVisitor<SplitPtrStructs, PtrParts> {
13631364 bool IsVolatile, SyncScope::ID SSID);
13641365
13651366public:
1366- SplitPtrStructs (LLVMContext &Ctx, const TargetMachine *TM)
1367- : TM(TM), IRB(Ctx) {}
1367+ SplitPtrStructs (const DataLayout &DL, LLVMContext &Ctx,
1368+ const TargetMachine *TM)
1369+ : TM(TM), IRB(Ctx, InstSimplifyFolder(DL)) {}
13681370
13691371 void processFunction (Function &F);
13701372
@@ -1415,7 +1417,7 @@ PtrParts SplitPtrStructs::getPtrParts(Value *V) {
14151417 return {*RsrcEntry = Rsrc, *OffEntry = Off};
14161418 }
14171419
1418- IRBuilder<>::InsertPointGuard Guard (IRB);
1420+ IRBuilder<InstSimplifyFolder >::InsertPointGuard Guard (IRB);
14191421 if (auto *I = dyn_cast<Instruction>(V)) {
14201422 LLVM_DEBUG (dbgs () << " Recursing to split parts of " << *I << " \n " );
14211423 auto [Rsrc, Off] = visit (*I);
@@ -1479,7 +1481,7 @@ void SplitPtrStructs::getPossibleRsrcRoots(Instruction *I,
14791481}
14801482
14811483void SplitPtrStructs::processConditionals () {
1482- SmallDenseMap<Instruction *, Value *> FoundRsrcs;
1484+ SmallDenseMap<Value *, Value *> FoundRsrcs;
14831485 SmallPtrSet<Value *, 4 > Roots;
14841486 SmallPtrSet<Value *, 4 > Seen;
14851487 for (Instruction *I : Conditionals) {
@@ -1493,7 +1495,7 @@ void SplitPtrStructs::processConditionals() {
14931495 if (MaybeFoundRsrc != FoundRsrcs.end ()) {
14941496 MaybeRsrc = MaybeFoundRsrc->second ;
14951497 } else {
1496- IRBuilder<>::InsertPointGuard Guard (IRB);
1498+ IRBuilder<InstSimplifyFolder >::InsertPointGuard Guard (IRB);
14971499 Roots.clear ();
14981500 Seen.clear ();
14991501 getPossibleRsrcRoots (I, Roots, Seen);
@@ -1558,21 +1560,29 @@ void SplitPtrStructs::processConditionals() {
15581560 // to put the corrections maps in an inconstent state. That'll be handed
15591561 // during the rest of the killing. Also, `ValueToValueMapTy` guarantees
15601562 // that references in that map will be updated as well.
1561- ConditionalTemps.push_back (cast<Instruction>(Rsrc));
1562- ConditionalTemps.push_back (cast<Instruction>(Off));
1563- Rsrc->replaceAllUsesWith (NewRsrc);
1564- Off->replaceAllUsesWith (NewOff);
1563+ // Note that if the temporary instruction got `InstSimplify`'d away, it
1564+ // might be something like a block argument.
1565+ if (auto *RsrcInst = dyn_cast<Instruction>(Rsrc)) {
1566+ ConditionalTemps.push_back (RsrcInst);
1567+ RsrcInst->replaceAllUsesWith (NewRsrc);
1568+ }
1569+ if (auto *OffInst = dyn_cast<Instruction>(Off)) {
1570+ ConditionalTemps.push_back (OffInst);
1571+ OffInst->replaceAllUsesWith (NewOff);
1572+ }
15651573
15661574 // Save on recomputing the cycle traversals in known-root cases.
15671575 if (MaybeRsrc)
15681576 for (Value *V : Seen)
1569- FoundRsrcs[cast<Instruction>(V) ] = NewRsrc;
1577+ FoundRsrcs[V ] = NewRsrc;
15701578 } else if (isa<SelectInst>(I)) {
15711579 if (MaybeRsrc) {
1572- ConditionalTemps.push_back (cast<Instruction>(Rsrc));
1573- Rsrc->replaceAllUsesWith (*MaybeRsrc);
1580+ if (auto *RsrcInst = dyn_cast<Instruction>(Rsrc)) {
1581+ ConditionalTemps.push_back (RsrcInst);
1582+ RsrcInst->replaceAllUsesWith (*MaybeRsrc);
1583+ }
15741584 for (Value *V : Seen)
1575- FoundRsrcs[cast<Instruction>(V) ] = *MaybeRsrc;
1585+ FoundRsrcs[V ] = *MaybeRsrc;
15761586 }
15771587 } else {
15781588 llvm_unreachable (" Only PHIs and selects go in the conditionals list" );
@@ -2426,8 +2436,8 @@ bool AMDGPULowerBufferFatPointers::run(Module &M, const TargetMachine &TM) {
24262436 /* RemoveDeadConstants=*/ false , /* IncludeSelf=*/ true );
24272437 }
24282438
2429- StoreFatPtrsAsIntsAndExpandMemcpyVisitor MemOpsRewrite (&IntTM, M. getContext () ,
2430- &TM);
2439+ StoreFatPtrsAsIntsAndExpandMemcpyVisitor MemOpsRewrite (&IntTM, DL ,
2440+ M. getContext (), &TM);
24312441 LegalizeBufferContentTypesVisitor BufferContentsTypeRewrite (DL,
24322442 M.getContext ());
24332443 for (Function &F : M.functions ()) {
@@ -2472,7 +2482,7 @@ bool AMDGPULowerBufferFatPointers::run(Module &M, const TargetMachine &TM) {
24722482 IntTM.clear ();
24732483 CloneMap.clear ();
24742484
2475- SplitPtrStructs Splitter (M.getContext (), &TM);
2485+ SplitPtrStructs Splitter (DL, M.getContext (), &TM);
24762486 for (Function *F : NeedsPostProcess)
24772487 Splitter.processFunction (*F);
24782488 for (Function *F : Intrinsics) {
0 commit comments