Skip to content

Commit cc2214c

Browse files
committed
Bail out jump threading on indirect branches
The bug was introduced by #68473
1 parent 0160d81 commit cc2214c

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,8 @@ CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ,
868868

869869
LLVM_DEBUG(dbgs() << "Looking to fold " << BB->getName() << " into "
870870
<< Succ->getName() << "\n");
871+
if (isa<IndirectBrInst>(Succ->getTerminator()))
872+
return false;
871873
// Shortcut, if there is only a single predecessor it must be BB and merging
872874
// is always safe
873875
if (Succ->getSinglePredecessor())
@@ -1029,11 +1031,12 @@ CanRedirectPredsOfEmptyBBToSucc(BasicBlock *BB, BasicBlock *Succ,
10291031
return false;
10301032

10311033
// Get single common predecessors of both BB and Succ
1034+
// TODO: Replace this with a proper `set intersect` algorithm
10321035
for (BasicBlock *SuccPred : SuccPreds) {
10331036
if (BBPreds.count(SuccPred)) {
1037+
CommonPred = SuccPred;
10341038
if (CommonPred)
10351039
return false;
1036-
CommonPred = SuccPred;
10371040
}
10381041
}
10391042

@@ -1133,7 +1136,7 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
11331136

11341137
bool BBKillable = CanPropagatePredecessorsForPHIs(BB, Succ, BBPreds);
11351138

1136-
// Even if we can not fold bB into Succ, we may be able to redirect the
1139+
// Even if we can not fold BB into Succ, we may be able to redirect the
11371140
// predecessors of BB to Succ.
11381141
bool BBPhisMergeable =
11391142
BBKillable ||
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
; RUN: opt < %s -passes=simplifycfg -S | FileCheck %s
2+
3+
; CHECK: switch i32 %state.0, label %sw.epilog
4+
; CHECK-NEXT: i32 0, label %sw.bb
5+
; CHECK-NEXT: i32 1, label %VM__OP_1
6+
; CHECK-NEXT: i32 2, label %sw.bb4
7+
8+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
9+
target triple = "x86_64-unknown-linux-gnu"
10+
11+
@.str = private unnamed_addr constant [23 x i8] c"OP_1:(instruction=%d)\0A\00", align 1
12+
@.str.1 = private unnamed_addr constant [28 x i8] c"TERMINATE:(instruction=%d)\0A\00", align 1
13+
14+
; Function Attrs: mustprogress norecurse uwtable
15+
define dso_local noundef i32 @main() #0 {
16+
entry:
17+
%bytecode = alloca [2 x ptr], align 16
18+
store ptr blockaddress(@main, %VM__OP_1), ptr %bytecode, align 16, !tbaa !5
19+
%arrayidx1 = getelementptr inbounds [2 x ptr], ptr %bytecode, i64 0, i64 1
20+
store ptr blockaddress(@main, %VM__TERMINATE), ptr %arrayidx1, align 8, !tbaa !5
21+
br label %while.body
22+
23+
while.body: ; preds = %entry, %sw.epilog
24+
%state.0 = phi i32 [ 0, %entry ], [ %state.1, %sw.epilog ]
25+
%index.0 = phi i32 [ 0, %entry ], [ %index.2, %sw.epilog ]
26+
switch i32 %state.0, label %sw.epilog [
27+
i32 0, label %sw.bb
28+
i32 1, label %VM__OP_1
29+
i32 2, label %sw.bb4
30+
]
31+
32+
sw.bb: ; preds = %while.body
33+
br label %indirectgoto
34+
35+
VM__OP_1: ; preds = %while.body, %indirectgoto
36+
%index.1 = phi i32 [ %index.3, %indirectgoto ], [ %index.0, %while.body ]
37+
br label %sw.epilog
38+
39+
sw.bb4: ; preds = %while.body
40+
%call = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %index.0)
41+
%inc = add nsw i32 %index.0, 1
42+
br label %indirectgoto
43+
44+
sw.epilog: ; preds = %while.body, %VM__OP_1
45+
%state.1 = phi i32 [ %state.0, %while.body ], [ 2, %VM__OP_1 ]
46+
%index.2 = phi i32 [ %index.0, %while.body ], [ %index.1, %VM__OP_1 ]
47+
br label %while.body, !llvm.loop !9
48+
49+
VM__TERMINATE: ; preds = %indirectgoto
50+
%call7 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %index.3)
51+
ret i32 0
52+
53+
indirectgoto: ; preds = %sw.bb4, %sw.bb
54+
%index.3 = phi i32 [ %inc, %sw.bb4 ], [ %index.0, %sw.bb ]
55+
%idxprom.pn = sext i32 %index.3 to i64
56+
%indirect.goto.dest.in = getelementptr inbounds [2 x ptr], ptr %bytecode, i64 0, i64 %idxprom.pn
57+
%indirect.goto.dest = load ptr, ptr %indirect.goto.dest.in, align 8, !tbaa !5
58+
indirectbr ptr %indirect.goto.dest, [label %VM__OP_1, label %VM__TERMINATE]
59+
}
60+
declare i32 @printf(ptr noundef, ...) #1
61+
62+
attributes #0 = { mustprogress norecurse uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
63+
attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
64+
65+
!llvm.module.flags = !{!0, !1, !2, !3}
66+
!llvm.ident = !{!4}
67+
68+
!0 = !{i32 1, !"wchar_size", i32 4}
69+
!1 = !{i32 8, !"PIC Level", i32 2}
70+
!2 = !{i32 7, !"PIE Level", i32 2}
71+
!3 = !{i32 7, !"uwtable", i32 2}
72+
!4 = !{!"clang version 18.0.0git (https://github.com/llvm/llvm-project.git 67782d2de5ea9c8653b8f0110237a3c355291c0e)"}
73+
!5 = !{!6, !6, i64 0}
74+
!6 = !{!"any pointer", !7, i64 0}
75+
!7 = !{!"omnipotent char", !8, i64 0}
76+
!8 = !{!"Simple C++ TBAA"}
77+
!9 = !{!10, !10, i64 0}
78+
!10 = !{!"int", !7, i64 0}
79+
!11 = distinct !{!11, !12, !13}
80+
!12 = !{!"llvm.loop.mustprogress"}
81+
!13 = !{!"llvm.loop.unroll.disable"}
82+

0 commit comments

Comments
 (0)