diff options
Diffstat (limited to 'llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp')
| -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 | 

