1*9880d681SAndroid Build Coastguard Worker //===-------- InlineSpiller.cpp - Insert spills and restores inline -------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // The inline spiller modifies the machine function directly instead of
11*9880d681SAndroid Build Coastguard Worker // inserting spills and restores in VirtRegMap.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard Worker #include "Spiller.h"
16*9880d681SAndroid Build Coastguard Worker #include "SplitKit.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/MapVector.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SetVector.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/TinyPtrVector.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AliasAnalysis.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/LiveIntervalAnalysis.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/LiveRangeEdit.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/LiveStackAnalysis.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineDominators.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFrameInfo.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstrBuilder.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstrBundle.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineLoopInfo.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/VirtRegMap.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DebugInfo.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
37*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
38*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
39*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
40*9880d681SAndroid Build Coastguard Worker
41*9880d681SAndroid Build Coastguard Worker using namespace llvm;
42*9880d681SAndroid Build Coastguard Worker
43*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "regalloc"
44*9880d681SAndroid Build Coastguard Worker
45*9880d681SAndroid Build Coastguard Worker STATISTIC(NumSpilledRanges, "Number of spilled live ranges");
46*9880d681SAndroid Build Coastguard Worker STATISTIC(NumSnippets, "Number of spilled snippets");
47*9880d681SAndroid Build Coastguard Worker STATISTIC(NumSpills, "Number of spills inserted");
48*9880d681SAndroid Build Coastguard Worker STATISTIC(NumSpillsRemoved, "Number of spills removed");
49*9880d681SAndroid Build Coastguard Worker STATISTIC(NumReloads, "Number of reloads inserted");
50*9880d681SAndroid Build Coastguard Worker STATISTIC(NumReloadsRemoved, "Number of reloads removed");
51*9880d681SAndroid Build Coastguard Worker STATISTIC(NumFolded, "Number of folded stack accesses");
52*9880d681SAndroid Build Coastguard Worker STATISTIC(NumFoldedLoads, "Number of folded loads");
53*9880d681SAndroid Build Coastguard Worker STATISTIC(NumRemats, "Number of rematerialized defs for spilling");
54*9880d681SAndroid Build Coastguard Worker
55*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisableHoisting("disable-spill-hoist", cl::Hidden,
56*9880d681SAndroid Build Coastguard Worker cl::desc("Disable inline spill hoisting"));
57*9880d681SAndroid Build Coastguard Worker
58*9880d681SAndroid Build Coastguard Worker namespace {
59*9880d681SAndroid Build Coastguard Worker class HoistSpillHelper : private LiveRangeEdit::Delegate {
60*9880d681SAndroid Build Coastguard Worker MachineFunction &MF;
61*9880d681SAndroid Build Coastguard Worker LiveIntervals &LIS;
62*9880d681SAndroid Build Coastguard Worker LiveStacks &LSS;
63*9880d681SAndroid Build Coastguard Worker AliasAnalysis *AA;
64*9880d681SAndroid Build Coastguard Worker MachineDominatorTree &MDT;
65*9880d681SAndroid Build Coastguard Worker MachineLoopInfo &Loops;
66*9880d681SAndroid Build Coastguard Worker VirtRegMap &VRM;
67*9880d681SAndroid Build Coastguard Worker MachineFrameInfo &MFI;
68*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo &MRI;
69*9880d681SAndroid Build Coastguard Worker const TargetInstrInfo &TII;
70*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo &TRI;
71*9880d681SAndroid Build Coastguard Worker const MachineBlockFrequencyInfo &MBFI;
72*9880d681SAndroid Build Coastguard Worker
73*9880d681SAndroid Build Coastguard Worker InsertPointAnalysis IPA;
74*9880d681SAndroid Build Coastguard Worker
75*9880d681SAndroid Build Coastguard Worker // Map from StackSlot to its original register.
76*9880d681SAndroid Build Coastguard Worker DenseMap<int, unsigned> StackSlotToReg;
77*9880d681SAndroid Build Coastguard Worker // Map from pair of (StackSlot and Original VNI) to a set of spills which
78*9880d681SAndroid Build Coastguard Worker // have the same stackslot and have equal values defined by Original VNI.
79*9880d681SAndroid Build Coastguard Worker // These spills are mergeable and are hoist candiates.
80*9880d681SAndroid Build Coastguard Worker typedef MapVector<std::pair<int, VNInfo *>, SmallPtrSet<MachineInstr *, 16>>
81*9880d681SAndroid Build Coastguard Worker MergeableSpillsMap;
82*9880d681SAndroid Build Coastguard Worker MergeableSpillsMap MergeableSpills;
83*9880d681SAndroid Build Coastguard Worker
84*9880d681SAndroid Build Coastguard Worker /// This is the map from original register to a set containing all its
85*9880d681SAndroid Build Coastguard Worker /// siblings. To hoist a spill to another BB, we need to find out a live
86*9880d681SAndroid Build Coastguard Worker /// sibling there and use it as the source of the new spill.
87*9880d681SAndroid Build Coastguard Worker DenseMap<unsigned, SmallSetVector<unsigned, 16>> Virt2SiblingsMap;
88*9880d681SAndroid Build Coastguard Worker
89*9880d681SAndroid Build Coastguard Worker bool isSpillCandBB(unsigned OrigReg, VNInfo &OrigVNI, MachineBasicBlock &BB,
90*9880d681SAndroid Build Coastguard Worker unsigned &LiveReg);
91*9880d681SAndroid Build Coastguard Worker
92*9880d681SAndroid Build Coastguard Worker void rmRedundantSpills(
93*9880d681SAndroid Build Coastguard Worker SmallPtrSet<MachineInstr *, 16> &Spills,
94*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<MachineInstr *> &SpillsToRm,
95*9880d681SAndroid Build Coastguard Worker DenseMap<MachineDomTreeNode *, MachineInstr *> &SpillBBToSpill);
96*9880d681SAndroid Build Coastguard Worker
97*9880d681SAndroid Build Coastguard Worker void getVisitOrders(
98*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *Root, SmallPtrSet<MachineInstr *, 16> &Spills,
99*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<MachineDomTreeNode *> &Orders,
100*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<MachineInstr *> &SpillsToRm,
101*9880d681SAndroid Build Coastguard Worker DenseMap<MachineDomTreeNode *, unsigned> &SpillsToKeep,
102*9880d681SAndroid Build Coastguard Worker DenseMap<MachineDomTreeNode *, MachineInstr *> &SpillBBToSpill);
103*9880d681SAndroid Build Coastguard Worker
104*9880d681SAndroid Build Coastguard Worker void runHoistSpills(unsigned OrigReg, VNInfo &OrigVNI,
105*9880d681SAndroid Build Coastguard Worker SmallPtrSet<MachineInstr *, 16> &Spills,
106*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<MachineInstr *> &SpillsToRm,
107*9880d681SAndroid Build Coastguard Worker DenseMap<MachineBasicBlock *, unsigned> &SpillsToIns);
108*9880d681SAndroid Build Coastguard Worker
109*9880d681SAndroid Build Coastguard Worker public:
HoistSpillHelper(MachineFunctionPass & pass,MachineFunction & mf,VirtRegMap & vrm)110*9880d681SAndroid Build Coastguard Worker HoistSpillHelper(MachineFunctionPass &pass, MachineFunction &mf,
111*9880d681SAndroid Build Coastguard Worker VirtRegMap &vrm)
112*9880d681SAndroid Build Coastguard Worker : MF(mf), LIS(pass.getAnalysis<LiveIntervals>()),
113*9880d681SAndroid Build Coastguard Worker LSS(pass.getAnalysis<LiveStacks>()),
114*9880d681SAndroid Build Coastguard Worker AA(&pass.getAnalysis<AAResultsWrapperPass>().getAAResults()),
115*9880d681SAndroid Build Coastguard Worker MDT(pass.getAnalysis<MachineDominatorTree>()),
116*9880d681SAndroid Build Coastguard Worker Loops(pass.getAnalysis<MachineLoopInfo>()), VRM(vrm),
117*9880d681SAndroid Build Coastguard Worker MFI(*mf.getFrameInfo()), MRI(mf.getRegInfo()),
118*9880d681SAndroid Build Coastguard Worker TII(*mf.getSubtarget().getInstrInfo()),
119*9880d681SAndroid Build Coastguard Worker TRI(*mf.getSubtarget().getRegisterInfo()),
120*9880d681SAndroid Build Coastguard Worker MBFI(pass.getAnalysis<MachineBlockFrequencyInfo>()),
121*9880d681SAndroid Build Coastguard Worker IPA(LIS, mf.getNumBlockIDs()) {}
122*9880d681SAndroid Build Coastguard Worker
123*9880d681SAndroid Build Coastguard Worker void addToMergeableSpills(MachineInstr &Spill, int StackSlot,
124*9880d681SAndroid Build Coastguard Worker unsigned Original);
125*9880d681SAndroid Build Coastguard Worker bool rmFromMergeableSpills(MachineInstr &Spill, int StackSlot);
126*9880d681SAndroid Build Coastguard Worker void hoistAllSpills();
127*9880d681SAndroid Build Coastguard Worker void LRE_DidCloneVirtReg(unsigned, unsigned) override;
128*9880d681SAndroid Build Coastguard Worker };
129*9880d681SAndroid Build Coastguard Worker
130*9880d681SAndroid Build Coastguard Worker class InlineSpiller : public Spiller {
131*9880d681SAndroid Build Coastguard Worker MachineFunction &MF;
132*9880d681SAndroid Build Coastguard Worker LiveIntervals &LIS;
133*9880d681SAndroid Build Coastguard Worker LiveStacks &LSS;
134*9880d681SAndroid Build Coastguard Worker AliasAnalysis *AA;
135*9880d681SAndroid Build Coastguard Worker MachineDominatorTree &MDT;
136*9880d681SAndroid Build Coastguard Worker MachineLoopInfo &Loops;
137*9880d681SAndroid Build Coastguard Worker VirtRegMap &VRM;
138*9880d681SAndroid Build Coastguard Worker MachineFrameInfo &MFI;
139*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo &MRI;
140*9880d681SAndroid Build Coastguard Worker const TargetInstrInfo &TII;
141*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo &TRI;
142*9880d681SAndroid Build Coastguard Worker const MachineBlockFrequencyInfo &MBFI;
143*9880d681SAndroid Build Coastguard Worker
144*9880d681SAndroid Build Coastguard Worker // Variables that are valid during spill(), but used by multiple methods.
145*9880d681SAndroid Build Coastguard Worker LiveRangeEdit *Edit;
146*9880d681SAndroid Build Coastguard Worker LiveInterval *StackInt;
147*9880d681SAndroid Build Coastguard Worker int StackSlot;
148*9880d681SAndroid Build Coastguard Worker unsigned Original;
149*9880d681SAndroid Build Coastguard Worker
150*9880d681SAndroid Build Coastguard Worker // All registers to spill to StackSlot, including the main register.
151*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned, 8> RegsToSpill;
152*9880d681SAndroid Build Coastguard Worker
153*9880d681SAndroid Build Coastguard Worker // All COPY instructions to/from snippets.
154*9880d681SAndroid Build Coastguard Worker // They are ignored since both operands refer to the same stack slot.
155*9880d681SAndroid Build Coastguard Worker SmallPtrSet<MachineInstr*, 8> SnippetCopies;
156*9880d681SAndroid Build Coastguard Worker
157*9880d681SAndroid Build Coastguard Worker // Values that failed to remat at some point.
158*9880d681SAndroid Build Coastguard Worker SmallPtrSet<VNInfo*, 8> UsedValues;
159*9880d681SAndroid Build Coastguard Worker
160*9880d681SAndroid Build Coastguard Worker // Dead defs generated during spilling.
161*9880d681SAndroid Build Coastguard Worker SmallVector<MachineInstr*, 8> DeadDefs;
162*9880d681SAndroid Build Coastguard Worker
163*9880d681SAndroid Build Coastguard Worker // Object records spills information and does the hoisting.
164*9880d681SAndroid Build Coastguard Worker HoistSpillHelper HSpiller;
165*9880d681SAndroid Build Coastguard Worker
~InlineSpiller()166*9880d681SAndroid Build Coastguard Worker ~InlineSpiller() override {}
167*9880d681SAndroid Build Coastguard Worker
168*9880d681SAndroid Build Coastguard Worker public:
InlineSpiller(MachineFunctionPass & pass,MachineFunction & mf,VirtRegMap & vrm)169*9880d681SAndroid Build Coastguard Worker InlineSpiller(MachineFunctionPass &pass, MachineFunction &mf, VirtRegMap &vrm)
170*9880d681SAndroid Build Coastguard Worker : MF(mf), LIS(pass.getAnalysis<LiveIntervals>()),
171*9880d681SAndroid Build Coastguard Worker LSS(pass.getAnalysis<LiveStacks>()),
172*9880d681SAndroid Build Coastguard Worker AA(&pass.getAnalysis<AAResultsWrapperPass>().getAAResults()),
173*9880d681SAndroid Build Coastguard Worker MDT(pass.getAnalysis<MachineDominatorTree>()),
174*9880d681SAndroid Build Coastguard Worker Loops(pass.getAnalysis<MachineLoopInfo>()), VRM(vrm),
175*9880d681SAndroid Build Coastguard Worker MFI(*mf.getFrameInfo()), MRI(mf.getRegInfo()),
176*9880d681SAndroid Build Coastguard Worker TII(*mf.getSubtarget().getInstrInfo()),
177*9880d681SAndroid Build Coastguard Worker TRI(*mf.getSubtarget().getRegisterInfo()),
178*9880d681SAndroid Build Coastguard Worker MBFI(pass.getAnalysis<MachineBlockFrequencyInfo>()),
179*9880d681SAndroid Build Coastguard Worker HSpiller(pass, mf, vrm) {}
180*9880d681SAndroid Build Coastguard Worker
181*9880d681SAndroid Build Coastguard Worker void spill(LiveRangeEdit &) override;
182*9880d681SAndroid Build Coastguard Worker void postOptimization() override;
183*9880d681SAndroid Build Coastguard Worker
184*9880d681SAndroid Build Coastguard Worker private:
185*9880d681SAndroid Build Coastguard Worker bool isSnippet(const LiveInterval &SnipLI);
186*9880d681SAndroid Build Coastguard Worker void collectRegsToSpill();
187*9880d681SAndroid Build Coastguard Worker
isRegToSpill(unsigned Reg)188*9880d681SAndroid Build Coastguard Worker bool isRegToSpill(unsigned Reg) {
189*9880d681SAndroid Build Coastguard Worker return std::find(RegsToSpill.begin(),
190*9880d681SAndroid Build Coastguard Worker RegsToSpill.end(), Reg) != RegsToSpill.end();
191*9880d681SAndroid Build Coastguard Worker }
192*9880d681SAndroid Build Coastguard Worker
193*9880d681SAndroid Build Coastguard Worker bool isSibling(unsigned Reg);
194*9880d681SAndroid Build Coastguard Worker bool hoistSpillInsideBB(LiveInterval &SpillLI, MachineInstr &CopyMI);
195*9880d681SAndroid Build Coastguard Worker void eliminateRedundantSpills(LiveInterval &LI, VNInfo *VNI);
196*9880d681SAndroid Build Coastguard Worker
197*9880d681SAndroid Build Coastguard Worker void markValueUsed(LiveInterval*, VNInfo*);
198*9880d681SAndroid Build Coastguard Worker bool reMaterializeFor(LiveInterval &, MachineInstr &MI);
199*9880d681SAndroid Build Coastguard Worker void reMaterializeAll();
200*9880d681SAndroid Build Coastguard Worker
201*9880d681SAndroid Build Coastguard Worker bool coalesceStackAccess(MachineInstr *MI, unsigned Reg);
202*9880d681SAndroid Build Coastguard Worker bool foldMemoryOperand(ArrayRef<std::pair<MachineInstr*, unsigned> >,
203*9880d681SAndroid Build Coastguard Worker MachineInstr *LoadMI = nullptr);
204*9880d681SAndroid Build Coastguard Worker void insertReload(unsigned VReg, SlotIndex, MachineBasicBlock::iterator MI);
205*9880d681SAndroid Build Coastguard Worker void insertSpill(unsigned VReg, bool isKill, MachineBasicBlock::iterator MI);
206*9880d681SAndroid Build Coastguard Worker
207*9880d681SAndroid Build Coastguard Worker void spillAroundUses(unsigned Reg);
208*9880d681SAndroid Build Coastguard Worker void spillAll();
209*9880d681SAndroid Build Coastguard Worker };
210*9880d681SAndroid Build Coastguard Worker }
211*9880d681SAndroid Build Coastguard Worker
212*9880d681SAndroid Build Coastguard Worker namespace llvm {
213*9880d681SAndroid Build Coastguard Worker
~Spiller()214*9880d681SAndroid Build Coastguard Worker Spiller::~Spiller() { }
anchor()215*9880d681SAndroid Build Coastguard Worker void Spiller::anchor() { }
216*9880d681SAndroid Build Coastguard Worker
createInlineSpiller(MachineFunctionPass & pass,MachineFunction & mf,VirtRegMap & vrm)217*9880d681SAndroid Build Coastguard Worker Spiller *createInlineSpiller(MachineFunctionPass &pass,
218*9880d681SAndroid Build Coastguard Worker MachineFunction &mf,
219*9880d681SAndroid Build Coastguard Worker VirtRegMap &vrm) {
220*9880d681SAndroid Build Coastguard Worker return new InlineSpiller(pass, mf, vrm);
221*9880d681SAndroid Build Coastguard Worker }
222*9880d681SAndroid Build Coastguard Worker
223*9880d681SAndroid Build Coastguard Worker }
224*9880d681SAndroid Build Coastguard Worker
225*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
226*9880d681SAndroid Build Coastguard Worker // Snippets
227*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
228*9880d681SAndroid Build Coastguard Worker
229*9880d681SAndroid Build Coastguard Worker // When spilling a virtual register, we also spill any snippets it is connected
230*9880d681SAndroid Build Coastguard Worker // to. The snippets are small live ranges that only have a single real use,
231*9880d681SAndroid Build Coastguard Worker // leftovers from live range splitting. Spilling them enables memory operand
232*9880d681SAndroid Build Coastguard Worker // folding or tightens the live range around the single use.
233*9880d681SAndroid Build Coastguard Worker //
234*9880d681SAndroid Build Coastguard Worker // This minimizes register pressure and maximizes the store-to-load distance for
235*9880d681SAndroid Build Coastguard Worker // spill slots which can be important in tight loops.
236*9880d681SAndroid Build Coastguard Worker
237*9880d681SAndroid Build Coastguard Worker /// isFullCopyOf - If MI is a COPY to or from Reg, return the other register,
238*9880d681SAndroid Build Coastguard Worker /// otherwise return 0.
isFullCopyOf(const MachineInstr & MI,unsigned Reg)239*9880d681SAndroid Build Coastguard Worker static unsigned isFullCopyOf(const MachineInstr &MI, unsigned Reg) {
240*9880d681SAndroid Build Coastguard Worker if (!MI.isFullCopy())
241*9880d681SAndroid Build Coastguard Worker return 0;
242*9880d681SAndroid Build Coastguard Worker if (MI.getOperand(0).getReg() == Reg)
243*9880d681SAndroid Build Coastguard Worker return MI.getOperand(1).getReg();
244*9880d681SAndroid Build Coastguard Worker if (MI.getOperand(1).getReg() == Reg)
245*9880d681SAndroid Build Coastguard Worker return MI.getOperand(0).getReg();
246*9880d681SAndroid Build Coastguard Worker return 0;
247*9880d681SAndroid Build Coastguard Worker }
248*9880d681SAndroid Build Coastguard Worker
249*9880d681SAndroid Build Coastguard Worker /// isSnippet - Identify if a live interval is a snippet that should be spilled.
250*9880d681SAndroid Build Coastguard Worker /// It is assumed that SnipLI is a virtual register with the same original as
251*9880d681SAndroid Build Coastguard Worker /// Edit->getReg().
isSnippet(const LiveInterval & SnipLI)252*9880d681SAndroid Build Coastguard Worker bool InlineSpiller::isSnippet(const LiveInterval &SnipLI) {
253*9880d681SAndroid Build Coastguard Worker unsigned Reg = Edit->getReg();
254*9880d681SAndroid Build Coastguard Worker
255*9880d681SAndroid Build Coastguard Worker // A snippet is a tiny live range with only a single instruction using it
256*9880d681SAndroid Build Coastguard Worker // besides copies to/from Reg or spills/fills. We accept:
257*9880d681SAndroid Build Coastguard Worker //
258*9880d681SAndroid Build Coastguard Worker // %snip = COPY %Reg / FILL fi#
259*9880d681SAndroid Build Coastguard Worker // %snip = USE %snip
260*9880d681SAndroid Build Coastguard Worker // %Reg = COPY %snip / SPILL %snip, fi#
261*9880d681SAndroid Build Coastguard Worker //
262*9880d681SAndroid Build Coastguard Worker if (SnipLI.getNumValNums() > 2 || !LIS.intervalIsInOneMBB(SnipLI))
263*9880d681SAndroid Build Coastguard Worker return false;
264*9880d681SAndroid Build Coastguard Worker
265*9880d681SAndroid Build Coastguard Worker MachineInstr *UseMI = nullptr;
266*9880d681SAndroid Build Coastguard Worker
267*9880d681SAndroid Build Coastguard Worker // Check that all uses satisfy our criteria.
268*9880d681SAndroid Build Coastguard Worker for (MachineRegisterInfo::reg_instr_nodbg_iterator
269*9880d681SAndroid Build Coastguard Worker RI = MRI.reg_instr_nodbg_begin(SnipLI.reg),
270*9880d681SAndroid Build Coastguard Worker E = MRI.reg_instr_nodbg_end(); RI != E; ) {
271*9880d681SAndroid Build Coastguard Worker MachineInstr &MI = *RI++;
272*9880d681SAndroid Build Coastguard Worker
273*9880d681SAndroid Build Coastguard Worker // Allow copies to/from Reg.
274*9880d681SAndroid Build Coastguard Worker if (isFullCopyOf(MI, Reg))
275*9880d681SAndroid Build Coastguard Worker continue;
276*9880d681SAndroid Build Coastguard Worker
277*9880d681SAndroid Build Coastguard Worker // Allow stack slot loads.
278*9880d681SAndroid Build Coastguard Worker int FI;
279*9880d681SAndroid Build Coastguard Worker if (SnipLI.reg == TII.isLoadFromStackSlot(MI, FI) && FI == StackSlot)
280*9880d681SAndroid Build Coastguard Worker continue;
281*9880d681SAndroid Build Coastguard Worker
282*9880d681SAndroid Build Coastguard Worker // Allow stack slot stores.
283*9880d681SAndroid Build Coastguard Worker if (SnipLI.reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot)
284*9880d681SAndroid Build Coastguard Worker continue;
285*9880d681SAndroid Build Coastguard Worker
286*9880d681SAndroid Build Coastguard Worker // Allow a single additional instruction.
287*9880d681SAndroid Build Coastguard Worker if (UseMI && &MI != UseMI)
288*9880d681SAndroid Build Coastguard Worker return false;
289*9880d681SAndroid Build Coastguard Worker UseMI = &MI;
290*9880d681SAndroid Build Coastguard Worker }
291*9880d681SAndroid Build Coastguard Worker return true;
292*9880d681SAndroid Build Coastguard Worker }
293*9880d681SAndroid Build Coastguard Worker
294*9880d681SAndroid Build Coastguard Worker /// collectRegsToSpill - Collect live range snippets that only have a single
295*9880d681SAndroid Build Coastguard Worker /// real use.
collectRegsToSpill()296*9880d681SAndroid Build Coastguard Worker void InlineSpiller::collectRegsToSpill() {
297*9880d681SAndroid Build Coastguard Worker unsigned Reg = Edit->getReg();
298*9880d681SAndroid Build Coastguard Worker
299*9880d681SAndroid Build Coastguard Worker // Main register always spills.
300*9880d681SAndroid Build Coastguard Worker RegsToSpill.assign(1, Reg);
301*9880d681SAndroid Build Coastguard Worker SnippetCopies.clear();
302*9880d681SAndroid Build Coastguard Worker
303*9880d681SAndroid Build Coastguard Worker // Snippets all have the same original, so there can't be any for an original
304*9880d681SAndroid Build Coastguard Worker // register.
305*9880d681SAndroid Build Coastguard Worker if (Original == Reg)
306*9880d681SAndroid Build Coastguard Worker return;
307*9880d681SAndroid Build Coastguard Worker
308*9880d681SAndroid Build Coastguard Worker for (MachineRegisterInfo::reg_instr_iterator
309*9880d681SAndroid Build Coastguard Worker RI = MRI.reg_instr_begin(Reg), E = MRI.reg_instr_end(); RI != E; ) {
310*9880d681SAndroid Build Coastguard Worker MachineInstr &MI = *RI++;
311*9880d681SAndroid Build Coastguard Worker unsigned SnipReg = isFullCopyOf(MI, Reg);
312*9880d681SAndroid Build Coastguard Worker if (!isSibling(SnipReg))
313*9880d681SAndroid Build Coastguard Worker continue;
314*9880d681SAndroid Build Coastguard Worker LiveInterval &SnipLI = LIS.getInterval(SnipReg);
315*9880d681SAndroid Build Coastguard Worker if (!isSnippet(SnipLI))
316*9880d681SAndroid Build Coastguard Worker continue;
317*9880d681SAndroid Build Coastguard Worker SnippetCopies.insert(&MI);
318*9880d681SAndroid Build Coastguard Worker if (isRegToSpill(SnipReg))
319*9880d681SAndroid Build Coastguard Worker continue;
320*9880d681SAndroid Build Coastguard Worker RegsToSpill.push_back(SnipReg);
321*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "\talso spill snippet " << SnipLI << '\n');
322*9880d681SAndroid Build Coastguard Worker ++NumSnippets;
323*9880d681SAndroid Build Coastguard Worker }
324*9880d681SAndroid Build Coastguard Worker }
325*9880d681SAndroid Build Coastguard Worker
isSibling(unsigned Reg)326*9880d681SAndroid Build Coastguard Worker bool InlineSpiller::isSibling(unsigned Reg) {
327*9880d681SAndroid Build Coastguard Worker return TargetRegisterInfo::isVirtualRegister(Reg) &&
328*9880d681SAndroid Build Coastguard Worker VRM.getOriginal(Reg) == Original;
329*9880d681SAndroid Build Coastguard Worker }
330*9880d681SAndroid Build Coastguard Worker
331*9880d681SAndroid Build Coastguard Worker /// It is beneficial to spill to earlier place in the same BB in case
332*9880d681SAndroid Build Coastguard Worker /// as follows:
333*9880d681SAndroid Build Coastguard Worker /// There is an alternative def earlier in the same MBB.
334*9880d681SAndroid Build Coastguard Worker /// Hoist the spill as far as possible in SpillMBB. This can ease
335*9880d681SAndroid Build Coastguard Worker /// register pressure:
336*9880d681SAndroid Build Coastguard Worker ///
337*9880d681SAndroid Build Coastguard Worker /// x = def
338*9880d681SAndroid Build Coastguard Worker /// y = use x
339*9880d681SAndroid Build Coastguard Worker /// s = copy x
340*9880d681SAndroid Build Coastguard Worker ///
341*9880d681SAndroid Build Coastguard Worker /// Hoisting the spill of s to immediately after the def removes the
342*9880d681SAndroid Build Coastguard Worker /// interference between x and y:
343*9880d681SAndroid Build Coastguard Worker ///
344*9880d681SAndroid Build Coastguard Worker /// x = def
345*9880d681SAndroid Build Coastguard Worker /// spill x
346*9880d681SAndroid Build Coastguard Worker /// y = use x<kill>
347*9880d681SAndroid Build Coastguard Worker ///
348*9880d681SAndroid Build Coastguard Worker /// This hoist only helps when the copy kills its source.
349*9880d681SAndroid Build Coastguard Worker ///
hoistSpillInsideBB(LiveInterval & SpillLI,MachineInstr & CopyMI)350*9880d681SAndroid Build Coastguard Worker bool InlineSpiller::hoistSpillInsideBB(LiveInterval &SpillLI,
351*9880d681SAndroid Build Coastguard Worker MachineInstr &CopyMI) {
352*9880d681SAndroid Build Coastguard Worker SlotIndex Idx = LIS.getInstructionIndex(CopyMI);
353*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
354*9880d681SAndroid Build Coastguard Worker VNInfo *VNI = SpillLI.getVNInfoAt(Idx.getRegSlot());
355*9880d681SAndroid Build Coastguard Worker assert(VNI && VNI->def == Idx.getRegSlot() && "Not defined by copy");
356*9880d681SAndroid Build Coastguard Worker #endif
357*9880d681SAndroid Build Coastguard Worker
358*9880d681SAndroid Build Coastguard Worker unsigned SrcReg = CopyMI.getOperand(1).getReg();
359*9880d681SAndroid Build Coastguard Worker LiveInterval &SrcLI = LIS.getInterval(SrcReg);
360*9880d681SAndroid Build Coastguard Worker VNInfo *SrcVNI = SrcLI.getVNInfoAt(Idx);
361*9880d681SAndroid Build Coastguard Worker LiveQueryResult SrcQ = SrcLI.Query(Idx);
362*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(SrcVNI->def);
363*9880d681SAndroid Build Coastguard Worker if (DefMBB != CopyMI.getParent() || !SrcQ.isKill())
364*9880d681SAndroid Build Coastguard Worker return false;
365*9880d681SAndroid Build Coastguard Worker
366*9880d681SAndroid Build Coastguard Worker // Conservatively extend the stack slot range to the range of the original
367*9880d681SAndroid Build Coastguard Worker // value. We may be able to do better with stack slot coloring by being more
368*9880d681SAndroid Build Coastguard Worker // careful here.
369*9880d681SAndroid Build Coastguard Worker assert(StackInt && "No stack slot assigned yet.");
370*9880d681SAndroid Build Coastguard Worker LiveInterval &OrigLI = LIS.getInterval(Original);
371*9880d681SAndroid Build Coastguard Worker VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
372*9880d681SAndroid Build Coastguard Worker StackInt->MergeValueInAsValue(OrigLI, OrigVNI, StackInt->getValNumInfo(0));
373*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "\tmerged orig valno " << OrigVNI->id << ": "
374*9880d681SAndroid Build Coastguard Worker << *StackInt << '\n');
375*9880d681SAndroid Build Coastguard Worker
376*9880d681SAndroid Build Coastguard Worker // We are going to spill SrcVNI immediately after its def, so clear out
377*9880d681SAndroid Build Coastguard Worker // any later spills of the same value.
378*9880d681SAndroid Build Coastguard Worker eliminateRedundantSpills(SrcLI, SrcVNI);
379*9880d681SAndroid Build Coastguard Worker
380*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = LIS.getMBBFromIndex(SrcVNI->def);
381*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator MII;
382*9880d681SAndroid Build Coastguard Worker if (SrcVNI->isPHIDef())
383*9880d681SAndroid Build Coastguard Worker MII = MBB->SkipPHIsAndLabels(MBB->begin());
384*9880d681SAndroid Build Coastguard Worker else {
385*9880d681SAndroid Build Coastguard Worker MachineInstr *DefMI = LIS.getInstructionFromIndex(SrcVNI->def);
386*9880d681SAndroid Build Coastguard Worker assert(DefMI && "Defining instruction disappeared");
387*9880d681SAndroid Build Coastguard Worker MII = DefMI;
388*9880d681SAndroid Build Coastguard Worker ++MII;
389*9880d681SAndroid Build Coastguard Worker }
390*9880d681SAndroid Build Coastguard Worker // Insert spill without kill flag immediately after def.
391*9880d681SAndroid Build Coastguard Worker TII.storeRegToStackSlot(*MBB, MII, SrcReg, false, StackSlot,
392*9880d681SAndroid Build Coastguard Worker MRI.getRegClass(SrcReg), &TRI);
393*9880d681SAndroid Build Coastguard Worker --MII; // Point to store instruction.
394*9880d681SAndroid Build Coastguard Worker LIS.InsertMachineInstrInMaps(*MII);
395*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "\thoisted: " << SrcVNI->def << '\t' << *MII);
396*9880d681SAndroid Build Coastguard Worker
397*9880d681SAndroid Build Coastguard Worker HSpiller.addToMergeableSpills(*MII, StackSlot, Original);
398*9880d681SAndroid Build Coastguard Worker ++NumSpills;
399*9880d681SAndroid Build Coastguard Worker return true;
400*9880d681SAndroid Build Coastguard Worker }
401*9880d681SAndroid Build Coastguard Worker
402*9880d681SAndroid Build Coastguard Worker /// eliminateRedundantSpills - SLI:VNI is known to be on the stack. Remove any
403*9880d681SAndroid Build Coastguard Worker /// redundant spills of this value in SLI.reg and sibling copies.
eliminateRedundantSpills(LiveInterval & SLI,VNInfo * VNI)404*9880d681SAndroid Build Coastguard Worker void InlineSpiller::eliminateRedundantSpills(LiveInterval &SLI, VNInfo *VNI) {
405*9880d681SAndroid Build Coastguard Worker assert(VNI && "Missing value");
406*9880d681SAndroid Build Coastguard Worker SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList;
407*9880d681SAndroid Build Coastguard Worker WorkList.push_back(std::make_pair(&SLI, VNI));
408*9880d681SAndroid Build Coastguard Worker assert(StackInt && "No stack slot assigned yet.");
409*9880d681SAndroid Build Coastguard Worker
410*9880d681SAndroid Build Coastguard Worker do {
411*9880d681SAndroid Build Coastguard Worker LiveInterval *LI;
412*9880d681SAndroid Build Coastguard Worker std::tie(LI, VNI) = WorkList.pop_back_val();
413*9880d681SAndroid Build Coastguard Worker unsigned Reg = LI->reg;
414*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Checking redundant spills for "
415*9880d681SAndroid Build Coastguard Worker << VNI->id << '@' << VNI->def << " in " << *LI << '\n');
416*9880d681SAndroid Build Coastguard Worker
417*9880d681SAndroid Build Coastguard Worker // Regs to spill are taken care of.
418*9880d681SAndroid Build Coastguard Worker if (isRegToSpill(Reg))
419*9880d681SAndroid Build Coastguard Worker continue;
420*9880d681SAndroid Build Coastguard Worker
421*9880d681SAndroid Build Coastguard Worker // Add all of VNI's live range to StackInt.
422*9880d681SAndroid Build Coastguard Worker StackInt->MergeValueInAsValue(*LI, VNI, StackInt->getValNumInfo(0));
423*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Merged to stack int: " << *StackInt << '\n');
424*9880d681SAndroid Build Coastguard Worker
425*9880d681SAndroid Build Coastguard Worker // Find all spills and copies of VNI.
426*9880d681SAndroid Build Coastguard Worker for (MachineRegisterInfo::use_instr_nodbg_iterator
427*9880d681SAndroid Build Coastguard Worker UI = MRI.use_instr_nodbg_begin(Reg), E = MRI.use_instr_nodbg_end();
428*9880d681SAndroid Build Coastguard Worker UI != E; ) {
429*9880d681SAndroid Build Coastguard Worker MachineInstr &MI = *UI++;
430*9880d681SAndroid Build Coastguard Worker if (!MI.isCopy() && !MI.mayStore())
431*9880d681SAndroid Build Coastguard Worker continue;
432*9880d681SAndroid Build Coastguard Worker SlotIndex Idx = LIS.getInstructionIndex(MI);
433*9880d681SAndroid Build Coastguard Worker if (LI->getVNInfoAt(Idx) != VNI)
434*9880d681SAndroid Build Coastguard Worker continue;
435*9880d681SAndroid Build Coastguard Worker
436*9880d681SAndroid Build Coastguard Worker // Follow sibling copies down the dominator tree.
437*9880d681SAndroid Build Coastguard Worker if (unsigned DstReg = isFullCopyOf(MI, Reg)) {
438*9880d681SAndroid Build Coastguard Worker if (isSibling(DstReg)) {
439*9880d681SAndroid Build Coastguard Worker LiveInterval &DstLI = LIS.getInterval(DstReg);
440*9880d681SAndroid Build Coastguard Worker VNInfo *DstVNI = DstLI.getVNInfoAt(Idx.getRegSlot());
441*9880d681SAndroid Build Coastguard Worker assert(DstVNI && "Missing defined value");
442*9880d681SAndroid Build Coastguard Worker assert(DstVNI->def == Idx.getRegSlot() && "Wrong copy def slot");
443*9880d681SAndroid Build Coastguard Worker WorkList.push_back(std::make_pair(&DstLI, DstVNI));
444*9880d681SAndroid Build Coastguard Worker }
445*9880d681SAndroid Build Coastguard Worker continue;
446*9880d681SAndroid Build Coastguard Worker }
447*9880d681SAndroid Build Coastguard Worker
448*9880d681SAndroid Build Coastguard Worker // Erase spills.
449*9880d681SAndroid Build Coastguard Worker int FI;
450*9880d681SAndroid Build Coastguard Worker if (Reg == TII.isStoreToStackSlot(MI, FI) && FI == StackSlot) {
451*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Redundant spill " << Idx << '\t' << MI);
452*9880d681SAndroid Build Coastguard Worker // eliminateDeadDefs won't normally remove stores, so switch opcode.
453*9880d681SAndroid Build Coastguard Worker MI.setDesc(TII.get(TargetOpcode::KILL));
454*9880d681SAndroid Build Coastguard Worker DeadDefs.push_back(&MI);
455*9880d681SAndroid Build Coastguard Worker ++NumSpillsRemoved;
456*9880d681SAndroid Build Coastguard Worker if (HSpiller.rmFromMergeableSpills(MI, StackSlot))
457*9880d681SAndroid Build Coastguard Worker --NumSpills;
458*9880d681SAndroid Build Coastguard Worker }
459*9880d681SAndroid Build Coastguard Worker }
460*9880d681SAndroid Build Coastguard Worker } while (!WorkList.empty());
461*9880d681SAndroid Build Coastguard Worker }
462*9880d681SAndroid Build Coastguard Worker
463*9880d681SAndroid Build Coastguard Worker
464*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
465*9880d681SAndroid Build Coastguard Worker // Rematerialization
466*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
467*9880d681SAndroid Build Coastguard Worker
468*9880d681SAndroid Build Coastguard Worker /// markValueUsed - Remember that VNI failed to rematerialize, so its defining
469*9880d681SAndroid Build Coastguard Worker /// instruction cannot be eliminated. See through snippet copies
markValueUsed(LiveInterval * LI,VNInfo * VNI)470*9880d681SAndroid Build Coastguard Worker void InlineSpiller::markValueUsed(LiveInterval *LI, VNInfo *VNI) {
471*9880d681SAndroid Build Coastguard Worker SmallVector<std::pair<LiveInterval*, VNInfo*>, 8> WorkList;
472*9880d681SAndroid Build Coastguard Worker WorkList.push_back(std::make_pair(LI, VNI));
473*9880d681SAndroid Build Coastguard Worker do {
474*9880d681SAndroid Build Coastguard Worker std::tie(LI, VNI) = WorkList.pop_back_val();
475*9880d681SAndroid Build Coastguard Worker if (!UsedValues.insert(VNI).second)
476*9880d681SAndroid Build Coastguard Worker continue;
477*9880d681SAndroid Build Coastguard Worker
478*9880d681SAndroid Build Coastguard Worker if (VNI->isPHIDef()) {
479*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
480*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock *P : MBB->predecessors()) {
481*9880d681SAndroid Build Coastguard Worker VNInfo *PVNI = LI->getVNInfoBefore(LIS.getMBBEndIdx(P));
482*9880d681SAndroid Build Coastguard Worker if (PVNI)
483*9880d681SAndroid Build Coastguard Worker WorkList.push_back(std::make_pair(LI, PVNI));
484*9880d681SAndroid Build Coastguard Worker }
485*9880d681SAndroid Build Coastguard Worker continue;
486*9880d681SAndroid Build Coastguard Worker }
487*9880d681SAndroid Build Coastguard Worker
488*9880d681SAndroid Build Coastguard Worker // Follow snippet copies.
489*9880d681SAndroid Build Coastguard Worker MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
490*9880d681SAndroid Build Coastguard Worker if (!SnippetCopies.count(MI))
491*9880d681SAndroid Build Coastguard Worker continue;
492*9880d681SAndroid Build Coastguard Worker LiveInterval &SnipLI = LIS.getInterval(MI->getOperand(1).getReg());
493*9880d681SAndroid Build Coastguard Worker assert(isRegToSpill(SnipLI.reg) && "Unexpected register in copy");
494*9880d681SAndroid Build Coastguard Worker VNInfo *SnipVNI = SnipLI.getVNInfoAt(VNI->def.getRegSlot(true));
495*9880d681SAndroid Build Coastguard Worker assert(SnipVNI && "Snippet undefined before copy");
496*9880d681SAndroid Build Coastguard Worker WorkList.push_back(std::make_pair(&SnipLI, SnipVNI));
497*9880d681SAndroid Build Coastguard Worker } while (!WorkList.empty());
498*9880d681SAndroid Build Coastguard Worker }
499*9880d681SAndroid Build Coastguard Worker
500*9880d681SAndroid Build Coastguard Worker /// reMaterializeFor - Attempt to rematerialize before MI instead of reloading.
reMaterializeFor(LiveInterval & VirtReg,MachineInstr & MI)501*9880d681SAndroid Build Coastguard Worker bool InlineSpiller::reMaterializeFor(LiveInterval &VirtReg, MachineInstr &MI) {
502*9880d681SAndroid Build Coastguard Worker
503*9880d681SAndroid Build Coastguard Worker // Analyze instruction
504*9880d681SAndroid Build Coastguard Worker SmallVector<std::pair<MachineInstr *, unsigned>, 8> Ops;
505*9880d681SAndroid Build Coastguard Worker MIBundleOperands::VirtRegInfo RI =
506*9880d681SAndroid Build Coastguard Worker MIBundleOperands(MI).analyzeVirtReg(VirtReg.reg, &Ops);
507*9880d681SAndroid Build Coastguard Worker
508*9880d681SAndroid Build Coastguard Worker if (!RI.Reads)
509*9880d681SAndroid Build Coastguard Worker return false;
510*9880d681SAndroid Build Coastguard Worker
511*9880d681SAndroid Build Coastguard Worker SlotIndex UseIdx = LIS.getInstructionIndex(MI).getRegSlot(true);
512*9880d681SAndroid Build Coastguard Worker VNInfo *ParentVNI = VirtReg.getVNInfoAt(UseIdx.getBaseIndex());
513*9880d681SAndroid Build Coastguard Worker
514*9880d681SAndroid Build Coastguard Worker if (!ParentVNI) {
515*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "\tadding <undef> flags: ");
516*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
517*9880d681SAndroid Build Coastguard Worker MachineOperand &MO = MI.getOperand(i);
518*9880d681SAndroid Build Coastguard Worker if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg)
519*9880d681SAndroid Build Coastguard Worker MO.setIsUndef();
520*9880d681SAndroid Build Coastguard Worker }
521*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << UseIdx << '\t' << MI);
522*9880d681SAndroid Build Coastguard Worker return true;
523*9880d681SAndroid Build Coastguard Worker }
524*9880d681SAndroid Build Coastguard Worker
525*9880d681SAndroid Build Coastguard Worker if (SnippetCopies.count(&MI))
526*9880d681SAndroid Build Coastguard Worker return false;
527*9880d681SAndroid Build Coastguard Worker
528*9880d681SAndroid Build Coastguard Worker LiveInterval &OrigLI = LIS.getInterval(Original);
529*9880d681SAndroid Build Coastguard Worker VNInfo *OrigVNI = OrigLI.getVNInfoAt(UseIdx);
530*9880d681SAndroid Build Coastguard Worker LiveRangeEdit::Remat RM(ParentVNI);
531*9880d681SAndroid Build Coastguard Worker RM.OrigMI = LIS.getInstructionFromIndex(OrigVNI->def);
532*9880d681SAndroid Build Coastguard Worker
533*9880d681SAndroid Build Coastguard Worker if (!Edit->canRematerializeAt(RM, OrigVNI, UseIdx, false)) {
534*9880d681SAndroid Build Coastguard Worker markValueUsed(&VirtReg, ParentVNI);
535*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "\tcannot remat for " << UseIdx << '\t' << MI);
536*9880d681SAndroid Build Coastguard Worker return false;
537*9880d681SAndroid Build Coastguard Worker }
538*9880d681SAndroid Build Coastguard Worker
539*9880d681SAndroid Build Coastguard Worker // If the instruction also writes VirtReg.reg, it had better not require the
540*9880d681SAndroid Build Coastguard Worker // same register for uses and defs.
541*9880d681SAndroid Build Coastguard Worker if (RI.Tied) {
542*9880d681SAndroid Build Coastguard Worker markValueUsed(&VirtReg, ParentVNI);
543*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "\tcannot remat tied reg: " << UseIdx << '\t' << MI);
544*9880d681SAndroid Build Coastguard Worker return false;
545*9880d681SAndroid Build Coastguard Worker }
546*9880d681SAndroid Build Coastguard Worker
547*9880d681SAndroid Build Coastguard Worker // Before rematerializing into a register for a single instruction, try to
548*9880d681SAndroid Build Coastguard Worker // fold a load into the instruction. That avoids allocating a new register.
549*9880d681SAndroid Build Coastguard Worker if (RM.OrigMI->canFoldAsLoad() &&
550*9880d681SAndroid Build Coastguard Worker foldMemoryOperand(Ops, RM.OrigMI)) {
551*9880d681SAndroid Build Coastguard Worker Edit->markRematerialized(RM.ParentVNI);
552*9880d681SAndroid Build Coastguard Worker ++NumFoldedLoads;
553*9880d681SAndroid Build Coastguard Worker return true;
554*9880d681SAndroid Build Coastguard Worker }
555*9880d681SAndroid Build Coastguard Worker
556*9880d681SAndroid Build Coastguard Worker // Alocate a new register for the remat.
557*9880d681SAndroid Build Coastguard Worker unsigned NewVReg = Edit->createFrom(Original);
558*9880d681SAndroid Build Coastguard Worker
559*9880d681SAndroid Build Coastguard Worker // Finally we can rematerialize OrigMI before MI.
560*9880d681SAndroid Build Coastguard Worker SlotIndex DefIdx =
561*9880d681SAndroid Build Coastguard Worker Edit->rematerializeAt(*MI.getParent(), MI, NewVReg, RM, TRI);
562*9880d681SAndroid Build Coastguard Worker (void)DefIdx;
563*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "\tremat: " << DefIdx << '\t'
564*9880d681SAndroid Build Coastguard Worker << *LIS.getInstructionFromIndex(DefIdx));
565*9880d681SAndroid Build Coastguard Worker
566*9880d681SAndroid Build Coastguard Worker // Replace operands
567*9880d681SAndroid Build Coastguard Worker for (const auto &OpPair : Ops) {
568*9880d681SAndroid Build Coastguard Worker MachineOperand &MO = OpPair.first->getOperand(OpPair.second);
569*9880d681SAndroid Build Coastguard Worker if (MO.isReg() && MO.isUse() && MO.getReg() == VirtReg.reg) {
570*9880d681SAndroid Build Coastguard Worker MO.setReg(NewVReg);
571*9880d681SAndroid Build Coastguard Worker MO.setIsKill();
572*9880d681SAndroid Build Coastguard Worker }
573*9880d681SAndroid Build Coastguard Worker }
574*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "\t " << UseIdx << '\t' << MI << '\n');
575*9880d681SAndroid Build Coastguard Worker
576*9880d681SAndroid Build Coastguard Worker ++NumRemats;
577*9880d681SAndroid Build Coastguard Worker return true;
578*9880d681SAndroid Build Coastguard Worker }
579*9880d681SAndroid Build Coastguard Worker
580*9880d681SAndroid Build Coastguard Worker /// reMaterializeAll - Try to rematerialize as many uses as possible,
581*9880d681SAndroid Build Coastguard Worker /// and trim the live ranges after.
reMaterializeAll()582*9880d681SAndroid Build Coastguard Worker void InlineSpiller::reMaterializeAll() {
583*9880d681SAndroid Build Coastguard Worker if (!Edit->anyRematerializable(AA))
584*9880d681SAndroid Build Coastguard Worker return;
585*9880d681SAndroid Build Coastguard Worker
586*9880d681SAndroid Build Coastguard Worker UsedValues.clear();
587*9880d681SAndroid Build Coastguard Worker
588*9880d681SAndroid Build Coastguard Worker // Try to remat before all uses of snippets.
589*9880d681SAndroid Build Coastguard Worker bool anyRemat = false;
590*9880d681SAndroid Build Coastguard Worker for (unsigned Reg : RegsToSpill) {
591*9880d681SAndroid Build Coastguard Worker LiveInterval &LI = LIS.getInterval(Reg);
592*9880d681SAndroid Build Coastguard Worker for (MachineRegisterInfo::reg_bundle_iterator
593*9880d681SAndroid Build Coastguard Worker RegI = MRI.reg_bundle_begin(Reg), E = MRI.reg_bundle_end();
594*9880d681SAndroid Build Coastguard Worker RegI != E; ) {
595*9880d681SAndroid Build Coastguard Worker MachineInstr &MI = *RegI++;
596*9880d681SAndroid Build Coastguard Worker
597*9880d681SAndroid Build Coastguard Worker // Debug values are not allowed to affect codegen.
598*9880d681SAndroid Build Coastguard Worker if (MI.isDebugValue())
599*9880d681SAndroid Build Coastguard Worker continue;
600*9880d681SAndroid Build Coastguard Worker
601*9880d681SAndroid Build Coastguard Worker anyRemat |= reMaterializeFor(LI, MI);
602*9880d681SAndroid Build Coastguard Worker }
603*9880d681SAndroid Build Coastguard Worker }
604*9880d681SAndroid Build Coastguard Worker if (!anyRemat)
605*9880d681SAndroid Build Coastguard Worker return;
606*9880d681SAndroid Build Coastguard Worker
607*9880d681SAndroid Build Coastguard Worker // Remove any values that were completely rematted.
608*9880d681SAndroid Build Coastguard Worker for (unsigned Reg : RegsToSpill) {
609*9880d681SAndroid Build Coastguard Worker LiveInterval &LI = LIS.getInterval(Reg);
610*9880d681SAndroid Build Coastguard Worker for (LiveInterval::vni_iterator I = LI.vni_begin(), E = LI.vni_end();
611*9880d681SAndroid Build Coastguard Worker I != E; ++I) {
612*9880d681SAndroid Build Coastguard Worker VNInfo *VNI = *I;
613*9880d681SAndroid Build Coastguard Worker if (VNI->isUnused() || VNI->isPHIDef() || UsedValues.count(VNI))
614*9880d681SAndroid Build Coastguard Worker continue;
615*9880d681SAndroid Build Coastguard Worker MachineInstr *MI = LIS.getInstructionFromIndex(VNI->def);
616*9880d681SAndroid Build Coastguard Worker MI->addRegisterDead(Reg, &TRI);
617*9880d681SAndroid Build Coastguard Worker if (!MI->allDefsAreDead())
618*9880d681SAndroid Build Coastguard Worker continue;
619*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "All defs dead: " << *MI);
620*9880d681SAndroid Build Coastguard Worker DeadDefs.push_back(MI);
621*9880d681SAndroid Build Coastguard Worker }
622*9880d681SAndroid Build Coastguard Worker }
623*9880d681SAndroid Build Coastguard Worker
624*9880d681SAndroid Build Coastguard Worker // Eliminate dead code after remat. Note that some snippet copies may be
625*9880d681SAndroid Build Coastguard Worker // deleted here.
626*9880d681SAndroid Build Coastguard Worker if (DeadDefs.empty())
627*9880d681SAndroid Build Coastguard Worker return;
628*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Remat created " << DeadDefs.size() << " dead defs.\n");
629*9880d681SAndroid Build Coastguard Worker Edit->eliminateDeadDefs(DeadDefs, RegsToSpill, AA);
630*9880d681SAndroid Build Coastguard Worker
631*9880d681SAndroid Build Coastguard Worker // LiveRangeEdit::eliminateDeadDef is used to remove dead define instructions
632*9880d681SAndroid Build Coastguard Worker // after rematerialization. To remove a VNI for a vreg from its LiveInterval,
633*9880d681SAndroid Build Coastguard Worker // LiveIntervals::removeVRegDefAt is used. However, after non-PHI VNIs are all
634*9880d681SAndroid Build Coastguard Worker // removed, PHI VNI are still left in the LiveInterval.
635*9880d681SAndroid Build Coastguard Worker // So to get rid of unused reg, we need to check whether it has non-dbg
636*9880d681SAndroid Build Coastguard Worker // reference instead of whether it has non-empty interval.
637*9880d681SAndroid Build Coastguard Worker unsigned ResultPos = 0;
638*9880d681SAndroid Build Coastguard Worker for (unsigned Reg : RegsToSpill) {
639*9880d681SAndroid Build Coastguard Worker if (MRI.reg_nodbg_empty(Reg)) {
640*9880d681SAndroid Build Coastguard Worker Edit->eraseVirtReg(Reg);
641*9880d681SAndroid Build Coastguard Worker continue;
642*9880d681SAndroid Build Coastguard Worker }
643*9880d681SAndroid Build Coastguard Worker assert((LIS.hasInterval(Reg) && !LIS.getInterval(Reg).empty()) &&
644*9880d681SAndroid Build Coastguard Worker "Reg with empty interval has reference");
645*9880d681SAndroid Build Coastguard Worker RegsToSpill[ResultPos++] = Reg;
646*9880d681SAndroid Build Coastguard Worker }
647*9880d681SAndroid Build Coastguard Worker RegsToSpill.erase(RegsToSpill.begin() + ResultPos, RegsToSpill.end());
648*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << RegsToSpill.size() << " registers to spill after remat.\n");
649*9880d681SAndroid Build Coastguard Worker }
650*9880d681SAndroid Build Coastguard Worker
651*9880d681SAndroid Build Coastguard Worker
652*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
653*9880d681SAndroid Build Coastguard Worker // Spilling
654*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
655*9880d681SAndroid Build Coastguard Worker
656*9880d681SAndroid Build Coastguard Worker /// If MI is a load or store of StackSlot, it can be removed.
coalesceStackAccess(MachineInstr * MI,unsigned Reg)657*9880d681SAndroid Build Coastguard Worker bool InlineSpiller::coalesceStackAccess(MachineInstr *MI, unsigned Reg) {
658*9880d681SAndroid Build Coastguard Worker int FI = 0;
659*9880d681SAndroid Build Coastguard Worker unsigned InstrReg = TII.isLoadFromStackSlot(*MI, FI);
660*9880d681SAndroid Build Coastguard Worker bool IsLoad = InstrReg;
661*9880d681SAndroid Build Coastguard Worker if (!IsLoad)
662*9880d681SAndroid Build Coastguard Worker InstrReg = TII.isStoreToStackSlot(*MI, FI);
663*9880d681SAndroid Build Coastguard Worker
664*9880d681SAndroid Build Coastguard Worker // We have a stack access. Is it the right register and slot?
665*9880d681SAndroid Build Coastguard Worker if (InstrReg != Reg || FI != StackSlot)
666*9880d681SAndroid Build Coastguard Worker return false;
667*9880d681SAndroid Build Coastguard Worker
668*9880d681SAndroid Build Coastguard Worker if (!IsLoad)
669*9880d681SAndroid Build Coastguard Worker HSpiller.rmFromMergeableSpills(*MI, StackSlot);
670*9880d681SAndroid Build Coastguard Worker
671*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Coalescing stack access: " << *MI);
672*9880d681SAndroid Build Coastguard Worker LIS.RemoveMachineInstrFromMaps(*MI);
673*9880d681SAndroid Build Coastguard Worker MI->eraseFromParent();
674*9880d681SAndroid Build Coastguard Worker
675*9880d681SAndroid Build Coastguard Worker if (IsLoad) {
676*9880d681SAndroid Build Coastguard Worker ++NumReloadsRemoved;
677*9880d681SAndroid Build Coastguard Worker --NumReloads;
678*9880d681SAndroid Build Coastguard Worker } else {
679*9880d681SAndroid Build Coastguard Worker ++NumSpillsRemoved;
680*9880d681SAndroid Build Coastguard Worker --NumSpills;
681*9880d681SAndroid Build Coastguard Worker }
682*9880d681SAndroid Build Coastguard Worker
683*9880d681SAndroid Build Coastguard Worker return true;
684*9880d681SAndroid Build Coastguard Worker }
685*9880d681SAndroid Build Coastguard Worker
686*9880d681SAndroid Build Coastguard Worker #if !defined(NDEBUG)
687*9880d681SAndroid Build Coastguard Worker // Dump the range of instructions from B to E with their slot indexes.
dumpMachineInstrRangeWithSlotIndex(MachineBasicBlock::iterator B,MachineBasicBlock::iterator E,LiveIntervals const & LIS,const char * const header,unsigned VReg=0)688*9880d681SAndroid Build Coastguard Worker static void dumpMachineInstrRangeWithSlotIndex(MachineBasicBlock::iterator B,
689*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator E,
690*9880d681SAndroid Build Coastguard Worker LiveIntervals const &LIS,
691*9880d681SAndroid Build Coastguard Worker const char *const header,
692*9880d681SAndroid Build Coastguard Worker unsigned VReg =0) {
693*9880d681SAndroid Build Coastguard Worker char NextLine = '\n';
694*9880d681SAndroid Build Coastguard Worker char SlotIndent = '\t';
695*9880d681SAndroid Build Coastguard Worker
696*9880d681SAndroid Build Coastguard Worker if (std::next(B) == E) {
697*9880d681SAndroid Build Coastguard Worker NextLine = ' ';
698*9880d681SAndroid Build Coastguard Worker SlotIndent = ' ';
699*9880d681SAndroid Build Coastguard Worker }
700*9880d681SAndroid Build Coastguard Worker
701*9880d681SAndroid Build Coastguard Worker dbgs() << '\t' << header << ": " << NextLine;
702*9880d681SAndroid Build Coastguard Worker
703*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::iterator I = B; I != E; ++I) {
704*9880d681SAndroid Build Coastguard Worker SlotIndex Idx = LIS.getInstructionIndex(*I).getRegSlot();
705*9880d681SAndroid Build Coastguard Worker
706*9880d681SAndroid Build Coastguard Worker // If a register was passed in and this instruction has it as a
707*9880d681SAndroid Build Coastguard Worker // destination that is marked as an early clobber, print the
708*9880d681SAndroid Build Coastguard Worker // early-clobber slot index.
709*9880d681SAndroid Build Coastguard Worker if (VReg) {
710*9880d681SAndroid Build Coastguard Worker MachineOperand *MO = I->findRegisterDefOperand(VReg);
711*9880d681SAndroid Build Coastguard Worker if (MO && MO->isEarlyClobber())
712*9880d681SAndroid Build Coastguard Worker Idx = Idx.getRegSlot(true);
713*9880d681SAndroid Build Coastguard Worker }
714*9880d681SAndroid Build Coastguard Worker
715*9880d681SAndroid Build Coastguard Worker dbgs() << SlotIndent << Idx << '\t' << *I;
716*9880d681SAndroid Build Coastguard Worker }
717*9880d681SAndroid Build Coastguard Worker }
718*9880d681SAndroid Build Coastguard Worker #endif
719*9880d681SAndroid Build Coastguard Worker
720*9880d681SAndroid Build Coastguard Worker /// foldMemoryOperand - Try folding stack slot references in Ops into their
721*9880d681SAndroid Build Coastguard Worker /// instructions.
722*9880d681SAndroid Build Coastguard Worker ///
723*9880d681SAndroid Build Coastguard Worker /// @param Ops Operand indices from analyzeVirtReg().
724*9880d681SAndroid Build Coastguard Worker /// @param LoadMI Load instruction to use instead of stack slot when non-null.
725*9880d681SAndroid Build Coastguard Worker /// @return True on success.
726*9880d681SAndroid Build Coastguard Worker bool InlineSpiller::
foldMemoryOperand(ArrayRef<std::pair<MachineInstr *,unsigned>> Ops,MachineInstr * LoadMI)727*9880d681SAndroid Build Coastguard Worker foldMemoryOperand(ArrayRef<std::pair<MachineInstr*, unsigned> > Ops,
728*9880d681SAndroid Build Coastguard Worker MachineInstr *LoadMI) {
729*9880d681SAndroid Build Coastguard Worker if (Ops.empty())
730*9880d681SAndroid Build Coastguard Worker return false;
731*9880d681SAndroid Build Coastguard Worker // Don't attempt folding in bundles.
732*9880d681SAndroid Build Coastguard Worker MachineInstr *MI = Ops.front().first;
733*9880d681SAndroid Build Coastguard Worker if (Ops.back().first != MI || MI->isBundled())
734*9880d681SAndroid Build Coastguard Worker return false;
735*9880d681SAndroid Build Coastguard Worker
736*9880d681SAndroid Build Coastguard Worker bool WasCopy = MI->isCopy();
737*9880d681SAndroid Build Coastguard Worker unsigned ImpReg = 0;
738*9880d681SAndroid Build Coastguard Worker
739*9880d681SAndroid Build Coastguard Worker bool SpillSubRegs = (MI->getOpcode() == TargetOpcode::STATEPOINT ||
740*9880d681SAndroid Build Coastguard Worker MI->getOpcode() == TargetOpcode::PATCHPOINT ||
741*9880d681SAndroid Build Coastguard Worker MI->getOpcode() == TargetOpcode::STACKMAP);
742*9880d681SAndroid Build Coastguard Worker
743*9880d681SAndroid Build Coastguard Worker // TargetInstrInfo::foldMemoryOperand only expects explicit, non-tied
744*9880d681SAndroid Build Coastguard Worker // operands.
745*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned, 8> FoldOps;
746*9880d681SAndroid Build Coastguard Worker for (const auto &OpPair : Ops) {
747*9880d681SAndroid Build Coastguard Worker unsigned Idx = OpPair.second;
748*9880d681SAndroid Build Coastguard Worker assert(MI == OpPair.first && "Instruction conflict during operand folding");
749*9880d681SAndroid Build Coastguard Worker MachineOperand &MO = MI->getOperand(Idx);
750*9880d681SAndroid Build Coastguard Worker if (MO.isImplicit()) {
751*9880d681SAndroid Build Coastguard Worker ImpReg = MO.getReg();
752*9880d681SAndroid Build Coastguard Worker continue;
753*9880d681SAndroid Build Coastguard Worker }
754*9880d681SAndroid Build Coastguard Worker // FIXME: Teach targets to deal with subregs.
755*9880d681SAndroid Build Coastguard Worker if (!SpillSubRegs && MO.getSubReg())
756*9880d681SAndroid Build Coastguard Worker return false;
757*9880d681SAndroid Build Coastguard Worker // We cannot fold a load instruction into a def.
758*9880d681SAndroid Build Coastguard Worker if (LoadMI && MO.isDef())
759*9880d681SAndroid Build Coastguard Worker return false;
760*9880d681SAndroid Build Coastguard Worker // Tied use operands should not be passed to foldMemoryOperand.
761*9880d681SAndroid Build Coastguard Worker if (!MI->isRegTiedToDefOperand(Idx))
762*9880d681SAndroid Build Coastguard Worker FoldOps.push_back(Idx);
763*9880d681SAndroid Build Coastguard Worker }
764*9880d681SAndroid Build Coastguard Worker
765*9880d681SAndroid Build Coastguard Worker MachineInstrSpan MIS(MI);
766*9880d681SAndroid Build Coastguard Worker
767*9880d681SAndroid Build Coastguard Worker MachineInstr *FoldMI =
768*9880d681SAndroid Build Coastguard Worker LoadMI ? TII.foldMemoryOperand(*MI, FoldOps, *LoadMI, &LIS)
769*9880d681SAndroid Build Coastguard Worker : TII.foldMemoryOperand(*MI, FoldOps, StackSlot, &LIS);
770*9880d681SAndroid Build Coastguard Worker if (!FoldMI)
771*9880d681SAndroid Build Coastguard Worker return false;
772*9880d681SAndroid Build Coastguard Worker
773*9880d681SAndroid Build Coastguard Worker // Remove LIS for any dead defs in the original MI not in FoldMI.
774*9880d681SAndroid Build Coastguard Worker for (MIBundleOperands MO(*MI); MO.isValid(); ++MO) {
775*9880d681SAndroid Build Coastguard Worker if (!MO->isReg())
776*9880d681SAndroid Build Coastguard Worker continue;
777*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO->getReg();
778*9880d681SAndroid Build Coastguard Worker if (!Reg || TargetRegisterInfo::isVirtualRegister(Reg) ||
779*9880d681SAndroid Build Coastguard Worker MRI.isReserved(Reg)) {
780*9880d681SAndroid Build Coastguard Worker continue;
781*9880d681SAndroid Build Coastguard Worker }
782*9880d681SAndroid Build Coastguard Worker // Skip non-Defs, including undef uses and internal reads.
783*9880d681SAndroid Build Coastguard Worker if (MO->isUse())
784*9880d681SAndroid Build Coastguard Worker continue;
785*9880d681SAndroid Build Coastguard Worker MIBundleOperands::PhysRegInfo RI =
786*9880d681SAndroid Build Coastguard Worker MIBundleOperands(*FoldMI).analyzePhysReg(Reg, &TRI);
787*9880d681SAndroid Build Coastguard Worker if (RI.FullyDefined)
788*9880d681SAndroid Build Coastguard Worker continue;
789*9880d681SAndroid Build Coastguard Worker // FoldMI does not define this physreg. Remove the LI segment.
790*9880d681SAndroid Build Coastguard Worker assert(MO->isDead() && "Cannot fold physreg def");
791*9880d681SAndroid Build Coastguard Worker SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot();
792*9880d681SAndroid Build Coastguard Worker LIS.removePhysRegDefAt(Reg, Idx);
793*9880d681SAndroid Build Coastguard Worker }
794*9880d681SAndroid Build Coastguard Worker
795*9880d681SAndroid Build Coastguard Worker int FI;
796*9880d681SAndroid Build Coastguard Worker if (TII.isStoreToStackSlot(*MI, FI) &&
797*9880d681SAndroid Build Coastguard Worker HSpiller.rmFromMergeableSpills(*MI, FI))
798*9880d681SAndroid Build Coastguard Worker --NumSpills;
799*9880d681SAndroid Build Coastguard Worker LIS.ReplaceMachineInstrInMaps(*MI, *FoldMI);
800*9880d681SAndroid Build Coastguard Worker MI->eraseFromParent();
801*9880d681SAndroid Build Coastguard Worker
802*9880d681SAndroid Build Coastguard Worker // Insert any new instructions other than FoldMI into the LIS maps.
803*9880d681SAndroid Build Coastguard Worker assert(!MIS.empty() && "Unexpected empty span of instructions!");
804*9880d681SAndroid Build Coastguard Worker for (MachineInstr &MI : MIS)
805*9880d681SAndroid Build Coastguard Worker if (&MI != FoldMI)
806*9880d681SAndroid Build Coastguard Worker LIS.InsertMachineInstrInMaps(MI);
807*9880d681SAndroid Build Coastguard Worker
808*9880d681SAndroid Build Coastguard Worker // TII.foldMemoryOperand may have left some implicit operands on the
809*9880d681SAndroid Build Coastguard Worker // instruction. Strip them.
810*9880d681SAndroid Build Coastguard Worker if (ImpReg)
811*9880d681SAndroid Build Coastguard Worker for (unsigned i = FoldMI->getNumOperands(); i; --i) {
812*9880d681SAndroid Build Coastguard Worker MachineOperand &MO = FoldMI->getOperand(i - 1);
813*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isImplicit())
814*9880d681SAndroid Build Coastguard Worker break;
815*9880d681SAndroid Build Coastguard Worker if (MO.getReg() == ImpReg)
816*9880d681SAndroid Build Coastguard Worker FoldMI->RemoveOperand(i - 1);
817*9880d681SAndroid Build Coastguard Worker }
818*9880d681SAndroid Build Coastguard Worker
819*9880d681SAndroid Build Coastguard Worker DEBUG(dumpMachineInstrRangeWithSlotIndex(MIS.begin(), MIS.end(), LIS,
820*9880d681SAndroid Build Coastguard Worker "folded"));
821*9880d681SAndroid Build Coastguard Worker
822*9880d681SAndroid Build Coastguard Worker if (!WasCopy)
823*9880d681SAndroid Build Coastguard Worker ++NumFolded;
824*9880d681SAndroid Build Coastguard Worker else if (Ops.front().second == 0) {
825*9880d681SAndroid Build Coastguard Worker ++NumSpills;
826*9880d681SAndroid Build Coastguard Worker HSpiller.addToMergeableSpills(*FoldMI, StackSlot, Original);
827*9880d681SAndroid Build Coastguard Worker } else
828*9880d681SAndroid Build Coastguard Worker ++NumReloads;
829*9880d681SAndroid Build Coastguard Worker return true;
830*9880d681SAndroid Build Coastguard Worker }
831*9880d681SAndroid Build Coastguard Worker
insertReload(unsigned NewVReg,SlotIndex Idx,MachineBasicBlock::iterator MI)832*9880d681SAndroid Build Coastguard Worker void InlineSpiller::insertReload(unsigned NewVReg,
833*9880d681SAndroid Build Coastguard Worker SlotIndex Idx,
834*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator MI) {
835*9880d681SAndroid Build Coastguard Worker MachineBasicBlock &MBB = *MI->getParent();
836*9880d681SAndroid Build Coastguard Worker
837*9880d681SAndroid Build Coastguard Worker MachineInstrSpan MIS(MI);
838*9880d681SAndroid Build Coastguard Worker TII.loadRegFromStackSlot(MBB, MI, NewVReg, StackSlot,
839*9880d681SAndroid Build Coastguard Worker MRI.getRegClass(NewVReg), &TRI);
840*9880d681SAndroid Build Coastguard Worker
841*9880d681SAndroid Build Coastguard Worker LIS.InsertMachineInstrRangeInMaps(MIS.begin(), MI);
842*9880d681SAndroid Build Coastguard Worker
843*9880d681SAndroid Build Coastguard Worker DEBUG(dumpMachineInstrRangeWithSlotIndex(MIS.begin(), MI, LIS, "reload",
844*9880d681SAndroid Build Coastguard Worker NewVReg));
845*9880d681SAndroid Build Coastguard Worker ++NumReloads;
846*9880d681SAndroid Build Coastguard Worker }
847*9880d681SAndroid Build Coastguard Worker
848*9880d681SAndroid Build Coastguard Worker /// insertSpill - Insert a spill of NewVReg after MI.
insertSpill(unsigned NewVReg,bool isKill,MachineBasicBlock::iterator MI)849*9880d681SAndroid Build Coastguard Worker void InlineSpiller::insertSpill(unsigned NewVReg, bool isKill,
850*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator MI) {
851*9880d681SAndroid Build Coastguard Worker MachineBasicBlock &MBB = *MI->getParent();
852*9880d681SAndroid Build Coastguard Worker
853*9880d681SAndroid Build Coastguard Worker MachineInstrSpan MIS(MI);
854*9880d681SAndroid Build Coastguard Worker TII.storeRegToStackSlot(MBB, std::next(MI), NewVReg, isKill, StackSlot,
855*9880d681SAndroid Build Coastguard Worker MRI.getRegClass(NewVReg), &TRI);
856*9880d681SAndroid Build Coastguard Worker
857*9880d681SAndroid Build Coastguard Worker LIS.InsertMachineInstrRangeInMaps(std::next(MI), MIS.end());
858*9880d681SAndroid Build Coastguard Worker
859*9880d681SAndroid Build Coastguard Worker DEBUG(dumpMachineInstrRangeWithSlotIndex(std::next(MI), MIS.end(), LIS,
860*9880d681SAndroid Build Coastguard Worker "spill"));
861*9880d681SAndroid Build Coastguard Worker ++NumSpills;
862*9880d681SAndroid Build Coastguard Worker HSpiller.addToMergeableSpills(*std::next(MI), StackSlot, Original);
863*9880d681SAndroid Build Coastguard Worker }
864*9880d681SAndroid Build Coastguard Worker
865*9880d681SAndroid Build Coastguard Worker /// spillAroundUses - insert spill code around each use of Reg.
spillAroundUses(unsigned Reg)866*9880d681SAndroid Build Coastguard Worker void InlineSpiller::spillAroundUses(unsigned Reg) {
867*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "spillAroundUses " << PrintReg(Reg) << '\n');
868*9880d681SAndroid Build Coastguard Worker LiveInterval &OldLI = LIS.getInterval(Reg);
869*9880d681SAndroid Build Coastguard Worker
870*9880d681SAndroid Build Coastguard Worker // Iterate over instructions using Reg.
871*9880d681SAndroid Build Coastguard Worker for (MachineRegisterInfo::reg_bundle_iterator
872*9880d681SAndroid Build Coastguard Worker RegI = MRI.reg_bundle_begin(Reg), E = MRI.reg_bundle_end();
873*9880d681SAndroid Build Coastguard Worker RegI != E; ) {
874*9880d681SAndroid Build Coastguard Worker MachineInstr *MI = &*(RegI++);
875*9880d681SAndroid Build Coastguard Worker
876*9880d681SAndroid Build Coastguard Worker // Debug values are not allowed to affect codegen.
877*9880d681SAndroid Build Coastguard Worker if (MI->isDebugValue()) {
878*9880d681SAndroid Build Coastguard Worker // Modify DBG_VALUE now that the value is in a spill slot.
879*9880d681SAndroid Build Coastguard Worker bool IsIndirect = MI->isIndirectDebugValue();
880*9880d681SAndroid Build Coastguard Worker uint64_t Offset = IsIndirect ? MI->getOperand(1).getImm() : 0;
881*9880d681SAndroid Build Coastguard Worker const MDNode *Var = MI->getDebugVariable();
882*9880d681SAndroid Build Coastguard Worker const MDNode *Expr = MI->getDebugExpression();
883*9880d681SAndroid Build Coastguard Worker DebugLoc DL = MI->getDebugLoc();
884*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << *MI);
885*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = MI->getParent();
886*9880d681SAndroid Build Coastguard Worker assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
887*9880d681SAndroid Build Coastguard Worker "Expected inlined-at fields to agree");
888*9880d681SAndroid Build Coastguard Worker BuildMI(*MBB, MBB->erase(MI), DL, TII.get(TargetOpcode::DBG_VALUE))
889*9880d681SAndroid Build Coastguard Worker .addFrameIndex(StackSlot)
890*9880d681SAndroid Build Coastguard Worker .addImm(Offset)
891*9880d681SAndroid Build Coastguard Worker .addMetadata(Var)
892*9880d681SAndroid Build Coastguard Worker .addMetadata(Expr);
893*9880d681SAndroid Build Coastguard Worker continue;
894*9880d681SAndroid Build Coastguard Worker }
895*9880d681SAndroid Build Coastguard Worker
896*9880d681SAndroid Build Coastguard Worker // Ignore copies to/from snippets. We'll delete them.
897*9880d681SAndroid Build Coastguard Worker if (SnippetCopies.count(MI))
898*9880d681SAndroid Build Coastguard Worker continue;
899*9880d681SAndroid Build Coastguard Worker
900*9880d681SAndroid Build Coastguard Worker // Stack slot accesses may coalesce away.
901*9880d681SAndroid Build Coastguard Worker if (coalesceStackAccess(MI, Reg))
902*9880d681SAndroid Build Coastguard Worker continue;
903*9880d681SAndroid Build Coastguard Worker
904*9880d681SAndroid Build Coastguard Worker // Analyze instruction.
905*9880d681SAndroid Build Coastguard Worker SmallVector<std::pair<MachineInstr*, unsigned>, 8> Ops;
906*9880d681SAndroid Build Coastguard Worker MIBundleOperands::VirtRegInfo RI =
907*9880d681SAndroid Build Coastguard Worker MIBundleOperands(*MI).analyzeVirtReg(Reg, &Ops);
908*9880d681SAndroid Build Coastguard Worker
909*9880d681SAndroid Build Coastguard Worker // Find the slot index where this instruction reads and writes OldLI.
910*9880d681SAndroid Build Coastguard Worker // This is usually the def slot, except for tied early clobbers.
911*9880d681SAndroid Build Coastguard Worker SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot();
912*9880d681SAndroid Build Coastguard Worker if (VNInfo *VNI = OldLI.getVNInfoAt(Idx.getRegSlot(true)))
913*9880d681SAndroid Build Coastguard Worker if (SlotIndex::isSameInstr(Idx, VNI->def))
914*9880d681SAndroid Build Coastguard Worker Idx = VNI->def;
915*9880d681SAndroid Build Coastguard Worker
916*9880d681SAndroid Build Coastguard Worker // Check for a sibling copy.
917*9880d681SAndroid Build Coastguard Worker unsigned SibReg = isFullCopyOf(*MI, Reg);
918*9880d681SAndroid Build Coastguard Worker if (SibReg && isSibling(SibReg)) {
919*9880d681SAndroid Build Coastguard Worker // This may actually be a copy between snippets.
920*9880d681SAndroid Build Coastguard Worker if (isRegToSpill(SibReg)) {
921*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Found new snippet copy: " << *MI);
922*9880d681SAndroid Build Coastguard Worker SnippetCopies.insert(MI);
923*9880d681SAndroid Build Coastguard Worker continue;
924*9880d681SAndroid Build Coastguard Worker }
925*9880d681SAndroid Build Coastguard Worker if (RI.Writes) {
926*9880d681SAndroid Build Coastguard Worker if (hoistSpillInsideBB(OldLI, *MI)) {
927*9880d681SAndroid Build Coastguard Worker // This COPY is now dead, the value is already in the stack slot.
928*9880d681SAndroid Build Coastguard Worker MI->getOperand(0).setIsDead();
929*9880d681SAndroid Build Coastguard Worker DeadDefs.push_back(MI);
930*9880d681SAndroid Build Coastguard Worker continue;
931*9880d681SAndroid Build Coastguard Worker }
932*9880d681SAndroid Build Coastguard Worker } else {
933*9880d681SAndroid Build Coastguard Worker // This is a reload for a sib-reg copy. Drop spills downstream.
934*9880d681SAndroid Build Coastguard Worker LiveInterval &SibLI = LIS.getInterval(SibReg);
935*9880d681SAndroid Build Coastguard Worker eliminateRedundantSpills(SibLI, SibLI.getVNInfoAt(Idx));
936*9880d681SAndroid Build Coastguard Worker // The COPY will fold to a reload below.
937*9880d681SAndroid Build Coastguard Worker }
938*9880d681SAndroid Build Coastguard Worker }
939*9880d681SAndroid Build Coastguard Worker
940*9880d681SAndroid Build Coastguard Worker // Attempt to fold memory ops.
941*9880d681SAndroid Build Coastguard Worker if (foldMemoryOperand(Ops))
942*9880d681SAndroid Build Coastguard Worker continue;
943*9880d681SAndroid Build Coastguard Worker
944*9880d681SAndroid Build Coastguard Worker // Create a new virtual register for spill/fill.
945*9880d681SAndroid Build Coastguard Worker // FIXME: Infer regclass from instruction alone.
946*9880d681SAndroid Build Coastguard Worker unsigned NewVReg = Edit->createFrom(Reg);
947*9880d681SAndroid Build Coastguard Worker
948*9880d681SAndroid Build Coastguard Worker if (RI.Reads)
949*9880d681SAndroid Build Coastguard Worker insertReload(NewVReg, Idx, MI);
950*9880d681SAndroid Build Coastguard Worker
951*9880d681SAndroid Build Coastguard Worker // Rewrite instruction operands.
952*9880d681SAndroid Build Coastguard Worker bool hasLiveDef = false;
953*9880d681SAndroid Build Coastguard Worker for (const auto &OpPair : Ops) {
954*9880d681SAndroid Build Coastguard Worker MachineOperand &MO = OpPair.first->getOperand(OpPair.second);
955*9880d681SAndroid Build Coastguard Worker MO.setReg(NewVReg);
956*9880d681SAndroid Build Coastguard Worker if (MO.isUse()) {
957*9880d681SAndroid Build Coastguard Worker if (!OpPair.first->isRegTiedToDefOperand(OpPair.second))
958*9880d681SAndroid Build Coastguard Worker MO.setIsKill();
959*9880d681SAndroid Build Coastguard Worker } else {
960*9880d681SAndroid Build Coastguard Worker if (!MO.isDead())
961*9880d681SAndroid Build Coastguard Worker hasLiveDef = true;
962*9880d681SAndroid Build Coastguard Worker }
963*9880d681SAndroid Build Coastguard Worker }
964*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "\trewrite: " << Idx << '\t' << *MI << '\n');
965*9880d681SAndroid Build Coastguard Worker
966*9880d681SAndroid Build Coastguard Worker // FIXME: Use a second vreg if instruction has no tied ops.
967*9880d681SAndroid Build Coastguard Worker if (RI.Writes)
968*9880d681SAndroid Build Coastguard Worker if (hasLiveDef)
969*9880d681SAndroid Build Coastguard Worker insertSpill(NewVReg, true, MI);
970*9880d681SAndroid Build Coastguard Worker }
971*9880d681SAndroid Build Coastguard Worker }
972*9880d681SAndroid Build Coastguard Worker
973*9880d681SAndroid Build Coastguard Worker /// spillAll - Spill all registers remaining after rematerialization.
spillAll()974*9880d681SAndroid Build Coastguard Worker void InlineSpiller::spillAll() {
975*9880d681SAndroid Build Coastguard Worker // Update LiveStacks now that we are committed to spilling.
976*9880d681SAndroid Build Coastguard Worker if (StackSlot == VirtRegMap::NO_STACK_SLOT) {
977*9880d681SAndroid Build Coastguard Worker StackSlot = VRM.assignVirt2StackSlot(Original);
978*9880d681SAndroid Build Coastguard Worker StackInt = &LSS.getOrCreateInterval(StackSlot, MRI.getRegClass(Original));
979*9880d681SAndroid Build Coastguard Worker StackInt->getNextValue(SlotIndex(), LSS.getVNInfoAllocator());
980*9880d681SAndroid Build Coastguard Worker } else
981*9880d681SAndroid Build Coastguard Worker StackInt = &LSS.getInterval(StackSlot);
982*9880d681SAndroid Build Coastguard Worker
983*9880d681SAndroid Build Coastguard Worker if (Original != Edit->getReg())
984*9880d681SAndroid Build Coastguard Worker VRM.assignVirt2StackSlot(Edit->getReg(), StackSlot);
985*9880d681SAndroid Build Coastguard Worker
986*9880d681SAndroid Build Coastguard Worker assert(StackInt->getNumValNums() == 1 && "Bad stack interval values");
987*9880d681SAndroid Build Coastguard Worker for (unsigned Reg : RegsToSpill)
988*9880d681SAndroid Build Coastguard Worker StackInt->MergeSegmentsInAsValue(LIS.getInterval(Reg),
989*9880d681SAndroid Build Coastguard Worker StackInt->getValNumInfo(0));
990*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Merged spilled regs: " << *StackInt << '\n');
991*9880d681SAndroid Build Coastguard Worker
992*9880d681SAndroid Build Coastguard Worker // Spill around uses of all RegsToSpill.
993*9880d681SAndroid Build Coastguard Worker for (unsigned Reg : RegsToSpill)
994*9880d681SAndroid Build Coastguard Worker spillAroundUses(Reg);
995*9880d681SAndroid Build Coastguard Worker
996*9880d681SAndroid Build Coastguard Worker // Hoisted spills may cause dead code.
997*9880d681SAndroid Build Coastguard Worker if (!DeadDefs.empty()) {
998*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Eliminating " << DeadDefs.size() << " dead defs\n");
999*9880d681SAndroid Build Coastguard Worker Edit->eliminateDeadDefs(DeadDefs, RegsToSpill, AA);
1000*9880d681SAndroid Build Coastguard Worker }
1001*9880d681SAndroid Build Coastguard Worker
1002*9880d681SAndroid Build Coastguard Worker // Finally delete the SnippetCopies.
1003*9880d681SAndroid Build Coastguard Worker for (unsigned Reg : RegsToSpill) {
1004*9880d681SAndroid Build Coastguard Worker for (MachineRegisterInfo::reg_instr_iterator
1005*9880d681SAndroid Build Coastguard Worker RI = MRI.reg_instr_begin(Reg), E = MRI.reg_instr_end();
1006*9880d681SAndroid Build Coastguard Worker RI != E; ) {
1007*9880d681SAndroid Build Coastguard Worker MachineInstr &MI = *(RI++);
1008*9880d681SAndroid Build Coastguard Worker assert(SnippetCopies.count(&MI) && "Remaining use wasn't a snippet copy");
1009*9880d681SAndroid Build Coastguard Worker // FIXME: Do this with a LiveRangeEdit callback.
1010*9880d681SAndroid Build Coastguard Worker LIS.RemoveMachineInstrFromMaps(MI);
1011*9880d681SAndroid Build Coastguard Worker MI.eraseFromParent();
1012*9880d681SAndroid Build Coastguard Worker }
1013*9880d681SAndroid Build Coastguard Worker }
1014*9880d681SAndroid Build Coastguard Worker
1015*9880d681SAndroid Build Coastguard Worker // Delete all spilled registers.
1016*9880d681SAndroid Build Coastguard Worker for (unsigned Reg : RegsToSpill)
1017*9880d681SAndroid Build Coastguard Worker Edit->eraseVirtReg(Reg);
1018*9880d681SAndroid Build Coastguard Worker }
1019*9880d681SAndroid Build Coastguard Worker
spill(LiveRangeEdit & edit)1020*9880d681SAndroid Build Coastguard Worker void InlineSpiller::spill(LiveRangeEdit &edit) {
1021*9880d681SAndroid Build Coastguard Worker ++NumSpilledRanges;
1022*9880d681SAndroid Build Coastguard Worker Edit = &edit;
1023*9880d681SAndroid Build Coastguard Worker assert(!TargetRegisterInfo::isStackSlot(edit.getReg())
1024*9880d681SAndroid Build Coastguard Worker && "Trying to spill a stack slot.");
1025*9880d681SAndroid Build Coastguard Worker // Share a stack slot among all descendants of Original.
1026*9880d681SAndroid Build Coastguard Worker Original = VRM.getOriginal(edit.getReg());
1027*9880d681SAndroid Build Coastguard Worker StackSlot = VRM.getStackSlot(Original);
1028*9880d681SAndroid Build Coastguard Worker StackInt = nullptr;
1029*9880d681SAndroid Build Coastguard Worker
1030*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Inline spilling "
1031*9880d681SAndroid Build Coastguard Worker << TRI.getRegClassName(MRI.getRegClass(edit.getReg()))
1032*9880d681SAndroid Build Coastguard Worker << ':' << edit.getParent()
1033*9880d681SAndroid Build Coastguard Worker << "\nFrom original " << PrintReg(Original) << '\n');
1034*9880d681SAndroid Build Coastguard Worker assert(edit.getParent().isSpillable() &&
1035*9880d681SAndroid Build Coastguard Worker "Attempting to spill already spilled value.");
1036*9880d681SAndroid Build Coastguard Worker assert(DeadDefs.empty() && "Previous spill didn't remove dead defs");
1037*9880d681SAndroid Build Coastguard Worker
1038*9880d681SAndroid Build Coastguard Worker collectRegsToSpill();
1039*9880d681SAndroid Build Coastguard Worker reMaterializeAll();
1040*9880d681SAndroid Build Coastguard Worker
1041*9880d681SAndroid Build Coastguard Worker // Remat may handle everything.
1042*9880d681SAndroid Build Coastguard Worker if (!RegsToSpill.empty())
1043*9880d681SAndroid Build Coastguard Worker spillAll();
1044*9880d681SAndroid Build Coastguard Worker
1045*9880d681SAndroid Build Coastguard Worker Edit->calculateRegClassAndHint(MF, Loops, MBFI);
1046*9880d681SAndroid Build Coastguard Worker }
1047*9880d681SAndroid Build Coastguard Worker
1048*9880d681SAndroid Build Coastguard Worker /// Optimizations after all the reg selections and spills are done.
1049*9880d681SAndroid Build Coastguard Worker ///
postOptimization()1050*9880d681SAndroid Build Coastguard Worker void InlineSpiller::postOptimization() { HSpiller.hoistAllSpills(); }
1051*9880d681SAndroid Build Coastguard Worker
1052*9880d681SAndroid Build Coastguard Worker /// When a spill is inserted, add the spill to MergeableSpills map.
1053*9880d681SAndroid Build Coastguard Worker ///
addToMergeableSpills(MachineInstr & Spill,int StackSlot,unsigned Original)1054*9880d681SAndroid Build Coastguard Worker void HoistSpillHelper::addToMergeableSpills(MachineInstr &Spill, int StackSlot,
1055*9880d681SAndroid Build Coastguard Worker unsigned Original) {
1056*9880d681SAndroid Build Coastguard Worker StackSlotToReg[StackSlot] = Original;
1057*9880d681SAndroid Build Coastguard Worker SlotIndex Idx = LIS.getInstructionIndex(Spill);
1058*9880d681SAndroid Build Coastguard Worker VNInfo *OrigVNI = LIS.getInterval(Original).getVNInfoAt(Idx.getRegSlot());
1059*9880d681SAndroid Build Coastguard Worker std::pair<int, VNInfo *> MIdx = std::make_pair(StackSlot, OrigVNI);
1060*9880d681SAndroid Build Coastguard Worker MergeableSpills[MIdx].insert(&Spill);
1061*9880d681SAndroid Build Coastguard Worker }
1062*9880d681SAndroid Build Coastguard Worker
1063*9880d681SAndroid Build Coastguard Worker /// When a spill is removed, remove the spill from MergeableSpills map.
1064*9880d681SAndroid Build Coastguard Worker /// Return true if the spill is removed successfully.
1065*9880d681SAndroid Build Coastguard Worker ///
rmFromMergeableSpills(MachineInstr & Spill,int StackSlot)1066*9880d681SAndroid Build Coastguard Worker bool HoistSpillHelper::rmFromMergeableSpills(MachineInstr &Spill,
1067*9880d681SAndroid Build Coastguard Worker int StackSlot) {
1068*9880d681SAndroid Build Coastguard Worker int Original = StackSlotToReg[StackSlot];
1069*9880d681SAndroid Build Coastguard Worker if (!Original)
1070*9880d681SAndroid Build Coastguard Worker return false;
1071*9880d681SAndroid Build Coastguard Worker SlotIndex Idx = LIS.getInstructionIndex(Spill);
1072*9880d681SAndroid Build Coastguard Worker VNInfo *OrigVNI = LIS.getInterval(Original).getVNInfoAt(Idx.getRegSlot());
1073*9880d681SAndroid Build Coastguard Worker std::pair<int, VNInfo *> MIdx = std::make_pair(StackSlot, OrigVNI);
1074*9880d681SAndroid Build Coastguard Worker return MergeableSpills[MIdx].erase(&Spill);
1075*9880d681SAndroid Build Coastguard Worker }
1076*9880d681SAndroid Build Coastguard Worker
1077*9880d681SAndroid Build Coastguard Worker /// Check BB to see if it is a possible target BB to place a hoisted spill,
1078*9880d681SAndroid Build Coastguard Worker /// i.e., there should be a living sibling of OrigReg at the insert point.
1079*9880d681SAndroid Build Coastguard Worker ///
isSpillCandBB(unsigned OrigReg,VNInfo & OrigVNI,MachineBasicBlock & BB,unsigned & LiveReg)1080*9880d681SAndroid Build Coastguard Worker bool HoistSpillHelper::isSpillCandBB(unsigned OrigReg, VNInfo &OrigVNI,
1081*9880d681SAndroid Build Coastguard Worker MachineBasicBlock &BB, unsigned &LiveReg) {
1082*9880d681SAndroid Build Coastguard Worker SlotIndex Idx;
1083*9880d681SAndroid Build Coastguard Worker LiveInterval &OrigLI = LIS.getInterval(OrigReg);
1084*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator MI = IPA.getLastInsertPointIter(OrigLI, BB);
1085*9880d681SAndroid Build Coastguard Worker if (MI != BB.end())
1086*9880d681SAndroid Build Coastguard Worker Idx = LIS.getInstructionIndex(*MI);
1087*9880d681SAndroid Build Coastguard Worker else
1088*9880d681SAndroid Build Coastguard Worker Idx = LIS.getMBBEndIdx(&BB).getPrevSlot();
1089*9880d681SAndroid Build Coastguard Worker SmallSetVector<unsigned, 16> &Siblings = Virt2SiblingsMap[OrigReg];
1090*9880d681SAndroid Build Coastguard Worker assert((LIS.getInterval(OrigReg)).getVNInfoAt(Idx) == &OrigVNI &&
1091*9880d681SAndroid Build Coastguard Worker "Unexpected VNI");
1092*9880d681SAndroid Build Coastguard Worker
1093*9880d681SAndroid Build Coastguard Worker for (auto const SibReg : Siblings) {
1094*9880d681SAndroid Build Coastguard Worker LiveInterval &LI = LIS.getInterval(SibReg);
1095*9880d681SAndroid Build Coastguard Worker VNInfo *VNI = LI.getVNInfoAt(Idx);
1096*9880d681SAndroid Build Coastguard Worker if (VNI) {
1097*9880d681SAndroid Build Coastguard Worker LiveReg = SibReg;
1098*9880d681SAndroid Build Coastguard Worker return true;
1099*9880d681SAndroid Build Coastguard Worker }
1100*9880d681SAndroid Build Coastguard Worker }
1101*9880d681SAndroid Build Coastguard Worker return false;
1102*9880d681SAndroid Build Coastguard Worker }
1103*9880d681SAndroid Build Coastguard Worker
1104*9880d681SAndroid Build Coastguard Worker /// Remove redundant spills in the same BB. Save those redundant spills in
1105*9880d681SAndroid Build Coastguard Worker /// SpillsToRm, and save the spill to keep and its BB in SpillBBToSpill map.
1106*9880d681SAndroid Build Coastguard Worker ///
rmRedundantSpills(SmallPtrSet<MachineInstr *,16> & Spills,SmallVectorImpl<MachineInstr * > & SpillsToRm,DenseMap<MachineDomTreeNode *,MachineInstr * > & SpillBBToSpill)1107*9880d681SAndroid Build Coastguard Worker void HoistSpillHelper::rmRedundantSpills(
1108*9880d681SAndroid Build Coastguard Worker SmallPtrSet<MachineInstr *, 16> &Spills,
1109*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<MachineInstr *> &SpillsToRm,
1110*9880d681SAndroid Build Coastguard Worker DenseMap<MachineDomTreeNode *, MachineInstr *> &SpillBBToSpill) {
1111*9880d681SAndroid Build Coastguard Worker // For each spill saw, check SpillBBToSpill[] and see if its BB already has
1112*9880d681SAndroid Build Coastguard Worker // another spill inside. If a BB contains more than one spill, only keep the
1113*9880d681SAndroid Build Coastguard Worker // earlier spill with smaller SlotIndex.
1114*9880d681SAndroid Build Coastguard Worker for (const auto CurrentSpill : Spills) {
1115*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *Block = CurrentSpill->getParent();
1116*9880d681SAndroid Build Coastguard Worker MachineDomTreeNode *Node = MDT.DT->getNode(Block);
1117*9880d681SAndroid Build Coastguard Worker MachineInstr *PrevSpill = SpillBBToSpill[Node];
1118*9880d681SAndroid Build Coastguard Worker if (PrevSpill) {
1119*9880d681SAndroid Build Coastguard Worker SlotIndex PIdx = LIS.getInstructionIndex(*PrevSpill);
1120*9880d681SAndroid Build Coastguard Worker SlotIndex CIdx = LIS.getInstructionIndex(*CurrentSpill);
1121*9880d681SAndroid Build Coastguard Worker MachineInstr *SpillToRm = (CIdx > PIdx) ? CurrentSpill : PrevSpill;
1122*9880d681SAndroid Build Coastguard Worker MachineInstr *SpillToKeep = (CIdx > PIdx) ? PrevSpill : CurrentSpill;
1123*9880d681SAndroid Build Coastguard Worker SpillsToRm.push_back(SpillToRm);
1124*9880d681SAndroid Build Coastguard Worker SpillBBToSpill[MDT.DT->getNode(Block)] = SpillToKeep;
1125*9880d681SAndroid Build Coastguard Worker } else {
1126*9880d681SAndroid Build Coastguard Worker SpillBBToSpill[MDT.DT->getNode(Block)] = CurrentSpill;
1127*9880d681SAndroid Build Coastguard Worker }
1128*9880d681SAndroid Build Coastguard Worker }
1129*9880d681SAndroid Build Coastguard Worker for (const auto SpillToRm : SpillsToRm)
1130*9880d681SAndroid Build Coastguard Worker Spills.erase(SpillToRm);
1131*9880d681SAndroid Build Coastguard Worker }
1132*9880d681SAndroid Build Coastguard Worker
1133*9880d681SAndroid Build Coastguard Worker /// Starting from \p Root find a top-down traversal order of the dominator
1134*9880d681SAndroid Build Coastguard Worker /// tree to visit all basic blocks containing the elements of \p Spills.
1135*9880d681SAndroid Build Coastguard Worker /// Redundant spills will be found and put into \p SpillsToRm at the same
1136*9880d681SAndroid Build Coastguard Worker /// time. \p SpillBBToSpill will be populated as part of the process and
1137*9880d681SAndroid Build Coastguard Worker /// maps a basic block to the first store occurring in the basic block.
1138*9880d681SAndroid Build Coastguard Worker /// \post SpillsToRm.union(Spills\@post) == Spills\@pre
1139*9880d681SAndroid Build Coastguard Worker ///
getVisitOrders(MachineBasicBlock * Root,SmallPtrSet<MachineInstr *,16> & Spills,SmallVectorImpl<MachineDomTreeNode * > & Orders,SmallVectorImpl<MachineInstr * > & SpillsToRm,DenseMap<MachineDomTreeNode *,unsigned> & SpillsToKeep,DenseMap<MachineDomTreeNode *,MachineInstr * > & SpillBBToSpill)1140*9880d681SAndroid Build Coastguard Worker void HoistSpillHelper::getVisitOrders(
1141*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *Root, SmallPtrSet<MachineInstr *, 16> &Spills,
1142*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<MachineDomTreeNode *> &Orders,
1143*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<MachineInstr *> &SpillsToRm,
1144*9880d681SAndroid Build Coastguard Worker DenseMap<MachineDomTreeNode *, unsigned> &SpillsToKeep,
1145*9880d681SAndroid Build Coastguard Worker DenseMap<MachineDomTreeNode *, MachineInstr *> &SpillBBToSpill) {
1146*9880d681SAndroid Build Coastguard Worker // The set contains all the possible BB nodes to which we may hoist
1147*9880d681SAndroid Build Coastguard Worker // original spills.
1148*9880d681SAndroid Build Coastguard Worker SmallPtrSet<MachineDomTreeNode *, 8> WorkSet;
1149*9880d681SAndroid Build Coastguard Worker // Save the BB nodes on the path from the first BB node containing
1150*9880d681SAndroid Build Coastguard Worker // non-redundant spill to the Root node.
1151*9880d681SAndroid Build Coastguard Worker SmallPtrSet<MachineDomTreeNode *, 8> NodesOnPath;
1152*9880d681SAndroid Build Coastguard Worker // All the spills to be hoisted must originate from a single def instruction
1153*9880d681SAndroid Build Coastguard Worker // to the OrigReg. It means the def instruction should dominate all the spills
1154*9880d681SAndroid Build Coastguard Worker // to be hoisted. We choose the BB where the def instruction is located as
1155*9880d681SAndroid Build Coastguard Worker // the Root.
1156*9880d681SAndroid Build Coastguard Worker MachineDomTreeNode *RootIDomNode = MDT[Root]->getIDom();
1157*9880d681SAndroid Build Coastguard Worker // For every node on the dominator tree with spill, walk up on the dominator
1158*9880d681SAndroid Build Coastguard Worker // tree towards the Root node until it is reached. If there is other node
1159*9880d681SAndroid Build Coastguard Worker // containing spill in the middle of the path, the previous spill saw will
1160*9880d681SAndroid Build Coastguard Worker // be redundant and the node containing it will be removed. All the nodes on
1161*9880d681SAndroid Build Coastguard Worker // the path starting from the first node with non-redundant spill to the Root
1162*9880d681SAndroid Build Coastguard Worker // node will be added to the WorkSet, which will contain all the possible
1163*9880d681SAndroid Build Coastguard Worker // locations where spills may be hoisted to after the loop below is done.
1164*9880d681SAndroid Build Coastguard Worker for (const auto Spill : Spills) {
1165*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *Block = Spill->getParent();
1166*9880d681SAndroid Build Coastguard Worker MachineDomTreeNode *Node = MDT[Block];
1167*9880d681SAndroid Build Coastguard Worker MachineInstr *SpillToRm = nullptr;
1168*9880d681SAndroid Build Coastguard Worker while (Node != RootIDomNode) {
1169*9880d681SAndroid Build Coastguard Worker // If Node dominates Block, and it already contains a spill, the spill in
1170*9880d681SAndroid Build Coastguard Worker // Block will be redundant.
1171*9880d681SAndroid Build Coastguard Worker if (Node != MDT[Block] && SpillBBToSpill[Node]) {
1172*9880d681SAndroid Build Coastguard Worker SpillToRm = SpillBBToSpill[MDT[Block]];
1173*9880d681SAndroid Build Coastguard Worker break;
1174*9880d681SAndroid Build Coastguard Worker /// If we see the Node already in WorkSet, the path from the Node to
1175*9880d681SAndroid Build Coastguard Worker /// the Root node must already be traversed by another spill.
1176*9880d681SAndroid Build Coastguard Worker /// Then no need to repeat.
1177*9880d681SAndroid Build Coastguard Worker } else if (WorkSet.count(Node)) {
1178*9880d681SAndroid Build Coastguard Worker break;
1179*9880d681SAndroid Build Coastguard Worker } else {
1180*9880d681SAndroid Build Coastguard Worker NodesOnPath.insert(Node);
1181*9880d681SAndroid Build Coastguard Worker }
1182*9880d681SAndroid Build Coastguard Worker Node = Node->getIDom();
1183*9880d681SAndroid Build Coastguard Worker }
1184*9880d681SAndroid Build Coastguard Worker if (SpillToRm) {
1185*9880d681SAndroid Build Coastguard Worker SpillsToRm.push_back(SpillToRm);
1186*9880d681SAndroid Build Coastguard Worker } else {
1187*9880d681SAndroid Build Coastguard Worker // Add a BB containing the original spills to SpillsToKeep -- i.e.,
1188*9880d681SAndroid Build Coastguard Worker // set the initial status before hoisting start. The value of BBs
1189*9880d681SAndroid Build Coastguard Worker // containing original spills is set to 0, in order to descriminate
1190*9880d681SAndroid Build Coastguard Worker // with BBs containing hoisted spills which will be inserted to
1191*9880d681SAndroid Build Coastguard Worker // SpillsToKeep later during hoisting.
1192*9880d681SAndroid Build Coastguard Worker SpillsToKeep[MDT[Block]] = 0;
1193*9880d681SAndroid Build Coastguard Worker WorkSet.insert(NodesOnPath.begin(), NodesOnPath.end());
1194*9880d681SAndroid Build Coastguard Worker }
1195*9880d681SAndroid Build Coastguard Worker NodesOnPath.clear();
1196*9880d681SAndroid Build Coastguard Worker }
1197*9880d681SAndroid Build Coastguard Worker
1198*9880d681SAndroid Build Coastguard Worker // Sort the nodes in WorkSet in top-down order and save the nodes
1199*9880d681SAndroid Build Coastguard Worker // in Orders. Orders will be used for hoisting in runHoistSpills.
1200*9880d681SAndroid Build Coastguard Worker unsigned idx = 0;
1201*9880d681SAndroid Build Coastguard Worker Orders.push_back(MDT.DT->getNode(Root));
1202*9880d681SAndroid Build Coastguard Worker do {
1203*9880d681SAndroid Build Coastguard Worker MachineDomTreeNode *Node = Orders[idx++];
1204*9880d681SAndroid Build Coastguard Worker const std::vector<MachineDomTreeNode *> &Children = Node->getChildren();
1205*9880d681SAndroid Build Coastguard Worker unsigned NumChildren = Children.size();
1206*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumChildren; ++i) {
1207*9880d681SAndroid Build Coastguard Worker MachineDomTreeNode *Child = Children[i];
1208*9880d681SAndroid Build Coastguard Worker if (WorkSet.count(Child))
1209*9880d681SAndroid Build Coastguard Worker Orders.push_back(Child);
1210*9880d681SAndroid Build Coastguard Worker }
1211*9880d681SAndroid Build Coastguard Worker } while (idx != Orders.size());
1212*9880d681SAndroid Build Coastguard Worker assert(Orders.size() == WorkSet.size() &&
1213*9880d681SAndroid Build Coastguard Worker "Orders have different size with WorkSet");
1214*9880d681SAndroid Build Coastguard Worker
1215*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
1216*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Orders size is " << Orders.size() << "\n");
1217*9880d681SAndroid Build Coastguard Worker SmallVector<MachineDomTreeNode *, 32>::reverse_iterator RIt = Orders.rbegin();
1218*9880d681SAndroid Build Coastguard Worker for (; RIt != Orders.rend(); RIt++)
1219*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "BB" << (*RIt)->getBlock()->getNumber() << ",");
1220*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "\n");
1221*9880d681SAndroid Build Coastguard Worker #endif
1222*9880d681SAndroid Build Coastguard Worker }
1223*9880d681SAndroid Build Coastguard Worker
1224*9880d681SAndroid Build Coastguard Worker /// Try to hoist spills according to BB hotness. The spills to removed will
1225*9880d681SAndroid Build Coastguard Worker /// be saved in \p SpillsToRm. The spills to be inserted will be saved in
1226*9880d681SAndroid Build Coastguard Worker /// \p SpillsToIns.
1227*9880d681SAndroid Build Coastguard Worker ///
runHoistSpills(unsigned OrigReg,VNInfo & OrigVNI,SmallPtrSet<MachineInstr *,16> & Spills,SmallVectorImpl<MachineInstr * > & SpillsToRm,DenseMap<MachineBasicBlock *,unsigned> & SpillsToIns)1228*9880d681SAndroid Build Coastguard Worker void HoistSpillHelper::runHoistSpills(
1229*9880d681SAndroid Build Coastguard Worker unsigned OrigReg, VNInfo &OrigVNI, SmallPtrSet<MachineInstr *, 16> &Spills,
1230*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<MachineInstr *> &SpillsToRm,
1231*9880d681SAndroid Build Coastguard Worker DenseMap<MachineBasicBlock *, unsigned> &SpillsToIns) {
1232*9880d681SAndroid Build Coastguard Worker // Visit order of dominator tree nodes.
1233*9880d681SAndroid Build Coastguard Worker SmallVector<MachineDomTreeNode *, 32> Orders;
1234*9880d681SAndroid Build Coastguard Worker // SpillsToKeep contains all the nodes where spills are to be inserted
1235*9880d681SAndroid Build Coastguard Worker // during hoisting. If the spill to be inserted is an original spill
1236*9880d681SAndroid Build Coastguard Worker // (not a hoisted one), the value of the map entry is 0. If the spill
1237*9880d681SAndroid Build Coastguard Worker // is a hoisted spill, the value of the map entry is the VReg to be used
1238*9880d681SAndroid Build Coastguard Worker // as the source of the spill.
1239*9880d681SAndroid Build Coastguard Worker DenseMap<MachineDomTreeNode *, unsigned> SpillsToKeep;
1240*9880d681SAndroid Build Coastguard Worker // Map from BB to the first spill inside of it.
1241*9880d681SAndroid Build Coastguard Worker DenseMap<MachineDomTreeNode *, MachineInstr *> SpillBBToSpill;
1242*9880d681SAndroid Build Coastguard Worker
1243*9880d681SAndroid Build Coastguard Worker rmRedundantSpills(Spills, SpillsToRm, SpillBBToSpill);
1244*9880d681SAndroid Build Coastguard Worker
1245*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *Root = LIS.getMBBFromIndex(OrigVNI.def);
1246*9880d681SAndroid Build Coastguard Worker getVisitOrders(Root, Spills, Orders, SpillsToRm, SpillsToKeep,
1247*9880d681SAndroid Build Coastguard Worker SpillBBToSpill);
1248*9880d681SAndroid Build Coastguard Worker
1249*9880d681SAndroid Build Coastguard Worker // SpillsInSubTreeMap keeps the map from a dom tree node to a pair of
1250*9880d681SAndroid Build Coastguard Worker // nodes set and the cost of all the spills inside those nodes.
1251*9880d681SAndroid Build Coastguard Worker // The nodes set are the locations where spills are to be inserted
1252*9880d681SAndroid Build Coastguard Worker // in the subtree of current node.
1253*9880d681SAndroid Build Coastguard Worker typedef std::pair<SmallPtrSet<MachineDomTreeNode *, 16>, BlockFrequency>
1254*9880d681SAndroid Build Coastguard Worker NodesCostPair;
1255*9880d681SAndroid Build Coastguard Worker DenseMap<MachineDomTreeNode *, NodesCostPair> SpillsInSubTreeMap;
1256*9880d681SAndroid Build Coastguard Worker // Iterate Orders set in reverse order, which will be a bottom-up order
1257*9880d681SAndroid Build Coastguard Worker // in the dominator tree. Once we visit a dom tree node, we know its
1258*9880d681SAndroid Build Coastguard Worker // children have already been visited and the spill locations in the
1259*9880d681SAndroid Build Coastguard Worker // subtrees of all the children have been determined.
1260*9880d681SAndroid Build Coastguard Worker SmallVector<MachineDomTreeNode *, 32>::reverse_iterator RIt = Orders.rbegin();
1261*9880d681SAndroid Build Coastguard Worker for (; RIt != Orders.rend(); RIt++) {
1262*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *Block = (*RIt)->getBlock();
1263*9880d681SAndroid Build Coastguard Worker
1264*9880d681SAndroid Build Coastguard Worker // If Block contains an original spill, simply continue.
1265*9880d681SAndroid Build Coastguard Worker if (SpillsToKeep.find(*RIt) != SpillsToKeep.end() && !SpillsToKeep[*RIt]) {
1266*9880d681SAndroid Build Coastguard Worker SpillsInSubTreeMap[*RIt].first.insert(*RIt);
1267*9880d681SAndroid Build Coastguard Worker // SpillsInSubTreeMap[*RIt].second contains the cost of spill.
1268*9880d681SAndroid Build Coastguard Worker SpillsInSubTreeMap[*RIt].second = MBFI.getBlockFreq(Block);
1269*9880d681SAndroid Build Coastguard Worker continue;
1270*9880d681SAndroid Build Coastguard Worker }
1271*9880d681SAndroid Build Coastguard Worker
1272*9880d681SAndroid Build Coastguard Worker // Collect spills in subtree of current node (*RIt) to
1273*9880d681SAndroid Build Coastguard Worker // SpillsInSubTreeMap[*RIt].first.
1274*9880d681SAndroid Build Coastguard Worker const std::vector<MachineDomTreeNode *> &Children = (*RIt)->getChildren();
1275*9880d681SAndroid Build Coastguard Worker unsigned NumChildren = Children.size();
1276*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumChildren; ++i) {
1277*9880d681SAndroid Build Coastguard Worker MachineDomTreeNode *Child = Children[i];
1278*9880d681SAndroid Build Coastguard Worker if (SpillsInSubTreeMap.find(Child) == SpillsInSubTreeMap.end())
1279*9880d681SAndroid Build Coastguard Worker continue;
1280*9880d681SAndroid Build Coastguard Worker // The stmt "SpillsInSubTree = SpillsInSubTreeMap[*RIt].first" below
1281*9880d681SAndroid Build Coastguard Worker // should be placed before getting the begin and end iterators of
1282*9880d681SAndroid Build Coastguard Worker // SpillsInSubTreeMap[Child].first, or else the iterators may be
1283*9880d681SAndroid Build Coastguard Worker // invalidated when SpillsInSubTreeMap[*RIt] is seen the first time
1284*9880d681SAndroid Build Coastguard Worker // and the map grows and then the original buckets in the map are moved.
1285*9880d681SAndroid Build Coastguard Worker SmallPtrSet<MachineDomTreeNode *, 16> &SpillsInSubTree =
1286*9880d681SAndroid Build Coastguard Worker SpillsInSubTreeMap[*RIt].first;
1287*9880d681SAndroid Build Coastguard Worker BlockFrequency &SubTreeCost = SpillsInSubTreeMap[*RIt].second;
1288*9880d681SAndroid Build Coastguard Worker SubTreeCost += SpillsInSubTreeMap[Child].second;
1289*9880d681SAndroid Build Coastguard Worker auto BI = SpillsInSubTreeMap[Child].first.begin();
1290*9880d681SAndroid Build Coastguard Worker auto EI = SpillsInSubTreeMap[Child].first.end();
1291*9880d681SAndroid Build Coastguard Worker SpillsInSubTree.insert(BI, EI);
1292*9880d681SAndroid Build Coastguard Worker SpillsInSubTreeMap.erase(Child);
1293*9880d681SAndroid Build Coastguard Worker }
1294*9880d681SAndroid Build Coastguard Worker
1295*9880d681SAndroid Build Coastguard Worker SmallPtrSet<MachineDomTreeNode *, 16> &SpillsInSubTree =
1296*9880d681SAndroid Build Coastguard Worker SpillsInSubTreeMap[*RIt].first;
1297*9880d681SAndroid Build Coastguard Worker BlockFrequency &SubTreeCost = SpillsInSubTreeMap[*RIt].second;
1298*9880d681SAndroid Build Coastguard Worker // No spills in subtree, simply continue.
1299*9880d681SAndroid Build Coastguard Worker if (SpillsInSubTree.empty())
1300*9880d681SAndroid Build Coastguard Worker continue;
1301*9880d681SAndroid Build Coastguard Worker
1302*9880d681SAndroid Build Coastguard Worker // Check whether Block is a possible candidate to insert spill.
1303*9880d681SAndroid Build Coastguard Worker unsigned LiveReg = 0;
1304*9880d681SAndroid Build Coastguard Worker if (!isSpillCandBB(OrigReg, OrigVNI, *Block, LiveReg))
1305*9880d681SAndroid Build Coastguard Worker continue;
1306*9880d681SAndroid Build Coastguard Worker
1307*9880d681SAndroid Build Coastguard Worker // If there are multiple spills that could be merged, bias a little
1308*9880d681SAndroid Build Coastguard Worker // to hoist the spill.
1309*9880d681SAndroid Build Coastguard Worker BranchProbability MarginProb = (SpillsInSubTree.size() > 1)
1310*9880d681SAndroid Build Coastguard Worker ? BranchProbability(9, 10)
1311*9880d681SAndroid Build Coastguard Worker : BranchProbability(1, 1);
1312*9880d681SAndroid Build Coastguard Worker if (SubTreeCost > MBFI.getBlockFreq(Block) * MarginProb) {
1313*9880d681SAndroid Build Coastguard Worker // Hoist: Move spills to current Block.
1314*9880d681SAndroid Build Coastguard Worker for (const auto SpillBB : SpillsInSubTree) {
1315*9880d681SAndroid Build Coastguard Worker // When SpillBB is a BB contains original spill, insert the spill
1316*9880d681SAndroid Build Coastguard Worker // to SpillsToRm.
1317*9880d681SAndroid Build Coastguard Worker if (SpillsToKeep.find(SpillBB) != SpillsToKeep.end() &&
1318*9880d681SAndroid Build Coastguard Worker !SpillsToKeep[SpillBB]) {
1319*9880d681SAndroid Build Coastguard Worker MachineInstr *SpillToRm = SpillBBToSpill[SpillBB];
1320*9880d681SAndroid Build Coastguard Worker SpillsToRm.push_back(SpillToRm);
1321*9880d681SAndroid Build Coastguard Worker }
1322*9880d681SAndroid Build Coastguard Worker // SpillBB will not contain spill anymore, remove it from SpillsToKeep.
1323*9880d681SAndroid Build Coastguard Worker SpillsToKeep.erase(SpillBB);
1324*9880d681SAndroid Build Coastguard Worker }
1325*9880d681SAndroid Build Coastguard Worker // Current Block is the BB containing the new hoisted spill. Add it to
1326*9880d681SAndroid Build Coastguard Worker // SpillsToKeep. LiveReg is the source of the new spill.
1327*9880d681SAndroid Build Coastguard Worker SpillsToKeep[*RIt] = LiveReg;
1328*9880d681SAndroid Build Coastguard Worker DEBUG({
1329*9880d681SAndroid Build Coastguard Worker dbgs() << "spills in BB: ";
1330*9880d681SAndroid Build Coastguard Worker for (const auto Rspill : SpillsInSubTree)
1331*9880d681SAndroid Build Coastguard Worker dbgs() << Rspill->getBlock()->getNumber() << " ";
1332*9880d681SAndroid Build Coastguard Worker dbgs() << "were promoted to BB" << (*RIt)->getBlock()->getNumber()
1333*9880d681SAndroid Build Coastguard Worker << "\n";
1334*9880d681SAndroid Build Coastguard Worker });
1335*9880d681SAndroid Build Coastguard Worker SpillsInSubTree.clear();
1336*9880d681SAndroid Build Coastguard Worker SpillsInSubTree.insert(*RIt);
1337*9880d681SAndroid Build Coastguard Worker SubTreeCost = MBFI.getBlockFreq(Block);
1338*9880d681SAndroid Build Coastguard Worker }
1339*9880d681SAndroid Build Coastguard Worker }
1340*9880d681SAndroid Build Coastguard Worker // For spills in SpillsToKeep with LiveReg set (i.e., not original spill),
1341*9880d681SAndroid Build Coastguard Worker // save them to SpillsToIns.
1342*9880d681SAndroid Build Coastguard Worker for (const auto Ent : SpillsToKeep) {
1343*9880d681SAndroid Build Coastguard Worker if (Ent.second)
1344*9880d681SAndroid Build Coastguard Worker SpillsToIns[Ent.first->getBlock()] = Ent.second;
1345*9880d681SAndroid Build Coastguard Worker }
1346*9880d681SAndroid Build Coastguard Worker }
1347*9880d681SAndroid Build Coastguard Worker
1348*9880d681SAndroid Build Coastguard Worker /// For spills with equal values, remove redundant spills and hoist those left
1349*9880d681SAndroid Build Coastguard Worker /// to less hot spots.
1350*9880d681SAndroid Build Coastguard Worker ///
1351*9880d681SAndroid Build Coastguard Worker /// Spills with equal values will be collected into the same set in
1352*9880d681SAndroid Build Coastguard Worker /// MergeableSpills when spill is inserted. These equal spills are originated
1353*9880d681SAndroid Build Coastguard Worker /// from the same defining instruction and are dominated by the instruction.
1354*9880d681SAndroid Build Coastguard Worker /// Before hoisting all the equal spills, redundant spills inside in the same
1355*9880d681SAndroid Build Coastguard Worker /// BB are first marked to be deleted. Then starting from the spills left, walk
1356*9880d681SAndroid Build Coastguard Worker /// up on the dominator tree towards the Root node where the define instruction
1357*9880d681SAndroid Build Coastguard Worker /// is located, mark the dominated spills to be deleted along the way and
1358*9880d681SAndroid Build Coastguard Worker /// collect the BB nodes on the path from non-dominated spills to the define
1359*9880d681SAndroid Build Coastguard Worker /// instruction into a WorkSet. The nodes in WorkSet are the candidate places
1360*9880d681SAndroid Build Coastguard Worker /// where we are considering to hoist the spills. We iterate the WorkSet in
1361*9880d681SAndroid Build Coastguard Worker /// bottom-up order, and for each node, we will decide whether to hoist spills
1362*9880d681SAndroid Build Coastguard Worker /// inside its subtree to that node. In this way, we can get benefit locally
1363*9880d681SAndroid Build Coastguard Worker /// even if hoisting all the equal spills to one cold place is impossible.
1364*9880d681SAndroid Build Coastguard Worker ///
hoistAllSpills()1365*9880d681SAndroid Build Coastguard Worker void HoistSpillHelper::hoistAllSpills() {
1366*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned, 4> NewVRegs;
1367*9880d681SAndroid Build Coastguard Worker LiveRangeEdit Edit(nullptr, NewVRegs, MF, LIS, &VRM, this);
1368*9880d681SAndroid Build Coastguard Worker
1369*9880d681SAndroid Build Coastguard Worker // Save the mapping between stackslot and its original reg.
1370*9880d681SAndroid Build Coastguard Worker DenseMap<int, unsigned> SlotToOrigReg;
1371*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = MRI.getNumVirtRegs(); i != e; ++i) {
1372*9880d681SAndroid Build Coastguard Worker unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
1373*9880d681SAndroid Build Coastguard Worker int Slot = VRM.getStackSlot(Reg);
1374*9880d681SAndroid Build Coastguard Worker if (Slot != VirtRegMap::NO_STACK_SLOT)
1375*9880d681SAndroid Build Coastguard Worker SlotToOrigReg[Slot] = VRM.getOriginal(Reg);
1376*9880d681SAndroid Build Coastguard Worker unsigned Original = VRM.getPreSplitReg(Reg);
1377*9880d681SAndroid Build Coastguard Worker if (!MRI.def_empty(Reg))
1378*9880d681SAndroid Build Coastguard Worker Virt2SiblingsMap[Original].insert(Reg);
1379*9880d681SAndroid Build Coastguard Worker }
1380*9880d681SAndroid Build Coastguard Worker
1381*9880d681SAndroid Build Coastguard Worker // Each entry in MergeableSpills contains a spill set with equal values.
1382*9880d681SAndroid Build Coastguard Worker for (auto &Ent : MergeableSpills) {
1383*9880d681SAndroid Build Coastguard Worker int Slot = Ent.first.first;
1384*9880d681SAndroid Build Coastguard Worker unsigned OrigReg = SlotToOrigReg[Slot];
1385*9880d681SAndroid Build Coastguard Worker LiveInterval &OrigLI = LIS.getInterval(OrigReg);
1386*9880d681SAndroid Build Coastguard Worker VNInfo *OrigVNI = Ent.first.second;
1387*9880d681SAndroid Build Coastguard Worker SmallPtrSet<MachineInstr *, 16> &EqValSpills = Ent.second;
1388*9880d681SAndroid Build Coastguard Worker if (Ent.second.empty())
1389*9880d681SAndroid Build Coastguard Worker continue;
1390*9880d681SAndroid Build Coastguard Worker
1391*9880d681SAndroid Build Coastguard Worker DEBUG({
1392*9880d681SAndroid Build Coastguard Worker dbgs() << "\nFor Slot" << Slot << " and VN" << OrigVNI->id << ":\n"
1393*9880d681SAndroid Build Coastguard Worker << "Equal spills in BB: ";
1394*9880d681SAndroid Build Coastguard Worker for (const auto spill : EqValSpills)
1395*9880d681SAndroid Build Coastguard Worker dbgs() << spill->getParent()->getNumber() << " ";
1396*9880d681SAndroid Build Coastguard Worker dbgs() << "\n";
1397*9880d681SAndroid Build Coastguard Worker });
1398*9880d681SAndroid Build Coastguard Worker
1399*9880d681SAndroid Build Coastguard Worker // SpillsToRm is the spill set to be removed from EqValSpills.
1400*9880d681SAndroid Build Coastguard Worker SmallVector<MachineInstr *, 16> SpillsToRm;
1401*9880d681SAndroid Build Coastguard Worker // SpillsToIns is the spill set to be newly inserted after hoisting.
1402*9880d681SAndroid Build Coastguard Worker DenseMap<MachineBasicBlock *, unsigned> SpillsToIns;
1403*9880d681SAndroid Build Coastguard Worker
1404*9880d681SAndroid Build Coastguard Worker runHoistSpills(OrigReg, *OrigVNI, EqValSpills, SpillsToRm, SpillsToIns);
1405*9880d681SAndroid Build Coastguard Worker
1406*9880d681SAndroid Build Coastguard Worker DEBUG({
1407*9880d681SAndroid Build Coastguard Worker dbgs() << "Finally inserted spills in BB: ";
1408*9880d681SAndroid Build Coastguard Worker for (const auto Ispill : SpillsToIns)
1409*9880d681SAndroid Build Coastguard Worker dbgs() << Ispill.first->getNumber() << " ";
1410*9880d681SAndroid Build Coastguard Worker dbgs() << "\nFinally removed spills in BB: ";
1411*9880d681SAndroid Build Coastguard Worker for (const auto Rspill : SpillsToRm)
1412*9880d681SAndroid Build Coastguard Worker dbgs() << Rspill->getParent()->getNumber() << " ";
1413*9880d681SAndroid Build Coastguard Worker dbgs() << "\n";
1414*9880d681SAndroid Build Coastguard Worker });
1415*9880d681SAndroid Build Coastguard Worker
1416*9880d681SAndroid Build Coastguard Worker // Stack live range update.
1417*9880d681SAndroid Build Coastguard Worker LiveInterval &StackIntvl = LSS.getInterval(Slot);
1418*9880d681SAndroid Build Coastguard Worker if (!SpillsToIns.empty() || !SpillsToRm.empty())
1419*9880d681SAndroid Build Coastguard Worker StackIntvl.MergeValueInAsValue(OrigLI, OrigVNI,
1420*9880d681SAndroid Build Coastguard Worker StackIntvl.getValNumInfo(0));
1421*9880d681SAndroid Build Coastguard Worker
1422*9880d681SAndroid Build Coastguard Worker // Insert hoisted spills.
1423*9880d681SAndroid Build Coastguard Worker for (auto const Insert : SpillsToIns) {
1424*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *BB = Insert.first;
1425*9880d681SAndroid Build Coastguard Worker unsigned LiveReg = Insert.second;
1426*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator MI = IPA.getLastInsertPointIter(OrigLI, *BB);
1427*9880d681SAndroid Build Coastguard Worker TII.storeRegToStackSlot(*BB, MI, LiveReg, false, Slot,
1428*9880d681SAndroid Build Coastguard Worker MRI.getRegClass(LiveReg), &TRI);
1429*9880d681SAndroid Build Coastguard Worker LIS.InsertMachineInstrRangeInMaps(std::prev(MI), MI);
1430*9880d681SAndroid Build Coastguard Worker ++NumSpills;
1431*9880d681SAndroid Build Coastguard Worker }
1432*9880d681SAndroid Build Coastguard Worker
1433*9880d681SAndroid Build Coastguard Worker // Remove redundant spills or change them to dead instructions.
1434*9880d681SAndroid Build Coastguard Worker NumSpills -= SpillsToRm.size();
1435*9880d681SAndroid Build Coastguard Worker for (auto const RMEnt : SpillsToRm) {
1436*9880d681SAndroid Build Coastguard Worker RMEnt->setDesc(TII.get(TargetOpcode::KILL));
1437*9880d681SAndroid Build Coastguard Worker for (unsigned i = RMEnt->getNumOperands(); i; --i) {
1438*9880d681SAndroid Build Coastguard Worker MachineOperand &MO = RMEnt->getOperand(i - 1);
1439*9880d681SAndroid Build Coastguard Worker if (MO.isReg() && MO.isImplicit() && MO.isDef() && !MO.isDead())
1440*9880d681SAndroid Build Coastguard Worker RMEnt->RemoveOperand(i - 1);
1441*9880d681SAndroid Build Coastguard Worker }
1442*9880d681SAndroid Build Coastguard Worker }
1443*9880d681SAndroid Build Coastguard Worker Edit.eliminateDeadDefs(SpillsToRm, None, AA);
1444*9880d681SAndroid Build Coastguard Worker }
1445*9880d681SAndroid Build Coastguard Worker }
1446*9880d681SAndroid Build Coastguard Worker
1447*9880d681SAndroid Build Coastguard Worker /// For VirtReg clone, the \p New register should have the same physreg or
1448*9880d681SAndroid Build Coastguard Worker /// stackslot as the \p old register.
LRE_DidCloneVirtReg(unsigned New,unsigned Old)1449*9880d681SAndroid Build Coastguard Worker void HoistSpillHelper::LRE_DidCloneVirtReg(unsigned New, unsigned Old) {
1450*9880d681SAndroid Build Coastguard Worker if (VRM.hasPhys(Old))
1451*9880d681SAndroid Build Coastguard Worker VRM.assignVirt2Phys(New, VRM.getPhys(Old));
1452*9880d681SAndroid Build Coastguard Worker else if (VRM.getStackSlot(Old) != VirtRegMap::NO_STACK_SLOT)
1453*9880d681SAndroid Build Coastguard Worker VRM.assignVirt2StackSlot(New, VRM.getStackSlot(Old));
1454*9880d681SAndroid Build Coastguard Worker else
1455*9880d681SAndroid Build Coastguard Worker llvm_unreachable("VReg should be assigned either physreg or stackslot");
1456*9880d681SAndroid Build Coastguard Worker }
1457