LLVM 22.0.0git
NVPTXInstrInfo.cpp
Go to the documentation of this file.
1//===- NVPTXInstrInfo.cpp - NVPTX Instruction 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 TargetInstrInfo class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "NVPTXInstrInfo.h"
14#include "NVPTX.h"
18
19using namespace llvm;
20
21#define GET_INSTRINFO_CTOR_DTOR
22#include "NVPTXGenInstrInfo.inc"
23
24// Pin the vtable to this file.
25void NVPTXInstrInfo::anchor() {}
26
28
31 const DebugLoc &DL, Register DestReg,
32 Register SrcReg, bool KillSrc,
33 bool RenamableDest, bool RenamableSrc) const {
35 const TargetRegisterClass *DestRC = MRI.getRegClass(DestReg);
36 const TargetRegisterClass *SrcRC = MRI.getRegClass(SrcReg);
37
38 if (DestRC != SrcRC)
39 report_fatal_error("Copy one register into another with a different width");
40
41 unsigned Op;
42 if (DestRC == &NVPTX::B1RegClass)
43 Op = NVPTX::MOV_B1_r;
44 else if (DestRC == &NVPTX::B16RegClass)
45 Op = NVPTX::MOV_B16_r;
46 else if (DestRC == &NVPTX::B32RegClass)
47 Op = NVPTX::MOV_B32_r;
48 else if (DestRC == &NVPTX::B64RegClass)
49 Op = NVPTX::MOV_B64_r;
50 else if (DestRC == &NVPTX::B128RegClass)
51 Op = NVPTX::MOV_B128_r;
52 else
53 llvm_unreachable("Bad register copy");
54
55 BuildMI(MBB, I, DL, get(Op), DestReg)
56 .addReg(SrcReg, getKillRegState(KillSrc));
57}
58
59/// analyzeBranch - Analyze the branching code at the end of MBB, returning
60/// true if it cannot be understood (e.g. it's a switch dispatch or isn't
61/// implemented for a target). Upon success, this returns false and returns
62/// with the following information in various cases:
63///
64/// 1. If this block ends with no branches (it just falls through to its succ)
65/// just return false, leaving TBB/FBB null.
66/// 2. If this block ends with only an unconditional branch, it sets TBB to be
67/// the destination block.
68/// 3. If this block ends with an conditional branch and it falls through to
69/// an successor block, it sets TBB to be the branch destination block and a
70/// list of operands that evaluate the condition. These
71/// operands can be passed to other TargetInstrInfo methods to create new
72/// branches.
73/// 4. If this block ends with an conditional branch and an unconditional
74/// block, it returns the 'true' destination in TBB, the 'false' destination
75/// in FBB, and a list of operands that evaluate the condition. These
76/// operands can be passed to other TargetInstrInfo methods to create new
77/// branches.
78///
79/// Note that removeBranch and insertBranch must be implemented to support
80/// cases where this method returns success.
81///
86 bool AllowModify) const {
87 // If the block has no terminators, it just falls into the block after it.
89 if (I == MBB.begin() || !isUnpredicatedTerminator(*--I))
90 return false;
91
92 // Get the last instruction in the block.
93 MachineInstr &LastInst = *I;
94
95 // If there is only one terminator instruction, process it.
96 if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
97 if (LastInst.getOpcode() == NVPTX::GOTO) {
98 TBB = LastInst.getOperand(0).getMBB();
99 return false;
100 } else if (LastInst.getOpcode() == NVPTX::CBranch) {
101 // Block ends with fall-through condbranch.
102 TBB = LastInst.getOperand(1).getMBB();
103 Cond.push_back(LastInst.getOperand(0));
104 return false;
105 }
106 // Otherwise, don't know what this is.
107 return true;
108 }
109
110 // Get the instruction before it if it's a terminator.
111 MachineInstr &SecondLastInst = *I;
112
113 // If there are three terminators, we don't know what sort of block this is.
114 if (I != MBB.begin() && isUnpredicatedTerminator(*--I))
115 return true;
116
117 // If the block ends with NVPTX::GOTO and NVPTX:CBranch, handle it.
118 if (SecondLastInst.getOpcode() == NVPTX::CBranch &&
119 LastInst.getOpcode() == NVPTX::GOTO) {
120 TBB = SecondLastInst.getOperand(1).getMBB();
121 Cond.push_back(SecondLastInst.getOperand(0));
122 FBB = LastInst.getOperand(0).getMBB();
123 return false;
124 }
125
126 // If the block ends with two NVPTX:GOTOs, handle it. The second one is not
127 // executed, so remove it.
128 if (SecondLastInst.getOpcode() == NVPTX::GOTO &&
129 LastInst.getOpcode() == NVPTX::GOTO) {
130 TBB = SecondLastInst.getOperand(0).getMBB();
131 I = LastInst;
132 if (AllowModify)
133 I->eraseFromParent();
134 return false;
135 }
136
137 // Otherwise, can't handle this.
138 return true;
139}
140
142 int *BytesRemoved) const {
143 assert(!BytesRemoved && "code size not handled");
145 if (I == MBB.begin())
146 return 0;
147 --I;
148 if (I->getOpcode() != NVPTX::GOTO && I->getOpcode() != NVPTX::CBranch)
149 return 0;
150
151 // Remove the branch.
152 I->eraseFromParent();
153
154 I = MBB.end();
155
156 if (I == MBB.begin())
157 return 1;
158 --I;
159 if (I->getOpcode() != NVPTX::CBranch)
160 return 1;
161
162 // Remove the branch.
163 I->eraseFromParent();
164 return 2;
165}
166
171 const DebugLoc &DL,
172 int *BytesAdded) const {
173 assert(!BytesAdded && "code size not handled");
174
175 // Shouldn't be a fall through.
176 assert(TBB && "insertBranch must not be told to insert a fallthrough");
177 assert((Cond.size() == 1 || Cond.size() == 0) &&
178 "NVPTX branch conditions have two components!");
179
180 // One-way branch.
181 if (!FBB) {
182 if (Cond.empty()) // Unconditional branch
183 BuildMI(&MBB, DL, get(NVPTX::GOTO)).addMBB(TBB);
184 else // Conditional branch
185 BuildMI(&MBB, DL, get(NVPTX::CBranch)).add(Cond[0]).addMBB(TBB);
186 return 1;
187 }
188
189 // Two-way Conditional Branch.
190 BuildMI(&MBB, DL, get(NVPTX::CBranch)).add(Cond[0]).addMBB(TBB);
191 BuildMI(&MBB, DL, get(NVPTX::GOTO)).addMBB(FBB);
192 return 2;
193}
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define I(x, y, z)
Definition: MD5.cpp:58
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
This class represents an Operation in the Expression.
A debug info location.
Definition: DebugLoc.h:124
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
Definition: MachineInstr.h:72
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:587
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:595
MachineBasicBlock * getMBB() const
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, ArrayRef< MachineOperand > Cond, const DebugLoc &DL, int *BytesAdded=nullptr) const override
unsigned removeBranch(MachineBasicBlock &MBB, int *BytesRemoved=nullptr) const override
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const override
analyzeBranch - Analyze the branching code at the end of MBB, returning true if it cannot be understo...
void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, const DebugLoc &DL, Register DestReg, Register SrcReg, bool KillSrc, bool RenamableDest=false, bool RenamableSrc=false) const override
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition: Error.cpp:167
unsigned getKillRegState(bool B)
DWARFExpression::Operation Op