Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 1 | //===-- LoopUnswitch.cpp - Hoist loop-invariant conditionals in loop ------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass transforms loops that contain branches on loop-invariant conditions |
| 11 | // to have multiple loops. For example, it turns the left into the right code: |
| 12 | // |
| 13 | // for (...) if (lic) |
| 14 | // A for (...) |
| 15 | // if (lic) A; B; C |
| 16 | // B else |
| 17 | // C for (...) |
| 18 | // A; C |
| 19 | // |
| 20 | // This can increase the size of the code exponentially (doubling it every time |
| 21 | // a loop is unswitched) so we only unswitch if the resultant code will be |
| 22 | // smaller than a threshold. |
| 23 | // |
| 24 | // This pass expects LICM to be run before it to hoist invariant conditions out |
| 25 | // of the loop, to make the unswitching opportunity obvious. |
| 26 | // |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Scalar.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/STLExtras.h" |
| 31 | #include "llvm/ADT/SmallPtrSet.h" |
| 32 | #include "llvm/ADT/Statistic.h" |
| 33 | #include "llvm/Analysis/CodeMetrics.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 34 | #include "llvm/Analysis/InstructionSimplify.h" |
| 35 | #include "llvm/Analysis/LoopInfo.h" |
| 36 | #include "llvm/Analysis/LoopPass.h" |
| 37 | #include "llvm/Analysis/ScalarEvolution.h" |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 38 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 39 | #include "llvm/IR/Constants.h" |
| 40 | #include "llvm/IR/DerivedTypes.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 41 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 42 | #include "llvm/IR/Function.h" |
| 43 | #include "llvm/IR/Instructions.h" |
Chris Lattner | e487abb | 2006-02-09 20:15:48 +0000 | [diff] [blame] | 44 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 46 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 47 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 48 | #include "llvm/Transforms/Utils/Cloning.h" |
| 49 | #include "llvm/Transforms/Utils/Local.h" |
Alkis Evlogimenos | 20aa474 | 2004-09-03 18:19:51 +0000 | [diff] [blame] | 50 | #include <algorithm> |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 51 | #include <map> |
Chris Lattner | 2f4b898 | 2006-02-09 19:14:52 +0000 | [diff] [blame] | 52 | #include <set> |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 53 | using namespace llvm; |
| 54 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 55 | #define DEBUG_TYPE "loop-unswitch" |
| 56 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 57 | STATISTIC(NumBranches, "Number of branches unswitched"); |
| 58 | STATISTIC(NumSwitches, "Number of switches unswitched"); |
| 59 | STATISTIC(NumSelects , "Number of selects unswitched"); |
| 60 | STATISTIC(NumTrivial , "Number of unswitches that are trivial"); |
| 61 | STATISTIC(NumSimplify, "Number of simplifications of unswitched code"); |
Stepan Dyatkovskiy | 88c5c42 | 2012-01-11 08:40:51 +0000 | [diff] [blame] | 62 | STATISTIC(TotalInsts, "Total number of instructions analyzed"); |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 63 | |
Stepan Dyatkovskiy | 27bf560 | 2012-01-16 20:48:04 +0000 | [diff] [blame] | 64 | // The specific value of 100 here was chosen based only on intuition and a |
Dan Gohman | b24f6c7 | 2009-10-13 17:50:43 +0000 | [diff] [blame] | 65 | // few specific examples. |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 66 | static cl::opt<unsigned> |
| 67 | Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"), |
Stepan Dyatkovskiy | 88c5c42 | 2012-01-11 08:40:51 +0000 | [diff] [blame] | 68 | cl::init(100), cl::Hidden); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 69 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 70 | namespace { |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 71 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 72 | class LUAnalysisCache { |
| 73 | |
| 74 | typedef DenseMap<const SwitchInst*, SmallPtrSet<const Value *, 8> > |
| 75 | UnswitchedValsMap; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 76 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 77 | typedef UnswitchedValsMap::iterator UnswitchedValsIt; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 78 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 79 | struct LoopProperties { |
| 80 | unsigned CanBeUnswitchedCount; |
| 81 | unsigned SizeEstimation; |
| 82 | UnswitchedValsMap UnswitchedVals; |
| 83 | }; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 84 | |
| 85 | // Here we use std::map instead of DenseMap, since we need to keep valid |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 86 | // LoopProperties pointer for current loop for better performance. |
| 87 | typedef std::map<const Loop*, LoopProperties> LoopPropsMap; |
| 88 | typedef LoopPropsMap::iterator LoopPropsMapIt; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 89 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 90 | LoopPropsMap LoopsProperties; |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 91 | UnswitchedValsMap *CurLoopInstructions; |
| 92 | LoopProperties *CurrentLoopProperties; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 93 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 94 | // Max size of code we can produce on remained iterations. |
| 95 | unsigned MaxSize; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 96 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 97 | public: |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 98 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 99 | LUAnalysisCache() : |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 100 | CurLoopInstructions(nullptr), CurrentLoopProperties(nullptr), |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 101 | MaxSize(Threshold) |
| 102 | {} |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 103 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 104 | // Analyze loop. Check its size, calculate is it possible to unswitch |
| 105 | // it. Returns true if we can unswitch this loop. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 106 | bool countLoop(const Loop *L, const TargetTransformInfo &TTI); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 107 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 108 | // Clean all data related to given loop. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 109 | void forgetLoop(const Loop *L); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 110 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 111 | // Mark case value as unswitched. |
| 112 | // Since SI instruction can be partly unswitched, in order to avoid |
| 113 | // extra unswitching in cloned loops keep track all unswitched values. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 114 | void setUnswitched(const SwitchInst *SI, const Value *V); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 115 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 116 | // Check was this case value unswitched before or not. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 117 | bool isUnswitched(const SwitchInst *SI, const Value *V); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 118 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 119 | // Clone all loop-unswitch related loop properties. |
| 120 | // Redistribute unswitching quotas. |
| 121 | // Note, that new loop data is stored inside the VMap. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 122 | void cloneData(const Loop *NewLoop, const Loop *OldLoop, |
| 123 | const ValueToValueMapTy &VMap); |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 124 | }; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 125 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 126 | class LoopUnswitch : public LoopPass { |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 127 | LoopInfo *LI; // Loop information |
Devang Patel | 1bc8936 | 2007-03-07 00:26:10 +0000 | [diff] [blame] | 128 | LPPassManager *LPM; |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 129 | |
Devang Patel | 1bc8936 | 2007-03-07 00:26:10 +0000 | [diff] [blame] | 130 | // LoopProcessWorklist - Used to check if second loop needs processing |
| 131 | // after RewriteLoopBodyWithConditionConstant rewrites first loop. |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 132 | std::vector<Loop*> LoopProcessWorklist; |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 133 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 134 | LUAnalysisCache BranchesInfo; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 135 | |
Devang Patel | 743f7e8 | 2007-06-06 00:21:03 +0000 | [diff] [blame] | 136 | bool OptimizeForSize; |
Devang Patel | 6f62af6 | 2007-07-30 23:07:10 +0000 | [diff] [blame] | 137 | bool redoLoop; |
Devang Patel | 5c4cd0d | 2007-10-05 22:29:34 +0000 | [diff] [blame] | 138 | |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 139 | Loop *currentLoop; |
Devang Patel | 5c4cd0d | 2007-10-05 22:29:34 +0000 | [diff] [blame] | 140 | DominatorTree *DT; |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 141 | BasicBlock *loopHeader; |
| 142 | BasicBlock *loopPreheader; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 143 | |
Devang Patel | 1e41f6d | 2008-07-02 01:44:29 +0000 | [diff] [blame] | 144 | // LoopBlocks contains all of the basic blocks of the loop, including the |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 145 | // preheader of the loop, the body of the loop, and the exit blocks of the |
Devang Patel | 1e41f6d | 2008-07-02 01:44:29 +0000 | [diff] [blame] | 146 | // loop, in that order. |
| 147 | std::vector<BasicBlock*> LoopBlocks; |
| 148 | // NewBlocks contained cloned copy of basic blocks from LoopBlocks. |
| 149 | std::vector<BasicBlock*> NewBlocks; |
Devang Patel | 77a0113 | 2008-07-03 17:37:52 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 151 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 152 | static char ID; // Pass ID, replacement for typeid |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 153 | explicit LoopUnswitch(bool Os = false) : |
| 154 | LoopPass(ID), OptimizeForSize(Os), redoLoop(false), |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 155 | currentLoop(nullptr), DT(nullptr), loopHeader(nullptr), |
| 156 | loopPreheader(nullptr) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 157 | initializeLoopUnswitchPass(*PassRegistry::getPassRegistry()); |
| 158 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 159 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 160 | bool runOnLoop(Loop *L, LPPassManager &LPM) override; |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 161 | bool processCurrentLoop(); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 162 | |
| 163 | /// This transformation requires natural loop information & requires that |
Chris Lattner | ea10923 | 2010-08-29 17:23:19 +0000 | [diff] [blame] | 164 | /// loop preheaders be inserted into the CFG. |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 165 | /// |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 166 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 167 | AU.addRequiredID(LoopSimplifyID); |
Chris Lattner | f4f5f4e | 2006-02-09 22:15:42 +0000 | [diff] [blame] | 168 | AU.addPreservedID(LoopSimplifyID); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 169 | AU.addRequired<LoopInfo>(); |
| 170 | AU.addPreserved<LoopInfo>(); |
Owen Anderson | 6edf399 | 2006-06-12 21:49:21 +0000 | [diff] [blame] | 171 | AU.addRequiredID(LCSSAID); |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 172 | AU.addPreservedID(LCSSAID); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 173 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Cameron Zwarich | 71132af | 2011-02-11 06:08:28 +0000 | [diff] [blame] | 174 | AU.addPreserved<ScalarEvolution>(); |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 175 | AU.addRequired<TargetTransformInfo>(); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | private: |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 179 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 180 | void releaseMemory() override { |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 181 | BranchesInfo.forgetLoop(currentLoop); |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 184 | void initLoopData() { |
| 185 | loopHeader = currentLoop->getHeader(); |
| 186 | loopPreheader = currentLoop->getLoopPreheader(); |
| 187 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 188 | |
Chris Lattner | 48a80b0 | 2008-04-21 00:25:49 +0000 | [diff] [blame] | 189 | /// Split all of the edges from inside the loop to their exit blocks. |
| 190 | /// Update the appropriate Phi nodes as we do so. |
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 191 | void SplitExitEdges(Loop *L, const SmallVectorImpl<BasicBlock *> &ExitBlocks); |
Devang Patel | 5c4cd0d | 2007-10-05 22:29:34 +0000 | [diff] [blame] | 192 | |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 193 | bool UnswitchIfProfitable(Value *LoopCond, Constant *Val); |
Chris Lattner | f4412d8 | 2006-02-18 01:27:45 +0000 | [diff] [blame] | 194 | void UnswitchTrivialCondition(Loop *L, Value *Cond, Constant *Val, |
Chris Lattner | bd28e3f | 2006-02-22 06:37:14 +0000 | [diff] [blame] | 195 | BasicBlock *ExitBlock); |
Andrew Trick | d9fc1ce | 2012-04-10 05:14:42 +0000 | [diff] [blame] | 196 | void UnswitchNontrivialCondition(Value *LIC, Constant *OnVal, Loop *L); |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 197 | |
| 198 | void RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC, |
| 199 | Constant *Val, bool isEqual); |
Devang Patel | cce624a | 2007-06-28 00:49:00 +0000 | [diff] [blame] | 200 | |
| 201 | void EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val, |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 202 | BasicBlock *TrueDest, |
Devang Patel | cce624a | 2007-06-28 00:49:00 +0000 | [diff] [blame] | 203 | BasicBlock *FalseDest, |
| 204 | Instruction *InsertPt); |
| 205 | |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 206 | void SimplifyCode(std::vector<Instruction*> &Worklist, Loop *L); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 207 | bool IsTrivialUnswitchCondition(Value *Cond, Constant **Val = nullptr, |
| 208 | BasicBlock **LoopExit = nullptr); |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 210 | }; |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 211 | } |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 212 | |
| 213 | // Analyze loop. Check its size, calculate is it possible to unswitch |
| 214 | // it. Returns true if we can unswitch this loop. |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 215 | bool LUAnalysisCache::countLoop(const Loop *L, const TargetTransformInfo &TTI) { |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 216 | |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 217 | LoopPropsMapIt PropsIt; |
| 218 | bool Inserted; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 219 | std::tie(PropsIt, Inserted) = |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 220 | LoopsProperties.insert(std::make_pair(L, LoopProperties())); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 221 | |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 222 | LoopProperties &Props = PropsIt->second; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 223 | |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 224 | if (Inserted) { |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 225 | // New loop. |
| 226 | |
| 227 | // Limit the number of instructions to avoid causing significant code |
| 228 | // expansion, and the number of basic blocks, to avoid loops with |
| 229 | // large numbers of branches which cause loop unswitching to go crazy. |
| 230 | // This is a very ad-hoc heuristic. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 231 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 232 | // FIXME: This is overly conservative because it does not take into |
| 233 | // consideration code simplification opportunities and code that can |
| 234 | // be shared by the resultant unswitched loops. |
| 235 | CodeMetrics Metrics; |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 236 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 237 | I != E; ++I) |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 238 | Metrics.analyzeBasicBlock(*I, TTI); |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 239 | |
| 240 | Props.SizeEstimation = std::min(Metrics.NumInsts, Metrics.NumBlocks * 5); |
| 241 | Props.CanBeUnswitchedCount = MaxSize / (Props.SizeEstimation); |
| 242 | MaxSize -= Props.SizeEstimation * Props.CanBeUnswitchedCount; |
James Molloy | 67ae135 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 243 | |
| 244 | if (Metrics.notDuplicatable) { |
| 245 | DEBUG(dbgs() << "NOT unswitching loop %" |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 246 | << L->getHeader()->getName() << ", contents cannot be " |
| 247 | << "duplicated!\n"); |
James Molloy | 67ae135 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 248 | return false; |
| 249 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 252 | if (!Props.CanBeUnswitchedCount) { |
| 253 | DEBUG(dbgs() << "NOT unswitching loop %" |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 254 | << L->getHeader()->getName() << ", cost too high: " |
| 255 | << L->getBlocks().size() << "\n"); |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 256 | return false; |
| 257 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 258 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 259 | // Be careful. This links are good only before new loop addition. |
| 260 | CurrentLoopProperties = &Props; |
| 261 | CurLoopInstructions = &Props.UnswitchedVals; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 262 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 263 | return true; |
| 264 | } |
| 265 | |
| 266 | // Clean all data related to given loop. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 267 | void LUAnalysisCache::forgetLoop(const Loop *L) { |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 268 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 269 | LoopPropsMapIt LIt = LoopsProperties.find(L); |
| 270 | |
| 271 | if (LIt != LoopsProperties.end()) { |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 272 | LoopProperties &Props = LIt->second; |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 273 | MaxSize += Props.CanBeUnswitchedCount * Props.SizeEstimation; |
| 274 | LoopsProperties.erase(LIt); |
| 275 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 276 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 277 | CurrentLoopProperties = nullptr; |
| 278 | CurLoopInstructions = nullptr; |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | // Mark case value as unswitched. |
| 282 | // Since SI instruction can be partly unswitched, in order to avoid |
| 283 | // extra unswitching in cloned loops keep track all unswitched values. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 284 | void LUAnalysisCache::setUnswitched(const SwitchInst *SI, const Value *V) { |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 285 | (*CurLoopInstructions)[SI].insert(V); |
| 286 | } |
| 287 | |
| 288 | // Check was this case value unswitched before or not. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 289 | bool LUAnalysisCache::isUnswitched(const SwitchInst *SI, const Value *V) { |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 290 | return (*CurLoopInstructions)[SI].count(V); |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | // Clone all loop-unswitch related loop properties. |
| 294 | // Redistribute unswitching quotas. |
| 295 | // Note, that new loop data is stored inside the VMap. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 296 | void LUAnalysisCache::cloneData(const Loop *NewLoop, const Loop *OldLoop, |
| 297 | const ValueToValueMapTy &VMap) { |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 298 | |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 299 | LoopProperties &NewLoopProps = LoopsProperties[NewLoop]; |
| 300 | LoopProperties &OldLoopProps = *CurrentLoopProperties; |
| 301 | UnswitchedValsMap &Insts = OldLoopProps.UnswitchedVals; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 302 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 303 | // Reallocate "can-be-unswitched quota" |
| 304 | |
| 305 | --OldLoopProps.CanBeUnswitchedCount; |
| 306 | unsigned Quota = OldLoopProps.CanBeUnswitchedCount; |
| 307 | NewLoopProps.CanBeUnswitchedCount = Quota / 2; |
| 308 | OldLoopProps.CanBeUnswitchedCount = Quota - Quota / 2; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 309 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 310 | NewLoopProps.SizeEstimation = OldLoopProps.SizeEstimation; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 311 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 312 | // Clone unswitched values info: |
| 313 | // for new loop switches we clone info about values that was |
| 314 | // already unswitched and has redundant successors. |
| 315 | for (UnswitchedValsIt I = Insts.begin(); I != Insts.end(); ++I) { |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 316 | const SwitchInst *OldInst = I->first; |
| 317 | Value *NewI = VMap.lookup(OldInst); |
| 318 | const SwitchInst *NewInst = cast_or_null<SwitchInst>(NewI); |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 319 | assert(NewInst && "All instructions that are in SrcBB must be in VMap."); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 320 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 321 | NewLoopProps.UnswitchedVals[NewInst] = OldLoopProps.UnswitchedVals[OldInst]; |
| 322 | } |
| 323 | } |
| 324 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 325 | char LoopUnswitch::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 326 | INITIALIZE_PASS_BEGIN(LoopUnswitch, "loop-unswitch", "Unswitch loops", |
| 327 | false, false) |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 328 | INITIALIZE_AG_DEPENDENCY(TargetTransformInfo) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 329 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
| 330 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 331 | INITIALIZE_PASS_DEPENDENCY(LCSSA) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 332 | INITIALIZE_PASS_END(LoopUnswitch, "loop-unswitch", "Unswitch loops", |
| 333 | false, false) |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 334 | |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 335 | Pass *llvm::createLoopUnswitchPass(bool Os) { |
| 336 | return new LoopUnswitch(Os); |
Devang Patel | 743f7e8 | 2007-06-06 00:21:03 +0000 | [diff] [blame] | 337 | } |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 338 | |
| 339 | /// FindLIVLoopCondition - Cond is a condition that occurs in L. If it is |
| 340 | /// invariant in the loop, or has an invariant piece, return the invariant. |
| 341 | /// Otherwise, return null. |
| 342 | static Value *FindLIVLoopCondition(Value *Cond, Loop *L, bool &Changed) { |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 343 | |
Stepan Dyatkovskiy | 88c5c42 | 2012-01-11 08:40:51 +0000 | [diff] [blame] | 344 | // We started analyze new instruction, increment scanned instructions counter. |
| 345 | ++TotalInsts; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 346 | |
Chris Lattner | 3d606bb | 2010-02-02 02:26:54 +0000 | [diff] [blame] | 347 | // We can never unswitch on vector conditions. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 348 | if (Cond->getType()->isVectorTy()) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 349 | return nullptr; |
Chris Lattner | 3d606bb | 2010-02-02 02:26:54 +0000 | [diff] [blame] | 350 | |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 351 | // Constants should be folded, not unswitched on! |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 352 | if (isa<Constant>(Cond)) return nullptr; |
Devang Patel | 558f1b8 | 2007-06-28 00:44:10 +0000 | [diff] [blame] | 353 | |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 354 | // TODO: Handle: br (VARIANT|INVARIANT). |
Devang Patel | 265ca5d | 2008-11-03 19:38:07 +0000 | [diff] [blame] | 355 | |
Dan Gohman | 0df6e09 | 2009-07-14 01:37:59 +0000 | [diff] [blame] | 356 | // Hoist simple values out. |
Dan Gohman | bdc017e | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 357 | if (L->makeLoopInvariant(Cond, Changed)) |
Dan Gohman | 0df6e09 | 2009-07-14 01:37:59 +0000 | [diff] [blame] | 358 | return Cond; |
Dan Gohman | 0df6e09 | 2009-07-14 01:37:59 +0000 | [diff] [blame] | 359 | |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 360 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Cond)) |
| 361 | if (BO->getOpcode() == Instruction::And || |
| 362 | BO->getOpcode() == Instruction::Or) { |
| 363 | // If either the left or right side is invariant, we can unswitch on this, |
| 364 | // which will cause the branch to go away in one loop and the condition to |
| 365 | // simplify in the other one. |
| 366 | if (Value *LHS = FindLIVLoopCondition(BO->getOperand(0), L, Changed)) |
| 367 | return LHS; |
| 368 | if (Value *RHS = FindLIVLoopCondition(BO->getOperand(1), L, Changed)) |
| 369 | return RHS; |
| 370 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 371 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 372 | return nullptr; |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Devang Patel | 1bc8936 | 2007-03-07 00:26:10 +0000 | [diff] [blame] | 375 | bool LoopUnswitch::runOnLoop(Loop *L, LPPassManager &LPM_Ref) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 376 | if (skipOptnoneFunction(L)) |
| 377 | return false; |
| 378 | |
Devang Patel | 1bc8936 | 2007-03-07 00:26:10 +0000 | [diff] [blame] | 379 | LI = &getAnalysis<LoopInfo>(); |
| 380 | LPM = &LPM_Ref; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 381 | DominatorTreeWrapperPass *DTWP = |
| 382 | getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 383 | DT = DTWP ? &DTWP->getDomTree() : nullptr; |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 384 | currentLoop = L; |
Devang Patel | deafefa | 2008-09-04 22:43:59 +0000 | [diff] [blame] | 385 | Function *F = currentLoop->getHeader()->getParent(); |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 386 | bool Changed = false; |
Devang Patel | 6f62af6 | 2007-07-30 23:07:10 +0000 | [diff] [blame] | 387 | do { |
Dan Gohman | bbf81d8 | 2010-03-10 19:38:49 +0000 | [diff] [blame] | 388 | assert(currentLoop->isLCSSAForm(*DT)); |
Devang Patel | 6f62af6 | 2007-07-30 23:07:10 +0000 | [diff] [blame] | 389 | redoLoop = false; |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 390 | Changed |= processCurrentLoop(); |
Devang Patel | 6f62af6 | 2007-07-30 23:07:10 +0000 | [diff] [blame] | 391 | } while(redoLoop); |
| 392 | |
Devang Patel | deafefa | 2008-09-04 22:43:59 +0000 | [diff] [blame] | 393 | if (Changed) { |
| 394 | // FIXME: Reconstruct dom info, because it is not preserved properly. |
| 395 | if (DT) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 396 | DT->recalculate(*F); |
Devang Patel | deafefa | 2008-09-04 22:43:59 +0000 | [diff] [blame] | 397 | } |
Devang Patel | 6f62af6 | 2007-07-30 23:07:10 +0000 | [diff] [blame] | 398 | return Changed; |
| 399 | } |
| 400 | |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 401 | /// processCurrentLoop - Do actual work and unswitch loop if possible |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 402 | /// and profitable. |
| 403 | bool LoopUnswitch::processCurrentLoop() { |
Devang Patel | 6f62af6 | 2007-07-30 23:07:10 +0000 | [diff] [blame] | 404 | bool Changed = false; |
Stepan Dyatkovskiy | 88c5c42 | 2012-01-11 08:40:51 +0000 | [diff] [blame] | 405 | |
| 406 | initLoopData(); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 407 | |
Stepan Dyatkovskiy | 88c5c42 | 2012-01-11 08:40:51 +0000 | [diff] [blame] | 408 | // If LoopSimplify was unable to form a preheader, don't do any unswitching. |
| 409 | if (!loopPreheader) |
| 410 | return false; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 411 | |
Andrew Trick | d9fc1ce | 2012-04-10 05:14:42 +0000 | [diff] [blame] | 412 | // Loops with indirectbr cannot be cloned. |
| 413 | if (!currentLoop->isSafeToClone()) |
| 414 | return false; |
| 415 | |
| 416 | // Without dedicated exits, splitting the exit edge may fail. |
| 417 | if (!currentLoop->hasDedicatedExits()) |
| 418 | return false; |
| 419 | |
Stepan Dyatkovskiy | 88c5c42 | 2012-01-11 08:40:51 +0000 | [diff] [blame] | 420 | LLVMContext &Context = loopHeader->getContext(); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 421 | |
Stepan Dyatkovskiy | 88c5c42 | 2012-01-11 08:40:51 +0000 | [diff] [blame] | 422 | // Probably we reach the quota of branches for this loop. If so |
| 423 | // stop unswitching. |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 424 | if (!BranchesInfo.countLoop(currentLoop, getAnalysis<TargetTransformInfo>())) |
Stepan Dyatkovskiy | 88c5c42 | 2012-01-11 08:40:51 +0000 | [diff] [blame] | 425 | return false; |
Devang Patel | 6f62af6 | 2007-07-30 23:07:10 +0000 | [diff] [blame] | 426 | |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 427 | // Loop over all of the basic blocks in the loop. If we find an interior |
| 428 | // block that is branching on a loop-invariant condition, we can unswitch this |
| 429 | // loop. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 430 | for (Loop::block_iterator I = currentLoop->block_begin(), |
Chris Lattner | 9e18580 | 2010-04-05 21:18:32 +0000 | [diff] [blame] | 431 | E = currentLoop->block_end(); I != E; ++I) { |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 432 | TerminatorInst *TI = (*I)->getTerminator(); |
| 433 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 434 | // If this isn't branching on an invariant condition, we can't unswitch |
| 435 | // it. |
| 436 | if (BI->isConditional()) { |
| 437 | // See if this, or some part of it, is loop invariant. If so, we can |
| 438 | // unswitch on it if we desire. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 439 | Value *LoopCond = FindLIVLoopCondition(BI->getCondition(), |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 440 | currentLoop, Changed); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 441 | if (LoopCond && UnswitchIfProfitable(LoopCond, |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 442 | ConstantInt::getTrue(Context))) { |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 443 | ++NumBranches; |
| 444 | return true; |
| 445 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 446 | } |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 447 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 448 | Value *LoopCond = FindLIVLoopCondition(SI->getCondition(), |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 449 | currentLoop, Changed); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 450 | unsigned NumCases = SI->getNumCases(); |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 451 | if (LoopCond && NumCases) { |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 452 | // Find a value to unswitch on: |
| 453 | // FIXME: this should chose the most expensive case! |
Nick Lewycky | 444f297 | 2011-06-03 06:27:15 +0000 | [diff] [blame] | 454 | // FIXME: scan for a case with a non-critical edge? |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 455 | Constant *UnswitchVal = nullptr; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 456 | |
Devang Patel | 5295692 | 2007-02-26 19:31:58 +0000 | [diff] [blame] | 457 | // Do not process same value again and again. |
Chad Rosier | a816bf7 | 2011-12-22 21:10:46 +0000 | [diff] [blame] | 458 | // At this point we have some cases already unswitched and |
| 459 | // some not yet unswitched. Let's find the first not yet unswitched one. |
Stepan Dyatkovskiy | 3d3abe0 | 2012-03-11 06:09:17 +0000 | [diff] [blame] | 460 | for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 461 | i != e; ++i) { |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 462 | Constant *UnswitchValCandidate = i.getCaseValue(); |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 463 | if (!BranchesInfo.isUnswitched(SI, UnswitchValCandidate)) { |
Chad Rosier | a816bf7 | 2011-12-22 21:10:46 +0000 | [diff] [blame] | 464 | UnswitchVal = UnswitchValCandidate; |
| 465 | break; |
| 466 | } |
| 467 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 468 | |
Chad Rosier | a816bf7 | 2011-12-22 21:10:46 +0000 | [diff] [blame] | 469 | if (!UnswitchVal) |
Devang Patel | 5295692 | 2007-02-26 19:31:58 +0000 | [diff] [blame] | 470 | continue; |
Devang Patel | 5295692 | 2007-02-26 19:31:58 +0000 | [diff] [blame] | 471 | |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 472 | if (UnswitchIfProfitable(LoopCond, UnswitchVal)) { |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 473 | ++NumSwitches; |
| 474 | return true; |
| 475 | } |
| 476 | } |
| 477 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 478 | |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 479 | // Scan the instructions to check for unswitchable values. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 480 | for (BasicBlock::iterator BBI = (*I)->begin(), E = (*I)->end(); |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 481 | BBI != E; ++BBI) |
| 482 | if (SelectInst *SI = dyn_cast<SelectInst>(BBI)) { |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 483 | Value *LoopCond = FindLIVLoopCondition(SI->getCondition(), |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 484 | currentLoop, Changed); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 485 | if (LoopCond && UnswitchIfProfitable(LoopCond, |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 486 | ConstantInt::getTrue(Context))) { |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 487 | ++NumSelects; |
| 488 | return true; |
| 489 | } |
| 490 | } |
| 491 | } |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 492 | return Changed; |
| 493 | } |
| 494 | |
Dan Gohman | b0a5721 | 2010-09-01 21:46:45 +0000 | [diff] [blame] | 495 | /// isTrivialLoopExitBlock - Check to see if all paths from BB exit the |
| 496 | /// loop with no side effects (including infinite loops). |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 497 | /// |
Dan Gohman | b0a5721 | 2010-09-01 21:46:45 +0000 | [diff] [blame] | 498 | /// If true, we return true and set ExitBB to the block we |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 499 | /// exit through. |
| 500 | /// |
| 501 | static bool isTrivialLoopExitBlockHelper(Loop *L, BasicBlock *BB, |
| 502 | BasicBlock *&ExitBB, |
| 503 | std::set<BasicBlock*> &Visited) { |
Chris Lattner | 0017d48 | 2006-02-17 06:39:56 +0000 | [diff] [blame] | 504 | if (!Visited.insert(BB).second) { |
Nick Lewycky | 8a5641d | 2011-12-23 23:49:25 +0000 | [diff] [blame] | 505 | // Already visited. Without more analysis, this could indicate an infinite |
| 506 | // loop. |
Dan Gohman | b0a5721 | 2010-09-01 21:46:45 +0000 | [diff] [blame] | 507 | return false; |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 508 | } |
| 509 | if (!L->contains(BB)) { |
Chris Lattner | 0017d48 | 2006-02-17 06:39:56 +0000 | [diff] [blame] | 510 | // Otherwise, this is a loop exit, this is fine so long as this is the |
| 511 | // first exit. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 512 | if (ExitBB) return false; |
Chris Lattner | 0017d48 | 2006-02-17 06:39:56 +0000 | [diff] [blame] | 513 | ExitBB = BB; |
Edward O'Callaghan | 2579843 | 2009-11-25 05:38:41 +0000 | [diff] [blame] | 514 | return true; |
Chris Lattner | 0017d48 | 2006-02-17 06:39:56 +0000 | [diff] [blame] | 515 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 516 | |
Chris Lattner | 0017d48 | 2006-02-17 06:39:56 +0000 | [diff] [blame] | 517 | // Otherwise, this is an unvisited intra-loop node. Check all successors. |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 518 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) { |
Chris Lattner | 0017d48 | 2006-02-17 06:39:56 +0000 | [diff] [blame] | 519 | // Check to see if the successor is a trivial loop exit. |
| 520 | if (!isTrivialLoopExitBlockHelper(L, *SI, ExitBB, Visited)) |
| 521 | return false; |
Chris Lattner | 708e1a5 | 2006-02-10 02:30:37 +0000 | [diff] [blame] | 522 | } |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 523 | |
| 524 | // Okay, everything after this looks good, check to make sure that this block |
| 525 | // doesn't include any side effects. |
Chris Lattner | a48654e | 2006-02-15 22:52:05 +0000 | [diff] [blame] | 526 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
Duncan Sands | 7af1c78 | 2009-05-06 06:49:50 +0000 | [diff] [blame] | 527 | if (I->mayHaveSideEffects()) |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 528 | return false; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 529 | |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 530 | return true; |
Chris Lattner | 708e1a5 | 2006-02-10 02:30:37 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 533 | /// isTrivialLoopExitBlock - Return true if the specified block unconditionally |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 534 | /// leads to an exit from the specified loop, and has no side-effects in the |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 535 | /// process. If so, return the block that is exited to, otherwise return null. |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 536 | static BasicBlock *isTrivialLoopExitBlock(Loop *L, BasicBlock *BB) { |
| 537 | std::set<BasicBlock*> Visited; |
Dan Gohman | b0a5721 | 2010-09-01 21:46:45 +0000 | [diff] [blame] | 538 | Visited.insert(L->getHeader()); // Branches to header make infinite loops. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 539 | BasicBlock *ExitBB = nullptr; |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 540 | if (isTrivialLoopExitBlockHelper(L, BB, ExitBB, Visited)) |
| 541 | return ExitBB; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 542 | return nullptr; |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 543 | } |
Chris Lattner | 708e1a5 | 2006-02-10 02:30:37 +0000 | [diff] [blame] | 544 | |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 545 | /// IsTrivialUnswitchCondition - Check to see if this unswitch condition is |
| 546 | /// trivial: that is, that the condition controls whether or not the loop does |
| 547 | /// anything at all. If this is a trivial condition, unswitching produces no |
| 548 | /// code duplications (equivalently, it produces a simpler loop and a new empty |
| 549 | /// loop, which gets deleted). |
| 550 | /// |
Chris Lattner | bd28e3f | 2006-02-22 06:37:14 +0000 | [diff] [blame] | 551 | /// If this is a trivial condition, return true, otherwise return false. When |
| 552 | /// returning true, this sets Cond and Val to the condition that controls the |
| 553 | /// trivial condition: when Cond dynamically equals Val, the loop is known to |
| 554 | /// exit. Finally, this sets LoopExit to the BB that the loop exits to when |
| 555 | /// Cond == Val. |
| 556 | /// |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 557 | bool LoopUnswitch::IsTrivialUnswitchCondition(Value *Cond, Constant **Val, |
| 558 | BasicBlock **LoopExit) { |
| 559 | BasicBlock *Header = currentLoop->getHeader(); |
Chris Lattner | a48654e | 2006-02-15 22:52:05 +0000 | [diff] [blame] | 560 | TerminatorInst *HeaderTerm = Header->getTerminator(); |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 561 | LLVMContext &Context = Header->getContext(); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 562 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 563 | BasicBlock *LoopExitBB = nullptr; |
Chris Lattner | a48654e | 2006-02-15 22:52:05 +0000 | [diff] [blame] | 564 | if (BranchInst *BI = dyn_cast<BranchInst>(HeaderTerm)) { |
| 565 | // If the header block doesn't end with a conditional branch on Cond, we |
| 566 | // can't handle it. |
| 567 | if (!BI->isConditional() || BI->getCondition() != Cond) |
| 568 | return false; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 569 | |
| 570 | // Check to see if a successor of the branch is guaranteed to |
| 571 | // exit through a unique exit block without having any |
Chris Lattner | a48654e | 2006-02-15 22:52:05 +0000 | [diff] [blame] | 572 | // side-effects. If so, determine the value of Cond that causes it to do |
| 573 | // this. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 574 | if ((LoopExitBB = isTrivialLoopExitBlock(currentLoop, |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 575 | BI->getSuccessor(0)))) { |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 576 | if (Val) *Val = ConstantInt::getTrue(Context); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 577 | } else if ((LoopExitBB = isTrivialLoopExitBlock(currentLoop, |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 578 | BI->getSuccessor(1)))) { |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 579 | if (Val) *Val = ConstantInt::getFalse(Context); |
Chris Lattner | a48654e | 2006-02-15 22:52:05 +0000 | [diff] [blame] | 580 | } |
| 581 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(HeaderTerm)) { |
| 582 | // If this isn't a switch on Cond, we can't handle it. |
| 583 | if (SI->getCondition() != Cond) return false; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 584 | |
Chris Lattner | a48654e | 2006-02-15 22:52:05 +0000 | [diff] [blame] | 585 | // Check to see if a successor of the switch is guaranteed to go to the |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 586 | // latch block or exit through a one exit block without having any |
Chris Lattner | a48654e | 2006-02-15 22:52:05 +0000 | [diff] [blame] | 587 | // side-effects. If so, determine the value of Cond that causes it to do |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 588 | // this. |
Chad Rosier | a816bf7 | 2011-12-22 21:10:46 +0000 | [diff] [blame] | 589 | // Note that we can't trivially unswitch on the default case or |
| 590 | // on already unswitched cases. |
Stepan Dyatkovskiy | 3d3abe0 | 2012-03-11 06:09:17 +0000 | [diff] [blame] | 591 | for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 592 | i != e; ++i) { |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 593 | BasicBlock *LoopExitCandidate; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 594 | if ((LoopExitCandidate = isTrivialLoopExitBlock(currentLoop, |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 595 | i.getCaseSuccessor()))) { |
Chris Lattner | a48654e | 2006-02-15 22:52:05 +0000 | [diff] [blame] | 596 | // Okay, we found a trivial case, remember the value that is trivial. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 597 | ConstantInt *CaseVal = i.getCaseValue(); |
Chad Rosier | a816bf7 | 2011-12-22 21:10:46 +0000 | [diff] [blame] | 598 | |
| 599 | // Check that it was not unswitched before, since already unswitched |
| 600 | // trivial vals are looks trivial too. |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 601 | if (BranchesInfo.isUnswitched(SI, CaseVal)) |
Chad Rosier | a816bf7 | 2011-12-22 21:10:46 +0000 | [diff] [blame] | 602 | continue; |
| 603 | LoopExitBB = LoopExitCandidate; |
| 604 | if (Val) *Val = CaseVal; |
Chris Lattner | a48654e | 2006-02-15 22:52:05 +0000 | [diff] [blame] | 605 | break; |
| 606 | } |
Chad Rosier | a816bf7 | 2011-12-22 21:10:46 +0000 | [diff] [blame] | 607 | } |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Chris Lattner | f8bf116 | 2006-02-22 23:55:00 +0000 | [diff] [blame] | 610 | // If we didn't find a single unique LoopExit block, or if the loop exit block |
| 611 | // contains phi nodes, this isn't trivial. |
| 612 | if (!LoopExitBB || isa<PHINode>(LoopExitBB->begin())) |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 613 | return false; // Can't handle this. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 614 | |
Chris Lattner | a48654e | 2006-02-15 22:52:05 +0000 | [diff] [blame] | 615 | if (LoopExit) *LoopExit = LoopExitBB; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 616 | |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 617 | // We already know that nothing uses any scalar values defined inside of this |
| 618 | // loop. As such, we just have to check to see if this loop will execute any |
| 619 | // side-effecting instructions (e.g. stores, calls, volatile loads) in the |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 620 | // part of the loop that the code *would* execute. We already checked the |
| 621 | // tail, check the header now. |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 622 | for (BasicBlock::iterator I = Header->begin(), E = Header->end(); I != E; ++I) |
Duncan Sands | 7af1c78 | 2009-05-06 06:49:50 +0000 | [diff] [blame] | 623 | if (I->mayHaveSideEffects()) |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 624 | return false; |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 625 | return true; |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 628 | /// UnswitchIfProfitable - We have found that we can unswitch currentLoop when |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 629 | /// LoopCond == Val to simplify the loop. If we decide that this is profitable, |
| 630 | /// unswitch the loop, reprocess the pieces, then return true. |
Chris Lattner | 3d606bb | 2010-02-02 02:26:54 +0000 | [diff] [blame] | 631 | bool LoopUnswitch::UnswitchIfProfitable(Value *LoopCond, Constant *Val) { |
Dan Gohman | f68d0c1 | 2009-12-09 22:55:01 +0000 | [diff] [blame] | 632 | Function *F = loopHeader->getParent(); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 633 | Constant *CondVal = nullptr; |
| 634 | BasicBlock *ExitBlock = nullptr; |
Bill Wendling | 6aa3327 | 2012-04-30 09:23:48 +0000 | [diff] [blame] | 635 | |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 636 | if (IsTrivialUnswitchCondition(LoopCond, &CondVal, &ExitBlock)) { |
Evan Cheng | 0272024 | 2010-04-03 02:23:43 +0000 | [diff] [blame] | 637 | // If the condition is trivial, always unswitch. There is no code growth |
| 638 | // for this case. |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 639 | UnswitchTrivialCondition(currentLoop, LoopCond, CondVal, ExitBlock); |
Evan Cheng | 0272024 | 2010-04-03 02:23:43 +0000 | [diff] [blame] | 640 | return true; |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 641 | } |
Devang Patel | 4be7d29 | 2008-07-03 06:48:21 +0000 | [diff] [blame] | 642 | |
Evan Cheng | 0272024 | 2010-04-03 02:23:43 +0000 | [diff] [blame] | 643 | // Check to see if it would be profitable to unswitch current loop. |
| 644 | |
| 645 | // Do not do non-trivial unswitch while optimizing for size. |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 646 | if (OptimizeForSize || |
Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 647 | F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 648 | Attribute::OptimizeForSize)) |
Evan Cheng | 0272024 | 2010-04-03 02:23:43 +0000 | [diff] [blame] | 649 | return false; |
| 650 | |
Andrew Trick | d9fc1ce | 2012-04-10 05:14:42 +0000 | [diff] [blame] | 651 | UnswitchNontrivialCondition(LoopCond, Val, currentLoop); |
| 652 | return true; |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 655 | /// CloneLoop - Recursively clone the specified loop and all of its children, |
| 656 | /// mapping the blocks with the specified map. |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 657 | static Loop *CloneLoop(Loop *L, Loop *PL, ValueToValueMapTy &VM, |
Devang Patel | 1bc8936 | 2007-03-07 00:26:10 +0000 | [diff] [blame] | 658 | LoopInfo *LI, LPPassManager *LPM) { |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 659 | Loop *New = new Loop(); |
Devang Patel | 1bc8936 | 2007-03-07 00:26:10 +0000 | [diff] [blame] | 660 | LPM->insertLoop(New, PL); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 661 | |
| 662 | // Add all of the blocks in L to the new loop. |
| 663 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
| 664 | I != E; ++I) |
| 665 | if (LI->getLoopFor(*I) == L) |
Owen Anderson | d735ee8 | 2007-11-27 03:43:35 +0000 | [diff] [blame] | 666 | New->addBasicBlockToLoop(cast<BasicBlock>(VM[*I]), LI->getBase()); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 667 | |
| 668 | // Add all of the subloops to the new loop. |
| 669 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
Devang Patel | 1bc8936 | 2007-03-07 00:26:10 +0000 | [diff] [blame] | 670 | CloneLoop(*I, New, VM, LI, LPM); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 671 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 672 | return New; |
| 673 | } |
| 674 | |
Chris Lattner | fed5d9d | 2006-02-15 00:07:43 +0000 | [diff] [blame] | 675 | /// EmitPreheaderBranchOnCondition - Emit a conditional branch on two values |
| 676 | /// if LIC == Val, branch to TrueDst, otherwise branch to FalseDest. Insert the |
| 677 | /// code immediately before InsertPt. |
Devang Patel | cce624a | 2007-06-28 00:49:00 +0000 | [diff] [blame] | 678 | void LoopUnswitch::EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val, |
| 679 | BasicBlock *TrueDest, |
| 680 | BasicBlock *FalseDest, |
| 681 | Instruction *InsertPt) { |
Chris Lattner | fed5d9d | 2006-02-15 00:07:43 +0000 | [diff] [blame] | 682 | // Insert a conditional branch on LIC to the two preheaders. The original |
| 683 | // code is the true version and the new code is the false version. |
| 684 | Value *BranchVal = LIC; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 685 | if (!isa<ConstantInt>(Val) || |
| 686 | Val->getType() != Type::getInt1Ty(LIC->getContext())) |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 687 | BranchVal = new ICmpInst(InsertPt, ICmpInst::ICMP_EQ, LIC, Val); |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 688 | else if (Val != ConstantInt::getTrue(Val->getContext())) |
Chris Lattner | fed5d9d | 2006-02-15 00:07:43 +0000 | [diff] [blame] | 689 | // We want to enter the new loop when the condition is true. |
| 690 | std::swap(TrueDest, FalseDest); |
Chris Lattner | fed5d9d | 2006-02-15 00:07:43 +0000 | [diff] [blame] | 691 | |
| 692 | // Insert the new branch. |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 693 | BranchInst *BI = BranchInst::Create(TrueDest, FalseDest, BranchVal, InsertPt); |
| 694 | |
| 695 | // If either edge is critical, split it. This helps preserve LoopSimplify |
| 696 | // form for enclosing loops. |
Bill Wendling | bfbab99 | 2012-04-30 10:44:54 +0000 | [diff] [blame] | 697 | SplitCriticalEdge(BI, 0, this, false, false, true); |
| 698 | SplitCriticalEdge(BI, 1, this, false, false, true); |
Chris Lattner | fed5d9d | 2006-02-15 00:07:43 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 701 | /// UnswitchTrivialCondition - Given a loop that has a trivial unswitchable |
| 702 | /// condition in it (a cond branch from its header block to its latch block, |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 703 | /// where the path through the loop that doesn't execute its body has no |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 704 | /// side-effects), unswitch it. This doesn't involve any code duplication, just |
| 705 | /// moving the conditional branch outside of the loop and updating loop info. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 706 | void LoopUnswitch::UnswitchTrivialCondition(Loop *L, Value *Cond, |
| 707 | Constant *Val, |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 708 | BasicBlock *ExitBlock) { |
David Greene | d4c56fb | 2010-01-05 01:27:04 +0000 | [diff] [blame] | 709 | DEBUG(dbgs() << "loop-unswitch: Trivial-Unswitch loop %" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 710 | << loopHeader->getName() << " [" << L->getBlocks().size() |
| 711 | << " blocks] in Function " << L->getHeader()->getParent()->getName() |
| 712 | << " on cond: " << *Val << " == " << *Cond << "\n"); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 713 | |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 714 | // First step, split the preheader, so that we know that there is a safe place |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 715 | // to insert the conditional branch. We will change loopPreheader to have a |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 716 | // conditional branch on Cond. |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 717 | BasicBlock *NewPH = SplitEdge(loopPreheader, loopHeader, this); |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 718 | |
| 719 | // Now that we have a place to insert the conditional branch, create a place |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 720 | // to branch to: this is the exit block out of the loop that we should |
| 721 | // short-circuit to. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 722 | |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 723 | // Split this block now, so that the loop maintains its exit block, and so |
| 724 | // that the jump from the preheader can execute the contents of the exit block |
| 725 | // without actually branching to it (the exit block should be dominated by the |
| 726 | // loop header, not the preheader). |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 727 | assert(!L->contains(ExitBlock) && "Exit block is in the loop?"); |
Devang Patel | 05c1dc6 | 2007-07-06 22:03:47 +0000 | [diff] [blame] | 728 | BasicBlock *NewExit = SplitBlock(ExitBlock, ExitBlock->begin(), this); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 729 | |
| 730 | // Okay, now we have a position to branch from and a position to branch to, |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 731 | // insert the new conditional branch. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 732 | EmitPreheaderBranchOnCondition(Cond, Val, NewExit, NewPH, |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 733 | loopPreheader->getTerminator()); |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 734 | LPM->deleteSimpleAnalysisValue(loopPreheader->getTerminator(), L); |
| 735 | loopPreheader->getTerminator()->eraseFromParent(); |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 736 | |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 737 | // We need to reprocess this loop, it could be unswitched again. |
Devang Patel | 6f62af6 | 2007-07-30 23:07:10 +0000 | [diff] [blame] | 738 | redoLoop = true; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 739 | |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 740 | // Now that we know that the loop is never entered when this condition is a |
| 741 | // particular value, rewrite the loop with this info. We know that this will |
| 742 | // at least eliminate the old branch. |
Chris Lattner | bd28e3f | 2006-02-22 06:37:14 +0000 | [diff] [blame] | 743 | RewriteLoopBodyWithConditionConstant(L, Cond, Val, false); |
Chris Lattner | 3dd4c40 | 2006-02-14 01:01:41 +0000 | [diff] [blame] | 744 | ++NumTrivial; |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Chris Lattner | 48a80b0 | 2008-04-21 00:25:49 +0000 | [diff] [blame] | 747 | /// SplitExitEdges - Split all of the edges from inside the loop to their exit |
| 748 | /// blocks. Update the appropriate Phi nodes as we do so. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 749 | void LoopUnswitch::SplitExitEdges(Loop *L, |
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 750 | const SmallVectorImpl<BasicBlock *> &ExitBlocks){ |
Devang Patel | 5c4cd0d | 2007-10-05 22:29:34 +0000 | [diff] [blame] | 751 | |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 752 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 753 | BasicBlock *ExitBlock = ExitBlocks[i]; |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 754 | SmallVector<BasicBlock *, 4> Preds(pred_begin(ExitBlock), |
| 755 | pred_end(ExitBlock)); |
Bill Wendling | d552098 | 2011-09-27 00:59:31 +0000 | [diff] [blame] | 756 | |
Nick Lewycky | 444f297 | 2011-06-03 06:27:15 +0000 | [diff] [blame] | 757 | // Although SplitBlockPredecessors doesn't preserve loop-simplify in |
| 758 | // general, if we call it on all predecessors of all exits then it does. |
Bill Wendling | d552098 | 2011-09-27 00:59:31 +0000 | [diff] [blame] | 759 | if (!ExitBlock->isLandingPad()) { |
Jakub Staszak | 2fac1d5 | 2011-12-09 21:19:53 +0000 | [diff] [blame] | 760 | SplitBlockPredecessors(ExitBlock, Preds, ".us-lcssa", this); |
Bill Wendling | d552098 | 2011-09-27 00:59:31 +0000 | [diff] [blame] | 761 | } else { |
| 762 | SmallVector<BasicBlock*, 2> NewBBs; |
| 763 | SplitLandingPadPredecessors(ExitBlock, Preds, ".us-lcssa", ".us-lcssa", |
| 764 | this, NewBBs); |
| 765 | } |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 766 | } |
Devang Patel | f476e8e | 2007-10-03 21:16:08 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 769 | /// UnswitchNontrivialCondition - We determined that the loop is profitable |
| 770 | /// to unswitch when LIC equal Val. Split it into loop versions and test the |
Devang Patel | c1e2660 | 2007-10-03 21:17:43 +0000 | [diff] [blame] | 771 | /// condition outside of either loop. Return the loops created as Out1/Out2. |
Andrew Trick | d9fc1ce | 2012-04-10 05:14:42 +0000 | [diff] [blame] | 772 | void LoopUnswitch::UnswitchNontrivialCondition(Value *LIC, Constant *Val, |
Devang Patel | f476e8e | 2007-10-03 21:16:08 +0000 | [diff] [blame] | 773 | Loop *L) { |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 774 | Function *F = loopHeader->getParent(); |
David Greene | d4c56fb | 2010-01-05 01:27:04 +0000 | [diff] [blame] | 775 | DEBUG(dbgs() << "loop-unswitch: Unswitching loop %" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 776 | << loopHeader->getName() << " [" << L->getBlocks().size() |
| 777 | << " blocks] in Function " << F->getName() |
| 778 | << " when '" << *Val << "' == " << *LIC << "\n"); |
Devang Patel | f476e8e | 2007-10-03 21:16:08 +0000 | [diff] [blame] | 779 | |
Cameron Zwarich | 71132af | 2011-02-11 06:08:28 +0000 | [diff] [blame] | 780 | if (ScalarEvolution *SE = getAnalysisIfAvailable<ScalarEvolution>()) |
| 781 | SE->forgetLoop(L); |
| 782 | |
Devang Patel | 1e41f6d | 2008-07-02 01:44:29 +0000 | [diff] [blame] | 783 | LoopBlocks.clear(); |
| 784 | NewBlocks.clear(); |
Devang Patel | f476e8e | 2007-10-03 21:16:08 +0000 | [diff] [blame] | 785 | |
| 786 | // First step, split the preheader and exit blocks, and add these blocks to |
| 787 | // the LoopBlocks list. |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 788 | BasicBlock *NewPreheader = SplitEdge(loopPreheader, loopHeader, this); |
Devang Patel | f476e8e | 2007-10-03 21:16:08 +0000 | [diff] [blame] | 789 | LoopBlocks.push_back(NewPreheader); |
| 790 | |
| 791 | // We want the loop to come after the preheader, but before the exit blocks. |
| 792 | LoopBlocks.insert(LoopBlocks.end(), L->block_begin(), L->block_end()); |
| 793 | |
| 794 | SmallVector<BasicBlock*, 8> ExitBlocks; |
| 795 | L->getUniqueExitBlocks(ExitBlocks); |
| 796 | |
| 797 | // Split all of the edges from inside the loop to their exit blocks. Update |
| 798 | // the appropriate Phi nodes as we do so. |
Devang Patel | 77a0113 | 2008-07-03 17:37:52 +0000 | [diff] [blame] | 799 | SplitExitEdges(L, ExitBlocks); |
Devang Patel | f476e8e | 2007-10-03 21:16:08 +0000 | [diff] [blame] | 800 | |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 801 | // The exit blocks may have been changed due to edge splitting, recompute. |
| 802 | ExitBlocks.clear(); |
Devang Patel | 4b8f36f | 2006-08-29 22:29:16 +0000 | [diff] [blame] | 803 | L->getUniqueExitBlocks(ExitBlocks); |
| 804 | |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 805 | // Add exit blocks to the loop blocks. |
| 806 | LoopBlocks.insert(LoopBlocks.end(), ExitBlocks.begin(), ExitBlocks.end()); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 807 | |
| 808 | // Next step, clone all of the basic blocks that make up the loop (including |
| 809 | // the loop preheader and exit blocks), keeping track of the mapping between |
| 810 | // the instructions and blocks. |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 811 | NewBlocks.reserve(LoopBlocks.size()); |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 812 | ValueToValueMapTy VMap; |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 813 | for (unsigned i = 0, e = LoopBlocks.size(); i != e; ++i) { |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 814 | BasicBlock *NewBB = CloneBasicBlock(LoopBlocks[i], VMap, ".us", F); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 815 | |
Evan Cheng | 0f1666b | 2010-04-05 21:16:25 +0000 | [diff] [blame] | 816 | NewBlocks.push_back(NewBB); |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 817 | VMap[LoopBlocks[i]] = NewBB; // Keep the BB mapping. |
Evan Cheng | 0f1666b | 2010-04-05 21:16:25 +0000 | [diff] [blame] | 818 | LPM->cloneBasicBlockSimpleAnalysis(LoopBlocks[i], NewBB, L); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | // Splice the newly inserted blocks into the function right before the |
| 822 | // original preheader. |
Evan Cheng | 0f1666b | 2010-04-05 21:16:25 +0000 | [diff] [blame] | 823 | F->getBasicBlockList().splice(NewPreheader, F->getBasicBlockList(), |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 824 | NewBlocks[0], F->end()); |
| 825 | |
| 826 | // Now we create the new Loop object for the versioned loop. |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 827 | Loop *NewLoop = CloneLoop(L, L->getParentLoop(), VMap, LI, LPM); |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 828 | |
| 829 | // Recalculate unswitching quota, inherit simplified switches info for NewBB, |
| 830 | // Probably clone more loop-unswitch related loop properties. |
| 831 | BranchesInfo.cloneData(NewLoop, L, VMap); |
| 832 | |
Chris Lattner | e825593 | 2006-02-10 23:26:14 +0000 | [diff] [blame] | 833 | Loop *ParentLoop = L->getParentLoop(); |
| 834 | if (ParentLoop) { |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 835 | // Make sure to add the cloned preheader and exit blocks to the parent loop |
| 836 | // as well. |
Owen Anderson | d735ee8 | 2007-11-27 03:43:35 +0000 | [diff] [blame] | 837 | ParentLoop->addBasicBlockToLoop(NewBlocks[0], LI->getBase()); |
Chris Lattner | e825593 | 2006-02-10 23:26:14 +0000 | [diff] [blame] | 838 | } |
Bill Wendling | d552098 | 2011-09-27 00:59:31 +0000 | [diff] [blame] | 839 | |
Chris Lattner | e825593 | 2006-02-10 23:26:14 +0000 | [diff] [blame] | 840 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 841 | BasicBlock *NewExit = cast<BasicBlock>(VMap[ExitBlocks[i]]); |
Chris Lattner | 25cae0f | 2006-02-18 00:55:32 +0000 | [diff] [blame] | 842 | // The new exit block should be in the same loop as the old one. |
| 843 | if (Loop *ExitBBLoop = LI->getLoopFor(ExitBlocks[i])) |
Owen Anderson | d735ee8 | 2007-11-27 03:43:35 +0000 | [diff] [blame] | 844 | ExitBBLoop->addBasicBlockToLoop(NewExit, LI->getBase()); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 845 | |
Chris Lattner | e825593 | 2006-02-10 23:26:14 +0000 | [diff] [blame] | 846 | assert(NewExit->getTerminator()->getNumSuccessors() == 1 && |
| 847 | "Exit block should have been split to have one successor!"); |
| 848 | BasicBlock *ExitSucc = NewExit->getTerminator()->getSuccessor(0); |
Devang Patel | 77a0113 | 2008-07-03 17:37:52 +0000 | [diff] [blame] | 849 | |
Chris Lattner | e825593 | 2006-02-10 23:26:14 +0000 | [diff] [blame] | 850 | // If the successor of the exit block had PHI nodes, add an entry for |
| 851 | // NewExit. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 852 | for (BasicBlock::iterator I = ExitSucc->begin(); |
| 853 | PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
Chris Lattner | e825593 | 2006-02-10 23:26:14 +0000 | [diff] [blame] | 854 | Value *V = PN->getIncomingValueForBlock(ExitBlocks[i]); |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 855 | ValueToValueMapTy::iterator It = VMap.find(V); |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 856 | if (It != VMap.end()) V = It->second; |
Chris Lattner | e825593 | 2006-02-10 23:26:14 +0000 | [diff] [blame] | 857 | PN->addIncoming(V, NewExit); |
| 858 | } |
Bill Wendling | d552098 | 2011-09-27 00:59:31 +0000 | [diff] [blame] | 859 | |
| 860 | if (LandingPadInst *LPad = NewExit->getLandingPadInst()) { |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 861 | PHINode *PN = PHINode::Create(LPad->getType(), 0, "", |
| 862 | ExitSucc->getFirstInsertionPt()); |
Bill Wendling | d552098 | 2011-09-27 00:59:31 +0000 | [diff] [blame] | 863 | |
| 864 | for (pred_iterator I = pred_begin(ExitSucc), E = pred_end(ExitSucc); |
| 865 | I != E; ++I) { |
| 866 | BasicBlock *BB = *I; |
| 867 | LandingPadInst *LPI = BB->getLandingPadInst(); |
| 868 | LPI->replaceAllUsesWith(PN); |
| 869 | PN->addIncoming(LPI, BB); |
| 870 | } |
| 871 | } |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | // Rewrite the code to refer to itself. |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 875 | for (unsigned i = 0, e = NewBlocks.size(); i != e; ++i) |
| 876 | for (BasicBlock::iterator I = NewBlocks[i]->begin(), |
| 877 | E = NewBlocks[i]->end(); I != E; ++I) |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 878 | RemapInstruction(I, VMap,RF_NoModuleLevelChanges|RF_IgnoreMissingEntries); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 879 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 880 | // Rewrite the original preheader to select between versions of the loop. |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 881 | BranchInst *OldBR = cast<BranchInst>(loopPreheader->getTerminator()); |
Chris Lattner | fed5d9d | 2006-02-15 00:07:43 +0000 | [diff] [blame] | 882 | assert(OldBR->isUnconditional() && OldBR->getSuccessor(0) == LoopBlocks[0] && |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 883 | "Preheader splitting did not work correctly!"); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 884 | |
Chris Lattner | fed5d9d | 2006-02-15 00:07:43 +0000 | [diff] [blame] | 885 | // Emit the new branch that selects between the two versions of this loop. |
| 886 | EmitPreheaderBranchOnCondition(LIC, Val, NewBlocks[0], LoopBlocks[0], OldBR); |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 887 | LPM->deleteSimpleAnalysisValue(OldBR, L); |
Devang Patel | 9ee49c5 | 2007-09-20 23:45:50 +0000 | [diff] [blame] | 888 | OldBR->eraseFromParent(); |
Devang Patel | 1ff6138 | 2007-08-02 15:25:57 +0000 | [diff] [blame] | 889 | |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 890 | LoopProcessWorklist.push_back(NewLoop); |
Devang Patel | 6f62af6 | 2007-07-30 23:07:10 +0000 | [diff] [blame] | 891 | redoLoop = true; |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 892 | |
Chris Lattner | a78130c | 2010-04-20 05:09:16 +0000 | [diff] [blame] | 893 | // Keep a WeakVH holding onto LIC. If the first call to RewriteLoopBody |
| 894 | // deletes the instruction (for example by simplifying a PHI that feeds into |
| 895 | // the condition that we're unswitching on), we don't rewrite the second |
| 896 | // iteration. |
| 897 | WeakVH LICHandle(LIC); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 898 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 899 | // Now we rewrite the original code to know that the condition is true and the |
| 900 | // new code to know that the condition is false. |
Evan Cheng | 0f1666b | 2010-04-05 21:16:25 +0000 | [diff] [blame] | 901 | RewriteLoopBodyWithConditionConstant(L, LIC, Val, false); |
Devang Patel | 77a0113 | 2008-07-03 17:37:52 +0000 | [diff] [blame] | 902 | |
Chris Lattner | a78130c | 2010-04-20 05:09:16 +0000 | [diff] [blame] | 903 | // It's possible that simplifying one loop could cause the other to be |
| 904 | // changed to another value or a constant. If its a constant, don't simplify |
| 905 | // it. |
| 906 | if (!LoopProcessWorklist.empty() && LoopProcessWorklist.back() == NewLoop && |
| 907 | LICHandle && !isa<Constant>(LICHandle)) |
| 908 | RewriteLoopBodyWithConditionConstant(NewLoop, LICHandle, Val, true); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 911 | /// RemoveFromWorklist - Remove all instances of I from the worklist vector |
| 912 | /// specified. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 913 | static void RemoveFromWorklist(Instruction *I, |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 914 | std::vector<Instruction*> &Worklist) { |
Jakub Staszak | be6f884 | 2012-10-16 19:52:32 +0000 | [diff] [blame] | 915 | |
| 916 | Worklist.erase(std::remove(Worklist.begin(), Worklist.end(), I), |
| 917 | Worklist.end()); |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | /// ReplaceUsesOfWith - When we find that I really equals V, remove I from the |
| 921 | /// program, replacing all uses with V and update the worklist. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 922 | static void ReplaceUsesOfWith(Instruction *I, Value *V, |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 923 | std::vector<Instruction*> &Worklist, |
| 924 | Loop *L, LPPassManager *LPM) { |
David Greene | d4c56fb | 2010-01-05 01:27:04 +0000 | [diff] [blame] | 925 | DEBUG(dbgs() << "Replace with '" << *V << "': " << *I); |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 926 | |
| 927 | // Add uses to the worklist, which may be dead now. |
| 928 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 929 | if (Instruction *Use = dyn_cast<Instruction>(I->getOperand(i))) |
| 930 | Worklist.push_back(Use); |
| 931 | |
| 932 | // Add users to the worklist which may be simplified now. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 933 | for (User *U : I->users()) |
| 934 | Worklist.push_back(cast<Instruction>(U)); |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 935 | LPM->deleteSimpleAnalysisValue(I, L); |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 936 | RemoveFromWorklist(I, Worklist); |
Devang Patel | 9ee49c5 | 2007-09-20 23:45:50 +0000 | [diff] [blame] | 937 | I->replaceAllUsesWith(V); |
| 938 | I->eraseFromParent(); |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 939 | ++NumSimplify; |
| 940 | } |
| 941 | |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 942 | // RewriteLoopBodyWithConditionConstant - We know either that the value LIC has |
| 943 | // the value specified by Val in the specified loop, or we know it does NOT have |
| 944 | // that value. Rewrite any uses of LIC or of properties correlated to it. |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 945 | void LoopUnswitch::RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC, |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 946 | Constant *Val, |
| 947 | bool IsEqual) { |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 948 | assert(!isa<Constant>(LIC) && "Why are we unswitching on a constant?"); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 949 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 950 | // FIXME: Support correlated properties, like: |
| 951 | // for (...) |
| 952 | // if (li1 < li2) |
| 953 | // ... |
| 954 | // if (li1 > li2) |
| 955 | // ... |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 956 | |
Chris Lattner | 708e1a5 | 2006-02-10 02:30:37 +0000 | [diff] [blame] | 957 | // FOLD boolean conditions (X|LIC), (X&LIC). Fold conditional branches, |
| 958 | // selects, switches. |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 959 | std::vector<Instruction*> Worklist; |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 960 | LLVMContext &Context = Val->getContext(); |
| 961 | |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 962 | // If we know that LIC == Val, or that LIC == NotVal, just replace uses of LIC |
| 963 | // in the loop with the appropriate one directly. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 964 | if (IsEqual || (isa<ConstantInt>(Val) && |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 965 | Val->getType()->isIntegerTy(1))) { |
Chris Lattner | bd28e3f | 2006-02-22 06:37:14 +0000 | [diff] [blame] | 966 | Value *Replacement; |
| 967 | if (IsEqual) |
| 968 | Replacement = Val; |
| 969 | else |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 970 | Replacement = ConstantInt::get(Type::getInt1Ty(Val->getContext()), |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 971 | !cast<ConstantInt>(Val)->getZExtValue()); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 972 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 973 | for (User *U : LIC->users()) { |
| 974 | Instruction *UI = dyn_cast<Instruction>(U); |
| 975 | if (!UI || !L->contains(UI)) |
Evan Cheng | 424641e | 2011-05-24 23:12:57 +0000 | [diff] [blame] | 976 | continue; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 977 | Worklist.push_back(UI); |
Evan Cheng | 424641e | 2011-05-24 23:12:57 +0000 | [diff] [blame] | 978 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 979 | |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 980 | for (std::vector<Instruction*>::iterator UI = Worklist.begin(), |
| 981 | UE = Worklist.end(); UI != UE; ++UI) |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 982 | (*UI)->replaceUsesOfWith(LIC, Replacement); |
| 983 | |
Chris Lattner | 9e18580 | 2010-04-05 21:18:32 +0000 | [diff] [blame] | 984 | SimplifyCode(Worklist, L); |
| 985 | return; |
| 986 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 987 | |
Chris Lattner | 9e18580 | 2010-04-05 21:18:32 +0000 | [diff] [blame] | 988 | // Otherwise, we don't know the precise value of LIC, but we do know that it |
| 989 | // is certainly NOT "Val". As such, simplify any uses in the loop that we |
| 990 | // can. This case occurs when we unswitch switch statements. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 991 | for (User *U : LIC->users()) { |
| 992 | Instruction *UI = dyn_cast<Instruction>(U); |
| 993 | if (!UI || !L->contains(UI)) |
Chris Lattner | 9e18580 | 2010-04-05 21:18:32 +0000 | [diff] [blame] | 994 | continue; |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 995 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 996 | Worklist.push_back(UI); |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 997 | |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 998 | // TODO: We could do other simplifications, for example, turning |
Chris Lattner | 9e18580 | 2010-04-05 21:18:32 +0000 | [diff] [blame] | 999 | // 'icmp eq LIC, Val' -> false. |
| 1000 | |
| 1001 | // If we know that LIC is not Val, use this info to simplify code. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1002 | SwitchInst *SI = dyn_cast<SwitchInst>(UI); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1003 | if (!SI || !isa<ConstantInt>(Val)) continue; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1004 | |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 1005 | SwitchInst::CaseIt DeadCase = SI->findCaseValue(cast<ConstantInt>(Val)); |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 1006 | // Default case is live for multiple values. |
Stepan Dyatkovskiy | 3d3abe0 | 2012-03-11 06:09:17 +0000 | [diff] [blame] | 1007 | if (DeadCase == SI->case_default()) continue; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1008 | |
| 1009 | // Found a dead case value. Don't remove PHI nodes in the |
Chris Lattner | 9e18580 | 2010-04-05 21:18:32 +0000 | [diff] [blame] | 1010 | // successor if they become single-entry, those PHI nodes may |
| 1011 | // be in the Users list. |
Nick Lewycky | 444f297 | 2011-06-03 06:27:15 +0000 | [diff] [blame] | 1012 | |
Evan Cheng | 424641e | 2011-05-24 23:12:57 +0000 | [diff] [blame] | 1013 | BasicBlock *Switch = SI->getParent(); |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 1014 | BasicBlock *SISucc = DeadCase.getCaseSuccessor(); |
Evan Cheng | 424641e | 2011-05-24 23:12:57 +0000 | [diff] [blame] | 1015 | BasicBlock *Latch = L->getLoopLatch(); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1016 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 1017 | BranchesInfo.setUnswitched(SI, Val); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1018 | |
Nick Lewycky | 444f297 | 2011-06-03 06:27:15 +0000 | [diff] [blame] | 1019 | if (!SI->findCaseDest(SISucc)) continue; // Edge is critical. |
Evan Cheng | 237d10d | 2011-05-25 18:17:13 +0000 | [diff] [blame] | 1020 | // If the DeadCase successor dominates the loop latch, then the |
| 1021 | // transformation isn't safe since it will delete the sole predecessor edge |
| 1022 | // to the latch. |
| 1023 | if (Latch && DT->dominates(SISucc, Latch)) |
| 1024 | continue; |
Evan Cheng | 424641e | 2011-05-24 23:12:57 +0000 | [diff] [blame] | 1025 | |
Chris Lattner | 9e18580 | 2010-04-05 21:18:32 +0000 | [diff] [blame] | 1026 | // FIXME: This is a hack. We need to keep the successor around |
| 1027 | // and hooked up so as to preserve the loop structure, because |
| 1028 | // trying to update it is complicated. So instead we preserve the |
| 1029 | // loop structure and put the block on a dead code path. |
Evan Cheng | 424641e | 2011-05-24 23:12:57 +0000 | [diff] [blame] | 1030 | SplitEdge(Switch, SISucc, this); |
Chris Lattner | 9e18580 | 2010-04-05 21:18:32 +0000 | [diff] [blame] | 1031 | // Compute the successors instead of relying on the return value |
| 1032 | // of SplitEdge, since it may have split the switch successor |
| 1033 | // after PHI nodes. |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 1034 | BasicBlock *NewSISucc = DeadCase.getCaseSuccessor(); |
Chris Lattner | 9e18580 | 2010-04-05 21:18:32 +0000 | [diff] [blame] | 1035 | BasicBlock *OldSISucc = *succ_begin(NewSISucc); |
| 1036 | // Create an "unreachable" destination. |
| 1037 | BasicBlock *Abort = BasicBlock::Create(Context, "us-unreachable", |
| 1038 | Switch->getParent(), |
| 1039 | OldSISucc); |
| 1040 | new UnreachableInst(Context, Abort); |
| 1041 | // Force the new case destination to branch to the "unreachable" |
| 1042 | // block while maintaining a (dead) CFG edge to the old block. |
| 1043 | NewSISucc->getTerminator()->eraseFromParent(); |
| 1044 | BranchInst::Create(Abort, OldSISucc, |
| 1045 | ConstantInt::getTrue(Context), NewSISucc); |
| 1046 | // Release the PHI operands for this edge. |
| 1047 | for (BasicBlock::iterator II = NewSISucc->begin(); |
| 1048 | PHINode *PN = dyn_cast<PHINode>(II); ++II) |
| 1049 | PN->setIncomingValue(PN->getBasicBlockIndex(Switch), |
| 1050 | UndefValue::get(PN->getType())); |
| 1051 | // Tell the domtree about the new block. We don't fully update the |
| 1052 | // domtree here -- instead we force it to do a full recomputation |
| 1053 | // after the pass is complete -- but we do need to inform it of |
| 1054 | // new blocks. |
| 1055 | if (DT) |
| 1056 | DT->addNewBlock(Abort, NewSISucc); |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1057 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1058 | |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 1059 | SimplifyCode(Worklist, L); |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
Mike Stump | c02f0d7 | 2009-09-09 17:57:16 +0000 | [diff] [blame] | 1062 | /// SimplifyCode - Okay, now that we have simplified some instructions in the |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 1063 | /// loop, walk over it and constant prop, dce, and fold control flow where |
| 1064 | /// possible. Note that this is effectively a very simple loop-structure-aware |
| 1065 | /// optimizer. During processing of this loop, L could very well be deleted, so |
| 1066 | /// it must not be used. |
| 1067 | /// |
| 1068 | /// FIXME: When the loop optimizer is more mature, separate this out to a new |
| 1069 | /// pass. |
| 1070 | /// |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 1071 | void LoopUnswitch::SimplifyCode(std::vector<Instruction*> &Worklist, Loop *L) { |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1072 | while (!Worklist.empty()) { |
| 1073 | Instruction *I = Worklist.back(); |
| 1074 | Worklist.pop_back(); |
Duncan Sands | 0f82763 | 2010-11-23 20:24:21 +0000 | [diff] [blame] | 1075 | |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1076 | // Simple DCE. |
| 1077 | if (isInstructionTriviallyDead(I)) { |
David Greene | d4c56fb | 2010-01-05 01:27:04 +0000 | [diff] [blame] | 1078 | DEBUG(dbgs() << "Remove dead instruction '" << *I); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1079 | |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1080 | // Add uses to the worklist, which may be dead now. |
| 1081 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 1082 | if (Instruction *Use = dyn_cast<Instruction>(I->getOperand(i))) |
| 1083 | Worklist.push_back(Use); |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 1084 | LPM->deleteSimpleAnalysisValue(I, L); |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1085 | RemoveFromWorklist(I, Worklist); |
Devang Patel | 9ee49c5 | 2007-09-20 23:45:50 +0000 | [diff] [blame] | 1086 | I->eraseFromParent(); |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1087 | ++NumSimplify; |
| 1088 | continue; |
| 1089 | } |
Duncan Sands | d0c6f3d | 2010-11-18 19:59:41 +0000 | [diff] [blame] | 1090 | |
Chris Lattner | 4f1f6f6 | 2010-04-20 05:33:18 +0000 | [diff] [blame] | 1091 | // See if instruction simplification can hack this up. This is common for |
| 1092 | // things like "select false, X, Y" after unswitching made the condition be |
Peter Collingbourne | fcaf5fa | 2012-05-20 01:32:09 +0000 | [diff] [blame] | 1093 | // 'false'. TODO: update the domtree properly so we can pass it here. |
| 1094 | if (Value *V = SimplifyInstruction(I)) |
Duncan Sands | d0c6f3d | 2010-11-18 19:59:41 +0000 | [diff] [blame] | 1095 | if (LI->replacementPreservesLCSSAForm(I, V)) { |
| 1096 | ReplaceUsesOfWith(I, V, Worklist, L, LPM); |
| 1097 | continue; |
| 1098 | } |
| 1099 | |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1100 | // Special case hacks that appear commonly in unswitched code. |
Chris Lattner | 4f1f6f6 | 2010-04-20 05:33:18 +0000 | [diff] [blame] | 1101 | if (BranchInst *BI = dyn_cast<BranchInst>(I)) { |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1102 | if (BI->isUnconditional()) { |
| 1103 | // If BI's parent is the only pred of the successor, fold the two blocks |
| 1104 | // together. |
| 1105 | BasicBlock *Pred = BI->getParent(); |
| 1106 | BasicBlock *Succ = BI->getSuccessor(0); |
| 1107 | BasicBlock *SinglePred = Succ->getSinglePredecessor(); |
| 1108 | if (!SinglePred) continue; // Nothing to do. |
| 1109 | assert(SinglePred == Pred && "CFG broken"); |
| 1110 | |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1111 | DEBUG(dbgs() << "Merging blocks: " << Pred->getName() << " <- " |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 1112 | << Succ->getName() << "\n"); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1113 | |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1114 | // Resolve any single entry PHI nodes in Succ. |
| 1115 | while (PHINode *PN = dyn_cast<PHINode>(Succ->begin())) |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 1116 | ReplaceUsesOfWith(PN, PN->getIncomingValue(0), Worklist, L, LPM); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1117 | |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 1118 | // If Succ has any successors with PHI nodes, update them to have |
| 1119 | // entries coming from Pred instead of Succ. |
| 1120 | Succ->replaceAllUsesWith(Pred); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1121 | |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1122 | // Move all of the successor contents from Succ to Pred. |
| 1123 | Pred->getInstList().splice(BI, Succ->getInstList(), Succ->begin(), |
| 1124 | Succ->end()); |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 1125 | LPM->deleteSimpleAnalysisValue(BI, L); |
Devang Patel | 9ee49c5 | 2007-09-20 23:45:50 +0000 | [diff] [blame] | 1126 | BI->eraseFromParent(); |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1127 | RemoveFromWorklist(BI, Worklist); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1128 | |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1129 | // Remove Succ from the loop tree. |
| 1130 | LI->removeBlock(Succ); |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 1131 | LPM->deleteSimpleAnalysisValue(Succ, L); |
Devang Patel | 9ee49c5 | 2007-09-20 23:45:50 +0000 | [diff] [blame] | 1132 | Succ->eraseFromParent(); |
Chris Lattner | f4412d8 | 2006-02-18 01:27:45 +0000 | [diff] [blame] | 1133 | ++NumSimplify; |
Chris Lattner | 4f1f6f6 | 2010-04-20 05:33:18 +0000 | [diff] [blame] | 1134 | continue; |
Chris Lattner | 9e18580 | 2010-04-05 21:18:32 +0000 | [diff] [blame] | 1135 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1136 | |
Chris Lattner | 4f1f6f6 | 2010-04-20 05:33:18 +0000 | [diff] [blame] | 1137 | continue; |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1138 | } |
| 1139 | } |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 1140 | } |