[PATCH] D138027: Clean up CFG on JumpThreading
Pedro Olsen Ferreira via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 15 05:11:13 PST 2022
arkangath created this revision.
arkangath added reviewers: StephenTozer, brzycki.
Herald added a subscriber: hiraditya.
Herald added a project: All.
arkangath requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
In some loop situations, when the loop blocks become unreachable, JumpThreading would leave dead blocks behind. It could also generate self-referencing instructions (on a dead cfg).
This patch cleans up those blocks.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D138027
Files:
llvm/lib/Transforms/Scalar/JumpThreading.cpp
llvm/test/Transforms/JumpThreading/removed-use.ll
llvm/test/Transforms/JumpThreading/self-block.ll
llvm/test/Transforms/JumpThreading/unreachable-loops.ll
Index: llvm/test/Transforms/JumpThreading/unreachable-loops.ll
===================================================================
--- llvm/test/Transforms/JumpThreading/unreachable-loops.ll
+++ llvm/test/Transforms/JumpThreading/unreachable-loops.ll
@@ -100,10 +100,7 @@
; CHECK-LABEL: @PR48362(
; CHECK-NEXT: cleanup.cont1500:
; CHECK-NEXT: unreachable
-; CHECK: if.end1733:
-; CHECK-NEXT: [[I82:%.*]] = load i32, i32* undef, align 1
-; CHECK-NEXT: [[TOBOOL1731_NOT:%.*]] = icmp eq i32 [[I82]], 0
-; CHECK-NEXT: br label [[IF_END1733:%.*]]
+; CHECK-NOT: if.end1733:
;
cleanup1491: ; preds = %for.body1140
switch i32 0, label %cleanup2343.loopexit4 [
Index: llvm/test/Transforms/JumpThreading/self-block.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/JumpThreading/self-block.ll
@@ -0,0 +1,25 @@
+; RUN: opt -S -jump-threading %s -o - | FileCheck %s
+
+; Verifies that a self-referencing BasicBlock gets culled.
+; Note: SimplifyCFG would also clean this up but the "add"
+; instruction would become self-referencing when the phi
+; is simplified.
+define void @test() {
+; CHECK-LABEL: @test
+; CHECK: bb397:
+; CHECK-NEXT: ret void
+; CHECK-NOT: bb243:
+entry:
+ br i1 true, label %bb397, label %bb215
+
+bb215: ; preds = %entry
+ br label %bb243
+
+bb243: ; preds = %bb243, %bb215
+ %_4792_ = phi i32 [ %_3125_, %bb243 ], [ 1, %bb215 ]
+ %_3125_ = add i32 %_4792_, 1
+ br label %bb243
+
+bb397: ; preds = %entry
+ ret void
+}
Index: llvm/test/Transforms/JumpThreading/removed-use.ll
===================================================================
--- llvm/test/Transforms/JumpThreading/removed-use.ll
+++ llvm/test/Transforms/JumpThreading/removed-use.ll
@@ -2,8 +2,7 @@
; CHECK-LABEL: @foo
; CHECK: bb6:
; CHECK-NEXT: ret void
-; CHECK: bb3:
-; CHECK: br label %bb3
+; CHECK-NOT: bb3:
define void @foo() {
entry:
br i1 true, label %bb6, label %bb3
Index: llvm/lib/Transforms/Scalar/JumpThreading.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/JumpThreading.cpp
+++ llvm/lib/Transforms/Scalar/JumpThreading.cpp
@@ -449,6 +449,21 @@
<< '\n');
LoopHeaders.erase(&BB);
LVI->eraseBlock(&BB);
+ // If the removed block is a predecessor of a loop block
+ // (referencing itself), then there might be PHIs that will
+ // be simplifiable. Preserve those to prevent invalid IR.
+ DeleteDeadBlock(&BB, DTU, true);
+ Changed = true;
+ continue;
+ }
+
+ if (pred_size(&BB) == 1 && *pred_begin(&BB) == &BB) {
+ LoopHeaders.erase(&BB);
+ LVI->eraseBlock(&BB);
+ // Manually remove the terminator, otherwise the block
+ // deletion will see a pending reference to the Value being
+ // destroyed, which will trigger assertions.
+ BB.getTerminator()->eraseFromParent();
DeleteDeadBlock(&BB, DTU);
Changed = true;
continue;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138027.475432.patch
Type: text/x-patch
Size: 3213 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221115/bdc7eaee/attachment.bin>
More information about the llvm-commits
mailing list