blob: 8a797018815dd32eda95cf5a77f25f7a82a126a1 [file] [log] [blame]
Quentin Colombet61b305e2015-05-05 17:38:16 +00001//===-- ShrinkWrap.cpp - Compute safe point for prolog/epilog insertion ---===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass looks for safe point where the prologue and epilogue can be
11// inserted.
12// The safe point for the prologue (resp. epilogue) is called Save
13// (resp. Restore).
14// A point is safe for prologue (resp. epilogue) if and only if
15// it 1) dominates (resp. post-dominates) all the frame related operations and
16// between 2) two executions of the Save (resp. Restore) point there is an
17// execution of the Restore (resp. Save) point.
18//
19// For instance, the following points are safe:
20// for (int i = 0; i < 10; ++i) {
21// Save
22// ...
23// Restore
24// }
25// Indeed, the execution looks like Save -> Restore -> Save -> Restore ...
26// And the following points are not:
27// for (int i = 0; i < 10; ++i) {
28// Save
29// ...
30// }
31// for (int i = 0; i < 10; ++i) {
32// ...
33// Restore
34// }
35// Indeed, the execution looks like Save -> Save -> ... -> Restore -> Restore.
36//
37// This pass also ensures that the safe points are 3) cheaper than the regular
38// entry and exits blocks.
39//
40// Property #1 is ensured via the use of MachineDominatorTree and
41// MachinePostDominatorTree.
42// Property #2 is ensured via property #1 and MachineLoopInfo, i.e., both
43// points must be in the same loop.
44// Property #3 is ensured via the MachineBlockFrequencyInfo.
45//
Quentin Colombet9a8efc02015-11-06 21:00:13 +000046// If this pass found points matching all these properties, then
Quentin Colombet61b305e2015-05-05 17:38:16 +000047// MachineFrameInfo is updated this that information.
48//===----------------------------------------------------------------------===//
Quentin Colombet9a8efc02015-11-06 21:00:13 +000049#include "llvm/ADT/BitVector.h"
50#include "llvm/ADT/SetVector.h"
Quentin Colombet61b305e2015-05-05 17:38:16 +000051#include "llvm/ADT/Statistic.h"
52// To check for profitability.
53#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
54// For property #1 for Save.
55#include "llvm/CodeGen/MachineDominators.h"
56#include "llvm/CodeGen/MachineFunctionPass.h"
57// To record the result of the analysis.
58#include "llvm/CodeGen/MachineFrameInfo.h"
59// For property #2.
60#include "llvm/CodeGen/MachineLoopInfo.h"
61// For property #1 for Restore.
62#include "llvm/CodeGen/MachinePostDominators.h"
63#include "llvm/CodeGen/Passes.h"
64// To know about callee-saved.
65#include "llvm/CodeGen/RegisterClassInfo.h"
Quentin Colombet94dc1e02015-11-12 18:13:42 +000066#include "llvm/MC/MCAsmInfo.h"
Quentin Colombet61b305e2015-05-05 17:38:16 +000067#include "llvm/Support/Debug.h"
Quentin Colombet80835882015-05-27 06:25:48 +000068// To query the target about frame lowering.
69#include "llvm/Target/TargetFrameLowering.h"
Quentin Colombet61b305e2015-05-05 17:38:16 +000070// To know about frame setup operation.
71#include "llvm/Target/TargetInstrInfo.h"
Quentin Colombet94dc1e02015-11-12 18:13:42 +000072#include "llvm/Target/TargetMachine.h"
Quentin Colombet61b305e2015-05-05 17:38:16 +000073// To access TargetInstrInfo.
74#include "llvm/Target/TargetSubtargetInfo.h"
75
76#define DEBUG_TYPE "shrink-wrap"
77
78using namespace llvm;
79
80STATISTIC(NumFunc, "Number of functions");
81STATISTIC(NumCandidates, "Number of shrink-wrapping candidates");
82STATISTIC(NumCandidatesDropped,
83 "Number of shrink-wrapping candidates dropped because of frequency");
84
Kit Bartond3cc1672015-08-31 18:26:45 +000085static cl::opt<cl::boolOrDefault>
86 EnableShrinkWrapOpt("enable-shrink-wrap", cl::Hidden,
87 cl::desc("enable the shrink-wrapping pass"));
88
Quentin Colombet61b305e2015-05-05 17:38:16 +000089namespace {
90/// \brief Class to determine where the safe point to insert the
91/// prologue and epilogue are.
92/// Unlike the paper from Fred C. Chow, PLDI'88, that introduces the
93/// shrink-wrapping term for prologue/epilogue placement, this pass
94/// does not rely on expensive data-flow analysis. Instead we use the
95/// dominance properties and loop information to decide which point
96/// are safe for such insertion.
97class ShrinkWrap : public MachineFunctionPass {
98 /// Hold callee-saved information.
99 RegisterClassInfo RCI;
100 MachineDominatorTree *MDT;
101 MachinePostDominatorTree *MPDT;
102 /// Current safe point found for the prologue.
103 /// The prologue will be inserted before the first instruction
104 /// in this basic block.
105 MachineBasicBlock *Save;
106 /// Current safe point found for the epilogue.
107 /// The epilogue will be inserted before the first terminator instruction
108 /// in this basic block.
109 MachineBasicBlock *Restore;
110 /// Hold the information of the basic block frequency.
111 /// Use to check the profitability of the new points.
112 MachineBlockFrequencyInfo *MBFI;
113 /// Hold the loop information. Used to determine if Save and Restore
114 /// are in the same loop.
115 MachineLoopInfo *MLI;
116 /// Frequency of the Entry block.
117 uint64_t EntryFreq;
118 /// Current opcode for frame setup.
Matthias Braunfa3872e2015-05-18 20:27:55 +0000119 unsigned FrameSetupOpcode;
Quentin Colombet61b305e2015-05-05 17:38:16 +0000120 /// Current opcode for frame destroy.
Matthias Braunfa3872e2015-05-18 20:27:55 +0000121 unsigned FrameDestroyOpcode;
Quentin Colombet61b305e2015-05-05 17:38:16 +0000122 /// Entry block.
123 const MachineBasicBlock *Entry;
Quentin Colombet9a8efc02015-11-06 21:00:13 +0000124 typedef SmallSetVector<unsigned, 16> SetOfRegs;
125 /// Registers that need to be saved for the current function.
126 mutable SetOfRegs CurrentCSRs;
127 /// Current MachineFunction.
128 MachineFunction *MachineFunc;
Quentin Colombet61b305e2015-05-05 17:38:16 +0000129
130 /// \brief Check if \p MI uses or defines a callee-saved register or
131 /// a frame index. If this is the case, this means \p MI must happen
132 /// after Save and before Restore.
133 bool useOrDefCSROrFI(const MachineInstr &MI) const;
134
Quentin Colombet9a8efc02015-11-06 21:00:13 +0000135 const SetOfRegs &getCurrentCSRs() const {
136 if (CurrentCSRs.empty()) {
137 BitVector SavedRegs;
138 const TargetFrameLowering *TFI =
139 MachineFunc->getSubtarget().getFrameLowering();
140
141 TFI->determineCalleeSaves(*MachineFunc, SavedRegs, nullptr);
142
143 for (int Reg = SavedRegs.find_first(); Reg != -1;
144 Reg = SavedRegs.find_next(Reg))
145 CurrentCSRs.insert((unsigned)Reg);
146 }
147 return CurrentCSRs;
148 }
149
Quentin Colombet61b305e2015-05-05 17:38:16 +0000150 /// \brief Update the Save and Restore points such that \p MBB is in
151 /// the region that is dominated by Save and post-dominated by Restore
152 /// and Save and Restore still match the safe point definition.
153 /// Such point may not exist and Save and/or Restore may be null after
154 /// this call.
155 void updateSaveRestorePoints(MachineBasicBlock &MBB);
156
157 /// \brief Initialize the pass for \p MF.
158 void init(MachineFunction &MF) {
159 RCI.runOnMachineFunction(MF);
160 MDT = &getAnalysis<MachineDominatorTree>();
161 MPDT = &getAnalysis<MachinePostDominatorTree>();
162 Save = nullptr;
163 Restore = nullptr;
164 MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
165 MLI = &getAnalysis<MachineLoopInfo>();
166 EntryFreq = MBFI->getEntryFreq();
167 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
168 FrameSetupOpcode = TII.getCallFrameSetupOpcode();
169 FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
170 Entry = &MF.front();
Quentin Colombet9a8efc02015-11-06 21:00:13 +0000171 CurrentCSRs.clear();
172 MachineFunc = &MF;
Quentin Colombet61b305e2015-05-05 17:38:16 +0000173
174 ++NumFunc;
175 }
176
177 /// Check whether or not Save and Restore points are still interesting for
178 /// shrink-wrapping.
179 bool ArePointsInteresting() const { return Save != Entry && Save && Restore; }
180
Kit Bartond3cc1672015-08-31 18:26:45 +0000181 /// \brief Check if shrink wrapping is enabled for this target and function.
182 static bool isShrinkWrapEnabled(const MachineFunction &MF);
183
Quentin Colombet61b305e2015-05-05 17:38:16 +0000184public:
185 static char ID;
186
187 ShrinkWrap() : MachineFunctionPass(ID) {
188 initializeShrinkWrapPass(*PassRegistry::getPassRegistry());
189 }
190
191 void getAnalysisUsage(AnalysisUsage &AU) const override {
192 AU.setPreservesAll();
193 AU.addRequired<MachineBlockFrequencyInfo>();
194 AU.addRequired<MachineDominatorTree>();
195 AU.addRequired<MachinePostDominatorTree>();
196 AU.addRequired<MachineLoopInfo>();
197 MachineFunctionPass::getAnalysisUsage(AU);
198 }
199
200 const char *getPassName() const override {
201 return "Shrink Wrapping analysis";
202 }
203
204 /// \brief Perform the shrink-wrapping analysis and update
205 /// the MachineFrameInfo attached to \p MF with the results.
206 bool runOnMachineFunction(MachineFunction &MF) override;
207};
208} // End anonymous namespace.
209
210char ShrinkWrap::ID = 0;
211char &llvm::ShrinkWrapID = ShrinkWrap::ID;
212
213INITIALIZE_PASS_BEGIN(ShrinkWrap, "shrink-wrap", "Shrink Wrap Pass", false,
214 false)
215INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo)
216INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
217INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree)
218INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
219INITIALIZE_PASS_END(ShrinkWrap, "shrink-wrap", "Shrink Wrap Pass", false, false)
220
221bool ShrinkWrap::useOrDefCSROrFI(const MachineInstr &MI) const {
222 if (MI.getOpcode() == FrameSetupOpcode ||
223 MI.getOpcode() == FrameDestroyOpcode) {
224 DEBUG(dbgs() << "Frame instruction: " << MI << '\n');
225 return true;
226 }
227 for (const MachineOperand &MO : MI.operands()) {
Quentin Colombet9a8efc02015-11-06 21:00:13 +0000228 bool UseOrDefCSR = false;
Quentin Colombet61b305e2015-05-05 17:38:16 +0000229 if (MO.isReg()) {
230 unsigned PhysReg = MO.getReg();
231 if (!PhysReg)
232 continue;
233 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
234 "Unallocated register?!");
Quentin Colombet9a8efc02015-11-06 21:00:13 +0000235 UseOrDefCSR = RCI.getLastCalleeSavedAlias(PhysReg);
236 } else if (MO.isRegMask()) {
237 // Check if this regmask clobbers any of the CSRs.
238 for (unsigned Reg : getCurrentCSRs()) {
239 if (MO.clobbersPhysReg(Reg)) {
240 UseOrDefCSR = true;
241 break;
242 }
243 }
Quentin Colombet61b305e2015-05-05 17:38:16 +0000244 }
Quentin Colombet9a8efc02015-11-06 21:00:13 +0000245 if (UseOrDefCSR || MO.isFI()) {
246 DEBUG(dbgs() << "Use or define CSR(" << UseOrDefCSR << ") or FI("
247 << MO.isFI() << "): " << MI << '\n');
Quentin Colombet61b305e2015-05-05 17:38:16 +0000248 return true;
249 }
250 }
251 return false;
252}
253
254/// \brief Helper function to find the immediate (post) dominator.
255template <typename ListOfBBs, typename DominanceAnalysis>
256MachineBasicBlock *FindIDom(MachineBasicBlock &Block, ListOfBBs BBs,
257 DominanceAnalysis &Dom) {
258 MachineBasicBlock *IDom = &Block;
259 for (MachineBasicBlock *BB : BBs) {
260 IDom = Dom.findNearestCommonDominator(IDom, BB);
261 if (!IDom)
262 break;
263 }
264 return IDom;
265}
266
267void ShrinkWrap::updateSaveRestorePoints(MachineBasicBlock &MBB) {
268 // Get rid of the easy cases first.
269 if (!Save)
270 Save = &MBB;
271 else
272 Save = MDT->findNearestCommonDominator(Save, &MBB);
273
274 if (!Save) {
275 DEBUG(dbgs() << "Found a block that is not reachable from Entry\n");
276 return;
277 }
278
279 if (!Restore)
280 Restore = &MBB;
281 else
282 Restore = MPDT->findNearestCommonDominator(Restore, &MBB);
283
284 // Make sure we would be able to insert the restore code before the
285 // terminator.
286 if (Restore == &MBB) {
287 for (const MachineInstr &Terminator : MBB.terminators()) {
288 if (!useOrDefCSROrFI(Terminator))
289 continue;
290 // One of the terminator needs to happen before the restore point.
291 if (MBB.succ_empty()) {
292 Restore = nullptr;
293 break;
294 }
295 // Look for a restore point that post-dominates all the successors.
296 // The immediate post-dominator is what we are looking for.
297 Restore = FindIDom<>(*Restore, Restore->successors(), *MPDT);
298 break;
299 }
300 }
301
302 if (!Restore) {
303 DEBUG(dbgs() << "Restore point needs to be spanned on several blocks\n");
304 return;
305 }
306
307 // Make sure Save and Restore are suitable for shrink-wrapping:
308 // 1. all path from Save needs to lead to Restore before exiting.
309 // 2. all path to Restore needs to go through Save from Entry.
310 // We achieve that by making sure that:
311 // A. Save dominates Restore.
312 // B. Restore post-dominates Save.
313 // C. Save and Restore are in the same loop.
314 bool SaveDominatesRestore = false;
315 bool RestorePostDominatesSave = false;
316 while (Save && Restore &&
317 (!(SaveDominatesRestore = MDT->dominates(Save, Restore)) ||
318 !(RestorePostDominatesSave = MPDT->dominates(Restore, Save)) ||
319 MLI->getLoopFor(Save) != MLI->getLoopFor(Restore))) {
320 // Fix (A).
321 if (!SaveDominatesRestore) {
322 Save = MDT->findNearestCommonDominator(Save, Restore);
323 continue;
324 }
325 // Fix (B).
326 if (!RestorePostDominatesSave)
327 Restore = MPDT->findNearestCommonDominator(Restore, Save);
328
329 // Fix (C).
330 if (Save && Restore && Save != Restore &&
331 MLI->getLoopFor(Save) != MLI->getLoopFor(Restore)) {
Kit Bartona7bf96a2015-08-06 19:01:57 +0000332 if (MLI->getLoopDepth(Save) > MLI->getLoopDepth(Restore)) {
333 // Push Save outside of this loop if immediate dominator is different
334 // from save block. If immediate dominator is not different, bail out.
335 MachineBasicBlock *IDom = FindIDom<>(*Save, Save->predecessors(), *MDT);
336 if (IDom != Save)
337 Save = IDom;
338 else {
339 Save = nullptr;
340 break;
341 }
342 }
343 else {
Quentin Colombetdc29c972015-09-15 18:19:39 +0000344 // If the loop does not exit, there is no point in looking
345 // for a post-dominator outside the loop.
346 SmallVector<MachineBasicBlock*, 4> ExitBlocks;
347 MLI->getLoopFor(Restore)->getExitingBlocks(ExitBlocks);
Quentin Colombetb4c68862015-09-17 23:21:34 +0000348 // Push Restore outside of this loop.
349 // Look for the immediate post-dominator of the loop exits.
350 MachineBasicBlock *IPdom = Restore;
351 for (MachineBasicBlock *LoopExitBB: ExitBlocks) {
352 IPdom = FindIDom<>(*IPdom, LoopExitBB->successors(), *MPDT);
353 if (!IPdom)
354 break;
Quentin Colombetdc29c972015-09-15 18:19:39 +0000355 }
Quentin Colombetb4c68862015-09-17 23:21:34 +0000356 // If the immediate post-dominator is not in a less nested loop,
357 // then we are stuck in a program with an infinite loop.
358 // In that case, we will not find a safe point, hence, bail out.
359 if (IPdom && MLI->getLoopDepth(IPdom) < MLI->getLoopDepth(Restore))
Kit Bartona7bf96a2015-08-06 19:01:57 +0000360 Restore = IPdom;
361 else {
362 Restore = nullptr;
363 break;
364 }
365 }
Quentin Colombet61b305e2015-05-05 17:38:16 +0000366 }
367 }
368}
369
370bool ShrinkWrap::runOnMachineFunction(MachineFunction &MF) {
Kit Bartond3cc1672015-08-31 18:26:45 +0000371 if (MF.empty() || !isShrinkWrapEnabled(MF))
Quentin Colombet61b305e2015-05-05 17:38:16 +0000372 return false;
Kit Bartond3cc1672015-08-31 18:26:45 +0000373
Quentin Colombet61b305e2015-05-05 17:38:16 +0000374 DEBUG(dbgs() << "**** Analysing " << MF.getName() << '\n');
375
376 init(MF);
377
378 for (MachineBasicBlock &MBB : MF) {
379 DEBUG(dbgs() << "Look into: " << MBB.getNumber() << ' ' << MBB.getName()
380 << '\n');
381
Quentin Colombet94dc1e02015-11-12 18:13:42 +0000382 if (MBB.isEHFuncletEntry()) {
383 DEBUG(dbgs() << "EH Funclets are not supported yet.\n");
384 return false;
385 }
386
Quentin Colombet61b305e2015-05-05 17:38:16 +0000387 for (const MachineInstr &MI : MBB) {
388 if (!useOrDefCSROrFI(MI))
389 continue;
390 // Save (resp. restore) point must dominate (resp. post dominate)
391 // MI. Look for the proper basic block for those.
392 updateSaveRestorePoints(MBB);
393 // If we are at a point where we cannot improve the placement of
394 // save/restore instructions, just give up.
395 if (!ArePointsInteresting()) {
396 DEBUG(dbgs() << "No Shrink wrap candidate found\n");
397 return false;
398 }
399 // No need to look for other instructions, this basic block
400 // will already be part of the handled region.
401 break;
402 }
403 }
404 if (!ArePointsInteresting()) {
405 // If the points are not interesting at this point, then they must be null
406 // because it means we did not encounter any frame/CSR related code.
407 // Otherwise, we would have returned from the previous loop.
408 assert(!Save && !Restore && "We miss a shrink-wrap opportunity?!");
409 DEBUG(dbgs() << "Nothing to shrink-wrap\n");
410 return false;
411 }
412
413 DEBUG(dbgs() << "\n ** Results **\nFrequency of the Entry: " << EntryFreq
414 << '\n');
415
Quentin Colombet80835882015-05-27 06:25:48 +0000416 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
Quentin Colombet61b305e2015-05-05 17:38:16 +0000417 do {
418 DEBUG(dbgs() << "Shrink wrap candidates (#, Name, Freq):\nSave: "
419 << Save->getNumber() << ' ' << Save->getName() << ' '
420 << MBFI->getBlockFreq(Save).getFrequency() << "\nRestore: "
421 << Restore->getNumber() << ' ' << Restore->getName() << ' '
422 << MBFI->getBlockFreq(Restore).getFrequency() << '\n');
423
Quentin Colombet80835882015-05-27 06:25:48 +0000424 bool IsSaveCheap, TargetCanUseSaveAsPrologue = false;
425 if (((IsSaveCheap = EntryFreq >= MBFI->getBlockFreq(Save).getFrequency()) &&
426 EntryFreq >= MBFI->getBlockFreq(Restore).getFrequency()) &&
427 ((TargetCanUseSaveAsPrologue = TFI->canUseAsPrologue(*Save)) &&
428 TFI->canUseAsEpilogue(*Restore)))
Quentin Colombet61b305e2015-05-05 17:38:16 +0000429 break;
Quentin Colombet80835882015-05-27 06:25:48 +0000430 DEBUG(dbgs() << "New points are too expensive or invalid for the target\n");
Quentin Colombet61b305e2015-05-05 17:38:16 +0000431 MachineBasicBlock *NewBB;
Quentin Colombet80835882015-05-27 06:25:48 +0000432 if (!IsSaveCheap || !TargetCanUseSaveAsPrologue) {
Quentin Colombet61b305e2015-05-05 17:38:16 +0000433 Save = FindIDom<>(*Save, Save->predecessors(), *MDT);
434 if (!Save)
435 break;
436 NewBB = Save;
437 } else {
438 // Restore is expensive.
439 Restore = FindIDom<>(*Restore, Restore->successors(), *MPDT);
440 if (!Restore)
441 break;
442 NewBB = Restore;
443 }
444 updateSaveRestorePoints(*NewBB);
445 } while (Save && Restore);
446
447 if (!ArePointsInteresting()) {
448 ++NumCandidatesDropped;
449 return false;
450 }
451
452 DEBUG(dbgs() << "Final shrink wrap candidates:\nSave: " << Save->getNumber()
453 << ' ' << Save->getName() << "\nRestore: "
454 << Restore->getNumber() << ' ' << Restore->getName() << '\n');
455
456 MachineFrameInfo *MFI = MF.getFrameInfo();
457 MFI->setSavePoint(Save);
458 MFI->setRestorePoint(Restore);
459 ++NumCandidates;
460 return false;
461}
Kit Bartond3cc1672015-08-31 18:26:45 +0000462
463bool ShrinkWrap::isShrinkWrapEnabled(const MachineFunction &MF) {
464 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
465
466 switch (EnableShrinkWrapOpt) {
467 case cl::BOU_UNSET:
Quentin Colombet94dc1e02015-11-12 18:13:42 +0000468 return TFI->enableShrinkWrapping(MF) &&
Quentin Colombetaeb85932015-11-12 18:16:27 +0000469 // Windows with CFI has some limitations that make it impossible
Quentin Colombet94dc1e02015-11-12 18:13:42 +0000470 // to use shrink-wrapping.
471 !MF.getTarget().getMCAsmInfo()->usesWindowsCFI();
Kit Bartond3cc1672015-08-31 18:26:45 +0000472 // If EnableShrinkWrap is set, it takes precedence on whatever the
473 // target sets. The rational is that we assume we want to test
474 // something related to shrink-wrapping.
475 case cl::BOU_TRUE:
476 return true;
477 case cl::BOU_FALSE:
478 return false;
479 }
480 llvm_unreachable("Invalid shrink-wrapping state");
481}