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