blob: afea5575a3ae5392b1c001d6c0b603206810641c [file] [log] [blame]
Eugene Zelenko4e9736b2017-05-31 01:10:10 +00001//===- lib/CodeGen/MachineInstr.cpp ---------------------------------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Brian Gaekee8f7c2f2004-02-13 04:39:32 +00009//
10// Methods common to all machine instructions.
11//
Chris Lattner959a5fb2002-08-09 20:08:06 +000012//===----------------------------------------------------------------------===//
Vikram S. Adveab9e5572001-07-21 12:41:50 +000013
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "llvm/CodeGen/MachineInstr.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000015#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/ArrayRef.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/FoldingSet.h"
18#include "llvm/ADT/Hashing.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000019#include "llvm/ADT/None.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000020#include "llvm/ADT/STLExtras.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000021#include "llvm/ADT/SmallString.h"
22#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/Analysis/AliasAnalysis.h"
Hiroshi Inoue95f24dc2017-06-24 15:17:38 +000024#include "llvm/Analysis/Loads.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000025#include "llvm/Analysis/MemoryLocation.h"
26#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
27#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner63f41ab2004-02-19 16:17:08 +000028#include "llvm/CodeGen/MachineFunction.h"
Reid Kleckner28865802016-04-14 18:29:59 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000030#include "llvm/CodeGen/MachineInstrBundle.h"
Dan Gohman48b185d2009-09-25 20:36:54 +000031#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000033#include "llvm/CodeGen/MachineOperand.h"
Chris Lattner961e7422008-01-01 01:12:31 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman2d489b52008-02-06 22:27:42 +000035#include "llvm/CodeGen/PseudoSourceValue.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000036#include "llvm/IR/Constants.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000037#include "llvm/IR/DebugInfoMetadata.h"
38#include "llvm/IR/DebugLoc.h"
39#include "llvm/IR/DerivedTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000040#include "llvm/IR/Function.h"
41#include "llvm/IR/InlineAsm.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000042#include "llvm/IR/InstrTypes.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000043#include "llvm/IR/Intrinsics.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000044#include "llvm/IR/LLVMContext.h"
45#include "llvm/IR/Metadata.h"
46#include "llvm/IR/Module.h"
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +000047#include "llvm/IR/ModuleSlotTracker.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000048#include "llvm/IR/Type.h"
49#include "llvm/IR/Value.h"
Evan Cheng6cc775f2011-06-28 19:10:37 +000050#include "llvm/MC/MCInstrDesc.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000051#include "llvm/MC/MCRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000052#include "llvm/MC/MCSymbol.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000053#include "llvm/Support/Casting.h"
Daniel Sanders1e97a0b2015-08-19 12:03:04 +000054#include "llvm/Support/CommandLine.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000055#include "llvm/Support/Compiler.h"
David Greene29388d62010-01-04 23:48:20 +000056#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000057#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000058#include "llvm/Support/LowLevelTypeImpl.h"
Dan Gohmanaedb4a62008-07-07 20:32:02 +000059#include "llvm/Support/MathExtras.h"
Chris Lattnera078d832008-08-24 20:37:32 +000060#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000061#include "llvm/Target/TargetInstrInfo.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000062#include "llvm/Target/TargetIntrinsicInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000063#include "llvm/Target/TargetMachine.h"
64#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000065#include "llvm/Target/TargetSubtargetInfo.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000066#include <algorithm>
67#include <cassert>
68#include <cstddef>
69#include <cstdint>
70#include <cstring>
71#include <iterator>
72#include <utility>
73
Chris Lattner43df6c22004-02-23 18:38:20 +000074using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000075
Daniel Sanders1e97a0b2015-08-19 12:03:04 +000076static cl::opt<bool> PrintWholeRegMask(
77 "print-whole-regmask",
78 cl::desc("Print the full contents of regmask operands in IR dumps"),
79 cl::init(true), cl::Hidden);
80
Chris Lattner60055892007-12-30 21:56:09 +000081//===----------------------------------------------------------------------===//
82// MachineOperand Implementation
83//===----------------------------------------------------------------------===//
84
Chris Lattner961e7422008-01-01 01:12:31 +000085void MachineOperand::setReg(unsigned Reg) {
86 if (getReg() == Reg) return; // No change.
Jim Grosbachdee9e8a2011-08-24 16:44:17 +000087
Chris Lattner961e7422008-01-01 01:12:31 +000088 // Otherwise, we have to change the register. If this operand is embedded
89 // into a machine function, we need to update the old and new register's
90 // use/def lists.
91 if (MachineInstr *MI = getParent())
92 if (MachineBasicBlock *MBB = MI->getParent())
93 if (MachineFunction *MF = MBB->getParent()) {
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +000094 MachineRegisterInfo &MRI = MF->getRegInfo();
95 MRI.removeRegOperandFromUseList(this);
Jakob Stoklund Olesena4941692010-10-19 20:56:32 +000096 SmallContents.RegNo = Reg;
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +000097 MRI.addRegOperandToUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +000098 return;
99 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000100
Chris Lattner961e7422008-01-01 01:12:31 +0000101 // Otherwise, just change the register, no problem. :)
Jakob Stoklund Olesena4941692010-10-19 20:56:32 +0000102 SmallContents.RegNo = Reg;
Chris Lattner961e7422008-01-01 01:12:31 +0000103}
104
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +0000105void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx,
106 const TargetRegisterInfo &TRI) {
107 assert(TargetRegisterInfo::isVirtualRegister(Reg));
108 if (SubIdx && getSubReg())
109 SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg());
110 setReg(Reg);
Jakob Stoklund Olesen7b0ac862010-06-01 22:39:25 +0000111 if (SubIdx)
112 setSubReg(SubIdx);
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +0000113}
114
115void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) {
116 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
117 if (getSubReg()) {
118 Reg = TRI.getSubReg(Reg, getSubReg());
Jakob Stoklund Olesen89bd2ae2011-05-08 19:21:08 +0000119 // Note that getSubReg() may return 0 if the sub-register doesn't exist.
120 // That won't happen in legal code.
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +0000121 setSubReg(0);
Krzysztof Parzyszek673b3472016-08-22 14:50:12 +0000122 if (isDef())
123 setIsUndef(false);
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +0000124 }
125 setReg(Reg);
126}
127
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000128/// Change a def to a use, or a use to a def.
129void MachineOperand::setIsDef(bool Val) {
130 assert(isReg() && "Wrong MachineOperand accessor");
131 assert((!Val || !isDebug()) && "Marking a debug operation as def");
132 if (IsDef == Val)
133 return;
134 // MRI may keep uses and defs in different list positions.
135 if (MachineInstr *MI = getParent())
136 if (MachineBasicBlock *MBB = MI->getParent())
137 if (MachineFunction *MF = MBB->getParent()) {
138 MachineRegisterInfo &MRI = MF->getRegInfo();
139 MRI.removeRegOperandFromUseList(this);
140 IsDef = Val;
141 MRI.addRegOperandToUseList(this);
142 return;
143 }
144 IsDef = Val;
145}
146
Matt Arsenault93ffe582014-09-28 19:24:59 +0000147// If this operand is currently a register operand, and if this is in a
148// function, deregister the operand from the register's use/def list.
149void MachineOperand::removeRegFromUses() {
150 if (!isReg() || !isOnRegUseList())
151 return;
152
153 if (MachineInstr *MI = getParent()) {
154 if (MachineBasicBlock *MBB = MI->getParent()) {
155 if (MachineFunction *MF = MBB->getParent())
156 MF->getRegInfo().removeRegOperandFromUseList(this);
157 }
158 }
159}
160
Chris Lattner961e7422008-01-01 01:12:31 +0000161/// ChangeToImmediate - Replace this operand with a new immediate operand of
162/// the specified value. If an operand is known to be an immediate already,
163/// the setImm method should be used.
164void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000165 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
Matt Arsenault93ffe582014-09-28 19:24:59 +0000166
167 removeRegFromUses();
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000168
Chris Lattner961e7422008-01-01 01:12:31 +0000169 OpKind = MO_Immediate;
170 Contents.ImmVal = ImmVal;
171}
172
Matt Arsenault93ffe582014-09-28 19:24:59 +0000173void MachineOperand::ChangeToFPImmediate(const ConstantFP *FPImm) {
174 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
175
176 removeRegFromUses();
177
178 OpKind = MO_FPImmediate;
179 Contents.CFP = FPImm;
180}
181
Matt Arsenault633dba42015-05-06 17:05:54 +0000182void MachineOperand::ChangeToES(const char *SymName, unsigned char TargetFlags) {
183 assert((!isReg() || !isTied()) &&
184 "Cannot change a tied operand into an external symbol");
185
186 removeRegFromUses();
187
188 OpKind = MO_ExternalSymbol;
189 Contents.OffsetedInfo.Val.SymbolName = SymName;
190 setOffset(0); // Offset is always 0.
191 setTargetFlags(TargetFlags);
192}
193
194void MachineOperand::ChangeToMCSymbol(MCSymbol *Sym) {
195 assert((!isReg() || !isTied()) &&
196 "Cannot change a tied operand into an MCSymbol");
197
198 removeRegFromUses();
199
200 OpKind = MO_MCSymbol;
201 Contents.Sym = Sym;
202}
203
Matt Arsenault25dba302016-09-13 19:03:12 +0000204void MachineOperand::ChangeToFrameIndex(int Idx) {
205 assert((!isReg() || !isTied()) &&
206 "Cannot change a tied operand into a FrameIndex");
207
208 removeRegFromUses();
209
210 OpKind = MO_FrameIndex;
211 setIndex(Idx);
212}
213
Chris Lattner961e7422008-01-01 01:12:31 +0000214/// ChangeToRegister - Replace this operand with a new register operand of
215/// the specified value. If an operand is known to be an register already,
216/// the setReg method should be used.
217void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
Dale Johannesend40d42c2010-02-10 00:41:49 +0000218 bool isKill, bool isDead, bool isUndef,
219 bool isDebug) {
Craig Topperc0196b12014-04-14 00:51:57 +0000220 MachineRegisterInfo *RegInfo = nullptr;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000221 if (MachineInstr *MI = getParent())
222 if (MachineBasicBlock *MBB = MI->getParent())
223 if (MachineFunction *MF = MBB->getParent())
224 RegInfo = &MF->getRegInfo();
225 // If this operand is already a register operand, remove it from the
Chris Lattner961e7422008-01-01 01:12:31 +0000226 // register's use/def lists.
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000227 bool WasReg = isReg();
228 if (RegInfo && WasReg)
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000229 RegInfo->removeRegOperandFromUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +0000230
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000231 // Change this to a register and set the reg#.
232 OpKind = MO_Register;
233 SmallContents.RegNo = Reg;
Jakob Stoklund Olesena1b246d2013-01-07 23:21:44 +0000234 SubReg_TargetFlags = 0;
Chris Lattner961e7422008-01-01 01:12:31 +0000235 IsDef = isDef;
236 IsImp = isImp;
237 IsKill = isKill;
238 IsDead = isDead;
Evan Cheng0dc101b2009-06-30 08:49:04 +0000239 IsUndef = isUndef;
Jakob Stoklund Olesenb0d91ab2011-12-07 00:22:07 +0000240 IsInternalRead = false;
Dale Johannesenc0d712d2008-09-14 01:44:36 +0000241 IsEarlyClobber = false;
Dale Johannesend40d42c2010-02-10 00:41:49 +0000242 IsDebug = isDebug;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000243 // Ensure isOnRegUseList() returns false.
Craig Topperc0196b12014-04-14 00:51:57 +0000244 Contents.Reg.Prev = nullptr;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000245 // Preserve the tie when the operand was already a register.
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000246 if (!WasReg)
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000247 TiedTo = 0;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000248
249 // If this operand is embedded in a function, add the operand to the
250 // register's use/def list.
251 if (RegInfo)
252 RegInfo->addRegOperandToUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +0000253}
254
Chris Lattner60055892007-12-30 21:56:09 +0000255/// isIdenticalTo - Return true if this operand is identical to the specified
Chandler Carruth264854f2012-07-05 11:06:22 +0000256/// operand. Note that this should stay in sync with the hash_value overload
257/// below.
Chris Lattner60055892007-12-30 21:56:09 +0000258bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
Chris Lattnerfd682802009-06-24 17:54:48 +0000259 if (getType() != Other.getType() ||
260 getTargetFlags() != Other.getTargetFlags())
261 return false;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000262
Chris Lattner60055892007-12-30 21:56:09 +0000263 switch (getType()) {
Chris Lattner60055892007-12-30 21:56:09 +0000264 case MachineOperand::MO_Register:
265 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
266 getSubReg() == Other.getSubReg();
267 case MachineOperand::MO_Immediate:
268 return getImm() == Other.getImm();
Cameron Zwarich7da0f9a2011-07-01 23:45:21 +0000269 case MachineOperand::MO_CImmediate:
270 return getCImm() == Other.getCImm();
Nate Begeman26b76b62008-02-14 07:39:30 +0000271 case MachineOperand::MO_FPImmediate:
272 return getFPImm() == Other.getFPImm();
Chris Lattner60055892007-12-30 21:56:09 +0000273 case MachineOperand::MO_MachineBasicBlock:
274 return getMBB() == Other.getMBB();
275 case MachineOperand::MO_FrameIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000276 return getIndex() == Other.getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000277 case MachineOperand::MO_ConstantPoolIndex:
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000278 case MachineOperand::MO_TargetIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000279 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattner60055892007-12-30 21:56:09 +0000280 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000281 return getIndex() == Other.getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000282 case MachineOperand::MO_GlobalAddress:
283 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
284 case MachineOperand::MO_ExternalSymbol:
Eugene Zelenko4e9736b2017-05-31 01:10:10 +0000285 return strcmp(getSymbolName(), Other.getSymbolName()) == 0 &&
Chris Lattner60055892007-12-30 21:56:09 +0000286 getOffset() == Other.getOffset();
Dan Gohman6c938802009-10-30 01:27:03 +0000287 case MachineOperand::MO_BlockAddress:
Michael Liaoabb87d42012-09-12 21:43:09 +0000288 return getBlockAddress() == Other.getBlockAddress() &&
289 getOffset() == Other.getOffset();
Juergen Ributzkae8294752013-12-14 06:53:06 +0000290 case MachineOperand::MO_RegisterMask:
Oren Ben Simhonfe34c5e2017-03-14 09:09:26 +0000291 case MachineOperand::MO_RegisterLiveOut: {
292 // Shallow compare of the two RegMasks
293 const uint32_t *RegMask = getRegMask();
294 const uint32_t *OtherRegMask = Other.getRegMask();
295 if (RegMask == OtherRegMask)
296 return true;
297
298 // Calculate the size of the RegMask
299 const MachineFunction *MF = getParent()->getParent()->getParent();
300 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
301 unsigned RegMaskSize = (TRI->getNumRegs() + 31) / 32;
302
303 // Deep compare of the two RegMasks
304 return std::equal(RegMask, RegMask + RegMaskSize, OtherRegMask);
305 }
Chris Lattner6c604e32010-03-13 08:14:18 +0000306 case MachineOperand::MO_MCSymbol:
307 return getMCSymbol() == Other.getMCSymbol();
Daniel Jasper559aa752017-06-29 13:58:24 +0000308 case MachineOperand::MO_CFIIndex:
309 return getCFIIndex() == Other.getCFIIndex();
Chris Lattnerf839ee02010-04-07 18:03:19 +0000310 case MachineOperand::MO_Metadata:
311 return getMetadata() == Other.getMetadata();
Tim Northover6b3bd612016-07-29 20:32:59 +0000312 case MachineOperand::MO_IntrinsicID:
313 return getIntrinsicID() == Other.getIntrinsicID();
Tim Northoverde3aea0412016-08-17 20:25:25 +0000314 case MachineOperand::MO_Predicate:
315 return getPredicate() == Other.getPredicate();
Chris Lattner60055892007-12-30 21:56:09 +0000316 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000317 llvm_unreachable("Invalid machine operand type");
Chris Lattner60055892007-12-30 21:56:09 +0000318}
319
Chandler Carruth264854f2012-07-05 11:06:22 +0000320// Note: this must stay exactly in sync with isIdenticalTo above.
321hash_code llvm::hash_value(const MachineOperand &MO) {
322 switch (MO.getType()) {
323 case MachineOperand::MO_Register:
Jakob Stoklund Olesendba99d02012-08-28 18:05:48 +0000324 // Register operands don't have target flags.
325 return hash_combine(MO.getType(), MO.getReg(), MO.getSubReg(), MO.isDef());
Chandler Carruth264854f2012-07-05 11:06:22 +0000326 case MachineOperand::MO_Immediate:
327 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm());
328 case MachineOperand::MO_CImmediate:
329 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm());
330 case MachineOperand::MO_FPImmediate:
331 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm());
332 case MachineOperand::MO_MachineBasicBlock:
333 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB());
334 case MachineOperand::MO_FrameIndex:
335 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
336 case MachineOperand::MO_ConstantPoolIndex:
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000337 case MachineOperand::MO_TargetIndex:
Chandler Carruth264854f2012-07-05 11:06:22 +0000338 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(),
339 MO.getOffset());
340 case MachineOperand::MO_JumpTableIndex:
341 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
342 case MachineOperand::MO_ExternalSymbol:
343 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(),
344 MO.getSymbolName());
345 case MachineOperand::MO_GlobalAddress:
346 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(),
347 MO.getOffset());
348 case MachineOperand::MO_BlockAddress:
349 return hash_combine(MO.getType(), MO.getTargetFlags(),
Michael Liaoabb87d42012-09-12 21:43:09 +0000350 MO.getBlockAddress(), MO.getOffset());
Chandler Carruth264854f2012-07-05 11:06:22 +0000351 case MachineOperand::MO_RegisterMask:
Juergen Ributzkae8294752013-12-14 06:53:06 +0000352 case MachineOperand::MO_RegisterLiveOut:
Chandler Carruth264854f2012-07-05 11:06:22 +0000353 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask());
354 case MachineOperand::MO_Metadata:
355 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata());
356 case MachineOperand::MO_MCSymbol:
357 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol());
Daniel Jasper559aa752017-06-29 13:58:24 +0000358 case MachineOperand::MO_CFIIndex:
359 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex());
Tim Northover6b3bd612016-07-29 20:32:59 +0000360 case MachineOperand::MO_IntrinsicID:
361 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIntrinsicID());
Tim Northoverde3aea0412016-08-17 20:25:25 +0000362 case MachineOperand::MO_Predicate:
363 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getPredicate());
Chandler Carruth264854f2012-07-05 11:06:22 +0000364 }
365 llvm_unreachable("Invalid machine operand type");
366}
367
Tim Northover6b3bd612016-07-29 20:32:59 +0000368void MachineOperand::print(raw_ostream &OS, const TargetRegisterInfo *TRI,
369 const TargetIntrinsicInfo *IntrinsicInfo) const {
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000370 ModuleSlotTracker DummyMST(nullptr);
Tim Northover6b3bd612016-07-29 20:32:59 +0000371 print(OS, DummyMST, TRI, IntrinsicInfo);
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000372}
373
374void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
Tim Northover6b3bd612016-07-29 20:32:59 +0000375 const TargetRegisterInfo *TRI,
376 const TargetIntrinsicInfo *IntrinsicInfo) const {
Chris Lattner60055892007-12-30 21:56:09 +0000377 switch (getType()) {
378 case MachineOperand::MO_Register:
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000379 OS << PrintReg(getReg(), TRI, getSubReg());
Dan Gohman0ab11442008-12-18 21:51:27 +0000380
Evan Cheng0dc101b2009-06-30 08:49:04 +0000381 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000382 isInternalRead() || isEarlyClobber() || isTied()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000383 OS << '<';
Chris Lattner60055892007-12-30 21:56:09 +0000384 bool NeedComma = false;
Evan Cheng70b1fa52009-10-14 23:37:31 +0000385 if (isDef()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000386 if (NeedComma) OS << ',';
Dale Johannesen1f3ab862008-09-12 17:49:03 +0000387 if (isEarlyClobber())
388 OS << "earlyclobber,";
Evan Cheng70b1fa52009-10-14 23:37:31 +0000389 if (isImplicit())
390 OS << "imp-";
Chris Lattner60055892007-12-30 21:56:09 +0000391 OS << "def";
392 NeedComma = true;
Jakob Stoklund Olesen7111a632012-04-20 21:45:33 +0000393 // <def,read-undef> only makes sense when getSubReg() is set.
394 // Don't clutter the output otherwise.
395 if (isUndef() && getSubReg())
396 OS << ",read-undef";
Evan Chengf781bd82009-10-21 07:56:02 +0000397 } else if (isImplicit()) {
Craig Topper9a9d58a2015-05-16 05:42:08 +0000398 OS << "imp-use";
399 NeedComma = true;
Evan Chengf781bd82009-10-21 07:56:02 +0000400 }
Evan Cheng70b1fa52009-10-14 23:37:31 +0000401
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000402 if (isKill()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000403 if (NeedComma) OS << ',';
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000404 OS << "kill";
405 NeedComma = true;
406 }
407 if (isDead()) {
408 if (NeedComma) OS << ',';
409 OS << "dead";
410 NeedComma = true;
411 }
412 if (isUndef() && isUse()) {
413 if (NeedComma) OS << ',';
414 OS << "undef";
415 NeedComma = true;
416 }
417 if (isInternalRead()) {
418 if (NeedComma) OS << ',';
419 OS << "internal";
420 NeedComma = true;
421 }
422 if (isTied()) {
423 if (NeedComma) OS << ',';
424 OS << "tied";
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000425 if (TiedTo != 15)
426 OS << unsigned(TiedTo - 1);
Chris Lattner60055892007-12-30 21:56:09 +0000427 }
Chris Lattnerfd682802009-06-24 17:54:48 +0000428 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000429 }
430 break;
431 case MachineOperand::MO_Immediate:
432 OS << getImm();
433 break;
Devang Patelf071d722011-06-24 20:46:11 +0000434 case MachineOperand::MO_CImmediate:
435 getCImm()->getValue().print(OS, false);
436 break;
Nate Begeman26b76b62008-02-14 07:39:30 +0000437 case MachineOperand::MO_FPImmediate:
Matt Arsenault59239732016-02-05 00:50:18 +0000438 if (getFPImm()->getType()->isFloatTy()) {
Nate Begeman26b76b62008-02-14 07:39:30 +0000439 OS << getFPImm()->getValueAPF().convertToFloat();
Matt Arsenault59239732016-02-05 00:50:18 +0000440 } else if (getFPImm()->getType()->isHalfTy()) {
441 APFloat APF = getFPImm()->getValueAPF();
442 bool Unused;
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000443 APF.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &Unused);
Matt Arsenault59239732016-02-05 00:50:18 +0000444 OS << "half " << APF.convertToFloat();
Tim Northover89268b12017-03-20 16:52:08 +0000445 } else if (getFPImm()->getType()->isFP128Ty()) {
446 APFloat APF = getFPImm()->getValueAPF();
447 SmallString<16> Str;
448 getFPImm()->getValueAPF().toString(Str);
449 OS << "quad " << Str;
Igor Breger55e2f592017-07-05 11:11:10 +0000450 } else if (getFPImm()->getType()->isX86_FP80Ty()) {
451 APFloat APF = getFPImm()->getValueAPF();
452 OS << "x86_fp80 0xK";
453 APInt API = APF.bitcastToAPInt();
454 OS << format_hex_no_prefix(API.getHiBits(16).getZExtValue(), 4,
455 /*Upper=*/true);
456 OS << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
457 /*Upper=*/true);
Matt Arsenault59239732016-02-05 00:50:18 +0000458 } else {
Nate Begeman26b76b62008-02-14 07:39:30 +0000459 OS << getFPImm()->getValueAPF().convertToDouble();
Matt Arsenault59239732016-02-05 00:50:18 +0000460 }
Nate Begeman26b76b62008-02-14 07:39:30 +0000461 break;
Chris Lattner60055892007-12-30 21:56:09 +0000462 case MachineOperand::MO_MachineBasicBlock:
Dan Gohman34341e62009-10-31 20:19:03 +0000463 OS << "<BB#" << getMBB()->getNumber() << ">";
Chris Lattner60055892007-12-30 21:56:09 +0000464 break;
465 case MachineOperand::MO_FrameIndex:
Chris Lattnerfd682802009-06-24 17:54:48 +0000466 OS << "<fi#" << getIndex() << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000467 break;
468 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000469 OS << "<cp#" << getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000470 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000471 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000472 break;
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000473 case MachineOperand::MO_TargetIndex:
474 OS << "<ti#" << getIndex();
475 if (getOffset()) OS << "+" << getOffset();
476 OS << '>';
477 break;
Chris Lattner60055892007-12-30 21:56:09 +0000478 case MachineOperand::MO_JumpTableIndex:
Chris Lattnerfd682802009-06-24 17:54:48 +0000479 OS << "<jt#" << getIndex() << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000480 break;
481 case MachineOperand::MO_GlobalAddress:
Dan Gohman0080ee22009-11-06 18:03:10 +0000482 OS << "<ga:";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000483 getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Chris Lattner60055892007-12-30 21:56:09 +0000484 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000485 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000486 break;
487 case MachineOperand::MO_ExternalSymbol:
488 OS << "<es:" << getSymbolName();
489 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000490 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000491 break;
Dan Gohman6c938802009-10-30 01:27:03 +0000492 case MachineOperand::MO_BlockAddress:
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000493 OS << '<';
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000494 getBlockAddress()->printAsOperand(OS, /*PrintType=*/false, MST);
Michael Liaoabb87d42012-09-12 21:43:09 +0000495 if (getOffset()) OS << "+" << getOffset();
Dan Gohman6c938802009-10-30 01:27:03 +0000496 OS << '>';
497 break;
Daniel Sanders1e97a0b2015-08-19 12:03:04 +0000498 case MachineOperand::MO_RegisterMask: {
499 unsigned NumRegsInMask = 0;
500 unsigned NumRegsEmitted = 0;
501 OS << "<regmask";
502 for (unsigned i = 0; i < TRI->getNumRegs(); ++i) {
503 unsigned MaskWord = i / 32;
504 unsigned MaskBit = i % 32;
505 if (getRegMask()[MaskWord] & (1 << MaskBit)) {
506 if (PrintWholeRegMask || NumRegsEmitted <= 10) {
507 OS << " " << PrintReg(i, TRI);
508 NumRegsEmitted++;
509 }
510 NumRegsInMask++;
511 }
512 }
513 if (NumRegsEmitted != NumRegsInMask)
514 OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more...";
515 OS << ">";
Jakob Stoklund Olesen374ed322012-01-16 19:22:00 +0000516 break;
Daniel Sanders1e97a0b2015-08-19 12:03:04 +0000517 }
Juergen Ributzkae8294752013-12-14 06:53:06 +0000518 case MachineOperand::MO_RegisterLiveOut:
519 OS << "<regliveout>";
520 break;
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000521 case MachineOperand::MO_Metadata:
522 OS << '<';
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000523 getMetadata()->printAsOperand(OS, MST);
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000524 OS << '>';
525 break;
Chris Lattner6c604e32010-03-13 08:14:18 +0000526 case MachineOperand::MO_MCSymbol:
527 OS << "<MCSym=" << *getMCSymbol() << '>';
528 break;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000529 case MachineOperand::MO_CFIIndex:
530 OS << "<call frame instruction>";
531 break;
Tim Northover6b3bd612016-07-29 20:32:59 +0000532 case MachineOperand::MO_IntrinsicID: {
533 Intrinsic::ID ID = getIntrinsicID();
534 if (ID < Intrinsic::num_intrinsics)
Ahmed Bougacha925961b2016-09-12 16:21:49 +0000535 OS << "<intrinsic:@" << Intrinsic::getName(ID, None) << '>';
Tim Northover6b3bd612016-07-29 20:32:59 +0000536 else if (IntrinsicInfo)
Ahmed Bougacha925961b2016-09-12 16:21:49 +0000537 OS << "<intrinsic:@" << IntrinsicInfo->getName(ID) << '>';
Tim Northover6b3bd612016-07-29 20:32:59 +0000538 else
539 OS << "<intrinsic:" << ID << '>';
540 break;
541 }
Tim Northoverde3aea0412016-08-17 20:25:25 +0000542 case MachineOperand::MO_Predicate: {
543 auto Pred = static_cast<CmpInst::Predicate>(getPredicate());
544 OS << '<' << (CmpInst::isIntPredicate(Pred) ? "intpred" : "floatpred")
545 << CmpInst::getPredicateName(Pred) << '>';
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000546 break;
Chris Lattner60055892007-12-30 21:56:09 +0000547 }
Tim Northoverde3aea0412016-08-17 20:25:25 +0000548 }
Chris Lattnerfd682802009-06-24 17:54:48 +0000549 if (unsigned TF = getTargetFlags())
550 OS << "[TF=" << TF << ']';
Chris Lattner60055892007-12-30 21:56:09 +0000551}
552
Matthias Braun637488d2016-11-18 02:40:40 +0000553#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
554LLVM_DUMP_METHOD void MachineOperand::dump() const {
555 dbgs() << *this << '\n';
556}
557#endif
558
Chris Lattner60055892007-12-30 21:56:09 +0000559//===----------------------------------------------------------------------===//
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000560// MachineMemOperand Implementation
561//===----------------------------------------------------------------------===//
562
Chris Lattnerde93bb02010-09-21 05:39:30 +0000563/// getAddrSpace - Return the LLVM IR address space number that this pointer
564/// points into.
565unsigned MachinePointerInfo::getAddrSpace() const {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000566 if (V.isNull() || V.is<const PseudoSourceValue*>()) return 0;
567 return cast<PointerType>(V.get<const Value*>()->getType())->getAddressSpace();
Chris Lattnerde93bb02010-09-21 05:39:30 +0000568}
569
Hiroshi Inoue95f24dc2017-06-24 15:17:38 +0000570/// isDereferenceable - Return true if V is always dereferenceable for
571/// Offset + Size byte.
572bool MachinePointerInfo::isDereferenceable(unsigned Size, LLVMContext &C,
573 const DataLayout &DL) const {
574 if (!V.is<const Value*>())
575 return false;
576
577 const Value *BasePtr = V.get<const Value*>();
578 if (BasePtr == nullptr)
579 return false;
580
581 return isDereferenceableAndAlignedPointer(BasePtr, 1,
582 APInt(DL.getPointerSize(),
583 Offset + Size),
584 DL);
585}
586
Chris Lattner82fd06d2010-09-21 06:22:23 +0000587/// getConstantPool - Return a MachinePointerInfo record that refers to the
588/// constant pool.
Alex Lorenze40c8a22015-08-11 23:09:45 +0000589MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) {
590 return MachinePointerInfo(MF.getPSVManager().getConstantPool());
Chris Lattner82fd06d2010-09-21 06:22:23 +0000591}
592
593/// getFixedStack - Return a MachinePointerInfo record that refers to the
594/// the specified FrameIndex.
Alex Lorenze40c8a22015-08-11 23:09:45 +0000595MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF,
596 int FI, int64_t Offset) {
597 return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset);
Chris Lattner82fd06d2010-09-21 06:22:23 +0000598}
599
Alex Lorenze40c8a22015-08-11 23:09:45 +0000600MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) {
601 return MachinePointerInfo(MF.getPSVManager().getJumpTable());
Chris Lattner50287ea2010-09-21 06:43:24 +0000602}
603
Alex Lorenze40c8a22015-08-11 23:09:45 +0000604MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) {
605 return MachinePointerInfo(MF.getPSVManager().getGOT());
Chris Lattner50287ea2010-09-21 06:43:24 +0000606}
Chris Lattnerde93bb02010-09-21 05:39:30 +0000607
Alex Lorenze40c8a22015-08-11 23:09:45 +0000608MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF,
609 int64_t Offset) {
610 return MachinePointerInfo(MF.getPSVManager().getStack(), Offset);
Chris Lattner886250c2010-09-21 18:51:21 +0000611}
612
Justin Lebara3b786a2016-07-14 17:07:44 +0000613MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags f,
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000614 uint64_t s, unsigned int a,
Hal Finkelcc39b672014-07-24 12:16:19 +0000615 const AAMDNodes &AAInfo,
Konstantin Zhuravlyov8ea02462016-10-15 22:01:18 +0000616 const MDNode *Ranges,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000617 SyncScope::ID SSID,
Konstantin Zhuravlyov8ea02462016-10-15 22:01:18 +0000618 AtomicOrdering Ordering,
619 AtomicOrdering FailureOrdering)
Justin Lebara3b786a2016-07-14 17:07:44 +0000620 : PtrInfo(ptrinfo), Size(s), FlagVals(f), BaseAlignLog2(Log2_32(a) + 1),
621 AAInfo(AAInfo), Ranges(Ranges) {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000622 assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue*>() ||
623 isa<PointerType>(PtrInfo.V.get<const Value*>()->getType())) &&
Chris Lattner00ca0b82010-09-21 04:32:08 +0000624 "invalid pointer value");
Dan Gohmane7c82422009-09-21 19:47:04 +0000625 assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
Dan Gohmanbf98f682008-07-16 15:56:42 +0000626 assert((isLoad() || isStore()) && "Not a load/store!");
Konstantin Zhuravlyov8ea02462016-10-15 22:01:18 +0000627
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000628 AtomicInfo.SSID = static_cast<unsigned>(SSID);
629 assert(getSyncScopeID() == SSID && "Value truncated");
Konstantin Zhuravlyov8ea02462016-10-15 22:01:18 +0000630 AtomicInfo.Ordering = static_cast<unsigned>(Ordering);
631 assert(getOrdering() == Ordering && "Value truncated");
632 AtomicInfo.FailureOrdering = static_cast<unsigned>(FailureOrdering);
633 assert(getFailureOrdering() == FailureOrdering && "Value truncated");
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000634}
635
Dan Gohman2da2bed2008-08-20 15:58:01 +0000636/// Profile - Gather unique data for the object.
637///
638void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
Chris Lattner187f6532010-09-21 04:23:39 +0000639 ID.AddInteger(getOffset());
Dan Gohman2da2bed2008-08-20 15:58:01 +0000640 ID.AddInteger(Size);
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000641 ID.AddPointer(getOpaqueValue());
Justin Lebara3b786a2016-07-14 17:07:44 +0000642 ID.AddInteger(getFlags());
643 ID.AddInteger(getBaseAlignment());
Dan Gohman2da2bed2008-08-20 15:58:01 +0000644}
645
Dan Gohman48b185d2009-09-25 20:36:54 +0000646void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
647 // The Value and Offset may differ due to CSE. But the flags and size
648 // should be the same.
649 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
650 assert(MMO->getSize() == getSize() && "Size mismatch!");
651
652 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
653 // Update the alignment value.
Justin Lebara3b786a2016-07-14 17:07:44 +0000654 BaseAlignLog2 = Log2_32(MMO->getBaseAlignment()) + 1;
Dan Gohman48b185d2009-09-25 20:36:54 +0000655 // Also update the base and offset, because the new alignment may
656 // not be applicable with the old ones.
Chris Lattner187f6532010-09-21 04:23:39 +0000657 PtrInfo = MMO->PtrInfo;
Dan Gohman48b185d2009-09-25 20:36:54 +0000658 }
659}
660
Dan Gohman5a6b11c2009-09-25 23:33:20 +0000661/// getAlignment - Return the minimum known alignment in bytes of the
662/// actual memory reference.
663uint64_t MachineMemOperand::getAlignment() const {
664 return MinAlign(getBaseAlignment(), getOffset());
665}
666
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000667void MachineMemOperand::print(raw_ostream &OS) const {
668 ModuleSlotTracker DummyMST(nullptr);
669 print(OS, DummyMST);
670}
671void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST) const {
672 assert((isLoad() || isStore()) &&
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000673 "SV has to be a load, store or both.");
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000674
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000675 if (isVolatile())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000676 OS << "Volatile ";
677
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000678 if (isLoad())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000679 OS << "LD";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000680 if (isStore())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000681 OS << "ST";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000682 OS << getSize();
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000683
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000684 // Print the address information.
685 OS << "[";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000686 if (const Value *V = getValue())
687 V->printAsOperand(OS, /*PrintType=*/false, MST);
688 else if (const PseudoSourceValue *PSV = getPseudoValue())
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000689 PSV->printCustom(OS);
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000690 else
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000691 OS << "<unknown>";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000692
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000693 unsigned AS = getAddrSpace();
Matt Arsenault68c38fd2013-12-14 00:24:02 +0000694 if (AS != 0)
695 OS << "(addrspace=" << AS << ')';
696
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000697 // If the alignment of the memory reference itself differs from the alignment
698 // of the base pointer, print the base alignment explicitly, next to the base
699 // pointer.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000700 if (getBaseAlignment() != getAlignment())
701 OS << "(align=" << getBaseAlignment() << ")";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000702
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000703 if (getOffset() != 0)
704 OS << "+" << getOffset();
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000705 OS << "]";
706
707 // Print the alignment of the reference.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000708 if (getBaseAlignment() != getAlignment() || getBaseAlignment() != getSize())
709 OS << "(align=" << getAlignment() << ")";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000710
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000711 // Print TBAA info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000712 if (const MDNode *TBAAInfo = getAAInfo().TBAA) {
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000713 OS << "(tbaa=";
714 if (TBAAInfo->getNumOperands() > 0)
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000715 TBAAInfo->getOperand(0)->printAsOperand(OS, MST);
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000716 else
717 OS << "<unknown>";
718 OS << ")";
719 }
720
Hal Finkel94146652014-07-24 14:25:39 +0000721 // Print AA scope info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000722 if (const MDNode *ScopeInfo = getAAInfo().Scope) {
Hal Finkel94146652014-07-24 14:25:39 +0000723 OS << "(alias.scope=";
724 if (ScopeInfo->getNumOperands() > 0)
725 for (unsigned i = 0, ie = ScopeInfo->getNumOperands(); i != ie; ++i) {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000726 ScopeInfo->getOperand(i)->printAsOperand(OS, MST);
Hal Finkel94146652014-07-24 14:25:39 +0000727 if (i != ie-1)
728 OS << ",";
729 }
730 else
731 OS << "<unknown>";
732 OS << ")";
733 }
734
735 // Print AA noalias scope info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000736 if (const MDNode *NoAliasInfo = getAAInfo().NoAlias) {
Hal Finkel94146652014-07-24 14:25:39 +0000737 OS << "(noalias=";
738 if (NoAliasInfo->getNumOperands() > 0)
739 for (unsigned i = 0, ie = NoAliasInfo->getNumOperands(); i != ie; ++i) {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000740 NoAliasInfo->getOperand(i)->printAsOperand(OS, MST);
Hal Finkel94146652014-07-24 14:25:39 +0000741 if (i != ie-1)
742 OS << ",";
743 }
744 else
745 OS << "<unknown>";
746 OS << ")";
747 }
748
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000749 if (isNonTemporal())
Bill Wendling9f638ab2011-04-29 23:45:22 +0000750 OS << "(nontemporal)";
Justin Lebaradbf09e2016-09-11 01:38:58 +0000751 if (isDereferenceable())
752 OS << "(dereferenceable)";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000753 if (isInvariant())
Matt Arsenault572c29a2015-06-26 19:00:11 +0000754 OS << "(invariant)";
Geoff Berry6748abe2017-07-13 02:28:54 +0000755 if (getFlags() & MOTargetFlag1)
756 OS << "(flag1)";
757 if (getFlags() & MOTargetFlag2)
758 OS << "(flag2)";
759 if (getFlags() & MOTargetFlag3)
760 OS << "(flag3)";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000761}
762
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000763//===----------------------------------------------------------------------===//
Chris Lattner60055892007-12-30 21:56:09 +0000764// MachineInstr Implementation
765//===----------------------------------------------------------------------===//
766
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000767void MachineInstr::addImplicitDefUseOperands(MachineFunction &MF) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000768 if (MCID->ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000769 for (const MCPhysReg *ImpDefs = MCID->getImplicitDefs(); *ImpDefs;
770 ++ImpDefs)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000771 addOperand(MF, MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng6cc775f2011-06-28 19:10:37 +0000772 if (MCID->ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000773 for (const MCPhysReg *ImpUses = MCID->getImplicitUses(); *ImpUses;
774 ++ImpUses)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000775 addOperand(MF, MachineOperand::CreateReg(*ImpUses, false, true));
Evan Cheng77af6ac2006-11-13 23:34:06 +0000776}
777
Bob Wilson406f2702010-04-09 04:34:03 +0000778/// MachineInstr ctor - This constructor creates a MachineInstr and adds the
779/// implicit operands. It reserves space for the number of operands specified by
Evan Cheng6cc775f2011-06-28 19:10:37 +0000780/// the MCInstrDesc.
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000781MachineInstr::MachineInstr(MachineFunction &MF, const MCInstrDesc &tid,
Benjamin Kramera9591b52015-02-07 12:28:15 +0000782 DebugLoc dl, bool NoImp)
Eugene Zelenko4e9736b2017-05-31 01:10:10 +0000783 : MCID(&tid), debugLoc(std::move(dl)) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000784 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
785
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000786 // Reserve space for the expected number of operands.
787 if (unsigned NumOps = MCID->getNumOperands() +
788 MCID->getNumImplicitDefs() + MCID->getNumImplicitUses()) {
789 CapOperands = OperandCapacity::get(NumOps);
790 Operands = MF.allocateOperandArray(CapOperands);
791 }
792
Dale Johannesen4e04ef32009-01-27 23:20:29 +0000793 if (!NoImp)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000794 addImplicitDefUseOperands(MF);
Dale Johannesen4e04ef32009-01-27 23:20:29 +0000795}
796
Misha Brukmanb47ab7a2004-07-09 14:45:17 +0000797/// MachineInstr ctor - Copies MachineInstr arg exactly
798///
Evan Chenga7a20c42008-07-19 00:37:25 +0000799MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Eugene Zelenko4e9736b2017-05-31 01:10:10 +0000800 : MCID(&MI.getDesc()), NumMemRefs(MI.NumMemRefs), MemRefs(MI.MemRefs),
801 debugLoc(MI.getDebugLoc()) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000802 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
803
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000804 CapOperands = OperandCapacity::get(MI.getNumOperands());
805 Operands = MF.allocateOperandArray(CapOperands);
Tanya Lattner9953d862004-05-23 20:58:02 +0000806
Jakob Stoklund Olesendc5285f2013-01-05 05:05:51 +0000807 // Copy operands.
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000808 for (const MachineOperand &MO : MI.operands())
809 addOperand(MF, MO);
Tanya Lattnerbcee21b2004-05-24 03:14:18 +0000810
Jakob Stoklund Olesena33f5042012-12-18 21:36:05 +0000811 // Copy all the sensible flags.
812 setFlags(MI.Flags);
Alkis Evlogimenos14f3fe82004-02-16 07:17:43 +0000813}
814
Chris Lattner961e7422008-01-01 01:12:31 +0000815/// getRegInfo - If this instruction is embedded into a MachineFunction,
816/// return the MachineRegisterInfo object for the current function, otherwise
817/// return null.
818MachineRegisterInfo *MachineInstr::getRegInfo() {
819 if (MachineBasicBlock *MBB = getParent())
Dan Gohmanf188fa42008-07-08 23:59:09 +0000820 return &MBB->getParent()->getRegInfo();
Craig Topperc0196b12014-04-14 00:51:57 +0000821 return nullptr;
Chris Lattner961e7422008-01-01 01:12:31 +0000822}
823
824/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
825/// this instruction from their respective use lists. This requires that the
826/// operands already be on their use lists.
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000827void MachineInstr::RemoveRegOperandsFromUseLists(MachineRegisterInfo &MRI) {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000828 for (MachineOperand &MO : operands())
829 if (MO.isReg())
830 MRI.removeRegOperandFromUseList(&MO);
Chris Lattner961e7422008-01-01 01:12:31 +0000831}
832
833/// AddRegOperandsToUseLists - Add all of the register operands in
834/// this instruction from their respective use lists. This requires that the
835/// operands not be on their use lists yet.
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000836void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &MRI) {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000837 for (MachineOperand &MO : operands())
838 if (MO.isReg())
839 MRI.addRegOperandToUseList(&MO);
Chris Lattner961e7422008-01-01 01:12:31 +0000840}
841
Jakob Stoklund Olesen2455b5852012-12-20 22:54:05 +0000842void MachineInstr::addOperand(const MachineOperand &Op) {
843 MachineBasicBlock *MBB = getParent();
844 assert(MBB && "Use MachineInstrBuilder to add operands to dangling instrs");
845 MachineFunction *MF = MBB->getParent();
846 assert(MF && "Use MachineInstrBuilder to add operands to dangling instrs");
847 addOperand(*MF, Op);
848}
849
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000850/// Move NumOps MachineOperands from Src to Dst, with support for overlapping
851/// ranges. If MRI is non-null also update use-def chains.
852static void moveOperands(MachineOperand *Dst, MachineOperand *Src,
853 unsigned NumOps, MachineRegisterInfo *MRI) {
854 if (MRI)
855 return MRI->moveOperands(Dst, Src, NumOps);
856
JF Bastiena874d1a2016-03-26 18:20:02 +0000857 // MachineOperand is a trivially copyable type so we can just use memmove.
Benjamin Kramer5c0e64f2015-02-21 16:22:48 +0000858 std::memmove(Dst, Src, NumOps * sizeof(MachineOperand));
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000859}
860
Chris Lattner961e7422008-01-01 01:12:31 +0000861/// addOperand - Add the specified operand to the instruction. If it is an
862/// implicit operand, it is added to the end of the operand list. If it is
863/// an explicit operand it is added at the end of the explicit operand list
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000864/// (before the first implicit operand).
Jakob Stoklund Olesen2455b5852012-12-20 22:54:05 +0000865void MachineInstr::addOperand(MachineFunction &MF, const MachineOperand &Op) {
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000866 assert(MCID && "Cannot add operands before providing an instr descriptor");
Dan Gohman9356d8f2008-12-09 22:45:08 +0000867
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000868 // Check if we're adding one of our existing operands.
869 if (&Op >= Operands && &Op < Operands + NumOperands) {
870 // This is unusual: MI->addOperand(MI->getOperand(i)).
871 // If adding Op requires reallocating or moving existing operands around,
872 // the Op reference could go stale. Support it by copying Op.
873 MachineOperand CopyOp(Op);
874 return addOperand(MF, CopyOp);
875 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000876
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000877 // Find the insert location for the new operand. Implicit registers go at
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000878 // the end, everything else goes before the implicit regs.
879 //
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000880 // FIXME: Allow mixed explicit and implicit operands on inline asm.
881 // InstrEmitter::EmitSpecialNode() is marking inline asm clobbers as
882 // implicit-defs, but they must not be moved around. See the FIXME in
883 // InstrEmitter.cpp.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000884 unsigned OpNo = getNumOperands();
885 bool isImpReg = Op.isReg() && Op.isImplicit();
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000886 if (!isImpReg && !isInlineAsm()) {
887 while (OpNo && Operands[OpNo-1].isReg() && Operands[OpNo-1].isImplicit()) {
888 --OpNo;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000889 assert(!Operands[OpNo].isTied() && "Cannot move tied operands");
Chris Lattner961e7422008-01-01 01:12:31 +0000890 }
891 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000892
Pekka Jaaskelaineneb4a6e72013-10-15 14:40:46 +0000893#ifndef NDEBUG
Pekka Jaaskelaineneb08e2e2013-10-15 14:18:10 +0000894 bool isMetaDataOp = Op.getType() == MachineOperand::MO_Metadata;
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000895 // OpNo now points as the desired insertion point. Unless this is a variadic
896 // instruction, only implicit regs are allowed beyond MCID->getNumOperands().
Jakob Stoklund Olesenc300ef02012-07-04 23:53:23 +0000897 // RegMask operands go between the explicit and implicit operands.
898 assert((isImpReg || Op.isRegMask() || MCID->isVariadic() ||
Pekka Jaaskelaineneb08e2e2013-10-15 14:18:10 +0000899 OpNo < MCID->getNumOperands() || isMetaDataOp) &&
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000900 "Trying to add an operand to a machine instr that is already done!");
Pekka Jaaskelaineneb4a6e72013-10-15 14:40:46 +0000901#endif
Chris Lattner961e7422008-01-01 01:12:31 +0000902
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000903 MachineRegisterInfo *MRI = getRegInfo();
Chris Lattner961e7422008-01-01 01:12:31 +0000904
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000905 // Determine if the Operands array needs to be reallocated.
906 // Save the old capacity and operand array.
907 OperandCapacity OldCap = CapOperands;
908 MachineOperand *OldOperands = Operands;
909 if (!OldOperands || OldCap.getSize() == getNumOperands()) {
910 CapOperands = OldOperands ? OldCap.getNext() : OldCap.get(1);
911 Operands = MF.allocateOperandArray(CapOperands);
912 // Move the operands before the insertion point.
913 if (OpNo)
914 moveOperands(Operands, OldOperands, OpNo, MRI);
915 }
Chris Lattner961e7422008-01-01 01:12:31 +0000916
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000917 // Move the operands following the insertion point.
918 if (OpNo != NumOperands)
919 moveOperands(Operands + OpNo + 1, OldOperands + OpNo, NumOperands - OpNo,
920 MRI);
921 ++NumOperands;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000922
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000923 // Deallocate the old operand array.
924 if (OldOperands != Operands && OldOperands)
925 MF.deallocateOperandArray(OldCap, OldOperands);
926
927 // Copy Op into place. It still needs to be inserted into the MRI use lists.
928 MachineOperand *NewMO = new (Operands + OpNo) MachineOperand(Op);
929 NewMO->ParentMI = this;
930
931 // When adding a register operand, tell MRI about it.
932 if (NewMO->isReg()) {
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000933 // Ensure isOnRegUseList() returns false, regardless of Op's status.
Craig Topperc0196b12014-04-14 00:51:57 +0000934 NewMO->Contents.Reg.Prev = nullptr;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000935 // Ignore existing ties. This is not a property that can be copied.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000936 NewMO->TiedTo = 0;
937 // Add the new operand to MRI, but only for instructions in an MBB.
938 if (MRI)
939 MRI->addRegOperandToUseList(NewMO);
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000940 // The MCID operand information isn't accurate until we start adding
941 // explicit operands. The implicit operands are added first, then the
942 // explicits are inserted before them.
943 if (!isImpReg) {
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000944 // Tie uses to defs as indicated in MCInstrDesc.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000945 if (NewMO->isUse()) {
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000946 int DefIdx = MCID->getOperandConstraint(OpNo, MCOI::TIED_TO);
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +0000947 if (DefIdx != -1)
948 tieOperands(DefIdx, OpNo);
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000949 }
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000950 // If the register operand is flagged as early, mark the operand as such.
951 if (MCID->getOperandConstraint(OpNo, MCOI::EARLY_CLOBBER) != -1)
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000952 NewMO->setIsEarlyClobber(true);
Chris Lattner961e7422008-01-01 01:12:31 +0000953 }
Chris Lattner961e7422008-01-01 01:12:31 +0000954 }
955}
956
957/// RemoveOperand - Erase an operand from an instruction, leaving it with one
958/// fewer operand than it started with.
959///
960void MachineInstr::RemoveOperand(unsigned OpNo) {
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +0000961 assert(OpNo < getNumOperands() && "Invalid operand number");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000962 untieRegOperand(OpNo);
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000963
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000964#ifndef NDEBUG
965 // Moving tied operands would break the ties.
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +0000966 for (unsigned i = OpNo + 1, e = getNumOperands(); i != e; ++i)
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000967 if (Operands[i].isReg())
968 assert(!Operands[i].isTied() && "Cannot move tied operands");
969#endif
970
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000971 MachineRegisterInfo *MRI = getRegInfo();
972 if (MRI && Operands[OpNo].isReg())
973 MRI->removeRegOperandFromUseList(Operands + OpNo);
Chris Lattner961e7422008-01-01 01:12:31 +0000974
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000975 // Don't call the MachineOperand destructor. A lot of this code depends on
976 // MachineOperand having a trivial destructor anyway, and adding a call here
977 // wouldn't make it 'destructor-correct'.
978
979 if (unsigned N = NumOperands - 1 - OpNo)
980 moveOperands(Operands + OpNo, Operands + OpNo + 1, N, MRI);
981 --NumOperands;
Chris Lattner961e7422008-01-01 01:12:31 +0000982}
983
Dan Gohman48b185d2009-09-25 20:36:54 +0000984/// addMemOperand - Add a MachineMemOperand to the machine instruction.
985/// This function should be used only occasionally. The setMemRefs function
986/// is the primary method for setting up a MachineInstr's MemRefs list.
Dan Gohman3b460302008-07-07 23:14:23 +0000987void MachineInstr::addMemOperand(MachineFunction &MF,
Dan Gohman48b185d2009-09-25 20:36:54 +0000988 MachineMemOperand *MO) {
989 mmo_iterator OldMemRefs = MemRefs;
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000990 unsigned OldNumMemRefs = NumMemRefs;
Dan Gohman3b460302008-07-07 23:14:23 +0000991
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000992 unsigned NewNum = NumMemRefs + 1;
Dan Gohman48b185d2009-09-25 20:36:54 +0000993 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
Dan Gohman3b460302008-07-07 23:14:23 +0000994
Benjamin Kramerd03878b2012-03-16 16:39:27 +0000995 std::copy(OldMemRefs, OldMemRefs + OldNumMemRefs, NewMemRefs);
Dan Gohman48b185d2009-09-25 20:36:54 +0000996 NewMemRefs[NewNum - 1] = MO;
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000997 setMemRefs(NewMemRefs, NewMemRefs + NewNum);
Dan Gohman48b185d2009-09-25 20:36:54 +0000998}
Chris Lattner961e7422008-01-01 01:12:31 +0000999
Philip Reames5eb90a72016-01-06 19:33:12 +00001000/// Check to see if the MMOs pointed to by the two MemRefs arrays are
Junmo Park820e3922016-02-26 02:07:36 +00001001/// identical.
Philip Reames5eb90a72016-01-06 19:33:12 +00001002static bool hasIdenticalMMOs(const MachineInstr &MI1, const MachineInstr &MI2) {
1003 auto I1 = MI1.memoperands_begin(), E1 = MI1.memoperands_end();
1004 auto I2 = MI2.memoperands_begin(), E2 = MI2.memoperands_end();
1005 if ((E1 - I1) != (E2 - I2))
1006 return false;
1007 for (; I1 != E1; ++I1, ++I2) {
1008 if (**I1 != **I2)
1009 return false;
1010 }
1011 return true;
1012}
1013
Philip Reamesc86ed002016-01-06 04:39:03 +00001014std::pair<MachineInstr::mmo_iterator, unsigned>
1015MachineInstr::mergeMemRefsWith(const MachineInstr& Other) {
Philip Reames5eb90a72016-01-06 19:33:12 +00001016
1017 // If either of the incoming memrefs are empty, we must be conservative and
1018 // treat this as if we've exhausted our space for memrefs and dropped them.
1019 if (memoperands_empty() || Other.memoperands_empty())
1020 return std::make_pair(nullptr, 0);
1021
1022 // If both instructions have identical memrefs, we don't need to merge them.
1023 // Since many instructions have a single memref, and we tend to merge things
1024 // like pairs of loads from the same location, this catches a large number of
1025 // cases in practice.
1026 if (hasIdenticalMMOs(*this, Other))
1027 return std::make_pair(MemRefs, NumMemRefs);
Junmo Park820e3922016-02-26 02:07:36 +00001028
Philip Reamesc86ed002016-01-06 04:39:03 +00001029 // TODO: consider uniquing elements within the operand lists to reduce
1030 // space usage and fall back to conservative information less often.
Philip Reames5eb90a72016-01-06 19:33:12 +00001031 size_t CombinedNumMemRefs = NumMemRefs + Other.NumMemRefs;
1032
1033 // If we don't have enough room to store this many memrefs, be conservative
1034 // and drop them. Otherwise, we'd fail asserts when trying to add them to
1035 // the new instruction.
1036 if (CombinedNumMemRefs != uint8_t(CombinedNumMemRefs))
1037 return std::make_pair(nullptr, 0);
Philip Reamesc86ed002016-01-06 04:39:03 +00001038
1039 MachineFunction *MF = getParent()->getParent();
1040 mmo_iterator MemBegin = MF->allocateMemRefsArray(CombinedNumMemRefs);
1041 mmo_iterator MemEnd = std::copy(memoperands_begin(), memoperands_end(),
1042 MemBegin);
1043 MemEnd = std::copy(Other.memoperands_begin(), Other.memoperands_end(),
1044 MemEnd);
Philip Reames2d2fc4a2016-01-06 05:53:09 +00001045 assert(MemEnd - MemBegin == (ptrdiff_t)CombinedNumMemRefs &&
1046 "missing memrefs");
Junmo Park820e3922016-02-26 02:07:36 +00001047
Philip Reamesc86ed002016-01-06 04:39:03 +00001048 return std::make_pair(MemBegin, CombinedNumMemRefs);
1049}
1050
Benjamin Kramer97f889f2012-03-17 17:03:45 +00001051bool MachineInstr::hasPropertyInBundle(unsigned Mask, QueryType Type) const {
Jakob Stoklund Olesenf0615c72013-01-10 18:42:44 +00001052 assert(!isBundledWithPred() && "Must be called on bundle header");
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001053 for (MachineBasicBlock::const_instr_iterator MII = getIterator();; ++MII) {
Benjamin Kramer97f889f2012-03-17 17:03:45 +00001054 if (MII->getDesc().getFlags() & Mask) {
Evan Chengcdf89fd2011-12-08 19:23:10 +00001055 if (Type == AnyInBundle)
Evan Cheng7f8e5632011-12-07 07:15:52 +00001056 return true;
1057 } else {
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +00001058 if (Type == AllInBundle && !MII->isBundle())
Evan Cheng7f8e5632011-12-07 07:15:52 +00001059 return false;
1060 }
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +00001061 // This was the last instruction in the bundle.
1062 if (!MII->isBundledWithSucc())
1063 return Type == AllInBundle;
Evan Cheng2a81dd42011-12-06 22:12:01 +00001064 }
Evan Cheng2a81dd42011-12-06 22:12:01 +00001065}
1066
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001067bool MachineInstr::isIdenticalTo(const MachineInstr &Other,
Evan Chenge9c46c22010-03-03 01:44:33 +00001068 MICheckType Check) const {
Evan Cheng0f260e12010-03-03 21:54:14 +00001069 // If opcodes or number of operands are not the same then the two
1070 // instructions are obviously not identical.
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001071 if (Other.getOpcode() != getOpcode() ||
1072 Other.getNumOperands() != getNumOperands())
Evan Cheng0f260e12010-03-03 21:54:14 +00001073 return false;
1074
Evan Cheng7fae11b2011-12-14 02:11:42 +00001075 if (isBundle()) {
Bjorn Petterssonb29a15e2016-12-19 11:20:57 +00001076 // We have passed the test above that both instructions have the same
1077 // opcode, so we know that both instructions are bundles here. Let's compare
1078 // MIs inside the bundle.
1079 assert(Other.isBundle() && "Expected that both instructions are bundles.");
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001080 MachineBasicBlock::const_instr_iterator I1 = getIterator();
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001081 MachineBasicBlock::const_instr_iterator I2 = Other.getIterator();
Bjorn Petterssonb29a15e2016-12-19 11:20:57 +00001082 // Loop until we analysed the last intruction inside at least one of the
1083 // bundles.
1084 while (I1->isBundledWithSucc() && I2->isBundledWithSucc()) {
1085 ++I1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00001086 ++I2;
Bjorn Petterssonb29a15e2016-12-19 11:20:57 +00001087 if (!I1->isIdenticalTo(*I2, Check))
Evan Cheng7fae11b2011-12-14 02:11:42 +00001088 return false;
1089 }
Bjorn Petterssonb29a15e2016-12-19 11:20:57 +00001090 // If we've reached the end of just one of the two bundles, but not both,
1091 // the instructions are not identical.
1092 if (I1->isBundledWithSucc() || I2->isBundledWithSucc())
1093 return false;
Evan Cheng7fae11b2011-12-14 02:11:42 +00001094 }
1095
Evan Cheng0f260e12010-03-03 21:54:14 +00001096 // Check operands to make sure they match.
1097 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1098 const MachineOperand &MO = getOperand(i);
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001099 const MachineOperand &OMO = Other.getOperand(i);
Evan Chengcfdf3392011-05-12 00:56:58 +00001100 if (!MO.isReg()) {
1101 if (!MO.isIdenticalTo(OMO))
1102 return false;
1103 continue;
1104 }
1105
Evan Cheng0f260e12010-03-03 21:54:14 +00001106 // Clients may or may not want to ignore defs when testing for equality.
1107 // For example, machine CSE pass only cares about finding common
1108 // subexpressions, so it's safe to ignore virtual register defs.
Evan Chengcfdf3392011-05-12 00:56:58 +00001109 if (MO.isDef()) {
Evan Cheng0f260e12010-03-03 21:54:14 +00001110 if (Check == IgnoreDefs)
1111 continue;
Evan Chengcfdf3392011-05-12 00:56:58 +00001112 else if (Check == IgnoreVRegDefs) {
1113 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) ||
1114 TargetRegisterInfo::isPhysicalRegister(OMO.getReg()))
1115 if (MO.getReg() != OMO.getReg())
1116 return false;
1117 } else {
1118 if (!MO.isIdenticalTo(OMO))
Evan Cheng0f260e12010-03-03 21:54:14 +00001119 return false;
Evan Chengcfdf3392011-05-12 00:56:58 +00001120 if (Check == CheckKillDead && MO.isDead() != OMO.isDead())
1121 return false;
1122 }
1123 } else {
1124 if (!MO.isIdenticalTo(OMO))
1125 return false;
1126 if (Check == CheckKillDead && MO.isKill() != OMO.isKill())
1127 return false;
1128 }
Evan Cheng0f260e12010-03-03 21:54:14 +00001129 }
Devang Patelbf8cc602011-07-07 17:45:33 +00001130 // If DebugLoc does not match then two dbg.values are not identical.
1131 if (isDebugValue())
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001132 if (getDebugLoc() && Other.getDebugLoc() &&
1133 getDebugLoc() != Other.getDebugLoc())
Devang Patelbf8cc602011-07-07 17:45:33 +00001134 return false;
Evan Cheng0f260e12010-03-03 21:54:14 +00001135 return true;
Evan Chenge9c46c22010-03-03 01:44:33 +00001136}
1137
Chris Lattnerbec79b42006-04-17 21:35:41 +00001138MachineInstr *MachineInstr::removeFromParent() {
1139 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001140 return getParent()->remove(this);
Chris Lattnerbec79b42006-04-17 21:35:41 +00001141}
1142
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001143MachineInstr *MachineInstr::removeFromBundle() {
1144 assert(getParent() && "Not embedded in a basic block!");
1145 return getParent()->remove_instr(this);
1146}
Chris Lattnerbec79b42006-04-17 21:35:41 +00001147
Dan Gohman3b460302008-07-07 23:14:23 +00001148void MachineInstr::eraseFromParent() {
1149 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001150 getParent()->erase(this);
Dan Gohman3b460302008-07-07 23:14:23 +00001151}
1152
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +00001153void MachineInstr::eraseFromParentAndMarkDBGValuesForRemoval() {
1154 assert(getParent() && "Not embedded in a basic block!");
1155 MachineBasicBlock *MBB = getParent();
1156 MachineFunction *MF = MBB->getParent();
1157 assert(MF && "Not embedded in a function!");
1158
1159 MachineInstr *MI = (MachineInstr *)this;
1160 MachineRegisterInfo &MRI = MF->getRegInfo();
1161
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001162 for (const MachineOperand &MO : MI->operands()) {
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +00001163 if (!MO.isReg() || !MO.isDef())
1164 continue;
1165 unsigned Reg = MO.getReg();
1166 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1167 continue;
1168 MRI.markUsesInDebugValueAsUndef(Reg);
1169 }
1170 MI->eraseFromParent();
1171}
1172
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001173void MachineInstr::eraseFromBundle() {
1174 assert(getParent() && "Not embedded in a basic block!");
1175 getParent()->erase_instr(this);
1176}
Dan Gohman3b460302008-07-07 23:14:23 +00001177
Evan Cheng4d728b02007-05-15 01:26:09 +00001178/// getNumExplicitOperands - Returns the number of non-implicit operands.
1179///
1180unsigned MachineInstr::getNumExplicitOperands() const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001181 unsigned NumOperands = MCID->getNumOperands();
1182 if (!MCID->isVariadic())
Evan Cheng4d728b02007-05-15 01:26:09 +00001183 return NumOperands;
1184
Dan Gohman37608532009-04-15 17:59:11 +00001185 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
1186 const MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001187 if (!MO.isReg() || !MO.isImplicit())
Evan Cheng4d728b02007-05-15 01:26:09 +00001188 NumOperands++;
1189 }
1190 return NumOperands;
1191}
1192
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001193void MachineInstr::bundleWithPred() {
1194 assert(!isBundledWithPred() && "MI is already bundled with its predecessor");
1195 setFlag(BundledPred);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001196 MachineBasicBlock::instr_iterator Pred = getIterator();
1197 --Pred;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001198 assert(!Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001199 Pred->setFlag(BundledSucc);
1200}
1201
1202void MachineInstr::bundleWithSucc() {
1203 assert(!isBundledWithSucc() && "MI is already bundled with its successor");
1204 setFlag(BundledSucc);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001205 MachineBasicBlock::instr_iterator Succ = getIterator();
1206 ++Succ;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001207 assert(!Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001208 Succ->setFlag(BundledPred);
1209}
1210
1211void MachineInstr::unbundleFromPred() {
1212 assert(isBundledWithPred() && "MI isn't bundled with its predecessor");
1213 clearFlag(BundledPred);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001214 MachineBasicBlock::instr_iterator Pred = getIterator();
1215 --Pred;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001216 assert(Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001217 Pred->clearFlag(BundledSucc);
1218}
1219
1220void MachineInstr::unbundleFromSucc() {
1221 assert(isBundledWithSucc() && "MI isn't bundled with its successor");
1222 clearFlag(BundledSucc);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001223 MachineBasicBlock::instr_iterator Succ = getIterator();
1224 ++Succ;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001225 assert(Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001226 Succ->clearFlag(BundledPred);
1227}
1228
Evan Cheng6eb516d2011-01-07 23:50:32 +00001229bool MachineInstr::isStackAligningInlineAsm() const {
1230 if (isInlineAsm()) {
1231 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1232 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
1233 return true;
1234 }
1235 return false;
1236}
Chris Lattner33f5af02006-10-20 22:39:59 +00001237
Chad Rosier994f4042012-09-05 21:00:58 +00001238InlineAsm::AsmDialect MachineInstr::getInlineAsmDialect() const {
1239 assert(isInlineAsm() && "getInlineAsmDialect() only works for inline asms!");
1240 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
Chad Rosiere53314f2012-09-05 22:40:13 +00001241 return InlineAsm::AsmDialect((ExtraInfo & InlineAsm::Extra_AsmDialect) != 0);
Chad Rosier994f4042012-09-05 21:00:58 +00001242}
1243
Jakob Stoklund Olesen1e737162011-10-12 23:37:33 +00001244int MachineInstr::findInlineAsmFlagIdx(unsigned OpIdx,
1245 unsigned *GroupNo) const {
1246 assert(isInlineAsm() && "Expected an inline asm instruction");
1247 assert(OpIdx < getNumOperands() && "OpIdx out of range");
1248
1249 // Ignore queries about the initial operands.
1250 if (OpIdx < InlineAsm::MIOp_FirstOperand)
1251 return -1;
1252
1253 unsigned Group = 0;
1254 unsigned NumOps;
1255 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
1256 i += NumOps) {
1257 const MachineOperand &FlagMO = getOperand(i);
1258 // If we reach the implicit register operands, stop looking.
1259 if (!FlagMO.isImm())
1260 return -1;
1261 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
1262 if (i + NumOps > OpIdx) {
1263 if (GroupNo)
1264 *GroupNo = Group;
1265 return i;
1266 }
1267 ++Group;
1268 }
1269 return -1;
1270}
1271
Reid Kleckner28865802016-04-14 18:29:59 +00001272const DILocalVariable *MachineInstr::getDebugVariable() const {
1273 assert(isDebugValue() && "not a DBG_VALUE");
1274 return cast<DILocalVariable>(getOperand(2).getMetadata());
1275}
1276
1277const DIExpression *MachineInstr::getDebugExpression() const {
1278 assert(isDebugValue() && "not a DBG_VALUE");
1279 return cast<DIExpression>(getOperand(3).getMetadata());
1280}
1281
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001282const TargetRegisterClass*
1283MachineInstr::getRegClassConstraint(unsigned OpIdx,
1284 const TargetInstrInfo *TII,
1285 const TargetRegisterInfo *TRI) const {
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001286 assert(getParent() && "Can't have an MBB reference here!");
1287 assert(getParent()->getParent() && "Can't have an MF reference here!");
1288 const MachineFunction &MF = *getParent()->getParent();
1289
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001290 // Most opcodes have fixed constraints in their MCInstrDesc.
1291 if (!isInlineAsm())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001292 return TII->getRegClass(getDesc(), OpIdx, TRI, MF);
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001293
1294 if (!getOperand(OpIdx).isReg())
Craig Topperc0196b12014-04-14 00:51:57 +00001295 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001296
1297 // For tied uses on inline asm, get the constraint from the def.
1298 unsigned DefIdx;
1299 if (getOperand(OpIdx).isUse() && isRegTiedToDefOperand(OpIdx, &DefIdx))
1300 OpIdx = DefIdx;
1301
1302 // Inline asm stores register class constraints in the flag word.
1303 int FlagIdx = findInlineAsmFlagIdx(OpIdx);
1304 if (FlagIdx < 0)
Craig Topperc0196b12014-04-14 00:51:57 +00001305 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001306
1307 unsigned Flag = getOperand(FlagIdx).getImm();
1308 unsigned RCID;
Simon Dardisd32a2d32016-07-18 13:17:31 +00001309 if ((InlineAsm::getKind(Flag) == InlineAsm::Kind_RegUse ||
1310 InlineAsm::getKind(Flag) == InlineAsm::Kind_RegDef ||
1311 InlineAsm::getKind(Flag) == InlineAsm::Kind_RegDefEarlyClobber) &&
1312 InlineAsm::hasRegClassConstraint(Flag, RCID))
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001313 return TRI->getRegClass(RCID);
1314
1315 // Assume that all registers in a memory operand are pointers.
1316 if (InlineAsm::getKind(Flag) == InlineAsm::Kind_Mem)
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001317 return TRI->getPointerRegClass(MF);
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001318
Craig Topperc0196b12014-04-14 00:51:57 +00001319 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001320}
1321
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001322const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVReg(
1323 unsigned Reg, const TargetRegisterClass *CurRC, const TargetInstrInfo *TII,
1324 const TargetRegisterInfo *TRI, bool ExploreBundle) const {
1325 // Check every operands inside the bundle if we have
1326 // been asked to.
1327 if (ExploreBundle)
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001328 for (ConstMIBundleOperands OpndIt(*this); OpndIt.isValid() && CurRC;
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001329 ++OpndIt)
1330 CurRC = OpndIt->getParent()->getRegClassConstraintEffectForVRegImpl(
1331 OpndIt.getOperandNo(), Reg, CurRC, TII, TRI);
1332 else
1333 // Otherwise, just check the current operands.
Matthias Braune41e1462015-05-29 02:56:46 +00001334 for (unsigned i = 0, e = NumOperands; i < e && CurRC; ++i)
1335 CurRC = getRegClassConstraintEffectForVRegImpl(i, Reg, CurRC, TII, TRI);
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001336 return CurRC;
1337}
1338
1339const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVRegImpl(
1340 unsigned OpIdx, unsigned Reg, const TargetRegisterClass *CurRC,
1341 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
1342 assert(CurRC && "Invalid initial register class");
1343 // Check if Reg is constrained by some of its use/def from MI.
1344 const MachineOperand &MO = getOperand(OpIdx);
1345 if (!MO.isReg() || MO.getReg() != Reg)
1346 return CurRC;
1347 // If yes, accumulate the constraints through the operand.
1348 return getRegClassConstraintEffect(OpIdx, CurRC, TII, TRI);
1349}
1350
1351const TargetRegisterClass *MachineInstr::getRegClassConstraintEffect(
1352 unsigned OpIdx, const TargetRegisterClass *CurRC,
1353 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
1354 const TargetRegisterClass *OpRC = getRegClassConstraint(OpIdx, TII, TRI);
1355 const MachineOperand &MO = getOperand(OpIdx);
1356 assert(MO.isReg() &&
1357 "Cannot get register constraints for non-register operand");
1358 assert(CurRC && "Invalid initial register class");
1359 if (unsigned SubIdx = MO.getSubReg()) {
1360 if (OpRC)
1361 CurRC = TRI->getMatchingSuperRegClass(CurRC, OpRC, SubIdx);
1362 else
1363 CurRC = TRI->getSubClassWithSubReg(CurRC, SubIdx);
1364 } else if (OpRC)
1365 CurRC = TRI->getCommonSubClass(CurRC, OpRC);
1366 return CurRC;
1367}
1368
Jakob Stoklund Olesen68d752b2013-01-09 18:28:16 +00001369/// Return the number of instructions inside the MI bundle, not counting the
1370/// header instruction.
Evan Cheng7fae11b2011-12-14 02:11:42 +00001371unsigned MachineInstr::getBundleSize() const {
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001372 MachineBasicBlock::const_instr_iterator I = getIterator();
Evan Cheng7fae11b2011-12-14 02:11:42 +00001373 unsigned Size = 0;
Richard Trieu7a083812016-02-18 22:09:30 +00001374 while (I->isBundledWithSucc()) {
1375 ++Size;
1376 ++I;
1377 }
Evan Cheng7fae11b2011-12-14 02:11:42 +00001378 return Size;
1379}
1380
Nicolai Haehnleb0c97482016-04-22 04:04:08 +00001381/// Returns true if the MachineInstr has an implicit-use operand of exactly
1382/// the given register (not considering sub/super-registers).
1383bool MachineInstr::hasRegisterImplicitUseOperand(unsigned Reg) const {
1384 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1385 const MachineOperand &MO = getOperand(i);
1386 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == Reg)
1387 return true;
1388 }
1389 return false;
1390}
1391
Evan Cheng910c8082007-04-26 19:00:32 +00001392/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Jim Grosbach9632c142009-09-17 17:57:26 +00001393/// the specific register or -1 if it is not found. It further tightens
Evan Cheng9965aeb2007-02-23 01:04:26 +00001394/// the search criteria to a use that kills the register if isKill is true.
Fraser Cormack48d9fdc2016-10-11 09:09:21 +00001395int MachineInstr::findRegisterUseOperandIdx(
1396 unsigned Reg, bool isKill, const TargetRegisterInfo *TRI) const {
Evan Cheng75c21942006-12-06 08:27:42 +00001397 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng5983bdb2007-05-29 18:35:22 +00001398 const MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001399 if (!MO.isReg() || !MO.isUse())
Evan Cheng63254462008-03-05 00:59:57 +00001400 continue;
1401 unsigned MOReg = MO.getReg();
1402 if (!MOReg)
1403 continue;
Fraser Cormack48d9fdc2016-10-11 09:09:21 +00001404 if (MOReg == Reg || (TRI && TargetRegisterInfo::isPhysicalRegister(MOReg) &&
1405 TargetRegisterInfo::isPhysicalRegister(Reg) &&
1406 TRI->isSubRegister(MOReg, Reg)))
Evan Cheng9965aeb2007-02-23 01:04:26 +00001407 if (!isKill || MO.isKill())
Evan Chengec3ac312007-03-26 22:37:45 +00001408 return i;
Evan Cheng75c21942006-12-06 08:27:42 +00001409 }
Evan Chengec3ac312007-03-26 22:37:45 +00001410 return -1;
Evan Cheng75c21942006-12-06 08:27:42 +00001411}
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001412
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001413/// readsWritesVirtualRegister - Return a pair of bools (reads, writes)
1414/// indicating if this instruction reads or writes Reg. This also considers
1415/// partial defines.
1416std::pair<bool,bool>
1417MachineInstr::readsWritesVirtualRegister(unsigned Reg,
1418 SmallVectorImpl<unsigned> *Ops) const {
1419 bool PartDef = false; // Partial redefine.
1420 bool FullDef = false; // Full define.
1421 bool Use = false;
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001422
1423 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1424 const MachineOperand &MO = getOperand(i);
1425 if (!MO.isReg() || MO.getReg() != Reg)
1426 continue;
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001427 if (Ops)
1428 Ops->push_back(i);
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001429 if (MO.isUse())
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001430 Use |= !MO.isUndef();
Jakob Stoklund Olesen9eb77bf2011-08-19 00:30:17 +00001431 else if (MO.getSubReg() && !MO.isUndef())
1432 // A partial <def,undef> doesn't count as reading the register.
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001433 PartDef = true;
1434 else
1435 FullDef = true;
1436 }
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001437 // A partial redefine uses Reg unless there is also a full define.
1438 return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef);
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001439}
1440
Evan Cheng63254462008-03-05 00:59:57 +00001441/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman72a0bc12008-05-06 00:20:10 +00001442/// the specified register or -1 if it is not found. If isDead is true, defs
1443/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
1444/// also checks if there is a def of a super-register.
Evan Cheng38584512010-05-21 20:53:24 +00001445int
1446MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, bool Overlap,
1447 const TargetRegisterInfo *TRI) const {
1448 bool isPhys = TargetRegisterInfo::isPhysicalRegister(Reg);
Evan Chengf7ed82d2007-02-19 21:49:54 +00001449 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng63254462008-03-05 00:59:57 +00001450 const MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesene7d3f442012-02-14 23:49:37 +00001451 // Accept regmask operands when Overlap is set.
1452 // Ignore them when looking for a specific def operand (Overlap == false).
1453 if (isPhys && Overlap && MO.isRegMask() && MO.clobbersPhysReg(Reg))
1454 return i;
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001455 if (!MO.isReg() || !MO.isDef())
Evan Cheng63254462008-03-05 00:59:57 +00001456 continue;
1457 unsigned MOReg = MO.getReg();
Evan Cheng38584512010-05-21 20:53:24 +00001458 bool Found = (MOReg == Reg);
1459 if (!Found && TRI && isPhys &&
1460 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
1461 if (Overlap)
1462 Found = TRI->regsOverlap(MOReg, Reg);
1463 else
1464 Found = TRI->isSubRegister(MOReg, Reg);
1465 }
1466 if (Found && (!isDead || MO.isDead()))
1467 return i;
Evan Chengf7ed82d2007-02-19 21:49:54 +00001468 }
Evan Cheng63254462008-03-05 00:59:57 +00001469 return -1;
Evan Chengf7ed82d2007-02-19 21:49:54 +00001470}
Evan Cheng4d728b02007-05-15 01:26:09 +00001471
Evan Cheng5983bdb2007-05-29 18:35:22 +00001472/// findFirstPredOperandIdx() - Find the index of the first operand in the
1473/// operand list that is used to represent the predicate. It returns -1 if
1474/// none is found.
1475int MachineInstr::findFirstPredOperandIdx() const {
Jim Grosbached16ec42011-08-29 22:24:09 +00001476 // Don't call MCID.findFirstPredOperandIdx() because this variant
1477 // is sometimes called on an instruction that's not yet complete, and
1478 // so the number of operands is less than the MCID indicates. In
1479 // particular, the PTX target does this.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001480 const MCInstrDesc &MCID = getDesc();
1481 if (MCID.isPredicable()) {
Evan Cheng4d728b02007-05-15 01:26:09 +00001482 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Evan Cheng6cc775f2011-06-28 19:10:37 +00001483 if (MCID.OpInfo[i].isPredicate())
Evan Cheng5983bdb2007-05-29 18:35:22 +00001484 return i;
Evan Cheng4d728b02007-05-15 01:26:09 +00001485 }
1486
Evan Cheng5983bdb2007-05-29 18:35:22 +00001487 return -1;
Evan Cheng4d728b02007-05-15 01:26:09 +00001488}
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001489
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001490// MachineOperand::TiedTo is 4 bits wide.
1491const unsigned TiedMax = 15;
1492
1493/// tieOperands - Mark operands at DefIdx and UseIdx as tied to each other.
1494///
1495/// Use and def operands can be tied together, indicated by a non-zero TiedTo
1496/// field. TiedTo can have these values:
1497///
1498/// 0: Operand is not tied to anything.
1499/// 1 to TiedMax-1: Tied to getOperand(TiedTo-1).
1500/// TiedMax: Tied to an operand >= TiedMax-1.
1501///
1502/// The tied def must be one of the first TiedMax operands on a normal
1503/// instruction. INLINEASM instructions allow more tied defs.
1504///
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001505void MachineInstr::tieOperands(unsigned DefIdx, unsigned UseIdx) {
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001506 MachineOperand &DefMO = getOperand(DefIdx);
1507 MachineOperand &UseMO = getOperand(UseIdx);
1508 assert(DefMO.isDef() && "DefIdx must be a def operand");
1509 assert(UseMO.isUse() && "UseIdx must be a use operand");
1510 assert(!DefMO.isTied() && "Def is already tied to another use");
1511 assert(!UseMO.isTied() && "Use is already tied to another def");
1512
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001513 if (DefIdx < TiedMax)
1514 UseMO.TiedTo = DefIdx + 1;
1515 else {
1516 // Inline asm can use the group descriptors to find tied operands, but on
1517 // normal instruction, the tied def must be within the first TiedMax
1518 // operands.
1519 assert(isInlineAsm() && "DefIdx out of range");
1520 UseMO.TiedTo = TiedMax;
1521 }
1522
1523 // UseIdx can be out of range, we'll search for it in findTiedOperandIdx().
1524 DefMO.TiedTo = std::min(UseIdx + 1, TiedMax);
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001525}
1526
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001527/// Given the index of a tied register operand, find the operand it is tied to.
1528/// Defs are tied to uses and vice versa. Returns the index of the tied operand
1529/// which must exist.
1530unsigned MachineInstr::findTiedOperandIdx(unsigned OpIdx) const {
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001531 const MachineOperand &MO = getOperand(OpIdx);
1532 assert(MO.isTied() && "Operand isn't tied");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001533
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001534 // Normally TiedTo is in range.
1535 if (MO.TiedTo < TiedMax)
1536 return MO.TiedTo - 1;
1537
1538 // Uses on normal instructions can be out of range.
1539 if (!isInlineAsm()) {
1540 // Normal tied defs must be in the 0..TiedMax-1 range.
1541 if (MO.isUse())
1542 return TiedMax - 1;
1543 // MO is a def. Search for the tied use.
1544 for (unsigned i = TiedMax - 1, e = getNumOperands(); i != e; ++i) {
1545 const MachineOperand &UseMO = getOperand(i);
1546 if (UseMO.isReg() && UseMO.isUse() && UseMO.TiedTo == OpIdx + 1)
1547 return i;
1548 }
1549 llvm_unreachable("Can't find tied use");
1550 }
1551
1552 // Now deal with inline asm by parsing the operand group descriptor flags.
1553 // Find the beginning of each operand group.
1554 SmallVector<unsigned, 8> GroupIdx;
1555 unsigned OpIdxGroup = ~0u;
1556 unsigned NumOps;
1557 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
1558 i += NumOps) {
1559 const MachineOperand &FlagMO = getOperand(i);
1560 assert(FlagMO.isImm() && "Invalid tied operand on inline asm");
1561 unsigned CurGroup = GroupIdx.size();
1562 GroupIdx.push_back(i);
1563 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
1564 // OpIdx belongs to this operand group.
1565 if (OpIdx > i && OpIdx < i + NumOps)
1566 OpIdxGroup = CurGroup;
1567 unsigned TiedGroup;
1568 if (!InlineAsm::isUseOperandTiedToDef(FlagMO.getImm(), TiedGroup))
1569 continue;
1570 // Operands in this group are tied to operands in TiedGroup which must be
1571 // earlier. Find the number of operands between the two groups.
1572 unsigned Delta = i - GroupIdx[TiedGroup];
1573
1574 // OpIdx is a use tied to TiedGroup.
1575 if (OpIdxGroup == CurGroup)
1576 return OpIdx - Delta;
1577
1578 // OpIdx is a def tied to this use group.
1579 if (OpIdxGroup == TiedGroup)
1580 return OpIdx + Delta;
1581 }
1582 llvm_unreachable("Invalid tied operand on inline asm");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001583}
1584
Dan Gohmanc90f51c2010-05-13 20:34:42 +00001585/// clearKillInfo - Clears kill flags on all operands.
1586///
1587void MachineInstr::clearKillInfo() {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001588 for (MachineOperand &MO : operands()) {
Dan Gohmanc90f51c2010-05-13 20:34:42 +00001589 if (MO.isReg() && MO.isUse())
1590 MO.setIsKill(false);
1591 }
1592}
1593
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001594void MachineInstr::substituteRegister(unsigned FromReg,
1595 unsigned ToReg,
1596 unsigned SubIdx,
1597 const TargetRegisterInfo &RegInfo) {
1598 if (TargetRegisterInfo::isPhysicalRegister(ToReg)) {
1599 if (SubIdx)
1600 ToReg = RegInfo.getSubReg(ToReg, SubIdx);
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001601 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001602 if (!MO.isReg() || MO.getReg() != FromReg)
1603 continue;
1604 MO.substPhysReg(ToReg, RegInfo);
1605 }
1606 } else {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001607 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001608 if (!MO.isReg() || MO.getReg() != FromReg)
1609 continue;
1610 MO.substVirtReg(ToReg, SubIdx, RegInfo);
1611 }
1612 }
1613}
1614
Evan Cheng7d98a482008-07-03 09:09:37 +00001615/// isSafeToMove - Return true if it is safe to move this instruction. If
1616/// SawStore is set to true, it means that there is a store (or call) between
1617/// the instruction's location and its intended destination.
Matthias Braun07066cc2015-05-19 21:22:20 +00001618bool MachineInstr::isSafeToMove(AliasAnalysis *AA, bool &SawStore) const {
Evan Cheng399e1102008-03-13 00:44:09 +00001619 // Ignore stuff that we obviously can't move.
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001620 //
1621 // Treat volatile loads as stores. This is not strictly necessary for
Jakob Stoklund Olesend92e2bc2012-09-04 18:44:43 +00001622 // volatiles, but it is required for atomic loads. It is not allowed to move
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001623 // a load across an atomic load with Ordering > Monotonic.
1624 if (mayStore() || isCall() ||
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001625 (mayLoad() && hasOrderedMemoryRef())) {
Evan Cheng399e1102008-03-13 00:44:09 +00001626 SawStore = true;
1627 return false;
1628 }
Evan Cheng0638c202011-01-07 21:08:26 +00001629
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001630 if (isPosition() || isDebugValue() || isTerminator() ||
1631 hasUnmodeledSideEffects())
Evan Cheng399e1102008-03-13 00:44:09 +00001632 return false;
1633
1634 // See if this instruction does a load. If so, we have to guarantee that the
1635 // loaded value doesn't change between the load and the its intended
1636 // destination. The check for isInvariantLoad gives the targe the chance to
1637 // classify the load as always returning a constant, e.g. a constant pool
1638 // load.
Justin Lebard98cf002016-09-10 01:03:20 +00001639 if (mayLoad() && !isDereferenceableInvariantLoad(AA))
Evan Cheng399e1102008-03-13 00:44:09 +00001640 // Otherwise, this is a real load. If there is a store between the load and
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001641 // end of block, we can't move it.
1642 return !SawStore;
Dan Gohman7c59ed62008-09-24 00:06:15 +00001643
Evan Cheng399e1102008-03-13 00:44:09 +00001644 return true;
1645}
1646
Eli Friedman93f47e52017-03-09 23:33:36 +00001647bool MachineInstr::mayAlias(AliasAnalysis *AA, MachineInstr &Other,
1648 bool UseTBAA) {
1649 const MachineFunction *MF = getParent()->getParent();
1650 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1651
1652 // If neither instruction stores to memory, they can't alias in any
1653 // meaningful way, even if they read from the same address.
1654 if (!mayStore() && !Other.mayStore())
1655 return false;
1656
1657 // Let the target decide if memory accesses cannot possibly overlap.
1658 if (TII->areMemAccessesTriviallyDisjoint(*this, Other, AA))
1659 return false;
1660
1661 if (!AA)
1662 return true;
1663
1664 // FIXME: Need to handle multiple memory operands to support all targets.
1665 if (!hasOneMemOperand() || !Other.hasOneMemOperand())
1666 return true;
1667
1668 MachineMemOperand *MMOa = *memoperands_begin();
1669 MachineMemOperand *MMOb = *Other.memoperands_begin();
1670
1671 if (!MMOa->getValue() || !MMOb->getValue())
1672 return true;
1673
1674 // The following interface to AA is fashioned after DAGCombiner::isAlias
1675 // and operates with MachineMemOperand offset with some important
1676 // assumptions:
1677 // - LLVM fundamentally assumes flat address spaces.
1678 // - MachineOperand offset can *only* result from legalization and
1679 // cannot affect queries other than the trivial case of overlap
1680 // checking.
1681 // - These offsets never wrap and never step outside
1682 // of allocated objects.
1683 // - There should never be any negative offsets here.
1684 //
1685 // FIXME: Modify API to hide this math from "user"
1686 // FIXME: Even before we go to AA we can reason locally about some
1687 // memory objects. It can save compile time, and possibly catch some
1688 // corner cases not currently covered.
1689
Eugene Zelenko4e9736b2017-05-31 01:10:10 +00001690 assert((MMOa->getOffset() >= 0) && "Negative MachineMemOperand offset");
1691 assert((MMOb->getOffset() >= 0) && "Negative MachineMemOperand offset");
Eli Friedman93f47e52017-03-09 23:33:36 +00001692
1693 int64_t MinOffset = std::min(MMOa->getOffset(), MMOb->getOffset());
1694 int64_t Overlapa = MMOa->getSize() + MMOa->getOffset() - MinOffset;
1695 int64_t Overlapb = MMOb->getSize() + MMOb->getOffset() - MinOffset;
1696
1697 AliasResult AAResult =
1698 AA->alias(MemoryLocation(MMOa->getValue(), Overlapa,
1699 UseTBAA ? MMOa->getAAInfo() : AAMDNodes()),
1700 MemoryLocation(MMOb->getValue(), Overlapb,
1701 UseTBAA ? MMOb->getAAInfo() : AAMDNodes()));
1702
1703 return (AAResult != NoAlias);
1704}
1705
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001706/// hasOrderedMemoryRef - Return true if this instruction may have an ordered
1707/// or volatile memory reference, or if the information describing the memory
1708/// reference is not available. Return false if it is known to have no ordered
1709/// memory references.
1710bool MachineInstr::hasOrderedMemoryRef() const {
Dan Gohman7c59ed62008-09-24 00:06:15 +00001711 // An instruction known never to access memory won't have a volatile access.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001712 if (!mayStore() &&
1713 !mayLoad() &&
1714 !isCall() &&
Evan Cheng6eb516d2011-01-07 23:50:32 +00001715 !hasUnmodeledSideEffects())
Dan Gohman7c59ed62008-09-24 00:06:15 +00001716 return false;
1717
1718 // Otherwise, if the instruction has no memory reference information,
1719 // conservatively assume it wasn't preserved.
1720 if (memoperands_empty())
1721 return true;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001722
Justin Lebardede81e2016-07-13 22:35:19 +00001723 // Check if any of our memory operands are ordered.
Eugene Zelenko4e9736b2017-05-31 01:10:10 +00001724 return llvm::any_of(memoperands(), [](const MachineMemOperand *MMO) {
Justin Lebardede81e2016-07-13 22:35:19 +00001725 return !MMO->isUnordered();
1726 });
Dan Gohman7c59ed62008-09-24 00:06:15 +00001727}
1728
Justin Lebard98cf002016-09-10 01:03:20 +00001729/// isDereferenceableInvariantLoad - Return true if this instruction will never
1730/// trap and is loading from a location whose value is invariant across a run of
1731/// this function.
1732bool MachineInstr::isDereferenceableInvariantLoad(AliasAnalysis *AA) const {
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001733 // If the instruction doesn't load at all, it isn't an invariant load.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001734 if (!mayLoad())
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001735 return false;
1736
1737 // If the instruction has lost its memoperands, conservatively assume that
1738 // it may not be an invariant load.
1739 if (memoperands_empty())
1740 return false;
1741
Matthias Braun941a7052016-07-28 18:40:00 +00001742 const MachineFrameInfo &MFI = getParent()->getParent()->getFrameInfo();
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001743
Justin Lebardede81e2016-07-13 22:35:19 +00001744 for (MachineMemOperand *MMO : memoperands()) {
1745 if (MMO->isVolatile()) return false;
1746 if (MMO->isStore()) return false;
Justin Lebaradbf09e2016-09-11 01:38:58 +00001747 if (MMO->isInvariant() && MMO->isDereferenceable())
1748 continue;
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001749
1750 // A load from a constant PseudoSourceValue is invariant.
Justin Lebardede81e2016-07-13 22:35:19 +00001751 if (const PseudoSourceValue *PSV = MMO->getPseudoValue())
Matthias Braun941a7052016-07-28 18:40:00 +00001752 if (PSV->isConstant(&MFI))
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001753 continue;
1754
Justin Lebardede81e2016-07-13 22:35:19 +00001755 if (const Value *V = MMO->getValue()) {
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001756 // If we have an AliasAnalysis, ask it whether the memory is constant.
Chandler Carruthac80dc72015-06-17 07:18:54 +00001757 if (AA &&
1758 AA->pointsToConstantMemory(
Justin Lebardede81e2016-07-13 22:35:19 +00001759 MemoryLocation(V, MMO->getSize(), MMO->getAAInfo())))
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001760 continue;
1761 }
1762
1763 // Otherwise assume conservatively.
1764 return false;
1765 }
1766
1767 // Everything checks out.
1768 return true;
1769}
1770
Evan Cheng71453822009-12-03 02:31:43 +00001771/// isConstantValuePHI - If the specified instruction is a PHI that always
1772/// merges together the same virtual register, return the register, otherwise
1773/// return 0.
1774unsigned MachineInstr::isConstantValuePHI() const {
Chris Lattnerb06015a2010-02-09 19:54:29 +00001775 if (!isPHI())
Evan Cheng71453822009-12-03 02:31:43 +00001776 return 0;
Evan Cheng5c668a22009-12-07 23:10:34 +00001777 assert(getNumOperands() >= 3 &&
1778 "It's illegal to have a PHI without source operands");
Evan Cheng71453822009-12-03 02:31:43 +00001779
1780 unsigned Reg = getOperand(1).getReg();
1781 for (unsigned i = 3, e = getNumOperands(); i < e; i += 2)
1782 if (getOperand(i).getReg() != Reg)
1783 return 0;
1784 return Reg;
1785}
1786
Evan Cheng6eb516d2011-01-07 23:50:32 +00001787bool MachineInstr::hasUnmodeledSideEffects() const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00001788 if (hasProperty(MCID::UnmodeledSideEffects))
Evan Cheng6eb516d2011-01-07 23:50:32 +00001789 return true;
1790 if (isInlineAsm()) {
1791 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1792 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1793 return true;
1794 }
1795
1796 return false;
1797}
1798
Michael Kupersteinbc7f99a2015-08-12 10:14:58 +00001799bool MachineInstr::isLoadFoldBarrier() const {
1800 return mayStore() || isCall() || hasUnmodeledSideEffects();
1801}
1802
Evan Chengb083c472010-04-08 20:02:37 +00001803/// allDefsAreDead - Return true if all the defs of this instruction are dead.
1804///
1805bool MachineInstr::allDefsAreDead() const {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001806 for (const MachineOperand &MO : operands()) {
Evan Chengb083c472010-04-08 20:02:37 +00001807 if (!MO.isReg() || MO.isUse())
1808 continue;
1809 if (!MO.isDead())
1810 return false;
1811 }
1812 return true;
1813}
1814
Evan Cheng21eedfb2010-10-22 21:49:09 +00001815/// copyImplicitOps - Copy implicit register operands from specified
1816/// instruction to this instruction.
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001817void MachineInstr::copyImplicitOps(MachineFunction &MF,
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001818 const MachineInstr &MI) {
1819 for (unsigned i = MI.getDesc().getNumOperands(), e = MI.getNumOperands();
Evan Cheng21eedfb2010-10-22 21:49:09 +00001820 i != e; ++i) {
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001821 const MachineOperand &MO = MI.getOperand(i);
Lang Hames7c8189c2014-03-17 01:22:54 +00001822 if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask())
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001823 addOperand(MF, MO);
Evan Cheng21eedfb2010-10-22 21:49:09 +00001824 }
1825}
1826
Manman Ren19f49ac2012-09-11 22:23:19 +00001827#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Brauna4976c62017-01-29 18:20:42 +00001828LLVM_DUMP_METHOD void MachineInstr::dump() const {
Sebastian Pop77794842016-12-21 01:41:12 +00001829 dbgs() << " ";
Matthias Brauna4976c62017-01-29 18:20:42 +00001830 print(dbgs());
Mon P Wangdfcc1ff2008-10-10 01:43:55 +00001831}
Matthias Braun8c209aa2017-01-28 02:02:38 +00001832#endif
Mon P Wangdfcc1ff2008-10-10 01:43:55 +00001833
Ahmed Bougacha43192242017-02-23 19:17:31 +00001834void MachineInstr::print(raw_ostream &OS, bool SkipOpers, bool SkipDebugLoc,
Sebastian Pop77794842016-12-21 01:41:12 +00001835 const TargetInstrInfo *TII) const {
Duncan P. N. Exon Smithc0374522015-06-26 23:18:44 +00001836 const Module *M = nullptr;
1837 if (const MachineBasicBlock *MBB = getParent())
1838 if (const MachineFunction *MF = MBB->getParent())
1839 M = MF->getFunction()->getParent();
1840
1841 ModuleSlotTracker MST(M);
Ahmed Bougacha43192242017-02-23 19:17:31 +00001842 print(OS, MST, SkipOpers, SkipDebugLoc, TII);
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001843}
1844
1845void MachineInstr::print(raw_ostream &OS, ModuleSlotTracker &MST,
Ahmed Bougacha43192242017-02-23 19:17:31 +00001846 bool SkipOpers, bool SkipDebugLoc,
1847 const TargetInstrInfo *TII) const {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001848 // We can be a bit tidier if we know the MachineFunction.
Craig Topperc0196b12014-04-14 00:51:57 +00001849 const MachineFunction *MF = nullptr;
Eric Christopher1cdefae2015-02-27 00:11:34 +00001850 const TargetRegisterInfo *TRI = nullptr;
Craig Topperc0196b12014-04-14 00:51:57 +00001851 const MachineRegisterInfo *MRI = nullptr;
Tim Northover6b3bd612016-07-29 20:32:59 +00001852 const TargetIntrinsicInfo *IntrinsicInfo = nullptr;
1853
Dan Gohman2745d192009-11-09 19:38:45 +00001854 if (const MachineBasicBlock *MBB = getParent()) {
1855 MF = MBB->getParent();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001856 if (MF) {
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001857 MRI = &MF->getRegInfo();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001858 TRI = MF->getSubtarget().getRegisterInfo();
Sebastian Pop77794842016-12-21 01:41:12 +00001859 if (!TII)
1860 TII = MF->getSubtarget().getInstrInfo();
Tim Northover6b3bd612016-07-29 20:32:59 +00001861 IntrinsicInfo = MF->getTarget().getIntrinsicInfo();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001862 }
Dan Gohman2745d192009-11-09 19:38:45 +00001863 }
Dan Gohman34341e62009-10-31 20:19:03 +00001864
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001865 // Save a list of virtual registers.
1866 SmallVector<unsigned, 8> VirtRegs;
1867
Dan Gohman34341e62009-10-31 20:19:03 +00001868 // Print explicitly defined operands on the left of an assignment syntax.
Dan Gohman2745d192009-11-09 19:38:45 +00001869 unsigned StartOp = 0, e = getNumOperands();
Dan Gohman34341e62009-10-31 20:19:03 +00001870 for (; StartOp < e && getOperand(StartOp).isReg() &&
1871 getOperand(StartOp).isDef() &&
1872 !getOperand(StartOp).isImplicit();
1873 ++StartOp) {
1874 if (StartOp != 0) OS << ", ";
Tim Northover6b3bd612016-07-29 20:32:59 +00001875 getOperand(StartOp).print(OS, MST, TRI, IntrinsicInfo);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001876 unsigned Reg = getOperand(StartOp).getReg();
Quentin Colombet36ce1b02016-02-10 23:43:48 +00001877 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001878 VirtRegs.push_back(Reg);
Tim Northover0f140c72016-09-09 11:46:34 +00001879 LLT Ty = MRI ? MRI->getType(Reg) : LLT{};
1880 if (Ty.isValid())
1881 OS << '(' << Ty << ')';
Quentin Colombet36ce1b02016-02-10 23:43:48 +00001882 }
Chris Lattnerac6e9742002-10-30 01:55:38 +00001883 }
Tanya Lattner23dbc812004-06-25 00:13:11 +00001884
Dan Gohman34341e62009-10-31 20:19:03 +00001885 if (StartOp != 0)
1886 OS << " = ";
1887
1888 // Print the opcode name.
Eric Christopher1cdefae2015-02-27 00:11:34 +00001889 if (TII)
1890 OS << TII->getName(getOpcode());
Benjamin Kramerbf152d52012-02-10 13:18:44 +00001891 else
1892 OS << "UNKNOWN";
Misha Brukman835702a2005-04-21 22:36:52 +00001893
Andrew Trickb36388a2013-01-25 07:45:25 +00001894 if (SkipOpers)
1895 return;
1896
Dan Gohman34341e62009-10-31 20:19:03 +00001897 // Print the rest of the operands.
Dan Gohman2745d192009-11-09 19:38:45 +00001898 bool FirstOp = true;
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001899 unsigned AsmDescOp = ~0u;
1900 unsigned AsmOpCount = 0;
Evan Cheng6eb516d2011-01-07 23:50:32 +00001901
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +00001902 if (isInlineAsm() && e >= InlineAsm::MIOp_FirstOperand) {
Evan Cheng6eb516d2011-01-07 23:50:32 +00001903 // Print asm string.
1904 OS << " ";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001905 getOperand(InlineAsm::MIOp_AsmString).print(OS, MST, TRI);
Evan Cheng6eb516d2011-01-07 23:50:32 +00001906
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001907 // Print HasSideEffects, MayLoad, MayStore, IsAlignStack
Evan Cheng6eb516d2011-01-07 23:50:32 +00001908 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1909 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1910 OS << " [sideeffect]";
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001911 if (ExtraInfo & InlineAsm::Extra_MayLoad)
1912 OS << " [mayload]";
1913 if (ExtraInfo & InlineAsm::Extra_MayStore)
1914 OS << " [maystore]";
Wei Ding0526e7f2016-06-22 18:51:08 +00001915 if (ExtraInfo & InlineAsm::Extra_IsConvergent)
1916 OS << " [isconvergent]";
Evan Cheng6eb516d2011-01-07 23:50:32 +00001917 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
1918 OS << " [alignstack]";
Chad Rosiercbd2a192012-09-05 22:17:43 +00001919 if (getInlineAsmDialect() == InlineAsm::AD_ATT)
Chad Rosier994f4042012-09-05 21:00:58 +00001920 OS << " [attdialect]";
Chad Rosiercbd2a192012-09-05 22:17:43 +00001921 if (getInlineAsmDialect() == InlineAsm::AD_Intel)
Chad Rosier994f4042012-09-05 21:00:58 +00001922 OS << " [inteldialect]";
Evan Cheng6eb516d2011-01-07 23:50:32 +00001923
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001924 StartOp = AsmDescOp = InlineAsm::MIOp_FirstOperand;
Evan Cheng6eb516d2011-01-07 23:50:32 +00001925 FirstOp = false;
1926 }
1927
Chris Lattnerac6e9742002-10-30 01:55:38 +00001928 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohman2745d192009-11-09 19:38:45 +00001929 const MachineOperand &MO = getOperand(i);
1930
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001931 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001932 VirtRegs.push_back(MO.getReg());
1933
Dan Gohman2745d192009-11-09 19:38:45 +00001934 if (FirstOp) FirstOp = false; else OS << ",";
Chris Lattnerac6e9742002-10-30 01:55:38 +00001935 OS << " ";
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001936 if (i < getDesc().NumOperands) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001937 const MCOperandInfo &MCOI = getDesc().OpInfo[i];
1938 if (MCOI.isPredicate())
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001939 OS << "pred:";
Evan Cheng6cc775f2011-06-28 19:10:37 +00001940 if (MCOI.isOptionalDef())
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001941 OS << "opt:";
1942 }
Evan Chengd4d1a512010-04-28 20:03:13 +00001943 if (isDebugValue() && MO.isMetadata()) {
1944 // Pretty print DBG_VALUE instructions.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001945 auto *DIV = dyn_cast<DILocalVariable>(MO.getMetadata());
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +00001946 if (DIV && !DIV->getName().empty())
1947 OS << "!\"" << DIV->getName() << '\"';
Evan Chengd4d1a512010-04-28 20:03:13 +00001948 else
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001949 MO.print(OS, MST, TRI);
Matthias Brauna3743082017-01-09 21:38:10 +00001950 } else if (TRI && (isInsertSubreg() || isRegSequence() ||
1951 (isSubregToReg() && i == 3)) && MO.isImm()) {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001952 OS << TRI->getSubRegIndexName(MO.getImm());
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001953 } else if (i == AsmDescOp && MO.isImm()) {
1954 // Pretty print the inline asm operand descriptor.
1955 OS << '$' << AsmOpCount++;
1956 unsigned Flag = MO.getImm();
1957 switch (InlineAsm::getKind(Flag)) {
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001958 case InlineAsm::Kind_RegUse: OS << ":[reguse"; break;
1959 case InlineAsm::Kind_RegDef: OS << ":[regdef"; break;
1960 case InlineAsm::Kind_RegDefEarlyClobber: OS << ":[regdef-ec"; break;
1961 case InlineAsm::Kind_Clobber: OS << ":[clobber"; break;
1962 case InlineAsm::Kind_Imm: OS << ":[imm"; break;
1963 case InlineAsm::Kind_Mem: OS << ":[mem"; break;
1964 default: OS << ":[??" << InlineAsm::getKind(Flag); break;
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001965 }
1966
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001967 unsigned RCID = 0;
Simon Dardisd32a2d32016-07-18 13:17:31 +00001968 if (!InlineAsm::isImmKind(Flag) && !InlineAsm::isMemKind(Flag) &&
1969 InlineAsm::hasRegClassConstraint(Flag, RCID)) {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001970 if (TRI) {
1971 OS << ':' << TRI->getRegClassName(TRI->getRegClass(RCID));
Craig Toppercf0444b2014-11-17 05:50:14 +00001972 } else
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001973 OS << ":RC" << RCID;
Nick Lewycky84882252011-10-13 00:54:59 +00001974 }
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001975
Simon Dardisd32a2d32016-07-18 13:17:31 +00001976 if (InlineAsm::isMemKind(Flag)) {
1977 unsigned MCID = InlineAsm::getMemoryConstraintID(Flag);
1978 switch (MCID) {
1979 case InlineAsm::Constraint_es: OS << ":es"; break;
1980 case InlineAsm::Constraint_i: OS << ":i"; break;
1981 case InlineAsm::Constraint_m: OS << ":m"; break;
1982 case InlineAsm::Constraint_o: OS << ":o"; break;
1983 case InlineAsm::Constraint_v: OS << ":v"; break;
1984 case InlineAsm::Constraint_Q: OS << ":Q"; break;
1985 case InlineAsm::Constraint_R: OS << ":R"; break;
1986 case InlineAsm::Constraint_S: OS << ":S"; break;
1987 case InlineAsm::Constraint_T: OS << ":T"; break;
1988 case InlineAsm::Constraint_Um: OS << ":Um"; break;
1989 case InlineAsm::Constraint_Un: OS << ":Un"; break;
1990 case InlineAsm::Constraint_Uq: OS << ":Uq"; break;
1991 case InlineAsm::Constraint_Us: OS << ":Us"; break;
1992 case InlineAsm::Constraint_Ut: OS << ":Ut"; break;
1993 case InlineAsm::Constraint_Uv: OS << ":Uv"; break;
1994 case InlineAsm::Constraint_Uy: OS << ":Uy"; break;
1995 case InlineAsm::Constraint_X: OS << ":X"; break;
1996 case InlineAsm::Constraint_Z: OS << ":Z"; break;
1997 case InlineAsm::Constraint_ZC: OS << ":ZC"; break;
1998 case InlineAsm::Constraint_Zy: OS << ":Zy"; break;
1999 default: OS << ":?"; break;
2000 }
2001 }
2002
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00002003 unsigned TiedTo = 0;
2004 if (InlineAsm::isUseOperandTiedToDef(Flag, TiedTo))
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00002005 OS << " tiedto:$" << TiedTo;
2006
2007 OS << ']';
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00002008
2009 // Compute the index of the next operand descriptor.
2010 AsmDescOp += 1 + InlineAsm::getNumOperandRegisters(Flag);
Evan Chengd4d1a512010-04-28 20:03:13 +00002011 } else
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00002012 MO.print(OS, MST, TRI);
Dan Gohman2745d192009-11-09 19:38:45 +00002013 }
2014
Dan Gohman34341e62009-10-31 20:19:03 +00002015 bool HaveSemi = false;
Michael Kuperstein098cd9f2015-09-16 11:18:25 +00002016 const unsigned PrintableFlags = FrameSetup | FrameDestroy;
Jakob Stoklund Olesen6922e9c2013-01-09 18:35:09 +00002017 if (Flags & PrintableFlags) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00002018 if (!HaveSemi) {
2019 OS << ";";
2020 HaveSemi = true;
2021 }
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00002022 OS << " flags: ";
2023
2024 if (Flags & FrameSetup)
2025 OS << "FrameSetup";
Michael Kuperstein098cd9f2015-09-16 11:18:25 +00002026
2027 if (Flags & FrameDestroy)
2028 OS << "FrameDestroy";
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00002029 }
2030
Dan Gohman3b460302008-07-07 23:14:23 +00002031 if (!memoperands_empty()) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00002032 if (!HaveSemi) {
2033 OS << ";";
2034 HaveSemi = true;
2035 }
Dan Gohman34341e62009-10-31 20:19:03 +00002036
2037 OS << " mem:";
Dan Gohman48b185d2009-09-25 20:36:54 +00002038 for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
2039 i != e; ++i) {
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00002040 (*i)->print(OS, MST);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002041 if (std::next(i) != e)
Dan Gohmanc0353bf2009-09-23 01:33:16 +00002042 OS << " ";
Dan Gohman2d489b52008-02-06 22:27:42 +00002043 }
2044 }
2045
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00002046 // Print the regclass of any virtual registers encountered.
2047 if (MRI && !VirtRegs.empty()) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00002048 if (!HaveSemi) {
2049 OS << ";";
2050 HaveSemi = true;
2051 }
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00002052 for (unsigned i = 0; i != VirtRegs.size(); ++i) {
Quentin Colombet03c41962016-04-07 23:18:11 +00002053 const RegClassOrRegBank &RC = MRI->getRegClassOrRegBank(VirtRegs[i]);
Quentin Colombete1494c32016-02-11 00:19:17 +00002054 if (!RC)
2055 continue;
Quentin Colombet03c41962016-04-07 23:18:11 +00002056 // Generic virtual registers do not have register classes.
2057 if (RC.is<const RegisterBank *>())
2058 OS << " " << RC.get<const RegisterBank *>()->getName();
2059 else
2060 OS << " "
2061 << TRI->getRegClassName(RC.get<const TargetRegisterClass *>());
2062 OS << ':' << PrintReg(VirtRegs[i]);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00002063 for (unsigned j = i+1; j != VirtRegs.size();) {
Quentin Colombet03c41962016-04-07 23:18:11 +00002064 if (MRI->getRegClassOrRegBank(VirtRegs[j]) != RC) {
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00002065 ++j;
2066 continue;
2067 }
2068 if (VirtRegs[i] != VirtRegs[j])
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +00002069 OS << "," << PrintReg(VirtRegs[j]);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00002070 VirtRegs.erase(VirtRegs.begin()+j);
2071 }
2072 }
2073 }
2074
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00002075 // Print debug location information.
Duncan P. N. Exon Smithc5bd3e02015-04-03 16:23:04 +00002076 if (isDebugValue() && getOperand(e - 2).isMetadata()) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00002077 if (!HaveSemi)
2078 OS << ";";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002079 auto *DV = cast<DILocalVariable>(getOperand(e - 2).getMetadata());
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +00002080 OS << " line no:" << DV->getLine();
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00002081 if (auto *InlinedAt = debugLoc->getInlinedAt()) {
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +00002082 DebugLoc InlinedAtDL(InlinedAt);
2083 if (InlinedAtDL && MF) {
Devang Pateld61b1d52011-08-04 20:44:26 +00002084 OS << " inlined @[ ";
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002085 InlinedAtDL.print(OS);
Devang Pateld61b1d52011-08-04 20:44:26 +00002086 OS << " ]";
2087 }
2088 }
Adrian Prantl87b7eb92014-10-01 18:55:02 +00002089 if (isIndirectDebugValue())
2090 OS << " indirect";
Ahmed Bougacha97119d42017-02-23 21:05:29 +00002091 } else if (SkipDebugLoc) {
2092 return;
2093 } else if (debugLoc && MF) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00002094 if (!HaveSemi)
2095 OS << ";";
Dan Gohman2e3f1872009-11-23 21:29:08 +00002096 OS << " dbg:";
Eric Christopherb9f00092015-02-26 23:32:17 +00002097 debugLoc.print(OS);
Bill Wendling1a0a3d02009-02-19 21:44:55 +00002098 }
2099
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00002100 OS << '\n';
Chris Lattner214808f2002-10-30 00:48:05 +00002101}
2102
Owen Anderson2a8a4852008-01-24 01:10:07 +00002103bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00002104 const TargetRegisterInfo *RegInfo,
Owen Anderson2a8a4852008-01-24 01:10:07 +00002105 bool AddIfNotFound) {
Evan Cheng6c177732008-04-16 09:41:59 +00002106 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00002107 bool hasAliases = isPhysReg &&
2108 MCRegAliasIterator(IncomingReg, RegInfo, false).isValid();
Dan Gohmanc7367b42008-09-03 15:56:16 +00002109 bool Found = false;
Evan Cheng6c177732008-04-16 09:41:59 +00002110 SmallVector<unsigned,4> DeadOps;
Bill Wendling7921ad02008-03-03 22:14:33 +00002111 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2112 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesenf465f062009-08-04 20:09:25 +00002113 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng6c177732008-04-16 09:41:59 +00002114 continue;
Mandeep Singh Grange5a2f112016-05-10 17:57:27 +00002115
2116 // DEBUG_VALUE nodes do not contribute to code generation and should
2117 // always be ignored. Failure to do so may result in trying to modify
2118 // KILL flags on DEBUG_VALUE nodes.
2119 if (MO.isDebug())
2120 continue;
2121
Evan Cheng6c177732008-04-16 09:41:59 +00002122 unsigned Reg = MO.getReg();
2123 if (!Reg)
2124 continue;
Bill Wendling7921ad02008-03-03 22:14:33 +00002125
Evan Cheng6c177732008-04-16 09:41:59 +00002126 if (Reg == IncomingReg) {
Dan Gohmanc7367b42008-09-03 15:56:16 +00002127 if (!Found) {
2128 if (MO.isKill())
2129 // The register is already marked kill.
2130 return true;
Jakob Stoklund Olesenc59cd9b2009-08-02 19:13:03 +00002131 if (isPhysReg && isRegTiedToDefOperand(i))
2132 // Two-address uses of physregs must not be marked kill.
2133 return true;
Dan Gohmanc7367b42008-09-03 15:56:16 +00002134 MO.setIsKill();
2135 Found = true;
2136 }
2137 } else if (hasAliases && MO.isKill() &&
2138 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng6c177732008-04-16 09:41:59 +00002139 // A super-register kill already exists.
2140 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohmanb2612922008-07-03 01:18:51 +00002141 return true;
2142 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng6c177732008-04-16 09:41:59 +00002143 DeadOps.push_back(i);
Bill Wendling7921ad02008-03-03 22:14:33 +00002144 }
2145 }
2146
Evan Cheng6c177732008-04-16 09:41:59 +00002147 // Trim unneeded kill operands.
2148 while (!DeadOps.empty()) {
2149 unsigned OpIdx = DeadOps.back();
2150 if (getOperand(OpIdx).isImplicit())
2151 RemoveOperand(OpIdx);
2152 else
2153 getOperand(OpIdx).setIsKill(false);
2154 DeadOps.pop_back();
2155 }
2156
Bill Wendling7921ad02008-03-03 22:14:33 +00002157 // If not found, this means an alias of one of the operands is killed. Add a
Owen Anderson2a8a4852008-01-24 01:10:07 +00002158 // new implicit operand if required.
Dan Gohmanc7367b42008-09-03 15:56:16 +00002159 if (!Found && AddIfNotFound) {
Bill Wendling7921ad02008-03-03 22:14:33 +00002160 addOperand(MachineOperand::CreateReg(IncomingReg,
2161 false /*IsDef*/,
2162 true /*IsImp*/,
2163 true /*IsKill*/));
Owen Anderson2a8a4852008-01-24 01:10:07 +00002164 return true;
2165 }
Dan Gohmanc7367b42008-09-03 15:56:16 +00002166 return Found;
Owen Anderson2a8a4852008-01-24 01:10:07 +00002167}
2168
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00002169void MachineInstr::clearRegisterKills(unsigned Reg,
2170 const TargetRegisterInfo *RegInfo) {
2171 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topperc0196b12014-04-14 00:51:57 +00002172 RegInfo = nullptr;
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002173 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00002174 if (!MO.isReg() || !MO.isUse() || !MO.isKill())
2175 continue;
2176 unsigned OpReg = MO.getReg();
Matthias Braunaca625a2016-02-24 19:21:48 +00002177 if ((RegInfo && RegInfo->regsOverlap(Reg, OpReg)) || Reg == OpReg)
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00002178 MO.setIsKill(false);
2179 }
2180}
2181
Matthias Braun1965bfa2013-10-10 21:28:38 +00002182bool MachineInstr::addRegisterDead(unsigned Reg,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00002183 const TargetRegisterInfo *RegInfo,
Owen Anderson2a8a4852008-01-24 01:10:07 +00002184 bool AddIfNotFound) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00002185 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(Reg);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00002186 bool hasAliases = isPhysReg &&
Matthias Braun1965bfa2013-10-10 21:28:38 +00002187 MCRegAliasIterator(Reg, RegInfo, false).isValid();
Dan Gohmanc7367b42008-09-03 15:56:16 +00002188 bool Found = false;
Evan Cheng6c177732008-04-16 09:41:59 +00002189 SmallVector<unsigned,4> DeadOps;
Owen Anderson2a8a4852008-01-24 01:10:07 +00002190 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2191 MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00002192 if (!MO.isReg() || !MO.isDef())
Evan Cheng6c177732008-04-16 09:41:59 +00002193 continue;
Matthias Braun1965bfa2013-10-10 21:28:38 +00002194 unsigned MOReg = MO.getReg();
2195 if (!MOReg)
Dan Gohmanc7367b42008-09-03 15:56:16 +00002196 continue;
2197
Matthias Braun1965bfa2013-10-10 21:28:38 +00002198 if (MOReg == Reg) {
Jakob Stoklund Olesen76ad3de2011-04-05 16:53:50 +00002199 MO.setIsDead();
2200 Found = true;
Dan Gohmanc7367b42008-09-03 15:56:16 +00002201 } else if (hasAliases && MO.isDead() &&
Matthias Braun1965bfa2013-10-10 21:28:38 +00002202 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
Evan Cheng6c177732008-04-16 09:41:59 +00002203 // There exists a super-register that's marked dead.
Matthias Braun1965bfa2013-10-10 21:28:38 +00002204 if (RegInfo->isSuperRegister(Reg, MOReg))
Dan Gohmanb2612922008-07-03 01:18:51 +00002205 return true;
Matthias Braun1965bfa2013-10-10 21:28:38 +00002206 if (RegInfo->isSubRegister(Reg, MOReg))
Evan Cheng6c177732008-04-16 09:41:59 +00002207 DeadOps.push_back(i);
Owen Anderson2a8a4852008-01-24 01:10:07 +00002208 }
2209 }
2210
Evan Cheng6c177732008-04-16 09:41:59 +00002211 // Trim unneeded dead operands.
2212 while (!DeadOps.empty()) {
2213 unsigned OpIdx = DeadOps.back();
2214 if (getOperand(OpIdx).isImplicit())
2215 RemoveOperand(OpIdx);
2216 else
2217 getOperand(OpIdx).setIsDead(false);
2218 DeadOps.pop_back();
2219 }
2220
Dan Gohmanc7367b42008-09-03 15:56:16 +00002221 // If not found, this means an alias of one of the operands is dead. Add a
2222 // new implicit operand if required.
Chris Lattnerfd682802009-06-24 17:54:48 +00002223 if (Found || !AddIfNotFound)
2224 return Found;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00002225
Matthias Braun1965bfa2013-10-10 21:28:38 +00002226 addOperand(MachineOperand::CreateReg(Reg,
Chris Lattnerfd682802009-06-24 17:54:48 +00002227 true /*IsDef*/,
2228 true /*IsImp*/,
2229 false /*IsKill*/,
2230 true /*IsDead*/));
2231 return true;
Owen Anderson2a8a4852008-01-24 01:10:07 +00002232}
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00002233
Matthias Braun26e7ea62015-02-04 19:35:16 +00002234void MachineInstr::clearRegisterDeads(unsigned Reg) {
2235 for (MachineOperand &MO : operands()) {
2236 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg)
2237 continue;
2238 MO.setIsDead(false);
2239 }
2240}
2241
Matthias Braun2c98d0f2015-11-11 00:41:58 +00002242void MachineInstr::setRegisterDefReadUndef(unsigned Reg, bool IsUndef) {
Matthias Braunc1988f32015-01-21 22:55:13 +00002243 for (MachineOperand &MO : operands()) {
2244 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg || MO.getSubReg() == 0)
2245 continue;
Matthias Braun2c98d0f2015-11-11 00:41:58 +00002246 MO.setIsUndef(IsUndef);
Matthias Braunc1988f32015-01-21 22:55:13 +00002247 }
2248}
2249
Matthias Braun1965bfa2013-10-10 21:28:38 +00002250void MachineInstr::addRegisterDefined(unsigned Reg,
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00002251 const TargetRegisterInfo *RegInfo) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00002252 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
2253 MachineOperand *MO = findRegisterDefOperand(Reg, false, RegInfo);
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00002254 if (MO)
2255 return;
2256 } else {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002257 for (const MachineOperand &MO : operands()) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00002258 if (MO.isReg() && MO.getReg() == Reg && MO.isDef() &&
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00002259 MO.getSubReg() == 0)
2260 return;
2261 }
2262 }
Matthias Braun1965bfa2013-10-10 21:28:38 +00002263 addOperand(MachineOperand::CreateReg(Reg,
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00002264 true /*IsDef*/,
2265 true /*IsImp*/));
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00002266}
Evan Cheng59d27fe2010-03-03 23:37:30 +00002267
Jakob Stoklund Olesen4290be42012-02-03 20:43:39 +00002268void MachineInstr::setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs,
Dan Gohman86936502010-06-18 23:28:01 +00002269 const TargetRegisterInfo &TRI) {
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00002270 bool HasRegMask = false;
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002271 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00002272 if (MO.isRegMask()) {
2273 HasRegMask = true;
2274 continue;
2275 }
Dan Gohman86936502010-06-18 23:28:01 +00002276 if (!MO.isReg() || !MO.isDef()) continue;
2277 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenf6507322012-02-03 20:43:35 +00002278 if (!TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Dan Gohman86936502010-06-18 23:28:01 +00002279 // If there are no uses, including partial uses, the def is dead.
Eugene Zelenko4e9736b2017-05-31 01:10:10 +00002280 if (llvm::none_of(UsedRegs,
2281 [&](unsigned Use) { return TRI.regsOverlap(Use, Reg); }))
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002282 MO.setIsDead();
Dan Gohman86936502010-06-18 23:28:01 +00002283 }
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00002284
2285 // This is a call with a register mask operand.
2286 // Mask clobbers are always dead, so add defs for the non-dead defines.
2287 if (HasRegMask)
2288 for (ArrayRef<unsigned>::iterator I = UsedRegs.begin(), E = UsedRegs.end();
2289 I != E; ++I)
2290 addRegisterDefined(*I, &TRI);
Dan Gohman86936502010-06-18 23:28:01 +00002291}
2292
Evan Cheng59d27fe2010-03-03 23:37:30 +00002293unsigned
2294MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) {
Chandler Carruth962152c2012-03-07 09:39:46 +00002295 // Build up a buffer of hash code components.
Chandler Carruth962152c2012-03-07 09:39:46 +00002296 SmallVector<size_t, 8> HashComponents;
2297 HashComponents.reserve(MI->getNumOperands() + 1);
2298 HashComponents.push_back(MI->getOpcode());
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002299 for (const MachineOperand &MO : MI->operands()) {
Chandler Carruth264854f2012-07-05 11:06:22 +00002300 if (MO.isReg() && MO.isDef() &&
2301 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
2302 continue; // Skip virtual register defs.
2303
2304 HashComponents.push_back(hash_value(MO));
Evan Cheng59d27fe2010-03-03 23:37:30 +00002305 }
Chandler Carruth962152c2012-03-07 09:39:46 +00002306 return hash_combine_range(HashComponents.begin(), HashComponents.end());
Evan Cheng59d27fe2010-03-03 23:37:30 +00002307}
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00002308
2309void MachineInstr::emitError(StringRef Msg) const {
2310 // Find the source location cookie.
2311 unsigned LocCookie = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00002312 const MDNode *LocMD = nullptr;
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00002313 for (unsigned i = getNumOperands(); i != 0; --i) {
2314 if (getOperand(i-1).isMetadata() &&
2315 (LocMD = getOperand(i-1).getMetadata()) &&
2316 LocMD->getNumOperands() != 0) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002317 if (const ConstantInt *CI =
2318 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00002319 LocCookie = CI->getZExtValue();
2320 break;
2321 }
2322 }
2323 }
2324
2325 if (const MachineBasicBlock *MBB = getParent())
2326 if (const MachineFunction *MF = MBB->getParent())
2327 return MF->getMMI().getModule()->getContext().emitError(LocCookie, Msg);
2328 report_fatal_error(Msg);
2329}
Reid Kleckner28865802016-04-14 18:29:59 +00002330
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002331MachineInstrBuilder llvm::BuildMI(MachineFunction &MF, const DebugLoc &DL,
Reid Kleckner28865802016-04-14 18:29:59 +00002332 const MCInstrDesc &MCID, bool IsIndirect,
2333 unsigned Reg, unsigned Offset,
2334 const MDNode *Variable, const MDNode *Expr) {
2335 assert(isa<DILocalVariable>(Variable) && "not a variable");
2336 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
2337 assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
2338 "Expected inlined-at fields to agree");
2339 if (IsIndirect)
2340 return BuildMI(MF, DL, MCID)
2341 .addReg(Reg, RegState::Debug)
2342 .addImm(Offset)
2343 .addMetadata(Variable)
2344 .addMetadata(Expr);
2345 else {
2346 assert(Offset == 0 && "A direct address cannot have an offset.");
2347 return BuildMI(MF, DL, MCID)
2348 .addReg(Reg, RegState::Debug)
2349 .addReg(0U, RegState::Debug)
2350 .addMetadata(Variable)
2351 .addMetadata(Expr);
2352 }
2353}
2354
2355MachineInstrBuilder llvm::BuildMI(MachineBasicBlock &BB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002356 MachineBasicBlock::iterator I,
2357 const DebugLoc &DL, const MCInstrDesc &MCID,
2358 bool IsIndirect, unsigned Reg,
2359 unsigned Offset, const MDNode *Variable,
2360 const MDNode *Expr) {
Reid Kleckner28865802016-04-14 18:29:59 +00002361 assert(isa<DILocalVariable>(Variable) && "not a variable");
2362 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
2363 MachineFunction &MF = *BB.getParent();
2364 MachineInstr *MI =
2365 BuildMI(MF, DL, MCID, IsIndirect, Reg, Offset, Variable, Expr);
2366 BB.insert(I, MI);
2367 return MachineInstrBuilder(MF, MI);
2368}
Adrian Prantl6825fb62017-04-18 01:21:53 +00002369
2370MachineInstr *llvm::buildDbgValueForSpill(MachineBasicBlock &BB,
2371 MachineBasicBlock::iterator I,
2372 const MachineInstr &Orig,
2373 int FrameIndex) {
2374 const MDNode *Var = Orig.getDebugVariable();
Adrian Prantl06d60962017-04-28 18:30:36 +00002375 const auto *Expr = cast_or_null<DIExpression>(Orig.getDebugExpression());
Adrian Prantl6825fb62017-04-18 01:21:53 +00002376 bool IsIndirect = Orig.isIndirectDebugValue();
2377 uint64_t Offset = IsIndirect ? Orig.getOperand(1).getImm() : 0;
2378 DebugLoc DL = Orig.getDebugLoc();
2379 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
2380 "Expected inlined-at fields to agree");
2381 // If the DBG_VALUE already was a memory location, add an extra
2382 // DW_OP_deref. Otherwise just turning this from a register into a
2383 // memory/indirect location is sufficient.
Adrian Prantl06d60962017-04-28 18:30:36 +00002384 if (IsIndirect)
2385 Expr = DIExpression::prepend(Expr, DIExpression::WithDeref);
Adrian Prantl6825fb62017-04-18 01:21:53 +00002386 return BuildMI(BB, I, DL, Orig.getDesc())
2387 .addFrameIndex(FrameIndex)
2388 .addImm(Offset)
2389 .addMetadata(Var)
2390 .addMetadata(Expr);
2391}