LLVM 22.0.0git
SPIRVInstrInfo.cpp
Go to the documentation of this file.
1//===-- SPIRVInstrInfo.cpp - SPIR-V Instruction 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 SPIR-V implementation of the TargetInstrInfo class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "SPIRVInstrInfo.h"
14#include "SPIRV.h"
15#include "SPIRVSubtarget.h"
19#include "llvm/IR/DebugLoc.h"
20
21#define GET_INSTRINFO_CTOR_DTOR
22#include "SPIRVGenInstrInfo.inc"
23
24using namespace llvm;
25
28
30 switch (MI.getOpcode()) {
31 case SPIRV::OpConstantTrue:
32 case SPIRV::OpConstantFalse:
33 case SPIRV::OpConstantI:
34 case SPIRV::OpConstantF:
35 case SPIRV::OpConstantComposite:
36 case SPIRV::OpConstantCompositeContinuedINTEL:
37 case SPIRV::OpConstantSampler:
38 case SPIRV::OpConstantNull:
39 case SPIRV::OpSpecConstantTrue:
40 case SPIRV::OpSpecConstantFalse:
41 case SPIRV::OpSpecConstant:
42 case SPIRV::OpSpecConstantComposite:
43 case SPIRV::OpSpecConstantCompositeContinuedINTEL:
44 case SPIRV::OpSpecConstantOp:
45 case SPIRV::OpUndef:
46 case SPIRV::OpConstantFunctionPointerINTEL:
47 return true;
48 default:
49 return false;
50 }
51}
52
54 switch (MI.getOpcode()) {
55 case SPIRV::OpSpecConstantTrue:
56 case SPIRV::OpSpecConstantFalse:
57 case SPIRV::OpSpecConstant:
58 case SPIRV::OpSpecConstantComposite:
59 case SPIRV::OpSpecConstantCompositeContinuedINTEL:
60 case SPIRV::OpSpecConstantOp:
61 return true;
62 default:
63 return false;
64 }
65}
66
68 switch (MI.getOpcode()) {
69 case SPIRV::OpAsmTargetINTEL:
70 case SPIRV::OpAsmINTEL:
71 return true;
72 default:
73 return false;
74 }
75}
76
78 auto &MRI = MI.getMF()->getRegInfo();
79 if (MI.getNumDefs() >= 1 && MI.getOperand(0).isReg()) {
80 auto DefRegClass = MRI.getRegClassOrNull(MI.getOperand(0).getReg());
81 return DefRegClass && DefRegClass->getID() == SPIRV::TYPERegClass.getID();
82 } else {
83 return MI.getOpcode() == SPIRV::OpTypeForwardPointer ||
84 MI.getOpcode() == SPIRV::OpTypeStructContinuedINTEL;
85 }
86}
87
89 switch (MI.getOpcode()) {
90 case SPIRV::OpDecorate:
91 case SPIRV::OpDecorateId:
92 case SPIRV::OpDecorateString:
93 case SPIRV::OpMemberDecorate:
94 case SPIRV::OpMemberDecorateString:
95 return true;
96 default:
97 return false;
98 }
99}
100
102 switch (MI.getOpcode()) {
103 case SPIRV::OpAliasDomainDeclINTEL:
104 case SPIRV::OpAliasScopeDeclINTEL:
105 case SPIRV::OpAliasScopeListDeclINTEL:
106 return true;
107 default:
108 return false;
109 }
110}
111
113 switch (MI.getOpcode()) {
114 case SPIRV::OpCapability:
115 case SPIRV::OpExtension:
116 case SPIRV::OpExtInstImport:
117 case SPIRV::OpMemoryModel:
118 case SPIRV::OpEntryPoint:
119 case SPIRV::OpExecutionMode:
120 case SPIRV::OpExecutionModeId:
121 case SPIRV::OpString:
122 case SPIRV::OpSourceExtension:
123 case SPIRV::OpSource:
124 case SPIRV::OpSourceContinued:
125 case SPIRV::OpName:
126 case SPIRV::OpMemberName:
127 case SPIRV::OpModuleProcessed:
128 return true;
129 default:
130 return isTypeDeclInstr(MI) || isConstantInstr(MI) ||
132 }
133}
134
136 switch (MI.getOpcode()) {
137 case SPIRV::OpFAddS:
138 case SPIRV::OpFSubS:
139 case SPIRV::OpFMulS:
140 case SPIRV::OpFDivS:
141 case SPIRV::OpFRemS:
142 case SPIRV::OpFAddV:
143 case SPIRV::OpFSubV:
144 case SPIRV::OpFMulV:
145 case SPIRV::OpFDivV:
146 case SPIRV::OpFRemV:
147 case SPIRV::OpFMod:
148 return true;
149 default:
150 return false;
151 }
152}
153
155 switch (MI.getOpcode()) {
156 case SPIRV::OpIAddS:
157 case SPIRV::OpIAddV:
158 case SPIRV::OpISubS:
159 case SPIRV::OpISubV:
160 case SPIRV::OpIMulS:
161 case SPIRV::OpIMulV:
162 case SPIRV::OpShiftLeftLogicalS:
163 case SPIRV::OpShiftLeftLogicalV:
164 case SPIRV::OpSNegate:
165 return true;
166 default:
167 return false;
168 }
169}
170
172 switch (MI.getOpcode()) {
173 case SPIRV::OpIAddS:
174 case SPIRV::OpIAddV:
175 case SPIRV::OpISubS:
176 case SPIRV::OpISubV:
177 case SPIRV::OpIMulS:
178 case SPIRV::OpIMulV:
179 return true;
180 default:
181 return false;
182 }
183}
184
185// Analyze the branching code at the end of MBB, returning
186// true if it cannot be understood (e.g. it's a switch dispatch or isn't
187// implemented for a target). Upon success, this returns false and returns
188// with the following information in various cases:
189//
190// 1. If this block ends with no branches (it just falls through to its succ)
191// just return false, leaving TBB/FBB null.
192// 2. If this block ends with only an unconditional branch, it sets TBB to be
193// the destination block.
194// 3. If this block ends with a conditional branch and it falls through to a
195// successor block, it sets TBB to be the branch destination block and a
196// list of operands that evaluate the condition. These operands can be
197// passed to other TargetInstrInfo methods to create new branches.
198// 4. If this block ends with a conditional branch followed by an
199// unconditional branch, it returns the 'true' destination in TBB, the
200// 'false' destination in FBB, and a list of operands that evaluate the
201// condition. These operands can be passed to other TargetInstrInfo
202// methods to create new branches.
203//
204// Note that removeBranch and insertBranch must be implemented to support
205// cases where this method returns success.
206//
207// If AllowModify is true, then this routine is allowed to modify the basic
208// block (e.g. delete instructions after the unconditional branch).
209//
210// The CFG information in MBB.Predecessors and MBB.Successors must be valid
211// before calling this function.
214 MachineBasicBlock *&FBB,
216 bool AllowModify) const {
217 // We do not allow to restructure blocks by results of analyzeBranch(),
218 // because it may potentially break structured control flow and anyway
219 // doubtedly may be useful in SPIRV, including such reasons as, e.g.:
220 // 1) there is no way to encode `if (Cond) then Stmt` logic, only full
221 // if-then-else is supported by OpBranchConditional, so if we supported
222 // splitting of blocks ending with OpBranchConditional MachineBasicBlock.cpp
223 // would expect successfull implementation of calls to insertBranch() setting
224 // FBB to null that is not feasible; 2) it's not possible to delete
225 // instructions after the unconditional branch, because this instruction must
226 // be the last instruction in a block.
227 return true;
228}
229
230// Remove the branching code at the end of the specific MBB.
231// This is only invoked in cases where analyzeBranch returns success. It
232// returns the number of instructions that were removed.
233// If \p BytesRemoved is non-null, report the change in code size from the
234// removed instructions.
236 int * /*BytesRemoved*/) const {
237 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
238 if (I == MBB.end())
239 return 0;
240
241 if (I->getOpcode() == SPIRV::OpBranch) {
242 I->eraseFromParent();
243 return 1;
244 }
245 return 0;
246}
247
248// Insert branch code into the end of the specified MachineBasicBlock. The
249// operands to this method are the same as those returned by analyzeBranch.
250// This is only invoked in cases where analyzeBranch returns success. It
251// returns the number of instructions inserted. If \p BytesAdded is non-null,
252// report the change in code size from the added instructions.
253//
254// It is also invoked by tail merging to add unconditional branches in
255// cases where analyzeBranch doesn't apply because there was no original
256// branch to analyze. At least this much must be implemented, else tail
257// merging needs to be disabled.
258//
259// The CFG information in MBB.Predecessors and MBB.Successors must be valid
260// before calling this function.
265 const DebugLoc &DL,
266 int * /*BytesAdded*/) const {
267 if (!TBB)
268 return 0;
269 BuildMI(&MBB, DL, get(SPIRV::OpBranch)).addMBB(TBB);
270 return 1;
271}
272
275 const DebugLoc &DL, Register DestReg,
276 Register SrcReg, bool KillSrc,
277 bool RenamableDest, bool RenamableSrc) const {
278 // Actually we don't need this COPY instruction. However if we do nothing with
279 // it, post RA pseudo instrs expansion just removes it and we get the code
280 // with undef registers. Therefore, we need to replace all uses of dst with
281 // the src register. COPY instr itself will be safely removed later.
282 assert(I->isCopy() && "Copy instruction is expected");
283 auto DstOp = I->getOperand(0);
284 auto SrcOp = I->getOperand(1);
285 assert(DstOp.isReg() && SrcOp.isReg() &&
286 "Register operands are expected in COPY");
287 auto &MRI = I->getMF()->getRegInfo();
288 MRI.replaceRegWith(DstOp.getReg(), SrcOp.getReg());
289}
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:58
This file declares the MachineIRBuilder class.
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
A debug info location.
Definition DebugLoc.h:124
Register getReg() const
MachineInstrBundleIterator< MachineInstr > iterator
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
Wrapper class representing virtual and physical registers.
Definition Register.h:19
bool isConstantInstr(const MachineInstr &MI) const
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify=false) const override
bool isInlineAsmDefInstr(const MachineInstr &MI) const
SPIRVInstrInfo(const SPIRVSubtarget &STI)
bool isTypeDeclInstr(const MachineInstr &MI) const
bool canUseFastMathFlags(const MachineInstr &MI) const
bool isDecorationInstr(const MachineInstr &MI) const
unsigned removeBranch(MachineBasicBlock &MBB, int *BytesRemoved=nullptr) const override
bool isAliasingInstr(const MachineInstr &MI) const
void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, const DebugLoc &DL, Register DestReg, Register SrcReg, bool KillSrc, bool RenamableDest=false, bool RenamableSrc=false) const override
bool isHeaderInstr(const MachineInstr &MI) const
bool canUseNUW(const MachineInstr &MI) const
bool isSpecConstantInstr(const MachineInstr &MI) const
bool canUseNSW(const MachineInstr &MI) const
unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, ArrayRef< MachineOperand > Cond, const DebugLoc &DL, int *BytesAdded=nullptr) const override
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Register getReg() const
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.
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)