blob: cbc563bd8998faf04515c7dd44d6e1ba4e313897 [file] [log] [blame]
Chris Lattnerf48f7772004-04-19 18:07:02 +00001//===-- LoopUnswitch.cpp - Hoist loop-invariant conditionals in loop ------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chris Lattnerf48f7772004-04-19 18:07:02 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Chris Lattnerf48f7772004-04-19 18:07:02 +00008//===----------------------------------------------------------------------===//
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 Lattnerf48f7772004-04-19 18:07:02 +000029#include "llvm/Transforms/Scalar.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/ADT/STLExtras.h"
31#include "llvm/ADT/SmallPtrSet.h"
32#include "llvm/ADT/Statistic.h"
Chandler Carruth66b31302015-01-04 12:03:27 +000033#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Analysis/CodeMetrics.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000035#include "llvm/Analysis/InstructionSimplify.h"
36#include "llvm/Analysis/LoopInfo.h"
37#include "llvm/Analysis/LoopPass.h"
38#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruthbb9caa92013-01-21 13:04:33 +000039#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000040#include "llvm/IR/Constants.h"
41#include "llvm/IR/DerivedTypes.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000042#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000043#include "llvm/IR/Function.h"
44#include "llvm/IR/Instructions.h"
Mehdi Aminia28d91d2015-03-10 02:37:25 +000045#include "llvm/IR/Module.h"
Weiming Zhaof1abad52015-06-23 05:31:09 +000046#include "llvm/IR/MDBuilder.h"
Chris Lattner89762192006-02-09 20:15:48 +000047#include "llvm/Support/CommandLine.h"
Reid Spencer557ab152007-02-05 23:32:05 +000048#include "llvm/Support/Debug.h"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +000049#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000050#include "llvm/Transforms/Utils/BasicBlockUtils.h"
51#include "llvm/Transforms/Utils/Cloning.h"
52#include "llvm/Transforms/Utils/Local.h"
Alkis Evlogimenosa5c04ee2004-09-03 18:19:51 +000053#include <algorithm>
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +000054#include <map>
Chris Lattner2826e052006-02-09 19:14:52 +000055#include <set>
Chris Lattnerf48f7772004-04-19 18:07:02 +000056using namespace llvm;
57
Chandler Carruth964daaa2014-04-22 02:55:47 +000058#define DEBUG_TYPE "loop-unswitch"
59
Chris Lattner79a42ac2006-12-19 21:40:18 +000060STATISTIC(NumBranches, "Number of branches unswitched");
61STATISTIC(NumSwitches, "Number of switches unswitched");
62STATISTIC(NumSelects , "Number of selects unswitched");
63STATISTIC(NumTrivial , "Number of unswitches that are trivial");
64STATISTIC(NumSimplify, "Number of simplifications of unswitched code");
Stepan Dyatkovskiy82165692012-01-11 08:40:51 +000065STATISTIC(TotalInsts, "Total number of instructions analyzed");
Chris Lattner79a42ac2006-12-19 21:40:18 +000066
Stepan Dyatkovskiy2931a592012-01-16 20:48:04 +000067// The specific value of 100 here was chosen based only on intuition and a
Dan Gohman71ca6522009-10-13 17:50:43 +000068// few specific examples.
Dan Gohmand78c4002008-05-13 00:00:25 +000069static cl::opt<unsigned>
70Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"),
Stepan Dyatkovskiy82165692012-01-11 08:40:51 +000071 cl::init(100), cl::Hidden);
Andrew Trick4104ed92012-04-10 05:14:37 +000072
Dan Gohmand78c4002008-05-13 00:00:25 +000073namespace {
Andrew Trick4104ed92012-04-10 05:14:37 +000074
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +000075 class LUAnalysisCache {
76
77 typedef DenseMap<const SwitchInst*, SmallPtrSet<const Value *, 8> >
78 UnswitchedValsMap;
Andrew Trick4104ed92012-04-10 05:14:37 +000079
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +000080 typedef UnswitchedValsMap::iterator UnswitchedValsIt;
Andrew Trick4104ed92012-04-10 05:14:37 +000081
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +000082 struct LoopProperties {
83 unsigned CanBeUnswitchedCount;
Mark Heffernan9b536a62015-06-23 18:26:50 +000084 unsigned WasUnswitchedCount;
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +000085 unsigned SizeEstimation;
86 UnswitchedValsMap UnswitchedVals;
87 };
Andrew Trick4104ed92012-04-10 05:14:37 +000088
89 // Here we use std::map instead of DenseMap, since we need to keep valid
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +000090 // LoopProperties pointer for current loop for better performance.
91 typedef std::map<const Loop*, LoopProperties> LoopPropsMap;
92 typedef LoopPropsMap::iterator LoopPropsMapIt;
Andrew Trick4104ed92012-04-10 05:14:37 +000093
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +000094 LoopPropsMap LoopsProperties;
Jakub Staszak27da1232013-08-06 17:03:42 +000095 UnswitchedValsMap *CurLoopInstructions;
96 LoopProperties *CurrentLoopProperties;
Andrew Trick4104ed92012-04-10 05:14:37 +000097
Mark Heffernan9b536a62015-06-23 18:26:50 +000098 // A loop unswitching with an estimated cost above this threshold
99 // is not performed. MaxSize is turned into unswitching quota for
100 // the current loop, and reduced correspondingly, though note that
101 // the quota is returned by releaseMemory() when the loop has been
102 // processed, so that MaxSize will return to its previous
103 // value. So in most cases MaxSize will equal the Threshold flag
104 // when a new loop is processed. An exception to that is that
105 // MaxSize will have a smaller value while processing nested loops
106 // that were introduced due to loop unswitching of an outer loop.
107 //
108 // FIXME: The way that MaxSize works is subtle and depends on the
109 // pass manager processing loops and calling releaseMemory() in a
110 // specific order. It would be good to find a more straightforward
111 // way of doing what MaxSize does.
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000112 unsigned MaxSize;
Andrew Trick4104ed92012-04-10 05:14:37 +0000113
Mark Heffernan9b536a62015-06-23 18:26:50 +0000114 public:
115 LUAnalysisCache()
116 : CurLoopInstructions(nullptr), CurrentLoopProperties(nullptr),
117 MaxSize(Threshold) {}
Andrew Trick4104ed92012-04-10 05:14:37 +0000118
Mark Heffernan9b536a62015-06-23 18:26:50 +0000119 // Analyze loop. Check its size, calculate is it possible to unswitch
120 // it. Returns true if we can unswitch this loop.
121 bool countLoop(const Loop *L, const TargetTransformInfo &TTI,
122 AssumptionCache *AC);
Andrew Trick4104ed92012-04-10 05:14:37 +0000123
Mark Heffernan9b536a62015-06-23 18:26:50 +0000124 // Clean all data related to given loop.
125 void forgetLoop(const Loop *L);
Andrew Trick4104ed92012-04-10 05:14:37 +0000126
Mark Heffernan9b536a62015-06-23 18:26:50 +0000127 // Mark case value as unswitched.
128 // Since SI instruction can be partly unswitched, in order to avoid
129 // extra unswitching in cloned loops keep track all unswitched values.
130 void setUnswitched(const SwitchInst *SI, const Value *V);
Andrew Trick4104ed92012-04-10 05:14:37 +0000131
Mark Heffernan9b536a62015-06-23 18:26:50 +0000132 // Check was this case value unswitched before or not.
133 bool isUnswitched(const SwitchInst *SI, const Value *V);
Andrew Trick4104ed92012-04-10 05:14:37 +0000134
Mark Heffernan9b536a62015-06-23 18:26:50 +0000135 // Returns true if another unswitching could be done within the cost
136 // threshold.
137 bool CostAllowsUnswitching();
Andrew Trick4104ed92012-04-10 05:14:37 +0000138
Mark Heffernan9b536a62015-06-23 18:26:50 +0000139 // Clone all loop-unswitch related loop properties.
140 // Redistribute unswitching quotas.
141 // Note, that new loop data is stored inside the VMap.
142 void cloneData(const Loop *NewLoop, const Loop *OldLoop,
143 const ValueToValueMapTy &VMap);
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000144 };
Andrew Trick4104ed92012-04-10 05:14:37 +0000145
Chris Lattner2dd09db2009-09-02 06:11:42 +0000146 class LoopUnswitch : public LoopPass {
Chris Lattnerf48f7772004-04-19 18:07:02 +0000147 LoopInfo *LI; // Loop information
Devang Patel901a27d2007-03-07 00:26:10 +0000148 LPPassManager *LPM;
Chandler Carruth66b31302015-01-04 12:03:27 +0000149 AssumptionCache *AC;
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000150
Devang Patel901a27d2007-03-07 00:26:10 +0000151 // LoopProcessWorklist - Used to check if second loop needs processing
152 // after RewriteLoopBodyWithConditionConstant rewrites first loop.
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000153 std::vector<Loop*> LoopProcessWorklist;
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000154
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000155 LUAnalysisCache BranchesInfo;
Andrew Trick4104ed92012-04-10 05:14:37 +0000156
Devang Patel506310d2007-06-06 00:21:03 +0000157 bool OptimizeForSize;
Devang Patel7d165e12007-07-30 23:07:10 +0000158 bool redoLoop;
Devang Patela69f9872007-10-05 22:29:34 +0000159
Devang Patele149d4e2008-07-02 01:18:13 +0000160 Loop *currentLoop;
Devang Patela69f9872007-10-05 22:29:34 +0000161 DominatorTree *DT;
Devang Patele149d4e2008-07-02 01:18:13 +0000162 BasicBlock *loopHeader;
163 BasicBlock *loopPreheader;
Andrew Trick4104ed92012-04-10 05:14:37 +0000164
Devang Pateled50fb52008-07-02 01:44:29 +0000165 // LoopBlocks contains all of the basic blocks of the loop, including the
Andrew Trick4104ed92012-04-10 05:14:37 +0000166 // preheader of the loop, the body of the loop, and the exit blocks of the
Devang Pateled50fb52008-07-02 01:44:29 +0000167 // loop, in that order.
168 std::vector<BasicBlock*> LoopBlocks;
169 // NewBlocks contained cloned copy of basic blocks from LoopBlocks.
170 std::vector<BasicBlock*> NewBlocks;
Devang Pateleb611dd2008-07-03 17:37:52 +0000171
Chris Lattnerf48f7772004-04-19 18:07:02 +0000172 public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000173 static char ID; // Pass ID, replacement for typeid
Andrew Trick4104ed92012-04-10 05:14:37 +0000174 explicit LoopUnswitch(bool Os = false) :
175 LoopPass(ID), OptimizeForSize(Os), redoLoop(false),
Craig Topperf40110f2014-04-25 05:29:35 +0000176 currentLoop(nullptr), DT(nullptr), loopHeader(nullptr),
177 loopPreheader(nullptr) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000178 initializeLoopUnswitchPass(*PassRegistry::getPassRegistry());
179 }
Devang Patel09f162c2007-05-01 21:15:47 +0000180
Craig Topper3e4c6972014-03-05 09:10:37 +0000181 bool runOnLoop(Loop *L, LPPassManager &LPM) override;
Devang Patele149d4e2008-07-02 01:18:13 +0000182 bool processCurrentLoop();
Chris Lattnerf48f7772004-04-19 18:07:02 +0000183
184 /// This transformation requires natural loop information & requires that
Chris Lattnerbc1a65a2010-08-29 17:23:19 +0000185 /// loop preheaders be inserted into the CFG.
Chris Lattnerf48f7772004-04-19 18:07:02 +0000186 ///
Craig Topper3e4c6972014-03-05 09:10:37 +0000187 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth66b31302015-01-04 12:03:27 +0000188 AU.addRequired<AssumptionCacheTracker>();
Chris Lattnerf48f7772004-04-19 18:07:02 +0000189 AU.addRequiredID(LoopSimplifyID);
Chris Lattner4f0e66d2006-02-09 22:15:42 +0000190 AU.addPreservedID(LoopSimplifyID);
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000191 AU.addRequired<LoopInfoWrapperPass>();
192 AU.addPreserved<LoopInfoWrapperPass>();
Owen Andersonfd0a3d62006-06-12 21:49:21 +0000193 AU.addRequiredID(LCSSAID);
Devang Pateld4911982007-07-31 08:03:26 +0000194 AU.addPreservedID(LCSSAID);
Chandler Carruth73523022014-01-13 13:07:17 +0000195 AU.addPreserved<DominatorTreeWrapperPass>();
Cameron Zwarich99de19b2011-02-11 06:08:28 +0000196 AU.addPreserved<ScalarEvolution>();
Chandler Carruth705b1852015-01-31 03:43:40 +0000197 AU.addRequired<TargetTransformInfoWrapperPass>();
Chris Lattnerf48f7772004-04-19 18:07:02 +0000198 }
199
200 private:
Devang Pateld4911982007-07-31 08:03:26 +0000201
Craig Topper3e4c6972014-03-05 09:10:37 +0000202 void releaseMemory() override {
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000203 BranchesInfo.forgetLoop(currentLoop);
Dan Gohman3ddbc242009-09-08 15:45:00 +0000204 }
205
Devang Patele149d4e2008-07-02 01:18:13 +0000206 void initLoopData() {
207 loopHeader = currentLoop->getHeader();
208 loopPreheader = currentLoop->getLoopPreheader();
209 }
Andrew Trick4104ed92012-04-10 05:14:37 +0000210
Chris Lattner559c8672008-04-21 00:25:49 +0000211 /// Split all of the edges from inside the loop to their exit blocks.
212 /// Update the appropriate Phi nodes as we do so.
Craig Topperb94011f2013-07-14 04:42:23 +0000213 void SplitExitEdges(Loop *L, const SmallVectorImpl<BasicBlock *> &ExitBlocks);
Devang Patela69f9872007-10-05 22:29:34 +0000214
Weiming Zhaof1abad52015-06-23 05:31:09 +0000215 bool UnswitchIfProfitable(Value *LoopCond, Constant *Val,
216 TerminatorInst *TI = nullptr);
Chris Lattner29f771b2006-02-18 01:27:45 +0000217 void UnswitchTrivialCondition(Loop *L, Value *Cond, Constant *Val,
Weiming Zhaof1abad52015-06-23 05:31:09 +0000218 BasicBlock *ExitBlock, TerminatorInst *TI);
219 void UnswitchNontrivialCondition(Value *LIC, Constant *OnVal, Loop *L,
220 TerminatorInst *TI);
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000221
222 void RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC,
223 Constant *Val, bool isEqual);
Devang Patel3304e462007-06-28 00:49:00 +0000224
225 void EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val,
Andrew Trick4104ed92012-04-10 05:14:37 +0000226 BasicBlock *TrueDest,
Devang Patel3304e462007-06-28 00:49:00 +0000227 BasicBlock *FalseDest,
Weiming Zhaof1abad52015-06-23 05:31:09 +0000228 Instruction *InsertPt,
229 TerminatorInst *TI);
Devang Patel3304e462007-06-28 00:49:00 +0000230
Devang Pateld4911982007-07-31 08:03:26 +0000231 void SimplifyCode(std::vector<Instruction*> &Worklist, Loop *L);
Craig Topperf40110f2014-04-25 05:29:35 +0000232 bool IsTrivialUnswitchCondition(Value *Cond, Constant **Val = nullptr,
233 BasicBlock **LoopExit = nullptr);
Devang Patele149d4e2008-07-02 01:18:13 +0000234
Chris Lattnerf48f7772004-04-19 18:07:02 +0000235 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000236}
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000237
238// Analyze loop. Check its size, calculate is it possible to unswitch
239// it. Returns true if we can unswitch this loop.
Hal Finkel57f03dd2014-09-07 13:49:57 +0000240bool LUAnalysisCache::countLoop(const Loop *L, const TargetTransformInfo &TTI,
Chandler Carruth66b31302015-01-04 12:03:27 +0000241 AssumptionCache *AC) {
Andrew Trick4104ed92012-04-10 05:14:37 +0000242
Jakub Staszak27da1232013-08-06 17:03:42 +0000243 LoopPropsMapIt PropsIt;
244 bool Inserted;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000245 std::tie(PropsIt, Inserted) =
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000246 LoopsProperties.insert(std::make_pair(L, LoopProperties()));
Andrew Trick4104ed92012-04-10 05:14:37 +0000247
Jakub Staszak27da1232013-08-06 17:03:42 +0000248 LoopProperties &Props = PropsIt->second;
Andrew Trick4104ed92012-04-10 05:14:37 +0000249
Jakub Staszak27da1232013-08-06 17:03:42 +0000250 if (Inserted) {
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000251 // New loop.
252
253 // Limit the number of instructions to avoid causing significant code
254 // expansion, and the number of basic blocks, to avoid loops with
255 // large numbers of branches which cause loop unswitching to go crazy.
256 // This is a very ad-hoc heuristic.
Andrew Trick4104ed92012-04-10 05:14:37 +0000257
Hal Finkel57f03dd2014-09-07 13:49:57 +0000258 SmallPtrSet<const Value *, 32> EphValues;
Chandler Carruth66b31302015-01-04 12:03:27 +0000259 CodeMetrics::collectEphemeralValues(L, AC, EphValues);
Hal Finkel57f03dd2014-09-07 13:49:57 +0000260
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000261 // FIXME: This is overly conservative because it does not take into
262 // consideration code simplification opportunities and code that can
263 // be shared by the resultant unswitched loops.
264 CodeMetrics Metrics;
Mark Heffernan9b536a62015-06-23 18:26:50 +0000265 for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); I != E;
266 ++I)
Hal Finkel57f03dd2014-09-07 13:49:57 +0000267 Metrics.analyzeBasicBlock(*I, TTI, EphValues);
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000268
Mark Heffernan9b536a62015-06-23 18:26:50 +0000269 Props.SizeEstimation = Metrics.NumInsts;
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000270 Props.CanBeUnswitchedCount = MaxSize / (Props.SizeEstimation);
Mark Heffernan9b536a62015-06-23 18:26:50 +0000271 Props.WasUnswitchedCount = 0;
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000272 MaxSize -= Props.SizeEstimation * Props.CanBeUnswitchedCount;
James Molloy4f6fb952012-12-20 16:04:27 +0000273
274 if (Metrics.notDuplicatable) {
275 DEBUG(dbgs() << "NOT unswitching loop %"
Jakub Staszak27da1232013-08-06 17:03:42 +0000276 << L->getHeader()->getName() << ", contents cannot be "
277 << "duplicated!\n");
James Molloy4f6fb952012-12-20 16:04:27 +0000278 return false;
279 }
Andrew Trick4104ed92012-04-10 05:14:37 +0000280 }
281
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000282 // Be careful. This links are good only before new loop addition.
283 CurrentLoopProperties = &Props;
284 CurLoopInstructions = &Props.UnswitchedVals;
Andrew Trick4104ed92012-04-10 05:14:37 +0000285
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000286 return true;
287}
288
289// Clean all data related to given loop.
Jakub Staszak27da1232013-08-06 17:03:42 +0000290void LUAnalysisCache::forgetLoop(const Loop *L) {
Andrew Trick4104ed92012-04-10 05:14:37 +0000291
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000292 LoopPropsMapIt LIt = LoopsProperties.find(L);
293
294 if (LIt != LoopsProperties.end()) {
Jakub Staszak27da1232013-08-06 17:03:42 +0000295 LoopProperties &Props = LIt->second;
Mark Heffernan9b536a62015-06-23 18:26:50 +0000296 MaxSize += (Props.CanBeUnswitchedCount + Props.WasUnswitchedCount) *
297 Props.SizeEstimation;
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000298 LoopsProperties.erase(LIt);
299 }
Andrew Trick4104ed92012-04-10 05:14:37 +0000300
Craig Topperf40110f2014-04-25 05:29:35 +0000301 CurrentLoopProperties = nullptr;
302 CurLoopInstructions = nullptr;
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000303}
304
305// Mark case value as unswitched.
306// Since SI instruction can be partly unswitched, in order to avoid
307// extra unswitching in cloned loops keep track all unswitched values.
Jakub Staszak27da1232013-08-06 17:03:42 +0000308void LUAnalysisCache::setUnswitched(const SwitchInst *SI, const Value *V) {
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000309 (*CurLoopInstructions)[SI].insert(V);
310}
311
312// Check was this case value unswitched before or not.
Jakub Staszak27da1232013-08-06 17:03:42 +0000313bool LUAnalysisCache::isUnswitched(const SwitchInst *SI, const Value *V) {
Andrew Trick4104ed92012-04-10 05:14:37 +0000314 return (*CurLoopInstructions)[SI].count(V);
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000315}
316
Mark Heffernan9b536a62015-06-23 18:26:50 +0000317bool LUAnalysisCache::CostAllowsUnswitching() {
318 return CurrentLoopProperties->CanBeUnswitchedCount > 0;
319}
320
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000321// Clone all loop-unswitch related loop properties.
322// Redistribute unswitching quotas.
323// Note, that new loop data is stored inside the VMap.
Jakub Staszak27da1232013-08-06 17:03:42 +0000324void LUAnalysisCache::cloneData(const Loop *NewLoop, const Loop *OldLoop,
325 const ValueToValueMapTy &VMap) {
Andrew Trick4104ed92012-04-10 05:14:37 +0000326
Jakub Staszak27da1232013-08-06 17:03:42 +0000327 LoopProperties &NewLoopProps = LoopsProperties[NewLoop];
328 LoopProperties &OldLoopProps = *CurrentLoopProperties;
329 UnswitchedValsMap &Insts = OldLoopProps.UnswitchedVals;
Andrew Trick4104ed92012-04-10 05:14:37 +0000330
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000331 // Reallocate "can-be-unswitched quota"
332
333 --OldLoopProps.CanBeUnswitchedCount;
Mark Heffernan9b536a62015-06-23 18:26:50 +0000334 ++OldLoopProps.WasUnswitchedCount;
335 NewLoopProps.WasUnswitchedCount = 0;
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000336 unsigned Quota = OldLoopProps.CanBeUnswitchedCount;
337 NewLoopProps.CanBeUnswitchedCount = Quota / 2;
338 OldLoopProps.CanBeUnswitchedCount = Quota - Quota / 2;
Andrew Trick4104ed92012-04-10 05:14:37 +0000339
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000340 NewLoopProps.SizeEstimation = OldLoopProps.SizeEstimation;
Andrew Trick4104ed92012-04-10 05:14:37 +0000341
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000342 // Clone unswitched values info:
343 // for new loop switches we clone info about values that was
344 // already unswitched and has redundant successors.
345 for (UnswitchedValsIt I = Insts.begin(); I != Insts.end(); ++I) {
Jakub Staszak27da1232013-08-06 17:03:42 +0000346 const SwitchInst *OldInst = I->first;
347 Value *NewI = VMap.lookup(OldInst);
348 const SwitchInst *NewInst = cast_or_null<SwitchInst>(NewI);
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000349 assert(NewInst && "All instructions that are in SrcBB must be in VMap.");
Andrew Trick4104ed92012-04-10 05:14:37 +0000350
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000351 NewLoopProps.UnswitchedVals[NewInst] = OldLoopProps.UnswitchedVals[OldInst];
352 }
353}
354
Dan Gohmand78c4002008-05-13 00:00:25 +0000355char LoopUnswitch::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +0000356INITIALIZE_PASS_BEGIN(LoopUnswitch, "loop-unswitch", "Unswitch loops",
357 false, false)
Chandler Carruth705b1852015-01-31 03:43:40 +0000358INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
Chandler Carruth66b31302015-01-04 12:03:27 +0000359INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000360INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000361INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000362INITIALIZE_PASS_DEPENDENCY(LCSSA)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000363INITIALIZE_PASS_END(LoopUnswitch, "loop-unswitch", "Unswitch loops",
364 false, false)
Chris Lattnerf48f7772004-04-19 18:07:02 +0000365
Andrew Trick4104ed92012-04-10 05:14:37 +0000366Pass *llvm::createLoopUnswitchPass(bool Os) {
367 return new LoopUnswitch(Os);
Devang Patel506310d2007-06-06 00:21:03 +0000368}
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000369
370/// FindLIVLoopCondition - Cond is a condition that occurs in L. If it is
371/// invariant in the loop, or has an invariant piece, return the invariant.
372/// Otherwise, return null.
373static Value *FindLIVLoopCondition(Value *Cond, Loop *L, bool &Changed) {
Andrew Trick4104ed92012-04-10 05:14:37 +0000374
Stepan Dyatkovskiy82165692012-01-11 08:40:51 +0000375 // We started analyze new instruction, increment scanned instructions counter.
376 ++TotalInsts;
Andrew Trick4104ed92012-04-10 05:14:37 +0000377
Chris Lattner302240d2010-02-02 02:26:54 +0000378 // We can never unswitch on vector conditions.
Duncan Sands19d0b472010-02-16 11:11:14 +0000379 if (Cond->getType()->isVectorTy())
Craig Topperf40110f2014-04-25 05:29:35 +0000380 return nullptr;
Chris Lattner302240d2010-02-02 02:26:54 +0000381
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000382 // Constants should be folded, not unswitched on!
Craig Topperf40110f2014-04-25 05:29:35 +0000383 if (isa<Constant>(Cond)) return nullptr;
Devang Patel3c723c82007-06-28 00:44:10 +0000384
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000385 // TODO: Handle: br (VARIANT|INVARIANT).
Devang Patelfe57d102008-11-03 19:38:07 +0000386
Dan Gohman4d6149f2009-07-14 01:37:59 +0000387 // Hoist simple values out.
Dan Gohmanc43e4792009-07-15 01:25:43 +0000388 if (L->makeLoopInvariant(Cond, Changed))
Dan Gohman4d6149f2009-07-14 01:37:59 +0000389 return Cond;
Dan Gohman4d6149f2009-07-14 01:37:59 +0000390
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000391 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Cond))
392 if (BO->getOpcode() == Instruction::And ||
393 BO->getOpcode() == Instruction::Or) {
394 // If either the left or right side is invariant, we can unswitch on this,
395 // which will cause the branch to go away in one loop and the condition to
396 // simplify in the other one.
397 if (Value *LHS = FindLIVLoopCondition(BO->getOperand(0), L, Changed))
398 return LHS;
399 if (Value *RHS = FindLIVLoopCondition(BO->getOperand(1), L, Changed))
400 return RHS;
401 }
Andrew Trick4104ed92012-04-10 05:14:37 +0000402
Craig Topperf40110f2014-04-25 05:29:35 +0000403 return nullptr;
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000404}
405
Devang Patel901a27d2007-03-07 00:26:10 +0000406bool LoopUnswitch::runOnLoop(Loop *L, LPPassManager &LPM_Ref) {
Paul Robinsonaf4e64d2014-02-06 00:07:05 +0000407 if (skipOptnoneFunction(L))
408 return false;
409
Chandler Carruth66b31302015-01-04 12:03:27 +0000410 AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
411 *L->getHeader()->getParent());
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000412 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Devang Patel901a27d2007-03-07 00:26:10 +0000413 LPM = &LPM_Ref;
Chandler Carruth73523022014-01-13 13:07:17 +0000414 DominatorTreeWrapperPass *DTWP =
415 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
Craig Topperf40110f2014-04-25 05:29:35 +0000416 DT = DTWP ? &DTWP->getDomTree() : nullptr;
Devang Patele149d4e2008-07-02 01:18:13 +0000417 currentLoop = L;
Devang Patel40519f02008-09-04 22:43:59 +0000418 Function *F = currentLoop->getHeader()->getParent();
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000419 bool Changed = false;
Devang Patel7d165e12007-07-30 23:07:10 +0000420 do {
Dan Gohman2734ebd2010-03-10 19:38:49 +0000421 assert(currentLoop->isLCSSAForm(*DT));
Devang Patel7d165e12007-07-30 23:07:10 +0000422 redoLoop = false;
Devang Patele149d4e2008-07-02 01:18:13 +0000423 Changed |= processCurrentLoop();
Devang Patel7d165e12007-07-30 23:07:10 +0000424 } while(redoLoop);
425
Devang Patel40519f02008-09-04 22:43:59 +0000426 if (Changed) {
427 // FIXME: Reconstruct dom info, because it is not preserved properly.
428 if (DT)
Chandler Carruth73523022014-01-13 13:07:17 +0000429 DT->recalculate(*F);
Devang Patel40519f02008-09-04 22:43:59 +0000430 }
Devang Patel7d165e12007-07-30 23:07:10 +0000431 return Changed;
432}
433
Andrew Trick4104ed92012-04-10 05:14:37 +0000434/// processCurrentLoop - Do actual work and unswitch loop if possible
Devang Patele149d4e2008-07-02 01:18:13 +0000435/// and profitable.
436bool LoopUnswitch::processCurrentLoop() {
Devang Patel7d165e12007-07-30 23:07:10 +0000437 bool Changed = false;
Stepan Dyatkovskiy82165692012-01-11 08:40:51 +0000438
439 initLoopData();
Andrew Trick4104ed92012-04-10 05:14:37 +0000440
Stepan Dyatkovskiy82165692012-01-11 08:40:51 +0000441 // If LoopSimplify was unable to form a preheader, don't do any unswitching.
442 if (!loopPreheader)
443 return false;
Andrew Trick4104ed92012-04-10 05:14:37 +0000444
Andrew Trick4442bfe2012-04-10 05:14:42 +0000445 // Loops with indirectbr cannot be cloned.
446 if (!currentLoop->isSafeToClone())
447 return false;
448
449 // Without dedicated exits, splitting the exit edge may fail.
450 if (!currentLoop->hasDedicatedExits())
451 return false;
452
Stepan Dyatkovskiy82165692012-01-11 08:40:51 +0000453 LLVMContext &Context = loopHeader->getContext();
Andrew Trick4104ed92012-04-10 05:14:37 +0000454
Stepan Dyatkovskiy82165692012-01-11 08:40:51 +0000455 // Probably we reach the quota of branches for this loop. If so
456 // stop unswitching.
Chandler Carruth705b1852015-01-31 03:43:40 +0000457 if (!BranchesInfo.countLoop(
Chandler Carruthfdb9c572015-02-01 12:01:35 +0000458 currentLoop, getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
459 *currentLoop->getHeader()->getParent()),
Chandler Carruth705b1852015-01-31 03:43:40 +0000460 AC))
Stepan Dyatkovskiy82165692012-01-11 08:40:51 +0000461 return false;
Devang Patel7d165e12007-07-30 23:07:10 +0000462
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000463 // Loop over all of the basic blocks in the loop. If we find an interior
464 // block that is branching on a loop-invariant condition, we can unswitch this
465 // loop.
Andrew Trick4104ed92012-04-10 05:14:37 +0000466 for (Loop::block_iterator I = currentLoop->block_begin(),
Chris Lattnerc832c1b2010-04-05 21:18:32 +0000467 E = currentLoop->block_end(); I != E; ++I) {
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000468 TerminatorInst *TI = (*I)->getTerminator();
469 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
470 // If this isn't branching on an invariant condition, we can't unswitch
471 // it.
472 if (BI->isConditional()) {
473 // See if this, or some part of it, is loop invariant. If so, we can
474 // unswitch on it if we desire.
Andrew Trick4104ed92012-04-10 05:14:37 +0000475 Value *LoopCond = FindLIVLoopCondition(BI->getCondition(),
Devang Patele149d4e2008-07-02 01:18:13 +0000476 currentLoop, Changed);
Weiming Zhaof1abad52015-06-23 05:31:09 +0000477 if (LoopCond &&
478 UnswitchIfProfitable(LoopCond, ConstantInt::getTrue(Context), TI)) {
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000479 ++NumBranches;
480 return true;
481 }
Andrew Trick4104ed92012-04-10 05:14:37 +0000482 }
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000483 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
Andrew Trick4104ed92012-04-10 05:14:37 +0000484 Value *LoopCond = FindLIVLoopCondition(SI->getCondition(),
Devang Patele149d4e2008-07-02 01:18:13 +0000485 currentLoop, Changed);
Andrew Trick4104ed92012-04-10 05:14:37 +0000486 unsigned NumCases = SI->getNumCases();
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +0000487 if (LoopCond && NumCases) {
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000488 // Find a value to unswitch on:
489 // FIXME: this should chose the most expensive case!
Nick Lewycky61158242011-06-03 06:27:15 +0000490 // FIXME: scan for a case with a non-critical edge?
Craig Topperf40110f2014-04-25 05:29:35 +0000491 Constant *UnswitchVal = nullptr;
Andrew Trick4104ed92012-04-10 05:14:37 +0000492
Devang Patel967b84c2007-02-26 19:31:58 +0000493 // Do not process same value again and again.
Chad Rosier3ba90a12011-12-22 21:10:46 +0000494 // At this point we have some cases already unswitched and
495 // some not yet unswitched. Let's find the first not yet unswitched one.
Stepan Dyatkovskiy97b02fc2012-03-11 06:09:17 +0000496 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +0000497 i != e; ++i) {
Jakub Staszak27da1232013-08-06 17:03:42 +0000498 Constant *UnswitchValCandidate = i.getCaseValue();
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000499 if (!BranchesInfo.isUnswitched(SI, UnswitchValCandidate)) {
Chad Rosier3ba90a12011-12-22 21:10:46 +0000500 UnswitchVal = UnswitchValCandidate;
501 break;
502 }
503 }
Andrew Trick4104ed92012-04-10 05:14:37 +0000504
Chad Rosier3ba90a12011-12-22 21:10:46 +0000505 if (!UnswitchVal)
Devang Patel967b84c2007-02-26 19:31:58 +0000506 continue;
Devang Patel967b84c2007-02-26 19:31:58 +0000507
Devang Patele149d4e2008-07-02 01:18:13 +0000508 if (UnswitchIfProfitable(LoopCond, UnswitchVal)) {
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000509 ++NumSwitches;
510 return true;
511 }
512 }
513 }
Andrew Trick4104ed92012-04-10 05:14:37 +0000514
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000515 // Scan the instructions to check for unswitchable values.
Andrew Trick4104ed92012-04-10 05:14:37 +0000516 for (BasicBlock::iterator BBI = (*I)->begin(), E = (*I)->end();
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000517 BBI != E; ++BBI)
518 if (SelectInst *SI = dyn_cast<SelectInst>(BBI)) {
Andrew Trick4104ed92012-04-10 05:14:37 +0000519 Value *LoopCond = FindLIVLoopCondition(SI->getCondition(),
Devang Patele149d4e2008-07-02 01:18:13 +0000520 currentLoop, Changed);
Andrew Trick4104ed92012-04-10 05:14:37 +0000521 if (LoopCond && UnswitchIfProfitable(LoopCond,
Owen Anderson23a204d2009-07-31 17:39:07 +0000522 ConstantInt::getTrue(Context))) {
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000523 ++NumSelects;
524 return true;
525 }
526 }
527 }
Chris Lattnerf48f7772004-04-19 18:07:02 +0000528 return Changed;
529}
530
Dan Gohman0ad7d9c2010-09-01 21:46:45 +0000531/// isTrivialLoopExitBlock - Check to see if all paths from BB exit the
532/// loop with no side effects (including infinite loops).
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000533///
Dan Gohman0ad7d9c2010-09-01 21:46:45 +0000534/// If true, we return true and set ExitBB to the block we
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000535/// exit through.
536///
537static bool isTrivialLoopExitBlockHelper(Loop *L, BasicBlock *BB,
538 BasicBlock *&ExitBB,
539 std::set<BasicBlock*> &Visited) {
Chris Lattnerbaddba42006-02-17 06:39:56 +0000540 if (!Visited.insert(BB).second) {
Nick Lewyckyd9d1de42011-12-23 23:49:25 +0000541 // Already visited. Without more analysis, this could indicate an infinite
542 // loop.
Dan Gohman0ad7d9c2010-09-01 21:46:45 +0000543 return false;
Jakub Staszak27da1232013-08-06 17:03:42 +0000544 }
545 if (!L->contains(BB)) {
Chris Lattnerbaddba42006-02-17 06:39:56 +0000546 // Otherwise, this is a loop exit, this is fine so long as this is the
547 // first exit.
Craig Topperf40110f2014-04-25 05:29:35 +0000548 if (ExitBB) return false;
Chris Lattnerbaddba42006-02-17 06:39:56 +0000549 ExitBB = BB;
Edward O'Callaghan2b8fed12009-11-25 05:38:41 +0000550 return true;
Chris Lattnerbaddba42006-02-17 06:39:56 +0000551 }
Andrew Trick4104ed92012-04-10 05:14:37 +0000552
Chris Lattnerbaddba42006-02-17 06:39:56 +0000553 // Otherwise, this is an unvisited intra-loop node. Check all successors.
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000554 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) {
Chris Lattnerbaddba42006-02-17 06:39:56 +0000555 // Check to see if the successor is a trivial loop exit.
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000556 if (!isTrivialLoopExitBlockHelper(L, *SI, ExitBB, Visited))
Chris Lattnerbaddba42006-02-17 06:39:56 +0000557 return false;
Chris Lattner6e263152006-02-10 02:30:37 +0000558 }
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000559
560 // Okay, everything after this looks good, check to make sure that this block
561 // doesn't include any side effects.
Chris Lattnerfdff0bb2006-02-15 22:52:05 +0000562 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Duncan Sands1efabaa2009-05-06 06:49:50 +0000563 if (I->mayHaveSideEffects())
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000564 return false;
Andrew Trick4104ed92012-04-10 05:14:37 +0000565
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000566 return true;
Chris Lattner6e263152006-02-10 02:30:37 +0000567}
568
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000569/// isTrivialLoopExitBlock - Return true if the specified block unconditionally
Andrew Trick4104ed92012-04-10 05:14:37 +0000570/// leads to an exit from the specified loop, and has no side-effects in the
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000571/// process. If so, return the block that is exited to, otherwise return null.
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000572static BasicBlock *isTrivialLoopExitBlock(Loop *L, BasicBlock *BB) {
573 std::set<BasicBlock*> Visited;
Dan Gohman0ad7d9c2010-09-01 21:46:45 +0000574 Visited.insert(L->getHeader()); // Branches to header make infinite loops.
Craig Topperf40110f2014-04-25 05:29:35 +0000575 BasicBlock *ExitBB = nullptr;
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000576 if (isTrivialLoopExitBlockHelper(L, BB, ExitBB, Visited))
577 return ExitBB;
Craig Topperf40110f2014-04-25 05:29:35 +0000578 return nullptr;
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000579}
Chris Lattner6e263152006-02-10 02:30:37 +0000580
Chris Lattnered7a67b2006-02-10 01:24:09 +0000581/// IsTrivialUnswitchCondition - Check to see if this unswitch condition is
582/// trivial: that is, that the condition controls whether or not the loop does
583/// anything at all. If this is a trivial condition, unswitching produces no
584/// code duplications (equivalently, it produces a simpler loop and a new empty
585/// loop, which gets deleted).
586///
Chris Lattner8a5a3242006-02-22 06:37:14 +0000587/// If this is a trivial condition, return true, otherwise return false. When
588/// returning true, this sets Cond and Val to the condition that controls the
589/// trivial condition: when Cond dynamically equals Val, the loop is known to
590/// exit. Finally, this sets LoopExit to the BB that the loop exits to when
591/// Cond == Val.
592///
Devang Patele149d4e2008-07-02 01:18:13 +0000593bool LoopUnswitch::IsTrivialUnswitchCondition(Value *Cond, Constant **Val,
594 BasicBlock **LoopExit) {
595 BasicBlock *Header = currentLoop->getHeader();
Chris Lattnerfdff0bb2006-02-15 22:52:05 +0000596 TerminatorInst *HeaderTerm = Header->getTerminator();
Owen Anderson47db9412009-07-22 00:24:57 +0000597 LLVMContext &Context = Header->getContext();
Andrew Trick4104ed92012-04-10 05:14:37 +0000598
Craig Topperf40110f2014-04-25 05:29:35 +0000599 BasicBlock *LoopExitBB = nullptr;
Chris Lattnerfdff0bb2006-02-15 22:52:05 +0000600 if (BranchInst *BI = dyn_cast<BranchInst>(HeaderTerm)) {
601 // If the header block doesn't end with a conditional branch on Cond, we
602 // can't handle it.
603 if (!BI->isConditional() || BI->getCondition() != Cond)
604 return false;
Andrew Trick4104ed92012-04-10 05:14:37 +0000605
606 // Check to see if a successor of the branch is guaranteed to
607 // exit through a unique exit block without having any
Chris Lattnerfdff0bb2006-02-15 22:52:05 +0000608 // side-effects. If so, determine the value of Cond that causes it to do
609 // this.
Andrew Trick4104ed92012-04-10 05:14:37 +0000610 if ((LoopExitBB = isTrivialLoopExitBlock(currentLoop,
Devang Patele149d4e2008-07-02 01:18:13 +0000611 BI->getSuccessor(0)))) {
Owen Anderson23a204d2009-07-31 17:39:07 +0000612 if (Val) *Val = ConstantInt::getTrue(Context);
Andrew Trick4104ed92012-04-10 05:14:37 +0000613 } else if ((LoopExitBB = isTrivialLoopExitBlock(currentLoop,
Devang Patele149d4e2008-07-02 01:18:13 +0000614 BI->getSuccessor(1)))) {
Owen Anderson23a204d2009-07-31 17:39:07 +0000615 if (Val) *Val = ConstantInt::getFalse(Context);
Chris Lattnerfdff0bb2006-02-15 22:52:05 +0000616 }
617 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(HeaderTerm)) {
618 // If this isn't a switch on Cond, we can't handle it.
619 if (SI->getCondition() != Cond) return false;
Andrew Trick4104ed92012-04-10 05:14:37 +0000620
Chris Lattnerfdff0bb2006-02-15 22:52:05 +0000621 // Check to see if a successor of the switch is guaranteed to go to the
Andrew Trick4104ed92012-04-10 05:14:37 +0000622 // latch block or exit through a one exit block without having any
Chris Lattnerfdff0bb2006-02-15 22:52:05 +0000623 // side-effects. If so, determine the value of Cond that causes it to do
Andrew Trick4104ed92012-04-10 05:14:37 +0000624 // this.
Chad Rosier3ba90a12011-12-22 21:10:46 +0000625 // Note that we can't trivially unswitch on the default case or
626 // on already unswitched cases.
Stepan Dyatkovskiy97b02fc2012-03-11 06:09:17 +0000627 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +0000628 i != e; ++i) {
Jakub Staszak27da1232013-08-06 17:03:42 +0000629 BasicBlock *LoopExitCandidate;
Andrew Trick4104ed92012-04-10 05:14:37 +0000630 if ((LoopExitCandidate = isTrivialLoopExitBlock(currentLoop,
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +0000631 i.getCaseSuccessor()))) {
Chris Lattnerfdff0bb2006-02-15 22:52:05 +0000632 // Okay, we found a trivial case, remember the value that is trivial.
Jakub Staszak27da1232013-08-06 17:03:42 +0000633 ConstantInt *CaseVal = i.getCaseValue();
Chad Rosier3ba90a12011-12-22 21:10:46 +0000634
635 // Check that it was not unswitched before, since already unswitched
636 // trivial vals are looks trivial too.
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000637 if (BranchesInfo.isUnswitched(SI, CaseVal))
Chad Rosier3ba90a12011-12-22 21:10:46 +0000638 continue;
639 LoopExitBB = LoopExitCandidate;
640 if (Val) *Val = CaseVal;
Chris Lattnerfdff0bb2006-02-15 22:52:05 +0000641 break;
642 }
Chad Rosier3ba90a12011-12-22 21:10:46 +0000643 }
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000644 }
645
Chris Lattnere5521db2006-02-22 23:55:00 +0000646 // If we didn't find a single unique LoopExit block, or if the loop exit block
647 // contains phi nodes, this isn't trivial.
648 if (!LoopExitBB || isa<PHINode>(LoopExitBB->begin()))
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000649 return false; // Can't handle this.
Andrew Trick4104ed92012-04-10 05:14:37 +0000650
Chris Lattnerfdff0bb2006-02-15 22:52:05 +0000651 if (LoopExit) *LoopExit = LoopExitBB;
Andrew Trick4104ed92012-04-10 05:14:37 +0000652
Chris Lattnered7a67b2006-02-10 01:24:09 +0000653 // We already know that nothing uses any scalar values defined inside of this
654 // loop. As such, we just have to check to see if this loop will execute any
655 // side-effecting instructions (e.g. stores, calls, volatile loads) in the
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000656 // part of the loop that the code *would* execute. We already checked the
657 // tail, check the header now.
Chris Lattnered7a67b2006-02-10 01:24:09 +0000658 for (BasicBlock::iterator I = Header->begin(), E = Header->end(); I != E; ++I)
Duncan Sands1efabaa2009-05-06 06:49:50 +0000659 if (I->mayHaveSideEffects())
Chris Lattner49354172006-02-10 02:01:22 +0000660 return false;
Chris Lattner49354172006-02-10 02:01:22 +0000661 return true;
Chris Lattnered7a67b2006-02-10 01:24:09 +0000662}
663
Devang Patele149d4e2008-07-02 01:18:13 +0000664/// UnswitchIfProfitable - We have found that we can unswitch currentLoop when
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000665/// LoopCond == Val to simplify the loop. If we decide that this is profitable,
666/// unswitch the loop, reprocess the pieces, then return true.
Weiming Zhaof1abad52015-06-23 05:31:09 +0000667bool LoopUnswitch::UnswitchIfProfitable(Value *LoopCond, Constant *Val,
668 TerminatorInst *TI) {
Dan Gohman72c367f2009-12-09 22:55:01 +0000669 Function *F = loopHeader->getParent();
Craig Topperf40110f2014-04-25 05:29:35 +0000670 Constant *CondVal = nullptr;
671 BasicBlock *ExitBlock = nullptr;
Bill Wendling712d85a2012-04-30 09:23:48 +0000672
Devang Patele149d4e2008-07-02 01:18:13 +0000673 if (IsTrivialUnswitchCondition(LoopCond, &CondVal, &ExitBlock)) {
Evan Chenged66db32010-04-03 02:23:43 +0000674 // If the condition is trivial, always unswitch. There is no code growth
675 // for this case.
Weiming Zhaof1abad52015-06-23 05:31:09 +0000676 UnswitchTrivialCondition(currentLoop, LoopCond, CondVal, ExitBlock, TI);
Evan Chenged66db32010-04-03 02:23:43 +0000677 return true;
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000678 }
Devang Patelc4dcf822008-07-03 06:48:21 +0000679
Evan Chenged66db32010-04-03 02:23:43 +0000680 // Check to see if it would be profitable to unswitch current loop.
Mark Heffernan9b536a62015-06-23 18:26:50 +0000681 if (!BranchesInfo.CostAllowsUnswitching()) {
682 DEBUG(dbgs() << "NOT unswitching loop %"
683 << currentLoop->getHeader()->getName()
684 << " at non-trivial condition '" << *Val
685 << "' == " << *LoopCond << "\n"
686 << ". Cost too high.\n");
687 return false;
688 }
Evan Chenged66db32010-04-03 02:23:43 +0000689
690 // Do not do non-trivial unswitch while optimizing for size.
Duncan P. N. Exon Smith2c79ad92015-02-14 01:11:29 +0000691 if (OptimizeForSize || F->hasFnAttribute(Attribute::OptimizeForSize))
Evan Chenged66db32010-04-03 02:23:43 +0000692 return false;
693
Weiming Zhaof1abad52015-06-23 05:31:09 +0000694 UnswitchNontrivialCondition(LoopCond, Val, currentLoop, TI);
Andrew Trick4442bfe2012-04-10 05:14:42 +0000695 return true;
Chris Lattnerfbadd7e2006-02-11 00:43:37 +0000696}
697
Chris Lattnerf48f7772004-04-19 18:07:02 +0000698/// CloneLoop - Recursively clone the specified loop and all of its children,
699/// mapping the blocks with the specified map.
Rafael Espindola229e38f2010-10-13 01:36:30 +0000700static Loop *CloneLoop(Loop *L, Loop *PL, ValueToValueMapTy &VM,
Devang Patel901a27d2007-03-07 00:26:10 +0000701 LoopInfo *LI, LPPassManager *LPM) {
Chris Lattnerf48f7772004-04-19 18:07:02 +0000702 Loop *New = new Loop();
Devang Patel901a27d2007-03-07 00:26:10 +0000703 LPM->insertLoop(New, PL);
Chris Lattnerf48f7772004-04-19 18:07:02 +0000704
705 // Add all of the blocks in L to the new loop.
706 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
707 I != E; ++I)
708 if (LI->getLoopFor(*I) == L)
Chandler Carruth691addc2015-01-18 01:25:51 +0000709 New->addBasicBlockToLoop(cast<BasicBlock>(VM[*I]), *LI);
Chris Lattnerf48f7772004-04-19 18:07:02 +0000710
711 // Add all of the subloops to the new loop.
712 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
Devang Patel901a27d2007-03-07 00:26:10 +0000713 CloneLoop(*I, New, VM, LI, LPM);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000714
Chris Lattnerf48f7772004-04-19 18:07:02 +0000715 return New;
716}
717
Weiming Zhaof1abad52015-06-23 05:31:09 +0000718static void copyMetadata(Instruction *DstInst, const Instruction *SrcInst,
719 bool Swapped) {
720 if (!SrcInst || !SrcInst->hasMetadata())
721 return;
722
723 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
724 SrcInst->getAllMetadata(MDs);
725 for (auto &MD : MDs) {
726 switch (MD.first) {
727 default:
728 break;
729 case LLVMContext::MD_prof:
730 if (Swapped && MD.second->getNumOperands() == 3 &&
731 isa<MDString>(MD.second->getOperand(0))) {
732 MDString *MDName = cast<MDString>(MD.second->getOperand(0));
733 if (MDName->getString() == "branch_weights") {
734 auto *ValT = cast_or_null<ConstantAsMetadata>(
735 MD.second->getOperand(1))->getValue();
736 auto *ValF = cast_or_null<ConstantAsMetadata>(
737 MD.second->getOperand(2))->getValue();
738 assert(ValT && ValF && "Invalid Operands of branch_weights");
739 auto NewMD =
740 MDBuilder(DstInst->getParent()->getContext())
741 .createBranchWeights(cast<ConstantInt>(ValF)->getZExtValue(),
742 cast<ConstantInt>(ValT)->getZExtValue());
743 MD.second = NewMD;
744 }
745 }
746 // fallthrough.
747 case LLVMContext::MD_dbg:
748 DstInst->setMetadata(MD.first, MD.second);
749 }
750 }
751}
752
Chris Lattnerb0cbe712006-02-15 00:07:43 +0000753/// EmitPreheaderBranchOnCondition - Emit a conditional branch on two values
754/// if LIC == Val, branch to TrueDst, otherwise branch to FalseDest. Insert the
755/// code immediately before InsertPt.
Devang Patel3304e462007-06-28 00:49:00 +0000756void LoopUnswitch::EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val,
757 BasicBlock *TrueDest,
758 BasicBlock *FalseDest,
Weiming Zhaof1abad52015-06-23 05:31:09 +0000759 Instruction *InsertPt,
760 TerminatorInst *TI) {
Chris Lattnerb0cbe712006-02-15 00:07:43 +0000761 // Insert a conditional branch on LIC to the two preheaders. The original
762 // code is the true version and the new code is the false version.
763 Value *BranchVal = LIC;
Weiming Zhaof1abad52015-06-23 05:31:09 +0000764 bool Swapped = false;
Owen Anderson55f1c092009-08-13 21:58:54 +0000765 if (!isa<ConstantInt>(Val) ||
766 Val->getType() != Type::getInt1Ty(LIC->getContext()))
Benjamin Kramer547b6c52011-09-27 20:39:19 +0000767 BranchVal = new ICmpInst(InsertPt, ICmpInst::ICMP_EQ, LIC, Val);
Weiming Zhaof1abad52015-06-23 05:31:09 +0000768 else if (Val != ConstantInt::getTrue(Val->getContext())) {
Chris Lattnerb0cbe712006-02-15 00:07:43 +0000769 // We want to enter the new loop when the condition is true.
770 std::swap(TrueDest, FalseDest);
Weiming Zhaof1abad52015-06-23 05:31:09 +0000771 Swapped = true;
772 }
Chris Lattnerb0cbe712006-02-15 00:07:43 +0000773
774 // Insert the new branch.
Dan Gohman3ddbc242009-09-08 15:45:00 +0000775 BranchInst *BI = BranchInst::Create(TrueDest, FalseDest, BranchVal, InsertPt);
Weiming Zhaof1abad52015-06-23 05:31:09 +0000776 copyMetadata(BI, TI, Swapped);
Dan Gohman3ddbc242009-09-08 15:45:00 +0000777
778 // If either edge is critical, split it. This helps preserve LoopSimplify
779 // form for enclosing loops.
Chandler Carruthf8753fc2015-01-19 12:12:00 +0000780 auto Options = CriticalEdgeSplittingOptions(DT, LI).setPreserveLCSSA();
Chandler Carruth37df2cf2015-01-19 12:09:11 +0000781 SplitCriticalEdge(BI, 0, Options);
782 SplitCriticalEdge(BI, 1, Options);
Chris Lattnerb0cbe712006-02-15 00:07:43 +0000783}
784
Chris Lattnered7a67b2006-02-10 01:24:09 +0000785/// UnswitchTrivialCondition - Given a loop that has a trivial unswitchable
786/// condition in it (a cond branch from its header block to its latch block,
Andrew Trick4104ed92012-04-10 05:14:37 +0000787/// where the path through the loop that doesn't execute its body has no
Chris Lattnered7a67b2006-02-10 01:24:09 +0000788/// side-effects), unswitch it. This doesn't involve any code duplication, just
789/// moving the conditional branch outside of the loop and updating loop info.
Weiming Zhaof1abad52015-06-23 05:31:09 +0000790void LoopUnswitch::UnswitchTrivialCondition(Loop *L, Value *Cond, Constant *Val,
791 BasicBlock *ExitBlock,
792 TerminatorInst *TI) {
David Greened9c355d2010-01-05 01:27:04 +0000793 DEBUG(dbgs() << "loop-unswitch: Trivial-Unswitch loop %"
Weiming Zhaof1abad52015-06-23 05:31:09 +0000794 << loopHeader->getName() << " [" << L->getBlocks().size()
795 << " blocks] in Function "
796 << L->getHeader()->getParent()->getName() << " on cond: " << *Val
797 << " == " << *Cond << "\n");
Andrew Trick4104ed92012-04-10 05:14:37 +0000798
Chris Lattnerfe4151e2006-02-10 23:16:39 +0000799 // First step, split the preheader, so that we know that there is a safe place
Devang Patele149d4e2008-07-02 01:18:13 +0000800 // to insert the conditional branch. We will change loopPreheader to have a
Chris Lattnered7a67b2006-02-10 01:24:09 +0000801 // conditional branch on Cond.
Chandler Carruthd4500562015-01-19 12:36:53 +0000802 BasicBlock *NewPH = SplitEdge(loopPreheader, loopHeader, DT, LI);
Chris Lattnered7a67b2006-02-10 01:24:09 +0000803
804 // Now that we have a place to insert the conditional branch, create a place
Chris Lattner49354172006-02-10 02:01:22 +0000805 // to branch to: this is the exit block out of the loop that we should
806 // short-circuit to.
Andrew Trick4104ed92012-04-10 05:14:37 +0000807
Chris Lattnere5cb76d2006-02-15 22:03:36 +0000808 // Split this block now, so that the loop maintains its exit block, and so
809 // that the jump from the preheader can execute the contents of the exit block
810 // without actually branching to it (the exit block should be dominated by the
811 // loop header, not the preheader).
Chris Lattner49354172006-02-10 02:01:22 +0000812 assert(!L->contains(ExitBlock) && "Exit block is in the loop?");
Chandler Carruth32c52c72015-01-18 02:39:37 +0000813 BasicBlock *NewExit = SplitBlock(ExitBlock, ExitBlock->begin(), DT, LI);
Andrew Trick4104ed92012-04-10 05:14:37 +0000814
815 // Okay, now we have a position to branch from and a position to branch to,
Chris Lattnered7a67b2006-02-10 01:24:09 +0000816 // insert the new conditional branch.
Andrew Trick4104ed92012-04-10 05:14:37 +0000817 EmitPreheaderBranchOnCondition(Cond, Val, NewExit, NewPH,
Weiming Zhaof1abad52015-06-23 05:31:09 +0000818 loopPreheader->getTerminator(), TI);
Devang Patele149d4e2008-07-02 01:18:13 +0000819 LPM->deleteSimpleAnalysisValue(loopPreheader->getTerminator(), L);
820 loopPreheader->getTerminator()->eraseFromParent();
Chris Lattnered7a67b2006-02-10 01:24:09 +0000821
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000822 // We need to reprocess this loop, it could be unswitched again.
Devang Patel7d165e12007-07-30 23:07:10 +0000823 redoLoop = true;
Andrew Trick4104ed92012-04-10 05:14:37 +0000824
Chris Lattnered7a67b2006-02-10 01:24:09 +0000825 // Now that we know that the loop is never entered when this condition is a
826 // particular value, rewrite the loop with this info. We know that this will
827 // at least eliminate the old branch.
Chris Lattner8a5a3242006-02-22 06:37:14 +0000828 RewriteLoopBodyWithConditionConstant(L, Cond, Val, false);
Chris Lattner0b8ec1a2006-02-14 01:01:41 +0000829 ++NumTrivial;
Chris Lattnered7a67b2006-02-10 01:24:09 +0000830}
831
Chris Lattner559c8672008-04-21 00:25:49 +0000832/// SplitExitEdges - Split all of the edges from inside the loop to their exit
833/// blocks. Update the appropriate Phi nodes as we do so.
Andrew Trick4104ed92012-04-10 05:14:37 +0000834void LoopUnswitch::SplitExitEdges(Loop *L,
Craig Topperb94011f2013-07-14 04:42:23 +0000835 const SmallVectorImpl<BasicBlock *> &ExitBlocks){
Devang Patela69f9872007-10-05 22:29:34 +0000836
Chris Lattnered7a67b2006-02-10 01:24:09 +0000837 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
Chris Lattnerfe4151e2006-02-10 23:16:39 +0000838 BasicBlock *ExitBlock = ExitBlocks[i];
Dan Gohman3ddbc242009-09-08 15:45:00 +0000839 SmallVector<BasicBlock *, 4> Preds(pred_begin(ExitBlock),
840 pred_end(ExitBlock));
Bill Wendling90f90da2011-09-27 00:59:31 +0000841
Nick Lewycky61158242011-06-03 06:27:15 +0000842 // Although SplitBlockPredecessors doesn't preserve loop-simplify in
843 // general, if we call it on all predecessors of all exits then it does.
Philip Reames9198b332015-01-28 23:06:47 +0000844 SplitBlockPredecessors(ExitBlock, Preds, ".us-lcssa",
845 /*AliasAnalysis*/ nullptr, DT, LI,
846 /*PreserveLCSSA*/ true);
Chris Lattnered7a67b2006-02-10 01:24:09 +0000847 }
Devang Patele192e3252007-10-03 21:16:08 +0000848}
849
Andrew Trick4104ed92012-04-10 05:14:37 +0000850/// UnswitchNontrivialCondition - We determined that the loop is profitable
851/// to unswitch when LIC equal Val. Split it into loop versions and test the
Devang Patel35747592007-10-03 21:17:43 +0000852/// condition outside of either loop. Return the loops created as Out1/Out2.
Andrew Trick4442bfe2012-04-10 05:14:42 +0000853void LoopUnswitch::UnswitchNontrivialCondition(Value *LIC, Constant *Val,
Weiming Zhaof1abad52015-06-23 05:31:09 +0000854 Loop *L, TerminatorInst *TI) {
Devang Patele149d4e2008-07-02 01:18:13 +0000855 Function *F = loopHeader->getParent();
David Greened9c355d2010-01-05 01:27:04 +0000856 DEBUG(dbgs() << "loop-unswitch: Unswitching loop %"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +0000857 << loopHeader->getName() << " [" << L->getBlocks().size()
858 << " blocks] in Function " << F->getName()
859 << " when '" << *Val << "' == " << *LIC << "\n");
Devang Patele192e3252007-10-03 21:16:08 +0000860
Cameron Zwarich99de19b2011-02-11 06:08:28 +0000861 if (ScalarEvolution *SE = getAnalysisIfAvailable<ScalarEvolution>())
862 SE->forgetLoop(L);
863
Devang Pateled50fb52008-07-02 01:44:29 +0000864 LoopBlocks.clear();
865 NewBlocks.clear();
Devang Patele192e3252007-10-03 21:16:08 +0000866
867 // First step, split the preheader and exit blocks, and add these blocks to
868 // the LoopBlocks list.
Chandler Carruthd4500562015-01-19 12:36:53 +0000869 BasicBlock *NewPreheader = SplitEdge(loopPreheader, loopHeader, DT, LI);
Devang Patele192e3252007-10-03 21:16:08 +0000870 LoopBlocks.push_back(NewPreheader);
871
872 // We want the loop to come after the preheader, but before the exit blocks.
873 LoopBlocks.insert(LoopBlocks.end(), L->block_begin(), L->block_end());
874
875 SmallVector<BasicBlock*, 8> ExitBlocks;
876 L->getUniqueExitBlocks(ExitBlocks);
877
878 // Split all of the edges from inside the loop to their exit blocks. Update
879 // the appropriate Phi nodes as we do so.
Devang Pateleb611dd2008-07-03 17:37:52 +0000880 SplitExitEdges(L, ExitBlocks);
Devang Patele192e3252007-10-03 21:16:08 +0000881
Chris Lattnerfe4151e2006-02-10 23:16:39 +0000882 // The exit blocks may have been changed due to edge splitting, recompute.
883 ExitBlocks.clear();
Devang Patelf489d0f2006-08-29 22:29:16 +0000884 L->getUniqueExitBlocks(ExitBlocks);
885
Chris Lattnerfe4151e2006-02-10 23:16:39 +0000886 // Add exit blocks to the loop blocks.
887 LoopBlocks.insert(LoopBlocks.end(), ExitBlocks.begin(), ExitBlocks.end());
Chris Lattnerf48f7772004-04-19 18:07:02 +0000888
889 // Next step, clone all of the basic blocks that make up the loop (including
890 // the loop preheader and exit blocks), keeping track of the mapping between
891 // the instructions and blocks.
Chris Lattnerf48f7772004-04-19 18:07:02 +0000892 NewBlocks.reserve(LoopBlocks.size());
Rafael Espindola229e38f2010-10-13 01:36:30 +0000893 ValueToValueMapTy VMap;
Chris Lattnerf48f7772004-04-19 18:07:02 +0000894 for (unsigned i = 0, e = LoopBlocks.size(); i != e; ++i) {
Devang Patel0dc3c2d2010-06-24 00:33:28 +0000895 BasicBlock *NewBB = CloneBasicBlock(LoopBlocks[i], VMap, ".us", F);
Andrew Trick4104ed92012-04-10 05:14:37 +0000896
Evan Chengba930442010-04-05 21:16:25 +0000897 NewBlocks.push_back(NewBB);
Devang Patel0dc3c2d2010-06-24 00:33:28 +0000898 VMap[LoopBlocks[i]] = NewBB; // Keep the BB mapping.
Evan Chengba930442010-04-05 21:16:25 +0000899 LPM->cloneBasicBlockSimpleAnalysis(LoopBlocks[i], NewBB, L);
Chris Lattnerf48f7772004-04-19 18:07:02 +0000900 }
901
902 // Splice the newly inserted blocks into the function right before the
903 // original preheader.
Evan Chengba930442010-04-05 21:16:25 +0000904 F->getBasicBlockList().splice(NewPreheader, F->getBasicBlockList(),
Chris Lattnerf48f7772004-04-19 18:07:02 +0000905 NewBlocks[0], F->end());
906
Hal Finkel74c2f352014-09-07 12:44:26 +0000907 // FIXME: We could register any cloned assumptions instead of clearing the
908 // whole function's cache.
Chandler Carruth66b31302015-01-04 12:03:27 +0000909 AC->clear();
Hal Finkel74c2f352014-09-07 12:44:26 +0000910
Chris Lattnerf48f7772004-04-19 18:07:02 +0000911 // Now we create the new Loop object for the versioned loop.
Devang Patel0dc3c2d2010-06-24 00:33:28 +0000912 Loop *NewLoop = CloneLoop(L, L->getParentLoop(), VMap, LI, LPM);
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +0000913
914 // Recalculate unswitching quota, inherit simplified switches info for NewBB,
915 // Probably clone more loop-unswitch related loop properties.
916 BranchesInfo.cloneData(NewLoop, L, VMap);
917
Chris Lattnerf1b15162006-02-10 23:26:14 +0000918 Loop *ParentLoop = L->getParentLoop();
919 if (ParentLoop) {
Chris Lattnerf48f7772004-04-19 18:07:02 +0000920 // Make sure to add the cloned preheader and exit blocks to the parent loop
921 // as well.
Chandler Carruth691addc2015-01-18 01:25:51 +0000922 ParentLoop->addBasicBlockToLoop(NewBlocks[0], *LI);
Chris Lattnerf1b15162006-02-10 23:26:14 +0000923 }
Bill Wendling90f90da2011-09-27 00:59:31 +0000924
Chris Lattnerf1b15162006-02-10 23:26:14 +0000925 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
Devang Patel0dc3c2d2010-06-24 00:33:28 +0000926 BasicBlock *NewExit = cast<BasicBlock>(VMap[ExitBlocks[i]]);
Chris Lattner8e44ff52006-02-18 00:55:32 +0000927 // The new exit block should be in the same loop as the old one.
928 if (Loop *ExitBBLoop = LI->getLoopFor(ExitBlocks[i]))
Chandler Carruth691addc2015-01-18 01:25:51 +0000929 ExitBBLoop->addBasicBlockToLoop(NewExit, *LI);
Andrew Trick4104ed92012-04-10 05:14:37 +0000930
Chris Lattnerf1b15162006-02-10 23:26:14 +0000931 assert(NewExit->getTerminator()->getNumSuccessors() == 1 &&
932 "Exit block should have been split to have one successor!");
933 BasicBlock *ExitSucc = NewExit->getTerminator()->getSuccessor(0);
Devang Pateleb611dd2008-07-03 17:37:52 +0000934
Chris Lattnerf1b15162006-02-10 23:26:14 +0000935 // If the successor of the exit block had PHI nodes, add an entry for
936 // NewExit.
Jakub Staszak27da1232013-08-06 17:03:42 +0000937 for (BasicBlock::iterator I = ExitSucc->begin();
938 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Chris Lattnerf1b15162006-02-10 23:26:14 +0000939 Value *V = PN->getIncomingValueForBlock(ExitBlocks[i]);
Rafael Espindola229e38f2010-10-13 01:36:30 +0000940 ValueToValueMapTy::iterator It = VMap.find(V);
Devang Patel0dc3c2d2010-06-24 00:33:28 +0000941 if (It != VMap.end()) V = It->second;
Chris Lattnerf1b15162006-02-10 23:26:14 +0000942 PN->addIncoming(V, NewExit);
943 }
Bill Wendling90f90da2011-09-27 00:59:31 +0000944
945 if (LandingPadInst *LPad = NewExit->getLandingPadInst()) {
Jakub Staszak27da1232013-08-06 17:03:42 +0000946 PHINode *PN = PHINode::Create(LPad->getType(), 0, "",
947 ExitSucc->getFirstInsertionPt());
Bill Wendling90f90da2011-09-27 00:59:31 +0000948
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000949 for (pred_iterator I = pred_begin(ExitSucc), E = pred_end(ExitSucc);
950 I != E; ++I) {
951 BasicBlock *BB = *I;
Bill Wendling90f90da2011-09-27 00:59:31 +0000952 LandingPadInst *LPI = BB->getLandingPadInst();
953 LPI->replaceAllUsesWith(PN);
954 PN->addIncoming(LPI, BB);
955 }
956 }
Chris Lattnerf48f7772004-04-19 18:07:02 +0000957 }
958
959 // Rewrite the code to refer to itself.
Nick Lewycky4d43d3c2008-04-25 16:53:59 +0000960 for (unsigned i = 0, e = NewBlocks.size(); i != e; ++i)
961 for (BasicBlock::iterator I = NewBlocks[i]->begin(),
962 E = NewBlocks[i]->end(); I != E; ++I)
Chris Lattner43f8d162011-01-08 08:15:20 +0000963 RemapInstruction(I, VMap,RF_NoModuleLevelChanges|RF_IgnoreMissingEntries);
Andrew Trick4104ed92012-04-10 05:14:37 +0000964
Chris Lattnerf48f7772004-04-19 18:07:02 +0000965 // Rewrite the original preheader to select between versions of the loop.
Devang Patele149d4e2008-07-02 01:18:13 +0000966 BranchInst *OldBR = cast<BranchInst>(loopPreheader->getTerminator());
Chris Lattnerb0cbe712006-02-15 00:07:43 +0000967 assert(OldBR->isUnconditional() && OldBR->getSuccessor(0) == LoopBlocks[0] &&
Chris Lattnerf48f7772004-04-19 18:07:02 +0000968 "Preheader splitting did not work correctly!");
Chris Lattnerf48f7772004-04-19 18:07:02 +0000969
Chris Lattnerb0cbe712006-02-15 00:07:43 +0000970 // Emit the new branch that selects between the two versions of this loop.
Weiming Zhaof1abad52015-06-23 05:31:09 +0000971 EmitPreheaderBranchOnCondition(LIC, Val, NewBlocks[0], LoopBlocks[0], OldBR,
972 TI);
Devang Pateld4911982007-07-31 08:03:26 +0000973 LPM->deleteSimpleAnalysisValue(OldBR, L);
Devang Patel83cc3f82007-09-20 23:45:50 +0000974 OldBR->eraseFromParent();
Devang Patela8823282007-08-02 15:25:57 +0000975
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +0000976 LoopProcessWorklist.push_back(NewLoop);
Devang Patel7d165e12007-07-30 23:07:10 +0000977 redoLoop = true;
Chris Lattnerf48f7772004-04-19 18:07:02 +0000978
Chris Lattner5814d9d92010-04-20 05:09:16 +0000979 // Keep a WeakVH holding onto LIC. If the first call to RewriteLoopBody
980 // deletes the instruction (for example by simplifying a PHI that feeds into
981 // the condition that we're unswitching on), we don't rewrite the second
982 // iteration.
983 WeakVH LICHandle(LIC);
Andrew Trick4104ed92012-04-10 05:14:37 +0000984
Chris Lattnerf48f7772004-04-19 18:07:02 +0000985 // Now we rewrite the original code to know that the condition is true and the
986 // new code to know that the condition is false.
Evan Chengba930442010-04-05 21:16:25 +0000987 RewriteLoopBodyWithConditionConstant(L, LIC, Val, false);
Devang Pateleb611dd2008-07-03 17:37:52 +0000988
Chris Lattner5814d9d92010-04-20 05:09:16 +0000989 // It's possible that simplifying one loop could cause the other to be
990 // changed to another value or a constant. If its a constant, don't simplify
991 // it.
992 if (!LoopProcessWorklist.empty() && LoopProcessWorklist.back() == NewLoop &&
993 LICHandle && !isa<Constant>(LICHandle))
994 RewriteLoopBodyWithConditionConstant(NewLoop, LICHandle, Val, true);
Chris Lattnerf48f7772004-04-19 18:07:02 +0000995}
996
Chris Lattner6fd13622006-02-17 00:31:07 +0000997/// RemoveFromWorklist - Remove all instances of I from the worklist vector
998/// specified.
Andrew Trick4104ed92012-04-10 05:14:37 +0000999static void RemoveFromWorklist(Instruction *I,
Chris Lattner6fd13622006-02-17 00:31:07 +00001000 std::vector<Instruction*> &Worklist) {
Jakub Staszak8f46e912012-10-16 19:52:32 +00001001
1002 Worklist.erase(std::remove(Worklist.begin(), Worklist.end(), I),
1003 Worklist.end());
Chris Lattner6fd13622006-02-17 00:31:07 +00001004}
1005
1006/// ReplaceUsesOfWith - When we find that I really equals V, remove I from the
1007/// program, replacing all uses with V and update the worklist.
Andrew Trick4104ed92012-04-10 05:14:37 +00001008static void ReplaceUsesOfWith(Instruction *I, Value *V,
Devang Pateld4911982007-07-31 08:03:26 +00001009 std::vector<Instruction*> &Worklist,
1010 Loop *L, LPPassManager *LPM) {
David Greened9c355d2010-01-05 01:27:04 +00001011 DEBUG(dbgs() << "Replace with '" << *V << "': " << *I);
Chris Lattner6fd13622006-02-17 00:31:07 +00001012
1013 // Add uses to the worklist, which may be dead now.
1014 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1015 if (Instruction *Use = dyn_cast<Instruction>(I->getOperand(i)))
1016 Worklist.push_back(Use);
1017
1018 // Add users to the worklist which may be simplified now.
Chandler Carruthcdf47882014-03-09 03:16:01 +00001019 for (User *U : I->users())
1020 Worklist.push_back(cast<Instruction>(U));
Devang Pateld4911982007-07-31 08:03:26 +00001021 LPM->deleteSimpleAnalysisValue(I, L);
Chris Lattner6fd13622006-02-17 00:31:07 +00001022 RemoveFromWorklist(I, Worklist);
Devang Patel83cc3f82007-09-20 23:45:50 +00001023 I->replaceAllUsesWith(V);
1024 I->eraseFromParent();
Chris Lattner6fd13622006-02-17 00:31:07 +00001025 ++NumSimplify;
1026}
1027
Chris Lattnerfbadd7e2006-02-11 00:43:37 +00001028// RewriteLoopBodyWithConditionConstant - We know either that the value LIC has
1029// the value specified by Val in the specified loop, or we know it does NOT have
1030// that value. Rewrite any uses of LIC or of properties correlated to it.
Chris Lattnerf48f7772004-04-19 18:07:02 +00001031void LoopUnswitch::RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC,
Chris Lattnerfbadd7e2006-02-11 00:43:37 +00001032 Constant *Val,
1033 bool IsEqual) {
Chris Lattnered7a67b2006-02-10 01:24:09 +00001034 assert(!isa<Constant>(LIC) && "Why are we unswitching on a constant?");
Andrew Trick4104ed92012-04-10 05:14:37 +00001035
Chris Lattnerf48f7772004-04-19 18:07:02 +00001036 // FIXME: Support correlated properties, like:
1037 // for (...)
1038 // if (li1 < li2)
1039 // ...
1040 // if (li1 > li2)
1041 // ...
Andrew Trick4104ed92012-04-10 05:14:37 +00001042
Chris Lattner6e263152006-02-10 02:30:37 +00001043 // FOLD boolean conditions (X|LIC), (X&LIC). Fold conditional branches,
1044 // selects, switches.
Chris Lattner6fd13622006-02-17 00:31:07 +00001045 std::vector<Instruction*> Worklist;
Owen Anderson47db9412009-07-22 00:24:57 +00001046 LLVMContext &Context = Val->getContext();
1047
Chris Lattner6fd13622006-02-17 00:31:07 +00001048 // If we know that LIC == Val, or that LIC == NotVal, just replace uses of LIC
1049 // in the loop with the appropriate one directly.
Owen Anderson55f1c092009-08-13 21:58:54 +00001050 if (IsEqual || (isa<ConstantInt>(Val) &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001051 Val->getType()->isIntegerTy(1))) {
Chris Lattner8a5a3242006-02-22 06:37:14 +00001052 Value *Replacement;
1053 if (IsEqual)
1054 Replacement = Val;
1055 else
Andrew Trick4104ed92012-04-10 05:14:37 +00001056 Replacement = ConstantInt::get(Type::getInt1Ty(Val->getContext()),
Reid Spencercddc9df2007-01-12 04:24:46 +00001057 !cast<ConstantInt>(Val)->getZExtValue());
Andrew Trick4104ed92012-04-10 05:14:37 +00001058
Chandler Carruthcdf47882014-03-09 03:16:01 +00001059 for (User *U : LIC->users()) {
1060 Instruction *UI = dyn_cast<Instruction>(U);
1061 if (!UI || !L->contains(UI))
Evan Cheng1b55f562011-05-24 23:12:57 +00001062 continue;
Chandler Carruthcdf47882014-03-09 03:16:01 +00001063 Worklist.push_back(UI);
Evan Cheng1b55f562011-05-24 23:12:57 +00001064 }
Andrew Trick4104ed92012-04-10 05:14:37 +00001065
Jakub Staszak27da1232013-08-06 17:03:42 +00001066 for (std::vector<Instruction*>::iterator UI = Worklist.begin(),
1067 UE = Worklist.end(); UI != UE; ++UI)
Andrew Trick4104ed92012-04-10 05:14:37 +00001068 (*UI)->replaceUsesOfWith(LIC, Replacement);
1069
Chris Lattnerc832c1b2010-04-05 21:18:32 +00001070 SimplifyCode(Worklist, L);
1071 return;
1072 }
Andrew Trick4104ed92012-04-10 05:14:37 +00001073
Chris Lattnerc832c1b2010-04-05 21:18:32 +00001074 // Otherwise, we don't know the precise value of LIC, but we do know that it
1075 // is certainly NOT "Val". As such, simplify any uses in the loop that we
1076 // can. This case occurs when we unswitch switch statements.
Chandler Carruthcdf47882014-03-09 03:16:01 +00001077 for (User *U : LIC->users()) {
1078 Instruction *UI = dyn_cast<Instruction>(U);
1079 if (!UI || !L->contains(UI))
Chris Lattnerc832c1b2010-04-05 21:18:32 +00001080 continue;
Chris Lattner6fd13622006-02-17 00:31:07 +00001081
Chandler Carruthcdf47882014-03-09 03:16:01 +00001082 Worklist.push_back(UI);
Chris Lattner6fd13622006-02-17 00:31:07 +00001083
Andrew Trick4104ed92012-04-10 05:14:37 +00001084 // TODO: We could do other simplifications, for example, turning
Chris Lattnerc832c1b2010-04-05 21:18:32 +00001085 // 'icmp eq LIC, Val' -> false.
1086
1087 // If we know that LIC is not Val, use this info to simplify code.
Chandler Carruthcdf47882014-03-09 03:16:01 +00001088 SwitchInst *SI = dyn_cast<SwitchInst>(UI);
Craig Topperf40110f2014-04-25 05:29:35 +00001089 if (!SI || !isa<ConstantInt>(Val)) continue;
Andrew Trick4104ed92012-04-10 05:14:37 +00001090
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001091 SwitchInst::CaseIt DeadCase = SI->findCaseValue(cast<ConstantInt>(Val));
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00001092 // Default case is live for multiple values.
Stepan Dyatkovskiy97b02fc2012-03-11 06:09:17 +00001093 if (DeadCase == SI->case_default()) continue;
Andrew Trick4104ed92012-04-10 05:14:37 +00001094
1095 // Found a dead case value. Don't remove PHI nodes in the
Chris Lattnerc832c1b2010-04-05 21:18:32 +00001096 // successor if they become single-entry, those PHI nodes may
1097 // be in the Users list.
Nick Lewycky61158242011-06-03 06:27:15 +00001098
Evan Cheng1b55f562011-05-24 23:12:57 +00001099 BasicBlock *Switch = SI->getParent();
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001100 BasicBlock *SISucc = DeadCase.getCaseSuccessor();
Evan Cheng1b55f562011-05-24 23:12:57 +00001101 BasicBlock *Latch = L->getLoopLatch();
Andrew Trick4104ed92012-04-10 05:14:37 +00001102
Stepan Dyatkovskiycb2adbac2012-01-15 09:44:07 +00001103 BranchesInfo.setUnswitched(SI, Val);
Andrew Trick4104ed92012-04-10 05:14:37 +00001104
Nick Lewycky61158242011-06-03 06:27:15 +00001105 if (!SI->findCaseDest(SISucc)) continue; // Edge is critical.
Evan Cheng9605a692011-05-25 18:17:13 +00001106 // If the DeadCase successor dominates the loop latch, then the
1107 // transformation isn't safe since it will delete the sole predecessor edge
1108 // to the latch.
1109 if (Latch && DT->dominates(SISucc, Latch))
1110 continue;
Evan Cheng1b55f562011-05-24 23:12:57 +00001111
Chris Lattnerc832c1b2010-04-05 21:18:32 +00001112 // FIXME: This is a hack. We need to keep the successor around
1113 // and hooked up so as to preserve the loop structure, because
1114 // trying to update it is complicated. So instead we preserve the
1115 // loop structure and put the block on a dead code path.
Chandler Carruthd4500562015-01-19 12:36:53 +00001116 SplitEdge(Switch, SISucc, DT, LI);
Chris Lattnerc832c1b2010-04-05 21:18:32 +00001117 // Compute the successors instead of relying on the return value
1118 // of SplitEdge, since it may have split the switch successor
1119 // after PHI nodes.
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001120 BasicBlock *NewSISucc = DeadCase.getCaseSuccessor();
Chris Lattnerc832c1b2010-04-05 21:18:32 +00001121 BasicBlock *OldSISucc = *succ_begin(NewSISucc);
1122 // Create an "unreachable" destination.
1123 BasicBlock *Abort = BasicBlock::Create(Context, "us-unreachable",
1124 Switch->getParent(),
1125 OldSISucc);
1126 new UnreachableInst(Context, Abort);
1127 // Force the new case destination to branch to the "unreachable"
1128 // block while maintaining a (dead) CFG edge to the old block.
1129 NewSISucc->getTerminator()->eraseFromParent();
1130 BranchInst::Create(Abort, OldSISucc,
1131 ConstantInt::getTrue(Context), NewSISucc);
1132 // Release the PHI operands for this edge.
1133 for (BasicBlock::iterator II = NewSISucc->begin();
1134 PHINode *PN = dyn_cast<PHINode>(II); ++II)
1135 PN->setIncomingValue(PN->getBasicBlockIndex(Switch),
1136 UndefValue::get(PN->getType()));
1137 // Tell the domtree about the new block. We don't fully update the
1138 // domtree here -- instead we force it to do a full recomputation
1139 // after the pass is complete -- but we do need to inform it of
1140 // new blocks.
1141 if (DT)
1142 DT->addNewBlock(Abort, NewSISucc);
Chris Lattner6fd13622006-02-17 00:31:07 +00001143 }
Andrew Trick4104ed92012-04-10 05:14:37 +00001144
Devang Pateld4911982007-07-31 08:03:26 +00001145 SimplifyCode(Worklist, L);
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +00001146}
1147
Mike Stumpdeaf5722009-09-09 17:57:16 +00001148/// SimplifyCode - Okay, now that we have simplified some instructions in the
Chris Lattnerc2e3a7a2006-02-18 07:57:38 +00001149/// loop, walk over it and constant prop, dce, and fold control flow where
1150/// possible. Note that this is effectively a very simple loop-structure-aware
1151/// optimizer. During processing of this loop, L could very well be deleted, so
1152/// it must not be used.
1153///
1154/// FIXME: When the loop optimizer is more mature, separate this out to a new
1155/// pass.
1156///
Devang Pateld4911982007-07-31 08:03:26 +00001157void LoopUnswitch::SimplifyCode(std::vector<Instruction*> &Worklist, Loop *L) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001158 const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
Chris Lattner6fd13622006-02-17 00:31:07 +00001159 while (!Worklist.empty()) {
1160 Instruction *I = Worklist.back();
1161 Worklist.pop_back();
Duncan Sandsbb2cd022010-11-23 20:24:21 +00001162
Chris Lattner6fd13622006-02-17 00:31:07 +00001163 // Simple DCE.
1164 if (isInstructionTriviallyDead(I)) {
David Greened9c355d2010-01-05 01:27:04 +00001165 DEBUG(dbgs() << "Remove dead instruction '" << *I);
Andrew Trick4104ed92012-04-10 05:14:37 +00001166
Chris Lattner6fd13622006-02-17 00:31:07 +00001167 // Add uses to the worklist, which may be dead now.
1168 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1169 if (Instruction *Use = dyn_cast<Instruction>(I->getOperand(i)))
1170 Worklist.push_back(Use);
Devang Pateld4911982007-07-31 08:03:26 +00001171 LPM->deleteSimpleAnalysisValue(I, L);
Chris Lattner6fd13622006-02-17 00:31:07 +00001172 RemoveFromWorklist(I, Worklist);
Devang Patel83cc3f82007-09-20 23:45:50 +00001173 I->eraseFromParent();
Chris Lattner6fd13622006-02-17 00:31:07 +00001174 ++NumSimplify;
1175 continue;
1176 }
Duncan Sandsaef146b2010-11-18 19:59:41 +00001177
Chris Lattner66e809a2010-04-20 05:33:18 +00001178 // See if instruction simplification can hack this up. This is common for
1179 // things like "select false, X, Y" after unswitching made the condition be
Peter Collingbourne9a03c732012-05-20 01:32:09 +00001180 // 'false'. TODO: update the domtree properly so we can pass it here.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001181 if (Value *V = SimplifyInstruction(I, DL))
Duncan Sandsaef146b2010-11-18 19:59:41 +00001182 if (LI->replacementPreservesLCSSAForm(I, V)) {
1183 ReplaceUsesOfWith(I, V, Worklist, L, LPM);
1184 continue;
1185 }
1186
Chris Lattner6fd13622006-02-17 00:31:07 +00001187 // Special case hacks that appear commonly in unswitched code.
Chris Lattner66e809a2010-04-20 05:33:18 +00001188 if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
Chris Lattner6fd13622006-02-17 00:31:07 +00001189 if (BI->isUnconditional()) {
1190 // If BI's parent is the only pred of the successor, fold the two blocks
1191 // together.
1192 BasicBlock *Pred = BI->getParent();
1193 BasicBlock *Succ = BI->getSuccessor(0);
1194 BasicBlock *SinglePred = Succ->getSinglePredecessor();
1195 if (!SinglePred) continue; // Nothing to do.
1196 assert(SinglePred == Pred && "CFG broken");
1197
Andrew Trick4104ed92012-04-10 05:14:37 +00001198 DEBUG(dbgs() << "Merging blocks: " << Pred->getName() << " <- "
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +00001199 << Succ->getName() << "\n");
Andrew Trick4104ed92012-04-10 05:14:37 +00001200
Chris Lattner6fd13622006-02-17 00:31:07 +00001201 // Resolve any single entry PHI nodes in Succ.
1202 while (PHINode *PN = dyn_cast<PHINode>(Succ->begin()))
Devang Pateld4911982007-07-31 08:03:26 +00001203 ReplaceUsesOfWith(PN, PN->getIncomingValue(0), Worklist, L, LPM);
Andrew Trick4104ed92012-04-10 05:14:37 +00001204
Jay Foad61ea0e42011-06-23 09:09:15 +00001205 // If Succ has any successors with PHI nodes, update them to have
1206 // entries coming from Pred instead of Succ.
1207 Succ->replaceAllUsesWith(Pred);
Andrew Trick4104ed92012-04-10 05:14:37 +00001208
Chris Lattner6fd13622006-02-17 00:31:07 +00001209 // Move all of the successor contents from Succ to Pred.
1210 Pred->getInstList().splice(BI, Succ->getInstList(), Succ->begin(),
1211 Succ->end());
Devang Pateld4911982007-07-31 08:03:26 +00001212 LPM->deleteSimpleAnalysisValue(BI, L);
Devang Patel83cc3f82007-09-20 23:45:50 +00001213 BI->eraseFromParent();
Chris Lattner6fd13622006-02-17 00:31:07 +00001214 RemoveFromWorklist(BI, Worklist);
Andrew Trick4104ed92012-04-10 05:14:37 +00001215
Chris Lattner6fd13622006-02-17 00:31:07 +00001216 // Remove Succ from the loop tree.
1217 LI->removeBlock(Succ);
Devang Pateld4911982007-07-31 08:03:26 +00001218 LPM->deleteSimpleAnalysisValue(Succ, L);
Devang Patel83cc3f82007-09-20 23:45:50 +00001219 Succ->eraseFromParent();
Chris Lattner29f771b2006-02-18 01:27:45 +00001220 ++NumSimplify;
Chris Lattner66e809a2010-04-20 05:33:18 +00001221 continue;
Chris Lattnerc832c1b2010-04-05 21:18:32 +00001222 }
Andrew Trick4104ed92012-04-10 05:14:37 +00001223
Chris Lattner66e809a2010-04-20 05:33:18 +00001224 continue;
Chris Lattner6fd13622006-02-17 00:31:07 +00001225 }
1226 }
Chris Lattnerf48f7772004-04-19 18:07:02 +00001227}