LLVM 24.0.0git
NVPTXRegisterInfo.cpp
Go to the documentation of this file.
1//===- NVPTXRegisterInfo.cpp - NVPTX Register 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 NVPTX implementation of the TargetRegisterInfo class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "NVPTXRegisterInfo.h"
16#include "NVPTX.h"
17#include "NVPTXTargetMachine.h"
18#include "llvm/ADT/BitVector.h"
23
24using namespace llvm;
25
26#define DEBUG_TYPE "nvptx-reg-info"
27
29 if (RC == &NVPTX::B128RegClass)
30 return ".b128";
31 if (RC == &NVPTX::B64RegClass)
32 // We use untyped (.b) integer registers here as NVCC does.
33 // Correctness of generated code does not depend on register type,
34 // but using .s/.u registers runs into ptxas bug that prevents
35 // assembly of otherwise valid PTX into SASS. Despite PTX ISA
36 // specifying only argument size for fp16 instructions, ptxas does
37 // not allow using .s16 or .u16 arguments for .fp16
38 // instructions. At the same time it allows using .s32/.u32
39 // arguments for .fp16v2 instructions:
40 //
41 // .reg .b16 rb16
42 // .reg .s16 rs16
43 // add.f16 rb16,rb16,rb16; // OK
44 // add.f16 rs16,rs16,rs16; // Arguments mismatch for instruction 'add'
45 // but:
46 // .reg .b32 rb32
47 // .reg .s32 rs32
48 // add.f16v2 rb32,rb32,rb32; // OK
49 // add.f16v2 rs32,rs32,rs32; // OK
50 return ".b64";
51 if (RC == &NVPTX::B32RegClass)
52 return ".b32";
53 if (RC == &NVPTX::B16RegClass)
54 return ".b16";
55 if (RC == &NVPTX::B1RegClass)
56 return ".pred";
57 if (RC == &NVPTX::SpecialRegsRegClass)
58 return "!Special!";
59 return "INTERNAL";
60}
61
63 if (RC == &NVPTX::B128RegClass)
64 return "%rq";
65 if (RC == &NVPTX::B64RegClass)
66 return "%rd";
67 if (RC == &NVPTX::B32RegClass)
68 return "%r";
69 if (RC == &NVPTX::B16RegClass)
70 return "%rs";
71 if (RC == &NVPTX::B1RegClass)
72 return "%p";
73 if (RC == &NVPTX::SpecialRegsRegClass)
74 return "!Special!";
75 return "INTERNAL";
76}
77
80
81#define GET_REGINFO_TARGET_DESC
82#include "NVPTXGenRegisterInfo.inc"
83
84/// NVPTX Callee Saved Registers
85const MCPhysReg *
87 static const MCPhysReg CalleeSavedRegs[] = { 0 };
88 return CalleeSavedRegs;
89}
90
92 BitVector Reserved(getNumRegs());
93 for (unsigned Reg = NVPTX::ENVREG0; Reg <= NVPTX::ENVREG31; ++Reg) {
94 markSuperRegs(Reserved, Reg);
95 }
96 markSuperRegs(Reserved, NVPTX::VRFrame32);
97 markSuperRegs(Reserved, NVPTX::VRFrameLocal32);
98 markSuperRegs(Reserved, NVPTX::VRFrame64);
99 markSuperRegs(Reserved, NVPTX::VRFrameLocal64);
100 markSuperRegs(Reserved, NVPTX::VRDepot);
101 return Reserved;
102}
103
105 int SPAdj, unsigned FIOperandNum,
106 RegScavenger *) const {
107 assert(SPAdj == 0 && "Unexpected");
108
109 MachineInstr &MI = *II;
110 if (MI.isLifetimeMarker()) {
111 MI.eraseFromParent();
112 return true;
113 }
114
115 const int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
116
117 const MachineFunction &MF = *MI.getParent()->getParent();
118 const MachineFrameInfo &MFI = MF.getFrameInfo();
119 const int Offset = MFI.getObjectOffset(FrameIndex) +
120 MI.getOperand(FIOperandNum + 1).getImm();
121
122 // Local (addrspace 5) allocas are addressed through the local frame pointer
123 // (%SPL); everything else uses the generic frame pointer (%SP).
124 const AllocaInst *AI = MFI.getObjectAllocation(FrameIndex);
125 const Register FrameReg = AI && AI->getAddressSpace() == ADDRESS_SPACE_LOCAL
127 : getFrameRegister(MF);
128 MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false);
129 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
130 return false;
131}
132
134 const NVPTXTargetMachine &TM =
135 static_cast<const NVPTXTargetMachine &>(MF.getTarget());
136 return TM.is64Bit() ? NVPTX::VRFrame64 : NVPTX::VRFrame32;
137}
138
142 ? NVPTX::VRFrameLocal64
143 : NVPTX::VRFrameLocal32;
144}
145
147 debugRegisterMap.clear();
148}
149
151 if (RegisterName.size() > 8)
152 // The name is more than 8 characters long, and so won't fit into 64 bits.
153 return 0;
154
155 // Encode the name string into a DWARF register number using cuda-gdb's
156 // encoding. See cuda_check_dwarf2_reg_ptx_virtual_register in cuda-tdep.c,
157 // https://github.com/NVIDIA/cuda-gdb/blob/e5cf3bddae520ffb326f95b4d98ce5c7474b828b/gdb/cuda/cuda-tdep.c#L353
158 // IE the bytes of the string are concatenated in reverse into a single
159 // number, which is stored in ULEB128, but in practice must be no more than 8
160 // bytes (excluding null terminator, which is not included).
161 uint64_t result = 0;
162 for (unsigned char c : RegisterName)
163 result = (result << 8) | c;
164 return result;
165}
166
168 uint64_t preEncodedVirtualRegister, StringRef RegisterName) const {
169 uint64_t mapped = encodeRegisterForDwarf(RegisterName);
170 if (mapped == 0)
171 return;
172 debugRegisterMap.insert({preEncodedVirtualRegister, mapped});
173}
174
175int64_t NVPTXRegisterInfo::getDwarfRegNum(MCRegister RegNum, bool isEH) const {
177 // In NVPTXFrameLowering.cpp, we do arrange for %Depot to be accessible from
178 // %SP. Using the %Depot register doesn't provide any debug info in
179 // cuda-gdb, but switching it to %SP does.
180 if (RegNum.id() == NVPTX::VRDepot)
181 Name = "%SP";
182 return encodeRegisterForDwarf(Name);
183}
184
186 bool isEH) const {
187 assert(RegNum.isVirtual());
188 uint64_t lookup = debugRegisterMap.lookup(RegNum.id());
189 if (lookup)
190 return lookup;
191 return -1;
192}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements the BitVector class.
IRTranslator LLVM IR MI
static bool lookup(const GsymReader &GR, GsymDataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs, llvm::Error &Err)
A Lookup helper functions.
static uint64_t encodeRegisterForDwarf(StringRef RegisterName)
uint64_t IntrinsicInst * II
an instruction to allocate memory on the stack
unsigned getAddressSpace() const
Return the address space for the allocation.
unsigned getPointerSizeInBits(unsigned AS=0) const
The size in bits of the pointer representation in a given address space.
Definition DataLayout.h:501
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
constexpr unsigned id() const
Definition MCRegister.h:82
MachineInstrBundleIterator< MachineInstr > iterator
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
const AllocaInst * getObjectAllocation(int ObjectIdx) const
Return the underlying Alloca of the specified stack object if it exists.
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Representation of each machine instruction.
static const char * getRegisterName(MCRegister Reg)
Register getFrameLocalRegister(const MachineFunction &MF) const
const MCPhysReg * getCalleeSavedRegs(const MachineFunction *MF) const override
NVPTX Callee Saved Registers.
bool eliminateFrameIndex(MachineBasicBlock::iterator MI, int SPAdj, unsigned FIOperandNum, RegScavenger *RS=nullptr) const override
BitVector getReservedRegs(const MachineFunction &MF) const override
int64_t getDwarfRegNum(MCRegister RegNum, bool isEH) const override
Register getFrameRegister(const MachineFunction &MF) const override
void addToDebugRegisterMap(uint64_t preEncodedVirtualRegister, StringRef RegisterName) const
int64_t getDwarfRegNumForVirtReg(Register RegNum, bool isEH) const override
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr unsigned id() const
Definition Register.h:100
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:578
StringRef getNVPTXRegClassStr(const TargetRegisterClass *RC)
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
StringRef getNVPTXRegClassName(const TargetRegisterClass *RC)
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58