diff --git a/llvm/lib/Analysis/StackSafetyAnalysis.cpp b/llvm/lib/Analysis/StackSafetyAnalysis.cpp --- a/llvm/lib/Analysis/StackSafetyAnalysis.cpp +++ b/llvm/lib/Analysis/StackSafetyAnalysis.cpp @@ -42,25 +42,6 @@ namespace { -/// Rewrite an SCEV expression for a memory access address to an expression that -/// represents offset from the given alloca. -class AllocaOffsetRewriter : public SCEVRewriteVisitor { - const Value *AllocaPtr; - -public: - AllocaOffsetRewriter(ScalarEvolution &SE, const Value *AllocaPtr) - : SCEVRewriteVisitor(SE), AllocaPtr(AllocaPtr) {} - - const SCEV *visitUnknown(const SCEVUnknown *Expr) { - // FIXME: look through one or several levels of definitions? - // This can be inttoptr(AllocaPtr) and SCEV would not unwrap - // it for us. - if (Expr->getValue() == AllocaPtr) - return SE.getZero(Expr->getType()); - return Expr; - } -}; - /// Describes use of address in as a function call argument. template struct CallInfo { /// Function being called. @@ -226,12 +207,15 @@ }; ConstantRange StackSafetyLocalAnalysis::offsetFrom(Value *Addr, Value *Base) { - if (!SE.isSCEVable(Addr->getType())) + if (!SE.isSCEVable(Addr->getType()) || !SE.isSCEVable(Base->getType())) return UnknownRange; - AllocaOffsetRewriter Rewriter(SE, Base); - const SCEV *Expr = Rewriter.visit(SE.getSCEV(Addr)); - ConstantRange Offset = SE.getSignedRange(Expr); + auto *PtrTy = IntegerType::getInt8PtrTy(SE.getContext()); + const SCEV *AddrExp = SE.getTruncateOrZeroExtend(SE.getSCEV(Addr), PtrTy); + const SCEV *BaseExp = SE.getTruncateOrZeroExtend(SE.getSCEV(Base), PtrTy); + const SCEV *Diff = SE.getMinusSCEV(AddrExp, BaseExp); + + ConstantRange Offset = SE.getSignedRange(Diff); if (isUnsafe(Offset)) return UnknownRange; return Offset.sextOrTrunc(PointerSize);