1 //===-- PrologEpilogInserter.h - Prolog/Epilog code insertion -*- C++ -* --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass is responsible for finalizing the functions frame layout, saving
11 // callee saved registers, and for emitting prolog & epilog code for the
14 // This pass must be run after register allocation. After this pass is
15 // executed, it is illegal to construct MO_FrameIndex operands.
17 // This pass also implements a shrink wrapping variant of prolog/epilog
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_CODEGEN_PEI_H
23 #define LLVM_CODEGEN_PEI_H
25 #include "llvm/CodeGen/Passes.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/CodeGen/MachineLoopInfo.h"
28 #include "llvm/ADT/SparseBitVector.h"
29 #include "llvm/ADT/DenseMap.h"
33 class MachineBasicBlock
;
35 class PEI
: public MachineFunctionPass
{
38 PEI() : MachineFunctionPass(&ID
) {}
40 const char *getPassName() const {
41 return "Prolog/Epilog Insertion & Frame Finalization";
44 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const;
46 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
47 /// frame indexes with appropriate references.
49 bool runOnMachineFunction(MachineFunction
&Fn
);
54 // MinCSFrameIndex, MaxCSFrameIndex - Keeps the range of callee saved
55 // stack frame indexes.
56 unsigned MinCSFrameIndex
, MaxCSFrameIndex
;
58 // Analysis info for spill/restore placement.
59 // "CSR": "callee saved register".
61 // CSRegSet contains indices into the Callee Saved Register Info
62 // vector built by calculateCalleeSavedRegisters() and accessed
63 // via MF.getFrameInfo()->getCalleeSavedInfo().
64 typedef SparseBitVector
<> CSRegSet
;
66 // CSRegBlockMap maps MachineBasicBlocks to sets of callee
67 // saved register indices.
68 typedef DenseMap
<MachineBasicBlock
*, CSRegSet
> CSRegBlockMap
;
70 // Set and maps for computing CSR spill/restore placement:
71 // used in function (UsedCSRegs)
72 // used in a basic block (CSRUsed)
73 // anticipatable in a basic block (Antic{In,Out})
74 // available in a basic block (Avail{In,Out})
75 // to be spilled at the entry to a basic block (CSRSave)
76 // to be restored at the end of a basic block (CSRRestore)
78 CSRegBlockMap CSRUsed
;
79 CSRegBlockMap AnticIn
, AnticOut
;
80 CSRegBlockMap AvailIn
, AvailOut
;
81 CSRegBlockMap CSRSave
;
82 CSRegBlockMap CSRRestore
;
84 // Entry and return blocks of the current function.
85 MachineBasicBlock
* EntryBlock
;
86 SmallVector
<MachineBasicBlock
*, 4> ReturnBlocks
;
88 // Map of MBBs to top level MachineLoops.
89 DenseMap
<MachineBasicBlock
*, MachineLoop
*> TLLoops
;
91 // Flag to control shrink wrapping per-function:
92 // may choose to skip shrink wrapping for certain
94 bool ShrinkWrapThisFunction
;
97 // Machine function handle.
100 // Flag indicating that the current function
101 // has at least one "short" path in the machine
102 // CFG from the entry block to an exit block.
103 bool HasFastExitPath
;
106 bool calculateSets(MachineFunction
&Fn
);
107 bool calcAnticInOut(MachineBasicBlock
* MBB
);
108 bool calcAvailInOut(MachineBasicBlock
* MBB
);
109 void calculateAnticAvail(MachineFunction
&Fn
);
110 bool addUsesForMEMERegion(MachineBasicBlock
* MBB
,
111 SmallVector
<MachineBasicBlock
*, 4>& blks
);
112 bool addUsesForTopLevelLoops(SmallVector
<MachineBasicBlock
*, 4>& blks
);
113 bool calcSpillPlacements(MachineBasicBlock
* MBB
,
114 SmallVector
<MachineBasicBlock
*, 4> &blks
,
115 CSRegBlockMap
&prevSpills
);
116 bool calcRestorePlacements(MachineBasicBlock
* MBB
,
117 SmallVector
<MachineBasicBlock
*, 4> &blks
,
118 CSRegBlockMap
&prevRestores
);
119 void placeSpillsAndRestores(MachineFunction
&Fn
);
120 void placeCSRSpillsAndRestores(MachineFunction
&Fn
);
121 void calculateCalleeSavedRegisters(MachineFunction
&Fn
);
122 void insertCSRSpillsAndRestores(MachineFunction
&Fn
);
123 void calculateFrameObjectOffsets(MachineFunction
&Fn
);
124 void replaceFrameIndices(MachineFunction
&Fn
);
125 void insertPrologEpilogCode(MachineFunction
&Fn
);
127 // Initialize DFA sets, called before iterations.
128 void clearAnticAvailSets();
129 // Clear all sets constructed by shrink wrapping.
132 // Initialize all shrink wrapping data.
133 void initShrinkWrappingInfo();
135 // Convienences for dealing with machine loops.
136 MachineBasicBlock
* getTopLevelLoopPreheader(MachineLoop
* LP
);
137 MachineLoop
* getTopLevelLoopParent(MachineLoop
*LP
);
139 // Propgate CSRs used in MBB to all MBBs of loop LP.
140 void propagateUsesAroundLoop(MachineBasicBlock
* MBB
, MachineLoop
* LP
);
142 // Convenience for recognizing return blocks.
143 bool isReturnBlock(MachineBasicBlock
* MBB
);
146 // Debugging methods.
148 // Mark this function as having fast exit paths.
149 void findFastExitPath();
151 // Verify placement of spills/restores.
152 void verifySpillRestorePlacement();
154 std::string
getBasicBlockName(const MachineBasicBlock
* MBB
);
155 std::string
stringifyCSRegSet(const CSRegSet
& s
);
156 void dumpSet(const CSRegSet
& s
);
157 void dumpUsed(MachineBasicBlock
* MBB
);
159 void dumpSets(MachineBasicBlock
* MBB
);
160 void dumpSets1(MachineBasicBlock
* MBB
);
166 } // End llvm namespace