Skip to content

Commit 3d4581c

Browse files
committed
wip
1 parent 8775232 commit 3d4581c

File tree

4 files changed

+59
-81
lines changed

4 files changed

+59
-81
lines changed

llvm/lib/Analysis/AssumptionCache.cpp

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -92,29 +92,19 @@ findAffectedValues(CallBase *CI, TargetTransformInfo *TTI,
9292
AddAffected(B);
9393

9494
if (Pred == ICmpInst::ICMP_EQ) {
95-
// For equality comparisons, we handle the case of bit inversion.
96-
auto AddAffectedFromEq = [&AddAffected](Value *V) {
97-
Value *A, *B;
98-
// (A & B) or (A | B) or (A ^ B).
99-
if (match(V, m_BitwiseLogic(m_Value(A), m_Value(B)))) {
100-
AddAffected(A);
101-
AddAffected(B);
102-
// (A << C) or (A >>_s C) or (A >>_u C) where C is some constant.
103-
} else if (match(V, m_Shift(m_Value(A), m_ConstantInt()))) {
104-
AddAffected(A);
105-
}
106-
};
107-
108-
AddAffectedFromEq(A);
109-
AddAffectedFromEq(B);
95+
if (match(B, m_ConstantInt())) {
96+
Value *X;
97+
// (X & C) or (X | C) or (X ^ C).
98+
// (X << C) or (X >>_s C) or (X >>_u C).
99+
if (match(A, m_BitwiseLogic(m_Value(X), m_ConstantInt())) ||
100+
match(A, m_Shift(m_Value(X), m_ConstantInt())))
101+
AddAffected(X);
102+
}
110103
} else if (Pred == ICmpInst::ICMP_NE) {
111-
Value *X, *Y;
112-
// Handle (a & b != 0). If a/b is a power of 2 we can use this
113-
// information.
114-
if (match(A, m_And(m_Value(X), m_Value(Y))) && match(B, m_Zero())) {
104+
Value *X;
105+
// Handle (X & pow2 != 0).
106+
if (match(A, m_And(m_Value(X), m_Power2())) && match(B, m_Zero()))
115107
AddAffected(X);
116-
AddAffected(Y);
117-
}
118108
} else if (Pred == ICmpInst::ICMP_ULT) {
119109
Value *X;
120110
// Handle (A + C1) u< C2, which is the canonical form of A > C3 && A < C4,

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 43 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -640,82 +640,69 @@ static void computeKnownBitsFromCmp(const Value *V, const ICmpInst *Cmp,
640640
QueryNoAC.AC = nullptr;
641641

642642
// Note that ptrtoint may change the bitwidth.
643-
Value *A, *B;
644643
auto m_V =
645644
m_CombineOr(m_Specific(V), m_PtrToIntSameSize(Q.DL, m_Specific(V)));
646645

647646
CmpInst::Predicate Pred;
648-
uint64_t C;
647+
const APInt *Mask, *C;
648+
uint64_t ShAmt;
649649
switch (Cmp->getPredicate()) {
650650
case ICmpInst::ICMP_EQ:
651-
// assume(v = a)
652-
if (match(Cmp, m_c_ICmp(Pred, m_V, m_Value(A)))) {
653-
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC);
654-
Known = Known.unionWith(RHSKnown);
655-
// assume(v & b = a)
651+
// assume(V = C)
652+
if (match(Cmp, m_ICmp(Pred, m_V, m_APInt(C)))) {
653+
Known = Known.unionWith(KnownBits::makeConstant(*C));
654+
// assume(V & Mask = C)
656655
} else if (match(Cmp,
657-
m_c_ICmp(Pred, m_c_And(m_V, m_Value(B)), m_Value(A)))) {
658-
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC);
659-
KnownBits MaskKnown = computeKnownBits(B, Depth + 1, QueryNoAC);
660-
661-
// For those bits in the mask that are known to be one, we can propagate
662-
// known bits from the RHS to V.
663-
Known.Zero |= RHSKnown.Zero & MaskKnown.One;
664-
Known.One |= RHSKnown.One & MaskKnown.One;
665-
// assume(v | b = a)
656+
m_ICmp(Pred, m_And(m_V, m_APInt(Mask)), m_APInt(C)))) {
657+
// For one bits in Mask, we can propagate bits from C to V.
658+
Known.Zero |= ~*C & *Mask;
659+
Known.One |= *C & *Mask;
660+
// assume(V | Mask = C)
666661
} else if (match(Cmp,
667-
m_c_ICmp(Pred, m_c_Or(m_V, m_Value(B)), m_Value(A)))) {
668-
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC);
669-
KnownBits BKnown = computeKnownBits(B, Depth + 1, QueryNoAC);
670-
671-
// For those bits in B that are known to be zero, we can propagate known
672-
// bits from the RHS to V.
673-
Known.Zero |= RHSKnown.Zero & BKnown.Zero;
674-
Known.One |= RHSKnown.One & BKnown.Zero;
675-
// assume(v ^ b = a)
662+
m_ICmp(Pred, m_Or(m_V, m_APInt(Mask)), m_APInt(C)))) {
663+
// For zero bits in Mask, we can propagate bits from C to V.
664+
Known.Zero |= ~*C & ~*Mask;
665+
Known.One |= *C & ~*Mask;
666+
// assume(V ^ Mask = C)
676667
} else if (match(Cmp,
677-
m_c_ICmp(Pred, m_c_Xor(m_V, m_Value(B)), m_Value(A)))) {
678-
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC);
679-
KnownBits BKnown = computeKnownBits(B, Depth + 1, QueryNoAC);
680-
681-
// For those bits in B that are known to be zero, we can propagate known
682-
// bits from the RHS to V. For those bits in B that are known to be one,
683-
// we can propagate inverted known bits from the RHS to V.
684-
Known.Zero |= RHSKnown.Zero & BKnown.Zero;
685-
Known.One |= RHSKnown.One & BKnown.Zero;
686-
Known.Zero |= RHSKnown.One & BKnown.One;
687-
Known.One |= RHSKnown.Zero & BKnown.One;
688-
// assume(v << c = a)
689-
} else if (match(Cmp, m_c_ICmp(Pred, m_Shl(m_V, m_ConstantInt(C)),
690-
m_Value(A))) &&
691-
C < BitWidth) {
692-
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC);
693-
694-
// For those bits in RHS that are known, we can propagate them to known
695-
// bits in V shifted to the right by C.
696-
RHSKnown.Zero.lshrInPlace(C);
697-
RHSKnown.One.lshrInPlace(C);
668+
m_ICmp(Pred, m_Xor(m_V, m_APInt(Mask)), m_APInt(C)))) {
669+
// For those bits in Mask that are zero, we can propagate known bits
670+
// from C to V. For those bits in Mask that are one, we can propagate
671+
// inverted bits from C to V.
672+
Known.Zero |= ~*C & ~*Mask;
673+
Known.One |= *C & ~*Mask;
674+
Known.Zero |= *C & *Mask;
675+
Known.One |= ~*C & *Mask;
676+
// assume(V << ShAmt = C)
677+
} else if (match(Cmp, m_ICmp(Pred, m_Shl(m_V, m_ConstantInt(ShAmt)),
678+
m_APInt(C))) &&
679+
ShAmt < BitWidth) {
680+
// For those bits in C that are known, we can propagate them to known
681+
// bits in V shifted to the right by ShAmt.
682+
KnownBits RHSKnown = KnownBits::makeConstant(*C);
683+
RHSKnown.Zero.lshrInPlace(ShAmt);
684+
RHSKnown.One.lshrInPlace(ShAmt);
698685
Known = Known.unionWith(RHSKnown);
699-
// assume(v >> c = a)
700-
} else if (match(Cmp, m_c_ICmp(Pred, m_Shr(m_V, m_ConstantInt(C)),
701-
m_Value(A))) &&
702-
C < BitWidth) {
703-
KnownBits RHSKnown = computeKnownBits(A, Depth + 1, QueryNoAC);
686+
// assume(V >> ShAmt = C)
687+
} else if (match(Cmp, m_ICmp(Pred, m_Shr(m_V, m_ConstantInt(ShAmt)),
688+
m_APInt(C))) &&
689+
ShAmt < BitWidth) {
690+
KnownBits RHSKnown = KnownBits::makeConstant(*C);
704691
// For those bits in RHS that are known, we can propagate them to known
705692
// bits in V shifted to the right by C.
706-
Known.Zero |= RHSKnown.Zero << C;
707-
Known.One |= RHSKnown.One << C;
693+
Known.Zero |= RHSKnown.Zero << ShAmt;
694+
Known.One |= RHSKnown.One << ShAmt;
708695
}
709696
break;
710697
case ICmpInst::ICMP_NE: {
711-
// assume (v & b != 0) where b is a power of 2
698+
// assume (V & B != 0) where B is a power of 2
712699
const APInt *BPow2;
713-
if (match(Cmp, m_ICmp(Pred, m_c_And(m_V, m_Power2(BPow2)), m_Zero()))) {
700+
if (match(Cmp, m_ICmp(Pred, m_And(m_V, m_Power2(BPow2)), m_Zero())))
714701
Known.One |= *BPow2;
715-
}
716702
break;
717703
}
718704
default:
705+
Value *A;
719706
const APInt *Offset = nullptr;
720707
if (match(Cmp, m_ICmp(Pred, m_CombineOr(m_V, m_Add(m_V, m_APInt(Offset))),
721708
m_Value(A)))) {

llvm/test/Transforms/InstCombine/assume.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ define i32 @bundle2(ptr %P) {
265265

266266
define i1 @nonnull1(ptr %a) {
267267
; CHECK-LABEL: @nonnull1(
268-
; CHECK-NEXT: [[LOAD:%.*]] = load ptr, ptr [[A:%.*]], align 8, !nonnull !6, !noundef !6
268+
; CHECK-NEXT: [[LOAD:%.*]] = load ptr, ptr [[A:%.*]], align 8, !nonnull [[META6:![0-9]+]], !noundef [[META6]]
269269
; CHECK-NEXT: tail call void @escape(ptr nonnull [[LOAD]])
270270
; CHECK-NEXT: ret i1 false
271271
;
@@ -383,7 +383,7 @@ define i1 @nonnull5(ptr %a) {
383383
define i32 @assumption_conflicts_with_known_bits(i32 %a, i32 %b) {
384384
; CHECK-LABEL: @assumption_conflicts_with_known_bits(
385385
; CHECK-NEXT: store i1 true, ptr poison, align 1
386-
; CHECK-NEXT: ret i32 poison
386+
; CHECK-NEXT: ret i32 1
387387
;
388388
%and1 = and i32 %b, 3
389389
%B1 = lshr i32 %and1, %and1

llvm/test/Transforms/InstCombine/icmp.ll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4016,9 +4016,10 @@ define i32 @abs_preserve(i32 %x) {
40164016
declare void @llvm.assume(i1)
40174017
define i1 @PR35794(ptr %a) {
40184018
; CHECK-LABEL: @PR35794(
4019-
; CHECK-NEXT: [[MASKCOND:%.*]] = icmp eq ptr [[A:%.*]], null
4019+
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt ptr [[A:%.*]], inttoptr (i64 -1 to ptr)
4020+
; CHECK-NEXT: [[MASKCOND:%.*]] = icmp eq ptr [[A]], null
40204021
; CHECK-NEXT: tail call void @llvm.assume(i1 [[MASKCOND]])
4021-
; CHECK-NEXT: ret i1 true
4022+
; CHECK-NEXT: ret i1 [[CMP]]
40224023
;
40234024
%cmp = icmp sgt ptr %a, inttoptr (i64 -1 to ptr)
40244025
%maskcond = icmp eq ptr %a, null

0 commit comments

Comments
 (0)