Skip to content

Commit b66f128

Browse files
committed
learning llvm...
1 parent 25aec6b commit b66f128

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,26 +1324,23 @@ Instruction *InstCombinerImpl::foldICmpWithZero(ICmpInst &Cmp) {
13241324
// to
13251325
// icmp eq 0, (and num, val - 1)
13261326
// For value being power of two
1327-
Instruction *InstCombinerImpl::foldNextMultiply(ICmpInst &Cmp) {
1328-
Value *Op0 = Cmp.getOperand(0);
1329-
Value *Neg, *Add, *Num, *Mask, *Value;
1327+
Instruction *InstCombinerImpl::foldIsMultipleOfAPowerOfTwo(ICmpInst &Cmp) {
1328+
Value *Op0 = Cmp.getOperand(0), *Op1 = Cmp.getOperand(1);
1329+
Value *Neg, *Num, *Mask, *Value;
13301330
CmpPredicate Pred;
13311331
const APInt *NegConst, *MaskConst;
13321332

1333-
// Match num + neg
1334-
if (!match(Op0, m_OneUse(m_c_And(m_Value(Add), m_Value(Neg)))))
1333+
if (!match(&Cmp, m_c_ICmp(Pred, m_Value(Num),
1334+
m_OneUse(m_c_And(
1335+
m_OneUse(m_c_Add(m_Value(Num), m_Value(Mask))),
1336+
m_Value(Neg))))))
13351337
return nullptr;
13361338

1337-
// Match num & mask and handle commutative care
1338-
if (!match(Op0, m_c_And(m_c_Add(m_Value(Num), m_Value(Mask)), m_Value(Neg))))
1339+
if (!ICmpInst::isEquality(Pred))
13391340
return nullptr;
13401341

13411342
// Check the constant case
1342-
if (match(Neg, m_APInt(NegConst)) && match(Mask, m_APInt(MaskConst))) {
1343-
// Mask + 1 should be a power-of-two
1344-
if (!(*MaskConst + 1).isPowerOf2())
1345-
return nullptr;
1346-
1343+
if (match(Neg, m_APInt(NegConst)) && match(Mask, m_LowBitMask(MaskConst))) {
13471344
// Neg = -(Mask + 1)
13481345
if (*NegConst != -(*MaskConst + 1))
13491346
return nullptr;
@@ -1367,11 +1364,7 @@ Instruction *InstCombinerImpl::foldNextMultiply(ICmpInst &Cmp) {
13671364
return nullptr;
13681365
}
13691366

1370-
// Verify that Add and Num are connected by ICmp.
1371-
if (!match(&Cmp, m_c_ICmp(Pred, m_Value(Add), m_Specific(Num))))
1372-
return nullptr;
1373-
1374-
if (!ICmpInst::isEquality(Pred))
1367+
if (!match(Op0, m_Specific(Num)) && !match(Op1, m_Specific(Num)))
13751368
return nullptr;
13761369

13771370
// Create new icmp eq (num & (val - 1)), 0
@@ -7705,7 +7698,7 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
77057698
if (Instruction *Res = foldICmpUsingKnownBits(I))
77067699
return Res;
77077700

7708-
if (Instruction *Res = foldNextMultiply(I))
7701+
if (Instruction *Res = foldIsMultipleOfAPowerOfTwo(I))
77097702
return Res;
77107703

77117704
// Test if the ICmpInst instruction is used exclusively by a select as

llvm/lib/Transforms/InstCombine/InstCombineInternal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
721721
Instruction *foldICmpUsingKnownBits(ICmpInst &Cmp);
722722
Instruction *foldICmpWithDominatingICmp(ICmpInst &Cmp);
723723
Instruction *foldICmpWithConstant(ICmpInst &Cmp);
724-
Instruction *foldNextMultiply(ICmpInst &Cmp);
724+
Instruction *foldIsMultipleOfAPowerOfTwo(ICmpInst &Cmp);
725725
Instruction *foldICmpUsingBoolRange(ICmpInst &I);
726726
Instruction *foldICmpInstWithConstant(ICmpInst &Cmp);
727727
Instruction *foldICmpInstWithConstantNotInt(ICmpInst &Cmp);

llvm/test/Transforms/InstCombine/icmp-add.ll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3454,12 +3454,13 @@ define i1 @val_is_aligend_const_pow2_multiuse(i32 %num) {
34543454
ret i1 %_0
34553455
}
34563456

3457+
; Applies since number of instructions do not change
34573458
define i1 @val_is_aligend_const_pow2_multiuse1(i32 %num) {
34583459
; CHECK-LABEL: @val_is_aligend_const_pow2_multiuse1(
34593460
; CHECK-NEXT: [[NUM_BIASED:%.*]] = add i32 [[NUM:%.*]], 4095
34603461
; CHECK-NEXT: call void @use(i32 [[NUM_BIASED]])
3461-
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[NUM]], 4095
3462-
; CHECK-NEXT: [[_0:%.*]] = icmp eq i32 [[TMP1]], 0
3462+
; CHECK-NEXT: [[_2_SROA_0_0:%.*]] = and i32 [[NUM_BIASED]], -4096
3463+
; CHECK-NEXT: [[_0:%.*]] = icmp eq i32 [[_2_SROA_0_0]], [[NUM]]
34633464
; CHECK-NEXT: ret i1 [[_0]]
34643465
;
34653466
%num.biased = add i32 %num, 4095

0 commit comments

Comments
 (0)