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