From 743f7e8839dc02f2e808e977803368c431279189 Mon Sep 17 00:00:00 2001 From: Devang Patel Date: Wed, 6 Jun 2007 00:21:03 +0000 Subject: [PATCH] Avoid non-trivial loop unswitching while optimizing for size. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@37446 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Transforms/Scalar.h | 2 +- lib/Transforms/Scalar/LoopUnswitch.cpp | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/include/llvm/Transforms/Scalar.h b/include/llvm/Transforms/Scalar.h index 67e881bb536..3d88a2f08ec 100644 --- a/include/llvm/Transforms/Scalar.h +++ b/include/llvm/Transforms/Scalar.h @@ -127,7 +127,7 @@ LoopPass *createLoopStrengthReducePass(const TargetLowering *TLI = 0); // // LoopUnswitch - This pass is a simple loop unswitching pass. // -LoopPass *createLoopUnswitchPass(); +LoopPass *createLoopUnswitchPass(bool Os = false); //===----------------------------------------------------------------------===// // diff --git a/lib/Transforms/Scalar/LoopUnswitch.cpp b/lib/Transforms/Scalar/LoopUnswitch.cpp index 61510a71003..7ad3bd626c1 100644 --- a/lib/Transforms/Scalar/LoopUnswitch.cpp +++ b/lib/Transforms/Scalar/LoopUnswitch.cpp @@ -67,10 +67,12 @@ namespace { // after RewriteLoopBodyWithConditionConstant rewrites first loop. std::vector LoopProcessWorklist; SmallPtrSet UnswitchedVals; - + + bool OptimizeForSize; public: static char ID; // Pass ID, replacement for typeid - LoopUnswitch() : LoopPass((intptr_t)&ID) {} + LoopUnswitch(bool Os = false) : + LoopPass((intptr_t)&ID), OptimizeForSize(Os) {} bool runOnLoop(Loop *L, LPPassManager &LPM); @@ -116,7 +118,9 @@ namespace { RegisterPass X("loop-unswitch", "Unswitch loops"); } -LoopPass *llvm::createLoopUnswitchPass() { return new LoopUnswitch(); } +LoopPass *llvm::createLoopUnswitchPass(bool Os) { + return new LoopUnswitch(Os); +} /// FindLIVLoopCondition - Cond is a condition that occurs in L. If it is /// invariant in the loop, or has an invariant piece, return the invariant. @@ -359,6 +363,11 @@ unsigned LoopUnswitch::getLoopUnswitchCost(Loop *L, Value *LIC) { bool LoopUnswitch::UnswitchIfProfitable(Value *LoopCond, Constant *Val,Loop *L){ // Check to see if it would be profitable to unswitch this loop. unsigned Cost = getLoopUnswitchCost(L, LoopCond); + + // Do not do non-trivial unswitch while optimizing for size. + if (Cost && OptimizeForSize) + return false; + if (Cost > Threshold) { // FIXME: this should estimate growth by the amount of code shared by the // resultant unswitched loops.