xref: /llvm-project/llvm/lib/Target/RISCV/RISCVMachineFunctionInfo.cpp (revision 708a478d6739aea20a8834cea45490f05b07ca10)
1 //=- RISCVMachineFunctionInfo.cpp - RISC-V machine function info --*- C++ -*-=//
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 declares RISCV-specific per-machine-function information.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "RISCVMachineFunctionInfo.h"
14 #include "llvm/IR/Module.h"
15 
16 using namespace llvm;
17 
18 yaml::RISCVMachineFunctionInfo::RISCVMachineFunctionInfo(
19     const llvm::RISCVMachineFunctionInfo &MFI)
20     : VarArgsFrameIndex(MFI.getVarArgsFrameIndex()),
21       VarArgsSaveSize(MFI.getVarArgsSaveSize()) {}
22 
23 MachineFunctionInfo *RISCVMachineFunctionInfo::clone(
24     BumpPtrAllocator &Allocator, MachineFunction &DestMF,
25     const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB)
26     const {
27   return DestMF.cloneInfo<RISCVMachineFunctionInfo>(*this);
28 }
29 
30 RISCVMachineFunctionInfo::RISCVMachineFunctionInfo(const Function &F,
31                                                    const RISCVSubtarget *STI) {
32 
33   // The default stack probe size is 4096 if the function has no
34   // stack-probe-size attribute. This is a safe default because it is the
35   // smallest possible guard page size.
36   uint64_t ProbeSize = 4096;
37   if (F.hasFnAttribute("stack-probe-size"))
38     ProbeSize = F.getFnAttributeAsParsedInteger("stack-probe-size");
39   else if (const auto *PS = mdconst::extract_or_null<ConstantInt>(
40                F.getParent()->getModuleFlag("stack-probe-size")))
41     ProbeSize = PS->getZExtValue();
42   assert(int64_t(ProbeSize) > 0 && "Invalid stack probe size");
43 
44   // Round down to the stack alignment.
45   uint64_t StackAlign =
46       STI->getFrameLowering()->getTransientStackAlign().value();
47   ProbeSize = std::max(StackAlign, alignDown(ProbeSize, StackAlign));
48   StringRef ProbeKind;
49   if (F.hasFnAttribute("probe-stack"))
50     ProbeKind = F.getFnAttribute("probe-stack").getValueAsString();
51   else if (const auto *PS = dyn_cast_or_null<MDString>(
52                F.getParent()->getModuleFlag("probe-stack")))
53     ProbeKind = PS->getString();
54   if (ProbeKind.size()) {
55     StackProbeSize = ProbeSize;
56   }
57 }
58 
59 void yaml::RISCVMachineFunctionInfo::mappingImpl(yaml::IO &YamlIO) {
60   MappingTraits<RISCVMachineFunctionInfo>::mapping(YamlIO, *this);
61 }
62 
63 void RISCVMachineFunctionInfo::initializeBaseYamlFields(
64     const yaml::RISCVMachineFunctionInfo &YamlMFI) {
65   VarArgsFrameIndex = YamlMFI.VarArgsFrameIndex;
66   VarArgsSaveSize = YamlMFI.VarArgsSaveSize;
67 }
68 
69 void RISCVMachineFunctionInfo::addSExt32Register(Register Reg) {
70   SExt32Registers.push_back(Reg);
71 }
72 
73 bool RISCVMachineFunctionInfo::isSExt32Register(Register Reg) const {
74   return is_contained(SExt32Registers, Reg);
75 }
76