blob: dc1ad953e71d460a10ba8f770ad2fd3edfdfed6e [file] [log] [blame]
Eugene Zelenko1d081e62017-05-31 01:10:10 +00001//===- lib/CodeGen/MachineInstr.cpp ---------------------------------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Brian Gaeke21326fc2004-02-13 04:39:32 +00009//
10// Methods common to all machine instructions.
11//
Chris Lattner035dfbe2002-08-09 20:08:06 +000012//===----------------------------------------------------------------------===//
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000013
Chandler Carruthe3e43d92017-06-06 11:49:48 +000014#include "llvm/CodeGen/MachineInstr.h"
Eugene Zelenko1d081e62017-05-31 01:10:10 +000015#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/ArrayRef.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/ADT/FoldingSet.h"
18#include "llvm/ADT/Hashing.h"
Eugene Zelenko1d081e62017-05-31 01:10:10 +000019#include "llvm/ADT/None.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000020#include "llvm/ADT/STLExtras.h"
Francis Visoiu Mistrihfd11bc02017-12-07 10:40:31 +000021#include "llvm/ADT/SmallBitVector.h"
Eugene Zelenko1d081e62017-05-31 01:10:10 +000022#include "llvm/ADT/SmallString.h"
23#include "llvm/ADT/SmallVector.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/Analysis/AliasAnalysis.h"
Hiroshi Inoue059bd0e2017-06-24 15:17:38 +000025#include "llvm/Analysis/Loads.h"
Eugene Zelenko1d081e62017-05-31 01:10:10 +000026#include "llvm/Analysis/MemoryLocation.h"
27#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
28#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner8517e1f2004-02-19 16:17:08 +000029#include "llvm/CodeGen/MachineFunction.h"
Reid Kleckner13fb5a32016-04-14 18:29:59 +000030#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko1d081e62017-05-31 01:10:10 +000031#include "llvm/CodeGen/MachineInstrBundle.h"
Dan Gohmanc76909a2009-09-25 20:36:54 +000032#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesend519de02011-07-02 03:53:34 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
Eugene Zelenko1d081e62017-05-31 01:10:10 +000034#include "llvm/CodeGen/MachineOperand.h"
Chris Lattner62ed6b92008-01-01 01:12:31 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000036#include "llvm/CodeGen/PseudoSourceValue.h"
David Blaikie48319232017-11-08 01:01:31 +000037#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000038#include "llvm/CodeGen/TargetRegisterInfo.h"
39#include "llvm/CodeGen/TargetSubtargetInfo.h"
Nico Weber0f38c602018-04-30 14:59:11 +000040#include "llvm/Config/llvm-config.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000041#include "llvm/IR/Constants.h"
Eugene Zelenko1d081e62017-05-31 01:10:10 +000042#include "llvm/IR/DebugInfoMetadata.h"
43#include "llvm/IR/DebugLoc.h"
44#include "llvm/IR/DerivedTypes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000045#include "llvm/IR/Function.h"
46#include "llvm/IR/InlineAsm.h"
Eugene Zelenko1d081e62017-05-31 01:10:10 +000047#include "llvm/IR/InstrTypes.h"
Tim Northover9c9955b2016-07-29 20:32:59 +000048#include "llvm/IR/Intrinsics.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000049#include "llvm/IR/LLVMContext.h"
50#include "llvm/IR/Metadata.h"
51#include "llvm/IR/Module.h"
Duncan P. N. Exon Smitha08efbf2015-06-26 22:06:47 +000052#include "llvm/IR/ModuleSlotTracker.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000053#include "llvm/IR/Type.h"
54#include "llvm/IR/Value.h"
Michael Berg9de32f72018-09-19 18:52:08 +000055#include "llvm/IR/Operator.h"
Evan Chenge837dea2011-06-28 19:10:37 +000056#include "llvm/MC/MCInstrDesc.h"
Eugene Zelenko1d081e62017-05-31 01:10:10 +000057#include "llvm/MC/MCRegisterInfo.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000058#include "llvm/MC/MCSymbol.h"
Eugene Zelenko1d081e62017-05-31 01:10:10 +000059#include "llvm/Support/Casting.h"
Daniel Sandersc457f1e2015-08-19 12:03:04 +000060#include "llvm/Support/CommandLine.h"
Eugene Zelenko1d081e62017-05-31 01:10:10 +000061#include "llvm/Support/Compiler.h"
David Greene3b325332010-01-04 23:48:20 +000062#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000063#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko1d081e62017-05-31 01:10:10 +000064#include "llvm/Support/LowLevelTypeImpl.h"
Dan Gohmance42e402008-07-07 20:32:02 +000065#include "llvm/Support/MathExtras.h"
Chris Lattneredfb72c2008-08-24 20:37:32 +000066#include "llvm/Support/raw_ostream.h"
Tim Northover9c9955b2016-07-29 20:32:59 +000067#include "llvm/Target/TargetIntrinsicInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000068#include "llvm/Target/TargetMachine.h"
Eugene Zelenko1d081e62017-05-31 01:10:10 +000069#include <algorithm>
70#include <cassert>
71#include <cstddef>
72#include <cstdint>
73#include <cstring>
74#include <iterator>
75#include <utility>
76
Chris Lattner0742b592004-02-23 18:38:20 +000077using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000078
Francis Visoiu Mistrih23b46122018-01-18 14:52:14 +000079static const MachineFunction *getMFIfAvailable(const MachineInstr &MI) {
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.
88static 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
Jakob Stoklund Olesen9500e5d2012-12-20 22:53:58 +0000102void MachineInstr::addImplicitDefUseOperands(MachineFunction &MF) {
Evan Chenge837dea2011-06-28 19:10:37 +0000103 if (MCID->ImplicitDefs)
Craig Topper79402ee2015-12-05 07:13:35 +0000104 for (const MCPhysReg *ImpDefs = MCID->getImplicitDefs(); *ImpDefs;
105 ++ImpDefs)
Jakob Stoklund Olesen9500e5d2012-12-20 22:53:58 +0000106 addOperand(MF, MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Chenge837dea2011-06-28 19:10:37 +0000107 if (MCID->ImplicitUses)
Craig Topper79402ee2015-12-05 07:13:35 +0000108 for (const MCPhysReg *ImpUses = MCID->getImplicitUses(); *ImpUses;
109 ++ImpUses)
Jakob Stoklund Olesen9500e5d2012-12-20 22:53:58 +0000110 addOperand(MF, MachineOperand::CreateReg(*ImpUses, false, true));
Evan Chengd7de4962006-11-13 23:34:06 +0000111}
112
Bob Wilson0855cad2010-04-09 04:34:03 +0000113/// MachineInstr ctor - This constructor creates a MachineInstr and adds the
114/// implicit operands. It reserves space for the number of operands specified by
Evan Chenge837dea2011-06-28 19:10:37 +0000115/// the MCInstrDesc.
Jakob Stoklund Olesen9500e5d2012-12-20 22:53:58 +0000116MachineInstr::MachineInstr(MachineFunction &MF, const MCInstrDesc &tid,
Benjamin Kramer74cdff82015-02-07 12:28:15 +0000117 DebugLoc dl, bool NoImp)
Eugene Zelenko1d081e62017-05-31 01:10:10 +0000118 : MCID(&tid), debugLoc(std::move(dl)) {
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +0000119 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
120
Jakob Stoklund Olesenf1d015f2013-01-05 05:00:09 +0000121 // 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
Dale Johannesen06efc022009-01-27 23:20:29 +0000128 if (!NoImp)
Jakob Stoklund Olesen9500e5d2012-12-20 22:53:58 +0000129 addImplicitDefUseOperands(MF);
Dale Johannesen06efc022009-01-27 23:20:29 +0000130}
131
Misha Brukmance22e762004-07-09 14:45:17 +0000132/// MachineInstr ctor - Copies MachineInstr arg exactly
133///
Evan Cheng1ed99222008-07-19 00:37:25 +0000134MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000135 : MCID(&MI.getDesc()), Info(MI.Info), debugLoc(MI.getDebugLoc()) {
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +0000136 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
137
Jakob Stoklund Olesenf1d015f2013-01-05 05:00:09 +0000138 CapOperands = OperandCapacity::get(MI.getNumOperands());
139 Operands = MF.allocateOperandArray(CapOperands);
Tanya Lattnerb5159ed2004-05-23 20:58:02 +0000140
Jakob Stoklund Olesen84be3d52013-01-05 05:05:51 +0000141 // Copy operands.
Benjamin Kramer59d81db2015-02-21 17:08:08 +0000142 for (const MachineOperand &MO : MI.operands())
143 addOperand(MF, MO);
Tanya Lattner0c63e032004-05-24 03:14:18 +0000144
Jakob Stoklund Olesenbd7b36e2012-12-18 21:36:05 +0000145 // Copy all the sensible flags.
146 setFlags(MI.Flags);
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000147}
148
Chris Lattner62ed6b92008-01-01 01:12:31 +0000149/// getRegInfo - If this instruction is embedded into a MachineFunction,
150/// return the MachineRegisterInfo object for the current function, otherwise
151/// return null.
152MachineRegisterInfo *MachineInstr::getRegInfo() {
153 if (MachineBasicBlock *MBB = getParent())
Dan Gohman4e526b92008-07-08 23:59:09 +0000154 return &MBB->getParent()->getRegInfo();
Craig Topper4ba84432014-04-14 00:51:57 +0000155 return nullptr;
Chris Lattner62ed6b92008-01-01 01:12:31 +0000156}
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.
Jakob Stoklund Olesenff2b99a2012-08-09 22:49:37 +0000161void MachineInstr::RemoveRegOperandsFromUseLists(MachineRegisterInfo &MRI) {
Benjamin Kramer59d81db2015-02-21 17:08:08 +0000162 for (MachineOperand &MO : operands())
163 if (MO.isReg())
164 MRI.removeRegOperandFromUseList(&MO);
Chris Lattner62ed6b92008-01-01 01:12:31 +0000165}
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.
Jakob Stoklund Olesenff2b99a2012-08-09 22:49:37 +0000170void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &MRI) {
Benjamin Kramer59d81db2015-02-21 17:08:08 +0000171 for (MachineOperand &MO : operands())
172 if (MO.isReg())
173 MRI.addRegOperandToUseList(&MO);
Chris Lattner62ed6b92008-01-01 01:12:31 +0000174}
175
Jakob Stoklund Olesen56706db2012-12-20 22:54:05 +0000176void MachineInstr::addOperand(const MachineOperand &Op) {
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
Jakob Stoklund Olesenf1d015f2013-01-05 05:00:09 +0000184/// Move NumOps MachineOperands from Src to Dst, with support for overlapping
185/// ranges. If MRI is non-null also update use-def chains.
186static void moveOperands(MachineOperand *Dst, MachineOperand *Src,
187 unsigned NumOps, MachineRegisterInfo *MRI) {
188 if (MRI)
189 return MRI->moveOperands(Dst, Src, NumOps);
190
JF Bastien2b9ff6b2016-03-26 18:20:02 +0000191 // MachineOperand is a trivially copyable type so we can just use memmove.
Benjamin Kramer7efcb332015-02-21 16:22:48 +0000192 std::memmove(Dst, Src, NumOps * sizeof(MachineOperand));
Jakob Stoklund Olesenf1d015f2013-01-05 05:00:09 +0000193}
194
Chris Lattner62ed6b92008-01-01 01:12:31 +0000195/// 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
Jim Grosbachee61d672011-08-24 16:44:17 +0000198/// (before the first implicit operand).
Jakob Stoklund Olesen56706db2012-12-20 22:54:05 +0000199void MachineInstr::addOperand(MachineFunction &MF, const MachineOperand &Op) {
Jakob Stoklund Olesen3627a462011-09-29 00:40:51 +0000200 assert(MCID && "Cannot add operands before providing an instr descriptor");
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000201
Jakob Stoklund Olesenf1d015f2013-01-05 05:00:09 +0000202 // 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 }
Jim Grosbachee61d672011-08-24 16:44:17 +0000210
Jakob Stoklund Olesen3627a462011-09-29 00:40:51 +0000211 // Find the insert location for the new operand. Implicit registers go at
Jakob Stoklund Olesenf1d015f2013-01-05 05:00:09 +0000212 // the end, everything else goes before the implicit regs.
213 //
Jakob Stoklund Olesen3627a462011-09-29 00:40:51 +0000214 // 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.
Jakob Stoklund Olesenf1d015f2013-01-05 05:00:09 +0000218 unsigned OpNo = getNumOperands();
219 bool isImpReg = Op.isReg() && Op.isImplicit();
Jakob Stoklund Olesen3627a462011-09-29 00:40:51 +0000220 if (!isImpReg && !isInlineAsm()) {
221 while (OpNo && Operands[OpNo-1].isReg() && Operands[OpNo-1].isImplicit()) {
222 --OpNo;
Jakob Stoklund Olesen9c130672012-09-04 18:36:28 +0000223 assert(!Operands[OpNo].isTied() && "Cannot move tied operands");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000224 }
225 }
Jim Grosbachee61d672011-08-24 16:44:17 +0000226
Pekka Jaaskelainend54946a2013-10-15 14:40:46 +0000227#ifndef NDEBUG
Hans Wennborg3292b912019-01-25 00:12:01 +0000228 bool isDebugOp = Op.getType() == MachineOperand::MO_Metadata ||
229 Op.getType() == MachineOperand::MO_MCSymbol;
Jakob Stoklund Olesen3627a462011-09-29 00:40:51 +0000230 // OpNo now points as the desired insertion point. Unless this is a variadic
231 // instruction, only implicit regs are allowed beyond MCID->getNumOperands().
Jakob Stoklund Olesen33a537a2012-07-04 23:53:23 +0000232 // RegMask operands go between the explicit and implicit operands.
233 assert((isImpReg || Op.isRegMask() || MCID->isVariadic() ||
Hans Wennborg3292b912019-01-25 00:12:01 +0000234 OpNo < MCID->getNumOperands() || isDebugOp) &&
Jakob Stoklund Olesen3627a462011-09-29 00:40:51 +0000235 "Trying to add an operand to a machine instr that is already done!");
Pekka Jaaskelainend54946a2013-10-15 14:40:46 +0000236#endif
Chris Lattner62ed6b92008-01-01 01:12:31 +0000237
Jakob Stoklund Olesenf1d015f2013-01-05 05:00:09 +0000238 MachineRegisterInfo *MRI = getRegInfo();
Chris Lattner62ed6b92008-01-01 01:12:31 +0000239
Jakob Stoklund Olesenf1d015f2013-01-05 05:00:09 +0000240 // 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 }
Chris Lattner62ed6b92008-01-01 01:12:31 +0000251
Jakob Stoklund Olesenf1d015f2013-01-05 05:00:09 +0000252 // Move the operands following the insertion point.
253 if (OpNo != NumOperands)
254 moveOperands(Operands + OpNo + 1, OldOperands + OpNo, NumOperands - OpNo,
255 MRI);
256 ++NumOperands;
Jim Grosbachee61d672011-08-24 16:44:17 +0000257
Jakob Stoklund Olesenf1d015f2013-01-05 05:00:09 +0000258 // 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()) {
Jakob Stoklund Olesenff2b99a2012-08-09 22:49:37 +0000268 // Ensure isOnRegUseList() returns false, regardless of Op's status.
Craig Topper4ba84432014-04-14 00:51:57 +0000269 NewMO->Contents.Reg.Prev = nullptr;
Jakob Stoklund Olesen9c130672012-09-04 18:36:28 +0000270 // Ignore existing ties. This is not a property that can be copied.
Jakob Stoklund Olesenf1d015f2013-01-05 05:00:09 +0000271 NewMO->TiedTo = 0;
272 // Add the new operand to MRI, but only for instructions in an MBB.
273 if (MRI)
274 MRI->addRegOperandToUseList(NewMO);
Jakob Stoklund Olesene941df52012-08-30 14:39:06 +0000275 // 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) {
Jakob Stoklund Olesen9c130672012-09-04 18:36:28 +0000279 // Tie uses to defs as indicated in MCInstrDesc.
Jakob Stoklund Olesenf1d015f2013-01-05 05:00:09 +0000280 if (NewMO->isUse()) {
Jakob Stoklund Olesene941df52012-08-30 14:39:06 +0000281 int DefIdx = MCID->getOperandConstraint(OpNo, MCOI::TIED_TO);
Jakob Stoklund Olesen94083142012-08-31 20:50:53 +0000282 if (DefIdx != -1)
283 tieOperands(DefIdx, OpNo);
Jakob Stoklund Olesen4ba69162012-08-28 18:34:41 +0000284 }
Jakob Stoklund Olesene941df52012-08-30 14:39:06 +0000285 // If the register operand is flagged as early, mark the operand as such.
286 if (MCID->getOperandConstraint(OpNo, MCOI::EARLY_CLOBBER) != -1)
Jakob Stoklund Olesenf1d015f2013-01-05 05:00:09 +0000287 NewMO->setIsEarlyClobber(true);
Chris Lattner62ed6b92008-01-01 01:12:31 +0000288 }
Chris Lattner62ed6b92008-01-01 01:12:31 +0000289 }
290}
291
292/// RemoveOperand - Erase an operand from an instruction, leaving it with one
293/// fewer operand than it started with.
294///
295void MachineInstr::RemoveOperand(unsigned OpNo) {
Jakob Stoklund Olesen021e3b62012-12-22 17:13:06 +0000296 assert(OpNo < getNumOperands() && "Invalid operand number");
Jakob Stoklund Olesen699ac042012-08-29 00:37:58 +0000297 untieRegOperand(OpNo);
Jim Grosbachee61d672011-08-24 16:44:17 +0000298
Jakob Stoklund Olesen9c130672012-09-04 18:36:28 +0000299#ifndef NDEBUG
300 // Moving tied operands would break the ties.
Jakob Stoklund Olesen021e3b62012-12-22 17:13:06 +0000301 for (unsigned i = OpNo + 1, e = getNumOperands(); i != e; ++i)
Jakob Stoklund Olesen9c130672012-09-04 18:36:28 +0000302 if (Operands[i].isReg())
303 assert(!Operands[i].isTied() && "Cannot move tied operands");
304#endif
305
Jakob Stoklund Olesenf1d015f2013-01-05 05:00:09 +0000306 MachineRegisterInfo *MRI = getRegInfo();
307 if (MRI && Operands[OpNo].isReg())
308 MRI->removeRegOperandFromUseList(Operands + OpNo);
Chris Lattner62ed6b92008-01-01 01:12:31 +0000309
Jakob Stoklund Olesenf1d015f2013-01-05 05:00:09 +0000310 // 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;
Chris Lattner62ed6b92008-01-01 01:12:31 +0000317}
318
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000319void MachineInstr::dropMemRefs(MachineFunction &MF) {
320 if (memoperands_empty())
321 return;
322
323 // See if we can just drop all of our extra info.
324 if (!getPreInstrSymbol() && !getPostInstrSymbol()) {
325 Info.clear();
326 return;
327 }
328 if (!getPostInstrSymbol()) {
329 Info.set<EIIK_PreInstrSymbol>(getPreInstrSymbol());
330 return;
331 }
332 if (!getPreInstrSymbol()) {
333 Info.set<EIIK_PostInstrSymbol>(getPostInstrSymbol());
334 return;
335 }
336
337 // Otherwise allocate a fresh extra info with just these symbols.
338 Info.set<EIIK_OutOfLine>(
339 MF.createMIExtraInfo({}, getPreInstrSymbol(), getPostInstrSymbol()));
340}
341
342void MachineInstr::setMemRefs(MachineFunction &MF,
343 ArrayRef<MachineMemOperand *> MMOs) {
344 if (MMOs.empty()) {
345 dropMemRefs(MF);
346 return;
347 }
348
349 // Try to store a single MMO inline.
350 if (MMOs.size() == 1 && !getPreInstrSymbol() && !getPostInstrSymbol()) {
351 Info.set<EIIK_MMO>(MMOs[0]);
352 return;
353 }
354
355 // Otherwise create an extra info struct with all of our info.
356 Info.set<EIIK_OutOfLine>(
357 MF.createMIExtraInfo(MMOs, getPreInstrSymbol(), getPostInstrSymbol()));
358}
359
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000360void MachineInstr::addMemOperand(MachineFunction &MF,
Dan Gohmanc76909a2009-09-25 20:36:54 +0000361 MachineMemOperand *MO) {
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000362 SmallVector<MachineMemOperand *, 2> MMOs;
363 MMOs.append(memoperands_begin(), memoperands_end());
364 MMOs.push_back(MO);
365 setMemRefs(MF, MMOs);
366}
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000367
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000368void MachineInstr::cloneMemRefs(MachineFunction &MF, const MachineInstr &MI) {
369 if (this == &MI)
370 // Nothing to do for a self-clone!
371 return;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000372
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000373 assert(&MF == MI.getMF() &&
374 "Invalid machine functions when cloning memory refrences!");
375 // See if we can just steal the extra info already allocated for the
376 // instruction. We can do this whenever the pre- and post-instruction symbols
377 // are the same (including null).
378 if (getPreInstrSymbol() == MI.getPreInstrSymbol() &&
379 getPostInstrSymbol() == MI.getPostInstrSymbol()) {
380 Info = MI.Info;
381 return;
382 }
383
384 // Otherwise, fall back on a copy-based clone.
385 setMemRefs(MF, MI.memoperands());
Dan Gohmanc76909a2009-09-25 20:36:54 +0000386}
Chris Lattner62ed6b92008-01-01 01:12:31 +0000387
Philip Reames0c161762016-01-06 19:33:12 +0000388/// Check to see if the MMOs pointed to by the two MemRefs arrays are
Junmo Park64739272016-02-26 02:07:36 +0000389/// identical.
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000390static bool hasIdenticalMMOs(ArrayRef<MachineMemOperand *> LHS,
391 ArrayRef<MachineMemOperand *> RHS) {
392 if (LHS.size() != RHS.size())
Philip Reames0c161762016-01-06 19:33:12 +0000393 return false;
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000394
395 auto LHSPointees = make_pointee_range(LHS);
396 auto RHSPointees = make_pointee_range(RHS);
397 return std::equal(LHSPointees.begin(), LHSPointees.end(),
398 RHSPointees.begin());
Philip Reames0c161762016-01-06 19:33:12 +0000399}
400
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000401void MachineInstr::cloneMergedMemRefs(MachineFunction &MF,
402 ArrayRef<const MachineInstr *> MIs) {
403 // Try handling easy numbers of MIs with simpler mechanisms.
404 if (MIs.empty()) {
405 dropMemRefs(MF);
406 return;
407 }
408 if (MIs.size() == 1) {
409 cloneMemRefs(MF, *MIs[0]);
410 return;
411 }
412 // Because an empty memoperands list provides *no* information and must be
413 // handled conservatively (assuming the instruction can do anything), the only
414 // way to merge with it is to drop all other memoperands.
415 if (MIs[0]->memoperands_empty()) {
416 dropMemRefs(MF);
417 return;
418 }
Philip Reames0c161762016-01-06 19:33:12 +0000419
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000420 // Handle the general case.
421 SmallVector<MachineMemOperand *, 2> MergedMMOs;
422 // Start with the first instruction.
423 assert(&MF == MIs[0]->getMF() &&
424 "Invalid machine functions when cloning memory references!");
425 MergedMMOs.append(MIs[0]->memoperands_begin(), MIs[0]->memoperands_end());
426 // Now walk all the other instructions and accumulate any different MMOs.
427 for (const MachineInstr &MI : make_pointee_range(MIs.slice(1))) {
428 assert(&MF == MI.getMF() &&
429 "Invalid machine functions when cloning memory references!");
Philip Reames0c161762016-01-06 19:33:12 +0000430
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000431 // Skip MIs with identical operands to the first. This is a somewhat
432 // arbitrary hack but will catch common cases without being quadratic.
433 // TODO: We could fully implement merge semantics here if needed.
434 if (hasIdenticalMMOs(MIs[0]->memoperands(), MI.memoperands()))
435 continue;
Junmo Park64739272016-02-26 02:07:36 +0000436
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000437 // Because an empty memoperands list provides *no* information and must be
438 // handled conservatively (assuming the instruction can do anything), the
439 // only way to merge with it is to drop all other memoperands.
440 if (MI.memoperands_empty()) {
441 dropMemRefs(MF);
442 return;
443 }
Philip Reames0c161762016-01-06 19:33:12 +0000444
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000445 // Otherwise accumulate these into our temporary buffer of the merged state.
446 MergedMMOs.append(MI.memoperands_begin(), MI.memoperands_end());
447 }
Philip Reamesf5d46752016-01-06 04:39:03 +0000448
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000449 setMemRefs(MF, MergedMMOs);
450}
Junmo Park64739272016-02-26 02:07:36 +0000451
Chandler Carruthe90d4402018-08-16 23:11:05 +0000452void MachineInstr::setPreInstrSymbol(MachineFunction &MF, MCSymbol *Symbol) {
453 MCSymbol *OldSymbol = getPreInstrSymbol();
454 if (OldSymbol == Symbol)
455 return;
456 if (OldSymbol && !Symbol) {
457 // We're removing a symbol rather than adding one. Try to clean up any
458 // extra info carried around.
459 if (Info.is<EIIK_PreInstrSymbol>()) {
460 Info.clear();
461 return;
462 }
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000463
Chandler Carruthe90d4402018-08-16 23:11:05 +0000464 if (memoperands_empty()) {
465 assert(getPostInstrSymbol() &&
466 "Should never have only a single symbol allocated out-of-line!");
467 Info.set<EIIK_PostInstrSymbol>(getPostInstrSymbol());
468 return;
469 }
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000470
Chandler Carruthe90d4402018-08-16 23:11:05 +0000471 // Otherwise fallback on the generic update.
472 } else if (!Info || Info.is<EIIK_PreInstrSymbol>()) {
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000473 // If we don't have any other extra info, we can store this inline.
Chandler Carruthe90d4402018-08-16 23:11:05 +0000474 Info.set<EIIK_PreInstrSymbol>(Symbol);
475 return;
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000476 }
477
Chandler Carruthe90d4402018-08-16 23:11:05 +0000478 // Otherwise, allocate a full new set of extra info.
479 // FIXME: Maybe we should make the symbols in the extra info mutable?
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000480 Info.set<EIIK_OutOfLine>(
Chandler Carruthe90d4402018-08-16 23:11:05 +0000481 MF.createMIExtraInfo(memoperands(), Symbol, getPostInstrSymbol()));
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000482}
483
Chandler Carruthe90d4402018-08-16 23:11:05 +0000484void MachineInstr::setPostInstrSymbol(MachineFunction &MF, MCSymbol *Symbol) {
485 MCSymbol *OldSymbol = getPostInstrSymbol();
486 if (OldSymbol == Symbol)
487 return;
488 if (OldSymbol && !Symbol) {
489 // We're removing a symbol rather than adding one. Try to clean up any
490 // extra info carried around.
491 if (Info.is<EIIK_PostInstrSymbol>()) {
492 Info.clear();
493 return;
494 }
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000495
Chandler Carruthe90d4402018-08-16 23:11:05 +0000496 if (memoperands_empty()) {
497 assert(getPreInstrSymbol() &&
498 "Should never have only a single symbol allocated out-of-line!");
499 Info.set<EIIK_PreInstrSymbol>(getPreInstrSymbol());
500 return;
501 }
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000502
Chandler Carruthe90d4402018-08-16 23:11:05 +0000503 // Otherwise fallback on the generic update.
504 } else if (!Info || Info.is<EIIK_PostInstrSymbol>()) {
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000505 // If we don't have any other extra info, we can store this inline.
Chandler Carruthe90d4402018-08-16 23:11:05 +0000506 Info.set<EIIK_PostInstrSymbol>(Symbol);
507 return;
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000508 }
509
Chandler Carruthe90d4402018-08-16 23:11:05 +0000510 // Otherwise, allocate a full new set of extra info.
511 // FIXME: Maybe we should make the symbols in the extra info mutable?
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000512 Info.set<EIIK_OutOfLine>(
Chandler Carruthe90d4402018-08-16 23:11:05 +0000513 MF.createMIExtraInfo(memoperands(), getPreInstrSymbol(), Symbol));
Philip Reamesf5d46752016-01-06 04:39:03 +0000514}
515
Michael Berg6f176b72018-06-18 18:37:48 +0000516uint16_t MachineInstr::mergeFlagsWith(const MachineInstr &Other) const {
Francis Visoiu Mistrihb34a8c92018-03-14 17:10:58 +0000517 // For now, the just return the union of the flags. If the flags get more
518 // complicated over time, we might need more logic here.
519 return getFlags() | Other.getFlags();
520}
521
Michael Berg9de32f72018-09-19 18:52:08 +0000522void MachineInstr::copyIRFlags(const Instruction &I) {
523 // Copy the wrapping flags.
524 if (const OverflowingBinaryOperator *OB =
525 dyn_cast<OverflowingBinaryOperator>(&I)) {
526 if (OB->hasNoSignedWrap())
527 setFlag(MachineInstr::MIFlag::NoSWrap);
528 if (OB->hasNoUnsignedWrap())
529 setFlag(MachineInstr::MIFlag::NoUWrap);
530 }
531
532 // Copy the exact flag.
533 if (const PossiblyExactOperator *PE = dyn_cast<PossiblyExactOperator>(&I))
534 if (PE->isExact())
535 setFlag(MachineInstr::MIFlag::IsExact);
536
537 // Copy the fast-math flags.
538 if (const FPMathOperator *FP = dyn_cast<FPMathOperator>(&I)) {
539 const FastMathFlags Flags = FP->getFastMathFlags();
540 if (Flags.noNaNs())
541 setFlag(MachineInstr::MIFlag::FmNoNans);
542 if (Flags.noInfs())
543 setFlag(MachineInstr::MIFlag::FmNoInfs);
544 if (Flags.noSignedZeros())
545 setFlag(MachineInstr::MIFlag::FmNsz);
546 if (Flags.allowReciprocal())
547 setFlag(MachineInstr::MIFlag::FmArcp);
548 if (Flags.allowContract())
549 setFlag(MachineInstr::MIFlag::FmContract);
550 if (Flags.approxFunc())
551 setFlag(MachineInstr::MIFlag::FmAfn);
552 if (Flags.allowReassoc())
553 setFlag(MachineInstr::MIFlag::FmReassoc);
554 }
555}
556
Sven van Haastregtb219bbc2018-09-06 10:25:59 +0000557bool MachineInstr::hasPropertyInBundle(uint64_t Mask, QueryType Type) const {
Jakob Stoklund Olesen4aebce82013-01-10 18:42:44 +0000558 assert(!isBundledWithPred() && "Must be called on bundle header");
Duncan P. N. Exon Smith20a62522016-02-22 20:49:58 +0000559 for (MachineBasicBlock::const_instr_iterator MII = getIterator();; ++MII) {
Benjamin Kramer85f9cef2012-03-17 17:03:45 +0000560 if (MII->getDesc().getFlags() & Mask) {
Evan Cheng43d5d4c2011-12-08 19:23:10 +0000561 if (Type == AnyInBundle)
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000562 return true;
563 } else {
Jakob Stoklund Olesenb11f0502013-01-10 01:29:42 +0000564 if (Type == AllInBundle && !MII->isBundle())
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000565 return false;
566 }
Jakob Stoklund Olesenb11f0502013-01-10 01:29:42 +0000567 // This was the last instruction in the bundle.
568 if (!MII->isBundledWithSucc())
569 return Type == AllInBundle;
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000570 }
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000571}
572
Duncan P. N. Exon Smith1d75c8d2016-02-27 20:01:33 +0000573bool MachineInstr::isIdenticalTo(const MachineInstr &Other,
Evan Cheng506049f2010-03-03 01:44:33 +0000574 MICheckType Check) const {
Evan Cheng34cdf6e2010-03-03 21:54:14 +0000575 // If opcodes or number of operands are not the same then the two
576 // instructions are obviously not identical.
Duncan P. N. Exon Smith1d75c8d2016-02-27 20:01:33 +0000577 if (Other.getOpcode() != getOpcode() ||
578 Other.getNumOperands() != getNumOperands())
Evan Cheng34cdf6e2010-03-03 21:54:14 +0000579 return false;
580
Evan Chengddfd1372011-12-14 02:11:42 +0000581 if (isBundle()) {
Bjorn Pettersson0fd1a2c2016-12-19 11:20:57 +0000582 // We have passed the test above that both instructions have the same
583 // opcode, so we know that both instructions are bundles here. Let's compare
584 // MIs inside the bundle.
585 assert(Other.isBundle() && "Expected that both instructions are bundles.");
Duncan P. N. Exon Smith20a62522016-02-22 20:49:58 +0000586 MachineBasicBlock::const_instr_iterator I1 = getIterator();
Duncan P. N. Exon Smith1d75c8d2016-02-27 20:01:33 +0000587 MachineBasicBlock::const_instr_iterator I2 = Other.getIterator();
Bjorn Pettersson0fd1a2c2016-12-19 11:20:57 +0000588 // Loop until we analysed the last intruction inside at least one of the
589 // bundles.
590 while (I1->isBundledWithSucc() && I2->isBundledWithSucc()) {
591 ++I1;
Evan Chengddfd1372011-12-14 02:11:42 +0000592 ++I2;
Bjorn Pettersson0fd1a2c2016-12-19 11:20:57 +0000593 if (!I1->isIdenticalTo(*I2, Check))
Evan Chengddfd1372011-12-14 02:11:42 +0000594 return false;
595 }
Bjorn Pettersson0fd1a2c2016-12-19 11:20:57 +0000596 // If we've reached the end of just one of the two bundles, but not both,
597 // the instructions are not identical.
598 if (I1->isBundledWithSucc() || I2->isBundledWithSucc())
599 return false;
Evan Chengddfd1372011-12-14 02:11:42 +0000600 }
601
Evan Cheng34cdf6e2010-03-03 21:54:14 +0000602 // Check operands to make sure they match.
603 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
604 const MachineOperand &MO = getOperand(i);
Duncan P. N. Exon Smith1d75c8d2016-02-27 20:01:33 +0000605 const MachineOperand &OMO = Other.getOperand(i);
Evan Chengcbc988b2011-05-12 00:56:58 +0000606 if (!MO.isReg()) {
607 if (!MO.isIdenticalTo(OMO))
608 return false;
609 continue;
610 }
611
Evan Cheng34cdf6e2010-03-03 21:54:14 +0000612 // Clients may or may not want to ignore defs when testing for equality.
613 // For example, machine CSE pass only cares about finding common
614 // subexpressions, so it's safe to ignore virtual register defs.
Evan Chengcbc988b2011-05-12 00:56:58 +0000615 if (MO.isDef()) {
Evan Cheng34cdf6e2010-03-03 21:54:14 +0000616 if (Check == IgnoreDefs)
617 continue;
Evan Chengcbc988b2011-05-12 00:56:58 +0000618 else if (Check == IgnoreVRegDefs) {
Diana Picus109da1d2017-10-12 13:59:51 +0000619 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg()) ||
620 !TargetRegisterInfo::isVirtualRegister(OMO.getReg()))
621 if (!MO.isIdenticalTo(OMO))
Evan Chengcbc988b2011-05-12 00:56:58 +0000622 return false;
623 } else {
624 if (!MO.isIdenticalTo(OMO))
Evan Cheng34cdf6e2010-03-03 21:54:14 +0000625 return false;
Evan Chengcbc988b2011-05-12 00:56:58 +0000626 if (Check == CheckKillDead && MO.isDead() != OMO.isDead())
627 return false;
628 }
629 } else {
630 if (!MO.isIdenticalTo(OMO))
631 return false;
632 if (Check == CheckKillDead && MO.isKill() != OMO.isKill())
633 return false;
634 }
Evan Cheng34cdf6e2010-03-03 21:54:14 +0000635 }
Shiva Chen24abe712018-05-09 02:42:00 +0000636 // If DebugLoc does not match then two debug instructions are not identical.
637 if (isDebugInstr())
Duncan P. N. Exon Smith1d75c8d2016-02-27 20:01:33 +0000638 if (getDebugLoc() && Other.getDebugLoc() &&
639 getDebugLoc() != Other.getDebugLoc())
Devang Patel9194c672011-07-07 17:45:33 +0000640 return false;
Evan Cheng34cdf6e2010-03-03 21:54:14 +0000641 return true;
Evan Cheng506049f2010-03-03 01:44:33 +0000642}
643
Justin Bogner9c03aa52017-10-10 23:34:01 +0000644const MachineFunction *MachineInstr::getMF() const {
645 return getParent()->getParent();
646}
647
Chris Lattner48d7c062006-04-17 21:35:41 +0000648MachineInstr *MachineInstr::removeFromParent() {
649 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesen9f4692d2012-12-17 23:55:38 +0000650 return getParent()->remove(this);
Chris Lattner48d7c062006-04-17 21:35:41 +0000651}
652
Jakob Stoklund Olesen9f4692d2012-12-17 23:55:38 +0000653MachineInstr *MachineInstr::removeFromBundle() {
654 assert(getParent() && "Not embedded in a basic block!");
655 return getParent()->remove_instr(this);
656}
Chris Lattner48d7c062006-04-17 21:35:41 +0000657
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000658void MachineInstr::eraseFromParent() {
659 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesen9f4692d2012-12-17 23:55:38 +0000660 getParent()->erase(this);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000661}
662
Gerolf Hoflehner4e917a22014-08-13 21:15:23 +0000663void MachineInstr::eraseFromParentAndMarkDBGValuesForRemoval() {
664 assert(getParent() && "Not embedded in a basic block!");
665 MachineBasicBlock *MBB = getParent();
666 MachineFunction *MF = MBB->getParent();
667 assert(MF && "Not embedded in a function!");
668
669 MachineInstr *MI = (MachineInstr *)this;
670 MachineRegisterInfo &MRI = MF->getRegInfo();
671
Benjamin Kramer59d81db2015-02-21 17:08:08 +0000672 for (const MachineOperand &MO : MI->operands()) {
Gerolf Hoflehner4e917a22014-08-13 21:15:23 +0000673 if (!MO.isReg() || !MO.isDef())
674 continue;
675 unsigned Reg = MO.getReg();
676 if (!TargetRegisterInfo::isVirtualRegister(Reg))
677 continue;
678 MRI.markUsesInDebugValueAsUndef(Reg);
679 }
680 MI->eraseFromParent();
681}
682
Jakob Stoklund Olesen9f4692d2012-12-17 23:55:38 +0000683void MachineInstr::eraseFromBundle() {
684 assert(getParent() && "Not embedded in a basic block!");
685 getParent()->erase_instr(this);
686}
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000687
Evan Cheng19e3f312007-05-15 01:26:09 +0000688unsigned MachineInstr::getNumExplicitOperands() const {
Evan Chenge837dea2011-06-28 19:10:37 +0000689 unsigned NumOperands = MCID->getNumOperands();
690 if (!MCID->isVariadic())
Evan Cheng19e3f312007-05-15 01:26:09 +0000691 return NumOperands;
692
Roman Tereshinb1c42de2018-06-12 18:30:37 +0000693 for (unsigned I = NumOperands, E = getNumOperands(); I != E; ++I) {
694 const MachineOperand &MO = getOperand(I);
695 // The operands must always be in the following order:
696 // - explicit reg defs,
697 // - other explicit operands (reg uses, immediates, etc.),
698 // - implicit reg defs
699 // - implicit reg uses
700 if (MO.isReg() && MO.isImplicit())
701 break;
702 ++NumOperands;
Evan Cheng19e3f312007-05-15 01:26:09 +0000703 }
704 return NumOperands;
705}
706
Roman Tereshinb1c42de2018-06-12 18:30:37 +0000707unsigned MachineInstr::getNumExplicitDefs() const {
708 unsigned NumDefs = MCID->getNumDefs();
709 if (!MCID->isVariadic())
710 return NumDefs;
711
712 for (unsigned I = NumDefs, E = getNumOperands(); I != E; ++I) {
713 const MachineOperand &MO = getOperand(I);
714 if (!MO.isReg() || !MO.isDef() || MO.isImplicit())
715 break;
716 ++NumDefs;
717 }
718 return NumDefs;
719}
720
Jakob Stoklund Olesenfad649a2012-12-07 04:23:29 +0000721void MachineInstr::bundleWithPred() {
722 assert(!isBundledWithPred() && "MI is already bundled with its predecessor");
723 setFlag(BundledPred);
Duncan P. N. Exon Smith20a62522016-02-22 20:49:58 +0000724 MachineBasicBlock::instr_iterator Pred = getIterator();
725 --Pred;
Jakob Stoklund Olesen582abdd2012-12-18 23:00:28 +0000726 assert(!Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfad649a2012-12-07 04:23:29 +0000727 Pred->setFlag(BundledSucc);
728}
729
730void MachineInstr::bundleWithSucc() {
731 assert(!isBundledWithSucc() && "MI is already bundled with its successor");
732 setFlag(BundledSucc);
Duncan P. N. Exon Smith20a62522016-02-22 20:49:58 +0000733 MachineBasicBlock::instr_iterator Succ = getIterator();
734 ++Succ;
Jakob Stoklund Olesen582abdd2012-12-18 23:00:28 +0000735 assert(!Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfad649a2012-12-07 04:23:29 +0000736 Succ->setFlag(BundledPred);
737}
738
739void MachineInstr::unbundleFromPred() {
740 assert(isBundledWithPred() && "MI isn't bundled with its predecessor");
741 clearFlag(BundledPred);
Duncan P. N. Exon Smith20a62522016-02-22 20:49:58 +0000742 MachineBasicBlock::instr_iterator Pred = getIterator();
743 --Pred;
Jakob Stoklund Olesen582abdd2012-12-18 23:00:28 +0000744 assert(Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfad649a2012-12-07 04:23:29 +0000745 Pred->clearFlag(BundledSucc);
746}
747
748void MachineInstr::unbundleFromSucc() {
749 assert(isBundledWithSucc() && "MI isn't bundled with its successor");
750 clearFlag(BundledSucc);
Duncan P. N. Exon Smith20a62522016-02-22 20:49:58 +0000751 MachineBasicBlock::instr_iterator Succ = getIterator();
752 ++Succ;
Jakob Stoklund Olesen582abdd2012-12-18 23:00:28 +0000753 assert(Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfad649a2012-12-07 04:23:29 +0000754 Succ->clearFlag(BundledPred);
755}
756
Evan Chengc36b7062011-01-07 23:50:32 +0000757bool MachineInstr::isStackAligningInlineAsm() const {
758 if (isInlineAsm()) {
759 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
760 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
761 return true;
762 }
763 return false;
764}
Chris Lattner8ace2cd2006-10-20 22:39:59 +0000765
Chad Rosier576cd112012-09-05 21:00:58 +0000766InlineAsm::AsmDialect MachineInstr::getInlineAsmDialect() const {
767 assert(isInlineAsm() && "getInlineAsmDialect() only works for inline asms!");
768 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
Chad Rosier2f1d8152012-09-05 22:40:13 +0000769 return InlineAsm::AsmDialect((ExtraInfo & InlineAsm::Extra_AsmDialect) != 0);
Chad Rosier576cd112012-09-05 21:00:58 +0000770}
771
Jakob Stoklund Olesen9dfaacb2011-10-12 23:37:33 +0000772int MachineInstr::findInlineAsmFlagIdx(unsigned OpIdx,
773 unsigned *GroupNo) const {
774 assert(isInlineAsm() && "Expected an inline asm instruction");
775 assert(OpIdx < getNumOperands() && "OpIdx out of range");
776
777 // Ignore queries about the initial operands.
778 if (OpIdx < InlineAsm::MIOp_FirstOperand)
779 return -1;
780
781 unsigned Group = 0;
782 unsigned NumOps;
783 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
784 i += NumOps) {
785 const MachineOperand &FlagMO = getOperand(i);
786 // If we reach the implicit register operands, stop looking.
787 if (!FlagMO.isImm())
788 return -1;
789 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
790 if (i + NumOps > OpIdx) {
791 if (GroupNo)
792 *GroupNo = Group;
793 return i;
794 }
795 ++Group;
796 }
797 return -1;
798}
799
Shiva Chen08534222018-05-09 02:41:08 +0000800const DILabel *MachineInstr::getDebugLabel() const {
801 assert(isDebugLabel() && "not a DBG_LABEL");
802 return cast<DILabel>(getOperand(0).getMetadata());
803}
804
Reid Kleckner13fb5a32016-04-14 18:29:59 +0000805const DILocalVariable *MachineInstr::getDebugVariable() const {
806 assert(isDebugValue() && "not a DBG_VALUE");
807 return cast<DILocalVariable>(getOperand(2).getMetadata());
808}
809
810const DIExpression *MachineInstr::getDebugExpression() const {
811 assert(isDebugValue() && "not a DBG_VALUE");
812 return cast<DIExpression>(getOperand(3).getMetadata());
813}
814
Jakob Stoklund Olesenf5916972011-10-12 23:37:36 +0000815const TargetRegisterClass*
816MachineInstr::getRegClassConstraint(unsigned OpIdx,
817 const TargetInstrInfo *TII,
818 const TargetRegisterInfo *TRI) const {
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000819 assert(getParent() && "Can't have an MBB reference here!");
Justin Bogner1842f4a2017-10-10 23:50:49 +0000820 assert(getMF() && "Can't have an MF reference here!");
821 const MachineFunction &MF = *getMF();
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000822
Jakob Stoklund Olesenf5916972011-10-12 23:37:36 +0000823 // Most opcodes have fixed constraints in their MCInstrDesc.
824 if (!isInlineAsm())
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000825 return TII->getRegClass(getDesc(), OpIdx, TRI, MF);
Jakob Stoklund Olesenf5916972011-10-12 23:37:36 +0000826
827 if (!getOperand(OpIdx).isReg())
Craig Topper4ba84432014-04-14 00:51:57 +0000828 return nullptr;
Jakob Stoklund Olesenf5916972011-10-12 23:37:36 +0000829
830 // For tied uses on inline asm, get the constraint from the def.
831 unsigned DefIdx;
832 if (getOperand(OpIdx).isUse() && isRegTiedToDefOperand(OpIdx, &DefIdx))
833 OpIdx = DefIdx;
834
835 // Inline asm stores register class constraints in the flag word.
836 int FlagIdx = findInlineAsmFlagIdx(OpIdx);
837 if (FlagIdx < 0)
Craig Topper4ba84432014-04-14 00:51:57 +0000838 return nullptr;
Jakob Stoklund Olesenf5916972011-10-12 23:37:36 +0000839
840 unsigned Flag = getOperand(FlagIdx).getImm();
841 unsigned RCID;
Simon Dardis5ebefb82016-07-18 13:17:31 +0000842 if ((InlineAsm::getKind(Flag) == InlineAsm::Kind_RegUse ||
843 InlineAsm::getKind(Flag) == InlineAsm::Kind_RegDef ||
844 InlineAsm::getKind(Flag) == InlineAsm::Kind_RegDefEarlyClobber) &&
845 InlineAsm::hasRegClassConstraint(Flag, RCID))
Jakob Stoklund Olesenf5916972011-10-12 23:37:36 +0000846 return TRI->getRegClass(RCID);
847
848 // Assume that all registers in a memory operand are pointers.
849 if (InlineAsm::getKind(Flag) == InlineAsm::Kind_Mem)
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000850 return TRI->getPointerRegClass(MF);
Jakob Stoklund Olesenf5916972011-10-12 23:37:36 +0000851
Craig Topper4ba84432014-04-14 00:51:57 +0000852 return nullptr;
Jakob Stoklund Olesenf5916972011-10-12 23:37:36 +0000853}
854
Quentin Colombetfb573922014-01-02 22:47:22 +0000855const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVReg(
856 unsigned Reg, const TargetRegisterClass *CurRC, const TargetInstrInfo *TII,
857 const TargetRegisterInfo *TRI, bool ExploreBundle) const {
858 // Check every operands inside the bundle if we have
859 // been asked to.
860 if (ExploreBundle)
Duncan P. N. Exon Smith63ec7f02016-02-27 17:05:33 +0000861 for (ConstMIBundleOperands OpndIt(*this); OpndIt.isValid() && CurRC;
Quentin Colombetfb573922014-01-02 22:47:22 +0000862 ++OpndIt)
863 CurRC = OpndIt->getParent()->getRegClassConstraintEffectForVRegImpl(
864 OpndIt.getOperandNo(), Reg, CurRC, TII, TRI);
865 else
866 // Otherwise, just check the current operands.
Matthias Braune67bd6c2015-05-29 02:56:46 +0000867 for (unsigned i = 0, e = NumOperands; i < e && CurRC; ++i)
868 CurRC = getRegClassConstraintEffectForVRegImpl(i, Reg, CurRC, TII, TRI);
Quentin Colombetfb573922014-01-02 22:47:22 +0000869 return CurRC;
870}
871
872const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVRegImpl(
873 unsigned OpIdx, unsigned Reg, const TargetRegisterClass *CurRC,
874 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
875 assert(CurRC && "Invalid initial register class");
876 // Check if Reg is constrained by some of its use/def from MI.
877 const MachineOperand &MO = getOperand(OpIdx);
878 if (!MO.isReg() || MO.getReg() != Reg)
879 return CurRC;
880 // If yes, accumulate the constraints through the operand.
881 return getRegClassConstraintEffect(OpIdx, CurRC, TII, TRI);
882}
883
884const TargetRegisterClass *MachineInstr::getRegClassConstraintEffect(
885 unsigned OpIdx, const TargetRegisterClass *CurRC,
886 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
887 const TargetRegisterClass *OpRC = getRegClassConstraint(OpIdx, TII, TRI);
888 const MachineOperand &MO = getOperand(OpIdx);
889 assert(MO.isReg() &&
890 "Cannot get register constraints for non-register operand");
891 assert(CurRC && "Invalid initial register class");
892 if (unsigned SubIdx = MO.getSubReg()) {
893 if (OpRC)
894 CurRC = TRI->getMatchingSuperRegClass(CurRC, OpRC, SubIdx);
895 else
896 CurRC = TRI->getSubClassWithSubReg(CurRC, SubIdx);
897 } else if (OpRC)
898 CurRC = TRI->getCommonSubClass(CurRC, OpRC);
899 return CurRC;
900}
901
Jakob Stoklund Olesen25377c82013-01-09 18:28:16 +0000902/// Return the number of instructions inside the MI bundle, not counting the
903/// header instruction.
Evan Chengddfd1372011-12-14 02:11:42 +0000904unsigned MachineInstr::getBundleSize() const {
Duncan P. N. Exon Smith20a62522016-02-22 20:49:58 +0000905 MachineBasicBlock::const_instr_iterator I = getIterator();
Evan Chengddfd1372011-12-14 02:11:42 +0000906 unsigned Size = 0;
Richard Trieu1b96cbe2016-02-18 22:09:30 +0000907 while (I->isBundledWithSucc()) {
908 ++Size;
909 ++I;
910 }
Evan Chengddfd1372011-12-14 02:11:42 +0000911 return Size;
912}
913
Nicolai Haehnle34417862016-04-22 04:04:08 +0000914/// Returns true if the MachineInstr has an implicit-use operand of exactly
915/// the given register (not considering sub/super-registers).
916bool MachineInstr::hasRegisterImplicitUseOperand(unsigned Reg) const {
917 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
918 const MachineOperand &MO = getOperand(i);
919 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == Reg)
920 return true;
921 }
922 return false;
923}
924
Evan Chengfaa51072007-04-26 19:00:32 +0000925/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Jim Grosbachf9ca50e2009-09-17 17:57:26 +0000926/// the specific register or -1 if it is not found. It further tightens
Evan Cheng76d7e762007-02-23 01:04:26 +0000927/// the search criteria to a use that kills the register if isKill is true.
Fraser Cormack33a142a2016-10-11 09:09:21 +0000928int MachineInstr::findRegisterUseOperandIdx(
929 unsigned Reg, bool isKill, const TargetRegisterInfo *TRI) const {
Evan Cheng576d1232006-12-06 08:27:42 +0000930 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Chengf277ee42007-05-29 18:35:22 +0000931 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000932 if (!MO.isReg() || !MO.isUse())
Evan Cheng6130f662008-03-05 00:59:57 +0000933 continue;
934 unsigned MOReg = MO.getReg();
935 if (!MOReg)
936 continue;
Stanislav Mekhanoshin1c82b7d2018-11-12 18:12:28 +0000937 if (MOReg == Reg || (TRI && Reg && MOReg && TRI->regsOverlap(MOReg, Reg)))
Evan Cheng76d7e762007-02-23 01:04:26 +0000938 if (!isKill || MO.isKill())
Evan Cheng32eb1f12007-03-26 22:37:45 +0000939 return i;
Evan Cheng576d1232006-12-06 08:27:42 +0000940 }
Evan Cheng32eb1f12007-03-26 22:37:45 +0000941 return -1;
Evan Cheng576d1232006-12-06 08:27:42 +0000942}
Jakob Stoklund Olesen7ebc4d62010-05-19 20:36:22 +0000943
Jakob Stoklund Olesen18b2c9d2010-05-21 20:02:01 +0000944/// readsWritesVirtualRegister - Return a pair of bools (reads, writes)
945/// indicating if this instruction reads or writes Reg. This also considers
946/// partial defines.
947std::pair<bool,bool>
948MachineInstr::readsWritesVirtualRegister(unsigned Reg,
949 SmallVectorImpl<unsigned> *Ops) const {
950 bool PartDef = false; // Partial redefine.
951 bool FullDef = false; // Full define.
952 bool Use = false;
Jakob Stoklund Olesen7ebc4d62010-05-19 20:36:22 +0000953
954 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
955 const MachineOperand &MO = getOperand(i);
956 if (!MO.isReg() || MO.getReg() != Reg)
957 continue;
Jakob Stoklund Olesen18b2c9d2010-05-21 20:02:01 +0000958 if (Ops)
959 Ops->push_back(i);
Jakob Stoklund Olesen7ebc4d62010-05-19 20:36:22 +0000960 if (MO.isUse())
Jakob Stoklund Olesen18b2c9d2010-05-21 20:02:01 +0000961 Use |= !MO.isUndef();
Jakob Stoklund Olesen201f2462011-08-19 00:30:17 +0000962 else if (MO.getSubReg() && !MO.isUndef())
Francis Visoiu Mistrihfd11bc02017-12-07 10:40:31 +0000963 // A partial def undef doesn't count as reading the register.
Jakob Stoklund Olesen7ebc4d62010-05-19 20:36:22 +0000964 PartDef = true;
965 else
966 FullDef = true;
967 }
Jakob Stoklund Olesen18b2c9d2010-05-21 20:02:01 +0000968 // A partial redefine uses Reg unless there is also a full define.
969 return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef);
Jakob Stoklund Olesen7ebc4d62010-05-19 20:36:22 +0000970}
971
Evan Cheng6130f662008-03-05 00:59:57 +0000972/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman703bfe62008-05-06 00:20:10 +0000973/// the specified register or -1 if it is not found. If isDead is true, defs
974/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
975/// also checks if there is a def of a super-register.
Evan Cheng1015ba72010-05-21 20:53:24 +0000976int
977MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, bool Overlap,
978 const TargetRegisterInfo *TRI) const {
979 bool isPhys = TargetRegisterInfo::isPhysicalRegister(Reg);
Evan Chengb371f452007-02-19 21:49:54 +0000980 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng6130f662008-03-05 00:59:57 +0000981 const MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesen1cf8b0f2012-02-14 23:49:37 +0000982 // Accept regmask operands when Overlap is set.
983 // Ignore them when looking for a specific def operand (Overlap == false).
984 if (isPhys && Overlap && MO.isRegMask() && MO.clobbersPhysReg(Reg))
985 return i;
Dan Gohmand735b802008-10-03 15:45:36 +0000986 if (!MO.isReg() || !MO.isDef())
Evan Cheng6130f662008-03-05 00:59:57 +0000987 continue;
988 unsigned MOReg = MO.getReg();
Evan Cheng1015ba72010-05-21 20:53:24 +0000989 bool Found = (MOReg == Reg);
990 if (!Found && TRI && isPhys &&
991 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
992 if (Overlap)
993 Found = TRI->regsOverlap(MOReg, Reg);
994 else
995 Found = TRI->isSubRegister(MOReg, Reg);
996 }
997 if (Found && (!isDead || MO.isDead()))
998 return i;
Evan Chengb371f452007-02-19 21:49:54 +0000999 }
Evan Cheng6130f662008-03-05 00:59:57 +00001000 return -1;
Evan Chengb371f452007-02-19 21:49:54 +00001001}
Evan Cheng19e3f312007-05-15 01:26:09 +00001002
Evan Chengf277ee42007-05-29 18:35:22 +00001003/// findFirstPredOperandIdx() - Find the index of the first operand in the
1004/// operand list that is used to represent the predicate. It returns -1 if
1005/// none is found.
1006int MachineInstr::findFirstPredOperandIdx() const {
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00001007 // Don't call MCID.findFirstPredOperandIdx() because this variant
1008 // is sometimes called on an instruction that's not yet complete, and
1009 // so the number of operands is less than the MCID indicates. In
1010 // particular, the PTX target does this.
Evan Chenge837dea2011-06-28 19:10:37 +00001011 const MCInstrDesc &MCID = getDesc();
1012 if (MCID.isPredicable()) {
Evan Cheng19e3f312007-05-15 01:26:09 +00001013 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Evan Chenge837dea2011-06-28 19:10:37 +00001014 if (MCID.OpInfo[i].isPredicate())
Evan Chengf277ee42007-05-29 18:35:22 +00001015 return i;
Evan Cheng19e3f312007-05-15 01:26:09 +00001016 }
1017
Evan Chengf277ee42007-05-29 18:35:22 +00001018 return -1;
Evan Cheng19e3f312007-05-15 01:26:09 +00001019}
Jim Grosbachee61d672011-08-24 16:44:17 +00001020
Jakob Stoklund Olesen9c130672012-09-04 18:36:28 +00001021// MachineOperand::TiedTo is 4 bits wide.
1022const unsigned TiedMax = 15;
1023
1024/// tieOperands - Mark operands at DefIdx and UseIdx as tied to each other.
1025///
1026/// Use and def operands can be tied together, indicated by a non-zero TiedTo
1027/// field. TiedTo can have these values:
1028///
1029/// 0: Operand is not tied to anything.
1030/// 1 to TiedMax-1: Tied to getOperand(TiedTo-1).
1031/// TiedMax: Tied to an operand >= TiedMax-1.
1032///
1033/// The tied def must be one of the first TiedMax operands on a normal
1034/// instruction. INLINEASM instructions allow more tied defs.
1035///
Jakob Stoklund Olesen94083142012-08-31 20:50:53 +00001036void MachineInstr::tieOperands(unsigned DefIdx, unsigned UseIdx) {
Jakob Stoklund Olesen94083142012-08-31 20:50:53 +00001037 MachineOperand &DefMO = getOperand(DefIdx);
1038 MachineOperand &UseMO = getOperand(UseIdx);
1039 assert(DefMO.isDef() && "DefIdx must be a def operand");
1040 assert(UseMO.isUse() && "UseIdx must be a use operand");
1041 assert(!DefMO.isTied() && "Def is already tied to another use");
1042 assert(!UseMO.isTied() && "Use is already tied to another def");
1043
Jakob Stoklund Olesen9c130672012-09-04 18:36:28 +00001044 if (DefIdx < TiedMax)
1045 UseMO.TiedTo = DefIdx + 1;
1046 else {
1047 // Inline asm can use the group descriptors to find tied operands, but on
1048 // normal instruction, the tied def must be within the first TiedMax
1049 // operands.
1050 assert(isInlineAsm() && "DefIdx out of range");
1051 UseMO.TiedTo = TiedMax;
1052 }
1053
1054 // UseIdx can be out of range, we'll search for it in findTiedOperandIdx().
1055 DefMO.TiedTo = std::min(UseIdx + 1, TiedMax);
Jakob Stoklund Olesen94083142012-08-31 20:50:53 +00001056}
1057
Jakob Stoklund Olesen699ac042012-08-29 00:37:58 +00001058/// Given the index of a tied register operand, find the operand it is tied to.
1059/// Defs are tied to uses and vice versa. Returns the index of the tied operand
1060/// which must exist.
1061unsigned MachineInstr::findTiedOperandIdx(unsigned OpIdx) const {
Jakob Stoklund Olesen9c130672012-09-04 18:36:28 +00001062 const MachineOperand &MO = getOperand(OpIdx);
1063 assert(MO.isTied() && "Operand isn't tied");
Jakob Stoklund Olesen699ac042012-08-29 00:37:58 +00001064
Jakob Stoklund Olesen9c130672012-09-04 18:36:28 +00001065 // Normally TiedTo is in range.
1066 if (MO.TiedTo < TiedMax)
1067 return MO.TiedTo - 1;
1068
1069 // Uses on normal instructions can be out of range.
1070 if (!isInlineAsm()) {
1071 // Normal tied defs must be in the 0..TiedMax-1 range.
1072 if (MO.isUse())
1073 return TiedMax - 1;
1074 // MO is a def. Search for the tied use.
1075 for (unsigned i = TiedMax - 1, e = getNumOperands(); i != e; ++i) {
1076 const MachineOperand &UseMO = getOperand(i);
1077 if (UseMO.isReg() && UseMO.isUse() && UseMO.TiedTo == OpIdx + 1)
1078 return i;
1079 }
1080 llvm_unreachable("Can't find tied use");
1081 }
1082
1083 // Now deal with inline asm by parsing the operand group descriptor flags.
1084 // Find the beginning of each operand group.
1085 SmallVector<unsigned, 8> GroupIdx;
1086 unsigned OpIdxGroup = ~0u;
1087 unsigned NumOps;
1088 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
1089 i += NumOps) {
1090 const MachineOperand &FlagMO = getOperand(i);
1091 assert(FlagMO.isImm() && "Invalid tied operand on inline asm");
1092 unsigned CurGroup = GroupIdx.size();
1093 GroupIdx.push_back(i);
1094 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
1095 // OpIdx belongs to this operand group.
1096 if (OpIdx > i && OpIdx < i + NumOps)
1097 OpIdxGroup = CurGroup;
1098 unsigned TiedGroup;
1099 if (!InlineAsm::isUseOperandTiedToDef(FlagMO.getImm(), TiedGroup))
1100 continue;
1101 // Operands in this group are tied to operands in TiedGroup which must be
1102 // earlier. Find the number of operands between the two groups.
1103 unsigned Delta = i - GroupIdx[TiedGroup];
1104
1105 // OpIdx is a use tied to TiedGroup.
1106 if (OpIdxGroup == CurGroup)
1107 return OpIdx - Delta;
1108
1109 // OpIdx is a def tied to this use group.
1110 if (OpIdxGroup == TiedGroup)
1111 return OpIdx + Delta;
1112 }
1113 llvm_unreachable("Invalid tied operand on inline asm");
Jakob Stoklund Olesen699ac042012-08-29 00:37:58 +00001114}
1115
Dan Gohmane6cd7572010-05-13 20:34:42 +00001116/// clearKillInfo - Clears kill flags on all operands.
1117///
1118void MachineInstr::clearKillInfo() {
Benjamin Kramer59d81db2015-02-21 17:08:08 +00001119 for (MachineOperand &MO : operands()) {
Dan Gohmane6cd7572010-05-13 20:34:42 +00001120 if (MO.isReg() && MO.isUse())
1121 MO.setIsKill(false);
1122 }
1123}
1124
Geoff Berryff75b992018-01-29 18:47:48 +00001125void MachineInstr::substituteRegister(unsigned FromReg, unsigned ToReg,
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +00001126 unsigned SubIdx,
Geoff Berry13357c92018-02-23 18:25:08 +00001127 const TargetRegisterInfo &RegInfo) {
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +00001128 if (TargetRegisterInfo::isPhysicalRegister(ToReg)) {
1129 if (SubIdx)
1130 ToReg = RegInfo.getSubReg(ToReg, SubIdx);
Benjamin Kramer59d81db2015-02-21 17:08:08 +00001131 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +00001132 if (!MO.isReg() || MO.getReg() != FromReg)
1133 continue;
1134 MO.substPhysReg(ToReg, RegInfo);
1135 }
1136 } else {
Benjamin Kramer59d81db2015-02-21 17:08:08 +00001137 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +00001138 if (!MO.isReg() || MO.getReg() != FromReg)
1139 continue;
1140 MO.substVirtReg(ToReg, SubIdx, RegInfo);
1141 }
1142 }
1143}
1144
Evan Cheng9f1c8312008-07-03 09:09:37 +00001145/// isSafeToMove - Return true if it is safe to move this instruction. If
1146/// SawStore is set to true, it means that there is a store (or call) between
1147/// the instruction's location and its intended destination.
Matthias Braundfc41db2015-05-19 21:22:20 +00001148bool MachineInstr::isSafeToMove(AliasAnalysis *AA, bool &SawStore) const {
Evan Chengb27087f2008-03-13 00:44:09 +00001149 // Ignore stuff that we obviously can't move.
Jakob Stoklund Olesen0d758582012-08-29 20:48:45 +00001150 //
1151 // Treat volatile loads as stores. This is not strictly necessary for
Jakob Stoklund Olesen4f1a56c2012-09-04 18:44:43 +00001152 // volatiles, but it is required for atomic loads. It is not allowed to move
Jakob Stoklund Olesen0d758582012-08-29 20:48:45 +00001153 // a load across an atomic load with Ordering > Monotonic.
Alex Bradbury81fd1382017-11-08 20:19:16 +00001154 if (mayStore() || isCall() || isPHI() ||
Jakob Stoklund Olesenf036f7a2012-08-29 21:19:21 +00001155 (mayLoad() && hasOrderedMemoryRef())) {
Evan Chengb27087f2008-03-13 00:44:09 +00001156 SawStore = true;
1157 return false;
1158 }
Evan Cheng30a343a2011-01-07 21:08:26 +00001159
Shiva Chen24abe712018-05-09 02:42:00 +00001160 if (isPosition() || isDebugInstr() || isTerminator() ||
Rafael Espindola7d7d9962014-03-07 06:08:31 +00001161 hasUnmodeledSideEffects())
Evan Chengb27087f2008-03-13 00:44:09 +00001162 return false;
1163
1164 // See if this instruction does a load. If so, we have to guarantee that the
1165 // loaded value doesn't change between the load and the its intended
1166 // destination. The check for isInvariantLoad gives the targe the chance to
1167 // classify the load as always returning a constant, e.g. a constant pool
1168 // load.
Justin Lebare7555f02016-09-10 01:03:20 +00001169 if (mayLoad() && !isDereferenceableInvariantLoad(AA))
Evan Chengb27087f2008-03-13 00:44:09 +00001170 // Otherwise, this is a real load. If there is a store between the load and
Jakob Stoklund Olesen0d758582012-08-29 20:48:45 +00001171 // end of block, we can't move it.
1172 return !SawStore;
Dan Gohman3e4fb702008-09-24 00:06:15 +00001173
Evan Chengb27087f2008-03-13 00:44:09 +00001174 return true;
1175}
1176
Eli Friedmanbfa11452017-03-09 23:33:36 +00001177bool MachineInstr::mayAlias(AliasAnalysis *AA, MachineInstr &Other,
1178 bool UseTBAA) {
Justin Bogner1842f4a2017-10-10 23:50:49 +00001179 const MachineFunction *MF = getMF();
Eli Friedmanbfa11452017-03-09 23:33:36 +00001180 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
Balaram Makamb3e25ce2017-08-30 14:57:12 +00001181 const MachineFrameInfo &MFI = MF->getFrameInfo();
Eli Friedmanbfa11452017-03-09 23:33:36 +00001182
1183 // If neither instruction stores to memory, they can't alias in any
1184 // meaningful way, even if they read from the same address.
1185 if (!mayStore() && !Other.mayStore())
1186 return false;
1187
1188 // Let the target decide if memory accesses cannot possibly overlap.
1189 if (TII->areMemAccessesTriviallyDisjoint(*this, Other, AA))
1190 return false;
1191
Eli Friedmanbfa11452017-03-09 23:33:36 +00001192 // FIXME: Need to handle multiple memory operands to support all targets.
1193 if (!hasOneMemOperand() || !Other.hasOneMemOperand())
1194 return true;
1195
1196 MachineMemOperand *MMOa = *memoperands_begin();
1197 MachineMemOperand *MMOb = *Other.memoperands_begin();
1198
Eli Friedmanbfa11452017-03-09 23:33:36 +00001199 // The following interface to AA is fashioned after DAGCombiner::isAlias
1200 // and operates with MachineMemOperand offset with some important
1201 // assumptions:
1202 // - LLVM fundamentally assumes flat address spaces.
1203 // - MachineOperand offset can *only* result from legalization and
1204 // cannot affect queries other than the trivial case of overlap
1205 // checking.
1206 // - These offsets never wrap and never step outside
1207 // of allocated objects.
1208 // - There should never be any negative offsets here.
1209 //
1210 // FIXME: Modify API to hide this math from "user"
Balaram Makamb3e25ce2017-08-30 14:57:12 +00001211 // Even before we go to AA we can reason locally about some
Eli Friedmanbfa11452017-03-09 23:33:36 +00001212 // memory objects. It can save compile time, and possibly catch some
1213 // corner cases not currently covered.
1214
Balaram Makamb3e25ce2017-08-30 14:57:12 +00001215 int64_t OffsetA = MMOa->getOffset();
1216 int64_t OffsetB = MMOb->getOffset();
Balaram Makamb3e25ce2017-08-30 14:57:12 +00001217 int64_t MinOffset = std::min(OffsetA, OffsetB);
Krzysztof Parzyszek7e54adc2018-08-20 20:37:57 +00001218
1219 uint64_t WidthA = MMOa->getSize();
1220 uint64_t WidthB = MMOb->getSize();
1221 bool KnownWidthA = WidthA != MemoryLocation::UnknownSize;
1222 bool KnownWidthB = WidthB != MemoryLocation::UnknownSize;
1223
Balaram Makamb3e25ce2017-08-30 14:57:12 +00001224 const Value *ValA = MMOa->getValue();
1225 const Value *ValB = MMOb->getValue();
1226 bool SameVal = (ValA && ValB && (ValA == ValB));
1227 if (!SameVal) {
1228 const PseudoSourceValue *PSVa = MMOa->getPseudoValue();
1229 const PseudoSourceValue *PSVb = MMOb->getPseudoValue();
1230 if (PSVa && ValB && !PSVa->mayAlias(&MFI))
1231 return false;
1232 if (PSVb && ValA && !PSVb->mayAlias(&MFI))
1233 return false;
1234 if (PSVa && PSVb && (PSVa == PSVb))
1235 SameVal = true;
1236 }
Eli Friedmanbfa11452017-03-09 23:33:36 +00001237
Balaram Makamb3e25ce2017-08-30 14:57:12 +00001238 if (SameVal) {
Krzysztof Parzyszek7e54adc2018-08-20 20:37:57 +00001239 if (!KnownWidthA || !KnownWidthB)
1240 return true;
Balaram Makamb3e25ce2017-08-30 14:57:12 +00001241 int64_t MaxOffset = std::max(OffsetA, OffsetB);
1242 int64_t LowWidth = (MinOffset == OffsetA) ? WidthA : WidthB;
1243 return (MinOffset + LowWidth > MaxOffset);
1244 }
1245
1246 if (!AA)
1247 return true;
1248
1249 if (!ValA || !ValB)
1250 return true;
1251
1252 assert((OffsetA >= 0) && "Negative MachineMemOperand offset");
1253 assert((OffsetB >= 0) && "Negative MachineMemOperand offset");
1254
Krzysztof Parzyszek7e54adc2018-08-20 20:37:57 +00001255 int64_t OverlapA = KnownWidthA ? WidthA + OffsetA - MinOffset
1256 : MemoryLocation::UnknownSize;
1257 int64_t OverlapB = KnownWidthB ? WidthB + OffsetB - MinOffset
1258 : MemoryLocation::UnknownSize;
Balaram Makamb3e25ce2017-08-30 14:57:12 +00001259
1260 AliasResult AAResult = AA->alias(
Krzysztof Parzyszek7e54adc2018-08-20 20:37:57 +00001261 MemoryLocation(ValA, OverlapA,
Balaram Makamb3e25ce2017-08-30 14:57:12 +00001262 UseTBAA ? MMOa->getAAInfo() : AAMDNodes()),
Krzysztof Parzyszek7e54adc2018-08-20 20:37:57 +00001263 MemoryLocation(ValB, OverlapB,
Balaram Makamb3e25ce2017-08-30 14:57:12 +00001264 UseTBAA ? MMOb->getAAInfo() : AAMDNodes()));
Eli Friedmanbfa11452017-03-09 23:33:36 +00001265
1266 return (AAResult != NoAlias);
1267}
1268
Jakob Stoklund Olesenf036f7a2012-08-29 21:19:21 +00001269/// hasOrderedMemoryRef - Return true if this instruction may have an ordered
1270/// or volatile memory reference, or if the information describing the memory
1271/// reference is not available. Return false if it is known to have no ordered
1272/// memory references.
1273bool MachineInstr::hasOrderedMemoryRef() const {
Dan Gohman3e4fb702008-09-24 00:06:15 +00001274 // An instruction known never to access memory won't have a volatile access.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00001275 if (!mayStore() &&
1276 !mayLoad() &&
1277 !isCall() &&
Evan Chengc36b7062011-01-07 23:50:32 +00001278 !hasUnmodeledSideEffects())
Dan Gohman3e4fb702008-09-24 00:06:15 +00001279 return false;
1280
1281 // Otherwise, if the instruction has no memory reference information,
1282 // conservatively assume it wasn't preserved.
1283 if (memoperands_empty())
1284 return true;
Jim Grosbachee61d672011-08-24 16:44:17 +00001285
Justin Lebar6e665c82016-07-13 22:35:19 +00001286 // Check if any of our memory operands are ordered.
Eugene Zelenko1d081e62017-05-31 01:10:10 +00001287 return llvm::any_of(memoperands(), [](const MachineMemOperand *MMO) {
Justin Lebar6e665c82016-07-13 22:35:19 +00001288 return !MMO->isUnordered();
1289 });
Dan Gohman3e4fb702008-09-24 00:06:15 +00001290}
1291
Justin Lebare7555f02016-09-10 01:03:20 +00001292/// isDereferenceableInvariantLoad - Return true if this instruction will never
1293/// trap and is loading from a location whose value is invariant across a run of
1294/// this function.
1295bool MachineInstr::isDereferenceableInvariantLoad(AliasAnalysis *AA) const {
Dan Gohmane33f44c2009-10-07 17:38:06 +00001296 // If the instruction doesn't load at all, it isn't an invariant load.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00001297 if (!mayLoad())
Dan Gohmane33f44c2009-10-07 17:38:06 +00001298 return false;
1299
1300 // If the instruction has lost its memoperands, conservatively assume that
1301 // it may not be an invariant load.
1302 if (memoperands_empty())
1303 return false;
1304
Matthias Braunf79c57a2016-07-28 18:40:00 +00001305 const MachineFrameInfo &MFI = getParent()->getParent()->getFrameInfo();
Dan Gohmane33f44c2009-10-07 17:38:06 +00001306
Justin Lebar6e665c82016-07-13 22:35:19 +00001307 for (MachineMemOperand *MMO : memoperands()) {
1308 if (MMO->isVolatile()) return false;
1309 if (MMO->isStore()) return false;
Justin Lebarc71d5b42016-09-11 01:38:58 +00001310 if (MMO->isInvariant() && MMO->isDereferenceable())
1311 continue;
Nick Lewyckyd63390c2014-04-15 07:22:52 +00001312
1313 // A load from a constant PseudoSourceValue is invariant.
Justin Lebar6e665c82016-07-13 22:35:19 +00001314 if (const PseudoSourceValue *PSV = MMO->getPseudoValue())
Matthias Braunf79c57a2016-07-28 18:40:00 +00001315 if (PSV->isConstant(&MFI))
Nick Lewyckyd63390c2014-04-15 07:22:52 +00001316 continue;
1317
Justin Lebar6e665c82016-07-13 22:35:19 +00001318 if (const Value *V = MMO->getValue()) {
Dan Gohmane33f44c2009-10-07 17:38:06 +00001319 // If we have an AliasAnalysis, ask it whether the memory is constant.
Chandler Carruth4d7ed392015-06-17 07:18:54 +00001320 if (AA &&
1321 AA->pointsToConstantMemory(
Justin Lebar6e665c82016-07-13 22:35:19 +00001322 MemoryLocation(V, MMO->getSize(), MMO->getAAInfo())))
Dan Gohmane33f44c2009-10-07 17:38:06 +00001323 continue;
1324 }
1325
1326 // Otherwise assume conservatively.
1327 return false;
1328 }
1329
1330 // Everything checks out.
1331 return true;
1332}
1333
Evan Cheng229694f2009-12-03 02:31:43 +00001334/// isConstantValuePHI - If the specified instruction is a PHI that always
1335/// merges together the same virtual register, return the register, otherwise
1336/// return 0.
1337unsigned MachineInstr::isConstantValuePHI() const {
Chris Lattner518bb532010-02-09 19:54:29 +00001338 if (!isPHI())
Evan Cheng229694f2009-12-03 02:31:43 +00001339 return 0;
Evan Chengd8f079c2009-12-07 23:10:34 +00001340 assert(getNumOperands() >= 3 &&
1341 "It's illegal to have a PHI without source operands");
Evan Cheng229694f2009-12-03 02:31:43 +00001342
1343 unsigned Reg = getOperand(1).getReg();
1344 for (unsigned i = 3, e = getNumOperands(); i < e; i += 2)
1345 if (getOperand(i).getReg() != Reg)
1346 return 0;
1347 return Reg;
1348}
1349
Evan Chengc36b7062011-01-07 23:50:32 +00001350bool MachineInstr::hasUnmodeledSideEffects() const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +00001351 if (hasProperty(MCID::UnmodeledSideEffects))
Evan Chengc36b7062011-01-07 23:50:32 +00001352 return true;
1353 if (isInlineAsm()) {
1354 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1355 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1356 return true;
1357 }
1358
1359 return false;
1360}
1361
Michael Kuperstein426921f2015-08-12 10:14:58 +00001362bool MachineInstr::isLoadFoldBarrier() const {
1363 return mayStore() || isCall() || hasUnmodeledSideEffects();
1364}
1365
Evan Chenga57fabe2010-04-08 20:02:37 +00001366/// allDefsAreDead - Return true if all the defs of this instruction are dead.
1367///
1368bool MachineInstr::allDefsAreDead() const {
Benjamin Kramer59d81db2015-02-21 17:08:08 +00001369 for (const MachineOperand &MO : operands()) {
Evan Chenga57fabe2010-04-08 20:02:37 +00001370 if (!MO.isReg() || MO.isUse())
1371 continue;
1372 if (!MO.isDead())
1373 return false;
1374 }
1375 return true;
1376}
1377
Evan Chengc8f46c42010-10-22 21:49:09 +00001378/// copyImplicitOps - Copy implicit register operands from specified
1379/// instruction to this instruction.
Jakob Stoklund Olesenbe06aac2012-12-20 22:54:02 +00001380void MachineInstr::copyImplicitOps(MachineFunction &MF,
Duncan P. N. Exon Smith1d75c8d2016-02-27 20:01:33 +00001381 const MachineInstr &MI) {
1382 for (unsigned i = MI.getDesc().getNumOperands(), e = MI.getNumOperands();
Evan Chengc8f46c42010-10-22 21:49:09 +00001383 i != e; ++i) {
Duncan P. N. Exon Smith1d75c8d2016-02-27 20:01:33 +00001384 const MachineOperand &MO = MI.getOperand(i);
Lang Hames3dd951e2014-03-17 01:22:54 +00001385 if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask())
Jakob Stoklund Olesenbe06aac2012-12-20 22:54:02 +00001386 addOperand(MF, MO);
Evan Chengc8f46c42010-10-22 21:49:09 +00001387 }
1388}
1389
Francis Visoiu Mistrihfd11bc02017-12-07 10:40:31 +00001390bool MachineInstr::hasComplexRegisterTies() const {
1391 const MCInstrDesc &MCID = getDesc();
1392 for (unsigned I = 0, E = getNumOperands(); I < E; ++I) {
1393 const auto &Operand = getOperand(I);
1394 if (!Operand.isReg() || Operand.isDef())
1395 // Ignore the defined registers as MCID marks only the uses as tied.
1396 continue;
1397 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
1398 int TiedIdx = Operand.isTied() ? int(findTiedOperandIdx(I)) : -1;
1399 if (ExpectedTiedIdx != TiedIdx)
1400 return true;
1401 }
1402 return false;
1403}
1404
1405LLT MachineInstr::getTypeToPrint(unsigned OpIdx, SmallBitVector &PrintedTypes,
1406 const MachineRegisterInfo &MRI) const {
1407 const MachineOperand &Op = getOperand(OpIdx);
1408 if (!Op.isReg())
1409 return LLT{};
1410
1411 if (isVariadic() || OpIdx >= getNumExplicitOperands())
1412 return MRI.getType(Op.getReg());
1413
1414 auto &OpInfo = getDesc().OpInfo[OpIdx];
1415 if (!OpInfo.isGenericType())
1416 return MRI.getType(Op.getReg());
1417
1418 if (PrintedTypes[OpInfo.getGenericTypeIndex()])
1419 return LLT{};
1420
Roman Tereshin297777e2018-05-07 22:31:47 +00001421 LLT TypeToPrint = MRI.getType(Op.getReg());
1422 // Don't mark the type index printed if it wasn't actually printed: maybe
1423 // another operand with the same type index has an actual type attached:
1424 if (TypeToPrint.isValid())
1425 PrintedTypes.set(OpInfo.getGenericTypeIndex());
1426 return TypeToPrint;
Francis Visoiu Mistrihfd11bc02017-12-07 10:40:31 +00001427}
1428
Aaron Ballman1d03d382017-10-15 14:32:27 +00001429#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braunaaf8acb2017-01-29 18:20:42 +00001430LLVM_DUMP_METHOD void MachineInstr::dump() const {
Sebastian Pop0ed0bfd2016-12-21 01:41:12 +00001431 dbgs() << " ";
Matthias Braunaaf8acb2017-01-29 18:20:42 +00001432 print(dbgs());
Mon P Wang5ca6bd12008-10-10 01:43:55 +00001433}
Matthias Braun88d20752017-01-28 02:02:38 +00001434#endif
Mon P Wang5ca6bd12008-10-10 01:43:55 +00001435
Francis Visoiu Mistrih7bee1ce2018-01-18 18:05:15 +00001436void MachineInstr::print(raw_ostream &OS, bool IsStandalone, bool SkipOpers,
Krzysztof Parzyszekb8924a02018-04-10 16:46:13 +00001437 bool SkipDebugLoc, bool AddNewLine,
1438 const TargetInstrInfo *TII) const {
Duncan P. N. Exon Smith340d78d2015-06-26 23:18:44 +00001439 const Module *M = nullptr;
Francis Visoiu Mistrih23b46122018-01-18 14:52:14 +00001440 const Function *F = nullptr;
1441 if (const MachineFunction *MF = getMFIfAvailable(*this)) {
1442 F = &MF->getFunction();
1443 M = F->getParent();
Andrew V. Tischenko07ea57a2018-02-26 09:43:21 +00001444 if (!TII)
1445 TII = MF->getSubtarget().getInstrInfo();
Francis Visoiu Mistrih23b46122018-01-18 14:52:14 +00001446 }
Duncan P. N. Exon Smith340d78d2015-06-26 23:18:44 +00001447
1448 ModuleSlotTracker MST(M);
Francis Visoiu Mistrih23b46122018-01-18 14:52:14 +00001449 if (F)
1450 MST.incorporateFunction(*F);
Francis Visoiu Mistrih7bee1ce2018-01-18 18:05:15 +00001451 print(OS, MST, IsStandalone, SkipOpers, SkipDebugLoc, TII);
Duncan P. N. Exon Smitha08efbf2015-06-26 22:06:47 +00001452}
1453
1454void MachineInstr::print(raw_ostream &OS, ModuleSlotTracker &MST,
Francis Visoiu Mistrih7bee1ce2018-01-18 18:05:15 +00001455 bool IsStandalone, bool SkipOpers, bool SkipDebugLoc,
Krzysztof Parzyszekb8924a02018-04-10 16:46:13 +00001456 bool AddNewLine, const TargetInstrInfo *TII) const {
Eric Christopher9656d2d2015-02-27 00:11:34 +00001457 // We can be a bit tidier if we know the MachineFunction.
Craig Topper4ba84432014-04-14 00:51:57 +00001458 const MachineFunction *MF = nullptr;
Eric Christopher9656d2d2015-02-27 00:11:34 +00001459 const TargetRegisterInfo *TRI = nullptr;
Craig Topper4ba84432014-04-14 00:51:57 +00001460 const MachineRegisterInfo *MRI = nullptr;
Tim Northover9c9955b2016-07-29 20:32:59 +00001461 const TargetIntrinsicInfo *IntrinsicInfo = nullptr;
Francis Visoiu Mistrih23b46122018-01-18 14:52:14 +00001462 tryToGetTargetInfo(*this, TRI, MRI, IntrinsicInfo, TII);
Tim Northover9c9955b2016-07-29 20:32:59 +00001463
Francis Visoiu Mistrih23b46122018-01-18 14:52:14 +00001464 if (isCFIInstruction())
1465 assert(getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Jakob Stoklund Olesena0c5bf12010-07-28 18:35:46 +00001466
Francis Visoiu Mistrihfd11bc02017-12-07 10:40:31 +00001467 SmallBitVector PrintedTypes(8);
Francis Visoiu Mistrihfa73cae2018-09-26 13:33:09 +00001468 bool ShouldPrintRegisterTies = IsStandalone || hasComplexRegisterTies();
Francis Visoiu Mistrihfd11bc02017-12-07 10:40:31 +00001469 auto getTiedOperandIdx = [&](unsigned OpIdx) {
1470 if (!ShouldPrintRegisterTies)
1471 return 0U;
1472 const MachineOperand &MO = getOperand(OpIdx);
1473 if (MO.isReg() && MO.isTied() && !MO.isDef())
1474 return findTiedOperandIdx(OpIdx);
1475 return 0U;
1476 };
Francis Visoiu Mistrih23b46122018-01-18 14:52:14 +00001477 unsigned StartOp = 0;
1478 unsigned e = getNumOperands();
1479
Dan Gohman0ba90f3e2009-10-31 20:19:03 +00001480 // Print explicitly defined operands on the left of an assignment syntax.
Francis Visoiu Mistrih23b46122018-01-18 14:52:14 +00001481 while (StartOp < e) {
1482 const MachineOperand &MO = getOperand(StartOp);
1483 if (!MO.isReg() || !MO.isDef() || MO.isImplicit())
1484 break;
1485
Francis Visoiu Mistrihfd11bc02017-12-07 10:40:31 +00001486 if (StartOp != 0)
1487 OS << ", ";
Francis Visoiu Mistrih23b46122018-01-18 14:52:14 +00001488
Francis Visoiu Mistrihfd11bc02017-12-07 10:40:31 +00001489 LLT TypeToPrint = MRI ? getTypeToPrint(StartOp, PrintedTypes, *MRI) : LLT{};
1490 unsigned TiedOperandIdx = getTiedOperandIdx(StartOp);
Francis Visoiu Mistrih7bee1ce2018-01-18 18:05:15 +00001491 MO.print(OS, MST, TypeToPrint, /*PrintDef=*/false, IsStandalone,
Francis Visoiu Mistrih96ed12f2018-01-18 17:59:06 +00001492 ShouldPrintRegisterTies, TiedOperandIdx, TRI, IntrinsicInfo);
Francis Visoiu Mistrih23b46122018-01-18 14:52:14 +00001493 ++StartOp;
Chris Lattner6a592272002-10-30 01:55:38 +00001494 }
Tanya Lattnerb1407622004-06-25 00:13:11 +00001495
Dan Gohman0ba90f3e2009-10-31 20:19:03 +00001496 if (StartOp != 0)
1497 OS << " = ";
1498
Francis Visoiu Mistrih2a215992018-01-09 16:11:51 +00001499 if (getFlag(MachineInstr::FrameSetup))
1500 OS << "frame-setup ";
Francis Visoiu Mistrih12d58072018-03-13 19:53:16 +00001501 if (getFlag(MachineInstr::FrameDestroy))
Francis Visoiu Mistrih2a215992018-01-09 16:11:51 +00001502 OS << "frame-destroy ";
Michael Bergecfd97d2018-05-03 00:07:56 +00001503 if (getFlag(MachineInstr::FmNoNans))
1504 OS << "nnan ";
1505 if (getFlag(MachineInstr::FmNoInfs))
1506 OS << "ninf ";
1507 if (getFlag(MachineInstr::FmNsz))
1508 OS << "nsz ";
1509 if (getFlag(MachineInstr::FmArcp))
1510 OS << "arcp ";
1511 if (getFlag(MachineInstr::FmContract))
1512 OS << "contract ";
1513 if (getFlag(MachineInstr::FmAfn))
1514 OS << "afn ";
1515 if (getFlag(MachineInstr::FmReassoc))
1516 OS << "reassoc ";
Michael Berg87d1f822018-09-11 21:35:32 +00001517 if (getFlag(MachineInstr::NoUWrap))
1518 OS << "nuw ";
1519 if (getFlag(MachineInstr::NoSWrap))
1520 OS << "nsw ";
1521 if (getFlag(MachineInstr::IsExact))
1522 OS << "exact ";
Francis Visoiu Mistrih2a215992018-01-09 16:11:51 +00001523
Dan Gohman0ba90f3e2009-10-31 20:19:03 +00001524 // Print the opcode name.
Eric Christopher9656d2d2015-02-27 00:11:34 +00001525 if (TII)
1526 OS << TII->getName(getOpcode());
Benjamin Kramerc667ba62012-02-10 13:18:44 +00001527 else
1528 OS << "UNKNOWN";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001529
Andrew Trickc6ada8e2013-01-25 07:45:25 +00001530 if (SkipOpers)
1531 return;
1532
Dan Gohman0ba90f3e2009-10-31 20:19:03 +00001533 // Print the rest of the operands.
Dan Gohman80f6c582009-11-09 19:38:45 +00001534 bool FirstOp = true;
Jakob Stoklund Olesen7a2ecd32011-06-27 04:08:29 +00001535 unsigned AsmDescOp = ~0u;
1536 unsigned AsmOpCount = 0;
Evan Chengc36b7062011-01-07 23:50:32 +00001537
Jakob Stoklund Olesen3627a462011-09-29 00:40:51 +00001538 if (isInlineAsm() && e >= InlineAsm::MIOp_FirstOperand) {
Evan Chengc36b7062011-01-07 23:50:32 +00001539 // Print asm string.
1540 OS << " ";
Francis Visoiu Mistrihfd11bc02017-12-07 10:40:31 +00001541 const unsigned OpIdx = InlineAsm::MIOp_AsmString;
1542 LLT TypeToPrint = MRI ? getTypeToPrint(OpIdx, PrintedTypes, *MRI) : LLT{};
Francis Visoiu Mistrihd3b84082017-12-07 17:12:30 +00001543 unsigned TiedOperandIdx = getTiedOperandIdx(OpIdx);
Francis Visoiu Mistrih7bee1ce2018-01-18 18:05:15 +00001544 getOperand(OpIdx).print(OS, MST, TypeToPrint, /*PrintDef=*/true, IsStandalone,
Francis Visoiu Mistrihfd11bc02017-12-07 10:40:31 +00001545 ShouldPrintRegisterTies, TiedOperandIdx, TRI,
1546 IntrinsicInfo);
Evan Chengc36b7062011-01-07 23:50:32 +00001547
Eric Christopherfffe3632013-01-11 18:12:39 +00001548 // Print HasSideEffects, MayLoad, MayStore, IsAlignStack
Evan Chengc36b7062011-01-07 23:50:32 +00001549 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1550 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1551 OS << " [sideeffect]";
Eric Christopherfffe3632013-01-11 18:12:39 +00001552 if (ExtraInfo & InlineAsm::Extra_MayLoad)
1553 OS << " [mayload]";
1554 if (ExtraInfo & InlineAsm::Extra_MayStore)
1555 OS << " [maystore]";
Wei Dingef869632016-06-22 18:51:08 +00001556 if (ExtraInfo & InlineAsm::Extra_IsConvergent)
1557 OS << " [isconvergent]";
Evan Chengc36b7062011-01-07 23:50:32 +00001558 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
1559 OS << " [alignstack]";
Chad Rosier77fffa62012-09-05 22:17:43 +00001560 if (getInlineAsmDialect() == InlineAsm::AD_ATT)
Chad Rosier576cd112012-09-05 21:00:58 +00001561 OS << " [attdialect]";
Chad Rosier77fffa62012-09-05 22:17:43 +00001562 if (getInlineAsmDialect() == InlineAsm::AD_Intel)
Chad Rosier576cd112012-09-05 21:00:58 +00001563 OS << " [inteldialect]";
Evan Chengc36b7062011-01-07 23:50:32 +00001564
Jakob Stoklund Olesen7a2ecd32011-06-27 04:08:29 +00001565 StartOp = AsmDescOp = InlineAsm::MIOp_FirstOperand;
Evan Chengc36b7062011-01-07 23:50:32 +00001566 FirstOp = false;
1567 }
1568
Chris Lattner6a592272002-10-30 01:55:38 +00001569 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohman80f6c582009-11-09 19:38:45 +00001570 const MachineOperand &MO = getOperand(i);
1571
Dan Gohman80f6c582009-11-09 19:38:45 +00001572 if (FirstOp) FirstOp = false; else OS << ",";
Chris Lattner6a592272002-10-30 01:55:38 +00001573 OS << " ";
Francis Visoiu Mistrih2cba0cc2018-01-09 17:31:07 +00001574
Evan Cheng59b36552010-04-28 20:03:13 +00001575 if (isDebugValue() && MO.isMetadata()) {
1576 // Pretty print DBG_VALUE instructions.
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001577 auto *DIV = dyn_cast<DILocalVariable>(MO.getMetadata());
Duncan P. N. Exon Smith355ec002015-04-14 02:22:36 +00001578 if (DIV && !DIV->getName().empty())
1579 OS << "!\"" << DIV->getName() << '\"';
Francis Visoiu Mistrihfd11bc02017-12-07 10:40:31 +00001580 else {
1581 LLT TypeToPrint = MRI ? getTypeToPrint(i, PrintedTypes, *MRI) : LLT{};
Francis Visoiu Mistrihd3b84082017-12-07 17:12:30 +00001582 unsigned TiedOperandIdx = getTiedOperandIdx(i);
Francis Visoiu Mistrih7bee1ce2018-01-18 18:05:15 +00001583 MO.print(OS, MST, TypeToPrint, /*PrintDef=*/true, IsStandalone,
Francis Visoiu Mistrihfd11bc02017-12-07 10:40:31 +00001584 ShouldPrintRegisterTies, TiedOperandIdx, TRI, IntrinsicInfo);
1585 }
Shiva Chen08534222018-05-09 02:41:08 +00001586 } else if (isDebugLabel() && MO.isMetadata()) {
1587 // Pretty print DBG_LABEL instructions.
1588 auto *DIL = dyn_cast<DILabel>(MO.getMetadata());
1589 if (DIL && !DIL->getName().empty())
1590 OS << "\"" << DIL->getName() << '\"';
1591 else {
1592 LLT TypeToPrint = MRI ? getTypeToPrint(i, PrintedTypes, *MRI) : LLT{};
1593 unsigned TiedOperandIdx = getTiedOperandIdx(i);
1594 MO.print(OS, MST, TypeToPrint, /*PrintDef=*/true, IsStandalone,
1595 ShouldPrintRegisterTies, TiedOperandIdx, TRI, IntrinsicInfo);
1596 }
Jakob Stoklund Olesen7a2ecd32011-06-27 04:08:29 +00001597 } else if (i == AsmDescOp && MO.isImm()) {
1598 // Pretty print the inline asm operand descriptor.
1599 OS << '$' << AsmOpCount++;
1600 unsigned Flag = MO.getImm();
1601 switch (InlineAsm::getKind(Flag)) {
Jakob Stoklund Olesen459b74b2011-10-12 23:37:29 +00001602 case InlineAsm::Kind_RegUse: OS << ":[reguse"; break;
1603 case InlineAsm::Kind_RegDef: OS << ":[regdef"; break;
1604 case InlineAsm::Kind_RegDefEarlyClobber: OS << ":[regdef-ec"; break;
1605 case InlineAsm::Kind_Clobber: OS << ":[clobber"; break;
1606 case InlineAsm::Kind_Imm: OS << ":[imm"; break;
1607 case InlineAsm::Kind_Mem: OS << ":[mem"; break;
1608 default: OS << ":[??" << InlineAsm::getKind(Flag); break;
Jakob Stoklund Olesen7a2ecd32011-06-27 04:08:29 +00001609 }
1610
Jakob Stoklund Olesen459b74b2011-10-12 23:37:29 +00001611 unsigned RCID = 0;
Simon Dardis5ebefb82016-07-18 13:17:31 +00001612 if (!InlineAsm::isImmKind(Flag) && !InlineAsm::isMemKind(Flag) &&
1613 InlineAsm::hasRegClassConstraint(Flag, RCID)) {
Eric Christopher9656d2d2015-02-27 00:11:34 +00001614 if (TRI) {
1615 OS << ':' << TRI->getRegClassName(TRI->getRegClass(RCID));
Craig Toppera5babc82014-11-17 05:50:14 +00001616 } else
Jakob Stoklund Olesen459b74b2011-10-12 23:37:29 +00001617 OS << ":RC" << RCID;
Nick Lewycky3821b182011-10-13 00:54:59 +00001618 }
Jakob Stoklund Olesen459b74b2011-10-12 23:37:29 +00001619
Simon Dardis5ebefb82016-07-18 13:17:31 +00001620 if (InlineAsm::isMemKind(Flag)) {
1621 unsigned MCID = InlineAsm::getMemoryConstraintID(Flag);
1622 switch (MCID) {
1623 case InlineAsm::Constraint_es: OS << ":es"; break;
1624 case InlineAsm::Constraint_i: OS << ":i"; break;
1625 case InlineAsm::Constraint_m: OS << ":m"; break;
1626 case InlineAsm::Constraint_o: OS << ":o"; break;
1627 case InlineAsm::Constraint_v: OS << ":v"; break;
1628 case InlineAsm::Constraint_Q: OS << ":Q"; break;
1629 case InlineAsm::Constraint_R: OS << ":R"; break;
1630 case InlineAsm::Constraint_S: OS << ":S"; break;
1631 case InlineAsm::Constraint_T: OS << ":T"; break;
1632 case InlineAsm::Constraint_Um: OS << ":Um"; break;
1633 case InlineAsm::Constraint_Un: OS << ":Un"; break;
1634 case InlineAsm::Constraint_Uq: OS << ":Uq"; break;
1635 case InlineAsm::Constraint_Us: OS << ":Us"; break;
1636 case InlineAsm::Constraint_Ut: OS << ":Ut"; break;
1637 case InlineAsm::Constraint_Uv: OS << ":Uv"; break;
1638 case InlineAsm::Constraint_Uy: OS << ":Uy"; break;
1639 case InlineAsm::Constraint_X: OS << ":X"; break;
1640 case InlineAsm::Constraint_Z: OS << ":Z"; break;
1641 case InlineAsm::Constraint_ZC: OS << ":ZC"; break;
1642 case InlineAsm::Constraint_Zy: OS << ":Zy"; break;
1643 default: OS << ":?"; break;
1644 }
1645 }
1646
Jakob Stoklund Olesen7a2ecd32011-06-27 04:08:29 +00001647 unsigned TiedTo = 0;
1648 if (InlineAsm::isUseOperandTiedToDef(Flag, TiedTo))
Jakob Stoklund Olesen459b74b2011-10-12 23:37:29 +00001649 OS << " tiedto:$" << TiedTo;
1650
1651 OS << ']';
Jakob Stoklund Olesen7a2ecd32011-06-27 04:08:29 +00001652
1653 // Compute the index of the next operand descriptor.
1654 AsmDescOp += 1 + InlineAsm::getNumOperandRegisters(Flag);
Francis Visoiu Mistrihfd11bc02017-12-07 10:40:31 +00001655 } else {
1656 LLT TypeToPrint = MRI ? getTypeToPrint(i, PrintedTypes, *MRI) : LLT{};
Francis Visoiu Mistrihd3b84082017-12-07 17:12:30 +00001657 unsigned TiedOperandIdx = getTiedOperandIdx(i);
Francis Visoiu Mistrihe28484a2017-12-08 22:53:21 +00001658 if (MO.isImm() && isOperandSubregIdx(i))
Francis Visoiu Mistrih19988dd2018-01-16 10:53:11 +00001659 MachineOperand::printSubRegIdx(OS, MO.getImm(), TRI);
Francis Visoiu Mistrihe28484a2017-12-08 22:53:21 +00001660 else
Francis Visoiu Mistrih7bee1ce2018-01-18 18:05:15 +00001661 MO.print(OS, MST, TypeToPrint, /*PrintDef=*/true, IsStandalone,
Francis Visoiu Mistrihe28484a2017-12-08 22:53:21 +00001662 ShouldPrintRegisterTies, TiedOperandIdx, TRI, IntrinsicInfo);
Francis Visoiu Mistrihfd11bc02017-12-07 10:40:31 +00001663 }
Dan Gohman80f6c582009-11-09 19:38:45 +00001664 }
1665
Chandler Carruthe90d4402018-08-16 23:11:05 +00001666 // Print any optional symbols attached to this instruction as-if they were
1667 // operands.
1668 if (MCSymbol *PreInstrSymbol = getPreInstrSymbol()) {
1669 if (!FirstOp) {
1670 FirstOp = false;
1671 OS << ',';
1672 }
1673 OS << " pre-instr-symbol ";
1674 MachineOperand::printSymbol(OS, *PreInstrSymbol);
1675 }
1676 if (MCSymbol *PostInstrSymbol = getPostInstrSymbol()) {
1677 if (!FirstOp) {
1678 FirstOp = false;
1679 OS << ',';
1680 }
1681 OS << " post-instr-symbol ";
1682 MachineOperand::printSymbol(OS, *PostInstrSymbol);
1683 }
1684
Francis Visoiu Mistrihe97eca62018-01-19 11:44:42 +00001685 if (!SkipDebugLoc) {
1686 if (const DebugLoc &DL = getDebugLoc()) {
1687 if (!FirstOp)
1688 OS << ',';
1689 OS << " debug-location ";
1690 DL->printAsOperand(OS, MST);
1691 }
1692 }
1693
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001694 if (!memoperands_empty()) {
Francis Visoiu Mistrih0d758f32018-03-14 21:52:13 +00001695 SmallVector<StringRef, 0> SSNs;
1696 const LLVMContext *Context = nullptr;
1697 std::unique_ptr<LLVMContext> CtxPtr;
1698 const MachineFrameInfo *MFI = nullptr;
1699 if (const MachineFunction *MF = getMFIfAvailable(*this)) {
1700 MFI = &MF->getFrameInfo();
1701 Context = &MF->getFunction().getContext();
1702 } else {
1703 CtxPtr = llvm::make_unique<LLVMContext>();
1704 Context = CtxPtr.get();
Yaron Keren8e6708e2016-01-02 13:40:36 +00001705 }
Dan Gohman0ba90f3e2009-10-31 20:19:03 +00001706
Francis Visoiu Mistrih0d758f32018-03-14 21:52:13 +00001707 OS << " :: ";
1708 bool NeedComma = false;
1709 for (const MachineMemOperand *Op : memoperands()) {
1710 if (NeedComma)
1711 OS << ", ";
1712 Op->print(OS, MST, SSNs, *Context, MFI, TII);
1713 NeedComma = true;
Dan Gohman69de1932008-02-06 22:27:42 +00001714 }
1715 }
1716
Francis Visoiu Mistrihe97eca62018-01-19 11:44:42 +00001717 if (SkipDebugLoc)
1718 return;
1719
Francis Visoiu Mistrih0d758f32018-03-14 21:52:13 +00001720 bool HaveSemi = false;
Francis Visoiu Mistrihb91ac892018-04-24 11:00:46 +00001721
Anton Korobeynikov6dd97472011-03-05 18:43:04 +00001722 // Print debug location information.
Francis Visoiu Mistrihb91ac892018-04-24 11:00:46 +00001723 if (const DebugLoc &DL = getDebugLoc()) {
1724 if (!HaveSemi) {
1725 OS << ';';
1726 HaveSemi = true;
1727 }
1728 OS << ' ';
1729 DL.print(OS);
1730 }
1731
1732 // Print extra comments for DEBUG_VALUE.
Duncan P. N. Exon Smith2d252092015-04-03 16:23:04 +00001733 if (isDebugValue() && getOperand(e - 2).isMetadata()) {
Francis Visoiu Mistrihb91ac892018-04-24 11:00:46 +00001734 if (!HaveSemi) {
Yaron Keren8e6708e2016-01-02 13:40:36 +00001735 OS << ";";
Francis Visoiu Mistrihb91ac892018-04-24 11:00:46 +00001736 HaveSemi = true;
1737 }
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +00001738 auto *DV = cast<DILocalVariable>(getOperand(e - 2).getMetadata());
Duncan P. N. Exon Smith355ec002015-04-14 02:22:36 +00001739 OS << " line no:" << DV->getLine();
Duncan P. N. Exon Smith88e419d2015-04-15 22:29:27 +00001740 if (auto *InlinedAt = debugLoc->getInlinedAt()) {
Duncan P. N. Exon Smith3637bab2015-03-30 19:14:47 +00001741 DebugLoc InlinedAtDL(InlinedAt);
1742 if (InlinedAtDL && MF) {
Devang Patel4d3586d2011-08-04 20:44:26 +00001743 OS << " inlined @[ ";
NAKAMURA Takumi09c0ea52015-09-22 11:15:07 +00001744 InlinedAtDL.print(OS);
Devang Patel4d3586d2011-08-04 20:44:26 +00001745 OS << " ]";
1746 }
1747 }
Adrian Prantl02474a32014-10-01 18:55:02 +00001748 if (isIndirectDebugValue())
1749 OS << " indirect";
Bill Wendlingb5ef2732009-02-19 21:44:55 +00001750 }
Shiva Chen24abe712018-05-09 02:42:00 +00001751 // TODO: DBG_LABEL
Francis Visoiu Mistrihf751ac32018-02-19 15:08:49 +00001752
Krzysztof Parzyszekb8924a02018-04-10 16:46:13 +00001753 if (AddNewLine)
1754 OS << '\n';
Chris Lattner10491642002-10-30 00:48:05 +00001755}
1756
Owen Andersonb487e722008-01-24 01:10:07 +00001757bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001758 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +00001759 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001760 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +00001761 bool hasAliases = isPhysReg &&
1762 MCRegAliasIterator(IncomingReg, RegInfo, false).isValid();
Dan Gohman3f629402008-09-03 15:56:16 +00001763 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001764 SmallVector<unsigned,4> DeadOps;
Bill Wendling4a23d722008-03-03 22:14:33 +00001765 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1766 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesenefb8e3e2009-08-04 20:09:25 +00001767 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001768 continue;
Mandeep Singh Grangefde4d32016-05-10 17:57:27 +00001769
1770 // DEBUG_VALUE nodes do not contribute to code generation and should
1771 // always be ignored. Failure to do so may result in trying to modify
1772 // KILL flags on DEBUG_VALUE nodes.
1773 if (MO.isDebug())
1774 continue;
1775
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001776 unsigned Reg = MO.getReg();
1777 if (!Reg)
1778 continue;
Bill Wendling4a23d722008-03-03 22:14:33 +00001779
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001780 if (Reg == IncomingReg) {
Dan Gohman3f629402008-09-03 15:56:16 +00001781 if (!Found) {
1782 if (MO.isKill())
1783 // The register is already marked kill.
1784 return true;
Jakob Stoklund Olesenece48182009-08-02 19:13:03 +00001785 if (isPhysReg && isRegTiedToDefOperand(i))
1786 // Two-address uses of physregs must not be marked kill.
1787 return true;
Dan Gohman3f629402008-09-03 15:56:16 +00001788 MO.setIsKill();
1789 Found = true;
1790 }
1791 } else if (hasAliases && MO.isKill() &&
1792 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001793 // A super-register kill already exists.
1794 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001795 return true;
1796 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001797 DeadOps.push_back(i);
Bill Wendling4a23d722008-03-03 22:14:33 +00001798 }
1799 }
1800
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001801 // Trim unneeded kill operands.
1802 while (!DeadOps.empty()) {
1803 unsigned OpIdx = DeadOps.back();
Craig Topperee5f4632018-09-13 20:51:27 +00001804 if (getOperand(OpIdx).isImplicit() &&
1805 (!isInlineAsm() || findInlineAsmFlagIdx(OpIdx) < 0))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001806 RemoveOperand(OpIdx);
1807 else
1808 getOperand(OpIdx).setIsKill(false);
1809 DeadOps.pop_back();
1810 }
1811
Bill Wendling4a23d722008-03-03 22:14:33 +00001812 // If not found, this means an alias of one of the operands is killed. Add a
Owen Andersonb487e722008-01-24 01:10:07 +00001813 // new implicit operand if required.
Dan Gohman3f629402008-09-03 15:56:16 +00001814 if (!Found && AddIfNotFound) {
Bill Wendling4a23d722008-03-03 22:14:33 +00001815 addOperand(MachineOperand::CreateReg(IncomingReg,
1816 false /*IsDef*/,
1817 true /*IsImp*/,
1818 true /*IsKill*/));
Owen Andersonb487e722008-01-24 01:10:07 +00001819 return true;
1820 }
Dan Gohman3f629402008-09-03 15:56:16 +00001821 return Found;
Owen Andersonb487e722008-01-24 01:10:07 +00001822}
1823
Jakob Stoklund Olesen1a96c912012-01-26 17:52:15 +00001824void MachineInstr::clearRegisterKills(unsigned Reg,
1825 const TargetRegisterInfo *RegInfo) {
1826 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topper4ba84432014-04-14 00:51:57 +00001827 RegInfo = nullptr;
Benjamin Kramer59d81db2015-02-21 17:08:08 +00001828 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen1a96c912012-01-26 17:52:15 +00001829 if (!MO.isReg() || !MO.isUse() || !MO.isKill())
1830 continue;
1831 unsigned OpReg = MO.getReg();
Matthias Brauncd4f2892016-02-24 19:21:48 +00001832 if ((RegInfo && RegInfo->regsOverlap(Reg, OpReg)) || Reg == OpReg)
Jakob Stoklund Olesen1a96c912012-01-26 17:52:15 +00001833 MO.setIsKill(false);
1834 }
1835}
1836
Matthias Braun4afb5f52013-10-10 21:28:38 +00001837bool MachineInstr::addRegisterDead(unsigned Reg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001838 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +00001839 bool AddIfNotFound) {
Matthias Braun4afb5f52013-10-10 21:28:38 +00001840 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(Reg);
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +00001841 bool hasAliases = isPhysReg &&
Matthias Braun4afb5f52013-10-10 21:28:38 +00001842 MCRegAliasIterator(Reg, RegInfo, false).isValid();
Dan Gohman3f629402008-09-03 15:56:16 +00001843 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001844 SmallVector<unsigned,4> DeadOps;
Owen Andersonb487e722008-01-24 01:10:07 +00001845 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1846 MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001847 if (!MO.isReg() || !MO.isDef())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001848 continue;
Matthias Braun4afb5f52013-10-10 21:28:38 +00001849 unsigned MOReg = MO.getReg();
1850 if (!MOReg)
Dan Gohman3f629402008-09-03 15:56:16 +00001851 continue;
1852
Matthias Braun4afb5f52013-10-10 21:28:38 +00001853 if (MOReg == Reg) {
Jakob Stoklund Olesenb793bc12011-04-05 16:53:50 +00001854 MO.setIsDead();
1855 Found = true;
Dan Gohman3f629402008-09-03 15:56:16 +00001856 } else if (hasAliases && MO.isDead() &&
Matthias Braun4afb5f52013-10-10 21:28:38 +00001857 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001858 // There exists a super-register that's marked dead.
Matthias Braun4afb5f52013-10-10 21:28:38 +00001859 if (RegInfo->isSuperRegister(Reg, MOReg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001860 return true;
Matthias Braun4afb5f52013-10-10 21:28:38 +00001861 if (RegInfo->isSubRegister(Reg, MOReg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001862 DeadOps.push_back(i);
Owen Andersonb487e722008-01-24 01:10:07 +00001863 }
1864 }
1865
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001866 // Trim unneeded dead operands.
1867 while (!DeadOps.empty()) {
1868 unsigned OpIdx = DeadOps.back();
Craig Topperee5f4632018-09-13 20:51:27 +00001869 if (getOperand(OpIdx).isImplicit() &&
1870 (!isInlineAsm() || findInlineAsmFlagIdx(OpIdx) < 0))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001871 RemoveOperand(OpIdx);
1872 else
1873 getOperand(OpIdx).setIsDead(false);
1874 DeadOps.pop_back();
1875 }
1876
Dan Gohman3f629402008-09-03 15:56:16 +00001877 // If not found, this means an alias of one of the operands is dead. Add a
1878 // new implicit operand if required.
Chris Lattner31530612009-06-24 17:54:48 +00001879 if (Found || !AddIfNotFound)
1880 return Found;
Jim Grosbachee61d672011-08-24 16:44:17 +00001881
Matthias Braun4afb5f52013-10-10 21:28:38 +00001882 addOperand(MachineOperand::CreateReg(Reg,
Chris Lattner31530612009-06-24 17:54:48 +00001883 true /*IsDef*/,
1884 true /*IsImp*/,
1885 false /*IsKill*/,
1886 true /*IsDead*/));
1887 return true;
Owen Andersonb487e722008-01-24 01:10:07 +00001888}
Jakob Stoklund Olesen8efadf92010-01-06 00:29:28 +00001889
Matthias Brauna602c102015-02-04 19:35:16 +00001890void MachineInstr::clearRegisterDeads(unsigned Reg) {
1891 for (MachineOperand &MO : operands()) {
1892 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg)
1893 continue;
1894 MO.setIsDead(false);
1895 }
1896}
1897
Matthias Braunf98fd352015-11-11 00:41:58 +00001898void MachineInstr::setRegisterDefReadUndef(unsigned Reg, bool IsUndef) {
Matthias Braun9a43e3d2015-01-21 22:55:13 +00001899 for (MachineOperand &MO : operands()) {
1900 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg || MO.getSubReg() == 0)
1901 continue;
Matthias Braunf98fd352015-11-11 00:41:58 +00001902 MO.setIsUndef(IsUndef);
Matthias Braun9a43e3d2015-01-21 22:55:13 +00001903 }
1904}
1905
Matthias Braun4afb5f52013-10-10 21:28:38 +00001906void MachineInstr::addRegisterDefined(unsigned Reg,
Jakob Stoklund Olesen8efadf92010-01-06 00:29:28 +00001907 const TargetRegisterInfo *RegInfo) {
Matthias Braun4afb5f52013-10-10 21:28:38 +00001908 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1909 MachineOperand *MO = findRegisterDefOperand(Reg, false, RegInfo);
Jakob Stoklund Olesen63e6a482010-05-21 16:32:16 +00001910 if (MO)
1911 return;
1912 } else {
Benjamin Kramer59d81db2015-02-21 17:08:08 +00001913 for (const MachineOperand &MO : operands()) {
Matthias Braun4afb5f52013-10-10 21:28:38 +00001914 if (MO.isReg() && MO.getReg() == Reg && MO.isDef() &&
Jakob Stoklund Olesen63e6a482010-05-21 16:32:16 +00001915 MO.getSubReg() == 0)
1916 return;
1917 }
1918 }
Matthias Braun4afb5f52013-10-10 21:28:38 +00001919 addOperand(MachineOperand::CreateReg(Reg,
Jakob Stoklund Olesen63e6a482010-05-21 16:32:16 +00001920 true /*IsDef*/,
1921 true /*IsImp*/));
Jakob Stoklund Olesen8efadf92010-01-06 00:29:28 +00001922}
Evan Cheng67eaa082010-03-03 23:37:30 +00001923
Jakob Stoklund Olesena37818d2012-02-03 20:43:39 +00001924void MachineInstr::setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs,
Dan Gohmandb497122010-06-18 23:28:01 +00001925 const TargetRegisterInfo &TRI) {
Jakob Stoklund Olesen77180e02012-02-03 21:23:14 +00001926 bool HasRegMask = false;
Benjamin Kramer59d81db2015-02-21 17:08:08 +00001927 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen77180e02012-02-03 21:23:14 +00001928 if (MO.isRegMask()) {
1929 HasRegMask = true;
1930 continue;
1931 }
Dan Gohmandb497122010-06-18 23:28:01 +00001932 if (!MO.isReg() || !MO.isDef()) continue;
1933 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen59cb77f2012-02-03 20:43:35 +00001934 if (!TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Dan Gohmandb497122010-06-18 23:28:01 +00001935 // If there are no uses, including partial uses, the def is dead.
Eugene Zelenko1d081e62017-05-31 01:10:10 +00001936 if (llvm::none_of(UsedRegs,
1937 [&](unsigned Use) { return TRI.regsOverlap(Use, Reg); }))
Benjamin Kramer59d81db2015-02-21 17:08:08 +00001938 MO.setIsDead();
Dan Gohmandb497122010-06-18 23:28:01 +00001939 }
Jakob Stoklund Olesen77180e02012-02-03 21:23:14 +00001940
1941 // This is a call with a register mask operand.
1942 // Mask clobbers are always dead, so add defs for the non-dead defines.
1943 if (HasRegMask)
1944 for (ArrayRef<unsigned>::iterator I = UsedRegs.begin(), E = UsedRegs.end();
1945 I != E; ++I)
1946 addRegisterDefined(*I, &TRI);
Dan Gohmandb497122010-06-18 23:28:01 +00001947}
1948
Evan Cheng67eaa082010-03-03 23:37:30 +00001949unsigned
1950MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) {
Chandler Carruthfc226252012-03-07 09:39:46 +00001951 // Build up a buffer of hash code components.
Chandler Carruthfc226252012-03-07 09:39:46 +00001952 SmallVector<size_t, 8> HashComponents;
1953 HashComponents.reserve(MI->getNumOperands() + 1);
1954 HashComponents.push_back(MI->getOpcode());
Benjamin Kramer59d81db2015-02-21 17:08:08 +00001955 for (const MachineOperand &MO : MI->operands()) {
Chandler Carruthd862d692012-07-05 11:06:22 +00001956 if (MO.isReg() && MO.isDef() &&
1957 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
1958 continue; // Skip virtual register defs.
1959
1960 HashComponents.push_back(hash_value(MO));
Evan Cheng67eaa082010-03-03 23:37:30 +00001961 }
Chandler Carruthfc226252012-03-07 09:39:46 +00001962 return hash_combine_range(HashComponents.begin(), HashComponents.end());
Evan Cheng67eaa082010-03-03 23:37:30 +00001963}
Jakob Stoklund Olesend519de02011-07-02 03:53:34 +00001964
1965void MachineInstr::emitError(StringRef Msg) const {
1966 // Find the source location cookie.
1967 unsigned LocCookie = 0;
Craig Topper4ba84432014-04-14 00:51:57 +00001968 const MDNode *LocMD = nullptr;
Jakob Stoklund Olesend519de02011-07-02 03:53:34 +00001969 for (unsigned i = getNumOperands(); i != 0; --i) {
1970 if (getOperand(i-1).isMetadata() &&
1971 (LocMD = getOperand(i-1).getMetadata()) &&
1972 LocMD->getNumOperands() != 0) {
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +00001973 if (const ConstantInt *CI =
1974 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
Jakob Stoklund Olesend519de02011-07-02 03:53:34 +00001975 LocCookie = CI->getZExtValue();
1976 break;
1977 }
1978 }
1979 }
1980
1981 if (const MachineBasicBlock *MBB = getParent())
1982 if (const MachineFunction *MF = MBB->getParent())
1983 return MF->getMMI().getModule()->getContext().emitError(LocCookie, Msg);
1984 report_fatal_error(Msg);
1985}
Reid Kleckner13fb5a32016-04-14 18:29:59 +00001986
Benjamin Krameraf18e012016-06-12 15:39:02 +00001987MachineInstrBuilder llvm::BuildMI(MachineFunction &MF, const DebugLoc &DL,
Reid Kleckner13fb5a32016-04-14 18:29:59 +00001988 const MCInstrDesc &MCID, bool IsIndirect,
Adrian Prantl4df9b5f2017-07-28 23:00:45 +00001989 unsigned Reg, const MDNode *Variable,
1990 const MDNode *Expr) {
Reid Kleckner13fb5a32016-04-14 18:29:59 +00001991 assert(isa<DILocalVariable>(Variable) && "not a variable");
1992 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
1993 assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
1994 "Expected inlined-at fields to agree");
Mikael Holmenb16b4ba2018-06-21 10:03:34 +00001995 auto MIB = BuildMI(MF, DL, MCID).addReg(Reg, RegState::Debug);
Reid Kleckner13fb5a32016-04-14 18:29:59 +00001996 if (IsIndirect)
Mikael Holmenb16b4ba2018-06-21 10:03:34 +00001997 MIB.addImm(0U);
Adrian Prantl4df9b5f2017-07-28 23:00:45 +00001998 else
Mikael Holmenb16b4ba2018-06-21 10:03:34 +00001999 MIB.addReg(0U, RegState::Debug);
2000 return MIB.addMetadata(Variable).addMetadata(Expr);
Reid Kleckner13fb5a32016-04-14 18:29:59 +00002001}
2002
Mikael Holmenb16b4ba2018-06-21 10:03:34 +00002003MachineInstrBuilder llvm::BuildMI(MachineFunction &MF, const DebugLoc &DL,
2004 const MCInstrDesc &MCID, bool IsIndirect,
2005 MachineOperand &MO, const MDNode *Variable,
2006 const MDNode *Expr) {
2007 assert(isa<DILocalVariable>(Variable) && "not a variable");
2008 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
2009 assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
2010 "Expected inlined-at fields to agree");
2011 if (MO.isReg())
2012 return BuildMI(MF, DL, MCID, IsIndirect, MO.getReg(), Variable, Expr);
2013
2014 auto MIB = BuildMI(MF, DL, MCID).add(MO);
2015 if (IsIndirect)
2016 MIB.addImm(0U);
2017 else
2018 MIB.addReg(0U, RegState::Debug);
2019 return MIB.addMetadata(Variable).addMetadata(Expr);
2020 }
2021
Reid Kleckner13fb5a32016-04-14 18:29:59 +00002022MachineInstrBuilder llvm::BuildMI(MachineBasicBlock &BB,
Benjamin Krameraf18e012016-06-12 15:39:02 +00002023 MachineBasicBlock::iterator I,
2024 const DebugLoc &DL, const MCInstrDesc &MCID,
2025 bool IsIndirect, unsigned Reg,
Adrian Prantl4df9b5f2017-07-28 23:00:45 +00002026 const MDNode *Variable, const MDNode *Expr) {
Reid Kleckner13fb5a32016-04-14 18:29:59 +00002027 MachineFunction &MF = *BB.getParent();
Adrian Prantl4df9b5f2017-07-28 23:00:45 +00002028 MachineInstr *MI = BuildMI(MF, DL, MCID, IsIndirect, Reg, Variable, Expr);
Reid Kleckner13fb5a32016-04-14 18:29:59 +00002029 BB.insert(I, MI);
2030 return MachineInstrBuilder(MF, MI);
2031}
Adrian Prantlb560ea72017-04-18 01:21:53 +00002032
Mikael Holmenb16b4ba2018-06-21 10:03:34 +00002033MachineInstrBuilder llvm::BuildMI(MachineBasicBlock &BB,
2034 MachineBasicBlock::iterator I,
2035 const DebugLoc &DL, const MCInstrDesc &MCID,
2036 bool IsIndirect, MachineOperand &MO,
2037 const MDNode *Variable, const MDNode *Expr) {
2038 MachineFunction &MF = *BB.getParent();
2039 MachineInstr *MI = BuildMI(MF, DL, MCID, IsIndirect, MO, Variable, Expr);
2040 BB.insert(I, MI);
2041 return MachineInstrBuilder(MF, *MI);
2042}
2043
Reid Kleckneraf2e5222017-09-15 21:49:56 +00002044/// Compute the new DIExpression to use with a DBG_VALUE for a spill slot.
2045/// This prepends DW_OP_deref when spilling an indirect DBG_VALUE.
2046static const DIExpression *computeExprForSpill(const MachineInstr &MI) {
2047 assert(MI.getOperand(0).isReg() && "can't spill non-register");
2048 assert(MI.getDebugVariable()->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
2049 "Expected inlined-at fields to agree");
2050
2051 const DIExpression *Expr = MI.getDebugExpression();
2052 if (MI.isIndirectDebugValue()) {
2053 assert(MI.getOperand(1).getImm() == 0 && "DBG_VALUE with nonzero offset");
2054 Expr = DIExpression::prepend(Expr, DIExpression::WithDeref);
2055 }
2056 return Expr;
2057}
2058
Adrian Prantlb560ea72017-04-18 01:21:53 +00002059MachineInstr *llvm::buildDbgValueForSpill(MachineBasicBlock &BB,
2060 MachineBasicBlock::iterator I,
2061 const MachineInstr &Orig,
2062 int FrameIndex) {
Reid Kleckneraf2e5222017-09-15 21:49:56 +00002063 const DIExpression *Expr = computeExprForSpill(Orig);
2064 return BuildMI(BB, I, Orig.getDebugLoc(), Orig.getDesc())
Adrian Prantlb560ea72017-04-18 01:21:53 +00002065 .addFrameIndex(FrameIndex)
Adrian Prantl4df9b5f2017-07-28 23:00:45 +00002066 .addImm(0U)
Reid Kleckneraf2e5222017-09-15 21:49:56 +00002067 .addMetadata(Orig.getDebugVariable())
Adrian Prantlb560ea72017-04-18 01:21:53 +00002068 .addMetadata(Expr);
2069}
Reid Kleckneraf2e5222017-09-15 21:49:56 +00002070
2071void llvm::updateDbgValueForSpill(MachineInstr &Orig, int FrameIndex) {
2072 const DIExpression *Expr = computeExprForSpill(Orig);
2073 Orig.getOperand(0).ChangeToFrameIndex(FrameIndex);
2074 Orig.getOperand(1).ChangeToImmediate(0U);
2075 Orig.getOperand(3).setMetadata(Expr);
2076}
Carlos Alberto Enciso03c16fb2018-08-30 07:17:41 +00002077
2078void MachineInstr::collectDebugValues(
2079 SmallVectorImpl<MachineInstr *> &DbgValues) {
2080 MachineInstr &MI = *this;
2081 if (!MI.getOperand(0).isReg())
2082 return;
2083
2084 MachineBasicBlock::iterator DI = MI; ++DI;
2085 for (MachineBasicBlock::iterator DE = MI.getParent()->end();
2086 DI != DE; ++DI) {
2087 if (!DI->isDebugValue())
2088 return;
2089 if (DI->getOperand(0).isReg() &&
2090 DI->getOperand(0).getReg() == MI.getOperand(0).getReg())
2091 DbgValues.push_back(&*DI);
2092 }
2093}
Carlos Alberto Enciso42b54432018-10-01 08:14:44 +00002094
2095void MachineInstr::changeDebugValuesDefReg(unsigned Reg) {
2096 // Collect matching debug values.
2097 SmallVector<MachineInstr *, 2> DbgValues;
2098 collectDebugValues(DbgValues);
2099
2100 // Propagate Reg to debug value instructions.
2101 for (auto *DBI : DbgValues)
2102 DBI->getOperand(0).setReg(Reg);
2103}