Chris Lattner | 58b3328 | 2002-12-28 20:43:30 +0000 | [diff] [blame] | 1 | //===-- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function --===// |
| 2 | // |
| 3 | // This pass is responsible for finalizing the functions frame layout, saving |
| 4 | // callee saved registers, and for emitting prolog & epilog code for the |
| 5 | // function. |
| 6 | // |
| 7 | // This pass must be run after register allocation. After this pass is |
| 8 | // executed, it is illegal to construct MO_FrameIndex operands. |
| 9 | // |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
Chris Lattner | f00a3f9 | 2003-01-13 00:23:41 +0000 | [diff] [blame] | 12 | #include "llvm/CodeGen/Passes.h" |
| 13 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chris Lattner | 58b3328 | 2002-12-28 20:43:30 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | eb24db9 | 2002-12-28 21:08:26 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | 58b3328 | 2002-12-28 20:43:30 +0000 | [diff] [blame] | 16 | #include "llvm/Target/TargetMachine.h" |
| 17 | #include "llvm/Target/MRegisterInfo.h" |
Chris Lattner | 8bd66e6 | 2002-12-28 21:00:25 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetFrameInfo.h" |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | 58b3328 | 2002-12-28 20:43:30 +0000 | [diff] [blame] | 20 | |
| 21 | namespace { |
Chris Lattner | f00a3f9 | 2003-01-13 00:23:41 +0000 | [diff] [blame] | 22 | struct PEI : public MachineFunctionPass { |
| 23 | const char *getPassName() const { |
| 24 | return "Prolog/Epilog Insertion & Frame Finalization"; |
Chris Lattner | 58b3328 | 2002-12-28 20:43:30 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | /// runOnMachineFunction - Insert prolog/epilog code and replace abstract |
| 28 | /// frame indexes with appropriate references. |
| 29 | /// |
| 30 | bool runOnMachineFunction(MachineFunction &Fn) { |
| 31 | // Scan the function for modified caller saved registers and insert spill |
| 32 | // code for any caller saved registers that are modified. Also calculate |
| 33 | // the MaxCallFrameSize and HasCalls variables for the function's frame |
| 34 | // information and eliminates call frame pseudo instructions. |
| 35 | saveCallerSavedRegisters(Fn); |
| 36 | |
| 37 | // Allow the target machine to make final modifications to the function |
| 38 | // before the frame layout is finalized. |
| 39 | Fn.getTarget().getRegisterInfo()->processFunctionBeforeFrameFinalized(Fn); |
| 40 | |
| 41 | // Calculate actual frame offsets for all of the abstract stack objects... |
| 42 | calculateFrameObjectOffsets(Fn); |
| 43 | |
Chris Lattner | 4ac7d73 | 2003-01-15 22:52:34 +0000 | [diff] [blame^] | 44 | // Add prolog and epilog code to the function. |
| 45 | insertPrologEpilogCode(Fn); |
| 46 | |
Chris Lattner | 58b3328 | 2002-12-28 20:43:30 +0000 | [diff] [blame] | 47 | // Replace all MO_FrameIndex operands with physical register references |
| 48 | // and actual offsets. |
| 49 | // |
| 50 | replaceFrameIndices(Fn); |
Chris Lattner | 58b3328 | 2002-12-28 20:43:30 +0000 | [diff] [blame] | 51 | return true; |
| 52 | } |
| 53 | |
| 54 | private: |
| 55 | void saveCallerSavedRegisters(MachineFunction &Fn); |
| 56 | void calculateFrameObjectOffsets(MachineFunction &Fn); |
| 57 | void replaceFrameIndices(MachineFunction &Fn); |
| 58 | void insertPrologEpilogCode(MachineFunction &Fn); |
| 59 | }; |
| 60 | } |
| 61 | |
| 62 | /// createPrologEpilogCodeInserter - This function returns a pass that inserts |
| 63 | /// prolog and epilog code, and eliminates abstract frame references. |
| 64 | /// |
| 65 | Pass *createPrologEpilogCodeInserter() { return new PEI(); } |
| 66 | |
| 67 | |
| 68 | /// saveCallerSavedRegisters - Scan the function for modified caller saved |
| 69 | /// registers and insert spill code for any caller saved registers that are |
| 70 | /// modified. Also calculate the MaxCallFrameSize and HasCalls variables for |
| 71 | /// the function's frame information and eliminates call frame pseudo |
| 72 | /// instructions. |
| 73 | /// |
| 74 | void PEI::saveCallerSavedRegisters(MachineFunction &Fn) { |
| 75 | const MRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo(); |
| 76 | const TargetFrameInfo &FrameInfo = Fn.getTarget().getFrameInfo(); |
| 77 | |
| 78 | // Get the callee saved register list... |
| 79 | const unsigned *CSRegs = RegInfo->getCalleeSaveRegs(); |
| 80 | |
| 81 | // Get the function call frame set-up and tear-down instruction opcode |
| 82 | int FrameSetupOpcode = RegInfo->getCallFrameSetupOpcode(); |
| 83 | int FrameDestroyOpcode = RegInfo->getCallFrameDestroyOpcode(); |
| 84 | |
| 85 | // Early exit for targets which have no callee saved registers and no call |
| 86 | // frame setup/destroy pseudo instructions. |
| 87 | if ((CSRegs == 0 || CSRegs[0] == 0) && |
| 88 | FrameSetupOpcode == -1 && FrameDestroyOpcode == -1) |
| 89 | return; |
| 90 | |
| 91 | // This bitset contains an entry for each physical register for the target... |
| 92 | std::vector<bool> ModifiedRegs(MRegisterInfo::FirstVirtualRegister); |
| 93 | unsigned MaxCallFrameSize = 0; |
| 94 | bool HasCalls = false; |
| 95 | |
| 96 | for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) |
| 97 | for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) |
| 98 | if ((*I)->getOpcode() == FrameSetupOpcode || |
| 99 | (*I)->getOpcode() == FrameDestroyOpcode) { |
| 100 | assert((*I)->getNumOperands() == 1 && "Call Frame Setup/Destroy Pseudo" |
| 101 | " instructions should have a single immediate argument!"); |
| 102 | unsigned Size = (*I)->getOperand(0).getImmedValue(); |
| 103 | if (Size > MaxCallFrameSize) MaxCallFrameSize = Size; |
| 104 | HasCalls = true; |
| 105 | RegInfo->eliminateCallFramePseudoInstr(Fn, *BB, I); |
| 106 | } else { |
| 107 | for (unsigned i = 0, e = (*I)->getNumOperands(); i != e; ++i) { |
| 108 | MachineOperand &MO = (*I)->getOperand(i); |
| 109 | assert(!MO.isVirtualRegister() && |
| 110 | "Register allocation must be performed!"); |
| 111 | if (MO.isPhysicalRegister() && MO.opIsDef()) |
| 112 | ModifiedRegs[MO.getReg()] = true; // Register is modified |
| 113 | } |
| 114 | ++I; |
| 115 | } |
| 116 | |
Chris Lattner | eb24db9 | 2002-12-28 21:08:26 +0000 | [diff] [blame] | 117 | MachineFrameInfo *FFI = Fn.getFrameInfo(); |
Chris Lattner | 58b3328 | 2002-12-28 20:43:30 +0000 | [diff] [blame] | 118 | FFI->setHasCalls(HasCalls); |
| 119 | FFI->setMaxCallFrameSize(MaxCallFrameSize); |
| 120 | |
| 121 | // Now figure out which *callee saved* registers are modified by the current |
| 122 | // function, thus needing to be saved and restored in the prolog/epilog. |
| 123 | // |
| 124 | std::vector<unsigned> RegsToSave; |
| 125 | for (unsigned i = 0; CSRegs[i]; ++i) { |
| 126 | unsigned Reg = CSRegs[i]; |
| 127 | if (ModifiedRegs[Reg]) { |
| 128 | RegsToSave.push_back(Reg); // If modified register... |
| 129 | } else if (const unsigned *AliasSet = RegInfo->getAliasSet(Reg)) |
| 130 | for (unsigned j = 0; AliasSet[j]; ++j) // Check alias registers too... |
| 131 | if (ModifiedRegs[AliasSet[j]]) { |
| 132 | RegsToSave.push_back(Reg); |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (RegsToSave.empty()) |
| 138 | return; // Early exit if no caller saved registers are modified! |
| 139 | |
| 140 | // Now that we know which registers need to be saved and restored, allocate |
| 141 | // stack slots for them. |
| 142 | std::vector<int> StackSlots; |
| 143 | for (unsigned i = 0, e = RegsToSave.size(); i != e; ++i) { |
Chris Lattner | f00a3f9 | 2003-01-13 00:23:41 +0000 | [diff] [blame] | 144 | int FrameIdx = FFI->CreateStackObject(RegInfo->getRegClass(RegsToSave[i])); |
Chris Lattner | 58b3328 | 2002-12-28 20:43:30 +0000 | [diff] [blame] | 145 | StackSlots.push_back(FrameIdx); |
| 146 | } |
| 147 | |
| 148 | // Now that we have a stack slot for each register to be saved, insert spill |
| 149 | // code into the entry block... |
| 150 | MachineBasicBlock *MBB = Fn.begin(); |
| 151 | MachineBasicBlock::iterator I = MBB->begin(); |
| 152 | for (unsigned i = 0, e = RegsToSave.size(); i != e; ++i) { |
| 153 | const TargetRegisterClass *RC = RegInfo->getRegClass(RegsToSave[i]); |
| 154 | |
| 155 | // Insert the spill to the stack frame... |
| 156 | RegInfo->storeRegToStackSlot(*MBB, I, RegsToSave[i], StackSlots[i], RC); |
| 157 | } |
| 158 | |
| 159 | // Add code to restore the callee-save registers in each exiting block. |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 160 | const TargetInstrInfo &TII = Fn.getTarget().getInstrInfo(); |
Chris Lattner | 58b3328 | 2002-12-28 20:43:30 +0000 | [diff] [blame] | 161 | for (MachineFunction::iterator FI = Fn.begin(), E = Fn.end(); FI != E; ++FI) { |
| 162 | // If last instruction is a return instruction, add an epilogue |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 163 | if (TII.isReturn(FI->back()->getOpcode())) { |
Chris Lattner | 58b3328 | 2002-12-28 20:43:30 +0000 | [diff] [blame] | 164 | MBB = FI; I = MBB->end()-1; |
| 165 | |
| 166 | for (unsigned i = 0, e = RegsToSave.size(); i != e; ++i) { |
| 167 | const TargetRegisterClass *RC = RegInfo->getRegClass(RegsToSave[i]); |
| 168 | RegInfo->loadRegFromStackSlot(*MBB, I, RegsToSave[i],StackSlots[i], RC); |
| 169 | --I; // Insert in reverse order |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the |
| 177 | /// abstract stack objects... |
| 178 | /// |
| 179 | void PEI::calculateFrameObjectOffsets(MachineFunction &Fn) { |
| 180 | const TargetFrameInfo &TFI = Fn.getTarget().getFrameInfo(); |
| 181 | |
| 182 | bool StackGrowsDown = |
| 183 | TFI.getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown; |
| 184 | assert(StackGrowsDown && "Only tested on stack down growing targets!"); |
| 185 | |
| 186 | // Loop over all of the stack objects, assigning sequential addresses... |
Chris Lattner | eb24db9 | 2002-12-28 21:08:26 +0000 | [diff] [blame] | 187 | MachineFrameInfo *FFI = Fn.getFrameInfo(); |
Chris Lattner | 58b3328 | 2002-12-28 20:43:30 +0000 | [diff] [blame] | 188 | |
| 189 | // Start at the beginning of the local area... |
Chris Lattner | 4ac7d73 | 2003-01-15 22:52:34 +0000 | [diff] [blame^] | 190 | int Offset = TFI.getOffsetOfLocalArea(); |
Chris Lattner | 58b3328 | 2002-12-28 20:43:30 +0000 | [diff] [blame] | 191 | for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) { |
| 192 | Offset += FFI->getObjectSize(i); // Allocate Size bytes... |
| 193 | |
| 194 | unsigned Align = FFI->getObjectAlignment(i); |
| 195 | Offset = (Offset+Align-1)/Align*Align; // Adjust to Alignment boundary... |
| 196 | |
| 197 | FFI->setObjectOffset(i, -Offset); // Set the computed offset |
| 198 | } |
| 199 | |
| 200 | // Align the final stack pointer offset... |
| 201 | unsigned StackAlign = TFI.getStackAlignment(); |
| 202 | Offset = (Offset+StackAlign-1)/StackAlign*StackAlign; |
| 203 | |
| 204 | // Set the final value of the stack pointer... |
Chris Lattner | 4ac7d73 | 2003-01-15 22:52:34 +0000 | [diff] [blame^] | 205 | FFI->setStackSize(Offset-TFI.getOffsetOfLocalArea()); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | /// insertPrologEpilogCode - Scan the function for modified caller saved |
| 210 | /// registers, insert spill code for these caller saved registers, then add |
| 211 | /// prolog and epilog code to the function. |
| 212 | /// |
| 213 | void PEI::insertPrologEpilogCode(MachineFunction &Fn) { |
| 214 | // Add prologue to the function... |
| 215 | Fn.getTarget().getRegisterInfo()->emitPrologue(Fn); |
| 216 | |
| 217 | // Add epilogue to restore the callee-save registers in each exiting block |
| 218 | const TargetInstrInfo &TII = Fn.getTarget().getInstrInfo(); |
| 219 | for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) { |
| 220 | // If last instruction is a return instruction, add an epilogue |
| 221 | if (TII.isReturn(I->back()->getOpcode())) |
| 222 | Fn.getTarget().getRegisterInfo()->emitEpilogue(Fn, *I); |
| 223 | } |
Chris Lattner | 58b3328 | 2002-12-28 20:43:30 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | |
| 227 | /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical |
| 228 | /// register references and actual offsets. |
| 229 | /// |
| 230 | void PEI::replaceFrameIndices(MachineFunction &Fn) { |
| 231 | if (!Fn.getFrameInfo()->hasStackObjects()) return; // Nothing to do? |
| 232 | |
| 233 | const TargetMachine &TM = Fn.getTarget(); |
| 234 | assert(TM.getRegisterInfo() && "TM::getRegisterInfo() must be implemented!"); |
| 235 | const MRegisterInfo &MRI = *TM.getRegisterInfo(); |
| 236 | |
| 237 | for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) |
| 238 | for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) |
| 239 | for (unsigned i = 0, e = (*I)->getNumOperands(); i != e; ++i) |
| 240 | if ((*I)->getOperand(i).isFrameIndex()) { |
| 241 | // If this instruction has a FrameIndex operand, we need to use that |
| 242 | // target machine register info object to eliminate it. |
| 243 | MRI.eliminateFrameIndex(Fn, I); |
| 244 | break; |
| 245 | } |
| 246 | } |