diff options
| author | Zinovy Nis <zinovy.nis@gmail.com> | 2014-10-02 13:01:15 +0000 |
|---|---|---|
| committer | Zinovy Nis <zinovy.nis@gmail.com> | 2014-10-02 13:01:15 +0000 |
| commit | ccc3e3733b1e92c01b9b764edc9b3f1f28f2dfed (patch) | |
| tree | 83ef669def3749b1a71ed40977352bcc652a871a /llvm/lib | |
| parent | 09c8f6f6a4f7d83a9633829915a59dcad1b3cfe3 (diff) | |
| download | bcm5719-llvm-ccc3e3733b1e92c01b9b764edc9b3f1f28f2dfed.tar.gz bcm5719-llvm-ccc3e3733b1e92c01b9b764edc9b3f1f28f2dfed.zip | |
[BUG][INDVAR] Fix for PR21014: wrong SCEV operands commuting for non-commutative instructions
My commit rL216160 introduced a bug PR21014: IndVars widens code 'for (i = ; i < ...; i++) arr[ CONST - i]' into 'for (i = ; i < ...; i++) arr[ i - CONST]'
thus inverting index expression. This patch fixes it.
Thanks to Jörg Sonnenberger for pointing.
Differential Revision: http://reviews.llvm.org/D5576
llvm-svn: 218867
Diffstat (limited to 'llvm/lib')
| -rw-r--r-- | llvm/lib/Transforms/Scalar/IndVarSimplify.cpp | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp index 2959c7bd3c7..03dffb66f35 100644 --- a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp +++ b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp @@ -865,7 +865,8 @@ const SCEVAddRecExpr* WidenIV::GetExtendedOperandRecurrence(NarrowIVDefUse DU) { // One operand (NarrowDef) has already been extended to WideDef. Now determine // if extending the other will lead to a recurrence. - unsigned ExtendOperIdx = DU.NarrowUse->getOperand(0) == DU.NarrowDef ? 1 : 0; + const unsigned ExtendOperIdx = + DU.NarrowUse->getOperand(0) == DU.NarrowDef ? 1 : 0; assert(DU.NarrowUse->getOperand(1-ExtendOperIdx) == DU.NarrowDef && "bad DU"); const SCEV *ExtendOperExpr = nullptr; @@ -885,8 +886,16 @@ const SCEVAddRecExpr* WidenIV::GetExtendedOperandRecurrence(NarrowIVDefUse DU) { // behavior depends on. Non-control-equivalent instructions can be mapped to // the same SCEV expression, and it would be incorrect to transfer NSW/NUW // semantics to those operations. - const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>( - GetSCEVByOpCode(SE->getSCEV(DU.WideDef), ExtendOperExpr, OpCode)); + const SCEV *lhs = SE->getSCEV(DU.WideDef); + const SCEV *rhs = ExtendOperExpr; + + // Let's swap operands to the initial order for the case of non-commutative + // operations, like SUB. See PR21014. + if (ExtendOperIdx == 0) + std::swap(lhs, rhs); + const SCEVAddRecExpr *AddRec = + dyn_cast<SCEVAddRecExpr>(GetSCEVByOpCode(lhs, rhs, OpCode)); + if (!AddRec || AddRec->getLoop() != L) return nullptr; return AddRec; |

