blob: 5b1c39eff7393047b2737efb9f56465445cb0673 [file] [log] [blame]
Chris Lattner0cb9dd72008-01-01 20:36:19 +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
Chris Lattner23fcc082001-09-07 17:18:30 +000014#include "llvm/CodeGen/MachineInstr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/FoldingSet.h"
16#include "llvm/ADT/Hashing.h"
17#include "llvm/Analysis/AliasAnalysis.h"
Evan Chenge9c46c22010-03-03 01:44:33 +000018#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner63f41ab2004-02-19 16:17:08 +000019#include "llvm/CodeGen/MachineFunction.h"
Reid Kleckner28865802016-04-14 18:29:59 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman48b185d2009-09-25 20:36:54 +000021#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +000022#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner961e7422008-01-01 01:12:31 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman2d489b52008-02-06 22:27:42 +000024#include "llvm/CodeGen/PseudoSourceValue.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Constants.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000026#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Function.h"
28#include "llvm/IR/InlineAsm.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000029#include "llvm/IR/Intrinsics.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/Metadata.h"
32#include "llvm/IR/Module.h"
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +000033#include "llvm/IR/ModuleSlotTracker.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/Type.h"
35#include "llvm/IR/Value.h"
Evan Cheng6cc775f2011-06-28 19:10:37 +000036#include "llvm/MC/MCInstrDesc.h"
Chris Lattner6c604e32010-03-13 08:14:18 +000037#include "llvm/MC/MCSymbol.h"
Daniel Sanders1e97a0b2015-08-19 12:03:04 +000038#include "llvm/Support/CommandLine.h"
David Greene29388d62010-01-04 23:48:20 +000039#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000040#include "llvm/Support/ErrorHandling.h"
Dan Gohmanaedb4a62008-07-07 20:32:02 +000041#include "llvm/Support/MathExtras.h"
Chris Lattnera078d832008-08-24 20:37:32 +000042#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000043#include "llvm/Target/TargetInstrInfo.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000044#include "llvm/Target/TargetIntrinsicInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000045#include "llvm/Target/TargetMachine.h"
46#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000047#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner43df6c22004-02-23 18:38:20 +000048using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000049
Daniel Sanders1e97a0b2015-08-19 12:03:04 +000050static cl::opt<bool> PrintWholeRegMask(
51 "print-whole-regmask",
52 cl::desc("Print the full contents of regmask operands in IR dumps"),
53 cl::init(true), cl::Hidden);
54
Chris Lattner60055892007-12-30 21:56:09 +000055//===----------------------------------------------------------------------===//
56// MachineOperand Implementation
57//===----------------------------------------------------------------------===//
58
Chris Lattner961e7422008-01-01 01:12:31 +000059void MachineOperand::setReg(unsigned Reg) {
60 if (getReg() == Reg) return; // No change.
Jim Grosbachdee9e8a2011-08-24 16:44:17 +000061
Chris Lattner961e7422008-01-01 01:12:31 +000062 // Otherwise, we have to change the register. If this operand is embedded
63 // into a machine function, we need to update the old and new register's
64 // use/def lists.
65 if (MachineInstr *MI = getParent())
66 if (MachineBasicBlock *MBB = MI->getParent())
67 if (MachineFunction *MF = MBB->getParent()) {
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +000068 MachineRegisterInfo &MRI = MF->getRegInfo();
69 MRI.removeRegOperandFromUseList(this);
Jakob Stoklund Olesena4941692010-10-19 20:56:32 +000070 SmallContents.RegNo = Reg;
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +000071 MRI.addRegOperandToUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +000072 return;
73 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +000074
Chris Lattner961e7422008-01-01 01:12:31 +000075 // Otherwise, just change the register, no problem. :)
Jakob Stoklund Olesena4941692010-10-19 20:56:32 +000076 SmallContents.RegNo = Reg;
Chris Lattner961e7422008-01-01 01:12:31 +000077}
78
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +000079void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx,
80 const TargetRegisterInfo &TRI) {
81 assert(TargetRegisterInfo::isVirtualRegister(Reg));
82 if (SubIdx && getSubReg())
83 SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg());
84 setReg(Reg);
Jakob Stoklund Olesen7b0ac862010-06-01 22:39:25 +000085 if (SubIdx)
86 setSubReg(SubIdx);
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +000087}
88
89void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) {
90 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
91 if (getSubReg()) {
92 Reg = TRI.getSubReg(Reg, getSubReg());
Jakob Stoklund Olesen89bd2ae2011-05-08 19:21:08 +000093 // Note that getSubReg() may return 0 if the sub-register doesn't exist.
94 // That won't happen in legal code.
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +000095 setSubReg(0);
Krzysztof Parzyszek673b3472016-08-22 14:50:12 +000096 if (isDef())
97 setIsUndef(false);
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +000098 }
99 setReg(Reg);
100}
101
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000102/// Change a def to a use, or a use to a def.
103void MachineOperand::setIsDef(bool Val) {
104 assert(isReg() && "Wrong MachineOperand accessor");
105 assert((!Val || !isDebug()) && "Marking a debug operation as def");
106 if (IsDef == Val)
107 return;
108 // MRI may keep uses and defs in different list positions.
109 if (MachineInstr *MI = getParent())
110 if (MachineBasicBlock *MBB = MI->getParent())
111 if (MachineFunction *MF = MBB->getParent()) {
112 MachineRegisterInfo &MRI = MF->getRegInfo();
113 MRI.removeRegOperandFromUseList(this);
114 IsDef = Val;
115 MRI.addRegOperandToUseList(this);
116 return;
117 }
118 IsDef = Val;
119}
120
Matt Arsenault93ffe582014-09-28 19:24:59 +0000121// If this operand is currently a register operand, and if this is in a
122// function, deregister the operand from the register's use/def list.
123void MachineOperand::removeRegFromUses() {
124 if (!isReg() || !isOnRegUseList())
125 return;
126
127 if (MachineInstr *MI = getParent()) {
128 if (MachineBasicBlock *MBB = MI->getParent()) {
129 if (MachineFunction *MF = MBB->getParent())
130 MF->getRegInfo().removeRegOperandFromUseList(this);
131 }
132 }
133}
134
Chris Lattner961e7422008-01-01 01:12:31 +0000135/// ChangeToImmediate - Replace this operand with a new immediate operand of
136/// the specified value. If an operand is known to be an immediate already,
137/// the setImm method should be used.
138void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000139 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
Matt Arsenault93ffe582014-09-28 19:24:59 +0000140
141 removeRegFromUses();
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000142
Chris Lattner961e7422008-01-01 01:12:31 +0000143 OpKind = MO_Immediate;
144 Contents.ImmVal = ImmVal;
145}
146
Matt Arsenault93ffe582014-09-28 19:24:59 +0000147void MachineOperand::ChangeToFPImmediate(const ConstantFP *FPImm) {
148 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
149
150 removeRegFromUses();
151
152 OpKind = MO_FPImmediate;
153 Contents.CFP = FPImm;
154}
155
Matt Arsenault633dba42015-05-06 17:05:54 +0000156void MachineOperand::ChangeToES(const char *SymName, unsigned char TargetFlags) {
157 assert((!isReg() || !isTied()) &&
158 "Cannot change a tied operand into an external symbol");
159
160 removeRegFromUses();
161
162 OpKind = MO_ExternalSymbol;
163 Contents.OffsetedInfo.Val.SymbolName = SymName;
164 setOffset(0); // Offset is always 0.
165 setTargetFlags(TargetFlags);
166}
167
168void MachineOperand::ChangeToMCSymbol(MCSymbol *Sym) {
169 assert((!isReg() || !isTied()) &&
170 "Cannot change a tied operand into an MCSymbol");
171
172 removeRegFromUses();
173
174 OpKind = MO_MCSymbol;
175 Contents.Sym = Sym;
176}
177
Chris Lattner961e7422008-01-01 01:12:31 +0000178/// ChangeToRegister - Replace this operand with a new register operand of
179/// the specified value. If an operand is known to be an register already,
180/// the setReg method should be used.
181void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
Dale Johannesend40d42c2010-02-10 00:41:49 +0000182 bool isKill, bool isDead, bool isUndef,
183 bool isDebug) {
Craig Topperc0196b12014-04-14 00:51:57 +0000184 MachineRegisterInfo *RegInfo = nullptr;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000185 if (MachineInstr *MI = getParent())
186 if (MachineBasicBlock *MBB = MI->getParent())
187 if (MachineFunction *MF = MBB->getParent())
188 RegInfo = &MF->getRegInfo();
189 // If this operand is already a register operand, remove it from the
Chris Lattner961e7422008-01-01 01:12:31 +0000190 // register's use/def lists.
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000191 bool WasReg = isReg();
192 if (RegInfo && WasReg)
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000193 RegInfo->removeRegOperandFromUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +0000194
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000195 // Change this to a register and set the reg#.
196 OpKind = MO_Register;
197 SmallContents.RegNo = Reg;
Jakob Stoklund Olesena1b246d2013-01-07 23:21:44 +0000198 SubReg_TargetFlags = 0;
Chris Lattner961e7422008-01-01 01:12:31 +0000199 IsDef = isDef;
200 IsImp = isImp;
201 IsKill = isKill;
202 IsDead = isDead;
Evan Cheng0dc101b2009-06-30 08:49:04 +0000203 IsUndef = isUndef;
Jakob Stoklund Olesenb0d91ab2011-12-07 00:22:07 +0000204 IsInternalRead = false;
Dale Johannesenc0d712d2008-09-14 01:44:36 +0000205 IsEarlyClobber = false;
Dale Johannesend40d42c2010-02-10 00:41:49 +0000206 IsDebug = isDebug;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000207 // Ensure isOnRegUseList() returns false.
Craig Topperc0196b12014-04-14 00:51:57 +0000208 Contents.Reg.Prev = nullptr;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000209 // Preserve the tie when the operand was already a register.
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000210 if (!WasReg)
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000211 TiedTo = 0;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000212
213 // If this operand is embedded in a function, add the operand to the
214 // register's use/def list.
215 if (RegInfo)
216 RegInfo->addRegOperandToUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +0000217}
218
Chris Lattner60055892007-12-30 21:56:09 +0000219/// isIdenticalTo - Return true if this operand is identical to the specified
Chandler Carruth264854f2012-07-05 11:06:22 +0000220/// operand. Note that this should stay in sync with the hash_value overload
221/// below.
Chris Lattner60055892007-12-30 21:56:09 +0000222bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
Chris Lattnerfd682802009-06-24 17:54:48 +0000223 if (getType() != Other.getType() ||
224 getTargetFlags() != Other.getTargetFlags())
225 return false;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000226
Chris Lattner60055892007-12-30 21:56:09 +0000227 switch (getType()) {
Chris Lattner60055892007-12-30 21:56:09 +0000228 case MachineOperand::MO_Register:
229 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
230 getSubReg() == Other.getSubReg();
231 case MachineOperand::MO_Immediate:
232 return getImm() == Other.getImm();
Cameron Zwarich7da0f9a2011-07-01 23:45:21 +0000233 case MachineOperand::MO_CImmediate:
234 return getCImm() == Other.getCImm();
Nate Begeman26b76b62008-02-14 07:39:30 +0000235 case MachineOperand::MO_FPImmediate:
236 return getFPImm() == Other.getFPImm();
Chris Lattner60055892007-12-30 21:56:09 +0000237 case MachineOperand::MO_MachineBasicBlock:
238 return getMBB() == Other.getMBB();
239 case MachineOperand::MO_FrameIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000240 return getIndex() == Other.getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000241 case MachineOperand::MO_ConstantPoolIndex:
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000242 case MachineOperand::MO_TargetIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000243 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattner60055892007-12-30 21:56:09 +0000244 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000245 return getIndex() == Other.getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000246 case MachineOperand::MO_GlobalAddress:
247 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
248 case MachineOperand::MO_ExternalSymbol:
249 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
250 getOffset() == Other.getOffset();
Dan Gohman6c938802009-10-30 01:27:03 +0000251 case MachineOperand::MO_BlockAddress:
Michael Liaoabb87d42012-09-12 21:43:09 +0000252 return getBlockAddress() == Other.getBlockAddress() &&
253 getOffset() == Other.getOffset();
Juergen Ributzkae8294752013-12-14 06:53:06 +0000254 case MachineOperand::MO_RegisterMask:
255 case MachineOperand::MO_RegisterLiveOut:
Jakob Stoklund Olesen374ed322012-01-16 19:22:00 +0000256 return getRegMask() == Other.getRegMask();
Chris Lattner6c604e32010-03-13 08:14:18 +0000257 case MachineOperand::MO_MCSymbol:
258 return getMCSymbol() == Other.getMCSymbol();
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000259 case MachineOperand::MO_CFIIndex:
260 return getCFIIndex() == Other.getCFIIndex();
Chris Lattnerf839ee02010-04-07 18:03:19 +0000261 case MachineOperand::MO_Metadata:
262 return getMetadata() == Other.getMetadata();
Tim Northover6b3bd612016-07-29 20:32:59 +0000263 case MachineOperand::MO_IntrinsicID:
264 return getIntrinsicID() == Other.getIntrinsicID();
Tim Northoverde3aea0412016-08-17 20:25:25 +0000265 case MachineOperand::MO_Predicate:
266 return getPredicate() == Other.getPredicate();
Chris Lattner60055892007-12-30 21:56:09 +0000267 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000268 llvm_unreachable("Invalid machine operand type");
Chris Lattner60055892007-12-30 21:56:09 +0000269}
270
Chandler Carruth264854f2012-07-05 11:06:22 +0000271// Note: this must stay exactly in sync with isIdenticalTo above.
272hash_code llvm::hash_value(const MachineOperand &MO) {
273 switch (MO.getType()) {
274 case MachineOperand::MO_Register:
Jakob Stoklund Olesendba99d02012-08-28 18:05:48 +0000275 // Register operands don't have target flags.
276 return hash_combine(MO.getType(), MO.getReg(), MO.getSubReg(), MO.isDef());
Chandler Carruth264854f2012-07-05 11:06:22 +0000277 case MachineOperand::MO_Immediate:
278 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm());
279 case MachineOperand::MO_CImmediate:
280 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm());
281 case MachineOperand::MO_FPImmediate:
282 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm());
283 case MachineOperand::MO_MachineBasicBlock:
284 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB());
285 case MachineOperand::MO_FrameIndex:
286 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
287 case MachineOperand::MO_ConstantPoolIndex:
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000288 case MachineOperand::MO_TargetIndex:
Chandler Carruth264854f2012-07-05 11:06:22 +0000289 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(),
290 MO.getOffset());
291 case MachineOperand::MO_JumpTableIndex:
292 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
293 case MachineOperand::MO_ExternalSymbol:
294 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(),
295 MO.getSymbolName());
296 case MachineOperand::MO_GlobalAddress:
297 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(),
298 MO.getOffset());
299 case MachineOperand::MO_BlockAddress:
300 return hash_combine(MO.getType(), MO.getTargetFlags(),
Michael Liaoabb87d42012-09-12 21:43:09 +0000301 MO.getBlockAddress(), MO.getOffset());
Chandler Carruth264854f2012-07-05 11:06:22 +0000302 case MachineOperand::MO_RegisterMask:
Juergen Ributzkae8294752013-12-14 06:53:06 +0000303 case MachineOperand::MO_RegisterLiveOut:
Chandler Carruth264854f2012-07-05 11:06:22 +0000304 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask());
305 case MachineOperand::MO_Metadata:
306 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata());
307 case MachineOperand::MO_MCSymbol:
308 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol());
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000309 case MachineOperand::MO_CFIIndex:
310 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex());
Tim Northover6b3bd612016-07-29 20:32:59 +0000311 case MachineOperand::MO_IntrinsicID:
312 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIntrinsicID());
Tim Northoverde3aea0412016-08-17 20:25:25 +0000313 case MachineOperand::MO_Predicate:
314 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getPredicate());
Chandler Carruth264854f2012-07-05 11:06:22 +0000315 }
316 llvm_unreachable("Invalid machine operand type");
317}
318
Tim Northover6b3bd612016-07-29 20:32:59 +0000319void MachineOperand::print(raw_ostream &OS, const TargetRegisterInfo *TRI,
320 const TargetIntrinsicInfo *IntrinsicInfo) const {
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000321 ModuleSlotTracker DummyMST(nullptr);
Tim Northover6b3bd612016-07-29 20:32:59 +0000322 print(OS, DummyMST, TRI, IntrinsicInfo);
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000323}
324
325void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
Tim Northover6b3bd612016-07-29 20:32:59 +0000326 const TargetRegisterInfo *TRI,
327 const TargetIntrinsicInfo *IntrinsicInfo) const {
Chris Lattner60055892007-12-30 21:56:09 +0000328 switch (getType()) {
329 case MachineOperand::MO_Register:
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000330 OS << PrintReg(getReg(), TRI, getSubReg());
Dan Gohman0ab11442008-12-18 21:51:27 +0000331
Evan Cheng0dc101b2009-06-30 08:49:04 +0000332 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000333 isInternalRead() || isEarlyClobber() || isTied()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000334 OS << '<';
Chris Lattner60055892007-12-30 21:56:09 +0000335 bool NeedComma = false;
Evan Cheng70b1fa52009-10-14 23:37:31 +0000336 if (isDef()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000337 if (NeedComma) OS << ',';
Dale Johannesen1f3ab862008-09-12 17:49:03 +0000338 if (isEarlyClobber())
339 OS << "earlyclobber,";
Evan Cheng70b1fa52009-10-14 23:37:31 +0000340 if (isImplicit())
341 OS << "imp-";
Chris Lattner60055892007-12-30 21:56:09 +0000342 OS << "def";
343 NeedComma = true;
Jakob Stoklund Olesen7111a632012-04-20 21:45:33 +0000344 // <def,read-undef> only makes sense when getSubReg() is set.
345 // Don't clutter the output otherwise.
346 if (isUndef() && getSubReg())
347 OS << ",read-undef";
Evan Chengf781bd82009-10-21 07:56:02 +0000348 } else if (isImplicit()) {
Craig Topper9a9d58a2015-05-16 05:42:08 +0000349 OS << "imp-use";
350 NeedComma = true;
Evan Chengf781bd82009-10-21 07:56:02 +0000351 }
Evan Cheng70b1fa52009-10-14 23:37:31 +0000352
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000353 if (isKill()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000354 if (NeedComma) OS << ',';
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000355 OS << "kill";
356 NeedComma = true;
357 }
358 if (isDead()) {
359 if (NeedComma) OS << ',';
360 OS << "dead";
361 NeedComma = true;
362 }
363 if (isUndef() && isUse()) {
364 if (NeedComma) OS << ',';
365 OS << "undef";
366 NeedComma = true;
367 }
368 if (isInternalRead()) {
369 if (NeedComma) OS << ',';
370 OS << "internal";
371 NeedComma = true;
372 }
373 if (isTied()) {
374 if (NeedComma) OS << ',';
375 OS << "tied";
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000376 if (TiedTo != 15)
377 OS << unsigned(TiedTo - 1);
Chris Lattner60055892007-12-30 21:56:09 +0000378 }
Chris Lattnerfd682802009-06-24 17:54:48 +0000379 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000380 }
381 break;
382 case MachineOperand::MO_Immediate:
383 OS << getImm();
384 break;
Devang Patelf071d722011-06-24 20:46:11 +0000385 case MachineOperand::MO_CImmediate:
386 getCImm()->getValue().print(OS, false);
387 break;
Nate Begeman26b76b62008-02-14 07:39:30 +0000388 case MachineOperand::MO_FPImmediate:
Matt Arsenault59239732016-02-05 00:50:18 +0000389 if (getFPImm()->getType()->isFloatTy()) {
Nate Begeman26b76b62008-02-14 07:39:30 +0000390 OS << getFPImm()->getValueAPF().convertToFloat();
Matt Arsenault59239732016-02-05 00:50:18 +0000391 } else if (getFPImm()->getType()->isHalfTy()) {
392 APFloat APF = getFPImm()->getValueAPF();
393 bool Unused;
394 APF.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &Unused);
395 OS << "half " << APF.convertToFloat();
396 } else {
Nate Begeman26b76b62008-02-14 07:39:30 +0000397 OS << getFPImm()->getValueAPF().convertToDouble();
Matt Arsenault59239732016-02-05 00:50:18 +0000398 }
Nate Begeman26b76b62008-02-14 07:39:30 +0000399 break;
Chris Lattner60055892007-12-30 21:56:09 +0000400 case MachineOperand::MO_MachineBasicBlock:
Dan Gohman34341e62009-10-31 20:19:03 +0000401 OS << "<BB#" << getMBB()->getNumber() << ">";
Chris Lattner60055892007-12-30 21:56:09 +0000402 break;
403 case MachineOperand::MO_FrameIndex:
Chris Lattnerfd682802009-06-24 17:54:48 +0000404 OS << "<fi#" << getIndex() << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000405 break;
406 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000407 OS << "<cp#" << getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000408 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000409 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000410 break;
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000411 case MachineOperand::MO_TargetIndex:
412 OS << "<ti#" << getIndex();
413 if (getOffset()) OS << "+" << getOffset();
414 OS << '>';
415 break;
Chris Lattner60055892007-12-30 21:56:09 +0000416 case MachineOperand::MO_JumpTableIndex:
Chris Lattnerfd682802009-06-24 17:54:48 +0000417 OS << "<jt#" << getIndex() << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000418 break;
419 case MachineOperand::MO_GlobalAddress:
Dan Gohman0080ee22009-11-06 18:03:10 +0000420 OS << "<ga:";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000421 getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Chris Lattner60055892007-12-30 21:56:09 +0000422 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000423 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000424 break;
425 case MachineOperand::MO_ExternalSymbol:
426 OS << "<es:" << getSymbolName();
427 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000428 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000429 break;
Dan Gohman6c938802009-10-30 01:27:03 +0000430 case MachineOperand::MO_BlockAddress:
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000431 OS << '<';
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000432 getBlockAddress()->printAsOperand(OS, /*PrintType=*/false, MST);
Michael Liaoabb87d42012-09-12 21:43:09 +0000433 if (getOffset()) OS << "+" << getOffset();
Dan Gohman6c938802009-10-30 01:27:03 +0000434 OS << '>';
435 break;
Daniel Sanders1e97a0b2015-08-19 12:03:04 +0000436 case MachineOperand::MO_RegisterMask: {
437 unsigned NumRegsInMask = 0;
438 unsigned NumRegsEmitted = 0;
439 OS << "<regmask";
440 for (unsigned i = 0; i < TRI->getNumRegs(); ++i) {
441 unsigned MaskWord = i / 32;
442 unsigned MaskBit = i % 32;
443 if (getRegMask()[MaskWord] & (1 << MaskBit)) {
444 if (PrintWholeRegMask || NumRegsEmitted <= 10) {
445 OS << " " << PrintReg(i, TRI);
446 NumRegsEmitted++;
447 }
448 NumRegsInMask++;
449 }
450 }
451 if (NumRegsEmitted != NumRegsInMask)
452 OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more...";
453 OS << ">";
Jakob Stoklund Olesen374ed322012-01-16 19:22:00 +0000454 break;
Daniel Sanders1e97a0b2015-08-19 12:03:04 +0000455 }
Juergen Ributzkae8294752013-12-14 06:53:06 +0000456 case MachineOperand::MO_RegisterLiveOut:
457 OS << "<regliveout>";
458 break;
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000459 case MachineOperand::MO_Metadata:
460 OS << '<';
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000461 getMetadata()->printAsOperand(OS, MST);
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000462 OS << '>';
463 break;
Chris Lattner6c604e32010-03-13 08:14:18 +0000464 case MachineOperand::MO_MCSymbol:
465 OS << "<MCSym=" << *getMCSymbol() << '>';
466 break;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000467 case MachineOperand::MO_CFIIndex:
468 OS << "<call frame instruction>";
469 break;
Tim Northover6b3bd612016-07-29 20:32:59 +0000470 case MachineOperand::MO_IntrinsicID: {
471 Intrinsic::ID ID = getIntrinsicID();
472 if (ID < Intrinsic::num_intrinsics)
Pete Cooper036b94d2016-08-23 16:23:45 +0000473 OS << "<intrinsic:@" << Intrinsic::getName(ID, None) << ')';
Tim Northover6b3bd612016-07-29 20:32:59 +0000474 else if (IntrinsicInfo)
475 OS << "<intrinsic:@" << IntrinsicInfo->getName(ID) << ')';
476 else
477 OS << "<intrinsic:" << ID << '>';
478 break;
479 }
Tim Northoverde3aea0412016-08-17 20:25:25 +0000480 case MachineOperand::MO_Predicate: {
481 auto Pred = static_cast<CmpInst::Predicate>(getPredicate());
482 OS << '<' << (CmpInst::isIntPredicate(Pred) ? "intpred" : "floatpred")
483 << CmpInst::getPredicateName(Pred) << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000484 }
Tim Northoverde3aea0412016-08-17 20:25:25 +0000485 }
Chris Lattnerfd682802009-06-24 17:54:48 +0000486 if (unsigned TF = getTargetFlags())
487 OS << "[TF=" << TF << ']';
Chris Lattner60055892007-12-30 21:56:09 +0000488}
489
490//===----------------------------------------------------------------------===//
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000491// MachineMemOperand Implementation
492//===----------------------------------------------------------------------===//
493
Chris Lattnerde93bb02010-09-21 05:39:30 +0000494/// getAddrSpace - Return the LLVM IR address space number that this pointer
495/// points into.
496unsigned MachinePointerInfo::getAddrSpace() const {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000497 if (V.isNull() || V.is<const PseudoSourceValue*>()) return 0;
498 return cast<PointerType>(V.get<const Value*>()->getType())->getAddressSpace();
Chris Lattnerde93bb02010-09-21 05:39:30 +0000499}
500
Chris Lattner82fd06d2010-09-21 06:22:23 +0000501/// getConstantPool - Return a MachinePointerInfo record that refers to the
502/// constant pool.
Alex Lorenze40c8a22015-08-11 23:09:45 +0000503MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) {
504 return MachinePointerInfo(MF.getPSVManager().getConstantPool());
Chris Lattner82fd06d2010-09-21 06:22:23 +0000505}
506
507/// getFixedStack - Return a MachinePointerInfo record that refers to the
508/// the specified FrameIndex.
Alex Lorenze40c8a22015-08-11 23:09:45 +0000509MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF,
510 int FI, int64_t Offset) {
511 return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset);
Chris Lattner82fd06d2010-09-21 06:22:23 +0000512}
513
Alex Lorenze40c8a22015-08-11 23:09:45 +0000514MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) {
515 return MachinePointerInfo(MF.getPSVManager().getJumpTable());
Chris Lattner50287ea2010-09-21 06:43:24 +0000516}
517
Alex Lorenze40c8a22015-08-11 23:09:45 +0000518MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) {
519 return MachinePointerInfo(MF.getPSVManager().getGOT());
Chris Lattner50287ea2010-09-21 06:43:24 +0000520}
Chris Lattnerde93bb02010-09-21 05:39:30 +0000521
Alex Lorenze40c8a22015-08-11 23:09:45 +0000522MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF,
523 int64_t Offset) {
524 return MachinePointerInfo(MF.getPSVManager().getStack(), Offset);
Chris Lattner886250c2010-09-21 18:51:21 +0000525}
526
Justin Lebara3b786a2016-07-14 17:07:44 +0000527MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags f,
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000528 uint64_t s, unsigned int a,
Hal Finkelcc39b672014-07-24 12:16:19 +0000529 const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +0000530 const MDNode *Ranges)
Justin Lebara3b786a2016-07-14 17:07:44 +0000531 : PtrInfo(ptrinfo), Size(s), FlagVals(f), BaseAlignLog2(Log2_32(a) + 1),
532 AAInfo(AAInfo), Ranges(Ranges) {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000533 assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue*>() ||
534 isa<PointerType>(PtrInfo.V.get<const Value*>()->getType())) &&
Chris Lattner00ca0b82010-09-21 04:32:08 +0000535 "invalid pointer value");
Dan Gohmane7c82422009-09-21 19:47:04 +0000536 assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
Dan Gohmanbf98f682008-07-16 15:56:42 +0000537 assert((isLoad() || isStore()) && "Not a load/store!");
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000538}
539
Dan Gohman2da2bed2008-08-20 15:58:01 +0000540/// Profile - Gather unique data for the object.
541///
542void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
Chris Lattner187f6532010-09-21 04:23:39 +0000543 ID.AddInteger(getOffset());
Dan Gohman2da2bed2008-08-20 15:58:01 +0000544 ID.AddInteger(Size);
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000545 ID.AddPointer(getOpaqueValue());
Justin Lebara3b786a2016-07-14 17:07:44 +0000546 ID.AddInteger(getFlags());
547 ID.AddInteger(getBaseAlignment());
Dan Gohman2da2bed2008-08-20 15:58:01 +0000548}
549
Dan Gohman48b185d2009-09-25 20:36:54 +0000550void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
551 // The Value and Offset may differ due to CSE. But the flags and size
552 // should be the same.
553 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
554 assert(MMO->getSize() == getSize() && "Size mismatch!");
555
556 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
557 // Update the alignment value.
Justin Lebara3b786a2016-07-14 17:07:44 +0000558 BaseAlignLog2 = Log2_32(MMO->getBaseAlignment()) + 1;
Dan Gohman48b185d2009-09-25 20:36:54 +0000559 // Also update the base and offset, because the new alignment may
560 // not be applicable with the old ones.
Chris Lattner187f6532010-09-21 04:23:39 +0000561 PtrInfo = MMO->PtrInfo;
Dan Gohman48b185d2009-09-25 20:36:54 +0000562 }
563}
564
Dan Gohman5a6b11c2009-09-25 23:33:20 +0000565/// getAlignment - Return the minimum known alignment in bytes of the
566/// actual memory reference.
567uint64_t MachineMemOperand::getAlignment() const {
568 return MinAlign(getBaseAlignment(), getOffset());
569}
570
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000571void MachineMemOperand::print(raw_ostream &OS) const {
572 ModuleSlotTracker DummyMST(nullptr);
573 print(OS, DummyMST);
574}
575void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST) const {
576 assert((isLoad() || isStore()) &&
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000577 "SV has to be a load, store or both.");
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000578
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000579 if (isVolatile())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000580 OS << "Volatile ";
581
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000582 if (isLoad())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000583 OS << "LD";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000584 if (isStore())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000585 OS << "ST";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000586 OS << getSize();
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000587
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000588 // Print the address information.
589 OS << "[";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000590 if (const Value *V = getValue())
591 V->printAsOperand(OS, /*PrintType=*/false, MST);
592 else if (const PseudoSourceValue *PSV = getPseudoValue())
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000593 PSV->printCustom(OS);
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000594 else
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000595 OS << "<unknown>";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000596
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000597 unsigned AS = getAddrSpace();
Matt Arsenault68c38fd2013-12-14 00:24:02 +0000598 if (AS != 0)
599 OS << "(addrspace=" << AS << ')';
600
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000601 // If the alignment of the memory reference itself differs from the alignment
602 // of the base pointer, print the base alignment explicitly, next to the base
603 // pointer.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000604 if (getBaseAlignment() != getAlignment())
605 OS << "(align=" << getBaseAlignment() << ")";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000606
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000607 if (getOffset() != 0)
608 OS << "+" << getOffset();
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000609 OS << "]";
610
611 // Print the alignment of the reference.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000612 if (getBaseAlignment() != getAlignment() || getBaseAlignment() != getSize())
613 OS << "(align=" << getAlignment() << ")";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000614
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000615 // Print TBAA info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000616 if (const MDNode *TBAAInfo = getAAInfo().TBAA) {
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000617 OS << "(tbaa=";
618 if (TBAAInfo->getNumOperands() > 0)
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000619 TBAAInfo->getOperand(0)->printAsOperand(OS, MST);
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000620 else
621 OS << "<unknown>";
622 OS << ")";
623 }
624
Hal Finkel94146652014-07-24 14:25:39 +0000625 // Print AA scope info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000626 if (const MDNode *ScopeInfo = getAAInfo().Scope) {
Hal Finkel94146652014-07-24 14:25:39 +0000627 OS << "(alias.scope=";
628 if (ScopeInfo->getNumOperands() > 0)
629 for (unsigned i = 0, ie = ScopeInfo->getNumOperands(); i != ie; ++i) {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000630 ScopeInfo->getOperand(i)->printAsOperand(OS, MST);
Hal Finkel94146652014-07-24 14:25:39 +0000631 if (i != ie-1)
632 OS << ",";
633 }
634 else
635 OS << "<unknown>";
636 OS << ")";
637 }
638
639 // Print AA noalias scope info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000640 if (const MDNode *NoAliasInfo = getAAInfo().NoAlias) {
Hal Finkel94146652014-07-24 14:25:39 +0000641 OS << "(noalias=";
642 if (NoAliasInfo->getNumOperands() > 0)
643 for (unsigned i = 0, ie = NoAliasInfo->getNumOperands(); i != ie; ++i) {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000644 NoAliasInfo->getOperand(i)->printAsOperand(OS, MST);
Hal Finkel94146652014-07-24 14:25:39 +0000645 if (i != ie-1)
646 OS << ",";
647 }
648 else
649 OS << "<unknown>";
650 OS << ")";
651 }
652
Bill Wendling9f638ab2011-04-29 23:45:22 +0000653 // Print nontemporal info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000654 if (isNonTemporal())
Bill Wendling9f638ab2011-04-29 23:45:22 +0000655 OS << "(nontemporal)";
656
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000657 if (isInvariant())
Matt Arsenault572c29a2015-06-26 19:00:11 +0000658 OS << "(invariant)";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000659}
660
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000661//===----------------------------------------------------------------------===//
Chris Lattner60055892007-12-30 21:56:09 +0000662// MachineInstr Implementation
663//===----------------------------------------------------------------------===//
664
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000665void MachineInstr::addImplicitDefUseOperands(MachineFunction &MF) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000666 if (MCID->ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000667 for (const MCPhysReg *ImpDefs = MCID->getImplicitDefs(); *ImpDefs;
668 ++ImpDefs)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000669 addOperand(MF, MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng6cc775f2011-06-28 19:10:37 +0000670 if (MCID->ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000671 for (const MCPhysReg *ImpUses = MCID->getImplicitUses(); *ImpUses;
672 ++ImpUses)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000673 addOperand(MF, MachineOperand::CreateReg(*ImpUses, false, true));
Evan Cheng77af6ac2006-11-13 23:34:06 +0000674}
675
Bob Wilson406f2702010-04-09 04:34:03 +0000676/// MachineInstr ctor - This constructor creates a MachineInstr and adds the
677/// implicit operands. It reserves space for the number of operands specified by
Evan Cheng6cc775f2011-06-28 19:10:37 +0000678/// the MCInstrDesc.
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000679MachineInstr::MachineInstr(MachineFunction &MF, const MCInstrDesc &tid,
Benjamin Kramera9591b52015-02-07 12:28:15 +0000680 DebugLoc dl, bool NoImp)
681 : MCID(&tid), Parent(nullptr), Operands(nullptr), NumOperands(0), Flags(0),
682 AsmPrinterFlags(0), NumMemRefs(0), MemRefs(nullptr),
Quentin Colombet98551112016-02-11 18:22:37 +0000683 debugLoc(std::move(dl))
684#ifdef LLVM_BUILD_GLOBAL_ISEL
685 ,
Tim Northover98a56eb2016-07-22 22:13:36 +0000686 Tys(0)
Quentin Colombet98551112016-02-11 18:22:37 +0000687#endif
688{
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000689 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
690
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000691 // Reserve space for the expected number of operands.
692 if (unsigned NumOps = MCID->getNumOperands() +
693 MCID->getNumImplicitDefs() + MCID->getNumImplicitUses()) {
694 CapOperands = OperandCapacity::get(NumOps);
695 Operands = MF.allocateOperandArray(CapOperands);
696 }
697
Dale Johannesen4e04ef32009-01-27 23:20:29 +0000698 if (!NoImp)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000699 addImplicitDefUseOperands(MF);
Dale Johannesen4e04ef32009-01-27 23:20:29 +0000700}
701
Misha Brukmanb47ab7a2004-07-09 14:45:17 +0000702/// MachineInstr ctor - Copies MachineInstr arg exactly
703///
Evan Chenga7a20c42008-07-19 00:37:25 +0000704MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Quentin Colombet98551112016-02-11 18:22:37 +0000705 : MCID(&MI.getDesc()), Parent(nullptr), Operands(nullptr), NumOperands(0),
706 Flags(0), AsmPrinterFlags(0), NumMemRefs(MI.NumMemRefs),
707 MemRefs(MI.MemRefs), debugLoc(MI.getDebugLoc())
708#ifdef LLVM_BUILD_GLOBAL_ISEL
709 ,
Tim Northover98a56eb2016-07-22 22:13:36 +0000710 Tys(0)
Quentin Colombet98551112016-02-11 18:22:37 +0000711#endif
712{
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000713 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
714
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000715 CapOperands = OperandCapacity::get(MI.getNumOperands());
716 Operands = MF.allocateOperandArray(CapOperands);
Tanya Lattner9953d862004-05-23 20:58:02 +0000717
Jakob Stoklund Olesendc5285f2013-01-05 05:05:51 +0000718 // Copy operands.
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000719 for (const MachineOperand &MO : MI.operands())
720 addOperand(MF, MO);
Tanya Lattnerbcee21b2004-05-24 03:14:18 +0000721
Jakob Stoklund Olesena33f5042012-12-18 21:36:05 +0000722 // Copy all the sensible flags.
723 setFlags(MI.Flags);
Alkis Evlogimenos14f3fe82004-02-16 07:17:43 +0000724}
725
Chris Lattner961e7422008-01-01 01:12:31 +0000726/// getRegInfo - If this instruction is embedded into a MachineFunction,
727/// return the MachineRegisterInfo object for the current function, otherwise
728/// return null.
729MachineRegisterInfo *MachineInstr::getRegInfo() {
730 if (MachineBasicBlock *MBB = getParent())
Dan Gohmanf188fa42008-07-08 23:59:09 +0000731 return &MBB->getParent()->getRegInfo();
Craig Topperc0196b12014-04-14 00:51:57 +0000732 return nullptr;
Chris Lattner961e7422008-01-01 01:12:31 +0000733}
734
Quentin Colombet41bea872016-03-07 22:47:23 +0000735// Implement dummy setter and getter for type when
736// global-isel is not built.
737// The proper implementation is WIP and is tracked here:
738// PR26576.
739#ifndef LLVM_BUILD_GLOBAL_ISEL
Tim Northover98a56eb2016-07-22 22:13:36 +0000740unsigned MachineInstr::getNumTypes() const { return 0; }
Quentin Colombet41bea872016-03-07 22:47:23 +0000741
Tim Northover98a56eb2016-07-22 22:13:36 +0000742void MachineInstr::setType(LLT Ty, unsigned Idx) {}
743
744LLT MachineInstr::getType(unsigned Idx) const { return LLT{}; }
Quentin Colombet41bea872016-03-07 22:47:23 +0000745
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000746void MachineInstr::removeTypes() {}
747
Quentin Colombet41bea872016-03-07 22:47:23 +0000748#else
Tim Northover98a56eb2016-07-22 22:13:36 +0000749unsigned MachineInstr::getNumTypes() const { return Tys.size(); }
750
751void MachineInstr::setType(LLT Ty, unsigned Idx) {
Tim Northover62ae5682016-07-20 19:09:30 +0000752 assert((!Ty.isValid() || isPreISelGenericOpcode(getOpcode())) &&
Quentin Colombet41bea872016-03-07 22:47:23 +0000753 "Non generic instructions are not supposed to be typed");
Tim Northover98a56eb2016-07-22 22:13:36 +0000754 if (Tys.size() < Idx + 1)
755 Tys.resize(Idx+1);
756 Tys[Idx] = Ty;
Quentin Colombet41bea872016-03-07 22:47:23 +0000757}
758
Tim Northover98a56eb2016-07-22 22:13:36 +0000759LLT MachineInstr::getType(unsigned Idx) const { return Tys[Idx]; }
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000760
761void MachineInstr::removeTypes() {
762 Tys.clear();
763}
Quentin Colombet41bea872016-03-07 22:47:23 +0000764#endif // LLVM_BUILD_GLOBAL_ISEL
765
Chris Lattner961e7422008-01-01 01:12:31 +0000766/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
767/// this instruction from their respective use lists. This requires that the
768/// operands already be on their use lists.
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000769void MachineInstr::RemoveRegOperandsFromUseLists(MachineRegisterInfo &MRI) {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000770 for (MachineOperand &MO : operands())
771 if (MO.isReg())
772 MRI.removeRegOperandFromUseList(&MO);
Chris Lattner961e7422008-01-01 01:12:31 +0000773}
774
775/// AddRegOperandsToUseLists - Add all of the register operands in
776/// this instruction from their respective use lists. This requires that the
777/// operands not be on their use lists yet.
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000778void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &MRI) {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000779 for (MachineOperand &MO : operands())
780 if (MO.isReg())
781 MRI.addRegOperandToUseList(&MO);
Chris Lattner961e7422008-01-01 01:12:31 +0000782}
783
Jakob Stoklund Olesen2455b5852012-12-20 22:54:05 +0000784void MachineInstr::addOperand(const MachineOperand &Op) {
785 MachineBasicBlock *MBB = getParent();
786 assert(MBB && "Use MachineInstrBuilder to add operands to dangling instrs");
787 MachineFunction *MF = MBB->getParent();
788 assert(MF && "Use MachineInstrBuilder to add operands to dangling instrs");
789 addOperand(*MF, Op);
790}
791
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000792/// Move NumOps MachineOperands from Src to Dst, with support for overlapping
793/// ranges. If MRI is non-null also update use-def chains.
794static void moveOperands(MachineOperand *Dst, MachineOperand *Src,
795 unsigned NumOps, MachineRegisterInfo *MRI) {
796 if (MRI)
797 return MRI->moveOperands(Dst, Src, NumOps);
798
JF Bastiena874d1a2016-03-26 18:20:02 +0000799 // MachineOperand is a trivially copyable type so we can just use memmove.
Benjamin Kramer5c0e64f2015-02-21 16:22:48 +0000800 std::memmove(Dst, Src, NumOps * sizeof(MachineOperand));
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000801}
802
Chris Lattner961e7422008-01-01 01:12:31 +0000803/// addOperand - Add the specified operand to the instruction. If it is an
804/// implicit operand, it is added to the end of the operand list. If it is
805/// an explicit operand it is added at the end of the explicit operand list
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000806/// (before the first implicit operand).
Jakob Stoklund Olesen2455b5852012-12-20 22:54:05 +0000807void MachineInstr::addOperand(MachineFunction &MF, const MachineOperand &Op) {
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000808 assert(MCID && "Cannot add operands before providing an instr descriptor");
Dan Gohman9356d8f2008-12-09 22:45:08 +0000809
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000810 // Check if we're adding one of our existing operands.
811 if (&Op >= Operands && &Op < Operands + NumOperands) {
812 // This is unusual: MI->addOperand(MI->getOperand(i)).
813 // If adding Op requires reallocating or moving existing operands around,
814 // the Op reference could go stale. Support it by copying Op.
815 MachineOperand CopyOp(Op);
816 return addOperand(MF, CopyOp);
817 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000818
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000819 // Find the insert location for the new operand. Implicit registers go at
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000820 // the end, everything else goes before the implicit regs.
821 //
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000822 // FIXME: Allow mixed explicit and implicit operands on inline asm.
823 // InstrEmitter::EmitSpecialNode() is marking inline asm clobbers as
824 // implicit-defs, but they must not be moved around. See the FIXME in
825 // InstrEmitter.cpp.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000826 unsigned OpNo = getNumOperands();
827 bool isImpReg = Op.isReg() && Op.isImplicit();
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000828 if (!isImpReg && !isInlineAsm()) {
829 while (OpNo && Operands[OpNo-1].isReg() && Operands[OpNo-1].isImplicit()) {
830 --OpNo;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000831 assert(!Operands[OpNo].isTied() && "Cannot move tied operands");
Chris Lattner961e7422008-01-01 01:12:31 +0000832 }
833 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000834
Pekka Jaaskelaineneb4a6e72013-10-15 14:40:46 +0000835#ifndef NDEBUG
Pekka Jaaskelaineneb08e2e2013-10-15 14:18:10 +0000836 bool isMetaDataOp = Op.getType() == MachineOperand::MO_Metadata;
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000837 // OpNo now points as the desired insertion point. Unless this is a variadic
838 // instruction, only implicit regs are allowed beyond MCID->getNumOperands().
Jakob Stoklund Olesenc300ef02012-07-04 23:53:23 +0000839 // RegMask operands go between the explicit and implicit operands.
840 assert((isImpReg || Op.isRegMask() || MCID->isVariadic() ||
Pekka Jaaskelaineneb08e2e2013-10-15 14:18:10 +0000841 OpNo < MCID->getNumOperands() || isMetaDataOp) &&
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000842 "Trying to add an operand to a machine instr that is already done!");
Pekka Jaaskelaineneb4a6e72013-10-15 14:40:46 +0000843#endif
Chris Lattner961e7422008-01-01 01:12:31 +0000844
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000845 MachineRegisterInfo *MRI = getRegInfo();
Chris Lattner961e7422008-01-01 01:12:31 +0000846
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000847 // Determine if the Operands array needs to be reallocated.
848 // Save the old capacity and operand array.
849 OperandCapacity OldCap = CapOperands;
850 MachineOperand *OldOperands = Operands;
851 if (!OldOperands || OldCap.getSize() == getNumOperands()) {
852 CapOperands = OldOperands ? OldCap.getNext() : OldCap.get(1);
853 Operands = MF.allocateOperandArray(CapOperands);
854 // Move the operands before the insertion point.
855 if (OpNo)
856 moveOperands(Operands, OldOperands, OpNo, MRI);
857 }
Chris Lattner961e7422008-01-01 01:12:31 +0000858
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000859 // Move the operands following the insertion point.
860 if (OpNo != NumOperands)
861 moveOperands(Operands + OpNo + 1, OldOperands + OpNo, NumOperands - OpNo,
862 MRI);
863 ++NumOperands;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000864
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000865 // Deallocate the old operand array.
866 if (OldOperands != Operands && OldOperands)
867 MF.deallocateOperandArray(OldCap, OldOperands);
868
869 // Copy Op into place. It still needs to be inserted into the MRI use lists.
870 MachineOperand *NewMO = new (Operands + OpNo) MachineOperand(Op);
871 NewMO->ParentMI = this;
872
873 // When adding a register operand, tell MRI about it.
874 if (NewMO->isReg()) {
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000875 // Ensure isOnRegUseList() returns false, regardless of Op's status.
Craig Topperc0196b12014-04-14 00:51:57 +0000876 NewMO->Contents.Reg.Prev = nullptr;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000877 // Ignore existing ties. This is not a property that can be copied.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000878 NewMO->TiedTo = 0;
879 // Add the new operand to MRI, but only for instructions in an MBB.
880 if (MRI)
881 MRI->addRegOperandToUseList(NewMO);
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000882 // The MCID operand information isn't accurate until we start adding
883 // explicit operands. The implicit operands are added first, then the
884 // explicits are inserted before them.
885 if (!isImpReg) {
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000886 // Tie uses to defs as indicated in MCInstrDesc.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000887 if (NewMO->isUse()) {
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000888 int DefIdx = MCID->getOperandConstraint(OpNo, MCOI::TIED_TO);
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +0000889 if (DefIdx != -1)
890 tieOperands(DefIdx, OpNo);
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000891 }
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000892 // If the register operand is flagged as early, mark the operand as such.
893 if (MCID->getOperandConstraint(OpNo, MCOI::EARLY_CLOBBER) != -1)
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000894 NewMO->setIsEarlyClobber(true);
Chris Lattner961e7422008-01-01 01:12:31 +0000895 }
Chris Lattner961e7422008-01-01 01:12:31 +0000896 }
897}
898
899/// RemoveOperand - Erase an operand from an instruction, leaving it with one
900/// fewer operand than it started with.
901///
902void MachineInstr::RemoveOperand(unsigned OpNo) {
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +0000903 assert(OpNo < getNumOperands() && "Invalid operand number");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000904 untieRegOperand(OpNo);
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000905
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000906#ifndef NDEBUG
907 // Moving tied operands would break the ties.
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +0000908 for (unsigned i = OpNo + 1, e = getNumOperands(); i != e; ++i)
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000909 if (Operands[i].isReg())
910 assert(!Operands[i].isTied() && "Cannot move tied operands");
911#endif
912
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000913 MachineRegisterInfo *MRI = getRegInfo();
914 if (MRI && Operands[OpNo].isReg())
915 MRI->removeRegOperandFromUseList(Operands + OpNo);
Chris Lattner961e7422008-01-01 01:12:31 +0000916
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000917 // Don't call the MachineOperand destructor. A lot of this code depends on
918 // MachineOperand having a trivial destructor anyway, and adding a call here
919 // wouldn't make it 'destructor-correct'.
920
921 if (unsigned N = NumOperands - 1 - OpNo)
922 moveOperands(Operands + OpNo, Operands + OpNo + 1, N, MRI);
923 --NumOperands;
Chris Lattner961e7422008-01-01 01:12:31 +0000924}
925
Dan Gohman48b185d2009-09-25 20:36:54 +0000926/// addMemOperand - Add a MachineMemOperand to the machine instruction.
927/// This function should be used only occasionally. The setMemRefs function
928/// is the primary method for setting up a MachineInstr's MemRefs list.
Dan Gohman3b460302008-07-07 23:14:23 +0000929void MachineInstr::addMemOperand(MachineFunction &MF,
Dan Gohman48b185d2009-09-25 20:36:54 +0000930 MachineMemOperand *MO) {
931 mmo_iterator OldMemRefs = MemRefs;
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000932 unsigned OldNumMemRefs = NumMemRefs;
Dan Gohman3b460302008-07-07 23:14:23 +0000933
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000934 unsigned NewNum = NumMemRefs + 1;
Dan Gohman48b185d2009-09-25 20:36:54 +0000935 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
Dan Gohman3b460302008-07-07 23:14:23 +0000936
Benjamin Kramerd03878b2012-03-16 16:39:27 +0000937 std::copy(OldMemRefs, OldMemRefs + OldNumMemRefs, NewMemRefs);
Dan Gohman48b185d2009-09-25 20:36:54 +0000938 NewMemRefs[NewNum - 1] = MO;
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000939 setMemRefs(NewMemRefs, NewMemRefs + NewNum);
Dan Gohman48b185d2009-09-25 20:36:54 +0000940}
Chris Lattner961e7422008-01-01 01:12:31 +0000941
Philip Reames5eb90a72016-01-06 19:33:12 +0000942/// Check to see if the MMOs pointed to by the two MemRefs arrays are
Junmo Park820e3922016-02-26 02:07:36 +0000943/// identical.
Philip Reames5eb90a72016-01-06 19:33:12 +0000944static bool hasIdenticalMMOs(const MachineInstr &MI1, const MachineInstr &MI2) {
945 auto I1 = MI1.memoperands_begin(), E1 = MI1.memoperands_end();
946 auto I2 = MI2.memoperands_begin(), E2 = MI2.memoperands_end();
947 if ((E1 - I1) != (E2 - I2))
948 return false;
949 for (; I1 != E1; ++I1, ++I2) {
950 if (**I1 != **I2)
951 return false;
952 }
953 return true;
954}
955
Philip Reamesc86ed002016-01-06 04:39:03 +0000956std::pair<MachineInstr::mmo_iterator, unsigned>
957MachineInstr::mergeMemRefsWith(const MachineInstr& Other) {
Philip Reames5eb90a72016-01-06 19:33:12 +0000958
959 // If either of the incoming memrefs are empty, we must be conservative and
960 // treat this as if we've exhausted our space for memrefs and dropped them.
961 if (memoperands_empty() || Other.memoperands_empty())
962 return std::make_pair(nullptr, 0);
963
964 // If both instructions have identical memrefs, we don't need to merge them.
965 // Since many instructions have a single memref, and we tend to merge things
966 // like pairs of loads from the same location, this catches a large number of
967 // cases in practice.
968 if (hasIdenticalMMOs(*this, Other))
969 return std::make_pair(MemRefs, NumMemRefs);
Junmo Park820e3922016-02-26 02:07:36 +0000970
Philip Reamesc86ed002016-01-06 04:39:03 +0000971 // TODO: consider uniquing elements within the operand lists to reduce
972 // space usage and fall back to conservative information less often.
Philip Reames5eb90a72016-01-06 19:33:12 +0000973 size_t CombinedNumMemRefs = NumMemRefs + Other.NumMemRefs;
974
975 // If we don't have enough room to store this many memrefs, be conservative
976 // and drop them. Otherwise, we'd fail asserts when trying to add them to
977 // the new instruction.
978 if (CombinedNumMemRefs != uint8_t(CombinedNumMemRefs))
979 return std::make_pair(nullptr, 0);
Philip Reamesc86ed002016-01-06 04:39:03 +0000980
981 MachineFunction *MF = getParent()->getParent();
982 mmo_iterator MemBegin = MF->allocateMemRefsArray(CombinedNumMemRefs);
983 mmo_iterator MemEnd = std::copy(memoperands_begin(), memoperands_end(),
984 MemBegin);
985 MemEnd = std::copy(Other.memoperands_begin(), Other.memoperands_end(),
986 MemEnd);
Philip Reames2d2fc4a2016-01-06 05:53:09 +0000987 assert(MemEnd - MemBegin == (ptrdiff_t)CombinedNumMemRefs &&
988 "missing memrefs");
Junmo Park820e3922016-02-26 02:07:36 +0000989
Philip Reamesc86ed002016-01-06 04:39:03 +0000990 return std::make_pair(MemBegin, CombinedNumMemRefs);
991}
992
Benjamin Kramer97f889f2012-03-17 17:03:45 +0000993bool MachineInstr::hasPropertyInBundle(unsigned Mask, QueryType Type) const {
Jakob Stoklund Olesenf0615c72013-01-10 18:42:44 +0000994 assert(!isBundledWithPred() && "Must be called on bundle header");
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000995 for (MachineBasicBlock::const_instr_iterator MII = getIterator();; ++MII) {
Benjamin Kramer97f889f2012-03-17 17:03:45 +0000996 if (MII->getDesc().getFlags() & Mask) {
Evan Chengcdf89fd2011-12-08 19:23:10 +0000997 if (Type == AnyInBundle)
Evan Cheng7f8e5632011-12-07 07:15:52 +0000998 return true;
999 } else {
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +00001000 if (Type == AllInBundle && !MII->isBundle())
Evan Cheng7f8e5632011-12-07 07:15:52 +00001001 return false;
1002 }
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +00001003 // This was the last instruction in the bundle.
1004 if (!MII->isBundledWithSucc())
1005 return Type == AllInBundle;
Evan Cheng2a81dd42011-12-06 22:12:01 +00001006 }
Evan Cheng2a81dd42011-12-06 22:12:01 +00001007}
1008
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001009bool MachineInstr::isIdenticalTo(const MachineInstr &Other,
Evan Chenge9c46c22010-03-03 01:44:33 +00001010 MICheckType Check) const {
Evan Cheng0f260e12010-03-03 21:54:14 +00001011 // If opcodes or number of operands are not the same then the two
1012 // instructions are obviously not identical.
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001013 if (Other.getOpcode() != getOpcode() ||
1014 Other.getNumOperands() != getNumOperands())
Evan Cheng0f260e12010-03-03 21:54:14 +00001015 return false;
1016
Evan Cheng7fae11b2011-12-14 02:11:42 +00001017 if (isBundle()) {
1018 // Both instructions are bundles, compare MIs inside the bundle.
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001019 MachineBasicBlock::const_instr_iterator I1 = getIterator();
1020 MachineBasicBlock::const_instr_iterator E1 = getParent()->instr_end();
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001021 MachineBasicBlock::const_instr_iterator I2 = Other.getIterator();
1022 MachineBasicBlock::const_instr_iterator E2 = Other.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00001023 while (++I1 != E1 && I1->isInsideBundle()) {
1024 ++I2;
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001025 if (I2 == E2 || !I2->isInsideBundle() || !I1->isIdenticalTo(*I2, Check))
Evan Cheng7fae11b2011-12-14 02:11:42 +00001026 return false;
1027 }
1028 }
1029
Evan Cheng0f260e12010-03-03 21:54:14 +00001030 // Check operands to make sure they match.
1031 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1032 const MachineOperand &MO = getOperand(i);
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001033 const MachineOperand &OMO = Other.getOperand(i);
Evan Chengcfdf3392011-05-12 00:56:58 +00001034 if (!MO.isReg()) {
1035 if (!MO.isIdenticalTo(OMO))
1036 return false;
1037 continue;
1038 }
1039
Evan Cheng0f260e12010-03-03 21:54:14 +00001040 // Clients may or may not want to ignore defs when testing for equality.
1041 // For example, machine CSE pass only cares about finding common
1042 // subexpressions, so it's safe to ignore virtual register defs.
Evan Chengcfdf3392011-05-12 00:56:58 +00001043 if (MO.isDef()) {
Evan Cheng0f260e12010-03-03 21:54:14 +00001044 if (Check == IgnoreDefs)
1045 continue;
Evan Chengcfdf3392011-05-12 00:56:58 +00001046 else if (Check == IgnoreVRegDefs) {
1047 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) ||
1048 TargetRegisterInfo::isPhysicalRegister(OMO.getReg()))
1049 if (MO.getReg() != OMO.getReg())
1050 return false;
1051 } else {
1052 if (!MO.isIdenticalTo(OMO))
Evan Cheng0f260e12010-03-03 21:54:14 +00001053 return false;
Evan Chengcfdf3392011-05-12 00:56:58 +00001054 if (Check == CheckKillDead && MO.isDead() != OMO.isDead())
1055 return false;
1056 }
1057 } else {
1058 if (!MO.isIdenticalTo(OMO))
1059 return false;
1060 if (Check == CheckKillDead && MO.isKill() != OMO.isKill())
1061 return false;
1062 }
Evan Cheng0f260e12010-03-03 21:54:14 +00001063 }
Devang Patelbf8cc602011-07-07 17:45:33 +00001064 // If DebugLoc does not match then two dbg.values are not identical.
1065 if (isDebugValue())
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001066 if (getDebugLoc() && Other.getDebugLoc() &&
1067 getDebugLoc() != Other.getDebugLoc())
Devang Patelbf8cc602011-07-07 17:45:33 +00001068 return false;
Evan Cheng0f260e12010-03-03 21:54:14 +00001069 return true;
Evan Chenge9c46c22010-03-03 01:44:33 +00001070}
1071
Chris Lattnerbec79b42006-04-17 21:35:41 +00001072MachineInstr *MachineInstr::removeFromParent() {
1073 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001074 return getParent()->remove(this);
Chris Lattnerbec79b42006-04-17 21:35:41 +00001075}
1076
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001077MachineInstr *MachineInstr::removeFromBundle() {
1078 assert(getParent() && "Not embedded in a basic block!");
1079 return getParent()->remove_instr(this);
1080}
Chris Lattnerbec79b42006-04-17 21:35:41 +00001081
Dan Gohman3b460302008-07-07 23:14:23 +00001082void MachineInstr::eraseFromParent() {
1083 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001084 getParent()->erase(this);
Dan Gohman3b460302008-07-07 23:14:23 +00001085}
1086
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +00001087void MachineInstr::eraseFromParentAndMarkDBGValuesForRemoval() {
1088 assert(getParent() && "Not embedded in a basic block!");
1089 MachineBasicBlock *MBB = getParent();
1090 MachineFunction *MF = MBB->getParent();
1091 assert(MF && "Not embedded in a function!");
1092
1093 MachineInstr *MI = (MachineInstr *)this;
1094 MachineRegisterInfo &MRI = MF->getRegInfo();
1095
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001096 for (const MachineOperand &MO : MI->operands()) {
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +00001097 if (!MO.isReg() || !MO.isDef())
1098 continue;
1099 unsigned Reg = MO.getReg();
1100 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1101 continue;
1102 MRI.markUsesInDebugValueAsUndef(Reg);
1103 }
1104 MI->eraseFromParent();
1105}
1106
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001107void MachineInstr::eraseFromBundle() {
1108 assert(getParent() && "Not embedded in a basic block!");
1109 getParent()->erase_instr(this);
1110}
Dan Gohman3b460302008-07-07 23:14:23 +00001111
Evan Cheng4d728b02007-05-15 01:26:09 +00001112/// getNumExplicitOperands - Returns the number of non-implicit operands.
1113///
1114unsigned MachineInstr::getNumExplicitOperands() const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001115 unsigned NumOperands = MCID->getNumOperands();
1116 if (!MCID->isVariadic())
Evan Cheng4d728b02007-05-15 01:26:09 +00001117 return NumOperands;
1118
Dan Gohman37608532009-04-15 17:59:11 +00001119 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
1120 const MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001121 if (!MO.isReg() || !MO.isImplicit())
Evan Cheng4d728b02007-05-15 01:26:09 +00001122 NumOperands++;
1123 }
1124 return NumOperands;
1125}
1126
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001127void MachineInstr::bundleWithPred() {
1128 assert(!isBundledWithPred() && "MI is already bundled with its predecessor");
1129 setFlag(BundledPred);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001130 MachineBasicBlock::instr_iterator Pred = getIterator();
1131 --Pred;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001132 assert(!Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001133 Pred->setFlag(BundledSucc);
1134}
1135
1136void MachineInstr::bundleWithSucc() {
1137 assert(!isBundledWithSucc() && "MI is already bundled with its successor");
1138 setFlag(BundledSucc);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001139 MachineBasicBlock::instr_iterator Succ = getIterator();
1140 ++Succ;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001141 assert(!Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001142 Succ->setFlag(BundledPred);
1143}
1144
1145void MachineInstr::unbundleFromPred() {
1146 assert(isBundledWithPred() && "MI isn't bundled with its predecessor");
1147 clearFlag(BundledPred);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001148 MachineBasicBlock::instr_iterator Pred = getIterator();
1149 --Pred;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001150 assert(Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001151 Pred->clearFlag(BundledSucc);
1152}
1153
1154void MachineInstr::unbundleFromSucc() {
1155 assert(isBundledWithSucc() && "MI isn't bundled with its successor");
1156 clearFlag(BundledSucc);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001157 MachineBasicBlock::instr_iterator Succ = getIterator();
1158 ++Succ;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001159 assert(Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001160 Succ->clearFlag(BundledPred);
1161}
1162
Evan Cheng6eb516d2011-01-07 23:50:32 +00001163bool MachineInstr::isStackAligningInlineAsm() const {
1164 if (isInlineAsm()) {
1165 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1166 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
1167 return true;
1168 }
1169 return false;
1170}
Chris Lattner33f5af02006-10-20 22:39:59 +00001171
Chad Rosier994f4042012-09-05 21:00:58 +00001172InlineAsm::AsmDialect MachineInstr::getInlineAsmDialect() const {
1173 assert(isInlineAsm() && "getInlineAsmDialect() only works for inline asms!");
1174 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
Chad Rosiere53314f2012-09-05 22:40:13 +00001175 return InlineAsm::AsmDialect((ExtraInfo & InlineAsm::Extra_AsmDialect) != 0);
Chad Rosier994f4042012-09-05 21:00:58 +00001176}
1177
Jakob Stoklund Olesen1e737162011-10-12 23:37:33 +00001178int MachineInstr::findInlineAsmFlagIdx(unsigned OpIdx,
1179 unsigned *GroupNo) const {
1180 assert(isInlineAsm() && "Expected an inline asm instruction");
1181 assert(OpIdx < getNumOperands() && "OpIdx out of range");
1182
1183 // Ignore queries about the initial operands.
1184 if (OpIdx < InlineAsm::MIOp_FirstOperand)
1185 return -1;
1186
1187 unsigned Group = 0;
1188 unsigned NumOps;
1189 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
1190 i += NumOps) {
1191 const MachineOperand &FlagMO = getOperand(i);
1192 // If we reach the implicit register operands, stop looking.
1193 if (!FlagMO.isImm())
1194 return -1;
1195 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
1196 if (i + NumOps > OpIdx) {
1197 if (GroupNo)
1198 *GroupNo = Group;
1199 return i;
1200 }
1201 ++Group;
1202 }
1203 return -1;
1204}
1205
Reid Kleckner28865802016-04-14 18:29:59 +00001206const DILocalVariable *MachineInstr::getDebugVariable() const {
1207 assert(isDebugValue() && "not a DBG_VALUE");
1208 return cast<DILocalVariable>(getOperand(2).getMetadata());
1209}
1210
1211const DIExpression *MachineInstr::getDebugExpression() const {
1212 assert(isDebugValue() && "not a DBG_VALUE");
1213 return cast<DIExpression>(getOperand(3).getMetadata());
1214}
1215
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001216const TargetRegisterClass*
1217MachineInstr::getRegClassConstraint(unsigned OpIdx,
1218 const TargetInstrInfo *TII,
1219 const TargetRegisterInfo *TRI) const {
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001220 assert(getParent() && "Can't have an MBB reference here!");
1221 assert(getParent()->getParent() && "Can't have an MF reference here!");
1222 const MachineFunction &MF = *getParent()->getParent();
1223
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001224 // Most opcodes have fixed constraints in their MCInstrDesc.
1225 if (!isInlineAsm())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001226 return TII->getRegClass(getDesc(), OpIdx, TRI, MF);
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001227
1228 if (!getOperand(OpIdx).isReg())
Craig Topperc0196b12014-04-14 00:51:57 +00001229 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001230
1231 // For tied uses on inline asm, get the constraint from the def.
1232 unsigned DefIdx;
1233 if (getOperand(OpIdx).isUse() && isRegTiedToDefOperand(OpIdx, &DefIdx))
1234 OpIdx = DefIdx;
1235
1236 // Inline asm stores register class constraints in the flag word.
1237 int FlagIdx = findInlineAsmFlagIdx(OpIdx);
1238 if (FlagIdx < 0)
Craig Topperc0196b12014-04-14 00:51:57 +00001239 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001240
1241 unsigned Flag = getOperand(FlagIdx).getImm();
1242 unsigned RCID;
Simon Dardisd32a2d32016-07-18 13:17:31 +00001243 if ((InlineAsm::getKind(Flag) == InlineAsm::Kind_RegUse ||
1244 InlineAsm::getKind(Flag) == InlineAsm::Kind_RegDef ||
1245 InlineAsm::getKind(Flag) == InlineAsm::Kind_RegDefEarlyClobber) &&
1246 InlineAsm::hasRegClassConstraint(Flag, RCID))
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001247 return TRI->getRegClass(RCID);
1248
1249 // Assume that all registers in a memory operand are pointers.
1250 if (InlineAsm::getKind(Flag) == InlineAsm::Kind_Mem)
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001251 return TRI->getPointerRegClass(MF);
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001252
Craig Topperc0196b12014-04-14 00:51:57 +00001253 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001254}
1255
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001256const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVReg(
1257 unsigned Reg, const TargetRegisterClass *CurRC, const TargetInstrInfo *TII,
1258 const TargetRegisterInfo *TRI, bool ExploreBundle) const {
1259 // Check every operands inside the bundle if we have
1260 // been asked to.
1261 if (ExploreBundle)
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001262 for (ConstMIBundleOperands OpndIt(*this); OpndIt.isValid() && CurRC;
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001263 ++OpndIt)
1264 CurRC = OpndIt->getParent()->getRegClassConstraintEffectForVRegImpl(
1265 OpndIt.getOperandNo(), Reg, CurRC, TII, TRI);
1266 else
1267 // Otherwise, just check the current operands.
Matthias Braune41e1462015-05-29 02:56:46 +00001268 for (unsigned i = 0, e = NumOperands; i < e && CurRC; ++i)
1269 CurRC = getRegClassConstraintEffectForVRegImpl(i, Reg, CurRC, TII, TRI);
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001270 return CurRC;
1271}
1272
1273const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVRegImpl(
1274 unsigned OpIdx, unsigned Reg, const TargetRegisterClass *CurRC,
1275 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
1276 assert(CurRC && "Invalid initial register class");
1277 // Check if Reg is constrained by some of its use/def from MI.
1278 const MachineOperand &MO = getOperand(OpIdx);
1279 if (!MO.isReg() || MO.getReg() != Reg)
1280 return CurRC;
1281 // If yes, accumulate the constraints through the operand.
1282 return getRegClassConstraintEffect(OpIdx, CurRC, TII, TRI);
1283}
1284
1285const TargetRegisterClass *MachineInstr::getRegClassConstraintEffect(
1286 unsigned OpIdx, const TargetRegisterClass *CurRC,
1287 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
1288 const TargetRegisterClass *OpRC = getRegClassConstraint(OpIdx, TII, TRI);
1289 const MachineOperand &MO = getOperand(OpIdx);
1290 assert(MO.isReg() &&
1291 "Cannot get register constraints for non-register operand");
1292 assert(CurRC && "Invalid initial register class");
1293 if (unsigned SubIdx = MO.getSubReg()) {
1294 if (OpRC)
1295 CurRC = TRI->getMatchingSuperRegClass(CurRC, OpRC, SubIdx);
1296 else
1297 CurRC = TRI->getSubClassWithSubReg(CurRC, SubIdx);
1298 } else if (OpRC)
1299 CurRC = TRI->getCommonSubClass(CurRC, OpRC);
1300 return CurRC;
1301}
1302
Jakob Stoklund Olesen68d752b2013-01-09 18:28:16 +00001303/// Return the number of instructions inside the MI bundle, not counting the
1304/// header instruction.
Evan Cheng7fae11b2011-12-14 02:11:42 +00001305unsigned MachineInstr::getBundleSize() const {
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001306 MachineBasicBlock::const_instr_iterator I = getIterator();
Evan Cheng7fae11b2011-12-14 02:11:42 +00001307 unsigned Size = 0;
Richard Trieu7a083812016-02-18 22:09:30 +00001308 while (I->isBundledWithSucc()) {
1309 ++Size;
1310 ++I;
1311 }
Evan Cheng7fae11b2011-12-14 02:11:42 +00001312 return Size;
1313}
1314
Nicolai Haehnleb0c97482016-04-22 04:04:08 +00001315/// Returns true if the MachineInstr has an implicit-use operand of exactly
1316/// the given register (not considering sub/super-registers).
1317bool MachineInstr::hasRegisterImplicitUseOperand(unsigned Reg) const {
1318 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1319 const MachineOperand &MO = getOperand(i);
1320 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == Reg)
1321 return true;
1322 }
1323 return false;
1324}
1325
Evan Cheng910c8082007-04-26 19:00:32 +00001326/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Jim Grosbach9632c142009-09-17 17:57:26 +00001327/// the specific register or -1 if it is not found. It further tightens
Evan Cheng9965aeb2007-02-23 01:04:26 +00001328/// the search criteria to a use that kills the register if isKill is true.
Evan Cheng63254462008-03-05 00:59:57 +00001329int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
1330 const TargetRegisterInfo *TRI) const {
Evan Cheng75c21942006-12-06 08:27:42 +00001331 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng5983bdb2007-05-29 18:35:22 +00001332 const MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001333 if (!MO.isReg() || !MO.isUse())
Evan Cheng63254462008-03-05 00:59:57 +00001334 continue;
1335 unsigned MOReg = MO.getReg();
1336 if (!MOReg)
1337 continue;
1338 if (MOReg == Reg ||
1339 (TRI &&
1340 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
1341 TargetRegisterInfo::isPhysicalRegister(Reg) &&
1342 TRI->isSubRegister(MOReg, Reg)))
Evan Cheng9965aeb2007-02-23 01:04:26 +00001343 if (!isKill || MO.isKill())
Evan Chengec3ac312007-03-26 22:37:45 +00001344 return i;
Evan Cheng75c21942006-12-06 08:27:42 +00001345 }
Evan Chengec3ac312007-03-26 22:37:45 +00001346 return -1;
Evan Cheng75c21942006-12-06 08:27:42 +00001347}
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001348
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001349/// readsWritesVirtualRegister - Return a pair of bools (reads, writes)
1350/// indicating if this instruction reads or writes Reg. This also considers
1351/// partial defines.
1352std::pair<bool,bool>
1353MachineInstr::readsWritesVirtualRegister(unsigned Reg,
1354 SmallVectorImpl<unsigned> *Ops) const {
1355 bool PartDef = false; // Partial redefine.
1356 bool FullDef = false; // Full define.
1357 bool Use = false;
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001358
1359 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1360 const MachineOperand &MO = getOperand(i);
1361 if (!MO.isReg() || MO.getReg() != Reg)
1362 continue;
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001363 if (Ops)
1364 Ops->push_back(i);
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001365 if (MO.isUse())
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001366 Use |= !MO.isUndef();
Jakob Stoklund Olesen9eb77bf2011-08-19 00:30:17 +00001367 else if (MO.getSubReg() && !MO.isUndef())
1368 // A partial <def,undef> doesn't count as reading the register.
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001369 PartDef = true;
1370 else
1371 FullDef = true;
1372 }
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001373 // A partial redefine uses Reg unless there is also a full define.
1374 return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef);
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001375}
1376
Evan Cheng63254462008-03-05 00:59:57 +00001377/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman72a0bc12008-05-06 00:20:10 +00001378/// the specified register or -1 if it is not found. If isDead is true, defs
1379/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
1380/// also checks if there is a def of a super-register.
Evan Cheng38584512010-05-21 20:53:24 +00001381int
1382MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, bool Overlap,
1383 const TargetRegisterInfo *TRI) const {
1384 bool isPhys = TargetRegisterInfo::isPhysicalRegister(Reg);
Evan Chengf7ed82d2007-02-19 21:49:54 +00001385 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng63254462008-03-05 00:59:57 +00001386 const MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesene7d3f442012-02-14 23:49:37 +00001387 // Accept regmask operands when Overlap is set.
1388 // Ignore them when looking for a specific def operand (Overlap == false).
1389 if (isPhys && Overlap && MO.isRegMask() && MO.clobbersPhysReg(Reg))
1390 return i;
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001391 if (!MO.isReg() || !MO.isDef())
Evan Cheng63254462008-03-05 00:59:57 +00001392 continue;
1393 unsigned MOReg = MO.getReg();
Evan Cheng38584512010-05-21 20:53:24 +00001394 bool Found = (MOReg == Reg);
1395 if (!Found && TRI && isPhys &&
1396 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
1397 if (Overlap)
1398 Found = TRI->regsOverlap(MOReg, Reg);
1399 else
1400 Found = TRI->isSubRegister(MOReg, Reg);
1401 }
1402 if (Found && (!isDead || MO.isDead()))
1403 return i;
Evan Chengf7ed82d2007-02-19 21:49:54 +00001404 }
Evan Cheng63254462008-03-05 00:59:57 +00001405 return -1;
Evan Chengf7ed82d2007-02-19 21:49:54 +00001406}
Evan Cheng4d728b02007-05-15 01:26:09 +00001407
Evan Cheng5983bdb2007-05-29 18:35:22 +00001408/// findFirstPredOperandIdx() - Find the index of the first operand in the
1409/// operand list that is used to represent the predicate. It returns -1 if
1410/// none is found.
1411int MachineInstr::findFirstPredOperandIdx() const {
Jim Grosbached16ec42011-08-29 22:24:09 +00001412 // Don't call MCID.findFirstPredOperandIdx() because this variant
1413 // is sometimes called on an instruction that's not yet complete, and
1414 // so the number of operands is less than the MCID indicates. In
1415 // particular, the PTX target does this.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001416 const MCInstrDesc &MCID = getDesc();
1417 if (MCID.isPredicable()) {
Evan Cheng4d728b02007-05-15 01:26:09 +00001418 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Evan Cheng6cc775f2011-06-28 19:10:37 +00001419 if (MCID.OpInfo[i].isPredicate())
Evan Cheng5983bdb2007-05-29 18:35:22 +00001420 return i;
Evan Cheng4d728b02007-05-15 01:26:09 +00001421 }
1422
Evan Cheng5983bdb2007-05-29 18:35:22 +00001423 return -1;
Evan Cheng4d728b02007-05-15 01:26:09 +00001424}
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001425
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001426// MachineOperand::TiedTo is 4 bits wide.
1427const unsigned TiedMax = 15;
1428
1429/// tieOperands - Mark operands at DefIdx and UseIdx as tied to each other.
1430///
1431/// Use and def operands can be tied together, indicated by a non-zero TiedTo
1432/// field. TiedTo can have these values:
1433///
1434/// 0: Operand is not tied to anything.
1435/// 1 to TiedMax-1: Tied to getOperand(TiedTo-1).
1436/// TiedMax: Tied to an operand >= TiedMax-1.
1437///
1438/// The tied def must be one of the first TiedMax operands on a normal
1439/// instruction. INLINEASM instructions allow more tied defs.
1440///
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001441void MachineInstr::tieOperands(unsigned DefIdx, unsigned UseIdx) {
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001442 MachineOperand &DefMO = getOperand(DefIdx);
1443 MachineOperand &UseMO = getOperand(UseIdx);
1444 assert(DefMO.isDef() && "DefIdx must be a def operand");
1445 assert(UseMO.isUse() && "UseIdx must be a use operand");
1446 assert(!DefMO.isTied() && "Def is already tied to another use");
1447 assert(!UseMO.isTied() && "Use is already tied to another def");
1448
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001449 if (DefIdx < TiedMax)
1450 UseMO.TiedTo = DefIdx + 1;
1451 else {
1452 // Inline asm can use the group descriptors to find tied operands, but on
1453 // normal instruction, the tied def must be within the first TiedMax
1454 // operands.
1455 assert(isInlineAsm() && "DefIdx out of range");
1456 UseMO.TiedTo = TiedMax;
1457 }
1458
1459 // UseIdx can be out of range, we'll search for it in findTiedOperandIdx().
1460 DefMO.TiedTo = std::min(UseIdx + 1, TiedMax);
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001461}
1462
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001463/// Given the index of a tied register operand, find the operand it is tied to.
1464/// Defs are tied to uses and vice versa. Returns the index of the tied operand
1465/// which must exist.
1466unsigned MachineInstr::findTiedOperandIdx(unsigned OpIdx) const {
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001467 const MachineOperand &MO = getOperand(OpIdx);
1468 assert(MO.isTied() && "Operand isn't tied");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001469
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001470 // Normally TiedTo is in range.
1471 if (MO.TiedTo < TiedMax)
1472 return MO.TiedTo - 1;
1473
1474 // Uses on normal instructions can be out of range.
1475 if (!isInlineAsm()) {
1476 // Normal tied defs must be in the 0..TiedMax-1 range.
1477 if (MO.isUse())
1478 return TiedMax - 1;
1479 // MO is a def. Search for the tied use.
1480 for (unsigned i = TiedMax - 1, e = getNumOperands(); i != e; ++i) {
1481 const MachineOperand &UseMO = getOperand(i);
1482 if (UseMO.isReg() && UseMO.isUse() && UseMO.TiedTo == OpIdx + 1)
1483 return i;
1484 }
1485 llvm_unreachable("Can't find tied use");
1486 }
1487
1488 // Now deal with inline asm by parsing the operand group descriptor flags.
1489 // Find the beginning of each operand group.
1490 SmallVector<unsigned, 8> GroupIdx;
1491 unsigned OpIdxGroup = ~0u;
1492 unsigned NumOps;
1493 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
1494 i += NumOps) {
1495 const MachineOperand &FlagMO = getOperand(i);
1496 assert(FlagMO.isImm() && "Invalid tied operand on inline asm");
1497 unsigned CurGroup = GroupIdx.size();
1498 GroupIdx.push_back(i);
1499 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
1500 // OpIdx belongs to this operand group.
1501 if (OpIdx > i && OpIdx < i + NumOps)
1502 OpIdxGroup = CurGroup;
1503 unsigned TiedGroup;
1504 if (!InlineAsm::isUseOperandTiedToDef(FlagMO.getImm(), TiedGroup))
1505 continue;
1506 // Operands in this group are tied to operands in TiedGroup which must be
1507 // earlier. Find the number of operands between the two groups.
1508 unsigned Delta = i - GroupIdx[TiedGroup];
1509
1510 // OpIdx is a use tied to TiedGroup.
1511 if (OpIdxGroup == CurGroup)
1512 return OpIdx - Delta;
1513
1514 // OpIdx is a def tied to this use group.
1515 if (OpIdxGroup == TiedGroup)
1516 return OpIdx + Delta;
1517 }
1518 llvm_unreachable("Invalid tied operand on inline asm");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001519}
1520
Dan Gohmanc90f51c2010-05-13 20:34:42 +00001521/// clearKillInfo - Clears kill flags on all operands.
1522///
1523void MachineInstr::clearKillInfo() {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001524 for (MachineOperand &MO : operands()) {
Dan Gohmanc90f51c2010-05-13 20:34:42 +00001525 if (MO.isReg() && MO.isUse())
1526 MO.setIsKill(false);
1527 }
1528}
1529
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001530void MachineInstr::substituteRegister(unsigned FromReg,
1531 unsigned ToReg,
1532 unsigned SubIdx,
1533 const TargetRegisterInfo &RegInfo) {
1534 if (TargetRegisterInfo::isPhysicalRegister(ToReg)) {
1535 if (SubIdx)
1536 ToReg = RegInfo.getSubReg(ToReg, SubIdx);
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001537 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001538 if (!MO.isReg() || MO.getReg() != FromReg)
1539 continue;
1540 MO.substPhysReg(ToReg, RegInfo);
1541 }
1542 } else {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001543 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001544 if (!MO.isReg() || MO.getReg() != FromReg)
1545 continue;
1546 MO.substVirtReg(ToReg, SubIdx, RegInfo);
1547 }
1548 }
1549}
1550
Evan Cheng7d98a482008-07-03 09:09:37 +00001551/// isSafeToMove - Return true if it is safe to move this instruction. If
1552/// SawStore is set to true, it means that there is a store (or call) between
1553/// the instruction's location and its intended destination.
Matthias Braun07066cc2015-05-19 21:22:20 +00001554bool MachineInstr::isSafeToMove(AliasAnalysis *AA, bool &SawStore) const {
Evan Cheng399e1102008-03-13 00:44:09 +00001555 // Ignore stuff that we obviously can't move.
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001556 //
1557 // Treat volatile loads as stores. This is not strictly necessary for
Jakob Stoklund Olesend92e2bc2012-09-04 18:44:43 +00001558 // volatiles, but it is required for atomic loads. It is not allowed to move
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001559 // a load across an atomic load with Ordering > Monotonic.
1560 if (mayStore() || isCall() ||
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001561 (mayLoad() && hasOrderedMemoryRef())) {
Evan Cheng399e1102008-03-13 00:44:09 +00001562 SawStore = true;
1563 return false;
1564 }
Evan Cheng0638c202011-01-07 21:08:26 +00001565
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001566 if (isPosition() || isDebugValue() || isTerminator() ||
1567 hasUnmodeledSideEffects())
Evan Cheng399e1102008-03-13 00:44:09 +00001568 return false;
1569
1570 // See if this instruction does a load. If so, we have to guarantee that the
1571 // loaded value doesn't change between the load and the its intended
1572 // destination. The check for isInvariantLoad gives the targe the chance to
1573 // classify the load as always returning a constant, e.g. a constant pool
1574 // load.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001575 if (mayLoad() && !isInvariantLoad(AA))
Evan Cheng399e1102008-03-13 00:44:09 +00001576 // Otherwise, this is a real load. If there is a store between the load and
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001577 // end of block, we can't move it.
1578 return !SawStore;
Dan Gohman7c59ed62008-09-24 00:06:15 +00001579
Evan Cheng399e1102008-03-13 00:44:09 +00001580 return true;
1581}
1582
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001583/// hasOrderedMemoryRef - Return true if this instruction may have an ordered
1584/// or volatile memory reference, or if the information describing the memory
1585/// reference is not available. Return false if it is known to have no ordered
1586/// memory references.
1587bool MachineInstr::hasOrderedMemoryRef() const {
Dan Gohman7c59ed62008-09-24 00:06:15 +00001588 // An instruction known never to access memory won't have a volatile access.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001589 if (!mayStore() &&
1590 !mayLoad() &&
1591 !isCall() &&
Evan Cheng6eb516d2011-01-07 23:50:32 +00001592 !hasUnmodeledSideEffects())
Dan Gohman7c59ed62008-09-24 00:06:15 +00001593 return false;
1594
1595 // Otherwise, if the instruction has no memory reference information,
1596 // conservatively assume it wasn't preserved.
1597 if (memoperands_empty())
1598 return true;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001599
Justin Lebardede81e2016-07-13 22:35:19 +00001600 // Check if any of our memory operands are ordered.
1601 return any_of(memoperands(), [](const MachineMemOperand *MMO) {
1602 return !MMO->isUnordered();
1603 });
Dan Gohman7c59ed62008-09-24 00:06:15 +00001604}
1605
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001606/// isInvariantLoad - Return true if this instruction is loading from a
1607/// location whose value is invariant across the function. For example,
Dan Gohman4a618822010-02-10 16:03:48 +00001608/// loading a value from the constant pool or from the argument area
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001609/// of a function if it does not change. This should only return true of
1610/// *all* loads the instruction does are invariant (if it does multiple loads).
1611bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const {
1612 // If the instruction doesn't load at all, it isn't an invariant load.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001613 if (!mayLoad())
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001614 return false;
1615
1616 // If the instruction has lost its memoperands, conservatively assume that
1617 // it may not be an invariant load.
1618 if (memoperands_empty())
1619 return false;
1620
Matthias Braun941a7052016-07-28 18:40:00 +00001621 const MachineFrameInfo &MFI = getParent()->getParent()->getFrameInfo();
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001622
Justin Lebardede81e2016-07-13 22:35:19 +00001623 for (MachineMemOperand *MMO : memoperands()) {
1624 if (MMO->isVolatile()) return false;
1625 if (MMO->isStore()) return false;
1626 if (MMO->isInvariant()) continue;
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001627
1628 // A load from a constant PseudoSourceValue is invariant.
Justin Lebardede81e2016-07-13 22:35:19 +00001629 if (const PseudoSourceValue *PSV = MMO->getPseudoValue())
Matthias Braun941a7052016-07-28 18:40:00 +00001630 if (PSV->isConstant(&MFI))
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001631 continue;
1632
Justin Lebardede81e2016-07-13 22:35:19 +00001633 if (const Value *V = MMO->getValue()) {
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001634 // If we have an AliasAnalysis, ask it whether the memory is constant.
Chandler Carruthac80dc72015-06-17 07:18:54 +00001635 if (AA &&
1636 AA->pointsToConstantMemory(
Justin Lebardede81e2016-07-13 22:35:19 +00001637 MemoryLocation(V, MMO->getSize(), MMO->getAAInfo())))
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001638 continue;
1639 }
1640
1641 // Otherwise assume conservatively.
1642 return false;
1643 }
1644
1645 // Everything checks out.
1646 return true;
1647}
1648
Evan Cheng71453822009-12-03 02:31:43 +00001649/// isConstantValuePHI - If the specified instruction is a PHI that always
1650/// merges together the same virtual register, return the register, otherwise
1651/// return 0.
1652unsigned MachineInstr::isConstantValuePHI() const {
Chris Lattnerb06015a2010-02-09 19:54:29 +00001653 if (!isPHI())
Evan Cheng71453822009-12-03 02:31:43 +00001654 return 0;
Evan Cheng5c668a22009-12-07 23:10:34 +00001655 assert(getNumOperands() >= 3 &&
1656 "It's illegal to have a PHI without source operands");
Evan Cheng71453822009-12-03 02:31:43 +00001657
1658 unsigned Reg = getOperand(1).getReg();
1659 for (unsigned i = 3, e = getNumOperands(); i < e; i += 2)
1660 if (getOperand(i).getReg() != Reg)
1661 return 0;
1662 return Reg;
1663}
1664
Evan Cheng6eb516d2011-01-07 23:50:32 +00001665bool MachineInstr::hasUnmodeledSideEffects() const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00001666 if (hasProperty(MCID::UnmodeledSideEffects))
Evan Cheng6eb516d2011-01-07 23:50:32 +00001667 return true;
1668 if (isInlineAsm()) {
1669 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1670 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1671 return true;
1672 }
1673
1674 return false;
1675}
1676
Michael Kupersteinbc7f99a2015-08-12 10:14:58 +00001677bool MachineInstr::isLoadFoldBarrier() const {
1678 return mayStore() || isCall() || hasUnmodeledSideEffects();
1679}
1680
Evan Chengb083c472010-04-08 20:02:37 +00001681/// allDefsAreDead - Return true if all the defs of this instruction are dead.
1682///
1683bool MachineInstr::allDefsAreDead() const {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001684 for (const MachineOperand &MO : operands()) {
Evan Chengb083c472010-04-08 20:02:37 +00001685 if (!MO.isReg() || MO.isUse())
1686 continue;
1687 if (!MO.isDead())
1688 return false;
1689 }
1690 return true;
1691}
1692
Evan Cheng21eedfb2010-10-22 21:49:09 +00001693/// copyImplicitOps - Copy implicit register operands from specified
1694/// instruction to this instruction.
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001695void MachineInstr::copyImplicitOps(MachineFunction &MF,
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001696 const MachineInstr &MI) {
1697 for (unsigned i = MI.getDesc().getNumOperands(), e = MI.getNumOperands();
Evan Cheng21eedfb2010-10-22 21:49:09 +00001698 i != e; ++i) {
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001699 const MachineOperand &MO = MI.getOperand(i);
Lang Hames7c8189c2014-03-17 01:22:54 +00001700 if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask())
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001701 addOperand(MF, MO);
Evan Cheng21eedfb2010-10-22 21:49:09 +00001702 }
1703}
1704
Yaron Kereneb2a2542016-01-29 20:50:44 +00001705LLVM_DUMP_METHOD void MachineInstr::dump() const {
Manman Ren19f49ac2012-09-11 22:23:19 +00001706#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
David Greene29388d62010-01-04 23:48:20 +00001707 dbgs() << " " << *this;
Manman Ren742534c2012-09-06 19:06:06 +00001708#endif
Mon P Wangdfcc1ff2008-10-10 01:43:55 +00001709}
1710
Eric Christopher1cdefae2015-02-27 00:11:34 +00001711void MachineInstr::print(raw_ostream &OS, bool SkipOpers) const {
Duncan P. N. Exon Smithc0374522015-06-26 23:18:44 +00001712 const Module *M = nullptr;
1713 if (const MachineBasicBlock *MBB = getParent())
1714 if (const MachineFunction *MF = MBB->getParent())
1715 M = MF->getFunction()->getParent();
1716
1717 ModuleSlotTracker MST(M);
1718 print(OS, MST, SkipOpers);
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001719}
1720
1721void MachineInstr::print(raw_ostream &OS, ModuleSlotTracker &MST,
1722 bool SkipOpers) const {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001723 // We can be a bit tidier if we know the MachineFunction.
Craig Topperc0196b12014-04-14 00:51:57 +00001724 const MachineFunction *MF = nullptr;
Eric Christopher1cdefae2015-02-27 00:11:34 +00001725 const TargetRegisterInfo *TRI = nullptr;
Craig Topperc0196b12014-04-14 00:51:57 +00001726 const MachineRegisterInfo *MRI = nullptr;
Eric Christopher1cdefae2015-02-27 00:11:34 +00001727 const TargetInstrInfo *TII = nullptr;
Tim Northover6b3bd612016-07-29 20:32:59 +00001728 const TargetIntrinsicInfo *IntrinsicInfo = nullptr;
1729
Dan Gohman2745d192009-11-09 19:38:45 +00001730 if (const MachineBasicBlock *MBB = getParent()) {
1731 MF = MBB->getParent();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001732 if (MF) {
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001733 MRI = &MF->getRegInfo();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001734 TRI = MF->getSubtarget().getRegisterInfo();
1735 TII = MF->getSubtarget().getInstrInfo();
Tim Northover6b3bd612016-07-29 20:32:59 +00001736 IntrinsicInfo = MF->getTarget().getIntrinsicInfo();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001737 }
Dan Gohman2745d192009-11-09 19:38:45 +00001738 }
Dan Gohman34341e62009-10-31 20:19:03 +00001739
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001740 // Save a list of virtual registers.
1741 SmallVector<unsigned, 8> VirtRegs;
1742
Dan Gohman34341e62009-10-31 20:19:03 +00001743 // Print explicitly defined operands on the left of an assignment syntax.
Dan Gohman2745d192009-11-09 19:38:45 +00001744 unsigned StartOp = 0, e = getNumOperands();
Dan Gohman34341e62009-10-31 20:19:03 +00001745 for (; StartOp < e && getOperand(StartOp).isReg() &&
1746 getOperand(StartOp).isDef() &&
1747 !getOperand(StartOp).isImplicit();
1748 ++StartOp) {
1749 if (StartOp != 0) OS << ", ";
Tim Northover6b3bd612016-07-29 20:32:59 +00001750 getOperand(StartOp).print(OS, MST, TRI, IntrinsicInfo);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001751 unsigned Reg = getOperand(StartOp).getReg();
Quentin Colombet36ce1b02016-02-10 23:43:48 +00001752 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001753 VirtRegs.push_back(Reg);
Quentin Colombet36ce1b02016-02-10 23:43:48 +00001754 unsigned Size;
Quentin Colombet03c41962016-04-07 23:18:11 +00001755 if (MRI && (Size = MRI->getSize(Reg)))
Quentin Colombet36ce1b02016-02-10 23:43:48 +00001756 OS << '(' << Size << ')';
Quentin Colombet36ce1b02016-02-10 23:43:48 +00001757 }
Chris Lattnerac6e9742002-10-30 01:55:38 +00001758 }
Tanya Lattner23dbc812004-06-25 00:13:11 +00001759
Dan Gohman34341e62009-10-31 20:19:03 +00001760 if (StartOp != 0)
1761 OS << " = ";
1762
1763 // Print the opcode name.
Eric Christopher1cdefae2015-02-27 00:11:34 +00001764 if (TII)
1765 OS << TII->getName(getOpcode());
Benjamin Kramerbf152d52012-02-10 13:18:44 +00001766 else
1767 OS << "UNKNOWN";
Misha Brukman835702a2005-04-21 22:36:52 +00001768
Tim Northover98a56eb2016-07-22 22:13:36 +00001769 if (getNumTypes() > 0) {
1770 OS << " { ";
1771 for (unsigned i = 0; i < getNumTypes(); ++i) {
1772 getType(i).print(OS);
1773 if (i + 1 != getNumTypes())
1774 OS << ", ";
1775 }
1776 OS << " } ";
Quentin Colombet41bea872016-03-07 22:47:23 +00001777 }
Quentin Colombet98551112016-02-11 18:22:37 +00001778
Andrew Trickb36388a2013-01-25 07:45:25 +00001779 if (SkipOpers)
1780 return;
1781
Dan Gohman34341e62009-10-31 20:19:03 +00001782 // Print the rest of the operands.
Dan Gohman2745d192009-11-09 19:38:45 +00001783 bool OmittedAnyCallClobbers = false;
1784 bool FirstOp = true;
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001785 unsigned AsmDescOp = ~0u;
1786 unsigned AsmOpCount = 0;
Evan Cheng6eb516d2011-01-07 23:50:32 +00001787
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +00001788 if (isInlineAsm() && e >= InlineAsm::MIOp_FirstOperand) {
Evan Cheng6eb516d2011-01-07 23:50:32 +00001789 // Print asm string.
1790 OS << " ";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001791 getOperand(InlineAsm::MIOp_AsmString).print(OS, MST, TRI);
Evan Cheng6eb516d2011-01-07 23:50:32 +00001792
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001793 // Print HasSideEffects, MayLoad, MayStore, IsAlignStack
Evan Cheng6eb516d2011-01-07 23:50:32 +00001794 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1795 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1796 OS << " [sideeffect]";
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001797 if (ExtraInfo & InlineAsm::Extra_MayLoad)
1798 OS << " [mayload]";
1799 if (ExtraInfo & InlineAsm::Extra_MayStore)
1800 OS << " [maystore]";
Wei Ding0526e7f2016-06-22 18:51:08 +00001801 if (ExtraInfo & InlineAsm::Extra_IsConvergent)
1802 OS << " [isconvergent]";
Evan Cheng6eb516d2011-01-07 23:50:32 +00001803 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
1804 OS << " [alignstack]";
Chad Rosiercbd2a192012-09-05 22:17:43 +00001805 if (getInlineAsmDialect() == InlineAsm::AD_ATT)
Chad Rosier994f4042012-09-05 21:00:58 +00001806 OS << " [attdialect]";
Chad Rosiercbd2a192012-09-05 22:17:43 +00001807 if (getInlineAsmDialect() == InlineAsm::AD_Intel)
Chad Rosier994f4042012-09-05 21:00:58 +00001808 OS << " [inteldialect]";
Evan Cheng6eb516d2011-01-07 23:50:32 +00001809
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001810 StartOp = AsmDescOp = InlineAsm::MIOp_FirstOperand;
Evan Cheng6eb516d2011-01-07 23:50:32 +00001811 FirstOp = false;
1812 }
1813
Chris Lattnerac6e9742002-10-30 01:55:38 +00001814 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohman2745d192009-11-09 19:38:45 +00001815 const MachineOperand &MO = getOperand(i);
1816
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001817 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001818 VirtRegs.push_back(MO.getReg());
1819
Dan Gohman2745d192009-11-09 19:38:45 +00001820 // Omit call-clobbered registers which aren't used anywhere. This makes
1821 // call instructions much less noisy on targets where calls clobber lots
1822 // of registers. Don't rely on MO.isDead() because we may be called before
1823 // LiveVariables is run, or we may be looking at a non-allocatable reg.
Craig Toppercf0444b2014-11-17 05:50:14 +00001824 if (MRI && isCall() &&
Dan Gohman2745d192009-11-09 19:38:45 +00001825 MO.isReg() && MO.isImplicit() && MO.isDef()) {
1826 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001827 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Craig Toppercf0444b2014-11-17 05:50:14 +00001828 if (MRI->use_empty(Reg)) {
Dan Gohman2745d192009-11-09 19:38:45 +00001829 bool HasAliasLive = false;
Eric Christopher1cdefae2015-02-27 00:11:34 +00001830 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001831 unsigned AliasReg = *AI;
Craig Toppercf0444b2014-11-17 05:50:14 +00001832 if (!MRI->use_empty(AliasReg)) {
Dan Gohman2745d192009-11-09 19:38:45 +00001833 HasAliasLive = true;
1834 break;
1835 }
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001836 }
Dan Gohman2745d192009-11-09 19:38:45 +00001837 if (!HasAliasLive) {
1838 OmittedAnyCallClobbers = true;
1839 continue;
1840 }
1841 }
1842 }
1843 }
1844
1845 if (FirstOp) FirstOp = false; else OS << ",";
Chris Lattnerac6e9742002-10-30 01:55:38 +00001846 OS << " ";
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001847 if (i < getDesc().NumOperands) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001848 const MCOperandInfo &MCOI = getDesc().OpInfo[i];
1849 if (MCOI.isPredicate())
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001850 OS << "pred:";
Evan Cheng6cc775f2011-06-28 19:10:37 +00001851 if (MCOI.isOptionalDef())
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001852 OS << "opt:";
1853 }
Evan Chengd4d1a512010-04-28 20:03:13 +00001854 if (isDebugValue() && MO.isMetadata()) {
1855 // Pretty print DBG_VALUE instructions.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001856 auto *DIV = dyn_cast<DILocalVariable>(MO.getMetadata());
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +00001857 if (DIV && !DIV->getName().empty())
1858 OS << "!\"" << DIV->getName() << '\"';
Evan Chengd4d1a512010-04-28 20:03:13 +00001859 else
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001860 MO.print(OS, MST, TRI);
Eric Christopher1cdefae2015-02-27 00:11:34 +00001861 } else if (TRI && (isInsertSubreg() || isRegSequence()) && MO.isImm()) {
1862 OS << TRI->getSubRegIndexName(MO.getImm());
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001863 } else if (i == AsmDescOp && MO.isImm()) {
1864 // Pretty print the inline asm operand descriptor.
1865 OS << '$' << AsmOpCount++;
1866 unsigned Flag = MO.getImm();
1867 switch (InlineAsm::getKind(Flag)) {
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001868 case InlineAsm::Kind_RegUse: OS << ":[reguse"; break;
1869 case InlineAsm::Kind_RegDef: OS << ":[regdef"; break;
1870 case InlineAsm::Kind_RegDefEarlyClobber: OS << ":[regdef-ec"; break;
1871 case InlineAsm::Kind_Clobber: OS << ":[clobber"; break;
1872 case InlineAsm::Kind_Imm: OS << ":[imm"; break;
1873 case InlineAsm::Kind_Mem: OS << ":[mem"; break;
1874 default: OS << ":[??" << InlineAsm::getKind(Flag); break;
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001875 }
1876
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001877 unsigned RCID = 0;
Simon Dardisd32a2d32016-07-18 13:17:31 +00001878 if (!InlineAsm::isImmKind(Flag) && !InlineAsm::isMemKind(Flag) &&
1879 InlineAsm::hasRegClassConstraint(Flag, RCID)) {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001880 if (TRI) {
1881 OS << ':' << TRI->getRegClassName(TRI->getRegClass(RCID));
Craig Toppercf0444b2014-11-17 05:50:14 +00001882 } else
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001883 OS << ":RC" << RCID;
Nick Lewycky84882252011-10-13 00:54:59 +00001884 }
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001885
Simon Dardisd32a2d32016-07-18 13:17:31 +00001886 if (InlineAsm::isMemKind(Flag)) {
1887 unsigned MCID = InlineAsm::getMemoryConstraintID(Flag);
1888 switch (MCID) {
1889 case InlineAsm::Constraint_es: OS << ":es"; break;
1890 case InlineAsm::Constraint_i: OS << ":i"; break;
1891 case InlineAsm::Constraint_m: OS << ":m"; break;
1892 case InlineAsm::Constraint_o: OS << ":o"; break;
1893 case InlineAsm::Constraint_v: OS << ":v"; break;
1894 case InlineAsm::Constraint_Q: OS << ":Q"; break;
1895 case InlineAsm::Constraint_R: OS << ":R"; break;
1896 case InlineAsm::Constraint_S: OS << ":S"; break;
1897 case InlineAsm::Constraint_T: OS << ":T"; break;
1898 case InlineAsm::Constraint_Um: OS << ":Um"; break;
1899 case InlineAsm::Constraint_Un: OS << ":Un"; break;
1900 case InlineAsm::Constraint_Uq: OS << ":Uq"; break;
1901 case InlineAsm::Constraint_Us: OS << ":Us"; break;
1902 case InlineAsm::Constraint_Ut: OS << ":Ut"; break;
1903 case InlineAsm::Constraint_Uv: OS << ":Uv"; break;
1904 case InlineAsm::Constraint_Uy: OS << ":Uy"; break;
1905 case InlineAsm::Constraint_X: OS << ":X"; break;
1906 case InlineAsm::Constraint_Z: OS << ":Z"; break;
1907 case InlineAsm::Constraint_ZC: OS << ":ZC"; break;
1908 case InlineAsm::Constraint_Zy: OS << ":Zy"; break;
1909 default: OS << ":?"; break;
1910 }
1911 }
1912
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001913 unsigned TiedTo = 0;
1914 if (InlineAsm::isUseOperandTiedToDef(Flag, TiedTo))
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001915 OS << " tiedto:$" << TiedTo;
1916
1917 OS << ']';
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001918
1919 // Compute the index of the next operand descriptor.
1920 AsmDescOp += 1 + InlineAsm::getNumOperandRegisters(Flag);
Evan Chengd4d1a512010-04-28 20:03:13 +00001921 } else
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001922 MO.print(OS, MST, TRI);
Dan Gohman2745d192009-11-09 19:38:45 +00001923 }
1924
1925 // Briefly indicate whether any call clobbers were omitted.
1926 if (OmittedAnyCallClobbers) {
Bill Wendlingec030f22009-12-25 13:45:50 +00001927 if (!FirstOp) OS << ",";
Dan Gohman2745d192009-11-09 19:38:45 +00001928 OS << " ...";
Chris Lattner214808f2002-10-30 00:48:05 +00001929 }
Misha Brukman835702a2005-04-21 22:36:52 +00001930
Dan Gohman34341e62009-10-31 20:19:03 +00001931 bool HaveSemi = false;
Michael Kuperstein098cd9f2015-09-16 11:18:25 +00001932 const unsigned PrintableFlags = FrameSetup | FrameDestroy;
Jakob Stoklund Olesen6922e9c2013-01-09 18:35:09 +00001933 if (Flags & PrintableFlags) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001934 if (!HaveSemi) {
1935 OS << ";";
1936 HaveSemi = true;
1937 }
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001938 OS << " flags: ";
1939
1940 if (Flags & FrameSetup)
1941 OS << "FrameSetup";
Michael Kuperstein098cd9f2015-09-16 11:18:25 +00001942
1943 if (Flags & FrameDestroy)
1944 OS << "FrameDestroy";
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001945 }
1946
Dan Gohman3b460302008-07-07 23:14:23 +00001947 if (!memoperands_empty()) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001948 if (!HaveSemi) {
1949 OS << ";";
1950 HaveSemi = true;
1951 }
Dan Gohman34341e62009-10-31 20:19:03 +00001952
1953 OS << " mem:";
Dan Gohman48b185d2009-09-25 20:36:54 +00001954 for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
1955 i != e; ++i) {
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001956 (*i)->print(OS, MST);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001957 if (std::next(i) != e)
Dan Gohmanc0353bf2009-09-23 01:33:16 +00001958 OS << " ";
Dan Gohman2d489b52008-02-06 22:27:42 +00001959 }
1960 }
1961
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001962 // Print the regclass of any virtual registers encountered.
1963 if (MRI && !VirtRegs.empty()) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001964 if (!HaveSemi) {
1965 OS << ";";
1966 HaveSemi = true;
1967 }
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001968 for (unsigned i = 0; i != VirtRegs.size(); ++i) {
Quentin Colombet03c41962016-04-07 23:18:11 +00001969 const RegClassOrRegBank &RC = MRI->getRegClassOrRegBank(VirtRegs[i]);
Quentin Colombete1494c32016-02-11 00:19:17 +00001970 if (!RC)
1971 continue;
Quentin Colombet03c41962016-04-07 23:18:11 +00001972 // Generic virtual registers do not have register classes.
1973 if (RC.is<const RegisterBank *>())
1974 OS << " " << RC.get<const RegisterBank *>()->getName();
1975 else
1976 OS << " "
1977 << TRI->getRegClassName(RC.get<const TargetRegisterClass *>());
1978 OS << ':' << PrintReg(VirtRegs[i]);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001979 for (unsigned j = i+1; j != VirtRegs.size();) {
Quentin Colombet03c41962016-04-07 23:18:11 +00001980 if (MRI->getRegClassOrRegBank(VirtRegs[j]) != RC) {
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001981 ++j;
1982 continue;
1983 }
1984 if (VirtRegs[i] != VirtRegs[j])
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +00001985 OS << "," << PrintReg(VirtRegs[j]);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001986 VirtRegs.erase(VirtRegs.begin()+j);
1987 }
1988 }
1989 }
1990
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001991 // Print debug location information.
Duncan P. N. Exon Smithc5bd3e02015-04-03 16:23:04 +00001992 if (isDebugValue() && getOperand(e - 2).isMetadata()) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001993 if (!HaveSemi)
1994 OS << ";";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001995 auto *DV = cast<DILocalVariable>(getOperand(e - 2).getMetadata());
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +00001996 OS << " line no:" << DV->getLine();
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001997 if (auto *InlinedAt = debugLoc->getInlinedAt()) {
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +00001998 DebugLoc InlinedAtDL(InlinedAt);
1999 if (InlinedAtDL && MF) {
Devang Pateld61b1d52011-08-04 20:44:26 +00002000 OS << " inlined @[ ";
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002001 InlinedAtDL.print(OS);
Devang Pateld61b1d52011-08-04 20:44:26 +00002002 OS << " ]";
2003 }
2004 }
Adrian Prantl87b7eb92014-10-01 18:55:02 +00002005 if (isIndirectDebugValue())
2006 OS << " indirect";
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +00002007 } else if (debugLoc && MF) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00002008 if (!HaveSemi)
2009 OS << ";";
Dan Gohman2e3f1872009-11-23 21:29:08 +00002010 OS << " dbg:";
Eric Christopherb9f00092015-02-26 23:32:17 +00002011 debugLoc.print(OS);
Bill Wendling1a0a3d02009-02-19 21:44:55 +00002012 }
2013
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00002014 OS << '\n';
Chris Lattner214808f2002-10-30 00:48:05 +00002015}
2016
Owen Anderson2a8a4852008-01-24 01:10:07 +00002017bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00002018 const TargetRegisterInfo *RegInfo,
Owen Anderson2a8a4852008-01-24 01:10:07 +00002019 bool AddIfNotFound) {
Evan Cheng6c177732008-04-16 09:41:59 +00002020 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00002021 bool hasAliases = isPhysReg &&
2022 MCRegAliasIterator(IncomingReg, RegInfo, false).isValid();
Dan Gohmanc7367b42008-09-03 15:56:16 +00002023 bool Found = false;
Evan Cheng6c177732008-04-16 09:41:59 +00002024 SmallVector<unsigned,4> DeadOps;
Bill Wendling7921ad02008-03-03 22:14:33 +00002025 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2026 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesenf465f062009-08-04 20:09:25 +00002027 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng6c177732008-04-16 09:41:59 +00002028 continue;
Mandeep Singh Grange5a2f112016-05-10 17:57:27 +00002029
2030 // DEBUG_VALUE nodes do not contribute to code generation and should
2031 // always be ignored. Failure to do so may result in trying to modify
2032 // KILL flags on DEBUG_VALUE nodes.
2033 if (MO.isDebug())
2034 continue;
2035
Evan Cheng6c177732008-04-16 09:41:59 +00002036 unsigned Reg = MO.getReg();
2037 if (!Reg)
2038 continue;
Bill Wendling7921ad02008-03-03 22:14:33 +00002039
Evan Cheng6c177732008-04-16 09:41:59 +00002040 if (Reg == IncomingReg) {
Dan Gohmanc7367b42008-09-03 15:56:16 +00002041 if (!Found) {
2042 if (MO.isKill())
2043 // The register is already marked kill.
2044 return true;
Jakob Stoklund Olesenc59cd9b2009-08-02 19:13:03 +00002045 if (isPhysReg && isRegTiedToDefOperand(i))
2046 // Two-address uses of physregs must not be marked kill.
2047 return true;
Dan Gohmanc7367b42008-09-03 15:56:16 +00002048 MO.setIsKill();
2049 Found = true;
2050 }
2051 } else if (hasAliases && MO.isKill() &&
2052 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng6c177732008-04-16 09:41:59 +00002053 // A super-register kill already exists.
2054 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohmanb2612922008-07-03 01:18:51 +00002055 return true;
2056 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng6c177732008-04-16 09:41:59 +00002057 DeadOps.push_back(i);
Bill Wendling7921ad02008-03-03 22:14:33 +00002058 }
2059 }
2060
Evan Cheng6c177732008-04-16 09:41:59 +00002061 // Trim unneeded kill operands.
2062 while (!DeadOps.empty()) {
2063 unsigned OpIdx = DeadOps.back();
2064 if (getOperand(OpIdx).isImplicit())
2065 RemoveOperand(OpIdx);
2066 else
2067 getOperand(OpIdx).setIsKill(false);
2068 DeadOps.pop_back();
2069 }
2070
Bill Wendling7921ad02008-03-03 22:14:33 +00002071 // If not found, this means an alias of one of the operands is killed. Add a
Owen Anderson2a8a4852008-01-24 01:10:07 +00002072 // new implicit operand if required.
Dan Gohmanc7367b42008-09-03 15:56:16 +00002073 if (!Found && AddIfNotFound) {
Bill Wendling7921ad02008-03-03 22:14:33 +00002074 addOperand(MachineOperand::CreateReg(IncomingReg,
2075 false /*IsDef*/,
2076 true /*IsImp*/,
2077 true /*IsKill*/));
Owen Anderson2a8a4852008-01-24 01:10:07 +00002078 return true;
2079 }
Dan Gohmanc7367b42008-09-03 15:56:16 +00002080 return Found;
Owen Anderson2a8a4852008-01-24 01:10:07 +00002081}
2082
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00002083void MachineInstr::clearRegisterKills(unsigned Reg,
2084 const TargetRegisterInfo *RegInfo) {
2085 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topperc0196b12014-04-14 00:51:57 +00002086 RegInfo = nullptr;
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002087 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00002088 if (!MO.isReg() || !MO.isUse() || !MO.isKill())
2089 continue;
2090 unsigned OpReg = MO.getReg();
Matthias Braunaca625a2016-02-24 19:21:48 +00002091 if ((RegInfo && RegInfo->regsOverlap(Reg, OpReg)) || Reg == OpReg)
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00002092 MO.setIsKill(false);
2093 }
2094}
2095
Matthias Braun1965bfa2013-10-10 21:28:38 +00002096bool MachineInstr::addRegisterDead(unsigned Reg,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00002097 const TargetRegisterInfo *RegInfo,
Owen Anderson2a8a4852008-01-24 01:10:07 +00002098 bool AddIfNotFound) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00002099 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(Reg);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00002100 bool hasAliases = isPhysReg &&
Matthias Braun1965bfa2013-10-10 21:28:38 +00002101 MCRegAliasIterator(Reg, RegInfo, false).isValid();
Dan Gohmanc7367b42008-09-03 15:56:16 +00002102 bool Found = false;
Evan Cheng6c177732008-04-16 09:41:59 +00002103 SmallVector<unsigned,4> DeadOps;
Owen Anderson2a8a4852008-01-24 01:10:07 +00002104 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2105 MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00002106 if (!MO.isReg() || !MO.isDef())
Evan Cheng6c177732008-04-16 09:41:59 +00002107 continue;
Matthias Braun1965bfa2013-10-10 21:28:38 +00002108 unsigned MOReg = MO.getReg();
2109 if (!MOReg)
Dan Gohmanc7367b42008-09-03 15:56:16 +00002110 continue;
2111
Matthias Braun1965bfa2013-10-10 21:28:38 +00002112 if (MOReg == Reg) {
Jakob Stoklund Olesen76ad3de2011-04-05 16:53:50 +00002113 MO.setIsDead();
2114 Found = true;
Dan Gohmanc7367b42008-09-03 15:56:16 +00002115 } else if (hasAliases && MO.isDead() &&
Matthias Braun1965bfa2013-10-10 21:28:38 +00002116 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
Evan Cheng6c177732008-04-16 09:41:59 +00002117 // There exists a super-register that's marked dead.
Matthias Braun1965bfa2013-10-10 21:28:38 +00002118 if (RegInfo->isSuperRegister(Reg, MOReg))
Dan Gohmanb2612922008-07-03 01:18:51 +00002119 return true;
Matthias Braun1965bfa2013-10-10 21:28:38 +00002120 if (RegInfo->isSubRegister(Reg, MOReg))
Evan Cheng6c177732008-04-16 09:41:59 +00002121 DeadOps.push_back(i);
Owen Anderson2a8a4852008-01-24 01:10:07 +00002122 }
2123 }
2124
Evan Cheng6c177732008-04-16 09:41:59 +00002125 // Trim unneeded dead operands.
2126 while (!DeadOps.empty()) {
2127 unsigned OpIdx = DeadOps.back();
2128 if (getOperand(OpIdx).isImplicit())
2129 RemoveOperand(OpIdx);
2130 else
2131 getOperand(OpIdx).setIsDead(false);
2132 DeadOps.pop_back();
2133 }
2134
Dan Gohmanc7367b42008-09-03 15:56:16 +00002135 // If not found, this means an alias of one of the operands is dead. Add a
2136 // new implicit operand if required.
Chris Lattnerfd682802009-06-24 17:54:48 +00002137 if (Found || !AddIfNotFound)
2138 return Found;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00002139
Matthias Braun1965bfa2013-10-10 21:28:38 +00002140 addOperand(MachineOperand::CreateReg(Reg,
Chris Lattnerfd682802009-06-24 17:54:48 +00002141 true /*IsDef*/,
2142 true /*IsImp*/,
2143 false /*IsKill*/,
2144 true /*IsDead*/));
2145 return true;
Owen Anderson2a8a4852008-01-24 01:10:07 +00002146}
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00002147
Matthias Braun26e7ea62015-02-04 19:35:16 +00002148void MachineInstr::clearRegisterDeads(unsigned Reg) {
2149 for (MachineOperand &MO : operands()) {
2150 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg)
2151 continue;
2152 MO.setIsDead(false);
2153 }
2154}
2155
Matthias Braun2c98d0f2015-11-11 00:41:58 +00002156void MachineInstr::setRegisterDefReadUndef(unsigned Reg, bool IsUndef) {
Matthias Braunc1988f32015-01-21 22:55:13 +00002157 for (MachineOperand &MO : operands()) {
2158 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg || MO.getSubReg() == 0)
2159 continue;
Matthias Braun2c98d0f2015-11-11 00:41:58 +00002160 MO.setIsUndef(IsUndef);
Matthias Braunc1988f32015-01-21 22:55:13 +00002161 }
2162}
2163
Matthias Braun1965bfa2013-10-10 21:28:38 +00002164void MachineInstr::addRegisterDefined(unsigned Reg,
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00002165 const TargetRegisterInfo *RegInfo) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00002166 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
2167 MachineOperand *MO = findRegisterDefOperand(Reg, false, RegInfo);
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00002168 if (MO)
2169 return;
2170 } else {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002171 for (const MachineOperand &MO : operands()) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00002172 if (MO.isReg() && MO.getReg() == Reg && MO.isDef() &&
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00002173 MO.getSubReg() == 0)
2174 return;
2175 }
2176 }
Matthias Braun1965bfa2013-10-10 21:28:38 +00002177 addOperand(MachineOperand::CreateReg(Reg,
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00002178 true /*IsDef*/,
2179 true /*IsImp*/));
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00002180}
Evan Cheng59d27fe2010-03-03 23:37:30 +00002181
Jakob Stoklund Olesen4290be42012-02-03 20:43:39 +00002182void MachineInstr::setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs,
Dan Gohman86936502010-06-18 23:28:01 +00002183 const TargetRegisterInfo &TRI) {
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00002184 bool HasRegMask = false;
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002185 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00002186 if (MO.isRegMask()) {
2187 HasRegMask = true;
2188 continue;
2189 }
Dan Gohman86936502010-06-18 23:28:01 +00002190 if (!MO.isReg() || !MO.isDef()) continue;
2191 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenf6507322012-02-03 20:43:35 +00002192 if (!TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Dan Gohman86936502010-06-18 23:28:01 +00002193 // If there are no uses, including partial uses, the def is dead.
David Majnemer0a16c222016-08-11 21:15:00 +00002194 if (none_of(UsedRegs,
2195 [&](unsigned Use) { return TRI.regsOverlap(Use, Reg); }))
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002196 MO.setIsDead();
Dan Gohman86936502010-06-18 23:28:01 +00002197 }
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00002198
2199 // This is a call with a register mask operand.
2200 // Mask clobbers are always dead, so add defs for the non-dead defines.
2201 if (HasRegMask)
2202 for (ArrayRef<unsigned>::iterator I = UsedRegs.begin(), E = UsedRegs.end();
2203 I != E; ++I)
2204 addRegisterDefined(*I, &TRI);
Dan Gohman86936502010-06-18 23:28:01 +00002205}
2206
Evan Cheng59d27fe2010-03-03 23:37:30 +00002207unsigned
2208MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) {
Chandler Carruth962152c2012-03-07 09:39:46 +00002209 // Build up a buffer of hash code components.
Chandler Carruth962152c2012-03-07 09:39:46 +00002210 SmallVector<size_t, 8> HashComponents;
2211 HashComponents.reserve(MI->getNumOperands() + 1);
2212 HashComponents.push_back(MI->getOpcode());
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002213 for (const MachineOperand &MO : MI->operands()) {
Chandler Carruth264854f2012-07-05 11:06:22 +00002214 if (MO.isReg() && MO.isDef() &&
2215 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
2216 continue; // Skip virtual register defs.
2217
2218 HashComponents.push_back(hash_value(MO));
Evan Cheng59d27fe2010-03-03 23:37:30 +00002219 }
Chandler Carruth962152c2012-03-07 09:39:46 +00002220 return hash_combine_range(HashComponents.begin(), HashComponents.end());
Evan Cheng59d27fe2010-03-03 23:37:30 +00002221}
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00002222
2223void MachineInstr::emitError(StringRef Msg) const {
2224 // Find the source location cookie.
2225 unsigned LocCookie = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00002226 const MDNode *LocMD = nullptr;
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00002227 for (unsigned i = getNumOperands(); i != 0; --i) {
2228 if (getOperand(i-1).isMetadata() &&
2229 (LocMD = getOperand(i-1).getMetadata()) &&
2230 LocMD->getNumOperands() != 0) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002231 if (const ConstantInt *CI =
2232 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00002233 LocCookie = CI->getZExtValue();
2234 break;
2235 }
2236 }
2237 }
2238
2239 if (const MachineBasicBlock *MBB = getParent())
2240 if (const MachineFunction *MF = MBB->getParent())
2241 return MF->getMMI().getModule()->getContext().emitError(LocCookie, Msg);
2242 report_fatal_error(Msg);
2243}
Reid Kleckner28865802016-04-14 18:29:59 +00002244
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002245MachineInstrBuilder llvm::BuildMI(MachineFunction &MF, const DebugLoc &DL,
Reid Kleckner28865802016-04-14 18:29:59 +00002246 const MCInstrDesc &MCID, bool IsIndirect,
2247 unsigned Reg, unsigned Offset,
2248 const MDNode *Variable, const MDNode *Expr) {
2249 assert(isa<DILocalVariable>(Variable) && "not a variable");
2250 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
2251 assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
2252 "Expected inlined-at fields to agree");
2253 if (IsIndirect)
2254 return BuildMI(MF, DL, MCID)
2255 .addReg(Reg, RegState::Debug)
2256 .addImm(Offset)
2257 .addMetadata(Variable)
2258 .addMetadata(Expr);
2259 else {
2260 assert(Offset == 0 && "A direct address cannot have an offset.");
2261 return BuildMI(MF, DL, MCID)
2262 .addReg(Reg, RegState::Debug)
2263 .addReg(0U, RegState::Debug)
2264 .addMetadata(Variable)
2265 .addMetadata(Expr);
2266 }
2267}
2268
2269MachineInstrBuilder llvm::BuildMI(MachineBasicBlock &BB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002270 MachineBasicBlock::iterator I,
2271 const DebugLoc &DL, const MCInstrDesc &MCID,
2272 bool IsIndirect, unsigned Reg,
2273 unsigned Offset, const MDNode *Variable,
2274 const MDNode *Expr) {
Reid Kleckner28865802016-04-14 18:29:59 +00002275 assert(isa<DILocalVariable>(Variable) && "not a variable");
2276 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
2277 MachineFunction &MF = *BB.getParent();
2278 MachineInstr *MI =
2279 BuildMI(MF, DL, MCID, IsIndirect, Reg, Offset, Variable, Expr);
2280 BB.insert(I, MI);
2281 return MachineInstrBuilder(MF, MI);
2282}