Skip to content

Commit d95e976

Browse files
committed
[Analysis] Unify most of the tracking between AssumptionCache and DomConditionCache
This helps cover some missing cases in both and hopefully serves as creating an easier framework for extending general condition based analysis.
1 parent 28e6568 commit d95e976

File tree

3 files changed

+77
-59
lines changed

3 files changed

+77
-59
lines changed

llvm/include/llvm/Analysis/ConditionCacheUtil.h

Lines changed: 74 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1+
//===- llvm/Analysis/ConditionCacheUtil.h -----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Shared by DomConditionCache and AssumptionCache. Holds common operation of
10+
// finding values potentially affected by an assumed/branched on condition.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
114
#ifndef LLVM_ANALYSIS_CONDITIONCACHEUTIL_H
215
#define LLVM_ANALYSIS_CONDITIONCACHEUTIL_H
316

17+
#include "llvm/ADT/SmallVector.h"
418
#include "llvm/IR/PatternMatch.h"
519
#include <functional>
620

@@ -33,6 +47,14 @@ findValuesAffectedByCondition(Value *Cond, bool IsAssume,
3347
addValueAffectedByCondition(V, InsertAffected);
3448
};
3549

50+
auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
51+
if (IsAssume) {
52+
AddAffected(LHS);
53+
AddAffected(RHS);
54+
} else if (match(RHS, m_Constant()))
55+
AddAffected(LHS);
56+
};
57+
3658
SmallVector<Value *, 8> Worklist;
3759
SmallPtrSet<Value *, 8> Visited;
3860
Worklist.push_back(Cond);
@@ -44,65 +66,62 @@ findValuesAffectedByCondition(Value *Cond, bool IsAssume,
4466
CmpInst::Predicate Pred;
4567
Value *A, *B, *X;
4668

47-
if (IsAssume)
69+
if (IsAssume) {
4870
AddAffected(V);
71+
if (match(V, m_Not(m_Value(X))))
72+
AddAffected(X);
73+
}
4974

50-
if (IsAssume && match(V, m_Not(m_Value(X))))
51-
AddAffected(X);
52-
if (!IsAssume && match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
53-
Worklist.push_back(A);
54-
Worklist.push_back(B);
55-
} else if (match(V, m_Cmp(Pred, m_Value(A), m_Value(B))) &&
56-
(IsAssume || isa<ICmpInst>(V))) {
57-
if (IsAssume || match(B, m_Constant())) {
58-
AddAffected(A);
59-
if (IsAssume)
60-
AddAffected(B);
61-
62-
if (IsAssume ? (Pred == ICmpInst::ICMP_EQ)
63-
: ICmpInst::isEquality(Pred)) {
64-
if (match(B, m_ConstantInt())) {
65-
// (X & C) or (X | C) or (X ^ C).
66-
// (X << C) or (X >>_s C) or (X >>_u C).
67-
if (match(A, m_BitwiseLogic(m_Value(X), m_ConstantInt())) ||
68-
match(A, m_Shift(m_Value(X), m_ConstantInt())))
69-
AddAffected(X);
70-
}
71-
} else {
72-
if (Pred == ICmpInst::ICMP_NE)
73-
if (match(A, m_And(m_Value(X), m_Power2())) && match(B, m_Zero()))
74-
AddAffected(X);
75-
76-
if (!IsAssume || Pred == ICmpInst::ICMP_ULT) {
77-
// Handle (A + C1) u< C2, which is the canonical form of
78-
// A > C3 && A < C4.
79-
if (match(A, m_Add(m_Value(X), m_ConstantInt())) &&
80-
match(B, m_ConstantInt()))
81-
AddAffected(X);
82-
}
83-
if (!IsAssume) {
84-
// Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
85-
// by computeKnownFPClass().
86-
if ((Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGT) &&
87-
match(A, m_ElementWiseBitCast(m_Value(X))))
88-
InsertAffected(X);
89-
}
90-
91-
if (IsAssume && CmpInst::isFPPredicate(Pred)) {
92-
// fcmp fneg(x), y
93-
// fcmp fabs(x), y
94-
// fcmp fneg(fabs(x)), y
95-
if (match(A, m_FNeg(m_Value(A))))
96-
AddAffected(A);
97-
if (match(A, m_FAbs(m_Value(A))))
98-
AddAffected(A);
99-
}
75+
if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
76+
// assume(A && B) is split to -> assume(A); assume(B);
77+
// assume(!(A || B)) is split to -> assume(!A); assume(!B);
78+
// Finally, assume(A || B) / assume(!(A && B)) generally don't provide
79+
// enough information to be worth handling (intersection of information as
80+
// opposed to union).
81+
if (!IsAssume) {
82+
Worklist.push_back(A);
83+
Worklist.push_back(B);
84+
}
85+
} else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
86+
AddCmpOperands(A, B);
87+
88+
if (ICmpInst::isEquality(Pred)) {
89+
if (match(B, m_ConstantInt())) {
90+
// (X & C) or (X | C) or (X ^ C).
91+
// (X << C) or (X >>_s C) or (X >>_u C).
92+
if (match(A, m_BitwiseLogic(m_Value(X), m_ConstantInt())) ||
93+
match(A, m_Shift(m_Value(X), m_ConstantInt())))
94+
AddAffected(X);
95+
}
96+
} else {
97+
// Handle (A + C1) u< C2, which is the canonical form of
98+
// A > C3 && A < C4.
99+
if (match(A, m_Add(m_Value(X), m_ConstantInt())) &&
100+
match(B, m_ConstantInt()))
101+
AddAffected(X);
102+
103+
// Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
104+
// by computeKnownFPClass().
105+
if (match(A, m_ElementWiseBitCast(m_Value(X)))) {
106+
if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
107+
InsertAffected(X);
108+
else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
109+
InsertAffected(X);
100110
}
101111
}
102-
} else if ((!IsAssume &&
103-
match(Cond, m_FCmp(Pred, m_Value(A), m_Constant()))) ||
104-
match(Cond, m_Intrinsic<Intrinsic::is_fpclass>(m_Value(A),
105-
m_Value(B)))) {
112+
} else if (match(Cond, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
113+
AddCmpOperands(A, B);
114+
115+
// fcmp fneg(x), y
116+
// fcmp fabs(x), y
117+
// fcmp fneg(fabs(x)), y
118+
if (match(A, m_FNeg(m_Value(A))))
119+
AddAffected(A);
120+
if (match(A, m_FAbs(m_Value(A))))
121+
AddAffected(A);
122+
123+
} else if (match(V, m_Intrinsic<Intrinsic::is_fpclass>(m_Value(A),
124+
m_Value()))) {
106125
// Handle patterns that computeKnownFPClass() support.
107126
AddAffected(A);
108127
}

llvm/test/Analysis/ValueTracking/numsignbits-from-assume.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ define i32 @computeNumSignBits_sub1(i32 %in) {
5151

5252
define i32 @computeNumSignBits_sub2(i32 %in) {
5353
; CHECK-LABEL: @computeNumSignBits_sub2(
54-
; CHECK-NEXT: [[SUB:%.*]] = add i32 [[IN:%.*]], -1
54+
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[IN:%.*]], -1
5555
; CHECK-NEXT: [[COND:%.*]] = icmp ult i32 [[SUB]], 43
5656
; CHECK-NEXT: call void @llvm.assume(i1 [[COND]])
5757
; CHECK-NEXT: [[SH:%.*]] = shl nuw nsw i32 [[SUB]], 3

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,9 @@ define i1 @test8(float %x) {
185185
; CHECK-NEXT: [[COND:%.*]] = fcmp oeq float [[ABS]], 0x7FF0000000000000
186186
; CHECK-NEXT: br i1 [[COND]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
187187
; CHECK: if.then:
188-
; CHECK-NEXT: [[RET1:%.*]] = call i1 @llvm.is.fpclass.f32(float [[X]], i32 575)
189-
; CHECK-NEXT: ret i1 [[RET1]]
188+
; CHECK-NEXT: ret i1 true
190189
; CHECK: if.else:
191-
; CHECK-NEXT: [[RET2:%.*]] = call i1 @llvm.is.fpclass.f32(float [[X]], i32 575)
190+
; CHECK-NEXT: [[RET2:%.*]] = call i1 @llvm.is.fpclass.f32(float [[X]], i32 59)
192191
; CHECK-NEXT: ret i1 [[RET2]]
193192
;
194193
%abs = call float @llvm.fabs.f32(float %x)

0 commit comments

Comments
 (0)