blob: 2348d83be09430b844a604bd4f62b31e0bf884d8 [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"
Chandler Carruth7b560d42015-09-09 17:55:00 +000037#include "llvm/Analysis/BasicAliasAnalysis.h"
Chris Lattner030f0202010-08-31 23:00:16 +000038#include "llvm/Analysis/ConstantFolding.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000039#include "llvm/Analysis/GlobalsModRef.h"
Chris Lattner030f0202010-08-31 23:00:16 +000040#include "llvm/Analysis/LoopInfo.h"
41#include "llvm/Analysis/LoopPass.h"
Chandler Carruthabfa3e52014-01-24 01:59:49 +000042#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000043#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000044#include "llvm/Analysis/TargetLibraryInfo.h"
Dan Gohman75d7d5e2011-12-14 23:49:11 +000045#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000046#include "llvm/IR/CFG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000047#include "llvm/IR/Constants.h"
48#include "llvm/IR/DataLayout.h"
49#include "llvm/IR/DerivedTypes.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000050#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000051#include "llvm/IR/Instructions.h"
52#include "llvm/IR/IntrinsicInst.h"
53#include "llvm/IR/LLVMContext.h"
Chris Lattner473988c2013-01-05 16:44:07 +000054#include "llvm/IR/Metadata.h"
Chandler Carruthaa0ab632014-03-04 12:09:19 +000055#include "llvm/IR/PredIteratorCache.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000056#include "llvm/Support/CommandLine.h"
57#include "llvm/Support/Debug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000058#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000059#include "llvm/Transforms/Utils/Local.h"
Chandler Carruth8765cf72014-01-25 04:07:24 +000060#include "llvm/Transforms/Utils/LoopUtils.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000061#include "llvm/Transforms/Utils/SSAUpdater.h"
Chris Lattner6ec05f52002-05-10 22:44:58 +000062#include <algorithm>
Chris Lattnerc0517682003-12-09 17:18:00 +000063using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000064
Chandler Carruth964daaa2014-04-22 02:55:47 +000065#define DEBUG_TYPE "licm"
66
Chris Lattner79a42ac2006-12-19 21:40:18 +000067STATISTIC(NumSunk , "Number of instructions sunk out of loop");
68STATISTIC(NumHoisted , "Number of instructions hoisted out of loop");
69STATISTIC(NumMovedLoads, "Number of load insts hoisted or sunk");
70STATISTIC(NumMovedCalls, "Number of call insts hoisted or sunk");
71STATISTIC(NumPromoted , "Number of memory locations promoted to registers");
72
Dan Gohmand78c4002008-05-13 00:00:25 +000073static cl::opt<bool>
74DisablePromotion("disable-licm-promotion", cl::Hidden,
75 cl::desc("Disable memory promotion in LICM pass"));
Chris Lattner45d67d62003-02-24 03:52:32 +000076
Hal Finkel3d4269a2015-02-22 18:35:32 +000077static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI);
Pete Cooper0cabcf22015-05-13 01:12:18 +000078static bool isNotUsedInLoop(const Instruction &I, const Loop *CurLoop);
Hal Finkel3d4269a2015-02-22 18:35:32 +000079static bool hoist(Instruction &I, BasicBlock *Preheader);
Pete Cooper0cabcf22015-05-13 01:12:18 +000080static bool sink(Instruction &I, const LoopInfo *LI, const DominatorTree *DT,
81 const Loop *CurLoop, AliasSetTracker *CurAST );
82static bool isGuaranteedToExecute(const Instruction &Inst,
83 const DominatorTree *DT,
84 const Loop *CurLoop,
85 const LICMSafetyInfo *SafetyInfo);
86static bool isSafeToExecuteUnconditionally(const Instruction &Inst,
87 const DominatorTree *DT,
Sanjoy Dasf8a0db52015-05-18 18:07:00 +000088 const TargetLibraryInfo *TLI,
Pete Cooper0cabcf22015-05-13 01:12:18 +000089 const Loop *CurLoop,
Philip Reamesb47b9c22015-05-22 02:14:05 +000090 const LICMSafetyInfo *SafetyInfo,
91 const Instruction *CtxI = nullptr);
Hal Finkel3d4269a2015-02-22 18:35:32 +000092static bool pointerInvalidatedByLoop(Value *V, uint64_t Size,
93 const AAMDNodes &AAInfo,
94 AliasSetTracker *CurAST);
Pete Cooper0cabcf22015-05-13 01:12:18 +000095static Instruction *CloneInstructionInExitBlock(const Instruction &I,
Hal Finkel3d4269a2015-02-22 18:35:32 +000096 BasicBlock &ExitBlock,
Pete Cooper0cabcf22015-05-13 01:12:18 +000097 PHINode &PN,
98 const LoopInfo *LI);
Mehdi Aminia28d91d2015-03-10 02:37:25 +000099static bool canSinkOrHoistInst(Instruction &I, AliasAnalysis *AA,
Sanjoy Dasf8a0db52015-05-18 18:07:00 +0000100 DominatorTree *DT, TargetLibraryInfo *TLI,
101 Loop *CurLoop, AliasSetTracker *CurAST,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000102 LICMSafetyInfo *SafetyInfo);
Hal Finkel3d4269a2015-02-22 18:35:32 +0000103
Dan Gohmand78c4002008-05-13 00:00:25 +0000104namespace {
Chris Lattner2dd09db2009-09-02 06:11:42 +0000105 struct LICM : public LoopPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +0000106 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000107 LICM() : LoopPass(ID) {
108 initializeLICMPass(*PassRegistry::getPassRegistry());
109 }
Devang Patel09f162c2007-05-01 21:15:47 +0000110
Craig Topper3e4c6972014-03-05 09:10:37 +0000111 bool runOnLoop(Loop *L, LPPassManager &LPM) override;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000112
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000113 /// This transformation requires natural loop information & requires that
114 /// loop preheaders be inserted into the CFG...
115 ///
Craig Topper3e4c6972014-03-05 09:10:37 +0000116 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner820d9712002-10-21 20:00:28 +0000117 AU.setPreservesCFG();
Chandler Carruth73523022014-01-13 13:07:17 +0000118 AU.addRequired<DominatorTreeWrapperPass>();
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000119 AU.addRequired<LoopInfoWrapperPass>();
Dan Gohmanefd7f9c2010-07-16 17:58:45 +0000120 AU.addRequiredID(LoopSimplifyID);
Chandler Carruth8765cf72014-01-25 04:07:24 +0000121 AU.addPreservedID(LoopSimplifyID);
122 AU.addRequiredID(LCSSAID);
123 AU.addPreservedID(LCSSAID);
Chandler Carruth7b560d42015-09-09 17:55:00 +0000124 AU.addRequired<AAResultsWrapperPass>();
125 AU.addPreserved<AAResultsWrapperPass>();
126 AU.addPreserved<BasicAAWrapperPass>();
127 AU.addPreserved<GlobalsAAWrapperPass>();
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000128 AU.addPreserved<ScalarEvolutionWrapperPass>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000129 AU.addPreserved<SCEVAAWrapperPass>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000130 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chris Lattner6ec05f52002-05-10 22:44:58 +0000131 }
132
Matt Beaumont-Gayabfc4462012-12-04 05:41:27 +0000133 using llvm::Pass::doFinalization;
134
Craig Topper3e4c6972014-03-05 09:10:37 +0000135 bool doFinalization() override {
Chris Lattnercc9cbc62010-08-29 17:46:00 +0000136 assert(LoopToAliasSetMap.empty() && "Didn't free loop alias sets");
Devang Patel69730c92007-03-07 04:41:30 +0000137 return false;
138 }
139
Chris Lattner6ec05f52002-05-10 22:44:58 +0000140 private:
Chris Lattner45d67d62003-02-24 03:52:32 +0000141 AliasAnalysis *AA; // Current AliasAnalysis information
Chris Lattnerc0517682003-12-09 17:18:00 +0000142 LoopInfo *LI; // Current LoopInfo
Chris Lattnerabe61ef2010-08-29 06:49:44 +0000143 DominatorTree *DT; // Dominator Tree for the current Loop.
Chris Lattnerc0517682003-12-09 17:18:00 +0000144
Chad Rosier43a33062011-12-02 01:26:24 +0000145 TargetLibraryInfo *TLI; // TargetLibraryInfo for constant folding.
146
Chris Lattnercc9cbc62010-08-29 17:46:00 +0000147 // State that is updated as we process loops.
Chris Lattner45d67d62003-02-24 03:52:32 +0000148 bool Changed; // Set to true when we change anything.
149 BasicBlock *Preheader; // The preheader block of the current loop...
150 Loop *CurLoop; // The current loop we are working on...
Chris Lattner0592bb72003-03-03 23:32:45 +0000151 AliasSetTracker *CurAST; // AliasSet information for the current loop...
Chris Lattnercc9cbc62010-08-29 17:46:00 +0000152 DenseMap<Loop*, AliasSetTracker*> LoopToAliasSetMap;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000153
Devang Patelb98a0972007-07-31 08:01:41 +0000154 /// cloneBasicBlockAnalysis - Simple Analysis hook. Clone alias set info.
Craig Topper3e4c6972014-03-05 09:10:37 +0000155 void cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To,
156 Loop *L) override;
Devang Patelb98a0972007-07-31 08:01:41 +0000157
158 /// deleteAnalysisValue - Simple Analysis hook. Delete value V from alias
159 /// set.
Craig Topper3e4c6972014-03-05 09:10:37 +0000160 void deleteAnalysisValue(Value *V, Loop *L) override;
Devang Patelb98a0972007-07-31 08:01:41 +0000161
David Peixotto0d4d5e62014-09-24 16:48:31 +0000162 /// Simple Analysis hook. Delete loop L from alias set map.
163 void deleteAnalysisLoop(Loop *L) override;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000164 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000165}
Chris Lattner6ec05f52002-05-10 22:44:58 +0000166
Dan Gohmand78c4002008-05-13 00:00:25 +0000167char LICM::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +0000168INITIALIZE_PASS_BEGIN(LICM, "licm", "Loop Invariant Code Motion", false, false)
Chandler Carruth73523022014-01-13 13:07:17 +0000169INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000170INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000171INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
Chandler Carruth8765cf72014-01-25 04:07:24 +0000172INITIALIZE_PASS_DEPENDENCY(LCSSA)
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000173INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000174INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000175INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass)
176INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
177INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
178INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000179INITIALIZE_PASS_END(LICM, "licm", "Loop Invariant Code Motion", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +0000180
Daniel Dunbar7f39e2d2008-10-22 23:32:42 +0000181Pass *llvm::createLICMPass() { return new LICM(); }
Chris Lattner6ec05f52002-05-10 22:44:58 +0000182
Devang Pateld8b1ceb2007-07-31 16:52:25 +0000183/// Hoist expressions out of the specified loop. Note, alias info for inner
Tobias Grossera3928f52011-07-06 19:20:02 +0000184/// loop is not preserved so it is not a good idea to run LICM multiple
Devang Pateld8b1ceb2007-07-31 16:52:25 +0000185/// times on one loop.
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000186///
Devang Patel69730c92007-03-07 04:41:30 +0000187bool LICM::runOnLoop(Loop *L, LPPassManager &LPM) {
Paul Robinsonaf4e64d2014-02-06 00:07:05 +0000188 if (skipOptnoneFunction(L))
189 return false;
190
Chris Lattner45d67d62003-02-24 03:52:32 +0000191 Changed = false;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000192
Chris Lattner45d67d62003-02-24 03:52:32 +0000193 // Get our Loop and Alias Analysis information...
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000194 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000195 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
Chandler Carruth73523022014-01-13 13:07:17 +0000196 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Chris Lattnera51fa882002-08-22 21:39:55 +0000197
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000198 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chad Rosier43a33062011-12-02 01:26:24 +0000199
Chandler Carruthfc258542014-02-11 12:52:27 +0000200 assert(L->isLCSSAForm(*DT) && "Loop is not in LCSSA form.");
201
Devang Patel69730c92007-03-07 04:41:30 +0000202 CurAST = new AliasSetTracker(*AA);
Chris Lattnercc9cbc62010-08-29 17:46:00 +0000203 // Collect Alias info from subloops.
Devang Patel69730c92007-03-07 04:41:30 +0000204 for (Loop::iterator LoopItr = L->begin(), LoopItrE = L->end();
205 LoopItr != LoopItrE; ++LoopItr) {
206 Loop *InnerL = *LoopItr;
Chris Lattnercc9cbc62010-08-29 17:46:00 +0000207 AliasSetTracker *InnerAST = LoopToAliasSetMap[InnerL];
208 assert(InnerAST && "Where is my AST?");
Devang Patel69730c92007-03-07 04:41:30 +0000209
210 // What if InnerLoop was modified by other passes ?
211 CurAST->add(*InnerAST);
Tobias Grossera3928f52011-07-06 19:20:02 +0000212
Chris Lattnercc9cbc62010-08-29 17:46:00 +0000213 // Once we've incorporated the inner loop's AST into ours, we don't need the
214 // subloop's anymore.
215 delete InnerAST;
216 LoopToAliasSetMap.erase(InnerL);
Chris Lattner45d67d62003-02-24 03:52:32 +0000217 }
Tobias Grossera3928f52011-07-06 19:20:02 +0000218
Chris Lattner6ec05f52002-05-10 22:44:58 +0000219 CurLoop = L;
220
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000221 // Get the preheader block to move instructions into...
222 Preheader = L->getLoopPreheader();
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000223
Chris Lattner45d67d62003-02-24 03:52:32 +0000224 // Loop over the body of this loop, looking for calls, invokes, and stores.
Chris Lattner0592bb72003-03-03 23:32:45 +0000225 // Because subloops have already been incorporated into AST, we skip blocks in
Chris Lattner45d67d62003-02-24 03:52:32 +0000226 // subloops.
227 //
Dan Gohman90071072008-06-22 20:18:58 +0000228 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
229 I != E; ++I) {
230 BasicBlock *BB = *I;
Chris Lattnercc9cbc62010-08-29 17:46:00 +0000231 if (LI->getLoopFor(BB) == L) // Ignore blocks in subloops.
Dan Gohman90071072008-06-22 20:18:58 +0000232 CurAST->add(*BB); // Incorporate the specified basic block
233 }
Chris Lattner45d67d62003-02-24 03:52:32 +0000234
Hal Finkel3d4269a2015-02-22 18:35:32 +0000235 // Compute loop safety information.
236 LICMSafetyInfo SafetyInfo;
237 computeLICMSafetyInfo(&SafetyInfo, CurLoop);
Nadav Rotem03dcd852012-09-04 10:25:04 +0000238
Chris Lattner6ec05f52002-05-10 22:44:58 +0000239 // We want to visit all of the instructions in this loop... that are not parts
240 // of our subloops (they have already had their invariants hoisted out of
241 // their loop, into this loop, so there is no need to process the BODIES of
242 // the subloops).
243 //
Chris Lattner64437692002-09-29 21:46:09 +0000244 // Traverse the body of the loop in depth first order on the dominator tree so
245 // that we are guaranteed to see definitions before we see uses. This allows
Nick Lewyckya0d49da2007-08-18 15:08:56 +0000246 // us to sink instructions in one pass, without iteration. After sinking
Chris Lattner547192d62003-12-19 07:22:45 +0000247 // instructions, we perform another pass to hoist them out of the loop.
Chris Lattner64437692002-09-29 21:46:09 +0000248 //
Dan Gohmana83ac2d2009-11-05 21:11:53 +0000249 if (L->hasDedicatedExits())
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000250 Changed |= sinkRegion(DT->getNode(L->getHeader()), AA, LI, DT, TLI, CurLoop,
251 CurAST, &SafetyInfo);
Dan Gohmana83ac2d2009-11-05 21:11:53 +0000252 if (Preheader)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000253 Changed |= hoistRegion(DT->getNode(L->getHeader()), AA, LI, DT, TLI,
Hal Finkel3d4269a2015-02-22 18:35:32 +0000254 CurLoop, CurAST, &SafetyInfo);
Chris Lattner6ec05f52002-05-10 22:44:58 +0000255
Chris Lattner45d67d62003-02-24 03:52:32 +0000256 // Now that all loop invariants have been removed from the loop, promote any
Chris Lattner1dc98b42010-08-29 06:43:52 +0000257 // memory references to scalars that we can.
Chandler Carruthcc497b62014-01-24 02:24:47 +0000258 if (!DisablePromotion && (Preheader || L->hasDedicatedExits())) {
Dan Gohmanb9487362012-08-08 00:00:26 +0000259 SmallVector<BasicBlock *, 8> ExitBlocks;
260 SmallVector<Instruction *, 8> InsertPts;
Chandler Carruthfc258542014-02-11 12:52:27 +0000261 PredIteratorCache PIC;
Dan Gohmanb9487362012-08-08 00:00:26 +0000262
Chris Lattner1dc98b42010-08-29 06:43:52 +0000263 // Loop over all of the alias sets in the tracker object.
264 for (AliasSetTracker::iterator I = CurAST->begin(), E = CurAST->end();
265 I != E; ++I)
Hal Finkel3d4269a2015-02-22 18:35:32 +0000266 Changed |= promoteLoopAccessesToScalars(*I, ExitBlocks, InsertPts,
267 PIC, LI, DT, CurLoop,
268 CurAST, &SafetyInfo);
Chandler Carruth16651522014-02-01 13:35:14 +0000269
270 // Once we have promoted values across the loop body we have to recursively
271 // reform LCSSA as any nested loop may now have values defined within the
272 // loop used in the outer loop.
273 // FIXME: This is really heavy handed. It would be a bit better to use an
274 // SSAUpdater strategy during promotion that was LCSSA aware and reformed
275 // it as it went.
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000276 if (Changed) {
277 auto *SEWP = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>();
278 formLCSSARecursively(*L, *DT, LI, SEWP ? &SEWP->getSE() : nullptr);
279 }
Chris Lattner1dc98b42010-08-29 06:43:52 +0000280 }
Tobias Grossera3928f52011-07-06 19:20:02 +0000281
Chandler Carruthfc258542014-02-11 12:52:27 +0000282 // Check that neither this loop nor its parent have had LCSSA broken. LICM is
283 // specifically moving instructions across the loop boundary and so it is
284 // especially in need of sanity checking here.
285 assert(L->isLCSSAForm(*DT) && "Loop not left in LCSSA form after LICM!");
286 assert((!L->getParentLoop() || L->getParentLoop()->isLCSSAForm(*DT)) &&
287 "Parent loop not left in LCSSA form after LICM!");
Chandler Carruth8765cf72014-01-25 04:07:24 +0000288
Chris Lattner6ec05f52002-05-10 22:44:58 +0000289 // Clear out loops state information for the next iteration
Craig Topperf40110f2014-04-25 05:29:35 +0000290 CurLoop = nullptr;
291 Preheader = nullptr;
Devang Patel69730c92007-03-07 04:41:30 +0000292
Chris Lattnercc9cbc62010-08-29 17:46:00 +0000293 // If this loop is nested inside of another one, save the alias information
294 // for when we process the outer loop.
295 if (L->getParentLoop())
296 LoopToAliasSetMap[L] = CurAST;
297 else
298 delete CurAST;
Devang Patel69730c92007-03-07 04:41:30 +0000299 return Changed;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000300}
301
Hal Finkel3d4269a2015-02-22 18:35:32 +0000302/// Walk the specified region of the CFG (defined by all blocks dominated by
303/// the specified block, and that are in the current loop) in reverse depth
304/// first order w.r.t the DominatorTree. This allows us to visit uses before
305/// definitions, allowing us to sink a loop body in one pass without iteration.
Chris Lattner547192d62003-12-19 07:22:45 +0000306///
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000307bool llvm::sinkRegion(DomTreeNode *N, AliasAnalysis *AA, LoopInfo *LI,
308 DominatorTree *DT, TargetLibraryInfo *TLI, Loop *CurLoop,
309 AliasSetTracker *CurAST, LICMSafetyInfo *SafetyInfo) {
Chris Lattner547192d62003-12-19 07:22:45 +0000310
Hal Finkel3d4269a2015-02-22 18:35:32 +0000311 // Verify inputs.
312 assert(N != nullptr && AA != nullptr && LI != nullptr &&
313 DT != nullptr && CurLoop != nullptr && CurAST != nullptr &&
314 SafetyInfo != nullptr && "Unexpected input to sinkRegion");
315
316 // Set changed as false.
317 bool Changed = false;
318 // Get basic block
319 BasicBlock *BB = N->getBlock();
Chris Lattner547192d62003-12-19 07:22:45 +0000320 // If this subregion is not in the top level loop at all, exit.
Hal Finkel3d4269a2015-02-22 18:35:32 +0000321 if (!CurLoop->contains(BB)) return Changed;
Chris Lattner547192d62003-12-19 07:22:45 +0000322
Chris Lattner263f8042010-08-29 18:22:25 +0000323 // We are processing blocks in reverse dfo, so process children first.
Devang Patelbdd1aae2007-06-04 00:32:22 +0000324 const std::vector<DomTreeNode*> &Children = N->getChildren();
Chris Lattner547192d62003-12-19 07:22:45 +0000325 for (unsigned i = 0, e = Children.size(); i != e; ++i)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000326 Changed |=
327 sinkRegion(Children[i], AA, LI, DT, TLI, CurLoop, CurAST, SafetyInfo);
Chris Lattner547192d62003-12-19 07:22:45 +0000328 // Only need to process the contents of this block if it is not part of a
329 // subloop (which would already have been processed).
Hal Finkel3d4269a2015-02-22 18:35:32 +0000330 if (inSubLoop(BB,CurLoop,LI)) return Changed;
Chris Lattner547192d62003-12-19 07:22:45 +0000331
Chris Lattner91846012003-12-19 08:18:16 +0000332 for (BasicBlock::iterator II = BB->end(); II != BB->begin(); ) {
333 Instruction &I = *--II;
Tobias Grossera3928f52011-07-06 19:20:02 +0000334
Chris Lattner263f8042010-08-29 18:22:25 +0000335 // If the instruction is dead, we would try to sink it because it isn't used
336 // in the loop, instead, just delete it.
Benjamin Kramer8bcc9712012-08-29 15:32:21 +0000337 if (isInstructionTriviallyDead(&I, TLI)) {
Chris Lattnerf58382e2010-08-29 18:42:23 +0000338 DEBUG(dbgs() << "LICM deleting dead inst: " << I << '\n');
Chris Lattner263f8042010-08-29 18:22:25 +0000339 ++II;
340 CurAST->deleteValue(&I);
341 I.eraseFromParent();
342 Changed = true;
343 continue;
344 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000345
Chris Lattner547192d62003-12-19 07:22:45 +0000346 // Check to see if we can sink this instruction to the exit blocks
347 // of the loop. We can do this if the all users of the instruction are
348 // outside of the loop. In this case, it doesn't even matter if the
349 // operands of the instruction are loop invariant.
350 //
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000351 if (isNotUsedInLoop(I, CurLoop) &&
Sanjoy Dasf8a0db52015-05-18 18:07:00 +0000352 canSinkOrHoistInst(I, AA, DT, TLI, CurLoop, CurAST, SafetyInfo)) {
Chris Lattner91846012003-12-19 08:18:16 +0000353 ++II;
Hal Finkel3d4269a2015-02-22 18:35:32 +0000354 Changed |= sink(I, LI, DT, CurLoop, CurAST);
Chris Lattner91846012003-12-19 08:18:16 +0000355 }
Chris Lattner547192d62003-12-19 07:22:45 +0000356 }
Hal Finkel3d4269a2015-02-22 18:35:32 +0000357 return Changed;
Chris Lattner547192d62003-12-19 07:22:45 +0000358}
359
Hal Finkel3d4269a2015-02-22 18:35:32 +0000360/// Walk the specified region of the CFG (defined by all blocks dominated by
361/// the specified block, and that are in the current loop) in depth first
362/// order w.r.t the DominatorTree. This allows us to visit definitions before
363/// uses, allowing us to hoist a loop body in one pass without iteration.
Chris Lattner64437692002-09-29 21:46:09 +0000364///
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000365bool llvm::hoistRegion(DomTreeNode *N, AliasAnalysis *AA, LoopInfo *LI,
366 DominatorTree *DT, TargetLibraryInfo *TLI, Loop *CurLoop,
367 AliasSetTracker *CurAST, LICMSafetyInfo *SafetyInfo) {
Hal Finkel3d4269a2015-02-22 18:35:32 +0000368 // Verify inputs.
369 assert(N != nullptr && AA != nullptr && LI != nullptr &&
370 DT != nullptr && CurLoop != nullptr && CurAST != nullptr &&
371 SafetyInfo != nullptr && "Unexpected input to hoistRegion");
372 // Set changed as false.
373 bool Changed = false;
374 // Get basic block
Owen Andersonc24701e2007-04-24 06:40:39 +0000375 BasicBlock *BB = N->getBlock();
Chris Lattner05e86302002-09-29 22:26:07 +0000376 // If this subregion is not in the top level loop at all, exit.
Hal Finkel3d4269a2015-02-22 18:35:32 +0000377 if (!CurLoop->contains(BB)) return Changed;
Chris Lattneraaaea512003-12-10 06:41:05 +0000378 // Only need to process the contents of this block if it is not part of a
379 // subloop (which would already have been processed).
Hal Finkel3d4269a2015-02-22 18:35:32 +0000380 if (!inSubLoop(BB, CurLoop, LI))
Chris Lattneraaaea512003-12-10 06:41:05 +0000381 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ) {
382 Instruction &I = *II++;
Chris Lattner030f0202010-08-31 23:00:16 +0000383 // Try constant folding this instruction. If all the operands are
384 // constants, it is technically hoistable, but it would be better to just
385 // fold it.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000386 if (Constant *C = ConstantFoldInstruction(
387 &I, I.getModule()->getDataLayout(), TLI)) {
Chris Lattner030f0202010-08-31 23:00:16 +0000388 DEBUG(dbgs() << "LICM folding inst: " << I << " --> " << *C << '\n');
389 CurAST->copyValue(&I, C);
390 CurAST->deleteValue(&I);
391 I.replaceAllUsesWith(C);
392 I.eraseFromParent();
393 continue;
394 }
Tobias Grossera3928f52011-07-06 19:20:02 +0000395
Chris Lattner547192d62003-12-19 07:22:45 +0000396 // Try hoisting the instruction out to the preheader. We can only do this
397 // if all of the operands of the instruction are loop invariant and if it
398 // is safe to hoist the instruction.
399 //
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000400 if (CurLoop->hasLoopInvariantOperands(&I) &&
Sanjoy Dasf8a0db52015-05-18 18:07:00 +0000401 canSinkOrHoistInst(I, AA, DT, TLI, CurLoop, CurAST, SafetyInfo) &&
Philip Reamesb47b9c22015-05-22 02:14:05 +0000402 isSafeToExecuteUnconditionally(I, DT, TLI, CurLoop, SafetyInfo,
403 CurLoop->getLoopPreheader()->getTerminator()))
Hal Finkel3d4269a2015-02-22 18:35:32 +0000404 Changed |= hoist(I, CurLoop->getLoopPreheader());
Chris Lattner030f0202010-08-31 23:00:16 +0000405 }
Chris Lattner64437692002-09-29 21:46:09 +0000406
Devang Patelbdd1aae2007-06-04 00:32:22 +0000407 const std::vector<DomTreeNode*> &Children = N->getChildren();
Chris Lattner64437692002-09-29 21:46:09 +0000408 for (unsigned i = 0, e = Children.size(); i != e; ++i)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000409 Changed |=
410 hoistRegion(Children[i], AA, LI, DT, TLI, CurLoop, CurAST, SafetyInfo);
Hal Finkel3d4269a2015-02-22 18:35:32 +0000411 return Changed;
412}
413
414/// Computes loop safety information, checks loop body & header
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000415/// for the possibility of may throw exception.
Hal Finkel3d4269a2015-02-22 18:35:32 +0000416///
417void llvm::computeLICMSafetyInfo(LICMSafetyInfo * SafetyInfo, Loop * CurLoop) {
418 assert(CurLoop != nullptr && "CurLoop cant be null");
419 BasicBlock *Header = CurLoop->getHeader();
420 // Setting default safety values.
421 SafetyInfo->MayThrow = false;
422 SafetyInfo->HeaderMayThrow = false;
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000423 // Iterate over header and compute safety info.
Hal Finkel3d4269a2015-02-22 18:35:32 +0000424 for (BasicBlock::iterator I = Header->begin(), E = Header->end();
425 (I != E) && !SafetyInfo->HeaderMayThrow; ++I)
426 SafetyInfo->HeaderMayThrow |= I->mayThrow();
427
428 SafetyInfo->MayThrow = SafetyInfo->HeaderMayThrow;
429 // Iterate over loop instructions and compute safety info.
430 for (Loop::block_iterator BB = CurLoop->block_begin(),
431 BBE = CurLoop->block_end(); (BB != BBE) && !SafetyInfo->MayThrow ; ++BB)
432 for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end();
433 (I != E) && !SafetyInfo->MayThrow; ++I)
434 SafetyInfo->MayThrow |= I->mayThrow();
Chris Lattner64437692002-09-29 21:46:09 +0000435}
436
Chris Lattneraaaea512003-12-10 06:41:05 +0000437/// canSinkOrHoistInst - Return true if the hoister and sinker can handle this
438/// instruction.
439///
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000440bool canSinkOrHoistInst(Instruction &I, AliasAnalysis *AA, DominatorTree *DT,
Sanjoy Dasf8a0db52015-05-18 18:07:00 +0000441 TargetLibraryInfo *TLI, Loop *CurLoop,
442 AliasSetTracker *CurAST, LICMSafetyInfo *SafetyInfo) {
Chris Lattner65c11932003-12-09 19:32:44 +0000443 // Loads have extra constraints we have to verify before we can hoist them.
444 if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
Eli Friedman91386c72011-08-15 20:52:09 +0000445 if (!LI->isUnordered())
446 return false; // Don't hoist volatile/atomic loads!
Chris Lattner65c11932003-12-09 19:32:44 +0000447
Chris Lattner8a8fb902008-07-23 05:06:28 +0000448 // Loads from constant memory are always safe to move, even if they end up
449 // in the same alias set as something that ends up being modified.
Dan Gohmancbc6ebb2009-11-19 19:00:10 +0000450 if (AA->pointsToConstantMemory(LI->getOperand(0)))
Chris Lattner8a8fb902008-07-23 05:06:28 +0000451 return true;
Philip Reames5a3f5f72014-10-21 00:13:20 +0000452 if (LI->getMetadata(LLVMContext::MD_invariant_load))
Pete Cooper9ee22092011-11-08 19:30:00 +0000453 return true;
Tobias Grossera3928f52011-07-06 19:20:02 +0000454
Chris Lattner65c11932003-12-09 19:32:44 +0000455 // Don't hoist loads which have may-aliased stores in loop.
Dan Gohmanf372cf82010-10-19 22:54:46 +0000456 uint64_t Size = 0;
Chris Lattnerb1374092004-11-26 21:20:09 +0000457 if (LI->getType()->isSized())
Chandler Carruth50fee932015-08-06 02:05:46 +0000458 Size = I.getModule()->getDataLayout().getTypeStoreSize(LI->getType());
Hal Finkelcc39b672014-07-24 12:16:19 +0000459
460 AAMDNodes AAInfo;
461 LI->getAAMetadata(AAInfo);
462
Hal Finkel3d4269a2015-02-22 18:35:32 +0000463 return !pointerInvalidatedByLoop(LI->getOperand(0), Size, AAInfo, CurAST);
Chris Lattner20cda262004-03-15 04:11:30 +0000464 } else if (CallInst *CI = dyn_cast<CallInst>(&I)) {
Eli Friedman942e1c12011-05-27 18:37:52 +0000465 // Don't sink or hoist dbg info; it's legal, but not useful.
466 if (isa<DbgInfoIntrinsic>(I))
467 return false;
468
469 // Handle simple cases by querying alias analysis.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000470 FunctionModRefBehavior Behavior = AA->getModRefBehavior(CI);
471 if (Behavior == FMRB_DoesNotAccessMemory)
Duncan Sands68b6f502007-12-01 07:51:45 +0000472 return true;
Dan Gohman0f175072010-11-09 19:58:21 +0000473 if (AliasAnalysis::onlyReadsMemory(Behavior)) {
Philip Reames5f994232015-09-21 22:27:59 +0000474 // A readonly argmemonly function only reads from memory pointed to by
475 // it's arguments with arbitrary offsets. If we can prove there are no
476 // writes to this memory in the loop, we can hoist or sink.
477 if (AliasAnalysis::onlyAccessesArgPointees(Behavior)) {
478 for (Value *Op : CI->arg_operands())
479 if (Op->getType()->isPointerTy() &&
480 pointerInvalidatedByLoop(Op, MemoryLocation::UnknownSize,
481 AAMDNodes(), CurAST))
482 return false;
483 return true;
484 }
Duncan Sands68b6f502007-12-01 07:51:45 +0000485 // If this call only reads from memory and there are no writes to memory
486 // in the loop, we can hoist or sink the call as appropriate.
487 bool FoundMod = false;
488 for (AliasSetTracker::iterator I = CurAST->begin(), E = CurAST->end();
489 I != E; ++I) {
490 AliasSet &AS = *I;
491 if (!AS.isForwardingAliasSet() && AS.isMod()) {
492 FoundMod = true;
493 break;
Chris Lattner20cda262004-03-15 04:11:30 +0000494 }
Chris Lattner20cda262004-03-15 04:11:30 +0000495 }
Duncan Sands68b6f502007-12-01 07:51:45 +0000496 if (!FoundMod) return true;
Chris Lattner20cda262004-03-15 04:11:30 +0000497 }
498
Nadav Rotem03dcd852012-09-04 10:25:04 +0000499 // FIXME: This should use mod/ref information to see if we can hoist or
500 // sink the call.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000501
Chris Lattner20cda262004-03-15 04:11:30 +0000502 return false;
Chris Lattner65c11932003-12-09 19:32:44 +0000503 }
504
Nadav Rotem03dcd852012-09-04 10:25:04 +0000505 // Only these instructions are hoistable/sinkable.
Benjamin Kramer130fcde2013-01-09 18:12:03 +0000506 if (!isa<BinaryOperator>(I) && !isa<CastInst>(I) && !isa<SelectInst>(I) &&
507 !isa<GetElementPtrInst>(I) && !isa<CmpInst>(I) &&
508 !isa<InsertElementInst>(I) && !isa<ExtractElementInst>(I) &&
509 !isa<ShuffleVectorInst>(I) && !isa<ExtractValueInst>(I) &&
510 !isa<InsertValueInst>(I))
511 return false;
Nadav Rotem03dcd852012-09-04 10:25:04 +0000512
Philip Reamesb47b9c22015-05-22 02:14:05 +0000513 // TODO: Plumb the context instruction through to make hoisting and sinking
514 // more powerful. Hoisting of loads already works due to the special casing
515 // above.
516 return isSafeToExecuteUnconditionally(I, DT, TLI, CurLoop, SafetyInfo,
517 nullptr);
Chris Lattneraaaea512003-12-10 06:41:05 +0000518}
519
Hal Finkel3d4269a2015-02-22 18:35:32 +0000520/// Returns true if a PHINode is a trivially replaceable with an
Chandler Carruth8765cf72014-01-25 04:07:24 +0000521/// Instruction.
Hal Finkel3d4269a2015-02-22 18:35:32 +0000522/// This is true when all incoming values are that instruction.
523/// This pattern occurs most often with LCSSA PHI nodes.
Chandler Carruth8765cf72014-01-25 04:07:24 +0000524///
Pete Cooper47e80cd2015-05-12 20:05:20 +0000525static bool isTriviallyReplacablePHI(const PHINode &PN, const Instruction &I) {
Pete Cooper833f34d2015-05-12 20:05:31 +0000526 for (const Value *IncValue : PN.incoming_values())
527 if (IncValue != &I)
Chandler Carruth8765cf72014-01-25 04:07:24 +0000528 return false;
529
530 return true;
531}
532
Hal Finkel3d4269a2015-02-22 18:35:32 +0000533/// Return true if the only users of this instruction are outside of
534/// the loop. If this is true, we can sink the instruction to the exit
535/// blocks of the loop.
Chris Lattneraaaea512003-12-10 06:41:05 +0000536///
Pete Cooper0cabcf22015-05-13 01:12:18 +0000537static bool isNotUsedInLoop(const Instruction &I, const Loop *CurLoop) {
538 for (const User *U : I.users()) {
539 const Instruction *UI = cast<Instruction>(U);
540 if (const PHINode *PN = dyn_cast<PHINode>(UI)) {
Chandler Carruth8765cf72014-01-25 04:07:24 +0000541 // A PHI node where all of the incoming values are this instruction are
542 // special -- they can just be RAUW'ed with the instruction and thus
543 // don't require a use in the predecessor. This is a particular important
544 // special case because it is the pattern found in LCSSA form.
545 if (isTriviallyReplacablePHI(*PN, I)) {
546 if (CurLoop->contains(PN))
547 return false;
548 else
549 continue;
550 }
551
552 // Otherwise, PHI node uses occur in predecessor blocks if the incoming
553 // values. Check for such a use being inside the loop.
Chris Lattner34399dd2003-12-11 22:23:32 +0000554 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
555 if (PN->getIncomingValue(i) == &I)
556 if (CurLoop->contains(PN->getIncomingBlock(i)))
557 return false;
Chandler Carruth8765cf72014-01-25 04:07:24 +0000558
559 continue;
Chris Lattner34399dd2003-12-11 22:23:32 +0000560 }
Chandler Carruth8765cf72014-01-25 04:07:24 +0000561
Chandler Carruthcdf47882014-03-09 03:16:01 +0000562 if (CurLoop->contains(UI))
Chandler Carruth8765cf72014-01-25 04:07:24 +0000563 return false;
Chris Lattner34399dd2003-12-11 22:23:32 +0000564 }
Chris Lattneraaaea512003-12-10 06:41:05 +0000565 return true;
566}
567
Pete Cooper0cabcf22015-05-13 01:12:18 +0000568static Instruction *CloneInstructionInExitBlock(const Instruction &I,
Hal Finkel3d4269a2015-02-22 18:35:32 +0000569 BasicBlock &ExitBlock,
Pete Cooper0cabcf22015-05-13 01:12:18 +0000570 PHINode &PN,
571 const LoopInfo *LI) {
Evgeniy Stepanovd99cca22014-06-25 09:17:21 +0000572 Instruction *New = I.clone();
573 ExitBlock.getInstList().insert(ExitBlock.getFirstInsertionPt(), New);
574 if (!I.getName().empty()) New->setName(I.getName() + ".le");
575
576 // Build LCSSA PHI nodes for any in-loop operands. Note that this is
577 // particularly cheap because we can rip off the PHI node that we're
578 // replacing for the number and blocks of the predecessors.
579 // OPT: If this shows up in a profile, we can instead finish sinking all
580 // invariant instructions, and then walk their operands to re-establish
581 // LCSSA. That will eliminate creating PHI nodes just to nuke them when
582 // sinking bottom-up.
583 for (User::op_iterator OI = New->op_begin(), OE = New->op_end(); OI != OE;
584 ++OI)
585 if (Instruction *OInst = dyn_cast<Instruction>(*OI))
586 if (Loop *OLoop = LI->getLoopFor(OInst->getParent()))
587 if (!OLoop->contains(&PN)) {
588 PHINode *OpPN =
589 PHINode::Create(OInst->getType(), PN.getNumIncomingValues(),
590 OInst->getName() + ".lcssa", ExitBlock.begin());
591 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
592 OpPN->addIncoming(OInst, PN.getIncomingBlock(i));
593 *OI = OpPN;
594 }
595 return New;
596}
597
Hal Finkel3d4269a2015-02-22 18:35:32 +0000598/// When an instruction is found to only be used outside of the loop, this
599/// function moves it to the exit blocks and patches up SSA form as needed.
Chris Lattner91846012003-12-19 08:18:16 +0000600/// This method is guaranteed to remove the original instruction from its
601/// position, and may either delete it or move it to outside of the loop.
Chris Lattneraaaea512003-12-10 06:41:05 +0000602///
Pete Cooper0cabcf22015-05-13 01:12:18 +0000603static bool sink(Instruction &I, const LoopInfo *LI, const DominatorTree *DT,
604 const Loop *CurLoop, AliasSetTracker *CurAST ) {
Nick Lewycky299c6df2010-07-30 20:27:01 +0000605 DEBUG(dbgs() << "LICM sinking instruction: " << I << "\n");
Hal Finkel3d4269a2015-02-22 18:35:32 +0000606 bool Changed = false;
Chris Lattner55c21132003-12-10 20:43:29 +0000607 if (isa<LoadInst>(I)) ++NumMovedLoads;
Chris Lattner20cda262004-03-15 04:11:30 +0000608 else if (isa<CallInst>(I)) ++NumMovedCalls;
Chris Lattner55c21132003-12-10 20:43:29 +0000609 ++NumSunk;
610 Changed = true;
611
Chandler Carruthfc258542014-02-11 12:52:27 +0000612#ifndef NDEBUG
613 SmallVector<BasicBlock *, 32> ExitBlocks;
614 CurLoop->getUniqueExitBlocks(ExitBlocks);
Hal Finkel3d4269a2015-02-22 18:35:32 +0000615 SmallPtrSet<BasicBlock *, 32> ExitBlockSet(ExitBlocks.begin(),
616 ExitBlocks.end());
Chandler Carruthfc258542014-02-11 12:52:27 +0000617#endif
Chandler Carruth8765cf72014-01-25 04:07:24 +0000618
Evgeniy Stepanov10280da2014-06-25 07:54:58 +0000619 // Clones of this instruction. Don't create more than one per exit block!
620 SmallDenseMap<BasicBlock *, Instruction *, 32> SunkCopies;
621
Chandler Carruthfc258542014-02-11 12:52:27 +0000622 // If this instruction is only used outside of the loop, then all users are
623 // PHI nodes in exit blocks due to LCSSA form. Just RAUW them with clones of
624 // the instruction.
625 while (!I.use_empty()) {
David Majnemer6bc83e02015-07-12 03:53:05 +0000626 Value::user_iterator UI = I.user_begin();
627 auto *User = cast<Instruction>(*UI);
David Majnemer49428102014-09-02 16:22:00 +0000628 if (!DT->isReachableFromEntry(User->getParent())) {
629 User->replaceUsesOfWith(&I, UndefValue::get(I.getType()));
630 continue;
631 }
Chandler Carruthfc258542014-02-11 12:52:27 +0000632 // The user must be a PHI node.
David Majnemer49428102014-09-02 16:22:00 +0000633 PHINode *PN = cast<PHINode>(User);
Chris Lattnercc9cbc62010-08-29 17:46:00 +0000634
David Majnemer6bc83e02015-07-12 03:53:05 +0000635 // Surprisingly, instructions can be used outside of loops without any
636 // exits. This can only happen in PHI nodes if the incoming block is
637 // unreachable.
638 Use &U = UI.getUse();
639 BasicBlock *BB = PN->getIncomingBlock(U);
640 if (!DT->isReachableFromEntry(BB)) {
641 U = UndefValue::get(I.getType());
642 continue;
643 }
644
Chandler Carruthfc258542014-02-11 12:52:27 +0000645 BasicBlock *ExitBlock = PN->getParent();
646 assert(ExitBlockSet.count(ExitBlock) &&
647 "The LCSSA PHI is not in an exit block!");
648
Evgeniy Stepanov10280da2014-06-25 07:54:58 +0000649 Instruction *New;
650 auto It = SunkCopies.find(ExitBlock);
Evgeniy Stepanovd99cca22014-06-25 09:17:21 +0000651 if (It != SunkCopies.end())
Evgeniy Stepanov10280da2014-06-25 07:54:58 +0000652 New = It->second;
Evgeniy Stepanovd99cca22014-06-25 09:17:21 +0000653 else
654 New = SunkCopies[ExitBlock] =
Hal Finkel3d4269a2015-02-22 18:35:32 +0000655 CloneInstructionInExitBlock(I, *ExitBlock, *PN, LI);
Chandler Carruthfc258542014-02-11 12:52:27 +0000656
657 PN->replaceAllUsesWith(New);
658 PN->eraseFromParent();
Chris Lattnercd96b4d2010-08-29 04:28:20 +0000659 }
Tobias Grossera3928f52011-07-06 19:20:02 +0000660
Chris Lattner1a1ed692010-08-29 18:00:00 +0000661 CurAST->deleteValue(&I);
Chandler Carruthfc258542014-02-11 12:52:27 +0000662 I.eraseFromParent();
Hal Finkel3d4269a2015-02-22 18:35:32 +0000663 return Changed;
Chris Lattneraaaea512003-12-10 06:41:05 +0000664}
Chris Lattner64437692002-09-29 21:46:09 +0000665
Hal Finkel3d4269a2015-02-22 18:35:32 +0000666/// When an instruction is found to only use loop invariant operands that
667/// is safe to hoist, this instruction is called to do the dirty work.
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000668///
Hal Finkel3d4269a2015-02-22 18:35:32 +0000669static bool hoist(Instruction &I, BasicBlock *Preheader) {
David Greene0fd86222010-01-05 01:27:30 +0000670 DEBUG(dbgs() << "LICM hoisting to " << Preheader->getName() << ": "
Evan Chengf8158612009-10-12 22:25:23 +0000671 << I << "\n");
Chris Lattner6ac06592010-08-29 18:18:40 +0000672 // Move the new node to the Preheader, before its terminator.
673 I.moveBefore(Preheader->getTerminator());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000674
Chris Lattneraaaea512003-12-10 06:41:05 +0000675 if (isa<LoadInst>(I)) ++NumMovedLoads;
Chris Lattner20cda262004-03-15 04:11:30 +0000676 else if (isa<CallInst>(I)) ++NumMovedCalls;
Chris Lattner718b2212002-09-26 16:38:03 +0000677 ++NumHoisted;
Hal Finkel3d4269a2015-02-22 18:35:32 +0000678 return true;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000679}
680
Sanjoy Dasf8a0db52015-05-18 18:07:00 +0000681/// Only sink or hoist an instruction if it is not a trapping instruction,
682/// or if the instruction is known not to trap when moved to the preheader.
Hal Finkel3d4269a2015-02-22 18:35:32 +0000683/// or if it is a trapping instruction and is guaranteed to execute.
Sanjoy Dasf8a0db52015-05-18 18:07:00 +0000684static bool isSafeToExecuteUnconditionally(const Instruction &Inst,
Pete Cooper0cabcf22015-05-13 01:12:18 +0000685 const DominatorTree *DT,
Sanjoy Dasf8a0db52015-05-18 18:07:00 +0000686 const TargetLibraryInfo *TLI,
Pete Cooper0cabcf22015-05-13 01:12:18 +0000687 const Loop *CurLoop,
Philip Reamesb47b9c22015-05-22 02:14:05 +0000688 const LICMSafetyInfo *SafetyInfo,
689 const Instruction *CtxI) {
Sanjoy Dasf8a0db52015-05-18 18:07:00 +0000690 if (isSafeToSpeculativelyExecute(&Inst, CtxI, DT, TLI))
Eli Friedmanb8f6a4f2009-07-17 04:28:42 +0000691 return true;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000692
Hal Finkel3d4269a2015-02-22 18:35:32 +0000693 return isGuaranteedToExecute(Inst, DT, CurLoop, SafetyInfo);
Eli Friedman0cdc1482011-07-20 21:37:47 +0000694}
695
Pete Cooper0cabcf22015-05-13 01:12:18 +0000696static bool isGuaranteedToExecute(const Instruction &Inst,
697 const DominatorTree *DT,
698 const Loop *CurLoop,
699 const LICMSafetyInfo * SafetyInfo) {
Nadav Rotem03dcd852012-09-04 10:25:04 +0000700
Philip Reamesb35f46c2014-12-29 23:00:57 +0000701 // We have to check to make sure that the instruction dominates all
Chris Lattnerc0517682003-12-09 17:18:00 +0000702 // of the exit blocks. If it doesn't, then there is a path out of the loop
703 // which does not execute this instruction, so we can't hoist it.
Tanya Lattner57c03df2003-08-05 18:45:46 +0000704
Chris Lattnerc0517682003-12-09 17:18:00 +0000705 // If the instruction is in the header block for the loop (which is very
706 // common), it is always guaranteed to dominate the exit blocks. Since this
707 // is a common case, and can save some work, check it now.
Chris Lattneraaaea512003-12-10 06:41:05 +0000708 if (Inst.getParent() == CurLoop->getHeader())
Philip Reamesb35f46c2014-12-29 23:00:57 +0000709 // If there's a throw in the header block, we can't guarantee we'll reach
710 // Inst.
Hal Finkel3d4269a2015-02-22 18:35:32 +0000711 return !SafetyInfo->HeaderMayThrow;
Philip Reamesb35f46c2014-12-29 23:00:57 +0000712
713 // Somewhere in this loop there is an instruction which may throw and make us
714 // exit the loop.
Hal Finkel3d4269a2015-02-22 18:35:32 +0000715 if (SafetyInfo->MayThrow)
Philip Reamesb35f46c2014-12-29 23:00:57 +0000716 return false;
Tanya Lattner57c03df2003-08-05 18:45:46 +0000717
Chris Lattnerc0517682003-12-09 17:18:00 +0000718 // Get the exit blocks for the current loop.
Devang Patelb5933bb2007-08-21 00:31:24 +0000719 SmallVector<BasicBlock*, 8> ExitBlocks;
Chris Lattner35eaa552004-04-18 22:15:13 +0000720 CurLoop->getExitBlocks(ExitBlocks);
Chris Lattnerc0517682003-12-09 17:18:00 +0000721
Chris Lattner27497ec2011-01-02 18:45:39 +0000722 // Verify that the block dominates each of the exit blocks of the loop.
Chris Lattneraaaea512003-12-10 06:41:05 +0000723 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
Chris Lattner27497ec2011-01-02 18:45:39 +0000724 if (!DT->dominates(Inst.getParent(), ExitBlocks[i]))
Chris Lattneraaaea512003-12-10 06:41:05 +0000725 return false;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000726
Nick Lewycky78ee67e2012-05-01 04:03:01 +0000727 // As a degenerate case, if the loop is statically infinite then we haven't
728 // proven anything since there are no exit blocks.
729 if (ExitBlocks.empty())
730 return false;
731
Tanya Lattner57c03df2003-08-05 18:45:46 +0000732 return true;
733}
734
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000735namespace {
736 class LoopPromoter : public LoadAndStorePromoter {
737 Value *SomePtr; // Designated pointer to store to.
Craig Topper71b7b682014-08-21 05:55:13 +0000738 SmallPtrSetImpl<Value*> &PointerMustAliases;
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000739 SmallVectorImpl<BasicBlock*> &LoopExitBlocks;
Dan Gohmanb9487362012-08-08 00:00:26 +0000740 SmallVectorImpl<Instruction*> &LoopInsertPts;
Chandler Carruthfc258542014-02-11 12:52:27 +0000741 PredIteratorCache &PredCache;
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000742 AliasSetTracker &AST;
Chandler Carruthfc258542014-02-11 12:52:27 +0000743 LoopInfo &LI;
Eli Friedmanddf7f552011-05-27 20:31:51 +0000744 DebugLoc DL;
Tobias Grosser4a5d9a92011-07-06 19:19:55 +0000745 int Alignment;
Hal Finkelcc39b672014-07-24 12:16:19 +0000746 AAMDNodes AATags;
Chandler Carruthfc258542014-02-11 12:52:27 +0000747
748 Value *maybeInsertLCSSAPHI(Value *V, BasicBlock *BB) const {
749 if (Instruction *I = dyn_cast<Instruction>(V))
750 if (Loop *L = LI.getLoopFor(I->getParent()))
751 if (!L->contains(BB)) {
752 // We need to create an LCSSA PHI node for the incoming value and
753 // store that.
754 PHINode *PN = PHINode::Create(
Daniel Berlinb4e7a4a2015-04-21 21:11:50 +0000755 I->getType(), PredCache.size(BB),
Chandler Carruthfc258542014-02-11 12:52:27 +0000756 I->getName() + ".lcssa", BB->begin());
Daniel Berlinb4e7a4a2015-04-21 21:11:50 +0000757 for (BasicBlock *Pred : PredCache.get(BB))
758 PN->addIncoming(I, Pred);
Chandler Carruthfc258542014-02-11 12:52:27 +0000759 return PN;
760 }
761 return V;
762 }
763
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000764 public:
Pete Cooper41e0ee32015-05-13 01:12:16 +0000765 LoopPromoter(Value *SP,
766 ArrayRef<const Instruction *> Insts,
Craig Topper71b7b682014-08-21 05:55:13 +0000767 SSAUpdater &S, SmallPtrSetImpl<Value *> &PMA,
Chandler Carruthfc258542014-02-11 12:52:27 +0000768 SmallVectorImpl<BasicBlock *> &LEB,
769 SmallVectorImpl<Instruction *> &LIP, PredIteratorCache &PIC,
770 AliasSetTracker &ast, LoopInfo &li, DebugLoc dl, int alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +0000771 const AAMDNodes &AATags)
Chandler Carruthfc258542014-02-11 12:52:27 +0000772 : LoadAndStorePromoter(Insts, S), SomePtr(SP), PointerMustAliases(PMA),
773 LoopExitBlocks(LEB), LoopInsertPts(LIP), PredCache(PIC), AST(ast),
Hal Finkelcc39b672014-07-24 12:16:19 +0000774 LI(li), DL(dl), Alignment(alignment), AATags(AATags) {}
Tobias Grossera3928f52011-07-06 19:20:02 +0000775
Craig Topper3e4c6972014-03-05 09:10:37 +0000776 bool isInstInList(Instruction *I,
777 const SmallVectorImpl<Instruction*> &) const override {
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000778 Value *Ptr;
779 if (LoadInst *LI = dyn_cast<LoadInst>(I))
780 Ptr = LI->getOperand(0);
781 else
782 Ptr = cast<StoreInst>(I)->getPointerOperand();
783 return PointerMustAliases.count(Ptr);
784 }
Tobias Grossera3928f52011-07-06 19:20:02 +0000785
Craig Topper3e4c6972014-03-05 09:10:37 +0000786 void doExtraRewritesBeforeFinalDeletion() const override {
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000787 // Insert stores after in the loop exit blocks. Each exit block gets a
788 // store of the live-out values that feed them. Since we've already told
789 // the SSA updater about the defs in the loop and the preheader
790 // definition, it is all set and we can start using it.
791 for (unsigned i = 0, e = LoopExitBlocks.size(); i != e; ++i) {
792 BasicBlock *ExitBlock = LoopExitBlocks[i];
793 Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock);
Chandler Carruthfc258542014-02-11 12:52:27 +0000794 LiveInValue = maybeInsertLCSSAPHI(LiveInValue, ExitBlock);
795 Value *Ptr = maybeInsertLCSSAPHI(SomePtr, ExitBlock);
Dan Gohmanb9487362012-08-08 00:00:26 +0000796 Instruction *InsertPos = LoopInsertPts[i];
Chandler Carruthfc258542014-02-11 12:52:27 +0000797 StoreInst *NewSI = new StoreInst(LiveInValue, Ptr, InsertPos);
Tobias Grosser4a5d9a92011-07-06 19:19:55 +0000798 NewSI->setAlignment(Alignment);
Eli Friedmanddf7f552011-05-27 20:31:51 +0000799 NewSI->setDebugLoc(DL);
Hal Finkelcc39b672014-07-24 12:16:19 +0000800 if (AATags) NewSI->setAAMetadata(AATags);
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000801 }
802 }
803
Craig Topper3e4c6972014-03-05 09:10:37 +0000804 void replaceLoadWithValue(LoadInst *LI, Value *V) const override {
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000805 // Update alias analysis.
806 AST.copyValue(LI, V);
807 }
Craig Topper3e4c6972014-03-05 09:10:37 +0000808 void instructionDeleted(Instruction *I) const override {
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000809 AST.deleteValue(I);
810 }
811 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000812} // end anon namespace
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000813
Hal Finkel3d4269a2015-02-22 18:35:32 +0000814/// Try to promote memory values to scalars by sinking stores out of the
815/// loop and moving loads to before the loop. We do this by looping over
816/// the stores in the loop, looking for stores to Must pointers which are
817/// loop invariant.
Chris Lattner45d67d62003-02-24 03:52:32 +0000818///
Hal Finkel3d4269a2015-02-22 18:35:32 +0000819bool llvm::promoteLoopAccessesToScalars(AliasSet &AS,
820 SmallVectorImpl<BasicBlock*>&ExitBlocks,
821 SmallVectorImpl<Instruction*>&InsertPts,
822 PredIteratorCache &PIC, LoopInfo *LI,
823 DominatorTree *DT, Loop *CurLoop,
824 AliasSetTracker *CurAST,
825 LICMSafetyInfo * SafetyInfo) {
826 // Verify inputs.
827 assert(LI != nullptr && DT != nullptr &&
828 CurLoop != nullptr && CurAST != nullptr &&
829 SafetyInfo != nullptr &&
830 "Unexpected Input to promoteLoopAccessesToScalars");
831 // Initially set Changed status to false.
832 bool Changed = false;
Chris Lattner1dc98b42010-08-29 06:43:52 +0000833 // We can promote this alias set if it has a store, if it is a "Must" alias
834 // set, if the pointer is loop invariant, and if we are not eliminating any
835 // volatile loads or stores.
836 if (AS.isForwardingAliasSet() || !AS.isMod() || !AS.isMustAlias() ||
837 AS.isVolatile() || !CurLoop->isLoopInvariant(AS.begin()->getValue()))
Hal Finkel3d4269a2015-02-22 18:35:32 +0000838 return Changed;
Tobias Grossera3928f52011-07-06 19:20:02 +0000839
Chris Lattner1dc98b42010-08-29 06:43:52 +0000840 assert(!AS.empty() &&
841 "Must alias set should have at least one pointer element in it!");
Hal Finkel3d4269a2015-02-22 18:35:32 +0000842
Chris Lattner1dc98b42010-08-29 06:43:52 +0000843 Value *SomePtr = AS.begin()->getValue();
Hal Finkel3d4269a2015-02-22 18:35:32 +0000844 BasicBlock * Preheader = CurLoop->getLoopPreheader();
Chris Lattner45d67d62003-02-24 03:52:32 +0000845
Chris Lattner1dc98b42010-08-29 06:43:52 +0000846 // It isn't safe to promote a load/store from the loop if the load/store is
847 // conditional. For example, turning:
Chris Lattner45d67d62003-02-24 03:52:32 +0000848 //
Chris Lattner1dc98b42010-08-29 06:43:52 +0000849 // for () { if (c) *P += 1; }
Chris Lattner45d67d62003-02-24 03:52:32 +0000850 //
Chris Lattner1dc98b42010-08-29 06:43:52 +0000851 // into:
852 //
853 // tmp = *P; for () { if (c) tmp +=1; } *P = tmp;
854 //
855 // is not safe, because *P may only be valid to access if 'c' is true.
Tobias Grossera3928f52011-07-06 19:20:02 +0000856 //
Chris Lattner1dc98b42010-08-29 06:43:52 +0000857 // It is safe to promote P if all uses are direct load/stores and if at
858 // least one is guaranteed to be executed.
859 bool GuaranteedToExecute = false;
Tobias Grosser4a5d9a92011-07-06 19:19:55 +0000860
Chris Lattner1dc98b42010-08-29 06:43:52 +0000861 SmallVector<Instruction*, 64> LoopUses;
862 SmallPtrSet<Value*, 4> PointerMustAliases;
Chris Lattner45d67d62003-02-24 03:52:32 +0000863
Tobias Grosser4a5d9a92011-07-06 19:19:55 +0000864 // We start with an alignment of one and try to find instructions that allow
865 // us to prove better alignment.
866 unsigned Alignment = 1;
Hal Finkelcc39b672014-07-24 12:16:19 +0000867 AAMDNodes AATags;
Bruno Cardoso Lopes46d5bf22014-11-28 19:47:46 +0000868 bool HasDedicatedExits = CurLoop->hasDedicatedExits();
Tobias Grosser4a5d9a92011-07-06 19:19:55 +0000869
Chris Lattner1dc98b42010-08-29 06:43:52 +0000870 // Check that all of the pointers in the alias set have the same type. We
871 // cannot (yet) promote a memory location that is loaded and stored in
Hal Finkelcc39b672014-07-24 12:16:19 +0000872 // different sizes. While we are at it, collect alignment and AA info.
Chris Lattner1dc98b42010-08-29 06:43:52 +0000873 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
874 Value *ASIV = ASI->getValue();
875 PointerMustAliases.insert(ASIV);
Tobias Grossera3928f52011-07-06 19:20:02 +0000876
Chris Lattnerf12c08d2008-05-22 00:53:38 +0000877 // Check that all of the pointers in the alias set have the same type. We
878 // cannot (yet) promote a memory location that is loaded and stored in
879 // different sizes.
Chris Lattner1dc98b42010-08-29 06:43:52 +0000880 if (SomePtr->getType() != ASIV->getType())
Hal Finkel3d4269a2015-02-22 18:35:32 +0000881 return Changed;
Tobias Grossera3928f52011-07-06 19:20:02 +0000882
Chandler Carruthcdf47882014-03-09 03:16:01 +0000883 for (User *U : ASIV->users()) {
Chris Lattner1dc98b42010-08-29 06:43:52 +0000884 // Ignore instructions that are outside the loop.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000885 Instruction *UI = dyn_cast<Instruction>(U);
886 if (!UI || !CurLoop->contains(UI))
Chris Lattnerf12c08d2008-05-22 00:53:38 +0000887 continue;
Tobias Grossera3928f52011-07-06 19:20:02 +0000888
Chris Lattner1dc98b42010-08-29 06:43:52 +0000889 // If there is an non-load/store instruction in the loop, we can't promote
890 // it.
Pete Cooper0cabcf22015-05-13 01:12:18 +0000891 if (const LoadInst *load = dyn_cast<LoadInst>(UI)) {
Eli Friedman91386c72011-08-15 20:52:09 +0000892 assert(!load->isVolatile() && "AST broken");
893 if (!load->isSimple())
Hal Finkel3d4269a2015-02-22 18:35:32 +0000894 return Changed;
Pete Cooper0cabcf22015-05-13 01:12:18 +0000895 } else if (const StoreInst *store = dyn_cast<StoreInst>(UI)) {
Chris Lattner408a6842010-12-19 05:57:25 +0000896 // Stores *of* the pointer are not interesting, only stores *to* the
897 // pointer.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000898 if (UI->getOperand(1) != ASIV)
Chris Lattner408a6842010-12-19 05:57:25 +0000899 continue;
Eli Friedman91386c72011-08-15 20:52:09 +0000900 assert(!store->isVolatile() && "AST broken");
901 if (!store->isSimple())
Hal Finkel3d4269a2015-02-22 18:35:32 +0000902 return Changed;
Bruno Cardoso Lopes46d5bf22014-11-28 19:47:46 +0000903 // Don't sink stores from loops without dedicated block exits. Exits
904 // containing indirect branches are not transformed by loop simplify,
Bruno Cardoso Lopesd035fbb2014-12-02 14:22:34 +0000905 // make sure we catch that. An additional load may be generated in the
906 // preheader for SSA updater, so also avoid sinking when no preheader
907 // is available.
908 if (!HasDedicatedExits || !Preheader)
Hal Finkel3d4269a2015-02-22 18:35:32 +0000909 return Changed;
Eli Friedman0cdc1482011-07-20 21:37:47 +0000910
911 // Note that we only check GuaranteedToExecute inside the store case
912 // so that we do not introduce stores where they did not exist before
913 // (which would break the LLVM concurrency model).
914
915 // If the alignment of this instruction allows us to specify a more
916 // restrictive (and performant) alignment and if we are sure this
917 // instruction will be executed, update the alignment.
918 // Larger is better, with the exception of 0 being the best alignment.
Eli Friedman91386c72011-08-15 20:52:09 +0000919 unsigned InstAlignment = store->getAlignment();
Chris Lattnerf5cca682012-12-31 08:37:17 +0000920 if ((InstAlignment > Alignment || InstAlignment == 0) && Alignment != 0)
Hal Finkel3d4269a2015-02-22 18:35:32 +0000921 if (isGuaranteedToExecute(*UI, DT, CurLoop, SafetyInfo)) {
Eli Friedman0cdc1482011-07-20 21:37:47 +0000922 GuaranteedToExecute = true;
923 Alignment = InstAlignment;
924 }
925
926 if (!GuaranteedToExecute)
Hal Finkel3d4269a2015-02-22 18:35:32 +0000927 GuaranteedToExecute = isGuaranteedToExecute(*UI, DT,
928 CurLoop, SafetyInfo);
Eli Friedman0cdc1482011-07-20 21:37:47 +0000929
Chris Lattnerbe901902010-09-06 05:11:24 +0000930 } else
Hal Finkel3d4269a2015-02-22 18:35:32 +0000931 return Changed; // Not a load or store.
Tobias Grosser4a5d9a92011-07-06 19:19:55 +0000932
Hal Finkelcc39b672014-07-24 12:16:19 +0000933 // Merge the AA tags.
Chris Lattnerf5cca682012-12-31 08:37:17 +0000934 if (LoopUses.empty()) {
Hal Finkelcc39b672014-07-24 12:16:19 +0000935 // On the first load/store, just take its AA tags.
936 UI->getAAMetadata(AATags);
937 } else if (AATags) {
938 UI->getAAMetadata(AATags, /* Merge = */ true);
Chris Lattnerf5cca682012-12-31 08:37:17 +0000939 }
Chandler Carruthcdf47882014-03-09 03:16:01 +0000940
941 LoopUses.push_back(UI);
Chris Lattner1dc98b42010-08-29 06:43:52 +0000942 }
943 }
Tobias Grosser4a5d9a92011-07-06 19:19:55 +0000944
Chris Lattner1dc98b42010-08-29 06:43:52 +0000945 // If there isn't a guaranteed-to-execute instruction, we can't promote.
946 if (!GuaranteedToExecute)
Hal Finkel3d4269a2015-02-22 18:35:32 +0000947 return Changed;
Tobias Grossera3928f52011-07-06 19:20:02 +0000948
Chris Lattner1dc98b42010-08-29 06:43:52 +0000949 // Otherwise, this is safe to promote, lets do it!
Tobias Grossera3928f52011-07-06 19:20:02 +0000950 DEBUG(dbgs() << "LICM: Promoting value stored to in loop: " <<*SomePtr<<'\n');
Chris Lattner1dc98b42010-08-29 06:43:52 +0000951 Changed = true;
952 ++NumPromoted;
953
Eli Friedmanddf7f552011-05-27 20:31:51 +0000954 // Grab a debug location for the inserted loads/stores; given that the
955 // inserted loads/stores have little relation to the original loads/stores,
956 // this code just arbitrarily picks a location from one, since any debug
957 // location is better than none.
958 DebugLoc DL = LoopUses[0]->getDebugLoc();
959
Dan Gohmanb9487362012-08-08 00:00:26 +0000960 // Figure out the loop exits and their insertion points, if this is the
961 // first promotion.
962 if (ExitBlocks.empty()) {
963 CurLoop->getUniqueExitBlocks(ExitBlocks);
964 InsertPts.resize(ExitBlocks.size());
965 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
966 InsertPts[i] = ExitBlocks[i]->getFirstInsertionPt();
967 }
Tobias Grossera3928f52011-07-06 19:20:02 +0000968
Chris Lattner1dc98b42010-08-29 06:43:52 +0000969 // We use the SSAUpdater interface to insert phi nodes as required.
970 SmallVector<PHINode*, 16> NewPHIs;
971 SSAUpdater SSA(&NewPHIs);
Pete Cooper7c4d7b82015-05-13 22:43:09 +0000972 LoopPromoter Promoter(SomePtr, LoopUses, SSA,
Pete Cooper41e0ee32015-05-13 01:12:16 +0000973 PointerMustAliases, ExitBlocks,
Hal Finkelcc39b672014-07-24 12:16:19 +0000974 InsertPts, PIC, *CurAST, *LI, DL, Alignment, AATags);
Tobias Grossera3928f52011-07-06 19:20:02 +0000975
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000976 // Set up the preheader to have a definition of the value. It is the live-out
977 // value from the preheader that uses in the loop will use.
978 LoadInst *PreheaderLoad =
979 new LoadInst(SomePtr, SomePtr->getName()+".promoted",
980 Preheader->getTerminator());
Tobias Grosser4a5d9a92011-07-06 19:19:55 +0000981 PreheaderLoad->setAlignment(Alignment);
Eli Friedmanddf7f552011-05-27 20:31:51 +0000982 PreheaderLoad->setDebugLoc(DL);
Hal Finkelcc39b672014-07-24 12:16:19 +0000983 if (AATags) PreheaderLoad->setAAMetadata(AATags);
Chris Lattner1dc98b42010-08-29 06:43:52 +0000984 SSA.AddAvailableValue(Preheader, PreheaderLoad);
985
Chris Lattnerb68ec5c2011-01-15 00:12:35 +0000986 // Rewrite all the loads in the loop and remember all the definitions from
987 // stores in the loop.
988 Promoter.run(LoopUses);
Eli Friedmanc5f22a72011-04-07 01:35:06 +0000989
990 // If the SSAUpdater didn't use the load in the preheader, just zap it now.
991 if (PreheaderLoad->use_empty())
992 PreheaderLoad->eraseFromParent();
Hal Finkel3d4269a2015-02-22 18:35:32 +0000993
994 return Changed;
Chris Lattnera51fa882002-08-22 21:39:55 +0000995}
Devang Patelb98a0972007-07-31 08:01:41 +0000996
Ashutosh Nema47802622015-08-13 11:18:35 +0000997/// Simple analysis hook. Clone alias set info.
Hal Finkel3d4269a2015-02-22 18:35:32 +0000998///
Devang Patelb98a0972007-07-31 08:01:41 +0000999void LICM::cloneBasicBlockAnalysis(BasicBlock *From, BasicBlock *To, Loop *L) {
Chris Lattnercc9cbc62010-08-29 17:46:00 +00001000 AliasSetTracker *AST = LoopToAliasSetMap.lookup(L);
Devang Patelb98a0972007-07-31 08:01:41 +00001001 if (!AST)
1002 return;
1003
1004 AST->copyValue(From, To);
1005}
1006
Hal Finkel3d4269a2015-02-22 18:35:32 +00001007/// Simple Analysis hook. Delete value V from alias set
1008///
Devang Patelb98a0972007-07-31 08:01:41 +00001009void LICM::deleteAnalysisValue(Value *V, Loop *L) {
Chris Lattnercc9cbc62010-08-29 17:46:00 +00001010 AliasSetTracker *AST = LoopToAliasSetMap.lookup(L);
Devang Patelb98a0972007-07-31 08:01:41 +00001011 if (!AST)
1012 return;
1013
1014 AST->deleteValue(V);
1015}
David Peixotto0d4d5e62014-09-24 16:48:31 +00001016
1017/// Simple Analysis hook. Delete value L from alias set map.
Hal Finkel3d4269a2015-02-22 18:35:32 +00001018///
David Peixotto0d4d5e62014-09-24 16:48:31 +00001019void LICM::deleteAnalysisLoop(Loop *L) {
1020 AliasSetTracker *AST = LoopToAliasSetMap.lookup(L);
1021 if (!AST)
1022 return;
1023
1024 delete AST;
1025 LoopToAliasSetMap.erase(L);
1026}
Hal Finkel3d4269a2015-02-22 18:35:32 +00001027
1028
1029/// Return true if the body of this loop may store into the memory
1030/// location pointed to by V.
1031///
1032static bool pointerInvalidatedByLoop(Value *V, uint64_t Size,
1033 const AAMDNodes &AAInfo,
1034 AliasSetTracker *CurAST) {
1035 // Check to see if any of the basic blocks in CurLoop invalidate *V.
1036 return CurAST->getAliasSetForPointer(V, Size, AAInfo).isMod();
1037}
1038
1039/// Little predicate that returns true if the specified basic block is in
1040/// a subloop of the current one, not the current one itself.
1041///
1042static bool inSubLoop(BasicBlock *BB, Loop *CurLoop, LoopInfo *LI) {
1043 assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop");
1044 return LI->getLoopFor(BB) != CurLoop;
1045}
1046