Skip to content

Commit

Permalink
[InstCombine] Simplify addends reordering logic
Browse files Browse the repository at this point in the history
Previously some constants were not pushed to the top of the resulting
expression tree as intended by the algorithm. We can remove the logic
from simplifyFAdd and rely on SimplifyAssociativeOrCommutative to do
that.

Differential Revision: https://reviews.llvm.org/D117302
  • Loading branch information
kovdan01 authored and asavonic committed Jan 18, 2022
1 parent 51f743d commit d8e0e12
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
28 changes: 10 additions & 18 deletions llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,6 @@ Value *FAddCombine::simplifyFAdd(AddendVect& Addends, unsigned InstrQuota) {
unsigned NextTmpIdx = 0;
FAddend TmpResult[3];

// Points to the constant addend of the resulting simplified expression.
// If the resulting expr has constant-addend, this constant-addend is
// desirable to reside at the top of the resulting expression tree. Placing
// constant close to supper-expr(s) will potentially reveal some optimization
// opportunities in super-expr(s).
const FAddend *ConstAdd = nullptr;

// Simplified addends are placed <SimpVect>.
AddendVect SimpVect;

Expand All @@ -541,6 +534,14 @@ Value *FAddCombine::simplifyFAdd(AddendVect& Addends, unsigned InstrQuota) {
}

Value *Val = ThisAddend->getSymVal();

// If the resulting expr has constant-addend, this constant-addend is
// desirable to reside at the top of the resulting expression tree. Placing
// constant close to super-expr(s) will potentially reveal some
// optimization opportunities in super-expr(s). Here we do not implement
// this logic intentionally and rely on SimplifyAssociativeOrCommutative
// call later.

unsigned StartIdx = SimpVect.size();
SimpVect.push_back(ThisAddend);

Expand Down Expand Up @@ -569,24 +570,15 @@ Value *FAddCombine::simplifyFAdd(AddendVect& Addends, unsigned InstrQuota) {

// Pop all addends being folded and push the resulting folded addend.
SimpVect.resize(StartIdx);
if (Val) {
if (!R.isZero()) {
SimpVect.push_back(&R);
}
} else {
// Don't push constant addend at this time. It will be the last element
// of <SimpVect>.
ConstAdd = &R;
if (!R.isZero()) {
SimpVect.push_back(&R);
}
}
}

assert((NextTmpIdx <= array_lengthof(TmpResult) + 1) &&
"out-of-bound access");

if (ConstAdd)
SimpVect.push_back(ConstAdd);

Value *Result;
if (!SimpVect.empty())
Result = createNaryFAdd(SimpVect, InstrQuota);
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Transforms/Reassociate/fast-basictest.ll
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ define float @test12(float %X) {
; Check again with 'reassoc' and 'nsz' ('nsz' not technically required).
define float @test12_reassoc_nsz(float %X) {
; CHECK-LABEL: @test12_reassoc_nsz(
; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc nsz float [[X:%.*]], 3.000000e+00
; CHECK-NEXT: [[TMP2:%.*]] = fsub reassoc nsz float 6.000000e+00, [[TMP1]]
; CHECK-NEXT: [[TMP1:%.*]] = fmul reassoc nsz float [[X:%.*]], -3.000000e+00
; CHECK-NEXT: [[TMP2:%.*]] = fadd reassoc nsz float [[TMP1]], 6.000000e+00
; CHECK-NEXT: ret float [[TMP2]]
;
%A = fsub reassoc nsz float 1.000000e+00, %X
Expand Down

0 comments on commit d8e0e12

Please sign in to comment.