LLVM 24.0.0git
PISAInstrInfo.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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/// \file
10/// This file implements instruction information for the PISA target.
11///
12//===----------------------------------------------------------------------===//
13
14#include "PISAInstrInfo.h"
15#include "PISA.h"
16#include "PISASubtarget.h"
20#include "llvm/IR/Constants.h"
21#include "llvm/IR/DebugLoc.h"
23
24#define GET_INSTRINFO_CTOR_DTOR
25#include "PISAGenInstrInfo.inc"
26
27using namespace llvm;
28using namespace PISA;
29
32
34 // Functions are emitted to C-style function signature.
35 // These instructions are not required to be in the output.
37}
38
40 switch (MI.getOpcode()) {
41 case PISA::functionParameter_i8:
42 case PISA::functionParameter_i16:
43 case PISA::functionParameter_i32:
44 case PISA::functionParameter_i64:
45 case PISA::functionParameter_v2i8:
46 case PISA::functionParameter_v3i8:
47 case PISA::functionParameter_v4i8:
48 case PISA::functionParameter_v2i16:
49 case PISA::functionParameter_v3i16:
50 case PISA::functionParameter_v4i16:
51 case PISA::functionParameter_v2i32:
52 case PISA::functionParameter_v3i32:
53 case PISA::functionParameter_v4i32:
54 case PISA::functionParameter_v5i32:
55 case PISA::functionParameter_v6i32:
56 case PISA::functionParameter_v7i32:
57 case PISA::functionParameter_v8i32:
58 case PISA::functionParameter_v16i32:
59 case PISA::functionParameter_v32i32:
60 case PISA::functionParameter_v64i32:
61 case PISA::functionParameter_v2i64:
62 case PISA::functionParameter_v3i64:
63 case PISA::functionParameter_v4i64:
64 return true;
65 default:
66 return false;
67 }
68}
69
70namespace llvm {
71namespace PISA {
72static const MachineOperand &getMO(const MachineInstr &MI, PISA::OpName Name) {
73 int16_t Idx = getNamedOperandIdx(MI.getOpcode(), Name);
74 assert(Idx >= 0 && "name not present!");
75
76 return MI.getOperand(Idx);
77}
78
81 MachineOperand &MO) {
82 unsigned SubReg = MO.getSubReg();
83 Register Reg = MO.getReg();
84 const TargetRegisterClass *SuperRC =
85 Reg.isPhysical() ? RI.getMinimalPhysRegClass(Reg) : MRI.getRegClass(Reg);
86 if (SubReg == 0)
87 return SuperRC;
88
89 return RI.getSubRegisterClass(SuperRC, SubReg);
90}
91} // namespace PISA
92} // namespace llvm
93
94// See description in TargetInstrInfo.h
99 bool /*AllowModify*/) const {
100 // If the block has no terminators, it just falls into the block after it.
101 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
102 if (I == MBB.end() || !isUnpredicatedTerminator(*I))
103 return false;
104
105 // Get the last instruction in the block.
106 MachineInstr *LastInst = &*I;
107
108 // If there is only one terminator instruction, process it.
109 if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
110 if (LastInst->isUnconditionalBranch()) {
111 if (LastInst->getOpcode() != PISA::gotolabel)
112 return true;
113 TBB = LastInst->getOperand(0).getMBB();
114 return false;
115 }
116 if (LastInst->isConditionalBranch()) {
117 if (LastInst->getOpcode() != PISA::predgoto)
118 return true;
119 // Block ends with fall-through condbranch.
120 TBB = getMO(*LastInst, OpName::label).getMBB();
121 // mod, pred
122 Cond.push_back(LastInst->getOperand(0));
123 Cond.push_back(LastInst->getOperand(1));
124 return false;
125 }
126 return true; // Can't handle indirect branch.
127 }
128
129 // Get the instruction before it if it is a terminator.
130 MachineInstr *SecondLastInst = &*I;
131
132 if (!SecondLastInst->isConditionalBranch() ||
133 !LastInst->isUnconditionalBranch() ||
134 // triple terminator?
135 (I != MBB.begin() && isUnpredicatedTerminator(*--I)))
136 return true;
137
138 if (SecondLastInst->getOpcode() != PISA::predgoto ||
139 LastInst->getOpcode() != PISA::gotolabel)
140 return true;
141
142 TBB = getMO(*SecondLastInst, OpName::label).getMBB();
143 FBB = LastInst->getOperand(0).getMBB();
144
145 // mod, pred
146 Cond.push_back(SecondLastInst->getOperand(0));
147 Cond.push_back(SecondLastInst->getOperand(1));
148
149 return false;
150}
151
152// See description in TargetInstrInfo.h
154 int *BytesRemoved) const {
155 assert(!BytesRemoved && "not supported!");
156
157 unsigned Count = 0;
158 for (MachineInstr &MI : llvm::make_early_inc_range(MBB.terminators())) {
159 assert(MI.isBranch() && "not a branch?");
160 MI.eraseFromParent();
161 Count++;
162 }
163
164 return Count;
165}
166
167// See description in TargetInstrInfo.h
170 ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const {
171
172 assert(!BytesAdded && "not supported!");
173 assert(TBB && "Should have at least one branch!");
174
175 if (!FBB) {
176 if (Cond.empty()) { // Unconditional branch
177 BuildMI(&MBB, DL, get(PISA::gotolabel)).addMBB(TBB);
178 } else { // Conditional branch
179 assert(Cond.size() == 2 && "wrong number of args?");
180 BuildMI(&MBB, DL, get(PISA::predgoto))
181 .add(Cond[0])
182 .add(Cond[1])
183 .addMBB(TBB);
184 }
185 return 1;
186 }
187
188 // Two-way Conditional Branch.
189 assert(Cond.size() == 2 && "wrong number of args?");
190 BuildMI(&MBB, DL, get(PISA::predgoto)).add(Cond[0]).add(Cond[1]).addMBB(TBB);
191 BuildMI(&MBB, DL, get(PISA::gotolabel)).addMBB(FBB);
192 return 2;
193}
194
197 if (Cond.empty())
198 return true;
199
200 uint64_t DoNegate = Cond[0].getImm();
201 Cond[0].setImm(!DoNegate);
202
203 return false;
204}
205
206// We have to override this because LowerCopy() in ExpandPostRAPseudos
207// gets rid of identity copies without checking subregs. It doesn't check
208// subregs because register allocation has already happened (but not for PISA)
209// so there aren't any subregs.
211 if (!MI.isCopy())
212 return false;
213
214 MachineOperand &DstMO = MI.getOperand(0);
215 MachineOperand &SrcMO = MI.getOperand(1);
216
217 // only generate instructions using defined registers
218 if (!SrcMO.isUndef())
219 copyPhysReg(*MI.getParent(), MI, MI.getDebugLoc(), DstMO.getReg(),
220 SrcMO.getReg(), SrcMO.isKill());
221
222 MI.eraseFromParent();
223
224 return true;
225}
226
228 const MachineBasicBlock *MBB,
229 const MachineFunction &MF) const {
230 // Convergent instructions must not be moved across basic blocks because
231 // doing so can change the set of threads executing the instruction,
232 // potentially producing incorrect results.
233 if (MI.isConvergent())
234 return false;
235 return true;
236}
237
240 const DebugLoc &DL, Register DestReg,
241 Register SrcReg, bool KillSrc,
242 bool RenamableDest, bool RenamableSrc) const {
243
244 assert(I->isCopy() && "Copy instruction is expected");
245 MachineRegisterInfo &MRI = I->getMF()->getRegInfo();
246
247 MachineOperand &DstOp = I->getOperand(0);
248 MachineOperand &SrcOp = I->getOperand(1);
249 const TargetRegisterClass *DstSubRC = getSubRegClass(RI, MRI, DstOp);
250 const TargetRegisterClass *SrcSubRC = getSubRegClass(RI, MRI, SrcOp);
251
252 unsigned Op = 0;
253
254 const unsigned DstSubNumElts = RI.getNumEltsFromRegClass(DstSubRC);
255 const unsigned DstSubEltSize = RI.getBitSizeFromRegClass(DstSubRC);
256 const unsigned SrcSubNumElts = RI.getNumEltsFromRegClass(SrcSubRC);
257 const unsigned SrcSubEltSize = RI.getBitSizeFromRegClass(SrcSubRC);
258 const bool DstSubIsVector = DstSubNumElts > 1;
259 const bool SrcSubIsVector = SrcSubNumElts > 1;
260 const bool DstSubIsScalar = !DstSubIsVector;
261 const bool SrcSubIsScalar = !SrcSubIsVector;
262
263 if (DstSubIsVector && SrcSubIsVector) {
264 if (DstSubNumElts == 4 && DstSubEltSize == 8 && SrcSubNumElts == 2 &&
265 SrcSubEltSize == 16)
266 Op = PISA::mov_v4i8_v2i16_r;
267 else if (DstSubNumElts == 2 && DstSubEltSize == 16 && SrcSubNumElts == 4 &&
268 SrcSubEltSize == 8)
269 Op = PISA::mov_v2i16_v4i8_r;
270 else if (DstSubNumElts == 4 && DstSubEltSize == 16 && SrcSubNumElts == 2 &&
271 SrcSubEltSize == 32)
272 Op = PISA::mov_v4i16_v2i32_r;
273 else if (DstSubNumElts == 2 && DstSubEltSize == 32 && SrcSubNumElts == 4 &&
274 SrcSubEltSize == 16)
275 Op = PISA::mov_v2i32_v4i16_r;
276 else if (DstSubNumElts == 4 && DstSubEltSize == 32 && SrcSubNumElts == 2 &&
277 SrcSubEltSize == 64)
278 Op = PISA::mov_v4i32_v2i64_r;
279 else if (DstSubNumElts == 2 && DstSubEltSize == 64 && SrcSubNumElts == 4 &&
280 SrcSubEltSize == 32)
281 Op = PISA::mov_v2i64_v4i32_r;
282 else {
283 assert(DstSubNumElts == SrcSubNumElts && "num elts mismatch!");
284 if (DstSubNumElts == 2) {
285 switch (DstSubEltSize) {
286 case 8:
287 Op = PISA::mov_v2i8_r;
288 break;
289 case 16:
290 Op = PISA::mov_v2i16_r;
291 break;
292 case 32:
293 Op = PISA::mov_v2i32_r;
294 break;
295 case 64:
296 Op = PISA::mov_v2i64_r;
297 break;
298 default:
299 llvm_unreachable("unknown elt size!");
300 }
301 } else if (DstSubNumElts == 3) {
302 switch (DstSubEltSize) {
303 case 8:
304 Op = PISA::mov_v3i8_r;
305 break;
306 case 16:
307 Op = PISA::mov_v3i16_r;
308 break;
309 default:
310 llvm_unreachable("unknown elt size!");
311 }
312 } else if (DstSubNumElts == 4) {
313 switch (DstSubEltSize) {
314 case 8:
315 Op = PISA::mov_v4i8_r;
316 break;
317 case 16:
318 Op = PISA::mov_v4i16_r;
319 break;
320 case 32:
321 Op = PISA::mov_v4i32_r;
322 break;
323 default:
324 llvm_unreachable("unknown elt size!");
325 }
326 } else {
327 llvm_unreachable("unknown vec size!");
328 }
329 }
330 } else if (DstSubIsVector && SrcSubIsScalar) {
331 if (DstSubNumElts == 2 && DstSubEltSize == 8 && SrcSubEltSize == 16)
332 Op = PISA::mov_v2i8_i16_r;
333 else if (DstSubNumElts == 4 && DstSubEltSize == 8 && SrcSubEltSize == 32)
334 Op = PISA::mov_v4i8_i32_r;
335 else if (DstSubNumElts == 2 && DstSubEltSize == 16 && SrcSubEltSize == 32)
336 Op = PISA::mov_v2i16_i32_r;
337 else if (DstSubNumElts == 4 && DstSubEltSize == 16 && SrcSubEltSize == 64)
338 Op = PISA::mov_v4i16_i64_r;
339 else if (DstSubNumElts == 2 && DstSubEltSize == 32 && SrcSubEltSize == 64)
340 Op = PISA::mov_v2i32_i64_r;
341 else if (DstSubNumElts == 4 && DstSubEltSize == 32 && SrcSubEltSize == 128)
342 Op = PISA::mov_v4i32_i128_r;
343 else if (DstSubNumElts == 2 && DstSubEltSize == 64 && SrcSubEltSize == 128)
344 Op = PISA::mov_v2i64_i128_r;
345 else
346 llvm_unreachable("wrong copy instruction");
347 } else if (DstSubIsScalar && SrcSubIsVector) {
348 if (DstSubEltSize == 16 && SrcSubNumElts == 2 && SrcSubEltSize == 8)
349 Op = PISA::mov_i16_v2i8_r;
350 else if (DstSubEltSize == 32 && SrcSubNumElts == 2 && SrcSubEltSize == 16)
351 Op = PISA::mov_i32_v2i16_r;
352 else if (DstSubEltSize == 32 && SrcSubNumElts == 4 && SrcSubEltSize == 8)
353 Op = PISA::mov_i32_v4i8_r;
354 else if (DstSubEltSize == 64 && SrcSubNumElts == 2 && SrcSubEltSize == 32)
355 Op = PISA::mov_i64_v2i32_r;
356 else if (DstSubEltSize == 64 && SrcSubNumElts == 4 && SrcSubEltSize == 16)
357 Op = PISA::mov_i64_v4i16_r;
358 else if (DstSubEltSize == 128 && SrcSubNumElts == 2 && SrcSubEltSize == 64)
359 Op = PISA::mov_i128_v2i64_r;
360 else if (DstSubEltSize == 128 && SrcSubNumElts == 4 && SrcSubEltSize == 32)
361 Op = PISA::mov_i128_v4i32_r;
362 else
363 llvm_unreachable("wrong copy operation");
364 } else if (DstSubIsScalar && SrcSubIsScalar) {
365 const PISARegisterInfo *TRI = static_cast<const PISARegisterInfo *>(
366 I->getMF()->getSubtarget().getRegisterInfo());
367 LLT I16 = LLT::integer(16);
368
369 if ((DstSubEltSize == 1) && (SrcSubEltSize == 1)) {
370 // sel.16b %tmp, 1, 0, %p_in
371 // ucmp.ne.16b %p_out, %tmp, 0
372 Register TmpReg = MRI.createGenericVirtualRegister(I16);
373 MRI.setRegClass(TmpReg, TRI->getRegClassFromLLT(I16));
374 Op = PISA::sel_16_iip;
375 BuildMI(MBB, I, DL, get(Op))
376 .addDef(TmpReg)
378 .addImm(1)
379 .addImm(0);
380 Op = PISA::ucmp_ne_16b_pri;
381 BuildMI(MBB, I, DL, get(Op))
383 .addReg(TmpReg)
384 .addImm(0);
385 return;
386 }
387 if (DstSubEltSize == 1) {
388 // ucmp.ne.??b %p, %src, 0
389 Register TmpReg = SrcOp.getReg();
390 if (SrcSubEltSize == 8) {
391 TmpReg = MRI.createGenericVirtualRegister(I16);
392 MRI.setRegClass(TmpReg, TRI->getRegClassFromLLT(I16));
393 Op = PISA::zext_16b_8b_r;
394 BuildMI(MBB, I, DL, get(Op)).addDef(TmpReg).addReg(SrcOp.getReg());
395 }
396 switch (SrcSubEltSize) {
397 default:
398 llvm_unreachable("unsupported source size!");
399 case 8:
400 case 16:
401 Op = PISA::ucmp_ne_16b_pri;
402 break;
403 case 32:
404 Op = PISA::ucmp_ne_32b_pri;
405 break;
406 case 64:
407 Op = PISA::ucmp_ne_64b_pri;
408 break;
409 }
410 BuildMI(MBB, I, DL, get(Op))
412 .addReg(TmpReg)
413 .addImm(0);
414 return;
415 }
416 if (SrcSubEltSize == 1) {
417 // sel.??b %dst, 1, 0, %p
418 Register TmpReg = DstOp.getReg();
419 if (DstSubEltSize == 8) {
420 TmpReg = MRI.createGenericVirtualRegister(I16);
421 MRI.setRegClass(TmpReg, TRI->getRegClassFromLLT(I16));
422 }
423 switch (DstSubEltSize) {
424 default:
425 llvm_unreachable("unsupported destination size!");
426 case 8:
427 case 16:
428 Op = PISA::sel_16_iip;
429 break;
430 case 32:
431 Op = PISA::sel_32_iip;
432 break;
433 case 64:
434 Op = PISA::sel_64_iip;
435 break;
436 }
437 BuildMI(MBB, I, DL, get(Op))
438 .addDef(TmpReg)
440 .addImm(1)
441 .addImm(0);
442 if (DstSubEltSize == 8) {
443 Op = PISA::trunc_8b_16b_r;
444 BuildMI(MBB, I, DL, get(Op)).addDef(DstOp.getReg()).addReg(TmpReg);
445 }
446 return;
447 }
448 switch (DstSubEltSize) {
449 case 8:
450 Op = PISA::mov_i8_r;
451 break;
452 case 16:
453 Op = PISA::mov_i16_r;
454 break;
455 case 32:
456 Op = PISA::mov_i32_r;
457 break;
458 case 64:
459 Op = PISA::mov_i64_r;
460 break;
461 default:
462 llvm_unreachable("unknown elt size!");
463 }
464 }
465
466 unsigned DstSubreg = DstOp.getSubReg();
467 unsigned SrcSubreg = SrcOp.getSubReg();
468
469 RegState Flags = DstOp.isUndef() ? RegState::Undef : RegState::NoFlags;
470 BuildMI(MBB, I, DL, get(Op))
471 .addDef(DstOp.getReg(), Flags, DstSubreg)
472 .addReg(SrcOp.getReg(), getKillRegState(KillSrc), SrcSubreg);
473}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the declarations for the subclasses of Constant, which represent the different fla...
IRTranslator LLVM IR MI
SDValue SrcReg
SmallVector< MachineOperand, 4 > Cond
#define I(x, y, z)
Definition MD5.cpp:57
This file declares the MachineIRBuilder class.
Register const TargetRegisterInfo * TRI
This file declares instruction information for the PISA target.
This file declares the PISA subtarget.
This file declares the top-level interfaces for the PISA target.
MachineRegisterInfo & MRI
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
This file defines the SmallVector class.
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
Register getReg() const
static LLT integer(unsigned SizeInBits)
MachineInstrBundleIterator< MachineInstr > iterator
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
bool isConditionalBranch(QueryType Type=AnyInBundle) const
Return true if this is a branch which may fall through to the next instruction or may transfer contro...
bool isUnconditionalBranch(QueryType Type=AnyInBundle) const
Return true if this is a branch which always transfers control flow to some other block.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
MachineBasicBlock * getMBB() const
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
unsigned removeBranch(MachineBasicBlock &MBB, int *BytesRemoved=nullptr) const override
bool isSafeToMove(const MachineInstr &MI, const MachineBasicBlock *MBB, const MachineFunction &MF) const override
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify=false) const override
unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, ArrayRef< MachineOperand > Cond, const DebugLoc &DL, int *BytesAdded=nullptr) const override
PISAInstrInfo(const PISASubtarget &STI)
bool isNoEmissionInstr(const MachineInstr &MI) const
bool expandPostRAPseudo(MachineInstr &MI) const override
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 reverseBranchCondition(SmallVectorImpl< MachineOperand > &Cond) const override
bool isFunctionParamInstr(const MachineInstr &MI) const
const PISARegisterInfo & getRegisterInfo() const
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...
Register getReg() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static const MachineOperand & getMO(const MachineInstr &MI, PISA::OpName Name)
static const TargetRegisterClass * getSubRegClass(const PISARegisterInfo &RI, MachineRegisterInfo &MRI, MachineOperand &MO)
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.
RegState
Flags to represent properties of register accesses.
@ Undef
Value of the register doesn't matter.
@ NoFlags
No Specific Flags.
constexpr RegState getKillRegState(bool B)
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:649
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
DWARFExpression::Operation Op
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58