diff options
author | Sanjay Patel <spatel@rotateright.com> | 2019-09-30 17:02:26 +0000 |
---|---|---|
committer | Sanjay Patel <spatel@rotateright.com> | 2019-09-30 17:02:26 +0000 |
commit | 712b7c24634f5c471be4752111836900d65a7a3a (patch) | |
tree | 4c911bbc0a839fae3f69b5fd0bd0139dcf11f856 /llvm/lib | |
parent | 926f55706605f8e1b8a8598448058e6a1875d92c (diff) | |
download | bcm5719-llvm-712b7c24634f5c471be4752111836900d65a7a3a.tar.gz bcm5719-llvm-712b7c24634f5c471be4752111836900d65a7a3a.zip |
[InstCombine] fold negate disguised as select+mul
Name: negate if true
%sel = select i1 %cond, i32 -1, i32 1
%r = mul i32 %sel, %x
=>
%m = sub i32 0, %x
%r = select i1 %cond, i32 %m, i32 %x
Name: negate if false
%sel = select i1 %cond, i32 1, i32 -1
%r = mul i32 %sel, %x
=>
%m = sub i32 0, %x
%r = select i1 %cond, i32 %x, i32 %m
https://rise4fun.com/Alive/Nlh
llvm-svn: 373230
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp index 7f152aac52d..a301d9eef60 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -213,6 +213,25 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) { if (Instruction *FoldedMul = foldBinOpIntoSelectOrPhi(I)) return FoldedMul; + // TODO: This is a specific form of a much more general pattern. + // We could detect a select with any binop identity constant, or we + // could use SimplifyBinOp to see if either arm of the select reduces. + // But that needs to be done carefully and/or while removing potential + // reverse canonicalizations as in InstCombiner::foldSelectIntoOp(). + // mul (select Cond, 1, -1), Op1 --> select Cond, Op1, -Op1 + // mul (select Cond, -1, 1), Op1 --> select Cond, -Op1, Op1 + // mul Op0, (select Cond, 1, -1) --> select Cond, Op0, -Op0 + // mul Op0, (select Cond, -1, 1) --> select Cond, -Op0, Op0 + Value *Cond; + if (match(Op0, m_OneUse(m_Select(m_Value(Cond), m_One(), m_AllOnes())))) + return SelectInst::Create(Cond, Op1, Builder.CreateNeg(Op1)); + if (match(Op0, m_OneUse(m_Select(m_Value(Cond), m_AllOnes(), m_One())))) + return SelectInst::Create(Cond, Builder.CreateNeg(Op1), Op1); + if (match(Op1, m_OneUse(m_Select(m_Value(Cond), m_One(), m_AllOnes())))) + return SelectInst::Create(Cond, Op0, Builder.CreateNeg(Op0)); + if (match(Op1, m_OneUse(m_Select(m_Value(Cond), m_AllOnes(), m_One())))) + return SelectInst::Create(Cond, Builder.CreateNeg(Op0), Op0); + // Simplify mul instructions with a constant RHS. if (isa<Constant>(Op1)) { // Canonicalize (X+C1)*CI -> X*CI+C1*CI. |