LLVM  10.0.0
MachineInstr.cpp
Go to the documentation of this file.
1 //===- lib/CodeGen/MachineInstr.cpp ---------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Methods common to all machine instructions.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/ADT/APFloat.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/FoldingSet.h"
17 #include "llvm/ADT/Hashing.h"
18 #include "llvm/ADT/None.h"
19 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/Analysis/Loads.h"
40 #include "llvm/Config/llvm-config.h"
41 #include "llvm/IR/Constants.h"
43 #include "llvm/IR/DebugLoc.h"
44 #include "llvm/IR/DerivedTypes.h"
45 #include "llvm/IR/Function.h"
46 #include "llvm/IR/InlineAsm.h"
47 #include "llvm/IR/InstrTypes.h"
48 #include "llvm/IR/Intrinsics.h"
49 #include "llvm/IR/LLVMContext.h"
50 #include "llvm/IR/Metadata.h"
51 #include "llvm/IR/Module.h"
53 #include "llvm/IR/Operator.h"
54 #include "llvm/IR/Type.h"
55 #include "llvm/IR/Value.h"
56 #include "llvm/MC/MCInstrDesc.h"
57 #include "llvm/MC/MCRegisterInfo.h"
58 #include "llvm/MC/MCSymbol.h"
59 #include "llvm/Support/Casting.h"
61 #include "llvm/Support/Compiler.h"
62 #include "llvm/Support/Debug.h"
69 #include <algorithm>
70 #include <cassert>
71 #include <cstddef>
72 #include <cstdint>
73 #include <cstring>
74 #include <iterator>
75 #include <utility>
76 
77 using namespace llvm;
78 
80  if (const MachineBasicBlock *MBB = MI.getParent())
81  if (const MachineFunction *MF = MBB->getParent())
82  return MF;
83  return nullptr;
84 }
85 
86 // Try to crawl up to the machine function and get TRI and IntrinsicInfo from
87 // it.
88 static void tryToGetTargetInfo(const MachineInstr &MI,
89  const TargetRegisterInfo *&TRI,
90  const MachineRegisterInfo *&MRI,
91  const TargetIntrinsicInfo *&IntrinsicInfo,
92  const TargetInstrInfo *&TII) {
93 
94  if (const MachineFunction *MF = getMFIfAvailable(MI)) {
95  TRI = MF->getSubtarget().getRegisterInfo();
96  MRI = &MF->getRegInfo();
97  IntrinsicInfo = MF->getTarget().getIntrinsicInfo();
98  TII = MF->getSubtarget().getInstrInfo();
99  }
100 }
101 
103  if (MCID->ImplicitDefs)
104  for (const MCPhysReg *ImpDefs = MCID->getImplicitDefs(); *ImpDefs;
105  ++ImpDefs)
106  addOperand(MF, MachineOperand::CreateReg(*ImpDefs, true, true));
107  if (MCID->ImplicitUses)
108  for (const MCPhysReg *ImpUses = MCID->getImplicitUses(); *ImpUses;
109  ++ImpUses)
110  addOperand(MF, MachineOperand::CreateReg(*ImpUses, false, true));
111 }
112 
113 /// MachineInstr ctor - This constructor creates a MachineInstr and adds the
114 /// implicit operands. It reserves space for the number of operands specified by
115 /// the MCInstrDesc.
116 MachineInstr::MachineInstr(MachineFunction &MF, const MCInstrDesc &tid,
117  DebugLoc dl, bool NoImp)
118  : MCID(&tid), debugLoc(std::move(dl)) {
119  assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
120 
121  // Reserve space for the expected number of operands.
122  if (unsigned NumOps = MCID->getNumOperands() +
123  MCID->getNumImplicitDefs() + MCID->getNumImplicitUses()) {
124  CapOperands = OperandCapacity::get(NumOps);
125  Operands = MF.allocateOperandArray(CapOperands);
126  }
127 
128  if (!NoImp)
130 }
131 
132 /// MachineInstr ctor - Copies MachineInstr arg exactly
133 ///
134 MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
135  : MCID(&MI.getDesc()), Info(MI.Info), debugLoc(MI.getDebugLoc()) {
136  assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
137 
138  CapOperands = OperandCapacity::get(MI.getNumOperands());
139  Operands = MF.allocateOperandArray(CapOperands);
140 
141  // Copy operands.
142  for (const MachineOperand &MO : MI.operands())
143  addOperand(MF, MO);
144 
145  // Copy all the sensible flags.
146  setFlags(MI.Flags);
147 }
148 
149 /// getRegInfo - If this instruction is embedded into a MachineFunction,
150 /// return the MachineRegisterInfo object for the current function, otherwise
151 /// return null.
152 MachineRegisterInfo *MachineInstr::getRegInfo() {
153  if (MachineBasicBlock *MBB = getParent())
154  return &MBB->getParent()->getRegInfo();
155  return nullptr;
156 }
157 
158 /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
159 /// this instruction from their respective use lists. This requires that the
160 /// operands already be on their use lists.
161 void MachineInstr::RemoveRegOperandsFromUseLists(MachineRegisterInfo &MRI) {
162  for (MachineOperand &MO : operands())
163  if (MO.isReg())
165 }
166 
167 /// AddRegOperandsToUseLists - Add all of the register operands in
168 /// this instruction from their respective use lists. This requires that the
169 /// operands not be on their use lists yet.
170 void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &MRI) {
171  for (MachineOperand &MO : operands())
172  if (MO.isReg())
173  MRI.addRegOperandToUseList(&MO);
174 }
175 
177  MachineBasicBlock *MBB = getParent();
178  assert(MBB && "Use MachineInstrBuilder to add operands to dangling instrs");
179  MachineFunction *MF = MBB->getParent();
180  assert(MF && "Use MachineInstrBuilder to add operands to dangling instrs");
181  addOperand(*MF, Op);
182 }
183 
184 /// Move NumOps MachineOperands from Src to Dst, with support for overlapping
185 /// ranges. If MRI is non-null also update use-def chains.
187  unsigned NumOps, MachineRegisterInfo *MRI) {
188  if (MRI)
189  return MRI->moveOperands(Dst, Src, NumOps);
190  // MachineOperand is a trivially copyable type so we can just use memmove.
191  assert(Dst && Src && "Unknown operands");
192  std::memmove(Dst, Src, NumOps * sizeof(MachineOperand));
193 }
194 
195 /// addOperand - Add the specified operand to the instruction. If it is an
196 /// implicit operand, it is added to the end of the operand list. If it is
197 /// an explicit operand it is added at the end of the explicit operand list
198 /// (before the first implicit operand).
200  assert(MCID && "Cannot add operands before providing an instr descriptor");
201 
202  // Check if we're adding one of our existing operands.
203  if (&Op >= Operands && &Op < Operands + NumOperands) {
204  // This is unusual: MI->addOperand(MI->getOperand(i)).
205  // If adding Op requires reallocating or moving existing operands around,
206  // the Op reference could go stale. Support it by copying Op.
207  MachineOperand CopyOp(Op);
208  return addOperand(MF, CopyOp);
209  }
210 
211  // Find the insert location for the new operand. Implicit registers go at
212  // the end, everything else goes before the implicit regs.
213  //
214  // FIXME: Allow mixed explicit and implicit operands on inline asm.
215  // InstrEmitter::EmitSpecialNode() is marking inline asm clobbers as
216  // implicit-defs, but they must not be moved around. See the FIXME in
217  // InstrEmitter.cpp.
218  unsigned OpNo = getNumOperands();
219  bool isImpReg = Op.isReg() && Op.isImplicit();
220  if (!isImpReg && !isInlineAsm()) {
221  while (OpNo && Operands[OpNo-1].isReg() && Operands[OpNo-1].isImplicit()) {
222  --OpNo;
223  assert(!Operands[OpNo].isTied() && "Cannot move tied operands");
224  }
225  }
226 
227 #ifndef NDEBUG
228  bool isDebugOp = Op.getType() == MachineOperand::MO_Metadata ||
230  // OpNo now points as the desired insertion point. Unless this is a variadic
231  // instruction, only implicit regs are allowed beyond MCID->getNumOperands().
232  // RegMask operands go between the explicit and implicit operands.
233  assert((isImpReg || Op.isRegMask() || MCID->isVariadic() ||
234  OpNo < MCID->getNumOperands() || isDebugOp) &&
235  "Trying to add an operand to a machine instr that is already done!");
236 #endif
237 
238  MachineRegisterInfo *MRI = getRegInfo();
239 
240  // Determine if the Operands array needs to be reallocated.
241  // Save the old capacity and operand array.
242  OperandCapacity OldCap = CapOperands;
243  MachineOperand *OldOperands = Operands;
244  if (!OldOperands || OldCap.getSize() == getNumOperands()) {
245  CapOperands = OldOperands ? OldCap.getNext() : OldCap.get(1);
246  Operands = MF.allocateOperandArray(CapOperands);
247  // Move the operands before the insertion point.
248  if (OpNo)
249  moveOperands(Operands, OldOperands, OpNo, MRI);
250  }
251 
252  // Move the operands following the insertion point.
253  if (OpNo != NumOperands)
254  moveOperands(Operands + OpNo + 1, OldOperands + OpNo, NumOperands - OpNo,
255  MRI);
256  ++NumOperands;
257 
258  // Deallocate the old operand array.
259  if (OldOperands != Operands && OldOperands)
260  MF.deallocateOperandArray(OldCap, OldOperands);
261 
262  // Copy Op into place. It still needs to be inserted into the MRI use lists.
263  MachineOperand *NewMO = new (Operands + OpNo) MachineOperand(Op);
264  NewMO->ParentMI = this;
265 
266  // When adding a register operand, tell MRI about it.
267  if (NewMO->isReg()) {
268  // Ensure isOnRegUseList() returns false, regardless of Op's status.
269  NewMO->Contents.Reg.Prev = nullptr;
270  // Ignore existing ties. This is not a property that can be copied.
271  NewMO->TiedTo = 0;
272  // Add the new operand to MRI, but only for instructions in an MBB.
273  if (MRI)
274  MRI->addRegOperandToUseList(NewMO);
275  // The MCID operand information isn't accurate until we start adding
276  // explicit operands. The implicit operands are added first, then the
277  // explicits are inserted before them.
278  if (!isImpReg) {
279  // Tie uses to defs as indicated in MCInstrDesc.
280  if (NewMO->isUse()) {
281  int DefIdx = MCID->getOperandConstraint(OpNo, MCOI::TIED_TO);
282  if (DefIdx != -1)
283  tieOperands(DefIdx, OpNo);
284  }
285  // If the register operand is flagged as early, mark the operand as such.
286  if (MCID->getOperandConstraint(OpNo, MCOI::EARLY_CLOBBER) != -1)
287  NewMO->setIsEarlyClobber(true);
288  }
289  }
290 }
291 
292 /// RemoveOperand - Erase an operand from an instruction, leaving it with one
293 /// fewer operand than it started with.
294 ///
295 void MachineInstr::RemoveOperand(unsigned OpNo) {
296  assert(OpNo < getNumOperands() && "Invalid operand number");
297  untieRegOperand(OpNo);
298 
299 #ifndef NDEBUG
300  // Moving tied operands would break the ties.
301  for (unsigned i = OpNo + 1, e = getNumOperands(); i != e; ++i)
302  if (Operands[i].isReg())
303  assert(!Operands[i].isTied() && "Cannot move tied operands");
304 #endif
305 
306  MachineRegisterInfo *MRI = getRegInfo();
307  if (MRI && Operands[OpNo].isReg())
308  MRI->removeRegOperandFromUseList(Operands + OpNo);
309 
310  // Don't call the MachineOperand destructor. A lot of this code depends on
311  // MachineOperand having a trivial destructor anyway, and adding a call here
312  // wouldn't make it 'destructor-correct'.
313 
314  if (unsigned N = NumOperands - 1 - OpNo)
315  moveOperands(Operands + OpNo, Operands + OpNo + 1, N, MRI);
316  --NumOperands;
317 }
318 
319 void MachineInstr::setExtraInfo(MachineFunction &MF,
321  MCSymbol *PreInstrSymbol,
322  MCSymbol *PostInstrSymbol,
323  MDNode *HeapAllocMarker) {
324  bool HasPreInstrSymbol = PreInstrSymbol != nullptr;
325  bool HasPostInstrSymbol = PostInstrSymbol != nullptr;
326  bool HasHeapAllocMarker = HeapAllocMarker != nullptr;
327  int NumPointers =
328  MMOs.size() + HasPreInstrSymbol + HasPostInstrSymbol + HasHeapAllocMarker;
329 
330  // Drop all extra info if there is none.
331  if (NumPointers <= 0) {
332  Info.clear();
333  return;
334  }
335 
336  // If more than one pointer, then store out of line. Store heap alloc markers
337  // out of line because PointerSumType cannot hold more than 4 tag types with
338  // 32-bit pointers.
339  // FIXME: Maybe we should make the symbols in the extra info mutable?
340  else if (NumPointers > 1 || HasHeapAllocMarker) {
341  Info.set<EIIK_OutOfLine>(MF.createMIExtraInfo(
342  MMOs, PreInstrSymbol, PostInstrSymbol, HeapAllocMarker));
343  return;
344  }
345 
346  // Otherwise store the single pointer inline.
347  if (HasPreInstrSymbol)
348  Info.set<EIIK_PreInstrSymbol>(PreInstrSymbol);
349  else if (HasPostInstrSymbol)
350  Info.set<EIIK_PostInstrSymbol>(PostInstrSymbol);
351  else
352  Info.set<EIIK_MMO>(MMOs[0]);
353 }
354 
356  if (memoperands_empty())
357  return;
358 
359  setExtraInfo(MF, {}, getPreInstrSymbol(), getPostInstrSymbol(),
361 }
362 
365  if (MMOs.empty()) {
366  dropMemRefs(MF);
367  return;
368  }
369 
370  setExtraInfo(MF, MMOs, getPreInstrSymbol(), getPostInstrSymbol(),
372 }
373 
375  MachineMemOperand *MO) {
378  MMOs.push_back(MO);
379  setMemRefs(MF, MMOs);
380 }
381 
383  if (this == &MI)
384  // Nothing to do for a self-clone!
385  return;
386 
387  assert(&MF == MI.getMF() &&
388  "Invalid machine functions when cloning memory refrences!");
389  // See if we can just steal the extra info already allocated for the
390  // instruction. We can do this whenever the pre- and post-instruction symbols
391  // are the same (including null).
392  if (getPreInstrSymbol() == MI.getPreInstrSymbol() &&
395  Info = MI.Info;
396  return;
397  }
398 
399  // Otherwise, fall back on a copy-based clone.
400  setMemRefs(MF, MI.memoperands());
401 }
402 
403 /// Check to see if the MMOs pointed to by the two MemRefs arrays are
404 /// identical.
407  if (LHS.size() != RHS.size())
408  return false;
409 
410  auto LHSPointees = make_pointee_range(LHS);
411  auto RHSPointees = make_pointee_range(RHS);
412  return std::equal(LHSPointees.begin(), LHSPointees.end(),
413  RHSPointees.begin());
414 }
415 
418  // Try handling easy numbers of MIs with simpler mechanisms.
419  if (MIs.empty()) {
420  dropMemRefs(MF);
421  return;
422  }
423  if (MIs.size() == 1) {
424  cloneMemRefs(MF, *MIs[0]);
425  return;
426  }
427  // Because an empty memoperands list provides *no* information and must be
428  // handled conservatively (assuming the instruction can do anything), the only
429  // way to merge with it is to drop all other memoperands.
430  if (MIs[0]->memoperands_empty()) {
431  dropMemRefs(MF);
432  return;
433  }
434 
435  // Handle the general case.
437  // Start with the first instruction.
438  assert(&MF == MIs[0]->getMF() &&
439  "Invalid machine functions when cloning memory references!");
440  MergedMMOs.append(MIs[0]->memoperands_begin(), MIs[0]->memoperands_end());
441  // Now walk all the other instructions and accumulate any different MMOs.
442  for (const MachineInstr &MI : make_pointee_range(MIs.slice(1))) {
443  assert(&MF == MI.getMF() &&
444  "Invalid machine functions when cloning memory references!");
445 
446  // Skip MIs with identical operands to the first. This is a somewhat
447  // arbitrary hack but will catch common cases without being quadratic.
448  // TODO: We could fully implement merge semantics here if needed.
449  if (hasIdenticalMMOs(MIs[0]->memoperands(), MI.memoperands()))
450  continue;
451 
452  // Because an empty memoperands list provides *no* information and must be
453  // handled conservatively (assuming the instruction can do anything), the
454  // only way to merge with it is to drop all other memoperands.
455  if (MI.memoperands_empty()) {
456  dropMemRefs(MF);
457  return;
458  }
459 
460  // Otherwise accumulate these into our temporary buffer of the merged state.
461  MergedMMOs.append(MI.memoperands_begin(), MI.memoperands_end());
462  }
463 
464  setMemRefs(MF, MergedMMOs);
465 }
466 
468  // Do nothing if old and new symbols are the same.
469  if (Symbol == getPreInstrSymbol())
470  return;
471 
472  // If there was only one symbol and we're removing it, just clear info.
473  if (!Symbol && Info.is<EIIK_PreInstrSymbol>()) {
474  Info.clear();
475  return;
476  }
477 
478  setExtraInfo(MF, memoperands(), Symbol, getPostInstrSymbol(),
480 }
481 
483  // Do nothing if old and new symbols are the same.
484  if (Symbol == getPostInstrSymbol())
485  return;
486 
487  // If there was only one symbol and we're removing it, just clear info.
488  if (!Symbol && Info.is<EIIK_PostInstrSymbol>()) {
489  Info.clear();
490  return;
491  }
492 
493  setExtraInfo(MF, memoperands(), getPreInstrSymbol(), Symbol,
495 }
496 
498  // Do nothing if old and new symbols are the same.
499  if (Marker == getHeapAllocMarker())
500  return;
501 
502  setExtraInfo(MF, memoperands(), getPreInstrSymbol(), getPostInstrSymbol(),
503  Marker);
504 }
505 
507  const MachineInstr &MI) {
508  if (this == &MI)
509  // Nothing to do for a self-clone!
510  return;
511 
512  assert(&MF == MI.getMF() &&
513  "Invalid machine functions when cloning instruction symbols!");
514 
518 }
519 
521  // For now, the just return the union of the flags. If the flags get more
522  // complicated over time, we might need more logic here.
523  return getFlags() | Other.getFlags();
524 }
525 
527  uint16_t MIFlags = 0;
528  // Copy the wrapping flags.
529  if (const OverflowingBinaryOperator *OB =
530  dyn_cast<OverflowingBinaryOperator>(&I)) {
531  if (OB->hasNoSignedWrap())
532  MIFlags |= MachineInstr::MIFlag::NoSWrap;
533  if (OB->hasNoUnsignedWrap())
534  MIFlags |= MachineInstr::MIFlag::NoUWrap;
535  }
536 
537  // Copy the exact flag.
538  if (const PossiblyExactOperator *PE = dyn_cast<PossiblyExactOperator>(&I))
539  if (PE->isExact())
540  MIFlags |= MachineInstr::MIFlag::IsExact;
541 
542  // Copy the fast-math flags.
543  if (const FPMathOperator *FP = dyn_cast<FPMathOperator>(&I)) {
544  const FastMathFlags Flags = FP->getFastMathFlags();
545  if (Flags.noNaNs())
546  MIFlags |= MachineInstr::MIFlag::FmNoNans;
547  if (Flags.noInfs())
548  MIFlags |= MachineInstr::MIFlag::FmNoInfs;
549  if (Flags.noSignedZeros())
550  MIFlags |= MachineInstr::MIFlag::FmNsz;
551  if (Flags.allowReciprocal())
552  MIFlags |= MachineInstr::MIFlag::FmArcp;
553  if (Flags.allowContract())
554  MIFlags |= MachineInstr::MIFlag::FmContract;
555  if (Flags.approxFunc())
556  MIFlags |= MachineInstr::MIFlag::FmAfn;
557  if (Flags.allowReassoc())
558  MIFlags |= MachineInstr::MIFlag::FmReassoc;
559  }
560 
561  return MIFlags;
562 }
563 
565  Flags = copyFlagsFromInstruction(I);
566 }
567 
568 bool MachineInstr::hasPropertyInBundle(uint64_t Mask, QueryType Type) const {
569  assert(!isBundledWithPred() && "Must be called on bundle header");
571  if (MII->getDesc().getFlags() & Mask) {
572  if (Type == AnyInBundle)
573  return true;
574  } else {
575  if (Type == AllInBundle && !MII->isBundle())
576  return false;
577  }
578  // This was the last instruction in the bundle.
579  if (!MII->isBundledWithSucc())
580  return Type == AllInBundle;
581  }
582 }
583 
585  MICheckType Check) const {
586  // If opcodes or number of operands are not the same then the two
587  // instructions are obviously not identical.
588  if (Other.getOpcode() != getOpcode() ||
589  Other.getNumOperands() != getNumOperands())
590  return false;
591 
592  if (isBundle()) {
593  // We have passed the test above that both instructions have the same
594  // opcode, so we know that both instructions are bundles here. Let's compare
595  // MIs inside the bundle.
596  assert(Other.isBundle() && "Expected that both instructions are bundles.");
599  // Loop until we analysed the last intruction inside at least one of the
600  // bundles.
601  while (I1->isBundledWithSucc() && I2->isBundledWithSucc()) {
602  ++I1;
603  ++I2;
604  if (!I1->isIdenticalTo(*I2, Check))
605  return false;
606  }
607  // If we've reached the end of just one of the two bundles, but not both,
608  // the instructions are not identical.
609  if (I1->isBundledWithSucc() || I2->isBundledWithSucc())
610  return false;
611  }
612 
613  // Check operands to make sure they match.
614  for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
615  const MachineOperand &MO = getOperand(i);
616  const MachineOperand &OMO = Other.getOperand(i);
617  if (!MO.isReg()) {
618  if (!MO.isIdenticalTo(OMO))
619  return false;
620  continue;
621  }
622 
623  // Clients may or may not want to ignore defs when testing for equality.
624  // For example, machine CSE pass only cares about finding common
625  // subexpressions, so it's safe to ignore virtual register defs.
626  if (MO.isDef()) {
627  if (Check == IgnoreDefs)
628  continue;
629  else if (Check == IgnoreVRegDefs) {
630  if (!Register::isVirtualRegister(MO.getReg()) ||
632  if (!MO.isIdenticalTo(OMO))
633  return false;
634  } else {
635  if (!MO.isIdenticalTo(OMO))
636  return false;
637  if (Check == CheckKillDead && MO.isDead() != OMO.isDead())
638  return false;
639  }
640  } else {
641  if (!MO.isIdenticalTo(OMO))
642  return false;
643  if (Check == CheckKillDead && MO.isKill() != OMO.isKill())
644  return false;
645  }
646  }
647  // If DebugLoc does not match then two debug instructions are not identical.
648  if (isDebugInstr())
649  if (getDebugLoc() && Other.getDebugLoc() &&
650  getDebugLoc() != Other.getDebugLoc())
651  return false;
652  return true;
653 }
654 
656  return getParent()->getParent();
657 }
658 
660  assert(getParent() && "Not embedded in a basic block!");
661  return getParent()->remove(this);
662 }
663 
665  assert(getParent() && "Not embedded in a basic block!");
666  return getParent()->remove_instr(this);
667 }
668 
670  assert(getParent() && "Not embedded in a basic block!");
671  getParent()->erase(this);
672 }
673 
675  assert(getParent() && "Not embedded in a basic block!");
676  MachineBasicBlock *MBB = getParent();
677  MachineFunction *MF = MBB->getParent();
678  assert(MF && "Not embedded in a function!");
679 
680  MachineInstr *MI = (MachineInstr *)this;
681  MachineRegisterInfo &MRI = MF->getRegInfo();
682 
683  for (const MachineOperand &MO : MI->operands()) {
684  if (!MO.isReg() || !MO.isDef())
685  continue;
686  Register Reg = MO.getReg();
687  if (!Reg.isVirtual())
688  continue;
690  }
691  MI->eraseFromParent();
692 }
693 
695  assert(getParent() && "Not embedded in a basic block!");
696  getParent()->erase_instr(this);
697 }
698 
700  unsigned NumOperands = MCID->getNumOperands();
701  if (!MCID->isVariadic())
702  return NumOperands;
703 
704  for (unsigned I = NumOperands, E = getNumOperands(); I != E; ++I) {
705  const MachineOperand &MO = getOperand(I);
706  // The operands must always be in the following order:
707  // - explicit reg defs,
708  // - other explicit operands (reg uses, immediates, etc.),
709  // - implicit reg defs
710  // - implicit reg uses
711  if (MO.isReg() && MO.isImplicit())
712  break;
713  ++NumOperands;
714  }
715  return NumOperands;
716 }
717 
719  unsigned NumDefs = MCID->getNumDefs();
720  if (!MCID->isVariadic())
721  return NumDefs;
722 
723  for (unsigned I = NumDefs, E = getNumOperands(); I != E; ++I) {
724  const MachineOperand &MO = getOperand(I);
725  if (!MO.isReg() || !MO.isDef() || MO.isImplicit())
726  break;
727  ++NumDefs;
728  }
729  return NumDefs;
730 }
731 
733  assert(!isBundledWithPred() && "MI is already bundled with its predecessor");
736  --Pred;
737  assert(!Pred->isBundledWithSucc() && "Inconsistent bundle flags");
738  Pred->setFlag(BundledSucc);
739 }
740 
742  assert(!isBundledWithSucc() && "MI is already bundled with its successor");
745  ++Succ;
746  assert(!Succ->isBundledWithPred() && "Inconsistent bundle flags");
747  Succ->setFlag(BundledPred);
748 }
749 
751  assert(isBundledWithPred() && "MI isn't bundled with its predecessor");
754  --Pred;
755  assert(Pred->isBundledWithSucc() && "Inconsistent bundle flags");
756  Pred->clearFlag(BundledSucc);
757 }
758 
760  assert(isBundledWithSucc() && "MI isn't bundled with its successor");
763  ++Succ;
764  assert(Succ->isBundledWithPred() && "Inconsistent bundle flags");
765  Succ->clearFlag(BundledPred);
766 }
767 
769  if (isInlineAsm()) {
770  unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
771  if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
772  return true;
773  }
774  return false;
775 }
776 
778  assert(isInlineAsm() && "getInlineAsmDialect() only works for inline asms!");
779  unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
780  return InlineAsm::AsmDialect((ExtraInfo & InlineAsm::Extra_AsmDialect) != 0);
781 }
782 
784  unsigned *GroupNo) const {
785  assert(isInlineAsm() && "Expected an inline asm instruction");
786  assert(OpIdx < getNumOperands() && "OpIdx out of range");
787 
788  // Ignore queries about the initial operands.
789  if (OpIdx < InlineAsm::MIOp_FirstOperand)
790  return -1;
791 
792  unsigned Group = 0;
793  unsigned NumOps;
794  for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
795  i += NumOps) {
796  const MachineOperand &FlagMO = getOperand(i);
797  // If we reach the implicit register operands, stop looking.
798  if (!FlagMO.isImm())
799  return -1;
800  NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
801  if (i + NumOps > OpIdx) {
802  if (GroupNo)
803  *GroupNo = Group;
804  return i;
805  }
806  ++Group;
807  }
808  return -1;
809 }
810 
812  assert(isDebugLabel() && "not a DBG_LABEL");
813  return cast<DILabel>(getOperand(0).getMetadata());
814 }
815 
817  assert(isDebugValue() && "not a DBG_VALUE");
818  return cast<DILocalVariable>(getOperand(2).getMetadata());
819 }
820 
822  assert(isDebugValue() && "not a DBG_VALUE");
823  return cast<DIExpression>(getOperand(3).getMetadata());
824 }
825 
828 }
829 
830 const TargetRegisterClass*
832  const TargetInstrInfo *TII,
833  const TargetRegisterInfo *TRI) const {
834  assert(getParent() && "Can't have an MBB reference here!");
835  assert(getMF() && "Can't have an MF reference here!");
836  const MachineFunction &MF = *getMF();
837 
838  // Most opcodes have fixed constraints in their MCInstrDesc.
839  if (!isInlineAsm())
840  return TII->getRegClass(getDesc(), OpIdx, TRI, MF);
841 
842  if (!getOperand(OpIdx).isReg())
843  return nullptr;
844 
845  // For tied uses on inline asm, get the constraint from the def.
846  unsigned DefIdx;
847  if (getOperand(OpIdx).isUse() && isRegTiedToDefOperand(OpIdx, &DefIdx))
848  OpIdx = DefIdx;
849 
850  // Inline asm stores register class constraints in the flag word.
851  int FlagIdx = findInlineAsmFlagIdx(OpIdx);
852  if (FlagIdx < 0)
853  return nullptr;
854 
855  unsigned Flag = getOperand(FlagIdx).getImm();
856  unsigned RCID;
861  return TRI->getRegClass(RCID);
862 
863  // Assume that all registers in a memory operand are pointers.
865  return TRI->getPointerRegClass(MF);
866 
867  return nullptr;
868 }
869 
871  Register Reg, const TargetRegisterClass *CurRC, const TargetInstrInfo *TII,
872  const TargetRegisterInfo *TRI, bool ExploreBundle) const {
873  // Check every operands inside the bundle if we have
874  // been asked to.
875  if (ExploreBundle)
876  for (ConstMIBundleOperands OpndIt(*this); OpndIt.isValid() && CurRC;
877  ++OpndIt)
878  CurRC = OpndIt->getParent()->getRegClassConstraintEffectForVRegImpl(
879  OpndIt.getOperandNo(), Reg, CurRC, TII, TRI);
880  else
881  // Otherwise, just check the current operands.
882  for (unsigned i = 0, e = NumOperands; i < e && CurRC; ++i)
883  CurRC = getRegClassConstraintEffectForVRegImpl(i, Reg, CurRC, TII, TRI);
884  return CurRC;
885 }
886 
887 const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVRegImpl(
888  unsigned OpIdx, Register Reg, const TargetRegisterClass *CurRC,
889  const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
890  assert(CurRC && "Invalid initial register class");
891  // Check if Reg is constrained by some of its use/def from MI.
892  const MachineOperand &MO = getOperand(OpIdx);
893  if (!MO.isReg() || MO.getReg() != Reg)
894  return CurRC;
895  // If yes, accumulate the constraints through the operand.
896  return getRegClassConstraintEffect(OpIdx, CurRC, TII, TRI);
897 }
898 
900  unsigned OpIdx, const TargetRegisterClass *CurRC,
901  const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
902  const TargetRegisterClass *OpRC = getRegClassConstraint(OpIdx, TII, TRI);
903  const MachineOperand &MO = getOperand(OpIdx);
904  assert(MO.isReg() &&
905  "Cannot get register constraints for non-register operand");
906  assert(CurRC && "Invalid initial register class");
907  if (unsigned SubIdx = MO.getSubReg()) {
908  if (OpRC)
909  CurRC = TRI->getMatchingSuperRegClass(CurRC, OpRC, SubIdx);
910  else
911  CurRC = TRI->getSubClassWithSubReg(CurRC, SubIdx);
912  } else if (OpRC)
913  CurRC = TRI->getCommonSubClass(CurRC, OpRC);
914  return CurRC;
915 }
916 
917 /// Return the number of instructions inside the MI bundle, not counting the
918 /// header instruction.
919 unsigned MachineInstr::getBundleSize() const {
921  unsigned Size = 0;
922  while (I->isBundledWithSucc()) {
923  ++Size;
924  ++I;
925  }
926  return Size;
927 }
928 
929 /// Returns true if the MachineInstr has an implicit-use operand of exactly
930 /// the given register (not considering sub/super-registers).
932  for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
933  const MachineOperand &MO = getOperand(i);
934  if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == Reg)
935  return true;
936  }
937  return false;
938 }
939 
940 /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
941 /// the specific register or -1 if it is not found. It further tightens
942 /// the search criteria to a use that kills the register if isKill is true.
944  Register Reg, bool isKill, const TargetRegisterInfo *TRI) const {
945  for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
946  const MachineOperand &MO = getOperand(i);
947  if (!MO.isReg() || !MO.isUse())
948  continue;
949  Register MOReg = MO.getReg();
950  if (!MOReg)
951  continue;
952  if (MOReg == Reg || (TRI && Reg && MOReg && TRI->regsOverlap(MOReg, Reg)))
953  if (!isKill || MO.isKill())
954  return i;
955  }
956  return -1;
957 }
958 
959 /// readsWritesVirtualRegister - Return a pair of bools (reads, writes)
960 /// indicating if this instruction reads or writes Reg. This also considers
961 /// partial defines.
962 std::pair<bool,bool>
964  SmallVectorImpl<unsigned> *Ops) const {
965  bool PartDef = false; // Partial redefine.
966  bool FullDef = false; // Full define.
967  bool Use = false;
968 
969  for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
970  const MachineOperand &MO = getOperand(i);
971  if (!MO.isReg() || MO.getReg() != Reg)
972  continue;
973  if (Ops)
974  Ops->push_back(i);
975  if (MO.isUse())
976  Use |= !MO.isUndef();
977  else if (MO.getSubReg() && !MO.isUndef())
978  // A partial def undef doesn't count as reading the register.
979  PartDef = true;
980  else
981  FullDef = true;
982  }
983  // A partial redefine uses Reg unless there is also a full define.
984  return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef);
985 }
986 
987 /// findRegisterDefOperandIdx() - Returns the operand index that is a def of
988 /// the specified register or -1 if it is not found. If isDead is true, defs
989 /// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
990 /// also checks if there is a def of a super-register.
991 int
992 MachineInstr::findRegisterDefOperandIdx(Register Reg, bool isDead, bool Overlap,
993  const TargetRegisterInfo *TRI) const {
994  bool isPhys = Register::isPhysicalRegister(Reg);
995  for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
996  const MachineOperand &MO = getOperand(i);
997  // Accept regmask operands when Overlap is set.
998  // Ignore them when looking for a specific def operand (Overlap == false).
999  if (isPhys && Overlap && MO.isRegMask() && MO.clobbersPhysReg(Reg))
1000  return i;
1001  if (!MO.isReg() || !MO.isDef())
1002  continue;
1003  Register MOReg = MO.getReg();
1004  bool Found = (MOReg == Reg);
1005  if (!Found && TRI && isPhys && Register::isPhysicalRegister(MOReg)) {
1006  if (Overlap)
1007  Found = TRI->regsOverlap(MOReg, Reg);
1008  else
1009  Found = TRI->isSubRegister(MOReg, Reg);
1010  }
1011  if (Found && (!isDead || MO.isDead()))
1012  return i;
1013  }
1014  return -1;
1015 }
1016 
1017 /// findFirstPredOperandIdx() - Find the index of the first operand in the
1018 /// operand list that is used to represent the predicate. It returns -1 if
1019 /// none is found.
1021  // Don't call MCID.findFirstPredOperandIdx() because this variant
1022  // is sometimes called on an instruction that's not yet complete, and
1023  // so the number of operands is less than the MCID indicates. In
1024  // particular, the PTX target does this.
1025  const MCInstrDesc &MCID = getDesc();
1026  if (MCID.isPredicable()) {
1027  for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1028  if (MCID.OpInfo[i].isPredicate())
1029  return i;
1030  }
1031 
1032  return -1;
1033 }
1034 
1035 // MachineOperand::TiedTo is 4 bits wide.
1036 const unsigned TiedMax = 15;
1037 
1038 /// tieOperands - Mark operands at DefIdx and UseIdx as tied to each other.
1039 ///
1040 /// Use and def operands can be tied together, indicated by a non-zero TiedTo
1041 /// field. TiedTo can have these values:
1042 ///
1043 /// 0: Operand is not tied to anything.
1044 /// 1 to TiedMax-1: Tied to getOperand(TiedTo-1).
1045 /// TiedMax: Tied to an operand >= TiedMax-1.
1046 ///
1047 /// The tied def must be one of the first TiedMax operands on a normal
1048 /// instruction. INLINEASM instructions allow more tied defs.
1049 ///
1050 void MachineInstr::tieOperands(unsigned DefIdx, unsigned UseIdx) {
1051  MachineOperand &DefMO = getOperand(DefIdx);
1052  MachineOperand &UseMO = getOperand(UseIdx);
1053  assert(DefMO.isDef() && "DefIdx must be a def operand");
1054  assert(UseMO.isUse() && "UseIdx must be a use operand");
1055  assert(!DefMO.isTied() && "Def is already tied to another use");
1056  assert(!UseMO.isTied() && "Use is already tied to another def");
1057 
1058  if (DefIdx < TiedMax)
1059  UseMO.TiedTo = DefIdx + 1;
1060  else {
1061  // Inline asm can use the group descriptors to find tied operands, but on
1062  // normal instruction, the tied def must be within the first TiedMax
1063  // operands.
1064  assert(isInlineAsm() && "DefIdx out of range");
1065  UseMO.TiedTo = TiedMax;
1066  }
1067 
1068  // UseIdx can be out of range, we'll search for it in findTiedOperandIdx().
1069  DefMO.TiedTo = std::min(UseIdx + 1, TiedMax);
1070 }
1071 
1072 /// Given the index of a tied register operand, find the operand it is tied to.
1073 /// Defs are tied to uses and vice versa. Returns the index of the tied operand
1074 /// which must exist.
1075 unsigned MachineInstr::findTiedOperandIdx(unsigned OpIdx) const {
1076  const MachineOperand &MO = getOperand(OpIdx);
1077  assert(MO.isTied() && "Operand isn't tied");
1078 
1079  // Normally TiedTo is in range.
1080  if (MO.TiedTo < TiedMax)
1081  return MO.TiedTo - 1;
1082 
1083  // Uses on normal instructions can be out of range.
1084  if (!isInlineAsm()) {
1085  // Normal tied defs must be in the 0..TiedMax-1 range.
1086  if (MO.isUse())
1087  return TiedMax - 1;
1088  // MO is a def. Search for the tied use.
1089  for (unsigned i = TiedMax - 1, e = getNumOperands(); i != e; ++i) {
1090  const MachineOperand &UseMO = getOperand(i);
1091  if (UseMO.isReg() && UseMO.isUse() && UseMO.TiedTo == OpIdx + 1)
1092  return i;
1093  }
1094  llvm_unreachable("Can't find tied use");
1095  }
1096 
1097  // Now deal with inline asm by parsing the operand group descriptor flags.
1098  // Find the beginning of each operand group.
1099  SmallVector<unsigned, 8> GroupIdx;
1100  unsigned OpIdxGroup = ~0u;
1101  unsigned NumOps;
1102  for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
1103  i += NumOps) {
1104  const MachineOperand &FlagMO = getOperand(i);
1105  assert(FlagMO.isImm() && "Invalid tied operand on inline asm");
1106  unsigned CurGroup = GroupIdx.size();
1107  GroupIdx.push_back(i);
1108  NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
1109  // OpIdx belongs to this operand group.
1110  if (OpIdx > i && OpIdx < i + NumOps)
1111  OpIdxGroup = CurGroup;
1112  unsigned TiedGroup;
1113  if (!InlineAsm::isUseOperandTiedToDef(FlagMO.getImm(), TiedGroup))
1114  continue;
1115  // Operands in this group are tied to operands in TiedGroup which must be
1116  // earlier. Find the number of operands between the two groups.
1117  unsigned Delta = i - GroupIdx[TiedGroup];
1118 
1119  // OpIdx is a use tied to TiedGroup.
1120  if (OpIdxGroup == CurGroup)
1121  return OpIdx - Delta;
1122 
1123  // OpIdx is a def tied to this use group.
1124  if (OpIdxGroup == TiedGroup)
1125  return OpIdx + Delta;
1126  }
1127  llvm_unreachable("Invalid tied operand on inline asm");
1128 }
1129 
1130 /// clearKillInfo - Clears kill flags on all operands.
1131 ///
1133  for (MachineOperand &MO : operands()) {
1134  if (MO.isReg() && MO.isUse())
1135  MO.setIsKill(false);
1136  }
1137 }
1138 
1140  unsigned SubIdx,
1141  const TargetRegisterInfo &RegInfo) {
1142  if (Register::isPhysicalRegister(ToReg)) {
1143  if (SubIdx)
1144  ToReg = RegInfo.getSubReg(ToReg, SubIdx);
1145  for (MachineOperand &MO : operands()) {
1146  if (!MO.isReg() || MO.getReg() != FromReg)
1147  continue;
1148  MO.substPhysReg(ToReg, RegInfo);
1149  }
1150  } else {
1151  for (MachineOperand &MO : operands()) {
1152  if (!MO.isReg() || MO.getReg() != FromReg)
1153  continue;
1154  MO.substVirtReg(ToReg, SubIdx, RegInfo);
1155  }
1156  }
1157 }
1158 
1159 /// isSafeToMove - Return true if it is safe to move this instruction. If
1160 /// SawStore is set to true, it means that there is a store (or call) between
1161 /// the instruction's location and its intended destination.
1162 bool MachineInstr::isSafeToMove(AAResults *AA, bool &SawStore) const {
1163  // Ignore stuff that we obviously can't move.
1164  //
1165  // Treat volatile loads as stores. This is not strictly necessary for
1166  // volatiles, but it is required for atomic loads. It is not allowed to move
1167  // a load across an atomic load with Ordering > Monotonic.
1168  if (mayStore() || isCall() || isPHI() ||
1169  (mayLoad() && hasOrderedMemoryRef())) {
1170  SawStore = true;
1171  return false;
1172  }
1173 
1174  if (isPosition() || isDebugInstr() || isTerminator() ||
1176  return false;
1177 
1178  // See if this instruction does a load. If so, we have to guarantee that the
1179  // loaded value doesn't change between the load and the its intended
1180  // destination. The check for isInvariantLoad gives the targe the chance to
1181  // classify the load as always returning a constant, e.g. a constant pool
1182  // load.
1184  // Otherwise, this is a real load. If there is a store between the load and
1185  // end of block, we can't move it.
1186  return !SawStore;
1187 
1188  return true;
1189 }
1190 
1192  bool UseTBAA) const {
1193  const MachineFunction *MF = getMF();
1194  const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1195  const MachineFrameInfo &MFI = MF->getFrameInfo();
1196 
1197  // If neither instruction stores to memory, they can't alias in any
1198  // meaningful way, even if they read from the same address.
1199  if (!mayStore() && !Other.mayStore())
1200  return false;
1201 
1202  // Let the target decide if memory accesses cannot possibly overlap.
1203  if (TII->areMemAccessesTriviallyDisjoint(*this, Other))
1204  return false;
1205 
1206  // FIXME: Need to handle multiple memory operands to support all targets.
1207  if (!hasOneMemOperand() || !Other.hasOneMemOperand())
1208  return true;
1209 
1211  MachineMemOperand *MMOb = *Other.memoperands_begin();
1212 
1213  // The following interface to AA is fashioned after DAGCombiner::isAlias
1214  // and operates with MachineMemOperand offset with some important
1215  // assumptions:
1216  // - LLVM fundamentally assumes flat address spaces.
1217  // - MachineOperand offset can *only* result from legalization and
1218  // cannot affect queries other than the trivial case of overlap
1219  // checking.
1220  // - These offsets never wrap and never step outside
1221  // of allocated objects.
1222  // - There should never be any negative offsets here.
1223  //
1224  // FIXME: Modify API to hide this math from "user"
1225  // Even before we go to AA we can reason locally about some
1226  // memory objects. It can save compile time, and possibly catch some
1227  // corner cases not currently covered.
1228 
1229  int64_t OffsetA = MMOa->getOffset();
1230  int64_t OffsetB = MMOb->getOffset();
1231  int64_t MinOffset = std::min(OffsetA, OffsetB);
1232 
1233  uint64_t WidthA = MMOa->getSize();
1234  uint64_t WidthB = MMOb->getSize();
1235  bool KnownWidthA = WidthA != MemoryLocation::UnknownSize;
1236  bool KnownWidthB = WidthB != MemoryLocation::UnknownSize;
1237 
1238  const Value *ValA = MMOa->getValue();
1239  const Value *ValB = MMOb->getValue();
1240  bool SameVal = (ValA && ValB && (ValA == ValB));
1241  if (!SameVal) {
1242  const PseudoSourceValue *PSVa = MMOa->getPseudoValue();
1243  const PseudoSourceValue *PSVb = MMOb->getPseudoValue();
1244  if (PSVa && ValB && !PSVa->mayAlias(&MFI))
1245  return false;
1246  if (PSVb && ValA && !PSVb->mayAlias(&MFI))
1247  return false;
1248  if (PSVa && PSVb && (PSVa == PSVb))
1249  SameVal = true;
1250  }
1251 
1252  if (SameVal) {
1253  if (!KnownWidthA || !KnownWidthB)
1254  return true;
1255  int64_t MaxOffset = std::max(OffsetA, OffsetB);
1256  int64_t LowWidth = (MinOffset == OffsetA) ? WidthA : WidthB;
1257  return (MinOffset + LowWidth > MaxOffset);
1258  }
1259 
1260  if (!AA)
1261  return true;
1262 
1263  if (!ValA || !ValB)
1264  return true;
1265 
1266  assert((OffsetA >= 0) && "Negative MachineMemOperand offset");
1267  assert((OffsetB >= 0) && "Negative MachineMemOperand offset");
1268 
1269  int64_t OverlapA = KnownWidthA ? WidthA + OffsetA - MinOffset
1271  int64_t OverlapB = KnownWidthB ? WidthB + OffsetB - MinOffset
1273 
1274  AliasResult AAResult = AA->alias(
1275  MemoryLocation(ValA, OverlapA,
1276  UseTBAA ? MMOa->getAAInfo() : AAMDNodes()),
1277  MemoryLocation(ValB, OverlapB,
1278  UseTBAA ? MMOb->getAAInfo() : AAMDNodes()));
1279 
1280  return (AAResult != NoAlias);
1281 }
1282 
1283 /// hasOrderedMemoryRef - Return true if this instruction may have an ordered
1284 /// or volatile memory reference, or if the information describing the memory
1285 /// reference is not available. Return false if it is known to have no ordered
1286 /// memory references.
1288  // An instruction known never to access memory won't have a volatile access.
1289  if (!mayStore() &&
1290  !mayLoad() &&
1291  !isCall() &&
1293  return false;
1294 
1295  // Otherwise, if the instruction has no memory reference information,
1296  // conservatively assume it wasn't preserved.
1297  if (memoperands_empty())
1298  return true;
1299 
1300  // Check if any of our memory operands are ordered.
1301  return llvm::any_of(memoperands(), [](const MachineMemOperand *MMO) {
1302  return !MMO->isUnordered();
1303  });
1304 }
1305 
1306 /// isDereferenceableInvariantLoad - Return true if this instruction will never
1307 /// trap and is loading from a location whose value is invariant across a run of
1308 /// this function.
1310  // If the instruction doesn't load at all, it isn't an invariant load.
1311  if (!mayLoad())
1312  return false;
1313 
1314  // If the instruction has lost its memoperands, conservatively assume that
1315  // it may not be an invariant load.
1316  if (memoperands_empty())
1317  return false;
1318 
1319  const MachineFrameInfo &MFI = getParent()->getParent()->getFrameInfo();
1320 
1321  for (MachineMemOperand *MMO : memoperands()) {
1322  if (!MMO->isUnordered())
1323  // If the memory operand has ordering side effects, we can't move the
1324  // instruction. Such an instruction is technically an invariant load,
1325  // but the caller code would need updated to expect that.
1326  return false;
1327  if (MMO->isStore()) return false;
1328  if (MMO->isInvariant() && MMO->isDereferenceable())
1329  continue;
1330 
1331  // A load from a constant PseudoSourceValue is invariant.
1332  if (const PseudoSourceValue *PSV = MMO->getPseudoValue())
1333  if (PSV->isConstant(&MFI))
1334  continue;
1335 
1336  if (const Value *V = MMO->getValue()) {
1337  // If we have an AliasAnalysis, ask it whether the memory is constant.
1338  if (AA &&
1340  MemoryLocation(V, MMO->getSize(), MMO->getAAInfo())))
1341  continue;
1342  }
1343 
1344  // Otherwise assume conservatively.
1345  return false;
1346  }
1347 
1348  // Everything checks out.
1349  return true;
1350 }
1351 
1352 /// isConstantValuePHI - If the specified instruction is a PHI that always
1353 /// merges together the same virtual register, return the register, otherwise
1354 /// return 0.
1356  if (!isPHI())
1357  return 0;
1358  assert(getNumOperands() >= 3 &&
1359  "It's illegal to have a PHI without source operands");
1360 
1361  Register Reg = getOperand(1).getReg();
1362  for (unsigned i = 3, e = getNumOperands(); i < e; i += 2)
1363  if (getOperand(i).getReg() != Reg)
1364  return 0;
1365  return Reg;
1366 }
1367 
1370  return true;
1371  if (isInlineAsm()) {
1372  unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1373  if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1374  return true;
1375  }
1376 
1377  return false;
1378 }
1379 
1381  return mayStore() || isCall() || hasUnmodeledSideEffects();
1382 }
1383 
1384 /// allDefsAreDead - Return true if all the defs of this instruction are dead.
1385 ///
1387  for (const MachineOperand &MO : operands()) {
1388  if (!MO.isReg() || MO.isUse())
1389  continue;
1390  if (!MO.isDead())
1391  return false;
1392  }
1393  return true;
1394 }
1395 
1396 /// copyImplicitOps - Copy implicit register operands from specified
1397 /// instruction to this instruction.
1399  const MachineInstr &MI) {
1400  for (unsigned i = MI.getDesc().getNumOperands(), e = MI.getNumOperands();
1401  i != e; ++i) {
1402  const MachineOperand &MO = MI.getOperand(i);
1403  if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask())
1404  addOperand(MF, MO);
1405  }
1406 }
1407 
1409  const MCInstrDesc &MCID = getDesc();
1410  for (unsigned I = 0, E = getNumOperands(); I < E; ++I) {
1411  const auto &Operand = getOperand(I);
1412  if (!Operand.isReg() || Operand.isDef())
1413  // Ignore the defined registers as MCID marks only the uses as tied.
1414  continue;
1415  int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
1416  int TiedIdx = Operand.isTied() ? int(findTiedOperandIdx(I)) : -1;
1417  if (ExpectedTiedIdx != TiedIdx)
1418  return true;
1419  }
1420  return false;
1421 }
1422 
1423 LLT MachineInstr::getTypeToPrint(unsigned OpIdx, SmallBitVector &PrintedTypes,
1424  const MachineRegisterInfo &MRI) const {
1425  const MachineOperand &Op = getOperand(OpIdx);
1426  if (!Op.isReg())
1427  return LLT{};
1428 
1429  if (isVariadic() || OpIdx >= getNumExplicitOperands())
1430  return MRI.getType(Op.getReg());
1431 
1432  auto &OpInfo = getDesc().OpInfo[OpIdx];
1433  if (!OpInfo.isGenericType())
1434  return MRI.getType(Op.getReg());
1435 
1436  if (PrintedTypes[OpInfo.getGenericTypeIndex()])
1437  return LLT{};
1438 
1439  LLT TypeToPrint = MRI.getType(Op.getReg());
1440  // Don't mark the type index printed if it wasn't actually printed: maybe
1441  // another operand with the same type index has an actual type attached:
1442  if (TypeToPrint.isValid())
1443  PrintedTypes.set(OpInfo.getGenericTypeIndex());
1444  return TypeToPrint;
1445 }
1446 
1447 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1449  dbgs() << " ";
1450  print(dbgs());
1451 }
1452 #endif
1453 
1454 void MachineInstr::print(raw_ostream &OS, bool IsStandalone, bool SkipOpers,
1455  bool SkipDebugLoc, bool AddNewLine,
1456  const TargetInstrInfo *TII) const {
1457  const Module *M = nullptr;
1458  const Function *F = nullptr;
1459  if (const MachineFunction *MF = getMFIfAvailable(*this)) {
1460  F = &MF->getFunction();
1461  M = F->getParent();
1462  if (!TII)
1463  TII = MF->getSubtarget().getInstrInfo();
1464  }
1465 
1466  ModuleSlotTracker MST(M);
1467  if (F)
1468  MST.incorporateFunction(*F);
1469  print(OS, MST, IsStandalone, SkipOpers, SkipDebugLoc, AddNewLine, TII);
1470 }
1471 
1473  bool IsStandalone, bool SkipOpers, bool SkipDebugLoc,
1474  bool AddNewLine, const TargetInstrInfo *TII) const {
1475  // We can be a bit tidier if we know the MachineFunction.
1476  const MachineFunction *MF = nullptr;
1477  const TargetRegisterInfo *TRI = nullptr;
1478  const MachineRegisterInfo *MRI = nullptr;
1479  const TargetIntrinsicInfo *IntrinsicInfo = nullptr;
1480  tryToGetTargetInfo(*this, TRI, MRI, IntrinsicInfo, TII);
1481 
1482  if (isCFIInstruction())
1483  assert(getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
1484 
1485  SmallBitVector PrintedTypes(8);
1486  bool ShouldPrintRegisterTies = IsStandalone || hasComplexRegisterTies();
1487  auto getTiedOperandIdx = [&](unsigned OpIdx) {
1488  if (!ShouldPrintRegisterTies)
1489  return 0U;
1490  const MachineOperand &MO = getOperand(OpIdx);
1491  if (MO.isReg() && MO.isTied() && !MO.isDef())
1492  return findTiedOperandIdx(OpIdx);
1493  return 0U;
1494  };
1495  unsigned StartOp = 0;
1496  unsigned e = getNumOperands();
1497 
1498  // Print explicitly defined operands on the left of an assignment syntax.
1499  while (StartOp < e) {
1500  const MachineOperand &MO = getOperand(StartOp);
1501  if (!MO.isReg() || !MO.isDef() || MO.isImplicit())
1502  break;
1503 
1504  if (StartOp != 0)
1505  OS << ", ";
1506 
1507  LLT TypeToPrint = MRI ? getTypeToPrint(StartOp, PrintedTypes, *MRI) : LLT{};
1508  unsigned TiedOperandIdx = getTiedOperandIdx(StartOp);
1509  MO.print(OS, MST, TypeToPrint, StartOp, /*PrintDef=*/false, IsStandalone,
1510  ShouldPrintRegisterTies, TiedOperandIdx, TRI, IntrinsicInfo);
1511  ++StartOp;
1512  }
1513 
1514  if (StartOp != 0)
1515  OS << " = ";
1516 
1518  OS << "frame-setup ";
1520  OS << "frame-destroy ";
1522  OS << "nnan ";
1524  OS << "ninf ";
1526  OS << "nsz ";
1528  OS << "arcp ";
1530  OS << "contract ";
1532  OS << "afn ";
1534  OS << "reassoc ";
1536  OS << "nuw ";
1538  OS << "nsw ";
1540  OS << "exact ";
1542  OS << "nofpexcept ";
1543 
1544  // Print the opcode name.
1545  if (TII)
1546  OS << TII->getName(getOpcode());
1547  else
1548  OS << "UNKNOWN";
1549 
1550  if (SkipOpers)
1551  return;
1552 
1553  // Print the rest of the operands.
1554  bool FirstOp = true;
1555  unsigned AsmDescOp = ~0u;
1556  unsigned AsmOpCount = 0;
1557 
1558  if (isInlineAsm() && e >= InlineAsm::MIOp_FirstOperand) {
1559  // Print asm string.
1560  OS << " ";
1561  const unsigned OpIdx = InlineAsm::MIOp_AsmString;
1562  LLT TypeToPrint = MRI ? getTypeToPrint(OpIdx, PrintedTypes, *MRI) : LLT{};
1563  unsigned TiedOperandIdx = getTiedOperandIdx(OpIdx);
1564  getOperand(OpIdx).print(OS, MST, TypeToPrint, OpIdx, /*PrintDef=*/true, IsStandalone,
1565  ShouldPrintRegisterTies, TiedOperandIdx, TRI,
1566  IntrinsicInfo);
1567 
1568  // Print HasSideEffects, MayLoad, MayStore, IsAlignStack
1569  unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1570  if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1571  OS << " [sideeffect]";
1572  if (ExtraInfo & InlineAsm::Extra_MayLoad)
1573  OS << " [mayload]";
1574  if (ExtraInfo & InlineAsm::Extra_MayStore)
1575  OS << " [maystore]";
1576  if (ExtraInfo & InlineAsm::Extra_IsConvergent)
1577  OS << " [isconvergent]";
1578  if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
1579  OS << " [alignstack]";
1581  OS << " [attdialect]";
1583  OS << " [inteldialect]";
1584 
1585  StartOp = AsmDescOp = InlineAsm::MIOp_FirstOperand;
1586  FirstOp = false;
1587  }
1588 
1589  for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
1590  const MachineOperand &MO = getOperand(i);
1591 
1592  if (FirstOp) FirstOp = false; else OS << ",";
1593  OS << " ";
1594 
1595  if (isDebugValue() && MO.isMetadata()) {
1596  // Pretty print DBG_VALUE instructions.
1597  auto *DIV = dyn_cast<DILocalVariable>(MO.getMetadata());
1598  if (DIV && !DIV->getName().empty())
1599  OS << "!\"" << DIV->getName() << '\"';
1600  else {
1601  LLT TypeToPrint = MRI ? getTypeToPrint(i, PrintedTypes, *MRI) : LLT{};
1602  unsigned TiedOperandIdx = getTiedOperandIdx(i);
1603  MO.print(OS, MST, TypeToPrint, i, /*PrintDef=*/true, IsStandalone,
1604  ShouldPrintRegisterTies, TiedOperandIdx, TRI, IntrinsicInfo);
1605  }
1606  } else if (isDebugLabel() && MO.isMetadata()) {
1607  // Pretty print DBG_LABEL instructions.
1608  auto *DIL = dyn_cast<DILabel>(MO.getMetadata());
1609  if (DIL && !DIL->getName().empty())
1610  OS << "\"" << DIL->getName() << '\"';
1611  else {
1612  LLT TypeToPrint = MRI ? getTypeToPrint(i, PrintedTypes, *MRI) : LLT{};
1613  unsigned TiedOperandIdx = getTiedOperandIdx(i);
1614  MO.print(OS, MST, TypeToPrint, i, /*PrintDef=*/true, IsStandalone,
1615  ShouldPrintRegisterTies, TiedOperandIdx, TRI, IntrinsicInfo);
1616  }
1617  } else if (i == AsmDescOp && MO.isImm()) {
1618  // Pretty print the inline asm operand descriptor.
1619  OS << '$' << AsmOpCount++;
1620  unsigned Flag = MO.getImm();
1621  switch (InlineAsm::getKind(Flag)) {
1622  case InlineAsm::Kind_RegUse: OS << ":[reguse"; break;
1623  case InlineAsm::Kind_RegDef: OS << ":[regdef"; break;
1624  case InlineAsm::Kind_RegDefEarlyClobber: OS << ":[regdef-ec"; break;
1625  case InlineAsm::Kind_Clobber: OS << ":[clobber"; break;
1626  case InlineAsm::Kind_Imm: OS << ":[imm"; break;
1627  case InlineAsm::Kind_Mem: OS << ":[mem"; break;
1628  default: OS << ":[??" << InlineAsm::getKind(Flag); break;
1629  }
1630 
1631  unsigned RCID = 0;
1632  if (!InlineAsm::isImmKind(Flag) && !InlineAsm::isMemKind(Flag) &&
1633  InlineAsm::hasRegClassConstraint(Flag, RCID)) {
1634  if (TRI) {
1635  OS << ':' << TRI->getRegClassName(TRI->getRegClass(RCID));
1636  } else
1637  OS << ":RC" << RCID;
1638  }
1639 
1640  if (InlineAsm::isMemKind(Flag)) {
1641  unsigned MCID = InlineAsm::getMemoryConstraintID(Flag);
1642  switch (MCID) {
1643  case InlineAsm::Constraint_es: OS << ":es"; break;
1644  case InlineAsm::Constraint_i: OS << ":i"; break;
1645  case InlineAsm::Constraint_m: OS << ":m"; break;
1646  case InlineAsm::Constraint_o: OS << ":o"; break;
1647  case InlineAsm::Constraint_v: OS << ":v"; break;
1648  case InlineAsm::Constraint_Q: OS << ":Q"; break;
1649  case InlineAsm::Constraint_R: OS << ":R"; break;
1650  case InlineAsm::Constraint_S: OS << ":S"; break;
1651  case InlineAsm::Constraint_T: OS << ":T"; break;
1652  case InlineAsm::Constraint_Um: OS << ":Um"; break;
1653  case InlineAsm::Constraint_Un: OS << ":Un"; break;
1654  case InlineAsm::Constraint_Uq: OS << ":Uq"; break;
1655  case InlineAsm::Constraint_Us: OS << ":Us"; break;
1656  case InlineAsm::Constraint_Ut: OS << ":Ut"; break;
1657  case InlineAsm::Constraint_Uv: OS << ":Uv"; break;
1658  case InlineAsm::Constraint_Uy: OS << ":Uy"; break;
1659  case InlineAsm::Constraint_X: OS << ":X"; break;
1660  case InlineAsm::Constraint_Z: OS << ":Z"; break;
1661  case InlineAsm::Constraint_ZC: OS << ":ZC"; break;
1662  case InlineAsm::Constraint_Zy: OS << ":Zy"; break;
1663  default: OS << ":?"; break;
1664  }
1665  }
1666 
1667  unsigned TiedTo = 0;
1668  if (InlineAsm::isUseOperandTiedToDef(Flag, TiedTo))
1669  OS << " tiedto:$" << TiedTo;
1670 
1671  OS << ']';
1672 
1673  // Compute the index of the next operand descriptor.
1674  AsmDescOp += 1 + InlineAsm::getNumOperandRegisters(Flag);
1675  } else {
1676  LLT TypeToPrint = MRI ? getTypeToPrint(i, PrintedTypes, *MRI) : LLT{};
1677  unsigned TiedOperandIdx = getTiedOperandIdx(i);
1678  if (MO.isImm() && isOperandSubregIdx(i))
1680  else
1681  MO.print(OS, MST, TypeToPrint, i, /*PrintDef=*/true, IsStandalone,
1682  ShouldPrintRegisterTies, TiedOperandIdx, TRI, IntrinsicInfo);
1683  }
1684  }
1685 
1686  // Print any optional symbols attached to this instruction as-if they were
1687  // operands.
1688  if (MCSymbol *PreInstrSymbol = getPreInstrSymbol()) {
1689  if (!FirstOp) {
1690  FirstOp = false;
1691  OS << ',';
1692  }
1693  OS << " pre-instr-symbol ";
1694  MachineOperand::printSymbol(OS, *PreInstrSymbol);
1695  }
1696  if (MCSymbol *PostInstrSymbol = getPostInstrSymbol()) {
1697  if (!FirstOp) {
1698  FirstOp = false;
1699  OS << ',';
1700  }
1701  OS << " post-instr-symbol ";
1702  MachineOperand::printSymbol(OS, *PostInstrSymbol);
1703  }
1704  if (MDNode *HeapAllocMarker = getHeapAllocMarker()) {
1705  if (!FirstOp) {
1706  FirstOp = false;
1707  OS << ',';
1708  }
1709  OS << " heap-alloc-marker ";
1710  HeapAllocMarker->printAsOperand(OS, MST);
1711  }
1712 
1713  if (!SkipDebugLoc) {
1714  if (const DebugLoc &DL = getDebugLoc()) {
1715  if (!FirstOp)
1716  OS << ',';
1717  OS << " debug-location ";
1718  DL->printAsOperand(OS, MST);
1719  }
1720  }
1721 
1722  if (!memoperands_empty()) {
1724  const LLVMContext *Context = nullptr;
1725  std::unique_ptr<LLVMContext> CtxPtr;
1726  const MachineFrameInfo *MFI = nullptr;
1727  if (const MachineFunction *MF = getMFIfAvailable(*this)) {
1728  MFI = &MF->getFrameInfo();
1729  Context = &MF->getFunction().getContext();
1730  } else {
1731  CtxPtr = std::make_unique<LLVMContext>();
1732  Context = CtxPtr.get();
1733  }
1734 
1735  OS << " :: ";
1736  bool NeedComma = false;
1737  for (const MachineMemOperand *Op : memoperands()) {
1738  if (NeedComma)
1739  OS << ", ";
1740  Op->print(OS, MST, SSNs, *Context, MFI, TII);
1741  NeedComma = true;
1742  }
1743  }
1744 
1745  if (SkipDebugLoc)
1746  return;
1747 
1748  bool HaveSemi = false;
1749 
1750  // Print debug location information.
1751  if (const DebugLoc &DL = getDebugLoc()) {
1752  if (!HaveSemi) {
1753  OS << ';';
1754  HaveSemi = true;
1755  }
1756  OS << ' ';
1757  DL.print(OS);
1758  }
1759 
1760  // Print extra comments for DEBUG_VALUE.
1761  if (isDebugValue() && getOperand(e - 2).isMetadata()) {
1762  if (!HaveSemi) {
1763  OS << ";";
1764  HaveSemi = true;
1765  }
1766  auto *DV = cast<DILocalVariable>(getOperand(e - 2).getMetadata());
1767  OS << " line no:" << DV->getLine();
1768  if (auto *InlinedAt = debugLoc->getInlinedAt()) {
1769  DebugLoc InlinedAtDL(InlinedAt);
1770  if (InlinedAtDL && MF) {
1771  OS << " inlined @[ ";
1772  InlinedAtDL.print(OS);
1773  OS << " ]";
1774  }
1775  }
1776  if (isIndirectDebugValue())
1777  OS << " indirect";
1778  }
1779  // TODO: DBG_LABEL
1780 
1781  if (AddNewLine)
1782  OS << '\n';
1783 }
1784 
1786  const TargetRegisterInfo *RegInfo,
1787  bool AddIfNotFound) {
1788  bool isPhysReg = Register::isPhysicalRegister(IncomingReg);
1789  bool hasAliases = isPhysReg &&
1790  MCRegAliasIterator(IncomingReg, RegInfo, false).isValid();
1791  bool Found = false;
1792  SmallVector<unsigned,4> DeadOps;
1793  for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1794  MachineOperand &MO = getOperand(i);
1795  if (!MO.isReg() || !MO.isUse() || MO.isUndef())
1796  continue;
1797 
1798  // DEBUG_VALUE nodes do not contribute to code generation and should
1799  // always be ignored. Failure to do so may result in trying to modify
1800  // KILL flags on DEBUG_VALUE nodes.
1801  if (MO.isDebug())
1802  continue;
1803 
1804  Register Reg = MO.getReg();
1805  if (!Reg)
1806  continue;
1807 
1808  if (Reg == IncomingReg) {
1809  if (!Found) {
1810  if (MO.isKill())
1811  // The register is already marked kill.
1812  return true;
1813  if (isPhysReg && isRegTiedToDefOperand(i))
1814  // Two-address uses of physregs must not be marked kill.
1815  return true;
1816  MO.setIsKill();
1817  Found = true;
1818  }
1819  } else if (hasAliases && MO.isKill() && Register::isPhysicalRegister(Reg)) {
1820  // A super-register kill already exists.
1821  if (RegInfo->isSuperRegister(IncomingReg, Reg))
1822  return true;
1823  if (RegInfo->isSubRegister(IncomingReg, Reg))
1824  DeadOps.push_back(i);
1825  }
1826  }
1827 
1828  // Trim unneeded kill operands.
1829  while (!DeadOps.empty()) {
1830  unsigned OpIdx = DeadOps.back();
1831  if (getOperand(OpIdx).isImplicit() &&
1832  (!isInlineAsm() || findInlineAsmFlagIdx(OpIdx) < 0))
1833  RemoveOperand(OpIdx);
1834  else
1835  getOperand(OpIdx).setIsKill(false);
1836  DeadOps.pop_back();
1837  }
1838 
1839  // If not found, this means an alias of one of the operands is killed. Add a
1840  // new implicit operand if required.
1841  if (!Found && AddIfNotFound) {
1843  false /*IsDef*/,
1844  true /*IsImp*/,
1845  true /*IsKill*/));
1846  return true;
1847  }
1848  return Found;
1849 }
1850 
1852  const TargetRegisterInfo *RegInfo) {
1853  if (!Register::isPhysicalRegister(Reg))
1854  RegInfo = nullptr;
1855  for (MachineOperand &MO : operands()) {
1856  if (!MO.isReg() || !MO.isUse() || !MO.isKill())
1857  continue;
1858  Register OpReg = MO.getReg();
1859  if ((RegInfo && RegInfo->regsOverlap(Reg, OpReg)) || Reg == OpReg)
1860  MO.setIsKill(false);
1861  }
1862 }
1863 
1865  const TargetRegisterInfo *RegInfo,
1866  bool AddIfNotFound) {
1867  bool isPhysReg = Register::isPhysicalRegister(Reg);
1868  bool hasAliases = isPhysReg &&
1869  MCRegAliasIterator(Reg, RegInfo, false).isValid();
1870  bool Found = false;
1871  SmallVector<unsigned,4> DeadOps;
1872  for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1873  MachineOperand &MO = getOperand(i);
1874  if (!MO.isReg() || !MO.isDef())
1875  continue;
1876  Register MOReg = MO.getReg();
1877  if (!MOReg)
1878  continue;
1879 
1880  if (MOReg == Reg) {
1881  MO.setIsDead();
1882  Found = true;
1883  } else if (hasAliases && MO.isDead() &&
1885  // There exists a super-register that's marked dead.
1886  if (RegInfo->isSuperRegister(Reg, MOReg))
1887  return true;
1888  if (RegInfo->isSubRegister(Reg, MOReg))
1889  DeadOps.push_back(i);
1890  }
1891  }
1892 
1893  // Trim unneeded dead operands.
1894  while (!DeadOps.empty()) {
1895  unsigned OpIdx = DeadOps.back();
1896  if (getOperand(OpIdx).isImplicit() &&
1897  (!isInlineAsm() || findInlineAsmFlagIdx(OpIdx) < 0))
1898  RemoveOperand(OpIdx);
1899  else
1900  getOperand(OpIdx).setIsDead(false);
1901  DeadOps.pop_back();
1902  }
1903 
1904  // If not found, this means an alias of one of the operands is dead. Add a
1905  // new implicit operand if required.
1906  if (Found || !AddIfNotFound)
1907  return Found;
1908 
1910  true /*IsDef*/,
1911  true /*IsImp*/,
1912  false /*IsKill*/,
1913  true /*IsDead*/));
1914  return true;
1915 }
1916 
1918  for (MachineOperand &MO : operands()) {
1919  if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg)
1920  continue;
1921  MO.setIsDead(false);
1922  }
1923 }
1924 
1926  for (MachineOperand &MO : operands()) {
1927  if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg || MO.getSubReg() == 0)
1928  continue;
1929  MO.setIsUndef(IsUndef);
1930  }
1931 }
1932 
1934  const TargetRegisterInfo *RegInfo) {
1935  if (Register::isPhysicalRegister(Reg)) {
1936  MachineOperand *MO = findRegisterDefOperand(Reg, false, false, RegInfo);
1937  if (MO)
1938  return;
1939  } else {
1940  for (const MachineOperand &MO : operands()) {
1941  if (MO.isReg() && MO.getReg() == Reg && MO.isDef() &&
1942  MO.getSubReg() == 0)
1943  return;
1944  }
1945  }
1947  true /*IsDef*/,
1948  true /*IsImp*/));
1949 }
1950 
1952  const TargetRegisterInfo &TRI) {
1953  bool HasRegMask = false;
1954  for (MachineOperand &MO : operands()) {
1955  if (MO.isRegMask()) {
1956  HasRegMask = true;
1957  continue;
1958  }
1959  if (!MO.isReg() || !MO.isDef()) continue;
1960  Register Reg = MO.getReg();
1961  if (!Reg.isPhysical())
1962  continue;
1963  // If there are no uses, including partial uses, the def is dead.
1964  if (llvm::none_of(UsedRegs,
1965  [&](MCRegister Use) { return TRI.regsOverlap(Use, Reg); }))
1966  MO.setIsDead();
1967  }
1968 
1969  // This is a call with a register mask operand.
1970  // Mask clobbers are always dead, so add defs for the non-dead defines.
1971  if (HasRegMask)
1972  for (ArrayRef<Register>::iterator I = UsedRegs.begin(), E = UsedRegs.end();
1973  I != E; ++I)
1974  addRegisterDefined(*I, &TRI);
1975 }
1976 
1977 unsigned
1979  // Build up a buffer of hash code components.
1980  SmallVector<size_t, 16> HashComponents;
1981  HashComponents.reserve(MI->getNumOperands() + 1);
1982  HashComponents.push_back(MI->getOpcode());
1983  for (const MachineOperand &MO : MI->operands()) {
1984  if (MO.isReg() && MO.isDef() && Register::isVirtualRegister(MO.getReg()))
1985  continue; // Skip virtual register defs.
1986 
1987  HashComponents.push_back(hash_value(MO));
1988  }
1989  return hash_combine_range(HashComponents.begin(), HashComponents.end());
1990 }
1991 
1993  // Find the source location cookie.
1994  unsigned LocCookie = 0;
1995  const MDNode *LocMD = nullptr;
1996  for (unsigned i = getNumOperands(); i != 0; --i) {
1997  if (getOperand(i-1).isMetadata() &&
1998  (LocMD = getOperand(i-1).getMetadata()) &&
1999  LocMD->getNumOperands() != 0) {
2000  if (const ConstantInt *CI =
2001  mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
2002  LocCookie = CI->getZExtValue();
2003  break;
2004  }
2005  }
2006  }
2007 
2008  if (const MachineBasicBlock *MBB = getParent())
2009  if (const MachineFunction *MF = MBB->getParent())
2010  return MF->getMMI().getModule()->getContext().emitError(LocCookie, Msg);
2011  report_fatal_error(Msg);
2012 }
2013 
2015  const MCInstrDesc &MCID, bool IsIndirect,
2016  Register Reg, const MDNode *Variable,
2017  const MDNode *Expr) {
2018  assert(isa<DILocalVariable>(Variable) && "not a variable");
2019  assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
2020  assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
2021  "Expected inlined-at fields to agree");
2022  auto MIB = BuildMI(MF, DL, MCID).addReg(Reg, RegState::Debug);
2023  if (IsIndirect)
2024  MIB.addImm(0U);
2025  else
2026  MIB.addReg(0U, RegState::Debug);
2027  return MIB.addMetadata(Variable).addMetadata(Expr);
2028 }
2029 
2031  const MCInstrDesc &MCID, bool IsIndirect,
2032  MachineOperand &MO, const MDNode *Variable,
2033  const MDNode *Expr) {
2034  assert(isa<DILocalVariable>(Variable) && "not a variable");
2035  assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
2036  assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
2037  "Expected inlined-at fields to agree");
2038  if (MO.isReg())
2039  return BuildMI(MF, DL, MCID, IsIndirect, MO.getReg(), Variable, Expr);
2040 
2041  auto MIB = BuildMI(MF, DL, MCID).add(MO);
2042  if (IsIndirect)
2043  MIB.addImm(0U);
2044  else
2045  MIB.addReg(0U, RegState::Debug);
2046  return MIB.addMetadata(Variable).addMetadata(Expr);
2047  }
2048 
2051  const DebugLoc &DL, const MCInstrDesc &MCID,
2052  bool IsIndirect, Register Reg,
2053  const MDNode *Variable, const MDNode *Expr) {
2054  MachineFunction &MF = *BB.getParent();
2055  MachineInstr *MI = BuildMI(MF, DL, MCID, IsIndirect, Reg, Variable, Expr);
2056  BB.insert(I, MI);
2057  return MachineInstrBuilder(MF, MI);
2058 }
2059 
2062  const DebugLoc &DL, const MCInstrDesc &MCID,
2063  bool IsIndirect, MachineOperand &MO,
2064  const MDNode *Variable, const MDNode *Expr) {
2065  MachineFunction &MF = *BB.getParent();
2066  MachineInstr *MI = BuildMI(MF, DL, MCID, IsIndirect, MO, Variable, Expr);
2067  BB.insert(I, MI);
2068  return MachineInstrBuilder(MF, *MI);
2069 }
2070 
2071 /// Compute the new DIExpression to use with a DBG_VALUE for a spill slot.
2072 /// This prepends DW_OP_deref when spilling an indirect DBG_VALUE.
2074  assert(MI.getOperand(0).isReg() && "can't spill non-register");
2076  "Expected inlined-at fields to agree");
2077 
2078  const DIExpression *Expr = MI.getDebugExpression();
2079  if (MI.isIndirectDebugValue()) {
2080  assert(MI.getOperand(1).getImm() == 0 && "DBG_VALUE with nonzero offset");
2082  }
2083  return Expr;
2084 }
2085 
2088  const MachineInstr &Orig,
2089  int FrameIndex) {
2090  const DIExpression *Expr = computeExprForSpill(Orig);
2091  return BuildMI(BB, I, Orig.getDebugLoc(), Orig.getDesc())
2092  .addFrameIndex(FrameIndex)
2093  .addImm(0U)
2094  .addMetadata(Orig.getDebugVariable())
2095  .addMetadata(Expr);
2096 }
2097 
2099  const DIExpression *Expr = computeExprForSpill(Orig);
2100  Orig.getOperand(0).ChangeToFrameIndex(FrameIndex);
2101  Orig.getOperand(1).ChangeToImmediate(0U);
2102  Orig.getOperand(3).setMetadata(Expr);
2103 }
2104 
2106  SmallVectorImpl<MachineInstr *> &DbgValues) {
2107  MachineInstr &MI = *this;
2108  if (!MI.getOperand(0).isReg())
2109  return;
2110 
2111  MachineBasicBlock::iterator DI = MI; ++DI;
2112  for (MachineBasicBlock::iterator DE = MI.getParent()->end();
2113  DI != DE; ++DI) {
2114  if (!DI->isDebugValue())
2115  return;
2116  if (DI->getOperand(0).isReg() &&
2117  DI->getOperand(0).getReg() == MI.getOperand(0).getReg())
2118  DbgValues.push_back(&*DI);
2119  }
2120 }
2121 
2123  // Collect matching debug values.
2125 
2126  if (!getOperand(0).isReg())
2127  return;
2128 
2129  unsigned DefReg = getOperand(0).getReg();
2130  auto *MRI = getRegInfo();
2131  for (auto &MO : MRI->use_operands(DefReg)) {
2132  auto *DI = MO.getParent();
2133  if (!DI->isDebugValue())
2134  continue;
2135  if (DI->getOperand(0).isReg() &&
2136  DI->getOperand(0).getReg() == DefReg){
2137  DbgValues.push_back(DI);
2138  }
2139  }
2140 
2141  // Propagate Reg to debug value instructions.
2142  for (auto *DBI : DbgValues)
2143  DBI->getOperand(0).setReg(Reg);
2144 }
2145 
2147 
2148 static unsigned getSpillSlotSize(MMOList &Accesses,
2149  const MachineFrameInfo &MFI) {
2150  unsigned Size = 0;
2151  for (auto A : Accesses)
2152  if (MFI.isSpillSlotObjectIndex(
2153  cast<FixedStackPseudoSourceValue>(A->getPseudoValue())
2154  ->getFrameIndex()))
2155  Size += A->getSize();
2156  return Size;
2157 }
2158 
2161  int FI;
2162  if (TII->isStoreToStackSlotPostFE(*this, FI)) {
2163  const MachineFrameInfo &MFI = getMF()->getFrameInfo();
2164  if (MFI.isSpillSlotObjectIndex(FI))
2165  return (*memoperands_begin())->getSize();
2166  }
2167  return None;
2168 }
2169 
2172  MMOList Accesses;
2173  if (TII->hasStoreToStackSlot(*this, Accesses))
2174  return getSpillSlotSize(Accesses, getMF()->getFrameInfo());
2175  return None;
2176 }
2177 
2180  int FI;
2181  if (TII->isLoadFromStackSlotPostFE(*this, FI)) {
2182  const MachineFrameInfo &MFI = getMF()->getFrameInfo();
2183  if (MFI.isSpillSlotObjectIndex(FI))
2184  return (*memoperands_begin())->getSize();
2185  }
2186  return None;
2187 }
2188 
2191  MMOList Accesses;
2192  if (TII->hasLoadFromStackSlot(*this, Accesses))
2193  return getSpillSlotSize(Accesses, getMF()->getFrameInfo());
2194  return None;
2195 }
static bool isReg(const MCInst &MI, unsigned OpNo)
void changeDebugValuesDefReg(Register Reg)
Find all DBG_VALUEs that point to the register def in this instruction and point them to Reg instead...
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
static bool Check(DecodeStatus &Out, DecodeStatus In)
unsigned getNumImplicitUses() const
Return the number of implicit uses this instruction has.
Definition: MCInstrDesc.h:569
void bundleWithPred()
Bundle this instruction with its predecessor.
const MachineInstrBuilder & addMetadata(const MDNode *MD) const
bool isUnordered() const
Returns true if this memory operation doesn&#39;t have any ordering constraints other than normal aliasin...
LLT getTypeToPrint(unsigned OpIdx, SmallBitVector &PrintedTypes, const MachineRegisterInfo &MRI) const
Debugging supportDetermine the generic type to be printed (if needed) on uses and defs...
const MachineInstrBuilder & add(const MachineOperand &MO) const
bool isDebugLabel() const
bool isEntryValue() const
Check if the expression consists of exactly one entry value operand.
void collectDebugValues(SmallVectorImpl< MachineInstr *> &DbgValues)
Scan instructions immediately following MI and collect any matching DBG_VALUEs.
LLVMContext & Context
int findFirstPredOperandIdx() const
Find the index of the first operand in the operand list that is used to represent the predicate...
bool isCall(QueryType Type=AnyInBundle) const
Definition: MachineInstr.h:682
bool noNaNs() const
Definition: Operator.h:206
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:22
void emitError(StringRef Msg) const
Emit an error referring to the source location of this instruction.
This is a &#39;bitvector&#39; (really, a variable-sized bit array), optimized for the case when the array is ...
unsigned getNumImplicitDefs() const
Return the number of implicit defs this instruct has.
Definition: MCInstrDesc.h:591
void removeRegOperandFromUseList(MachineOperand *MO)
Remove MO from its use-def list.
const MachineFunction * getMF() const
Return the function that contains the basic block that this instruction belongs to.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
This class represents lattice values for constants.
Definition: AllocatorList.h:23
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:492
virtual bool areMemAccessesTriviallyDisjoint(const MachineInstr &MIa, const MachineInstr &MIb) const
Sometimes, it is possible for the target to tell, even without aliasing information, that two MIs access different memory addresses.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
iterator begin() const
Definition: ArrayRef.h:146
static const DIExpression * computeExprForSpill(const MachineInstr &MI)
Compute the new DIExpression to use with a DBG_VALUE for a spill slot.
virtual bool mayAlias(const MachineFrameInfo *) const
Return true if the memory pointed to by this PseudoSourceValue can ever alias an LLVM IR Value...
static const MachineFunction * getMFIfAvailable(const MachineInstr &MI)
bool isCFIInstruction() const
void setHeapAllocMarker(MachineFunction &MF, MDNode *MD)
Set a marker on instructions that denotes where we should create and emit heap alloc site labels...
bool isBundledWithPred() const
Return true if this instruction is part of a bundle, and it is not the first instruction in the bundl...
Definition: MachineInstr.h:379
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:400
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
Definition: Register.h:63
const MCPhysReg * getImplicitUses() const
Return a list of registers that are potentially read by any instance of this machine instruction...
Definition: MCInstrDesc.h:566
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:188
unsigned Reg
bool addRegisterDead(Register Reg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
We have determined MI defined a register without a use.
int findRegisterDefOperandIdx(Register Reg, bool isDead=false, bool Overlap=false, const TargetRegisterInfo *TRI=nullptr) const
Returns the operand index that is a def of the specified register or -1 if it is not found...
This file contains the declarations for metadata subclasses.
unsigned getSubReg() const
bool isInlineAsm() const
bool isValid() const
isValid - Returns true until all the operands have been visited.
The two locations do not alias at all.
Definition: AliasAnalysis.h:84
bool noInfs() const
Definition: Operator.h:207
LLT getType(unsigned Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register...
uint64_t getSize() const
Return the size in bytes of the memory reference.
bool isMetadata() const
isMetadata - Tests if this is a MO_Metadata operand.
uint16_t mergeFlagsWith(const MachineInstr &Other) const
Return the MIFlags which represent both MachineInstrs.
unsigned const TargetRegisterInfo * TRI
A debug info location.
Definition: DebugLoc.h:33
Metadata node.
Definition: Metadata.h:872
void setIsDead(bool Val=true)
F(f)
bool mayAlias(AAResults *AA, const MachineInstr &Other, bool UseTBAA) const
Returns true if this instruction&#39;s memory access aliases the memory access of Other.
MachineModuleInfo & getMMI() const
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1077
Manage lifetime of a slot tracker for printing IR.
void reserve(size_type N)
Definition: SmallVector.h:369
iterator_range< mop_iterator > operands()
Definition: MachineInstr.h:492
bool isPHI() const
void print(raw_ostream &OS) const
prints source location /path/to/file.exe:line:col @[inlined at]
Definition: DebugLoc.cpp:118
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
static bool isImmKind(unsigned Flag)
Definition: InlineAsm.h:276
static bool isUseOperandTiedToDef(unsigned Flag, unsigned &Idx)
isUseOperandTiedToDef - Return true if the flag of the inline asm operand indicates it is an use oper...
Definition: InlineAsm.h:342
void clearRegisterDeads(Register Reg)
Clear all dead flags on operands defining register Reg.
const TargetRegisterClass * getRegClass(unsigned i) const
Returns the register class associated with the enumeration value.
static DIExpression * prepend(const DIExpression *Expr, uint8_t Flags, int64_t Offset=0)
Prepend DIExpr with a deref and offset operation and optionally turn it into a stack value or/and an ...
const char * getRegClassName(const TargetRegisterClass *Class) const
Returns the name of the register class.
void clearKillInfo()
Clears kill flags on all operands.
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
The main low level interface to the alias analysis implementation.
AAMDNodes getAAInfo() const
Return the AA tags for the memory reference.
static void tryToGetTargetInfo(const MachineInstr &MI, const TargetRegisterInfo *&TRI, const MachineRegisterInfo *&MRI, const TargetIntrinsicInfo *&IntrinsicInfo, const TargetInstrInfo *&TII)
A description of a memory reference used in the backend.
MachineInstr * buildDbgValueForSpill(MachineBasicBlock &BB, MachineBasicBlock::iterator I, const MachineInstr &Orig, int FrameIndex)
Clone a DBG_VALUE whose value has been spilled to FrameIndex.
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
Definition: MCInstrDesc.h:235
const HexagonInstrInfo * TII
void substituteRegister(Register FromReg, Register ToReg, unsigned SubIdx, const TargetRegisterInfo &RegInfo)
Replace all occurrences of FromReg with ToReg:SubIdx, properly composing subreg indices where necessa...
std::pair< bool, bool > readsWritesVirtualRegister(Register Reg, SmallVectorImpl< unsigned > *Ops=nullptr) const
Return a pair of bools (reads, writes) indicating if this instruction reads or writes Reg...
const TargetRegisterClass * getRegClassConstraint(unsigned OpIdx, const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const
Compute the static register class constraint for operand OpIdx.
unsigned getNumOperands() const
Retuns the total number of operands.
Definition: MachineInstr.h:429
virtual const TargetRegisterClass * getRegClass(const MCInstrDesc &MCID, unsigned OpNum, const TargetRegisterInfo *TRI, const MachineFunction &MF) const
Given a machine instruction descriptor, returns the register class constraint for OpNum...
bool isBundledWithSucc() const
Return true if this instruction is part of a bundle, and it is not the last instruction in the bundle...
Definition: MachineInstr.h:383
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:245
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
void eraseFromParent()
Unlink &#39;this&#39; from the containing basic block and delete it.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1196
bool isTerminator(QueryType Type=AnyInBundle) const
Returns true if this instruction part of the terminator for a basic block.
Definition: MachineInstr.h:698
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:426
void bundleWithSucc()
Bundle this instruction with its successor.
MachineInstr * remove_instr(MachineInstr *I)
Remove the possibly bundled instruction from the instruction list without deleting it...
virtual const TargetRegisterClass * getSubClassWithSubReg(const TargetRegisterClass *RC, unsigned Idx) const
Returns the largest legal sub-class of RC that supports the sub-register index Idx.
const TargetRegisterClass * getRegClassConstraintEffect(unsigned OpIdx, const TargetRegisterClass *CurRC, const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const
Applies the constraints (def/use) implied by the OpIdx operand to the given CurRC.
void eraseFromParentAndMarkDBGValuesForRemoval()
Unlink &#39;this&#39; from the containing basic block and delete it.
Optional< unsigned > getFoldedRestoreSize(const TargetInstrInfo *TII) const
Return a valid size if the instruction is a folded restore instruction.
bool regsOverlap(Register regA, Register regB) const
Returns true if the two registers are equal or alias each other.
void emitError(unsigned LocCookie, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
void setIsEarlyClobber(bool Val=true)
void unbundleFromPred()
Break bundle above this instruction.
const TargetRegisterClass * getRegClassConstraintEffectForVReg(Register Reg, const TargetRegisterClass *CurRC, const TargetInstrInfo *TII, const TargetRegisterInfo *TRI, bool ExploreBundle=false) const
Applies the constraints (def/use) implied by this MI on Reg to the given CurRC.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
Definition: MachineInstr.h:423
void copyImplicitOps(MachineFunction &MF, const MachineInstr &MI)
Copy implicit register operands from specified instruction to this instruction.
bool isBundle() const
bool mayRaiseFPException() const
Return true if this instruction could possibly raise a floating-point exception.
Definition: MachineInstr.h:889
void setRegisterDefReadUndef(Register Reg, bool IsUndef=true)
Mark all subregister defs of register Reg with the undef flag.
bool addRegisterKilled(Register IncomingReg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
We have determined MI kills a register.
Optional< unsigned > getRestoreSize(const TargetInstrInfo *TII) const
Return a valid size if the instruction is a restore instruction.
Register getSubReg(MCRegister Reg, unsigned Idx) const
Returns the physical register number of sub-register "Index" for physical register RegNo...
bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition: Register.h:95
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
hash_code hash_value(const APFloat &Arg)
See friend declarations above.
Definition: APFloat.cpp:4510
bool allowContract() const
Definition: Operator.h:210
void untieRegOperand(unsigned OpIdx)
Break any tie involving OpIdx.
bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:89
int findInlineAsmFlagIdx(unsigned OpIdx, unsigned *GroupNo=nullptr) const
Find the index of the flag word operand that corresponds to operand OpIdx on an inline asm instructio...
void setPostInstrSymbol(MachineFunction &MF, MCSymbol *Symbol)
Set a symbol that will be emitted just after the instruction itself.
virtual const TargetInstrInfo * getInstrInfo() const
bool isPredicate() const
Set if this is one of the operands that made up of the predicate operand that controls an isPredicabl...
Definition: MCInstrDesc.h:101
void addMemOperand(MachineFunction &MF, MachineMemOperand *MO)
Add a MachineMemOperand to the machine instruction.
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
bool allDefsAreDead() const
Return true if all the defs of this instruction are dead.
MCSymbol * getPreInstrSymbol() const
Helper to extract a pre-instruction symbol if one has been added.
Definition: MachineInstr.h:588
void dropMemRefs(MachineFunction &MF)
Clear this MachineInstr&#39;s memory reference descriptor list.
void ChangeToImmediate(int64_t ImmVal)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value...
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:139
TargetInstrInfo - Interface to description of machine instruction set.
AliasResult
The possible results of an alias query.
Definition: AliasAnalysis.h:78
const Value * getValue() const
Return the base address of the memory access.
bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
virtual unsigned isStoreToStackSlotPostFE(const MachineInstr &MI, int &FrameIndex) const
Check for post-frame ptr elimination stack locations as well.
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
Checks whether the given location points to constant memory, or if OrLocal is true whether it points ...
Optional< unsigned > getSpillSize(const TargetInstrInfo *TII) const
Return a valid size if the instruction is a spill instruction.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
bool mayStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly modify memory.
Definition: MachineInstr.h:869
const MCPhysReg * getImplicitDefs() const
Return a list of registers that are potentially written by any instance of this machine instruction...
Definition: MCInstrDesc.h:588
instr_iterator erase_instr(MachineInstr *I)
Remove an instruction from the instruction list and delete it.
unsigned const MachineRegisterInfo * MRI
void cloneInstrSymbols(MachineFunction &MF, const MachineInstr &MI)
Clone another MachineInstr&#39;s pre- and post- instruction symbols and replace ours with it...
ArrayRef< MachineMemOperand * > memoperands() const
Access to memory operands of the instruction.
Definition: MachineInstr.h:549
unsigned isConstantValuePHI() const
If the specified instruction is a PHI that always merges together the same virtual register...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:63
bool isVariadic() const
Return true if this instruction can have a variable number of operands.
Definition: MCInstrDesc.h:263
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:158
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isOperandSubregIdx(unsigned OpIdx) const
Return true if operand OpIdx is a subregister index.
Definition: MachineInstr.h:462
const MCPhysReg * ImplicitDefs
Definition: MCInstrDesc.h:198
static unsigned getNumOperandRegisters(unsigned Flag)
getNumOperandRegisters - Extract the number of registers field from the inline asm operand flag...
Definition: InlineAsm.h:336
A udiv or sdiv instruction, which can be marked as "exact", indicating that no bits are destroyed...
Definition: Operator.h:120
bool isValidLocationForIntrinsic(const DILocation *DL) const
Check that a location is valid for this variable.
void setFlag(MIFlag Flag)
Set a MI flag.
Definition: MachineInstr.h:317
MCRegAliasIterator enumerates all registers aliasing Reg.
bool hasRegisterImplicitUseOperand(Register Reg) const
Returns true if the MachineInstr has an implicit-use operand of exactly the given register (not consi...
void clearFlag(MIFlag Flag)
clearFlag - Clear a MI flag.
Definition: MachineInstr.h:328
This file declares a class to represent arbitrary precision floating point values and provide a varie...
bool hasOneMemOperand() const
Return true if this instruction has exactly one MachineMemOperand.
Definition: MachineInstr.h:582
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1189
static unsigned getMemoryConstraintID(unsigned Flag)
Definition: InlineAsm.h:329
bool isValid() const
const PseudoSourceValue * getPseudoValue() const
const DILabel * getDebugLabel() const
Return the debug label referenced by this DBG_LABEL instruction.
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl...
Definition: Operator.h:66
constexpr double e
Definition: MathExtras.h:57
static unsigned getKind(unsigned Flags)
Definition: InlineAsm.h:325
bool isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx=nullptr) const
Return true if the use operand of the specified index is tied to a def operand.
virtual const TargetRegisterClass * getMatchingSuperRegClass(const TargetRegisterClass *A, const TargetRegisterClass *B, unsigned Idx) const
Return a subclass of the specified register class A so that each register in it has a sub-register of...
bool isSuperRegister(MCRegister RegA, MCRegister RegB) const
Returns true if RegB is a super-register of RegA.
bool allowReciprocal() const
Definition: Operator.h:209
MDNode * getHeapAllocMarker() const
Helper to extract a heap alloc marker if one has been added.
Definition: MachineInstr.h:612
bool isPredicable() const
Return true if this instruction has a predicate operand that controls execution.
Definition: MCInstrDesc.h:336
void print(raw_ostream &OS, bool IsStandalone=true, bool SkipOpers=false, bool SkipDebugLoc=false, bool AddNewLine=true, const TargetInstrInfo *TII=nullptr) const
Print this MI to OS.
bool hasComplexRegisterTies() const
Return true when an instruction has tied register that can&#39;t be determined by the instruction&#39;s descr...
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:223
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
void incorporateFunction(const Function &F)
Incorporate the given function.
Definition: AsmWriter.cpp:848
bool isStackAligningInlineAsm() const
static unsigned getHashValue(const MachineInstr *const &MI)
static bool isMemKind(unsigned Flag)
Definition: InlineAsm.h:277
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
StringRef getName(unsigned Opcode) const
Returns the name for the instructions with the given opcode.
Definition: MCInstrInfo.h:50
size_t size() const
Definition: SmallVector.h:52
Optional< unsigned > getFoldedSpillSize(const TargetInstrInfo *TII) const
Return a valid size if the instruction is a folded spill instruction.
static uint16_t copyFlagsFromInstruction(const Instruction &I)
bool isDebugInstr() const
unsigned getBundleSize() const
Return the number of instructions inside the MI bundle, excluding the bundle header.
static void printSymbol(raw_ostream &OS, MCSymbol &Sym)
Print a MCSymbol as an operand.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned getNumExplicitOperands() const
Returns the number of non-implicit operands.
const DIExpression * getDebugExpression() const
Return the complex address expression referenced by this DBG_VALUE instruction.
MachineOperand * findRegisterDefOperand(Register Reg, bool isDead=false, bool Overlap=false, const TargetRegisterInfo *TRI=nullptr)
Wrapper for findRegisterDefOperandIdx, it returns a pointer to the MachineOperand rather than an inde...
MCSymbol * getPostInstrSymbol() const
Helper to extract a post-instruction symbol if one has been added.
Definition: MachineInstr.h:600
void eraseFromBundle()
Unlink &#39;this&#39; form its basic block and delete it.
void setIsKill(bool Val=true)
void copyIRFlags(const Instruction &I)
Copy all flags to MachineInst MIFlags.
TargetIntrinsicInfo - Interface to description of machine instruction set.
SmallBitVector & set()
Representation for a specific memory location.
int getOperandConstraint(unsigned OpNum, MCOI::OperandConstraint Constraint) const
Returns the value of the specific constraint if it is set.
Definition: MCInstrDesc.h:212
MachineInstr * removeFromBundle()
Unlink this instruction from its basic block and return it without deleting it.
virtual bool hasLoadFromStackSlot(const MachineInstr &MI, SmallVectorImpl< const MachineMemOperand *> &Accesses) const
If the specified machine instruction has a load from a stack slot, return true along with the FrameIn...
void cloneMemRefs(MachineFunction &MF, const MachineInstr &MI)
Clone another MachineInstr&#39;s memory reference descriptor list and replace ours with it...
unsigned findTiedOperandIdx(unsigned OpIdx) const
Given the index of a tied register operand, find the operand it is tied to.
Iterator for intrusive lists based on ilist_node.
ConstMIBundleOperands - Iterate over all operands in a const bundle of machine instructions.
InlineAsm::AsmDialect getInlineAsmDialect() const
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:390
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.
Definition: MachineInstr.h:567
void setFlags(unsigned flags)
Definition: MachineInstr.h:321
void print(raw_ostream &os, const TargetRegisterInfo *TRI=nullptr, const TargetIntrinsicInfo *IntrinsicInfo=nullptr) const
Print the MachineOperand to os.
iterator_range< use_iterator > use_operands(unsigned Reg) const
bool isDereferenceableInvariantLoad(AAResults *AA) const
Return true if this load instruction never traps and points to a memory location whose value doesn&#39;t ...
bool isDebugValue() const
MachineOperand class - Representation of each machine instruction operand.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition: Operator.h:246
Module.h This file contains the declarations for the Module class.
iterator end() const
Definition: ArrayRef.h:147
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:644
bool hasOrderedMemoryRef() const
Return true if this instruction may have an ordered or volatile memory reference, or if the informati...
void updateDbgValueForSpill(MachineInstr &Orig, int FrameIndex)
Update a DBG_VALUE whose value has been spilled to FrameIndex.
unsigned getNumDefs() const
Return the number of MachineOperands that are register definitions.
Definition: MCInstrDesc.h:250
bool isVariadic(QueryType Type=IgnoreBundle) const
Return true if this instruction can have a variable number of operands.
Definition: MachineInstr.h:656
MachineInstr * remove(MachineInstr *I)
Remove the unbundled instruction from the instruction list without deleting it.
int64_t getImm() const
void setMetadata(const MDNode *MD)
DWARF expression.
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
MCSymbol reference (for debug/eh info)
static bool clobbersPhysReg(const uint32_t *RegMask, unsigned PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
bool isSubRegister(MCRegister RegA, MCRegister RegB) const
Returns true if RegB is a sub-register of RegA.
bool hasProperty(unsigned MCFlag, QueryType Type=AnyInBundle) const
Return true if the instruction (or in the case of a bundle, the instructions inside the bundle) has t...
Definition: MachineInstr.h:635
Special value supplied for machine level alias analysis.
static unsigned getReg(const void *D, unsigned RC, unsigned RegNo)
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition: Hashing.h:478
void addImplicitDefUseOperands(MachineFunction &MF)
Add all implicit def and use operands to this instruction.
int findRegisterUseOperandIdx(Register Reg, bool isKill=false, const TargetRegisterInfo *TRI=nullptr) const
Returns the operand index that is a use of the specific register or -1 if it is not found...
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array...
Definition: ArrayRef.h:188
static void printSubRegIdx(raw_ostream &OS, uint64_t Index, const TargetRegisterInfo *TRI)
Print a subreg index operand.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:387
static unsigned getSpillSlotSize(MMOList &Accesses, const MachineFrameInfo &MFI)
const unsigned TiedMax
QueryType
API for querying MachineInstr properties.
Definition: MachineInstr.h:624
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:271
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
static bool hasRegClassConstraint(unsigned Flag, unsigned &RC)
hasRegClassConstraint - Returns true if the flag contains a register class constraint.
Definition: InlineAsm.h:351
const TargetRegisterClass * getCommonSubClass(const TargetRegisterClass *A, const TargetRegisterClass *B) const
Find the largest common subclass of A and B.
Representation of each machine instruction.
Definition: MachineInstr.h:63
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
virtual bool hasStoreToStackSlot(const MachineInstr &MI, SmallVectorImpl< const MachineMemOperand *> &Accesses) const
If the specified machine instruction has a store to a stack slot, return true along with the FrameInd...
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
static cl::opt< bool > UseTBAA("use-tbaa-in-sched-mi", cl::Hidden, cl::init(true), cl::desc("Enable use of TBAA during MI DAG construction"))
virtual unsigned isLoadFromStackSlotPostFE(const MachineInstr &MI, int &FrameIndex) const
Check for post-frame ptr elimination stack locations as well.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void setPreInstrSymbol(MachineFunction &MF, MCSymbol *Symbol)
Set a symbol that will be emitted just prior to the instruction itself.
static void moveOperands(MachineOperand *Dst, MachineOperand *Src, unsigned NumOps, MachineRegisterInfo *MRI)
Move NumOps MachineOperands from Src to Dst, with support for overlapping ranges. ...
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
This file provides utility analysis objects describing memory locations.
void cloneMergedMemRefs(MachineFunction &MF, ArrayRef< const MachineInstr *> MIs)
Clone the merge of multiple MachineInstrs&#39; memory reference descriptors list and replace ours with it...
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
iterator_range< pointee_iterator< WrappedIteratorT > > make_pointee_range(RangeT &&Range)
Definition: iterator.h:302
static bool hasIdenticalMMOs(ArrayRef< MachineMemOperand *> LHS, ArrayRef< MachineMemOperand *> RHS)
Check to see if the MMOs pointed to by the two MemRefs arrays are identical.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
uint32_t Size
Definition: Profile.cpp:46
void markUsesInDebugValueAsUndef(unsigned Reg) const
markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the specified register as undefined wh...
void setPhysRegsDeadExcept(ArrayRef< Register > UsedRegs, const TargetRegisterInfo &TRI)
Mark every physreg used by this instruction as dead except those in the UsedRegs list.
bool isLoadFoldBarrier() const
Returns true if it is illegal to fold a load across this instruction.
bool isKill() const
const Module * getModule() const
bool isDebugEntryValue() const
A DBG_VALUE is an entry value iff its debug expression contains the DW_OP_LLVM_entry_value operation...
MachineInstr::ExtraInfo * createMIExtraInfo(ArrayRef< MachineMemOperand *> MMOs, MCSymbol *PreInstrSymbol=nullptr, MCSymbol *PostInstrSymbol=nullptr, MDNode *HeapAllocMarker=nullptr)
Allocate and construct an extra info structure for a MachineInstr.
MachineInstr * removeFromParent()
Unlink &#39;this&#39; from the containing basic block, and return it without deleting it. ...
unsigned getNumExplicitDefs() const
Returns the number of non-implicit definitions.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
uint16_t getFlags() const
Return the MI flags bitvector.
Definition: MachineInstr.h:307
void printAsOperand(raw_ostream &OS, const Module *M=nullptr) const
Print as operand.
Definition: AsmWriter.cpp:4435
bool mayLoad(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read memory.
Definition: MachineInstr.h:856
bool memoperands_empty() const
Return true if we don&#39;t have any memory operands which described the memory access done by this instr...
Definition: MachineInstr.h:579
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isIdenticalTo(const MachineInstr &Other, MICheckType Check=CheckDefs) const
Return true if this instruction is identical to Other.
void setMemRefs(MachineFunction &MF, ArrayRef< MachineMemOperand *> MemRefs)
Assign this MachineInstr&#39;s memory reference descriptor list.
void unbundleFromSucc()
Break bundle below this instruction.
bool isSafeToMove(AAResults *AA, bool &SawStore) const
Return true if it is safe to move this instruction.
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:575
int64_t getOffset() const
For normal values, this is a byte offset added to the base address.
LLVM Value Representation.
Definition: Value.h:74
void addRegisterDefined(Register Reg, const TargetRegisterInfo *RegInfo=nullptr)
We have determined MI defines a register.
void deallocateOperandArray(OperandCapacity Cap, MachineOperand *Array)
Dellocate an array of MachineOperands and recycle the memory.
void moveOperands(MachineOperand *Dst, MachineOperand *Src, unsigned NumOps)
Move NumOps operands from Src to Dst, updating use-def lists as needed.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:69
bool isPosition() const
const DILocalVariable * getDebugVariable() const
Return the debug variable referenced by this DBG_VALUE instruction.
const MCOperandInfo * OpInfo
Definition: MCInstrDesc.h:199
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
void ChangeToFrameIndex(int Idx)
Replace this operand with a frame index.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
bool allowReassoc() const
Flag queries.
Definition: Operator.h:205
bool hasUnmodeledSideEffects() const
Return true if this instruction has side effects that are not modeled by mayLoad / mayStore...
IRTranslator LLVM IR MI
Convenience struct for specifying and reasoning about fast-math flags.
Definition: Operator.h:160
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
void RemoveOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with...
bool approxFunc() const
Definition: Operator.h:211
Register getReg() const
getReg - Returns the register number.
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1083
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:431
void clearRegisterKills(Register Reg, const TargetRegisterInfo *RegInfo)
Clear all kill flags affecting Reg.
bool isIndirectDebugValue() const
A DBG_VALUE is indirect iff the first operand is a register and the second operand is an immediate...
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
bool getFlag(MIFlag Flag) const
Return whether an MI flag is set.
Definition: MachineInstr.h:312
MachineOperand * allocateOperandArray(OperandCapacity Cap)
Allocate an array of MachineOperands.
const MDNode * getMetadata() const
bool isSpillSlotObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a spill slot.
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:153
const MCPhysReg * ImplicitUses
Definition: MCInstrDesc.h:197
bool noSignedZeros() const
Definition: Operator.h:208
mmo_iterator memoperands_end() const
Access to memory operands of the instruction.
Definition: MachineInstr.h:574
virtual const TargetRegisterClass * getPointerRegClass(const MachineFunction &MF, unsigned Kind=0) const
Returns a TargetRegisterClass used for pointer values.
bool isImplicit() const
Metadata reference (for debug info)
bool hasTrivialDestructor() const
Check whether this has a trivial destructor.
Definition: DebugLoc.h:69
void tieOperands(unsigned DefIdx, unsigned UseIdx)
Add a tie between the register operands at DefIdx and UseIdx.
void addRegOperandToUseList(MachineOperand *MO)
Add MO to the linked list of operands for its register.