Skip to content

Commit 16ec534

Browse files
authored
[ValueTracking] Handle and/or of conditions in computeKnownFPClassFromContext (#118257)
Fix a typo introduced by #83161. This patch also supports decomposition of and/or expressions in `computeKnownFPClassFromContext`. Compile-time improvement: http://llvm-compile-time-tracker.com/compare.php?from=688bb432c4b618de69a1d0e7807077a22f15762a&to=07493fc354b686f0aca79d6f817091a757bd7cd5&stat=instructions:u
1 parent 0611fdd commit 16ec534

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4882,9 +4882,19 @@ llvm::fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS,
48824882
}
48834883

48844884
static void computeKnownFPClassFromCond(const Value *V, Value *Cond,
4885-
bool CondIsTrue,
4885+
unsigned Depth, bool CondIsTrue,
48864886
const Instruction *CxtI,
48874887
KnownFPClass &KnownFromContext) {
4888+
Value *A, *B;
4889+
if (Depth < MaxAnalysisRecursionDepth &&
4890+
(CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
4891+
: match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
4892+
computeKnownFPClassFromCond(V, A, Depth + 1, CondIsTrue, CxtI,
4893+
KnownFromContext);
4894+
computeKnownFPClassFromCond(V, B, Depth + 1, CondIsTrue, CxtI,
4895+
KnownFromContext);
4896+
return;
4897+
}
48884898
CmpInst::Predicate Pred;
48894899
Value *LHS;
48904900
uint64_t ClassVal = 0;
@@ -4925,13 +4935,13 @@ static KnownFPClass computeKnownFPClassFromContext(const Value *V,
49254935

49264936
BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
49274937
if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4928-
computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CxtI,
4929-
KnownFromContext);
4938+
computeKnownFPClassFromCond(V, Cond, /*Depth=*/0, /*CondIsTrue=*/true,
4939+
Q.CxtI, KnownFromContext);
49304940

49314941
BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
49324942
if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4933-
computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CxtI,
4934-
KnownFromContext);
4943+
computeKnownFPClassFromCond(V, Cond, /*Depth=*/0, /*CondIsTrue=*/false,
4944+
Q.CxtI, KnownFromContext);
49354945
}
49364946
}
49374947

@@ -4953,8 +4963,8 @@ static KnownFPClass computeKnownFPClassFromContext(const Value *V,
49534963
if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
49544964
continue;
49554965

4956-
computeKnownFPClassFromCond(V, I->getArgOperand(0), /*CondIsTrue=*/true,
4957-
Q.CxtI, KnownFromContext);
4966+
computeKnownFPClassFromCond(V, I->getArgOperand(0), /*Depth=*/0,
4967+
/*CondIsTrue=*/true, Q.CxtI, KnownFromContext);
49584968
}
49594969

49604970
return KnownFromContext;
@@ -10117,7 +10127,7 @@ void llvm::findValuesAffectedByCondition(
1011710127

1011810128
if (HasRHSC && match(A, m_Intrinsic<Intrinsic::ctpop>(m_Value(X))))
1011910129
AddAffected(X);
10120-
} else if (match(Cond, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
10130+
} else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
1012110131
AddCmpOperands(A, B);
1012210132

1012310133
// fcmp fneg(x), y

llvm/test/Transforms/InstCombine/fpclass-from-dom-cond.ll

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,31 @@ if.end:
4646
ret i1 %cmp.i
4747
}
4848

49+
define i1 @test2_or(double %x, i1 %cond) {
50+
; CHECK-LABEL: define i1 @test2_or(
51+
; CHECK-SAME: double [[X:%.*]], i1 [[COND:%.*]]) {
52+
; CHECK-NEXT: entry:
53+
; CHECK-NEXT: [[CMP:%.*]] = fcmp olt double [[X]], 0x3EB0C6F7A0000000
54+
; CHECK-NEXT: [[OR:%.*]] = or i1 [[CMP]], [[COND]]
55+
; CHECK-NEXT: br i1 [[OR]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
56+
; CHECK: if.then:
57+
; CHECK-NEXT: ret i1 false
58+
; CHECK: if.end:
59+
; CHECK-NEXT: ret i1 false
60+
;
61+
entry:
62+
%cmp = fcmp olt double %x, 0x3EB0C6F7A0000000
63+
%or = or i1 %cmp, %cond
64+
br i1 %or, label %if.then, label %if.end
65+
66+
if.then:
67+
ret i1 false
68+
69+
if.end:
70+
%cmp.i = fcmp oeq double %x, 0.000000e+00
71+
ret i1 %cmp.i
72+
}
73+
4974
define i1 @test3(float %x) {
5075
; CHECK-LABEL: define i1 @test3(
5176
; CHECK-SAME: float [[X:%.*]]) {
@@ -240,16 +265,14 @@ if.else:
240265
ret i1 false
241266
}
242267

243-
; TODO: handle and/or conditions
244268
define i1 @test11_and(float %x, i1 %cond2) {
245269
; CHECK-LABEL: define i1 @test11_and(
246270
; CHECK-SAME: float [[X:%.*]], i1 [[COND2:%.*]]) {
247271
; CHECK-NEXT: [[COND:%.*]] = fcmp olt float [[X]], -1.000000e+00
248272
; CHECK-NEXT: [[AND:%.*]] = and i1 [[COND]], [[COND2]]
249273
; CHECK-NEXT: br i1 [[AND]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
250274
; CHECK: if.then:
251-
; CHECK-NEXT: [[RET1:%.*]] = fcmp oeq float [[X]], 0x7FF0000000000000
252-
; CHECK-NEXT: ret i1 [[RET1]]
275+
; CHECK-NEXT: ret i1 false
253276
; CHECK: if.else:
254277
; CHECK-NEXT: ret i1 false
255278
;
@@ -264,7 +287,6 @@ if.else:
264287
ret i1 false
265288
}
266289

267-
; TODO: handle and/or conditions
268290
define i1 @test12_or(float %x, i1 %cond2) {
269291
; CHECK-LABEL: define i1 @test12_or(
270292
; CHECK-SAME: float [[X:%.*]], i1 [[COND2:%.*]]) {
@@ -275,7 +297,7 @@ define i1 @test12_or(float %x, i1 %cond2) {
275297
; CHECK: if.then:
276298
; CHECK-NEXT: ret i1 false
277299
; CHECK: if.else:
278-
; CHECK-NEXT: [[RET:%.*]] = call i1 @llvm.is.fpclass.f32(float [[X]], i32 783)
300+
; CHECK-NEXT: [[RET:%.*]] = call i1 @llvm.is.fpclass.f32(float [[X]], i32 780)
279301
; CHECK-NEXT: ret i1 [[RET]]
280302
;
281303
entry:

0 commit comments

Comments
 (0)