diff options
author | Sanjay Patel <spatel@rotateright.com> | 2016-09-22 22:36:26 +0000 |
---|---|---|
committer | Sanjay Patel <spatel@rotateright.com> | 2016-09-22 22:36:26 +0000 |
commit | 30ef70b090ac5917bf97c92d8bab05d48cc3057b (patch) | |
tree | 9b255b4aa09aebaf05d0b41ca742337808578141 /llvm/lib | |
parent | 3acdf38519923f1ce293c7bacac542804145ea7c (diff) | |
download | bcm5719-llvm-30ef70b090ac5917bf97c92d8bab05d48cc3057b.tar.gz bcm5719-llvm-30ef70b090ac5917bf97c92d8bab05d48cc3057b.zip |
[InstCombine] fold X urem C -> X < C ? X : X - C when C is big (PR28672)
We already have the udiv variant of this transform, so I think this is ok for
InstCombine too even though there is an increase in IR instructions. As the
tests and TODO comments show, the transform can lead to follow-on combines.
This should fix: https://llvm.org/bugs/show_bug.cgi?id=28672
Differential Revision: https://reviews.llvm.org/D24527
llvm-svn: 282209
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp index 7f125905029..cd31c63ff28 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -1447,6 +1447,14 @@ Instruction *InstCombiner::visitURem(BinaryOperator &I) { return replaceInstUsesWith(I, Ext); } + // X urem C -> X < C ? X : X - C, where C >= signbit. + const APInt *DivisorC; + if (match(Op1, m_APInt(DivisorC)) && DivisorC->isNegative()) { + Value *Cmp = Builder->CreateICmpULT(Op0, Op1); + Value *Sub = Builder->CreateSub(Op0, Op1); + return SelectInst::Create(Cmp, Op0, Sub); + } + return nullptr; } |