Skip to content

Commit 370880c

Browse files
committed
[InstCombine] Fold icmp into phi beyond the same BB.
The icmp is being folded in phi only if they belong in the same BB. This patch extends the same beyond the BB. Have seen scenarios where this seems to be beneficial. Differential Revision: https://reviews.llvm.org/D157740
1 parent 26f7150 commit 370880c

File tree

4 files changed

+18
-29
lines changed

4 files changed

+18
-29
lines changed

compiler-rt/test/dfsan/conditional_callbacks.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ int main(int Argc, char *Argv[]) {
8080
if (DataI) {
8181
result = 42;
8282
}
83-
83+
84+
fprintf(stderr, "Result is %d\n", result);
8485
assert(dfsan_get_label(DataJ) == LabelJ);
8586

8687
// CHECK: Label 2 used as condition
@@ -96,11 +97,13 @@ int main(int Argc, char *Argv[]) {
9697
}
9798

9899
int tainted_cond = ((DataI * DataJ) != 1);
100+
fprintf(stderr, "Result is %d\n", result);
99101
assert(dfsan_get_label(tainted_cond) == LabelIJ);
100102

101103
// CHECK: Label 3 used as condition
102104
result = tainted_cond ? result + 420000 : 9;
103105

106+
fprintf(stderr, "Result is %d\n", result);
104107
assert(result == 424242);
105108
return 0;
106109
}

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3893,12 +3893,8 @@ Instruction *InstCombinerImpl::foldICmpInstWithConstantNotInt(ICmpInst &I) {
38933893
Constant::getNullValue(LHSI->getOperand(0)->getType()));
38943894
break;
38953895
case Instruction::PHI:
3896-
// Only fold icmp into the PHI if the phi and icmp are in the same
3897-
// block. If in the same block, we're encouraging jump threading. If
3898-
// not, we are just pessimizing the code by making an i1 phi.
3899-
if (LHSI->getParent() == I.getParent())
3900-
if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
3901-
return NV;
3896+
if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
3897+
return NV;
39023898
break;
39033899
case Instruction::IntToPtr:
39043900
// icmp pred inttoptr(X), null -> icmp pred X, 0
@@ -7576,12 +7572,8 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
75767572
if (match(Op0, m_Instruction(LHSI)) && match(Op1, m_Constant(RHSC))) {
75777573
switch (LHSI->getOpcode()) {
75787574
case Instruction::PHI:
7579-
// Only fold fcmp into the PHI if the phi and fcmp are in the same
7580-
// block. If in the same block, we're encouraging jump threading. If
7581-
// not, we are just pessimizing the code by making an i1 phi.
7582-
if (LHSI->getParent() == I.getParent())
7583-
if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
7584-
return NV;
7575+
if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
7576+
return NV;
75857577
break;
75867578
case Instruction::SIToFP:
75877579
case Instruction::UIToFP:

llvm/test/Transforms/InstCombine/icmp-constant-phi.ll

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,9 @@ define i1 @test_ne_float(i1 %cond) {
199199
; CHECK: if.false:
200200
; CHECK-NEXT: br label [[MERGE]]
201201
; CHECK: merge:
202-
; CHECK-NEXT: [[PHI:%.*]] = phi float [ 1.000000e+00, [[IF_TRUE]] ], [ 1.250000e+00, [[IF_FALSE]] ]
203202
; CHECK-NEXT: br label [[EXIT:%.*]]
204203
; CHECK: exit:
205-
; CHECK-NEXT: [[COMPARE:%.*]] = fcmp one float [[PHI]], 1.250000e+00
206-
; CHECK-NEXT: ret i1 [[COMPARE]]
204+
; CHECK-NEXT: ret i1 [[COND]]
207205
;
208206
entry:
209207
br i1 %cond, label %if.true, label %if.false
@@ -263,11 +261,10 @@ define <2 x i1> @test_ne_float_vector(i1 %cond) {
263261
; CHECK: if.false:
264262
; CHECK-NEXT: br label [[MERGE]]
265263
; CHECK: merge:
266-
; CHECK-NEXT: [[PHI:%.*]] = phi <2 x float> [ <float 1.232500e+02, float 1.232500e+02>, [[IF_TRUE]] ], [ <float 4.562500e+02, float 4.562500e+02>, [[IF_FALSE]] ]
264+
; CHECK-NEXT: [[PHI:%.*]] = phi <2 x i1> [ <i1 false, i1 true>, [[IF_TRUE]] ], [ <i1 true, i1 false>, [[IF_FALSE]] ]
267265
; CHECK-NEXT: br label [[EXIT:%.*]]
268266
; CHECK: exit:
269-
; CHECK-NEXT: [[COMPARE:%.*]] = fcmp one <2 x float> [[PHI]], <float 1.232500e+02, float 4.562500e+02>
270-
; CHECK-NEXT: ret <2 x i1> [[COMPARE]]
267+
; CHECK-NEXT: ret <2 x i1> [[PHI]]
271268
;
272269
entry:
273270
br i1 %cond, label %if.true, label %if.false

llvm/test/Transforms/InstCombine/phi.ll

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2304,27 +2304,24 @@ define i1 @icmp_fold_into_phi_beyondBB(ptr noundef %val1, ptr noundef readonly %
23042304
; CHECK-NEXT: [[INCDEC_PTR_I]] = getelementptr inbounds i8, ptr [[TEST_0_I]], i64 1
23052305
; CHECK-NEXT: br i1 [[CMP1_NOT_I]], label [[WHILE_END_I:%.*]], label [[WHILE_COND_I]]
23062306
; CHECK: while.end.i:
2307-
; CHECK-NEXT: [[SUB_PTR_LHS_CAST_I:%.*]] = ptrtoint ptr [[TEST_0_I]] to i64
2308-
; CHECK-NEXT: [[SUB_PTR_RHS_CAST_I:%.*]] = ptrtoint ptr [[VAL1]] to i64
2309-
; CHECK-NEXT: [[SUB_PTR_SUB_I:%.*]] = sub i64 [[SUB_PTR_LHS_CAST_I]], [[SUB_PTR_RHS_CAST_I]]
2307+
; CHECK-NEXT: [[TMP1:%.*]] = icmp ne ptr [[TEST_0_I]], [[VAL1]]
23102308
; CHECK-NEXT: br label [[_Z3FOOPKC_EXIT]]
23112309
; CHECK: _Z3fooPKc.exit:
2312-
; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i64 [ [[SUB_PTR_SUB_I]], [[WHILE_END_I]] ], [ 0, [[ENTRY]] ]
2310+
; CHECK-NEXT: [[RETVAL_0_I:%.*]] = phi i1 [ [[TMP1]], [[WHILE_END_I]] ], [ false, [[ENTRY]] ]
23132311
; CHECK-NEXT: [[CMP_I10:%.*]] = icmp eq ptr [[VAL2:%.*]], null
23142312
; CHECK-NEXT: br i1 [[CMP_I10]], label [[_Z3FOOPKC_EXIT20:%.*]], label [[WHILE_COND_I11:%.*]]
23152313
; CHECK: while.cond.i11:
23162314
; CHECK-NEXT: [[TEST_0_I12:%.*]] = phi ptr [ [[INCDEC_PTR_I14:%.*]], [[WHILE_COND_I11]] ], [ [[VAL2]], [[_Z3FOOPKC_EXIT]] ]
2317-
; CHECK-NEXT: [[TMP1:%.*]] = load i8, ptr [[TEST_0_I12]], align 1
2318-
; CHECK-NEXT: [[CMP1_NOT_I13:%.*]] = icmp eq i8 [[TMP1]], 0
2315+
; CHECK-NEXT: [[TMP2:%.*]] = load i8, ptr [[TEST_0_I12]], align 1
2316+
; CHECK-NEXT: [[CMP1_NOT_I13:%.*]] = icmp eq i8 [[TMP2]], 0
23192317
; CHECK-NEXT: [[INCDEC_PTR_I14]] = getelementptr inbounds i8, ptr [[TEST_0_I12]], i64 1
23202318
; CHECK-NEXT: br i1 [[CMP1_NOT_I13]], label [[WHILE_END_I15:%.*]], label [[WHILE_COND_I11]]
23212319
; CHECK: while.end.i15:
2322-
; CHECK-NEXT: [[TMP2:%.*]] = icmp ne ptr [[TEST_0_I12]], [[VAL2]]
2320+
; CHECK-NEXT: [[TMP3:%.*]] = icmp ne ptr [[TEST_0_I12]], [[VAL2]]
23232321
; CHECK-NEXT: br label [[_Z3FOOPKC_EXIT20]]
23242322
; CHECK: _Z3fooPKc.exit20:
2325-
; CHECK-NEXT: [[RETVAL_0_I19:%.*]] = phi i1 [ [[TMP2]], [[WHILE_END_I15]] ], [ false, [[_Z3FOOPKC_EXIT]] ]
2326-
; CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne i64 [[RETVAL_0_I]], 0
2327-
; CHECK-NEXT: [[AND9:%.*]] = and i1 [[TOBOOL]], [[RETVAL_0_I19]]
2323+
; CHECK-NEXT: [[RETVAL_0_I19:%.*]] = phi i1 [ [[TMP3]], [[WHILE_END_I15]] ], [ false, [[_Z3FOOPKC_EXIT]] ]
2324+
; CHECK-NEXT: [[AND9:%.*]] = and i1 [[RETVAL_0_I]], [[RETVAL_0_I19]]
23282325
; CHECK-NEXT: ret i1 [[AND9]]
23292326
;
23302327
entry:

0 commit comments

Comments
 (0)