[PATCH] D158609: [LICM][LoopSimplify] Create preheader for callbr (PR64215)

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 23 06:07:29 PDT 2023


nikic created this revision.
nikic added reviewers: nickdesaulniers, efriedma, fhahn.
Herald added subscribers: StephenFan, asbirlea, hiraditya.
Herald added a project: All.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

isLegalToHoistInto() currently return true for callbr instructions. That means that a callbr with one successor will be considered a proper loop preheader, which may result in instructions that use the callbr return value being hoisted past it.

Fix this by not considering callbr a preheader, and forcing LoopSimplify to create one.

Fixes https://github.com/llvm/llvm-project/issues/64215.


https://reviews.llvm.org/D158609

Files:
  llvm/lib/IR/BasicBlock.cpp
  llvm/test/Transforms/LICM/callbr-crash.ll


Index: llvm/test/Transforms/LICM/callbr-crash.ll
===================================================================
--- llvm/test/Transforms/LICM/callbr-crash.ll
+++ llvm/test/Transforms/LICM/callbr-crash.ll
@@ -35,3 +35,27 @@
   %phi = phi i32 [ %asmresult1.i.i, %cond.true.i ], [ undef, %for.cond ]
   ret i32 %phi
 }
+
+declare void @use_i32(i32)
+
+define void @pr64215() {
+; CHECK-LABEL: @pr64215(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[X:%.*]] = callbr i32 asm sideeffect "", "=r"()
+; CHECK-NEXT:    to label [[LOOP_PREHEADER:%.*]] []
+; CHECK:       loop.preheader:
+; CHECK-NEXT:    [[ADD:%.*]] = add i32 [[X]], 1
+; CHECK-NEXT:    br label [[LOOP:%.*]]
+; CHECK:       loop:
+; CHECK-NEXT:    call void @use_i32(i32 [[ADD]])
+; CHECK-NEXT:    br label [[LOOP]]
+;
+entry:
+  %x = callbr i32 asm sideeffect "", "=r"()
+  to label %loop []
+
+loop:
+  %add = add i32 %x, 1
+  call void @use_i32(i32 %add)
+  br label %loop
+}
Index: llvm/lib/IR/BasicBlock.cpp
===================================================================
--- llvm/lib/IR/BasicBlock.cpp
+++ llvm/lib/IR/BasicBlock.cpp
@@ -397,7 +397,8 @@
   assert(Term->getNumSuccessors() > 0);
 
   // Instructions should not be hoisted across exception handling boundaries.
-  return !Term->isExceptionalTerminator();
+  // Also don't hoist across callbr, which produces a value.
+  return !Term->isExceptionalTerminator() && !isa<CallBrInst>(Term);
 }
 
 bool BasicBlock::isEntryBlock() const {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158609.552685.patch
Type: text/x-patch
Size: 1468 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230823/72e0d438/attachment.bin>


More information about the llvm-commits mailing list