Path: blob/main/contrib/llvm-project/llvm/lib/Target/VE/VEFrameLowering.cpp
35266 views
//===-- VEFrameLowering.cpp - VE Frame Information ------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains the VE implementation of TargetFrameLowering class.9//10// On VE, stack frames are structured as follows:11//12// The stack grows downward.13//14// All of the individual frame areas on the frame below are optional, i.e. it's15// possible to create a function so that the particular area isn't present16// in the frame.17//18// At function entry, the "frame" looks as follows:19//20// | | Higher address21// |----------------------------------------------|22// | Parameter area for this function |23// |----------------------------------------------|24// | Register save area (RSA) for this function |25// |----------------------------------------------|26// | Return address for this function |27// |----------------------------------------------|28// | Frame pointer for this function |29// |----------------------------------------------| <- sp30// | | Lower address31//32// VE doesn't use on demand stack allocation, so user code generated by LLVM33// needs to call VEOS to allocate stack frame. VE's ABI want to reduce the34// number of VEOS calls, so ABI requires to allocate not only RSA (in general35// CSR, callee saved register) area but also call frame at the prologue of36// caller function.37//38// After the prologue has run, the frame has the following general structure.39// Note that technically the last frame area (VLAs) doesn't get created until40// in the main function body, after the prologue is run. However, it's depicted41// here for completeness.42//43// | | Higher address44// |----------------------------------------------|45// | Parameter area for this function |46// |----------------------------------------------|47// | Register save area (RSA) for this function |48// |----------------------------------------------|49// | Return address for this function |50// |----------------------------------------------|51// | Frame pointer for this function |52// |----------------------------------------------| <- fp(=old sp)53// |.empty.space.to.make.part.below.aligned.in....|54// |.case.it.needs.more.than.the.standard.16-byte.| (size of this area is55// |.alignment....................................| unknown at compile time)56// |----------------------------------------------|57// | Local variables of fixed size including spill|58// | slots |59// |----------------------------------------------| <- bp(not defined by ABI,60// |.variable-sized.local.variables.(VLAs)........| LLVM chooses SX17)61// |..............................................| (size of this area is62// |..............................................| unknown at compile time)63// |----------------------------------------------| <- stack top (returned by64// | Parameter area for callee | alloca)65// |----------------------------------------------|66// | Register save area (RSA) for callee |67// |----------------------------------------------|68// | Return address for callee |69// |----------------------------------------------|70// | Frame pointer for callee |71// |----------------------------------------------| <- sp72// | | Lower address73//74// To access the data in a frame, at-compile time, a constant offset must be75// computable from one of the pointers (fp, bp, sp) to access it. The size76// of the areas with a dotted background cannot be computed at compile-time77// if they are present, making it required to have all three of fp, bp and78// sp to be set up to be able to access all contents in the frame areas,79// assuming all of the frame areas are non-empty.80//81// For most functions, some of the frame areas are empty. For those functions,82// it may not be necessary to set up fp or bp:83// * A base pointer is definitely needed when there are both VLAs and local84// variables with more-than-default alignment requirements.85// * A frame pointer is definitely needed when there are local variables with86// more-than-default alignment requirements.87//88// In addition, VE ABI defines RSA frame, return address, and frame pointer89// as follows:90//91// |----------------------------------------------| <- sp+17692// | %s18...%s33 |93// |----------------------------------------------| <- sp+4894// | Linkage area register (%s17) |95// |----------------------------------------------| <- sp+4096// | Procedure linkage table register (%plt=%s16) |97// |----------------------------------------------| <- sp+3298// | Global offset table register (%got=%s15) |99// |----------------------------------------------| <- sp+24100// | Thread pointer register (%tp=%s14) |101// |----------------------------------------------| <- sp+16102// | Return address |103// |----------------------------------------------| <- sp+8104// | Frame pointer |105// |----------------------------------------------| <- sp+0106//107// NOTE: This description is based on VE ABI and description in108// AArch64FrameLowering.cpp. Thanks a lot.109//===----------------------------------------------------------------------===//110111#include "VEFrameLowering.h"112#include "VEInstrInfo.h"113#include "VEMachineFunctionInfo.h"114#include "VESubtarget.h"115#include "llvm/CodeGen/MachineFrameInfo.h"116#include "llvm/CodeGen/MachineFunction.h"117#include "llvm/CodeGen/MachineInstrBuilder.h"118#include "llvm/CodeGen/MachineModuleInfo.h"119#include "llvm/CodeGen/MachineRegisterInfo.h"120#include "llvm/CodeGen/RegisterScavenging.h"121#include "llvm/IR/DataLayout.h"122#include "llvm/IR/Function.h"123#include "llvm/Support/CommandLine.h"124#include "llvm/Target/TargetOptions.h"125#include "llvm/Support/MathExtras.h"126127using namespace llvm;128129VEFrameLowering::VEFrameLowering(const VESubtarget &ST)130: TargetFrameLowering(TargetFrameLowering::StackGrowsDown, Align(16), 0,131Align(16)),132STI(ST) {}133134void VEFrameLowering::emitPrologueInsns(MachineFunction &MF,135MachineBasicBlock &MBB,136MachineBasicBlock::iterator MBBI,137uint64_t NumBytes,138bool RequireFPUpdate) const {139const VEMachineFunctionInfo *FuncInfo = MF.getInfo<VEMachineFunctionInfo>();140DebugLoc DL;141const VEInstrInfo &TII = *STI.getInstrInfo();142143// Insert following codes here as prologue144//145// st %fp, 0(, %sp) iff !isLeafProc146// st %lr, 8(, %sp) iff !isLeafProc147// st %got, 24(, %sp) iff hasGOT148// st %plt, 32(, %sp) iff hasGOT149// st %s17, 40(, %sp) iff hasBP150if (!FuncInfo->isLeafProc()) {151BuildMI(MBB, MBBI, DL, TII.get(VE::STrii))152.addReg(VE::SX11)153.addImm(0)154.addImm(0)155.addReg(VE::SX9);156BuildMI(MBB, MBBI, DL, TII.get(VE::STrii))157.addReg(VE::SX11)158.addImm(0)159.addImm(8)160.addReg(VE::SX10);161}162if (hasGOT(MF)) {163BuildMI(MBB, MBBI, DL, TII.get(VE::STrii))164.addReg(VE::SX11)165.addImm(0)166.addImm(24)167.addReg(VE::SX15);168BuildMI(MBB, MBBI, DL, TII.get(VE::STrii))169.addReg(VE::SX11)170.addImm(0)171.addImm(32)172.addReg(VE::SX16);173}174if (hasBP(MF))175BuildMI(MBB, MBBI, DL, TII.get(VE::STrii))176.addReg(VE::SX11)177.addImm(0)178.addImm(40)179.addReg(VE::SX17);180}181182void VEFrameLowering::emitEpilogueInsns(MachineFunction &MF,183MachineBasicBlock &MBB,184MachineBasicBlock::iterator MBBI,185uint64_t NumBytes,186bool RequireFPUpdate) const {187const VEMachineFunctionInfo *FuncInfo = MF.getInfo<VEMachineFunctionInfo>();188DebugLoc DL;189const VEInstrInfo &TII = *STI.getInstrInfo();190191// Insert following codes here as epilogue192//193// ld %s17, 40(, %sp) iff hasBP194// ld %plt, 32(, %sp) iff hasGOT195// ld %got, 24(, %sp) iff hasGOT196// ld %lr, 8(, %sp) iff !isLeafProc197// ld %fp, 0(, %sp) iff !isLeafProc198if (hasBP(MF))199BuildMI(MBB, MBBI, DL, TII.get(VE::LDrii), VE::SX17)200.addReg(VE::SX11)201.addImm(0)202.addImm(40);203if (hasGOT(MF)) {204BuildMI(MBB, MBBI, DL, TII.get(VE::LDrii), VE::SX16)205.addReg(VE::SX11)206.addImm(0)207.addImm(32);208BuildMI(MBB, MBBI, DL, TII.get(VE::LDrii), VE::SX15)209.addReg(VE::SX11)210.addImm(0)211.addImm(24);212}213if (!FuncInfo->isLeafProc()) {214BuildMI(MBB, MBBI, DL, TII.get(VE::LDrii), VE::SX10)215.addReg(VE::SX11)216.addImm(0)217.addImm(8);218BuildMI(MBB, MBBI, DL, TII.get(VE::LDrii), VE::SX9)219.addReg(VE::SX11)220.addImm(0)221.addImm(0);222}223}224225void VEFrameLowering::emitSPAdjustment(MachineFunction &MF,226MachineBasicBlock &MBB,227MachineBasicBlock::iterator MBBI,228int64_t NumBytes,229MaybeAlign MaybeAlign) const {230DebugLoc DL;231const VEInstrInfo &TII = *STI.getInstrInfo();232233if (NumBytes == 0) {234// Nothing to do here.235} else if (isInt<7>(NumBytes)) {236// adds.l %s11, NumBytes@lo, %s11237BuildMI(MBB, MBBI, DL, TII.get(VE::ADDSLri), VE::SX11)238.addReg(VE::SX11)239.addImm(NumBytes);240} else if (isInt<32>(NumBytes)) {241// lea %s11, NumBytes@lo(, %s11)242BuildMI(MBB, MBBI, DL, TII.get(VE::LEArii), VE::SX11)243.addReg(VE::SX11)244.addImm(0)245.addImm(Lo_32(NumBytes));246} else {247// Emit following codes. This clobbers SX13 which we always know is248// available here.249// lea %s13, NumBytes@lo250// and %s13, %s13, (32)0251// lea.sl %sp, NumBytes@hi(%s13, %sp)252BuildMI(MBB, MBBI, DL, TII.get(VE::LEAzii), VE::SX13)253.addImm(0)254.addImm(0)255.addImm(Lo_32(NumBytes));256BuildMI(MBB, MBBI, DL, TII.get(VE::ANDrm), VE::SX13)257.addReg(VE::SX13)258.addImm(M0(32));259BuildMI(MBB, MBBI, DL, TII.get(VE::LEASLrri), VE::SX11)260.addReg(VE::SX11)261.addReg(VE::SX13)262.addImm(Hi_32(NumBytes));263}264265if (MaybeAlign) {266// and %sp, %sp, Align-1267BuildMI(MBB, MBBI, DL, TII.get(VE::ANDrm), VE::SX11)268.addReg(VE::SX11)269.addImm(M1(64 - Log2_64(MaybeAlign.valueOrOne().value())));270}271}272273void VEFrameLowering::emitSPExtend(MachineFunction &MF, MachineBasicBlock &MBB,274MachineBasicBlock::iterator MBBI) const {275DebugLoc DL;276const VEInstrInfo &TII = *STI.getInstrInfo();277278// Emit following codes. It is not possible to insert multiple279// BasicBlocks in PEI pass, so we emit two pseudo instructions here.280//281// EXTEND_STACK // pseudo instrcution282// EXTEND_STACK_GUARD // pseudo instrcution283//284// EXTEND_STACK pseudo will be converted by ExpandPostRA pass into285// following instructions with multiple basic blocks later.286//287// thisBB:288// brge.l.t %sp, %sl, sinkBB289// syscallBB:290// ld %s61, 0x18(, %tp) // load param area291// or %s62, 0, %s0 // spill the value of %s0292// lea %s63, 0x13b // syscall # of grow293// shm.l %s63, 0x0(%s61) // store syscall # at addr:0294// shm.l %sl, 0x8(%s61) // store old limit at addr:8295// shm.l %sp, 0x10(%s61) // store new limit at addr:16296// monc // call monitor297// or %s0, 0, %s62 // restore the value of %s0298// sinkBB:299//300// EXTEND_STACK_GUARD pseudo will be simply eliminated by ExpandPostRA301// pass. This pseudo is required to be at the next of EXTEND_STACK302// pseudo in order to protect iteration loop in ExpandPostRA.303BuildMI(MBB, MBBI, DL, TII.get(VE::EXTEND_STACK));304BuildMI(MBB, MBBI, DL, TII.get(VE::EXTEND_STACK_GUARD));305}306307void VEFrameLowering::emitPrologue(MachineFunction &MF,308MachineBasicBlock &MBB) const {309const VEMachineFunctionInfo *FuncInfo = MF.getInfo<VEMachineFunctionInfo>();310assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");311MachineFrameInfo &MFI = MF.getFrameInfo();312const VEInstrInfo &TII = *STI.getInstrInfo();313const VERegisterInfo &RegInfo = *STI.getRegisterInfo();314MachineBasicBlock::iterator MBBI = MBB.begin();315bool NeedsStackRealignment = RegInfo.shouldRealignStack(MF);316317// Debug location must be unknown since the first debug location is used318// to determine the end of the prologue.319DebugLoc DL;320321if (NeedsStackRealignment && !RegInfo.canRealignStack(MF))322report_fatal_error("Function \"" + Twine(MF.getName()) +323"\" required "324"stack re-alignment, but LLVM couldn't handle it "325"(probably because it has a dynamic alloca).");326327// Get the number of bytes to allocate from the FrameInfo.328// This number of bytes is already aligned to ABI stack alignment.329uint64_t NumBytes = MFI.getStackSize();330331// Adjust stack size if this function is not a leaf function since the332// VE ABI requires a reserved area at the top of stack as described in333// VEFrameLowering.cpp.334if (!FuncInfo->isLeafProc()) {335// NOTE: The number is aligned to ABI stack alignment after adjustment.336NumBytes = STI.getAdjustedFrameSize(NumBytes);337}338339// Finally, ensure that the size is sufficiently aligned for the340// data on the stack.341NumBytes = alignTo(NumBytes, MFI.getMaxAlign());342343// Update stack size with corrected value.344MFI.setStackSize(NumBytes);345346// Emit Prologue instructions to save multiple registers.347emitPrologueInsns(MF, MBB, MBBI, NumBytes, true);348349// Emit instructions to save SP in FP as follows if this is not a leaf350// function:351// or %fp, 0, %sp352if (!FuncInfo->isLeafProc())353BuildMI(MBB, MBBI, DL, TII.get(VE::ORri), VE::SX9)354.addReg(VE::SX11)355.addImm(0);356357// Emit stack adjust instructions358MaybeAlign RuntimeAlign =359NeedsStackRealignment ? MaybeAlign(MFI.getMaxAlign()) : std::nullopt;360assert((RuntimeAlign == std::nullopt || !FuncInfo->isLeafProc()) &&361"SP has to be saved in order to align variable sized stack object!");362emitSPAdjustment(MF, MBB, MBBI, -(int64_t)NumBytes, RuntimeAlign);363364if (hasBP(MF)) {365// Copy SP to BP.366BuildMI(MBB, MBBI, DL, TII.get(VE::ORri), VE::SX17)367.addReg(VE::SX11)368.addImm(0);369}370371// Emit stack extend instructions372if (NumBytes != 0)373emitSPExtend(MF, MBB, MBBI);374}375376MachineBasicBlock::iterator VEFrameLowering::eliminateCallFramePseudoInstr(377MachineFunction &MF, MachineBasicBlock &MBB,378MachineBasicBlock::iterator I) const {379if (!hasReservedCallFrame(MF)) {380MachineInstr &MI = *I;381int64_t Size = MI.getOperand(0).getImm();382if (MI.getOpcode() == VE::ADJCALLSTACKDOWN)383Size = -Size;384385if (Size)386emitSPAdjustment(MF, MBB, I, Size);387}388return MBB.erase(I);389}390391void VEFrameLowering::emitEpilogue(MachineFunction &MF,392MachineBasicBlock &MBB) const {393const VEMachineFunctionInfo *FuncInfo = MF.getInfo<VEMachineFunctionInfo>();394DebugLoc DL;395MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();396MachineFrameInfo &MFI = MF.getFrameInfo();397const VEInstrInfo &TII = *STI.getInstrInfo();398399uint64_t NumBytes = MFI.getStackSize();400401// Emit instructions to retrieve original SP.402if (!FuncInfo->isLeafProc()) {403// If SP is saved in FP, retrieve it as follows:404// or %sp, 0, %fp iff !isLeafProc405BuildMI(MBB, MBBI, DL, TII.get(VE::ORri), VE::SX11)406.addReg(VE::SX9)407.addImm(0);408} else {409// Emit stack adjust instructions.410emitSPAdjustment(MF, MBB, MBBI, NumBytes, std::nullopt);411}412413// Emit Epilogue instructions to restore multiple registers.414emitEpilogueInsns(MF, MBB, MBBI, NumBytes, true);415}416417// hasFP - Return true if the specified function should have a dedicated frame418// pointer register. This is true if the function has variable sized allocas419// or if frame pointer elimination is disabled.420bool VEFrameLowering::hasFP(const MachineFunction &MF) const {421const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();422423const MachineFrameInfo &MFI = MF.getFrameInfo();424return MF.getTarget().Options.DisableFramePointerElim(MF) ||425RegInfo->hasStackRealignment(MF) || MFI.hasVarSizedObjects() ||426MFI.isFrameAddressTaken();427}428429bool VEFrameLowering::hasBP(const MachineFunction &MF) const {430const MachineFrameInfo &MFI = MF.getFrameInfo();431const TargetRegisterInfo *TRI = STI.getRegisterInfo();432433return MFI.hasVarSizedObjects() && TRI->hasStackRealignment(MF);434}435436bool VEFrameLowering::hasGOT(const MachineFunction &MF) const {437const VEMachineFunctionInfo *FuncInfo = MF.getInfo<VEMachineFunctionInfo>();438439// If a global base register is assigned (!= 0), GOT is used.440return FuncInfo->getGlobalBaseReg() != 0;441}442443StackOffset VEFrameLowering::getFrameIndexReference(const MachineFunction &MF,444int FI,445Register &FrameReg) const {446const MachineFrameInfo &MFI = MF.getFrameInfo();447const VERegisterInfo *RegInfo = STI.getRegisterInfo();448bool isFixed = MFI.isFixedObjectIndex(FI);449450int64_t FrameOffset = MF.getFrameInfo().getObjectOffset(FI);451452if (!hasFP(MF)) {453// If FP is not used, frame indexies are based on a %sp regiter.454FrameReg = VE::SX11; // %sp455return StackOffset::getFixed(FrameOffset +456MF.getFrameInfo().getStackSize());457}458if (RegInfo->hasStackRealignment(MF) && !isFixed) {459// If data on stack require realignemnt, frame indexies are based on a %sp460// or %s17 (bp) register. If there is a variable sized object, bp is used.461if (hasBP(MF))462FrameReg = VE::SX17; // %bp463else464FrameReg = VE::SX11; // %sp465return StackOffset::getFixed(FrameOffset +466MF.getFrameInfo().getStackSize());467}468// Use %fp by default.469FrameReg = RegInfo->getFrameRegister(MF);470return StackOffset::getFixed(FrameOffset);471}472473bool VEFrameLowering::isLeafProc(MachineFunction &MF) const {474475MachineRegisterInfo &MRI = MF.getRegInfo();476MachineFrameInfo &MFI = MF.getFrameInfo();477478return !MFI.hasCalls() // No calls479&& !MRI.isPhysRegUsed(VE::SX18) // Registers within limits480// (s18 is first CSR)481&& !MRI.isPhysRegUsed(VE::SX11) // %sp un-used482&& !hasFP(MF); // Don't need %fp483}484485void VEFrameLowering::determineCalleeSaves(MachineFunction &MF,486BitVector &SavedRegs,487RegScavenger *RS) const {488TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);489490// Functions having BP need to emit prologue and epilogue to allocate local491// buffer on the stack even if the function is a leaf function.492if (isLeafProc(MF) && !hasBP(MF)) {493VEMachineFunctionInfo *FuncInfo = MF.getInfo<VEMachineFunctionInfo>();494FuncInfo->setLeafProc(true);495}496}497498499