1f4a2713aSLionel Sambuc //===-- MachineLICM.cpp - Machine Loop Invariant Code Motion Pass ---------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This pass performs loop invariant code motion on machine instructions. We
11f4a2713aSLionel Sambuc // attempt to remove as much code from the body of a loop as possible.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc // This pass does not attempt to throttle itself to limit register pressure.
14f4a2713aSLionel Sambuc // The register allocation phases are expected to perform rematerialization
15f4a2713aSLionel Sambuc // to recover when register pressure is high.
16f4a2713aSLionel Sambuc //
17f4a2713aSLionel Sambuc // This pass is not intended to be a replacement or a complete alternative
18f4a2713aSLionel Sambuc // for the LLVM-IR-level LICM pass. It is only designed to hoist simple
19f4a2713aSLionel Sambuc // constructs that are not exposed before lowering and instruction selection.
20f4a2713aSLionel Sambuc //
21f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
22f4a2713aSLionel Sambuc
23f4a2713aSLionel Sambuc #include "llvm/CodeGen/Passes.h"
24f4a2713aSLionel Sambuc #include "llvm/ADT/DenseMap.h"
25f4a2713aSLionel Sambuc #include "llvm/ADT/SmallSet.h"
26f4a2713aSLionel Sambuc #include "llvm/ADT/Statistic.h"
27f4a2713aSLionel Sambuc #include "llvm/Analysis/AliasAnalysis.h"
28f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineDominators.h"
29f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineFrameInfo.h"
30f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineLoopInfo.h"
31f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineMemOperand.h"
32f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineRegisterInfo.h"
33f4a2713aSLionel Sambuc #include "llvm/CodeGen/PseudoSourceValue.h"
34f4a2713aSLionel Sambuc #include "llvm/MC/MCInstrItineraries.h"
35f4a2713aSLionel Sambuc #include "llvm/Support/CommandLine.h"
36f4a2713aSLionel Sambuc #include "llvm/Support/Debug.h"
37f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
38f4a2713aSLionel Sambuc #include "llvm/Target/TargetInstrInfo.h"
39f4a2713aSLionel Sambuc #include "llvm/Target/TargetLowering.h"
40f4a2713aSLionel Sambuc #include "llvm/Target/TargetMachine.h"
41f4a2713aSLionel Sambuc #include "llvm/Target/TargetRegisterInfo.h"
42*0a6a1f1dSLionel Sambuc #include "llvm/Target/TargetSubtargetInfo.h"
43f4a2713aSLionel Sambuc using namespace llvm;
44f4a2713aSLionel Sambuc
45*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "machine-licm"
46*0a6a1f1dSLionel Sambuc
47f4a2713aSLionel Sambuc static cl::opt<bool>
48f4a2713aSLionel Sambuc AvoidSpeculation("avoid-speculation",
49f4a2713aSLionel Sambuc cl::desc("MachineLICM should avoid speculation"),
50f4a2713aSLionel Sambuc cl::init(true), cl::Hidden);
51f4a2713aSLionel Sambuc
52*0a6a1f1dSLionel Sambuc static cl::opt<bool>
53*0a6a1f1dSLionel Sambuc HoistCheapInsts("hoist-cheap-insts",
54*0a6a1f1dSLionel Sambuc cl::desc("MachineLICM should hoist even cheap instructions"),
55*0a6a1f1dSLionel Sambuc cl::init(false), cl::Hidden);
56*0a6a1f1dSLionel Sambuc
57f4a2713aSLionel Sambuc STATISTIC(NumHoisted,
58f4a2713aSLionel Sambuc "Number of machine instructions hoisted out of loops");
59f4a2713aSLionel Sambuc STATISTIC(NumLowRP,
60f4a2713aSLionel Sambuc "Number of instructions hoisted in low reg pressure situation");
61f4a2713aSLionel Sambuc STATISTIC(NumHighLatency,
62f4a2713aSLionel Sambuc "Number of high latency instructions hoisted");
63f4a2713aSLionel Sambuc STATISTIC(NumCSEed,
64f4a2713aSLionel Sambuc "Number of hoisted machine instructions CSEed");
65f4a2713aSLionel Sambuc STATISTIC(NumPostRAHoisted,
66f4a2713aSLionel Sambuc "Number of machine instructions hoisted out of loops post regalloc");
67f4a2713aSLionel Sambuc
68f4a2713aSLionel Sambuc namespace {
69f4a2713aSLionel Sambuc class MachineLICM : public MachineFunctionPass {
70f4a2713aSLionel Sambuc const TargetInstrInfo *TII;
71f4a2713aSLionel Sambuc const TargetLoweringBase *TLI;
72f4a2713aSLionel Sambuc const TargetRegisterInfo *TRI;
73f4a2713aSLionel Sambuc const MachineFrameInfo *MFI;
74f4a2713aSLionel Sambuc MachineRegisterInfo *MRI;
75f4a2713aSLionel Sambuc const InstrItineraryData *InstrItins;
76f4a2713aSLionel Sambuc bool PreRegAlloc;
77f4a2713aSLionel Sambuc
78f4a2713aSLionel Sambuc // Various analyses that we use...
79f4a2713aSLionel Sambuc AliasAnalysis *AA; // Alias analysis info.
80f4a2713aSLionel Sambuc MachineLoopInfo *MLI; // Current MachineLoopInfo
81f4a2713aSLionel Sambuc MachineDominatorTree *DT; // Machine dominator tree for the cur loop
82f4a2713aSLionel Sambuc
83f4a2713aSLionel Sambuc // State that is updated as we process loops
84f4a2713aSLionel Sambuc bool Changed; // True if a loop is changed.
85f4a2713aSLionel Sambuc bool FirstInLoop; // True if it's the first LICM in the loop.
86f4a2713aSLionel Sambuc MachineLoop *CurLoop; // The current loop we are working on.
87f4a2713aSLionel Sambuc MachineBasicBlock *CurPreheader; // The preheader for CurLoop.
88f4a2713aSLionel Sambuc
89f4a2713aSLionel Sambuc // Exit blocks for CurLoop.
90f4a2713aSLionel Sambuc SmallVector<MachineBasicBlock*, 8> ExitBlocks;
91f4a2713aSLionel Sambuc
isExitBlock(const MachineBasicBlock * MBB) const92f4a2713aSLionel Sambuc bool isExitBlock(const MachineBasicBlock *MBB) const {
93f4a2713aSLionel Sambuc return std::find(ExitBlocks.begin(), ExitBlocks.end(), MBB) !=
94f4a2713aSLionel Sambuc ExitBlocks.end();
95f4a2713aSLionel Sambuc }
96f4a2713aSLionel Sambuc
97f4a2713aSLionel Sambuc // Track 'estimated' register pressure.
98f4a2713aSLionel Sambuc SmallSet<unsigned, 32> RegSeen;
99f4a2713aSLionel Sambuc SmallVector<unsigned, 8> RegPressure;
100f4a2713aSLionel Sambuc
101f4a2713aSLionel Sambuc // Register pressure "limit" per register class. If the pressure
102f4a2713aSLionel Sambuc // is higher than the limit, then it's considered high.
103f4a2713aSLionel Sambuc SmallVector<unsigned, 8> RegLimit;
104f4a2713aSLionel Sambuc
105f4a2713aSLionel Sambuc // Register pressure on path leading from loop preheader to current BB.
106f4a2713aSLionel Sambuc SmallVector<SmallVector<unsigned, 8>, 16> BackTrace;
107f4a2713aSLionel Sambuc
108f4a2713aSLionel Sambuc // For each opcode, keep a list of potential CSE instructions.
109f4a2713aSLionel Sambuc DenseMap<unsigned, std::vector<const MachineInstr*> > CSEMap;
110f4a2713aSLionel Sambuc
111f4a2713aSLionel Sambuc enum {
112f4a2713aSLionel Sambuc SpeculateFalse = 0,
113f4a2713aSLionel Sambuc SpeculateTrue = 1,
114f4a2713aSLionel Sambuc SpeculateUnknown = 2
115f4a2713aSLionel Sambuc };
116f4a2713aSLionel Sambuc
117f4a2713aSLionel Sambuc // If a MBB does not dominate loop exiting blocks then it may not safe
118f4a2713aSLionel Sambuc // to hoist loads from this block.
119f4a2713aSLionel Sambuc // Tri-state: 0 - false, 1 - true, 2 - unknown
120f4a2713aSLionel Sambuc unsigned SpeculationState;
121f4a2713aSLionel Sambuc
122f4a2713aSLionel Sambuc public:
123f4a2713aSLionel Sambuc static char ID; // Pass identification, replacement for typeid
MachineLICM()124f4a2713aSLionel Sambuc MachineLICM() :
125f4a2713aSLionel Sambuc MachineFunctionPass(ID), PreRegAlloc(true) {
126f4a2713aSLionel Sambuc initializeMachineLICMPass(*PassRegistry::getPassRegistry());
127f4a2713aSLionel Sambuc }
128f4a2713aSLionel Sambuc
MachineLICM(bool PreRA)129f4a2713aSLionel Sambuc explicit MachineLICM(bool PreRA) :
130f4a2713aSLionel Sambuc MachineFunctionPass(ID), PreRegAlloc(PreRA) {
131f4a2713aSLionel Sambuc initializeMachineLICMPass(*PassRegistry::getPassRegistry());
132f4a2713aSLionel Sambuc }
133f4a2713aSLionel Sambuc
134*0a6a1f1dSLionel Sambuc bool runOnMachineFunction(MachineFunction &MF) override;
135f4a2713aSLionel Sambuc
getAnalysisUsage(AnalysisUsage & AU) const136*0a6a1f1dSLionel Sambuc void getAnalysisUsage(AnalysisUsage &AU) const override {
137f4a2713aSLionel Sambuc AU.addRequired<MachineLoopInfo>();
138f4a2713aSLionel Sambuc AU.addRequired<MachineDominatorTree>();
139f4a2713aSLionel Sambuc AU.addRequired<AliasAnalysis>();
140f4a2713aSLionel Sambuc AU.addPreserved<MachineLoopInfo>();
141f4a2713aSLionel Sambuc AU.addPreserved<MachineDominatorTree>();
142f4a2713aSLionel Sambuc MachineFunctionPass::getAnalysisUsage(AU);
143f4a2713aSLionel Sambuc }
144f4a2713aSLionel Sambuc
releaseMemory()145*0a6a1f1dSLionel Sambuc void releaseMemory() override {
146f4a2713aSLionel Sambuc RegSeen.clear();
147f4a2713aSLionel Sambuc RegPressure.clear();
148f4a2713aSLionel Sambuc RegLimit.clear();
149f4a2713aSLionel Sambuc BackTrace.clear();
150f4a2713aSLionel Sambuc CSEMap.clear();
151f4a2713aSLionel Sambuc }
152f4a2713aSLionel Sambuc
153f4a2713aSLionel Sambuc private:
154f4a2713aSLionel Sambuc /// CandidateInfo - Keep track of information about hoisting candidates.
155f4a2713aSLionel Sambuc struct CandidateInfo {
156f4a2713aSLionel Sambuc MachineInstr *MI;
157f4a2713aSLionel Sambuc unsigned Def;
158f4a2713aSLionel Sambuc int FI;
CandidateInfo__anonb770ed810111::MachineLICM::CandidateInfo159f4a2713aSLionel Sambuc CandidateInfo(MachineInstr *mi, unsigned def, int fi)
160f4a2713aSLionel Sambuc : MI(mi), Def(def), FI(fi) {}
161f4a2713aSLionel Sambuc };
162f4a2713aSLionel Sambuc
163f4a2713aSLionel Sambuc /// HoistRegionPostRA - Walk the specified region of the CFG and hoist loop
164f4a2713aSLionel Sambuc /// invariants out to the preheader.
165f4a2713aSLionel Sambuc void HoistRegionPostRA();
166f4a2713aSLionel Sambuc
167f4a2713aSLionel Sambuc /// HoistPostRA - When an instruction is found to only use loop invariant
168f4a2713aSLionel Sambuc /// operands that is safe to hoist, this instruction is called to do the
169f4a2713aSLionel Sambuc /// dirty work.
170f4a2713aSLionel Sambuc void HoistPostRA(MachineInstr *MI, unsigned Def);
171f4a2713aSLionel Sambuc
172f4a2713aSLionel Sambuc /// ProcessMI - Examine the instruction for potentai LICM candidate. Also
173f4a2713aSLionel Sambuc /// gather register def and frame object update information.
174f4a2713aSLionel Sambuc void ProcessMI(MachineInstr *MI,
175f4a2713aSLionel Sambuc BitVector &PhysRegDefs,
176f4a2713aSLionel Sambuc BitVector &PhysRegClobbers,
177f4a2713aSLionel Sambuc SmallSet<int, 32> &StoredFIs,
178f4a2713aSLionel Sambuc SmallVectorImpl<CandidateInfo> &Candidates);
179f4a2713aSLionel Sambuc
180f4a2713aSLionel Sambuc /// AddToLiveIns - Add register 'Reg' to the livein sets of BBs in the
181f4a2713aSLionel Sambuc /// current loop.
182f4a2713aSLionel Sambuc void AddToLiveIns(unsigned Reg);
183f4a2713aSLionel Sambuc
184f4a2713aSLionel Sambuc /// IsLICMCandidate - Returns true if the instruction may be a suitable
185f4a2713aSLionel Sambuc /// candidate for LICM. e.g. If the instruction is a call, then it's
186f4a2713aSLionel Sambuc /// obviously not safe to hoist it.
187f4a2713aSLionel Sambuc bool IsLICMCandidate(MachineInstr &I);
188f4a2713aSLionel Sambuc
189f4a2713aSLionel Sambuc /// IsLoopInvariantInst - Returns true if the instruction is loop
190f4a2713aSLionel Sambuc /// invariant. I.e., all virtual register operands are defined outside of
191f4a2713aSLionel Sambuc /// the loop, physical registers aren't accessed (explicitly or implicitly),
192f4a2713aSLionel Sambuc /// and the instruction is hoistable.
193f4a2713aSLionel Sambuc ///
194f4a2713aSLionel Sambuc bool IsLoopInvariantInst(MachineInstr &I);
195f4a2713aSLionel Sambuc
196f4a2713aSLionel Sambuc /// HasLoopPHIUse - Return true if the specified instruction is used by any
197f4a2713aSLionel Sambuc /// phi node in the current loop.
198f4a2713aSLionel Sambuc bool HasLoopPHIUse(const MachineInstr *MI) const;
199f4a2713aSLionel Sambuc
200f4a2713aSLionel Sambuc /// HasHighOperandLatency - Compute operand latency between a def of 'Reg'
201f4a2713aSLionel Sambuc /// and an use in the current loop, return true if the target considered
202f4a2713aSLionel Sambuc /// it 'high'.
203f4a2713aSLionel Sambuc bool HasHighOperandLatency(MachineInstr &MI, unsigned DefIdx,
204f4a2713aSLionel Sambuc unsigned Reg) const;
205f4a2713aSLionel Sambuc
206f4a2713aSLionel Sambuc bool IsCheapInstruction(MachineInstr &MI) const;
207f4a2713aSLionel Sambuc
208f4a2713aSLionel Sambuc /// CanCauseHighRegPressure - Visit BBs from header to current BB,
209f4a2713aSLionel Sambuc /// check if hoisting an instruction of the given cost matrix can cause high
210f4a2713aSLionel Sambuc /// register pressure.
211f4a2713aSLionel Sambuc bool CanCauseHighRegPressure(DenseMap<unsigned, int> &Cost, bool Cheap);
212f4a2713aSLionel Sambuc
213f4a2713aSLionel Sambuc /// UpdateBackTraceRegPressure - Traverse the back trace from header to
214f4a2713aSLionel Sambuc /// the current block and update their register pressures to reflect the
215f4a2713aSLionel Sambuc /// effect of hoisting MI from the current block to the preheader.
216f4a2713aSLionel Sambuc void UpdateBackTraceRegPressure(const MachineInstr *MI);
217f4a2713aSLionel Sambuc
218f4a2713aSLionel Sambuc /// IsProfitableToHoist - Return true if it is potentially profitable to
219f4a2713aSLionel Sambuc /// hoist the given loop invariant.
220f4a2713aSLionel Sambuc bool IsProfitableToHoist(MachineInstr &MI);
221f4a2713aSLionel Sambuc
222f4a2713aSLionel Sambuc /// IsGuaranteedToExecute - Check if this mbb is guaranteed to execute.
223f4a2713aSLionel Sambuc /// If not then a load from this mbb may not be safe to hoist.
224f4a2713aSLionel Sambuc bool IsGuaranteedToExecute(MachineBasicBlock *BB);
225f4a2713aSLionel Sambuc
226f4a2713aSLionel Sambuc void EnterScope(MachineBasicBlock *MBB);
227f4a2713aSLionel Sambuc
228f4a2713aSLionel Sambuc void ExitScope(MachineBasicBlock *MBB);
229f4a2713aSLionel Sambuc
230f4a2713aSLionel Sambuc /// ExitScopeIfDone - Destroy scope for the MBB that corresponds to given
231f4a2713aSLionel Sambuc /// dominator tree node if its a leaf or all of its children are done. Walk
232f4a2713aSLionel Sambuc /// up the dominator tree to destroy ancestors which are now done.
233f4a2713aSLionel Sambuc void ExitScopeIfDone(MachineDomTreeNode *Node,
234f4a2713aSLionel Sambuc DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren,
235f4a2713aSLionel Sambuc DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> &ParentMap);
236f4a2713aSLionel Sambuc
237f4a2713aSLionel Sambuc /// HoistOutOfLoop - Walk the specified loop in the CFG (defined by all
238f4a2713aSLionel Sambuc /// blocks dominated by the specified header block, and that are in the
239f4a2713aSLionel Sambuc /// current loop) in depth first order w.r.t the DominatorTree. This allows
240f4a2713aSLionel Sambuc /// us to visit definitions before uses, allowing us to hoist a loop body in
241f4a2713aSLionel Sambuc /// one pass without iteration.
242f4a2713aSLionel Sambuc ///
243f4a2713aSLionel Sambuc void HoistOutOfLoop(MachineDomTreeNode *LoopHeaderNode);
244f4a2713aSLionel Sambuc void HoistRegion(MachineDomTreeNode *N, bool IsHeader);
245f4a2713aSLionel Sambuc
246f4a2713aSLionel Sambuc /// getRegisterClassIDAndCost - For a given MI, register, and the operand
247f4a2713aSLionel Sambuc /// index, return the ID and cost of its representative register class by
248f4a2713aSLionel Sambuc /// reference.
249f4a2713aSLionel Sambuc void getRegisterClassIDAndCost(const MachineInstr *MI,
250f4a2713aSLionel Sambuc unsigned Reg, unsigned OpIdx,
251f4a2713aSLionel Sambuc unsigned &RCId, unsigned &RCCost) const;
252f4a2713aSLionel Sambuc
253f4a2713aSLionel Sambuc /// InitRegPressure - Find all virtual register references that are liveout
254f4a2713aSLionel Sambuc /// of the preheader to initialize the starting "register pressure". Note
255f4a2713aSLionel Sambuc /// this does not count live through (livein but not used) registers.
256f4a2713aSLionel Sambuc void InitRegPressure(MachineBasicBlock *BB);
257f4a2713aSLionel Sambuc
258f4a2713aSLionel Sambuc /// UpdateRegPressure - Update estimate of register pressure after the
259f4a2713aSLionel Sambuc /// specified instruction.
260f4a2713aSLionel Sambuc void UpdateRegPressure(const MachineInstr *MI);
261f4a2713aSLionel Sambuc
262f4a2713aSLionel Sambuc /// ExtractHoistableLoad - Unfold a load from the given machineinstr if
263f4a2713aSLionel Sambuc /// the load itself could be hoisted. Return the unfolded and hoistable
264f4a2713aSLionel Sambuc /// load, or null if the load couldn't be unfolded or if it wouldn't
265f4a2713aSLionel Sambuc /// be hoistable.
266f4a2713aSLionel Sambuc MachineInstr *ExtractHoistableLoad(MachineInstr *MI);
267f4a2713aSLionel Sambuc
268f4a2713aSLionel Sambuc /// LookForDuplicate - Find an instruction amount PrevMIs that is a
269f4a2713aSLionel Sambuc /// duplicate of MI. Return this instruction if it's found.
270f4a2713aSLionel Sambuc const MachineInstr *LookForDuplicate(const MachineInstr *MI,
271f4a2713aSLionel Sambuc std::vector<const MachineInstr*> &PrevMIs);
272f4a2713aSLionel Sambuc
273f4a2713aSLionel Sambuc /// EliminateCSE - Given a LICM'ed instruction, look for an instruction on
274f4a2713aSLionel Sambuc /// the preheader that compute the same value. If it's found, do a RAU on
275f4a2713aSLionel Sambuc /// with the definition of the existing instruction rather than hoisting
276f4a2713aSLionel Sambuc /// the instruction to the preheader.
277f4a2713aSLionel Sambuc bool EliminateCSE(MachineInstr *MI,
278f4a2713aSLionel Sambuc DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI);
279f4a2713aSLionel Sambuc
280f4a2713aSLionel Sambuc /// MayCSE - Return true if the given instruction will be CSE'd if it's
281f4a2713aSLionel Sambuc /// hoisted out of the loop.
282f4a2713aSLionel Sambuc bool MayCSE(MachineInstr *MI);
283f4a2713aSLionel Sambuc
284f4a2713aSLionel Sambuc /// Hoist - When an instruction is found to only use loop invariant operands
285f4a2713aSLionel Sambuc /// that is safe to hoist, this instruction is called to do the dirty work.
286f4a2713aSLionel Sambuc /// It returns true if the instruction is hoisted.
287f4a2713aSLionel Sambuc bool Hoist(MachineInstr *MI, MachineBasicBlock *Preheader);
288f4a2713aSLionel Sambuc
289f4a2713aSLionel Sambuc /// InitCSEMap - Initialize the CSE map with instructions that are in the
290f4a2713aSLionel Sambuc /// current loop preheader that may become duplicates of instructions that
291f4a2713aSLionel Sambuc /// are hoisted out of the loop.
292f4a2713aSLionel Sambuc void InitCSEMap(MachineBasicBlock *BB);
293f4a2713aSLionel Sambuc
294f4a2713aSLionel Sambuc /// getCurPreheader - Get the preheader for the current loop, splitting
295f4a2713aSLionel Sambuc /// a critical edge if needed.
296f4a2713aSLionel Sambuc MachineBasicBlock *getCurPreheader();
297f4a2713aSLionel Sambuc };
298f4a2713aSLionel Sambuc } // end anonymous namespace
299f4a2713aSLionel Sambuc
300f4a2713aSLionel Sambuc char MachineLICM::ID = 0;
301f4a2713aSLionel Sambuc char &llvm::MachineLICMID = MachineLICM::ID;
302f4a2713aSLionel Sambuc INITIALIZE_PASS_BEGIN(MachineLICM, "machinelicm",
303f4a2713aSLionel Sambuc "Machine Loop Invariant Code Motion", false, false)
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)304f4a2713aSLionel Sambuc INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
305f4a2713aSLionel Sambuc INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
306f4a2713aSLionel Sambuc INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
307f4a2713aSLionel Sambuc INITIALIZE_PASS_END(MachineLICM, "machinelicm",
308f4a2713aSLionel Sambuc "Machine Loop Invariant Code Motion", false, false)
309f4a2713aSLionel Sambuc
310f4a2713aSLionel Sambuc /// LoopIsOuterMostWithPredecessor - Test if the given loop is the outer-most
311f4a2713aSLionel Sambuc /// loop that has a unique predecessor.
312f4a2713aSLionel Sambuc static bool LoopIsOuterMostWithPredecessor(MachineLoop *CurLoop) {
313f4a2713aSLionel Sambuc // Check whether this loop even has a unique predecessor.
314f4a2713aSLionel Sambuc if (!CurLoop->getLoopPredecessor())
315f4a2713aSLionel Sambuc return false;
316f4a2713aSLionel Sambuc // Ok, now check to see if any of its outer loops do.
317f4a2713aSLionel Sambuc for (MachineLoop *L = CurLoop->getParentLoop(); L; L = L->getParentLoop())
318f4a2713aSLionel Sambuc if (L->getLoopPredecessor())
319f4a2713aSLionel Sambuc return false;
320f4a2713aSLionel Sambuc // None of them did, so this is the outermost with a unique predecessor.
321f4a2713aSLionel Sambuc return true;
322f4a2713aSLionel Sambuc }
323f4a2713aSLionel Sambuc
runOnMachineFunction(MachineFunction & MF)324f4a2713aSLionel Sambuc bool MachineLICM::runOnMachineFunction(MachineFunction &MF) {
325*0a6a1f1dSLionel Sambuc if (skipOptnoneFunction(*MF.getFunction()))
326*0a6a1f1dSLionel Sambuc return false;
327*0a6a1f1dSLionel Sambuc
328f4a2713aSLionel Sambuc Changed = FirstInLoop = false;
329*0a6a1f1dSLionel Sambuc TII = MF.getSubtarget().getInstrInfo();
330*0a6a1f1dSLionel Sambuc TLI = MF.getSubtarget().getTargetLowering();
331*0a6a1f1dSLionel Sambuc TRI = MF.getSubtarget().getRegisterInfo();
332f4a2713aSLionel Sambuc MFI = MF.getFrameInfo();
333f4a2713aSLionel Sambuc MRI = &MF.getRegInfo();
334*0a6a1f1dSLionel Sambuc InstrItins = MF.getSubtarget().getInstrItineraryData();
335f4a2713aSLionel Sambuc
336f4a2713aSLionel Sambuc PreRegAlloc = MRI->isSSA();
337f4a2713aSLionel Sambuc
338f4a2713aSLionel Sambuc if (PreRegAlloc)
339f4a2713aSLionel Sambuc DEBUG(dbgs() << "******** Pre-regalloc Machine LICM: ");
340f4a2713aSLionel Sambuc else
341f4a2713aSLionel Sambuc DEBUG(dbgs() << "******** Post-regalloc Machine LICM: ");
342f4a2713aSLionel Sambuc DEBUG(dbgs() << MF.getName() << " ********\n");
343f4a2713aSLionel Sambuc
344f4a2713aSLionel Sambuc if (PreRegAlloc) {
345f4a2713aSLionel Sambuc // Estimate register pressure during pre-regalloc pass.
346f4a2713aSLionel Sambuc unsigned NumRC = TRI->getNumRegClasses();
347f4a2713aSLionel Sambuc RegPressure.resize(NumRC);
348f4a2713aSLionel Sambuc std::fill(RegPressure.begin(), RegPressure.end(), 0);
349f4a2713aSLionel Sambuc RegLimit.resize(NumRC);
350f4a2713aSLionel Sambuc for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
351f4a2713aSLionel Sambuc E = TRI->regclass_end(); I != E; ++I)
352f4a2713aSLionel Sambuc RegLimit[(*I)->getID()] = TRI->getRegPressureLimit(*I, MF);
353f4a2713aSLionel Sambuc }
354f4a2713aSLionel Sambuc
355f4a2713aSLionel Sambuc // Get our Loop information...
356f4a2713aSLionel Sambuc MLI = &getAnalysis<MachineLoopInfo>();
357f4a2713aSLionel Sambuc DT = &getAnalysis<MachineDominatorTree>();
358f4a2713aSLionel Sambuc AA = &getAnalysis<AliasAnalysis>();
359f4a2713aSLionel Sambuc
360f4a2713aSLionel Sambuc SmallVector<MachineLoop *, 8> Worklist(MLI->begin(), MLI->end());
361f4a2713aSLionel Sambuc while (!Worklist.empty()) {
362f4a2713aSLionel Sambuc CurLoop = Worklist.pop_back_val();
363*0a6a1f1dSLionel Sambuc CurPreheader = nullptr;
364f4a2713aSLionel Sambuc ExitBlocks.clear();
365f4a2713aSLionel Sambuc
366f4a2713aSLionel Sambuc // If this is done before regalloc, only visit outer-most preheader-sporting
367f4a2713aSLionel Sambuc // loops.
368f4a2713aSLionel Sambuc if (PreRegAlloc && !LoopIsOuterMostWithPredecessor(CurLoop)) {
369f4a2713aSLionel Sambuc Worklist.append(CurLoop->begin(), CurLoop->end());
370f4a2713aSLionel Sambuc continue;
371f4a2713aSLionel Sambuc }
372f4a2713aSLionel Sambuc
373f4a2713aSLionel Sambuc CurLoop->getExitBlocks(ExitBlocks);
374f4a2713aSLionel Sambuc
375f4a2713aSLionel Sambuc if (!PreRegAlloc)
376f4a2713aSLionel Sambuc HoistRegionPostRA();
377f4a2713aSLionel Sambuc else {
378f4a2713aSLionel Sambuc // CSEMap is initialized for loop header when the first instruction is
379f4a2713aSLionel Sambuc // being hoisted.
380f4a2713aSLionel Sambuc MachineDomTreeNode *N = DT->getNode(CurLoop->getHeader());
381f4a2713aSLionel Sambuc FirstInLoop = true;
382f4a2713aSLionel Sambuc HoistOutOfLoop(N);
383f4a2713aSLionel Sambuc CSEMap.clear();
384f4a2713aSLionel Sambuc }
385f4a2713aSLionel Sambuc }
386f4a2713aSLionel Sambuc
387f4a2713aSLionel Sambuc return Changed;
388f4a2713aSLionel Sambuc }
389f4a2713aSLionel Sambuc
390f4a2713aSLionel Sambuc /// InstructionStoresToFI - Return true if instruction stores to the
391f4a2713aSLionel Sambuc /// specified frame.
InstructionStoresToFI(const MachineInstr * MI,int FI)392f4a2713aSLionel Sambuc static bool InstructionStoresToFI(const MachineInstr *MI, int FI) {
393f4a2713aSLionel Sambuc for (MachineInstr::mmo_iterator o = MI->memoperands_begin(),
394f4a2713aSLionel Sambuc oe = MI->memoperands_end(); o != oe; ++o) {
395*0a6a1f1dSLionel Sambuc if (!(*o)->isStore() || !(*o)->getPseudoValue())
396f4a2713aSLionel Sambuc continue;
397f4a2713aSLionel Sambuc if (const FixedStackPseudoSourceValue *Value =
398*0a6a1f1dSLionel Sambuc dyn_cast<FixedStackPseudoSourceValue>((*o)->getPseudoValue())) {
399f4a2713aSLionel Sambuc if (Value->getFrameIndex() == FI)
400f4a2713aSLionel Sambuc return true;
401f4a2713aSLionel Sambuc }
402f4a2713aSLionel Sambuc }
403f4a2713aSLionel Sambuc return false;
404f4a2713aSLionel Sambuc }
405f4a2713aSLionel Sambuc
406f4a2713aSLionel Sambuc /// ProcessMI - Examine the instruction for potentai LICM candidate. Also
407f4a2713aSLionel Sambuc /// gather register def and frame object update information.
ProcessMI(MachineInstr * MI,BitVector & PhysRegDefs,BitVector & PhysRegClobbers,SmallSet<int,32> & StoredFIs,SmallVectorImpl<CandidateInfo> & Candidates)408f4a2713aSLionel Sambuc void MachineLICM::ProcessMI(MachineInstr *MI,
409f4a2713aSLionel Sambuc BitVector &PhysRegDefs,
410f4a2713aSLionel Sambuc BitVector &PhysRegClobbers,
411f4a2713aSLionel Sambuc SmallSet<int, 32> &StoredFIs,
412f4a2713aSLionel Sambuc SmallVectorImpl<CandidateInfo> &Candidates) {
413f4a2713aSLionel Sambuc bool RuledOut = false;
414f4a2713aSLionel Sambuc bool HasNonInvariantUse = false;
415f4a2713aSLionel Sambuc unsigned Def = 0;
416f4a2713aSLionel Sambuc for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
417f4a2713aSLionel Sambuc const MachineOperand &MO = MI->getOperand(i);
418f4a2713aSLionel Sambuc if (MO.isFI()) {
419f4a2713aSLionel Sambuc // Remember if the instruction stores to the frame index.
420f4a2713aSLionel Sambuc int FI = MO.getIndex();
421f4a2713aSLionel Sambuc if (!StoredFIs.count(FI) &&
422f4a2713aSLionel Sambuc MFI->isSpillSlotObjectIndex(FI) &&
423f4a2713aSLionel Sambuc InstructionStoresToFI(MI, FI))
424f4a2713aSLionel Sambuc StoredFIs.insert(FI);
425f4a2713aSLionel Sambuc HasNonInvariantUse = true;
426f4a2713aSLionel Sambuc continue;
427f4a2713aSLionel Sambuc }
428f4a2713aSLionel Sambuc
429f4a2713aSLionel Sambuc // We can't hoist an instruction defining a physreg that is clobbered in
430f4a2713aSLionel Sambuc // the loop.
431f4a2713aSLionel Sambuc if (MO.isRegMask()) {
432f4a2713aSLionel Sambuc PhysRegClobbers.setBitsNotInMask(MO.getRegMask());
433f4a2713aSLionel Sambuc continue;
434f4a2713aSLionel Sambuc }
435f4a2713aSLionel Sambuc
436f4a2713aSLionel Sambuc if (!MO.isReg())
437f4a2713aSLionel Sambuc continue;
438f4a2713aSLionel Sambuc unsigned Reg = MO.getReg();
439f4a2713aSLionel Sambuc if (!Reg)
440f4a2713aSLionel Sambuc continue;
441f4a2713aSLionel Sambuc assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
442f4a2713aSLionel Sambuc "Not expecting virtual register!");
443f4a2713aSLionel Sambuc
444f4a2713aSLionel Sambuc if (!MO.isDef()) {
445f4a2713aSLionel Sambuc if (Reg && (PhysRegDefs.test(Reg) || PhysRegClobbers.test(Reg)))
446f4a2713aSLionel Sambuc // If it's using a non-loop-invariant register, then it's obviously not
447f4a2713aSLionel Sambuc // safe to hoist.
448f4a2713aSLionel Sambuc HasNonInvariantUse = true;
449f4a2713aSLionel Sambuc continue;
450f4a2713aSLionel Sambuc }
451f4a2713aSLionel Sambuc
452f4a2713aSLionel Sambuc if (MO.isImplicit()) {
453f4a2713aSLionel Sambuc for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
454f4a2713aSLionel Sambuc PhysRegClobbers.set(*AI);
455f4a2713aSLionel Sambuc if (!MO.isDead())
456f4a2713aSLionel Sambuc // Non-dead implicit def? This cannot be hoisted.
457f4a2713aSLionel Sambuc RuledOut = true;
458f4a2713aSLionel Sambuc // No need to check if a dead implicit def is also defined by
459f4a2713aSLionel Sambuc // another instruction.
460f4a2713aSLionel Sambuc continue;
461f4a2713aSLionel Sambuc }
462f4a2713aSLionel Sambuc
463f4a2713aSLionel Sambuc // FIXME: For now, avoid instructions with multiple defs, unless
464f4a2713aSLionel Sambuc // it's a dead implicit def.
465f4a2713aSLionel Sambuc if (Def)
466f4a2713aSLionel Sambuc RuledOut = true;
467f4a2713aSLionel Sambuc else
468f4a2713aSLionel Sambuc Def = Reg;
469f4a2713aSLionel Sambuc
470f4a2713aSLionel Sambuc // If we have already seen another instruction that defines the same
471f4a2713aSLionel Sambuc // register, then this is not safe. Two defs is indicated by setting a
472f4a2713aSLionel Sambuc // PhysRegClobbers bit.
473f4a2713aSLionel Sambuc for (MCRegAliasIterator AS(Reg, TRI, true); AS.isValid(); ++AS) {
474f4a2713aSLionel Sambuc if (PhysRegDefs.test(*AS))
475f4a2713aSLionel Sambuc PhysRegClobbers.set(*AS);
476f4a2713aSLionel Sambuc PhysRegDefs.set(*AS);
477f4a2713aSLionel Sambuc }
478f4a2713aSLionel Sambuc if (PhysRegClobbers.test(Reg))
479f4a2713aSLionel Sambuc // MI defined register is seen defined by another instruction in
480f4a2713aSLionel Sambuc // the loop, it cannot be a LICM candidate.
481f4a2713aSLionel Sambuc RuledOut = true;
482f4a2713aSLionel Sambuc }
483f4a2713aSLionel Sambuc
484f4a2713aSLionel Sambuc // Only consider reloads for now and remats which do not have register
485f4a2713aSLionel Sambuc // operands. FIXME: Consider unfold load folding instructions.
486f4a2713aSLionel Sambuc if (Def && !RuledOut) {
487f4a2713aSLionel Sambuc int FI = INT_MIN;
488f4a2713aSLionel Sambuc if ((!HasNonInvariantUse && IsLICMCandidate(*MI)) ||
489f4a2713aSLionel Sambuc (TII->isLoadFromStackSlot(MI, FI) && MFI->isSpillSlotObjectIndex(FI)))
490f4a2713aSLionel Sambuc Candidates.push_back(CandidateInfo(MI, Def, FI));
491f4a2713aSLionel Sambuc }
492f4a2713aSLionel Sambuc }
493f4a2713aSLionel Sambuc
494f4a2713aSLionel Sambuc /// HoistRegionPostRA - Walk the specified region of the CFG and hoist loop
495f4a2713aSLionel Sambuc /// invariants out to the preheader.
HoistRegionPostRA()496f4a2713aSLionel Sambuc void MachineLICM::HoistRegionPostRA() {
497f4a2713aSLionel Sambuc MachineBasicBlock *Preheader = getCurPreheader();
498f4a2713aSLionel Sambuc if (!Preheader)
499f4a2713aSLionel Sambuc return;
500f4a2713aSLionel Sambuc
501f4a2713aSLionel Sambuc unsigned NumRegs = TRI->getNumRegs();
502f4a2713aSLionel Sambuc BitVector PhysRegDefs(NumRegs); // Regs defined once in the loop.
503f4a2713aSLionel Sambuc BitVector PhysRegClobbers(NumRegs); // Regs defined more than once.
504f4a2713aSLionel Sambuc
505f4a2713aSLionel Sambuc SmallVector<CandidateInfo, 32> Candidates;
506f4a2713aSLionel Sambuc SmallSet<int, 32> StoredFIs;
507f4a2713aSLionel Sambuc
508f4a2713aSLionel Sambuc // Walk the entire region, count number of defs for each register, and
509f4a2713aSLionel Sambuc // collect potential LICM candidates.
510f4a2713aSLionel Sambuc const std::vector<MachineBasicBlock *> &Blocks = CurLoop->getBlocks();
511f4a2713aSLionel Sambuc for (unsigned i = 0, e = Blocks.size(); i != e; ++i) {
512f4a2713aSLionel Sambuc MachineBasicBlock *BB = Blocks[i];
513f4a2713aSLionel Sambuc
514f4a2713aSLionel Sambuc // If the header of the loop containing this basic block is a landing pad,
515f4a2713aSLionel Sambuc // then don't try to hoist instructions out of this loop.
516f4a2713aSLionel Sambuc const MachineLoop *ML = MLI->getLoopFor(BB);
517f4a2713aSLionel Sambuc if (ML && ML->getHeader()->isLandingPad()) continue;
518f4a2713aSLionel Sambuc
519f4a2713aSLionel Sambuc // Conservatively treat live-in's as an external def.
520f4a2713aSLionel Sambuc // FIXME: That means a reload that're reused in successor block(s) will not
521f4a2713aSLionel Sambuc // be LICM'ed.
522f4a2713aSLionel Sambuc for (MachineBasicBlock::livein_iterator I = BB->livein_begin(),
523f4a2713aSLionel Sambuc E = BB->livein_end(); I != E; ++I) {
524f4a2713aSLionel Sambuc unsigned Reg = *I;
525f4a2713aSLionel Sambuc for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
526f4a2713aSLionel Sambuc PhysRegDefs.set(*AI);
527f4a2713aSLionel Sambuc }
528f4a2713aSLionel Sambuc
529f4a2713aSLionel Sambuc SpeculationState = SpeculateUnknown;
530f4a2713aSLionel Sambuc for (MachineBasicBlock::iterator
531f4a2713aSLionel Sambuc MII = BB->begin(), E = BB->end(); MII != E; ++MII) {
532f4a2713aSLionel Sambuc MachineInstr *MI = &*MII;
533f4a2713aSLionel Sambuc ProcessMI(MI, PhysRegDefs, PhysRegClobbers, StoredFIs, Candidates);
534f4a2713aSLionel Sambuc }
535f4a2713aSLionel Sambuc }
536f4a2713aSLionel Sambuc
537f4a2713aSLionel Sambuc // Gather the registers read / clobbered by the terminator.
538f4a2713aSLionel Sambuc BitVector TermRegs(NumRegs);
539f4a2713aSLionel Sambuc MachineBasicBlock::iterator TI = Preheader->getFirstTerminator();
540f4a2713aSLionel Sambuc if (TI != Preheader->end()) {
541f4a2713aSLionel Sambuc for (unsigned i = 0, e = TI->getNumOperands(); i != e; ++i) {
542f4a2713aSLionel Sambuc const MachineOperand &MO = TI->getOperand(i);
543f4a2713aSLionel Sambuc if (!MO.isReg())
544f4a2713aSLionel Sambuc continue;
545f4a2713aSLionel Sambuc unsigned Reg = MO.getReg();
546f4a2713aSLionel Sambuc if (!Reg)
547f4a2713aSLionel Sambuc continue;
548f4a2713aSLionel Sambuc for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
549f4a2713aSLionel Sambuc TermRegs.set(*AI);
550f4a2713aSLionel Sambuc }
551f4a2713aSLionel Sambuc }
552f4a2713aSLionel Sambuc
553f4a2713aSLionel Sambuc // Now evaluate whether the potential candidates qualify.
554f4a2713aSLionel Sambuc // 1. Check if the candidate defined register is defined by another
555f4a2713aSLionel Sambuc // instruction in the loop.
556f4a2713aSLionel Sambuc // 2. If the candidate is a load from stack slot (always true for now),
557f4a2713aSLionel Sambuc // check if the slot is stored anywhere in the loop.
558f4a2713aSLionel Sambuc // 3. Make sure candidate def should not clobber
559f4a2713aSLionel Sambuc // registers read by the terminator. Similarly its def should not be
560f4a2713aSLionel Sambuc // clobbered by the terminator.
561f4a2713aSLionel Sambuc for (unsigned i = 0, e = Candidates.size(); i != e; ++i) {
562f4a2713aSLionel Sambuc if (Candidates[i].FI != INT_MIN &&
563f4a2713aSLionel Sambuc StoredFIs.count(Candidates[i].FI))
564f4a2713aSLionel Sambuc continue;
565f4a2713aSLionel Sambuc
566f4a2713aSLionel Sambuc unsigned Def = Candidates[i].Def;
567f4a2713aSLionel Sambuc if (!PhysRegClobbers.test(Def) && !TermRegs.test(Def)) {
568f4a2713aSLionel Sambuc bool Safe = true;
569f4a2713aSLionel Sambuc MachineInstr *MI = Candidates[i].MI;
570f4a2713aSLionel Sambuc for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
571f4a2713aSLionel Sambuc const MachineOperand &MO = MI->getOperand(j);
572f4a2713aSLionel Sambuc if (!MO.isReg() || MO.isDef() || !MO.getReg())
573f4a2713aSLionel Sambuc continue;
574f4a2713aSLionel Sambuc unsigned Reg = MO.getReg();
575f4a2713aSLionel Sambuc if (PhysRegDefs.test(Reg) ||
576f4a2713aSLionel Sambuc PhysRegClobbers.test(Reg)) {
577f4a2713aSLionel Sambuc // If it's using a non-loop-invariant register, then it's obviously
578f4a2713aSLionel Sambuc // not safe to hoist.
579f4a2713aSLionel Sambuc Safe = false;
580f4a2713aSLionel Sambuc break;
581f4a2713aSLionel Sambuc }
582f4a2713aSLionel Sambuc }
583f4a2713aSLionel Sambuc if (Safe)
584f4a2713aSLionel Sambuc HoistPostRA(MI, Candidates[i].Def);
585f4a2713aSLionel Sambuc }
586f4a2713aSLionel Sambuc }
587f4a2713aSLionel Sambuc }
588f4a2713aSLionel Sambuc
589f4a2713aSLionel Sambuc /// AddToLiveIns - Add register 'Reg' to the livein sets of BBs in the current
590f4a2713aSLionel Sambuc /// loop, and make sure it is not killed by any instructions in the loop.
AddToLiveIns(unsigned Reg)591f4a2713aSLionel Sambuc void MachineLICM::AddToLiveIns(unsigned Reg) {
592f4a2713aSLionel Sambuc const std::vector<MachineBasicBlock *> &Blocks = CurLoop->getBlocks();
593f4a2713aSLionel Sambuc for (unsigned i = 0, e = Blocks.size(); i != e; ++i) {
594f4a2713aSLionel Sambuc MachineBasicBlock *BB = Blocks[i];
595f4a2713aSLionel Sambuc if (!BB->isLiveIn(Reg))
596f4a2713aSLionel Sambuc BB->addLiveIn(Reg);
597f4a2713aSLionel Sambuc for (MachineBasicBlock::iterator
598f4a2713aSLionel Sambuc MII = BB->begin(), E = BB->end(); MII != E; ++MII) {
599f4a2713aSLionel Sambuc MachineInstr *MI = &*MII;
600f4a2713aSLionel Sambuc for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
601f4a2713aSLionel Sambuc MachineOperand &MO = MI->getOperand(i);
602f4a2713aSLionel Sambuc if (!MO.isReg() || !MO.getReg() || MO.isDef()) continue;
603f4a2713aSLionel Sambuc if (MO.getReg() == Reg || TRI->isSuperRegister(Reg, MO.getReg()))
604f4a2713aSLionel Sambuc MO.setIsKill(false);
605f4a2713aSLionel Sambuc }
606f4a2713aSLionel Sambuc }
607f4a2713aSLionel Sambuc }
608f4a2713aSLionel Sambuc }
609f4a2713aSLionel Sambuc
610f4a2713aSLionel Sambuc /// HoistPostRA - When an instruction is found to only use loop invariant
611f4a2713aSLionel Sambuc /// operands that is safe to hoist, this instruction is called to do the
612f4a2713aSLionel Sambuc /// dirty work.
HoistPostRA(MachineInstr * MI,unsigned Def)613f4a2713aSLionel Sambuc void MachineLICM::HoistPostRA(MachineInstr *MI, unsigned Def) {
614f4a2713aSLionel Sambuc MachineBasicBlock *Preheader = getCurPreheader();
615f4a2713aSLionel Sambuc
616f4a2713aSLionel Sambuc // Now move the instructions to the predecessor, inserting it before any
617f4a2713aSLionel Sambuc // terminator instructions.
618f4a2713aSLionel Sambuc DEBUG(dbgs() << "Hoisting to BB#" << Preheader->getNumber() << " from BB#"
619f4a2713aSLionel Sambuc << MI->getParent()->getNumber() << ": " << *MI);
620f4a2713aSLionel Sambuc
621f4a2713aSLionel Sambuc // Splice the instruction to the preheader.
622f4a2713aSLionel Sambuc MachineBasicBlock *MBB = MI->getParent();
623f4a2713aSLionel Sambuc Preheader->splice(Preheader->getFirstTerminator(), MBB, MI);
624f4a2713aSLionel Sambuc
625f4a2713aSLionel Sambuc // Add register to livein list to all the BBs in the current loop since a
626f4a2713aSLionel Sambuc // loop invariant must be kept live throughout the whole loop. This is
627f4a2713aSLionel Sambuc // important to ensure later passes do not scavenge the def register.
628f4a2713aSLionel Sambuc AddToLiveIns(Def);
629f4a2713aSLionel Sambuc
630f4a2713aSLionel Sambuc ++NumPostRAHoisted;
631f4a2713aSLionel Sambuc Changed = true;
632f4a2713aSLionel Sambuc }
633f4a2713aSLionel Sambuc
634f4a2713aSLionel Sambuc // IsGuaranteedToExecute - Check if this mbb is guaranteed to execute.
635f4a2713aSLionel Sambuc // If not then a load from this mbb may not be safe to hoist.
IsGuaranteedToExecute(MachineBasicBlock * BB)636f4a2713aSLionel Sambuc bool MachineLICM::IsGuaranteedToExecute(MachineBasicBlock *BB) {
637f4a2713aSLionel Sambuc if (SpeculationState != SpeculateUnknown)
638f4a2713aSLionel Sambuc return SpeculationState == SpeculateFalse;
639f4a2713aSLionel Sambuc
640f4a2713aSLionel Sambuc if (BB != CurLoop->getHeader()) {
641f4a2713aSLionel Sambuc // Check loop exiting blocks.
642f4a2713aSLionel Sambuc SmallVector<MachineBasicBlock*, 8> CurrentLoopExitingBlocks;
643f4a2713aSLionel Sambuc CurLoop->getExitingBlocks(CurrentLoopExitingBlocks);
644f4a2713aSLionel Sambuc for (unsigned i = 0, e = CurrentLoopExitingBlocks.size(); i != e; ++i)
645f4a2713aSLionel Sambuc if (!DT->dominates(BB, CurrentLoopExitingBlocks[i])) {
646f4a2713aSLionel Sambuc SpeculationState = SpeculateTrue;
647f4a2713aSLionel Sambuc return false;
648f4a2713aSLionel Sambuc }
649f4a2713aSLionel Sambuc }
650f4a2713aSLionel Sambuc
651f4a2713aSLionel Sambuc SpeculationState = SpeculateFalse;
652f4a2713aSLionel Sambuc return true;
653f4a2713aSLionel Sambuc }
654f4a2713aSLionel Sambuc
EnterScope(MachineBasicBlock * MBB)655f4a2713aSLionel Sambuc void MachineLICM::EnterScope(MachineBasicBlock *MBB) {
656f4a2713aSLionel Sambuc DEBUG(dbgs() << "Entering: " << MBB->getName() << '\n');
657f4a2713aSLionel Sambuc
658f4a2713aSLionel Sambuc // Remember livein register pressure.
659f4a2713aSLionel Sambuc BackTrace.push_back(RegPressure);
660f4a2713aSLionel Sambuc }
661f4a2713aSLionel Sambuc
ExitScope(MachineBasicBlock * MBB)662f4a2713aSLionel Sambuc void MachineLICM::ExitScope(MachineBasicBlock *MBB) {
663f4a2713aSLionel Sambuc DEBUG(dbgs() << "Exiting: " << MBB->getName() << '\n');
664f4a2713aSLionel Sambuc BackTrace.pop_back();
665f4a2713aSLionel Sambuc }
666f4a2713aSLionel Sambuc
667f4a2713aSLionel Sambuc /// ExitScopeIfDone - Destroy scope for the MBB that corresponds to the given
668f4a2713aSLionel Sambuc /// dominator tree node if its a leaf or all of its children are done. Walk
669f4a2713aSLionel Sambuc /// up the dominator tree to destroy ancestors which are now done.
ExitScopeIfDone(MachineDomTreeNode * Node,DenseMap<MachineDomTreeNode *,unsigned> & OpenChildren,DenseMap<MachineDomTreeNode *,MachineDomTreeNode * > & ParentMap)670f4a2713aSLionel Sambuc void MachineLICM::ExitScopeIfDone(MachineDomTreeNode *Node,
671f4a2713aSLionel Sambuc DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren,
672f4a2713aSLionel Sambuc DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> &ParentMap) {
673f4a2713aSLionel Sambuc if (OpenChildren[Node])
674f4a2713aSLionel Sambuc return;
675f4a2713aSLionel Sambuc
676f4a2713aSLionel Sambuc // Pop scope.
677f4a2713aSLionel Sambuc ExitScope(Node->getBlock());
678f4a2713aSLionel Sambuc
679f4a2713aSLionel Sambuc // Now traverse upwards to pop ancestors whose offsprings are all done.
680f4a2713aSLionel Sambuc while (MachineDomTreeNode *Parent = ParentMap[Node]) {
681f4a2713aSLionel Sambuc unsigned Left = --OpenChildren[Parent];
682f4a2713aSLionel Sambuc if (Left != 0)
683f4a2713aSLionel Sambuc break;
684f4a2713aSLionel Sambuc ExitScope(Parent->getBlock());
685f4a2713aSLionel Sambuc Node = Parent;
686f4a2713aSLionel Sambuc }
687f4a2713aSLionel Sambuc }
688f4a2713aSLionel Sambuc
689f4a2713aSLionel Sambuc /// HoistOutOfLoop - Walk the specified loop in the CFG (defined by all
690f4a2713aSLionel Sambuc /// blocks dominated by the specified header block, and that are in the
691f4a2713aSLionel Sambuc /// current loop) in depth first order w.r.t the DominatorTree. This allows
692f4a2713aSLionel Sambuc /// us to visit definitions before uses, allowing us to hoist a loop body in
693f4a2713aSLionel Sambuc /// one pass without iteration.
694f4a2713aSLionel Sambuc ///
HoistOutOfLoop(MachineDomTreeNode * HeaderN)695f4a2713aSLionel Sambuc void MachineLICM::HoistOutOfLoop(MachineDomTreeNode *HeaderN) {
696f4a2713aSLionel Sambuc SmallVector<MachineDomTreeNode*, 32> Scopes;
697f4a2713aSLionel Sambuc SmallVector<MachineDomTreeNode*, 8> WorkList;
698f4a2713aSLionel Sambuc DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> ParentMap;
699f4a2713aSLionel Sambuc DenseMap<MachineDomTreeNode*, unsigned> OpenChildren;
700f4a2713aSLionel Sambuc
701f4a2713aSLionel Sambuc // Perform a DFS walk to determine the order of visit.
702f4a2713aSLionel Sambuc WorkList.push_back(HeaderN);
703f4a2713aSLionel Sambuc do {
704f4a2713aSLionel Sambuc MachineDomTreeNode *Node = WorkList.pop_back_val();
705*0a6a1f1dSLionel Sambuc assert(Node && "Null dominator tree node?");
706f4a2713aSLionel Sambuc MachineBasicBlock *BB = Node->getBlock();
707f4a2713aSLionel Sambuc
708f4a2713aSLionel Sambuc // If the header of the loop containing this basic block is a landing pad,
709f4a2713aSLionel Sambuc // then don't try to hoist instructions out of this loop.
710f4a2713aSLionel Sambuc const MachineLoop *ML = MLI->getLoopFor(BB);
711f4a2713aSLionel Sambuc if (ML && ML->getHeader()->isLandingPad())
712f4a2713aSLionel Sambuc continue;
713f4a2713aSLionel Sambuc
714f4a2713aSLionel Sambuc // If this subregion is not in the top level loop at all, exit.
715f4a2713aSLionel Sambuc if (!CurLoop->contains(BB))
716f4a2713aSLionel Sambuc continue;
717f4a2713aSLionel Sambuc
718f4a2713aSLionel Sambuc Scopes.push_back(Node);
719f4a2713aSLionel Sambuc const std::vector<MachineDomTreeNode*> &Children = Node->getChildren();
720f4a2713aSLionel Sambuc unsigned NumChildren = Children.size();
721f4a2713aSLionel Sambuc
722f4a2713aSLionel Sambuc // Don't hoist things out of a large switch statement. This often causes
723f4a2713aSLionel Sambuc // code to be hoisted that wasn't going to be executed, and increases
724f4a2713aSLionel Sambuc // register pressure in a situation where it's likely to matter.
725f4a2713aSLionel Sambuc if (BB->succ_size() >= 25)
726f4a2713aSLionel Sambuc NumChildren = 0;
727f4a2713aSLionel Sambuc
728f4a2713aSLionel Sambuc OpenChildren[Node] = NumChildren;
729f4a2713aSLionel Sambuc // Add children in reverse order as then the next popped worklist node is
730f4a2713aSLionel Sambuc // the first child of this node. This means we ultimately traverse the
731f4a2713aSLionel Sambuc // DOM tree in exactly the same order as if we'd recursed.
732f4a2713aSLionel Sambuc for (int i = (int)NumChildren-1; i >= 0; --i) {
733f4a2713aSLionel Sambuc MachineDomTreeNode *Child = Children[i];
734f4a2713aSLionel Sambuc ParentMap[Child] = Node;
735f4a2713aSLionel Sambuc WorkList.push_back(Child);
736f4a2713aSLionel Sambuc }
737f4a2713aSLionel Sambuc } while (!WorkList.empty());
738f4a2713aSLionel Sambuc
739f4a2713aSLionel Sambuc if (Scopes.size() != 0) {
740f4a2713aSLionel Sambuc MachineBasicBlock *Preheader = getCurPreheader();
741f4a2713aSLionel Sambuc if (!Preheader)
742f4a2713aSLionel Sambuc return;
743f4a2713aSLionel Sambuc
744f4a2713aSLionel Sambuc // Compute registers which are livein into the loop headers.
745f4a2713aSLionel Sambuc RegSeen.clear();
746f4a2713aSLionel Sambuc BackTrace.clear();
747f4a2713aSLionel Sambuc InitRegPressure(Preheader);
748f4a2713aSLionel Sambuc }
749f4a2713aSLionel Sambuc
750f4a2713aSLionel Sambuc // Now perform LICM.
751f4a2713aSLionel Sambuc for (unsigned i = 0, e = Scopes.size(); i != e; ++i) {
752f4a2713aSLionel Sambuc MachineDomTreeNode *Node = Scopes[i];
753f4a2713aSLionel Sambuc MachineBasicBlock *MBB = Node->getBlock();
754f4a2713aSLionel Sambuc
755f4a2713aSLionel Sambuc MachineBasicBlock *Preheader = getCurPreheader();
756f4a2713aSLionel Sambuc if (!Preheader)
757f4a2713aSLionel Sambuc continue;
758f4a2713aSLionel Sambuc
759f4a2713aSLionel Sambuc EnterScope(MBB);
760f4a2713aSLionel Sambuc
761f4a2713aSLionel Sambuc // Process the block
762f4a2713aSLionel Sambuc SpeculationState = SpeculateUnknown;
763f4a2713aSLionel Sambuc for (MachineBasicBlock::iterator
764f4a2713aSLionel Sambuc MII = MBB->begin(), E = MBB->end(); MII != E; ) {
765f4a2713aSLionel Sambuc MachineBasicBlock::iterator NextMII = MII; ++NextMII;
766f4a2713aSLionel Sambuc MachineInstr *MI = &*MII;
767f4a2713aSLionel Sambuc if (!Hoist(MI, Preheader))
768f4a2713aSLionel Sambuc UpdateRegPressure(MI);
769f4a2713aSLionel Sambuc MII = NextMII;
770f4a2713aSLionel Sambuc }
771f4a2713aSLionel Sambuc
772f4a2713aSLionel Sambuc // If it's a leaf node, it's done. Traverse upwards to pop ancestors.
773f4a2713aSLionel Sambuc ExitScopeIfDone(Node, OpenChildren, ParentMap);
774f4a2713aSLionel Sambuc }
775f4a2713aSLionel Sambuc }
776f4a2713aSLionel Sambuc
isOperandKill(const MachineOperand & MO,MachineRegisterInfo * MRI)777f4a2713aSLionel Sambuc static bool isOperandKill(const MachineOperand &MO, MachineRegisterInfo *MRI) {
778f4a2713aSLionel Sambuc return MO.isKill() || MRI->hasOneNonDBGUse(MO.getReg());
779f4a2713aSLionel Sambuc }
780f4a2713aSLionel Sambuc
781f4a2713aSLionel Sambuc /// getRegisterClassIDAndCost - For a given MI, register, and the operand
782f4a2713aSLionel Sambuc /// index, return the ID and cost of its representative register class.
783f4a2713aSLionel Sambuc void
getRegisterClassIDAndCost(const MachineInstr * MI,unsigned Reg,unsigned OpIdx,unsigned & RCId,unsigned & RCCost) const784f4a2713aSLionel Sambuc MachineLICM::getRegisterClassIDAndCost(const MachineInstr *MI,
785f4a2713aSLionel Sambuc unsigned Reg, unsigned OpIdx,
786f4a2713aSLionel Sambuc unsigned &RCId, unsigned &RCCost) const {
787f4a2713aSLionel Sambuc const TargetRegisterClass *RC = MRI->getRegClass(Reg);
788f4a2713aSLionel Sambuc MVT VT = *RC->vt_begin();
789f4a2713aSLionel Sambuc if (VT == MVT::Untyped) {
790f4a2713aSLionel Sambuc RCId = RC->getID();
791f4a2713aSLionel Sambuc RCCost = 1;
792f4a2713aSLionel Sambuc } else {
793f4a2713aSLionel Sambuc RCId = TLI->getRepRegClassFor(VT)->getID();
794f4a2713aSLionel Sambuc RCCost = TLI->getRepRegClassCostFor(VT);
795f4a2713aSLionel Sambuc }
796f4a2713aSLionel Sambuc }
797f4a2713aSLionel Sambuc
798f4a2713aSLionel Sambuc /// InitRegPressure - Find all virtual register references that are liveout of
799f4a2713aSLionel Sambuc /// the preheader to initialize the starting "register pressure". Note this
800f4a2713aSLionel Sambuc /// does not count live through (livein but not used) registers.
InitRegPressure(MachineBasicBlock * BB)801f4a2713aSLionel Sambuc void MachineLICM::InitRegPressure(MachineBasicBlock *BB) {
802f4a2713aSLionel Sambuc std::fill(RegPressure.begin(), RegPressure.end(), 0);
803f4a2713aSLionel Sambuc
804f4a2713aSLionel Sambuc // If the preheader has only a single predecessor and it ends with a
805f4a2713aSLionel Sambuc // fallthrough or an unconditional branch, then scan its predecessor for live
806f4a2713aSLionel Sambuc // defs as well. This happens whenever the preheader is created by splitting
807f4a2713aSLionel Sambuc // the critical edge from the loop predecessor to the loop header.
808f4a2713aSLionel Sambuc if (BB->pred_size() == 1) {
809*0a6a1f1dSLionel Sambuc MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
810f4a2713aSLionel Sambuc SmallVector<MachineOperand, 4> Cond;
811f4a2713aSLionel Sambuc if (!TII->AnalyzeBranch(*BB, TBB, FBB, Cond, false) && Cond.empty())
812f4a2713aSLionel Sambuc InitRegPressure(*BB->pred_begin());
813f4a2713aSLionel Sambuc }
814f4a2713aSLionel Sambuc
815f4a2713aSLionel Sambuc for (MachineBasicBlock::iterator MII = BB->begin(), E = BB->end();
816f4a2713aSLionel Sambuc MII != E; ++MII) {
817f4a2713aSLionel Sambuc MachineInstr *MI = &*MII;
818f4a2713aSLionel Sambuc for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) {
819f4a2713aSLionel Sambuc const MachineOperand &MO = MI->getOperand(i);
820f4a2713aSLionel Sambuc if (!MO.isReg() || MO.isImplicit())
821f4a2713aSLionel Sambuc continue;
822f4a2713aSLionel Sambuc unsigned Reg = MO.getReg();
823f4a2713aSLionel Sambuc if (!TargetRegisterInfo::isVirtualRegister(Reg))
824f4a2713aSLionel Sambuc continue;
825f4a2713aSLionel Sambuc
826*0a6a1f1dSLionel Sambuc bool isNew = RegSeen.insert(Reg).second;
827f4a2713aSLionel Sambuc unsigned RCId, RCCost;
828f4a2713aSLionel Sambuc getRegisterClassIDAndCost(MI, Reg, i, RCId, RCCost);
829f4a2713aSLionel Sambuc if (MO.isDef())
830f4a2713aSLionel Sambuc RegPressure[RCId] += RCCost;
831f4a2713aSLionel Sambuc else {
832f4a2713aSLionel Sambuc bool isKill = isOperandKill(MO, MRI);
833f4a2713aSLionel Sambuc if (isNew && !isKill)
834f4a2713aSLionel Sambuc // Haven't seen this, it must be a livein.
835f4a2713aSLionel Sambuc RegPressure[RCId] += RCCost;
836f4a2713aSLionel Sambuc else if (!isNew && isKill)
837f4a2713aSLionel Sambuc RegPressure[RCId] -= RCCost;
838f4a2713aSLionel Sambuc }
839f4a2713aSLionel Sambuc }
840f4a2713aSLionel Sambuc }
841f4a2713aSLionel Sambuc }
842f4a2713aSLionel Sambuc
843f4a2713aSLionel Sambuc /// UpdateRegPressure - Update estimate of register pressure after the
844f4a2713aSLionel Sambuc /// specified instruction.
UpdateRegPressure(const MachineInstr * MI)845f4a2713aSLionel Sambuc void MachineLICM::UpdateRegPressure(const MachineInstr *MI) {
846f4a2713aSLionel Sambuc if (MI->isImplicitDef())
847f4a2713aSLionel Sambuc return;
848f4a2713aSLionel Sambuc
849f4a2713aSLionel Sambuc SmallVector<unsigned, 4> Defs;
850f4a2713aSLionel Sambuc for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) {
851f4a2713aSLionel Sambuc const MachineOperand &MO = MI->getOperand(i);
852f4a2713aSLionel Sambuc if (!MO.isReg() || MO.isImplicit())
853f4a2713aSLionel Sambuc continue;
854f4a2713aSLionel Sambuc unsigned Reg = MO.getReg();
855f4a2713aSLionel Sambuc if (!TargetRegisterInfo::isVirtualRegister(Reg))
856f4a2713aSLionel Sambuc continue;
857f4a2713aSLionel Sambuc
858*0a6a1f1dSLionel Sambuc bool isNew = RegSeen.insert(Reg).second;
859f4a2713aSLionel Sambuc if (MO.isDef())
860f4a2713aSLionel Sambuc Defs.push_back(Reg);
861f4a2713aSLionel Sambuc else if (!isNew && isOperandKill(MO, MRI)) {
862f4a2713aSLionel Sambuc unsigned RCId, RCCost;
863f4a2713aSLionel Sambuc getRegisterClassIDAndCost(MI, Reg, i, RCId, RCCost);
864f4a2713aSLionel Sambuc if (RCCost > RegPressure[RCId])
865f4a2713aSLionel Sambuc RegPressure[RCId] = 0;
866f4a2713aSLionel Sambuc else
867f4a2713aSLionel Sambuc RegPressure[RCId] -= RCCost;
868f4a2713aSLionel Sambuc }
869f4a2713aSLionel Sambuc }
870f4a2713aSLionel Sambuc
871f4a2713aSLionel Sambuc unsigned Idx = 0;
872f4a2713aSLionel Sambuc while (!Defs.empty()) {
873f4a2713aSLionel Sambuc unsigned Reg = Defs.pop_back_val();
874f4a2713aSLionel Sambuc unsigned RCId, RCCost;
875f4a2713aSLionel Sambuc getRegisterClassIDAndCost(MI, Reg, Idx, RCId, RCCost);
876f4a2713aSLionel Sambuc RegPressure[RCId] += RCCost;
877f4a2713aSLionel Sambuc ++Idx;
878f4a2713aSLionel Sambuc }
879f4a2713aSLionel Sambuc }
880f4a2713aSLionel Sambuc
881f4a2713aSLionel Sambuc /// isLoadFromGOTOrConstantPool - Return true if this machine instruction
882f4a2713aSLionel Sambuc /// loads from global offset table or constant pool.
isLoadFromGOTOrConstantPool(MachineInstr & MI)883f4a2713aSLionel Sambuc static bool isLoadFromGOTOrConstantPool(MachineInstr &MI) {
884f4a2713aSLionel Sambuc assert (MI.mayLoad() && "Expected MI that loads!");
885f4a2713aSLionel Sambuc for (MachineInstr::mmo_iterator I = MI.memoperands_begin(),
886f4a2713aSLionel Sambuc E = MI.memoperands_end(); I != E; ++I) {
887*0a6a1f1dSLionel Sambuc if (const PseudoSourceValue *PSV = (*I)->getPseudoValue()) {
888f4a2713aSLionel Sambuc if (PSV == PSV->getGOT() || PSV == PSV->getConstantPool())
889f4a2713aSLionel Sambuc return true;
890f4a2713aSLionel Sambuc }
891f4a2713aSLionel Sambuc }
892f4a2713aSLionel Sambuc return false;
893f4a2713aSLionel Sambuc }
894f4a2713aSLionel Sambuc
895f4a2713aSLionel Sambuc /// IsLICMCandidate - Returns true if the instruction may be a suitable
896f4a2713aSLionel Sambuc /// candidate for LICM. e.g. If the instruction is a call, then it's obviously
897f4a2713aSLionel Sambuc /// not safe to hoist it.
IsLICMCandidate(MachineInstr & I)898f4a2713aSLionel Sambuc bool MachineLICM::IsLICMCandidate(MachineInstr &I) {
899f4a2713aSLionel Sambuc // Check if it's safe to move the instruction.
900f4a2713aSLionel Sambuc bool DontMoveAcrossStore = true;
901f4a2713aSLionel Sambuc if (!I.isSafeToMove(TII, AA, DontMoveAcrossStore))
902f4a2713aSLionel Sambuc return false;
903f4a2713aSLionel Sambuc
904f4a2713aSLionel Sambuc // If it is load then check if it is guaranteed to execute by making sure that
905f4a2713aSLionel Sambuc // it dominates all exiting blocks. If it doesn't, then there is a path out of
906f4a2713aSLionel Sambuc // the loop which does not execute this load, so we can't hoist it. Loads
907f4a2713aSLionel Sambuc // from constant memory are not safe to speculate all the time, for example
908f4a2713aSLionel Sambuc // indexed load from a jump table.
909f4a2713aSLionel Sambuc // Stores and side effects are already checked by isSafeToMove.
910f4a2713aSLionel Sambuc if (I.mayLoad() && !isLoadFromGOTOrConstantPool(I) &&
911f4a2713aSLionel Sambuc !IsGuaranteedToExecute(I.getParent()))
912f4a2713aSLionel Sambuc return false;
913f4a2713aSLionel Sambuc
914f4a2713aSLionel Sambuc return true;
915f4a2713aSLionel Sambuc }
916f4a2713aSLionel Sambuc
917f4a2713aSLionel Sambuc /// IsLoopInvariantInst - Returns true if the instruction is loop
918f4a2713aSLionel Sambuc /// invariant. I.e., all virtual register operands are defined outside of the
919f4a2713aSLionel Sambuc /// loop, physical registers aren't accessed explicitly, and there are no side
920f4a2713aSLionel Sambuc /// effects that aren't captured by the operands or other flags.
921f4a2713aSLionel Sambuc ///
IsLoopInvariantInst(MachineInstr & I)922f4a2713aSLionel Sambuc bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) {
923f4a2713aSLionel Sambuc if (!IsLICMCandidate(I))
924f4a2713aSLionel Sambuc return false;
925f4a2713aSLionel Sambuc
926f4a2713aSLionel Sambuc // The instruction is loop invariant if all of its operands are.
927f4a2713aSLionel Sambuc for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
928f4a2713aSLionel Sambuc const MachineOperand &MO = I.getOperand(i);
929f4a2713aSLionel Sambuc
930f4a2713aSLionel Sambuc if (!MO.isReg())
931f4a2713aSLionel Sambuc continue;
932f4a2713aSLionel Sambuc
933f4a2713aSLionel Sambuc unsigned Reg = MO.getReg();
934f4a2713aSLionel Sambuc if (Reg == 0) continue;
935f4a2713aSLionel Sambuc
936f4a2713aSLionel Sambuc // Don't hoist an instruction that uses or defines a physical register.
937f4a2713aSLionel Sambuc if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
938f4a2713aSLionel Sambuc if (MO.isUse()) {
939f4a2713aSLionel Sambuc // If the physreg has no defs anywhere, it's just an ambient register
940f4a2713aSLionel Sambuc // and we can freely move its uses. Alternatively, if it's allocatable,
941f4a2713aSLionel Sambuc // it could get allocated to something with a def during allocation.
942f4a2713aSLionel Sambuc if (!MRI->isConstantPhysReg(Reg, *I.getParent()->getParent()))
943f4a2713aSLionel Sambuc return false;
944f4a2713aSLionel Sambuc // Otherwise it's safe to move.
945f4a2713aSLionel Sambuc continue;
946f4a2713aSLionel Sambuc } else if (!MO.isDead()) {
947f4a2713aSLionel Sambuc // A def that isn't dead. We can't move it.
948f4a2713aSLionel Sambuc return false;
949f4a2713aSLionel Sambuc } else if (CurLoop->getHeader()->isLiveIn(Reg)) {
950f4a2713aSLionel Sambuc // If the reg is live into the loop, we can't hoist an instruction
951f4a2713aSLionel Sambuc // which would clobber it.
952f4a2713aSLionel Sambuc return false;
953f4a2713aSLionel Sambuc }
954f4a2713aSLionel Sambuc }
955f4a2713aSLionel Sambuc
956f4a2713aSLionel Sambuc if (!MO.isUse())
957f4a2713aSLionel Sambuc continue;
958f4a2713aSLionel Sambuc
959f4a2713aSLionel Sambuc assert(MRI->getVRegDef(Reg) &&
960f4a2713aSLionel Sambuc "Machine instr not mapped for this vreg?!");
961f4a2713aSLionel Sambuc
962f4a2713aSLionel Sambuc // If the loop contains the definition of an operand, then the instruction
963f4a2713aSLionel Sambuc // isn't loop invariant.
964f4a2713aSLionel Sambuc if (CurLoop->contains(MRI->getVRegDef(Reg)))
965f4a2713aSLionel Sambuc return false;
966f4a2713aSLionel Sambuc }
967f4a2713aSLionel Sambuc
968f4a2713aSLionel Sambuc // If we got this far, the instruction is loop invariant!
969f4a2713aSLionel Sambuc return true;
970f4a2713aSLionel Sambuc }
971f4a2713aSLionel Sambuc
972f4a2713aSLionel Sambuc
973f4a2713aSLionel Sambuc /// HasLoopPHIUse - Return true if the specified instruction is used by a
974f4a2713aSLionel Sambuc /// phi node and hoisting it could cause a copy to be inserted.
HasLoopPHIUse(const MachineInstr * MI) const975f4a2713aSLionel Sambuc bool MachineLICM::HasLoopPHIUse(const MachineInstr *MI) const {
976f4a2713aSLionel Sambuc SmallVector<const MachineInstr*, 8> Work(1, MI);
977f4a2713aSLionel Sambuc do {
978f4a2713aSLionel Sambuc MI = Work.pop_back_val();
979f4a2713aSLionel Sambuc for (ConstMIOperands MO(MI); MO.isValid(); ++MO) {
980f4a2713aSLionel Sambuc if (!MO->isReg() || !MO->isDef())
981f4a2713aSLionel Sambuc continue;
982f4a2713aSLionel Sambuc unsigned Reg = MO->getReg();
983f4a2713aSLionel Sambuc if (!TargetRegisterInfo::isVirtualRegister(Reg))
984f4a2713aSLionel Sambuc continue;
985*0a6a1f1dSLionel Sambuc for (MachineInstr &UseMI : MRI->use_instructions(Reg)) {
986f4a2713aSLionel Sambuc // A PHI may cause a copy to be inserted.
987*0a6a1f1dSLionel Sambuc if (UseMI.isPHI()) {
988f4a2713aSLionel Sambuc // A PHI inside the loop causes a copy because the live range of Reg is
989f4a2713aSLionel Sambuc // extended across the PHI.
990*0a6a1f1dSLionel Sambuc if (CurLoop->contains(&UseMI))
991f4a2713aSLionel Sambuc return true;
992f4a2713aSLionel Sambuc // A PHI in an exit block can cause a copy to be inserted if the PHI
993f4a2713aSLionel Sambuc // has multiple predecessors in the loop with different values.
994f4a2713aSLionel Sambuc // For now, approximate by rejecting all exit blocks.
995*0a6a1f1dSLionel Sambuc if (isExitBlock(UseMI.getParent()))
996f4a2713aSLionel Sambuc return true;
997f4a2713aSLionel Sambuc continue;
998f4a2713aSLionel Sambuc }
999f4a2713aSLionel Sambuc // Look past copies as well.
1000*0a6a1f1dSLionel Sambuc if (UseMI.isCopy() && CurLoop->contains(&UseMI))
1001*0a6a1f1dSLionel Sambuc Work.push_back(&UseMI);
1002f4a2713aSLionel Sambuc }
1003f4a2713aSLionel Sambuc }
1004f4a2713aSLionel Sambuc } while (!Work.empty());
1005f4a2713aSLionel Sambuc return false;
1006f4a2713aSLionel Sambuc }
1007f4a2713aSLionel Sambuc
1008f4a2713aSLionel Sambuc /// HasHighOperandLatency - Compute operand latency between a def of 'Reg'
1009f4a2713aSLionel Sambuc /// and an use in the current loop, return true if the target considered
1010f4a2713aSLionel Sambuc /// it 'high'.
HasHighOperandLatency(MachineInstr & MI,unsigned DefIdx,unsigned Reg) const1011f4a2713aSLionel Sambuc bool MachineLICM::HasHighOperandLatency(MachineInstr &MI,
1012f4a2713aSLionel Sambuc unsigned DefIdx, unsigned Reg) const {
1013f4a2713aSLionel Sambuc if (!InstrItins || InstrItins->isEmpty() || MRI->use_nodbg_empty(Reg))
1014f4a2713aSLionel Sambuc return false;
1015f4a2713aSLionel Sambuc
1016*0a6a1f1dSLionel Sambuc for (MachineInstr &UseMI : MRI->use_nodbg_instructions(Reg)) {
1017*0a6a1f1dSLionel Sambuc if (UseMI.isCopyLike())
1018f4a2713aSLionel Sambuc continue;
1019*0a6a1f1dSLionel Sambuc if (!CurLoop->contains(UseMI.getParent()))
1020f4a2713aSLionel Sambuc continue;
1021*0a6a1f1dSLionel Sambuc for (unsigned i = 0, e = UseMI.getNumOperands(); i != e; ++i) {
1022*0a6a1f1dSLionel Sambuc const MachineOperand &MO = UseMI.getOperand(i);
1023f4a2713aSLionel Sambuc if (!MO.isReg() || !MO.isUse())
1024f4a2713aSLionel Sambuc continue;
1025f4a2713aSLionel Sambuc unsigned MOReg = MO.getReg();
1026f4a2713aSLionel Sambuc if (MOReg != Reg)
1027f4a2713aSLionel Sambuc continue;
1028f4a2713aSLionel Sambuc
1029*0a6a1f1dSLionel Sambuc if (TII->hasHighOperandLatency(InstrItins, MRI, &MI, DefIdx, &UseMI, i))
1030f4a2713aSLionel Sambuc return true;
1031f4a2713aSLionel Sambuc }
1032f4a2713aSLionel Sambuc
1033f4a2713aSLionel Sambuc // Only look at the first in loop use.
1034f4a2713aSLionel Sambuc break;
1035f4a2713aSLionel Sambuc }
1036f4a2713aSLionel Sambuc
1037f4a2713aSLionel Sambuc return false;
1038f4a2713aSLionel Sambuc }
1039f4a2713aSLionel Sambuc
1040f4a2713aSLionel Sambuc /// IsCheapInstruction - Return true if the instruction is marked "cheap" or
1041f4a2713aSLionel Sambuc /// the operand latency between its def and a use is one or less.
IsCheapInstruction(MachineInstr & MI) const1042f4a2713aSLionel Sambuc bool MachineLICM::IsCheapInstruction(MachineInstr &MI) const {
1043*0a6a1f1dSLionel Sambuc if (TII->isAsCheapAsAMove(&MI) || MI.isCopyLike())
1044f4a2713aSLionel Sambuc return true;
1045f4a2713aSLionel Sambuc if (!InstrItins || InstrItins->isEmpty())
1046f4a2713aSLionel Sambuc return false;
1047f4a2713aSLionel Sambuc
1048f4a2713aSLionel Sambuc bool isCheap = false;
1049f4a2713aSLionel Sambuc unsigned NumDefs = MI.getDesc().getNumDefs();
1050f4a2713aSLionel Sambuc for (unsigned i = 0, e = MI.getNumOperands(); NumDefs && i != e; ++i) {
1051f4a2713aSLionel Sambuc MachineOperand &DefMO = MI.getOperand(i);
1052f4a2713aSLionel Sambuc if (!DefMO.isReg() || !DefMO.isDef())
1053f4a2713aSLionel Sambuc continue;
1054f4a2713aSLionel Sambuc --NumDefs;
1055f4a2713aSLionel Sambuc unsigned Reg = DefMO.getReg();
1056f4a2713aSLionel Sambuc if (TargetRegisterInfo::isPhysicalRegister(Reg))
1057f4a2713aSLionel Sambuc continue;
1058f4a2713aSLionel Sambuc
1059f4a2713aSLionel Sambuc if (!TII->hasLowDefLatency(InstrItins, &MI, i))
1060f4a2713aSLionel Sambuc return false;
1061f4a2713aSLionel Sambuc isCheap = true;
1062f4a2713aSLionel Sambuc }
1063f4a2713aSLionel Sambuc
1064f4a2713aSLionel Sambuc return isCheap;
1065f4a2713aSLionel Sambuc }
1066f4a2713aSLionel Sambuc
1067f4a2713aSLionel Sambuc /// CanCauseHighRegPressure - Visit BBs from header to current BB, check
1068f4a2713aSLionel Sambuc /// if hoisting an instruction of the given cost matrix can cause high
1069f4a2713aSLionel Sambuc /// register pressure.
CanCauseHighRegPressure(DenseMap<unsigned,int> & Cost,bool CheapInstr)1070f4a2713aSLionel Sambuc bool MachineLICM::CanCauseHighRegPressure(DenseMap<unsigned, int> &Cost,
1071f4a2713aSLionel Sambuc bool CheapInstr) {
1072f4a2713aSLionel Sambuc for (DenseMap<unsigned, int>::iterator CI = Cost.begin(), CE = Cost.end();
1073f4a2713aSLionel Sambuc CI != CE; ++CI) {
1074f4a2713aSLionel Sambuc if (CI->second <= 0)
1075f4a2713aSLionel Sambuc continue;
1076f4a2713aSLionel Sambuc
1077f4a2713aSLionel Sambuc unsigned RCId = CI->first;
1078f4a2713aSLionel Sambuc unsigned Limit = RegLimit[RCId];
1079f4a2713aSLionel Sambuc int Cost = CI->second;
1080f4a2713aSLionel Sambuc
1081f4a2713aSLionel Sambuc // Don't hoist cheap instructions if they would increase register pressure,
1082f4a2713aSLionel Sambuc // even if we're under the limit.
1083*0a6a1f1dSLionel Sambuc if (CheapInstr && !HoistCheapInsts)
1084f4a2713aSLionel Sambuc return true;
1085f4a2713aSLionel Sambuc
1086f4a2713aSLionel Sambuc for (unsigned i = BackTrace.size(); i != 0; --i) {
1087f4a2713aSLionel Sambuc SmallVectorImpl<unsigned> &RP = BackTrace[i-1];
1088f4a2713aSLionel Sambuc if (RP[RCId] + Cost >= Limit)
1089f4a2713aSLionel Sambuc return true;
1090f4a2713aSLionel Sambuc }
1091f4a2713aSLionel Sambuc }
1092f4a2713aSLionel Sambuc
1093f4a2713aSLionel Sambuc return false;
1094f4a2713aSLionel Sambuc }
1095f4a2713aSLionel Sambuc
1096f4a2713aSLionel Sambuc /// UpdateBackTraceRegPressure - Traverse the back trace from header to the
1097f4a2713aSLionel Sambuc /// current block and update their register pressures to reflect the effect
1098f4a2713aSLionel Sambuc /// of hoisting MI from the current block to the preheader.
UpdateBackTraceRegPressure(const MachineInstr * MI)1099f4a2713aSLionel Sambuc void MachineLICM::UpdateBackTraceRegPressure(const MachineInstr *MI) {
1100f4a2713aSLionel Sambuc if (MI->isImplicitDef())
1101f4a2713aSLionel Sambuc return;
1102f4a2713aSLionel Sambuc
1103f4a2713aSLionel Sambuc // First compute the 'cost' of the instruction, i.e. its contribution
1104f4a2713aSLionel Sambuc // to register pressure.
1105f4a2713aSLionel Sambuc DenseMap<unsigned, int> Cost;
1106f4a2713aSLionel Sambuc for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) {
1107f4a2713aSLionel Sambuc const MachineOperand &MO = MI->getOperand(i);
1108f4a2713aSLionel Sambuc if (!MO.isReg() || MO.isImplicit())
1109f4a2713aSLionel Sambuc continue;
1110f4a2713aSLionel Sambuc unsigned Reg = MO.getReg();
1111f4a2713aSLionel Sambuc if (!TargetRegisterInfo::isVirtualRegister(Reg))
1112f4a2713aSLionel Sambuc continue;
1113f4a2713aSLionel Sambuc
1114f4a2713aSLionel Sambuc unsigned RCId, RCCost;
1115f4a2713aSLionel Sambuc getRegisterClassIDAndCost(MI, Reg, i, RCId, RCCost);
1116f4a2713aSLionel Sambuc if (MO.isDef()) {
1117f4a2713aSLionel Sambuc DenseMap<unsigned, int>::iterator CI = Cost.find(RCId);
1118f4a2713aSLionel Sambuc if (CI != Cost.end())
1119f4a2713aSLionel Sambuc CI->second += RCCost;
1120f4a2713aSLionel Sambuc else
1121f4a2713aSLionel Sambuc Cost.insert(std::make_pair(RCId, RCCost));
1122f4a2713aSLionel Sambuc } else if (isOperandKill(MO, MRI)) {
1123f4a2713aSLionel Sambuc DenseMap<unsigned, int>::iterator CI = Cost.find(RCId);
1124f4a2713aSLionel Sambuc if (CI != Cost.end())
1125f4a2713aSLionel Sambuc CI->second -= RCCost;
1126f4a2713aSLionel Sambuc else
1127f4a2713aSLionel Sambuc Cost.insert(std::make_pair(RCId, -RCCost));
1128f4a2713aSLionel Sambuc }
1129f4a2713aSLionel Sambuc }
1130f4a2713aSLionel Sambuc
1131f4a2713aSLionel Sambuc // Update register pressure of blocks from loop header to current block.
1132f4a2713aSLionel Sambuc for (unsigned i = 0, e = BackTrace.size(); i != e; ++i) {
1133f4a2713aSLionel Sambuc SmallVectorImpl<unsigned> &RP = BackTrace[i];
1134f4a2713aSLionel Sambuc for (DenseMap<unsigned, int>::iterator CI = Cost.begin(), CE = Cost.end();
1135f4a2713aSLionel Sambuc CI != CE; ++CI) {
1136f4a2713aSLionel Sambuc unsigned RCId = CI->first;
1137f4a2713aSLionel Sambuc RP[RCId] += CI->second;
1138f4a2713aSLionel Sambuc }
1139f4a2713aSLionel Sambuc }
1140f4a2713aSLionel Sambuc }
1141f4a2713aSLionel Sambuc
1142f4a2713aSLionel Sambuc /// IsProfitableToHoist - Return true if it is potentially profitable to hoist
1143f4a2713aSLionel Sambuc /// the given loop invariant.
IsProfitableToHoist(MachineInstr & MI)1144f4a2713aSLionel Sambuc bool MachineLICM::IsProfitableToHoist(MachineInstr &MI) {
1145f4a2713aSLionel Sambuc if (MI.isImplicitDef())
1146f4a2713aSLionel Sambuc return true;
1147f4a2713aSLionel Sambuc
1148f4a2713aSLionel Sambuc // Besides removing computation from the loop, hoisting an instruction has
1149f4a2713aSLionel Sambuc // these effects:
1150f4a2713aSLionel Sambuc //
1151f4a2713aSLionel Sambuc // - The value defined by the instruction becomes live across the entire
1152f4a2713aSLionel Sambuc // loop. This increases register pressure in the loop.
1153f4a2713aSLionel Sambuc //
1154f4a2713aSLionel Sambuc // - If the value is used by a PHI in the loop, a copy will be required for
1155f4a2713aSLionel Sambuc // lowering the PHI after extending the live range.
1156f4a2713aSLionel Sambuc //
1157f4a2713aSLionel Sambuc // - When hoisting the last use of a value in the loop, that value no longer
1158f4a2713aSLionel Sambuc // needs to be live in the loop. This lowers register pressure in the loop.
1159f4a2713aSLionel Sambuc
1160f4a2713aSLionel Sambuc bool CheapInstr = IsCheapInstruction(MI);
1161f4a2713aSLionel Sambuc bool CreatesCopy = HasLoopPHIUse(&MI);
1162f4a2713aSLionel Sambuc
1163f4a2713aSLionel Sambuc // Don't hoist a cheap instruction if it would create a copy in the loop.
1164f4a2713aSLionel Sambuc if (CheapInstr && CreatesCopy) {
1165f4a2713aSLionel Sambuc DEBUG(dbgs() << "Won't hoist cheap instr with loop PHI use: " << MI);
1166f4a2713aSLionel Sambuc return false;
1167f4a2713aSLionel Sambuc }
1168f4a2713aSLionel Sambuc
1169f4a2713aSLionel Sambuc // Rematerializable instructions should always be hoisted since the register
1170f4a2713aSLionel Sambuc // allocator can just pull them down again when needed.
1171f4a2713aSLionel Sambuc if (TII->isTriviallyReMaterializable(&MI, AA))
1172f4a2713aSLionel Sambuc return true;
1173f4a2713aSLionel Sambuc
1174f4a2713aSLionel Sambuc // Estimate register pressure to determine whether to LICM the instruction.
1175f4a2713aSLionel Sambuc // In low register pressure situation, we can be more aggressive about
1176f4a2713aSLionel Sambuc // hoisting. Also, favors hoisting long latency instructions even in
1177f4a2713aSLionel Sambuc // moderately high pressure situation.
1178f4a2713aSLionel Sambuc // Cheap instructions will only be hoisted if they don't increase register
1179f4a2713aSLionel Sambuc // pressure at all.
1180f4a2713aSLionel Sambuc // FIXME: If there are long latency loop-invariant instructions inside the
1181f4a2713aSLionel Sambuc // loop at this point, why didn't the optimizer's LICM hoist them?
1182f4a2713aSLionel Sambuc DenseMap<unsigned, int> Cost;
1183f4a2713aSLionel Sambuc for (unsigned i = 0, e = MI.getDesc().getNumOperands(); i != e; ++i) {
1184f4a2713aSLionel Sambuc const MachineOperand &MO = MI.getOperand(i);
1185f4a2713aSLionel Sambuc if (!MO.isReg() || MO.isImplicit())
1186f4a2713aSLionel Sambuc continue;
1187f4a2713aSLionel Sambuc unsigned Reg = MO.getReg();
1188f4a2713aSLionel Sambuc if (!TargetRegisterInfo::isVirtualRegister(Reg))
1189f4a2713aSLionel Sambuc continue;
1190f4a2713aSLionel Sambuc
1191f4a2713aSLionel Sambuc unsigned RCId, RCCost;
1192f4a2713aSLionel Sambuc getRegisterClassIDAndCost(&MI, Reg, i, RCId, RCCost);
1193f4a2713aSLionel Sambuc if (MO.isDef()) {
1194f4a2713aSLionel Sambuc if (HasHighOperandLatency(MI, i, Reg)) {
1195f4a2713aSLionel Sambuc DEBUG(dbgs() << "Hoist High Latency: " << MI);
1196f4a2713aSLionel Sambuc ++NumHighLatency;
1197f4a2713aSLionel Sambuc return true;
1198f4a2713aSLionel Sambuc }
1199f4a2713aSLionel Sambuc Cost[RCId] += RCCost;
1200f4a2713aSLionel Sambuc } else if (isOperandKill(MO, MRI)) {
1201f4a2713aSLionel Sambuc // Is a virtual register use is a kill, hoisting it out of the loop
1202f4a2713aSLionel Sambuc // may actually reduce register pressure or be register pressure
1203f4a2713aSLionel Sambuc // neutral.
1204f4a2713aSLionel Sambuc Cost[RCId] -= RCCost;
1205f4a2713aSLionel Sambuc }
1206f4a2713aSLionel Sambuc }
1207f4a2713aSLionel Sambuc
1208f4a2713aSLionel Sambuc // Visit BBs from header to current BB, if hoisting this doesn't cause
1209f4a2713aSLionel Sambuc // high register pressure, then it's safe to proceed.
1210f4a2713aSLionel Sambuc if (!CanCauseHighRegPressure(Cost, CheapInstr)) {
1211f4a2713aSLionel Sambuc DEBUG(dbgs() << "Hoist non-reg-pressure: " << MI);
1212f4a2713aSLionel Sambuc ++NumLowRP;
1213f4a2713aSLionel Sambuc return true;
1214f4a2713aSLionel Sambuc }
1215f4a2713aSLionel Sambuc
1216f4a2713aSLionel Sambuc // Don't risk increasing register pressure if it would create copies.
1217f4a2713aSLionel Sambuc if (CreatesCopy) {
1218f4a2713aSLionel Sambuc DEBUG(dbgs() << "Won't hoist instr with loop PHI use: " << MI);
1219f4a2713aSLionel Sambuc return false;
1220f4a2713aSLionel Sambuc }
1221f4a2713aSLionel Sambuc
1222f4a2713aSLionel Sambuc // Do not "speculate" in high register pressure situation. If an
1223f4a2713aSLionel Sambuc // instruction is not guaranteed to be executed in the loop, it's best to be
1224f4a2713aSLionel Sambuc // conservative.
1225f4a2713aSLionel Sambuc if (AvoidSpeculation &&
1226f4a2713aSLionel Sambuc (!IsGuaranteedToExecute(MI.getParent()) && !MayCSE(&MI))) {
1227f4a2713aSLionel Sambuc DEBUG(dbgs() << "Won't speculate: " << MI);
1228f4a2713aSLionel Sambuc return false;
1229f4a2713aSLionel Sambuc }
1230f4a2713aSLionel Sambuc
1231f4a2713aSLionel Sambuc // High register pressure situation, only hoist if the instruction is going
1232f4a2713aSLionel Sambuc // to be remat'ed.
1233f4a2713aSLionel Sambuc if (!TII->isTriviallyReMaterializable(&MI, AA) &&
1234f4a2713aSLionel Sambuc !MI.isInvariantLoad(AA)) {
1235f4a2713aSLionel Sambuc DEBUG(dbgs() << "Can't remat / high reg-pressure: " << MI);
1236f4a2713aSLionel Sambuc return false;
1237f4a2713aSLionel Sambuc }
1238f4a2713aSLionel Sambuc
1239f4a2713aSLionel Sambuc return true;
1240f4a2713aSLionel Sambuc }
1241f4a2713aSLionel Sambuc
ExtractHoistableLoad(MachineInstr * MI)1242f4a2713aSLionel Sambuc MachineInstr *MachineLICM::ExtractHoistableLoad(MachineInstr *MI) {
1243f4a2713aSLionel Sambuc // Don't unfold simple loads.
1244f4a2713aSLionel Sambuc if (MI->canFoldAsLoad())
1245*0a6a1f1dSLionel Sambuc return nullptr;
1246f4a2713aSLionel Sambuc
1247f4a2713aSLionel Sambuc // If not, we may be able to unfold a load and hoist that.
1248f4a2713aSLionel Sambuc // First test whether the instruction is loading from an amenable
1249f4a2713aSLionel Sambuc // memory location.
1250f4a2713aSLionel Sambuc if (!MI->isInvariantLoad(AA))
1251*0a6a1f1dSLionel Sambuc return nullptr;
1252f4a2713aSLionel Sambuc
1253f4a2713aSLionel Sambuc // Next determine the register class for a temporary register.
1254f4a2713aSLionel Sambuc unsigned LoadRegIndex;
1255f4a2713aSLionel Sambuc unsigned NewOpc =
1256f4a2713aSLionel Sambuc TII->getOpcodeAfterMemoryUnfold(MI->getOpcode(),
1257f4a2713aSLionel Sambuc /*UnfoldLoad=*/true,
1258f4a2713aSLionel Sambuc /*UnfoldStore=*/false,
1259f4a2713aSLionel Sambuc &LoadRegIndex);
1260*0a6a1f1dSLionel Sambuc if (NewOpc == 0) return nullptr;
1261f4a2713aSLionel Sambuc const MCInstrDesc &MID = TII->get(NewOpc);
1262*0a6a1f1dSLionel Sambuc if (MID.getNumDefs() != 1) return nullptr;
1263f4a2713aSLionel Sambuc MachineFunction &MF = *MI->getParent()->getParent();
1264f4a2713aSLionel Sambuc const TargetRegisterClass *RC = TII->getRegClass(MID, LoadRegIndex, TRI, MF);
1265f4a2713aSLionel Sambuc // Ok, we're unfolding. Create a temporary register and do the unfold.
1266f4a2713aSLionel Sambuc unsigned Reg = MRI->createVirtualRegister(RC);
1267f4a2713aSLionel Sambuc
1268f4a2713aSLionel Sambuc SmallVector<MachineInstr *, 2> NewMIs;
1269f4a2713aSLionel Sambuc bool Success =
1270f4a2713aSLionel Sambuc TII->unfoldMemoryOperand(MF, MI, Reg,
1271f4a2713aSLionel Sambuc /*UnfoldLoad=*/true, /*UnfoldStore=*/false,
1272f4a2713aSLionel Sambuc NewMIs);
1273f4a2713aSLionel Sambuc (void)Success;
1274f4a2713aSLionel Sambuc assert(Success &&
1275f4a2713aSLionel Sambuc "unfoldMemoryOperand failed when getOpcodeAfterMemoryUnfold "
1276f4a2713aSLionel Sambuc "succeeded!");
1277f4a2713aSLionel Sambuc assert(NewMIs.size() == 2 &&
1278f4a2713aSLionel Sambuc "Unfolded a load into multiple instructions!");
1279f4a2713aSLionel Sambuc MachineBasicBlock *MBB = MI->getParent();
1280f4a2713aSLionel Sambuc MachineBasicBlock::iterator Pos = MI;
1281f4a2713aSLionel Sambuc MBB->insert(Pos, NewMIs[0]);
1282f4a2713aSLionel Sambuc MBB->insert(Pos, NewMIs[1]);
1283f4a2713aSLionel Sambuc // If unfolding produced a load that wasn't loop-invariant or profitable to
1284f4a2713aSLionel Sambuc // hoist, discard the new instructions and bail.
1285f4a2713aSLionel Sambuc if (!IsLoopInvariantInst(*NewMIs[0]) || !IsProfitableToHoist(*NewMIs[0])) {
1286f4a2713aSLionel Sambuc NewMIs[0]->eraseFromParent();
1287f4a2713aSLionel Sambuc NewMIs[1]->eraseFromParent();
1288*0a6a1f1dSLionel Sambuc return nullptr;
1289f4a2713aSLionel Sambuc }
1290f4a2713aSLionel Sambuc
1291f4a2713aSLionel Sambuc // Update register pressure for the unfolded instruction.
1292f4a2713aSLionel Sambuc UpdateRegPressure(NewMIs[1]);
1293f4a2713aSLionel Sambuc
1294f4a2713aSLionel Sambuc // Otherwise we successfully unfolded a load that we can hoist.
1295f4a2713aSLionel Sambuc MI->eraseFromParent();
1296f4a2713aSLionel Sambuc return NewMIs[0];
1297f4a2713aSLionel Sambuc }
1298f4a2713aSLionel Sambuc
InitCSEMap(MachineBasicBlock * BB)1299f4a2713aSLionel Sambuc void MachineLICM::InitCSEMap(MachineBasicBlock *BB) {
1300f4a2713aSLionel Sambuc for (MachineBasicBlock::iterator I = BB->begin(),E = BB->end(); I != E; ++I) {
1301f4a2713aSLionel Sambuc const MachineInstr *MI = &*I;
1302f4a2713aSLionel Sambuc unsigned Opcode = MI->getOpcode();
1303*0a6a1f1dSLionel Sambuc CSEMap[Opcode].push_back(MI);
1304f4a2713aSLionel Sambuc }
1305f4a2713aSLionel Sambuc }
1306f4a2713aSLionel Sambuc
1307f4a2713aSLionel Sambuc const MachineInstr*
LookForDuplicate(const MachineInstr * MI,std::vector<const MachineInstr * > & PrevMIs)1308f4a2713aSLionel Sambuc MachineLICM::LookForDuplicate(const MachineInstr *MI,
1309f4a2713aSLionel Sambuc std::vector<const MachineInstr*> &PrevMIs) {
1310f4a2713aSLionel Sambuc for (unsigned i = 0, e = PrevMIs.size(); i != e; ++i) {
1311f4a2713aSLionel Sambuc const MachineInstr *PrevMI = PrevMIs[i];
1312*0a6a1f1dSLionel Sambuc if (TII->produceSameValue(MI, PrevMI, (PreRegAlloc ? MRI : nullptr)))
1313f4a2713aSLionel Sambuc return PrevMI;
1314f4a2713aSLionel Sambuc }
1315*0a6a1f1dSLionel Sambuc return nullptr;
1316f4a2713aSLionel Sambuc }
1317f4a2713aSLionel Sambuc
EliminateCSE(MachineInstr * MI,DenseMap<unsigned,std::vector<const MachineInstr * >>::iterator & CI)1318f4a2713aSLionel Sambuc bool MachineLICM::EliminateCSE(MachineInstr *MI,
1319f4a2713aSLionel Sambuc DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI) {
1320f4a2713aSLionel Sambuc // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate
1321f4a2713aSLionel Sambuc // the undef property onto uses.
1322f4a2713aSLionel Sambuc if (CI == CSEMap.end() || MI->isImplicitDef())
1323f4a2713aSLionel Sambuc return false;
1324f4a2713aSLionel Sambuc
1325f4a2713aSLionel Sambuc if (const MachineInstr *Dup = LookForDuplicate(MI, CI->second)) {
1326f4a2713aSLionel Sambuc DEBUG(dbgs() << "CSEing " << *MI << " with " << *Dup);
1327f4a2713aSLionel Sambuc
1328f4a2713aSLionel Sambuc // Replace virtual registers defined by MI by their counterparts defined
1329f4a2713aSLionel Sambuc // by Dup.
1330f4a2713aSLionel Sambuc SmallVector<unsigned, 2> Defs;
1331f4a2713aSLionel Sambuc for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1332f4a2713aSLionel Sambuc const MachineOperand &MO = MI->getOperand(i);
1333f4a2713aSLionel Sambuc
1334f4a2713aSLionel Sambuc // Physical registers may not differ here.
1335f4a2713aSLionel Sambuc assert((!MO.isReg() || MO.getReg() == 0 ||
1336f4a2713aSLionel Sambuc !TargetRegisterInfo::isPhysicalRegister(MO.getReg()) ||
1337f4a2713aSLionel Sambuc MO.getReg() == Dup->getOperand(i).getReg()) &&
1338f4a2713aSLionel Sambuc "Instructions with different phys regs are not identical!");
1339f4a2713aSLionel Sambuc
1340f4a2713aSLionel Sambuc if (MO.isReg() && MO.isDef() &&
1341f4a2713aSLionel Sambuc !TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
1342f4a2713aSLionel Sambuc Defs.push_back(i);
1343f4a2713aSLionel Sambuc }
1344f4a2713aSLionel Sambuc
1345f4a2713aSLionel Sambuc SmallVector<const TargetRegisterClass*, 2> OrigRCs;
1346f4a2713aSLionel Sambuc for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
1347f4a2713aSLionel Sambuc unsigned Idx = Defs[i];
1348f4a2713aSLionel Sambuc unsigned Reg = MI->getOperand(Idx).getReg();
1349f4a2713aSLionel Sambuc unsigned DupReg = Dup->getOperand(Idx).getReg();
1350f4a2713aSLionel Sambuc OrigRCs.push_back(MRI->getRegClass(DupReg));
1351f4a2713aSLionel Sambuc
1352f4a2713aSLionel Sambuc if (!MRI->constrainRegClass(DupReg, MRI->getRegClass(Reg))) {
1353f4a2713aSLionel Sambuc // Restore old RCs if more than one defs.
1354f4a2713aSLionel Sambuc for (unsigned j = 0; j != i; ++j)
1355f4a2713aSLionel Sambuc MRI->setRegClass(Dup->getOperand(Defs[j]).getReg(), OrigRCs[j]);
1356f4a2713aSLionel Sambuc return false;
1357f4a2713aSLionel Sambuc }
1358f4a2713aSLionel Sambuc }
1359f4a2713aSLionel Sambuc
1360f4a2713aSLionel Sambuc for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
1361f4a2713aSLionel Sambuc unsigned Idx = Defs[i];
1362f4a2713aSLionel Sambuc unsigned Reg = MI->getOperand(Idx).getReg();
1363f4a2713aSLionel Sambuc unsigned DupReg = Dup->getOperand(Idx).getReg();
1364f4a2713aSLionel Sambuc MRI->replaceRegWith(Reg, DupReg);
1365f4a2713aSLionel Sambuc MRI->clearKillFlags(DupReg);
1366f4a2713aSLionel Sambuc }
1367f4a2713aSLionel Sambuc
1368f4a2713aSLionel Sambuc MI->eraseFromParent();
1369f4a2713aSLionel Sambuc ++NumCSEed;
1370f4a2713aSLionel Sambuc return true;
1371f4a2713aSLionel Sambuc }
1372f4a2713aSLionel Sambuc return false;
1373f4a2713aSLionel Sambuc }
1374f4a2713aSLionel Sambuc
1375f4a2713aSLionel Sambuc /// MayCSE - Return true if the given instruction will be CSE'd if it's
1376f4a2713aSLionel Sambuc /// hoisted out of the loop.
MayCSE(MachineInstr * MI)1377f4a2713aSLionel Sambuc bool MachineLICM::MayCSE(MachineInstr *MI) {
1378f4a2713aSLionel Sambuc unsigned Opcode = MI->getOpcode();
1379f4a2713aSLionel Sambuc DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator
1380f4a2713aSLionel Sambuc CI = CSEMap.find(Opcode);
1381f4a2713aSLionel Sambuc // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate
1382f4a2713aSLionel Sambuc // the undef property onto uses.
1383f4a2713aSLionel Sambuc if (CI == CSEMap.end() || MI->isImplicitDef())
1384f4a2713aSLionel Sambuc return false;
1385f4a2713aSLionel Sambuc
1386*0a6a1f1dSLionel Sambuc return LookForDuplicate(MI, CI->second) != nullptr;
1387f4a2713aSLionel Sambuc }
1388f4a2713aSLionel Sambuc
1389f4a2713aSLionel Sambuc /// Hoist - When an instruction is found to use only loop invariant operands
1390f4a2713aSLionel Sambuc /// that are safe to hoist, this instruction is called to do the dirty work.
1391f4a2713aSLionel Sambuc ///
Hoist(MachineInstr * MI,MachineBasicBlock * Preheader)1392f4a2713aSLionel Sambuc bool MachineLICM::Hoist(MachineInstr *MI, MachineBasicBlock *Preheader) {
1393f4a2713aSLionel Sambuc // First check whether we should hoist this instruction.
1394f4a2713aSLionel Sambuc if (!IsLoopInvariantInst(*MI) || !IsProfitableToHoist(*MI)) {
1395f4a2713aSLionel Sambuc // If not, try unfolding a hoistable load.
1396f4a2713aSLionel Sambuc MI = ExtractHoistableLoad(MI);
1397f4a2713aSLionel Sambuc if (!MI) return false;
1398f4a2713aSLionel Sambuc }
1399f4a2713aSLionel Sambuc
1400f4a2713aSLionel Sambuc // Now move the instructions to the predecessor, inserting it before any
1401f4a2713aSLionel Sambuc // terminator instructions.
1402f4a2713aSLionel Sambuc DEBUG({
1403f4a2713aSLionel Sambuc dbgs() << "Hoisting " << *MI;
1404f4a2713aSLionel Sambuc if (Preheader->getBasicBlock())
1405f4a2713aSLionel Sambuc dbgs() << " to MachineBasicBlock "
1406f4a2713aSLionel Sambuc << Preheader->getName();
1407f4a2713aSLionel Sambuc if (MI->getParent()->getBasicBlock())
1408f4a2713aSLionel Sambuc dbgs() << " from MachineBasicBlock "
1409f4a2713aSLionel Sambuc << MI->getParent()->getName();
1410f4a2713aSLionel Sambuc dbgs() << "\n";
1411f4a2713aSLionel Sambuc });
1412f4a2713aSLionel Sambuc
1413f4a2713aSLionel Sambuc // If this is the first instruction being hoisted to the preheader,
1414f4a2713aSLionel Sambuc // initialize the CSE map with potential common expressions.
1415f4a2713aSLionel Sambuc if (FirstInLoop) {
1416f4a2713aSLionel Sambuc InitCSEMap(Preheader);
1417f4a2713aSLionel Sambuc FirstInLoop = false;
1418f4a2713aSLionel Sambuc }
1419f4a2713aSLionel Sambuc
1420f4a2713aSLionel Sambuc // Look for opportunity to CSE the hoisted instruction.
1421f4a2713aSLionel Sambuc unsigned Opcode = MI->getOpcode();
1422f4a2713aSLionel Sambuc DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator
1423f4a2713aSLionel Sambuc CI = CSEMap.find(Opcode);
1424f4a2713aSLionel Sambuc if (!EliminateCSE(MI, CI)) {
1425f4a2713aSLionel Sambuc // Otherwise, splice the instruction to the preheader.
1426f4a2713aSLionel Sambuc Preheader->splice(Preheader->getFirstTerminator(),MI->getParent(),MI);
1427f4a2713aSLionel Sambuc
1428f4a2713aSLionel Sambuc // Update register pressure for BBs from header to this block.
1429f4a2713aSLionel Sambuc UpdateBackTraceRegPressure(MI);
1430f4a2713aSLionel Sambuc
1431f4a2713aSLionel Sambuc // Clear the kill flags of any register this instruction defines,
1432f4a2713aSLionel Sambuc // since they may need to be live throughout the entire loop
1433f4a2713aSLionel Sambuc // rather than just live for part of it.
1434f4a2713aSLionel Sambuc for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1435f4a2713aSLionel Sambuc MachineOperand &MO = MI->getOperand(i);
1436f4a2713aSLionel Sambuc if (MO.isReg() && MO.isDef() && !MO.isDead())
1437f4a2713aSLionel Sambuc MRI->clearKillFlags(MO.getReg());
1438f4a2713aSLionel Sambuc }
1439f4a2713aSLionel Sambuc
1440f4a2713aSLionel Sambuc // Add to the CSE map.
1441f4a2713aSLionel Sambuc if (CI != CSEMap.end())
1442f4a2713aSLionel Sambuc CI->second.push_back(MI);
1443*0a6a1f1dSLionel Sambuc else
1444*0a6a1f1dSLionel Sambuc CSEMap[Opcode].push_back(MI);
1445f4a2713aSLionel Sambuc }
1446f4a2713aSLionel Sambuc
1447f4a2713aSLionel Sambuc ++NumHoisted;
1448f4a2713aSLionel Sambuc Changed = true;
1449f4a2713aSLionel Sambuc
1450f4a2713aSLionel Sambuc return true;
1451f4a2713aSLionel Sambuc }
1452f4a2713aSLionel Sambuc
getCurPreheader()1453f4a2713aSLionel Sambuc MachineBasicBlock *MachineLICM::getCurPreheader() {
1454f4a2713aSLionel Sambuc // Determine the block to which to hoist instructions. If we can't find a
1455f4a2713aSLionel Sambuc // suitable loop predecessor, we can't do any hoisting.
1456f4a2713aSLionel Sambuc
1457f4a2713aSLionel Sambuc // If we've tried to get a preheader and failed, don't try again.
1458f4a2713aSLionel Sambuc if (CurPreheader == reinterpret_cast<MachineBasicBlock *>(-1))
1459*0a6a1f1dSLionel Sambuc return nullptr;
1460f4a2713aSLionel Sambuc
1461f4a2713aSLionel Sambuc if (!CurPreheader) {
1462f4a2713aSLionel Sambuc CurPreheader = CurLoop->getLoopPreheader();
1463f4a2713aSLionel Sambuc if (!CurPreheader) {
1464f4a2713aSLionel Sambuc MachineBasicBlock *Pred = CurLoop->getLoopPredecessor();
1465f4a2713aSLionel Sambuc if (!Pred) {
1466f4a2713aSLionel Sambuc CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1);
1467*0a6a1f1dSLionel Sambuc return nullptr;
1468f4a2713aSLionel Sambuc }
1469f4a2713aSLionel Sambuc
1470f4a2713aSLionel Sambuc CurPreheader = Pred->SplitCriticalEdge(CurLoop->getHeader(), this);
1471f4a2713aSLionel Sambuc if (!CurPreheader) {
1472f4a2713aSLionel Sambuc CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1);
1473*0a6a1f1dSLionel Sambuc return nullptr;
1474f4a2713aSLionel Sambuc }
1475f4a2713aSLionel Sambuc }
1476f4a2713aSLionel Sambuc }
1477f4a2713aSLionel Sambuc return CurPreheader;
1478f4a2713aSLionel Sambuc }
1479