LLVM 20.0.0git
BPFRegisterInfo.cpp
Go to the documentation of this file.
1//===-- BPFRegisterInfo.cpp - BPF Register Information ----------*- 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 contains the BPF implementation of the TargetRegisterInfo class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "BPFRegisterInfo.h"
14#include "BPF.h"
15#include "BPFSubtarget.h"
25
26#define GET_REGINFO_TARGET_DESC
27#include "BPFGenRegisterInfo.inc"
28using namespace llvm;
29
30static cl::opt<int>
31 BPFStackSizeOption("bpf-stack-size",
32 cl::desc("Specify the BPF stack size limit"),
33 cl::init(512));
34
36 : BPFGenRegisterInfo(BPF::R0) {}
37
38const MCPhysReg *
40 return CSR_SaveList;
41}
42
43const uint32_t *
45 CallingConv::ID CC) const {
46 switch (CC) {
47 default:
48 return CSR_RegMask;
50 return CSR_PreserveAll_RegMask;
51 }
52}
53
55 BitVector Reserved(getNumRegs());
56 markSuperRegs(Reserved, BPF::W10); // [W|R]10 is read only frame pointer
57 markSuperRegs(Reserved, BPF::W11); // [W|R]11 is pseudo stack pointer
58 return Reserved;
59}
60
61static void WarnSize(int Offset, MachineFunction &MF, DebugLoc& DL,
63 if (Offset <= -BPFStackSizeOption) {
64 if (!DL)
65 /* try harder to get some debug loc */
66 for (auto &I : MBB)
67 if (I.getDebugLoc()) {
68 DL = I.getDebugLoc();
69 break;
70 }
71
72 const Function &F = MF.getFunction();
73 DiagnosticInfoUnsupported DiagStackSize(
74 F,
75 "Looks like the BPF stack limit is exceeded. "
76 "Please move large on stack variables into BPF per-cpu array map. For "
77 "non-kernel uses, the stack can be increased using -mllvm "
78 "-bpf-stack-size.\n",
79 DL);
80 F.getContext().diagnose(DiagStackSize);
81 }
82}
83
85 int SPAdj, unsigned FIOperandNum,
86 RegScavenger *RS) const {
87 assert(SPAdj == 0 && "Unexpected");
88
89 unsigned i = 0;
90 MachineInstr &MI = *II;
91 MachineBasicBlock &MBB = *MI.getParent();
93 DebugLoc DL = MI.getDebugLoc();
94
95 while (!MI.getOperand(i).isFI()) {
96 ++i;
97 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
98 }
99
100 Register FrameReg = getFrameRegister(MF);
101 int FrameIndex = MI.getOperand(i).getIndex();
103
104 if (MI.getOpcode() == BPF::MOV_rr) {
105 int Offset = MF.getFrameInfo().getObjectOffset(FrameIndex);
106
107 WarnSize(Offset, MF, DL, MBB);
108 MI.getOperand(i).ChangeToRegister(FrameReg, false);
109 Register reg = MI.getOperand(i - 1).getReg();
110 BuildMI(MBB, ++II, DL, TII.get(BPF::ADD_ri), reg)
111 .addReg(reg)
112 .addImm(Offset);
113 return false;
114 }
115
116 int Offset = MF.getFrameInfo().getObjectOffset(FrameIndex) +
117 MI.getOperand(i + 1).getImm();
118
119 if (!isInt<32>(Offset))
120 llvm_unreachable("bug in frame offset");
121
122 WarnSize(Offset, MF, DL, MBB);
123
124 if (MI.getOpcode() == BPF::FI_ri) {
125 // architecture does not really support FI_ri, replace it with
126 // MOV_rr <target_reg>, frame_reg
127 // ADD_ri <target_reg>, imm
128 Register reg = MI.getOperand(i - 1).getReg();
129
130 BuildMI(MBB, ++II, DL, TII.get(BPF::MOV_rr), reg)
131 .addReg(FrameReg);
132 BuildMI(MBB, II, DL, TII.get(BPF::ADD_ri), reg)
133 .addReg(reg)
134 .addImm(Offset);
135
136 // Remove FI_ri instruction
137 MI.eraseFromParent();
138 } else {
139 MI.getOperand(i).ChangeToRegister(FrameReg, false);
140 MI.getOperand(i + 1).ChangeToImmediate(Offset);
141 }
142 return false;
143}
144
146 return BPF::R10;
147}
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static void WarnSize(int Offset, MachineFunction &MF, DebugLoc &DL, MachineBasicBlock &MBB)
static cl::opt< int > BPFStackSizeOption("bpf-stack-size", cl::desc("Specify the BPF stack size limit"), cl::init(512))
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
uint64_t IntrinsicInst * II
This file declares the machine register scavenger class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A debug info location.
Definition: DebugLoc.h:33
Diagnostic information for unsupported feature in backend.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
Definition: MachineInstr.h:69
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
TargetInstrInfo - Interface to description of machine instruction set.
virtual const TargetInstrInfo * getInstrInfo() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ PreserveAll
Used for runtime calls that preserves (almost) all registers.
Definition: CallingConv.h:66
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
const MCPhysReg * getCalleeSavedRegs(const MachineFunction *MF) const override
BitVector getReservedRegs(const MachineFunction &MF) const override
Register getFrameRegister(const MachineFunction &MF) const override
bool eliminateFrameIndex(MachineBasicBlock::iterator MI, int SPAdj, unsigned FIOperandNum, RegScavenger *RS=nullptr) const override
const uint32_t * getCallPreservedMask(const MachineFunction &MF, CallingConv::ID) const override