Skip to content

Commit 6350acd

Browse files
dtcxzywtstellar
authored andcommitted
[CGP] Drop poison-generating flags after hoisting (#90382)
See the following case: ``` define i8 @SRC1(i8 %x) { entry: %cmp = icmp eq i8 %x, -1 br i1 %cmp, label %exit, label %if.then if.then: %inc = add nuw nsw i8 %x, 1 br label %exit exit: %retval = phi i8 [ %inc, %if.then ], [ -1, %entry ] ret i8 %retval } define i8 @tgt1(i8 %x) { entry: %inc = add nuw nsw i8 %x, 1 %0 = icmp eq i8 %inc, 0 br i1 %0, label %exit, label %if.then if.then: ; preds = %entry br label %exit exit: ; preds = %if.then, %entry %retval = phi i8 [ %inc, %if.then ], [ -1, %entry ] ret i8 %retval } ``` `optimizeBranch` converts `icmp eq X, -1` into cmp to zero on RISC-V and hoists the add into the entry block. Poison-generating flags should be dropped as they don't still hold. Proof: https://alive2.llvm.org/ce/z/sP7mvK Fixes #90380 (cherry picked from commit ab12bba)
1 parent f341c76 commit 6350acd

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

llvm/lib/CodeGen/CodeGenPrepare.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -8154,6 +8154,7 @@ static bool optimizeBranch(BranchInst *Branch, const TargetLowering &TLI,
81548154
IRBuilder<> Builder(Branch);
81558155
if (UI->getParent() != Branch->getParent())
81568156
UI->moveBefore(Branch);
8157+
UI->dropPoisonGeneratingFlags();
81578158
Value *NewCmp = Builder.CreateCmp(ICmpInst::ICMP_EQ, UI,
81588159
ConstantInt::get(UI->getType(), 0));
81598160
LLVM_DEBUG(dbgs() << "Converting " << *Cmp << "\n");
@@ -8167,6 +8168,7 @@ static bool optimizeBranch(BranchInst *Branch, const TargetLowering &TLI,
81678168
IRBuilder<> Builder(Branch);
81688169
if (UI->getParent() != Branch->getParent())
81698170
UI->moveBefore(Branch);
8171+
UI->dropPoisonGeneratingFlags();
81708172
Value *NewCmp = Builder.CreateCmp(Cmp->getPredicate(), UI,
81718173
ConstantInt::get(UI->getType(), 0));
81728174
LLVM_DEBUG(dbgs() << "Converting " << *Cmp << "\n");

llvm/test/Transforms/CodeGenPrepare/ARM/branch-on-zero.ll

+23
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,29 @@ else:
211211
ret i32 %l
212212
}
213213

214+
define i32 @sub10_else_drop_nuw(i32 %a) {
215+
; CHECK-LABEL: @sub10_else_drop_nuw(
216+
; CHECK-NEXT: entry:
217+
; CHECK-NEXT: [[L:%.*]] = sub i32 [[A:%.*]], 10
218+
; CHECK-NEXT: [[TMP0:%.*]] = icmp eq i32 [[L]], 0
219+
; CHECK-NEXT: br i1 [[TMP0]], label [[THEN:%.*]], label [[ELSE:%.*]]
220+
; CHECK: then:
221+
; CHECK-NEXT: ret i32 0
222+
; CHECK: else:
223+
; CHECK-NEXT: ret i32 [[L]]
224+
;
225+
entry:
226+
%c = icmp eq i32 %a, 10
227+
br i1 %c, label %then, label %else
228+
229+
then:
230+
ret i32 0
231+
232+
else:
233+
%l = sub nuw i32 %a, 10
234+
ret i32 %l
235+
}
236+
214237
define i32 @subm10_then(i32 %a) {
215238
; CHECK-LABEL: @subm10_then(
216239
; CHECK-NEXT: entry:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
2+
; RUN: opt -codegenprepare -S -mtriple=riscv64 < %s | FileCheck %s
3+
4+
define i8 @hoist_add(i8 %x) {
5+
; CHECK-LABEL: define i8 @hoist_add(
6+
; CHECK-SAME: i8 [[X:%.*]]) {
7+
; CHECK-NEXT: entry:
8+
; CHECK-NEXT: [[INC:%.*]] = add i8 [[X]], 1
9+
; CHECK-NEXT: [[TMP0:%.*]] = icmp eq i8 [[INC]], 0
10+
; CHECK-NEXT: br i1 [[TMP0]], label [[EXIT:%.*]], label [[IF_THEN:%.*]]
11+
; CHECK: if.then:
12+
; CHECK-NEXT: br label [[EXIT]]
13+
; CHECK: exit:
14+
; CHECK-NEXT: [[RETVAL:%.*]] = phi i8 [ [[INC]], [[IF_THEN]] ], [ -1, [[ENTRY:%.*]] ]
15+
; CHECK-NEXT: ret i8 [[RETVAL]]
16+
;
17+
entry:
18+
%cmp = icmp eq i8 %x, -1
19+
br i1 %cmp, label %exit, label %if.then
20+
21+
if.then:
22+
%inc = add nuw nsw i8 %x, 1
23+
br label %exit
24+
25+
exit:
26+
%retval = phi i8 [ %inc, %if.then ], [ -1, %entry ]
27+
ret i8 %retval
28+
}
29+
30+
define i8 @hoist_lshr(i8 %x) {
31+
; CHECK-LABEL: define i8 @hoist_lshr(
32+
; CHECK-SAME: i8 [[X:%.*]]) {
33+
; CHECK-NEXT: entry:
34+
; CHECK-NEXT: [[INC:%.*]] = lshr i8 [[X]], 3
35+
; CHECK-NEXT: [[TMP0:%.*]] = icmp eq i8 [[INC]], 0
36+
; CHECK-NEXT: br i1 [[TMP0]], label [[EXIT:%.*]], label [[IF_THEN:%.*]]
37+
; CHECK: if.then:
38+
; CHECK-NEXT: br label [[EXIT]]
39+
; CHECK: exit:
40+
; CHECK-NEXT: [[RETVAL:%.*]] = phi i8 [ [[INC]], [[IF_THEN]] ], [ -1, [[ENTRY:%.*]] ]
41+
; CHECK-NEXT: ret i8 [[RETVAL]]
42+
;
43+
entry:
44+
%cmp = icmp ult i8 %x, 8
45+
br i1 %cmp, label %exit, label %if.then
46+
47+
if.then:
48+
%inc = lshr exact i8 %x, 3
49+
br label %exit
50+
51+
exit:
52+
%retval = phi i8 [ %inc, %if.then ], [ -1, %entry ]
53+
ret i8 %retval
54+
}
55+
56+
define i8 @nomove_add(i8 %x) {
57+
; CHECK-LABEL: define i8 @nomove_add(
58+
; CHECK-SAME: i8 [[X:%.*]]) {
59+
; CHECK-NEXT: entry:
60+
; CHECK-NEXT: [[INC:%.*]] = add i8 [[X]], 1
61+
; CHECK-NEXT: [[TMP0:%.*]] = icmp eq i8 [[INC]], 0
62+
; CHECK-NEXT: br i1 [[TMP0]], label [[EXIT:%.*]], label [[IF_THEN:%.*]]
63+
; CHECK: if.then:
64+
; CHECK-NEXT: br label [[EXIT]]
65+
; CHECK: exit:
66+
; CHECK-NEXT: [[RETVAL:%.*]] = phi i8 [ [[INC]], [[IF_THEN]] ], [ -1, [[ENTRY:%.*]] ]
67+
; CHECK-NEXT: ret i8 [[RETVAL]]
68+
;
69+
entry:
70+
%inc = add nuw nsw i8 %x, 1
71+
%cmp = icmp eq i8 %x, -1
72+
br i1 %cmp, label %exit, label %if.then
73+
74+
if.then:
75+
br label %exit
76+
77+
exit:
78+
%retval = phi i8 [ %inc, %if.then ], [ -1, %entry ]
79+
ret i8 %retval
80+
}

0 commit comments

Comments
 (0)