blob: a78d77fa6c06f435af604836f437cbb179a4983e [file] [log] [blame]
Chris Lattner6ec05f52002-05-10 22:44:58 +00001//===-- LICM.cpp - Loop Invariant Code Motion Pass ------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +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//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner6ec05f52002-05-10 22:44:58 +00009//
Chris Lattnerc0517682003-12-09 17:18:00 +000010// This pass performs loop invariant code motion, attempting to remove as much
11// code from the body of a loop as possible. It does this by either hoisting
12// code into the preheader block, or by sinking code to the exit blocks if it is
13// safe. This pass also promotes must-aliased memory locations in the loop to
Chris Lattner547192d62003-12-19 07:22:45 +000014// live in registers, thus hoisting and sinking "invariant" loads and stores.
Chris Lattnerc0517682003-12-09 17:18:00 +000015//
16// This pass uses alias analysis for two purposes:
Chris Lattner45d67d62003-02-24 03:52:32 +000017//
Chris Lattner289ba2a2004-05-23 21:20:19 +000018// 1. Moving loop invariant loads and calls out of loops. If we can determine
19// that a load or call inside of a loop never aliases anything stored to,
20// we can hoist it or sink it like any other instruction.
Chris Lattner45d67d62003-02-24 03:52:32 +000021// 2. Scalar Promotion of Memory - If there is a store instruction inside of
22// the loop, we try to move the store to happen AFTER the loop instead of
23// inside of the loop. This can only happen if a few conditions are true:
24// A. The pointer stored through is loop invariant
25// B. There are no stores or loads in the loop which _may_ alias the
26// pointer. There are no calls in the loop which mod/ref the pointer.
27// If these conditions are true, we can promote the loads and stores in the
28// loop of the pointer to use a temporary alloca'd variable. We then use
Chris Lattner1dc98b42010-08-29 06:43:52 +000029// the SSAUpdater to construct the appropriate SSA form for the value.
Chris Lattner6ec05f52002-05-10 22:44:58 +000030//
Chris Lattner6ec05f52002-05-10 22:44:58 +000031//===----------------------------------------------------------------------===//
32
33#include "llvm/Transforms/Scalar.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/ADT/Statistic.h"
Chris Lattnera51fa882002-08-22 21:39:55 +000035#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner0592bb72003-03-03 23:32:45 +000036#include "llvm/Analysis/AliasSetTracker.h"
Chris Lattner030f0202010-08-31 23:00:16 +000037#include "llvm/Analysis/ConstantFolding.h"
38#include "llvm/Analysis/LoopInfo.h"
39#include "llvm/Analysis/LoopPass.h"
Chandler Carruthabfa3e52014-01-24 01:59:49 +000040#include "llvm/Analysis/ScalarEvolution.h"
Dan Gohman75d7d5e2011-12-14 23:49:11 +000041#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000042#include "llvm/IR/CFG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000043#include "llvm/IR/Constants.h"
44#include "llvm/IR/DataLayout.h"
45#include "llvm/IR/DerivedTypes.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000046#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000047#include "llvm/IR/Instructions.h"
48#include "llvm/IR/IntrinsicInst.h"
49#include "llvm/IR/LLVMContext.h"
Chris Lattner473988c2013-01-05 16:44:07 +000050#include "llvm/IR/Metadata.h"
Chandler Carruthaa0ab632014-03-04 12:09:19 +000051#include "llvm/IR/PredIteratorCache.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000052#include "llvm/Support/CommandLine.h"
53#include "llvm/Support/Debug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000054#include "llvm/Support/raw_ostream.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000055#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000056#include "llvm/Transforms/Utils/Local.h"
Chandler Carruth8765cf72014-01-25 04:07:24 +000057#include "llvm/Transforms/Utils/LoopUtils.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000058#include "llvm/Transforms/Utils/SSAUpdater.h"
Chris Lattner6ec05f52002-05-10 22:44:58 +000059#include <algorithm>
Chris Lattnerc0517682003-12-09 17:18:00 +000060using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000061
Chandler Carruth964daaa2014-04-22 02:55:47 +000062#define DEBUG_TYPE "licm"
63
Chris Lattner79a42ac2006-12-19 21:40:18 +000064STATISTIC(NumSunk , "Number of instructions sunk out of loop");
65STATISTIC(NumHoisted , "Number of instructions hoisted out of loop");
66STATISTIC(NumMovedLoads, "Number of load insts hoisted or sunk");
67STATISTIC(NumMovedCalls, "Number of call insts hoisted or sunk");
68STATISTIC(NumPromoted , "Number of memory locations promoted to registers");
69
Dan Gohmand78c4002008-05-13 00:00:25 +000070static cl::opt<bool>
71DisablePromotion("disable-licm-promotion", cl::Hidden,
72 cl::desc("Disable memory promotion in LICM pass"));
Chris Lattner45d67d62003-02-24 03:52:32 +000073
Dan Gohmand78c4002008-05-13 00:00:25 +000074namespace {
Chris Lattner2dd09db2009-09-02 06:11:42 +000075 struct LICM : public LoopPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000076 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000077 LICM() : LoopPass(ID) {
78 initializeLICMPass(*PassRegistry::getPassRegistry());
79 }
Devang Patel09f162c2007-05-01 21:15:47 +000080
Craig Topper3e4c6972014-03-05 09:10:37 +000081 bool runOnLoop(Loop *L, LPPassManager &LPM) override;
Chris Lattner6ec05f52002-05-10 22:44:58 +000082
Chris Lattnerf64f2d32002-09-26 16:52:07 +000083 /// This transformation requires natural loop information & requires that
84 /// loop preheaders be inserted into the CFG...
85 ///
Craig Topper3e4c6972014-03-05 09:10:37 +000086 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner820d9712002-10-21 20:00:28 +000087 AU.setPreservesCFG();
Chandler Carruth73523022014-01-13 13:07:17 +000088 AU.addRequired<DominatorTreeWrapperPass>();
Chandler Carruth4f8f3072015-01-17 14:16:18 +000089 AU.addRequired<LoopInfoWrapperPass>();
Dan Gohmanefd7f9c2010-07-16 17:58:45 +000090 AU.addRequiredID(LoopSimplifyID);
Chandler Carruth8765cf72014-01-25 04:07:24 +000091 AU.addPreservedID(LoopSimplifyID);
92 AU.addRequiredID(LCSSAID);
93 AU.addPreservedID(LCSSAID);
Chris Lattnera51fa882002-08-22 21:39:55 +000094 AU.addRequired<AliasAnalysis>();
Chris Lattnerf94f6bb2010-08-29 07:02:56 +000095 AU.addPreserved<AliasAnalysis>();
Chandler Carruthabfa3e52014-01-24 01:59:49 +000096 AU.addPreserved<ScalarEvolution>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +000097 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chris Lattner6ec05f52002-05-10 22:44:58 +000098 }
99
Matt Beaumont-Gayabfc4462012-12-04 05:41:27 +0000100 using llvm::Pass::doFinalization;
101
Craig Topper3e4c6972014-03-05 09:10:37 +0000102 bool doFinalization() override {
Chris Lattnercc9cbc62010-08-29 17:46:00 +0000103 assert(LoopToAliasSetMap.empty() && "Didn't free loop alias sets");
Devang Patel69730c92007-03-07 04:41:30 +0000104 return false;
105 }
106
Chris Lattner6ec05f52002-05-10 22:44:58 +0000107 private:
Chris Lattner45d67d62003-02-24 03:52:32 +0000108 AliasAnalysis *AA; // Current AliasAnalysis information
Chris Lattnerc0517682003-12-09 17:18:00 +0000109 LoopInfo *LI; // Current LoopInfo
Chris Lattnerabe61ef2010-08-29 06:49:44 +0000110 DominatorTree *DT; // Dominator Tree for the current Loop.
Chris Lattnerc0517682003-12-09 17:18:00 +0000111
Rafael Espindolaaeff8a92014-02-24 23:12:18 +0000112 const DataLayout *DL; // DataLayout for constant folding.
Chad Rosier43a33062011-12-02 01:26:24 +0000113 TargetLibraryInfo *TLI; // TargetLibraryInfo for constant folding.
114
Chris Lattnercc9cbc62010-08-29 17:46:00 +0000115 // State that is updated as we process loops.
Chris Lattner45d67d62003-02-24 03:52:32 +0000116 bool Changed; // Set to true when we change anything.
117 BasicBlock *Preheader; // The preheader block of the current loop...
118 Loop *CurLoop; // The current loop we are working on...
Chris Lattner0592bb72003-03-03 23:32:45 +0000119 AliasSetTracker *CurAST; // AliasSet information for the current loop...
Nadav Rotem03dcd852012-09-04 10:25:04 +0000120 bool MayThrow; // The current loop contains an instruction which
121 // may throw, thus preventing code motion of
122 // instructions with side effects.
Philip Reamesb35f46c2014-12-29 23:00:57 +0000123 bool HeaderMayThrow; // Same as previous, but specific to loop header
Chris Lattnercc9cbc62010-08-29 17:46:00 +0000124 DenseMap<Loop*, AliasSetTracker*> LoopToAliasSetMap;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000125
Devang Patelb98a0972007-07-31 08:01:41 +0000126 /// cloneBasicBlockAnalysis - Simple Analysis hook. Clone alias set info.
Craig Topper3e4c6972014-03-05 09:10:37 +0000127 void cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To,
128 Loop *L) override;
Devang Patelb98a0972007-07-31 08:01:41 +0000129
130 /// deleteAnalysisValue - Simple Analysis hook. Delete value V from alias
131 /// set.
Craig Topper3e4c6972014-03-05 09:10:37 +0000132 void deleteAnalysisValue(Value *V, Loop *L) override;
Devang Patelb98a0972007-07-31 08:01:41 +0000133
David Peixotto0d4d5e62014-09-24 16:48:31 +0000134 /// Simple Analysis hook. Delete loop L from alias set map.
135 void deleteAnalysisLoop(Loop *L) override;
136
Chris Lattner547192d62003-12-19 07:22:45 +0000137 /// SinkRegion - Walk the specified region of the CFG (defined by all blocks
138 /// dominated by the specified block, and that are in the current loop) in
Owen Andersonc24701e2007-04-24 06:40:39 +0000139 /// reverse depth first order w.r.t the DominatorTree. This allows us to
Chris Lattner547192d62003-12-19 07:22:45 +0000140 /// visit uses before definitions, allowing us to sink a loop body in one
141 /// pass without iteration.
142 ///
Devang Patelbdd1aae2007-06-04 00:32:22 +0000143 void SinkRegion(DomTreeNode *N);
Chris Lattner547192d62003-12-19 07:22:45 +0000144
Chris Lattner64437692002-09-29 21:46:09 +0000145 /// HoistRegion - Walk the specified region of the CFG (defined by all
146 /// blocks dominated by the specified block, and that are in the current
Owen Andersonc24701e2007-04-24 06:40:39 +0000147 /// loop) in depth first order w.r.t the DominatorTree. This allows us to
Misha Brukman8b2bd4e2003-10-10 17:57:28 +0000148 /// visit definitions before uses, allowing us to hoist a loop body in one
Chris Lattner64437692002-09-29 21:46:09 +0000149 /// pass without iteration.
150 ///
Devang Patelbdd1aae2007-06-04 00:32:22 +0000151 void HoistRegion(DomTreeNode *N);
Chris Lattner64437692002-09-29 21:46:09 +0000152
Chris Lattner05e86302002-09-29 22:26:07 +0000153 /// inSubLoop - Little predicate that returns true if the specified basic
154 /// block is in a subloop of the current one, not the current one itself.
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000155 ///
Chris Lattner05e86302002-09-29 22:26:07 +0000156 bool inSubLoop(BasicBlock *BB) {
157 assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop");
Chris Lattner0cdc6f62011-01-02 18:53:08 +0000158 return LI->getLoopFor(BB) != CurLoop;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000159 }
160
Chris Lattneraaaea512003-12-10 06:41:05 +0000161 /// sink - When an instruction is found to only be used outside of the loop,
162 /// this function moves it to the exit blocks and patches up SSA form as
163 /// needed.
164 ///
165 void sink(Instruction &I);
166
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000167 /// hoist - When an instruction is found to only use loop invariant operands
168 /// that is safe to hoist, this instruction is called to do the dirty work.
169 ///
Chris Lattner113f4f42002-06-25 16:13:24 +0000170 void hoist(Instruction &I);
Chris Lattner6ec05f52002-05-10 22:44:58 +0000171
Chris Lattneraaaea512003-12-10 06:41:05 +0000172 /// isSafeToExecuteUnconditionally - Only sink or hoist an instruction if it
173 /// is not a trapping instruction or if it is a trapping instruction and is
174 /// guaranteed to execute.
Tanya Lattner57c03df2003-08-05 18:45:46 +0000175 ///
Chris Lattneraaaea512003-12-10 06:41:05 +0000176 bool isSafeToExecuteUnconditionally(Instruction &I);
Tanya Lattner57c03df2003-08-05 18:45:46 +0000177
Eli Friedman0cdc1482011-07-20 21:37:47 +0000178 /// isGuaranteedToExecute - Check that the instruction is guaranteed to
179 /// execute.
180 ///
181 bool isGuaranteedToExecute(Instruction &I);
182
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000183 /// pointerInvalidatedByLoop - Return true if the body of this loop may
184 /// store into the memory location pointed to by V.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000185 ///
Dan Gohmanf372cf82010-10-19 22:54:46 +0000186 bool pointerInvalidatedByLoop(Value *V, uint64_t Size,
Hal Finkelcc39b672014-07-24 12:16:19 +0000187 const AAMDNodes &AAInfo) {
Chris Lattner0592bb72003-03-03 23:32:45 +0000188 // Check to see if any of the basic blocks in CurLoop invalidate *V.
Hal Finkelcc39b672014-07-24 12:16:19 +0000189 return CurAST->getAliasSetForPointer(V, Size, AAInfo).isMod();
Chris Lattner45d67d62003-02-24 03:52:32 +0000190 }
Chris Lattnera51fa882002-08-22 21:39:55 +0000191
Chris Lattneraaaea512003-12-10 06:41:05 +0000192 bool canSinkOrHoistInst(Instruction &I);
Chris Lattneraaaea512003-12-10 06:41:05 +0000193 bool isNotUsedInLoop(Instruction &I);
Chris Lattner6ec05f52002-05-10 22:44:58 +0000194
Dan Gohmanb9487362012-08-08 00:00:26 +0000195 void PromoteAliasSet(AliasSet &AS,
196 SmallVectorImpl<BasicBlock*> &ExitBlocks,
Chandler Carruthfc258542014-02-11 12:52:27 +0000197 SmallVectorImpl<Instruction*> &InsertPts,
198 PredIteratorCache &PIC);
Evgeniy Stepanovd99cca22014-06-25 09:17:21 +0000199
200 /// \brief Create a copy of the instruction in the exit block and patch up
201 /// SSA.
202 /// PN is a user of I in ExitBlock that can be used to get the number and
203 /// list of predecessors fast.
204 Instruction *CloneInstructionInExitBlock(Instruction &I,
205 BasicBlock &ExitBlock,
206 PHINode &PN);
Chris Lattner6ec05f52002-05-10 22:44:58 +0000207 };
208}
209
Dan Gohmand78c4002008-05-13 00:00:25 +0000210char LICM::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +0000211INITIALIZE_PASS_BEGIN(LICM, "licm", "Loop Invariant Code Motion", false, false)
Chandler Carruth73523022014-01-13 13:07:17 +0000212INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000213INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000214INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
Chandler Carruth8765cf72014-01-25 04:07:24 +0000215INITIALIZE_PASS_DEPENDENCY(LCSSA)
216INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000217INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000218INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
219INITIALIZE_PASS_END(LICM, "licm", "Loop Invariant Code Motion", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +0000220
Daniel Dunbar7f39e2d2008-10-22 23:32:42 +0000221Pass *llvm::createLICMPass() { return new LICM(); }
Chris Lattner6ec05f52002-05-10 22:44:58 +0000222
Devang Pateld8b1ceb2007-07-31 16:52:25 +0000223/// Hoist expressions out of the specified loop. Note, alias info for inner
Tobias Grossera3928f52011-07-06 19:20:02 +0000224/// loop is not preserved so it is not a good idea to run LICM multiple
Devang Pateld8b1ceb2007-07-31 16:52:25 +0000225/// times on one loop.
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000226///
Devang Patel69730c92007-03-07 04:41:30 +0000227bool LICM::runOnLoop(Loop *L, LPPassManager &LPM) {
Paul Robinsonaf4e64d2014-02-06 00:07:05 +0000228 if (skipOptnoneFunction(L))
229 return false;
230
Chris Lattner45d67d62003-02-24 03:52:32 +0000231 Changed = false;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000232
Chris Lattner45d67d62003-02-24 03:52:32 +0000233 // Get our Loop and Alias Analysis information...
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000234 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Chris Lattnera51fa882002-08-22 21:39:55 +0000235 AA = &getAnalysis<AliasAnalysis>();
Chandler Carruth73523022014-01-13 13:07:17 +0000236 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Chris Lattnera51fa882002-08-22 21:39:55 +0000237
Rafael Espindola93512512014-02-25 17:30:31 +0000238 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
Craig Topperf40110f2014-04-25 05:29:35 +0000239 DL = DLP ? &DLP->getDataLayout() : nullptr;
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000240 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chad Rosier43a33062011-12-02 01:26:24 +0000241
Chandler Carruthfc258542014-02-11 12:52:27 +0000242 assert(L->isLCSSAForm(*DT) && "Loop is not in LCSSA form.");
243
Devang Patel69730c92007-03-07 04:41:30 +0000244 CurAST = new AliasSetTracker(*AA);
Chris Lattnercc9cbc62010-08-29 17:46:00 +0000245 // Collect Alias info from subloops.
Devang Patel69730c92007-03-07 04:41:30 +0000246 for (Loop::iterator LoopItr = L->begin(), LoopItrE = L->end();
247 LoopItr != LoopItrE; ++LoopItr) {
248 Loop *InnerL = *LoopItr;
Chris Lattnercc9cbc62010-08-29 17:46:00 +0000249 AliasSetTracker *InnerAST = LoopToAliasSetMap[InnerL];
250 assert(InnerAST && "Where is my AST?");
Devang Patel69730c92007-03-07 04:41:30 +0000251
252 // What if InnerLoop was modified by other passes ?
253 CurAST->add(*InnerAST);
Tobias Grossera3928f52011-07-06 19:20:02 +0000254
Chris Lattnercc9cbc62010-08-29 17:46:00 +0000255 // Once we've incorporated the inner loop's AST into ours, we don't need the
256 // subloop's anymore.
257 delete InnerAST;
258 LoopToAliasSetMap.erase(InnerL);
Chris Lattner45d67d62003-02-24 03:52:32 +0000259 }
Tobias Grossera3928f52011-07-06 19:20:02 +0000260
Chris Lattner6ec05f52002-05-10 22:44:58 +0000261 CurLoop = L;
262
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000263 // Get the preheader block to move instructions into...
264 Preheader = L->getLoopPreheader();
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000265
Chris Lattner45d67d62003-02-24 03:52:32 +0000266 // Loop over the body of this loop, looking for calls, invokes, and stores.
Chris Lattner0592bb72003-03-03 23:32:45 +0000267 // Because subloops have already been incorporated into AST, we skip blocks in
Chris Lattner45d67d62003-02-24 03:52:32 +0000268 // subloops.
269 //
Dan Gohman90071072008-06-22 20:18:58 +0000270 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
271 I != E; ++I) {
272 BasicBlock *BB = *I;
Chris Lattnercc9cbc62010-08-29 17:46:00 +0000273 if (LI->getLoopFor(BB) == L) // Ignore blocks in subloops.
Dan Gohman90071072008-06-22 20:18:58 +0000274 CurAST->add(*BB); // Incorporate the specified basic block
275 }
Chris Lattner45d67d62003-02-24 03:52:32 +0000276
Philip Reamesb35f46c2014-12-29 23:00:57 +0000277 HeaderMayThrow = false;
278 BasicBlock *Header = L->getHeader();
279 for (BasicBlock::iterator I = Header->begin(), E = Header->end();
280 (I != E) && !HeaderMayThrow; ++I)
281 HeaderMayThrow |= I->mayThrow();
282 MayThrow = HeaderMayThrow;
Nadav Rotem03dcd852012-09-04 10:25:04 +0000283 // TODO: We've already searched for instructions which may throw in subloops.
284 // We may want to reuse this information.
285 for (Loop::block_iterator BB = L->block_begin(), BBE = L->block_end();
286 (BB != BBE) && !MayThrow ; ++BB)
287 for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end();
288 (I != E) && !MayThrow; ++I)
289 MayThrow |= I->mayThrow();
290
Chris Lattner6ec05f52002-05-10 22:44:58 +0000291 // We want to visit all of the instructions in this loop... that are not parts
292 // of our subloops (they have already had their invariants hoisted out of
293 // their loop, into this loop, so there is no need to process the BODIES of
294 // the subloops).
295 //
Chris Lattner64437692002-09-29 21:46:09 +0000296 // Traverse the body of the loop in depth first order on the dominator tree so
297 // that we are guaranteed to see definitions before we see uses. This allows
Nick Lewyckya0d49da2007-08-18 15:08:56 +0000298 // us to sink instructions in one pass, without iteration. After sinking
Chris Lattner547192d62003-12-19 07:22:45 +0000299 // instructions, we perform another pass to hoist them out of the loop.
Chris Lattner64437692002-09-29 21:46:09 +0000300 //
Dan Gohmana83ac2d2009-11-05 21:11:53 +0000301 if (L->hasDedicatedExits())
302 SinkRegion(DT->getNode(L->getHeader()));
303 if (Preheader)
304 HoistRegion(DT->getNode(L->getHeader()));
Chris Lattner6ec05f52002-05-10 22:44:58 +0000305
Chris Lattner45d67d62003-02-24 03:52:32 +0000306 // Now that all loop invariants have been removed from the loop, promote any
Chris Lattner1dc98b42010-08-29 06:43:52 +0000307 // memory references to scalars that we can.
Chandler Carruthcc497b62014-01-24 02:24:47 +0000308 if (!DisablePromotion && (Preheader || L->hasDedicatedExits())) {
Dan Gohmanb9487362012-08-08 00:00:26 +0000309 SmallVector<BasicBlock *, 8> ExitBlocks;
310 SmallVector<Instruction *, 8> InsertPts;
Chandler Carruthfc258542014-02-11 12:52:27 +0000311 PredIteratorCache PIC;
Dan Gohmanb9487362012-08-08 00:00:26 +0000312
Chris Lattner1dc98b42010-08-29 06:43:52 +0000313 // Loop over all of the alias sets in the tracker object.
314 for (AliasSetTracker::iterator I = CurAST->begin(), E = CurAST->end();
315 I != E; ++I)
Chandler Carruthfc258542014-02-11 12:52:27 +0000316 PromoteAliasSet(*I, ExitBlocks, InsertPts, PIC);
Chandler Carruth16651522014-02-01 13:35:14 +0000317
318 // Once we have promoted values across the loop body we have to recursively
319 // reform LCSSA as any nested loop may now have values defined within the
320 // loop used in the outer loop.
321 // FIXME: This is really heavy handed. It would be a bit better to use an
322 // SSAUpdater strategy during promotion that was LCSSA aware and reformed
323 // it as it went.
324 if (Changed)
Bruno Cardoso Lopesbad65c32014-12-22 22:35:46 +0000325 formLCSSARecursively(*L, *DT, LI,
326 getAnalysisIfAvailable<ScalarEvolution>());
Chris Lattner1dc98b42010-08-29 06:43:52 +0000327 }
Tobias Grossera3928f52011-07-06 19:20:02 +0000328
Chandler Carruthfc258542014-02-11 12:52:27 +0000329 // Check that neither this loop nor its parent have had LCSSA broken. LICM is
330 // specifically moving instructions across the loop boundary and so it is
331 // especially in need of sanity checking here.
332 assert(L->isLCSSAForm(*DT) && "Loop not left in LCSSA form after LICM!");
333 assert((!L->getParentLoop() || L->getParentLoop()->isLCSSAForm(*DT)) &&
334 "Parent loop not left in LCSSA form after LICM!");
Chandler Carruth8765cf72014-01-25 04:07:24 +0000335
Chris Lattner6ec05f52002-05-10 22:44:58 +0000336 // Clear out loops state information for the next iteration
Craig Topperf40110f2014-04-25 05:29:35 +0000337 CurLoop = nullptr;
338 Preheader = nullptr;
Devang Patel69730c92007-03-07 04:41:30 +0000339
Chris Lattnercc9cbc62010-08-29 17:46:00 +0000340 // If this loop is nested inside of another one, save the alias information
341 // for when we process the outer loop.
342 if (L->getParentLoop())
343 LoopToAliasSetMap[L] = CurAST;
344 else
345 delete CurAST;
Devang Patel69730c92007-03-07 04:41:30 +0000346 return Changed;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000347}
348
Chris Lattner547192d62003-12-19 07:22:45 +0000349/// SinkRegion - Walk the specified region of the CFG (defined by all blocks
350/// dominated by the specified block, and that are in the current loop) in
Owen Andersonc24701e2007-04-24 06:40:39 +0000351/// reverse depth first order w.r.t the DominatorTree. This allows us to visit
Chris Lattner547192d62003-12-19 07:22:45 +0000352/// uses before definitions, allowing us to sink a loop body in one pass without
353/// iteration.
354///
Devang Patelbdd1aae2007-06-04 00:32:22 +0000355void LICM::SinkRegion(DomTreeNode *N) {
Craig Toppere73658d2014-04-28 04:05:08 +0000356 assert(N != nullptr && "Null dominator tree node?");
Owen Andersonc24701e2007-04-24 06:40:39 +0000357 BasicBlock *BB = N->getBlock();
Chris Lattner547192d62003-12-19 07:22:45 +0000358
359 // If this subregion is not in the top level loop at all, exit.
360 if (!CurLoop->contains(BB)) return;
361
Chris Lattner263f8042010-08-29 18:22:25 +0000362 // We are processing blocks in reverse dfo, so process children first.
Devang Patelbdd1aae2007-06-04 00:32:22 +0000363 const std::vector<DomTreeNode*> &Children = N->getChildren();
Chris Lattner547192d62003-12-19 07:22:45 +0000364 for (unsigned i = 0, e = Children.size(); i != e; ++i)
365 SinkRegion(Children[i]);
366
367 // Only need to process the contents of this block if it is not part of a
368 // subloop (which would already have been processed).
369 if (inSubLoop(BB)) return;
370
Chris Lattner91846012003-12-19 08:18:16 +0000371 for (BasicBlock::iterator II = BB->end(); II != BB->begin(); ) {
372 Instruction &I = *--II;
Tobias Grossera3928f52011-07-06 19:20:02 +0000373
Chris Lattner263f8042010-08-29 18:22:25 +0000374 // If the instruction is dead, we would try to sink it because it isn't used
375 // in the loop, instead, just delete it.
Benjamin Kramer8bcc9712012-08-29 15:32:21 +0000376 if (isInstructionTriviallyDead(&I, TLI)) {
Chris Lattnerf58382e2010-08-29 18:42:23 +0000377 DEBUG(dbgs() << "LICM deleting dead inst: " << I << '\n');
Chris Lattner263f8042010-08-29 18:22:25 +0000378 ++II;
379 CurAST->deleteValue(&I);
380 I.eraseFromParent();
381 Changed = true;
382 continue;
383 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000384
Chris Lattner547192d62003-12-19 07:22:45 +0000385 // Check to see if we can sink this instruction to the exit blocks
386 // of the loop. We can do this if the all users of the instruction are
387 // outside of the loop. In this case, it doesn't even matter if the
388 // operands of the instruction are loop invariant.
389 //
Chris Lattnerfaf77912005-03-25 00:22:36 +0000390 if (isNotUsedInLoop(I) && canSinkOrHoistInst(I)) {
Chris Lattner91846012003-12-19 08:18:16 +0000391 ++II;
Chris Lattner547192d62003-12-19 07:22:45 +0000392 sink(I);
Chris Lattner91846012003-12-19 08:18:16 +0000393 }
Chris Lattner547192d62003-12-19 07:22:45 +0000394 }
395}
396
Chris Lattner64437692002-09-29 21:46:09 +0000397/// HoistRegion - Walk the specified region of the CFG (defined by all blocks
398/// dominated by the specified block, and that are in the current loop) in depth
Owen Andersonc24701e2007-04-24 06:40:39 +0000399/// first order w.r.t the DominatorTree. This allows us to visit definitions
Chris Lattner64437692002-09-29 21:46:09 +0000400/// before uses, allowing us to hoist a loop body in one pass without iteration.
401///
Devang Patelbdd1aae2007-06-04 00:32:22 +0000402void LICM::HoistRegion(DomTreeNode *N) {
Craig Toppere73658d2014-04-28 04:05:08 +0000403 assert(N != nullptr && "Null dominator tree node?");
Owen Andersonc24701e2007-04-24 06:40:39 +0000404 BasicBlock *BB = N->getBlock();
Chris Lattner64437692002-09-29 21:46:09 +0000405
Chris Lattner05e86302002-09-29 22:26:07 +0000406 // If this subregion is not in the top level loop at all, exit.
Chris Lattner65c11932003-12-09 19:32:44 +0000407 if (!CurLoop->contains(BB)) return;
Chris Lattner64437692002-09-29 21:46:09 +0000408
Chris Lattneraaaea512003-12-10 06:41:05 +0000409 // Only need to process the contents of this block if it is not part of a
410 // subloop (which would already have been processed).
Chris Lattner65c11932003-12-09 19:32:44 +0000411 if (!inSubLoop(BB))
Chris Lattneraaaea512003-12-10 06:41:05 +0000412 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ) {
413 Instruction &I = *II++;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000414
Chris Lattner030f0202010-08-31 23:00:16 +0000415 // Try constant folding this instruction. If all the operands are
416 // constants, it is technically hoistable, but it would be better to just
417 // fold it.
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000418 if (Constant *C = ConstantFoldInstruction(&I, DL, TLI)) {
Chris Lattner030f0202010-08-31 23:00:16 +0000419 DEBUG(dbgs() << "LICM folding inst: " << I << " --> " << *C << '\n');
420 CurAST->copyValue(&I, C);
421 CurAST->deleteValue(&I);
422 I.replaceAllUsesWith(C);
423 I.eraseFromParent();
424 continue;
425 }
Tobias Grossera3928f52011-07-06 19:20:02 +0000426
Chris Lattner547192d62003-12-19 07:22:45 +0000427 // Try hoisting the instruction out to the preheader. We can only do this
428 // if all of the operands of the instruction are loop invariant and if it
429 // is safe to hoist the instruction.
430 //
Chris Lattnerda24b9a2010-09-06 01:05:37 +0000431 if (CurLoop->hasLoopInvariantOperands(&I) && canSinkOrHoistInst(I) &&
Chris Lattner547192d62003-12-19 07:22:45 +0000432 isSafeToExecuteUnconditionally(I))
Chris Lattner49771a02006-06-26 19:10:05 +0000433 hoist(I);
Chris Lattner030f0202010-08-31 23:00:16 +0000434 }
Chris Lattner64437692002-09-29 21:46:09 +0000435
Devang Patelbdd1aae2007-06-04 00:32:22 +0000436 const std::vector<DomTreeNode*> &Children = N->getChildren();
Chris Lattner64437692002-09-29 21:46:09 +0000437 for (unsigned i = 0, e = Children.size(); i != e; ++i)
Chris Lattner42ad6462004-06-19 20:23:35 +0000438 HoistRegion(Children[i]);
Chris Lattner64437692002-09-29 21:46:09 +0000439}
440
Chris Lattneraaaea512003-12-10 06:41:05 +0000441/// canSinkOrHoistInst - Return true if the hoister and sinker can handle this
442/// instruction.
443///
444bool LICM::canSinkOrHoistInst(Instruction &I) {
Chris Lattner65c11932003-12-09 19:32:44 +0000445 // Loads have extra constraints we have to verify before we can hoist them.
446 if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
Eli Friedman91386c72011-08-15 20:52:09 +0000447 if (!LI->isUnordered())
448 return false; // Don't hoist volatile/atomic loads!
Chris Lattner65c11932003-12-09 19:32:44 +0000449
Chris Lattner8a8fb902008-07-23 05:06:28 +0000450 // Loads from constant memory are always safe to move, even if they end up
451 // in the same alias set as something that ends up being modified.
Dan Gohmancbc6ebb2009-11-19 19:00:10 +0000452 if (AA->pointsToConstantMemory(LI->getOperand(0)))
Chris Lattner8a8fb902008-07-23 05:06:28 +0000453 return true;
Philip Reames5a3f5f72014-10-21 00:13:20 +0000454 if (LI->getMetadata(LLVMContext::MD_invariant_load))
Pete Cooper9ee22092011-11-08 19:30:00 +0000455 return true;
Tobias Grossera3928f52011-07-06 19:20:02 +0000456
Chris Lattner65c11932003-12-09 19:32:44 +0000457 // Don't hoist loads which have may-aliased stores in loop.
Dan Gohmanf372cf82010-10-19 22:54:46 +0000458 uint64_t Size = 0;
Chris Lattnerb1374092004-11-26 21:20:09 +0000459 if (LI->getType()->isSized())
Dan Gohman43d19d62009-07-25 00:48:42 +0000460 Size = AA->getTypeStoreSize(LI->getType());
Hal Finkelcc39b672014-07-24 12:16:19 +0000461
462 AAMDNodes AAInfo;
463 LI->getAAMetadata(AAInfo);
464
465 return !pointerInvalidatedByLoop(LI->getOperand(0), Size, AAInfo);
Chris Lattner20cda262004-03-15 04:11:30 +0000466 } else if (CallInst *CI = dyn_cast<CallInst>(&I)) {
Eli Friedman942e1c12011-05-27 18:37:52 +0000467 // Don't sink or hoist dbg info; it's legal, but not useful.
468 if (isa<DbgInfoIntrinsic>(I))
469 return false;
470
471 // Handle simple cases by querying alias analysis.
Duncan Sands68b6f502007-12-01 07:51:45 +0000472 AliasAnalysis::ModRefBehavior Behavior = AA->getModRefBehavior(CI);
473 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
474 return true;
Dan Gohman0f175072010-11-09 19:58:21 +0000475 if (AliasAnalysis::onlyReadsMemory(Behavior)) {
Duncan Sands68b6f502007-12-01 07:51:45 +0000476 // If this call only reads from memory and there are no writes to memory
477 // in the loop, we can hoist or sink the call as appropriate.
478 bool FoundMod = false;
479 for (AliasSetTracker::iterator I = CurAST->begin(), E = CurAST->end();
480 I != E; ++I) {
481 AliasSet &AS = *I;
482 if (!AS.isForwardingAliasSet() && AS.isMod()) {
483 FoundMod = true;
484 break;
Chris Lattner20cda262004-03-15 04:11:30 +0000485 }
Chris Lattner20cda262004-03-15 04:11:30 +0000486 }
Duncan Sands68b6f502007-12-01 07:51:45 +0000487 if (!FoundMod) return true;
Chris Lattner20cda262004-03-15 04:11:30 +0000488 }
489
Nadav Rotem03dcd852012-09-04 10:25:04 +0000490 // FIXME: This should use mod/ref information to see if we can hoist or
491 // sink the call.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000492
Chris Lattner20cda262004-03-15 04:11:30 +0000493 return false;
Chris Lattner65c11932003-12-09 19:32:44 +0000494 }
495
Nadav Rotem03dcd852012-09-04 10:25:04 +0000496 // Only these instructions are hoistable/sinkable.
Benjamin Kramer130fcde2013-01-09 18:12:03 +0000497 if (!isa<BinaryOperator>(I) && !isa<CastInst>(I) && !isa<SelectInst>(I) &&
498 !isa<GetElementPtrInst>(I) && !isa<CmpInst>(I) &&
499 !isa<InsertElementInst>(I) && !isa<ExtractElementInst>(I) &&
500 !isa<ShuffleVectorInst>(I) && !isa<ExtractValueInst>(I) &&
501 !isa<InsertValueInst>(I))
502 return false;
Nadav Rotem03dcd852012-09-04 10:25:04 +0000503
504 return isSafeToExecuteUnconditionally(I);
Chris Lattneraaaea512003-12-10 06:41:05 +0000505}
506
Chandler Carruth8765cf72014-01-25 04:07:24 +0000507/// \brief Returns true if a PHINode is a trivially replaceable with an
508/// Instruction.
509///
510/// This is true when all incoming values are that instruction. This pattern
511/// occurs most often with LCSSA PHI nodes.
512static bool isTriviallyReplacablePHI(PHINode &PN, Instruction &I) {
513 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
514 if (PN.getIncomingValue(i) != &I)
515 return false;
516
517 return true;
518}
519
Chris Lattneraaaea512003-12-10 06:41:05 +0000520/// isNotUsedInLoop - Return true if the only users of this instruction are
521/// outside of the loop. If this is true, we can sink the instruction to the
522/// exit blocks of the loop.
523///
524bool LICM::isNotUsedInLoop(Instruction &I) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000525 for (User *U : I.users()) {
526 Instruction *UI = cast<Instruction>(U);
527 if (PHINode *PN = dyn_cast<PHINode>(UI)) {
Chandler Carruth8765cf72014-01-25 04:07:24 +0000528 // A PHI node where all of the incoming values are this instruction are
529 // special -- they can just be RAUW'ed with the instruction and thus
530 // don't require a use in the predecessor. This is a particular important
531 // special case because it is the pattern found in LCSSA form.
532 if (isTriviallyReplacablePHI(*PN, I)) {
533 if (CurLoop->contains(PN))
534 return false;
535 else
536 continue;
537 }
538
539 // Otherwise, PHI node uses occur in predecessor blocks if the incoming
540 // values. Check for such a use being inside the loop.
Chris Lattner34399dd2003-12-11 22:23:32 +0000541 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
542 if (PN->getIncomingValue(i) == &I)
543 if (CurLoop->contains(PN->getIncomingBlock(i)))
544 return false;
Chandler Carruth8765cf72014-01-25 04:07:24 +0000545
546 continue;
Chris Lattner34399dd2003-12-11 22:23:32 +0000547 }
Chandler Carruth8765cf72014-01-25 04:07:24 +0000548
Chandler Carruthcdf47882014-03-09 03:16:01 +0000549 if (CurLoop->contains(UI))
Chandler Carruth8765cf72014-01-25 04:07:24 +0000550 return false;
Chris Lattner34399dd2003-12-11 22:23:32 +0000551 }
Chris Lattneraaaea512003-12-10 06:41:05 +0000552 return true;
553}
554
Evgeniy Stepanovd99cca22014-06-25 09:17:21 +0000555Instruction *LICM::CloneInstructionInExitBlock(Instruction &I,
556 BasicBlock &ExitBlock,
557 PHINode &PN) {
558 Instruction *New = I.clone();
559 ExitBlock.getInstList().insert(ExitBlock.getFirstInsertionPt(), New);
560 if (!I.getName().empty()) New->setName(I.getName() + ".le");
561
562 // Build LCSSA PHI nodes for any in-loop operands. Note that this is
563 // particularly cheap because we can rip off the PHI node that we're
564 // replacing for the number and blocks of the predecessors.
565 // OPT: If this shows up in a profile, we can instead finish sinking all
566 // invariant instructions, and then walk their operands to re-establish
567 // LCSSA. That will eliminate creating PHI nodes just to nuke them when
568 // sinking bottom-up.
569 for (User::op_iterator OI = New->op_begin(), OE = New->op_end(); OI != OE;
570 ++OI)
571 if (Instruction *OInst = dyn_cast<Instruction>(*OI))
572 if (Loop *OLoop = LI->getLoopFor(OInst->getParent()))
573 if (!OLoop->contains(&PN)) {
574 PHINode *OpPN =
575 PHINode::Create(OInst->getType(), PN.getNumIncomingValues(),
576 OInst->getName() + ".lcssa", ExitBlock.begin());
577 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
578 OpPN->addIncoming(OInst, PN.getIncomingBlock(i));
579 *OI = OpPN;
580 }
581 return New;
582}
583
Chris Lattneraaaea512003-12-10 06:41:05 +0000584/// sink - When an instruction is found to only be used outside of the loop,
Chris Lattner91846012003-12-19 08:18:16 +0000585/// this function moves it to the exit blocks and patches up SSA form as needed.
586/// This method is guaranteed to remove the original instruction from its
587/// position, and may either delete it or move it to outside of the loop.
Chris Lattneraaaea512003-12-10 06:41:05 +0000588///
589void LICM::sink(Instruction &I) {
Nick Lewycky299c6df2010-07-30 20:27:01 +0000590 DEBUG(dbgs() << "LICM sinking instruction: " << I << "\n");
Chris Lattneraaaea512003-12-10 06:41:05 +0000591
Chris Lattner55c21132003-12-10 20:43:29 +0000592 if (isa<LoadInst>(I)) ++NumMovedLoads;
Chris Lattner20cda262004-03-15 04:11:30 +0000593 else if (isa<CallInst>(I)) ++NumMovedCalls;
Chris Lattner55c21132003-12-10 20:43:29 +0000594 ++NumSunk;
595 Changed = true;
596
Chandler Carruthfc258542014-02-11 12:52:27 +0000597#ifndef NDEBUG
598 SmallVector<BasicBlock *, 32> ExitBlocks;
599 CurLoop->getUniqueExitBlocks(ExitBlocks);
600 SmallPtrSet<BasicBlock *, 32> ExitBlockSet(ExitBlocks.begin(), ExitBlocks.end());
601#endif
Chandler Carruth8765cf72014-01-25 04:07:24 +0000602
Evgeniy Stepanov10280da2014-06-25 07:54:58 +0000603 // Clones of this instruction. Don't create more than one per exit block!
604 SmallDenseMap<BasicBlock *, Instruction *, 32> SunkCopies;
605
Chandler Carruthfc258542014-02-11 12:52:27 +0000606 // If this instruction is only used outside of the loop, then all users are
607 // PHI nodes in exit blocks due to LCSSA form. Just RAUW them with clones of
608 // the instruction.
609 while (!I.use_empty()) {
David Majnemer49428102014-09-02 16:22:00 +0000610 Instruction *User = I.user_back();
611 if (!DT->isReachableFromEntry(User->getParent())) {
612 User->replaceUsesOfWith(&I, UndefValue::get(I.getType()));
613 continue;
614 }
Chandler Carruthfc258542014-02-11 12:52:27 +0000615 // The user must be a PHI node.
David Majnemer49428102014-09-02 16:22:00 +0000616 PHINode *PN = cast<PHINode>(User);
Chris Lattnercc9cbc62010-08-29 17:46:00 +0000617
Chandler Carruthfc258542014-02-11 12:52:27 +0000618 BasicBlock *ExitBlock = PN->getParent();
619 assert(ExitBlockSet.count(ExitBlock) &&
620 "The LCSSA PHI is not in an exit block!");
621
Evgeniy Stepanov10280da2014-06-25 07:54:58 +0000622 Instruction *New;
623 auto It = SunkCopies.find(ExitBlock);
Evgeniy Stepanovd99cca22014-06-25 09:17:21 +0000624 if (It != SunkCopies.end())
Evgeniy Stepanov10280da2014-06-25 07:54:58 +0000625 New = It->second;
Evgeniy Stepanovd99cca22014-06-25 09:17:21 +0000626 else
627 New = SunkCopies[ExitBlock] =
628 CloneInstructionInExitBlock(I, *ExitBlock, *PN);
Chandler Carruthfc258542014-02-11 12:52:27 +0000629
630 PN->replaceAllUsesWith(New);
631 PN->eraseFromParent();
Chris Lattnercd96b4d2010-08-29 04:28:20 +0000632 }
Tobias Grossera3928f52011-07-06 19:20:02 +0000633
Chris Lattner1a1ed692010-08-29 18:00:00 +0000634 CurAST->deleteValue(&I);
Chandler Carruthfc258542014-02-11 12:52:27 +0000635 I.eraseFromParent();
Chris Lattneraaaea512003-12-10 06:41:05 +0000636}
Chris Lattner64437692002-09-29 21:46:09 +0000637
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000638/// hoist - When an instruction is found to only use loop invariant operands
639/// that is safe to hoist, this instruction is called to do the dirty work.
640///
Chris Lattneraaaea512003-12-10 06:41:05 +0000641void LICM::hoist(Instruction &I) {
David Greene0fd86222010-01-05 01:27:30 +0000642 DEBUG(dbgs() << "LICM hoisting to " << Preheader->getName() << ": "
Evan Chengf8158612009-10-12 22:25:23 +0000643 << I << "\n");
Chris Lattner65c11932003-12-09 19:32:44 +0000644
Chris Lattner6ac06592010-08-29 18:18:40 +0000645 // Move the new node to the Preheader, before its terminator.
646 I.moveBefore(Preheader->getTerminator());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000647
Chris Lattneraaaea512003-12-10 06:41:05 +0000648 if (isa<LoadInst>(I)) ++NumMovedLoads;
Chris Lattner20cda262004-03-15 04:11:30 +0000649 else if (isa<CallInst>(I)) ++NumMovedCalls;
Chris Lattner718b2212002-09-26 16:38:03 +0000650 ++NumHoisted;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000651 Changed = true;
652}
653
Chris Lattneraaaea512003-12-10 06:41:05 +0000654/// isSafeToExecuteUnconditionally - Only sink or hoist an instruction if it is
655/// not a trapping instruction or if it is a trapping instruction and is
656/// guaranteed to execute.
Tanya Lattner57c03df2003-08-05 18:45:46 +0000657///
Chris Lattneraaaea512003-12-10 06:41:05 +0000658bool LICM::isSafeToExecuteUnconditionally(Instruction &Inst) {
Chris Lattnerc0517682003-12-09 17:18:00 +0000659 // If it is not a trapping instruction, it is always safe to hoist.
Hal Finkel2e42c342014-07-10 05:27:53 +0000660 if (isSafeToSpeculativelyExecute(&Inst, DL))
Eli Friedmanb8f6a4f2009-07-17 04:28:42 +0000661 return true;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000662
Eli Friedman0cdc1482011-07-20 21:37:47 +0000663 return isGuaranteedToExecute(Inst);
664}
665
666bool LICM::isGuaranteedToExecute(Instruction &Inst) {
Nadav Rotem03dcd852012-09-04 10:25:04 +0000667
Philip Reamesb35f46c2014-12-29 23:00:57 +0000668 // We have to check to make sure that the instruction dominates all
Chris Lattnerc0517682003-12-09 17:18:00 +0000669 // of the exit blocks. If it doesn't, then there is a path out of the loop
670 // which does not execute this instruction, so we can't hoist it.
Tanya Lattner57c03df2003-08-05 18:45:46 +0000671
Chris Lattnerc0517682003-12-09 17:18:00 +0000672 // If the instruction is in the header block for the loop (which is very
673 // common), it is always guaranteed to dominate the exit blocks. Since this
674 // is a common case, and can save some work, check it now.
Chris Lattneraaaea512003-12-10 06:41:05 +0000675 if (Inst.getParent() == CurLoop->getHeader())
Philip Reamesb35f46c2014-12-29 23:00:57 +0000676 // If there's a throw in the header block, we can't guarantee we'll reach
677 // Inst.
678 return !HeaderMayThrow;
679
680 // Somewhere in this loop there is an instruction which may throw and make us
681 // exit the loop.
682 if (MayThrow)
683 return false;
Tanya Lattner57c03df2003-08-05 18:45:46 +0000684
Chris Lattnerc0517682003-12-09 17:18:00 +0000685 // Get the exit blocks for the current loop.
Devang Patelb5933bb2007-08-21 00:31:24 +0000686 SmallVector<BasicBlock*, 8> ExitBlocks;
Chris Lattner35eaa552004-04-18 22:15:13 +0000687 CurLoop->getExitBlocks(ExitBlocks);
Chris Lattnerc0517682003-12-09 17:18:00 +0000688
Chris Lattner27497ec2011-01-02 18:45:39 +0000689 // Verify that the block dominates each of the exit blocks of the loop.
Chris Lattneraaaea512003-12-10 06:41:05 +0000690 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
Chris Lattner27497ec2011-01-02 18:45:39 +0000691 if (!DT->dominates(Inst.getParent(), ExitBlocks[i]))
Chris Lattneraaaea512003-12-10 06:41:05 +0000692 return false;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000693
Nick Lewycky78ee67e2012-05-01 04:03:01 +0000694 // As a degenerate case, if the loop is statically infinite then we haven't
695 // proven anything since there are no exit blocks.
696 if (ExitBlocks.empty())
697 return false;
698
Tanya Lattner57c03df2003-08-05 18:45:46 +0000699 return true;
700}
701
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000702namespace {
703 class LoopPromoter : public LoadAndStorePromoter {
704 Value *SomePtr; // Designated pointer to store to.
Craig Topper71b7b682014-08-21 05:55:13 +0000705 SmallPtrSetImpl<Value*> &PointerMustAliases;
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000706 SmallVectorImpl<BasicBlock*> &LoopExitBlocks;
Dan Gohmanb9487362012-08-08 00:00:26 +0000707 SmallVectorImpl<Instruction*> &LoopInsertPts;
Chandler Carruthfc258542014-02-11 12:52:27 +0000708 PredIteratorCache &PredCache;
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000709 AliasSetTracker &AST;
Chandler Carruthfc258542014-02-11 12:52:27 +0000710 LoopInfo &LI;
Eli Friedmanddf7f552011-05-27 20:31:51 +0000711 DebugLoc DL;
Tobias Grosser4a5d9a92011-07-06 19:19:55 +0000712 int Alignment;
Hal Finkelcc39b672014-07-24 12:16:19 +0000713 AAMDNodes AATags;
Chandler Carruthfc258542014-02-11 12:52:27 +0000714
715 Value *maybeInsertLCSSAPHI(Value *V, BasicBlock *BB) const {
716 if (Instruction *I = dyn_cast<Instruction>(V))
717 if (Loop *L = LI.getLoopFor(I->getParent()))
718 if (!L->contains(BB)) {
719 // We need to create an LCSSA PHI node for the incoming value and
720 // store that.
721 PHINode *PN = PHINode::Create(
722 I->getType(), PredCache.GetNumPreds(BB),
723 I->getName() + ".lcssa", BB->begin());
724 for (BasicBlock **PI = PredCache.GetPreds(BB); *PI; ++PI)
725 PN->addIncoming(I, *PI);
726 return PN;
727 }
728 return V;
729 }
730
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000731 public:
Chandler Carruthfc258542014-02-11 12:52:27 +0000732 LoopPromoter(Value *SP, const SmallVectorImpl<Instruction *> &Insts,
Craig Topper71b7b682014-08-21 05:55:13 +0000733 SSAUpdater &S, SmallPtrSetImpl<Value *> &PMA,
Chandler Carruthfc258542014-02-11 12:52:27 +0000734 SmallVectorImpl<BasicBlock *> &LEB,
735 SmallVectorImpl<Instruction *> &LIP, PredIteratorCache &PIC,
736 AliasSetTracker &ast, LoopInfo &li, DebugLoc dl, int alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +0000737 const AAMDNodes &AATags)
Chandler Carruthfc258542014-02-11 12:52:27 +0000738 : LoadAndStorePromoter(Insts, S), SomePtr(SP), PointerMustAliases(PMA),
739 LoopExitBlocks(LEB), LoopInsertPts(LIP), PredCache(PIC), AST(ast),
Hal Finkelcc39b672014-07-24 12:16:19 +0000740 LI(li), DL(dl), Alignment(alignment), AATags(AATags) {}
Tobias Grossera3928f52011-07-06 19:20:02 +0000741
Craig Topper3e4c6972014-03-05 09:10:37 +0000742 bool isInstInList(Instruction *I,
743 const SmallVectorImpl<Instruction*> &) const override {
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000744 Value *Ptr;
745 if (LoadInst *LI = dyn_cast<LoadInst>(I))
746 Ptr = LI->getOperand(0);
747 else
748 Ptr = cast<StoreInst>(I)->getPointerOperand();
749 return PointerMustAliases.count(Ptr);
750 }
Tobias Grossera3928f52011-07-06 19:20:02 +0000751
Craig Topper3e4c6972014-03-05 09:10:37 +0000752 void doExtraRewritesBeforeFinalDeletion() const override {
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000753 // Insert stores after in the loop exit blocks. Each exit block gets a
754 // store of the live-out values that feed them. Since we've already told
755 // the SSA updater about the defs in the loop and the preheader
756 // definition, it is all set and we can start using it.
757 for (unsigned i = 0, e = LoopExitBlocks.size(); i != e; ++i) {
758 BasicBlock *ExitBlock = LoopExitBlocks[i];
759 Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock);
Chandler Carruthfc258542014-02-11 12:52:27 +0000760 LiveInValue = maybeInsertLCSSAPHI(LiveInValue, ExitBlock);
761 Value *Ptr = maybeInsertLCSSAPHI(SomePtr, ExitBlock);
Dan Gohmanb9487362012-08-08 00:00:26 +0000762 Instruction *InsertPos = LoopInsertPts[i];
Chandler Carruthfc258542014-02-11 12:52:27 +0000763 StoreInst *NewSI = new StoreInst(LiveInValue, Ptr, InsertPos);
Tobias Grosser4a5d9a92011-07-06 19:19:55 +0000764 NewSI->setAlignment(Alignment);
Eli Friedmanddf7f552011-05-27 20:31:51 +0000765 NewSI->setDebugLoc(DL);
Hal Finkelcc39b672014-07-24 12:16:19 +0000766 if (AATags) NewSI->setAAMetadata(AATags);
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000767 }
768 }
769
Craig Topper3e4c6972014-03-05 09:10:37 +0000770 void replaceLoadWithValue(LoadInst *LI, Value *V) const override {
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000771 // Update alias analysis.
772 AST.copyValue(LI, V);
773 }
Craig Topper3e4c6972014-03-05 09:10:37 +0000774 void instructionDeleted(Instruction *I) const override {
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000775 AST.deleteValue(I);
776 }
777 };
778} // end anon namespace
779
Chris Lattner1dc98b42010-08-29 06:43:52 +0000780/// PromoteAliasSet - Try to promote memory values to scalars by sinking
Chris Lattner45d67d62003-02-24 03:52:32 +0000781/// stores out of the loop and moving loads to before the loop. We do this by
782/// looping over the stores in the loop, looking for stores to Must pointers
Chris Lattner1dc98b42010-08-29 06:43:52 +0000783/// which are loop invariant.
Chris Lattner45d67d62003-02-24 03:52:32 +0000784///
Dan Gohmanb9487362012-08-08 00:00:26 +0000785void LICM::PromoteAliasSet(AliasSet &AS,
786 SmallVectorImpl<BasicBlock*> &ExitBlocks,
Chandler Carruthfc258542014-02-11 12:52:27 +0000787 SmallVectorImpl<Instruction*> &InsertPts,
788 PredIteratorCache &PIC) {
Chris Lattner1dc98b42010-08-29 06:43:52 +0000789 // We can promote this alias set if it has a store, if it is a "Must" alias
790 // set, if the pointer is loop invariant, and if we are not eliminating any
791 // volatile loads or stores.
792 if (AS.isForwardingAliasSet() || !AS.isMod() || !AS.isMustAlias() ||
793 AS.isVolatile() || !CurLoop->isLoopInvariant(AS.begin()->getValue()))
794 return;
Tobias Grossera3928f52011-07-06 19:20:02 +0000795
Chris Lattner1dc98b42010-08-29 06:43:52 +0000796 assert(!AS.empty() &&
797 "Must alias set should have at least one pointer element in it!");
798 Value *SomePtr = AS.begin()->getValue();
Chris Lattner45d67d62003-02-24 03:52:32 +0000799
Chris Lattner1dc98b42010-08-29 06:43:52 +0000800 // It isn't safe to promote a load/store from the loop if the load/store is
801 // conditional. For example, turning:
Chris Lattner45d67d62003-02-24 03:52:32 +0000802 //
Chris Lattner1dc98b42010-08-29 06:43:52 +0000803 // for () { if (c) *P += 1; }
Chris Lattner45d67d62003-02-24 03:52:32 +0000804 //
Chris Lattner1dc98b42010-08-29 06:43:52 +0000805 // into:
806 //
807 // tmp = *P; for () { if (c) tmp +=1; } *P = tmp;
808 //
809 // is not safe, because *P may only be valid to access if 'c' is true.
Tobias Grossera3928f52011-07-06 19:20:02 +0000810 //
Chris Lattner1dc98b42010-08-29 06:43:52 +0000811 // It is safe to promote P if all uses are direct load/stores and if at
812 // least one is guaranteed to be executed.
813 bool GuaranteedToExecute = false;
Tobias Grosser4a5d9a92011-07-06 19:19:55 +0000814
Chris Lattner1dc98b42010-08-29 06:43:52 +0000815 SmallVector<Instruction*, 64> LoopUses;
816 SmallPtrSet<Value*, 4> PointerMustAliases;
Chris Lattner45d67d62003-02-24 03:52:32 +0000817
Tobias Grosser4a5d9a92011-07-06 19:19:55 +0000818 // We start with an alignment of one and try to find instructions that allow
819 // us to prove better alignment.
820 unsigned Alignment = 1;
Hal Finkelcc39b672014-07-24 12:16:19 +0000821 AAMDNodes AATags;
Bruno Cardoso Lopes46d5bf22014-11-28 19:47:46 +0000822 bool HasDedicatedExits = CurLoop->hasDedicatedExits();
Tobias Grosser4a5d9a92011-07-06 19:19:55 +0000823
Chris Lattner1dc98b42010-08-29 06:43:52 +0000824 // Check that all of the pointers in the alias set have the same type. We
825 // cannot (yet) promote a memory location that is loaded and stored in
Hal Finkelcc39b672014-07-24 12:16:19 +0000826 // different sizes. While we are at it, collect alignment and AA info.
Chris Lattner1dc98b42010-08-29 06:43:52 +0000827 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
828 Value *ASIV = ASI->getValue();
829 PointerMustAliases.insert(ASIV);
Tobias Grossera3928f52011-07-06 19:20:02 +0000830
Chris Lattnerf12c08d2008-05-22 00:53:38 +0000831 // Check that all of the pointers in the alias set have the same type. We
832 // cannot (yet) promote a memory location that is loaded and stored in
833 // different sizes.
Chris Lattner1dc98b42010-08-29 06:43:52 +0000834 if (SomePtr->getType() != ASIV->getType())
835 return;
Tobias Grossera3928f52011-07-06 19:20:02 +0000836
Chandler Carruthcdf47882014-03-09 03:16:01 +0000837 for (User *U : ASIV->users()) {
Chris Lattner1dc98b42010-08-29 06:43:52 +0000838 // Ignore instructions that are outside the loop.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000839 Instruction *UI = dyn_cast<Instruction>(U);
840 if (!UI || !CurLoop->contains(UI))
Chris Lattnerf12c08d2008-05-22 00:53:38 +0000841 continue;
Tobias Grossera3928f52011-07-06 19:20:02 +0000842
Chris Lattner1dc98b42010-08-29 06:43:52 +0000843 // If there is an non-load/store instruction in the loop, we can't promote
844 // it.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000845 if (LoadInst *load = dyn_cast<LoadInst>(UI)) {
Eli Friedman91386c72011-08-15 20:52:09 +0000846 assert(!load->isVolatile() && "AST broken");
847 if (!load->isSimple())
848 return;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000849 } else if (StoreInst *store = dyn_cast<StoreInst>(UI)) {
Chris Lattner408a6842010-12-19 05:57:25 +0000850 // Stores *of* the pointer are not interesting, only stores *to* the
851 // pointer.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000852 if (UI->getOperand(1) != ASIV)
Chris Lattner408a6842010-12-19 05:57:25 +0000853 continue;
Eli Friedman91386c72011-08-15 20:52:09 +0000854 assert(!store->isVolatile() && "AST broken");
855 if (!store->isSimple())
856 return;
Bruno Cardoso Lopes46d5bf22014-11-28 19:47:46 +0000857 // Don't sink stores from loops without dedicated block exits. Exits
858 // containing indirect branches are not transformed by loop simplify,
Bruno Cardoso Lopesd035fbb2014-12-02 14:22:34 +0000859 // make sure we catch that. An additional load may be generated in the
860 // preheader for SSA updater, so also avoid sinking when no preheader
861 // is available.
862 if (!HasDedicatedExits || !Preheader)
Bruno Cardoso Lopes46d5bf22014-11-28 19:47:46 +0000863 return;
Eli Friedman0cdc1482011-07-20 21:37:47 +0000864
865 // Note that we only check GuaranteedToExecute inside the store case
866 // so that we do not introduce stores where they did not exist before
867 // (which would break the LLVM concurrency model).
868
869 // If the alignment of this instruction allows us to specify a more
870 // restrictive (and performant) alignment and if we are sure this
871 // instruction will be executed, update the alignment.
872 // Larger is better, with the exception of 0 being the best alignment.
Eli Friedman91386c72011-08-15 20:52:09 +0000873 unsigned InstAlignment = store->getAlignment();
Chris Lattnerf5cca682012-12-31 08:37:17 +0000874 if ((InstAlignment > Alignment || InstAlignment == 0) && Alignment != 0)
Chandler Carruthcdf47882014-03-09 03:16:01 +0000875 if (isGuaranteedToExecute(*UI)) {
Eli Friedman0cdc1482011-07-20 21:37:47 +0000876 GuaranteedToExecute = true;
877 Alignment = InstAlignment;
878 }
879
880 if (!GuaranteedToExecute)
Chandler Carruthcdf47882014-03-09 03:16:01 +0000881 GuaranteedToExecute = isGuaranteedToExecute(*UI);
Eli Friedman0cdc1482011-07-20 21:37:47 +0000882
Chris Lattnerbe901902010-09-06 05:11:24 +0000883 } else
Chris Lattner1dc98b42010-08-29 06:43:52 +0000884 return; // Not a load or store.
Tobias Grosser4a5d9a92011-07-06 19:19:55 +0000885
Hal Finkelcc39b672014-07-24 12:16:19 +0000886 // Merge the AA tags.
Chris Lattnerf5cca682012-12-31 08:37:17 +0000887 if (LoopUses.empty()) {
Hal Finkelcc39b672014-07-24 12:16:19 +0000888 // On the first load/store, just take its AA tags.
889 UI->getAAMetadata(AATags);
890 } else if (AATags) {
891 UI->getAAMetadata(AATags, /* Merge = */ true);
Chris Lattnerf5cca682012-12-31 08:37:17 +0000892 }
Chandler Carruthcdf47882014-03-09 03:16:01 +0000893
894 LoopUses.push_back(UI);
Chris Lattner1dc98b42010-08-29 06:43:52 +0000895 }
896 }
Tobias Grosser4a5d9a92011-07-06 19:19:55 +0000897
Chris Lattner1dc98b42010-08-29 06:43:52 +0000898 // If there isn't a guaranteed-to-execute instruction, we can't promote.
899 if (!GuaranteedToExecute)
900 return;
Tobias Grossera3928f52011-07-06 19:20:02 +0000901
Chris Lattner1dc98b42010-08-29 06:43:52 +0000902 // Otherwise, this is safe to promote, lets do it!
Tobias Grossera3928f52011-07-06 19:20:02 +0000903 DEBUG(dbgs() << "LICM: Promoting value stored to in loop: " <<*SomePtr<<'\n');
Chris Lattner1dc98b42010-08-29 06:43:52 +0000904 Changed = true;
905 ++NumPromoted;
906
Eli Friedmanddf7f552011-05-27 20:31:51 +0000907 // Grab a debug location for the inserted loads/stores; given that the
908 // inserted loads/stores have little relation to the original loads/stores,
909 // this code just arbitrarily picks a location from one, since any debug
910 // location is better than none.
911 DebugLoc DL = LoopUses[0]->getDebugLoc();
912
Dan Gohmanb9487362012-08-08 00:00:26 +0000913 // Figure out the loop exits and their insertion points, if this is the
914 // first promotion.
915 if (ExitBlocks.empty()) {
916 CurLoop->getUniqueExitBlocks(ExitBlocks);
917 InsertPts.resize(ExitBlocks.size());
918 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
919 InsertPts[i] = ExitBlocks[i]->getFirstInsertionPt();
920 }
Tobias Grossera3928f52011-07-06 19:20:02 +0000921
Chris Lattner1dc98b42010-08-29 06:43:52 +0000922 // We use the SSAUpdater interface to insert phi nodes as required.
923 SmallVector<PHINode*, 16> NewPHIs;
924 SSAUpdater SSA(&NewPHIs);
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000925 LoopPromoter Promoter(SomePtr, LoopUses, SSA, PointerMustAliases, ExitBlocks,
Hal Finkelcc39b672014-07-24 12:16:19 +0000926 InsertPts, PIC, *CurAST, *LI, DL, Alignment, AATags);
Tobias Grossera3928f52011-07-06 19:20:02 +0000927
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000928 // Set up the preheader to have a definition of the value. It is the live-out
929 // value from the preheader that uses in the loop will use.
930 LoadInst *PreheaderLoad =
931 new LoadInst(SomePtr, SomePtr->getName()+".promoted",
932 Preheader->getTerminator());
Tobias Grosser4a5d9a92011-07-06 19:19:55 +0000933 PreheaderLoad->setAlignment(Alignment);
Eli Friedmanddf7f552011-05-27 20:31:51 +0000934 PreheaderLoad->setDebugLoc(DL);
Hal Finkelcc39b672014-07-24 12:16:19 +0000935 if (AATags) PreheaderLoad->setAAMetadata(AATags);
Chris Lattner1dc98b42010-08-29 06:43:52 +0000936 SSA.AddAvailableValue(Preheader, PreheaderLoad);
937
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000938 // Rewrite all the loads in the loop and remember all the definitions from
939 // stores in the loop.
940 Promoter.run(LoopUses);
Eli Friedmanc5f22a72011-04-07 01:35:06 +0000941
942 // If the SSAUpdater didn't use the load in the preheader, just zap it now.
943 if (PreheaderLoad->use_empty())
944 PreheaderLoad->eraseFromParent();
Chris Lattnera51fa882002-08-22 21:39:55 +0000945}
Devang Patelb98a0972007-07-31 08:01:41 +0000946
Chris Lattner1dc98b42010-08-29 06:43:52 +0000947
Devang Patelb98a0972007-07-31 08:01:41 +0000948/// cloneBasicBlockAnalysis - Simple Analysis hook. Clone alias set info.
949void LICM::cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To, Loop *L) {
Chris Lattnercc9cbc62010-08-29 17:46:00 +0000950 AliasSetTracker *AST = LoopToAliasSetMap.lookup(L);
Devang Patelb98a0972007-07-31 08:01:41 +0000951 if (!AST)
952 return;
953
954 AST->copyValue(From, To);
955}
956
957/// deleteAnalysisValue - Simple Analysis hook. Delete value V from alias
958/// set.
959void LICM::deleteAnalysisValue(Value *V, Loop *L) {
Chris Lattnercc9cbc62010-08-29 17:46:00 +0000960 AliasSetTracker *AST = LoopToAliasSetMap.lookup(L);
Devang Patelb98a0972007-07-31 08:01:41 +0000961 if (!AST)
962 return;
963
964 AST->deleteValue(V);
965}
David Peixotto0d4d5e62014-09-24 16:48:31 +0000966
967/// Simple Analysis hook. Delete value L from alias set map.
968void LICM::deleteAnalysisLoop(Loop *L) {
969 AliasSetTracker *AST = LoopToAliasSetMap.lookup(L);
970 if (!AST)
971 return;
972
973 delete AST;
974 LoopToAliasSetMap.erase(L);
975}