Thanks to visit codestin.com
Credit goes to llvm.org

LLVM 22.0.0git
SIShrinkInstructions.cpp
Go to the documentation of this file.
1//===-- SIShrinkInstructions.cpp - Shrink Instructions --------------------===//
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/// The pass tries to use the 32-bit encoding for instructions when possible.
8//===----------------------------------------------------------------------===//
9//
10
12#include "AMDGPU.h"
13#include "GCNSubtarget.h"
16#include "llvm/ADT/Statistic.h"
18
19#define DEBUG_TYPE "si-shrink-instructions"
20
21STATISTIC(NumInstructionsShrunk,
22 "Number of 64-bit instruction reduced to 32-bit.");
23STATISTIC(NumLiteralConstantsFolded,
24 "Number of literal constants folded into 32-bit instructions.");
25
26using namespace llvm;
27
28namespace {
29
30class SIShrinkInstructions {
33 const GCNSubtarget *ST;
34 const SIInstrInfo *TII;
35 const SIRegisterInfo *TRI;
36 bool IsPostRA;
37
38 bool foldImmediates(MachineInstr &MI, bool TryToCommute = true) const;
39 bool shouldShrinkTrue16(MachineInstr &MI) const;
40 bool isKImmOperand(const MachineOperand &Src) const;
41 bool isKUImmOperand(const MachineOperand &Src) const;
42 bool isKImmOrKUImmOperand(const MachineOperand &Src, bool &IsUnsigned) const;
43 void copyExtraImplicitOps(MachineInstr &NewMI, MachineInstr &MI) const;
44 void shrinkScalarCompare(MachineInstr &MI) const;
45 void shrinkMIMG(MachineInstr &MI) const;
46 void shrinkMadFma(MachineInstr &MI) const;
47 bool shrinkScalarLogicOp(MachineInstr &MI) const;
48 bool tryReplaceDeadSDST(MachineInstr &MI) const;
50 Register Reg, unsigned SubReg) const;
51 bool instReadsReg(const MachineInstr *MI, unsigned Reg,
52 unsigned SubReg) const;
53 bool instModifiesReg(const MachineInstr *MI, unsigned Reg,
54 unsigned SubReg) const;
55 TargetInstrInfo::RegSubRegPair getSubRegForIndex(Register Reg, unsigned Sub,
56 unsigned I) const;
57 void dropInstructionKeepingImpDefs(MachineInstr &MI) const;
58 MachineInstr *matchSwap(MachineInstr &MovT) const;
59
60public:
61 SIShrinkInstructions() = default;
62 bool run(MachineFunction &MF);
63};
64
65class SIShrinkInstructionsLegacy : public MachineFunctionPass {
66
67public:
68 static char ID;
69
70 SIShrinkInstructionsLegacy() : MachineFunctionPass(ID) {}
71
72 bool runOnMachineFunction(MachineFunction &MF) override;
73
74 StringRef getPassName() const override { return "SI Shrink Instructions"; }
75
76 void getAnalysisUsage(AnalysisUsage &AU) const override {
77 AU.setPreservesCFG();
79 }
80};
81
82} // End anonymous namespace.
83
84INITIALIZE_PASS(SIShrinkInstructionsLegacy, DEBUG_TYPE,
85 "SI Shrink Instructions", false, false)
86
87char SIShrinkInstructionsLegacy::ID = 0;
88
90 return new SIShrinkInstructionsLegacy();
91}
92
93/// This function checks \p MI for operands defined by a move immediate
94/// instruction and then folds the literal constant into the instruction if it
95/// can. This function assumes that \p MI is a VOP1, VOP2, or VOPC instructions.
96bool SIShrinkInstructions::foldImmediates(MachineInstr &MI,
97 bool TryToCommute) const {
98 assert(TII->isVOP1(MI) || TII->isVOP2(MI) || TII->isVOPC(MI));
99
100 int Src0Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src0);
101
102 // Try to fold Src0
103 MachineOperand &Src0 = MI.getOperand(Src0Idx);
104 if (Src0.isReg()) {
105 Register Reg = Src0.getReg();
106 if (Reg.isVirtual()) {
107 MachineInstr *Def = MRI->getUniqueVRegDef(Reg);
108 if (Def && Def->isMoveImmediate()) {
109 MachineOperand &MovSrc = Def->getOperand(1);
110 bool ConstantFolded = false;
111
112 if (TII->isOperandLegal(MI, Src0Idx, &MovSrc)) {
113 if (MovSrc.isImm()) {
114 Src0.ChangeToImmediate(MovSrc.getImm());
115 ConstantFolded = true;
116 } else if (MovSrc.isFI()) {
117 Src0.ChangeToFrameIndex(MovSrc.getIndex());
118 ConstantFolded = true;
119 } else if (MovSrc.isGlobal()) {
120 Src0.ChangeToGA(MovSrc.getGlobal(), MovSrc.getOffset(),
121 MovSrc.getTargetFlags());
122 ConstantFolded = true;
123 }
124 }
125
126 if (ConstantFolded) {
127 if (MRI->use_nodbg_empty(Reg))
128 Def->eraseFromParent();
129 ++NumLiteralConstantsFolded;
130 return true;
131 }
132 }
133 }
134 }
135
136 // We have failed to fold src0, so commute the instruction and try again.
137 if (TryToCommute && MI.isCommutable()) {
138 if (TII->commuteInstruction(MI)) {
139 if (foldImmediates(MI, false))
140 return true;
141
142 // Commute back.
143 TII->commuteInstruction(MI);
144 }
145 }
146
147 return false;
148}
149
150/// Do not shrink the instruction if its registers are not expressible in the
151/// shrunk encoding.
152bool SIShrinkInstructions::shouldShrinkTrue16(MachineInstr &MI) const {
153 for (unsigned I = 0, E = MI.getNumExplicitOperands(); I != E; ++I) {
154 const MachineOperand &MO = MI.getOperand(I);
155 if (MO.isReg()) {
156 Register Reg = MO.getReg();
157 assert(!Reg.isVirtual() && "Prior checks should ensure we only shrink "
158 "True16 Instructions post-RA");
159 if (AMDGPU::VGPR_32RegClass.contains(Reg) &&
160 !AMDGPU::VGPR_32_Lo128RegClass.contains(Reg))
161 return false;
162
163 if (AMDGPU::VGPR_16RegClass.contains(Reg) &&
164 !AMDGPU::VGPR_16_Lo128RegClass.contains(Reg))
165 return false;
166 }
167 }
168 return true;
169}
170
171bool SIShrinkInstructions::isKImmOperand(const MachineOperand &Src) const {
172 return isInt<16>(SignExtend64(Src.getImm(), 32)) &&
173 !TII->isInlineConstant(*Src.getParent(), Src.getOperandNo());
174}
175
176bool SIShrinkInstructions::isKUImmOperand(const MachineOperand &Src) const {
177 return isUInt<16>(Src.getImm()) &&
178 !TII->isInlineConstant(*Src.getParent(), Src.getOperandNo());
179}
180
181bool SIShrinkInstructions::isKImmOrKUImmOperand(const MachineOperand &Src,
182 bool &IsUnsigned) const {
183 if (isInt<16>(SignExtend64(Src.getImm(), 32))) {
184 IsUnsigned = false;
185 return !TII->isInlineConstant(Src);
186 }
187
188 if (isUInt<16>(Src.getImm())) {
189 IsUnsigned = true;
190 return !TII->isInlineConstant(Src);
191 }
192
193 return false;
194}
195
196/// \returns the opcode of an instruction a move immediate of the constant \p
197/// Src can be replaced with if the constant is replaced with \p ModifiedImm.
198/// i.e.
199///
200/// If the bitreverse of a constant is an inline immediate, reverse the
201/// immediate and return the bitreverse opcode.
202///
203/// If the bitwise negation of a constant is an inline immediate, reverse the
204/// immediate and return the bitwise not opcode.
206 const MachineOperand &Src,
207 int32_t &ModifiedImm, bool Scalar) {
208 if (TII->isInlineConstant(Src))
209 return 0;
210 int32_t SrcImm = static_cast<int32_t>(Src.getImm());
211
212 if (!Scalar) {
213 // We could handle the scalar case with here, but we would need to check
214 // that SCC is not live as S_NOT_B32 clobbers it. It's probably not worth
215 // it, as the reasonable values are already covered by s_movk_i32.
216 ModifiedImm = ~SrcImm;
217 if (TII->isInlineConstant(APInt(32, ModifiedImm, true)))
218 return AMDGPU::V_NOT_B32_e32;
219 }
220
221 ModifiedImm = reverseBits<int32_t>(SrcImm);
222 if (TII->isInlineConstant(APInt(32, ModifiedImm, true)))
223 return Scalar ? AMDGPU::S_BREV_B32 : AMDGPU::V_BFREV_B32_e32;
224
225 return 0;
226}
227
228/// Copy implicit register operands from specified instruction to this
229/// instruction that are not part of the instruction definition.
230void SIShrinkInstructions::copyExtraImplicitOps(MachineInstr &NewMI,
231 MachineInstr &MI) const {
232 MachineFunction &MF = *MI.getMF();
233 for (unsigned i = MI.getDesc().getNumOperands() +
234 MI.getDesc().implicit_uses().size() +
235 MI.getDesc().implicit_defs().size(),
236 e = MI.getNumOperands();
237 i != e; ++i) {
238 const MachineOperand &MO = MI.getOperand(i);
239 if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask())
240 NewMI.addOperand(MF, MO);
241 }
242}
243
244void SIShrinkInstructions::shrinkScalarCompare(MachineInstr &MI) const {
245 if (!ST->hasSCmpK())
246 return;
247
248 // cmpk instructions do scc = dst <cc op> imm16, so commute the instruction to
249 // get constants on the RHS.
250 if (!MI.getOperand(0).isReg())
251 TII->commuteInstruction(MI, false, 0, 1);
252
253 // cmpk requires src0 to be a register
254 const MachineOperand &Src0 = MI.getOperand(0);
255 if (!Src0.isReg())
256 return;
257
258 MachineOperand &Src1 = MI.getOperand(1);
259 if (!Src1.isImm())
260 return;
261
262 int SOPKOpc = AMDGPU::getSOPKOp(MI.getOpcode());
263 if (SOPKOpc == -1)
264 return;
265
266 // eq/ne is special because the imm16 can be treated as signed or unsigned,
267 // and initially selected to the unsigned versions.
268 if (SOPKOpc == AMDGPU::S_CMPK_EQ_U32 || SOPKOpc == AMDGPU::S_CMPK_LG_U32) {
269 bool HasUImm;
270 if (isKImmOrKUImmOperand(Src1, HasUImm)) {
271 if (!HasUImm) {
272 SOPKOpc = (SOPKOpc == AMDGPU::S_CMPK_EQ_U32) ?
273 AMDGPU::S_CMPK_EQ_I32 : AMDGPU::S_CMPK_LG_I32;
274 Src1.setImm(SignExtend32(Src1.getImm(), 32));
275 }
276
277 MI.setDesc(TII->get(SOPKOpc));
278 }
279
280 return;
281 }
282
283 const MCInstrDesc &NewDesc = TII->get(SOPKOpc);
284
285 if ((SIInstrInfo::sopkIsZext(SOPKOpc) && isKUImmOperand(Src1)) ||
286 (!SIInstrInfo::sopkIsZext(SOPKOpc) && isKImmOperand(Src1))) {
287 if (!SIInstrInfo::sopkIsZext(SOPKOpc))
288 Src1.setImm(SignExtend64(Src1.getImm(), 32));
289 MI.setDesc(NewDesc);
290 }
291}
292
293// Shrink NSA encoded instructions with contiguous VGPRs to non-NSA encoding.
294void SIShrinkInstructions::shrinkMIMG(MachineInstr &MI) const {
295 const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(MI.getOpcode());
296 if (!Info)
297 return;
298
299 uint8_t NewEncoding;
300 switch (Info->MIMGEncoding) {
301 case AMDGPU::MIMGEncGfx10NSA:
302 NewEncoding = AMDGPU::MIMGEncGfx10Default;
303 break;
304 case AMDGPU::MIMGEncGfx11NSA:
305 NewEncoding = AMDGPU::MIMGEncGfx11Default;
306 break;
307 default:
308 return;
309 }
310
311 int VAddr0Idx =
312 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vaddr0);
313 unsigned NewAddrDwords = Info->VAddrDwords;
314 const TargetRegisterClass *RC;
315
316 if (Info->VAddrDwords == 2) {
317 RC = &AMDGPU::VReg_64RegClass;
318 } else if (Info->VAddrDwords == 3) {
319 RC = &AMDGPU::VReg_96RegClass;
320 } else if (Info->VAddrDwords == 4) {
321 RC = &AMDGPU::VReg_128RegClass;
322 } else if (Info->VAddrDwords == 5) {
323 RC = &AMDGPU::VReg_160RegClass;
324 } else if (Info->VAddrDwords == 6) {
325 RC = &AMDGPU::VReg_192RegClass;
326 } else if (Info->VAddrDwords == 7) {
327 RC = &AMDGPU::VReg_224RegClass;
328 } else if (Info->VAddrDwords == 8) {
329 RC = &AMDGPU::VReg_256RegClass;
330 } else if (Info->VAddrDwords == 9) {
331 RC = &AMDGPU::VReg_288RegClass;
332 } else if (Info->VAddrDwords == 10) {
333 RC = &AMDGPU::VReg_320RegClass;
334 } else if (Info->VAddrDwords == 11) {
335 RC = &AMDGPU::VReg_352RegClass;
336 } else if (Info->VAddrDwords == 12) {
337 RC = &AMDGPU::VReg_384RegClass;
338 } else {
339 RC = &AMDGPU::VReg_512RegClass;
340 NewAddrDwords = 16;
341 }
342
343 unsigned VgprBase = 0;
344 unsigned NextVgpr = 0;
345 bool IsUndef = true;
346 bool IsKill = NewAddrDwords == Info->VAddrDwords;
347 const unsigned NSAMaxSize = ST->getNSAMaxSize();
348 const bool IsPartialNSA = NewAddrDwords > NSAMaxSize;
349 const unsigned EndVAddr = IsPartialNSA ? NSAMaxSize : Info->VAddrOperands;
350 for (unsigned Idx = 0; Idx < EndVAddr; ++Idx) {
351 const MachineOperand &Op = MI.getOperand(VAddr0Idx + Idx);
352 unsigned Vgpr = TRI->getHWRegIndex(Op.getReg());
353 unsigned Dwords = TRI->getRegSizeInBits(Op.getReg(), *MRI) / 32;
354 assert(Dwords > 0 && "Un-implemented for less than 32 bit regs");
355
356 if (Idx == 0) {
357 VgprBase = Vgpr;
358 NextVgpr = Vgpr + Dwords;
359 } else if (Vgpr == NextVgpr) {
360 NextVgpr = Vgpr + Dwords;
361 } else {
362 return;
363 }
364
365 if (!Op.isUndef())
366 IsUndef = false;
367 if (!Op.isKill())
368 IsKill = false;
369 }
370
371 if (VgprBase + NewAddrDwords > 256)
372 return;
373
374 // Further check for implicit tied operands - this may be present if TFE is
375 // enabled
376 int TFEIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::tfe);
377 int LWEIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::lwe);
378 unsigned TFEVal = (TFEIdx == -1) ? 0 : MI.getOperand(TFEIdx).getImm();
379 unsigned LWEVal = (LWEIdx == -1) ? 0 : MI.getOperand(LWEIdx).getImm();
380 int ToUntie = -1;
381 if (TFEVal || LWEVal) {
382 // TFE/LWE is enabled so we need to deal with an implicit tied operand
383 for (unsigned i = LWEIdx + 1, e = MI.getNumOperands(); i != e; ++i) {
384 if (MI.getOperand(i).isReg() && MI.getOperand(i).isTied() &&
385 MI.getOperand(i).isImplicit()) {
386 // This is the tied operand
387 assert(
388 ToUntie == -1 &&
389 "found more than one tied implicit operand when expecting only 1");
390 ToUntie = i;
391 MI.untieRegOperand(ToUntie);
392 }
393 }
394 }
395
396 unsigned NewOpcode = AMDGPU::getMIMGOpcode(Info->BaseOpcode, NewEncoding,
397 Info->VDataDwords, NewAddrDwords);
398 MI.setDesc(TII->get(NewOpcode));
399 MI.getOperand(VAddr0Idx).setReg(RC->getRegister(VgprBase));
400 MI.getOperand(VAddr0Idx).setIsUndef(IsUndef);
401 MI.getOperand(VAddr0Idx).setIsKill(IsKill);
402
403 for (unsigned i = 1; i < EndVAddr; ++i)
404 MI.removeOperand(VAddr0Idx + 1);
405
406 if (ToUntie >= 0) {
407 MI.tieOperands(
408 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdata),
409 ToUntie - (EndVAddr - 1));
410 }
411}
412
413// Shrink MAD to MADAK/MADMK and FMA to FMAAK/FMAMK.
414void SIShrinkInstructions::shrinkMadFma(MachineInstr &MI) const {
415 // Pre-GFX10 VOP3 instructions like MAD/FMA cannot take a literal operand so
416 // there is no reason to try to shrink them.
417 if (!ST->hasVOP3Literal())
418 return;
419
420 // There is no advantage to doing this pre-RA.
421 if (!IsPostRA)
422 return;
423
424 if (TII->hasAnyModifiersSet(MI))
425 return;
426
427 const unsigned Opcode = MI.getOpcode();
428 MachineOperand &Src0 = *TII->getNamedOperand(MI, AMDGPU::OpName::src0);
429 MachineOperand &Src1 = *TII->getNamedOperand(MI, AMDGPU::OpName::src1);
430 MachineOperand &Src2 = *TII->getNamedOperand(MI, AMDGPU::OpName::src2);
431 unsigned NewOpcode = AMDGPU::INSTRUCTION_LIST_END;
432
433 bool Swap;
434
435 // Detect "Dst = VSrc * VGPR + Imm" and convert to AK form.
436 if (Src2.isImm() && !TII->isInlineConstant(Src2)) {
437 if (Src1.isReg() && TRI->isVGPR(*MRI, Src1.getReg()))
438 Swap = false;
439 else if (Src0.isReg() && TRI->isVGPR(*MRI, Src0.getReg()))
440 Swap = true;
441 else
442 return;
443
444 switch (Opcode) {
445 default:
446 llvm_unreachable("Unexpected mad/fma opcode!");
447 case AMDGPU::V_MAD_F32_e64:
448 NewOpcode = AMDGPU::V_MADAK_F32;
449 break;
450 case AMDGPU::V_FMA_F32_e64:
451 NewOpcode = AMDGPU::V_FMAAK_F32;
452 break;
453 case AMDGPU::V_MAD_F16_e64:
454 NewOpcode = AMDGPU::V_MADAK_F16;
455 break;
456 case AMDGPU::V_FMA_F16_e64:
457 case AMDGPU::V_FMA_F16_gfx9_e64:
458 NewOpcode = AMDGPU::V_FMAAK_F16;
459 break;
460 case AMDGPU::V_FMA_F16_gfx9_t16_e64:
461 NewOpcode = AMDGPU::V_FMAAK_F16_t16;
462 break;
463 case AMDGPU::V_FMA_F16_gfx9_fake16_e64:
464 NewOpcode = AMDGPU::V_FMAAK_F16_fake16;
465 break;
466 case AMDGPU::V_FMA_F64_e64:
467 if (ST->hasFmaakFmamkF64Insts())
468 NewOpcode = AMDGPU::V_FMAAK_F64;
469 break;
470 }
471 }
472
473 // Detect "Dst = VSrc * Imm + VGPR" and convert to MK form.
474 if (Src2.isReg() && TRI->isVGPR(*MRI, Src2.getReg())) {
475 if (Src1.isImm() && !TII->isInlineConstant(Src1))
476 Swap = false;
477 else if (Src0.isImm() && !TII->isInlineConstant(Src0))
478 Swap = true;
479 else
480 return;
481
482 switch (Opcode) {
483 default:
484 llvm_unreachable("Unexpected mad/fma opcode!");
485 case AMDGPU::V_MAD_F32_e64:
486 NewOpcode = AMDGPU::V_MADMK_F32;
487 break;
488 case AMDGPU::V_FMA_F32_e64:
489 NewOpcode = AMDGPU::V_FMAMK_F32;
490 break;
491 case AMDGPU::V_MAD_F16_e64:
492 NewOpcode = AMDGPU::V_MADMK_F16;
493 break;
494 case AMDGPU::V_FMA_F16_e64:
495 case AMDGPU::V_FMA_F16_gfx9_e64:
496 NewOpcode = AMDGPU::V_FMAMK_F16;
497 break;
498 case AMDGPU::V_FMA_F16_gfx9_t16_e64:
499 NewOpcode = AMDGPU::V_FMAMK_F16_t16;
500 break;
501 case AMDGPU::V_FMA_F16_gfx9_fake16_e64:
502 NewOpcode = AMDGPU::V_FMAMK_F16_fake16;
503 break;
504 case AMDGPU::V_FMA_F64_e64:
505 if (ST->hasFmaakFmamkF64Insts())
506 NewOpcode = AMDGPU::V_FMAMK_F64;
507 break;
508 }
509 }
510
511 if (NewOpcode == AMDGPU::INSTRUCTION_LIST_END)
512 return;
513
514 if (AMDGPU::isTrue16Inst(NewOpcode) && !shouldShrinkTrue16(MI))
515 return;
516
517 if (Swap) {
518 // Swap Src0 and Src1 by building a new instruction.
519 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(NewOpcode),
520 MI.getOperand(0).getReg())
521 .add(Src1)
522 .add(Src0)
523 .add(Src2)
524 .setMIFlags(MI.getFlags());
525 MI.eraseFromParent();
526 } else {
527 TII->removeModOperands(MI);
528 MI.setDesc(TII->get(NewOpcode));
529 }
530}
531
532/// Attempt to shrink AND/OR/XOR operations requiring non-inlineable literals.
533/// For AND or OR, try using S_BITSET{0,1} to clear or set bits.
534/// If the inverse of the immediate is legal, use ANDN2, ORN2 or
535/// XNOR (as a ^ b == ~(a ^ ~b)).
536/// \returns true if the caller should continue the machine function iterator
537bool SIShrinkInstructions::shrinkScalarLogicOp(MachineInstr &MI) const {
538 unsigned Opc = MI.getOpcode();
539 const MachineOperand *Dest = &MI.getOperand(0);
540 MachineOperand *Src0 = &MI.getOperand(1);
541 MachineOperand *Src1 = &MI.getOperand(2);
542 MachineOperand *SrcReg = Src0;
543 MachineOperand *SrcImm = Src1;
544
545 if (!SrcImm->isImm() ||
547 return false;
548
549 uint32_t Imm = static_cast<uint32_t>(SrcImm->getImm());
550 uint32_t NewImm = 0;
551
552 if (Opc == AMDGPU::S_AND_B32) {
553 if (isPowerOf2_32(~Imm)) {
554 NewImm = llvm::countr_one(Imm);
555 Opc = AMDGPU::S_BITSET0_B32;
556 } else if (AMDGPU::isInlinableLiteral32(~Imm, ST->hasInv2PiInlineImm())) {
557 NewImm = ~Imm;
558 Opc = AMDGPU::S_ANDN2_B32;
559 }
560 } else if (Opc == AMDGPU::S_OR_B32) {
561 if (isPowerOf2_32(Imm)) {
562 NewImm = llvm::countr_zero(Imm);
563 Opc = AMDGPU::S_BITSET1_B32;
564 } else if (AMDGPU::isInlinableLiteral32(~Imm, ST->hasInv2PiInlineImm())) {
565 NewImm = ~Imm;
566 Opc = AMDGPU::S_ORN2_B32;
567 }
568 } else if (Opc == AMDGPU::S_XOR_B32) {
570 NewImm = ~Imm;
571 Opc = AMDGPU::S_XNOR_B32;
572 }
573 } else {
574 llvm_unreachable("unexpected opcode");
575 }
576
577 if (NewImm != 0) {
578 if (Dest->getReg().isVirtual() && SrcReg->isReg()) {
579 MRI->setRegAllocationHint(Dest->getReg(), 0, SrcReg->getReg());
580 MRI->setRegAllocationHint(SrcReg->getReg(), 0, Dest->getReg());
581 return true;
582 }
583
584 if (SrcReg->isReg() && SrcReg->getReg() == Dest->getReg()) {
585 const bool IsUndef = SrcReg->isUndef();
586 const bool IsKill = SrcReg->isKill();
587 MI.setDesc(TII->get(Opc));
588 if (Opc == AMDGPU::S_BITSET0_B32 ||
589 Opc == AMDGPU::S_BITSET1_B32) {
590 Src0->ChangeToImmediate(NewImm);
591 // Remove the immediate and add the tied input.
592 MI.getOperand(2).ChangeToRegister(Dest->getReg(), /*IsDef*/ false,
593 /*isImp*/ false, IsKill,
594 /*isDead*/ false, IsUndef);
595 MI.tieOperands(0, 2);
596 } else {
597 SrcImm->setImm(NewImm);
598 }
599 }
600 }
601
602 return false;
603}
604
605// This is the same as MachineInstr::readsRegister/modifiesRegister except
606// it takes subregs into account.
607bool SIShrinkInstructions::instAccessReg(
609 unsigned SubReg) const {
610 for (const MachineOperand &MO : R) {
611 if (!MO.isReg())
612 continue;
613
614 if (Reg.isPhysical() && MO.getReg().isPhysical()) {
615 if (TRI->regsOverlap(Reg, MO.getReg()))
616 return true;
617 } else if (MO.getReg() == Reg && Reg.isVirtual()) {
618 LaneBitmask Overlap = TRI->getSubRegIndexLaneMask(SubReg) &
619 TRI->getSubRegIndexLaneMask(MO.getSubReg());
620 if (Overlap.any())
621 return true;
622 }
623 }
624 return false;
625}
626
627bool SIShrinkInstructions::instReadsReg(const MachineInstr *MI, unsigned Reg,
628 unsigned SubReg) const {
629 return instAccessReg(MI->uses(), Reg, SubReg);
630}
631
632bool SIShrinkInstructions::instModifiesReg(const MachineInstr *MI, unsigned Reg,
633 unsigned SubReg) const {
634 return instAccessReg(MI->defs(), Reg, SubReg);
635}
636
637TargetInstrInfo::RegSubRegPair
638SIShrinkInstructions::getSubRegForIndex(Register Reg, unsigned Sub,
639 unsigned I) const {
640 if (TRI->getRegSizeInBits(Reg, *MRI) != 32) {
641 if (Reg.isPhysical()) {
642 Reg = TRI->getSubReg(Reg, TRI->getSubRegFromChannel(I));
643 } else {
644 Sub = TRI->getSubRegFromChannel(I + TRI->getChannelFromSubReg(Sub));
645 }
646 }
647 return TargetInstrInfo::RegSubRegPair(Reg, Sub);
648}
649
650void SIShrinkInstructions::dropInstructionKeepingImpDefs(
651 MachineInstr &MI) const {
652 for (unsigned i = MI.getDesc().getNumOperands() +
653 MI.getDesc().implicit_uses().size() +
654 MI.getDesc().implicit_defs().size(),
655 e = MI.getNumOperands();
656 i != e; ++i) {
657 const MachineOperand &Op = MI.getOperand(i);
658 if (!Op.isDef())
659 continue;
660 BuildMI(*MI.getParent(), MI.getIterator(), MI.getDebugLoc(),
661 TII->get(AMDGPU::IMPLICIT_DEF), Op.getReg());
662 }
663
664 MI.eraseFromParent();
665}
666
667// Match:
668// mov t, x
669// mov x, y
670// mov y, t
671//
672// =>
673//
674// mov t, x (t is potentially dead and move eliminated)
675// v_swap_b32 x, y
676//
677// Returns next valid instruction pointer if was able to create v_swap_b32.
678//
679// This shall not be done too early not to prevent possible folding which may
680// remove matched moves, and this should preferably be done before RA to
681// release saved registers and also possibly after RA which can insert copies
682// too.
683//
684// This is really just a generic peephole that is not a canonical shrinking,
685// although requirements match the pass placement and it reduces code size too.
686MachineInstr *SIShrinkInstructions::matchSwap(MachineInstr &MovT) const {
687 assert(MovT.getOpcode() == AMDGPU::V_MOV_B32_e32 ||
688 MovT.getOpcode() == AMDGPU::V_MOV_B16_t16_e32 ||
689 MovT.getOpcode() == AMDGPU::COPY);
690
691 Register T = MovT.getOperand(0).getReg();
692 unsigned Tsub = MovT.getOperand(0).getSubReg();
693 MachineOperand &Xop = MovT.getOperand(1);
694
695 if (!Xop.isReg())
696 return nullptr;
697 Register X = Xop.getReg();
698 unsigned Xsub = Xop.getSubReg();
699
700 unsigned Size = TII->getOpSize(MovT, 0);
701
702 // We can't match v_swap_b16 pre-RA, because VGPR_16_Lo128 registers
703 // are not allocatble.
704 if (Size == 2 && X.isVirtual())
705 return nullptr;
706
707 if (!TRI->isVGPR(*MRI, X))
708 return nullptr;
709
710 const unsigned SearchLimit = 16;
711 unsigned Count = 0;
712 bool KilledT = false;
713 for (auto Iter = std::next(MovT.getIterator()),
714 E = MovT.getParent()->instr_end();
715 Iter != E && Count < SearchLimit && !KilledT; ++Iter) {
716
717 MachineInstr *MovY = &*Iter;
718 KilledT = MovY->killsRegister(T, TRI);
719 if (MovY->isDebugInstr())
720 continue;
721 ++Count;
722
723 if ((MovY->getOpcode() != AMDGPU::V_MOV_B32_e32 &&
724 MovY->getOpcode() != AMDGPU::V_MOV_B16_t16_e32 &&
725 MovY->getOpcode() != AMDGPU::COPY) ||
726 !MovY->getOperand(1).isReg() || MovY->getOperand(1).getReg() != T ||
727 MovY->getOperand(1).getSubReg() != Tsub)
728 continue;
729
730 Register Y = MovY->getOperand(0).getReg();
731 unsigned Ysub = MovY->getOperand(0).getSubReg();
732
733 if (!TRI->isVGPR(*MRI, Y))
734 continue;
735
736 MachineInstr *MovX = nullptr;
737 for (auto IY = MovY->getIterator(), I = std::next(MovT.getIterator());
738 I != IY; ++I) {
739 if (I->isDebugInstr())
740 continue;
741 if (instReadsReg(&*I, X, Xsub) || instModifiesReg(&*I, Y, Ysub) ||
742 instModifiesReg(&*I, T, Tsub) ||
743 (MovX && instModifiesReg(&*I, X, Xsub))) {
744 MovX = nullptr;
745 break;
746 }
747 if (!instReadsReg(&*I, Y, Ysub)) {
748 if (!MovX && instModifiesReg(&*I, X, Xsub)) {
749 MovX = nullptr;
750 break;
751 }
752 continue;
753 }
754 if (MovX ||
755 (I->getOpcode() != AMDGPU::V_MOV_B32_e32 &&
756 I->getOpcode() != AMDGPU::V_MOV_B16_t16_e32 &&
757 I->getOpcode() != AMDGPU::COPY) ||
758 I->getOperand(0).getReg() != X ||
759 I->getOperand(0).getSubReg() != Xsub) {
760 MovX = nullptr;
761 break;
762 }
763
764 if (Size > 4 && (I->getNumImplicitOperands() > (I->isCopy() ? 0U : 1U)))
765 continue;
766
767 MovX = &*I;
768 }
769
770 if (!MovX)
771 continue;
772
773 LLVM_DEBUG(dbgs() << "Matched v_swap:\n" << MovT << *MovX << *MovY);
774
775 MachineBasicBlock &MBB = *MovT.getParent();
776 SmallVector<MachineInstr *, 4> Swaps;
777 if (Size == 2) {
778 auto *MIB = BuildMI(MBB, MovX->getIterator(), MovT.getDebugLoc(),
779 TII->get(AMDGPU::V_SWAP_B16))
780 .addDef(X)
781 .addDef(Y)
782 .addReg(Y)
783 .addReg(X)
784 .getInstr();
785 Swaps.push_back(MIB);
786 } else {
787 assert(Size > 0 && Size % 4 == 0);
788 for (unsigned I = 0; I < Size / 4; ++I) {
789 TargetInstrInfo::RegSubRegPair X1, Y1;
790 X1 = getSubRegForIndex(X, Xsub, I);
791 Y1 = getSubRegForIndex(Y, Ysub, I);
792 auto *MIB = BuildMI(MBB, MovX->getIterator(), MovT.getDebugLoc(),
793 TII->get(AMDGPU::V_SWAP_B32))
794 .addDef(X1.Reg, 0, X1.SubReg)
795 .addDef(Y1.Reg, 0, Y1.SubReg)
796 .addReg(Y1.Reg, 0, Y1.SubReg)
797 .addReg(X1.Reg, 0, X1.SubReg)
798 .getInstr();
799 Swaps.push_back(MIB);
800 }
801 }
802 // Drop implicit EXEC.
803 if (MovX->hasRegisterImplicitUseOperand(AMDGPU::EXEC)) {
804 for (MachineInstr *Swap : Swaps) {
805 Swap->removeOperand(Swap->getNumExplicitOperands());
806 Swap->copyImplicitOps(*MBB.getParent(), *MovX);
807 }
808 }
809 MovX->eraseFromParent();
810 dropInstructionKeepingImpDefs(*MovY);
811 MachineInstr *Next = &*std::next(MovT.getIterator());
812
813 if (T.isVirtual() && MRI->use_nodbg_empty(T)) {
814 dropInstructionKeepingImpDefs(MovT);
815 } else {
816 Xop.setIsKill(false);
817 for (int I = MovT.getNumImplicitOperands() - 1; I >= 0; --I ) {
818 unsigned OpNo = MovT.getNumExplicitOperands() + I;
819 const MachineOperand &Op = MovT.getOperand(OpNo);
820 if (Op.isKill() && TRI->regsOverlap(X, Op.getReg()))
821 MovT.removeOperand(OpNo);
822 }
823 }
824
825 return Next;
826 }
827
828 return nullptr;
829}
830
831// If an instruction has dead sdst replace it with NULL register on gfx1030+
832bool SIShrinkInstructions::tryReplaceDeadSDST(MachineInstr &MI) const {
833 if (!ST->hasGFX10_3Insts())
834 return false;
835
836 MachineOperand *Op = TII->getNamedOperand(MI, AMDGPU::OpName::sdst);
837 if (!Op)
838 return false;
839 Register SDstReg = Op->getReg();
840 if (SDstReg.isPhysical() || !MRI->use_nodbg_empty(SDstReg))
841 return false;
842
843 Op->setReg(ST->isWave32() ? AMDGPU::SGPR_NULL : AMDGPU::SGPR_NULL64);
844 return true;
845}
846
847bool SIShrinkInstructions::run(MachineFunction &MF) {
848
849 this->MF = &MF;
850 MRI = &MF.getRegInfo();
851 ST = &MF.getSubtarget<GCNSubtarget>();
852 TII = ST->getInstrInfo();
853 TRI = &TII->getRegisterInfo();
854 IsPostRA = MF.getProperties().hasNoVRegs();
855
856 unsigned VCCReg = ST->isWave32() ? AMDGPU::VCC_LO : AMDGPU::VCC;
857
858 for (MachineBasicBlock &MBB : MF) {
860 for (I = MBB.begin(); I != MBB.end(); I = Next) {
861 Next = std::next(I);
862 MachineInstr &MI = *I;
863
864 if (MI.getOpcode() == AMDGPU::V_MOV_B32_e32) {
865 // If this has a literal constant source that is the same as the
866 // reversed bits of an inline immediate, replace with a bitreverse of
867 // that constant. This saves 4 bytes in the common case of materializing
868 // sign bits.
869
870 // Test if we are after regalloc. We only want to do this after any
871 // optimizations happen because this will confuse them.
872 MachineOperand &Src = MI.getOperand(1);
873 if (Src.isImm() && IsPostRA) {
874 int32_t ModImm;
875 unsigned ModOpcode =
876 canModifyToInlineImmOp32(TII, Src, ModImm, /*Scalar=*/false);
877 if (ModOpcode != 0) {
878 MI.setDesc(TII->get(ModOpcode));
879 Src.setImm(static_cast<int64_t>(ModImm));
880 continue;
881 }
882 }
883 }
884
885 if (ST->hasSwap() && (MI.getOpcode() == AMDGPU::V_MOV_B32_e32 ||
886 MI.getOpcode() == AMDGPU::V_MOV_B16_t16_e32 ||
887 MI.getOpcode() == AMDGPU::COPY)) {
888 if (auto *NextMI = matchSwap(MI)) {
889 Next = NextMI->getIterator();
890 continue;
891 }
892 }
893
894 // Try to use S_ADDK_I32 and S_MULK_I32.
895 if (MI.getOpcode() == AMDGPU::S_ADD_I32 ||
896 MI.getOpcode() == AMDGPU::S_MUL_I32) {
897 const MachineOperand *Dest = &MI.getOperand(0);
898 MachineOperand *Src0 = &MI.getOperand(1);
899 MachineOperand *Src1 = &MI.getOperand(2);
900
901 if (!Src0->isReg() && Src1->isReg()) {
902 if (TII->commuteInstruction(MI, false, 1, 2))
903 std::swap(Src0, Src1);
904 }
905
906 // FIXME: This could work better if hints worked with subregisters. If
907 // we have a vector add of a constant, we usually don't get the correct
908 // allocation due to the subregister usage.
909 if (Dest->getReg().isVirtual() && Src0->isReg()) {
910 MRI->setRegAllocationHint(Dest->getReg(), 0, Src0->getReg());
911 MRI->setRegAllocationHint(Src0->getReg(), 0, Dest->getReg());
912 continue;
913 }
914
915 if (Src0->isReg() && Src0->getReg() == Dest->getReg()) {
916 if (Src1->isImm() && isKImmOperand(*Src1)) {
917 unsigned Opc = (MI.getOpcode() == AMDGPU::S_ADD_I32) ?
918 AMDGPU::S_ADDK_I32 : AMDGPU::S_MULK_I32;
919
920 Src1->setImm(SignExtend64(Src1->getImm(), 32));
921 MI.setDesc(TII->get(Opc));
922 MI.tieOperands(0, 1);
923 }
924 }
925 }
926
927 // Try to use s_cmpk_*
928 if (MI.isCompare() && TII->isSOPC(MI)) {
929 shrinkScalarCompare(MI);
930 continue;
931 }
932
933 // Try to use S_MOVK_I32, which will save 4 bytes for small immediates.
934 if (MI.getOpcode() == AMDGPU::S_MOV_B32) {
935 const MachineOperand &Dst = MI.getOperand(0);
936 MachineOperand &Src = MI.getOperand(1);
937
938 if (Src.isImm() && Dst.getReg().isPhysical()) {
939 unsigned ModOpc;
940 int32_t ModImm;
941 if (isKImmOperand(Src)) {
942 MI.setDesc(TII->get(AMDGPU::S_MOVK_I32));
943 Src.setImm(SignExtend64(Src.getImm(), 32));
944 } else if ((ModOpc = canModifyToInlineImmOp32(TII, Src, ModImm,
945 /*Scalar=*/true))) {
946 MI.setDesc(TII->get(ModOpc));
947 Src.setImm(static_cast<int64_t>(ModImm));
948 }
949 }
950
951 continue;
952 }
953
954 // Shrink scalar logic operations.
955 if (MI.getOpcode() == AMDGPU::S_AND_B32 ||
956 MI.getOpcode() == AMDGPU::S_OR_B32 ||
957 MI.getOpcode() == AMDGPU::S_XOR_B32) {
958 if (shrinkScalarLogicOp(MI))
959 continue;
960 }
961
962 if (IsPostRA && TII->isMIMG(MI.getOpcode()) &&
963 ST->getGeneration() >= AMDGPUSubtarget::GFX10) {
964 shrinkMIMG(MI);
965 continue;
966 }
967
968 if (!TII->isVOP3(MI))
969 continue;
970
971 if (MI.getOpcode() == AMDGPU::V_MAD_F32_e64 ||
972 MI.getOpcode() == AMDGPU::V_FMA_F32_e64 ||
973 MI.getOpcode() == AMDGPU::V_MAD_F16_e64 ||
974 MI.getOpcode() == AMDGPU::V_FMA_F16_e64 ||
975 MI.getOpcode() == AMDGPU::V_FMA_F16_gfx9_e64 ||
976 MI.getOpcode() == AMDGPU::V_FMA_F16_gfx9_t16_e64 ||
977 MI.getOpcode() == AMDGPU::V_FMA_F16_gfx9_fake16_e64 ||
978 (MI.getOpcode() == AMDGPU::V_FMA_F64_e64 &&
979 ST->hasFmaakFmamkF64Insts())) {
980 shrinkMadFma(MI);
981 continue;
982 }
983
984 // If there is no chance we will shrink it and use VCC as sdst to get
985 // a 32 bit form try to replace dead sdst with NULL.
986 if (TII->isVOP3(MI.getOpcode())) {
987 tryReplaceDeadSDST(MI);
988 if (!TII->hasVALU32BitEncoding(MI.getOpcode())) {
989 continue;
990 }
991 }
992
993 if (!TII->canShrink(MI, *MRI)) {
994 // Try commuting the instruction and see if that enables us to shrink
995 // it.
996 if (!MI.isCommutable() || !TII->commuteInstruction(MI) ||
997 !TII->canShrink(MI, *MRI)) {
998 tryReplaceDeadSDST(MI);
999 continue;
1000 }
1001 }
1002
1003 int Op32 = AMDGPU::getVOPe32(MI.getOpcode());
1004
1005 if (TII->isVOPC(Op32)) {
1006 MachineOperand &Op0 = MI.getOperand(0);
1007 if (Op0.isReg()) {
1008 // Exclude VOPCX instructions as these don't explicitly write a
1009 // dst.
1010 Register DstReg = Op0.getReg();
1011 if (DstReg.isVirtual()) {
1012 // VOPC instructions can only write to the VCC register. We can't
1013 // force them to use VCC here, because this is only one register and
1014 // cannot deal with sequences which would require multiple copies of
1015 // VCC, e.g. S_AND_B64 (vcc = V_CMP_...), (vcc = V_CMP_...)
1016 //
1017 // So, instead of forcing the instruction to write to VCC, we
1018 // provide a hint to the register allocator to use VCC and then we
1019 // will run this pass again after RA and shrink it if it outputs to
1020 // VCC.
1021 MRI->setRegAllocationHint(DstReg, 0, VCCReg);
1022 continue;
1023 }
1024 if (DstReg != VCCReg)
1025 continue;
1026 }
1027 }
1028
1029 if (Op32 == AMDGPU::V_CNDMASK_B32_e32) {
1030 // We shrink V_CNDMASK_B32_e64 using regalloc hints like we do for VOPC
1031 // instructions.
1032 const MachineOperand *Src2 =
1033 TII->getNamedOperand(MI, AMDGPU::OpName::src2);
1034 if (!Src2->isReg())
1035 continue;
1036 Register SReg = Src2->getReg();
1037 if (SReg.isVirtual()) {
1038 MRI->setRegAllocationHint(SReg, 0, VCCReg);
1039 continue;
1040 }
1041 if (SReg != VCCReg)
1042 continue;
1043 }
1044
1045 // Check for the bool flag output for instructions like V_ADD_I32_e64.
1046 const MachineOperand *SDst = TII->getNamedOperand(MI,
1047 AMDGPU::OpName::sdst);
1048
1049 if (SDst) {
1050 bool Next = false;
1051
1052 if (SDst->getReg() != VCCReg) {
1053 if (SDst->getReg().isVirtual())
1054 MRI->setRegAllocationHint(SDst->getReg(), 0, VCCReg);
1055 Next = true;
1056 }
1057
1058 // All of the instructions with carry outs also have an SGPR input in
1059 // src2.
1060 const MachineOperand *Src2 = TII->getNamedOperand(MI,
1061 AMDGPU::OpName::src2);
1062 if (Src2 && Src2->getReg() != VCCReg) {
1063 if (Src2->getReg().isVirtual())
1064 MRI->setRegAllocationHint(Src2->getReg(), 0, VCCReg);
1065 Next = true;
1066 }
1067
1068 if (Next)
1069 continue;
1070 }
1071
1072 // Pre-GFX10, shrinking VOP3 instructions pre-RA gave us the chance to
1073 // fold an immediate into the shrunk instruction as a literal operand. In
1074 // GFX10 VOP3 instructions can take a literal operand anyway, so there is
1075 // no advantage to doing this.
1076 // However, if 64-bit literals are allowed we still need to shrink it
1077 // for such literal to be able to fold.
1078 if (ST->hasVOP3Literal() &&
1079 (!ST->has64BitLiterals() || AMDGPU::isTrue16Inst(MI.getOpcode())) &&
1080 !IsPostRA)
1081 continue;
1082
1083 if (ST->hasTrue16BitInsts() && AMDGPU::isTrue16Inst(MI.getOpcode()) &&
1084 !shouldShrinkTrue16(MI))
1085 continue;
1086
1087 // We can shrink this instruction
1088 LLVM_DEBUG(dbgs() << "Shrinking " << MI);
1089
1090 MachineInstr *Inst32 = TII->buildShrunkInst(MI, Op32);
1091 ++NumInstructionsShrunk;
1092
1093 // Copy extra operands not present in the instruction definition.
1094 copyExtraImplicitOps(*Inst32, MI);
1095
1096 // Copy deadness from the old explicit vcc def to the new implicit def.
1097 if (SDst && SDst->isDead())
1098 Inst32->findRegisterDefOperand(VCCReg, /*TRI=*/nullptr)->setIsDead();
1099
1100 MI.eraseFromParent();
1101 foldImmediates(*Inst32);
1102
1103 LLVM_DEBUG(dbgs() << "e32 MI = " << *Inst32 << '\n');
1104 }
1105 }
1106 return false;
1107}
1108
1109bool SIShrinkInstructionsLegacy::runOnMachineFunction(MachineFunction &MF) {
1110 if (skipFunction(MF.getFunction()))
1111 return false;
1112
1113 return SIShrinkInstructions().run(MF);
1114}
1115
1116PreservedAnalyses
1119 if (MF.getFunction().hasOptNone() || !SIShrinkInstructions().run(MF))
1120 return PreservedAnalyses::all();
1121
1123 PA.preserveSet<CFGAnalyses>();
1124 return PA;
1125}
unsigned SubReg
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Provides AMDGPU specific target descriptions.
MachineBasicBlock & MBB
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Definition CSEInfo.cpp:27
AMD GCN specific subclass of TargetSubtarget.
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:58
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define T
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static unsigned canModifyToInlineImmOp32(const SIInstrInfo *TII, const MachineOperand &Src, int32_t &ModifiedImm, bool Scalar)
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:480
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
bool hasTrue16BitInsts() const
Return true if the subtarget supports True16 instructions.
bool hasInv2PiInlineImm() const
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:270
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
bool hasOptNone() const
Do not optimize this function (-O0).
Definition Function.h:700
bool hasSwap() const
bool hasFmaakFmamkF64Insts() const
const SIInstrInfo * getInstrInfo() const override
bool hasGFX10_3Insts() const
bool has64BitLiterals() const
bool isWave32() const
unsigned getNSAMaxSize(bool HasSampler=false) const
bool hasSCmpK() const
Generation getGeneration() const
bool hasVOP3Literal() const
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineFunctionProperties & getProperties() const
Get the function properties.
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 & setMIFlags(unsigned Flags) const
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
const MachineInstrBuilder & addDef(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
unsigned getNumImplicitOperands() const
Returns the implicit operands number.
const MachineBasicBlock * getParent() const
bool isDebugInstr() const
LLVM_ABI void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
LLVM_ABI unsigned getNumExplicitOperands() const
Returns the number of non-implicit operands.
bool killsRegister(Register Reg, const TargetRegisterInfo *TRI) const
Return true if the MachineInstr kills the specified register.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
LLVM_ABI void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
LLVM_ABI void removeOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with.
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI bool hasRegisterImplicitUseOperand(Register Reg) const
Returns true if the MachineInstr has an implicit-use operand of exactly the given register (not consi...
MachineOperand * findRegisterDefOperand(Register Reg, const TargetRegisterInfo *TRI, bool isDead=false, bool Overlap=false)
Wrapper for findRegisterDefOperandIdx, it returns a pointer to the MachineOperand rather than an inde...
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
const GlobalValue * getGlobal() const
LLVM_ABI void ChangeToFrameIndex(int Idx, unsigned TargetFlags=0)
Replace this operand with a frame index.
void setImm(int64_t immVal)
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
void setIsDead(bool Val=true)
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
LLVM_ABI void ChangeToImmediate(int64_t ImmVal, unsigned TargetFlags=0)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value.
LLVM_ABI void ChangeToGA(const GlobalValue *GV, int64_t Offset, unsigned TargetFlags=0)
ChangeToGA - Replace this operand with a new global address operand.
void setIsKill(bool Val=true)
unsigned getTargetFlags() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
Register getReg() const
getReg - Returns the register number.
bool isFI() const
isFI - Tests if this is a MO_FrameIndex operand.
int64_t getOffset() const
Return the offset from the symbol in this operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Wrapper class representing virtual and physical registers.
Definition Register.h:19
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:74
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:78
static bool sopkIsZext(unsigned Opcode)
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &)
void push_back(const T &Elt)
MCRegister getRegister(unsigned i) const
Return the specified register in the class.
self_iterator getIterator()
Definition ilist_node.h:123
A range adaptor for a pair of iterators.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_READONLY int getSOPKOp(uint16_t Opcode)
LLVM_READONLY const MIMGInfo * getMIMGInfo(unsigned Opc)
int getMIMGOpcode(unsigned BaseOpcode, unsigned MIMGEncoding, unsigned VDataDwords, unsigned VAddrDwords)
LLVM_READONLY int getVOPe32(uint16_t Opcode)
bool isKImmOperand(const MCInstrDesc &Desc, unsigned OpNo)
Is this a KImm operand?
bool isTrue16Inst(unsigned Opc)
bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
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 bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:174
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:279
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:186
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:288
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:198
iterator_range(Container &&) -> iterator_range< llvm::detail::IterOfRange< Container > >
@ Sub
Subtraction of integers.
FunctionAddr VTableAddr Next
Definition InstrProf.h:141
DWARFExpression::Operation Op
constexpr int32_t SignExtend32(uint32_t X)
Sign-extend the number in the bottom B bits of X to a 32-bit integer.
Definition MathExtras.h:565
constexpr T reverseBits(T Val)
Reverse the bits in Val.
Definition MathExtras.h:127
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:583
FunctionPass * createSIShrinkInstructionsLegacyPass()
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:853
constexpr bool any() const
Definition LaneBitmask.h:53
A pair composed of a register and a sub-register index.