xref: /llvm-project/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp (revision 44a8a756e2f7ead6d9a37e6a0eb0384438850e5e)
1 //===-- SIFoldOperands.cpp - Fold operands --- ----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 /// \file
9 //===----------------------------------------------------------------------===//
10 //
11 
12 #include "AMDGPU.h"
13 #include "AMDGPUSubtarget.h"
14 #include "SIInstrInfo.h"
15 #include "SIMachineFunctionInfo.h"
16 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
17 #include "llvm/ADT/DepthFirstIterator.h"
18 #include "llvm/CodeGen/LiveIntervals.h"
19 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Target/TargetMachine.h"
25 
26 #define DEBUG_TYPE "si-fold-operands"
27 using namespace llvm;
28 
29 namespace {
30 
31 struct FoldCandidate {
32   MachineInstr *UseMI;
33   union {
34     MachineOperand *OpToFold;
35     uint64_t ImmToFold;
36     int FrameIndexToFold;
37   };
38   int ShrinkOpcode;
39   unsigned char UseOpNo;
40   MachineOperand::MachineOperandType Kind;
41   bool Commuted;
42 
43   FoldCandidate(MachineInstr *MI, unsigned OpNo, MachineOperand *FoldOp,
44                 bool Commuted_ = false,
45                 int ShrinkOp = -1) :
46     UseMI(MI), OpToFold(nullptr), ShrinkOpcode(ShrinkOp), UseOpNo(OpNo),
47     Kind(FoldOp->getType()),
48     Commuted(Commuted_) {
49     if (FoldOp->isImm()) {
50       ImmToFold = FoldOp->getImm();
51     } else if (FoldOp->isFI()) {
52       FrameIndexToFold = FoldOp->getIndex();
53     } else {
54       assert(FoldOp->isReg());
55       OpToFold = FoldOp;
56     }
57   }
58 
59   bool isFI() const {
60     return Kind == MachineOperand::MO_FrameIndex;
61   }
62 
63   bool isImm() const {
64     return Kind == MachineOperand::MO_Immediate;
65   }
66 
67   bool isReg() const {
68     return Kind == MachineOperand::MO_Register;
69   }
70 
71   bool isCommuted() const {
72     return Commuted;
73   }
74 
75   bool needsShrink() const {
76     return ShrinkOpcode != -1;
77   }
78 
79   int getShrinkOpcode() const {
80     return ShrinkOpcode;
81   }
82 };
83 
84 class SIFoldOperands : public MachineFunctionPass {
85 public:
86   static char ID;
87   MachineRegisterInfo *MRI;
88   const SIInstrInfo *TII;
89   const SIRegisterInfo *TRI;
90   const GCNSubtarget *ST;
91 
92   void foldOperand(MachineOperand &OpToFold,
93                    MachineInstr *UseMI,
94                    unsigned UseOpIdx,
95                    SmallVectorImpl<FoldCandidate> &FoldList,
96                    SmallVectorImpl<MachineInstr *> &CopiesToReplace) const;
97 
98   void foldInstOperand(MachineInstr &MI, MachineOperand &OpToFold) const;
99 
100   const MachineOperand *isClamp(const MachineInstr &MI) const;
101   bool tryFoldClamp(MachineInstr &MI);
102 
103   std::pair<const MachineOperand *, int> isOMod(const MachineInstr &MI) const;
104   bool tryFoldOMod(MachineInstr &MI);
105 
106 public:
107   SIFoldOperands() : MachineFunctionPass(ID) {
108     initializeSIFoldOperandsPass(*PassRegistry::getPassRegistry());
109   }
110 
111   bool runOnMachineFunction(MachineFunction &MF) override;
112 
113   StringRef getPassName() const override { return "SI Fold Operands"; }
114 
115   void getAnalysisUsage(AnalysisUsage &AU) const override {
116     AU.setPreservesCFG();
117     MachineFunctionPass::getAnalysisUsage(AU);
118   }
119 };
120 
121 } // End anonymous namespace.
122 
123 INITIALIZE_PASS(SIFoldOperands, DEBUG_TYPE,
124                 "SI Fold Operands", false, false)
125 
126 char SIFoldOperands::ID = 0;
127 
128 char &llvm::SIFoldOperandsID = SIFoldOperands::ID;
129 
130 // Wrapper around isInlineConstant that understands special cases when
131 // instruction types are replaced during operand folding.
132 static bool isInlineConstantIfFolded(const SIInstrInfo *TII,
133                                      const MachineInstr &UseMI,
134                                      unsigned OpNo,
135                                      const MachineOperand &OpToFold) {
136   if (TII->isInlineConstant(UseMI, OpNo, OpToFold))
137     return true;
138 
139   unsigned Opc = UseMI.getOpcode();
140   switch (Opc) {
141   case AMDGPU::V_MAC_F32_e64:
142   case AMDGPU::V_MAC_F16_e64:
143   case AMDGPU::V_FMAC_F32_e64: {
144     // Special case for mac. Since this is replaced with mad when folded into
145     // src2, we need to check the legality for the final instruction.
146     int Src2Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2);
147     if (static_cast<int>(OpNo) == Src2Idx) {
148       bool IsFMA = Opc == AMDGPU::V_FMAC_F32_e64;
149       bool IsF32 = Opc == AMDGPU::V_MAC_F32_e64;
150 
151       unsigned Opc = IsFMA ?
152         AMDGPU::V_FMA_F32 : (IsF32 ? AMDGPU::V_MAD_F32 : AMDGPU::V_MAD_F16);
153       const MCInstrDesc &MadDesc = TII->get(Opc);
154       return TII->isInlineConstant(OpToFold, MadDesc.OpInfo[OpNo].OperandType);
155     }
156     return false;
157   }
158   default:
159     return false;
160   }
161 }
162 
163 FunctionPass *llvm::createSIFoldOperandsPass() {
164   return new SIFoldOperands();
165 }
166 
167 static bool updateOperand(FoldCandidate &Fold,
168                           const SIInstrInfo &TII,
169                           const TargetRegisterInfo &TRI) {
170   MachineInstr *MI = Fold.UseMI;
171   MachineOperand &Old = MI->getOperand(Fold.UseOpNo);
172   assert(Old.isReg());
173 
174   if (Fold.isImm()) {
175     if (MI->getDesc().TSFlags & SIInstrFlags::IsPacked) {
176       // Set op_sel/op_sel_hi on this operand or bail out if op_sel is
177       // already set.
178       unsigned Opcode = MI->getOpcode();
179       int OpNo = MI->getOperandNo(&Old);
180       int ModIdx = -1;
181       if (OpNo == AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0))
182         ModIdx = AMDGPU::OpName::src0_modifiers;
183       else if (OpNo == AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src1))
184         ModIdx = AMDGPU::OpName::src1_modifiers;
185       else if (OpNo == AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2))
186         ModIdx = AMDGPU::OpName::src2_modifiers;
187       assert(ModIdx != -1);
188       ModIdx = AMDGPU::getNamedOperandIdx(Opcode, ModIdx);
189       MachineOperand &Mod = MI->getOperand(ModIdx);
190       unsigned Val = Mod.getImm();
191       if ((Val & SISrcMods::OP_SEL_0) || !(Val & SISrcMods::OP_SEL_1))
192         return false;
193       // If upper part is all zero we do not need op_sel_hi.
194       if (!isUInt<16>(Fold.ImmToFold)) {
195         if (!(Fold.ImmToFold & 0xffff)) {
196           Mod.setImm(Mod.getImm() | SISrcMods::OP_SEL_0);
197           Mod.setImm(Mod.getImm() & ~SISrcMods::OP_SEL_1);
198           Old.ChangeToImmediate((Fold.ImmToFold >> 16) & 0xffff);
199           return true;
200         }
201         Mod.setImm(Mod.getImm() & ~SISrcMods::OP_SEL_1);
202       }
203     }
204 
205     if (Fold.needsShrink()) {
206       MachineBasicBlock *MBB = MI->getParent();
207       auto Liveness = MBB->computeRegisterLiveness(&TRI, AMDGPU::VCC, MI);
208       if (Liveness != MachineBasicBlock::LQR_Dead)
209         return false;
210 
211       MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
212       int Op32 = Fold.getShrinkOpcode();
213       MachineOperand &Dst0 = MI->getOperand(0);
214       MachineOperand &Dst1 = MI->getOperand(1);
215       assert(Dst0.isDef() && Dst1.isDef());
216 
217       bool HaveNonDbgCarryUse = !MRI.use_nodbg_empty(Dst1.getReg());
218 
219       const TargetRegisterClass *Dst0RC = MRI.getRegClass(Dst0.getReg());
220       unsigned NewReg0 = MRI.createVirtualRegister(Dst0RC);
221       const TargetRegisterClass *Dst1RC = MRI.getRegClass(Dst1.getReg());
222       unsigned NewReg1 = MRI.createVirtualRegister(Dst1RC);
223 
224       MachineInstr *Inst32 = TII.buildShrunkInst(*MI, Op32);
225 
226       if (HaveNonDbgCarryUse) {
227         BuildMI(*MBB, MI, MI->getDebugLoc(), TII.get(AMDGPU::COPY), Dst1.getReg())
228           .addReg(AMDGPU::VCC, RegState::Kill);
229       }
230 
231       // Keep the old instruction around to avoid breaking iterators, but
232       // replace the outputs with dummy registers.
233       Dst0.setReg(NewReg0);
234       Dst1.setReg(NewReg1);
235 
236       if (Fold.isCommuted())
237         TII.commuteInstruction(*Inst32, false);
238       return true;
239     }
240 
241     Old.ChangeToImmediate(Fold.ImmToFold);
242     return true;
243   }
244 
245   assert(!Fold.needsShrink() && "not handled");
246 
247   if (Fold.isFI()) {
248     Old.ChangeToFrameIndex(Fold.FrameIndexToFold);
249     return true;
250   }
251 
252   MachineOperand *New = Fold.OpToFold;
253   if (TargetRegisterInfo::isVirtualRegister(Old.getReg()) &&
254       TargetRegisterInfo::isVirtualRegister(New->getReg())) {
255     Old.substVirtReg(New->getReg(), New->getSubReg(), TRI);
256 
257     Old.setIsUndef(New->isUndef());
258     return true;
259   }
260 
261   // FIXME: Handle physical registers.
262 
263   return false;
264 }
265 
266 static bool isUseMIInFoldList(ArrayRef<FoldCandidate> FoldList,
267                               const MachineInstr *MI) {
268   for (auto Candidate : FoldList) {
269     if (Candidate.UseMI == MI)
270       return true;
271   }
272   return false;
273 }
274 
275 static bool tryAddToFoldList(SmallVectorImpl<FoldCandidate> &FoldList,
276                              MachineInstr *MI, unsigned OpNo,
277                              MachineOperand *OpToFold,
278                              const SIInstrInfo *TII) {
279   if (!TII->isOperandLegal(*MI, OpNo, OpToFold)) {
280 
281     // Special case for v_mac_{f16, f32}_e64 if we are trying to fold into src2
282     unsigned Opc = MI->getOpcode();
283     if ((Opc == AMDGPU::V_MAC_F32_e64 || Opc == AMDGPU::V_MAC_F16_e64 ||
284          Opc == AMDGPU::V_FMAC_F32_e64) &&
285         (int)OpNo == AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2)) {
286       bool IsFMA = Opc == AMDGPU::V_FMAC_F32_e64;
287       bool IsF32 = Opc == AMDGPU::V_MAC_F32_e64;
288       unsigned NewOpc = IsFMA ?
289         AMDGPU::V_FMA_F32 : (IsF32 ? AMDGPU::V_MAD_F32 : AMDGPU::V_MAD_F16);
290 
291       // Check if changing this to a v_mad_{f16, f32} instruction will allow us
292       // to fold the operand.
293       MI->setDesc(TII->get(NewOpc));
294       bool FoldAsMAD = tryAddToFoldList(FoldList, MI, OpNo, OpToFold, TII);
295       if (FoldAsMAD) {
296         MI->untieRegOperand(OpNo);
297         return true;
298       }
299       MI->setDesc(TII->get(Opc));
300     }
301 
302     // Special case for s_setreg_b32
303     if (Opc == AMDGPU::S_SETREG_B32 && OpToFold->isImm()) {
304       MI->setDesc(TII->get(AMDGPU::S_SETREG_IMM32_B32));
305       FoldList.push_back(FoldCandidate(MI, OpNo, OpToFold));
306       return true;
307     }
308 
309     // If we are already folding into another operand of MI, then
310     // we can't commute the instruction, otherwise we risk making the
311     // other fold illegal.
312     if (isUseMIInFoldList(FoldList, MI))
313       return false;
314 
315     unsigned CommuteOpNo = OpNo;
316 
317     // Operand is not legal, so try to commute the instruction to
318     // see if this makes it possible to fold.
319     unsigned CommuteIdx0 = TargetInstrInfo::CommuteAnyOperandIndex;
320     unsigned CommuteIdx1 = TargetInstrInfo::CommuteAnyOperandIndex;
321     bool CanCommute = TII->findCommutedOpIndices(*MI, CommuteIdx0, CommuteIdx1);
322 
323     if (CanCommute) {
324       if (CommuteIdx0 == OpNo)
325         CommuteOpNo = CommuteIdx1;
326       else if (CommuteIdx1 == OpNo)
327         CommuteOpNo = CommuteIdx0;
328     }
329 
330 
331     // One of operands might be an Imm operand, and OpNo may refer to it after
332     // the call of commuteInstruction() below. Such situations are avoided
333     // here explicitly as OpNo must be a register operand to be a candidate
334     // for memory folding.
335     if (CanCommute && (!MI->getOperand(CommuteIdx0).isReg() ||
336                        !MI->getOperand(CommuteIdx1).isReg()))
337       return false;
338 
339     if (!CanCommute ||
340         !TII->commuteInstruction(*MI, false, CommuteIdx0, CommuteIdx1))
341       return false;
342 
343     if (!TII->isOperandLegal(*MI, CommuteOpNo, OpToFold)) {
344       if ((Opc == AMDGPU::V_ADD_I32_e64 ||
345            Opc == AMDGPU::V_SUB_I32_e64 ||
346            Opc == AMDGPU::V_SUBREV_I32_e64) && // FIXME
347           OpToFold->isImm()) {
348         MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
349 
350         // Verify the other operand is a VGPR, otherwise we would violate the
351         // constant bus restriction.
352         unsigned OtherIdx = CommuteOpNo == CommuteIdx0 ? CommuteIdx1 : CommuteIdx0;
353         MachineOperand &OtherOp = MI->getOperand(OtherIdx);
354         if (!OtherOp.isReg() ||
355             !TII->getRegisterInfo().isVGPR(MRI, OtherOp.getReg()))
356           return false;
357 
358         const MachineOperand &SDst = MI->getOperand(1);
359         assert(SDst.isDef());
360 
361         int Op32 =  AMDGPU::getVOPe32(Opc);
362         FoldList.push_back(FoldCandidate(MI, CommuteOpNo, OpToFold, true,
363                                          Op32));
364         return true;
365       }
366 
367       TII->commuteInstruction(*MI, false, CommuteIdx0, CommuteIdx1);
368       return false;
369     }
370 
371     FoldList.push_back(FoldCandidate(MI, CommuteOpNo, OpToFold, true));
372     return true;
373   }
374 
375   FoldList.push_back(FoldCandidate(MI, OpNo, OpToFold));
376   return true;
377 }
378 
379 // If the use operand doesn't care about the value, this may be an operand only
380 // used for register indexing, in which case it is unsafe to fold.
381 static bool isUseSafeToFold(const SIInstrInfo *TII,
382                             const MachineInstr &MI,
383                             const MachineOperand &UseMO) {
384   return !UseMO.isUndef() && !TII->isSDWA(MI);
385   //return !MI.hasRegisterImplicitUseOperand(UseMO.getReg());
386 }
387 
388 void SIFoldOperands::foldOperand(
389   MachineOperand &OpToFold,
390   MachineInstr *UseMI,
391   unsigned UseOpIdx,
392   SmallVectorImpl<FoldCandidate> &FoldList,
393   SmallVectorImpl<MachineInstr *> &CopiesToReplace) const {
394   const MachineOperand &UseOp = UseMI->getOperand(UseOpIdx);
395 
396   if (!isUseSafeToFold(TII, *UseMI, UseOp))
397     return;
398 
399   // FIXME: Fold operands with subregs.
400   if (UseOp.isReg() && OpToFold.isReg()) {
401     if (UseOp.isImplicit() || UseOp.getSubReg() != AMDGPU::NoSubRegister)
402       return;
403 
404     // Don't fold subregister extracts into tied operands, only if it is a full
405     // copy since a subregister use tied to a full register def doesn't really
406     // make sense. e.g. don't fold:
407     //
408     // %1 = COPY %0:sub1
409     // %2<tied3> = V_MAC_{F16, F32} %3, %4, %1<tied0>
410     //
411     //  into
412     // %2<tied3> = V_MAC_{F16, F32} %3, %4, %0:sub1<tied0>
413     if (UseOp.isTied() && OpToFold.getSubReg() != AMDGPU::NoSubRegister)
414       return;
415   }
416 
417   // Special case for REG_SEQUENCE: We can't fold literals into
418   // REG_SEQUENCE instructions, so we have to fold them into the
419   // uses of REG_SEQUENCE.
420   if (UseMI->isRegSequence()) {
421     unsigned RegSeqDstReg = UseMI->getOperand(0).getReg();
422     unsigned RegSeqDstSubReg = UseMI->getOperand(UseOpIdx + 1).getImm();
423 
424     for (MachineRegisterInfo::use_iterator
425            RSUse = MRI->use_begin(RegSeqDstReg), RSE = MRI->use_end();
426          RSUse != RSE; ++RSUse) {
427 
428       MachineInstr *RSUseMI = RSUse->getParent();
429       if (RSUse->getSubReg() != RegSeqDstSubReg)
430         continue;
431 
432       foldOperand(OpToFold, RSUseMI, RSUse.getOperandNo(), FoldList,
433                   CopiesToReplace);
434     }
435 
436     return;
437   }
438 
439 
440   bool FoldingImm = OpToFold.isImm();
441 
442   // In order to fold immediates into copies, we need to change the
443   // copy to a MOV.
444   if (FoldingImm && UseMI->isCopy()) {
445     unsigned DestReg = UseMI->getOperand(0).getReg();
446     const TargetRegisterClass *DestRC
447       = TargetRegisterInfo::isVirtualRegister(DestReg) ?
448       MRI->getRegClass(DestReg) :
449       TRI->getPhysRegClass(DestReg);
450 
451     unsigned MovOp = TII->getMovOpcode(DestRC);
452     if (MovOp == AMDGPU::COPY)
453       return;
454 
455     UseMI->setDesc(TII->get(MovOp));
456     CopiesToReplace.push_back(UseMI);
457   } else {
458     const MCInstrDesc &UseDesc = UseMI->getDesc();
459 
460     // Don't fold into target independent nodes.  Target independent opcodes
461     // don't have defined register classes.
462     if (UseDesc.isVariadic() ||
463         UseOp.isImplicit() ||
464         UseDesc.OpInfo[UseOpIdx].RegClass == -1)
465       return;
466   }
467 
468   if (!FoldingImm) {
469     tryAddToFoldList(FoldList, UseMI, UseOpIdx, &OpToFold, TII);
470 
471     // FIXME: We could try to change the instruction from 64-bit to 32-bit
472     // to enable more folding opportunites.  The shrink operands pass
473     // already does this.
474     return;
475   }
476 
477 
478   const MCInstrDesc &FoldDesc = OpToFold.getParent()->getDesc();
479   const TargetRegisterClass *FoldRC =
480     TRI->getRegClass(FoldDesc.OpInfo[0].RegClass);
481 
482 
483   // Split 64-bit constants into 32-bits for folding.
484   if (UseOp.getSubReg() && AMDGPU::getRegBitWidth(FoldRC->getID()) == 64) {
485     unsigned UseReg = UseOp.getReg();
486     const TargetRegisterClass *UseRC
487       = TargetRegisterInfo::isVirtualRegister(UseReg) ?
488       MRI->getRegClass(UseReg) :
489       TRI->getPhysRegClass(UseReg);
490 
491     if (AMDGPU::getRegBitWidth(UseRC->getID()) != 64)
492       return;
493 
494     APInt Imm(64, OpToFold.getImm());
495     if (UseOp.getSubReg() == AMDGPU::sub0) {
496       Imm = Imm.getLoBits(32);
497     } else {
498       assert(UseOp.getSubReg() == AMDGPU::sub1);
499       Imm = Imm.getHiBits(32);
500     }
501 
502     MachineOperand ImmOp = MachineOperand::CreateImm(Imm.getSExtValue());
503     tryAddToFoldList(FoldList, UseMI, UseOpIdx, &ImmOp, TII);
504     return;
505   }
506 
507 
508 
509   tryAddToFoldList(FoldList, UseMI, UseOpIdx, &OpToFold, TII);
510 }
511 
512 static bool evalBinaryInstruction(unsigned Opcode, int32_t &Result,
513                                   uint32_t LHS, uint32_t RHS) {
514   switch (Opcode) {
515   case AMDGPU::V_AND_B32_e64:
516   case AMDGPU::V_AND_B32_e32:
517   case AMDGPU::S_AND_B32:
518     Result = LHS & RHS;
519     return true;
520   case AMDGPU::V_OR_B32_e64:
521   case AMDGPU::V_OR_B32_e32:
522   case AMDGPU::S_OR_B32:
523     Result = LHS | RHS;
524     return true;
525   case AMDGPU::V_XOR_B32_e64:
526   case AMDGPU::V_XOR_B32_e32:
527   case AMDGPU::S_XOR_B32:
528     Result = LHS ^ RHS;
529     return true;
530   case AMDGPU::V_LSHL_B32_e64:
531   case AMDGPU::V_LSHL_B32_e32:
532   case AMDGPU::S_LSHL_B32:
533     // The instruction ignores the high bits for out of bounds shifts.
534     Result = LHS << (RHS & 31);
535     return true;
536   case AMDGPU::V_LSHLREV_B32_e64:
537   case AMDGPU::V_LSHLREV_B32_e32:
538     Result = RHS << (LHS & 31);
539     return true;
540   case AMDGPU::V_LSHR_B32_e64:
541   case AMDGPU::V_LSHR_B32_e32:
542   case AMDGPU::S_LSHR_B32:
543     Result = LHS >> (RHS & 31);
544     return true;
545   case AMDGPU::V_LSHRREV_B32_e64:
546   case AMDGPU::V_LSHRREV_B32_e32:
547     Result = RHS >> (LHS & 31);
548     return true;
549   case AMDGPU::V_ASHR_I32_e64:
550   case AMDGPU::V_ASHR_I32_e32:
551   case AMDGPU::S_ASHR_I32:
552     Result = static_cast<int32_t>(LHS) >> (RHS & 31);
553     return true;
554   case AMDGPU::V_ASHRREV_I32_e64:
555   case AMDGPU::V_ASHRREV_I32_e32:
556     Result = static_cast<int32_t>(RHS) >> (LHS & 31);
557     return true;
558   default:
559     return false;
560   }
561 }
562 
563 static unsigned getMovOpc(bool IsScalar) {
564   return IsScalar ? AMDGPU::S_MOV_B32 : AMDGPU::V_MOV_B32_e32;
565 }
566 
567 /// Remove any leftover implicit operands from mutating the instruction. e.g.
568 /// if we replace an s_and_b32 with a copy, we don't need the implicit scc def
569 /// anymore.
570 static void stripExtraCopyOperands(MachineInstr &MI) {
571   const MCInstrDesc &Desc = MI.getDesc();
572   unsigned NumOps = Desc.getNumOperands() +
573                     Desc.getNumImplicitUses() +
574                     Desc.getNumImplicitDefs();
575 
576   for (unsigned I = MI.getNumOperands() - 1; I >= NumOps; --I)
577     MI.RemoveOperand(I);
578 }
579 
580 static void mutateCopyOp(MachineInstr &MI, const MCInstrDesc &NewDesc) {
581   MI.setDesc(NewDesc);
582   stripExtraCopyOperands(MI);
583 }
584 
585 static MachineOperand *getImmOrMaterializedImm(MachineRegisterInfo &MRI,
586                                                MachineOperand &Op) {
587   if (Op.isReg()) {
588     // If this has a subregister, it obviously is a register source.
589     if (Op.getSubReg() != AMDGPU::NoSubRegister ||
590         !TargetRegisterInfo::isVirtualRegister(Op.getReg()))
591       return &Op;
592 
593     MachineInstr *Def = MRI.getVRegDef(Op.getReg());
594     if (Def && Def->isMoveImmediate()) {
595       MachineOperand &ImmSrc = Def->getOperand(1);
596       if (ImmSrc.isImm())
597         return &ImmSrc;
598     }
599   }
600 
601   return &Op;
602 }
603 
604 // Try to simplify operations with a constant that may appear after instruction
605 // selection.
606 // TODO: See if a frame index with a fixed offset can fold.
607 static bool tryConstantFoldOp(MachineRegisterInfo &MRI,
608                               const SIInstrInfo *TII,
609                               MachineInstr *MI,
610                               MachineOperand *ImmOp) {
611   unsigned Opc = MI->getOpcode();
612   if (Opc == AMDGPU::V_NOT_B32_e64 || Opc == AMDGPU::V_NOT_B32_e32 ||
613       Opc == AMDGPU::S_NOT_B32) {
614     MI->getOperand(1).ChangeToImmediate(~ImmOp->getImm());
615     mutateCopyOp(*MI, TII->get(getMovOpc(Opc == AMDGPU::S_NOT_B32)));
616     return true;
617   }
618 
619   int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1);
620   if (Src1Idx == -1)
621     return false;
622 
623   int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0);
624   MachineOperand *Src0 = getImmOrMaterializedImm(MRI, MI->getOperand(Src0Idx));
625   MachineOperand *Src1 = getImmOrMaterializedImm(MRI, MI->getOperand(Src1Idx));
626 
627   if (!Src0->isImm() && !Src1->isImm())
628     return false;
629 
630   if (MI->getOpcode() == AMDGPU::V_LSHL_OR_B32) {
631     if (Src0->isImm() && Src0->getImm() == 0) {
632       // v_lshl_or_b32 0, X, Y -> copy Y
633       // v_lshl_or_b32 0, X, K -> v_mov_b32 K
634       bool UseCopy = TII->getNamedOperand(*MI, AMDGPU::OpName::src2)->isReg();
635       MI->RemoveOperand(Src1Idx);
636       MI->RemoveOperand(Src0Idx);
637 
638       MI->setDesc(TII->get(UseCopy ? AMDGPU::COPY : AMDGPU::V_MOV_B32_e32));
639       return true;
640     }
641   }
642 
643   // and k0, k1 -> v_mov_b32 (k0 & k1)
644   // or k0, k1 -> v_mov_b32 (k0 | k1)
645   // xor k0, k1 -> v_mov_b32 (k0 ^ k1)
646   if (Src0->isImm() && Src1->isImm()) {
647     int32_t NewImm;
648     if (!evalBinaryInstruction(Opc, NewImm, Src0->getImm(), Src1->getImm()))
649       return false;
650 
651     const SIRegisterInfo &TRI = TII->getRegisterInfo();
652     bool IsSGPR = TRI.isSGPRReg(MRI, MI->getOperand(0).getReg());
653 
654     // Be careful to change the right operand, src0 may belong to a different
655     // instruction.
656     MI->getOperand(Src0Idx).ChangeToImmediate(NewImm);
657     MI->RemoveOperand(Src1Idx);
658     mutateCopyOp(*MI, TII->get(getMovOpc(IsSGPR)));
659     return true;
660   }
661 
662   if (!MI->isCommutable())
663     return false;
664 
665   if (Src0->isImm() && !Src1->isImm()) {
666     std::swap(Src0, Src1);
667     std::swap(Src0Idx, Src1Idx);
668   }
669 
670   int32_t Src1Val = static_cast<int32_t>(Src1->getImm());
671   if (Opc == AMDGPU::V_OR_B32_e64 ||
672       Opc == AMDGPU::V_OR_B32_e32 ||
673       Opc == AMDGPU::S_OR_B32) {
674     if (Src1Val == 0) {
675       // y = or x, 0 => y = copy x
676       MI->RemoveOperand(Src1Idx);
677       mutateCopyOp(*MI, TII->get(AMDGPU::COPY));
678     } else if (Src1Val == -1) {
679       // y = or x, -1 => y = v_mov_b32 -1
680       MI->RemoveOperand(Src1Idx);
681       mutateCopyOp(*MI, TII->get(getMovOpc(Opc == AMDGPU::S_OR_B32)));
682     } else
683       return false;
684 
685     return true;
686   }
687 
688   if (MI->getOpcode() == AMDGPU::V_AND_B32_e64 ||
689       MI->getOpcode() == AMDGPU::V_AND_B32_e32 ||
690       MI->getOpcode() == AMDGPU::S_AND_B32) {
691     if (Src1Val == 0) {
692       // y = and x, 0 => y = v_mov_b32 0
693       MI->RemoveOperand(Src0Idx);
694       mutateCopyOp(*MI, TII->get(getMovOpc(Opc == AMDGPU::S_AND_B32)));
695     } else if (Src1Val == -1) {
696       // y = and x, -1 => y = copy x
697       MI->RemoveOperand(Src1Idx);
698       mutateCopyOp(*MI, TII->get(AMDGPU::COPY));
699       stripExtraCopyOperands(*MI);
700     } else
701       return false;
702 
703     return true;
704   }
705 
706   if (MI->getOpcode() == AMDGPU::V_XOR_B32_e64 ||
707       MI->getOpcode() == AMDGPU::V_XOR_B32_e32 ||
708       MI->getOpcode() == AMDGPU::S_XOR_B32) {
709     if (Src1Val == 0) {
710       // y = xor x, 0 => y = copy x
711       MI->RemoveOperand(Src1Idx);
712       mutateCopyOp(*MI, TII->get(AMDGPU::COPY));
713       return true;
714     }
715   }
716 
717   return false;
718 }
719 
720 // Try to fold an instruction into a simpler one
721 static bool tryFoldInst(const SIInstrInfo *TII,
722                         MachineInstr *MI) {
723   unsigned Opc = MI->getOpcode();
724 
725   if (Opc == AMDGPU::V_CNDMASK_B32_e32    ||
726       Opc == AMDGPU::V_CNDMASK_B32_e64    ||
727       Opc == AMDGPU::V_CNDMASK_B64_PSEUDO) {
728     const MachineOperand *Src0 = TII->getNamedOperand(*MI, AMDGPU::OpName::src0);
729     const MachineOperand *Src1 = TII->getNamedOperand(*MI, AMDGPU::OpName::src1);
730     if (Src1->isIdenticalTo(*Src0)) {
731       LLVM_DEBUG(dbgs() << "Folded " << *MI << " into ");
732       int Src2Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2);
733       if (Src2Idx != -1)
734         MI->RemoveOperand(Src2Idx);
735       MI->RemoveOperand(AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1));
736       mutateCopyOp(*MI, TII->get(Src0->isReg() ? (unsigned)AMDGPU::COPY
737                                                : getMovOpc(false)));
738       LLVM_DEBUG(dbgs() << *MI << '\n');
739       return true;
740     }
741   }
742 
743   return false;
744 }
745 
746 void SIFoldOperands::foldInstOperand(MachineInstr &MI,
747                                      MachineOperand &OpToFold) const {
748   // We need mutate the operands of new mov instructions to add implicit
749   // uses of EXEC, but adding them invalidates the use_iterator, so defer
750   // this.
751   SmallVector<MachineInstr *, 4> CopiesToReplace;
752   SmallVector<FoldCandidate, 4> FoldList;
753   MachineOperand &Dst = MI.getOperand(0);
754 
755   bool FoldingImm = OpToFold.isImm() || OpToFold.isFI();
756   if (FoldingImm) {
757     unsigned NumLiteralUses = 0;
758     MachineOperand *NonInlineUse = nullptr;
759     int NonInlineUseOpNo = -1;
760 
761     MachineRegisterInfo::use_iterator NextUse;
762     for (MachineRegisterInfo::use_iterator
763            Use = MRI->use_begin(Dst.getReg()), E = MRI->use_end();
764          Use != E; Use = NextUse) {
765       NextUse = std::next(Use);
766       MachineInstr *UseMI = Use->getParent();
767       unsigned OpNo = Use.getOperandNo();
768 
769       // Folding the immediate may reveal operations that can be constant
770       // folded or replaced with a copy. This can happen for example after
771       // frame indices are lowered to constants or from splitting 64-bit
772       // constants.
773       //
774       // We may also encounter cases where one or both operands are
775       // immediates materialized into a register, which would ordinarily not
776       // be folded due to multiple uses or operand constraints.
777 
778       if (OpToFold.isImm() && tryConstantFoldOp(*MRI, TII, UseMI, &OpToFold)) {
779         LLVM_DEBUG(dbgs() << "Constant folded " << *UseMI << '\n');
780 
781         // Some constant folding cases change the same immediate's use to a new
782         // instruction, e.g. and x, 0 -> 0. Make sure we re-visit the user
783         // again. The same constant folded instruction could also have a second
784         // use operand.
785         NextUse = MRI->use_begin(Dst.getReg());
786         FoldList.clear();
787         continue;
788       }
789 
790       // Try to fold any inline immediate uses, and then only fold other
791       // constants if they have one use.
792       //
793       // The legality of the inline immediate must be checked based on the use
794       // operand, not the defining instruction, because 32-bit instructions
795       // with 32-bit inline immediate sources may be used to materialize
796       // constants used in 16-bit operands.
797       //
798       // e.g. it is unsafe to fold:
799       //  s_mov_b32 s0, 1.0    // materializes 0x3f800000
800       //  v_add_f16 v0, v1, s0 // 1.0 f16 inline immediate sees 0x00003c00
801 
802       // Folding immediates with more than one use will increase program size.
803       // FIXME: This will also reduce register usage, which may be better
804       // in some cases. A better heuristic is needed.
805       if (isInlineConstantIfFolded(TII, *UseMI, OpNo, OpToFold)) {
806         foldOperand(OpToFold, UseMI, OpNo, FoldList, CopiesToReplace);
807       } else {
808         if (++NumLiteralUses == 1) {
809           NonInlineUse = &*Use;
810           NonInlineUseOpNo = OpNo;
811         }
812       }
813     }
814 
815     if (NumLiteralUses == 1) {
816       MachineInstr *UseMI = NonInlineUse->getParent();
817       foldOperand(OpToFold, UseMI, NonInlineUseOpNo, FoldList, CopiesToReplace);
818     }
819   } else {
820     // Folding register.
821     for (MachineRegisterInfo::use_iterator
822            Use = MRI->use_begin(Dst.getReg()), E = MRI->use_end();
823          Use != E; ++Use) {
824       MachineInstr *UseMI = Use->getParent();
825 
826       foldOperand(OpToFold, UseMI, Use.getOperandNo(),
827                   FoldList, CopiesToReplace);
828     }
829   }
830 
831   MachineFunction *MF = MI.getParent()->getParent();
832   // Make sure we add EXEC uses to any new v_mov instructions created.
833   for (MachineInstr *Copy : CopiesToReplace)
834     Copy->addImplicitDefUseOperands(*MF);
835 
836   for (FoldCandidate &Fold : FoldList) {
837     if (updateOperand(Fold, *TII, *TRI)) {
838       // Clear kill flags.
839       if (Fold.isReg()) {
840         assert(Fold.OpToFold && Fold.OpToFold->isReg());
841         // FIXME: Probably shouldn't bother trying to fold if not an
842         // SGPR. PeepholeOptimizer can eliminate redundant VGPR->VGPR
843         // copies.
844         MRI->clearKillFlags(Fold.OpToFold->getReg());
845       }
846       LLVM_DEBUG(dbgs() << "Folded source from " << MI << " into OpNo "
847                         << static_cast<int>(Fold.UseOpNo) << " of "
848                         << *Fold.UseMI << '\n');
849       tryFoldInst(TII, Fold.UseMI);
850     } else if (Fold.isCommuted()) {
851       // Restoring instruction's original operand order if fold has failed.
852       TII->commuteInstruction(*Fold.UseMI, false);
853     }
854   }
855 }
856 
857 // Clamp patterns are canonically selected to v_max_* instructions, so only
858 // handle them.
859 const MachineOperand *SIFoldOperands::isClamp(const MachineInstr &MI) const {
860   unsigned Op = MI.getOpcode();
861   switch (Op) {
862   case AMDGPU::V_MAX_F32_e64:
863   case AMDGPU::V_MAX_F16_e64:
864   case AMDGPU::V_MAX_F64:
865   case AMDGPU::V_PK_MAX_F16: {
866     if (!TII->getNamedOperand(MI, AMDGPU::OpName::clamp)->getImm())
867       return nullptr;
868 
869     // Make sure sources are identical.
870     const MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
871     const MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
872     if (!Src0->isReg() || !Src1->isReg() ||
873         Src0->getReg() != Src1->getReg() ||
874         Src0->getSubReg() != Src1->getSubReg() ||
875         Src0->getSubReg() != AMDGPU::NoSubRegister)
876       return nullptr;
877 
878     // Can't fold up if we have modifiers.
879     if (TII->hasModifiersSet(MI, AMDGPU::OpName::omod))
880       return nullptr;
881 
882     unsigned Src0Mods
883       = TII->getNamedOperand(MI, AMDGPU::OpName::src0_modifiers)->getImm();
884     unsigned Src1Mods
885       = TII->getNamedOperand(MI, AMDGPU::OpName::src1_modifiers)->getImm();
886 
887     // Having a 0 op_sel_hi would require swizzling the output in the source
888     // instruction, which we can't do.
889     unsigned UnsetMods = (Op == AMDGPU::V_PK_MAX_F16) ? SISrcMods::OP_SEL_1 : 0;
890     if (Src0Mods != UnsetMods && Src1Mods != UnsetMods)
891       return nullptr;
892     return Src0;
893   }
894   default:
895     return nullptr;
896   }
897 }
898 
899 // We obviously have multiple uses in a clamp since the register is used twice
900 // in the same instruction.
901 static bool hasOneNonDBGUseInst(const MachineRegisterInfo &MRI, unsigned Reg) {
902   int Count = 0;
903   for (auto I = MRI.use_instr_nodbg_begin(Reg), E = MRI.use_instr_nodbg_end();
904        I != E; ++I) {
905     if (++Count > 1)
906       return false;
907   }
908 
909   return true;
910 }
911 
912 // FIXME: Clamp for v_mad_mixhi_f16 handled during isel.
913 bool SIFoldOperands::tryFoldClamp(MachineInstr &MI) {
914   const MachineOperand *ClampSrc = isClamp(MI);
915   if (!ClampSrc || !hasOneNonDBGUseInst(*MRI, ClampSrc->getReg()))
916     return false;
917 
918   MachineInstr *Def = MRI->getVRegDef(ClampSrc->getReg());
919 
920   // The type of clamp must be compatible.
921   if (TII->getClampMask(*Def) != TII->getClampMask(MI))
922     return false;
923 
924   MachineOperand *DefClamp = TII->getNamedOperand(*Def, AMDGPU::OpName::clamp);
925   if (!DefClamp)
926     return false;
927 
928   LLVM_DEBUG(dbgs() << "Folding clamp " << *DefClamp << " into " << *Def
929                     << '\n');
930 
931   // Clamp is applied after omod, so it is OK if omod is set.
932   DefClamp->setImm(1);
933   MRI->replaceRegWith(MI.getOperand(0).getReg(), Def->getOperand(0).getReg());
934   MI.eraseFromParent();
935   return true;
936 }
937 
938 static int getOModValue(unsigned Opc, int64_t Val) {
939   switch (Opc) {
940   case AMDGPU::V_MUL_F32_e64: {
941     switch (static_cast<uint32_t>(Val)) {
942     case 0x3f000000: // 0.5
943       return SIOutMods::DIV2;
944     case 0x40000000: // 2.0
945       return SIOutMods::MUL2;
946     case 0x40800000: // 4.0
947       return SIOutMods::MUL4;
948     default:
949       return SIOutMods::NONE;
950     }
951   }
952   case AMDGPU::V_MUL_F16_e64: {
953     switch (static_cast<uint16_t>(Val)) {
954     case 0x3800: // 0.5
955       return SIOutMods::DIV2;
956     case 0x4000: // 2.0
957       return SIOutMods::MUL2;
958     case 0x4400: // 4.0
959       return SIOutMods::MUL4;
960     default:
961       return SIOutMods::NONE;
962     }
963   }
964   default:
965     llvm_unreachable("invalid mul opcode");
966   }
967 }
968 
969 // FIXME: Does this really not support denormals with f16?
970 // FIXME: Does this need to check IEEE mode bit? SNaNs are generally not
971 // handled, so will anything other than that break?
972 std::pair<const MachineOperand *, int>
973 SIFoldOperands::isOMod(const MachineInstr &MI) const {
974   unsigned Op = MI.getOpcode();
975   switch (Op) {
976   case AMDGPU::V_MUL_F32_e64:
977   case AMDGPU::V_MUL_F16_e64: {
978     // If output denormals are enabled, omod is ignored.
979     if ((Op == AMDGPU::V_MUL_F32_e64 && ST->hasFP32Denormals()) ||
980         (Op == AMDGPU::V_MUL_F16_e64 && ST->hasFP16Denormals()))
981       return std::make_pair(nullptr, SIOutMods::NONE);
982 
983     const MachineOperand *RegOp = nullptr;
984     const MachineOperand *ImmOp = nullptr;
985     const MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
986     const MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
987     if (Src0->isImm()) {
988       ImmOp = Src0;
989       RegOp = Src1;
990     } else if (Src1->isImm()) {
991       ImmOp = Src1;
992       RegOp = Src0;
993     } else
994       return std::make_pair(nullptr, SIOutMods::NONE);
995 
996     int OMod = getOModValue(Op, ImmOp->getImm());
997     if (OMod == SIOutMods::NONE ||
998         TII->hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers) ||
999         TII->hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers) ||
1000         TII->hasModifiersSet(MI, AMDGPU::OpName::omod) ||
1001         TII->hasModifiersSet(MI, AMDGPU::OpName::clamp))
1002       return std::make_pair(nullptr, SIOutMods::NONE);
1003 
1004     return std::make_pair(RegOp, OMod);
1005   }
1006   case AMDGPU::V_ADD_F32_e64:
1007   case AMDGPU::V_ADD_F16_e64: {
1008     // If output denormals are enabled, omod is ignored.
1009     if ((Op == AMDGPU::V_ADD_F32_e64 && ST->hasFP32Denormals()) ||
1010         (Op == AMDGPU::V_ADD_F16_e64 && ST->hasFP16Denormals()))
1011       return std::make_pair(nullptr, SIOutMods::NONE);
1012 
1013     // Look through the DAGCombiner canonicalization fmul x, 2 -> fadd x, x
1014     const MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
1015     const MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
1016 
1017     if (Src0->isReg() && Src1->isReg() && Src0->getReg() == Src1->getReg() &&
1018         Src0->getSubReg() == Src1->getSubReg() &&
1019         !TII->hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers) &&
1020         !TII->hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers) &&
1021         !TII->hasModifiersSet(MI, AMDGPU::OpName::clamp) &&
1022         !TII->hasModifiersSet(MI, AMDGPU::OpName::omod))
1023       return std::make_pair(Src0, SIOutMods::MUL2);
1024 
1025     return std::make_pair(nullptr, SIOutMods::NONE);
1026   }
1027   default:
1028     return std::make_pair(nullptr, SIOutMods::NONE);
1029   }
1030 }
1031 
1032 // FIXME: Does this need to check IEEE bit on function?
1033 bool SIFoldOperands::tryFoldOMod(MachineInstr &MI) {
1034   const MachineOperand *RegOp;
1035   int OMod;
1036   std::tie(RegOp, OMod) = isOMod(MI);
1037   if (OMod == SIOutMods::NONE || !RegOp->isReg() ||
1038       RegOp->getSubReg() != AMDGPU::NoSubRegister ||
1039       !hasOneNonDBGUseInst(*MRI, RegOp->getReg()))
1040     return false;
1041 
1042   MachineInstr *Def = MRI->getVRegDef(RegOp->getReg());
1043   MachineOperand *DefOMod = TII->getNamedOperand(*Def, AMDGPU::OpName::omod);
1044   if (!DefOMod || DefOMod->getImm() != SIOutMods::NONE)
1045     return false;
1046 
1047   // Clamp is applied after omod. If the source already has clamp set, don't
1048   // fold it.
1049   if (TII->hasModifiersSet(*Def, AMDGPU::OpName::clamp))
1050     return false;
1051 
1052   LLVM_DEBUG(dbgs() << "Folding omod " << MI << " into " << *Def << '\n');
1053 
1054   DefOMod->setImm(OMod);
1055   MRI->replaceRegWith(MI.getOperand(0).getReg(), Def->getOperand(0).getReg());
1056   MI.eraseFromParent();
1057   return true;
1058 }
1059 
1060 bool SIFoldOperands::runOnMachineFunction(MachineFunction &MF) {
1061   if (skipFunction(MF.getFunction()))
1062     return false;
1063 
1064   MRI = &MF.getRegInfo();
1065   ST = &MF.getSubtarget<GCNSubtarget>();
1066   TII = ST->getInstrInfo();
1067   TRI = &TII->getRegisterInfo();
1068 
1069   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1070 
1071   // omod is ignored by hardware if IEEE bit is enabled. omod also does not
1072   // correctly handle signed zeros.
1073   //
1074   bool IsIEEEMode = ST->enableIEEEBit(MF);
1075   bool HasNSZ = MFI->hasNoSignedZerosFPMath();
1076 
1077   for (MachineBasicBlock *MBB : depth_first(&MF)) {
1078     MachineBasicBlock::iterator I, Next;
1079     for (I = MBB->begin(); I != MBB->end(); I = Next) {
1080       Next = std::next(I);
1081       MachineInstr &MI = *I;
1082 
1083       tryFoldInst(TII, &MI);
1084 
1085       if (!TII->isFoldableCopy(MI)) {
1086         // TODO: Omod might be OK if there is NSZ only on the source
1087         // instruction, and not the omod multiply.
1088         if (IsIEEEMode || (!HasNSZ && !MI.getFlag(MachineInstr::FmNsz)) ||
1089             !tryFoldOMod(MI))
1090           tryFoldClamp(MI);
1091         continue;
1092       }
1093 
1094       MachineOperand &OpToFold = MI.getOperand(1);
1095       bool FoldingImm = OpToFold.isImm() || OpToFold.isFI();
1096 
1097       // FIXME: We could also be folding things like TargetIndexes.
1098       if (!FoldingImm && !OpToFold.isReg())
1099         continue;
1100 
1101       if (OpToFold.isReg() &&
1102           !TargetRegisterInfo::isVirtualRegister(OpToFold.getReg()))
1103         continue;
1104 
1105       // Prevent folding operands backwards in the function. For example,
1106       // the COPY opcode must not be replaced by 1 in this example:
1107       //
1108       //    %3 = COPY %vgpr0; VGPR_32:%3
1109       //    ...
1110       //    %vgpr0 = V_MOV_B32_e32 1, implicit %exec
1111       MachineOperand &Dst = MI.getOperand(0);
1112       if (Dst.isReg() &&
1113           !TargetRegisterInfo::isVirtualRegister(Dst.getReg()))
1114         continue;
1115 
1116       foldInstOperand(MI, OpToFold);
1117     }
1118   }
1119   return false;
1120 }
1121