Skip to content

Commit 1578c39

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 56cee12 commit 1578c39

File tree

4 files changed

+69
-55
lines changed

4 files changed

+69
-55
lines changed

llvm/include/llvm/Analysis/ConditionCacheUtil.h

Lines changed: 64 additions & 51 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

@@ -43,65 +57,64 @@ findValuesAffectedByCondition(Value *Cond, bool IsAssume,
4357
CmpInst::Predicate Pred;
4458
Value *A, *B, *X;
4559

46-
if (IsAssume)
60+
if (IsAssume) {
4761
AddAffected(V);
62+
if (match(V, m_Not(m_Value(X))))
63+
AddAffected(X);
64+
}
4865

49-
if (IsAssume && match(V, m_Not(m_Value(X))))
50-
AddAffected(X);
51-
if (!IsAssume && match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
52-
Worklist.push_back(A);
53-
Worklist.push_back(B);
54-
} else if (match(V, m_Cmp(Pred, m_Value(A), m_Value(B))) &&
55-
(IsAssume || isa<ICmpInst>(V))) {
56-
if (IsAssume || match(B, m_Constant())) {
66+
if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
67+
// assume(A && B) is split to -> assume(A); assume(B);
68+
// assume(!(A || B)) is split to -> assume(!A); assume(!B);
69+
// TODO: We might want to handle assume(A || B) / assume(!(A && B)). If we
70+
// make this change make sure to update computeKnownBitsFromContext.
71+
if (!IsAssume) {
72+
Worklist.push_back(A);
73+
Worklist.push_back(B);
74+
}
75+
} else if (match(V, m_Cmp(Pred, m_Value(A), m_Value(B)))) {
76+
if (IsAssume) {
77+
AddAffected(A);
78+
AddAffected(B);
79+
} else if (match(B, m_Constant()))
5780
AddAffected(A);
58-
if (IsAssume)
59-
AddAffected(B);
6081

61-
if (IsAssume ? (Pred == ICmpInst::ICMP_EQ)
62-
: ICmpInst::isEquality(Pred)) {
63-
if (match(B, m_ConstantInt())) {
64-
// (X & C) or (X | C) or (X ^ C).
65-
// (X << C) or (X >>_s C) or (X >>_u C).
66-
if (match(A, m_BitwiseLogic(m_Value(X), m_ConstantInt())) ||
67-
match(A, m_Shift(m_Value(X), m_ConstantInt())))
68-
AddAffected(X);
69-
}
70-
} else {
71-
if (Pred == ICmpInst::ICMP_NE)
72-
if (match(A, m_And(m_Value(X), m_Power2())) && match(B, m_Zero()))
73-
AddAffected(X);
82+
if (ICmpInst::isEquality(Pred)) {
83+
if (match(B, m_ConstantInt())) {
84+
// (X & C) or (X | C) or (X ^ C).
85+
// (X << C) or (X >>_s C) or (X >>_u C).
86+
if (match(A, m_BitwiseLogic(m_Value(X), m_ConstantInt())) ||
87+
match(A, m_Shift(m_Value(X), m_ConstantInt())))
88+
AddAffected(X);
89+
}
90+
} else {
91+
// Handle (A + C1) u< C2, which is the canonical form of
92+
// A > C3 && A < C4.
93+
if (match(A, m_Add(m_Value(X), m_ConstantInt())) &&
94+
match(B, m_ConstantInt()))
95+
AddAffected(X);
7496

75-
if (!IsAssume || Pred == ICmpInst::ICMP_ULT) {
76-
// Handle (A + C1) u< C2, which is the canonical form of
77-
// A > C3 && A < C4.
78-
if (match(A, m_Add(m_Value(X), m_ConstantInt())) &&
79-
match(B, m_ConstantInt()))
80-
AddAffected(X);
81-
}
82-
if (!IsAssume) {
83-
// Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
84-
// by computeKnownFPClass().
85-
if ((Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGT) &&
86-
match(A, m_ElementWiseBitCast(m_Value(X))))
87-
InsertAffected(X, -1);
88-
}
97+
// Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
98+
// by computeKnownFPClass().
99+
if (match(A, m_ElementWiseBitCast(m_Value(X)))) {
100+
if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
101+
InsertAffected(X, -1);
102+
else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
103+
InsertAffected(X, -1);
104+
}
89105

90-
if (IsAssume && CmpInst::isFPPredicate(Pred)) {
91-
// fcmp fneg(x), y
92-
// fcmp fabs(x), y
93-
// fcmp fneg(fabs(x)), y
94-
if (match(A, m_FNeg(m_Value(A))))
95-
AddAffected(A);
96-
if (match(A, m_FAbs(m_Value(A))))
97-
AddAffected(A);
98-
}
106+
if (CmpInst::isFPPredicate(Pred)) {
107+
// fcmp fneg(x), y
108+
// fcmp fabs(x), y
109+
// fcmp fneg(fabs(x)), y
110+
if (match(A, m_FNeg(m_Value(A))))
111+
AddAffected(A);
112+
if (match(A, m_FAbs(m_Value(A))))
113+
AddAffected(A);
99114
}
100115
}
101-
} else if ((!IsAssume &&
102-
match(Cond, m_FCmp(Pred, m_Value(A), m_Constant()))) ||
103-
match(Cond, m_Intrinsic<Intrinsic::is_fpclass>(m_Value(A),
104-
m_Value(B)))) {
116+
} else if (match(V, m_Intrinsic<Intrinsic::is_fpclass>(m_Value(A),
117+
m_Value()))) {
105118
// Handle patterns that computeKnownFPClass() support.
106119
AddAffected(A);
107120
}

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,8 @@ void llvm::computeKnownBitsFromContext(const Value *V, KnownBits &Known,
813813
if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
814814
continue;
815815

816+
// Change to `computeKnownBitsFromCond` if we start supporting assume of
817+
// logic op in ConditionCacheUtil.
816818
computeKnownBitsFromCmp(V, Cmp->getPredicate(), Cmp->getOperand(0),
817819
Cmp->getOperand(1), Known, Q);
818820
}

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)