diff options
author | Michael Zolotukhin <mzolotukhin@apple.com> | 2014-11-20 20:19:55 +0000 |
---|---|---|
committer | Michael Zolotukhin <mzolotukhin@apple.com> | 2014-11-20 20:19:55 +0000 |
commit | 0dcae714493752ed441ee827a7d3abc95c98e499 (patch) | |
tree | eb6cbc4aec42108f046b857911f82d0b3ba5c6a2 /llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp | |
parent | 365eb05c705800f952ec9d6847478e600fffe4e7 (diff) | |
download | bcm5719-llvm-0dcae714493752ed441ee827a7d3abc95c98e499.tar.gz bcm5719-llvm-0dcae714493752ed441ee827a7d3abc95c98e499.zip |
Fix a trip-count overflow issue in LoopUnroll.
Currently LoopUnroll generates a prologue loop before the main loop
body to execute first N%UnrollFactor iterations. Also, this loop is
used if trip-count can overflow - it's determined by a runtime check.
However, we've been mistakenly optimizing this loop to a linear code for
UnrollFactor = 2, not taking into account that it also serves as a safe
version of the loop if its trip-count overflows.
llvm-svn: 222451
Diffstat (limited to 'llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp b/llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp index 4241fcaa880..3d9133684db 100644 --- a/llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp +++ b/llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp @@ -295,6 +295,10 @@ bool llvm::UnrollRuntimeLoopProlog(Loop *L, unsigned Count, LoopInfo *LI, if (isa<SCEVCouldNotCompute>(BECount) || !BECount->getType()->isIntegerTy()) return false; + // If BECount is INT_MAX, we can't compute trip-count without overflow. + if (BECount->isAllOnesValue()) + return false; + // Add 1 since the backedge count doesn't include the first loop iteration const SCEV *TripCountSC = SE->getAddExpr(BECount, SE->getConstant(BECount->getType(), 1)); @@ -357,11 +361,16 @@ bool llvm::UnrollRuntimeLoopProlog(Loop *L, unsigned Count, LoopInfo *LI, std::vector<BasicBlock *> NewBlocks; ValueToValueMapTy VMap; + // If unroll count is 2 and we can't overflow in tripcount computation (which + // is BECount + 1), then we don't need a loop for prologue, and we can unroll + // it. We can be sure that we don't overflow only if tripcount is a constant. + bool UnrollPrologue = (Count == 2 && isa<ConstantInt>(TripCount)); + // Clone all the basic blocks in the loop. If Count is 2, we don't clone // the loop, otherwise we create a cloned loop to execute the extra // iterations. This function adds the appropriate CFG connections. - CloneLoopBlocks(L, ModVal, Count == 2, PH, PEnd, NewBlocks, LoopBlocks, VMap, - LI); + CloneLoopBlocks(L, ModVal, UnrollPrologue, PH, PEnd, NewBlocks, LoopBlocks, + VMap, LI); // Insert the cloned blocks into function just before the original loop F->getBasicBlockList().splice(PEnd, F->getBasicBlockList(), NewBlocks[0], |