blob: 560ebe8778225b937d67c584f81c071acd7ce088 [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"
Dan Gohman48b185d2009-09-25 20:36:54 +000020#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner961e7422008-01-01 01:12:31 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman2d489b52008-02-06 22:27:42 +000023#include "llvm/CodeGen/PseudoSourceValue.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000025#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Function.h"
27#include "llvm/IR/InlineAsm.h"
28#include "llvm/IR/LLVMContext.h"
29#include "llvm/IR/Metadata.h"
30#include "llvm/IR/Module.h"
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +000031#include "llvm/IR/ModuleSlotTracker.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Type.h"
33#include "llvm/IR/Value.h"
Evan Cheng6cc775f2011-06-28 19:10:37 +000034#include "llvm/MC/MCInstrDesc.h"
Chris Lattner6c604e32010-03-13 08:14:18 +000035#include "llvm/MC/MCSymbol.h"
Daniel Sanders1e97a0b2015-08-19 12:03:04 +000036#include "llvm/Support/CommandLine.h"
David Greene29388d62010-01-04 23:48:20 +000037#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000038#include "llvm/Support/ErrorHandling.h"
Dan Gohmanaedb4a62008-07-07 20:32:02 +000039#include "llvm/Support/MathExtras.h"
Chris Lattnera078d832008-08-24 20:37:32 +000040#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000041#include "llvm/Target/TargetInstrInfo.h"
42#include "llvm/Target/TargetMachine.h"
43#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000044#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner43df6c22004-02-23 18:38:20 +000045using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000046
Daniel Sanders1e97a0b2015-08-19 12:03:04 +000047static cl::opt<bool> PrintWholeRegMask(
48 "print-whole-regmask",
49 cl::desc("Print the full contents of regmask operands in IR dumps"),
50 cl::init(true), cl::Hidden);
51
Chris Lattner60055892007-12-30 21:56:09 +000052//===----------------------------------------------------------------------===//
53// MachineOperand Implementation
54//===----------------------------------------------------------------------===//
55
Chris Lattner961e7422008-01-01 01:12:31 +000056void MachineOperand::setReg(unsigned Reg) {
57 if (getReg() == Reg) return; // No change.
Jim Grosbachdee9e8a2011-08-24 16:44:17 +000058
Chris Lattner961e7422008-01-01 01:12:31 +000059 // Otherwise, we have to change the register. If this operand is embedded
60 // into a machine function, we need to update the old and new register's
61 // use/def lists.
62 if (MachineInstr *MI = getParent())
63 if (MachineBasicBlock *MBB = MI->getParent())
64 if (MachineFunction *MF = MBB->getParent()) {
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +000065 MachineRegisterInfo &MRI = MF->getRegInfo();
66 MRI.removeRegOperandFromUseList(this);
Jakob Stoklund Olesena4941692010-10-19 20:56:32 +000067 SmallContents.RegNo = Reg;
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +000068 MRI.addRegOperandToUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +000069 return;
70 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +000071
Chris Lattner961e7422008-01-01 01:12:31 +000072 // Otherwise, just change the register, no problem. :)
Jakob Stoklund Olesena4941692010-10-19 20:56:32 +000073 SmallContents.RegNo = Reg;
Chris Lattner961e7422008-01-01 01:12:31 +000074}
75
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +000076void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx,
77 const TargetRegisterInfo &TRI) {
78 assert(TargetRegisterInfo::isVirtualRegister(Reg));
79 if (SubIdx && getSubReg())
80 SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg());
81 setReg(Reg);
Jakob Stoklund Olesen7b0ac862010-06-01 22:39:25 +000082 if (SubIdx)
83 setSubReg(SubIdx);
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +000084}
85
86void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) {
87 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
88 if (getSubReg()) {
89 Reg = TRI.getSubReg(Reg, getSubReg());
Jakob Stoklund Olesen89bd2ae2011-05-08 19:21:08 +000090 // Note that getSubReg() may return 0 if the sub-register doesn't exist.
91 // That won't happen in legal code.
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +000092 setSubReg(0);
93 }
94 setReg(Reg);
95}
96
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +000097/// Change a def to a use, or a use to a def.
98void MachineOperand::setIsDef(bool Val) {
99 assert(isReg() && "Wrong MachineOperand accessor");
100 assert((!Val || !isDebug()) && "Marking a debug operation as def");
101 if (IsDef == Val)
102 return;
103 // MRI may keep uses and defs in different list positions.
104 if (MachineInstr *MI = getParent())
105 if (MachineBasicBlock *MBB = MI->getParent())
106 if (MachineFunction *MF = MBB->getParent()) {
107 MachineRegisterInfo &MRI = MF->getRegInfo();
108 MRI.removeRegOperandFromUseList(this);
109 IsDef = Val;
110 MRI.addRegOperandToUseList(this);
111 return;
112 }
113 IsDef = Val;
114}
115
Matt Arsenault93ffe582014-09-28 19:24:59 +0000116// If this operand is currently a register operand, and if this is in a
117// function, deregister the operand from the register's use/def list.
118void MachineOperand::removeRegFromUses() {
119 if (!isReg() || !isOnRegUseList())
120 return;
121
122 if (MachineInstr *MI = getParent()) {
123 if (MachineBasicBlock *MBB = MI->getParent()) {
124 if (MachineFunction *MF = MBB->getParent())
125 MF->getRegInfo().removeRegOperandFromUseList(this);
126 }
127 }
128}
129
Chris Lattner961e7422008-01-01 01:12:31 +0000130/// ChangeToImmediate - Replace this operand with a new immediate operand of
131/// the specified value. If an operand is known to be an immediate already,
132/// the setImm method should be used.
133void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000134 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
Matt Arsenault93ffe582014-09-28 19:24:59 +0000135
136 removeRegFromUses();
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000137
Chris Lattner961e7422008-01-01 01:12:31 +0000138 OpKind = MO_Immediate;
139 Contents.ImmVal = ImmVal;
140}
141
Matt Arsenault93ffe582014-09-28 19:24:59 +0000142void MachineOperand::ChangeToFPImmediate(const ConstantFP *FPImm) {
143 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
144
145 removeRegFromUses();
146
147 OpKind = MO_FPImmediate;
148 Contents.CFP = FPImm;
149}
150
Matt Arsenault633dba42015-05-06 17:05:54 +0000151void MachineOperand::ChangeToES(const char *SymName, unsigned char TargetFlags) {
152 assert((!isReg() || !isTied()) &&
153 "Cannot change a tied operand into an external symbol");
154
155 removeRegFromUses();
156
157 OpKind = MO_ExternalSymbol;
158 Contents.OffsetedInfo.Val.SymbolName = SymName;
159 setOffset(0); // Offset is always 0.
160 setTargetFlags(TargetFlags);
161}
162
163void MachineOperand::ChangeToMCSymbol(MCSymbol *Sym) {
164 assert((!isReg() || !isTied()) &&
165 "Cannot change a tied operand into an MCSymbol");
166
167 removeRegFromUses();
168
169 OpKind = MO_MCSymbol;
170 Contents.Sym = Sym;
171}
172
Chris Lattner961e7422008-01-01 01:12:31 +0000173/// ChangeToRegister - Replace this operand with a new register operand of
174/// the specified value. If an operand is known to be an register already,
175/// the setReg method should be used.
176void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
Dale Johannesend40d42c2010-02-10 00:41:49 +0000177 bool isKill, bool isDead, bool isUndef,
178 bool isDebug) {
Craig Topperc0196b12014-04-14 00:51:57 +0000179 MachineRegisterInfo *RegInfo = nullptr;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000180 if (MachineInstr *MI = getParent())
181 if (MachineBasicBlock *MBB = MI->getParent())
182 if (MachineFunction *MF = MBB->getParent())
183 RegInfo = &MF->getRegInfo();
184 // If this operand is already a register operand, remove it from the
Chris Lattner961e7422008-01-01 01:12:31 +0000185 // register's use/def lists.
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000186 bool WasReg = isReg();
187 if (RegInfo && WasReg)
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000188 RegInfo->removeRegOperandFromUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +0000189
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000190 // Change this to a register and set the reg#.
191 OpKind = MO_Register;
192 SmallContents.RegNo = Reg;
Jakob Stoklund Olesena1b246d2013-01-07 23:21:44 +0000193 SubReg_TargetFlags = 0;
Chris Lattner961e7422008-01-01 01:12:31 +0000194 IsDef = isDef;
195 IsImp = isImp;
196 IsKill = isKill;
197 IsDead = isDead;
Evan Cheng0dc101b2009-06-30 08:49:04 +0000198 IsUndef = isUndef;
Jakob Stoklund Olesenb0d91ab2011-12-07 00:22:07 +0000199 IsInternalRead = false;
Dale Johannesenc0d712d2008-09-14 01:44:36 +0000200 IsEarlyClobber = false;
Dale Johannesend40d42c2010-02-10 00:41:49 +0000201 IsDebug = isDebug;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000202 // Ensure isOnRegUseList() returns false.
Craig Topperc0196b12014-04-14 00:51:57 +0000203 Contents.Reg.Prev = nullptr;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000204 // Preserve the tie when the operand was already a register.
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000205 if (!WasReg)
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000206 TiedTo = 0;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000207
208 // If this operand is embedded in a function, add the operand to the
209 // register's use/def list.
210 if (RegInfo)
211 RegInfo->addRegOperandToUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +0000212}
213
Chris Lattner60055892007-12-30 21:56:09 +0000214/// isIdenticalTo - Return true if this operand is identical to the specified
Chandler Carruth264854f2012-07-05 11:06:22 +0000215/// operand. Note that this should stay in sync with the hash_value overload
216/// below.
Chris Lattner60055892007-12-30 21:56:09 +0000217bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
Chris Lattnerfd682802009-06-24 17:54:48 +0000218 if (getType() != Other.getType() ||
219 getTargetFlags() != Other.getTargetFlags())
220 return false;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000221
Chris Lattner60055892007-12-30 21:56:09 +0000222 switch (getType()) {
Chris Lattner60055892007-12-30 21:56:09 +0000223 case MachineOperand::MO_Register:
224 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
225 getSubReg() == Other.getSubReg();
226 case MachineOperand::MO_Immediate:
227 return getImm() == Other.getImm();
Cameron Zwarich7da0f9a2011-07-01 23:45:21 +0000228 case MachineOperand::MO_CImmediate:
229 return getCImm() == Other.getCImm();
Nate Begeman26b76b62008-02-14 07:39:30 +0000230 case MachineOperand::MO_FPImmediate:
231 return getFPImm() == Other.getFPImm();
Chris Lattner60055892007-12-30 21:56:09 +0000232 case MachineOperand::MO_MachineBasicBlock:
233 return getMBB() == Other.getMBB();
234 case MachineOperand::MO_FrameIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000235 return getIndex() == Other.getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000236 case MachineOperand::MO_ConstantPoolIndex:
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000237 case MachineOperand::MO_TargetIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000238 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattner60055892007-12-30 21:56:09 +0000239 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000240 return getIndex() == Other.getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000241 case MachineOperand::MO_GlobalAddress:
242 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
243 case MachineOperand::MO_ExternalSymbol:
244 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
245 getOffset() == Other.getOffset();
Dan Gohman6c938802009-10-30 01:27:03 +0000246 case MachineOperand::MO_BlockAddress:
Michael Liaoabb87d42012-09-12 21:43:09 +0000247 return getBlockAddress() == Other.getBlockAddress() &&
248 getOffset() == Other.getOffset();
Juergen Ributzkae8294752013-12-14 06:53:06 +0000249 case MachineOperand::MO_RegisterMask:
250 case MachineOperand::MO_RegisterLiveOut:
Jakob Stoklund Olesen374ed322012-01-16 19:22:00 +0000251 return getRegMask() == Other.getRegMask();
Chris Lattner6c604e32010-03-13 08:14:18 +0000252 case MachineOperand::MO_MCSymbol:
253 return getMCSymbol() == Other.getMCSymbol();
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000254 case MachineOperand::MO_CFIIndex:
255 return getCFIIndex() == Other.getCFIIndex();
Chris Lattnerf839ee02010-04-07 18:03:19 +0000256 case MachineOperand::MO_Metadata:
257 return getMetadata() == Other.getMetadata();
Chris Lattner60055892007-12-30 21:56:09 +0000258 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000259 llvm_unreachable("Invalid machine operand type");
Chris Lattner60055892007-12-30 21:56:09 +0000260}
261
Chandler Carruth264854f2012-07-05 11:06:22 +0000262// Note: this must stay exactly in sync with isIdenticalTo above.
263hash_code llvm::hash_value(const MachineOperand &MO) {
264 switch (MO.getType()) {
265 case MachineOperand::MO_Register:
Jakob Stoklund Olesendba99d02012-08-28 18:05:48 +0000266 // Register operands don't have target flags.
267 return hash_combine(MO.getType(), MO.getReg(), MO.getSubReg(), MO.isDef());
Chandler Carruth264854f2012-07-05 11:06:22 +0000268 case MachineOperand::MO_Immediate:
269 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm());
270 case MachineOperand::MO_CImmediate:
271 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm());
272 case MachineOperand::MO_FPImmediate:
273 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm());
274 case MachineOperand::MO_MachineBasicBlock:
275 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB());
276 case MachineOperand::MO_FrameIndex:
277 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
278 case MachineOperand::MO_ConstantPoolIndex:
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000279 case MachineOperand::MO_TargetIndex:
Chandler Carruth264854f2012-07-05 11:06:22 +0000280 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(),
281 MO.getOffset());
282 case MachineOperand::MO_JumpTableIndex:
283 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
284 case MachineOperand::MO_ExternalSymbol:
285 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(),
286 MO.getSymbolName());
287 case MachineOperand::MO_GlobalAddress:
288 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(),
289 MO.getOffset());
290 case MachineOperand::MO_BlockAddress:
291 return hash_combine(MO.getType(), MO.getTargetFlags(),
Michael Liaoabb87d42012-09-12 21:43:09 +0000292 MO.getBlockAddress(), MO.getOffset());
Chandler Carruth264854f2012-07-05 11:06:22 +0000293 case MachineOperand::MO_RegisterMask:
Juergen Ributzkae8294752013-12-14 06:53:06 +0000294 case MachineOperand::MO_RegisterLiveOut:
Chandler Carruth264854f2012-07-05 11:06:22 +0000295 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask());
296 case MachineOperand::MO_Metadata:
297 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata());
298 case MachineOperand::MO_MCSymbol:
299 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol());
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000300 case MachineOperand::MO_CFIIndex:
301 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex());
Chandler Carruth264854f2012-07-05 11:06:22 +0000302 }
303 llvm_unreachable("Invalid machine operand type");
304}
305
Eric Christopher1cdefae2015-02-27 00:11:34 +0000306void MachineOperand::print(raw_ostream &OS,
307 const TargetRegisterInfo *TRI) const {
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000308 ModuleSlotTracker DummyMST(nullptr);
309 print(OS, DummyMST, TRI);
310}
311
312void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
313 const TargetRegisterInfo *TRI) const {
Chris Lattner60055892007-12-30 21:56:09 +0000314 switch (getType()) {
315 case MachineOperand::MO_Register:
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000316 OS << PrintReg(getReg(), TRI, getSubReg());
Dan Gohman0ab11442008-12-18 21:51:27 +0000317
Evan Cheng0dc101b2009-06-30 08:49:04 +0000318 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000319 isInternalRead() || isEarlyClobber() || isTied()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000320 OS << '<';
Chris Lattner60055892007-12-30 21:56:09 +0000321 bool NeedComma = false;
Evan Cheng70b1fa52009-10-14 23:37:31 +0000322 if (isDef()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000323 if (NeedComma) OS << ',';
Dale Johannesen1f3ab862008-09-12 17:49:03 +0000324 if (isEarlyClobber())
325 OS << "earlyclobber,";
Evan Cheng70b1fa52009-10-14 23:37:31 +0000326 if (isImplicit())
327 OS << "imp-";
Chris Lattner60055892007-12-30 21:56:09 +0000328 OS << "def";
329 NeedComma = true;
Jakob Stoklund Olesen7111a632012-04-20 21:45:33 +0000330 // <def,read-undef> only makes sense when getSubReg() is set.
331 // Don't clutter the output otherwise.
332 if (isUndef() && getSubReg())
333 OS << ",read-undef";
Evan Chengf781bd82009-10-21 07:56:02 +0000334 } else if (isImplicit()) {
Craig Topper9a9d58a2015-05-16 05:42:08 +0000335 OS << "imp-use";
336 NeedComma = true;
Evan Chengf781bd82009-10-21 07:56:02 +0000337 }
Evan Cheng70b1fa52009-10-14 23:37:31 +0000338
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000339 if (isKill()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000340 if (NeedComma) OS << ',';
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000341 OS << "kill";
342 NeedComma = true;
343 }
344 if (isDead()) {
345 if (NeedComma) OS << ',';
346 OS << "dead";
347 NeedComma = true;
348 }
349 if (isUndef() && isUse()) {
350 if (NeedComma) OS << ',';
351 OS << "undef";
352 NeedComma = true;
353 }
354 if (isInternalRead()) {
355 if (NeedComma) OS << ',';
356 OS << "internal";
357 NeedComma = true;
358 }
359 if (isTied()) {
360 if (NeedComma) OS << ',';
361 OS << "tied";
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000362 if (TiedTo != 15)
363 OS << unsigned(TiedTo - 1);
Chris Lattner60055892007-12-30 21:56:09 +0000364 }
Chris Lattnerfd682802009-06-24 17:54:48 +0000365 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000366 }
367 break;
368 case MachineOperand::MO_Immediate:
369 OS << getImm();
370 break;
Devang Patelf071d722011-06-24 20:46:11 +0000371 case MachineOperand::MO_CImmediate:
372 getCImm()->getValue().print(OS, false);
373 break;
Nate Begeman26b76b62008-02-14 07:39:30 +0000374 case MachineOperand::MO_FPImmediate:
Chris Lattnerfdd87902009-10-05 05:54:46 +0000375 if (getFPImm()->getType()->isFloatTy())
Nate Begeman26b76b62008-02-14 07:39:30 +0000376 OS << getFPImm()->getValueAPF().convertToFloat();
Chris Lattnerfd682802009-06-24 17:54:48 +0000377 else
Nate Begeman26b76b62008-02-14 07:39:30 +0000378 OS << getFPImm()->getValueAPF().convertToDouble();
Nate Begeman26b76b62008-02-14 07:39:30 +0000379 break;
Chris Lattner60055892007-12-30 21:56:09 +0000380 case MachineOperand::MO_MachineBasicBlock:
Dan Gohman34341e62009-10-31 20:19:03 +0000381 OS << "<BB#" << getMBB()->getNumber() << ">";
Chris Lattner60055892007-12-30 21:56:09 +0000382 break;
383 case MachineOperand::MO_FrameIndex:
Chris Lattnerfd682802009-06-24 17:54:48 +0000384 OS << "<fi#" << getIndex() << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000385 break;
386 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000387 OS << "<cp#" << getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000388 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000389 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000390 break;
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000391 case MachineOperand::MO_TargetIndex:
392 OS << "<ti#" << getIndex();
393 if (getOffset()) OS << "+" << getOffset();
394 OS << '>';
395 break;
Chris Lattner60055892007-12-30 21:56:09 +0000396 case MachineOperand::MO_JumpTableIndex:
Chris Lattnerfd682802009-06-24 17:54:48 +0000397 OS << "<jt#" << getIndex() << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000398 break;
399 case MachineOperand::MO_GlobalAddress:
Dan Gohman0080ee22009-11-06 18:03:10 +0000400 OS << "<ga:";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000401 getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Chris Lattner60055892007-12-30 21:56:09 +0000402 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000403 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000404 break;
405 case MachineOperand::MO_ExternalSymbol:
406 OS << "<es:" << getSymbolName();
407 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000408 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000409 break;
Dan Gohman6c938802009-10-30 01:27:03 +0000410 case MachineOperand::MO_BlockAddress:
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000411 OS << '<';
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000412 getBlockAddress()->printAsOperand(OS, /*PrintType=*/false, MST);
Michael Liaoabb87d42012-09-12 21:43:09 +0000413 if (getOffset()) OS << "+" << getOffset();
Dan Gohman6c938802009-10-30 01:27:03 +0000414 OS << '>';
415 break;
Daniel Sanders1e97a0b2015-08-19 12:03:04 +0000416 case MachineOperand::MO_RegisterMask: {
417 unsigned NumRegsInMask = 0;
418 unsigned NumRegsEmitted = 0;
419 OS << "<regmask";
420 for (unsigned i = 0; i < TRI->getNumRegs(); ++i) {
421 unsigned MaskWord = i / 32;
422 unsigned MaskBit = i % 32;
423 if (getRegMask()[MaskWord] & (1 << MaskBit)) {
424 if (PrintWholeRegMask || NumRegsEmitted <= 10) {
425 OS << " " << PrintReg(i, TRI);
426 NumRegsEmitted++;
427 }
428 NumRegsInMask++;
429 }
430 }
431 if (NumRegsEmitted != NumRegsInMask)
432 OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more...";
433 OS << ">";
Jakob Stoklund Olesen374ed322012-01-16 19:22:00 +0000434 break;
Daniel Sanders1e97a0b2015-08-19 12:03:04 +0000435 }
Juergen Ributzkae8294752013-12-14 06:53:06 +0000436 case MachineOperand::MO_RegisterLiveOut:
437 OS << "<regliveout>";
438 break;
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000439 case MachineOperand::MO_Metadata:
440 OS << '<';
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000441 getMetadata()->printAsOperand(OS, MST);
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000442 OS << '>';
443 break;
Chris Lattner6c604e32010-03-13 08:14:18 +0000444 case MachineOperand::MO_MCSymbol:
445 OS << "<MCSym=" << *getMCSymbol() << '>';
446 break;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000447 case MachineOperand::MO_CFIIndex:
448 OS << "<call frame instruction>";
449 break;
Chris Lattner60055892007-12-30 21:56:09 +0000450 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000451
Chris Lattnerfd682802009-06-24 17:54:48 +0000452 if (unsigned TF = getTargetFlags())
453 OS << "[TF=" << TF << ']';
Chris Lattner60055892007-12-30 21:56:09 +0000454}
455
456//===----------------------------------------------------------------------===//
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000457// MachineMemOperand Implementation
458//===----------------------------------------------------------------------===//
459
Chris Lattnerde93bb02010-09-21 05:39:30 +0000460/// getAddrSpace - Return the LLVM IR address space number that this pointer
461/// points into.
462unsigned MachinePointerInfo::getAddrSpace() const {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000463 if (V.isNull() || V.is<const PseudoSourceValue*>()) return 0;
464 return cast<PointerType>(V.get<const Value*>()->getType())->getAddressSpace();
Chris Lattnerde93bb02010-09-21 05:39:30 +0000465}
466
Chris Lattner82fd06d2010-09-21 06:22:23 +0000467/// getConstantPool - Return a MachinePointerInfo record that refers to the
468/// constant pool.
Alex Lorenze40c8a22015-08-11 23:09:45 +0000469MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) {
470 return MachinePointerInfo(MF.getPSVManager().getConstantPool());
Chris Lattner82fd06d2010-09-21 06:22:23 +0000471}
472
473/// getFixedStack - Return a MachinePointerInfo record that refers to the
474/// the specified FrameIndex.
Alex Lorenze40c8a22015-08-11 23:09:45 +0000475MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF,
476 int FI, int64_t Offset) {
477 return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset);
Chris Lattner82fd06d2010-09-21 06:22:23 +0000478}
479
Alex Lorenze40c8a22015-08-11 23:09:45 +0000480MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) {
481 return MachinePointerInfo(MF.getPSVManager().getJumpTable());
Chris Lattner50287ea2010-09-21 06:43:24 +0000482}
483
Alex Lorenze40c8a22015-08-11 23:09:45 +0000484MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) {
485 return MachinePointerInfo(MF.getPSVManager().getGOT());
Chris Lattner50287ea2010-09-21 06:43:24 +0000486}
Chris Lattnerde93bb02010-09-21 05:39:30 +0000487
Alex Lorenze40c8a22015-08-11 23:09:45 +0000488MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF,
489 int64_t Offset) {
490 return MachinePointerInfo(MF.getPSVManager().getStack(), Offset);
Chris Lattner886250c2010-09-21 18:51:21 +0000491}
492
Chris Lattner00ca0b82010-09-21 04:32:08 +0000493MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, unsigned f,
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000494 uint64_t s, unsigned int a,
Hal Finkelcc39b672014-07-24 12:16:19 +0000495 const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +0000496 const MDNode *Ranges)
Chris Lattner00ca0b82010-09-21 04:32:08 +0000497 : PtrInfo(ptrinfo), Size(s),
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000498 Flags((f & ((1 << MOMaxBits) - 1)) | ((Log2_32(a) + 1) << MOMaxBits)),
Hal Finkelcc39b672014-07-24 12:16:19 +0000499 AAInfo(AAInfo), Ranges(Ranges) {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000500 assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue*>() ||
501 isa<PointerType>(PtrInfo.V.get<const Value*>()->getType())) &&
Chris Lattner00ca0b82010-09-21 04:32:08 +0000502 "invalid pointer value");
Dan Gohmane7c82422009-09-21 19:47:04 +0000503 assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
Dan Gohmanbf98f682008-07-16 15:56:42 +0000504 assert((isLoad() || isStore()) && "Not a load/store!");
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000505}
506
Dan Gohman2da2bed2008-08-20 15:58:01 +0000507/// Profile - Gather unique data for the object.
508///
509void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
Chris Lattner187f6532010-09-21 04:23:39 +0000510 ID.AddInteger(getOffset());
Dan Gohman2da2bed2008-08-20 15:58:01 +0000511 ID.AddInteger(Size);
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000512 ID.AddPointer(getOpaqueValue());
Dan Gohman2da2bed2008-08-20 15:58:01 +0000513 ID.AddInteger(Flags);
514}
515
Dan Gohman48b185d2009-09-25 20:36:54 +0000516void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
517 // The Value and Offset may differ due to CSE. But the flags and size
518 // should be the same.
519 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
520 assert(MMO->getSize() == getSize() && "Size mismatch!");
521
522 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
523 // Update the alignment value.
David Greene3a0412f2010-02-15 16:48:31 +0000524 Flags = (Flags & ((1 << MOMaxBits) - 1)) |
525 ((Log2_32(MMO->getBaseAlignment()) + 1) << MOMaxBits);
Dan Gohman48b185d2009-09-25 20:36:54 +0000526 // Also update the base and offset, because the new alignment may
527 // not be applicable with the old ones.
Chris Lattner187f6532010-09-21 04:23:39 +0000528 PtrInfo = MMO->PtrInfo;
Dan Gohman48b185d2009-09-25 20:36:54 +0000529 }
530}
531
Dan Gohman5a6b11c2009-09-25 23:33:20 +0000532/// getAlignment - Return the minimum known alignment in bytes of the
533/// actual memory reference.
534uint64_t MachineMemOperand::getAlignment() const {
535 return MinAlign(getBaseAlignment(), getOffset());
536}
537
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000538void MachineMemOperand::print(raw_ostream &OS) const {
539 ModuleSlotTracker DummyMST(nullptr);
540 print(OS, DummyMST);
541}
542void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST) const {
543 assert((isLoad() || isStore()) &&
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000544 "SV has to be a load, store or both.");
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000545
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000546 if (isVolatile())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000547 OS << "Volatile ";
548
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000549 if (isLoad())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000550 OS << "LD";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000551 if (isStore())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000552 OS << "ST";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000553 OS << getSize();
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000554
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000555 // Print the address information.
556 OS << "[";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000557 if (const Value *V = getValue())
558 V->printAsOperand(OS, /*PrintType=*/false, MST);
559 else if (const PseudoSourceValue *PSV = getPseudoValue())
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000560 PSV->printCustom(OS);
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000561 else
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000562 OS << "<unknown>";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000563
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000564 unsigned AS = getAddrSpace();
Matt Arsenault68c38fd2013-12-14 00:24:02 +0000565 if (AS != 0)
566 OS << "(addrspace=" << AS << ')';
567
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000568 // If the alignment of the memory reference itself differs from the alignment
569 // of the base pointer, print the base alignment explicitly, next to the base
570 // pointer.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000571 if (getBaseAlignment() != getAlignment())
572 OS << "(align=" << getBaseAlignment() << ")";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000573
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000574 if (getOffset() != 0)
575 OS << "+" << getOffset();
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000576 OS << "]";
577
578 // Print the alignment of the reference.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000579 if (getBaseAlignment() != getAlignment() || getBaseAlignment() != getSize())
580 OS << "(align=" << getAlignment() << ")";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000581
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000582 // Print TBAA info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000583 if (const MDNode *TBAAInfo = getAAInfo().TBAA) {
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000584 OS << "(tbaa=";
585 if (TBAAInfo->getNumOperands() > 0)
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000586 TBAAInfo->getOperand(0)->printAsOperand(OS, MST);
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000587 else
588 OS << "<unknown>";
589 OS << ")";
590 }
591
Hal Finkel94146652014-07-24 14:25:39 +0000592 // Print AA scope info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000593 if (const MDNode *ScopeInfo = getAAInfo().Scope) {
Hal Finkel94146652014-07-24 14:25:39 +0000594 OS << "(alias.scope=";
595 if (ScopeInfo->getNumOperands() > 0)
596 for (unsigned i = 0, ie = ScopeInfo->getNumOperands(); i != ie; ++i) {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000597 ScopeInfo->getOperand(i)->printAsOperand(OS, MST);
Hal Finkel94146652014-07-24 14:25:39 +0000598 if (i != ie-1)
599 OS << ",";
600 }
601 else
602 OS << "<unknown>";
603 OS << ")";
604 }
605
606 // Print AA noalias scope info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000607 if (const MDNode *NoAliasInfo = getAAInfo().NoAlias) {
Hal Finkel94146652014-07-24 14:25:39 +0000608 OS << "(noalias=";
609 if (NoAliasInfo->getNumOperands() > 0)
610 for (unsigned i = 0, ie = NoAliasInfo->getNumOperands(); i != ie; ++i) {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000611 NoAliasInfo->getOperand(i)->printAsOperand(OS, MST);
Hal Finkel94146652014-07-24 14:25:39 +0000612 if (i != ie-1)
613 OS << ",";
614 }
615 else
616 OS << "<unknown>";
617 OS << ")";
618 }
619
Bill Wendling9f638ab2011-04-29 23:45:22 +0000620 // Print nontemporal info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000621 if (isNonTemporal())
Bill Wendling9f638ab2011-04-29 23:45:22 +0000622 OS << "(nontemporal)";
623
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000624 if (isInvariant())
Matt Arsenault572c29a2015-06-26 19:00:11 +0000625 OS << "(invariant)";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000626}
627
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000628//===----------------------------------------------------------------------===//
Chris Lattner60055892007-12-30 21:56:09 +0000629// MachineInstr Implementation
630//===----------------------------------------------------------------------===//
631
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000632void MachineInstr::addImplicitDefUseOperands(MachineFunction &MF) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000633 if (MCID->ImplicitDefs)
Craig Topper5a4bcc72012-03-08 08:22:45 +0000634 for (const uint16_t *ImpDefs = MCID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000635 addOperand(MF, MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng6cc775f2011-06-28 19:10:37 +0000636 if (MCID->ImplicitUses)
Craig Topper5a4bcc72012-03-08 08:22:45 +0000637 for (const uint16_t *ImpUses = MCID->getImplicitUses(); *ImpUses; ++ImpUses)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000638 addOperand(MF, MachineOperand::CreateReg(*ImpUses, false, true));
Evan Cheng77af6ac2006-11-13 23:34:06 +0000639}
640
Bob Wilson406f2702010-04-09 04:34:03 +0000641/// MachineInstr ctor - This constructor creates a MachineInstr and adds the
642/// implicit operands. It reserves space for the number of operands specified by
Evan Cheng6cc775f2011-06-28 19:10:37 +0000643/// the MCInstrDesc.
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000644MachineInstr::MachineInstr(MachineFunction &MF, const MCInstrDesc &tid,
Benjamin Kramera9591b52015-02-07 12:28:15 +0000645 DebugLoc dl, bool NoImp)
646 : MCID(&tid), Parent(nullptr), Operands(nullptr), NumOperands(0), Flags(0),
647 AsmPrinterFlags(0), NumMemRefs(0), MemRefs(nullptr),
648 debugLoc(std::move(dl)) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000649 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
650
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000651 // Reserve space for the expected number of operands.
652 if (unsigned NumOps = MCID->getNumOperands() +
653 MCID->getNumImplicitDefs() + MCID->getNumImplicitUses()) {
654 CapOperands = OperandCapacity::get(NumOps);
655 Operands = MF.allocateOperandArray(CapOperands);
656 }
657
Dale Johannesen4e04ef32009-01-27 23:20:29 +0000658 if (!NoImp)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000659 addImplicitDefUseOperands(MF);
Dale Johannesen4e04ef32009-01-27 23:20:29 +0000660}
661
Misha Brukmanb47ab7a2004-07-09 14:45:17 +0000662/// MachineInstr ctor - Copies MachineInstr arg exactly
663///
Evan Chenga7a20c42008-07-19 00:37:25 +0000664MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Craig Topperc0196b12014-04-14 00:51:57 +0000665 : MCID(&MI.getDesc()), Parent(nullptr), Operands(nullptr), NumOperands(0),
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000666 Flags(0), AsmPrinterFlags(0),
Benjamin Kramerd03878b2012-03-16 16:39:27 +0000667 NumMemRefs(MI.NumMemRefs), MemRefs(MI.MemRefs),
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000668 debugLoc(MI.getDebugLoc()) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000669 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
670
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000671 CapOperands = OperandCapacity::get(MI.getNumOperands());
672 Operands = MF.allocateOperandArray(CapOperands);
Tanya Lattner9953d862004-05-23 20:58:02 +0000673
Jakob Stoklund Olesendc5285f2013-01-05 05:05:51 +0000674 // Copy operands.
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000675 for (const MachineOperand &MO : MI.operands())
676 addOperand(MF, MO);
Tanya Lattnerbcee21b2004-05-24 03:14:18 +0000677
Jakob Stoklund Olesena33f5042012-12-18 21:36:05 +0000678 // Copy all the sensible flags.
679 setFlags(MI.Flags);
Alkis Evlogimenos14f3fe82004-02-16 07:17:43 +0000680}
681
Chris Lattner961e7422008-01-01 01:12:31 +0000682/// getRegInfo - If this instruction is embedded into a MachineFunction,
683/// return the MachineRegisterInfo object for the current function, otherwise
684/// return null.
685MachineRegisterInfo *MachineInstr::getRegInfo() {
686 if (MachineBasicBlock *MBB = getParent())
Dan Gohmanf188fa42008-07-08 23:59:09 +0000687 return &MBB->getParent()->getRegInfo();
Craig Topperc0196b12014-04-14 00:51:57 +0000688 return nullptr;
Chris Lattner961e7422008-01-01 01:12:31 +0000689}
690
691/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
692/// this instruction from their respective use lists. This requires that the
693/// operands already be on their use lists.
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000694void MachineInstr::RemoveRegOperandsFromUseLists(MachineRegisterInfo &MRI) {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000695 for (MachineOperand &MO : operands())
696 if (MO.isReg())
697 MRI.removeRegOperandFromUseList(&MO);
Chris Lattner961e7422008-01-01 01:12:31 +0000698}
699
700/// AddRegOperandsToUseLists - Add all of the register operands in
701/// this instruction from their respective use lists. This requires that the
702/// operands not be on their use lists yet.
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000703void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &MRI) {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000704 for (MachineOperand &MO : operands())
705 if (MO.isReg())
706 MRI.addRegOperandToUseList(&MO);
Chris Lattner961e7422008-01-01 01:12:31 +0000707}
708
Jakob Stoklund Olesen2455b5852012-12-20 22:54:05 +0000709void MachineInstr::addOperand(const MachineOperand &Op) {
710 MachineBasicBlock *MBB = getParent();
711 assert(MBB && "Use MachineInstrBuilder to add operands to dangling instrs");
712 MachineFunction *MF = MBB->getParent();
713 assert(MF && "Use MachineInstrBuilder to add operands to dangling instrs");
714 addOperand(*MF, Op);
715}
716
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000717/// Move NumOps MachineOperands from Src to Dst, with support for overlapping
718/// ranges. If MRI is non-null also update use-def chains.
719static void moveOperands(MachineOperand *Dst, MachineOperand *Src,
720 unsigned NumOps, MachineRegisterInfo *MRI) {
721 if (MRI)
722 return MRI->moveOperands(Dst, Src, NumOps);
723
Benjamin Kramer5c0e64f2015-02-21 16:22:48 +0000724 // MachineOperand is a trivially copyable type so we can just use memmove.
725 std::memmove(Dst, Src, NumOps * sizeof(MachineOperand));
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000726}
727
Chris Lattner961e7422008-01-01 01:12:31 +0000728/// addOperand - Add the specified operand to the instruction. If it is an
729/// implicit operand, it is added to the end of the operand list. If it is
730/// an explicit operand it is added at the end of the explicit operand list
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000731/// (before the first implicit operand).
Jakob Stoklund Olesen2455b5852012-12-20 22:54:05 +0000732void MachineInstr::addOperand(MachineFunction &MF, const MachineOperand &Op) {
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000733 assert(MCID && "Cannot add operands before providing an instr descriptor");
Dan Gohman9356d8f2008-12-09 22:45:08 +0000734
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000735 // Check if we're adding one of our existing operands.
736 if (&Op >= Operands && &Op < Operands + NumOperands) {
737 // This is unusual: MI->addOperand(MI->getOperand(i)).
738 // If adding Op requires reallocating or moving existing operands around,
739 // the Op reference could go stale. Support it by copying Op.
740 MachineOperand CopyOp(Op);
741 return addOperand(MF, CopyOp);
742 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000743
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000744 // Find the insert location for the new operand. Implicit registers go at
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000745 // the end, everything else goes before the implicit regs.
746 //
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000747 // FIXME: Allow mixed explicit and implicit operands on inline asm.
748 // InstrEmitter::EmitSpecialNode() is marking inline asm clobbers as
749 // implicit-defs, but they must not be moved around. See the FIXME in
750 // InstrEmitter.cpp.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000751 unsigned OpNo = getNumOperands();
752 bool isImpReg = Op.isReg() && Op.isImplicit();
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000753 if (!isImpReg && !isInlineAsm()) {
754 while (OpNo && Operands[OpNo-1].isReg() && Operands[OpNo-1].isImplicit()) {
755 --OpNo;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000756 assert(!Operands[OpNo].isTied() && "Cannot move tied operands");
Chris Lattner961e7422008-01-01 01:12:31 +0000757 }
758 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000759
Pekka Jaaskelaineneb4a6e72013-10-15 14:40:46 +0000760#ifndef NDEBUG
Pekka Jaaskelaineneb08e2e2013-10-15 14:18:10 +0000761 bool isMetaDataOp = Op.getType() == MachineOperand::MO_Metadata;
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000762 // OpNo now points as the desired insertion point. Unless this is a variadic
763 // instruction, only implicit regs are allowed beyond MCID->getNumOperands().
Jakob Stoklund Olesenc300ef02012-07-04 23:53:23 +0000764 // RegMask operands go between the explicit and implicit operands.
765 assert((isImpReg || Op.isRegMask() || MCID->isVariadic() ||
Pekka Jaaskelaineneb08e2e2013-10-15 14:18:10 +0000766 OpNo < MCID->getNumOperands() || isMetaDataOp) &&
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000767 "Trying to add an operand to a machine instr that is already done!");
Pekka Jaaskelaineneb4a6e72013-10-15 14:40:46 +0000768#endif
Chris Lattner961e7422008-01-01 01:12:31 +0000769
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000770 MachineRegisterInfo *MRI = getRegInfo();
Chris Lattner961e7422008-01-01 01:12:31 +0000771
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000772 // Determine if the Operands array needs to be reallocated.
773 // Save the old capacity and operand array.
774 OperandCapacity OldCap = CapOperands;
775 MachineOperand *OldOperands = Operands;
776 if (!OldOperands || OldCap.getSize() == getNumOperands()) {
777 CapOperands = OldOperands ? OldCap.getNext() : OldCap.get(1);
778 Operands = MF.allocateOperandArray(CapOperands);
779 // Move the operands before the insertion point.
780 if (OpNo)
781 moveOperands(Operands, OldOperands, OpNo, MRI);
782 }
Chris Lattner961e7422008-01-01 01:12:31 +0000783
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000784 // Move the operands following the insertion point.
785 if (OpNo != NumOperands)
786 moveOperands(Operands + OpNo + 1, OldOperands + OpNo, NumOperands - OpNo,
787 MRI);
788 ++NumOperands;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000789
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000790 // Deallocate the old operand array.
791 if (OldOperands != Operands && OldOperands)
792 MF.deallocateOperandArray(OldCap, OldOperands);
793
794 // Copy Op into place. It still needs to be inserted into the MRI use lists.
795 MachineOperand *NewMO = new (Operands + OpNo) MachineOperand(Op);
796 NewMO->ParentMI = this;
797
798 // When adding a register operand, tell MRI about it.
799 if (NewMO->isReg()) {
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000800 // Ensure isOnRegUseList() returns false, regardless of Op's status.
Craig Topperc0196b12014-04-14 00:51:57 +0000801 NewMO->Contents.Reg.Prev = nullptr;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000802 // Ignore existing ties. This is not a property that can be copied.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000803 NewMO->TiedTo = 0;
804 // Add the new operand to MRI, but only for instructions in an MBB.
805 if (MRI)
806 MRI->addRegOperandToUseList(NewMO);
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000807 // The MCID operand information isn't accurate until we start adding
808 // explicit operands. The implicit operands are added first, then the
809 // explicits are inserted before them.
810 if (!isImpReg) {
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000811 // Tie uses to defs as indicated in MCInstrDesc.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000812 if (NewMO->isUse()) {
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000813 int DefIdx = MCID->getOperandConstraint(OpNo, MCOI::TIED_TO);
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +0000814 if (DefIdx != -1)
815 tieOperands(DefIdx, OpNo);
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000816 }
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000817 // If the register operand is flagged as early, mark the operand as such.
818 if (MCID->getOperandConstraint(OpNo, MCOI::EARLY_CLOBBER) != -1)
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000819 NewMO->setIsEarlyClobber(true);
Chris Lattner961e7422008-01-01 01:12:31 +0000820 }
Chris Lattner961e7422008-01-01 01:12:31 +0000821 }
822}
823
824/// RemoveOperand - Erase an operand from an instruction, leaving it with one
825/// fewer operand than it started with.
826///
827void MachineInstr::RemoveOperand(unsigned OpNo) {
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +0000828 assert(OpNo < getNumOperands() && "Invalid operand number");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000829 untieRegOperand(OpNo);
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000830
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000831#ifndef NDEBUG
832 // Moving tied operands would break the ties.
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +0000833 for (unsigned i = OpNo + 1, e = getNumOperands(); i != e; ++i)
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000834 if (Operands[i].isReg())
835 assert(!Operands[i].isTied() && "Cannot move tied operands");
836#endif
837
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000838 MachineRegisterInfo *MRI = getRegInfo();
839 if (MRI && Operands[OpNo].isReg())
840 MRI->removeRegOperandFromUseList(Operands + OpNo);
Chris Lattner961e7422008-01-01 01:12:31 +0000841
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000842 // Don't call the MachineOperand destructor. A lot of this code depends on
843 // MachineOperand having a trivial destructor anyway, and adding a call here
844 // wouldn't make it 'destructor-correct'.
845
846 if (unsigned N = NumOperands - 1 - OpNo)
847 moveOperands(Operands + OpNo, Operands + OpNo + 1, N, MRI);
848 --NumOperands;
Chris Lattner961e7422008-01-01 01:12:31 +0000849}
850
Dan Gohman48b185d2009-09-25 20:36:54 +0000851/// addMemOperand - Add a MachineMemOperand to the machine instruction.
852/// This function should be used only occasionally. The setMemRefs function
853/// is the primary method for setting up a MachineInstr's MemRefs list.
Dan Gohman3b460302008-07-07 23:14:23 +0000854void MachineInstr::addMemOperand(MachineFunction &MF,
Dan Gohman48b185d2009-09-25 20:36:54 +0000855 MachineMemOperand *MO) {
856 mmo_iterator OldMemRefs = MemRefs;
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000857 unsigned OldNumMemRefs = NumMemRefs;
Dan Gohman3b460302008-07-07 23:14:23 +0000858
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000859 unsigned NewNum = NumMemRefs + 1;
Dan Gohman48b185d2009-09-25 20:36:54 +0000860 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
Dan Gohman3b460302008-07-07 23:14:23 +0000861
Benjamin Kramerd03878b2012-03-16 16:39:27 +0000862 std::copy(OldMemRefs, OldMemRefs + OldNumMemRefs, NewMemRefs);
Dan Gohman48b185d2009-09-25 20:36:54 +0000863 NewMemRefs[NewNum - 1] = MO;
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000864 setMemRefs(NewMemRefs, NewMemRefs + NewNum);
Dan Gohman48b185d2009-09-25 20:36:54 +0000865}
Chris Lattner961e7422008-01-01 01:12:31 +0000866
Benjamin Kramer97f889f2012-03-17 17:03:45 +0000867bool MachineInstr::hasPropertyInBundle(unsigned Mask, QueryType Type) const {
Jakob Stoklund Olesenf0615c72013-01-10 18:42:44 +0000868 assert(!isBundledWithPred() && "Must be called on bundle header");
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +0000869 for (MachineBasicBlock::const_instr_iterator MII = this;; ++MII) {
Benjamin Kramer97f889f2012-03-17 17:03:45 +0000870 if (MII->getDesc().getFlags() & Mask) {
Evan Chengcdf89fd2011-12-08 19:23:10 +0000871 if (Type == AnyInBundle)
Evan Cheng7f8e5632011-12-07 07:15:52 +0000872 return true;
873 } else {
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +0000874 if (Type == AllInBundle && !MII->isBundle())
Evan Cheng7f8e5632011-12-07 07:15:52 +0000875 return false;
876 }
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +0000877 // This was the last instruction in the bundle.
878 if (!MII->isBundledWithSucc())
879 return Type == AllInBundle;
Evan Cheng2a81dd42011-12-06 22:12:01 +0000880 }
Evan Cheng2a81dd42011-12-06 22:12:01 +0000881}
882
Evan Chenge9c46c22010-03-03 01:44:33 +0000883bool MachineInstr::isIdenticalTo(const MachineInstr *Other,
884 MICheckType Check) const {
Evan Cheng0f260e12010-03-03 21:54:14 +0000885 // If opcodes or number of operands are not the same then the two
886 // instructions are obviously not identical.
887 if (Other->getOpcode() != getOpcode() ||
888 Other->getNumOperands() != getNumOperands())
889 return false;
890
Evan Cheng7fae11b2011-12-14 02:11:42 +0000891 if (isBundle()) {
892 // Both instructions are bundles, compare MIs inside the bundle.
893 MachineBasicBlock::const_instr_iterator I1 = *this;
894 MachineBasicBlock::const_instr_iterator E1 = getParent()->instr_end();
895 MachineBasicBlock::const_instr_iterator I2 = *Other;
896 MachineBasicBlock::const_instr_iterator E2= Other->getParent()->instr_end();
897 while (++I1 != E1 && I1->isInsideBundle()) {
898 ++I2;
899 if (I2 == E2 || !I2->isInsideBundle() || !I1->isIdenticalTo(I2, Check))
900 return false;
901 }
902 }
903
Evan Cheng0f260e12010-03-03 21:54:14 +0000904 // Check operands to make sure they match.
905 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
906 const MachineOperand &MO = getOperand(i);
907 const MachineOperand &OMO = Other->getOperand(i);
Evan Chengcfdf3392011-05-12 00:56:58 +0000908 if (!MO.isReg()) {
909 if (!MO.isIdenticalTo(OMO))
910 return false;
911 continue;
912 }
913
Evan Cheng0f260e12010-03-03 21:54:14 +0000914 // Clients may or may not want to ignore defs when testing for equality.
915 // For example, machine CSE pass only cares about finding common
916 // subexpressions, so it's safe to ignore virtual register defs.
Evan Chengcfdf3392011-05-12 00:56:58 +0000917 if (MO.isDef()) {
Evan Cheng0f260e12010-03-03 21:54:14 +0000918 if (Check == IgnoreDefs)
919 continue;
Evan Chengcfdf3392011-05-12 00:56:58 +0000920 else if (Check == IgnoreVRegDefs) {
921 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) ||
922 TargetRegisterInfo::isPhysicalRegister(OMO.getReg()))
923 if (MO.getReg() != OMO.getReg())
924 return false;
925 } else {
926 if (!MO.isIdenticalTo(OMO))
Evan Cheng0f260e12010-03-03 21:54:14 +0000927 return false;
Evan Chengcfdf3392011-05-12 00:56:58 +0000928 if (Check == CheckKillDead && MO.isDead() != OMO.isDead())
929 return false;
930 }
931 } else {
932 if (!MO.isIdenticalTo(OMO))
933 return false;
934 if (Check == CheckKillDead && MO.isKill() != OMO.isKill())
935 return false;
936 }
Evan Cheng0f260e12010-03-03 21:54:14 +0000937 }
Devang Patelbf8cc602011-07-07 17:45:33 +0000938 // If DebugLoc does not match then two dbg.values are not identical.
939 if (isDebugValue())
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000940 if (getDebugLoc() && Other->getDebugLoc() &&
941 getDebugLoc() != Other->getDebugLoc())
Devang Patelbf8cc602011-07-07 17:45:33 +0000942 return false;
Evan Cheng0f260e12010-03-03 21:54:14 +0000943 return true;
Evan Chenge9c46c22010-03-03 01:44:33 +0000944}
945
Chris Lattnerbec79b42006-04-17 21:35:41 +0000946MachineInstr *MachineInstr::removeFromParent() {
947 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +0000948 return getParent()->remove(this);
Chris Lattnerbec79b42006-04-17 21:35:41 +0000949}
950
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +0000951MachineInstr *MachineInstr::removeFromBundle() {
952 assert(getParent() && "Not embedded in a basic block!");
953 return getParent()->remove_instr(this);
954}
Chris Lattnerbec79b42006-04-17 21:35:41 +0000955
Dan Gohman3b460302008-07-07 23:14:23 +0000956void MachineInstr::eraseFromParent() {
957 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +0000958 getParent()->erase(this);
Dan Gohman3b460302008-07-07 23:14:23 +0000959}
960
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +0000961void MachineInstr::eraseFromParentAndMarkDBGValuesForRemoval() {
962 assert(getParent() && "Not embedded in a basic block!");
963 MachineBasicBlock *MBB = getParent();
964 MachineFunction *MF = MBB->getParent();
965 assert(MF && "Not embedded in a function!");
966
967 MachineInstr *MI = (MachineInstr *)this;
968 MachineRegisterInfo &MRI = MF->getRegInfo();
969
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000970 for (const MachineOperand &MO : MI->operands()) {
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +0000971 if (!MO.isReg() || !MO.isDef())
972 continue;
973 unsigned Reg = MO.getReg();
974 if (!TargetRegisterInfo::isVirtualRegister(Reg))
975 continue;
976 MRI.markUsesInDebugValueAsUndef(Reg);
977 }
978 MI->eraseFromParent();
979}
980
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +0000981void MachineInstr::eraseFromBundle() {
982 assert(getParent() && "Not embedded in a basic block!");
983 getParent()->erase_instr(this);
984}
Dan Gohman3b460302008-07-07 23:14:23 +0000985
Evan Cheng4d728b02007-05-15 01:26:09 +0000986/// getNumExplicitOperands - Returns the number of non-implicit operands.
987///
988unsigned MachineInstr::getNumExplicitOperands() const {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000989 unsigned NumOperands = MCID->getNumOperands();
990 if (!MCID->isVariadic())
Evan Cheng4d728b02007-05-15 01:26:09 +0000991 return NumOperands;
992
Dan Gohman37608532009-04-15 17:59:11 +0000993 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
994 const MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000995 if (!MO.isReg() || !MO.isImplicit())
Evan Cheng4d728b02007-05-15 01:26:09 +0000996 NumOperands++;
997 }
998 return NumOperands;
999}
1000
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001001void MachineInstr::bundleWithPred() {
1002 assert(!isBundledWithPred() && "MI is already bundled with its predecessor");
1003 setFlag(BundledPred);
1004 MachineBasicBlock::instr_iterator Pred = this;
1005 --Pred;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001006 assert(!Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001007 Pred->setFlag(BundledSucc);
1008}
1009
1010void MachineInstr::bundleWithSucc() {
1011 assert(!isBundledWithSucc() && "MI is already bundled with its successor");
1012 setFlag(BundledSucc);
1013 MachineBasicBlock::instr_iterator Succ = this;
1014 ++Succ;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001015 assert(!Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001016 Succ->setFlag(BundledPred);
1017}
1018
1019void MachineInstr::unbundleFromPred() {
1020 assert(isBundledWithPred() && "MI isn't bundled with its predecessor");
1021 clearFlag(BundledPred);
1022 MachineBasicBlock::instr_iterator Pred = this;
1023 --Pred;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001024 assert(Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001025 Pred->clearFlag(BundledSucc);
1026}
1027
1028void MachineInstr::unbundleFromSucc() {
1029 assert(isBundledWithSucc() && "MI isn't bundled with its successor");
1030 clearFlag(BundledSucc);
1031 MachineBasicBlock::instr_iterator Succ = this;
Sergei Larin3b46d7e2013-01-09 17:54:33 +00001032 ++Succ;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001033 assert(Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001034 Succ->clearFlag(BundledPred);
1035}
1036
Evan Cheng6eb516d2011-01-07 23:50:32 +00001037bool MachineInstr::isStackAligningInlineAsm() const {
1038 if (isInlineAsm()) {
1039 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1040 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
1041 return true;
1042 }
1043 return false;
1044}
Chris Lattner33f5af02006-10-20 22:39:59 +00001045
Chad Rosier994f4042012-09-05 21:00:58 +00001046InlineAsm::AsmDialect MachineInstr::getInlineAsmDialect() const {
1047 assert(isInlineAsm() && "getInlineAsmDialect() only works for inline asms!");
1048 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
Chad Rosiere53314f2012-09-05 22:40:13 +00001049 return InlineAsm::AsmDialect((ExtraInfo & InlineAsm::Extra_AsmDialect) != 0);
Chad Rosier994f4042012-09-05 21:00:58 +00001050}
1051
Jakob Stoklund Olesen1e737162011-10-12 23:37:33 +00001052int MachineInstr::findInlineAsmFlagIdx(unsigned OpIdx,
1053 unsigned *GroupNo) const {
1054 assert(isInlineAsm() && "Expected an inline asm instruction");
1055 assert(OpIdx < getNumOperands() && "OpIdx out of range");
1056
1057 // Ignore queries about the initial operands.
1058 if (OpIdx < InlineAsm::MIOp_FirstOperand)
1059 return -1;
1060
1061 unsigned Group = 0;
1062 unsigned NumOps;
1063 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
1064 i += NumOps) {
1065 const MachineOperand &FlagMO = getOperand(i);
1066 // If we reach the implicit register operands, stop looking.
1067 if (!FlagMO.isImm())
1068 return -1;
1069 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
1070 if (i + NumOps > OpIdx) {
1071 if (GroupNo)
1072 *GroupNo = Group;
1073 return i;
1074 }
1075 ++Group;
1076 }
1077 return -1;
1078}
1079
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001080const TargetRegisterClass*
1081MachineInstr::getRegClassConstraint(unsigned OpIdx,
1082 const TargetInstrInfo *TII,
1083 const TargetRegisterInfo *TRI) const {
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001084 assert(getParent() && "Can't have an MBB reference here!");
1085 assert(getParent()->getParent() && "Can't have an MF reference here!");
1086 const MachineFunction &MF = *getParent()->getParent();
1087
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001088 // Most opcodes have fixed constraints in their MCInstrDesc.
1089 if (!isInlineAsm())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001090 return TII->getRegClass(getDesc(), OpIdx, TRI, MF);
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001091
1092 if (!getOperand(OpIdx).isReg())
Craig Topperc0196b12014-04-14 00:51:57 +00001093 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001094
1095 // For tied uses on inline asm, get the constraint from the def.
1096 unsigned DefIdx;
1097 if (getOperand(OpIdx).isUse() && isRegTiedToDefOperand(OpIdx, &DefIdx))
1098 OpIdx = DefIdx;
1099
1100 // Inline asm stores register class constraints in the flag word.
1101 int FlagIdx = findInlineAsmFlagIdx(OpIdx);
1102 if (FlagIdx < 0)
Craig Topperc0196b12014-04-14 00:51:57 +00001103 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001104
1105 unsigned Flag = getOperand(FlagIdx).getImm();
1106 unsigned RCID;
1107 if (InlineAsm::hasRegClassConstraint(Flag, RCID))
1108 return TRI->getRegClass(RCID);
1109
1110 // Assume that all registers in a memory operand are pointers.
1111 if (InlineAsm::getKind(Flag) == InlineAsm::Kind_Mem)
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001112 return TRI->getPointerRegClass(MF);
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001113
Craig Topperc0196b12014-04-14 00:51:57 +00001114 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001115}
1116
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001117const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVReg(
1118 unsigned Reg, const TargetRegisterClass *CurRC, const TargetInstrInfo *TII,
1119 const TargetRegisterInfo *TRI, bool ExploreBundle) const {
1120 // Check every operands inside the bundle if we have
1121 // been asked to.
1122 if (ExploreBundle)
1123 for (ConstMIBundleOperands OpndIt(this); OpndIt.isValid() && CurRC;
1124 ++OpndIt)
1125 CurRC = OpndIt->getParent()->getRegClassConstraintEffectForVRegImpl(
1126 OpndIt.getOperandNo(), Reg, CurRC, TII, TRI);
1127 else
1128 // Otherwise, just check the current operands.
Matthias Braune41e1462015-05-29 02:56:46 +00001129 for (unsigned i = 0, e = NumOperands; i < e && CurRC; ++i)
1130 CurRC = getRegClassConstraintEffectForVRegImpl(i, Reg, CurRC, TII, TRI);
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001131 return CurRC;
1132}
1133
1134const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVRegImpl(
1135 unsigned OpIdx, unsigned Reg, const TargetRegisterClass *CurRC,
1136 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
1137 assert(CurRC && "Invalid initial register class");
1138 // Check if Reg is constrained by some of its use/def from MI.
1139 const MachineOperand &MO = getOperand(OpIdx);
1140 if (!MO.isReg() || MO.getReg() != Reg)
1141 return CurRC;
1142 // If yes, accumulate the constraints through the operand.
1143 return getRegClassConstraintEffect(OpIdx, CurRC, TII, TRI);
1144}
1145
1146const TargetRegisterClass *MachineInstr::getRegClassConstraintEffect(
1147 unsigned OpIdx, const TargetRegisterClass *CurRC,
1148 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
1149 const TargetRegisterClass *OpRC = getRegClassConstraint(OpIdx, TII, TRI);
1150 const MachineOperand &MO = getOperand(OpIdx);
1151 assert(MO.isReg() &&
1152 "Cannot get register constraints for non-register operand");
1153 assert(CurRC && "Invalid initial register class");
1154 if (unsigned SubIdx = MO.getSubReg()) {
1155 if (OpRC)
1156 CurRC = TRI->getMatchingSuperRegClass(CurRC, OpRC, SubIdx);
1157 else
1158 CurRC = TRI->getSubClassWithSubReg(CurRC, SubIdx);
1159 } else if (OpRC)
1160 CurRC = TRI->getCommonSubClass(CurRC, OpRC);
1161 return CurRC;
1162}
1163
Jakob Stoklund Olesen68d752b2013-01-09 18:28:16 +00001164/// Return the number of instructions inside the MI bundle, not counting the
1165/// header instruction.
Evan Cheng7fae11b2011-12-14 02:11:42 +00001166unsigned MachineInstr::getBundleSize() const {
Jakob Stoklund Olesen68d752b2013-01-09 18:28:16 +00001167 MachineBasicBlock::const_instr_iterator I = this;
Evan Cheng7fae11b2011-12-14 02:11:42 +00001168 unsigned Size = 0;
Jakob Stoklund Olesen68d752b2013-01-09 18:28:16 +00001169 while (I->isBundledWithSucc())
1170 ++Size, ++I;
Evan Cheng7fae11b2011-12-14 02:11:42 +00001171 return Size;
1172}
1173
Evan Cheng910c8082007-04-26 19:00:32 +00001174/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Jim Grosbach9632c142009-09-17 17:57:26 +00001175/// the specific register or -1 if it is not found. It further tightens
Evan Cheng9965aeb2007-02-23 01:04:26 +00001176/// the search criteria to a use that kills the register if isKill is true.
Evan Cheng63254462008-03-05 00:59:57 +00001177int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
1178 const TargetRegisterInfo *TRI) const {
Evan Cheng75c21942006-12-06 08:27:42 +00001179 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng5983bdb2007-05-29 18:35:22 +00001180 const MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001181 if (!MO.isReg() || !MO.isUse())
Evan Cheng63254462008-03-05 00:59:57 +00001182 continue;
1183 unsigned MOReg = MO.getReg();
1184 if (!MOReg)
1185 continue;
1186 if (MOReg == Reg ||
1187 (TRI &&
1188 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
1189 TargetRegisterInfo::isPhysicalRegister(Reg) &&
1190 TRI->isSubRegister(MOReg, Reg)))
Evan Cheng9965aeb2007-02-23 01:04:26 +00001191 if (!isKill || MO.isKill())
Evan Chengec3ac312007-03-26 22:37:45 +00001192 return i;
Evan Cheng75c21942006-12-06 08:27:42 +00001193 }
Evan Chengec3ac312007-03-26 22:37:45 +00001194 return -1;
Evan Cheng75c21942006-12-06 08:27:42 +00001195}
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001196
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001197/// readsWritesVirtualRegister - Return a pair of bools (reads, writes)
1198/// indicating if this instruction reads or writes Reg. This also considers
1199/// partial defines.
1200std::pair<bool,bool>
1201MachineInstr::readsWritesVirtualRegister(unsigned Reg,
1202 SmallVectorImpl<unsigned> *Ops) const {
1203 bool PartDef = false; // Partial redefine.
1204 bool FullDef = false; // Full define.
1205 bool Use = false;
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001206
1207 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1208 const MachineOperand &MO = getOperand(i);
1209 if (!MO.isReg() || MO.getReg() != Reg)
1210 continue;
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001211 if (Ops)
1212 Ops->push_back(i);
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001213 if (MO.isUse())
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001214 Use |= !MO.isUndef();
Jakob Stoklund Olesen9eb77bf2011-08-19 00:30:17 +00001215 else if (MO.getSubReg() && !MO.isUndef())
1216 // A partial <def,undef> doesn't count as reading the register.
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001217 PartDef = true;
1218 else
1219 FullDef = true;
1220 }
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001221 // A partial redefine uses Reg unless there is also a full define.
1222 return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef);
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001223}
1224
Evan Cheng63254462008-03-05 00:59:57 +00001225/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman72a0bc12008-05-06 00:20:10 +00001226/// the specified register or -1 if it is not found. If isDead is true, defs
1227/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
1228/// also checks if there is a def of a super-register.
Evan Cheng38584512010-05-21 20:53:24 +00001229int
1230MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, bool Overlap,
1231 const TargetRegisterInfo *TRI) const {
1232 bool isPhys = TargetRegisterInfo::isPhysicalRegister(Reg);
Evan Chengf7ed82d2007-02-19 21:49:54 +00001233 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng63254462008-03-05 00:59:57 +00001234 const MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesene7d3f442012-02-14 23:49:37 +00001235 // Accept regmask operands when Overlap is set.
1236 // Ignore them when looking for a specific def operand (Overlap == false).
1237 if (isPhys && Overlap && MO.isRegMask() && MO.clobbersPhysReg(Reg))
1238 return i;
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001239 if (!MO.isReg() || !MO.isDef())
Evan Cheng63254462008-03-05 00:59:57 +00001240 continue;
1241 unsigned MOReg = MO.getReg();
Evan Cheng38584512010-05-21 20:53:24 +00001242 bool Found = (MOReg == Reg);
1243 if (!Found && TRI && isPhys &&
1244 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
1245 if (Overlap)
1246 Found = TRI->regsOverlap(MOReg, Reg);
1247 else
1248 Found = TRI->isSubRegister(MOReg, Reg);
1249 }
1250 if (Found && (!isDead || MO.isDead()))
1251 return i;
Evan Chengf7ed82d2007-02-19 21:49:54 +00001252 }
Evan Cheng63254462008-03-05 00:59:57 +00001253 return -1;
Evan Chengf7ed82d2007-02-19 21:49:54 +00001254}
Evan Cheng4d728b02007-05-15 01:26:09 +00001255
Evan Cheng5983bdb2007-05-29 18:35:22 +00001256/// findFirstPredOperandIdx() - Find the index of the first operand in the
1257/// operand list that is used to represent the predicate. It returns -1 if
1258/// none is found.
1259int MachineInstr::findFirstPredOperandIdx() const {
Jim Grosbached16ec42011-08-29 22:24:09 +00001260 // Don't call MCID.findFirstPredOperandIdx() because this variant
1261 // is sometimes called on an instruction that's not yet complete, and
1262 // so the number of operands is less than the MCID indicates. In
1263 // particular, the PTX target does this.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001264 const MCInstrDesc &MCID = getDesc();
1265 if (MCID.isPredicable()) {
Evan Cheng4d728b02007-05-15 01:26:09 +00001266 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Evan Cheng6cc775f2011-06-28 19:10:37 +00001267 if (MCID.OpInfo[i].isPredicate())
Evan Cheng5983bdb2007-05-29 18:35:22 +00001268 return i;
Evan Cheng4d728b02007-05-15 01:26:09 +00001269 }
1270
Evan Cheng5983bdb2007-05-29 18:35:22 +00001271 return -1;
Evan Cheng4d728b02007-05-15 01:26:09 +00001272}
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001273
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001274// MachineOperand::TiedTo is 4 bits wide.
1275const unsigned TiedMax = 15;
1276
1277/// tieOperands - Mark operands at DefIdx and UseIdx as tied to each other.
1278///
1279/// Use and def operands can be tied together, indicated by a non-zero TiedTo
1280/// field. TiedTo can have these values:
1281///
1282/// 0: Operand is not tied to anything.
1283/// 1 to TiedMax-1: Tied to getOperand(TiedTo-1).
1284/// TiedMax: Tied to an operand >= TiedMax-1.
1285///
1286/// The tied def must be one of the first TiedMax operands on a normal
1287/// instruction. INLINEASM instructions allow more tied defs.
1288///
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001289void MachineInstr::tieOperands(unsigned DefIdx, unsigned UseIdx) {
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001290 MachineOperand &DefMO = getOperand(DefIdx);
1291 MachineOperand &UseMO = getOperand(UseIdx);
1292 assert(DefMO.isDef() && "DefIdx must be a def operand");
1293 assert(UseMO.isUse() && "UseIdx must be a use operand");
1294 assert(!DefMO.isTied() && "Def is already tied to another use");
1295 assert(!UseMO.isTied() && "Use is already tied to another def");
1296
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001297 if (DefIdx < TiedMax)
1298 UseMO.TiedTo = DefIdx + 1;
1299 else {
1300 // Inline asm can use the group descriptors to find tied operands, but on
1301 // normal instruction, the tied def must be within the first TiedMax
1302 // operands.
1303 assert(isInlineAsm() && "DefIdx out of range");
1304 UseMO.TiedTo = TiedMax;
1305 }
1306
1307 // UseIdx can be out of range, we'll search for it in findTiedOperandIdx().
1308 DefMO.TiedTo = std::min(UseIdx + 1, TiedMax);
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001309}
1310
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001311/// Given the index of a tied register operand, find the operand it is tied to.
1312/// Defs are tied to uses and vice versa. Returns the index of the tied operand
1313/// which must exist.
1314unsigned MachineInstr::findTiedOperandIdx(unsigned OpIdx) const {
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001315 const MachineOperand &MO = getOperand(OpIdx);
1316 assert(MO.isTied() && "Operand isn't tied");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001317
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001318 // Normally TiedTo is in range.
1319 if (MO.TiedTo < TiedMax)
1320 return MO.TiedTo - 1;
1321
1322 // Uses on normal instructions can be out of range.
1323 if (!isInlineAsm()) {
1324 // Normal tied defs must be in the 0..TiedMax-1 range.
1325 if (MO.isUse())
1326 return TiedMax - 1;
1327 // MO is a def. Search for the tied use.
1328 for (unsigned i = TiedMax - 1, e = getNumOperands(); i != e; ++i) {
1329 const MachineOperand &UseMO = getOperand(i);
1330 if (UseMO.isReg() && UseMO.isUse() && UseMO.TiedTo == OpIdx + 1)
1331 return i;
1332 }
1333 llvm_unreachable("Can't find tied use");
1334 }
1335
1336 // Now deal with inline asm by parsing the operand group descriptor flags.
1337 // Find the beginning of each operand group.
1338 SmallVector<unsigned, 8> GroupIdx;
1339 unsigned OpIdxGroup = ~0u;
1340 unsigned NumOps;
1341 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
1342 i += NumOps) {
1343 const MachineOperand &FlagMO = getOperand(i);
1344 assert(FlagMO.isImm() && "Invalid tied operand on inline asm");
1345 unsigned CurGroup = GroupIdx.size();
1346 GroupIdx.push_back(i);
1347 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
1348 // OpIdx belongs to this operand group.
1349 if (OpIdx > i && OpIdx < i + NumOps)
1350 OpIdxGroup = CurGroup;
1351 unsigned TiedGroup;
1352 if (!InlineAsm::isUseOperandTiedToDef(FlagMO.getImm(), TiedGroup))
1353 continue;
1354 // Operands in this group are tied to operands in TiedGroup which must be
1355 // earlier. Find the number of operands between the two groups.
1356 unsigned Delta = i - GroupIdx[TiedGroup];
1357
1358 // OpIdx is a use tied to TiedGroup.
1359 if (OpIdxGroup == CurGroup)
1360 return OpIdx - Delta;
1361
1362 // OpIdx is a def tied to this use group.
1363 if (OpIdxGroup == TiedGroup)
1364 return OpIdx + Delta;
1365 }
1366 llvm_unreachable("Invalid tied operand on inline asm");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001367}
1368
Dan Gohmanc90f51c2010-05-13 20:34:42 +00001369/// clearKillInfo - Clears kill flags on all operands.
1370///
1371void MachineInstr::clearKillInfo() {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001372 for (MachineOperand &MO : operands()) {
Dan Gohmanc90f51c2010-05-13 20:34:42 +00001373 if (MO.isReg() && MO.isUse())
1374 MO.setIsKill(false);
1375 }
1376}
1377
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001378void MachineInstr::substituteRegister(unsigned FromReg,
1379 unsigned ToReg,
1380 unsigned SubIdx,
1381 const TargetRegisterInfo &RegInfo) {
1382 if (TargetRegisterInfo::isPhysicalRegister(ToReg)) {
1383 if (SubIdx)
1384 ToReg = RegInfo.getSubReg(ToReg, SubIdx);
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001385 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001386 if (!MO.isReg() || MO.getReg() != FromReg)
1387 continue;
1388 MO.substPhysReg(ToReg, RegInfo);
1389 }
1390 } else {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001391 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001392 if (!MO.isReg() || MO.getReg() != FromReg)
1393 continue;
1394 MO.substVirtReg(ToReg, SubIdx, RegInfo);
1395 }
1396 }
1397}
1398
Evan Cheng7d98a482008-07-03 09:09:37 +00001399/// isSafeToMove - Return true if it is safe to move this instruction. If
1400/// SawStore is set to true, it means that there is a store (or call) between
1401/// the instruction's location and its intended destination.
Matthias Braun07066cc2015-05-19 21:22:20 +00001402bool MachineInstr::isSafeToMove(AliasAnalysis *AA, bool &SawStore) const {
Evan Cheng399e1102008-03-13 00:44:09 +00001403 // Ignore stuff that we obviously can't move.
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001404 //
1405 // Treat volatile loads as stores. This is not strictly necessary for
Jakob Stoklund Olesend92e2bc2012-09-04 18:44:43 +00001406 // volatiles, but it is required for atomic loads. It is not allowed to move
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001407 // a load across an atomic load with Ordering > Monotonic.
1408 if (mayStore() || isCall() ||
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001409 (mayLoad() && hasOrderedMemoryRef())) {
Evan Cheng399e1102008-03-13 00:44:09 +00001410 SawStore = true;
1411 return false;
1412 }
Evan Cheng0638c202011-01-07 21:08:26 +00001413
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001414 if (isPosition() || isDebugValue() || isTerminator() ||
1415 hasUnmodeledSideEffects())
Evan Cheng399e1102008-03-13 00:44:09 +00001416 return false;
1417
1418 // See if this instruction does a load. If so, we have to guarantee that the
1419 // loaded value doesn't change between the load and the its intended
1420 // destination. The check for isInvariantLoad gives the targe the chance to
1421 // classify the load as always returning a constant, e.g. a constant pool
1422 // load.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001423 if (mayLoad() && !isInvariantLoad(AA))
Evan Cheng399e1102008-03-13 00:44:09 +00001424 // Otherwise, this is a real load. If there is a store between the load and
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001425 // end of block, we can't move it.
1426 return !SawStore;
Dan Gohman7c59ed62008-09-24 00:06:15 +00001427
Evan Cheng399e1102008-03-13 00:44:09 +00001428 return true;
1429}
1430
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001431/// hasOrderedMemoryRef - Return true if this instruction may have an ordered
1432/// or volatile memory reference, or if the information describing the memory
1433/// reference is not available. Return false if it is known to have no ordered
1434/// memory references.
1435bool MachineInstr::hasOrderedMemoryRef() const {
Dan Gohman7c59ed62008-09-24 00:06:15 +00001436 // An instruction known never to access memory won't have a volatile access.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001437 if (!mayStore() &&
1438 !mayLoad() &&
1439 !isCall() &&
Evan Cheng6eb516d2011-01-07 23:50:32 +00001440 !hasUnmodeledSideEffects())
Dan Gohman7c59ed62008-09-24 00:06:15 +00001441 return false;
1442
1443 // Otherwise, if the instruction has no memory reference information,
1444 // conservatively assume it wasn't preserved.
1445 if (memoperands_empty())
1446 return true;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001447
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001448 // Check the memory reference information for ordered references.
Dan Gohman48b185d2009-09-25 20:36:54 +00001449 for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I)
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001450 if (!(*I)->isUnordered())
Dan Gohman7c59ed62008-09-24 00:06:15 +00001451 return true;
1452
1453 return false;
1454}
1455
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001456/// isInvariantLoad - Return true if this instruction is loading from a
1457/// location whose value is invariant across the function. For example,
Dan Gohman4a618822010-02-10 16:03:48 +00001458/// loading a value from the constant pool or from the argument area
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001459/// of a function if it does not change. This should only return true of
1460/// *all* loads the instruction does are invariant (if it does multiple loads).
1461bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const {
1462 // If the instruction doesn't load at all, it isn't an invariant load.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001463 if (!mayLoad())
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001464 return false;
1465
1466 // If the instruction has lost its memoperands, conservatively assume that
1467 // it may not be an invariant load.
1468 if (memoperands_empty())
1469 return false;
1470
1471 const MachineFrameInfo *MFI = getParent()->getParent()->getFrameInfo();
1472
1473 for (mmo_iterator I = memoperands_begin(),
1474 E = memoperands_end(); I != E; ++I) {
1475 if ((*I)->isVolatile()) return false;
1476 if ((*I)->isStore()) return false;
Pete Cooper82cd9e82011-11-08 18:42:53 +00001477 if ((*I)->isInvariant()) return true;
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001478
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001479
1480 // A load from a constant PseudoSourceValue is invariant.
1481 if (const PseudoSourceValue *PSV = (*I)->getPseudoValue())
1482 if (PSV->isConstant(MFI))
1483 continue;
1484
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001485 if (const Value *V = (*I)->getValue()) {
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001486 // If we have an AliasAnalysis, ask it whether the memory is constant.
Chandler Carruthac80dc72015-06-17 07:18:54 +00001487 if (AA &&
1488 AA->pointsToConstantMemory(
1489 MemoryLocation(V, (*I)->getSize(), (*I)->getAAInfo())))
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001490 continue;
1491 }
1492
1493 // Otherwise assume conservatively.
1494 return false;
1495 }
1496
1497 // Everything checks out.
1498 return true;
1499}
1500
Evan Cheng71453822009-12-03 02:31:43 +00001501/// isConstantValuePHI - If the specified instruction is a PHI that always
1502/// merges together the same virtual register, return the register, otherwise
1503/// return 0.
1504unsigned MachineInstr::isConstantValuePHI() const {
Chris Lattnerb06015a2010-02-09 19:54:29 +00001505 if (!isPHI())
Evan Cheng71453822009-12-03 02:31:43 +00001506 return 0;
Evan Cheng5c668a22009-12-07 23:10:34 +00001507 assert(getNumOperands() >= 3 &&
1508 "It's illegal to have a PHI without source operands");
Evan Cheng71453822009-12-03 02:31:43 +00001509
1510 unsigned Reg = getOperand(1).getReg();
1511 for (unsigned i = 3, e = getNumOperands(); i < e; i += 2)
1512 if (getOperand(i).getReg() != Reg)
1513 return 0;
1514 return Reg;
1515}
1516
Evan Cheng6eb516d2011-01-07 23:50:32 +00001517bool MachineInstr::hasUnmodeledSideEffects() const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00001518 if (hasProperty(MCID::UnmodeledSideEffects))
Evan Cheng6eb516d2011-01-07 23:50:32 +00001519 return true;
1520 if (isInlineAsm()) {
1521 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1522 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1523 return true;
1524 }
1525
1526 return false;
1527}
1528
Michael Kupersteinbc7f99a2015-08-12 10:14:58 +00001529bool MachineInstr::isLoadFoldBarrier() const {
1530 return mayStore() || isCall() || hasUnmodeledSideEffects();
1531}
1532
Evan Chengb083c472010-04-08 20:02:37 +00001533/// allDefsAreDead - Return true if all the defs of this instruction are dead.
1534///
1535bool MachineInstr::allDefsAreDead() const {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001536 for (const MachineOperand &MO : operands()) {
Evan Chengb083c472010-04-08 20:02:37 +00001537 if (!MO.isReg() || MO.isUse())
1538 continue;
1539 if (!MO.isDead())
1540 return false;
1541 }
1542 return true;
1543}
1544
Evan Cheng21eedfb2010-10-22 21:49:09 +00001545/// copyImplicitOps - Copy implicit register operands from specified
1546/// instruction to this instruction.
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001547void MachineInstr::copyImplicitOps(MachineFunction &MF,
1548 const MachineInstr *MI) {
Evan Cheng21eedfb2010-10-22 21:49:09 +00001549 for (unsigned i = MI->getDesc().getNumOperands(), e = MI->getNumOperands();
1550 i != e; ++i) {
1551 const MachineOperand &MO = MI->getOperand(i);
Lang Hames7c8189c2014-03-17 01:22:54 +00001552 if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask())
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001553 addOperand(MF, MO);
Evan Cheng21eedfb2010-10-22 21:49:09 +00001554 }
1555}
1556
Brian Gaekee8f7c2f2004-02-13 04:39:32 +00001557void MachineInstr::dump() const {
Manman Ren19f49ac2012-09-11 22:23:19 +00001558#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
David Greene29388d62010-01-04 23:48:20 +00001559 dbgs() << " " << *this;
Manman Ren742534c2012-09-06 19:06:06 +00001560#endif
Mon P Wangdfcc1ff2008-10-10 01:43:55 +00001561}
1562
Eric Christopher1cdefae2015-02-27 00:11:34 +00001563void MachineInstr::print(raw_ostream &OS, bool SkipOpers) const {
Duncan P. N. Exon Smithc0374522015-06-26 23:18:44 +00001564 const Module *M = nullptr;
1565 if (const MachineBasicBlock *MBB = getParent())
1566 if (const MachineFunction *MF = MBB->getParent())
1567 M = MF->getFunction()->getParent();
1568
1569 ModuleSlotTracker MST(M);
1570 print(OS, MST, SkipOpers);
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001571}
1572
1573void MachineInstr::print(raw_ostream &OS, ModuleSlotTracker &MST,
1574 bool SkipOpers) const {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001575 // We can be a bit tidier if we know the MachineFunction.
Craig Topperc0196b12014-04-14 00:51:57 +00001576 const MachineFunction *MF = nullptr;
Eric Christopher1cdefae2015-02-27 00:11:34 +00001577 const TargetRegisterInfo *TRI = nullptr;
Craig Topperc0196b12014-04-14 00:51:57 +00001578 const MachineRegisterInfo *MRI = nullptr;
Eric Christopher1cdefae2015-02-27 00:11:34 +00001579 const TargetInstrInfo *TII = nullptr;
Dan Gohman2745d192009-11-09 19:38:45 +00001580 if (const MachineBasicBlock *MBB = getParent()) {
1581 MF = MBB->getParent();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001582 if (MF) {
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001583 MRI = &MF->getRegInfo();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001584 TRI = MF->getSubtarget().getRegisterInfo();
1585 TII = MF->getSubtarget().getInstrInfo();
1586 }
Dan Gohman2745d192009-11-09 19:38:45 +00001587 }
Dan Gohman34341e62009-10-31 20:19:03 +00001588
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001589 // Save a list of virtual registers.
1590 SmallVector<unsigned, 8> VirtRegs;
1591
Dan Gohman34341e62009-10-31 20:19:03 +00001592 // Print explicitly defined operands on the left of an assignment syntax.
Dan Gohman2745d192009-11-09 19:38:45 +00001593 unsigned StartOp = 0, e = getNumOperands();
Dan Gohman34341e62009-10-31 20:19:03 +00001594 for (; StartOp < e && getOperand(StartOp).isReg() &&
1595 getOperand(StartOp).isDef() &&
1596 !getOperand(StartOp).isImplicit();
1597 ++StartOp) {
1598 if (StartOp != 0) OS << ", ";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001599 getOperand(StartOp).print(OS, MST, TRI);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001600 unsigned Reg = getOperand(StartOp).getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001601 if (TargetRegisterInfo::isVirtualRegister(Reg))
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001602 VirtRegs.push_back(Reg);
Chris Lattnerac6e9742002-10-30 01:55:38 +00001603 }
Tanya Lattner23dbc812004-06-25 00:13:11 +00001604
Dan Gohman34341e62009-10-31 20:19:03 +00001605 if (StartOp != 0)
1606 OS << " = ";
1607
1608 // Print the opcode name.
Eric Christopher1cdefae2015-02-27 00:11:34 +00001609 if (TII)
1610 OS << TII->getName(getOpcode());
Benjamin Kramerbf152d52012-02-10 13:18:44 +00001611 else
1612 OS << "UNKNOWN";
Misha Brukman835702a2005-04-21 22:36:52 +00001613
Andrew Trickb36388a2013-01-25 07:45:25 +00001614 if (SkipOpers)
1615 return;
1616
Dan Gohman34341e62009-10-31 20:19:03 +00001617 // Print the rest of the operands.
Dan Gohman2745d192009-11-09 19:38:45 +00001618 bool OmittedAnyCallClobbers = false;
1619 bool FirstOp = true;
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001620 unsigned AsmDescOp = ~0u;
1621 unsigned AsmOpCount = 0;
Evan Cheng6eb516d2011-01-07 23:50:32 +00001622
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +00001623 if (isInlineAsm() && e >= InlineAsm::MIOp_FirstOperand) {
Evan Cheng6eb516d2011-01-07 23:50:32 +00001624 // Print asm string.
1625 OS << " ";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001626 getOperand(InlineAsm::MIOp_AsmString).print(OS, MST, TRI);
Evan Cheng6eb516d2011-01-07 23:50:32 +00001627
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001628 // Print HasSideEffects, MayLoad, MayStore, IsAlignStack
Evan Cheng6eb516d2011-01-07 23:50:32 +00001629 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1630 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1631 OS << " [sideeffect]";
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001632 if (ExtraInfo & InlineAsm::Extra_MayLoad)
1633 OS << " [mayload]";
1634 if (ExtraInfo & InlineAsm::Extra_MayStore)
1635 OS << " [maystore]";
Evan Cheng6eb516d2011-01-07 23:50:32 +00001636 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
1637 OS << " [alignstack]";
Chad Rosiercbd2a192012-09-05 22:17:43 +00001638 if (getInlineAsmDialect() == InlineAsm::AD_ATT)
Chad Rosier994f4042012-09-05 21:00:58 +00001639 OS << " [attdialect]";
Chad Rosiercbd2a192012-09-05 22:17:43 +00001640 if (getInlineAsmDialect() == InlineAsm::AD_Intel)
Chad Rosier994f4042012-09-05 21:00:58 +00001641 OS << " [inteldialect]";
Evan Cheng6eb516d2011-01-07 23:50:32 +00001642
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001643 StartOp = AsmDescOp = InlineAsm::MIOp_FirstOperand;
Evan Cheng6eb516d2011-01-07 23:50:32 +00001644 FirstOp = false;
1645 }
1646
Chris Lattnerac6e9742002-10-30 01:55:38 +00001647 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohman2745d192009-11-09 19:38:45 +00001648 const MachineOperand &MO = getOperand(i);
1649
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001650 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001651 VirtRegs.push_back(MO.getReg());
1652
Dan Gohman2745d192009-11-09 19:38:45 +00001653 // Omit call-clobbered registers which aren't used anywhere. This makes
1654 // call instructions much less noisy on targets where calls clobber lots
1655 // of registers. Don't rely on MO.isDead() because we may be called before
1656 // LiveVariables is run, or we may be looking at a non-allocatable reg.
Craig Toppercf0444b2014-11-17 05:50:14 +00001657 if (MRI && isCall() &&
Dan Gohman2745d192009-11-09 19:38:45 +00001658 MO.isReg() && MO.isImplicit() && MO.isDef()) {
1659 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001660 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Craig Toppercf0444b2014-11-17 05:50:14 +00001661 if (MRI->use_empty(Reg)) {
Dan Gohman2745d192009-11-09 19:38:45 +00001662 bool HasAliasLive = false;
Eric Christopher1cdefae2015-02-27 00:11:34 +00001663 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001664 unsigned AliasReg = *AI;
Craig Toppercf0444b2014-11-17 05:50:14 +00001665 if (!MRI->use_empty(AliasReg)) {
Dan Gohman2745d192009-11-09 19:38:45 +00001666 HasAliasLive = true;
1667 break;
1668 }
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001669 }
Dan Gohman2745d192009-11-09 19:38:45 +00001670 if (!HasAliasLive) {
1671 OmittedAnyCallClobbers = true;
1672 continue;
1673 }
1674 }
1675 }
1676 }
1677
1678 if (FirstOp) FirstOp = false; else OS << ",";
Chris Lattnerac6e9742002-10-30 01:55:38 +00001679 OS << " ";
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001680 if (i < getDesc().NumOperands) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001681 const MCOperandInfo &MCOI = getDesc().OpInfo[i];
1682 if (MCOI.isPredicate())
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001683 OS << "pred:";
Evan Cheng6cc775f2011-06-28 19:10:37 +00001684 if (MCOI.isOptionalDef())
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001685 OS << "opt:";
1686 }
Evan Chengd4d1a512010-04-28 20:03:13 +00001687 if (isDebugValue() && MO.isMetadata()) {
1688 // Pretty print DBG_VALUE instructions.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001689 auto *DIV = dyn_cast<DILocalVariable>(MO.getMetadata());
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +00001690 if (DIV && !DIV->getName().empty())
1691 OS << "!\"" << DIV->getName() << '\"';
Evan Chengd4d1a512010-04-28 20:03:13 +00001692 else
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001693 MO.print(OS, MST, TRI);
Eric Christopher1cdefae2015-02-27 00:11:34 +00001694 } else if (TRI && (isInsertSubreg() || isRegSequence()) && MO.isImm()) {
1695 OS << TRI->getSubRegIndexName(MO.getImm());
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001696 } else if (i == AsmDescOp && MO.isImm()) {
1697 // Pretty print the inline asm operand descriptor.
1698 OS << '$' << AsmOpCount++;
1699 unsigned Flag = MO.getImm();
1700 switch (InlineAsm::getKind(Flag)) {
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001701 case InlineAsm::Kind_RegUse: OS << ":[reguse"; break;
1702 case InlineAsm::Kind_RegDef: OS << ":[regdef"; break;
1703 case InlineAsm::Kind_RegDefEarlyClobber: OS << ":[regdef-ec"; break;
1704 case InlineAsm::Kind_Clobber: OS << ":[clobber"; break;
1705 case InlineAsm::Kind_Imm: OS << ":[imm"; break;
1706 case InlineAsm::Kind_Mem: OS << ":[mem"; break;
1707 default: OS << ":[??" << InlineAsm::getKind(Flag); break;
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001708 }
1709
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001710 unsigned RCID = 0;
Nick Lewycky84882252011-10-13 00:54:59 +00001711 if (InlineAsm::hasRegClassConstraint(Flag, RCID)) {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001712 if (TRI) {
1713 OS << ':' << TRI->getRegClassName(TRI->getRegClass(RCID));
Craig Toppercf0444b2014-11-17 05:50:14 +00001714 } else
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001715 OS << ":RC" << RCID;
Nick Lewycky84882252011-10-13 00:54:59 +00001716 }
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001717
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001718 unsigned TiedTo = 0;
1719 if (InlineAsm::isUseOperandTiedToDef(Flag, TiedTo))
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001720 OS << " tiedto:$" << TiedTo;
1721
1722 OS << ']';
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001723
1724 // Compute the index of the next operand descriptor.
1725 AsmDescOp += 1 + InlineAsm::getNumOperandRegisters(Flag);
Evan Chengd4d1a512010-04-28 20:03:13 +00001726 } else
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001727 MO.print(OS, MST, TRI);
Dan Gohman2745d192009-11-09 19:38:45 +00001728 }
1729
1730 // Briefly indicate whether any call clobbers were omitted.
1731 if (OmittedAnyCallClobbers) {
Bill Wendlingec030f22009-12-25 13:45:50 +00001732 if (!FirstOp) OS << ",";
Dan Gohman2745d192009-11-09 19:38:45 +00001733 OS << " ...";
Chris Lattner214808f2002-10-30 00:48:05 +00001734 }
Misha Brukman835702a2005-04-21 22:36:52 +00001735
Dan Gohman34341e62009-10-31 20:19:03 +00001736 bool HaveSemi = false;
Michael Kuperstein098cd9f2015-09-16 11:18:25 +00001737 const unsigned PrintableFlags = FrameSetup | FrameDestroy;
Jakob Stoklund Olesen6922e9c2013-01-09 18:35:09 +00001738 if (Flags & PrintableFlags) {
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001739 if (!HaveSemi) OS << ";"; HaveSemi = true;
1740 OS << " flags: ";
1741
1742 if (Flags & FrameSetup)
1743 OS << "FrameSetup";
Michael Kuperstein098cd9f2015-09-16 11:18:25 +00001744
1745 if (Flags & FrameDestroy)
1746 OS << "FrameDestroy";
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001747 }
1748
Dan Gohman3b460302008-07-07 23:14:23 +00001749 if (!memoperands_empty()) {
Dan Gohman34341e62009-10-31 20:19:03 +00001750 if (!HaveSemi) OS << ";"; HaveSemi = true;
1751
1752 OS << " mem:";
Dan Gohman48b185d2009-09-25 20:36:54 +00001753 for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
1754 i != e; ++i) {
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001755 (*i)->print(OS, MST);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001756 if (std::next(i) != e)
Dan Gohmanc0353bf2009-09-23 01:33:16 +00001757 OS << " ";
Dan Gohman2d489b52008-02-06 22:27:42 +00001758 }
1759 }
1760
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001761 // Print the regclass of any virtual registers encountered.
1762 if (MRI && !VirtRegs.empty()) {
1763 if (!HaveSemi) OS << ";"; HaveSemi = true;
1764 for (unsigned i = 0; i != VirtRegs.size(); ++i) {
1765 const TargetRegisterClass *RC = MRI->getRegClass(VirtRegs[i]);
Eric Christopher1cdefae2015-02-27 00:11:34 +00001766 OS << " " << TRI->getRegClassName(RC)
Craig Toppercf0444b2014-11-17 05:50:14 +00001767 << ':' << PrintReg(VirtRegs[i]);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001768 for (unsigned j = i+1; j != VirtRegs.size();) {
1769 if (MRI->getRegClass(VirtRegs[j]) != RC) {
1770 ++j;
1771 continue;
1772 }
1773 if (VirtRegs[i] != VirtRegs[j])
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +00001774 OS << "," << PrintReg(VirtRegs[j]);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001775 VirtRegs.erase(VirtRegs.begin()+j);
1776 }
1777 }
1778 }
1779
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001780 // Print debug location information.
Duncan P. N. Exon Smithc5bd3e02015-04-03 16:23:04 +00001781 if (isDebugValue() && getOperand(e - 2).isMetadata()) {
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001782 if (!HaveSemi) OS << ";";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001783 auto *DV = cast<DILocalVariable>(getOperand(e - 2).getMetadata());
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +00001784 OS << " line no:" << DV->getLine();
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001785 if (auto *InlinedAt = debugLoc->getInlinedAt()) {
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +00001786 DebugLoc InlinedAtDL(InlinedAt);
1787 if (InlinedAtDL && MF) {
Devang Pateld61b1d52011-08-04 20:44:26 +00001788 OS << " inlined @[ ";
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00001789 InlinedAtDL.print(OS);
Devang Pateld61b1d52011-08-04 20:44:26 +00001790 OS << " ]";
1791 }
1792 }
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001793 if (isIndirectDebugValue())
1794 OS << " indirect";
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +00001795 } else if (debugLoc && MF) {
Arnaud A. de Grandmaison75c9e6d2014-03-15 22:13:15 +00001796 if (!HaveSemi) OS << ";";
Dan Gohman2e3f1872009-11-23 21:29:08 +00001797 OS << " dbg:";
Eric Christopherb9f00092015-02-26 23:32:17 +00001798 debugLoc.print(OS);
Bill Wendling1a0a3d02009-02-19 21:44:55 +00001799 }
1800
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001801 OS << '\n';
Chris Lattner214808f2002-10-30 00:48:05 +00001802}
1803
Owen Anderson2a8a4852008-01-24 01:10:07 +00001804bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001805 const TargetRegisterInfo *RegInfo,
Owen Anderson2a8a4852008-01-24 01:10:07 +00001806 bool AddIfNotFound) {
Evan Cheng6c177732008-04-16 09:41:59 +00001807 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001808 bool hasAliases = isPhysReg &&
1809 MCRegAliasIterator(IncomingReg, RegInfo, false).isValid();
Dan Gohmanc7367b42008-09-03 15:56:16 +00001810 bool Found = false;
Evan Cheng6c177732008-04-16 09:41:59 +00001811 SmallVector<unsigned,4> DeadOps;
Bill Wendling7921ad02008-03-03 22:14:33 +00001812 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1813 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesenf465f062009-08-04 20:09:25 +00001814 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng6c177732008-04-16 09:41:59 +00001815 continue;
1816 unsigned Reg = MO.getReg();
1817 if (!Reg)
1818 continue;
Bill Wendling7921ad02008-03-03 22:14:33 +00001819
Evan Cheng6c177732008-04-16 09:41:59 +00001820 if (Reg == IncomingReg) {
Dan Gohmanc7367b42008-09-03 15:56:16 +00001821 if (!Found) {
1822 if (MO.isKill())
1823 // The register is already marked kill.
1824 return true;
Jakob Stoklund Olesenc59cd9b2009-08-02 19:13:03 +00001825 if (isPhysReg && isRegTiedToDefOperand(i))
1826 // Two-address uses of physregs must not be marked kill.
1827 return true;
Dan Gohmanc7367b42008-09-03 15:56:16 +00001828 MO.setIsKill();
1829 Found = true;
1830 }
1831 } else if (hasAliases && MO.isKill() &&
1832 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng6c177732008-04-16 09:41:59 +00001833 // A super-register kill already exists.
1834 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohmanb2612922008-07-03 01:18:51 +00001835 return true;
1836 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng6c177732008-04-16 09:41:59 +00001837 DeadOps.push_back(i);
Bill Wendling7921ad02008-03-03 22:14:33 +00001838 }
1839 }
1840
Evan Cheng6c177732008-04-16 09:41:59 +00001841 // Trim unneeded kill operands.
1842 while (!DeadOps.empty()) {
1843 unsigned OpIdx = DeadOps.back();
1844 if (getOperand(OpIdx).isImplicit())
1845 RemoveOperand(OpIdx);
1846 else
1847 getOperand(OpIdx).setIsKill(false);
1848 DeadOps.pop_back();
1849 }
1850
Bill Wendling7921ad02008-03-03 22:14:33 +00001851 // If not found, this means an alias of one of the operands is killed. Add a
Owen Anderson2a8a4852008-01-24 01:10:07 +00001852 // new implicit operand if required.
Dan Gohmanc7367b42008-09-03 15:56:16 +00001853 if (!Found && AddIfNotFound) {
Bill Wendling7921ad02008-03-03 22:14:33 +00001854 addOperand(MachineOperand::CreateReg(IncomingReg,
1855 false /*IsDef*/,
1856 true /*IsImp*/,
1857 true /*IsKill*/));
Owen Anderson2a8a4852008-01-24 01:10:07 +00001858 return true;
1859 }
Dan Gohmanc7367b42008-09-03 15:56:16 +00001860 return Found;
Owen Anderson2a8a4852008-01-24 01:10:07 +00001861}
1862
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00001863void MachineInstr::clearRegisterKills(unsigned Reg,
1864 const TargetRegisterInfo *RegInfo) {
1865 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topperc0196b12014-04-14 00:51:57 +00001866 RegInfo = nullptr;
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001867 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00001868 if (!MO.isReg() || !MO.isUse() || !MO.isKill())
1869 continue;
1870 unsigned OpReg = MO.getReg();
1871 if (OpReg == Reg || (RegInfo && RegInfo->isSuperRegister(Reg, OpReg)))
1872 MO.setIsKill(false);
1873 }
1874}
1875
Matthias Braun1965bfa2013-10-10 21:28:38 +00001876bool MachineInstr::addRegisterDead(unsigned Reg,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001877 const TargetRegisterInfo *RegInfo,
Owen Anderson2a8a4852008-01-24 01:10:07 +00001878 bool AddIfNotFound) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00001879 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(Reg);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001880 bool hasAliases = isPhysReg &&
Matthias Braun1965bfa2013-10-10 21:28:38 +00001881 MCRegAliasIterator(Reg, RegInfo, false).isValid();
Dan Gohmanc7367b42008-09-03 15:56:16 +00001882 bool Found = false;
Evan Cheng6c177732008-04-16 09:41:59 +00001883 SmallVector<unsigned,4> DeadOps;
Owen Anderson2a8a4852008-01-24 01:10:07 +00001884 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1885 MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001886 if (!MO.isReg() || !MO.isDef())
Evan Cheng6c177732008-04-16 09:41:59 +00001887 continue;
Matthias Braun1965bfa2013-10-10 21:28:38 +00001888 unsigned MOReg = MO.getReg();
1889 if (!MOReg)
Dan Gohmanc7367b42008-09-03 15:56:16 +00001890 continue;
1891
Matthias Braun1965bfa2013-10-10 21:28:38 +00001892 if (MOReg == Reg) {
Jakob Stoklund Olesen76ad3de2011-04-05 16:53:50 +00001893 MO.setIsDead();
1894 Found = true;
Dan Gohmanc7367b42008-09-03 15:56:16 +00001895 } else if (hasAliases && MO.isDead() &&
Matthias Braun1965bfa2013-10-10 21:28:38 +00001896 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
Evan Cheng6c177732008-04-16 09:41:59 +00001897 // There exists a super-register that's marked dead.
Matthias Braun1965bfa2013-10-10 21:28:38 +00001898 if (RegInfo->isSuperRegister(Reg, MOReg))
Dan Gohmanb2612922008-07-03 01:18:51 +00001899 return true;
Matthias Braun1965bfa2013-10-10 21:28:38 +00001900 if (RegInfo->isSubRegister(Reg, MOReg))
Evan Cheng6c177732008-04-16 09:41:59 +00001901 DeadOps.push_back(i);
Owen Anderson2a8a4852008-01-24 01:10:07 +00001902 }
1903 }
1904
Evan Cheng6c177732008-04-16 09:41:59 +00001905 // Trim unneeded dead operands.
1906 while (!DeadOps.empty()) {
1907 unsigned OpIdx = DeadOps.back();
1908 if (getOperand(OpIdx).isImplicit())
1909 RemoveOperand(OpIdx);
1910 else
1911 getOperand(OpIdx).setIsDead(false);
1912 DeadOps.pop_back();
1913 }
1914
Dan Gohmanc7367b42008-09-03 15:56:16 +00001915 // If not found, this means an alias of one of the operands is dead. Add a
1916 // new implicit operand if required.
Chris Lattnerfd682802009-06-24 17:54:48 +00001917 if (Found || !AddIfNotFound)
1918 return Found;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001919
Matthias Braun1965bfa2013-10-10 21:28:38 +00001920 addOperand(MachineOperand::CreateReg(Reg,
Chris Lattnerfd682802009-06-24 17:54:48 +00001921 true /*IsDef*/,
1922 true /*IsImp*/,
1923 false /*IsKill*/,
1924 true /*IsDead*/));
1925 return true;
Owen Anderson2a8a4852008-01-24 01:10:07 +00001926}
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00001927
Matthias Braun26e7ea62015-02-04 19:35:16 +00001928void MachineInstr::clearRegisterDeads(unsigned Reg) {
1929 for (MachineOperand &MO : operands()) {
1930 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg)
1931 continue;
1932 MO.setIsDead(false);
1933 }
1934}
1935
Matthias Braunc1988f32015-01-21 22:55:13 +00001936void MachineInstr::addRegisterDefReadUndef(unsigned Reg) {
1937 for (MachineOperand &MO : operands()) {
1938 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg || MO.getSubReg() == 0)
1939 continue;
1940 MO.setIsUndef();
1941 }
1942}
1943
Matthias Braun1965bfa2013-10-10 21:28:38 +00001944void MachineInstr::addRegisterDefined(unsigned Reg,
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00001945 const TargetRegisterInfo *RegInfo) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00001946 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1947 MachineOperand *MO = findRegisterDefOperand(Reg, false, RegInfo);
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00001948 if (MO)
1949 return;
1950 } else {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001951 for (const MachineOperand &MO : operands()) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00001952 if (MO.isReg() && MO.getReg() == Reg && MO.isDef() &&
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00001953 MO.getSubReg() == 0)
1954 return;
1955 }
1956 }
Matthias Braun1965bfa2013-10-10 21:28:38 +00001957 addOperand(MachineOperand::CreateReg(Reg,
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00001958 true /*IsDef*/,
1959 true /*IsImp*/));
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00001960}
Evan Cheng59d27fe2010-03-03 23:37:30 +00001961
Jakob Stoklund Olesen4290be42012-02-03 20:43:39 +00001962void MachineInstr::setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs,
Dan Gohman86936502010-06-18 23:28:01 +00001963 const TargetRegisterInfo &TRI) {
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00001964 bool HasRegMask = false;
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001965 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00001966 if (MO.isRegMask()) {
1967 HasRegMask = true;
1968 continue;
1969 }
Dan Gohman86936502010-06-18 23:28:01 +00001970 if (!MO.isReg() || !MO.isDef()) continue;
1971 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenf6507322012-02-03 20:43:35 +00001972 if (!TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Dan Gohman86936502010-06-18 23:28:01 +00001973 // If there are no uses, including partial uses, the def is dead.
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001974 if (std::none_of(UsedRegs.begin(), UsedRegs.end(),
1975 [&](unsigned Use) { return TRI.regsOverlap(Use, Reg); }))
1976 MO.setIsDead();
Dan Gohman86936502010-06-18 23:28:01 +00001977 }
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00001978
1979 // This is a call with a register mask operand.
1980 // Mask clobbers are always dead, so add defs for the non-dead defines.
1981 if (HasRegMask)
1982 for (ArrayRef<unsigned>::iterator I = UsedRegs.begin(), E = UsedRegs.end();
1983 I != E; ++I)
1984 addRegisterDefined(*I, &TRI);
Dan Gohman86936502010-06-18 23:28:01 +00001985}
1986
Evan Cheng59d27fe2010-03-03 23:37:30 +00001987unsigned
1988MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) {
Chandler Carruth962152c2012-03-07 09:39:46 +00001989 // Build up a buffer of hash code components.
Chandler Carruth962152c2012-03-07 09:39:46 +00001990 SmallVector<size_t, 8> HashComponents;
1991 HashComponents.reserve(MI->getNumOperands() + 1);
1992 HashComponents.push_back(MI->getOpcode());
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001993 for (const MachineOperand &MO : MI->operands()) {
Chandler Carruth264854f2012-07-05 11:06:22 +00001994 if (MO.isReg() && MO.isDef() &&
1995 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
1996 continue; // Skip virtual register defs.
1997
1998 HashComponents.push_back(hash_value(MO));
Evan Cheng59d27fe2010-03-03 23:37:30 +00001999 }
Chandler Carruth962152c2012-03-07 09:39:46 +00002000 return hash_combine_range(HashComponents.begin(), HashComponents.end());
Evan Cheng59d27fe2010-03-03 23:37:30 +00002001}
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00002002
2003void MachineInstr::emitError(StringRef Msg) const {
2004 // Find the source location cookie.
2005 unsigned LocCookie = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00002006 const MDNode *LocMD = nullptr;
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00002007 for (unsigned i = getNumOperands(); i != 0; --i) {
2008 if (getOperand(i-1).isMetadata() &&
2009 (LocMD = getOperand(i-1).getMetadata()) &&
2010 LocMD->getNumOperands() != 0) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002011 if (const ConstantInt *CI =
2012 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00002013 LocCookie = CI->getZExtValue();
2014 break;
2015 }
2016 }
2017 }
2018
2019 if (const MachineBasicBlock *MBB = getParent())
2020 if (const MachineFunction *MF = MBB->getParent())
2021 return MF->getMMI().getModule()->getContext().emitError(LocCookie, Msg);
2022 report_fatal_error(Msg);
2023}