diff options
author | Roman Lebedev <lebedev.ri@gmail.com> | 2019-08-29 12:47:20 +0000 |
---|---|---|
committer | Roman Lebedev <lebedev.ri@gmail.com> | 2019-08-29 12:47:20 +0000 |
commit | 473a063a5e1381287880e8b9c54cce901712f01d (patch) | |
tree | af3d95c2ff970dba721b57527db6e468d8598bb6 /llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | |
parent | fb38b7aab3f23604611ae424f3dc702124962524 (diff) | |
download | bcm5719-llvm-473a063a5e1381287880e8b9c54cce901712f01d.tar.gz bcm5719-llvm-473a063a5e1381287880e8b9c54cce901712f01d.zip |
[InstCombine] Fold '((%x * %y) u/ %x) != %y' to '@llvm.umul.with.overflow' + overflow bit extraction
Summary:
`((%x * %y) u/ %x) != %y` is one of (3?) common ways to check that
some unsigned multiplication (will not) overflow.
Currently, we don't catch it. We could:
```
$ /repositories/alive2/build-Clang-unknown/alive -root-only ~/llvm-patch1.ll
Processing /home/lebedevri/llvm-patch1.ll..
----------------------------------------
Name: no overflow
%o0 = mul i4 %y, %x
%o1 = udiv i4 %o0, %x
%r = icmp ne i4 %o1, %y
ret i1 %r
=>
%n0 = umul_overflow i4 %x, %y
%o0 = extractvalue {i4, i1} %n0, 0
%o1 = udiv %o0, %x
%r = extractvalue {i4, i1} %n0, 1
ret %r
Done: 1
Optimization is correct!
----------------------------------------
Name: no overflow
%o0 = mul i4 %y, %x
%o1 = udiv i4 %o0, %x
%r = icmp eq i4 %o1, %y
ret i1 %r
=>
%n0 = umul_overflow i4 %x, %y
%o0 = extractvalue {i4, i1} %n0, 0
%o1 = udiv %o0, %x
%n1 = extractvalue {i4, i1} %n0, 1
%r = xor %n1, -1
ret i1 %r
Done: 1
Optimization is correct!
```
Reviewers: nikic, spatel, efriedma, xbolva00, RKSimon
Reviewed By: nikic
Subscribers: hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65144
llvm-svn: 370348
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp')
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index 955886c1cbb..8cb0a26cfe3 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -3527,20 +3527,21 @@ foldShiftIntoShiftInAnotherHandOfAndInICmp(ICmpInst &I, const SimplifyQuery SQ, /// Fold /// (-1 u/ x) u< y +/// ((x * y) u/ x) != y /// to /// @llvm.umul.with.overflow(x, y) plus extraction of overflow bit -/// Note that the comparison is commutative, while inverted (u>=) predicate +/// Note that the comparison is commutative, while inverted (u>=, ==) predicate /// will mean that we are looking for the opposite answer. -static Value * -foldUnsignedMultiplicationOverflowCheck(ICmpInst &I, - InstCombiner::BuilderTy &Builder) { +Value *InstCombiner::foldUnsignedMultiplicationOverflowCheck(ICmpInst &I) { ICmpInst::Predicate Pred; Value *X, *Y; + Instruction *Mul; bool NeedNegation; // Look for: (-1 u/ x) u</u>= y if (!I.isEquality() && match(&I, m_c_ICmp(Pred, m_OneUse(m_UDiv(m_AllOnes(), m_Value(X))), m_Value(Y)))) { + Mul = nullptr; // Canonicalize as-if y was on RHS. if (I.getOperand(1) != Y) Pred = I.getSwappedPredicate(); @@ -3556,12 +3557,34 @@ foldUnsignedMultiplicationOverflowCheck(ICmpInst &I, default: return nullptr; // Wrong predicate. } + } else // Look for: ((x * y) u/ x) !=/== y + if (I.isEquality() && + match(&I, m_c_ICmp(Pred, m_Value(Y), + m_OneUse(m_UDiv(m_CombineAnd(m_c_Mul(m_Deferred(Y), + m_Value(X)), + m_Instruction(Mul)), + m_Deferred(X)))))) { + NeedNegation = Pred == ICmpInst::Predicate::ICMP_EQ; } else return nullptr; + BuilderTy::InsertPointGuard Guard(Builder); + // If the pattern included (x * y), we'll want to insert new instructions + // right before that original multiplication so that we can replace it. + bool MulHadOtherUses = Mul && !Mul->hasOneUse(); + if (MulHadOtherUses) + Builder.SetInsertPoint(Mul); + Function *F = Intrinsic::getDeclaration( I.getModule(), Intrinsic::umul_with_overflow, X->getType()); CallInst *Call = Builder.CreateCall(F, {X, Y}, "umul"); + + // If the multiplication was used elsewhere, to ensure that we don't leave + // "duplicate" instructions, replace uses of that original multiplication + // with the multiplication result from the with.overflow intrinsic. + if (MulHadOtherUses) + replaceInstUsesWith(*Mul, Builder.CreateExtractValue(Call, 0, "umul.val")); + Value *Res = Builder.CreateExtractValue(Call, 1, "umul.ov"); if (NeedNegation) // This technically increases instruction count. Res = Builder.CreateNot(Res, "umul.not.ov"); @@ -3918,7 +3941,7 @@ Instruction *InstCombiner::foldICmpBinOp(ICmpInst &I) { } } - if (Value *V = foldUnsignedMultiplicationOverflowCheck(I, Builder)) + if (Value *V = foldUnsignedMultiplicationOverflowCheck(I)) return replaceInstUsesWith(I, V); if (Value *V = foldICmpWithLowBitMaskedVal(I, Builder)) |