| Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 1 | //===-- WebAssemblyPEI.cpp - Insert Prolog/Epilog code in function --===// | 
|  | 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 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 | // This is a copy of lib/CodeGen/PrologEpilogInserter.cpp except that it does | 
|  | 18 | // not assert that all virtual registers are gone (because WebAssembly currently | 
|  | 19 | // uses virtual rather than physical registers), and only runs | 
|  | 20 | // MRI.clearVirtRegs() if scavenging happened (which it never does). It also | 
|  | 21 | // uses a different class name so it can be registered via INITIALIZE_PASS. | 
|  | 22 | // It is otherwise unmodified, so any changes to the target-independent PEI | 
|  | 23 | // can be easily applied. | 
|  | 24 | //===----------------------------------------------------------------------===// | 
|  | 25 |  | 
| Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/STLExtras.h" | 
|  | 27 | #include "llvm/ADT/SetVector.h" | 
|  | 28 | #include "llvm/ADT/SmallSet.h" | 
|  | 29 | #include "llvm/ADT/Statistic.h" | 
|  | 30 | #include "llvm/CodeGen/MachineDominators.h" | 
|  | 31 | #include "llvm/CodeGen/MachineFrameInfo.h" | 
|  | 32 | #include "llvm/CodeGen/MachineInstr.h" | 
|  | 33 | #include "llvm/CodeGen/MachineLoopInfo.h" | 
|  | 34 | #include "llvm/CodeGen/MachineModuleInfo.h" | 
|  | 35 | #include "llvm/CodeGen/MachineRegisterInfo.h" | 
|  | 36 | #include "llvm/CodeGen/Passes.h" | 
|  | 37 | #include "llvm/CodeGen/RegisterScavenging.h" | 
|  | 38 | #include "llvm/CodeGen/StackProtector.h" | 
|  | 39 | #include "llvm/CodeGen/WinEHFuncInfo.h" | 
|  | 40 | #include "llvm/IR/DiagnosticInfo.h" | 
|  | 41 | #include "llvm/IR/InlineAsm.h" | 
|  | 42 | #include "llvm/IR/LLVMContext.h" | 
|  | 43 | #include "llvm/Support/CommandLine.h" | 
| Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 44 | #include "llvm/Support/Debug.h" | 
|  | 45 | #include "llvm/Support/raw_ostream.h" | 
|  | 46 | #include "llvm/Target/TargetFrameLowering.h" | 
|  | 47 | #include "llvm/Target/TargetInstrInfo.h" | 
|  | 48 | #include "llvm/Target/TargetMachine.h" | 
|  | 49 | #include "llvm/Target/TargetRegisterInfo.h" | 
|  | 50 | #include "llvm/Target/TargetSubtargetInfo.h" | 
|  | 51 | #include <climits> | 
|  | 52 |  | 
|  | 53 | using namespace llvm; | 
|  | 54 |  | 
|  | 55 | #define DEBUG_TYPE "pei" | 
|  | 56 | namespace llvm { | 
|  | 57 | void initializeWasmPEIPass(PassRegistry&); | 
|  | 58 | } | 
|  | 59 | namespace { | 
| Dan Gohman | da323e8 | 2016-03-11 19:45:37 +0000 | [diff] [blame] | 60 | class WasmPEI final : public MachineFunctionPass { | 
| Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 61 | public: | 
|  | 62 | static char ID; | 
|  | 63 | WasmPEI() : MachineFunctionPass(ID) { | 
|  | 64 | initializeWasmPEIPass(*PassRegistry::getPassRegistry()); | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | void getAnalysisUsage(AnalysisUsage &AU) const override; | 
|  | 68 |  | 
|  | 69 | /// runOnMachineFunction - Insert prolog/epilog code and replace abstract | 
|  | 70 | /// frame indexes with appropriate references. | 
|  | 71 | /// | 
|  | 72 | bool runOnMachineFunction(MachineFunction &Fn) override; | 
|  | 73 |  | 
|  | 74 | private: | 
|  | 75 | RegScavenger *RS; | 
|  | 76 |  | 
|  | 77 | // MinCSFrameIndex, MaxCSFrameIndex - Keeps the range of callee saved | 
|  | 78 | // stack frame indexes. | 
|  | 79 | unsigned MinCSFrameIndex, MaxCSFrameIndex; | 
|  | 80 |  | 
|  | 81 | // Save and Restore blocks of the current function. Typically there is a | 
|  | 82 | // single save block, unless Windows EH funclets are involved. | 
|  | 83 | SmallVector<MachineBasicBlock *, 1> SaveBlocks; | 
|  | 84 | SmallVector<MachineBasicBlock *, 4> RestoreBlocks; | 
|  | 85 |  | 
|  | 86 | // Flag to control whether to use the register scavenger to resolve | 
|  | 87 | // frame index materialization registers. Set according to | 
|  | 88 | // TRI->requiresFrameIndexScavenging() for the current function. | 
|  | 89 | bool FrameIndexVirtualScavenging; | 
|  | 90 |  | 
|  | 91 | void calculateSets(MachineFunction &Fn); | 
|  | 92 | void calculateCallsInformation(MachineFunction &Fn); | 
|  | 93 | void assignCalleeSavedSpillSlots(MachineFunction &Fn, | 
|  | 94 | const BitVector &SavedRegs); | 
|  | 95 | void insertCSRSpillsAndRestores(MachineFunction &Fn); | 
|  | 96 | void calculateFrameObjectOffsets(MachineFunction &Fn); | 
|  | 97 | void replaceFrameIndices(MachineFunction &Fn); | 
|  | 98 | void replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &Fn, | 
|  | 99 | int &SPAdj); | 
|  | 100 | void scavengeFrameVirtualRegs(MachineFunction &Fn); | 
|  | 101 | void insertPrologEpilogCode(MachineFunction &Fn); | 
|  | 102 | }; | 
|  | 103 | } // namespace | 
|  | 104 |  | 
|  | 105 | char WasmPEI::ID = 0; | 
|  | 106 |  | 
|  | 107 | namespace llvm { | 
|  | 108 | FunctionPass *createWebAssemblyPEI() { | 
|  | 109 | return new WasmPEI(); | 
|  | 110 | } | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | static cl::opt<unsigned> | 
|  | 114 | WarnStackSize("wasm-warn-stack-size", cl::Hidden, cl::init((unsigned)-1), | 
|  | 115 | cl::desc("Warn for stack size bigger than the given" | 
|  | 116 | " number")); | 
|  | 117 |  | 
|  | 118 | INITIALIZE_PASS_BEGIN(WasmPEI, "wasmprologepilog", | 
|  | 119 | "Wasm Prologue/Epilogue Insertion", false, false) | 
|  | 120 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) | 
|  | 121 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) | 
|  | 122 | INITIALIZE_PASS_DEPENDENCY(StackProtector) | 
|  | 123 | INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) | 
|  | 124 | INITIALIZE_PASS_END(WasmPEI, "wasmprologepilog", | 
|  | 125 | "Wasm Prologue/Epilogue Insertion & Frame Finalization", | 
|  | 126 | false, false) | 
|  | 127 |  | 
|  | 128 | STATISTIC(NumScavengedRegs, "Number of frame index regs scavenged"); | 
|  | 129 | STATISTIC(NumBytesStackSpace, | 
|  | 130 | "Number of bytes used for stack in all functions"); | 
|  | 131 |  | 
|  | 132 | void WasmPEI::getAnalysisUsage(AnalysisUsage &AU) const { | 
|  | 133 | AU.setPreservesCFG(); | 
|  | 134 | AU.addPreserved<MachineLoopInfo>(); | 
|  | 135 | AU.addPreserved<MachineDominatorTree>(); | 
|  | 136 | AU.addRequired<StackProtector>(); | 
|  | 137 | AU.addRequired<TargetPassConfig>(); | 
|  | 138 | MachineFunctionPass::getAnalysisUsage(AU); | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | /// Compute the set of return blocks | 
|  | 142 | void WasmPEI::calculateSets(MachineFunction &Fn) { | 
|  | 143 | const MachineFrameInfo *MFI = Fn.getFrameInfo(); | 
|  | 144 |  | 
|  | 145 | // Even when we do not change any CSR, we still want to insert the | 
|  | 146 | // prologue and epilogue of the function. | 
|  | 147 | // So set the save points for those. | 
|  | 148 |  | 
|  | 149 | // Use the points found by shrink-wrapping, if any. | 
|  | 150 | if (MFI->getSavePoint()) { | 
|  | 151 | SaveBlocks.push_back(MFI->getSavePoint()); | 
|  | 152 | assert(MFI->getRestorePoint() && "Both restore and save must be set"); | 
|  | 153 | MachineBasicBlock *RestoreBlock = MFI->getRestorePoint(); | 
|  | 154 | // If RestoreBlock does not have any successor and is not a return block | 
|  | 155 | // then the end point is unreachable and we do not need to insert any | 
|  | 156 | // epilogue. | 
|  | 157 | if (!RestoreBlock->succ_empty() || RestoreBlock->isReturnBlock()) | 
|  | 158 | RestoreBlocks.push_back(RestoreBlock); | 
|  | 159 | return; | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | // Save refs to entry and return blocks. | 
|  | 163 | SaveBlocks.push_back(&Fn.front()); | 
|  | 164 | for (MachineBasicBlock &MBB : Fn) { | 
|  | 165 | if (MBB.isEHFuncletEntry()) | 
|  | 166 | SaveBlocks.push_back(&MBB); | 
|  | 167 | if (MBB.isReturnBlock()) | 
|  | 168 | RestoreBlocks.push_back(&MBB); | 
|  | 169 | } | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | /// StackObjSet - A set of stack object indexes | 
|  | 173 | typedef SmallSetVector<int, 8> StackObjSet; | 
|  | 174 |  | 
|  | 175 | /// runOnMachineFunction - Insert prolog/epilog code and replace abstract | 
|  | 176 | /// frame indexes with appropriate references. | 
|  | 177 | /// | 
|  | 178 | bool WasmPEI::runOnMachineFunction(MachineFunction &Fn) { | 
|  | 179 | const Function* F = Fn.getFunction(); | 
|  | 180 | const TargetRegisterInfo *TRI = Fn.getSubtarget().getRegisterInfo(); | 
|  | 181 | const TargetFrameLowering *TFI = Fn.getSubtarget().getFrameLowering(); | 
|  | 182 |  | 
|  | 183 | // LOCALMOD: assert removed from target-independent PEI | 
|  | 184 | //assert(!Fn.getRegInfo().getNumVirtRegs() && "Regalloc must assign all vregs"); | 
|  | 185 |  | 
|  | 186 | RS = TRI->requiresRegisterScavenging(Fn) ? new RegScavenger() : nullptr; | 
|  | 187 | FrameIndexVirtualScavenging = TRI->requiresFrameIndexScavenging(Fn); | 
|  | 188 |  | 
|  | 189 | // Calculate the MaxCallFrameSize and AdjustsStack variables for the | 
|  | 190 | // function's frame information. Also eliminates call frame pseudo | 
|  | 191 | // instructions. | 
|  | 192 | calculateCallsInformation(Fn); | 
|  | 193 |  | 
|  | 194 | // Determine which of the registers in the callee save list should be saved. | 
|  | 195 | BitVector SavedRegs; | 
|  | 196 | TFI->determineCalleeSaves(Fn, SavedRegs, RS); | 
|  | 197 |  | 
|  | 198 | // Insert spill code for any callee saved registers that are modified. | 
|  | 199 | assignCalleeSavedSpillSlots(Fn, SavedRegs); | 
|  | 200 |  | 
|  | 201 | // Determine placement of CSR spill/restore code: | 
|  | 202 | // place all spills in the entry block, all restores in return blocks. | 
|  | 203 | calculateSets(Fn); | 
|  | 204 |  | 
|  | 205 | // Add the code to save and restore the callee saved registers. | 
|  | 206 | if (!F->hasFnAttribute(Attribute::Naked)) | 
|  | 207 | insertCSRSpillsAndRestores(Fn); | 
|  | 208 |  | 
|  | 209 | // Allow the target machine to make final modifications to the function | 
|  | 210 | // before the frame layout is finalized. | 
|  | 211 | TFI->processFunctionBeforeFrameFinalized(Fn, RS); | 
|  | 212 |  | 
|  | 213 | // Calculate actual frame offsets for all abstract stack objects... | 
|  | 214 | calculateFrameObjectOffsets(Fn); | 
|  | 215 |  | 
|  | 216 | // Add prolog and epilog code to the function.  This function is required | 
|  | 217 | // to align the stack frame as necessary for any stack variables or | 
|  | 218 | // called functions.  Because of this, calculateCalleeSavedRegisters() | 
|  | 219 | // must be called before this function in order to set the AdjustsStack | 
|  | 220 | // and MaxCallFrameSize variables. | 
|  | 221 | if (!F->hasFnAttribute(Attribute::Naked)) | 
|  | 222 | insertPrologEpilogCode(Fn); | 
|  | 223 |  | 
|  | 224 | // Replace all MO_FrameIndex operands with physical register references | 
|  | 225 | // and actual offsets. | 
|  | 226 | // | 
|  | 227 | replaceFrameIndices(Fn); | 
|  | 228 |  | 
|  | 229 | // If register scavenging is needed, as we've enabled doing it as a | 
|  | 230 | // post-pass, scavenge the virtual registers that frame index elimination | 
|  | 231 | // inserted. | 
|  | 232 | if (TRI->requiresRegisterScavenging(Fn) && FrameIndexVirtualScavenging) { | 
|  | 233 | scavengeFrameVirtualRegs(Fn); | 
|  | 234 | // Clear any vregs created by virtual scavenging. | 
|  | 235 | // LOCALMOD: made this call conditional with scavengeFrameVirtualregs() | 
|  | 236 | Fn.getRegInfo().clearVirtRegs(); | 
|  | 237 | } | 
|  | 238 |  | 
|  | 239 | // Warn on stack size when we exceeds the given limit. | 
|  | 240 | MachineFrameInfo *MFI = Fn.getFrameInfo(); | 
|  | 241 | uint64_t StackSize = MFI->getStackSize(); | 
|  | 242 | if (WarnStackSize.getNumOccurrences() > 0 && WarnStackSize < StackSize) { | 
|  | 243 | DiagnosticInfoStackSize DiagStackSize(*F, StackSize); | 
|  | 244 | F->getContext().diagnose(DiagStackSize); | 
|  | 245 | } | 
|  | 246 |  | 
|  | 247 | delete RS; | 
|  | 248 | SaveBlocks.clear(); | 
|  | 249 | RestoreBlocks.clear(); | 
|  | 250 | return true; | 
|  | 251 | } | 
|  | 252 |  | 
|  | 253 | /// calculateCallsInformation - Calculate the MaxCallFrameSize and AdjustsStack | 
|  | 254 | /// variables for the function's frame information and eliminate call frame | 
|  | 255 | /// pseudo instructions. | 
|  | 256 | void WasmPEI::calculateCallsInformation(MachineFunction &Fn) { | 
|  | 257 | const TargetInstrInfo &TII = *Fn.getSubtarget().getInstrInfo(); | 
|  | 258 | const TargetFrameLowering *TFI = Fn.getSubtarget().getFrameLowering(); | 
|  | 259 | MachineFrameInfo *MFI = Fn.getFrameInfo(); | 
|  | 260 |  | 
|  | 261 | unsigned MaxCallFrameSize = 0; | 
|  | 262 | bool AdjustsStack = MFI->adjustsStack(); | 
|  | 263 |  | 
|  | 264 | // Get the function call frame set-up and tear-down instruction opcode | 
|  | 265 | unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode(); | 
|  | 266 | unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode(); | 
|  | 267 |  | 
|  | 268 | // Early exit for targets which have no call frame setup/destroy pseudo | 
|  | 269 | // instructions. | 
|  | 270 | if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u) | 
|  | 271 | return; | 
|  | 272 |  | 
|  | 273 | std::vector<MachineBasicBlock::iterator> FrameSDOps; | 
|  | 274 | for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) | 
|  | 275 | for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) | 
|  | 276 | if (I->getOpcode() == FrameSetupOpcode || | 
|  | 277 | I->getOpcode() == FrameDestroyOpcode) { | 
|  | 278 | assert(I->getNumOperands() >= 1 && "Call Frame Setup/Destroy Pseudo" | 
|  | 279 | " instructions should have a single immediate argument!"); | 
|  | 280 | unsigned Size = I->getOperand(0).getImm(); | 
|  | 281 | if (Size > MaxCallFrameSize) MaxCallFrameSize = Size; | 
|  | 282 | AdjustsStack = true; | 
|  | 283 | FrameSDOps.push_back(I); | 
|  | 284 | } else if (I->isInlineAsm()) { | 
|  | 285 | // Some inline asm's need a stack frame, as indicated by operand 1. | 
|  | 286 | unsigned ExtraInfo = I->getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); | 
|  | 287 | if (ExtraInfo & InlineAsm::Extra_IsAlignStack) | 
|  | 288 | AdjustsStack = true; | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | MFI->setAdjustsStack(AdjustsStack); | 
|  | 292 | MFI->setMaxCallFrameSize(MaxCallFrameSize); | 
|  | 293 |  | 
|  | 294 | for (std::vector<MachineBasicBlock::iterator>::iterator | 
|  | 295 | i = FrameSDOps.begin(), e = FrameSDOps.end(); i != e; ++i) { | 
|  | 296 | MachineBasicBlock::iterator I = *i; | 
|  | 297 |  | 
|  | 298 | // If call frames are not being included as part of the stack frame, and | 
|  | 299 | // the target doesn't indicate otherwise, remove the call frame pseudos | 
|  | 300 | // here. The sub/add sp instruction pairs are still inserted, but we don't | 
|  | 301 | // need to track the SP adjustment for frame index elimination. | 
|  | 302 | if (TFI->canSimplifyCallFramePseudos(Fn)) | 
|  | 303 | TFI->eliminateCallFramePseudoInstr(Fn, *I->getParent(), I); | 
|  | 304 | } | 
|  | 305 | } | 
|  | 306 |  | 
|  | 307 | void WasmPEI::assignCalleeSavedSpillSlots(MachineFunction &F, | 
|  | 308 | const BitVector &SavedRegs) { | 
|  | 309 | // These are used to keep track the callee-save area. Initialize them. | 
|  | 310 | MinCSFrameIndex = INT_MAX; | 
|  | 311 | MaxCSFrameIndex = 0; | 
|  | 312 |  | 
|  | 313 | if (SavedRegs.empty()) | 
|  | 314 | return; | 
|  | 315 |  | 
|  | 316 | const TargetRegisterInfo *RegInfo = F.getSubtarget().getRegisterInfo(); | 
|  | 317 | const MCPhysReg *CSRegs = RegInfo->getCalleeSavedRegs(&F); | 
|  | 318 |  | 
|  | 319 | std::vector<CalleeSavedInfo> CSI; | 
|  | 320 | for (unsigned i = 0; CSRegs[i]; ++i) { | 
|  | 321 | unsigned Reg = CSRegs[i]; | 
|  | 322 | if (SavedRegs.test(Reg)) | 
|  | 323 | CSI.push_back(CalleeSavedInfo(Reg)); | 
|  | 324 | } | 
|  | 325 |  | 
|  | 326 | const TargetFrameLowering *TFI = F.getSubtarget().getFrameLowering(); | 
|  | 327 | MachineFrameInfo *MFI = F.getFrameInfo(); | 
|  | 328 | if (!TFI->assignCalleeSavedSpillSlots(F, RegInfo, CSI)) { | 
|  | 329 | // If target doesn't implement this, use generic code. | 
|  | 330 |  | 
|  | 331 | if (CSI.empty()) | 
|  | 332 | return; // Early exit if no callee saved registers are modified! | 
|  | 333 |  | 
|  | 334 | unsigned NumFixedSpillSlots; | 
|  | 335 | const TargetFrameLowering::SpillSlot *FixedSpillSlots = | 
|  | 336 | TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots); | 
|  | 337 |  | 
|  | 338 | // Now that we know which registers need to be saved and restored, allocate | 
|  | 339 | // stack slots for them. | 
|  | 340 | for (std::vector<CalleeSavedInfo>::iterator I = CSI.begin(), E = CSI.end(); | 
|  | 341 | I != E; ++I) { | 
|  | 342 | unsigned Reg = I->getReg(); | 
|  | 343 | const TargetRegisterClass *RC = RegInfo->getMinimalPhysRegClass(Reg); | 
|  | 344 |  | 
|  | 345 | int FrameIdx; | 
|  | 346 | if (RegInfo->hasReservedSpillSlot(F, Reg, FrameIdx)) { | 
|  | 347 | I->setFrameIdx(FrameIdx); | 
|  | 348 | continue; | 
|  | 349 | } | 
|  | 350 |  | 
|  | 351 | // Check to see if this physreg must be spilled to a particular stack slot | 
|  | 352 | // on this target. | 
|  | 353 | const TargetFrameLowering::SpillSlot *FixedSlot = FixedSpillSlots; | 
|  | 354 | while (FixedSlot != FixedSpillSlots + NumFixedSpillSlots && | 
|  | 355 | FixedSlot->Reg != Reg) | 
|  | 356 | ++FixedSlot; | 
|  | 357 |  | 
|  | 358 | if (FixedSlot == FixedSpillSlots + NumFixedSpillSlots) { | 
|  | 359 | // Nope, just spill it anywhere convenient. | 
|  | 360 | unsigned Align = RC->getAlignment(); | 
|  | 361 | unsigned StackAlign = TFI->getStackAlignment(); | 
|  | 362 |  | 
|  | 363 | // We may not be able to satisfy the desired alignment specification of | 
|  | 364 | // the TargetRegisterClass if the stack alignment is smaller. Use the | 
|  | 365 | // min. | 
|  | 366 | Align = std::min(Align, StackAlign); | 
|  | 367 | FrameIdx = MFI->CreateStackObject(RC->getSize(), Align, true); | 
|  | 368 | if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx; | 
|  | 369 | if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx; | 
|  | 370 | } else { | 
|  | 371 | // Spill it to the stack where we must. | 
|  | 372 | FrameIdx = | 
|  | 373 | MFI->CreateFixedSpillStackObject(RC->getSize(), FixedSlot->Offset); | 
|  | 374 | } | 
|  | 375 |  | 
|  | 376 | I->setFrameIdx(FrameIdx); | 
|  | 377 | } | 
|  | 378 | } | 
|  | 379 |  | 
|  | 380 | MFI->setCalleeSavedInfo(CSI); | 
|  | 381 | } | 
|  | 382 |  | 
|  | 383 | /// Helper function to update the liveness information for the callee-saved | 
|  | 384 | /// registers. | 
|  | 385 | static void updateLiveness(MachineFunction &MF) { | 
|  | 386 | MachineFrameInfo *MFI = MF.getFrameInfo(); | 
|  | 387 | // Visited will contain all the basic blocks that are in the region | 
|  | 388 | // where the callee saved registers are alive: | 
|  | 389 | // - Anything that is not Save or Restore -> LiveThrough. | 
|  | 390 | // - Save -> LiveIn. | 
|  | 391 | // - Restore -> LiveOut. | 
|  | 392 | // The live-out is not attached to the block, so no need to keep | 
|  | 393 | // Restore in this set. | 
|  | 394 | SmallPtrSet<MachineBasicBlock *, 8> Visited; | 
|  | 395 | SmallVector<MachineBasicBlock *, 8> WorkList; | 
|  | 396 | MachineBasicBlock *Entry = &MF.front(); | 
|  | 397 | MachineBasicBlock *Save = MFI->getSavePoint(); | 
|  | 398 |  | 
|  | 399 | if (!Save) | 
|  | 400 | Save = Entry; | 
|  | 401 |  | 
|  | 402 | if (Entry != Save) { | 
|  | 403 | WorkList.push_back(Entry); | 
|  | 404 | Visited.insert(Entry); | 
|  | 405 | } | 
|  | 406 | Visited.insert(Save); | 
|  | 407 |  | 
|  | 408 | MachineBasicBlock *Restore = MFI->getRestorePoint(); | 
|  | 409 | if (Restore) | 
|  | 410 | // By construction Restore cannot be visited, otherwise it | 
|  | 411 | // means there exists a path to Restore that does not go | 
|  | 412 | // through Save. | 
|  | 413 | WorkList.push_back(Restore); | 
|  | 414 |  | 
|  | 415 | while (!WorkList.empty()) { | 
|  | 416 | const MachineBasicBlock *CurBB = WorkList.pop_back_val(); | 
|  | 417 | // By construction, the region that is after the save point is | 
|  | 418 | // dominated by the Save and post-dominated by the Restore. | 
|  | 419 | if (CurBB == Save && Save != Restore) | 
|  | 420 | continue; | 
|  | 421 | // Enqueue all the successors not already visited. | 
|  | 422 | // Those are by construction either before Save or after Restore. | 
|  | 423 | for (MachineBasicBlock *SuccBB : CurBB->successors()) | 
|  | 424 | if (Visited.insert(SuccBB).second) | 
|  | 425 | WorkList.push_back(SuccBB); | 
|  | 426 | } | 
|  | 427 |  | 
|  | 428 | const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); | 
|  | 429 |  | 
|  | 430 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { | 
|  | 431 | for (MachineBasicBlock *MBB : Visited) { | 
|  | 432 | MCPhysReg Reg = CSI[i].getReg(); | 
|  | 433 | // Add the callee-saved register as live-in. | 
|  | 434 | // It's killed at the spill. | 
|  | 435 | if (!MBB->isLiveIn(Reg)) | 
|  | 436 | MBB->addLiveIn(Reg); | 
|  | 437 | } | 
|  | 438 | } | 
|  | 439 | } | 
|  | 440 |  | 
|  | 441 | /// insertCSRSpillsAndRestores - Insert spill and restore code for | 
|  | 442 | /// callee saved registers used in the function. | 
|  | 443 | /// | 
|  | 444 | void WasmPEI::insertCSRSpillsAndRestores(MachineFunction &Fn) { | 
|  | 445 | // Get callee saved register information. | 
|  | 446 | MachineFrameInfo *MFI = Fn.getFrameInfo(); | 
|  | 447 | const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); | 
|  | 448 |  | 
|  | 449 | MFI->setCalleeSavedInfoValid(true); | 
|  | 450 |  | 
|  | 451 | // Early exit if no callee saved registers are modified! | 
|  | 452 | if (CSI.empty()) | 
|  | 453 | return; | 
|  | 454 |  | 
|  | 455 | const TargetInstrInfo &TII = *Fn.getSubtarget().getInstrInfo(); | 
|  | 456 | const TargetFrameLowering *TFI = Fn.getSubtarget().getFrameLowering(); | 
|  | 457 | const TargetRegisterInfo *TRI = Fn.getSubtarget().getRegisterInfo(); | 
|  | 458 | MachineBasicBlock::iterator I; | 
|  | 459 |  | 
|  | 460 | // Spill using target interface. | 
|  | 461 | for (MachineBasicBlock *SaveBlock : SaveBlocks) { | 
|  | 462 | I = SaveBlock->begin(); | 
|  | 463 | if (!TFI->spillCalleeSavedRegisters(*SaveBlock, I, CSI, TRI)) { | 
|  | 464 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { | 
|  | 465 | // Insert the spill to the stack frame. | 
|  | 466 | unsigned Reg = CSI[i].getReg(); | 
|  | 467 | const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); | 
|  | 468 | TII.storeRegToStackSlot(*SaveBlock, I, Reg, true, CSI[i].getFrameIdx(), | 
|  | 469 | RC, TRI); | 
|  | 470 | } | 
|  | 471 | } | 
|  | 472 | // Update the live-in information of all the blocks up to the save point. | 
|  | 473 | updateLiveness(Fn); | 
|  | 474 | } | 
|  | 475 |  | 
|  | 476 | // Restore using target interface. | 
|  | 477 | for (MachineBasicBlock *MBB : RestoreBlocks) { | 
|  | 478 | I = MBB->end(); | 
|  | 479 |  | 
|  | 480 | // Skip over all terminator instructions, which are part of the return | 
|  | 481 | // sequence. | 
|  | 482 | MachineBasicBlock::iterator I2 = I; | 
|  | 483 | while (I2 != MBB->begin() && (--I2)->isTerminator()) | 
|  | 484 | I = I2; | 
|  | 485 |  | 
|  | 486 | bool AtStart = I == MBB->begin(); | 
|  | 487 | MachineBasicBlock::iterator BeforeI = I; | 
|  | 488 | if (!AtStart) | 
|  | 489 | --BeforeI; | 
|  | 490 |  | 
|  | 491 | // Restore all registers immediately before the return and any | 
|  | 492 | // terminators that precede it. | 
|  | 493 | if (!TFI->restoreCalleeSavedRegisters(*MBB, I, CSI, TRI)) { | 
|  | 494 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) { | 
|  | 495 | unsigned Reg = CSI[i].getReg(); | 
|  | 496 | const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); | 
|  | 497 | TII.loadRegFromStackSlot(*MBB, I, Reg, CSI[i].getFrameIdx(), RC, TRI); | 
|  | 498 | assert(I != MBB->begin() && | 
|  | 499 | "loadRegFromStackSlot didn't insert any code!"); | 
|  | 500 | // Insert in reverse order.  loadRegFromStackSlot can insert | 
|  | 501 | // multiple instructions. | 
|  | 502 | if (AtStart) | 
|  | 503 | I = MBB->begin(); | 
|  | 504 | else { | 
|  | 505 | I = BeforeI; | 
|  | 506 | ++I; | 
|  | 507 | } | 
|  | 508 | } | 
|  | 509 | } | 
|  | 510 | } | 
|  | 511 | } | 
|  | 512 |  | 
|  | 513 | /// AdjustStackOffset - Helper function used to adjust the stack frame offset. | 
|  | 514 | static inline void | 
|  | 515 | AdjustStackOffset(MachineFrameInfo *MFI, int FrameIdx, | 
|  | 516 | bool StackGrowsDown, int64_t &Offset, | 
|  | 517 | unsigned &MaxAlign, unsigned Skew) { | 
|  | 518 | // If the stack grows down, add the object size to find the lowest address. | 
|  | 519 | if (StackGrowsDown) | 
|  | 520 | Offset += MFI->getObjectSize(FrameIdx); | 
|  | 521 |  | 
|  | 522 | unsigned Align = MFI->getObjectAlignment(FrameIdx); | 
|  | 523 |  | 
|  | 524 | // If the alignment of this object is greater than that of the stack, then | 
|  | 525 | // increase the stack alignment to match. | 
|  | 526 | MaxAlign = std::max(MaxAlign, Align); | 
|  | 527 |  | 
|  | 528 | // Adjust to alignment boundary. | 
| Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 529 | Offset = alignTo(Offset, Align, Skew); | 
| Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 530 |  | 
|  | 531 | if (StackGrowsDown) { | 
|  | 532 | DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset << "]\n"); | 
|  | 533 | MFI->setObjectOffset(FrameIdx, -Offset); // Set the computed offset | 
|  | 534 | } else { | 
|  | 535 | DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset << "]\n"); | 
|  | 536 | MFI->setObjectOffset(FrameIdx, Offset); | 
|  | 537 | Offset += MFI->getObjectSize(FrameIdx); | 
|  | 538 | } | 
|  | 539 | } | 
|  | 540 |  | 
|  | 541 | /// AssignProtectedObjSet - Helper function to assign large stack objects (i.e., | 
|  | 542 | /// those required to be close to the Stack Protector) to stack offsets. | 
|  | 543 | static void | 
|  | 544 | AssignProtectedObjSet(const StackObjSet &UnassignedObjs, | 
|  | 545 | SmallSet<int, 16> &ProtectedObjs, | 
|  | 546 | MachineFrameInfo *MFI, bool StackGrowsDown, | 
|  | 547 | int64_t &Offset, unsigned &MaxAlign, unsigned Skew) { | 
|  | 548 |  | 
|  | 549 | for (StackObjSet::const_iterator I = UnassignedObjs.begin(), | 
|  | 550 | E = UnassignedObjs.end(); I != E; ++I) { | 
|  | 551 | int i = *I; | 
|  | 552 | AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign, Skew); | 
|  | 553 | ProtectedObjs.insert(i); | 
|  | 554 | } | 
|  | 555 | } | 
|  | 556 |  | 
|  | 557 | /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the | 
|  | 558 | /// abstract stack objects. | 
|  | 559 | /// | 
|  | 560 | void WasmPEI::calculateFrameObjectOffsets(MachineFunction &Fn) { | 
|  | 561 | const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering(); | 
|  | 562 | StackProtector *SP = &getAnalysis<StackProtector>(); | 
|  | 563 |  | 
|  | 564 | bool StackGrowsDown = | 
|  | 565 | TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown; | 
|  | 566 |  | 
|  | 567 | // Loop over all of the stack objects, assigning sequential addresses... | 
|  | 568 | MachineFrameInfo *MFI = Fn.getFrameInfo(); | 
|  | 569 |  | 
|  | 570 | // Start at the beginning of the local area. | 
|  | 571 | // The Offset is the distance from the stack top in the direction | 
|  | 572 | // of stack growth -- so it's always nonnegative. | 
|  | 573 | int LocalAreaOffset = TFI.getOffsetOfLocalArea(); | 
|  | 574 | if (StackGrowsDown) | 
|  | 575 | LocalAreaOffset = -LocalAreaOffset; | 
|  | 576 | assert(LocalAreaOffset >= 0 | 
|  | 577 | && "Local area offset should be in direction of stack growth"); | 
|  | 578 | int64_t Offset = LocalAreaOffset; | 
|  | 579 |  | 
|  | 580 | // Skew to be applied to alignment. | 
|  | 581 | unsigned Skew = TFI.getStackAlignmentSkew(Fn); | 
|  | 582 |  | 
|  | 583 | // If there are fixed sized objects that are preallocated in the local area, | 
|  | 584 | // non-fixed objects can't be allocated right at the start of local area. | 
|  | 585 | // We currently don't support filling in holes in between fixed sized | 
|  | 586 | // objects, so we adjust 'Offset' to point to the end of last fixed sized | 
|  | 587 | // preallocated object. | 
|  | 588 | for (int i = MFI->getObjectIndexBegin(); i != 0; ++i) { | 
|  | 589 | int64_t FixedOff; | 
|  | 590 | if (StackGrowsDown) { | 
|  | 591 | // The maximum distance from the stack pointer is at lower address of | 
|  | 592 | // the object -- which is given by offset. For down growing stack | 
|  | 593 | // the offset is negative, so we negate the offset to get the distance. | 
|  | 594 | FixedOff = -MFI->getObjectOffset(i); | 
|  | 595 | } else { | 
|  | 596 | // The maximum distance from the start pointer is at the upper | 
|  | 597 | // address of the object. | 
|  | 598 | FixedOff = MFI->getObjectOffset(i) + MFI->getObjectSize(i); | 
|  | 599 | } | 
|  | 600 | if (FixedOff > Offset) Offset = FixedOff; | 
|  | 601 | } | 
|  | 602 |  | 
|  | 603 | // First assign frame offsets to stack objects that are used to spill | 
|  | 604 | // callee saved registers. | 
|  | 605 | if (StackGrowsDown) { | 
|  | 606 | for (unsigned i = MinCSFrameIndex; i <= MaxCSFrameIndex; ++i) { | 
|  | 607 | // If the stack grows down, we need to add the size to find the lowest | 
|  | 608 | // address of the object. | 
|  | 609 | Offset += MFI->getObjectSize(i); | 
|  | 610 |  | 
|  | 611 | unsigned Align = MFI->getObjectAlignment(i); | 
|  | 612 | // Adjust to alignment boundary | 
| Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 613 | Offset = alignTo(Offset, Align, Skew); | 
| Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 614 |  | 
|  | 615 | MFI->setObjectOffset(i, -Offset);        // Set the computed offset | 
|  | 616 | } | 
|  | 617 | } else { | 
|  | 618 | int MaxCSFI = MaxCSFrameIndex, MinCSFI = MinCSFrameIndex; | 
|  | 619 | for (int i = MaxCSFI; i >= MinCSFI ; --i) { | 
|  | 620 | unsigned Align = MFI->getObjectAlignment(i); | 
|  | 621 | // Adjust to alignment boundary | 
| Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 622 | Offset = alignTo(Offset, Align, Skew); | 
| Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 623 |  | 
|  | 624 | MFI->setObjectOffset(i, Offset); | 
|  | 625 | Offset += MFI->getObjectSize(i); | 
|  | 626 | } | 
|  | 627 | } | 
|  | 628 |  | 
|  | 629 | unsigned MaxAlign = MFI->getMaxAlignment(); | 
|  | 630 |  | 
|  | 631 | // Make sure the special register scavenging spill slot is closest to the | 
|  | 632 | // incoming stack pointer if a frame pointer is required and is closer | 
|  | 633 | // to the incoming rather than the final stack pointer. | 
|  | 634 | const TargetRegisterInfo *RegInfo = Fn.getSubtarget().getRegisterInfo(); | 
|  | 635 | bool EarlyScavengingSlots = (TFI.hasFP(Fn) && | 
|  | 636 | TFI.isFPCloseToIncomingSP() && | 
|  | 637 | RegInfo->useFPForScavengingIndex(Fn) && | 
|  | 638 | !RegInfo->needsStackRealignment(Fn)); | 
|  | 639 | if (RS && EarlyScavengingSlots) { | 
|  | 640 | SmallVector<int, 2> SFIs; | 
|  | 641 | RS->getScavengingFrameIndices(SFIs); | 
|  | 642 | for (SmallVectorImpl<int>::iterator I = SFIs.begin(), | 
|  | 643 | IE = SFIs.end(); I != IE; ++I) | 
|  | 644 | AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign, Skew); | 
|  | 645 | } | 
|  | 646 |  | 
|  | 647 | // FIXME: Once this is working, then enable flag will change to a target | 
|  | 648 | // check for whether the frame is large enough to want to use virtual | 
|  | 649 | // frame index registers. Functions which don't want/need this optimization | 
|  | 650 | // will continue to use the existing code path. | 
|  | 651 | if (MFI->getUseLocalStackAllocationBlock()) { | 
|  | 652 | unsigned Align = MFI->getLocalFrameMaxAlign(); | 
|  | 653 |  | 
|  | 654 | // Adjust to alignment boundary. | 
| Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 655 | Offset = alignTo(Offset, Align, Skew); | 
| Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 656 |  | 
|  | 657 | DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n"); | 
|  | 658 |  | 
|  | 659 | // Resolve offsets for objects in the local block. | 
|  | 660 | for (unsigned i = 0, e = MFI->getLocalFrameObjectCount(); i != e; ++i) { | 
|  | 661 | std::pair<int, int64_t> Entry = MFI->getLocalFrameObjectMap(i); | 
|  | 662 | int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second; | 
|  | 663 | DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" << | 
|  | 664 | FIOffset << "]\n"); | 
|  | 665 | MFI->setObjectOffset(Entry.first, FIOffset); | 
|  | 666 | } | 
|  | 667 | // Allocate the local block | 
|  | 668 | Offset += MFI->getLocalFrameSize(); | 
|  | 669 |  | 
|  | 670 | MaxAlign = std::max(Align, MaxAlign); | 
|  | 671 | } | 
|  | 672 |  | 
|  | 673 | // Make sure that the stack protector comes before the local variables on the | 
|  | 674 | // stack. | 
|  | 675 | SmallSet<int, 16> ProtectedObjs; | 
|  | 676 | if (MFI->getStackProtectorIndex() >= 0) { | 
|  | 677 | StackObjSet LargeArrayObjs; | 
|  | 678 | StackObjSet SmallArrayObjs; | 
|  | 679 | StackObjSet AddrOfObjs; | 
|  | 680 |  | 
|  | 681 | AdjustStackOffset(MFI, MFI->getStackProtectorIndex(), StackGrowsDown, | 
|  | 682 | Offset, MaxAlign, Skew); | 
|  | 683 |  | 
|  | 684 | // Assign large stack objects first. | 
|  | 685 | for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) { | 
|  | 686 | if (MFI->isObjectPreAllocated(i) && | 
|  | 687 | MFI->getUseLocalStackAllocationBlock()) | 
|  | 688 | continue; | 
|  | 689 | if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex) | 
|  | 690 | continue; | 
|  | 691 | if (RS && RS->isScavengingFrameIndex((int)i)) | 
|  | 692 | continue; | 
|  | 693 | if (MFI->isDeadObjectIndex(i)) | 
|  | 694 | continue; | 
|  | 695 | if (MFI->getStackProtectorIndex() == (int)i) | 
|  | 696 | continue; | 
|  | 697 |  | 
|  | 698 | switch (SP->getSSPLayout(MFI->getObjectAllocation(i))) { | 
|  | 699 | case StackProtector::SSPLK_None: | 
|  | 700 | continue; | 
|  | 701 | case StackProtector::SSPLK_SmallArray: | 
|  | 702 | SmallArrayObjs.insert(i); | 
|  | 703 | continue; | 
|  | 704 | case StackProtector::SSPLK_AddrOf: | 
|  | 705 | AddrOfObjs.insert(i); | 
|  | 706 | continue; | 
|  | 707 | case StackProtector::SSPLK_LargeArray: | 
|  | 708 | LargeArrayObjs.insert(i); | 
|  | 709 | continue; | 
|  | 710 | } | 
|  | 711 | llvm_unreachable("Unexpected SSPLayoutKind."); | 
|  | 712 | } | 
|  | 713 |  | 
|  | 714 | AssignProtectedObjSet(LargeArrayObjs, ProtectedObjs, MFI, StackGrowsDown, | 
|  | 715 | Offset, MaxAlign, Skew); | 
|  | 716 | AssignProtectedObjSet(SmallArrayObjs, ProtectedObjs, MFI, StackGrowsDown, | 
|  | 717 | Offset, MaxAlign, Skew); | 
|  | 718 | AssignProtectedObjSet(AddrOfObjs, ProtectedObjs, MFI, StackGrowsDown, | 
|  | 719 | Offset, MaxAlign, Skew); | 
|  | 720 | } | 
|  | 721 |  | 
|  | 722 | // Then assign frame offsets to stack objects that are not used to spill | 
|  | 723 | // callee saved registers. | 
|  | 724 | for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) { | 
|  | 725 | if (MFI->isObjectPreAllocated(i) && | 
|  | 726 | MFI->getUseLocalStackAllocationBlock()) | 
|  | 727 | continue; | 
|  | 728 | if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex) | 
|  | 729 | continue; | 
|  | 730 | if (RS && RS->isScavengingFrameIndex((int)i)) | 
|  | 731 | continue; | 
|  | 732 | if (MFI->isDeadObjectIndex(i)) | 
|  | 733 | continue; | 
|  | 734 | if (MFI->getStackProtectorIndex() == (int)i) | 
|  | 735 | continue; | 
|  | 736 | if (ProtectedObjs.count(i)) | 
|  | 737 | continue; | 
|  | 738 |  | 
|  | 739 | AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign, Skew); | 
|  | 740 | } | 
|  | 741 |  | 
|  | 742 | // Make sure the special register scavenging spill slot is closest to the | 
|  | 743 | // stack pointer. | 
|  | 744 | if (RS && !EarlyScavengingSlots) { | 
|  | 745 | SmallVector<int, 2> SFIs; | 
|  | 746 | RS->getScavengingFrameIndices(SFIs); | 
|  | 747 | for (SmallVectorImpl<int>::iterator I = SFIs.begin(), | 
|  | 748 | IE = SFIs.end(); I != IE; ++I) | 
|  | 749 | AdjustStackOffset(MFI, *I, StackGrowsDown, Offset, MaxAlign, Skew); | 
|  | 750 | } | 
|  | 751 |  | 
|  | 752 | if (!TFI.targetHandlesStackFrameRounding()) { | 
|  | 753 | // If we have reserved argument space for call sites in the function | 
|  | 754 | // immediately on entry to the current function, count it as part of the | 
|  | 755 | // overall stack size. | 
|  | 756 | if (MFI->adjustsStack() && TFI.hasReservedCallFrame(Fn)) | 
|  | 757 | Offset += MFI->getMaxCallFrameSize(); | 
|  | 758 |  | 
|  | 759 | // Round up the size to a multiple of the alignment.  If the function has | 
|  | 760 | // any calls or alloca's, align to the target's StackAlignment value to | 
|  | 761 | // ensure that the callee's frame or the alloca data is suitably aligned; | 
|  | 762 | // otherwise, for leaf functions, align to the TransientStackAlignment | 
|  | 763 | // value. | 
|  | 764 | unsigned StackAlign; | 
|  | 765 | if (MFI->adjustsStack() || MFI->hasVarSizedObjects() || | 
|  | 766 | (RegInfo->needsStackRealignment(Fn) && MFI->getObjectIndexEnd() != 0)) | 
|  | 767 | StackAlign = TFI.getStackAlignment(); | 
|  | 768 | else | 
|  | 769 | StackAlign = TFI.getTransientStackAlignment(); | 
|  | 770 |  | 
|  | 771 | // If the frame pointer is eliminated, all frame offsets will be relative to | 
|  | 772 | // SP not FP. Align to MaxAlign so this works. | 
|  | 773 | StackAlign = std::max(StackAlign, MaxAlign); | 
| Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 774 | Offset = alignTo(Offset, StackAlign, Skew); | 
| Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 775 | } | 
|  | 776 |  | 
|  | 777 | // Update frame info to pretend that this is part of the stack... | 
|  | 778 | int64_t StackSize = Offset - LocalAreaOffset; | 
|  | 779 | MFI->setStackSize(StackSize); | 
|  | 780 | NumBytesStackSpace += StackSize; | 
|  | 781 | } | 
|  | 782 |  | 
|  | 783 | /// insertPrologEpilogCode - Scan the function for modified callee saved | 
|  | 784 | /// registers, insert spill code for these callee saved registers, then add | 
|  | 785 | /// prolog and epilog code to the function. | 
|  | 786 | /// | 
|  | 787 | void WasmPEI::insertPrologEpilogCode(MachineFunction &Fn) { | 
|  | 788 | const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering(); | 
|  | 789 |  | 
|  | 790 | // Add prologue to the function... | 
|  | 791 | for (MachineBasicBlock *SaveBlock : SaveBlocks) | 
|  | 792 | TFI.emitPrologue(Fn, *SaveBlock); | 
|  | 793 |  | 
|  | 794 | // Add epilogue to restore the callee-save registers in each exiting block. | 
|  | 795 | for (MachineBasicBlock *RestoreBlock : RestoreBlocks) | 
|  | 796 | TFI.emitEpilogue(Fn, *RestoreBlock); | 
|  | 797 |  | 
|  | 798 | for (MachineBasicBlock *SaveBlock : SaveBlocks) | 
|  | 799 | TFI.inlineStackProbe(Fn, *SaveBlock); | 
|  | 800 |  | 
|  | 801 | // Emit additional code that is required to support segmented stacks, if | 
|  | 802 | // we've been asked for it.  This, when linked with a runtime with support | 
|  | 803 | // for segmented stacks (libgcc is one), will result in allocating stack | 
|  | 804 | // space in small chunks instead of one large contiguous block. | 
|  | 805 | if (Fn.shouldSplitStack()) { | 
|  | 806 | for (MachineBasicBlock *SaveBlock : SaveBlocks) | 
|  | 807 | TFI.adjustForSegmentedStacks(Fn, *SaveBlock); | 
|  | 808 | } | 
|  | 809 |  | 
|  | 810 | // Emit additional code that is required to explicitly handle the stack in | 
|  | 811 | // HiPE native code (if needed) when loaded in the Erlang/OTP runtime. The | 
|  | 812 | // approach is rather similar to that of Segmented Stacks, but it uses a | 
|  | 813 | // different conditional check and another BIF for allocating more stack | 
|  | 814 | // space. | 
|  | 815 | if (Fn.getFunction()->getCallingConv() == CallingConv::HiPE) | 
|  | 816 | for (MachineBasicBlock *SaveBlock : SaveBlocks) | 
|  | 817 | TFI.adjustForHiPEPrologue(Fn, *SaveBlock); | 
|  | 818 | } | 
|  | 819 |  | 
|  | 820 | /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical | 
|  | 821 | /// register references and actual offsets. | 
|  | 822 | /// | 
|  | 823 | void WasmPEI::replaceFrameIndices(MachineFunction &Fn) { | 
|  | 824 | const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering(); | 
|  | 825 | if (!TFI.needsFrameIndexResolution(Fn)) return; | 
|  | 826 |  | 
|  | 827 | // Store SPAdj at exit of a basic block. | 
|  | 828 | SmallVector<int, 8> SPState; | 
|  | 829 | SPState.resize(Fn.getNumBlockIDs()); | 
|  | 830 | SmallPtrSet<MachineBasicBlock*, 8> Reachable; | 
|  | 831 |  | 
|  | 832 | // Iterate over the reachable blocks in DFS order. | 
|  | 833 | for (auto DFI = df_ext_begin(&Fn, Reachable), DFE = df_ext_end(&Fn, Reachable); | 
|  | 834 | DFI != DFE; ++DFI) { | 
|  | 835 | int SPAdj = 0; | 
|  | 836 | // Check the exit state of the DFS stack predecessor. | 
|  | 837 | if (DFI.getPathLength() >= 2) { | 
|  | 838 | MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2); | 
|  | 839 | assert(Reachable.count(StackPred) && | 
|  | 840 | "DFS stack predecessor is already visited.\n"); | 
|  | 841 | SPAdj = SPState[StackPred->getNumber()]; | 
|  | 842 | } | 
|  | 843 | MachineBasicBlock *BB = *DFI; | 
|  | 844 | replaceFrameIndices(BB, Fn, SPAdj); | 
|  | 845 | SPState[BB->getNumber()] = SPAdj; | 
|  | 846 | } | 
|  | 847 |  | 
|  | 848 | // Handle the unreachable blocks. | 
|  | 849 | for (auto &BB : Fn) { | 
|  | 850 | if (Reachable.count(&BB)) | 
|  | 851 | // Already handled in DFS traversal. | 
|  | 852 | continue; | 
|  | 853 | int SPAdj = 0; | 
|  | 854 | replaceFrameIndices(&BB, Fn, SPAdj); | 
|  | 855 | } | 
|  | 856 | } | 
|  | 857 |  | 
|  | 858 | void WasmPEI::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &Fn, | 
|  | 859 | int &SPAdj) { | 
|  | 860 | assert(Fn.getSubtarget().getRegisterInfo() && | 
|  | 861 | "getRegisterInfo() must be implemented!"); | 
|  | 862 | const TargetInstrInfo &TII = *Fn.getSubtarget().getInstrInfo(); | 
|  | 863 | const TargetRegisterInfo &TRI = *Fn.getSubtarget().getRegisterInfo(); | 
|  | 864 | const TargetFrameLowering *TFI = Fn.getSubtarget().getFrameLowering(); | 
|  | 865 | unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode(); | 
|  | 866 | unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode(); | 
|  | 867 |  | 
| Matthias Braun | 7dc03f0 | 2016-04-06 02:47:09 +0000 | [diff] [blame] | 868 | if (RS && !FrameIndexVirtualScavenging) RS->enterBasicBlock(*BB); | 
| Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 869 |  | 
|  | 870 | bool InsideCallSequence = false; | 
|  | 871 |  | 
|  | 872 | for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) { | 
|  | 873 |  | 
|  | 874 | if (I->getOpcode() == FrameSetupOpcode || | 
|  | 875 | I->getOpcode() == FrameDestroyOpcode) { | 
|  | 876 | InsideCallSequence = (I->getOpcode() == FrameSetupOpcode); | 
|  | 877 | SPAdj += TII.getSPAdjust(I); | 
| Hans Wennborg | e1a2e90 | 2016-03-31 18:33:38 +0000 | [diff] [blame] | 878 | I = TFI->eliminateCallFramePseudoInstr(Fn, *BB, I); | 
| Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 879 | continue; | 
|  | 880 | } | 
|  | 881 |  | 
|  | 882 | MachineInstr *MI = I; | 
|  | 883 | bool DoIncr = true; | 
|  | 884 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { | 
|  | 885 | if (!MI->getOperand(i).isFI()) | 
|  | 886 | continue; | 
|  | 887 |  | 
|  | 888 | // Frame indices in debug values are encoded in a target independent | 
|  | 889 | // way with simply the frame index and offset rather than any | 
|  | 890 | // target-specific addressing mode. | 
|  | 891 | if (MI->isDebugValue()) { | 
|  | 892 | assert(i == 0 && "Frame indices can only appear as the first " | 
|  | 893 | "operand of a DBG_VALUE machine instruction"); | 
|  | 894 | unsigned Reg; | 
|  | 895 | MachineOperand &Offset = MI->getOperand(1); | 
|  | 896 | Offset.setImm(Offset.getImm() + | 
|  | 897 | TFI->getFrameIndexReference( | 
|  | 898 | Fn, MI->getOperand(0).getIndex(), Reg)); | 
|  | 899 | MI->getOperand(0).ChangeToRegister(Reg, false /*isDef*/); | 
|  | 900 | continue; | 
|  | 901 | } | 
|  | 902 |  | 
|  | 903 | // TODO: This code should be commoned with the code for | 
|  | 904 | // PATCHPOINT. There's no good reason for the difference in | 
|  | 905 | // implementation other than historical accident.  The only | 
|  | 906 | // remaining difference is the unconditional use of the stack | 
|  | 907 | // pointer as the base register. | 
|  | 908 | if (MI->getOpcode() == TargetOpcode::STATEPOINT) { | 
|  | 909 | assert((!MI->isDebugValue() || i == 0) && | 
|  | 910 | "Frame indicies can only appear as the first operand of a " | 
|  | 911 | "DBG_VALUE machine instruction"); | 
|  | 912 | unsigned Reg; | 
|  | 913 | MachineOperand &Offset = MI->getOperand(i + 1); | 
|  | 914 | const unsigned refOffset = | 
|  | 915 | TFI->getFrameIndexReferenceFromSP(Fn, MI->getOperand(i).getIndex(), | 
|  | 916 | Reg); | 
|  | 917 |  | 
|  | 918 | Offset.setImm(Offset.getImm() + refOffset); | 
|  | 919 | MI->getOperand(i).ChangeToRegister(Reg, false /*isDef*/); | 
|  | 920 | continue; | 
|  | 921 | } | 
|  | 922 |  | 
|  | 923 | // Some instructions (e.g. inline asm instructions) can have | 
|  | 924 | // multiple frame indices and/or cause eliminateFrameIndex | 
|  | 925 | // to insert more than one instruction. We need the register | 
|  | 926 | // scavenger to go through all of these instructions so that | 
|  | 927 | // it can update its register information. We keep the | 
|  | 928 | // iterator at the point before insertion so that we can | 
|  | 929 | // revisit them in full. | 
|  | 930 | bool AtBeginning = (I == BB->begin()); | 
|  | 931 | if (!AtBeginning) --I; | 
|  | 932 |  | 
|  | 933 | // If this instruction has a FrameIndex operand, we need to | 
|  | 934 | // use that target machine register info object to eliminate | 
|  | 935 | // it. | 
|  | 936 | TRI.eliminateFrameIndex(MI, SPAdj, i, | 
|  | 937 | FrameIndexVirtualScavenging ?  nullptr : RS); | 
|  | 938 |  | 
|  | 939 | // Reset the iterator if we were at the beginning of the BB. | 
|  | 940 | if (AtBeginning) { | 
|  | 941 | I = BB->begin(); | 
|  | 942 | DoIncr = false; | 
|  | 943 | } | 
|  | 944 |  | 
|  | 945 | MI = nullptr; | 
|  | 946 | break; | 
|  | 947 | } | 
|  | 948 |  | 
|  | 949 | // If we are looking at a call sequence, we need to keep track of | 
|  | 950 | // the SP adjustment made by each instruction in the sequence. | 
|  | 951 | // This includes both the frame setup/destroy pseudos (handled above), | 
|  | 952 | // as well as other instructions that have side effects w.r.t the SP. | 
|  | 953 | // Note that this must come after eliminateFrameIndex, because | 
|  | 954 | // if I itself referred to a frame index, we shouldn't count its own | 
|  | 955 | // adjustment. | 
|  | 956 | if (MI && InsideCallSequence) | 
|  | 957 | SPAdj += TII.getSPAdjust(MI); | 
|  | 958 |  | 
|  | 959 | if (DoIncr && I != BB->end()) ++I; | 
|  | 960 |  | 
|  | 961 | // Update register states. | 
|  | 962 | if (RS && !FrameIndexVirtualScavenging && MI) RS->forward(MI); | 
|  | 963 | } | 
|  | 964 | } | 
|  | 965 |  | 
|  | 966 | /// scavengeFrameVirtualRegs - Replace all frame index virtual registers | 
|  | 967 | /// with physical registers. Use the register scavenger to find an | 
|  | 968 | /// appropriate register to use. | 
|  | 969 | /// | 
|  | 970 | /// FIXME: Iterating over the instruction stream is unnecessary. We can simply | 
|  | 971 | /// iterate over the vreg use list, which at this point only contains machine | 
|  | 972 | /// operands for which eliminateFrameIndex need a new scratch reg. | 
|  | 973 | void | 
|  | 974 | WasmPEI::scavengeFrameVirtualRegs(MachineFunction &Fn) { | 
|  | 975 | // Run through the instructions and find any virtual registers. | 
|  | 976 | for (MachineFunction::iterator BB = Fn.begin(), | 
|  | 977 | E = Fn.end(); BB != E; ++BB) { | 
| Matthias Braun | 7dc03f0 | 2016-04-06 02:47:09 +0000 | [diff] [blame] | 978 | RS->enterBasicBlock(*BB); | 
| Derek Schuff | 9769deb | 2015-12-11 23:49:46 +0000 | [diff] [blame] | 979 |  | 
|  | 980 | int SPAdj = 0; | 
|  | 981 |  | 
|  | 982 | // The instruction stream may change in the loop, so check BB->end() | 
|  | 983 | // directly. | 
|  | 984 | for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) { | 
|  | 985 | // We might end up here again with a NULL iterator if we scavenged a | 
|  | 986 | // register for which we inserted spill code for definition by what was | 
|  | 987 | // originally the first instruction in BB. | 
|  | 988 | if (I == MachineBasicBlock::iterator(nullptr)) | 
|  | 989 | I = BB->begin(); | 
|  | 990 |  | 
|  | 991 | MachineInstr *MI = I; | 
|  | 992 | MachineBasicBlock::iterator J = std::next(I); | 
|  | 993 | MachineBasicBlock::iterator P = | 
|  | 994 | I == BB->begin() ? MachineBasicBlock::iterator(nullptr) | 
|  | 995 | : std::prev(I); | 
|  | 996 |  | 
|  | 997 | // RS should process this instruction before we might scavenge at this | 
|  | 998 | // location. This is because we might be replacing a virtual register | 
|  | 999 | // defined by this instruction, and if so, registers killed by this | 
|  | 1000 | // instruction are available, and defined registers are not. | 
|  | 1001 | RS->forward(I); | 
|  | 1002 |  | 
|  | 1003 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { | 
|  | 1004 | if (MI->getOperand(i).isReg()) { | 
|  | 1005 | MachineOperand &MO = MI->getOperand(i); | 
|  | 1006 | unsigned Reg = MO.getReg(); | 
|  | 1007 | if (Reg == 0) | 
|  | 1008 | continue; | 
|  | 1009 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) | 
|  | 1010 | continue; | 
|  | 1011 |  | 
|  | 1012 | // When we first encounter a new virtual register, it | 
|  | 1013 | // must be a definition. | 
|  | 1014 | assert(MI->getOperand(i).isDef() && | 
|  | 1015 | "frame index virtual missing def!"); | 
|  | 1016 | // Scavenge a new scratch register | 
|  | 1017 | const TargetRegisterClass *RC = Fn.getRegInfo().getRegClass(Reg); | 
|  | 1018 | unsigned ScratchReg = RS->scavengeRegister(RC, J, SPAdj); | 
|  | 1019 |  | 
|  | 1020 | ++NumScavengedRegs; | 
|  | 1021 |  | 
|  | 1022 | // Replace this reference to the virtual register with the | 
|  | 1023 | // scratch register. | 
|  | 1024 | assert (ScratchReg && "Missing scratch register!"); | 
|  | 1025 | Fn.getRegInfo().replaceRegWith(Reg, ScratchReg); | 
|  | 1026 |  | 
|  | 1027 | // Because this instruction was processed by the RS before this | 
|  | 1028 | // register was allocated, make sure that the RS now records the | 
|  | 1029 | // register as being used. | 
|  | 1030 | RS->setRegUsed(ScratchReg); | 
|  | 1031 | } | 
|  | 1032 | } | 
|  | 1033 |  | 
|  | 1034 | // If the scavenger needed to use one of its spill slots, the | 
|  | 1035 | // spill code will have been inserted in between I and J. This is a | 
|  | 1036 | // problem because we need the spill code before I: Move I to just | 
|  | 1037 | // prior to J. | 
|  | 1038 | if (I != std::prev(J)) { | 
|  | 1039 | BB->splice(J, &*BB, I); | 
|  | 1040 |  | 
|  | 1041 | // Before we move I, we need to prepare the RS to visit I again. | 
|  | 1042 | // Specifically, RS will assert if it sees uses of registers that | 
|  | 1043 | // it believes are undefined. Because we have already processed | 
|  | 1044 | // register kills in I, when it visits I again, it will believe that | 
|  | 1045 | // those registers are undefined. To avoid this situation, unprocess | 
|  | 1046 | // the instruction I. | 
|  | 1047 | assert(RS->getCurrentPosition() == I && | 
|  | 1048 | "The register scavenger has an unexpected position"); | 
|  | 1049 | I = P; | 
|  | 1050 | RS->unprocess(P); | 
|  | 1051 | } else | 
|  | 1052 | ++I; | 
|  | 1053 | } | 
|  | 1054 | } | 
|  | 1055 | } |