diff options
| author | Jingyue Wu <jingyue@google.com> | 2016-07-09 19:13:18 +0000 |
|---|---|---|
| committer | Jingyue Wu <jingyue@google.com> | 2016-07-09 19:13:18 +0000 |
| commit | debce55ac3048eda31c2e97ab9eefc0d10c45fd2 (patch) | |
| tree | 2a69b2b84b0a3276a6c16d82e11bfba498a8081a /llvm/lib | |
| parent | 6170b4bebdcf3c9eae3eb0d88dbe855c98977a08 (diff) | |
| download | bcm5719-llvm-debce55ac3048eda31c2e97ab9eefc0d10c45fd2.tar.gz bcm5719-llvm-debce55ac3048eda31c2e97ab9eefc0d10c45fd2.zip | |
[SLSR] Fix crash on handling 128-bit integers.
ConstantInt::getSExtValue may fail on >64-bit integers. Add checks to call
getSExtValue only on narrow integers.
As a minor aside, simplify slsr-gep.ll to remove unnecessary load instructions.
llvm-svn: 274982
Diffstat (limited to 'llvm/lib')
| -rw-r--r-- | llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp index 411033bd583..77f0a78b928 100644 --- a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp +++ b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp @@ -246,7 +246,9 @@ static bool isGEPFoldable(GetElementPtrInst *GEP, // Returns whether (Base + Index * Stride) can be folded to an addressing mode. static bool isAddFoldable(const SCEV *Base, ConstantInt *Index, Value *Stride, TargetTransformInfo *TTI) { - return TTI->isLegalAddressingMode(Base->getType(), nullptr, 0, true, + // Index->getSExtValue() may crash if Index is wider than 64-bit. + return Index->getBitWidth() <= 64 && + TTI->isLegalAddressingMode(Base->getType(), nullptr, 0, true, Index->getSExtValue(), UnknownAddressSpace); } @@ -502,13 +504,23 @@ void StraightLineStrengthReduce::allocateCandidatesAndFindBasisForGEP( IndexExprs, GEP->isInBounds()); Value *ArrayIdx = GEP->getOperand(I); uint64_t ElementSize = DL->getTypeAllocSize(*GTI); - factorArrayIndex(ArrayIdx, BaseExpr, ElementSize, GEP); + if (ArrayIdx->getType()->getIntegerBitWidth() <= + DL->getPointerSizeInBits()) { + // Skip factoring if ArrayIdx is wider than the pointer size, because + // ArrayIdx is implicitly truncated to the pointer size. + factorArrayIndex(ArrayIdx, BaseExpr, ElementSize, GEP); + } // When ArrayIdx is the sext of a value, we try to factor that value as // well. Handling this case is important because array indices are // typically sign-extended to the pointer size. Value *TruncatedArrayIdx = nullptr; - if (match(ArrayIdx, m_SExt(m_Value(TruncatedArrayIdx)))) + if (match(ArrayIdx, m_SExt(m_Value(TruncatedArrayIdx))) && + TruncatedArrayIdx->getType()->getIntegerBitWidth() <= + DL->getPointerSizeInBits()) { + // Skip factoring if TruncatedArrayIdx is wider than the pointer size, + // because TruncatedArrayIdx is implicitly truncated to the pointer size. factorArrayIndex(TruncatedArrayIdx, BaseExpr, ElementSize, GEP); + } IndexExprs[I - 1] = OrigIndexExpr; } @@ -535,10 +547,11 @@ Value *StraightLineStrengthReduce::emitBump(const Candidate &Basis, if (Basis.CandidateKind == Candidate::GEP) { APInt ElementSize( IndexOffset.getBitWidth(), - DL->getTypeAllocSize(cast<GetElementPtrInst>(Basis.Ins)->getResultElementType())); + DL->getTypeAllocSize( + cast<GetElementPtrInst>(Basis.Ins)->getResultElementType())); APInt Q, R; APInt::sdivrem(IndexOffset, ElementSize, Q, R); - if (R.getSExtValue() == 0) + if (R == 0) IndexOffset = Q; else BumpWithUglyGEP = true; @@ -546,10 +559,10 @@ Value *StraightLineStrengthReduce::emitBump(const Candidate &Basis, // Compute Bump = C - Basis = (i' - i) * S. // Common case 1: if (i' - i) is 1, Bump = S. - if (IndexOffset.getSExtValue() == 1) + if (IndexOffset == 1) return C.Stride; // Common case 2: if (i' - i) is -1, Bump = -S. - if (IndexOffset.getSExtValue() == -1) + if (IndexOffset.isAllOnesValue()) return Builder.CreateNeg(C.Stride); // Otherwise, Bump = (i' - i) * sext/trunc(S). Note that (i' - i) and S may |

