From 6f0ec20e8fa23e9588205ea0eaba8f3021abe1ac Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Fri, 12 Jul 2013 11:18:55 +0000 Subject: Revert "indvars: Improve LFTR by eliminating truncation when comparing against a constant." This reverts commit r186107. It didn't handle wrapping arithmetic in the loop correctly and thus caused the following C program to count from 0 to UINT64_MAX instead of from 0 to 255 as intended: #include int main() { unsigned char first = 0, last = 255; do { printf("%d\n", first); } while (first++ != last); } Full test case and instructions to reproduce with just the -indvars pass sent to the original review thread rather than to r186107's commit. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@186152 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/IndVarSimplify.cpp | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) (limited to 'lib/Transforms/Scalar/IndVarSimplify.cpp') diff --git a/lib/Transforms/Scalar/IndVarSimplify.cpp b/lib/Transforms/Scalar/IndVarSimplify.cpp index ddb5b270d0..df11e92c9e 100644 --- a/lib/Transforms/Scalar/IndVarSimplify.cpp +++ b/lib/Transforms/Scalar/IndVarSimplify.cpp @@ -1612,29 +1612,10 @@ LinearFunctionTestReplace(Loop *L, << " IVCount:\t" << *IVCount << "\n"); IRBuilder<> Builder(BI); - - unsigned CmpIndVarSize = SE->getTypeSizeInBits(CmpIndVar->getType()); - unsigned ExitCntSize = SE->getTypeSizeInBits(ExitCnt->getType()); - if (CmpIndVarSize > ExitCntSize) { - const SCEVAddRecExpr *AR = cast(SE->getSCEV(IndVar)); - const SCEV *ARStart = AR->getStart(); - const SCEV *ARStep = AR->getStepRecurrence(*SE); - if (isa(ARStart) && isa(IVCount)) { - const APInt &Start = cast(ARStart)->getValue()->getValue(); - const APInt &Count = cast(IVCount)->getValue()->getValue(); - - APInt NewLimit; - if (cast(ARStep)->getValue()->isNegative()) - NewLimit = Start - Count.zext(CmpIndVarSize); - else - NewLimit = Start + Count.zext(CmpIndVarSize); - ExitCnt = ConstantInt::get(CmpIndVar->getType(), NewLimit); - - DEBUG(dbgs() << " Widen RHS:\t" << *ExitCnt << "\n"); - } else { - CmpIndVar = Builder.CreateTrunc(CmpIndVar, ExitCnt->getType(), - "lftr.wideiv"); - } + if (SE->getTypeSizeInBits(CmpIndVar->getType()) + > SE->getTypeSizeInBits(ExitCnt->getType())) { + CmpIndVar = Builder.CreateTrunc(CmpIndVar, ExitCnt->getType(), + "lftr.wideiv"); } Value *Cond = Builder.CreateICmp(P, CmpIndVar, ExitCnt, "exitcond"); -- cgit v1.2.3