blob: 7149903412d24e55fe9d4a5c30e1fc928b1fb1e1 [file] [log] [blame]
Eugene Zelenko4e9736b2017-05-31 01:10:10 +00001//===- lib/CodeGen/MachineInstr.cpp ---------------------------------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman835702a2005-04-21 22:36:52 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Brian Gaekee8f7c2f2004-02-13 04:39:32 +00009//
10// Methods common to all machine instructions.
11//
Chris Lattner959a5fb2002-08-09 20:08:06 +000012//===----------------------------------------------------------------------===//
Vikram S. Adveab9e5572001-07-21 12:41:50 +000013
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "llvm/CodeGen/MachineInstr.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000015#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/ArrayRef.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/FoldingSet.h"
18#include "llvm/ADT/Hashing.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000019#include "llvm/ADT/None.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000020#include "llvm/ADT/STLExtras.h"
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +000021#include "llvm/ADT/SmallBitVector.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000022#include "llvm/ADT/SmallString.h"
23#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Analysis/AliasAnalysis.h"
Hiroshi Inoue95f24dc2017-06-24 15:17:38 +000025#include "llvm/Analysis/Loads.h"
Eugene Zelenko4e9736b2017-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 Lattner63f41ab2004-02-19 16:17:08 +000029#include "llvm/CodeGen/MachineFunction.h"
Reid Kleckner28865802016-04-14 18:29:59 +000030#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000031#include "llvm/CodeGen/MachineInstrBundle.h"
Dan Gohman48b185d2009-09-25 20:36:54 +000032#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000034#include "llvm/CodeGen/MachineOperand.h"
Chris Lattner961e7422008-01-01 01:12:31 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman2d489b52008-02-06 22:27:42 +000036#include "llvm/CodeGen/PseudoSourceValue.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000037#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000038#include "llvm/CodeGen/TargetRegisterInfo.h"
39#include "llvm/CodeGen/TargetSubtargetInfo.h"
Nico Weber432a3882018-04-30 14:59:11 +000040#include "llvm/Config/llvm-config.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000041#include "llvm/IR/Constants.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000042#include "llvm/IR/DebugInfoMetadata.h"
43#include "llvm/IR/DebugLoc.h"
44#include "llvm/IR/DerivedTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000045#include "llvm/IR/Function.h"
46#include "llvm/IR/InlineAsm.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000047#include "llvm/IR/InstrTypes.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000048#include "llvm/IR/Intrinsics.h"
Chandler Carruth9fb823b2013-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 Smithf48e9822015-06-26 22:06:47 +000052#include "llvm/IR/ModuleSlotTracker.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000053#include "llvm/IR/Type.h"
54#include "llvm/IR/Value.h"
Evan Cheng6cc775f2011-06-28 19:10:37 +000055#include "llvm/MC/MCInstrDesc.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000056#include "llvm/MC/MCRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000057#include "llvm/MC/MCSymbol.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000058#include "llvm/Support/Casting.h"
Daniel Sanders1e97a0b2015-08-19 12:03:04 +000059#include "llvm/Support/CommandLine.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000060#include "llvm/Support/Compiler.h"
David Greene29388d62010-01-04 23:48:20 +000061#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000062#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000063#include "llvm/Support/LowLevelTypeImpl.h"
Dan Gohmanaedb4a62008-07-07 20:32:02 +000064#include "llvm/Support/MathExtras.h"
Chris Lattnera078d832008-08-24 20:37:32 +000065#include "llvm/Support/raw_ostream.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000066#include "llvm/Target/TargetIntrinsicInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000067#include "llvm/Target/TargetMachine.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000068#include <algorithm>
69#include <cassert>
70#include <cstddef>
71#include <cstdint>
72#include <cstring>
73#include <iterator>
74#include <utility>
75
Chris Lattner43df6c22004-02-23 18:38:20 +000076using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000077
Francis Visoiu Mistrih586444e2018-01-18 14:52:14 +000078static const MachineFunction *getMFIfAvailable(const MachineInstr &MI) {
79 if (const MachineBasicBlock *MBB = MI.getParent())
80 if (const MachineFunction *MF = MBB->getParent())
81 return MF;
82 return nullptr;
83}
84
85// Try to crawl up to the machine function and get TRI and IntrinsicInfo from
86// it.
87static void tryToGetTargetInfo(const MachineInstr &MI,
88 const TargetRegisterInfo *&TRI,
89 const MachineRegisterInfo *&MRI,
90 const TargetIntrinsicInfo *&IntrinsicInfo,
91 const TargetInstrInfo *&TII) {
92
93 if (const MachineFunction *MF = getMFIfAvailable(MI)) {
94 TRI = MF->getSubtarget().getRegisterInfo();
95 MRI = &MF->getRegInfo();
96 IntrinsicInfo = MF->getTarget().getIntrinsicInfo();
97 TII = MF->getSubtarget().getInstrInfo();
98 }
99}
100
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000101void MachineInstr::addImplicitDefUseOperands(MachineFunction &MF) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000102 if (MCID->ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000103 for (const MCPhysReg *ImpDefs = MCID->getImplicitDefs(); *ImpDefs;
104 ++ImpDefs)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000105 addOperand(MF, MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng6cc775f2011-06-28 19:10:37 +0000106 if (MCID->ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000107 for (const MCPhysReg *ImpUses = MCID->getImplicitUses(); *ImpUses;
108 ++ImpUses)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000109 addOperand(MF, MachineOperand::CreateReg(*ImpUses, false, true));
Evan Cheng77af6ac2006-11-13 23:34:06 +0000110}
111
Bob Wilson406f2702010-04-09 04:34:03 +0000112/// MachineInstr ctor - This constructor creates a MachineInstr and adds the
113/// implicit operands. It reserves space for the number of operands specified by
Evan Cheng6cc775f2011-06-28 19:10:37 +0000114/// the MCInstrDesc.
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000115MachineInstr::MachineInstr(MachineFunction &MF, const MCInstrDesc &tid,
Benjamin Kramera9591b52015-02-07 12:28:15 +0000116 DebugLoc dl, bool NoImp)
Eugene Zelenko4e9736b2017-05-31 01:10:10 +0000117 : MCID(&tid), debugLoc(std::move(dl)) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000118 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
119
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000120 // Reserve space for the expected number of operands.
121 if (unsigned NumOps = MCID->getNumOperands() +
122 MCID->getNumImplicitDefs() + MCID->getNumImplicitUses()) {
123 CapOperands = OperandCapacity::get(NumOps);
124 Operands = MF.allocateOperandArray(CapOperands);
125 }
126
Dale Johannesen4e04ef32009-01-27 23:20:29 +0000127 if (!NoImp)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000128 addImplicitDefUseOperands(MF);
Dale Johannesen4e04ef32009-01-27 23:20:29 +0000129}
130
Misha Brukmanb47ab7a2004-07-09 14:45:17 +0000131/// MachineInstr ctor - Copies MachineInstr arg exactly
132///
Evan Chenga7a20c42008-07-19 00:37:25 +0000133MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Eugene Zelenko4e9736b2017-05-31 01:10:10 +0000134 : MCID(&MI.getDesc()), NumMemRefs(MI.NumMemRefs), MemRefs(MI.MemRefs),
135 debugLoc(MI.getDebugLoc()) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000136 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
137
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000138 CapOperands = OperandCapacity::get(MI.getNumOperands());
139 Operands = MF.allocateOperandArray(CapOperands);
Tanya Lattner9953d862004-05-23 20:58:02 +0000140
Jakob Stoklund Olesendc5285f2013-01-05 05:05:51 +0000141 // Copy operands.
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000142 for (const MachineOperand &MO : MI.operands())
143 addOperand(MF, MO);
Tanya Lattnerbcee21b2004-05-24 03:14:18 +0000144
Jakob Stoklund Olesena33f5042012-12-18 21:36:05 +0000145 // Copy all the sensible flags.
146 setFlags(MI.Flags);
Alkis Evlogimenos14f3fe82004-02-16 07:17:43 +0000147}
148
Chris Lattner961e7422008-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 Gohmanf188fa42008-07-08 23:59:09 +0000154 return &MBB->getParent()->getRegInfo();
Craig Topperc0196b12014-04-14 00:51:57 +0000155 return nullptr;
Chris Lattner961e7422008-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 Olesenc4102d42012-08-09 22:49:37 +0000161void MachineInstr::RemoveRegOperandsFromUseLists(MachineRegisterInfo &MRI) {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000162 for (MachineOperand &MO : operands())
163 if (MO.isReg())
164 MRI.removeRegOperandFromUseList(&MO);
Chris Lattner961e7422008-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 Olesenc4102d42012-08-09 22:49:37 +0000170void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &MRI) {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000171 for (MachineOperand &MO : operands())
172 if (MO.isReg())
173 MRI.addRegOperandToUseList(&MO);
Chris Lattner961e7422008-01-01 01:12:31 +0000174}
175
Jakob Stoklund Olesen2455b5852012-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 Olesen1bfeecb2013-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 Bastiena874d1a2016-03-26 18:20:02 +0000191 // MachineOperand is a trivially copyable type so we can just use memmove.
Benjamin Kramer5c0e64f2015-02-21 16:22:48 +0000192 std::memmove(Dst, Src, NumOps * sizeof(MachineOperand));
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000193}
194
Chris Lattner961e7422008-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 Grosbachdee9e8a2011-08-24 16:44:17 +0000198/// (before the first implicit operand).
Jakob Stoklund Olesen2455b5852012-12-20 22:54:05 +0000199void MachineInstr::addOperand(MachineFunction &MF, const MachineOperand &Op) {
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000200 assert(MCID && "Cannot add operands before providing an instr descriptor");
Dan Gohman9356d8f2008-12-09 22:45:08 +0000201
Jakob Stoklund Olesen1bfeecb2013-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 Grosbachdee9e8a2011-08-24 16:44:17 +0000210
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000211 // Find the insert location for the new operand. Implicit registers go at
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000212 // the end, everything else goes before the implicit regs.
213 //
Jakob Stoklund Olesen2318d1e2011-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 Olesen1bfeecb2013-01-05 05:00:09 +0000218 unsigned OpNo = getNumOperands();
219 bool isImpReg = Op.isReg() && Op.isImplicit();
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000220 if (!isImpReg && !isInlineAsm()) {
221 while (OpNo && Operands[OpNo-1].isReg() && Operands[OpNo-1].isImplicit()) {
222 --OpNo;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000223 assert(!Operands[OpNo].isTied() && "Cannot move tied operands");
Chris Lattner961e7422008-01-01 01:12:31 +0000224 }
225 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000226
Pekka Jaaskelaineneb4a6e72013-10-15 14:40:46 +0000227#ifndef NDEBUG
Pekka Jaaskelaineneb08e2e2013-10-15 14:18:10 +0000228 bool isMetaDataOp = Op.getType() == MachineOperand::MO_Metadata;
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000229 // OpNo now points as the desired insertion point. Unless this is a variadic
230 // instruction, only implicit regs are allowed beyond MCID->getNumOperands().
Jakob Stoklund Olesenc300ef02012-07-04 23:53:23 +0000231 // RegMask operands go between the explicit and implicit operands.
232 assert((isImpReg || Op.isRegMask() || MCID->isVariadic() ||
Pekka Jaaskelaineneb08e2e2013-10-15 14:18:10 +0000233 OpNo < MCID->getNumOperands() || isMetaDataOp) &&
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000234 "Trying to add an operand to a machine instr that is already done!");
Pekka Jaaskelaineneb4a6e72013-10-15 14:40:46 +0000235#endif
Chris Lattner961e7422008-01-01 01:12:31 +0000236
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000237 MachineRegisterInfo *MRI = getRegInfo();
Chris Lattner961e7422008-01-01 01:12:31 +0000238
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000239 // Determine if the Operands array needs to be reallocated.
240 // Save the old capacity and operand array.
241 OperandCapacity OldCap = CapOperands;
242 MachineOperand *OldOperands = Operands;
243 if (!OldOperands || OldCap.getSize() == getNumOperands()) {
244 CapOperands = OldOperands ? OldCap.getNext() : OldCap.get(1);
245 Operands = MF.allocateOperandArray(CapOperands);
246 // Move the operands before the insertion point.
247 if (OpNo)
248 moveOperands(Operands, OldOperands, OpNo, MRI);
249 }
Chris Lattner961e7422008-01-01 01:12:31 +0000250
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000251 // Move the operands following the insertion point.
252 if (OpNo != NumOperands)
253 moveOperands(Operands + OpNo + 1, OldOperands + OpNo, NumOperands - OpNo,
254 MRI);
255 ++NumOperands;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000256
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000257 // Deallocate the old operand array.
258 if (OldOperands != Operands && OldOperands)
259 MF.deallocateOperandArray(OldCap, OldOperands);
260
261 // Copy Op into place. It still needs to be inserted into the MRI use lists.
262 MachineOperand *NewMO = new (Operands + OpNo) MachineOperand(Op);
263 NewMO->ParentMI = this;
264
265 // When adding a register operand, tell MRI about it.
266 if (NewMO->isReg()) {
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000267 // Ensure isOnRegUseList() returns false, regardless of Op's status.
Craig Topperc0196b12014-04-14 00:51:57 +0000268 NewMO->Contents.Reg.Prev = nullptr;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000269 // Ignore existing ties. This is not a property that can be copied.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000270 NewMO->TiedTo = 0;
271 // Add the new operand to MRI, but only for instructions in an MBB.
272 if (MRI)
273 MRI->addRegOperandToUseList(NewMO);
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000274 // The MCID operand information isn't accurate until we start adding
275 // explicit operands. The implicit operands are added first, then the
276 // explicits are inserted before them.
277 if (!isImpReg) {
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000278 // Tie uses to defs as indicated in MCInstrDesc.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000279 if (NewMO->isUse()) {
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000280 int DefIdx = MCID->getOperandConstraint(OpNo, MCOI::TIED_TO);
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +0000281 if (DefIdx != -1)
282 tieOperands(DefIdx, OpNo);
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000283 }
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000284 // If the register operand is flagged as early, mark the operand as such.
285 if (MCID->getOperandConstraint(OpNo, MCOI::EARLY_CLOBBER) != -1)
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000286 NewMO->setIsEarlyClobber(true);
Chris Lattner961e7422008-01-01 01:12:31 +0000287 }
Chris Lattner961e7422008-01-01 01:12:31 +0000288 }
289}
290
291/// RemoveOperand - Erase an operand from an instruction, leaving it with one
292/// fewer operand than it started with.
293///
294void MachineInstr::RemoveOperand(unsigned OpNo) {
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +0000295 assert(OpNo < getNumOperands() && "Invalid operand number");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000296 untieRegOperand(OpNo);
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000297
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000298#ifndef NDEBUG
299 // Moving tied operands would break the ties.
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +0000300 for (unsigned i = OpNo + 1, e = getNumOperands(); i != e; ++i)
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000301 if (Operands[i].isReg())
302 assert(!Operands[i].isTied() && "Cannot move tied operands");
303#endif
304
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000305 MachineRegisterInfo *MRI = getRegInfo();
306 if (MRI && Operands[OpNo].isReg())
307 MRI->removeRegOperandFromUseList(Operands + OpNo);
Chris Lattner961e7422008-01-01 01:12:31 +0000308
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000309 // Don't call the MachineOperand destructor. A lot of this code depends on
310 // MachineOperand having a trivial destructor anyway, and adding a call here
311 // wouldn't make it 'destructor-correct'.
312
313 if (unsigned N = NumOperands - 1 - OpNo)
314 moveOperands(Operands + OpNo, Operands + OpNo + 1, N, MRI);
315 --NumOperands;
Chris Lattner961e7422008-01-01 01:12:31 +0000316}
317
Dan Gohman48b185d2009-09-25 20:36:54 +0000318/// addMemOperand - Add a MachineMemOperand to the machine instruction.
319/// This function should be used only occasionally. The setMemRefs function
320/// is the primary method for setting up a MachineInstr's MemRefs list.
Dan Gohman3b460302008-07-07 23:14:23 +0000321void MachineInstr::addMemOperand(MachineFunction &MF,
Dan Gohman48b185d2009-09-25 20:36:54 +0000322 MachineMemOperand *MO) {
323 mmo_iterator OldMemRefs = MemRefs;
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000324 unsigned OldNumMemRefs = NumMemRefs;
Dan Gohman3b460302008-07-07 23:14:23 +0000325
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000326 unsigned NewNum = NumMemRefs + 1;
Dan Gohman48b185d2009-09-25 20:36:54 +0000327 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
Dan Gohman3b460302008-07-07 23:14:23 +0000328
Benjamin Kramerd03878b2012-03-16 16:39:27 +0000329 std::copy(OldMemRefs, OldMemRefs + OldNumMemRefs, NewMemRefs);
Dan Gohman48b185d2009-09-25 20:36:54 +0000330 NewMemRefs[NewNum - 1] = MO;
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000331 setMemRefs(NewMemRefs, NewMemRefs + NewNum);
Dan Gohman48b185d2009-09-25 20:36:54 +0000332}
Chris Lattner961e7422008-01-01 01:12:31 +0000333
Philip Reames5eb90a72016-01-06 19:33:12 +0000334/// Check to see if the MMOs pointed to by the two MemRefs arrays are
Junmo Park820e3922016-02-26 02:07:36 +0000335/// identical.
Philip Reames5eb90a72016-01-06 19:33:12 +0000336static bool hasIdenticalMMOs(const MachineInstr &MI1, const MachineInstr &MI2) {
337 auto I1 = MI1.memoperands_begin(), E1 = MI1.memoperands_end();
338 auto I2 = MI2.memoperands_begin(), E2 = MI2.memoperands_end();
339 if ((E1 - I1) != (E2 - I2))
340 return false;
341 for (; I1 != E1; ++I1, ++I2) {
342 if (**I1 != **I2)
343 return false;
344 }
345 return true;
346}
347
Philip Reamesc86ed002016-01-06 04:39:03 +0000348std::pair<MachineInstr::mmo_iterator, unsigned>
349MachineInstr::mergeMemRefsWith(const MachineInstr& Other) {
Philip Reames5eb90a72016-01-06 19:33:12 +0000350
351 // If either of the incoming memrefs are empty, we must be conservative and
352 // treat this as if we've exhausted our space for memrefs and dropped them.
353 if (memoperands_empty() || Other.memoperands_empty())
354 return std::make_pair(nullptr, 0);
355
356 // If both instructions have identical memrefs, we don't need to merge them.
357 // Since many instructions have a single memref, and we tend to merge things
358 // like pairs of loads from the same location, this catches a large number of
359 // cases in practice.
360 if (hasIdenticalMMOs(*this, Other))
361 return std::make_pair(MemRefs, NumMemRefs);
Junmo Park820e3922016-02-26 02:07:36 +0000362
Philip Reamesc86ed002016-01-06 04:39:03 +0000363 // TODO: consider uniquing elements within the operand lists to reduce
364 // space usage and fall back to conservative information less often.
Philip Reames5eb90a72016-01-06 19:33:12 +0000365 size_t CombinedNumMemRefs = NumMemRefs + Other.NumMemRefs;
366
367 // If we don't have enough room to store this many memrefs, be conservative
368 // and drop them. Otherwise, we'd fail asserts when trying to add them to
369 // the new instruction.
370 if (CombinedNumMemRefs != uint8_t(CombinedNumMemRefs))
371 return std::make_pair(nullptr, 0);
Philip Reamesc86ed002016-01-06 04:39:03 +0000372
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000373 MachineFunction *MF = getMF();
Philip Reamesc86ed002016-01-06 04:39:03 +0000374 mmo_iterator MemBegin = MF->allocateMemRefsArray(CombinedNumMemRefs);
375 mmo_iterator MemEnd = std::copy(memoperands_begin(), memoperands_end(),
376 MemBegin);
377 MemEnd = std::copy(Other.memoperands_begin(), Other.memoperands_end(),
378 MemEnd);
Philip Reames2d2fc4a2016-01-06 05:53:09 +0000379 assert(MemEnd - MemBegin == (ptrdiff_t)CombinedNumMemRefs &&
380 "missing memrefs");
Junmo Park820e3922016-02-26 02:07:36 +0000381
Philip Reamesc86ed002016-01-06 04:39:03 +0000382 return std::make_pair(MemBegin, CombinedNumMemRefs);
383}
384
Michael Bergcafe9472018-06-18 18:37:48 +0000385uint16_t MachineInstr::mergeFlagsWith(const MachineInstr &Other) const {
Francis Visoiu Mistrih084e7d82018-03-14 17:10:58 +0000386 // For now, the just return the union of the flags. If the flags get more
387 // complicated over time, we might need more logic here.
388 return getFlags() | Other.getFlags();
389}
390
Benjamin Kramer97f889f2012-03-17 17:03:45 +0000391bool MachineInstr::hasPropertyInBundle(unsigned Mask, QueryType Type) const {
Jakob Stoklund Olesenf0615c72013-01-10 18:42:44 +0000392 assert(!isBundledWithPred() && "Must be called on bundle header");
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000393 for (MachineBasicBlock::const_instr_iterator MII = getIterator();; ++MII) {
Benjamin Kramer97f889f2012-03-17 17:03:45 +0000394 if (MII->getDesc().getFlags() & Mask) {
Evan Chengcdf89fd2011-12-08 19:23:10 +0000395 if (Type == AnyInBundle)
Evan Cheng7f8e5632011-12-07 07:15:52 +0000396 return true;
397 } else {
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +0000398 if (Type == AllInBundle && !MII->isBundle())
Evan Cheng7f8e5632011-12-07 07:15:52 +0000399 return false;
400 }
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +0000401 // This was the last instruction in the bundle.
402 if (!MII->isBundledWithSucc())
403 return Type == AllInBundle;
Evan Cheng2a81dd42011-12-06 22:12:01 +0000404 }
Evan Cheng2a81dd42011-12-06 22:12:01 +0000405}
406
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000407bool MachineInstr::isIdenticalTo(const MachineInstr &Other,
Evan Chenge9c46c22010-03-03 01:44:33 +0000408 MICheckType Check) const {
Evan Cheng0f260e12010-03-03 21:54:14 +0000409 // If opcodes or number of operands are not the same then the two
410 // instructions are obviously not identical.
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000411 if (Other.getOpcode() != getOpcode() ||
412 Other.getNumOperands() != getNumOperands())
Evan Cheng0f260e12010-03-03 21:54:14 +0000413 return false;
414
Evan Cheng7fae11b2011-12-14 02:11:42 +0000415 if (isBundle()) {
Bjorn Petterssonb29a15e2016-12-19 11:20:57 +0000416 // We have passed the test above that both instructions have the same
417 // opcode, so we know that both instructions are bundles here. Let's compare
418 // MIs inside the bundle.
419 assert(Other.isBundle() && "Expected that both instructions are bundles.");
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000420 MachineBasicBlock::const_instr_iterator I1 = getIterator();
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000421 MachineBasicBlock::const_instr_iterator I2 = Other.getIterator();
Bjorn Petterssonb29a15e2016-12-19 11:20:57 +0000422 // Loop until we analysed the last intruction inside at least one of the
423 // bundles.
424 while (I1->isBundledWithSucc() && I2->isBundledWithSucc()) {
425 ++I1;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000426 ++I2;
Bjorn Petterssonb29a15e2016-12-19 11:20:57 +0000427 if (!I1->isIdenticalTo(*I2, Check))
Evan Cheng7fae11b2011-12-14 02:11:42 +0000428 return false;
429 }
Bjorn Petterssonb29a15e2016-12-19 11:20:57 +0000430 // If we've reached the end of just one of the two bundles, but not both,
431 // the instructions are not identical.
432 if (I1->isBundledWithSucc() || I2->isBundledWithSucc())
433 return false;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000434 }
435
Evan Cheng0f260e12010-03-03 21:54:14 +0000436 // Check operands to make sure they match.
437 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
438 const MachineOperand &MO = getOperand(i);
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000439 const MachineOperand &OMO = Other.getOperand(i);
Evan Chengcfdf3392011-05-12 00:56:58 +0000440 if (!MO.isReg()) {
441 if (!MO.isIdenticalTo(OMO))
442 return false;
443 continue;
444 }
445
Evan Cheng0f260e12010-03-03 21:54:14 +0000446 // Clients may or may not want to ignore defs when testing for equality.
447 // For example, machine CSE pass only cares about finding common
448 // subexpressions, so it's safe to ignore virtual register defs.
Evan Chengcfdf3392011-05-12 00:56:58 +0000449 if (MO.isDef()) {
Evan Cheng0f260e12010-03-03 21:54:14 +0000450 if (Check == IgnoreDefs)
451 continue;
Evan Chengcfdf3392011-05-12 00:56:58 +0000452 else if (Check == IgnoreVRegDefs) {
Diana Picus4a5f5222017-10-12 13:59:51 +0000453 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg()) ||
454 !TargetRegisterInfo::isVirtualRegister(OMO.getReg()))
455 if (!MO.isIdenticalTo(OMO))
Evan Chengcfdf3392011-05-12 00:56:58 +0000456 return false;
457 } else {
458 if (!MO.isIdenticalTo(OMO))
Evan Cheng0f260e12010-03-03 21:54:14 +0000459 return false;
Evan Chengcfdf3392011-05-12 00:56:58 +0000460 if (Check == CheckKillDead && MO.isDead() != OMO.isDead())
461 return false;
462 }
463 } else {
464 if (!MO.isIdenticalTo(OMO))
465 return false;
466 if (Check == CheckKillDead && MO.isKill() != OMO.isKill())
467 return false;
468 }
Evan Cheng0f260e12010-03-03 21:54:14 +0000469 }
Shiva Chen801bf7e2018-05-09 02:42:00 +0000470 // If DebugLoc does not match then two debug instructions are not identical.
471 if (isDebugInstr())
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000472 if (getDebugLoc() && Other.getDebugLoc() &&
473 getDebugLoc() != Other.getDebugLoc())
Devang Patelbf8cc602011-07-07 17:45:33 +0000474 return false;
Evan Cheng0f260e12010-03-03 21:54:14 +0000475 return true;
Evan Chenge9c46c22010-03-03 01:44:33 +0000476}
477
Justin Bognerec7cba52017-10-10 23:34:01 +0000478const MachineFunction *MachineInstr::getMF() const {
479 return getParent()->getParent();
480}
481
Chris Lattnerbec79b42006-04-17 21:35:41 +0000482MachineInstr *MachineInstr::removeFromParent() {
483 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +0000484 return getParent()->remove(this);
Chris Lattnerbec79b42006-04-17 21:35:41 +0000485}
486
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +0000487MachineInstr *MachineInstr::removeFromBundle() {
488 assert(getParent() && "Not embedded in a basic block!");
489 return getParent()->remove_instr(this);
490}
Chris Lattnerbec79b42006-04-17 21:35:41 +0000491
Dan Gohman3b460302008-07-07 23:14:23 +0000492void MachineInstr::eraseFromParent() {
493 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +0000494 getParent()->erase(this);
Dan Gohman3b460302008-07-07 23:14:23 +0000495}
496
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +0000497void MachineInstr::eraseFromParentAndMarkDBGValuesForRemoval() {
498 assert(getParent() && "Not embedded in a basic block!");
499 MachineBasicBlock *MBB = getParent();
500 MachineFunction *MF = MBB->getParent();
501 assert(MF && "Not embedded in a function!");
502
503 MachineInstr *MI = (MachineInstr *)this;
504 MachineRegisterInfo &MRI = MF->getRegInfo();
505
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000506 for (const MachineOperand &MO : MI->operands()) {
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +0000507 if (!MO.isReg() || !MO.isDef())
508 continue;
509 unsigned Reg = MO.getReg();
510 if (!TargetRegisterInfo::isVirtualRegister(Reg))
511 continue;
512 MRI.markUsesInDebugValueAsUndef(Reg);
513 }
514 MI->eraseFromParent();
515}
516
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +0000517void MachineInstr::eraseFromBundle() {
518 assert(getParent() && "Not embedded in a basic block!");
519 getParent()->erase_instr(this);
520}
Dan Gohman3b460302008-07-07 23:14:23 +0000521
Evan Cheng4d728b02007-05-15 01:26:09 +0000522unsigned MachineInstr::getNumExplicitOperands() const {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000523 unsigned NumOperands = MCID->getNumOperands();
524 if (!MCID->isVariadic())
Evan Cheng4d728b02007-05-15 01:26:09 +0000525 return NumOperands;
526
Roman Tereshinb2d3f2e2018-06-12 18:30:37 +0000527 for (unsigned I = NumOperands, E = getNumOperands(); I != E; ++I) {
528 const MachineOperand &MO = getOperand(I);
529 // The operands must always be in the following order:
530 // - explicit reg defs,
531 // - other explicit operands (reg uses, immediates, etc.),
532 // - implicit reg defs
533 // - implicit reg uses
534 if (MO.isReg() && MO.isImplicit())
535 break;
536 ++NumOperands;
Evan Cheng4d728b02007-05-15 01:26:09 +0000537 }
538 return NumOperands;
539}
540
Roman Tereshinb2d3f2e2018-06-12 18:30:37 +0000541unsigned MachineInstr::getNumExplicitDefs() const {
542 unsigned NumDefs = MCID->getNumDefs();
543 if (!MCID->isVariadic())
544 return NumDefs;
545
546 for (unsigned I = NumDefs, E = getNumOperands(); I != E; ++I) {
547 const MachineOperand &MO = getOperand(I);
548 if (!MO.isReg() || !MO.isDef() || MO.isImplicit())
549 break;
550 ++NumDefs;
551 }
552 return NumDefs;
553}
554
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +0000555void MachineInstr::bundleWithPred() {
556 assert(!isBundledWithPred() && "MI is already bundled with its predecessor");
557 setFlag(BundledPred);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000558 MachineBasicBlock::instr_iterator Pred = getIterator();
559 --Pred;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +0000560 assert(!Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +0000561 Pred->setFlag(BundledSucc);
562}
563
564void MachineInstr::bundleWithSucc() {
565 assert(!isBundledWithSucc() && "MI is already bundled with its successor");
566 setFlag(BundledSucc);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000567 MachineBasicBlock::instr_iterator Succ = getIterator();
568 ++Succ;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +0000569 assert(!Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +0000570 Succ->setFlag(BundledPred);
571}
572
573void MachineInstr::unbundleFromPred() {
574 assert(isBundledWithPred() && "MI isn't bundled with its predecessor");
575 clearFlag(BundledPred);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000576 MachineBasicBlock::instr_iterator Pred = getIterator();
577 --Pred;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +0000578 assert(Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +0000579 Pred->clearFlag(BundledSucc);
580}
581
582void MachineInstr::unbundleFromSucc() {
583 assert(isBundledWithSucc() && "MI isn't bundled with its successor");
584 clearFlag(BundledSucc);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000585 MachineBasicBlock::instr_iterator Succ = getIterator();
586 ++Succ;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +0000587 assert(Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +0000588 Succ->clearFlag(BundledPred);
589}
590
Evan Cheng6eb516d2011-01-07 23:50:32 +0000591bool MachineInstr::isStackAligningInlineAsm() const {
592 if (isInlineAsm()) {
593 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
594 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
595 return true;
596 }
597 return false;
598}
Chris Lattner33f5af02006-10-20 22:39:59 +0000599
Chad Rosier994f4042012-09-05 21:00:58 +0000600InlineAsm::AsmDialect MachineInstr::getInlineAsmDialect() const {
601 assert(isInlineAsm() && "getInlineAsmDialect() only works for inline asms!");
602 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
Chad Rosiere53314f2012-09-05 22:40:13 +0000603 return InlineAsm::AsmDialect((ExtraInfo & InlineAsm::Extra_AsmDialect) != 0);
Chad Rosier994f4042012-09-05 21:00:58 +0000604}
605
Jakob Stoklund Olesen1e737162011-10-12 23:37:33 +0000606int MachineInstr::findInlineAsmFlagIdx(unsigned OpIdx,
607 unsigned *GroupNo) const {
608 assert(isInlineAsm() && "Expected an inline asm instruction");
609 assert(OpIdx < getNumOperands() && "OpIdx out of range");
610
611 // Ignore queries about the initial operands.
612 if (OpIdx < InlineAsm::MIOp_FirstOperand)
613 return -1;
614
615 unsigned Group = 0;
616 unsigned NumOps;
617 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
618 i += NumOps) {
619 const MachineOperand &FlagMO = getOperand(i);
620 // If we reach the implicit register operands, stop looking.
621 if (!FlagMO.isImm())
622 return -1;
623 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
624 if (i + NumOps > OpIdx) {
625 if (GroupNo)
626 *GroupNo = Group;
627 return i;
628 }
629 ++Group;
630 }
631 return -1;
632}
633
Shiva Chencd070cd2018-05-09 02:41:08 +0000634const DILabel *MachineInstr::getDebugLabel() const {
635 assert(isDebugLabel() && "not a DBG_LABEL");
636 return cast<DILabel>(getOperand(0).getMetadata());
637}
638
Reid Kleckner28865802016-04-14 18:29:59 +0000639const DILocalVariable *MachineInstr::getDebugVariable() const {
640 assert(isDebugValue() && "not a DBG_VALUE");
641 return cast<DILocalVariable>(getOperand(2).getMetadata());
642}
643
644const DIExpression *MachineInstr::getDebugExpression() const {
645 assert(isDebugValue() && "not a DBG_VALUE");
646 return cast<DIExpression>(getOperand(3).getMetadata());
647}
648
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +0000649const TargetRegisterClass*
650MachineInstr::getRegClassConstraint(unsigned OpIdx,
651 const TargetInstrInfo *TII,
652 const TargetRegisterInfo *TRI) const {
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000653 assert(getParent() && "Can't have an MBB reference here!");
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000654 assert(getMF() && "Can't have an MF reference here!");
655 const MachineFunction &MF = *getMF();
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000656
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +0000657 // Most opcodes have fixed constraints in their MCInstrDesc.
658 if (!isInlineAsm())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000659 return TII->getRegClass(getDesc(), OpIdx, TRI, MF);
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +0000660
661 if (!getOperand(OpIdx).isReg())
Craig Topperc0196b12014-04-14 00:51:57 +0000662 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +0000663
664 // For tied uses on inline asm, get the constraint from the def.
665 unsigned DefIdx;
666 if (getOperand(OpIdx).isUse() && isRegTiedToDefOperand(OpIdx, &DefIdx))
667 OpIdx = DefIdx;
668
669 // Inline asm stores register class constraints in the flag word.
670 int FlagIdx = findInlineAsmFlagIdx(OpIdx);
671 if (FlagIdx < 0)
Craig Topperc0196b12014-04-14 00:51:57 +0000672 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +0000673
674 unsigned Flag = getOperand(FlagIdx).getImm();
675 unsigned RCID;
Simon Dardisd32a2d32016-07-18 13:17:31 +0000676 if ((InlineAsm::getKind(Flag) == InlineAsm::Kind_RegUse ||
677 InlineAsm::getKind(Flag) == InlineAsm::Kind_RegDef ||
678 InlineAsm::getKind(Flag) == InlineAsm::Kind_RegDefEarlyClobber) &&
679 InlineAsm::hasRegClassConstraint(Flag, RCID))
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +0000680 return TRI->getRegClass(RCID);
681
682 // Assume that all registers in a memory operand are pointers.
683 if (InlineAsm::getKind(Flag) == InlineAsm::Kind_Mem)
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000684 return TRI->getPointerRegClass(MF);
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +0000685
Craig Topperc0196b12014-04-14 00:51:57 +0000686 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +0000687}
688
Quentin Colombet1fb3362a2014-01-02 22:47:22 +0000689const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVReg(
690 unsigned Reg, const TargetRegisterClass *CurRC, const TargetInstrInfo *TII,
691 const TargetRegisterInfo *TRI, bool ExploreBundle) const {
692 // Check every operands inside the bundle if we have
693 // been asked to.
694 if (ExploreBundle)
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +0000695 for (ConstMIBundleOperands OpndIt(*this); OpndIt.isValid() && CurRC;
Quentin Colombet1fb3362a2014-01-02 22:47:22 +0000696 ++OpndIt)
697 CurRC = OpndIt->getParent()->getRegClassConstraintEffectForVRegImpl(
698 OpndIt.getOperandNo(), Reg, CurRC, TII, TRI);
699 else
700 // Otherwise, just check the current operands.
Matthias Braune41e1462015-05-29 02:56:46 +0000701 for (unsigned i = 0, e = NumOperands; i < e && CurRC; ++i)
702 CurRC = getRegClassConstraintEffectForVRegImpl(i, Reg, CurRC, TII, TRI);
Quentin Colombet1fb3362a2014-01-02 22:47:22 +0000703 return CurRC;
704}
705
706const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVRegImpl(
707 unsigned OpIdx, unsigned Reg, const TargetRegisterClass *CurRC,
708 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
709 assert(CurRC && "Invalid initial register class");
710 // Check if Reg is constrained by some of its use/def from MI.
711 const MachineOperand &MO = getOperand(OpIdx);
712 if (!MO.isReg() || MO.getReg() != Reg)
713 return CurRC;
714 // If yes, accumulate the constraints through the operand.
715 return getRegClassConstraintEffect(OpIdx, CurRC, TII, TRI);
716}
717
718const TargetRegisterClass *MachineInstr::getRegClassConstraintEffect(
719 unsigned OpIdx, const TargetRegisterClass *CurRC,
720 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
721 const TargetRegisterClass *OpRC = getRegClassConstraint(OpIdx, TII, TRI);
722 const MachineOperand &MO = getOperand(OpIdx);
723 assert(MO.isReg() &&
724 "Cannot get register constraints for non-register operand");
725 assert(CurRC && "Invalid initial register class");
726 if (unsigned SubIdx = MO.getSubReg()) {
727 if (OpRC)
728 CurRC = TRI->getMatchingSuperRegClass(CurRC, OpRC, SubIdx);
729 else
730 CurRC = TRI->getSubClassWithSubReg(CurRC, SubIdx);
731 } else if (OpRC)
732 CurRC = TRI->getCommonSubClass(CurRC, OpRC);
733 return CurRC;
734}
735
Jakob Stoklund Olesen68d752b2013-01-09 18:28:16 +0000736/// Return the number of instructions inside the MI bundle, not counting the
737/// header instruction.
Evan Cheng7fae11b2011-12-14 02:11:42 +0000738unsigned MachineInstr::getBundleSize() const {
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000739 MachineBasicBlock::const_instr_iterator I = getIterator();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000740 unsigned Size = 0;
Richard Trieu7a083812016-02-18 22:09:30 +0000741 while (I->isBundledWithSucc()) {
742 ++Size;
743 ++I;
744 }
Evan Cheng7fae11b2011-12-14 02:11:42 +0000745 return Size;
746}
747
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000748/// Returns true if the MachineInstr has an implicit-use operand of exactly
749/// the given register (not considering sub/super-registers).
750bool MachineInstr::hasRegisterImplicitUseOperand(unsigned Reg) const {
751 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
752 const MachineOperand &MO = getOperand(i);
753 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == Reg)
754 return true;
755 }
756 return false;
757}
758
Evan Cheng910c8082007-04-26 19:00:32 +0000759/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Jim Grosbach9632c142009-09-17 17:57:26 +0000760/// the specific register or -1 if it is not found. It further tightens
Evan Cheng9965aeb2007-02-23 01:04:26 +0000761/// the search criteria to a use that kills the register if isKill is true.
Fraser Cormack48d9fdc2016-10-11 09:09:21 +0000762int MachineInstr::findRegisterUseOperandIdx(
763 unsigned Reg, bool isKill, const TargetRegisterInfo *TRI) const {
Evan Cheng75c21942006-12-06 08:27:42 +0000764 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng5983bdb2007-05-29 18:35:22 +0000765 const MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000766 if (!MO.isReg() || !MO.isUse())
Evan Cheng63254462008-03-05 00:59:57 +0000767 continue;
768 unsigned MOReg = MO.getReg();
769 if (!MOReg)
770 continue;
Fraser Cormack48d9fdc2016-10-11 09:09:21 +0000771 if (MOReg == Reg || (TRI && TargetRegisterInfo::isPhysicalRegister(MOReg) &&
772 TargetRegisterInfo::isPhysicalRegister(Reg) &&
773 TRI->isSubRegister(MOReg, Reg)))
Evan Cheng9965aeb2007-02-23 01:04:26 +0000774 if (!isKill || MO.isKill())
Evan Chengec3ac312007-03-26 22:37:45 +0000775 return i;
Evan Cheng75c21942006-12-06 08:27:42 +0000776 }
Evan Chengec3ac312007-03-26 22:37:45 +0000777 return -1;
Evan Cheng75c21942006-12-06 08:27:42 +0000778}
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +0000779
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +0000780/// readsWritesVirtualRegister - Return a pair of bools (reads, writes)
781/// indicating if this instruction reads or writes Reg. This also considers
782/// partial defines.
783std::pair<bool,bool>
784MachineInstr::readsWritesVirtualRegister(unsigned Reg,
785 SmallVectorImpl<unsigned> *Ops) const {
786 bool PartDef = false; // Partial redefine.
787 bool FullDef = false; // Full define.
788 bool Use = false;
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +0000789
790 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
791 const MachineOperand &MO = getOperand(i);
792 if (!MO.isReg() || MO.getReg() != Reg)
793 continue;
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +0000794 if (Ops)
795 Ops->push_back(i);
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +0000796 if (MO.isUse())
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +0000797 Use |= !MO.isUndef();
Jakob Stoklund Olesen9eb77bf2011-08-19 00:30:17 +0000798 else if (MO.getSubReg() && !MO.isUndef())
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000799 // A partial def undef doesn't count as reading the register.
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +0000800 PartDef = true;
801 else
802 FullDef = true;
803 }
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +0000804 // A partial redefine uses Reg unless there is also a full define.
805 return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef);
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +0000806}
807
Evan Cheng63254462008-03-05 00:59:57 +0000808/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman72a0bc12008-05-06 00:20:10 +0000809/// the specified register or -1 if it is not found. If isDead is true, defs
810/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
811/// also checks if there is a def of a super-register.
Evan Cheng38584512010-05-21 20:53:24 +0000812int
813MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, bool Overlap,
814 const TargetRegisterInfo *TRI) const {
815 bool isPhys = TargetRegisterInfo::isPhysicalRegister(Reg);
Evan Chengf7ed82d2007-02-19 21:49:54 +0000816 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng63254462008-03-05 00:59:57 +0000817 const MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesene7d3f442012-02-14 23:49:37 +0000818 // Accept regmask operands when Overlap is set.
819 // Ignore them when looking for a specific def operand (Overlap == false).
820 if (isPhys && Overlap && MO.isRegMask() && MO.clobbersPhysReg(Reg))
821 return i;
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000822 if (!MO.isReg() || !MO.isDef())
Evan Cheng63254462008-03-05 00:59:57 +0000823 continue;
824 unsigned MOReg = MO.getReg();
Evan Cheng38584512010-05-21 20:53:24 +0000825 bool Found = (MOReg == Reg);
826 if (!Found && TRI && isPhys &&
827 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
828 if (Overlap)
829 Found = TRI->regsOverlap(MOReg, Reg);
830 else
831 Found = TRI->isSubRegister(MOReg, Reg);
832 }
833 if (Found && (!isDead || MO.isDead()))
834 return i;
Evan Chengf7ed82d2007-02-19 21:49:54 +0000835 }
Evan Cheng63254462008-03-05 00:59:57 +0000836 return -1;
Evan Chengf7ed82d2007-02-19 21:49:54 +0000837}
Evan Cheng4d728b02007-05-15 01:26:09 +0000838
Evan Cheng5983bdb2007-05-29 18:35:22 +0000839/// findFirstPredOperandIdx() - Find the index of the first operand in the
840/// operand list that is used to represent the predicate. It returns -1 if
841/// none is found.
842int MachineInstr::findFirstPredOperandIdx() const {
Jim Grosbached16ec42011-08-29 22:24:09 +0000843 // Don't call MCID.findFirstPredOperandIdx() because this variant
844 // is sometimes called on an instruction that's not yet complete, and
845 // so the number of operands is less than the MCID indicates. In
846 // particular, the PTX target does this.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000847 const MCInstrDesc &MCID = getDesc();
848 if (MCID.isPredicable()) {
Evan Cheng4d728b02007-05-15 01:26:09 +0000849 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Evan Cheng6cc775f2011-06-28 19:10:37 +0000850 if (MCID.OpInfo[i].isPredicate())
Evan Cheng5983bdb2007-05-29 18:35:22 +0000851 return i;
Evan Cheng4d728b02007-05-15 01:26:09 +0000852 }
853
Evan Cheng5983bdb2007-05-29 18:35:22 +0000854 return -1;
Evan Cheng4d728b02007-05-15 01:26:09 +0000855}
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000856
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000857// MachineOperand::TiedTo is 4 bits wide.
858const unsigned TiedMax = 15;
859
860/// tieOperands - Mark operands at DefIdx and UseIdx as tied to each other.
861///
862/// Use and def operands can be tied together, indicated by a non-zero TiedTo
863/// field. TiedTo can have these values:
864///
865/// 0: Operand is not tied to anything.
866/// 1 to TiedMax-1: Tied to getOperand(TiedTo-1).
867/// TiedMax: Tied to an operand >= TiedMax-1.
868///
869/// The tied def must be one of the first TiedMax operands on a normal
870/// instruction. INLINEASM instructions allow more tied defs.
871///
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +0000872void MachineInstr::tieOperands(unsigned DefIdx, unsigned UseIdx) {
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +0000873 MachineOperand &DefMO = getOperand(DefIdx);
874 MachineOperand &UseMO = getOperand(UseIdx);
875 assert(DefMO.isDef() && "DefIdx must be a def operand");
876 assert(UseMO.isUse() && "UseIdx must be a use operand");
877 assert(!DefMO.isTied() && "Def is already tied to another use");
878 assert(!UseMO.isTied() && "Use is already tied to another def");
879
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000880 if (DefIdx < TiedMax)
881 UseMO.TiedTo = DefIdx + 1;
882 else {
883 // Inline asm can use the group descriptors to find tied operands, but on
884 // normal instruction, the tied def must be within the first TiedMax
885 // operands.
886 assert(isInlineAsm() && "DefIdx out of range");
887 UseMO.TiedTo = TiedMax;
888 }
889
890 // UseIdx can be out of range, we'll search for it in findTiedOperandIdx().
891 DefMO.TiedTo = std::min(UseIdx + 1, TiedMax);
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +0000892}
893
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000894/// Given the index of a tied register operand, find the operand it is tied to.
895/// Defs are tied to uses and vice versa. Returns the index of the tied operand
896/// which must exist.
897unsigned MachineInstr::findTiedOperandIdx(unsigned OpIdx) const {
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000898 const MachineOperand &MO = getOperand(OpIdx);
899 assert(MO.isTied() && "Operand isn't tied");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000900
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000901 // Normally TiedTo is in range.
902 if (MO.TiedTo < TiedMax)
903 return MO.TiedTo - 1;
904
905 // Uses on normal instructions can be out of range.
906 if (!isInlineAsm()) {
907 // Normal tied defs must be in the 0..TiedMax-1 range.
908 if (MO.isUse())
909 return TiedMax - 1;
910 // MO is a def. Search for the tied use.
911 for (unsigned i = TiedMax - 1, e = getNumOperands(); i != e; ++i) {
912 const MachineOperand &UseMO = getOperand(i);
913 if (UseMO.isReg() && UseMO.isUse() && UseMO.TiedTo == OpIdx + 1)
914 return i;
915 }
916 llvm_unreachable("Can't find tied use");
917 }
918
919 // Now deal with inline asm by parsing the operand group descriptor flags.
920 // Find the beginning of each operand group.
921 SmallVector<unsigned, 8> GroupIdx;
922 unsigned OpIdxGroup = ~0u;
923 unsigned NumOps;
924 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
925 i += NumOps) {
926 const MachineOperand &FlagMO = getOperand(i);
927 assert(FlagMO.isImm() && "Invalid tied operand on inline asm");
928 unsigned CurGroup = GroupIdx.size();
929 GroupIdx.push_back(i);
930 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
931 // OpIdx belongs to this operand group.
932 if (OpIdx > i && OpIdx < i + NumOps)
933 OpIdxGroup = CurGroup;
934 unsigned TiedGroup;
935 if (!InlineAsm::isUseOperandTiedToDef(FlagMO.getImm(), TiedGroup))
936 continue;
937 // Operands in this group are tied to operands in TiedGroup which must be
938 // earlier. Find the number of operands between the two groups.
939 unsigned Delta = i - GroupIdx[TiedGroup];
940
941 // OpIdx is a use tied to TiedGroup.
942 if (OpIdxGroup == CurGroup)
943 return OpIdx - Delta;
944
945 // OpIdx is a def tied to this use group.
946 if (OpIdxGroup == TiedGroup)
947 return OpIdx + Delta;
948 }
949 llvm_unreachable("Invalid tied operand on inline asm");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000950}
951
Dan Gohmanc90f51c2010-05-13 20:34:42 +0000952/// clearKillInfo - Clears kill flags on all operands.
953///
954void MachineInstr::clearKillInfo() {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000955 for (MachineOperand &MO : operands()) {
Dan Gohmanc90f51c2010-05-13 20:34:42 +0000956 if (MO.isReg() && MO.isUse())
957 MO.setIsKill(false);
958 }
959}
960
Geoff Berryd37dc772018-01-29 18:47:48 +0000961void MachineInstr::substituteRegister(unsigned FromReg, unsigned ToReg,
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +0000962 unsigned SubIdx,
Geoff Berryf8bf2ec2018-02-23 18:25:08 +0000963 const TargetRegisterInfo &RegInfo) {
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +0000964 if (TargetRegisterInfo::isPhysicalRegister(ToReg)) {
965 if (SubIdx)
966 ToReg = RegInfo.getSubReg(ToReg, SubIdx);
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000967 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +0000968 if (!MO.isReg() || MO.getReg() != FromReg)
969 continue;
970 MO.substPhysReg(ToReg, RegInfo);
971 }
972 } else {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000973 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +0000974 if (!MO.isReg() || MO.getReg() != FromReg)
975 continue;
976 MO.substVirtReg(ToReg, SubIdx, RegInfo);
977 }
978 }
979}
980
Evan Cheng7d98a482008-07-03 09:09:37 +0000981/// isSafeToMove - Return true if it is safe to move this instruction. If
982/// SawStore is set to true, it means that there is a store (or call) between
983/// the instruction's location and its intended destination.
Matthias Braun07066cc2015-05-19 21:22:20 +0000984bool MachineInstr::isSafeToMove(AliasAnalysis *AA, bool &SawStore) const {
Evan Cheng399e1102008-03-13 00:44:09 +0000985 // Ignore stuff that we obviously can't move.
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +0000986 //
987 // Treat volatile loads as stores. This is not strictly necessary for
Jakob Stoklund Olesend92e2bc2012-09-04 18:44:43 +0000988 // volatiles, but it is required for atomic loads. It is not allowed to move
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +0000989 // a load across an atomic load with Ordering > Monotonic.
Alex Bradburyfa18b9e2017-11-08 20:19:16 +0000990 if (mayStore() || isCall() || isPHI() ||
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +0000991 (mayLoad() && hasOrderedMemoryRef())) {
Evan Cheng399e1102008-03-13 00:44:09 +0000992 SawStore = true;
993 return false;
994 }
Evan Cheng0638c202011-01-07 21:08:26 +0000995
Shiva Chen801bf7e2018-05-09 02:42:00 +0000996 if (isPosition() || isDebugInstr() || isTerminator() ||
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000997 hasUnmodeledSideEffects())
Evan Cheng399e1102008-03-13 00:44:09 +0000998 return false;
999
1000 // See if this instruction does a load. If so, we have to guarantee that the
1001 // loaded value doesn't change between the load and the its intended
1002 // destination. The check for isInvariantLoad gives the targe the chance to
1003 // classify the load as always returning a constant, e.g. a constant pool
1004 // load.
Justin Lebard98cf002016-09-10 01:03:20 +00001005 if (mayLoad() && !isDereferenceableInvariantLoad(AA))
Evan Cheng399e1102008-03-13 00:44:09 +00001006 // Otherwise, this is a real load. If there is a store between the load and
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001007 // end of block, we can't move it.
1008 return !SawStore;
Dan Gohman7c59ed62008-09-24 00:06:15 +00001009
Evan Cheng399e1102008-03-13 00:44:09 +00001010 return true;
1011}
1012
Eli Friedman93f47e52017-03-09 23:33:36 +00001013bool MachineInstr::mayAlias(AliasAnalysis *AA, MachineInstr &Other,
1014 bool UseTBAA) {
Justin Bognerfdf9bf42017-10-10 23:50:49 +00001015 const MachineFunction *MF = getMF();
Eli Friedman93f47e52017-03-09 23:33:36 +00001016 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
Balaram Makam42adadf2017-08-30 14:57:12 +00001017 const MachineFrameInfo &MFI = MF->getFrameInfo();
Eli Friedman93f47e52017-03-09 23:33:36 +00001018
1019 // If neither instruction stores to memory, they can't alias in any
1020 // meaningful way, even if they read from the same address.
1021 if (!mayStore() && !Other.mayStore())
1022 return false;
1023
1024 // Let the target decide if memory accesses cannot possibly overlap.
1025 if (TII->areMemAccessesTriviallyDisjoint(*this, Other, AA))
1026 return false;
1027
Eli Friedman93f47e52017-03-09 23:33:36 +00001028 // FIXME: Need to handle multiple memory operands to support all targets.
1029 if (!hasOneMemOperand() || !Other.hasOneMemOperand())
1030 return true;
1031
1032 MachineMemOperand *MMOa = *memoperands_begin();
1033 MachineMemOperand *MMOb = *Other.memoperands_begin();
1034
Eli Friedman93f47e52017-03-09 23:33:36 +00001035 // The following interface to AA is fashioned after DAGCombiner::isAlias
1036 // and operates with MachineMemOperand offset with some important
1037 // assumptions:
1038 // - LLVM fundamentally assumes flat address spaces.
1039 // - MachineOperand offset can *only* result from legalization and
1040 // cannot affect queries other than the trivial case of overlap
1041 // checking.
1042 // - These offsets never wrap and never step outside
1043 // of allocated objects.
1044 // - There should never be any negative offsets here.
1045 //
1046 // FIXME: Modify API to hide this math from "user"
Balaram Makam42adadf2017-08-30 14:57:12 +00001047 // Even before we go to AA we can reason locally about some
Eli Friedman93f47e52017-03-09 23:33:36 +00001048 // memory objects. It can save compile time, and possibly catch some
1049 // corner cases not currently covered.
1050
Balaram Makam42adadf2017-08-30 14:57:12 +00001051 int64_t OffsetA = MMOa->getOffset();
1052 int64_t OffsetB = MMOb->getOffset();
Eli Friedman93f47e52017-03-09 23:33:36 +00001053
Balaram Makam42adadf2017-08-30 14:57:12 +00001054 int64_t MinOffset = std::min(OffsetA, OffsetB);
1055 int64_t WidthA = MMOa->getSize();
1056 int64_t WidthB = MMOb->getSize();
1057 const Value *ValA = MMOa->getValue();
1058 const Value *ValB = MMOb->getValue();
1059 bool SameVal = (ValA && ValB && (ValA == ValB));
1060 if (!SameVal) {
1061 const PseudoSourceValue *PSVa = MMOa->getPseudoValue();
1062 const PseudoSourceValue *PSVb = MMOb->getPseudoValue();
1063 if (PSVa && ValB && !PSVa->mayAlias(&MFI))
1064 return false;
1065 if (PSVb && ValA && !PSVb->mayAlias(&MFI))
1066 return false;
1067 if (PSVa && PSVb && (PSVa == PSVb))
1068 SameVal = true;
1069 }
Eli Friedman93f47e52017-03-09 23:33:36 +00001070
Balaram Makam42adadf2017-08-30 14:57:12 +00001071 if (SameVal) {
1072 int64_t MaxOffset = std::max(OffsetA, OffsetB);
1073 int64_t LowWidth = (MinOffset == OffsetA) ? WidthA : WidthB;
1074 return (MinOffset + LowWidth > MaxOffset);
1075 }
1076
1077 if (!AA)
1078 return true;
1079
1080 if (!ValA || !ValB)
1081 return true;
1082
1083 assert((OffsetA >= 0) && "Negative MachineMemOperand offset");
1084 assert((OffsetB >= 0) && "Negative MachineMemOperand offset");
1085
1086 int64_t Overlapa = WidthA + OffsetA - MinOffset;
1087 int64_t Overlapb = WidthB + OffsetB - MinOffset;
1088
1089 AliasResult AAResult = AA->alias(
1090 MemoryLocation(ValA, Overlapa,
1091 UseTBAA ? MMOa->getAAInfo() : AAMDNodes()),
1092 MemoryLocation(ValB, Overlapb,
1093 UseTBAA ? MMOb->getAAInfo() : AAMDNodes()));
Eli Friedman93f47e52017-03-09 23:33:36 +00001094
1095 return (AAResult != NoAlias);
1096}
1097
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001098/// hasOrderedMemoryRef - Return true if this instruction may have an ordered
1099/// or volatile memory reference, or if the information describing the memory
1100/// reference is not available. Return false if it is known to have no ordered
1101/// memory references.
1102bool MachineInstr::hasOrderedMemoryRef() const {
Dan Gohman7c59ed62008-09-24 00:06:15 +00001103 // An instruction known never to access memory won't have a volatile access.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001104 if (!mayStore() &&
1105 !mayLoad() &&
1106 !isCall() &&
Evan Cheng6eb516d2011-01-07 23:50:32 +00001107 !hasUnmodeledSideEffects())
Dan Gohman7c59ed62008-09-24 00:06:15 +00001108 return false;
1109
1110 // Otherwise, if the instruction has no memory reference information,
1111 // conservatively assume it wasn't preserved.
1112 if (memoperands_empty())
1113 return true;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001114
Justin Lebardede81e2016-07-13 22:35:19 +00001115 // Check if any of our memory operands are ordered.
Eugene Zelenko4e9736b2017-05-31 01:10:10 +00001116 return llvm::any_of(memoperands(), [](const MachineMemOperand *MMO) {
Justin Lebardede81e2016-07-13 22:35:19 +00001117 return !MMO->isUnordered();
1118 });
Dan Gohman7c59ed62008-09-24 00:06:15 +00001119}
1120
Justin Lebard98cf002016-09-10 01:03:20 +00001121/// isDereferenceableInvariantLoad - Return true if this instruction will never
1122/// trap and is loading from a location whose value is invariant across a run of
1123/// this function.
1124bool MachineInstr::isDereferenceableInvariantLoad(AliasAnalysis *AA) const {
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001125 // If the instruction doesn't load at all, it isn't an invariant load.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001126 if (!mayLoad())
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001127 return false;
1128
1129 // If the instruction has lost its memoperands, conservatively assume that
1130 // it may not be an invariant load.
1131 if (memoperands_empty())
1132 return false;
1133
Matthias Braun941a7052016-07-28 18:40:00 +00001134 const MachineFrameInfo &MFI = getParent()->getParent()->getFrameInfo();
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001135
Justin Lebardede81e2016-07-13 22:35:19 +00001136 for (MachineMemOperand *MMO : memoperands()) {
1137 if (MMO->isVolatile()) return false;
1138 if (MMO->isStore()) return false;
Justin Lebaradbf09e2016-09-11 01:38:58 +00001139 if (MMO->isInvariant() && MMO->isDereferenceable())
1140 continue;
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001141
1142 // A load from a constant PseudoSourceValue is invariant.
Justin Lebardede81e2016-07-13 22:35:19 +00001143 if (const PseudoSourceValue *PSV = MMO->getPseudoValue())
Matthias Braun941a7052016-07-28 18:40:00 +00001144 if (PSV->isConstant(&MFI))
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001145 continue;
1146
Justin Lebardede81e2016-07-13 22:35:19 +00001147 if (const Value *V = MMO->getValue()) {
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001148 // If we have an AliasAnalysis, ask it whether the memory is constant.
Chandler Carruthac80dc72015-06-17 07:18:54 +00001149 if (AA &&
1150 AA->pointsToConstantMemory(
Justin Lebardede81e2016-07-13 22:35:19 +00001151 MemoryLocation(V, MMO->getSize(), MMO->getAAInfo())))
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001152 continue;
1153 }
1154
1155 // Otherwise assume conservatively.
1156 return false;
1157 }
1158
1159 // Everything checks out.
1160 return true;
1161}
1162
Evan Cheng71453822009-12-03 02:31:43 +00001163/// isConstantValuePHI - If the specified instruction is a PHI that always
1164/// merges together the same virtual register, return the register, otherwise
1165/// return 0.
1166unsigned MachineInstr::isConstantValuePHI() const {
Chris Lattnerb06015a2010-02-09 19:54:29 +00001167 if (!isPHI())
Evan Cheng71453822009-12-03 02:31:43 +00001168 return 0;
Evan Cheng5c668a22009-12-07 23:10:34 +00001169 assert(getNumOperands() >= 3 &&
1170 "It's illegal to have a PHI without source operands");
Evan Cheng71453822009-12-03 02:31:43 +00001171
1172 unsigned Reg = getOperand(1).getReg();
1173 for (unsigned i = 3, e = getNumOperands(); i < e; i += 2)
1174 if (getOperand(i).getReg() != Reg)
1175 return 0;
1176 return Reg;
1177}
1178
Evan Cheng6eb516d2011-01-07 23:50:32 +00001179bool MachineInstr::hasUnmodeledSideEffects() const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00001180 if (hasProperty(MCID::UnmodeledSideEffects))
Evan Cheng6eb516d2011-01-07 23:50:32 +00001181 return true;
1182 if (isInlineAsm()) {
1183 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1184 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1185 return true;
1186 }
1187
1188 return false;
1189}
1190
Michael Kupersteinbc7f99a2015-08-12 10:14:58 +00001191bool MachineInstr::isLoadFoldBarrier() const {
1192 return mayStore() || isCall() || hasUnmodeledSideEffects();
1193}
1194
Evan Chengb083c472010-04-08 20:02:37 +00001195/// allDefsAreDead - Return true if all the defs of this instruction are dead.
1196///
1197bool MachineInstr::allDefsAreDead() const {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001198 for (const MachineOperand &MO : operands()) {
Evan Chengb083c472010-04-08 20:02:37 +00001199 if (!MO.isReg() || MO.isUse())
1200 continue;
1201 if (!MO.isDead())
1202 return false;
1203 }
1204 return true;
1205}
1206
Evan Cheng21eedfb2010-10-22 21:49:09 +00001207/// copyImplicitOps - Copy implicit register operands from specified
1208/// instruction to this instruction.
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001209void MachineInstr::copyImplicitOps(MachineFunction &MF,
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001210 const MachineInstr &MI) {
1211 for (unsigned i = MI.getDesc().getNumOperands(), e = MI.getNumOperands();
Evan Cheng21eedfb2010-10-22 21:49:09 +00001212 i != e; ++i) {
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001213 const MachineOperand &MO = MI.getOperand(i);
Lang Hames7c8189c2014-03-17 01:22:54 +00001214 if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask())
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001215 addOperand(MF, MO);
Evan Cheng21eedfb2010-10-22 21:49:09 +00001216 }
1217}
1218
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001219bool MachineInstr::hasComplexRegisterTies() const {
1220 const MCInstrDesc &MCID = getDesc();
1221 for (unsigned I = 0, E = getNumOperands(); I < E; ++I) {
1222 const auto &Operand = getOperand(I);
1223 if (!Operand.isReg() || Operand.isDef())
1224 // Ignore the defined registers as MCID marks only the uses as tied.
1225 continue;
1226 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
1227 int TiedIdx = Operand.isTied() ? int(findTiedOperandIdx(I)) : -1;
1228 if (ExpectedTiedIdx != TiedIdx)
1229 return true;
1230 }
1231 return false;
1232}
1233
1234LLT MachineInstr::getTypeToPrint(unsigned OpIdx, SmallBitVector &PrintedTypes,
1235 const MachineRegisterInfo &MRI) const {
1236 const MachineOperand &Op = getOperand(OpIdx);
1237 if (!Op.isReg())
1238 return LLT{};
1239
1240 if (isVariadic() || OpIdx >= getNumExplicitOperands())
1241 return MRI.getType(Op.getReg());
1242
1243 auto &OpInfo = getDesc().OpInfo[OpIdx];
1244 if (!OpInfo.isGenericType())
1245 return MRI.getType(Op.getReg());
1246
1247 if (PrintedTypes[OpInfo.getGenericTypeIndex()])
1248 return LLT{};
1249
Roman Tereshind29fc892018-05-07 22:31:47 +00001250 LLT TypeToPrint = MRI.getType(Op.getReg());
1251 // Don't mark the type index printed if it wasn't actually printed: maybe
1252 // another operand with the same type index has an actual type attached:
1253 if (TypeToPrint.isValid())
1254 PrintedTypes.set(OpInfo.getGenericTypeIndex());
1255 return TypeToPrint;
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001256}
1257
Aaron Ballman615eb472017-10-15 14:32:27 +00001258#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Brauna4976c62017-01-29 18:20:42 +00001259LLVM_DUMP_METHOD void MachineInstr::dump() const {
Sebastian Pop77794842016-12-21 01:41:12 +00001260 dbgs() << " ";
Matthias Brauna4976c62017-01-29 18:20:42 +00001261 print(dbgs());
Mon P Wangdfcc1ff2008-10-10 01:43:55 +00001262}
Matthias Braun8c209aa2017-01-28 02:02:38 +00001263#endif
Mon P Wangdfcc1ff2008-10-10 01:43:55 +00001264
Francis Visoiu Mistriheb3f76f2018-01-18 18:05:15 +00001265void MachineInstr::print(raw_ostream &OS, bool IsStandalone, bool SkipOpers,
Krzysztof Parzyszek71a4c0c2018-04-10 16:46:13 +00001266 bool SkipDebugLoc, bool AddNewLine,
1267 const TargetInstrInfo *TII) const {
Duncan P. N. Exon Smithc0374522015-06-26 23:18:44 +00001268 const Module *M = nullptr;
Francis Visoiu Mistrih586444e2018-01-18 14:52:14 +00001269 const Function *F = nullptr;
1270 if (const MachineFunction *MF = getMFIfAvailable(*this)) {
1271 F = &MF->getFunction();
1272 M = F->getParent();
Andrew V. Tischenko08389192018-02-26 09:43:21 +00001273 if (!TII)
1274 TII = MF->getSubtarget().getInstrInfo();
Francis Visoiu Mistrih586444e2018-01-18 14:52:14 +00001275 }
Duncan P. N. Exon Smithc0374522015-06-26 23:18:44 +00001276
1277 ModuleSlotTracker MST(M);
Francis Visoiu Mistrih586444e2018-01-18 14:52:14 +00001278 if (F)
1279 MST.incorporateFunction(*F);
Francis Visoiu Mistriheb3f76f2018-01-18 18:05:15 +00001280 print(OS, MST, IsStandalone, SkipOpers, SkipDebugLoc, TII);
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001281}
1282
1283void MachineInstr::print(raw_ostream &OS, ModuleSlotTracker &MST,
Francis Visoiu Mistriheb3f76f2018-01-18 18:05:15 +00001284 bool IsStandalone, bool SkipOpers, bool SkipDebugLoc,
Krzysztof Parzyszek71a4c0c2018-04-10 16:46:13 +00001285 bool AddNewLine, const TargetInstrInfo *TII) const {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001286 // We can be a bit tidier if we know the MachineFunction.
Craig Topperc0196b12014-04-14 00:51:57 +00001287 const MachineFunction *MF = nullptr;
Eric Christopher1cdefae2015-02-27 00:11:34 +00001288 const TargetRegisterInfo *TRI = nullptr;
Craig Topperc0196b12014-04-14 00:51:57 +00001289 const MachineRegisterInfo *MRI = nullptr;
Tim Northover6b3bd612016-07-29 20:32:59 +00001290 const TargetIntrinsicInfo *IntrinsicInfo = nullptr;
Francis Visoiu Mistrih586444e2018-01-18 14:52:14 +00001291 tryToGetTargetInfo(*this, TRI, MRI, IntrinsicInfo, TII);
Tim Northover6b3bd612016-07-29 20:32:59 +00001292
Francis Visoiu Mistrih586444e2018-01-18 14:52:14 +00001293 if (isCFIInstruction())
1294 assert(getNumOperands() == 1 && "Expected 1 operand in CFI instruction");
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001295
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001296 SmallBitVector PrintedTypes(8);
1297 bool ShouldPrintRegisterTies = hasComplexRegisterTies();
1298 auto getTiedOperandIdx = [&](unsigned OpIdx) {
1299 if (!ShouldPrintRegisterTies)
1300 return 0U;
1301 const MachineOperand &MO = getOperand(OpIdx);
1302 if (MO.isReg() && MO.isTied() && !MO.isDef())
1303 return findTiedOperandIdx(OpIdx);
1304 return 0U;
1305 };
Francis Visoiu Mistrih586444e2018-01-18 14:52:14 +00001306 unsigned StartOp = 0;
1307 unsigned e = getNumOperands();
1308
Dan Gohman34341e62009-10-31 20:19:03 +00001309 // Print explicitly defined operands on the left of an assignment syntax.
Francis Visoiu Mistrih586444e2018-01-18 14:52:14 +00001310 while (StartOp < e) {
1311 const MachineOperand &MO = getOperand(StartOp);
1312 if (!MO.isReg() || !MO.isDef() || MO.isImplicit())
1313 break;
1314
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001315 if (StartOp != 0)
1316 OS << ", ";
Francis Visoiu Mistrih586444e2018-01-18 14:52:14 +00001317
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001318 LLT TypeToPrint = MRI ? getTypeToPrint(StartOp, PrintedTypes, *MRI) : LLT{};
1319 unsigned TiedOperandIdx = getTiedOperandIdx(StartOp);
Francis Visoiu Mistriheb3f76f2018-01-18 18:05:15 +00001320 MO.print(OS, MST, TypeToPrint, /*PrintDef=*/false, IsStandalone,
Francis Visoiu Mistrih378b5f32018-01-18 17:59:06 +00001321 ShouldPrintRegisterTies, TiedOperandIdx, TRI, IntrinsicInfo);
Francis Visoiu Mistrih586444e2018-01-18 14:52:14 +00001322 ++StartOp;
Chris Lattnerac6e9742002-10-30 01:55:38 +00001323 }
Tanya Lattner23dbc812004-06-25 00:13:11 +00001324
Dan Gohman34341e62009-10-31 20:19:03 +00001325 if (StartOp != 0)
1326 OS << " = ";
1327
Francis Visoiu Mistrih72cc21e2018-01-09 16:11:51 +00001328 if (getFlag(MachineInstr::FrameSetup))
1329 OS << "frame-setup ";
Francis Visoiu Mistrih3abf05732018-03-13 19:53:16 +00001330 if (getFlag(MachineInstr::FrameDestroy))
Francis Visoiu Mistrih72cc21e2018-01-09 16:11:51 +00001331 OS << "frame-destroy ";
Michael Berg7d1b25d2018-05-03 00:07:56 +00001332 if (getFlag(MachineInstr::FmNoNans))
1333 OS << "nnan ";
1334 if (getFlag(MachineInstr::FmNoInfs))
1335 OS << "ninf ";
1336 if (getFlag(MachineInstr::FmNsz))
1337 OS << "nsz ";
1338 if (getFlag(MachineInstr::FmArcp))
1339 OS << "arcp ";
1340 if (getFlag(MachineInstr::FmContract))
1341 OS << "contract ";
1342 if (getFlag(MachineInstr::FmAfn))
1343 OS << "afn ";
1344 if (getFlag(MachineInstr::FmReassoc))
1345 OS << "reassoc ";
Francis Visoiu Mistrih72cc21e2018-01-09 16:11:51 +00001346
Dan Gohman34341e62009-10-31 20:19:03 +00001347 // Print the opcode name.
Eric Christopher1cdefae2015-02-27 00:11:34 +00001348 if (TII)
1349 OS << TII->getName(getOpcode());
Benjamin Kramerbf152d52012-02-10 13:18:44 +00001350 else
1351 OS << "UNKNOWN";
Misha Brukman835702a2005-04-21 22:36:52 +00001352
Andrew Trickb36388a2013-01-25 07:45:25 +00001353 if (SkipOpers)
1354 return;
1355
Dan Gohman34341e62009-10-31 20:19:03 +00001356 // Print the rest of the operands.
Dan Gohman2745d192009-11-09 19:38:45 +00001357 bool FirstOp = true;
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001358 unsigned AsmDescOp = ~0u;
1359 unsigned AsmOpCount = 0;
Evan Cheng6eb516d2011-01-07 23:50:32 +00001360
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +00001361 if (isInlineAsm() && e >= InlineAsm::MIOp_FirstOperand) {
Evan Cheng6eb516d2011-01-07 23:50:32 +00001362 // Print asm string.
1363 OS << " ";
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001364 const unsigned OpIdx = InlineAsm::MIOp_AsmString;
1365 LLT TypeToPrint = MRI ? getTypeToPrint(OpIdx, PrintedTypes, *MRI) : LLT{};
Francis Visoiu Mistrihe6fc3ce2017-12-07 17:12:30 +00001366 unsigned TiedOperandIdx = getTiedOperandIdx(OpIdx);
Francis Visoiu Mistriheb3f76f2018-01-18 18:05:15 +00001367 getOperand(OpIdx).print(OS, MST, TypeToPrint, /*PrintDef=*/true, IsStandalone,
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001368 ShouldPrintRegisterTies, TiedOperandIdx, TRI,
1369 IntrinsicInfo);
Evan Cheng6eb516d2011-01-07 23:50:32 +00001370
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001371 // Print HasSideEffects, MayLoad, MayStore, IsAlignStack
Evan Cheng6eb516d2011-01-07 23:50:32 +00001372 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1373 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1374 OS << " [sideeffect]";
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001375 if (ExtraInfo & InlineAsm::Extra_MayLoad)
1376 OS << " [mayload]";
1377 if (ExtraInfo & InlineAsm::Extra_MayStore)
1378 OS << " [maystore]";
Wei Ding0526e7f2016-06-22 18:51:08 +00001379 if (ExtraInfo & InlineAsm::Extra_IsConvergent)
1380 OS << " [isconvergent]";
Evan Cheng6eb516d2011-01-07 23:50:32 +00001381 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
1382 OS << " [alignstack]";
Chad Rosiercbd2a192012-09-05 22:17:43 +00001383 if (getInlineAsmDialect() == InlineAsm::AD_ATT)
Chad Rosier994f4042012-09-05 21:00:58 +00001384 OS << " [attdialect]";
Chad Rosiercbd2a192012-09-05 22:17:43 +00001385 if (getInlineAsmDialect() == InlineAsm::AD_Intel)
Chad Rosier994f4042012-09-05 21:00:58 +00001386 OS << " [inteldialect]";
Evan Cheng6eb516d2011-01-07 23:50:32 +00001387
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001388 StartOp = AsmDescOp = InlineAsm::MIOp_FirstOperand;
Evan Cheng6eb516d2011-01-07 23:50:32 +00001389 FirstOp = false;
1390 }
1391
Chris Lattnerac6e9742002-10-30 01:55:38 +00001392 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohman2745d192009-11-09 19:38:45 +00001393 const MachineOperand &MO = getOperand(i);
1394
Dan Gohman2745d192009-11-09 19:38:45 +00001395 if (FirstOp) FirstOp = false; else OS << ",";
Chris Lattnerac6e9742002-10-30 01:55:38 +00001396 OS << " ";
Francis Visoiu Mistrih7d9bef82018-01-09 17:31:07 +00001397
Evan Chengd4d1a512010-04-28 20:03:13 +00001398 if (isDebugValue() && MO.isMetadata()) {
1399 // Pretty print DBG_VALUE instructions.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001400 auto *DIV = dyn_cast<DILocalVariable>(MO.getMetadata());
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +00001401 if (DIV && !DIV->getName().empty())
1402 OS << "!\"" << DIV->getName() << '\"';
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001403 else {
1404 LLT TypeToPrint = MRI ? getTypeToPrint(i, PrintedTypes, *MRI) : LLT{};
Francis Visoiu Mistrihe6fc3ce2017-12-07 17:12:30 +00001405 unsigned TiedOperandIdx = getTiedOperandIdx(i);
Francis Visoiu Mistriheb3f76f2018-01-18 18:05:15 +00001406 MO.print(OS, MST, TypeToPrint, /*PrintDef=*/true, IsStandalone,
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001407 ShouldPrintRegisterTies, TiedOperandIdx, TRI, IntrinsicInfo);
1408 }
Shiva Chencd070cd2018-05-09 02:41:08 +00001409 } else if (isDebugLabel() && MO.isMetadata()) {
1410 // Pretty print DBG_LABEL instructions.
1411 auto *DIL = dyn_cast<DILabel>(MO.getMetadata());
1412 if (DIL && !DIL->getName().empty())
1413 OS << "\"" << DIL->getName() << '\"';
1414 else {
1415 LLT TypeToPrint = MRI ? getTypeToPrint(i, PrintedTypes, *MRI) : LLT{};
1416 unsigned TiedOperandIdx = getTiedOperandIdx(i);
1417 MO.print(OS, MST, TypeToPrint, /*PrintDef=*/true, IsStandalone,
1418 ShouldPrintRegisterTies, TiedOperandIdx, TRI, IntrinsicInfo);
1419 }
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001420 } else if (i == AsmDescOp && MO.isImm()) {
1421 // Pretty print the inline asm operand descriptor.
1422 OS << '$' << AsmOpCount++;
1423 unsigned Flag = MO.getImm();
1424 switch (InlineAsm::getKind(Flag)) {
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001425 case InlineAsm::Kind_RegUse: OS << ":[reguse"; break;
1426 case InlineAsm::Kind_RegDef: OS << ":[regdef"; break;
1427 case InlineAsm::Kind_RegDefEarlyClobber: OS << ":[regdef-ec"; break;
1428 case InlineAsm::Kind_Clobber: OS << ":[clobber"; break;
1429 case InlineAsm::Kind_Imm: OS << ":[imm"; break;
1430 case InlineAsm::Kind_Mem: OS << ":[mem"; break;
1431 default: OS << ":[??" << InlineAsm::getKind(Flag); break;
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001432 }
1433
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001434 unsigned RCID = 0;
Simon Dardisd32a2d32016-07-18 13:17:31 +00001435 if (!InlineAsm::isImmKind(Flag) && !InlineAsm::isMemKind(Flag) &&
1436 InlineAsm::hasRegClassConstraint(Flag, RCID)) {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001437 if (TRI) {
1438 OS << ':' << TRI->getRegClassName(TRI->getRegClass(RCID));
Craig Toppercf0444b2014-11-17 05:50:14 +00001439 } else
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001440 OS << ":RC" << RCID;
Nick Lewycky84882252011-10-13 00:54:59 +00001441 }
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001442
Simon Dardisd32a2d32016-07-18 13:17:31 +00001443 if (InlineAsm::isMemKind(Flag)) {
1444 unsigned MCID = InlineAsm::getMemoryConstraintID(Flag);
1445 switch (MCID) {
1446 case InlineAsm::Constraint_es: OS << ":es"; break;
1447 case InlineAsm::Constraint_i: OS << ":i"; break;
1448 case InlineAsm::Constraint_m: OS << ":m"; break;
1449 case InlineAsm::Constraint_o: OS << ":o"; break;
1450 case InlineAsm::Constraint_v: OS << ":v"; break;
1451 case InlineAsm::Constraint_Q: OS << ":Q"; break;
1452 case InlineAsm::Constraint_R: OS << ":R"; break;
1453 case InlineAsm::Constraint_S: OS << ":S"; break;
1454 case InlineAsm::Constraint_T: OS << ":T"; break;
1455 case InlineAsm::Constraint_Um: OS << ":Um"; break;
1456 case InlineAsm::Constraint_Un: OS << ":Un"; break;
1457 case InlineAsm::Constraint_Uq: OS << ":Uq"; break;
1458 case InlineAsm::Constraint_Us: OS << ":Us"; break;
1459 case InlineAsm::Constraint_Ut: OS << ":Ut"; break;
1460 case InlineAsm::Constraint_Uv: OS << ":Uv"; break;
1461 case InlineAsm::Constraint_Uy: OS << ":Uy"; break;
1462 case InlineAsm::Constraint_X: OS << ":X"; break;
1463 case InlineAsm::Constraint_Z: OS << ":Z"; break;
1464 case InlineAsm::Constraint_ZC: OS << ":ZC"; break;
1465 case InlineAsm::Constraint_Zy: OS << ":Zy"; break;
1466 default: OS << ":?"; break;
1467 }
1468 }
1469
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001470 unsigned TiedTo = 0;
1471 if (InlineAsm::isUseOperandTiedToDef(Flag, TiedTo))
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001472 OS << " tiedto:$" << TiedTo;
1473
1474 OS << ']';
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001475
1476 // Compute the index of the next operand descriptor.
1477 AsmDescOp += 1 + InlineAsm::getNumOperandRegisters(Flag);
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001478 } else {
1479 LLT TypeToPrint = MRI ? getTypeToPrint(i, PrintedTypes, *MRI) : LLT{};
Francis Visoiu Mistrihe6fc3ce2017-12-07 17:12:30 +00001480 unsigned TiedOperandIdx = getTiedOperandIdx(i);
Francis Visoiu Mistrih440f69c2017-12-08 22:53:21 +00001481 if (MO.isImm() && isOperandSubregIdx(i))
Francis Visoiu Mistrihecd0b832018-01-16 10:53:11 +00001482 MachineOperand::printSubRegIdx(OS, MO.getImm(), TRI);
Francis Visoiu Mistrih440f69c2017-12-08 22:53:21 +00001483 else
Francis Visoiu Mistriheb3f76f2018-01-18 18:05:15 +00001484 MO.print(OS, MST, TypeToPrint, /*PrintDef=*/true, IsStandalone,
Francis Visoiu Mistrih440f69c2017-12-08 22:53:21 +00001485 ShouldPrintRegisterTies, TiedOperandIdx, TRI, IntrinsicInfo);
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001486 }
Dan Gohman2745d192009-11-09 19:38:45 +00001487 }
1488
Francis Visoiu Mistrih548add92018-01-19 11:44:42 +00001489 if (!SkipDebugLoc) {
1490 if (const DebugLoc &DL = getDebugLoc()) {
1491 if (!FirstOp)
1492 OS << ',';
1493 OS << " debug-location ";
1494 DL->printAsOperand(OS, MST);
1495 }
1496 }
1497
Dan Gohman3b460302008-07-07 23:14:23 +00001498 if (!memoperands_empty()) {
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001499 SmallVector<StringRef, 0> SSNs;
1500 const LLVMContext *Context = nullptr;
1501 std::unique_ptr<LLVMContext> CtxPtr;
1502 const MachineFrameInfo *MFI = nullptr;
1503 if (const MachineFunction *MF = getMFIfAvailable(*this)) {
1504 MFI = &MF->getFrameInfo();
1505 Context = &MF->getFunction().getContext();
1506 } else {
1507 CtxPtr = llvm::make_unique<LLVMContext>();
1508 Context = CtxPtr.get();
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001509 }
Dan Gohman34341e62009-10-31 20:19:03 +00001510
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001511 OS << " :: ";
1512 bool NeedComma = false;
1513 for (const MachineMemOperand *Op : memoperands()) {
1514 if (NeedComma)
1515 OS << ", ";
1516 Op->print(OS, MST, SSNs, *Context, MFI, TII);
1517 NeedComma = true;
Dan Gohman2d489b52008-02-06 22:27:42 +00001518 }
1519 }
1520
Francis Visoiu Mistrih548add92018-01-19 11:44:42 +00001521 if (SkipDebugLoc)
1522 return;
1523
Francis Visoiu Mistrihe85b06d2018-03-14 21:52:13 +00001524 bool HaveSemi = false;
Francis Visoiu Mistrih8ed0f742018-04-24 11:00:46 +00001525
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001526 // Print debug location information.
Francis Visoiu Mistrih8ed0f742018-04-24 11:00:46 +00001527 if (const DebugLoc &DL = getDebugLoc()) {
1528 if (!HaveSemi) {
1529 OS << ';';
1530 HaveSemi = true;
1531 }
1532 OS << ' ';
1533 DL.print(OS);
1534 }
1535
1536 // Print extra comments for DEBUG_VALUE.
Duncan P. N. Exon Smithc5bd3e02015-04-03 16:23:04 +00001537 if (isDebugValue() && getOperand(e - 2).isMetadata()) {
Francis Visoiu Mistrih8ed0f742018-04-24 11:00:46 +00001538 if (!HaveSemi) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001539 OS << ";";
Francis Visoiu Mistrih8ed0f742018-04-24 11:00:46 +00001540 HaveSemi = true;
1541 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001542 auto *DV = cast<DILocalVariable>(getOperand(e - 2).getMetadata());
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +00001543 OS << " line no:" << DV->getLine();
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001544 if (auto *InlinedAt = debugLoc->getInlinedAt()) {
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +00001545 DebugLoc InlinedAtDL(InlinedAt);
1546 if (InlinedAtDL && MF) {
Devang Pateld61b1d52011-08-04 20:44:26 +00001547 OS << " inlined @[ ";
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00001548 InlinedAtDL.print(OS);
Devang Pateld61b1d52011-08-04 20:44:26 +00001549 OS << " ]";
1550 }
1551 }
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001552 if (isIndirectDebugValue())
1553 OS << " indirect";
Bill Wendling1a0a3d02009-02-19 21:44:55 +00001554 }
Shiva Chen801bf7e2018-05-09 02:42:00 +00001555 // TODO: DBG_LABEL
Francis Visoiu Mistrih68ced402018-02-19 15:08:49 +00001556
Krzysztof Parzyszek71a4c0c2018-04-10 16:46:13 +00001557 if (AddNewLine)
1558 OS << '\n';
Chris Lattner214808f2002-10-30 00:48:05 +00001559}
1560
Owen Anderson2a8a4852008-01-24 01:10:07 +00001561bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001562 const TargetRegisterInfo *RegInfo,
Owen Anderson2a8a4852008-01-24 01:10:07 +00001563 bool AddIfNotFound) {
Evan Cheng6c177732008-04-16 09:41:59 +00001564 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001565 bool hasAliases = isPhysReg &&
1566 MCRegAliasIterator(IncomingReg, RegInfo, false).isValid();
Dan Gohmanc7367b42008-09-03 15:56:16 +00001567 bool Found = false;
Evan Cheng6c177732008-04-16 09:41:59 +00001568 SmallVector<unsigned,4> DeadOps;
Bill Wendling7921ad02008-03-03 22:14:33 +00001569 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1570 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesenf465f062009-08-04 20:09:25 +00001571 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng6c177732008-04-16 09:41:59 +00001572 continue;
Mandeep Singh Grange5a2f112016-05-10 17:57:27 +00001573
1574 // DEBUG_VALUE nodes do not contribute to code generation and should
1575 // always be ignored. Failure to do so may result in trying to modify
1576 // KILL flags on DEBUG_VALUE nodes.
1577 if (MO.isDebug())
1578 continue;
1579
Evan Cheng6c177732008-04-16 09:41:59 +00001580 unsigned Reg = MO.getReg();
1581 if (!Reg)
1582 continue;
Bill Wendling7921ad02008-03-03 22:14:33 +00001583
Evan Cheng6c177732008-04-16 09:41:59 +00001584 if (Reg == IncomingReg) {
Dan Gohmanc7367b42008-09-03 15:56:16 +00001585 if (!Found) {
1586 if (MO.isKill())
1587 // The register is already marked kill.
1588 return true;
Jakob Stoklund Olesenc59cd9b2009-08-02 19:13:03 +00001589 if (isPhysReg && isRegTiedToDefOperand(i))
1590 // Two-address uses of physregs must not be marked kill.
1591 return true;
Dan Gohmanc7367b42008-09-03 15:56:16 +00001592 MO.setIsKill();
1593 Found = true;
1594 }
1595 } else if (hasAliases && MO.isKill() &&
1596 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng6c177732008-04-16 09:41:59 +00001597 // A super-register kill already exists.
1598 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohmanb2612922008-07-03 01:18:51 +00001599 return true;
1600 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng6c177732008-04-16 09:41:59 +00001601 DeadOps.push_back(i);
Bill Wendling7921ad02008-03-03 22:14:33 +00001602 }
1603 }
1604
Evan Cheng6c177732008-04-16 09:41:59 +00001605 // Trim unneeded kill operands.
1606 while (!DeadOps.empty()) {
1607 unsigned OpIdx = DeadOps.back();
1608 if (getOperand(OpIdx).isImplicit())
1609 RemoveOperand(OpIdx);
1610 else
1611 getOperand(OpIdx).setIsKill(false);
1612 DeadOps.pop_back();
1613 }
1614
Bill Wendling7921ad02008-03-03 22:14:33 +00001615 // If not found, this means an alias of one of the operands is killed. Add a
Owen Anderson2a8a4852008-01-24 01:10:07 +00001616 // new implicit operand if required.
Dan Gohmanc7367b42008-09-03 15:56:16 +00001617 if (!Found && AddIfNotFound) {
Bill Wendling7921ad02008-03-03 22:14:33 +00001618 addOperand(MachineOperand::CreateReg(IncomingReg,
1619 false /*IsDef*/,
1620 true /*IsImp*/,
1621 true /*IsKill*/));
Owen Anderson2a8a4852008-01-24 01:10:07 +00001622 return true;
1623 }
Dan Gohmanc7367b42008-09-03 15:56:16 +00001624 return Found;
Owen Anderson2a8a4852008-01-24 01:10:07 +00001625}
1626
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00001627void MachineInstr::clearRegisterKills(unsigned Reg,
1628 const TargetRegisterInfo *RegInfo) {
1629 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topperc0196b12014-04-14 00:51:57 +00001630 RegInfo = nullptr;
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001631 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00001632 if (!MO.isReg() || !MO.isUse() || !MO.isKill())
1633 continue;
1634 unsigned OpReg = MO.getReg();
Matthias Braunaca625a2016-02-24 19:21:48 +00001635 if ((RegInfo && RegInfo->regsOverlap(Reg, OpReg)) || Reg == OpReg)
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00001636 MO.setIsKill(false);
1637 }
1638}
1639
Matthias Braun1965bfa2013-10-10 21:28:38 +00001640bool MachineInstr::addRegisterDead(unsigned Reg,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001641 const TargetRegisterInfo *RegInfo,
Owen Anderson2a8a4852008-01-24 01:10:07 +00001642 bool AddIfNotFound) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00001643 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(Reg);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001644 bool hasAliases = isPhysReg &&
Matthias Braun1965bfa2013-10-10 21:28:38 +00001645 MCRegAliasIterator(Reg, RegInfo, false).isValid();
Dan Gohmanc7367b42008-09-03 15:56:16 +00001646 bool Found = false;
Evan Cheng6c177732008-04-16 09:41:59 +00001647 SmallVector<unsigned,4> DeadOps;
Owen Anderson2a8a4852008-01-24 01:10:07 +00001648 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1649 MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001650 if (!MO.isReg() || !MO.isDef())
Evan Cheng6c177732008-04-16 09:41:59 +00001651 continue;
Matthias Braun1965bfa2013-10-10 21:28:38 +00001652 unsigned MOReg = MO.getReg();
1653 if (!MOReg)
Dan Gohmanc7367b42008-09-03 15:56:16 +00001654 continue;
1655
Matthias Braun1965bfa2013-10-10 21:28:38 +00001656 if (MOReg == Reg) {
Jakob Stoklund Olesen76ad3de2011-04-05 16:53:50 +00001657 MO.setIsDead();
1658 Found = true;
Dan Gohmanc7367b42008-09-03 15:56:16 +00001659 } else if (hasAliases && MO.isDead() &&
Matthias Braun1965bfa2013-10-10 21:28:38 +00001660 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
Evan Cheng6c177732008-04-16 09:41:59 +00001661 // There exists a super-register that's marked dead.
Matthias Braun1965bfa2013-10-10 21:28:38 +00001662 if (RegInfo->isSuperRegister(Reg, MOReg))
Dan Gohmanb2612922008-07-03 01:18:51 +00001663 return true;
Matthias Braun1965bfa2013-10-10 21:28:38 +00001664 if (RegInfo->isSubRegister(Reg, MOReg))
Evan Cheng6c177732008-04-16 09:41:59 +00001665 DeadOps.push_back(i);
Owen Anderson2a8a4852008-01-24 01:10:07 +00001666 }
1667 }
1668
Evan Cheng6c177732008-04-16 09:41:59 +00001669 // Trim unneeded dead operands.
1670 while (!DeadOps.empty()) {
1671 unsigned OpIdx = DeadOps.back();
1672 if (getOperand(OpIdx).isImplicit())
1673 RemoveOperand(OpIdx);
1674 else
1675 getOperand(OpIdx).setIsDead(false);
1676 DeadOps.pop_back();
1677 }
1678
Dan Gohmanc7367b42008-09-03 15:56:16 +00001679 // If not found, this means an alias of one of the operands is dead. Add a
1680 // new implicit operand if required.
Chris Lattnerfd682802009-06-24 17:54:48 +00001681 if (Found || !AddIfNotFound)
1682 return Found;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001683
Matthias Braun1965bfa2013-10-10 21:28:38 +00001684 addOperand(MachineOperand::CreateReg(Reg,
Chris Lattnerfd682802009-06-24 17:54:48 +00001685 true /*IsDef*/,
1686 true /*IsImp*/,
1687 false /*IsKill*/,
1688 true /*IsDead*/));
1689 return true;
Owen Anderson2a8a4852008-01-24 01:10:07 +00001690}
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00001691
Matthias Braun26e7ea62015-02-04 19:35:16 +00001692void MachineInstr::clearRegisterDeads(unsigned Reg) {
1693 for (MachineOperand &MO : operands()) {
1694 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg)
1695 continue;
1696 MO.setIsDead(false);
1697 }
1698}
1699
Matthias Braun2c98d0f2015-11-11 00:41:58 +00001700void MachineInstr::setRegisterDefReadUndef(unsigned Reg, bool IsUndef) {
Matthias Braunc1988f32015-01-21 22:55:13 +00001701 for (MachineOperand &MO : operands()) {
1702 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg || MO.getSubReg() == 0)
1703 continue;
Matthias Braun2c98d0f2015-11-11 00:41:58 +00001704 MO.setIsUndef(IsUndef);
Matthias Braunc1988f32015-01-21 22:55:13 +00001705 }
1706}
1707
Matthias Braun1965bfa2013-10-10 21:28:38 +00001708void MachineInstr::addRegisterDefined(unsigned Reg,
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00001709 const TargetRegisterInfo *RegInfo) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00001710 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1711 MachineOperand *MO = findRegisterDefOperand(Reg, false, RegInfo);
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00001712 if (MO)
1713 return;
1714 } else {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001715 for (const MachineOperand &MO : operands()) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00001716 if (MO.isReg() && MO.getReg() == Reg && MO.isDef() &&
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00001717 MO.getSubReg() == 0)
1718 return;
1719 }
1720 }
Matthias Braun1965bfa2013-10-10 21:28:38 +00001721 addOperand(MachineOperand::CreateReg(Reg,
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00001722 true /*IsDef*/,
1723 true /*IsImp*/));
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00001724}
Evan Cheng59d27fe2010-03-03 23:37:30 +00001725
Jakob Stoklund Olesen4290be42012-02-03 20:43:39 +00001726void MachineInstr::setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs,
Dan Gohman86936502010-06-18 23:28:01 +00001727 const TargetRegisterInfo &TRI) {
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00001728 bool HasRegMask = false;
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001729 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00001730 if (MO.isRegMask()) {
1731 HasRegMask = true;
1732 continue;
1733 }
Dan Gohman86936502010-06-18 23:28:01 +00001734 if (!MO.isReg() || !MO.isDef()) continue;
1735 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenf6507322012-02-03 20:43:35 +00001736 if (!TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Dan Gohman86936502010-06-18 23:28:01 +00001737 // If there are no uses, including partial uses, the def is dead.
Eugene Zelenko4e9736b2017-05-31 01:10:10 +00001738 if (llvm::none_of(UsedRegs,
1739 [&](unsigned Use) { return TRI.regsOverlap(Use, Reg); }))
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001740 MO.setIsDead();
Dan Gohman86936502010-06-18 23:28:01 +00001741 }
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00001742
1743 // This is a call with a register mask operand.
1744 // Mask clobbers are always dead, so add defs for the non-dead defines.
1745 if (HasRegMask)
1746 for (ArrayRef<unsigned>::iterator I = UsedRegs.begin(), E = UsedRegs.end();
1747 I != E; ++I)
1748 addRegisterDefined(*I, &TRI);
Dan Gohman86936502010-06-18 23:28:01 +00001749}
1750
Evan Cheng59d27fe2010-03-03 23:37:30 +00001751unsigned
1752MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) {
Chandler Carruth962152c2012-03-07 09:39:46 +00001753 // Build up a buffer of hash code components.
Chandler Carruth962152c2012-03-07 09:39:46 +00001754 SmallVector<size_t, 8> HashComponents;
1755 HashComponents.reserve(MI->getNumOperands() + 1);
1756 HashComponents.push_back(MI->getOpcode());
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001757 for (const MachineOperand &MO : MI->operands()) {
Chandler Carruth264854f2012-07-05 11:06:22 +00001758 if (MO.isReg() && MO.isDef() &&
1759 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
1760 continue; // Skip virtual register defs.
1761
1762 HashComponents.push_back(hash_value(MO));
Evan Cheng59d27fe2010-03-03 23:37:30 +00001763 }
Chandler Carruth962152c2012-03-07 09:39:46 +00001764 return hash_combine_range(HashComponents.begin(), HashComponents.end());
Evan Cheng59d27fe2010-03-03 23:37:30 +00001765}
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00001766
1767void MachineInstr::emitError(StringRef Msg) const {
1768 // Find the source location cookie.
1769 unsigned LocCookie = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00001770 const MDNode *LocMD = nullptr;
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00001771 for (unsigned i = getNumOperands(); i != 0; --i) {
1772 if (getOperand(i-1).isMetadata() &&
1773 (LocMD = getOperand(i-1).getMetadata()) &&
1774 LocMD->getNumOperands() != 0) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001775 if (const ConstantInt *CI =
1776 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00001777 LocCookie = CI->getZExtValue();
1778 break;
1779 }
1780 }
1781 }
1782
1783 if (const MachineBasicBlock *MBB = getParent())
1784 if (const MachineFunction *MF = MBB->getParent())
1785 return MF->getMMI().getModule()->getContext().emitError(LocCookie, Msg);
1786 report_fatal_error(Msg);
1787}
Reid Kleckner28865802016-04-14 18:29:59 +00001788
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001789MachineInstrBuilder llvm::BuildMI(MachineFunction &MF, const DebugLoc &DL,
Reid Kleckner28865802016-04-14 18:29:59 +00001790 const MCInstrDesc &MCID, bool IsIndirect,
Adrian Prantl8b9bb532017-07-28 23:00:45 +00001791 unsigned Reg, const MDNode *Variable,
1792 const MDNode *Expr) {
Reid Kleckner28865802016-04-14 18:29:59 +00001793 assert(isa<DILocalVariable>(Variable) && "not a variable");
1794 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
1795 assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
1796 "Expected inlined-at fields to agree");
1797 if (IsIndirect)
1798 return BuildMI(MF, DL, MCID)
1799 .addReg(Reg, RegState::Debug)
Adrian Prantl8b9bb532017-07-28 23:00:45 +00001800 .addImm(0U)
Reid Kleckner28865802016-04-14 18:29:59 +00001801 .addMetadata(Variable)
1802 .addMetadata(Expr);
Adrian Prantl8b9bb532017-07-28 23:00:45 +00001803 else
Reid Kleckner28865802016-04-14 18:29:59 +00001804 return BuildMI(MF, DL, MCID)
1805 .addReg(Reg, RegState::Debug)
1806 .addReg(0U, RegState::Debug)
1807 .addMetadata(Variable)
1808 .addMetadata(Expr);
Reid Kleckner28865802016-04-14 18:29:59 +00001809}
1810
1811MachineInstrBuilder llvm::BuildMI(MachineBasicBlock &BB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001812 MachineBasicBlock::iterator I,
1813 const DebugLoc &DL, const MCInstrDesc &MCID,
1814 bool IsIndirect, unsigned Reg,
Adrian Prantl8b9bb532017-07-28 23:00:45 +00001815 const MDNode *Variable, const MDNode *Expr) {
Reid Kleckner28865802016-04-14 18:29:59 +00001816 assert(isa<DILocalVariable>(Variable) && "not a variable");
1817 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
1818 MachineFunction &MF = *BB.getParent();
Adrian Prantl8b9bb532017-07-28 23:00:45 +00001819 MachineInstr *MI = BuildMI(MF, DL, MCID, IsIndirect, Reg, Variable, Expr);
Reid Kleckner28865802016-04-14 18:29:59 +00001820 BB.insert(I, MI);
1821 return MachineInstrBuilder(MF, MI);
1822}
Adrian Prantl6825fb62017-04-18 01:21:53 +00001823
Reid Kleckner9e6c3092017-09-15 21:49:56 +00001824/// Compute the new DIExpression to use with a DBG_VALUE for a spill slot.
1825/// This prepends DW_OP_deref when spilling an indirect DBG_VALUE.
1826static const DIExpression *computeExprForSpill(const MachineInstr &MI) {
1827 assert(MI.getOperand(0).isReg() && "can't spill non-register");
1828 assert(MI.getDebugVariable()->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
1829 "Expected inlined-at fields to agree");
1830
1831 const DIExpression *Expr = MI.getDebugExpression();
1832 if (MI.isIndirectDebugValue()) {
1833 assert(MI.getOperand(1).getImm() == 0 && "DBG_VALUE with nonzero offset");
1834 Expr = DIExpression::prepend(Expr, DIExpression::WithDeref);
1835 }
1836 return Expr;
1837}
1838
Adrian Prantl6825fb62017-04-18 01:21:53 +00001839MachineInstr *llvm::buildDbgValueForSpill(MachineBasicBlock &BB,
1840 MachineBasicBlock::iterator I,
1841 const MachineInstr &Orig,
1842 int FrameIndex) {
Reid Kleckner9e6c3092017-09-15 21:49:56 +00001843 const DIExpression *Expr = computeExprForSpill(Orig);
1844 return BuildMI(BB, I, Orig.getDebugLoc(), Orig.getDesc())
Adrian Prantl6825fb62017-04-18 01:21:53 +00001845 .addFrameIndex(FrameIndex)
Adrian Prantl8b9bb532017-07-28 23:00:45 +00001846 .addImm(0U)
Reid Kleckner9e6c3092017-09-15 21:49:56 +00001847 .addMetadata(Orig.getDebugVariable())
Adrian Prantl6825fb62017-04-18 01:21:53 +00001848 .addMetadata(Expr);
1849}
Reid Kleckner9e6c3092017-09-15 21:49:56 +00001850
1851void llvm::updateDbgValueForSpill(MachineInstr &Orig, int FrameIndex) {
1852 const DIExpression *Expr = computeExprForSpill(Orig);
1853 Orig.getOperand(0).ChangeToFrameIndex(FrameIndex);
1854 Orig.getOperand(1).ChangeToImmediate(0U);
1855 Orig.getOperand(3).setMetadata(Expr);
1856}