[PATCH] D79956: [InstCombine] Teach PromoteCastOfAllocation to not insert a bitcast into the middle of a group of allocas

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 14 12:31:16 PDT 2020


craig.topper created this revision.
craig.topper added reviewers: spatel, efriedma.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.

Alloca's are usually grouped together at the top of the entry block. We shouldn't insert other instructions between them.

Fixes the weird IR seen in the stack coloring pass here http://lists.llvm.org/pipermail/llvm-dev/2020-May/141421.html   StackColoring should probably also be fixed to handle this better too, but I didn't look into it any further.


https://reviews.llvm.org/D79956

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
  llvm/test/Transforms/InstCombine/alloca.ll


Index: llvm/test/Transforms/InstCombine/alloca.ll
===================================================================
--- llvm/test/Transforms/InstCombine/alloca.ll
+++ llvm/test/Transforms/InstCombine/alloca.ll
@@ -177,3 +177,33 @@
   call void (...) @use(i32* nonnull @int) [ "blah"(i32* %y) ]
   ret void
 }
+
+%struct.pluto = type { [4 x float] }
+%struct.bar = type { [4 x i32*] }
+
+; Make sure we put the TMPCAST bitcast  after the allocas not between.
+define void @test12(<2 x float> %arg) {
+; CHECK-LABEL: @test12(
+; CHECK-NEXT:  bb:
+; CHECK-NEXT:    [[TMP1:%.*]] = alloca { <2 x float>, <2 x float> }, align 8
+; CHECK-NEXT:    [[TMP:%.*]] = alloca [[STRUCT_BAR:%.*]], align 8
+; CHECK-NEXT:    [[TMPCAST:%.*]] = bitcast { <2 x float>, <2 x float> }* [[TMP1]] to %struct.pluto*
+; CHECK-NEXT:    [[TMP5:%.*]] = getelementptr inbounds { <2 x float>, <2 x float> }, { <2 x float>, <2 x float> }* [[TMP1]], i64 0, i32 0
+; CHECK-NEXT:    store <2 x float> [[ARG:%.*]], <2 x float>* [[TMP5]], align 8
+; CHECK-NEXT:    call void @snork(%struct.bar* nonnull [[TMP]], %struct.pluto* nonnull [[TMPCAST]])
+; CHECK-NEXT:    ret void
+bb:
+  %tmp1 = alloca %struct.pluto, align 4
+  %tmp = alloca %struct.bar, align 8
+  %tmp2 = bitcast %struct.pluto* %tmp1 to i8*
+  %tmp3 = getelementptr inbounds %struct.pluto, %struct.pluto* %tmp1, i32 0, i32 0
+  %tmp4 = bitcast [4 x float]* %tmp3 to { <2 x float>, <2 x float> }*
+  %tmp5 = getelementptr inbounds { <2 x float>, <2 x float> }, { <2 x float>, <2 x float> }* %tmp4, i32 0, i32 0
+  store <2 x float> %arg, <2 x float>* %tmp5, align 4
+  call void @snork(%struct.bar* %tmp, %struct.pluto* %tmp1)
+  ret void
+}
+
+declare { <2 x float>, <2 x float> } @zot(<2 x float>)
+
+declare void @snork(%struct.bar* sret, %struct.pluto*)
Index: llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -151,6 +151,14 @@
   if (!AI.hasOneUse()) {
     // New is the allocation instruction, pointer typed. AI is the original
     // allocation instruction, also pointer typed. Thus, cast to use is BitCast.
+
+    // Scan to the end of the allocation instructions, to avoid inserting
+    // in the middle of a block of allocas.
+    BasicBlock::iterator It(New);
+    while (isa<AllocaInst>(*It))
+      ++It;
+    Builder.SetInsertPoint(&*It);
+
     Value *NewCast = Builder.CreateBitCast(New, AI.getType(), "tmpcast");
     replaceInstUsesWith(AI, NewCast);
     eraseInstFromFunction(AI);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79956.264054.patch
Type: text/x-patch
Size: 2621 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200514/6adb3f68/attachment.bin>


More information about the llvm-commits mailing list