diff options
| author | Roman Lebedev <lebedev.ri@gmail.com> | 2020-01-04 16:50:53 +0300 |
|---|---|---|
| committer | Roman Lebedev <lebedev.ri@gmail.com> | 2020-01-04 17:30:51 +0300 |
| commit | 772ede3d5d552e0214473f247f7f98f15e596fe5 (patch) | |
| tree | ec805d546dcd3c3221ae62298421dfdeef9a0f12 /llvm/lib/Transforms/InstCombine | |
| parent | d2b79c76be5b1613d80364888286e7ead70674eb (diff) | |
| download | bcm5719-llvm-772ede3d5d552e0214473f247f7f98f15e596fe5.tar.gz bcm5719-llvm-772ede3d5d552e0214473f247f7f98f15e596fe5.zip | |
[InstCombine] Sink sub into hands of select if one hand becomes zero. Part 2 (PR44426)
This decreases use count of %Op0, makes one hand of select to be 0,
and possibly exposes further folding potential.
Name: sub %Op0, (select %Cond, %Op0, %FalseVal) -> select %Cond, 0, (sub %Op0, %FalseVal)
%Op0 = %TrueVal
%o = select i1 %Cond, i8 %Op0, i8 %FalseVal
%r = sub i8 %Op0, %o
=>
%n = sub i8 %Op0, %FalseVal
%r = select i1 %Cond, i8 0, i8 %n
Name: sub %Op0, (select %Cond, %TrueVal, %Op0) -> select %Cond, (sub %Op0, %TrueVal), 0
%Op0 = %FalseVal
%o = select i1 %Cond, i8 %TrueVal, i8 %Op0
%r = sub i8 %Op0, %o
=>
%n = sub i8 %Op0, %TrueVal
%r = select i1 %Cond, i8 %n, i8 0
https://rise4fun.com/Alive/aHRt
https://bugs.llvm.org/show_bug.cgi?id=44426
Diffstat (limited to 'llvm/lib/Transforms/InstCombine')
| -rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp index 05d13aeabe5..c723ba0a683 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -1899,6 +1899,33 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) { } { + // If we are subtracting the select with one hand matching the value + // we are subtracting from, sink subtraction into hands of select: + // sub %Op0, (select %Cond, %TrueVal, %FalseVal) + // -> + // select %Cond, (sub %Op0, %TrueVal), (sub %Op0, %FalseVal) + // This will result in select between new subtraction and 0. + Value *Cond, *TrueVal, *FalseVal; + if (match(Op1, m_OneUse(m_Select(m_Value(Cond), m_Value(TrueVal), + m_Value(FalseVal)))) && + (Op0 == TrueVal || Op0 == FalseVal)) { + // While it is really tempting to just create two subtractions and let + // InstCombine fold one of those to 0, it isn't possible to do so + // because of worklist visitation order. So ugly it is. + bool SubtractingFromTrueVal = Op0 == TrueVal; + Value *NewSub = + Builder.CreateSub(Op0, SubtractingFromTrueVal ? FalseVal : TrueVal); + Constant *Zero = Constant::getNullValue(I.getType()); + SelectInst *NewSel = + SelectInst::Create(Cond, SubtractingFromTrueVal ? Zero : NewSub, + SubtractingFromTrueVal ? NewSub : Zero); + // Preserve prof metadata if any. + NewSel->copyMetadata(cast<Instruction>(*Op1)); + return NewSel; + } + } + + { // If we are subtracting from select with one hand matching the value // we are subtracting, sink subtraction into hands of select: // sub (select %Cond, %TrueVal, %FalseVal), %Op1 |

