[PATCH] D96881: Avoid Bitcast-GEP fusion for void pointers

mohammad hadi jooybar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 22 12:43:10 PST 2021


hjooybar2 updated this revision to Diff 325541.
hjooybar2 added a comment.

I have updated my implementation base on reviewer's comments. The only reason that I had used `void*` pointer detection instead of `Alloc like function` was to cover more cases. However, since using pointer size in optimization passes are prohibited, I changed the implementation back to `Alloc like function`


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D96881/new/

https://reviews.llvm.org/D96881

Files:
  llvm/lib/Transforms/InstCombine/InstructionCombining.cpp


Index: llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2433,24 +2433,18 @@
     // analysis of unions. If "A" is also a bitcast, wait for A/X to be merged.
     unsigned OffsetBits = DL.getIndexTypeSizeInBits(GEPType);
     APInt Offset(OffsetBits, 0);
-    if (!isa<BitCastInst>(SrcOp) && GEP.accumulateConstantOffset(DL, Offset)) {
+
+    // If the bitcast argument is an allocation, The bitcast is for convertion to
+    // actual type of allocation. Removing such bitcasts, results in having GEPs
+    // with i8* base and pure byte offsets. That means GEP is not aware of Struct
+    // or array hierarchy. 
+    // By avoiding such GEPs, phi translation and MemoryDependencyAnalysis have 
+    // a better chance to succeed
+    if (!isa<BitCastInst>(SrcOp) && GEP.accumulateConstantOffset(DL, Offset) &&
+        !(isa<AllocaInst>(SrcOp) || isAllocationFn(SrcOp, &TLI))) {
       // If this GEP instruction doesn't move the pointer, just replace the GEP
       // with a bitcast of the real input to the dest type.
       if (!Offset) {
-        // If the bitcast is of an allocation, and the allocation will be
-        // converted to match the type of the cast, don't touch this.
-        if (isa<AllocaInst>(SrcOp) || isAllocationFn(SrcOp, &TLI)) {
-          // See if the bitcast simplifies, if so, don't nuke this GEP yet.
-          if (Instruction *I = visitBitCast(*BCI)) {
-            if (I != BCI) {
-              I->takeName(BCI);
-              BCI->getParent()->getInstList().insert(BCI->getIterator(), I);
-              replaceInstUsesWith(*BCI, I);
-            }
-            return &GEP;
-          }
-        }
-
         if (SrcType->getPointerAddressSpace() != GEP.getAddressSpace())
           return new AddrSpaceCastInst(SrcOp, GEPType);
         return new BitCastInst(SrcOp, GEPType);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96881.325541.patch
Type: text/x-patch
Size: 2025 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210222/aee4309d/attachment.bin>


More information about the llvm-commits mailing list