From 498d3113c35d8d4f80e4d79ac9b75c3980bfc8c3 Mon Sep 17 00:00:00 2001 From: Andrew Kaylor Date: Wed, 10 Aug 2016 18:56:35 +0000 Subject: [IndVarSimplify] Eliminate zext of a signed IV when the IV is known to be non-negative Patch by Li Huang Differential Revision: https://reviews.llvm.org/D18867 llvm-svn: 278269 --- llvm/lib/Transforms/Scalar/IndVarSimplify.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'llvm/lib') diff --git a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp index 4f6dbd994e0..4cb2d0d1bf9 100644 --- a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp +++ b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp @@ -36,6 +36,7 @@ #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" #include "llvm/Analysis/TargetLibraryInfo.h" #include "llvm/Analysis/TargetTransformInfo.h" +#include "llvm/Analysis/ValueTracking.h" #include "llvm/IR/BasicBlock.h" #include "llvm/IR/CFG.h" #include "llvm/IR/Constants.h" @@ -1281,7 +1282,8 @@ Instruction *WidenIV::widenIVUse(NarrowIVDefUse DU, SCEVExpander &Rewriter) { } } // Our raison d'etre! Eliminate sign and zero extension. - if (IsSigned ? isa(DU.NarrowUse) : isa(DU.NarrowUse)) { + if ((isa(DU.NarrowUse) && (IsSigned || DU.NeverNegative)) || + (isa(DU.NarrowUse) && (!IsSigned || DU.NeverNegative))) { Value *NewDef = DU.WideDef; if (DU.NarrowUse->getType() != WideType) { unsigned CastWidth = SE->getTypeSizeInBits(DU.NarrowUse->getType()); @@ -1370,9 +1372,12 @@ Instruction *WidenIV::widenIVUse(NarrowIVDefUse DU, SCEVExpander &Rewriter) { /// void WidenIV::pushNarrowIVUsers(Instruction *NarrowDef, Instruction *WideDef) { const SCEV *NarrowSCEV = SE->getSCEV(NarrowDef); + // isKnownPredicate is enough for most cases but still need isKnownNonNegative + // here to work around conservatism in ScalarEvolution about no-wrap flags. bool NeverNegative = SE->isKnownPredicate(ICmpInst::ICMP_SGE, NarrowSCEV, - SE->getConstant(NarrowSCEV->getType(), 0)); + SE->getConstant(NarrowSCEV->getType(), 0)) || + isKnownNonNegative(NarrowDef, NarrowDef->getModule()->getDataLayout()); for (User *U : NarrowDef->users()) { Instruction *NarrowUser = cast(U); -- cgit v1.2.3