Skip to content

Commit adbe9e2

Browse files
authored
llvm-reduce: Skip setting br conditions on already constant branches (#133841)
If we are trying to simplify branch conditions to true, ignore branches already set to a constant true. If we are simplifying to constant false, ignore the already constant false cases. This saves steps in this edge case, and avoids the side effect of running simplifycfg on blocks we did not intend to modify.
1 parent 0cfabd3 commit adbe9e2

File tree

2 files changed

+146
-6
lines changed

2 files changed

+146
-6
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=simplify-conditionals-false --test FileCheck --test-arg --check-prefixes=INTERESTING --test-arg %s --test-arg --input-file %s -o %t
2+
; RUN: FileCheck -check-prefixes=RESULT-FALSE,CHECK %s < %t
3+
4+
; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=simplify-conditionals-true --test FileCheck --test-arg --check-prefixes=INTERESTING --test-arg %s --test-arg --input-file %s -o %t
5+
; RUN: FileCheck -check-prefixes=RESULT-TRUE,CHECK %s < %t
6+
7+
; Check that simplify-conditionals-true/false do not attempt block
8+
; simplification in cases that happened to already use a constant
9+
; true/false branch. We should not get the side effect of running
10+
; simplifycfg on blocks where we did not change the terminator value,
11+
; and not introduce unreachable code.
12+
13+
14+
; CHECK-LABEL: @br_false(
15+
; RESULT-FALSE: br i1 false, label %will_be_unreachable, label %exit
16+
; RESULT-TRUE: br label %will_be_unreachable
17+
define i1 @br_false(i64 %int) {
18+
entry:
19+
%i2p = inttoptr i64 %int to ptr
20+
br i1 false, label %will_be_unreachable, label %exit
21+
22+
will_be_unreachable:
23+
%load = load ptr, ptr %i2p, align 8
24+
br label %for.body
25+
26+
for.body:
27+
br label %for.body
28+
29+
exit:
30+
ret i1 false
31+
}
32+
33+
; CHECK-LABEL: @br_false_keep_in_unreachable(
34+
; CHECK: entry
35+
; INTERESTING: [[I2P:%.+]] = inttoptr i64 %int to ptr
36+
; INTERESTING: load ptr, ptr [[I2P]]
37+
38+
; RESULT-FALSE: br i1 false, label %will_be_unreachable, label %exit
39+
; RESULT-TRUE: br label %will_be_unreachable
40+
define i1 @br_false_keep_in_unreachable(i64 %int) {
41+
entry:
42+
br i1 false, label %will_be_unreachable, label %exit
43+
44+
will_be_unreachable:
45+
%i2p = inttoptr i64 %int to ptr
46+
%load = load ptr, ptr %i2p, align 8
47+
br label %for.body
48+
49+
for.body:
50+
br label %for.body
51+
52+
exit:
53+
ret i1 false
54+
}
55+
56+
; CHECK-LABEL: @br_true(
57+
58+
; RESULT-FALSE: br label %will_be_unreachable
59+
; RESULT-TRUE: br i1 true, label %exit, label %will_be_unreachable
60+
define i1 @br_true(i64 %int) {
61+
entry:
62+
%i2p = inttoptr i64 %int to ptr
63+
br i1 true, label %exit, label %will_be_unreachable
64+
65+
will_be_unreachable:
66+
%load = load ptr, ptr %i2p, align 8
67+
br label %for.body
68+
69+
for.body:
70+
br label %for.body
71+
72+
exit:
73+
ret i1 false
74+
}
75+
76+
; CHECK-LABEL: @br_true_keep_in_unreachable(
77+
; CHECK: entry:
78+
; INTERESTING: [[I2P:%.+]] = inttoptr i64 %int to ptr
79+
; INTERESTING: load ptr, ptr [[I2P]]
80+
81+
; RESULT-FALSE: br label %will_be_unreachable
82+
; RESULT-TRUE: br i1 true, label %exit, label %will_be_unreachable
83+
define i1 @br_true_keep_in_unreachable(i64 %int) {
84+
entry:
85+
%i2p = inttoptr i64 %int to ptr
86+
br i1 true, label %exit, label %will_be_unreachable
87+
88+
will_be_unreachable:
89+
%load = load ptr, ptr %i2p, align 8
90+
br label %for.body
91+
92+
for.body:
93+
br label %for.body
94+
95+
exit:
96+
ret i1 false
97+
}
98+
99+
; CHECK-LABEL: @br_poison(
100+
; RESULT-FALSE: br label %will_be_unreachable
101+
; RESULT-TRUE: br label %exit
102+
define i1 @br_poison(i64 %int) {
103+
entry:
104+
%i2p = inttoptr i64 %int to ptr
105+
br i1 poison, label %exit, label %will_be_unreachable
106+
107+
will_be_unreachable:
108+
%load = load ptr, ptr %i2p, align 8
109+
br label %for.body
110+
111+
for.body:
112+
br label %for.body
113+
114+
exit:
115+
ret i1 false
116+
}
117+
118+
; CHECK-LABEL: @br_poison_keep_in_unreachable(
119+
; CHECK: entry:
120+
; INTERESTING: [[I2P:%.+]] = inttoptr i64 %int to ptr
121+
; INTERESTING: load ptr, ptr [[I2P]]
122+
123+
; RESULT-FALSE: br label %will_be_unreachable
124+
; RESULT-TRUE: br label %exit
125+
define i1 @br_poison_keep_in_unreachable(i64 %int) {
126+
entry:
127+
%i2p = inttoptr i64 %int to ptr
128+
br i1 poison, label %exit, label %will_be_unreachable
129+
130+
will_be_unreachable:
131+
%load = load ptr, ptr %i2p, align 8
132+
br label %for.body
133+
134+
for.body:
135+
br label %for.body
136+
137+
exit:
138+
ret i1 false
139+
}

llvm/tools/llvm-reduce/deltas/ReduceUsingSimplifyCFG.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,18 @@ static void reduceConditionals(Oracle &O, ReducerWorkItem &WorkItem,
3737
Module &M = WorkItem.getModule();
3838
SmallVector<BasicBlock *, 16> ToSimplify;
3939

40+
LLVMContext &Ctx = M.getContext();
41+
ConstantInt *ConstValToSet =
42+
Direction ? ConstantInt::getTrue(Ctx) : ConstantInt::getFalse(Ctx);
43+
4044
for (auto &F : M) {
4145
for (auto &BB : F) {
4246
auto *BR = dyn_cast<BranchInst>(BB.getTerminator());
43-
if (!BR || !BR->isConditional() || O.shouldKeep())
47+
if (!BR || !BR->isConditional() || BR->getCondition() == ConstValToSet ||
48+
O.shouldKeep())
4449
continue;
4550

46-
if (Direction)
47-
BR->setCondition(ConstantInt::getTrue(BR->getContext()));
48-
else
49-
BR->setCondition(ConstantInt::getFalse(BR->getContext()));
50-
51+
BR->setCondition(ConstValToSet);
5152
ToSimplify.push_back(&BB);
5253
}
5354
}

0 commit comments

Comments
 (0)