1 //===-- VEFrameLowering.cpp - VE Frame Information ------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains the VE implementation of TargetFrameLowering class. 10 // 11 // On VE, stack frames are structured as follows: 12 // 13 // The stack grows downward. 14 // 15 // All of the individual frame areas on the frame below are optional, i.e. it's 16 // possible to create a function so that the particular area isn't present 17 // in the frame. 18 // 19 // At function entry, the "frame" looks as follows: 20 // 21 // | | Higher address 22 // |----------------------------------------------| 23 // | Parameter area for this function | 24 // |----------------------------------------------| 25 // | Register save area (RSA) for this function | 26 // |----------------------------------------------| 27 // | Return address for this function | 28 // |----------------------------------------------| 29 // | Frame pointer for this function | 30 // |----------------------------------------------| <- sp 31 // | | Lower address 32 // 33 // VE doesn't use on demand stack allocation, so user code generated by LLVM 34 // needs to call VEOS to allocate stack frame. VE's ABI want to reduce the 35 // number of VEOS calls, so ABI requires to allocate not only RSA (in general 36 // CSR, callee saved register) area but also call frame at the prologue of 37 // caller function. 38 // 39 // After the prologue has run, the frame has the following general structure. 40 // Note that technically the last frame area (VLAs) doesn't get created until 41 // in the main function body, after the prologue is run. However, it's depicted 42 // here for completeness. 43 // 44 // | | Higher address 45 // |----------------------------------------------| 46 // | Parameter area for this function | 47 // |----------------------------------------------| 48 // | Register save area (RSA) for this function | 49 // |----------------------------------------------| 50 // | Return address for this function | 51 // |----------------------------------------------| 52 // | Frame pointer for this function | 53 // |----------------------------------------------| <- fp(=old sp) 54 // |.empty.space.to.make.part.below.aligned.in....| 55 // |.case.it.needs.more.than.the.standard.16-byte.| (size of this area is 56 // |.alignment....................................| unknown at compile time) 57 // |----------------------------------------------| 58 // | Local variables of fixed size including spill| 59 // | slots | 60 // |----------------------------------------------| <- bp(not defined by ABI, 61 // |.variable-sized.local.variables.(VLAs)........| LLVM chooses SX17) 62 // |..............................................| (size of this area is 63 // |..............................................| unknown at compile time) 64 // |----------------------------------------------| <- stack top (returned by 65 // | Parameter area for callee | alloca) 66 // |----------------------------------------------| 67 // | Register save area (RSA) for callee | 68 // |----------------------------------------------| 69 // | Return address for callee | 70 // |----------------------------------------------| 71 // | Frame pointer for callee | 72 // |----------------------------------------------| <- sp 73 // | | Lower address 74 // 75 // To access the data in a frame, at-compile time, a constant offset must be 76 // computable from one of the pointers (fp, bp, sp) to access it. The size 77 // of the areas with a dotted background cannot be computed at compile-time 78 // if they are present, making it required to have all three of fp, bp and 79 // sp to be set up to be able to access all contents in the frame areas, 80 // assuming all of the frame areas are non-empty. 81 // 82 // For most functions, some of the frame areas are empty. For those functions, 83 // it may not be necessary to set up fp or bp: 84 // * A base pointer is definitely needed when there are both VLAs and local 85 // variables with more-than-default alignment requirements. 86 // * A frame pointer is definitely needed when there are local variables with 87 // more-than-default alignment requirements. 88 // 89 // In addition, VE ABI defines RSA frame, return address, and frame pointer 90 // as follows: 91 // 92 // |----------------------------------------------| <- sp+176 93 // | %s18...%s33 | 94 // |----------------------------------------------| <- sp+48 95 // | Linkage area register (%s17) | 96 // |----------------------------------------------| <- sp+40 97 // | Procedure linkage table register (%plt=%s16) | 98 // |----------------------------------------------| <- sp+32 99 // | Global offset table register (%got=%s15) | 100 // |----------------------------------------------| <- sp+24 101 // | Thread pointer register (%tp=%s14) | 102 // |----------------------------------------------| <- sp+16 103 // | Return address | 104 // |----------------------------------------------| <- sp+8 105 // | Frame pointer | 106 // |----------------------------------------------| <- sp+0 107 // 108 // NOTE: This description is based on VE ABI and description in 109 // AArch64FrameLowering.cpp. Thanks a lot. 110 //===----------------------------------------------------------------------===// 111 112 #include "VEFrameLowering.h" 113 #include "VEInstrInfo.h" 114 #include "VEMachineFunctionInfo.h" 115 #include "VESubtarget.h" 116 #include "llvm/CodeGen/MachineFrameInfo.h" 117 #include "llvm/CodeGen/MachineFunction.h" 118 #include "llvm/CodeGen/MachineInstrBuilder.h" 119 #include "llvm/CodeGen/MachineModuleInfo.h" 120 #include "llvm/CodeGen/MachineRegisterInfo.h" 121 #include "llvm/CodeGen/RegisterScavenging.h" 122 #include "llvm/IR/DataLayout.h" 123 #include "llvm/IR/Function.h" 124 #include "llvm/Support/CommandLine.h" 125 #include "llvm/Target/TargetOptions.h" 126 #include "llvm/Support/MathExtras.h" 127 128 using namespace llvm; 129 130 VEFrameLowering::VEFrameLowering(const VESubtarget &ST) 131 : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, Align(16), 0, 132 Align(16)), 133 STI(ST) {} 134 135 void VEFrameLowering::emitPrologueInsns(MachineFunction &MF, 136 MachineBasicBlock &MBB, 137 MachineBasicBlock::iterator MBBI, 138 uint64_t NumBytes, 139 bool RequireFPUpdate) const { 140 DebugLoc DL; 141 const VEInstrInfo &TII = 142 *static_cast<const VEInstrInfo *>(MF.getSubtarget().getInstrInfo()); 143 144 // Insert following codes here as prologue 145 // 146 // st %fp, 0(,%sp) 147 // st %lr, 8(,%sp) 148 // st %got, 24(,%sp) 149 // st %plt, 32(,%sp) 150 // st %s17, 40(,%sp) iff this function is using s17 as BP 151 // or %fp, 0, %sp 152 BuildMI(MBB, MBBI, DL, TII.get(VE::STrii)) 153 .addReg(VE::SX11) 154 .addImm(0) 155 .addImm(0) 156 .addReg(VE::SX9); 157 BuildMI(MBB, MBBI, DL, TII.get(VE::STrii)) 158 .addReg(VE::SX11) 159 .addImm(0) 160 .addImm(8) 161 .addReg(VE::SX10); 162 BuildMI(MBB, MBBI, DL, TII.get(VE::STrii)) 163 .addReg(VE::SX11) 164 .addImm(0) 165 .addImm(24) 166 .addReg(VE::SX15); 167 BuildMI(MBB, MBBI, DL, TII.get(VE::STrii)) 168 .addReg(VE::SX11) 169 .addImm(0) 170 .addImm(32) 171 .addReg(VE::SX16); 172 if (hasBP(MF)) 173 BuildMI(MBB, MBBI, DL, TII.get(VE::STrii)) 174 .addReg(VE::SX11) 175 .addImm(0) 176 .addImm(40) 177 .addReg(VE::SX17); 178 BuildMI(MBB, MBBI, DL, TII.get(VE::ORri), VE::SX9).addReg(VE::SX11).addImm(0); 179 } 180 181 void VEFrameLowering::emitEpilogueInsns(MachineFunction &MF, 182 MachineBasicBlock &MBB, 183 MachineBasicBlock::iterator MBBI, 184 uint64_t NumBytes, 185 bool RequireFPUpdate) const { 186 DebugLoc DL; 187 const VEInstrInfo &TII = 188 *static_cast<const VEInstrInfo *>(MF.getSubtarget().getInstrInfo()); 189 190 // Insert following codes here as epilogue 191 // 192 // or %sp, 0, %fp 193 // ld %s17, 40(,%sp) iff this function is using s17 as BP 194 // ld %plt, 32(,%sp) 195 // ld %got, 24(,%sp) 196 // ld %lr, 8(,%sp) 197 // ld %fp, 0(,%sp) 198 BuildMI(MBB, MBBI, DL, TII.get(VE::ORri), VE::SX11).addReg(VE::SX9).addImm(0); 199 if (hasBP(MF)) 200 BuildMI(MBB, MBBI, DL, TII.get(VE::LDrii), VE::SX17) 201 .addReg(VE::SX11) 202 .addImm(0) 203 .addImm(40); 204 BuildMI(MBB, MBBI, DL, TII.get(VE::LDrii), VE::SX16) 205 .addReg(VE::SX11) 206 .addImm(0) 207 .addImm(32); 208 BuildMI(MBB, MBBI, DL, TII.get(VE::LDrii), VE::SX15) 209 .addReg(VE::SX11) 210 .addImm(0) 211 .addImm(24); 212 BuildMI(MBB, MBBI, DL, TII.get(VE::LDrii), VE::SX10) 213 .addReg(VE::SX11) 214 .addImm(0) 215 .addImm(8); 216 BuildMI(MBB, MBBI, DL, TII.get(VE::LDrii), VE::SX9) 217 .addReg(VE::SX11) 218 .addImm(0) 219 .addImm(0); 220 } 221 222 void VEFrameLowering::emitSPAdjustment(MachineFunction &MF, 223 MachineBasicBlock &MBB, 224 MachineBasicBlock::iterator MBBI, 225 int64_t NumBytes, 226 MaybeAlign MaybeAlign) const { 227 DebugLoc DL; 228 const VEInstrInfo &TII = 229 *static_cast<const VEInstrInfo *>(MF.getSubtarget().getInstrInfo()); 230 231 if (NumBytes >= -64 && NumBytes < 63) { 232 BuildMI(MBB, MBBI, DL, TII.get(VE::ADDSLri), VE::SX11) 233 .addReg(VE::SX11) 234 .addImm(NumBytes); 235 return; 236 } 237 238 // Emit following codes. This clobbers SX13 which we always know is 239 // available here. 240 // lea %s13,%lo(NumBytes) 241 // and %s13,%s13,(32)0 242 // lea.sl %sp,%hi(NumBytes)(%sp, %s13) 243 BuildMI(MBB, MBBI, DL, TII.get(VE::LEAzii), VE::SX13) 244 .addImm(0) 245 .addImm(0) 246 .addImm(Lo_32(NumBytes)); 247 BuildMI(MBB, MBBI, DL, TII.get(VE::ANDrm), VE::SX13) 248 .addReg(VE::SX13) 249 .addImm(M0(32)); 250 BuildMI(MBB, MBBI, DL, TII.get(VE::LEASLrri), VE::SX11) 251 .addReg(VE::SX11) 252 .addReg(VE::SX13) 253 .addImm(Hi_32(NumBytes)); 254 255 if (MaybeAlign) { 256 // and %sp, %sp, Align-1 257 BuildMI(MBB, MBBI, DL, TII.get(VE::ANDrm), VE::SX11) 258 .addReg(VE::SX11) 259 .addImm(M1(64 - Log2_64(MaybeAlign.valueOrOne().value()))); 260 } 261 } 262 263 void VEFrameLowering::emitSPExtend(MachineFunction &MF, MachineBasicBlock &MBB, 264 MachineBasicBlock::iterator MBBI) const { 265 DebugLoc DL; 266 const VEInstrInfo &TII = 267 *static_cast<const VEInstrInfo *>(MF.getSubtarget().getInstrInfo()); 268 269 // Emit following codes. It is not possible to insert multiple 270 // BasicBlocks in PEI pass, so we emit two pseudo instructions here. 271 // 272 // EXTEND_STACK // pseudo instrcution 273 // EXTEND_STACK_GUARD // pseudo instrcution 274 // 275 // EXTEND_STACK pseudo will be converted by ExpandPostRA pass into 276 // following instructions with multiple basic blocks later. 277 // 278 // thisBB: 279 // brge.l.t %sp, %sl, sinkBB 280 // syscallBB: 281 // ld %s61, 0x18(, %tp) // load param area 282 // or %s62, 0, %s0 // spill the value of %s0 283 // lea %s63, 0x13b // syscall # of grow 284 // shm.l %s63, 0x0(%s61) // store syscall # at addr:0 285 // shm.l %sl, 0x8(%s61) // store old limit at addr:8 286 // shm.l %sp, 0x10(%s61) // store new limit at addr:16 287 // monc // call monitor 288 // or %s0, 0, %s62 // restore the value of %s0 289 // sinkBB: 290 // 291 // EXTEND_STACK_GUARD pseudo will be simply eliminated by ExpandPostRA 292 // pass. This pseudo is required to be at the next of EXTEND_STACK 293 // pseudo in order to protect iteration loop in ExpandPostRA. 294 BuildMI(MBB, MBBI, DL, TII.get(VE::EXTEND_STACK)); 295 BuildMI(MBB, MBBI, DL, TII.get(VE::EXTEND_STACK_GUARD)); 296 } 297 298 void VEFrameLowering::emitPrologue(MachineFunction &MF, 299 MachineBasicBlock &MBB) const { 300 const VEMachineFunctionInfo *FuncInfo = MF.getInfo<VEMachineFunctionInfo>(); 301 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported"); 302 MachineFrameInfo &MFI = MF.getFrameInfo(); 303 const VEInstrInfo &TII = *STI.getInstrInfo(); 304 const VERegisterInfo &RegInfo = *STI.getRegisterInfo(); 305 MachineBasicBlock::iterator MBBI = MBB.begin(); 306 bool NeedsStackRealignment = RegInfo.needsStackRealignment(MF); 307 308 // Debug location must be unknown since the first debug location is used 309 // to determine the end of the prologue. 310 DebugLoc DL; 311 312 // FIXME: unfortunately, returning false from canRealignStack 313 // actually just causes needsStackRealignment to return false, 314 // rather than reporting an error, as would be sensible. This is 315 // poor, but fixing that bogosity is going to be a large project. 316 // For now, just see if it's lied, and report an error here. 317 if (!NeedsStackRealignment && MFI.getMaxAlign() > getStackAlign()) 318 report_fatal_error("Function \"" + Twine(MF.getName()) + 319 "\" required " 320 "stack re-alignment, but LLVM couldn't handle it " 321 "(probably because it has a dynamic alloca)."); 322 323 // Get the number of bytes to allocate from the FrameInfo 324 uint64_t NumBytes = MFI.getStackSize(); 325 326 // The VE ABI requires a reserved area at the top of stack as described 327 // in VESubtarget.cpp. So, we adjust it here. 328 NumBytes = STI.getAdjustedFrameSize(NumBytes); 329 330 // Finally, ensure that the size is sufficiently aligned for the 331 // data on the stack. 332 NumBytes = alignTo(NumBytes, MFI.getMaxAlign()); 333 334 // Update stack size with corrected value. 335 MFI.setStackSize(NumBytes); 336 337 if (FuncInfo->isLeafProc()) 338 return; 339 340 // Emit Prologue instructions to save multiple registers. 341 emitPrologueInsns(MF, MBB, MBBI, NumBytes, true); 342 343 // Emit stack adjust instructions 344 MaybeAlign RuntimeAlign = 345 NeedsStackRealignment ? MaybeAlign(MFI.getMaxAlign()) : None; 346 emitSPAdjustment(MF, MBB, MBBI, -(int64_t)NumBytes, RuntimeAlign); 347 348 if (hasBP(MF)) { 349 // Copy SP to BP. 350 BuildMI(MBB, MBBI, DL, TII.get(VE::ORri), VE::SX17) 351 .addReg(VE::SX11) 352 .addImm(0); 353 } 354 355 // Emit stack extend instructions 356 emitSPExtend(MF, MBB, MBBI); 357 358 Register RegFP = RegInfo.getDwarfRegNum(VE::SX9, true); 359 360 // Emit ".cfi_def_cfa_register 30". 361 unsigned CFIIndex = 362 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, RegFP)); 363 BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) 364 .addCFIIndex(CFIIndex); 365 366 // Emit ".cfi_window_save". 367 CFIIndex = MF.addFrameInst(MCCFIInstruction::createWindowSave(nullptr)); 368 BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) 369 .addCFIIndex(CFIIndex); 370 } 371 372 MachineBasicBlock::iterator VEFrameLowering::eliminateCallFramePseudoInstr( 373 MachineFunction &MF, MachineBasicBlock &MBB, 374 MachineBasicBlock::iterator I) const { 375 if (!hasReservedCallFrame(MF)) { 376 MachineInstr &MI = *I; 377 int64_t Size = MI.getOperand(0).getImm(); 378 if (MI.getOpcode() == VE::ADJCALLSTACKDOWN) 379 Size = -Size; 380 381 if (Size) 382 emitSPAdjustment(MF, MBB, I, Size); 383 } 384 return MBB.erase(I); 385 } 386 387 void VEFrameLowering::emitEpilogue(MachineFunction &MF, 388 MachineBasicBlock &MBB) const { 389 const VEMachineFunctionInfo *FuncInfo = MF.getInfo<VEMachineFunctionInfo>(); 390 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 391 MachineFrameInfo &MFI = MF.getFrameInfo(); 392 393 uint64_t NumBytes = MFI.getStackSize(); 394 395 if (FuncInfo->isLeafProc()) 396 return; 397 398 // Emit Epilogue instructions to restore multiple registers. 399 emitEpilogueInsns(MF, MBB, MBBI, NumBytes, true); 400 } 401 402 // hasFP - Return true if the specified function should have a dedicated frame 403 // pointer register. This is true if the function has variable sized allocas 404 // or if frame pointer elimination is disabled. 405 bool VEFrameLowering::hasFP(const MachineFunction &MF) const { 406 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo(); 407 408 const MachineFrameInfo &MFI = MF.getFrameInfo(); 409 return MF.getTarget().Options.DisableFramePointerElim(MF) || 410 RegInfo->needsStackRealignment(MF) || MFI.hasVarSizedObjects() || 411 MFI.isFrameAddressTaken(); 412 } 413 414 bool VEFrameLowering::hasBP(const MachineFunction &MF) const { 415 const MachineFrameInfo &MFI = MF.getFrameInfo(); 416 const TargetRegisterInfo *TRI = STI.getRegisterInfo(); 417 418 return MFI.hasVarSizedObjects() && TRI->needsStackRealignment(MF); 419 } 420 421 StackOffset VEFrameLowering::getFrameIndexReference(const MachineFunction &MF, 422 int FI, 423 Register &FrameReg) const { 424 const MachineFrameInfo &MFI = MF.getFrameInfo(); 425 const VERegisterInfo *RegInfo = STI.getRegisterInfo(); 426 bool isFixed = MFI.isFixedObjectIndex(FI); 427 428 int64_t FrameOffset = MF.getFrameInfo().getObjectOffset(FI); 429 430 if (!hasFP(MF)) { 431 // If FP is not used, frame indexies are based on a %sp regiter. 432 FrameReg = VE::SX11; // %sp 433 return StackOffset::getFixed(FrameOffset + 434 MF.getFrameInfo().getStackSize()); 435 } 436 if (RegInfo->needsStackRealignment(MF) && !isFixed) { 437 // If data on stack require realignemnt, frame indexies are based on a %sp 438 // or %s17 (bp) register. If there is a variable sized object, bp is used. 439 if (hasBP(MF)) 440 FrameReg = VE::SX17; // %bp 441 else 442 FrameReg = VE::SX11; // %sp 443 return StackOffset::getFixed(FrameOffset + 444 MF.getFrameInfo().getStackSize()); 445 } 446 // Use %fp by default. 447 FrameReg = RegInfo->getFrameRegister(MF); 448 return StackOffset::getFixed(FrameOffset); 449 } 450 451 bool VEFrameLowering::isLeafProc(MachineFunction &MF) const { 452 453 MachineRegisterInfo &MRI = MF.getRegInfo(); 454 MachineFrameInfo &MFI = MF.getFrameInfo(); 455 456 return !MFI.hasCalls() // No calls 457 && !MRI.isPhysRegUsed(VE::SX18) // Registers within limits 458 // (s18 is first CSR) 459 && !MRI.isPhysRegUsed(VE::SX11) // %sp un-used 460 && !hasFP(MF); // Don't need %fp 461 } 462 463 void VEFrameLowering::determineCalleeSaves(MachineFunction &MF, 464 BitVector &SavedRegs, 465 RegScavenger *RS) const { 466 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS); 467 const MachineFrameInfo &MFI = MF.getFrameInfo(); 468 469 // Functions having BP or stack objects need to emit prologue and epilogue 470 // to allocate local buffer on the stack. 471 if (isLeafProc(MF) && !hasBP(MF) && !MFI.hasStackObjects()) { 472 VEMachineFunctionInfo *FuncInfo = MF.getInfo<VEMachineFunctionInfo>(); 473 FuncInfo->setLeafProc(true); 474 } 475 } 476