Chris Lattner | 3ea9346 | 2004-08-06 06:58:50 +0000 | [diff] [blame] | 1 | //===-- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function --===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass is responsible for finalizing the functions frame layout, saving |
| 11 | // callee saved registers, and for emitting prolog & epilog code for the |
| 12 | // function. |
| 13 | // |
| 14 | // This pass must be run after register allocation. After this pass is |
| 15 | // executed, it is illegal to construct MO_FrameIndex operands. |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | // |
| 19 | // FIXME: The contents of this file should be merged with the target generic |
| 20 | // CodeGen/PrologEpilogInserter.cpp |
| 21 | // |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
| 24 | #include "PowerPC.h" |
| 25 | #include "llvm/CodeGen/Passes.h" |
| 26 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 27 | #include "llvm/CodeGen/MachineInstr.h" |
| 28 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 29 | #include "llvm/Target/TargetMachine.h" |
| 30 | #include "llvm/Target/MRegisterInfo.h" |
| 31 | #include "llvm/Target/TargetFrameInfo.h" |
| 32 | #include "llvm/Target/TargetInstrInfo.h" |
| 33 | #include "Support/Debug.h" |
| 34 | using namespace llvm; |
| 35 | |
| 36 | namespace { |
| 37 | struct PPCPEI : public MachineFunctionPass { |
| 38 | const char *getPassName() const { |
| 39 | return "PowerPC Frame Finalization & Prolog/Epilog Insertion"; |
| 40 | } |
| 41 | |
| 42 | /// runOnMachineFunction - Insert prolog/epilog code and replace abstract |
| 43 | /// frame indexes with appropriate references. |
| 44 | /// |
| 45 | bool runOnMachineFunction(MachineFunction &Fn) { |
| 46 | RegsToSave.clear(); |
| 47 | StackSlots.clear(); |
| 48 | |
| 49 | // Scan the function for modified caller saved registers and insert spill |
| 50 | // code for any caller saved registers that are modified. Also calculate |
| 51 | // the MaxCallFrameSize and HasCalls variables for the function's frame |
| 52 | // information and eliminates call frame pseudo instructions. |
| 53 | calculateCallerSavedRegisters(Fn); |
| 54 | |
| 55 | // Calculate actual frame offsets for all of the abstract stack objects... |
| 56 | calculateFrameObjectOffsets(Fn); |
| 57 | |
| 58 | // Add prolog and epilog code to the function. |
| 59 | insertPrologEpilogCode(Fn); |
| 60 | |
| 61 | // Add register spills and fills before prolog and after epilog so that in |
| 62 | // the event of a very large fixed size alloca, we don't have to do |
| 63 | // anything weird. |
| 64 | saveCallerSavedRegisters(Fn); |
| 65 | |
| 66 | // Replace all MO_FrameIndex operands with physical register references |
| 67 | // and actual offsets. |
| 68 | // |
| 69 | replaceFrameIndices(Fn); |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | std::vector<unsigned> RegsToSave; |
| 75 | std::vector<int> StackSlots; |
| 76 | |
| 77 | void calculateCallerSavedRegisters(MachineFunction &Fn); |
| 78 | void saveCallerSavedRegisters(MachineFunction &Fn); |
| 79 | void calculateFrameObjectOffsets(MachineFunction &Fn); |
| 80 | void replaceFrameIndices(MachineFunction &Fn); |
| 81 | void insertPrologEpilogCode(MachineFunction &Fn); |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /// createPowerPCPEI - This function returns a pass that inserts |
| 87 | /// prolog and epilog code, and eliminates abstract frame references. |
| 88 | /// |
| 89 | FunctionPass *llvm::createPowerPCPEI() { return new PPCPEI(); } |
| 90 | |
| 91 | |
| 92 | /// calculateCallerSavedRegisters - Scan the function for modified caller saved |
| 93 | /// registers. Also calculate the MaxCallFrameSize and HasCalls variables for |
| 94 | /// the function's frame information and eliminates call frame pseudo |
| 95 | /// instructions. |
| 96 | /// |
| 97 | void PPCPEI::calculateCallerSavedRegisters(MachineFunction &Fn) { |
| 98 | const MRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo(); |
| 99 | const TargetFrameInfo &FrameInfo = *Fn.getTarget().getFrameInfo(); |
| 100 | |
| 101 | // Get the callee saved register list... |
| 102 | const unsigned *CSRegs = RegInfo->getCalleeSaveRegs(); |
| 103 | |
| 104 | // Get the function call frame set-up and tear-down instruction opcode |
| 105 | int FrameSetupOpcode = RegInfo->getCallFrameSetupOpcode(); |
| 106 | int FrameDestroyOpcode = RegInfo->getCallFrameDestroyOpcode(); |
| 107 | |
| 108 | // Early exit for targets which have no callee saved registers and no call |
| 109 | // frame setup/destroy pseudo instructions. |
| 110 | if ((CSRegs == 0 || CSRegs[0] == 0) && |
| 111 | FrameSetupOpcode == -1 && FrameDestroyOpcode == -1) |
| 112 | return; |
| 113 | |
| 114 | // This bitset contains an entry for each physical register for the target... |
| 115 | std::vector<bool> ModifiedRegs(RegInfo->getNumRegs()); |
| 116 | unsigned MaxCallFrameSize = 0; |
| 117 | bool HasCalls = false; |
| 118 | |
| 119 | for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) |
| 120 | for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) |
| 121 | if (I->getOpcode() == FrameSetupOpcode || |
| 122 | I->getOpcode() == FrameDestroyOpcode) { |
| 123 | assert(I->getNumOperands() == 1 && "Call Frame Setup/Destroy Pseudo" |
| 124 | " instructions should have a single immediate argument!"); |
| 125 | unsigned Size = I->getOperand(0).getImmedValue(); |
| 126 | if (Size > MaxCallFrameSize) MaxCallFrameSize = Size; |
| 127 | HasCalls = true; |
| 128 | RegInfo->eliminateCallFramePseudoInstr(Fn, *BB, I++); |
| 129 | } else { |
| 130 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 131 | MachineOperand &MO = I->getOperand(i); |
| 132 | if (MO.isRegister() && MO.isDef()) { |
| 133 | assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) && |
| 134 | "Register allocation must be performed!"); |
| 135 | ModifiedRegs[MO.getReg()] = true; // Register is modified |
| 136 | } |
| 137 | } |
| 138 | ++I; |
| 139 | } |
| 140 | |
| 141 | MachineFrameInfo *FFI = Fn.getFrameInfo(); |
| 142 | FFI->setHasCalls(HasCalls); |
| 143 | FFI->setMaxCallFrameSize(MaxCallFrameSize); |
| 144 | |
| 145 | // Now figure out which *callee saved* registers are modified by the current |
| 146 | // function, thus needing to be saved and restored in the prolog/epilog. |
| 147 | // |
| 148 | for (unsigned i = 0; CSRegs[i]; ++i) { |
| 149 | unsigned Reg = CSRegs[i]; |
| 150 | if (ModifiedRegs[Reg]) { |
| 151 | RegsToSave.push_back(Reg); // If modified register... |
| 152 | } else { |
| 153 | for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg); |
| 154 | *AliasSet; ++AliasSet) { // Check alias registers too... |
| 155 | if (ModifiedRegs[*AliasSet]) { |
| 156 | RegsToSave.push_back(Reg); |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // FIXME: should we sort the regs to save so that we always get the regs in |
| 164 | // the correct order? |
| 165 | |
| 166 | // Now that we know which registers need to be saved and restored, allocate |
| 167 | // stack slots for them. |
| 168 | int Offset = 0; |
| 169 | for (unsigned i = 0, e = RegsToSave.size(); i != e; ++i) { |
| 170 | unsigned RegSize = RegInfo->getRegClass(RegsToSave[i])->getSize(); |
| 171 | int FrameIdx; |
| 172 | |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 173 | if (RegsToSave[i] == PPC::LR) { |
Chris Lattner | 3ea9346 | 2004-08-06 06:58:50 +0000 | [diff] [blame] | 174 | FrameIdx = FFI->CreateFixedObject(RegSize, 8); // LR lives at +8 |
| 175 | } else { |
| 176 | Offset -= RegSize; |
| 177 | FrameIdx = FFI->CreateFixedObject(RegSize, Offset); |
| 178 | } |
| 179 | StackSlots.push_back(FrameIdx); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | |
| 184 | /// saveCallerSavedRegisters - Insert spill code for any caller saved registers |
| 185 | /// that are modified in the function. |
| 186 | /// |
| 187 | void PPCPEI::saveCallerSavedRegisters(MachineFunction &Fn) { |
| 188 | // Early exit if no caller saved registers are modified! |
| 189 | if (RegsToSave.empty()) |
| 190 | return; |
| 191 | |
| 192 | const MRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo(); |
| 193 | |
| 194 | // Now that we have a stack slot for each register to be saved, insert spill |
| 195 | // code into the entry block... |
| 196 | MachineBasicBlock *MBB = Fn.begin(); |
| 197 | MachineBasicBlock::iterator I = MBB->begin(); |
| 198 | for (unsigned i = 0, e = RegsToSave.size(); i != e; ++i) { |
| 199 | const TargetRegisterClass *RC = RegInfo->getRegClass(RegsToSave[i]); |
| 200 | // Insert the spill to the stack frame... |
| 201 | RegInfo->storeRegToStackSlot(*MBB, I, RegsToSave[i], StackSlots[i], RC); |
| 202 | } |
| 203 | |
| 204 | // Add code to restore the callee-save registers in each exiting block. |
| 205 | const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo(); |
| 206 | for (MachineFunction::iterator FI = Fn.begin(), E = Fn.end(); FI != E; ++FI) { |
| 207 | // If last instruction is a return instruction, add an epilogue |
| 208 | if (!FI->empty() && TII.isReturn(FI->back().getOpcode())) { |
| 209 | MBB = FI; |
| 210 | I = MBB->end(); --I; |
| 211 | |
| 212 | for (unsigned i = 0, e = RegsToSave.size(); i != e; ++i) { |
| 213 | const TargetRegisterClass *RC = RegInfo->getRegClass(RegsToSave[i]); |
| 214 | RegInfo->loadRegFromStackSlot(*MBB, I, RegsToSave[i],StackSlots[i], RC); |
| 215 | --I; // Insert in reverse order |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | |
| 222 | /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the |
| 223 | /// abstract stack objects... |
| 224 | /// |
| 225 | void PPCPEI::calculateFrameObjectOffsets(MachineFunction &Fn) { |
| 226 | const TargetFrameInfo &TFI = *Fn.getTarget().getFrameInfo(); |
| 227 | |
| 228 | bool StackGrowsDown = |
| 229 | TFI.getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown; |
| 230 | |
| 231 | // Loop over all of the stack objects, assigning sequential addresses... |
| 232 | MachineFrameInfo *FFI = Fn.getFrameInfo(); |
| 233 | |
| 234 | unsigned StackAlignment = TFI.getStackAlignment(); |
| 235 | |
| 236 | // Start at the beginning of the local area. |
| 237 | // The Offset is the distance from the stack top in the direction |
| 238 | // of stack growth -- so it's always positive. |
| 239 | int Offset = TFI.getOffsetOfLocalArea(); |
| 240 | if (StackGrowsDown) |
| 241 | Offset = -Offset; |
| 242 | assert(Offset >= 0 |
| 243 | && "Local area offset should be in direction of stack growth"); |
| 244 | |
| 245 | // If there are fixed sized objects that are preallocated in the local area, |
| 246 | // non-fixed objects can't be allocated right at the start of local area. |
| 247 | // We currently don't support filling in holes in between fixed sized objects, |
| 248 | // so we adjust 'Offset' to point to the end of last fixed sized |
| 249 | // preallocated object. |
| 250 | for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) { |
| 251 | int FixedOff; |
| 252 | if (StackGrowsDown) { |
| 253 | // The maximum distance from the stack pointer is at lower address of |
| 254 | // the object -- which is given by offset. For down growing stack |
| 255 | // the offset is negative, so we negate the offset to get the distance. |
| 256 | FixedOff = -FFI->getObjectOffset(i); |
| 257 | } else { |
| 258 | // The maximum distance from the start pointer is at the upper |
| 259 | // address of the object. |
| 260 | FixedOff = FFI->getObjectOffset(i) + FFI->getObjectSize(i); |
| 261 | } |
| 262 | if (FixedOff > Offset) Offset = FixedOff; |
| 263 | } |
| 264 | |
| 265 | for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) { |
| 266 | // If stack grows down, we need to add size of find the lowest |
| 267 | // address of the object. |
| 268 | if (StackGrowsDown) |
| 269 | Offset += FFI->getObjectSize(i); |
| 270 | |
| 271 | unsigned Align = FFI->getObjectAlignment(i); |
| 272 | assert(Align <= StackAlignment && "Cannot align stack object to higher " |
| 273 | "alignment boundary than the stack itself!"); |
| 274 | Offset = (Offset+Align-1)/Align*Align; // Adjust to Alignment boundary... |
| 275 | |
| 276 | if (StackGrowsDown) { |
| 277 | FFI->setObjectOffset(i, -Offset); // Set the computed offset |
| 278 | } else { |
| 279 | FFI->setObjectOffset(i, Offset); |
| 280 | Offset += FFI->getObjectSize(i); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // Set the final value of the stack pointer... |
| 285 | FFI->setStackSize(Offset); |
| 286 | } |
| 287 | |
| 288 | |
| 289 | /// insertPrologEpilogCode - Scan the function for modified caller saved |
| 290 | /// registers, insert spill code for these caller saved registers, then add |
| 291 | /// prolog and epilog code to the function. |
| 292 | /// |
| 293 | void PPCPEI::insertPrologEpilogCode(MachineFunction &Fn) { |
| 294 | // Add prologue to the function... |
| 295 | Fn.getTarget().getRegisterInfo()->emitPrologue(Fn); |
| 296 | |
| 297 | // Add epilogue to restore the callee-save registers in each exiting block |
| 298 | const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo(); |
| 299 | for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) { |
| 300 | // If last instruction is a return instruction, add an epilogue |
| 301 | if (!I->empty() && TII.isReturn(I->back().getOpcode())) |
| 302 | Fn.getTarget().getRegisterInfo()->emitEpilogue(Fn, *I); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | |
| 307 | /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical |
| 308 | /// register references and actual offsets. |
| 309 | /// |
| 310 | void PPCPEI::replaceFrameIndices(MachineFunction &Fn) { |
| 311 | if (!Fn.getFrameInfo()->hasStackObjects()) return; // Nothing to do? |
| 312 | |
| 313 | const TargetMachine &TM = Fn.getTarget(); |
| 314 | assert(TM.getRegisterInfo() && "TM::getRegisterInfo() must be implemented!"); |
| 315 | const MRegisterInfo &MRI = *TM.getRegisterInfo(); |
| 316 | |
| 317 | for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) |
| 318 | for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) |
| 319 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 320 | if (I->getOperand(i).isFrameIndex()) { |
| 321 | // If this instruction has a FrameIndex operand, we need to use that |
| 322 | // target machine register info object to eliminate it. |
| 323 | MRI.eliminateFrameIndex(Fn, I); |
| 324 | break; |
| 325 | } |
| 326 | } |