blob: 968ec2c68e8c9df50865c7f455d401c22efdd26d [file] [log] [blame]
Chris Lattner0cb9dd72008-01-01 20:36:19 +00001//===-- lib/CodeGen/MachineInstr.cpp --------------------------------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Brian Gaekee8f7c2f2004-02-13 04:39:32 +00009//
10// Methods common to all machine instructions.
11//
Chris Lattner959a5fb2002-08-09 20:08:06 +000012//===----------------------------------------------------------------------===//
Vikram S. Adveab9e5572001-07-21 12:41:50 +000013
Chris Lattner23fcc082001-09-07 17:18:30 +000014#include "llvm/CodeGen/MachineInstr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/FoldingSet.h"
16#include "llvm/ADT/Hashing.h"
17#include "llvm/Analysis/AliasAnalysis.h"
Evan Chenge9c46c22010-03-03 01:44:33 +000018#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner63f41ab2004-02-19 16:17:08 +000019#include "llvm/CodeGen/MachineFunction.h"
Dan Gohman48b185d2009-09-25 20:36:54 +000020#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner961e7422008-01-01 01:12:31 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman2d489b52008-02-06 22:27:42 +000023#include "llvm/CodeGen/PseudoSourceValue.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000025#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Function.h"
27#include "llvm/IR/InlineAsm.h"
28#include "llvm/IR/LLVMContext.h"
29#include "llvm/IR/Metadata.h"
30#include "llvm/IR/Module.h"
31#include "llvm/IR/Type.h"
32#include "llvm/IR/Value.h"
Evan Cheng6cc775f2011-06-28 19:10:37 +000033#include "llvm/MC/MCInstrDesc.h"
Chris Lattner6c604e32010-03-13 08:14:18 +000034#include "llvm/MC/MCSymbol.h"
David Greene29388d62010-01-04 23:48:20 +000035#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000036#include "llvm/Support/ErrorHandling.h"
Dan Gohmanaedb4a62008-07-07 20:32:02 +000037#include "llvm/Support/MathExtras.h"
Chris Lattnera078d832008-08-24 20:37:32 +000038#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000039#include "llvm/Target/TargetInstrInfo.h"
40#include "llvm/Target/TargetMachine.h"
41#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000042#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner43df6c22004-02-23 18:38:20 +000043using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000044
Chris Lattner60055892007-12-30 21:56:09 +000045//===----------------------------------------------------------------------===//
46// MachineOperand Implementation
47//===----------------------------------------------------------------------===//
48
Chris Lattner961e7422008-01-01 01:12:31 +000049void MachineOperand::setReg(unsigned Reg) {
50 if (getReg() == Reg) return; // No change.
Jim Grosbachdee9e8a2011-08-24 16:44:17 +000051
Chris Lattner961e7422008-01-01 01:12:31 +000052 // Otherwise, we have to change the register. If this operand is embedded
53 // into a machine function, we need to update the old and new register's
54 // use/def lists.
55 if (MachineInstr *MI = getParent())
56 if (MachineBasicBlock *MBB = MI->getParent())
57 if (MachineFunction *MF = MBB->getParent()) {
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +000058 MachineRegisterInfo &MRI = MF->getRegInfo();
59 MRI.removeRegOperandFromUseList(this);
Jakob Stoklund Olesena4941692010-10-19 20:56:32 +000060 SmallContents.RegNo = Reg;
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +000061 MRI.addRegOperandToUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +000062 return;
63 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +000064
Chris Lattner961e7422008-01-01 01:12:31 +000065 // Otherwise, just change the register, no problem. :)
Jakob Stoklund Olesena4941692010-10-19 20:56:32 +000066 SmallContents.RegNo = Reg;
Chris Lattner961e7422008-01-01 01:12:31 +000067}
68
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +000069void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx,
70 const TargetRegisterInfo &TRI) {
71 assert(TargetRegisterInfo::isVirtualRegister(Reg));
72 if (SubIdx && getSubReg())
73 SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg());
74 setReg(Reg);
Jakob Stoklund Olesen7b0ac862010-06-01 22:39:25 +000075 if (SubIdx)
76 setSubReg(SubIdx);
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +000077}
78
79void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) {
80 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
81 if (getSubReg()) {
82 Reg = TRI.getSubReg(Reg, getSubReg());
Jakob Stoklund Olesen89bd2ae2011-05-08 19:21:08 +000083 // Note that getSubReg() may return 0 if the sub-register doesn't exist.
84 // That won't happen in legal code.
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +000085 setSubReg(0);
86 }
87 setReg(Reg);
88}
89
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +000090/// Change a def to a use, or a use to a def.
91void MachineOperand::setIsDef(bool Val) {
92 assert(isReg() && "Wrong MachineOperand accessor");
93 assert((!Val || !isDebug()) && "Marking a debug operation as def");
94 if (IsDef == Val)
95 return;
96 // MRI may keep uses and defs in different list positions.
97 if (MachineInstr *MI = getParent())
98 if (MachineBasicBlock *MBB = MI->getParent())
99 if (MachineFunction *MF = MBB->getParent()) {
100 MachineRegisterInfo &MRI = MF->getRegInfo();
101 MRI.removeRegOperandFromUseList(this);
102 IsDef = Val;
103 MRI.addRegOperandToUseList(this);
104 return;
105 }
106 IsDef = Val;
107}
108
Matt Arsenault93ffe582014-09-28 19:24:59 +0000109// If this operand is currently a register operand, and if this is in a
110// function, deregister the operand from the register's use/def list.
111void MachineOperand::removeRegFromUses() {
112 if (!isReg() || !isOnRegUseList())
113 return;
114
115 if (MachineInstr *MI = getParent()) {
116 if (MachineBasicBlock *MBB = MI->getParent()) {
117 if (MachineFunction *MF = MBB->getParent())
118 MF->getRegInfo().removeRegOperandFromUseList(this);
119 }
120 }
121}
122
Chris Lattner961e7422008-01-01 01:12:31 +0000123/// ChangeToImmediate - Replace this operand with a new immediate operand of
124/// the specified value. If an operand is known to be an immediate already,
125/// the setImm method should be used.
126void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000127 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
Matt Arsenault93ffe582014-09-28 19:24:59 +0000128
129 removeRegFromUses();
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000130
Chris Lattner961e7422008-01-01 01:12:31 +0000131 OpKind = MO_Immediate;
132 Contents.ImmVal = ImmVal;
133}
134
Matt Arsenault93ffe582014-09-28 19:24:59 +0000135void MachineOperand::ChangeToFPImmediate(const ConstantFP *FPImm) {
136 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
137
138 removeRegFromUses();
139
140 OpKind = MO_FPImmediate;
141 Contents.CFP = FPImm;
142}
143
Chris Lattner961e7422008-01-01 01:12:31 +0000144/// ChangeToRegister - Replace this operand with a new register operand of
145/// the specified value. If an operand is known to be an register already,
146/// the setReg method should be used.
147void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
Dale Johannesend40d42c2010-02-10 00:41:49 +0000148 bool isKill, bool isDead, bool isUndef,
149 bool isDebug) {
Craig Topperc0196b12014-04-14 00:51:57 +0000150 MachineRegisterInfo *RegInfo = nullptr;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000151 if (MachineInstr *MI = getParent())
152 if (MachineBasicBlock *MBB = MI->getParent())
153 if (MachineFunction *MF = MBB->getParent())
154 RegInfo = &MF->getRegInfo();
155 // If this operand is already a register operand, remove it from the
Chris Lattner961e7422008-01-01 01:12:31 +0000156 // register's use/def lists.
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000157 bool WasReg = isReg();
158 if (RegInfo && WasReg)
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000159 RegInfo->removeRegOperandFromUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +0000160
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000161 // Change this to a register and set the reg#.
162 OpKind = MO_Register;
163 SmallContents.RegNo = Reg;
Jakob Stoklund Olesena1b246d2013-01-07 23:21:44 +0000164 SubReg_TargetFlags = 0;
Chris Lattner961e7422008-01-01 01:12:31 +0000165 IsDef = isDef;
166 IsImp = isImp;
167 IsKill = isKill;
168 IsDead = isDead;
Evan Cheng0dc101b2009-06-30 08:49:04 +0000169 IsUndef = isUndef;
Jakob Stoklund Olesenb0d91ab2011-12-07 00:22:07 +0000170 IsInternalRead = false;
Dale Johannesenc0d712d2008-09-14 01:44:36 +0000171 IsEarlyClobber = false;
Dale Johannesend40d42c2010-02-10 00:41:49 +0000172 IsDebug = isDebug;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000173 // Ensure isOnRegUseList() returns false.
Craig Topperc0196b12014-04-14 00:51:57 +0000174 Contents.Reg.Prev = nullptr;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000175 // Preserve the tie when the operand was already a register.
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000176 if (!WasReg)
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000177 TiedTo = 0;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000178
179 // If this operand is embedded in a function, add the operand to the
180 // register's use/def list.
181 if (RegInfo)
182 RegInfo->addRegOperandToUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +0000183}
184
Chris Lattner60055892007-12-30 21:56:09 +0000185/// isIdenticalTo - Return true if this operand is identical to the specified
Chandler Carruth264854f2012-07-05 11:06:22 +0000186/// operand. Note that this should stay in sync with the hash_value overload
187/// below.
Chris Lattner60055892007-12-30 21:56:09 +0000188bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
Chris Lattnerfd682802009-06-24 17:54:48 +0000189 if (getType() != Other.getType() ||
190 getTargetFlags() != Other.getTargetFlags())
191 return false;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000192
Chris Lattner60055892007-12-30 21:56:09 +0000193 switch (getType()) {
Chris Lattner60055892007-12-30 21:56:09 +0000194 case MachineOperand::MO_Register:
195 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
196 getSubReg() == Other.getSubReg();
197 case MachineOperand::MO_Immediate:
198 return getImm() == Other.getImm();
Cameron Zwarich7da0f9a2011-07-01 23:45:21 +0000199 case MachineOperand::MO_CImmediate:
200 return getCImm() == Other.getCImm();
Nate Begeman26b76b62008-02-14 07:39:30 +0000201 case MachineOperand::MO_FPImmediate:
202 return getFPImm() == Other.getFPImm();
Chris Lattner60055892007-12-30 21:56:09 +0000203 case MachineOperand::MO_MachineBasicBlock:
204 return getMBB() == Other.getMBB();
205 case MachineOperand::MO_FrameIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000206 return getIndex() == Other.getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000207 case MachineOperand::MO_ConstantPoolIndex:
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000208 case MachineOperand::MO_TargetIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000209 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattner60055892007-12-30 21:56:09 +0000210 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000211 return getIndex() == Other.getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000212 case MachineOperand::MO_GlobalAddress:
213 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
214 case MachineOperand::MO_ExternalSymbol:
215 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
216 getOffset() == Other.getOffset();
Dan Gohman6c938802009-10-30 01:27:03 +0000217 case MachineOperand::MO_BlockAddress:
Michael Liaoabb87d42012-09-12 21:43:09 +0000218 return getBlockAddress() == Other.getBlockAddress() &&
219 getOffset() == Other.getOffset();
Juergen Ributzkae8294752013-12-14 06:53:06 +0000220 case MachineOperand::MO_RegisterMask:
221 case MachineOperand::MO_RegisterLiveOut:
Jakob Stoklund Olesen374ed322012-01-16 19:22:00 +0000222 return getRegMask() == Other.getRegMask();
Chris Lattner6c604e32010-03-13 08:14:18 +0000223 case MachineOperand::MO_MCSymbol:
224 return getMCSymbol() == Other.getMCSymbol();
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000225 case MachineOperand::MO_CFIIndex:
226 return getCFIIndex() == Other.getCFIIndex();
Chris Lattnerf839ee02010-04-07 18:03:19 +0000227 case MachineOperand::MO_Metadata:
228 return getMetadata() == Other.getMetadata();
Chris Lattner60055892007-12-30 21:56:09 +0000229 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000230 llvm_unreachable("Invalid machine operand type");
Chris Lattner60055892007-12-30 21:56:09 +0000231}
232
Chandler Carruth264854f2012-07-05 11:06:22 +0000233// Note: this must stay exactly in sync with isIdenticalTo above.
234hash_code llvm::hash_value(const MachineOperand &MO) {
235 switch (MO.getType()) {
236 case MachineOperand::MO_Register:
Jakob Stoklund Olesendba99d02012-08-28 18:05:48 +0000237 // Register operands don't have target flags.
238 return hash_combine(MO.getType(), MO.getReg(), MO.getSubReg(), MO.isDef());
Chandler Carruth264854f2012-07-05 11:06:22 +0000239 case MachineOperand::MO_Immediate:
240 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm());
241 case MachineOperand::MO_CImmediate:
242 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm());
243 case MachineOperand::MO_FPImmediate:
244 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm());
245 case MachineOperand::MO_MachineBasicBlock:
246 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB());
247 case MachineOperand::MO_FrameIndex:
248 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
249 case MachineOperand::MO_ConstantPoolIndex:
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000250 case MachineOperand::MO_TargetIndex:
Chandler Carruth264854f2012-07-05 11:06:22 +0000251 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(),
252 MO.getOffset());
253 case MachineOperand::MO_JumpTableIndex:
254 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
255 case MachineOperand::MO_ExternalSymbol:
256 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(),
257 MO.getSymbolName());
258 case MachineOperand::MO_GlobalAddress:
259 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(),
260 MO.getOffset());
261 case MachineOperand::MO_BlockAddress:
262 return hash_combine(MO.getType(), MO.getTargetFlags(),
Michael Liaoabb87d42012-09-12 21:43:09 +0000263 MO.getBlockAddress(), MO.getOffset());
Chandler Carruth264854f2012-07-05 11:06:22 +0000264 case MachineOperand::MO_RegisterMask:
Juergen Ributzkae8294752013-12-14 06:53:06 +0000265 case MachineOperand::MO_RegisterLiveOut:
Chandler Carruth264854f2012-07-05 11:06:22 +0000266 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask());
267 case MachineOperand::MO_Metadata:
268 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata());
269 case MachineOperand::MO_MCSymbol:
270 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol());
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000271 case MachineOperand::MO_CFIIndex:
272 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex());
Chandler Carruth264854f2012-07-05 11:06:22 +0000273 }
274 llvm_unreachable("Invalid machine operand type");
275}
276
Chris Lattner60055892007-12-30 21:56:09 +0000277/// print - Print the specified machine operand.
278///
Mon P Wangdfcc1ff2008-10-10 01:43:55 +0000279void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const {
Dan Gohman2745d192009-11-09 19:38:45 +0000280 // If the instruction is embedded into a basic block, we can find the
281 // target info for the instruction.
282 if (!TM)
283 if (const MachineInstr *MI = getParent())
284 if (const MachineBasicBlock *MBB = MI->getParent())
285 if (const MachineFunction *MF = MBB->getParent())
286 TM = &MF->getTarget();
Eric Christopherd9134482014-08-04 21:25:23 +0000287 const TargetRegisterInfo *TRI =
288 TM ? TM->getSubtargetImpl()->getRegisterInfo() : nullptr;
Dan Gohman2745d192009-11-09 19:38:45 +0000289
Chris Lattner60055892007-12-30 21:56:09 +0000290 switch (getType()) {
291 case MachineOperand::MO_Register:
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000292 OS << PrintReg(getReg(), TRI, getSubReg());
Dan Gohman0ab11442008-12-18 21:51:27 +0000293
Evan Cheng0dc101b2009-06-30 08:49:04 +0000294 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000295 isInternalRead() || isEarlyClobber() || isTied()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000296 OS << '<';
Chris Lattner60055892007-12-30 21:56:09 +0000297 bool NeedComma = false;
Evan Cheng70b1fa52009-10-14 23:37:31 +0000298 if (isDef()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000299 if (NeedComma) OS << ',';
Dale Johannesen1f3ab862008-09-12 17:49:03 +0000300 if (isEarlyClobber())
301 OS << "earlyclobber,";
Evan Cheng70b1fa52009-10-14 23:37:31 +0000302 if (isImplicit())
303 OS << "imp-";
Chris Lattner60055892007-12-30 21:56:09 +0000304 OS << "def";
305 NeedComma = true;
Jakob Stoklund Olesen7111a632012-04-20 21:45:33 +0000306 // <def,read-undef> only makes sense when getSubReg() is set.
307 // Don't clutter the output otherwise.
308 if (isUndef() && getSubReg())
309 OS << ",read-undef";
Evan Chengf781bd82009-10-21 07:56:02 +0000310 } else if (isImplicit()) {
Evan Cheng70b1fa52009-10-14 23:37:31 +0000311 OS << "imp-use";
Evan Chengf781bd82009-10-21 07:56:02 +0000312 NeedComma = true;
313 }
Evan Cheng70b1fa52009-10-14 23:37:31 +0000314
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000315 if (isKill()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000316 if (NeedComma) OS << ',';
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000317 OS << "kill";
318 NeedComma = true;
319 }
320 if (isDead()) {
321 if (NeedComma) OS << ',';
322 OS << "dead";
323 NeedComma = true;
324 }
325 if (isUndef() && isUse()) {
326 if (NeedComma) OS << ',';
327 OS << "undef";
328 NeedComma = true;
329 }
330 if (isInternalRead()) {
331 if (NeedComma) OS << ',';
332 OS << "internal";
333 NeedComma = true;
334 }
335 if (isTied()) {
336 if (NeedComma) OS << ',';
337 OS << "tied";
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000338 if (TiedTo != 15)
339 OS << unsigned(TiedTo - 1);
Chris Lattner60055892007-12-30 21:56:09 +0000340 }
Chris Lattnerfd682802009-06-24 17:54:48 +0000341 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000342 }
343 break;
344 case MachineOperand::MO_Immediate:
345 OS << getImm();
346 break;
Devang Patelf071d722011-06-24 20:46:11 +0000347 case MachineOperand::MO_CImmediate:
348 getCImm()->getValue().print(OS, false);
349 break;
Nate Begeman26b76b62008-02-14 07:39:30 +0000350 case MachineOperand::MO_FPImmediate:
Chris Lattnerfdd87902009-10-05 05:54:46 +0000351 if (getFPImm()->getType()->isFloatTy())
Nate Begeman26b76b62008-02-14 07:39:30 +0000352 OS << getFPImm()->getValueAPF().convertToFloat();
Chris Lattnerfd682802009-06-24 17:54:48 +0000353 else
Nate Begeman26b76b62008-02-14 07:39:30 +0000354 OS << getFPImm()->getValueAPF().convertToDouble();
Nate Begeman26b76b62008-02-14 07:39:30 +0000355 break;
Chris Lattner60055892007-12-30 21:56:09 +0000356 case MachineOperand::MO_MachineBasicBlock:
Dan Gohman34341e62009-10-31 20:19:03 +0000357 OS << "<BB#" << getMBB()->getNumber() << ">";
Chris Lattner60055892007-12-30 21:56:09 +0000358 break;
359 case MachineOperand::MO_FrameIndex:
Chris Lattnerfd682802009-06-24 17:54:48 +0000360 OS << "<fi#" << getIndex() << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000361 break;
362 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000363 OS << "<cp#" << getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000364 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000365 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000366 break;
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000367 case MachineOperand::MO_TargetIndex:
368 OS << "<ti#" << getIndex();
369 if (getOffset()) OS << "+" << getOffset();
370 OS << '>';
371 break;
Chris Lattner60055892007-12-30 21:56:09 +0000372 case MachineOperand::MO_JumpTableIndex:
Chris Lattnerfd682802009-06-24 17:54:48 +0000373 OS << "<jt#" << getIndex() << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000374 break;
375 case MachineOperand::MO_GlobalAddress:
Dan Gohman0080ee22009-11-06 18:03:10 +0000376 OS << "<ga:";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000377 getGlobal()->printAsOperand(OS, /*PrintType=*/false);
Chris Lattner60055892007-12-30 21:56:09 +0000378 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000379 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000380 break;
381 case MachineOperand::MO_ExternalSymbol:
382 OS << "<es:" << getSymbolName();
383 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000384 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000385 break;
Dan Gohman6c938802009-10-30 01:27:03 +0000386 case MachineOperand::MO_BlockAddress:
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000387 OS << '<';
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000388 getBlockAddress()->printAsOperand(OS, /*PrintType=*/false);
Michael Liaoabb87d42012-09-12 21:43:09 +0000389 if (getOffset()) OS << "+" << getOffset();
Dan Gohman6c938802009-10-30 01:27:03 +0000390 OS << '>';
391 break;
Jakob Stoklund Olesen374ed322012-01-16 19:22:00 +0000392 case MachineOperand::MO_RegisterMask:
Jakob Stoklund Olesen5e1ac452012-02-02 23:52:57 +0000393 OS << "<regmask>";
Jakob Stoklund Olesen374ed322012-01-16 19:22:00 +0000394 break;
Juergen Ributzkae8294752013-12-14 06:53:06 +0000395 case MachineOperand::MO_RegisterLiveOut:
396 OS << "<regliveout>";
397 break;
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000398 case MachineOperand::MO_Metadata:
399 OS << '<';
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000400 getMetadata()->printAsOperand(OS);
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000401 OS << '>';
402 break;
Chris Lattner6c604e32010-03-13 08:14:18 +0000403 case MachineOperand::MO_MCSymbol:
404 OS << "<MCSym=" << *getMCSymbol() << '>';
405 break;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000406 case MachineOperand::MO_CFIIndex:
407 OS << "<call frame instruction>";
408 break;
Chris Lattner60055892007-12-30 21:56:09 +0000409 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000410
Chris Lattnerfd682802009-06-24 17:54:48 +0000411 if (unsigned TF = getTargetFlags())
412 OS << "[TF=" << TF << ']';
Chris Lattner60055892007-12-30 21:56:09 +0000413}
414
415//===----------------------------------------------------------------------===//
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000416// MachineMemOperand Implementation
417//===----------------------------------------------------------------------===//
418
Chris Lattnerde93bb02010-09-21 05:39:30 +0000419/// getAddrSpace - Return the LLVM IR address space number that this pointer
420/// points into.
421unsigned MachinePointerInfo::getAddrSpace() const {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000422 if (V.isNull() || V.is<const PseudoSourceValue*>()) return 0;
423 return cast<PointerType>(V.get<const Value*>()->getType())->getAddressSpace();
Chris Lattnerde93bb02010-09-21 05:39:30 +0000424}
425
Chris Lattner82fd06d2010-09-21 06:22:23 +0000426/// getConstantPool - Return a MachinePointerInfo record that refers to the
427/// constant pool.
428MachinePointerInfo MachinePointerInfo::getConstantPool() {
429 return MachinePointerInfo(PseudoSourceValue::getConstantPool());
430}
431
432/// getFixedStack - Return a MachinePointerInfo record that refers to the
433/// the specified FrameIndex.
434MachinePointerInfo MachinePointerInfo::getFixedStack(int FI, int64_t offset) {
435 return MachinePointerInfo(PseudoSourceValue::getFixedStack(FI), offset);
436}
437
Chris Lattner50287ea2010-09-21 06:43:24 +0000438MachinePointerInfo MachinePointerInfo::getJumpTable() {
439 return MachinePointerInfo(PseudoSourceValue::getJumpTable());
440}
441
442MachinePointerInfo MachinePointerInfo::getGOT() {
443 return MachinePointerInfo(PseudoSourceValue::getGOT());
444}
Chris Lattnerde93bb02010-09-21 05:39:30 +0000445
Chris Lattner886250c2010-09-21 18:51:21 +0000446MachinePointerInfo MachinePointerInfo::getStack(int64_t Offset) {
447 return MachinePointerInfo(PseudoSourceValue::getStack(), Offset);
448}
449
Chris Lattner00ca0b82010-09-21 04:32:08 +0000450MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, unsigned f,
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000451 uint64_t s, unsigned int a,
Hal Finkelcc39b672014-07-24 12:16:19 +0000452 const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +0000453 const MDNode *Ranges)
Chris Lattner00ca0b82010-09-21 04:32:08 +0000454 : PtrInfo(ptrinfo), Size(s),
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000455 Flags((f & ((1 << MOMaxBits) - 1)) | ((Log2_32(a) + 1) << MOMaxBits)),
Hal Finkelcc39b672014-07-24 12:16:19 +0000456 AAInfo(AAInfo), Ranges(Ranges) {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000457 assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue*>() ||
458 isa<PointerType>(PtrInfo.V.get<const Value*>()->getType())) &&
Chris Lattner00ca0b82010-09-21 04:32:08 +0000459 "invalid pointer value");
Dan Gohmane7c82422009-09-21 19:47:04 +0000460 assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
Dan Gohmanbf98f682008-07-16 15:56:42 +0000461 assert((isLoad() || isStore()) && "Not a load/store!");
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000462}
463
Dan Gohman2da2bed2008-08-20 15:58:01 +0000464/// Profile - Gather unique data for the object.
465///
466void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
Chris Lattner187f6532010-09-21 04:23:39 +0000467 ID.AddInteger(getOffset());
Dan Gohman2da2bed2008-08-20 15:58:01 +0000468 ID.AddInteger(Size);
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000469 ID.AddPointer(getOpaqueValue());
Dan Gohman2da2bed2008-08-20 15:58:01 +0000470 ID.AddInteger(Flags);
471}
472
Dan Gohman48b185d2009-09-25 20:36:54 +0000473void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
474 // The Value and Offset may differ due to CSE. But the flags and size
475 // should be the same.
476 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
477 assert(MMO->getSize() == getSize() && "Size mismatch!");
478
479 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
480 // Update the alignment value.
David Greene3a0412f2010-02-15 16:48:31 +0000481 Flags = (Flags & ((1 << MOMaxBits) - 1)) |
482 ((Log2_32(MMO->getBaseAlignment()) + 1) << MOMaxBits);
Dan Gohman48b185d2009-09-25 20:36:54 +0000483 // Also update the base and offset, because the new alignment may
484 // not be applicable with the old ones.
Chris Lattner187f6532010-09-21 04:23:39 +0000485 PtrInfo = MMO->PtrInfo;
Dan Gohman48b185d2009-09-25 20:36:54 +0000486 }
487}
488
Dan Gohman5a6b11c2009-09-25 23:33:20 +0000489/// getAlignment - Return the minimum known alignment in bytes of the
490/// actual memory reference.
491uint64_t MachineMemOperand::getAlignment() const {
492 return MinAlign(getBaseAlignment(), getOffset());
493}
494
Dan Gohman48b185d2009-09-25 20:36:54 +0000495raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) {
496 assert((MMO.isLoad() || MMO.isStore()) &&
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000497 "SV has to be a load, store or both.");
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000498
Dan Gohman48b185d2009-09-25 20:36:54 +0000499 if (MMO.isVolatile())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000500 OS << "Volatile ";
501
Dan Gohman48b185d2009-09-25 20:36:54 +0000502 if (MMO.isLoad())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000503 OS << "LD";
Dan Gohman48b185d2009-09-25 20:36:54 +0000504 if (MMO.isStore())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000505 OS << "ST";
Dan Gohman48b185d2009-09-25 20:36:54 +0000506 OS << MMO.getSize();
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000507
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000508 // Print the address information.
509 OS << "[";
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000510 if (const Value *V = MMO.getValue())
511 V->printAsOperand(OS, /*PrintType=*/false);
512 else if (const PseudoSourceValue *PSV = MMO.getPseudoValue())
513 PSV->printCustom(OS);
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000514 else
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000515 OS << "<unknown>";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000516
Matt Arsenault68c38fd2013-12-14 00:24:02 +0000517 unsigned AS = MMO.getAddrSpace();
518 if (AS != 0)
519 OS << "(addrspace=" << AS << ')';
520
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000521 // If the alignment of the memory reference itself differs from the alignment
522 // of the base pointer, print the base alignment explicitly, next to the base
523 // pointer.
Dan Gohman48b185d2009-09-25 20:36:54 +0000524 if (MMO.getBaseAlignment() != MMO.getAlignment())
525 OS << "(align=" << MMO.getBaseAlignment() << ")";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000526
Dan Gohman48b185d2009-09-25 20:36:54 +0000527 if (MMO.getOffset() != 0)
528 OS << "+" << MMO.getOffset();
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000529 OS << "]";
530
531 // Print the alignment of the reference.
Dan Gohman48b185d2009-09-25 20:36:54 +0000532 if (MMO.getBaseAlignment() != MMO.getAlignment() ||
533 MMO.getBaseAlignment() != MMO.getSize())
534 OS << "(align=" << MMO.getAlignment() << ")";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000535
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000536 // Print TBAA info.
Hal Finkelcc39b672014-07-24 12:16:19 +0000537 if (const MDNode *TBAAInfo = MMO.getAAInfo().TBAA) {
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000538 OS << "(tbaa=";
539 if (TBAAInfo->getNumOperands() > 0)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000540 TBAAInfo->getOperand(0)->printAsOperand(OS);
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000541 else
542 OS << "<unknown>";
543 OS << ")";
544 }
545
Hal Finkel94146652014-07-24 14:25:39 +0000546 // Print AA scope info.
547 if (const MDNode *ScopeInfo = MMO.getAAInfo().Scope) {
548 OS << "(alias.scope=";
549 if (ScopeInfo->getNumOperands() > 0)
550 for (unsigned i = 0, ie = ScopeInfo->getNumOperands(); i != ie; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000551 ScopeInfo->getOperand(i)->printAsOperand(OS);
Hal Finkel94146652014-07-24 14:25:39 +0000552 if (i != ie-1)
553 OS << ",";
554 }
555 else
556 OS << "<unknown>";
557 OS << ")";
558 }
559
560 // Print AA noalias scope info.
561 if (const MDNode *NoAliasInfo = MMO.getAAInfo().NoAlias) {
562 OS << "(noalias=";
563 if (NoAliasInfo->getNumOperands() > 0)
564 for (unsigned i = 0, ie = NoAliasInfo->getNumOperands(); i != ie; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000565 NoAliasInfo->getOperand(i)->printAsOperand(OS);
Hal Finkel94146652014-07-24 14:25:39 +0000566 if (i != ie-1)
567 OS << ",";
568 }
569 else
570 OS << "<unknown>";
571 OS << ")";
572 }
573
Bill Wendling9f638ab2011-04-29 23:45:22 +0000574 // Print nontemporal info.
575 if (MMO.isNonTemporal())
576 OS << "(nontemporal)";
577
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000578 return OS;
579}
580
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000581//===----------------------------------------------------------------------===//
Chris Lattner60055892007-12-30 21:56:09 +0000582// MachineInstr Implementation
583//===----------------------------------------------------------------------===//
584
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000585void MachineInstr::addImplicitDefUseOperands(MachineFunction &MF) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000586 if (MCID->ImplicitDefs)
Craig Topper5a4bcc72012-03-08 08:22:45 +0000587 for (const uint16_t *ImpDefs = MCID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000588 addOperand(MF, MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng6cc775f2011-06-28 19:10:37 +0000589 if (MCID->ImplicitUses)
Craig Topper5a4bcc72012-03-08 08:22:45 +0000590 for (const uint16_t *ImpUses = MCID->getImplicitUses(); *ImpUses; ++ImpUses)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000591 addOperand(MF, MachineOperand::CreateReg(*ImpUses, false, true));
Evan Cheng77af6ac2006-11-13 23:34:06 +0000592}
593
Bob Wilson406f2702010-04-09 04:34:03 +0000594/// MachineInstr ctor - This constructor creates a MachineInstr and adds the
595/// implicit operands. It reserves space for the number of operands specified by
Evan Cheng6cc775f2011-06-28 19:10:37 +0000596/// the MCInstrDesc.
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000597MachineInstr::MachineInstr(MachineFunction &MF, const MCInstrDesc &tid,
598 const DebugLoc dl, bool NoImp)
Craig Topperc0196b12014-04-14 00:51:57 +0000599 : MCID(&tid), Parent(nullptr), Operands(nullptr), NumOperands(0),
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000600 Flags(0), AsmPrinterFlags(0),
Craig Topperc0196b12014-04-14 00:51:57 +0000601 NumMemRefs(0), MemRefs(nullptr), debugLoc(dl) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000602 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
603
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000604 // Reserve space for the expected number of operands.
605 if (unsigned NumOps = MCID->getNumOperands() +
606 MCID->getNumImplicitDefs() + MCID->getNumImplicitUses()) {
607 CapOperands = OperandCapacity::get(NumOps);
608 Operands = MF.allocateOperandArray(CapOperands);
609 }
610
Dale Johannesen4e04ef32009-01-27 23:20:29 +0000611 if (!NoImp)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000612 addImplicitDefUseOperands(MF);
Dale Johannesen4e04ef32009-01-27 23:20:29 +0000613}
614
Misha Brukmanb47ab7a2004-07-09 14:45:17 +0000615/// MachineInstr ctor - Copies MachineInstr arg exactly
616///
Evan Chenga7a20c42008-07-19 00:37:25 +0000617MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Craig Topperc0196b12014-04-14 00:51:57 +0000618 : MCID(&MI.getDesc()), Parent(nullptr), Operands(nullptr), NumOperands(0),
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000619 Flags(0), AsmPrinterFlags(0),
Benjamin Kramerd03878b2012-03-16 16:39:27 +0000620 NumMemRefs(MI.NumMemRefs), MemRefs(MI.MemRefs),
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000621 debugLoc(MI.getDebugLoc()) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000622 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
623
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000624 CapOperands = OperandCapacity::get(MI.getNumOperands());
625 Operands = MF.allocateOperandArray(CapOperands);
Tanya Lattner9953d862004-05-23 20:58:02 +0000626
Jakob Stoklund Olesendc5285f2013-01-05 05:05:51 +0000627 // Copy operands.
Evan Chenga7a20c42008-07-19 00:37:25 +0000628 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000629 addOperand(MF, MI.getOperand(i));
Tanya Lattnerbcee21b2004-05-24 03:14:18 +0000630
Jakob Stoklund Olesena33f5042012-12-18 21:36:05 +0000631 // Copy all the sensible flags.
632 setFlags(MI.Flags);
Alkis Evlogimenos14f3fe82004-02-16 07:17:43 +0000633}
634
Chris Lattner961e7422008-01-01 01:12:31 +0000635/// getRegInfo - If this instruction is embedded into a MachineFunction,
636/// return the MachineRegisterInfo object for the current function, otherwise
637/// return null.
638MachineRegisterInfo *MachineInstr::getRegInfo() {
639 if (MachineBasicBlock *MBB = getParent())
Dan Gohmanf188fa42008-07-08 23:59:09 +0000640 return &MBB->getParent()->getRegInfo();
Craig Topperc0196b12014-04-14 00:51:57 +0000641 return nullptr;
Chris Lattner961e7422008-01-01 01:12:31 +0000642}
643
644/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
645/// this instruction from their respective use lists. This requires that the
646/// operands already be on their use lists.
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000647void MachineInstr::RemoveRegOperandsFromUseLists(MachineRegisterInfo &MRI) {
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +0000648 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000649 if (Operands[i].isReg())
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000650 MRI.removeRegOperandFromUseList(&Operands[i]);
Chris Lattner961e7422008-01-01 01:12:31 +0000651}
652
653/// AddRegOperandsToUseLists - Add all of the register operands in
654/// this instruction from their respective use lists. This requires that the
655/// operands not be on their use lists yet.
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000656void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &MRI) {
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +0000657 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000658 if (Operands[i].isReg())
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000659 MRI.addRegOperandToUseList(&Operands[i]);
Chris Lattner961e7422008-01-01 01:12:31 +0000660}
661
Jakob Stoklund Olesen2455b5852012-12-20 22:54:05 +0000662void MachineInstr::addOperand(const MachineOperand &Op) {
663 MachineBasicBlock *MBB = getParent();
664 assert(MBB && "Use MachineInstrBuilder to add operands to dangling instrs");
665 MachineFunction *MF = MBB->getParent();
666 assert(MF && "Use MachineInstrBuilder to add operands to dangling instrs");
667 addOperand(*MF, Op);
668}
669
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000670/// Move NumOps MachineOperands from Src to Dst, with support for overlapping
671/// ranges. If MRI is non-null also update use-def chains.
672static void moveOperands(MachineOperand *Dst, MachineOperand *Src,
673 unsigned NumOps, MachineRegisterInfo *MRI) {
674 if (MRI)
675 return MRI->moveOperands(Dst, Src, NumOps);
676
677 // Here it would be convenient to call memmove, so that isn't allowed because
678 // MachineOperand has a constructor and so isn't a POD type.
679 if (Dst < Src)
680 for (unsigned i = 0; i != NumOps; ++i)
681 new (Dst + i) MachineOperand(Src[i]);
682 else
683 for (unsigned i = NumOps; i ; --i)
684 new (Dst + i - 1) MachineOperand(Src[i - 1]);
685}
686
Chris Lattner961e7422008-01-01 01:12:31 +0000687/// addOperand - Add the specified operand to the instruction. If it is an
688/// implicit operand, it is added to the end of the operand list. If it is
689/// an explicit operand it is added at the end of the explicit operand list
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000690/// (before the first implicit operand).
Jakob Stoklund Olesen2455b5852012-12-20 22:54:05 +0000691void MachineInstr::addOperand(MachineFunction &MF, const MachineOperand &Op) {
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000692 assert(MCID && "Cannot add operands before providing an instr descriptor");
Dan Gohman9356d8f2008-12-09 22:45:08 +0000693
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000694 // Check if we're adding one of our existing operands.
695 if (&Op >= Operands && &Op < Operands + NumOperands) {
696 // This is unusual: MI->addOperand(MI->getOperand(i)).
697 // If adding Op requires reallocating or moving existing operands around,
698 // the Op reference could go stale. Support it by copying Op.
699 MachineOperand CopyOp(Op);
700 return addOperand(MF, CopyOp);
701 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000702
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000703 // Find the insert location for the new operand. Implicit registers go at
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000704 // the end, everything else goes before the implicit regs.
705 //
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000706 // FIXME: Allow mixed explicit and implicit operands on inline asm.
707 // InstrEmitter::EmitSpecialNode() is marking inline asm clobbers as
708 // implicit-defs, but they must not be moved around. See the FIXME in
709 // InstrEmitter.cpp.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000710 unsigned OpNo = getNumOperands();
711 bool isImpReg = Op.isReg() && Op.isImplicit();
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000712 if (!isImpReg && !isInlineAsm()) {
713 while (OpNo && Operands[OpNo-1].isReg() && Operands[OpNo-1].isImplicit()) {
714 --OpNo;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000715 assert(!Operands[OpNo].isTied() && "Cannot move tied operands");
Chris Lattner961e7422008-01-01 01:12:31 +0000716 }
717 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000718
Pekka Jaaskelaineneb4a6e72013-10-15 14:40:46 +0000719#ifndef NDEBUG
Pekka Jaaskelaineneb08e2e2013-10-15 14:18:10 +0000720 bool isMetaDataOp = Op.getType() == MachineOperand::MO_Metadata;
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000721 // OpNo now points as the desired insertion point. Unless this is a variadic
722 // instruction, only implicit regs are allowed beyond MCID->getNumOperands().
Jakob Stoklund Olesenc300ef02012-07-04 23:53:23 +0000723 // RegMask operands go between the explicit and implicit operands.
724 assert((isImpReg || Op.isRegMask() || MCID->isVariadic() ||
Pekka Jaaskelaineneb08e2e2013-10-15 14:18:10 +0000725 OpNo < MCID->getNumOperands() || isMetaDataOp) &&
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000726 "Trying to add an operand to a machine instr that is already done!");
Pekka Jaaskelaineneb4a6e72013-10-15 14:40:46 +0000727#endif
Chris Lattner961e7422008-01-01 01:12:31 +0000728
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000729 MachineRegisterInfo *MRI = getRegInfo();
Chris Lattner961e7422008-01-01 01:12:31 +0000730
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000731 // Determine if the Operands array needs to be reallocated.
732 // Save the old capacity and operand array.
733 OperandCapacity OldCap = CapOperands;
734 MachineOperand *OldOperands = Operands;
735 if (!OldOperands || OldCap.getSize() == getNumOperands()) {
736 CapOperands = OldOperands ? OldCap.getNext() : OldCap.get(1);
737 Operands = MF.allocateOperandArray(CapOperands);
738 // Move the operands before the insertion point.
739 if (OpNo)
740 moveOperands(Operands, OldOperands, OpNo, MRI);
741 }
Chris Lattner961e7422008-01-01 01:12:31 +0000742
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000743 // Move the operands following the insertion point.
744 if (OpNo != NumOperands)
745 moveOperands(Operands + OpNo + 1, OldOperands + OpNo, NumOperands - OpNo,
746 MRI);
747 ++NumOperands;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000748
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000749 // Deallocate the old operand array.
750 if (OldOperands != Operands && OldOperands)
751 MF.deallocateOperandArray(OldCap, OldOperands);
752
753 // Copy Op into place. It still needs to be inserted into the MRI use lists.
754 MachineOperand *NewMO = new (Operands + OpNo) MachineOperand(Op);
755 NewMO->ParentMI = this;
756
757 // When adding a register operand, tell MRI about it.
758 if (NewMO->isReg()) {
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000759 // Ensure isOnRegUseList() returns false, regardless of Op's status.
Craig Topperc0196b12014-04-14 00:51:57 +0000760 NewMO->Contents.Reg.Prev = nullptr;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000761 // Ignore existing ties. This is not a property that can be copied.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000762 NewMO->TiedTo = 0;
763 // Add the new operand to MRI, but only for instructions in an MBB.
764 if (MRI)
765 MRI->addRegOperandToUseList(NewMO);
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000766 // The MCID operand information isn't accurate until we start adding
767 // explicit operands. The implicit operands are added first, then the
768 // explicits are inserted before them.
769 if (!isImpReg) {
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000770 // Tie uses to defs as indicated in MCInstrDesc.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000771 if (NewMO->isUse()) {
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000772 int DefIdx = MCID->getOperandConstraint(OpNo, MCOI::TIED_TO);
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +0000773 if (DefIdx != -1)
774 tieOperands(DefIdx, OpNo);
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000775 }
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000776 // If the register operand is flagged as early, mark the operand as such.
777 if (MCID->getOperandConstraint(OpNo, MCOI::EARLY_CLOBBER) != -1)
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000778 NewMO->setIsEarlyClobber(true);
Chris Lattner961e7422008-01-01 01:12:31 +0000779 }
Chris Lattner961e7422008-01-01 01:12:31 +0000780 }
781}
782
783/// RemoveOperand - Erase an operand from an instruction, leaving it with one
784/// fewer operand than it started with.
785///
786void MachineInstr::RemoveOperand(unsigned OpNo) {
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +0000787 assert(OpNo < getNumOperands() && "Invalid operand number");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000788 untieRegOperand(OpNo);
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000789
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000790#ifndef NDEBUG
791 // Moving tied operands would break the ties.
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +0000792 for (unsigned i = OpNo + 1, e = getNumOperands(); i != e; ++i)
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000793 if (Operands[i].isReg())
794 assert(!Operands[i].isTied() && "Cannot move tied operands");
795#endif
796
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000797 MachineRegisterInfo *MRI = getRegInfo();
798 if (MRI && Operands[OpNo].isReg())
799 MRI->removeRegOperandFromUseList(Operands + OpNo);
Chris Lattner961e7422008-01-01 01:12:31 +0000800
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000801 // Don't call the MachineOperand destructor. A lot of this code depends on
802 // MachineOperand having a trivial destructor anyway, and adding a call here
803 // wouldn't make it 'destructor-correct'.
804
805 if (unsigned N = NumOperands - 1 - OpNo)
806 moveOperands(Operands + OpNo, Operands + OpNo + 1, N, MRI);
807 --NumOperands;
Chris Lattner961e7422008-01-01 01:12:31 +0000808}
809
Dan Gohman48b185d2009-09-25 20:36:54 +0000810/// addMemOperand - Add a MachineMemOperand to the machine instruction.
811/// This function should be used only occasionally. The setMemRefs function
812/// is the primary method for setting up a MachineInstr's MemRefs list.
Dan Gohman3b460302008-07-07 23:14:23 +0000813void MachineInstr::addMemOperand(MachineFunction &MF,
Dan Gohman48b185d2009-09-25 20:36:54 +0000814 MachineMemOperand *MO) {
815 mmo_iterator OldMemRefs = MemRefs;
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000816 unsigned OldNumMemRefs = NumMemRefs;
Dan Gohman3b460302008-07-07 23:14:23 +0000817
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000818 unsigned NewNum = NumMemRefs + 1;
Dan Gohman48b185d2009-09-25 20:36:54 +0000819 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
Dan Gohman3b460302008-07-07 23:14:23 +0000820
Benjamin Kramerd03878b2012-03-16 16:39:27 +0000821 std::copy(OldMemRefs, OldMemRefs + OldNumMemRefs, NewMemRefs);
Dan Gohman48b185d2009-09-25 20:36:54 +0000822 NewMemRefs[NewNum - 1] = MO;
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000823 setMemRefs(NewMemRefs, NewMemRefs + NewNum);
Dan Gohman48b185d2009-09-25 20:36:54 +0000824}
Chris Lattner961e7422008-01-01 01:12:31 +0000825
Benjamin Kramer97f889f2012-03-17 17:03:45 +0000826bool MachineInstr::hasPropertyInBundle(unsigned Mask, QueryType Type) const {
Jakob Stoklund Olesenf0615c72013-01-10 18:42:44 +0000827 assert(!isBundledWithPred() && "Must be called on bundle header");
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +0000828 for (MachineBasicBlock::const_instr_iterator MII = this;; ++MII) {
Benjamin Kramer97f889f2012-03-17 17:03:45 +0000829 if (MII->getDesc().getFlags() & Mask) {
Evan Chengcdf89fd2011-12-08 19:23:10 +0000830 if (Type == AnyInBundle)
Evan Cheng7f8e5632011-12-07 07:15:52 +0000831 return true;
832 } else {
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +0000833 if (Type == AllInBundle && !MII->isBundle())
Evan Cheng7f8e5632011-12-07 07:15:52 +0000834 return false;
835 }
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +0000836 // This was the last instruction in the bundle.
837 if (!MII->isBundledWithSucc())
838 return Type == AllInBundle;
Evan Cheng2a81dd42011-12-06 22:12:01 +0000839 }
Evan Cheng2a81dd42011-12-06 22:12:01 +0000840}
841
Evan Chenge9c46c22010-03-03 01:44:33 +0000842bool MachineInstr::isIdenticalTo(const MachineInstr *Other,
843 MICheckType Check) const {
Evan Cheng0f260e12010-03-03 21:54:14 +0000844 // If opcodes or number of operands are not the same then the two
845 // instructions are obviously not identical.
846 if (Other->getOpcode() != getOpcode() ||
847 Other->getNumOperands() != getNumOperands())
848 return false;
849
Evan Cheng7fae11b2011-12-14 02:11:42 +0000850 if (isBundle()) {
851 // Both instructions are bundles, compare MIs inside the bundle.
852 MachineBasicBlock::const_instr_iterator I1 = *this;
853 MachineBasicBlock::const_instr_iterator E1 = getParent()->instr_end();
854 MachineBasicBlock::const_instr_iterator I2 = *Other;
855 MachineBasicBlock::const_instr_iterator E2= Other->getParent()->instr_end();
856 while (++I1 != E1 && I1->isInsideBundle()) {
857 ++I2;
858 if (I2 == E2 || !I2->isInsideBundle() || !I1->isIdenticalTo(I2, Check))
859 return false;
860 }
861 }
862
Evan Cheng0f260e12010-03-03 21:54:14 +0000863 // Check operands to make sure they match.
864 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
865 const MachineOperand &MO = getOperand(i);
866 const MachineOperand &OMO = Other->getOperand(i);
Evan Chengcfdf3392011-05-12 00:56:58 +0000867 if (!MO.isReg()) {
868 if (!MO.isIdenticalTo(OMO))
869 return false;
870 continue;
871 }
872
Evan Cheng0f260e12010-03-03 21:54:14 +0000873 // Clients may or may not want to ignore defs when testing for equality.
874 // For example, machine CSE pass only cares about finding common
875 // subexpressions, so it's safe to ignore virtual register defs.
Evan Chengcfdf3392011-05-12 00:56:58 +0000876 if (MO.isDef()) {
Evan Cheng0f260e12010-03-03 21:54:14 +0000877 if (Check == IgnoreDefs)
878 continue;
Evan Chengcfdf3392011-05-12 00:56:58 +0000879 else if (Check == IgnoreVRegDefs) {
880 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) ||
881 TargetRegisterInfo::isPhysicalRegister(OMO.getReg()))
882 if (MO.getReg() != OMO.getReg())
883 return false;
884 } else {
885 if (!MO.isIdenticalTo(OMO))
Evan Cheng0f260e12010-03-03 21:54:14 +0000886 return false;
Evan Chengcfdf3392011-05-12 00:56:58 +0000887 if (Check == CheckKillDead && MO.isDead() != OMO.isDead())
888 return false;
889 }
890 } else {
891 if (!MO.isIdenticalTo(OMO))
892 return false;
893 if (Check == CheckKillDead && MO.isKill() != OMO.isKill())
894 return false;
895 }
Evan Cheng0f260e12010-03-03 21:54:14 +0000896 }
Devang Patelbf8cc602011-07-07 17:45:33 +0000897 // If DebugLoc does not match then two dbg.values are not identical.
898 if (isDebugValue())
899 if (!getDebugLoc().isUnknown() && !Other->getDebugLoc().isUnknown()
900 && getDebugLoc() != Other->getDebugLoc())
901 return false;
Evan Cheng0f260e12010-03-03 21:54:14 +0000902 return true;
Evan Chenge9c46c22010-03-03 01:44:33 +0000903}
904
Chris Lattnerbec79b42006-04-17 21:35:41 +0000905MachineInstr *MachineInstr::removeFromParent() {
906 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +0000907 return getParent()->remove(this);
Chris Lattnerbec79b42006-04-17 21:35:41 +0000908}
909
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +0000910MachineInstr *MachineInstr::removeFromBundle() {
911 assert(getParent() && "Not embedded in a basic block!");
912 return getParent()->remove_instr(this);
913}
Chris Lattnerbec79b42006-04-17 21:35:41 +0000914
Dan Gohman3b460302008-07-07 23:14:23 +0000915void MachineInstr::eraseFromParent() {
916 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +0000917 getParent()->erase(this);
Dan Gohman3b460302008-07-07 23:14:23 +0000918}
919
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +0000920void MachineInstr::eraseFromParentAndMarkDBGValuesForRemoval() {
921 assert(getParent() && "Not embedded in a basic block!");
922 MachineBasicBlock *MBB = getParent();
923 MachineFunction *MF = MBB->getParent();
924 assert(MF && "Not embedded in a function!");
925
926 MachineInstr *MI = (MachineInstr *)this;
927 MachineRegisterInfo &MRI = MF->getRegInfo();
928
929 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
930 const MachineOperand &MO = MI->getOperand(i);
931 if (!MO.isReg() || !MO.isDef())
932 continue;
933 unsigned Reg = MO.getReg();
934 if (!TargetRegisterInfo::isVirtualRegister(Reg))
935 continue;
936 MRI.markUsesInDebugValueAsUndef(Reg);
937 }
938 MI->eraseFromParent();
939}
940
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +0000941void MachineInstr::eraseFromBundle() {
942 assert(getParent() && "Not embedded in a basic block!");
943 getParent()->erase_instr(this);
944}
Dan Gohman3b460302008-07-07 23:14:23 +0000945
Evan Cheng4d728b02007-05-15 01:26:09 +0000946/// getNumExplicitOperands - Returns the number of non-implicit operands.
947///
948unsigned MachineInstr::getNumExplicitOperands() const {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000949 unsigned NumOperands = MCID->getNumOperands();
950 if (!MCID->isVariadic())
Evan Cheng4d728b02007-05-15 01:26:09 +0000951 return NumOperands;
952
Dan Gohman37608532009-04-15 17:59:11 +0000953 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
954 const MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000955 if (!MO.isReg() || !MO.isImplicit())
Evan Cheng4d728b02007-05-15 01:26:09 +0000956 NumOperands++;
957 }
958 return NumOperands;
959}
960
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +0000961void MachineInstr::bundleWithPred() {
962 assert(!isBundledWithPred() && "MI is already bundled with its predecessor");
963 setFlag(BundledPred);
964 MachineBasicBlock::instr_iterator Pred = this;
965 --Pred;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +0000966 assert(!Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +0000967 Pred->setFlag(BundledSucc);
968}
969
970void MachineInstr::bundleWithSucc() {
971 assert(!isBundledWithSucc() && "MI is already bundled with its successor");
972 setFlag(BundledSucc);
973 MachineBasicBlock::instr_iterator Succ = this;
974 ++Succ;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +0000975 assert(!Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +0000976 Succ->setFlag(BundledPred);
977}
978
979void MachineInstr::unbundleFromPred() {
980 assert(isBundledWithPred() && "MI isn't bundled with its predecessor");
981 clearFlag(BundledPred);
982 MachineBasicBlock::instr_iterator Pred = this;
983 --Pred;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +0000984 assert(Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +0000985 Pred->clearFlag(BundledSucc);
986}
987
988void MachineInstr::unbundleFromSucc() {
989 assert(isBundledWithSucc() && "MI isn't bundled with its successor");
990 clearFlag(BundledSucc);
991 MachineBasicBlock::instr_iterator Succ = this;
Sergei Larin3b46d7e2013-01-09 17:54:33 +0000992 ++Succ;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +0000993 assert(Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +0000994 Succ->clearFlag(BundledPred);
995}
996
Evan Cheng6eb516d2011-01-07 23:50:32 +0000997bool MachineInstr::isStackAligningInlineAsm() const {
998 if (isInlineAsm()) {
999 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1000 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
1001 return true;
1002 }
1003 return false;
1004}
Chris Lattner33f5af02006-10-20 22:39:59 +00001005
Chad Rosier994f4042012-09-05 21:00:58 +00001006InlineAsm::AsmDialect MachineInstr::getInlineAsmDialect() const {
1007 assert(isInlineAsm() && "getInlineAsmDialect() only works for inline asms!");
1008 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
Chad Rosiere53314f2012-09-05 22:40:13 +00001009 return InlineAsm::AsmDialect((ExtraInfo & InlineAsm::Extra_AsmDialect) != 0);
Chad Rosier994f4042012-09-05 21:00:58 +00001010}
1011
Jakob Stoklund Olesen1e737162011-10-12 23:37:33 +00001012int MachineInstr::findInlineAsmFlagIdx(unsigned OpIdx,
1013 unsigned *GroupNo) const {
1014 assert(isInlineAsm() && "Expected an inline asm instruction");
1015 assert(OpIdx < getNumOperands() && "OpIdx out of range");
1016
1017 // Ignore queries about the initial operands.
1018 if (OpIdx < InlineAsm::MIOp_FirstOperand)
1019 return -1;
1020
1021 unsigned Group = 0;
1022 unsigned NumOps;
1023 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
1024 i += NumOps) {
1025 const MachineOperand &FlagMO = getOperand(i);
1026 // If we reach the implicit register operands, stop looking.
1027 if (!FlagMO.isImm())
1028 return -1;
1029 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
1030 if (i + NumOps > OpIdx) {
1031 if (GroupNo)
1032 *GroupNo = Group;
1033 return i;
1034 }
1035 ++Group;
1036 }
1037 return -1;
1038}
1039
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001040const TargetRegisterClass*
1041MachineInstr::getRegClassConstraint(unsigned OpIdx,
1042 const TargetInstrInfo *TII,
1043 const TargetRegisterInfo *TRI) const {
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001044 assert(getParent() && "Can't have an MBB reference here!");
1045 assert(getParent()->getParent() && "Can't have an MF reference here!");
1046 const MachineFunction &MF = *getParent()->getParent();
1047
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001048 // Most opcodes have fixed constraints in their MCInstrDesc.
1049 if (!isInlineAsm())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001050 return TII->getRegClass(getDesc(), OpIdx, TRI, MF);
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001051
1052 if (!getOperand(OpIdx).isReg())
Craig Topperc0196b12014-04-14 00:51:57 +00001053 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001054
1055 // For tied uses on inline asm, get the constraint from the def.
1056 unsigned DefIdx;
1057 if (getOperand(OpIdx).isUse() && isRegTiedToDefOperand(OpIdx, &DefIdx))
1058 OpIdx = DefIdx;
1059
1060 // Inline asm stores register class constraints in the flag word.
1061 int FlagIdx = findInlineAsmFlagIdx(OpIdx);
1062 if (FlagIdx < 0)
Craig Topperc0196b12014-04-14 00:51:57 +00001063 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001064
1065 unsigned Flag = getOperand(FlagIdx).getImm();
1066 unsigned RCID;
1067 if (InlineAsm::hasRegClassConstraint(Flag, RCID))
1068 return TRI->getRegClass(RCID);
1069
1070 // Assume that all registers in a memory operand are pointers.
1071 if (InlineAsm::getKind(Flag) == InlineAsm::Kind_Mem)
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001072 return TRI->getPointerRegClass(MF);
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001073
Craig Topperc0196b12014-04-14 00:51:57 +00001074 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001075}
1076
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001077const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVReg(
1078 unsigned Reg, const TargetRegisterClass *CurRC, const TargetInstrInfo *TII,
1079 const TargetRegisterInfo *TRI, bool ExploreBundle) const {
1080 // Check every operands inside the bundle if we have
1081 // been asked to.
1082 if (ExploreBundle)
1083 for (ConstMIBundleOperands OpndIt(this); OpndIt.isValid() && CurRC;
1084 ++OpndIt)
1085 CurRC = OpndIt->getParent()->getRegClassConstraintEffectForVRegImpl(
1086 OpndIt.getOperandNo(), Reg, CurRC, TII, TRI);
1087 else
1088 // Otherwise, just check the current operands.
1089 for (ConstMIOperands OpndIt(this); OpndIt.isValid() && CurRC; ++OpndIt)
1090 CurRC = getRegClassConstraintEffectForVRegImpl(OpndIt.getOperandNo(), Reg,
1091 CurRC, TII, TRI);
1092 return CurRC;
1093}
1094
1095const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVRegImpl(
1096 unsigned OpIdx, unsigned Reg, const TargetRegisterClass *CurRC,
1097 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
1098 assert(CurRC && "Invalid initial register class");
1099 // Check if Reg is constrained by some of its use/def from MI.
1100 const MachineOperand &MO = getOperand(OpIdx);
1101 if (!MO.isReg() || MO.getReg() != Reg)
1102 return CurRC;
1103 // If yes, accumulate the constraints through the operand.
1104 return getRegClassConstraintEffect(OpIdx, CurRC, TII, TRI);
1105}
1106
1107const TargetRegisterClass *MachineInstr::getRegClassConstraintEffect(
1108 unsigned OpIdx, const TargetRegisterClass *CurRC,
1109 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
1110 const TargetRegisterClass *OpRC = getRegClassConstraint(OpIdx, TII, TRI);
1111 const MachineOperand &MO = getOperand(OpIdx);
1112 assert(MO.isReg() &&
1113 "Cannot get register constraints for non-register operand");
1114 assert(CurRC && "Invalid initial register class");
1115 if (unsigned SubIdx = MO.getSubReg()) {
1116 if (OpRC)
1117 CurRC = TRI->getMatchingSuperRegClass(CurRC, OpRC, SubIdx);
1118 else
1119 CurRC = TRI->getSubClassWithSubReg(CurRC, SubIdx);
1120 } else if (OpRC)
1121 CurRC = TRI->getCommonSubClass(CurRC, OpRC);
1122 return CurRC;
1123}
1124
Jakob Stoklund Olesen68d752b2013-01-09 18:28:16 +00001125/// Return the number of instructions inside the MI bundle, not counting the
1126/// header instruction.
Evan Cheng7fae11b2011-12-14 02:11:42 +00001127unsigned MachineInstr::getBundleSize() const {
Jakob Stoklund Olesen68d752b2013-01-09 18:28:16 +00001128 MachineBasicBlock::const_instr_iterator I = this;
Evan Cheng7fae11b2011-12-14 02:11:42 +00001129 unsigned Size = 0;
Jakob Stoklund Olesen68d752b2013-01-09 18:28:16 +00001130 while (I->isBundledWithSucc())
1131 ++Size, ++I;
Evan Cheng7fae11b2011-12-14 02:11:42 +00001132 return Size;
1133}
1134
Evan Cheng910c8082007-04-26 19:00:32 +00001135/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Jim Grosbach9632c142009-09-17 17:57:26 +00001136/// the specific register or -1 if it is not found. It further tightens
Evan Cheng9965aeb2007-02-23 01:04:26 +00001137/// the search criteria to a use that kills the register if isKill is true.
Evan Cheng63254462008-03-05 00:59:57 +00001138int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
1139 const TargetRegisterInfo *TRI) const {
Evan Cheng75c21942006-12-06 08:27:42 +00001140 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng5983bdb2007-05-29 18:35:22 +00001141 const MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001142 if (!MO.isReg() || !MO.isUse())
Evan Cheng63254462008-03-05 00:59:57 +00001143 continue;
1144 unsigned MOReg = MO.getReg();
1145 if (!MOReg)
1146 continue;
1147 if (MOReg == Reg ||
1148 (TRI &&
1149 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
1150 TargetRegisterInfo::isPhysicalRegister(Reg) &&
1151 TRI->isSubRegister(MOReg, Reg)))
Evan Cheng9965aeb2007-02-23 01:04:26 +00001152 if (!isKill || MO.isKill())
Evan Chengec3ac312007-03-26 22:37:45 +00001153 return i;
Evan Cheng75c21942006-12-06 08:27:42 +00001154 }
Evan Chengec3ac312007-03-26 22:37:45 +00001155 return -1;
Evan Cheng75c21942006-12-06 08:27:42 +00001156}
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001157
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001158/// readsWritesVirtualRegister - Return a pair of bools (reads, writes)
1159/// indicating if this instruction reads or writes Reg. This also considers
1160/// partial defines.
1161std::pair<bool,bool>
1162MachineInstr::readsWritesVirtualRegister(unsigned Reg,
1163 SmallVectorImpl<unsigned> *Ops) const {
1164 bool PartDef = false; // Partial redefine.
1165 bool FullDef = false; // Full define.
1166 bool Use = false;
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001167
1168 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1169 const MachineOperand &MO = getOperand(i);
1170 if (!MO.isReg() || MO.getReg() != Reg)
1171 continue;
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001172 if (Ops)
1173 Ops->push_back(i);
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001174 if (MO.isUse())
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001175 Use |= !MO.isUndef();
Jakob Stoklund Olesen9eb77bf2011-08-19 00:30:17 +00001176 else if (MO.getSubReg() && !MO.isUndef())
1177 // A partial <def,undef> doesn't count as reading the register.
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001178 PartDef = true;
1179 else
1180 FullDef = true;
1181 }
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001182 // A partial redefine uses Reg unless there is also a full define.
1183 return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef);
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001184}
1185
Evan Cheng63254462008-03-05 00:59:57 +00001186/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman72a0bc12008-05-06 00:20:10 +00001187/// the specified register or -1 if it is not found. If isDead is true, defs
1188/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
1189/// also checks if there is a def of a super-register.
Evan Cheng38584512010-05-21 20:53:24 +00001190int
1191MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, bool Overlap,
1192 const TargetRegisterInfo *TRI) const {
1193 bool isPhys = TargetRegisterInfo::isPhysicalRegister(Reg);
Evan Chengf7ed82d2007-02-19 21:49:54 +00001194 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng63254462008-03-05 00:59:57 +00001195 const MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesene7d3f442012-02-14 23:49:37 +00001196 // Accept regmask operands when Overlap is set.
1197 // Ignore them when looking for a specific def operand (Overlap == false).
1198 if (isPhys && Overlap && MO.isRegMask() && MO.clobbersPhysReg(Reg))
1199 return i;
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001200 if (!MO.isReg() || !MO.isDef())
Evan Cheng63254462008-03-05 00:59:57 +00001201 continue;
1202 unsigned MOReg = MO.getReg();
Evan Cheng38584512010-05-21 20:53:24 +00001203 bool Found = (MOReg == Reg);
1204 if (!Found && TRI && isPhys &&
1205 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
1206 if (Overlap)
1207 Found = TRI->regsOverlap(MOReg, Reg);
1208 else
1209 Found = TRI->isSubRegister(MOReg, Reg);
1210 }
1211 if (Found && (!isDead || MO.isDead()))
1212 return i;
Evan Chengf7ed82d2007-02-19 21:49:54 +00001213 }
Evan Cheng63254462008-03-05 00:59:57 +00001214 return -1;
Evan Chengf7ed82d2007-02-19 21:49:54 +00001215}
Evan Cheng4d728b02007-05-15 01:26:09 +00001216
Evan Cheng5983bdb2007-05-29 18:35:22 +00001217/// findFirstPredOperandIdx() - Find the index of the first operand in the
1218/// operand list that is used to represent the predicate. It returns -1 if
1219/// none is found.
1220int MachineInstr::findFirstPredOperandIdx() const {
Jim Grosbached16ec42011-08-29 22:24:09 +00001221 // Don't call MCID.findFirstPredOperandIdx() because this variant
1222 // is sometimes called on an instruction that's not yet complete, and
1223 // so the number of operands is less than the MCID indicates. In
1224 // particular, the PTX target does this.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001225 const MCInstrDesc &MCID = getDesc();
1226 if (MCID.isPredicable()) {
Evan Cheng4d728b02007-05-15 01:26:09 +00001227 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Evan Cheng6cc775f2011-06-28 19:10:37 +00001228 if (MCID.OpInfo[i].isPredicate())
Evan Cheng5983bdb2007-05-29 18:35:22 +00001229 return i;
Evan Cheng4d728b02007-05-15 01:26:09 +00001230 }
1231
Evan Cheng5983bdb2007-05-29 18:35:22 +00001232 return -1;
Evan Cheng4d728b02007-05-15 01:26:09 +00001233}
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001234
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001235// MachineOperand::TiedTo is 4 bits wide.
1236const unsigned TiedMax = 15;
1237
1238/// tieOperands - Mark operands at DefIdx and UseIdx as tied to each other.
1239///
1240/// Use and def operands can be tied together, indicated by a non-zero TiedTo
1241/// field. TiedTo can have these values:
1242///
1243/// 0: Operand is not tied to anything.
1244/// 1 to TiedMax-1: Tied to getOperand(TiedTo-1).
1245/// TiedMax: Tied to an operand >= TiedMax-1.
1246///
1247/// The tied def must be one of the first TiedMax operands on a normal
1248/// instruction. INLINEASM instructions allow more tied defs.
1249///
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001250void MachineInstr::tieOperands(unsigned DefIdx, unsigned UseIdx) {
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001251 MachineOperand &DefMO = getOperand(DefIdx);
1252 MachineOperand &UseMO = getOperand(UseIdx);
1253 assert(DefMO.isDef() && "DefIdx must be a def operand");
1254 assert(UseMO.isUse() && "UseIdx must be a use operand");
1255 assert(!DefMO.isTied() && "Def is already tied to another use");
1256 assert(!UseMO.isTied() && "Use is already tied to another def");
1257
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001258 if (DefIdx < TiedMax)
1259 UseMO.TiedTo = DefIdx + 1;
1260 else {
1261 // Inline asm can use the group descriptors to find tied operands, but on
1262 // normal instruction, the tied def must be within the first TiedMax
1263 // operands.
1264 assert(isInlineAsm() && "DefIdx out of range");
1265 UseMO.TiedTo = TiedMax;
1266 }
1267
1268 // UseIdx can be out of range, we'll search for it in findTiedOperandIdx().
1269 DefMO.TiedTo = std::min(UseIdx + 1, TiedMax);
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001270}
1271
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001272/// Given the index of a tied register operand, find the operand it is tied to.
1273/// Defs are tied to uses and vice versa. Returns the index of the tied operand
1274/// which must exist.
1275unsigned MachineInstr::findTiedOperandIdx(unsigned OpIdx) const {
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001276 const MachineOperand &MO = getOperand(OpIdx);
1277 assert(MO.isTied() && "Operand isn't tied");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001278
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001279 // Normally TiedTo is in range.
1280 if (MO.TiedTo < TiedMax)
1281 return MO.TiedTo - 1;
1282
1283 // Uses on normal instructions can be out of range.
1284 if (!isInlineAsm()) {
1285 // Normal tied defs must be in the 0..TiedMax-1 range.
1286 if (MO.isUse())
1287 return TiedMax - 1;
1288 // MO is a def. Search for the tied use.
1289 for (unsigned i = TiedMax - 1, e = getNumOperands(); i != e; ++i) {
1290 const MachineOperand &UseMO = getOperand(i);
1291 if (UseMO.isReg() && UseMO.isUse() && UseMO.TiedTo == OpIdx + 1)
1292 return i;
1293 }
1294 llvm_unreachable("Can't find tied use");
1295 }
1296
1297 // Now deal with inline asm by parsing the operand group descriptor flags.
1298 // Find the beginning of each operand group.
1299 SmallVector<unsigned, 8> GroupIdx;
1300 unsigned OpIdxGroup = ~0u;
1301 unsigned NumOps;
1302 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
1303 i += NumOps) {
1304 const MachineOperand &FlagMO = getOperand(i);
1305 assert(FlagMO.isImm() && "Invalid tied operand on inline asm");
1306 unsigned CurGroup = GroupIdx.size();
1307 GroupIdx.push_back(i);
1308 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
1309 // OpIdx belongs to this operand group.
1310 if (OpIdx > i && OpIdx < i + NumOps)
1311 OpIdxGroup = CurGroup;
1312 unsigned TiedGroup;
1313 if (!InlineAsm::isUseOperandTiedToDef(FlagMO.getImm(), TiedGroup))
1314 continue;
1315 // Operands in this group are tied to operands in TiedGroup which must be
1316 // earlier. Find the number of operands between the two groups.
1317 unsigned Delta = i - GroupIdx[TiedGroup];
1318
1319 // OpIdx is a use tied to TiedGroup.
1320 if (OpIdxGroup == CurGroup)
1321 return OpIdx - Delta;
1322
1323 // OpIdx is a def tied to this use group.
1324 if (OpIdxGroup == TiedGroup)
1325 return OpIdx + Delta;
1326 }
1327 llvm_unreachable("Invalid tied operand on inline asm");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001328}
1329
Dan Gohmanc90f51c2010-05-13 20:34:42 +00001330/// clearKillInfo - Clears kill flags on all operands.
1331///
1332void MachineInstr::clearKillInfo() {
1333 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1334 MachineOperand &MO = getOperand(i);
1335 if (MO.isReg() && MO.isUse())
1336 MO.setIsKill(false);
1337 }
1338}
1339
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001340void MachineInstr::substituteRegister(unsigned FromReg,
1341 unsigned ToReg,
1342 unsigned SubIdx,
1343 const TargetRegisterInfo &RegInfo) {
1344 if (TargetRegisterInfo::isPhysicalRegister(ToReg)) {
1345 if (SubIdx)
1346 ToReg = RegInfo.getSubReg(ToReg, SubIdx);
1347 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1348 MachineOperand &MO = getOperand(i);
1349 if (!MO.isReg() || MO.getReg() != FromReg)
1350 continue;
1351 MO.substPhysReg(ToReg, RegInfo);
1352 }
1353 } else {
1354 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1355 MachineOperand &MO = getOperand(i);
1356 if (!MO.isReg() || MO.getReg() != FromReg)
1357 continue;
1358 MO.substVirtReg(ToReg, SubIdx, RegInfo);
1359 }
1360 }
1361}
1362
Evan Cheng7d98a482008-07-03 09:09:37 +00001363/// isSafeToMove - Return true if it is safe to move this instruction. If
1364/// SawStore is set to true, it means that there is a store (or call) between
1365/// the instruction's location and its intended destination.
Dan Gohman0d9d8ae2008-11-18 19:04:29 +00001366bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII,
Evan Cheng62e795a2010-03-02 19:03:01 +00001367 AliasAnalysis *AA,
1368 bool &SawStore) const {
Evan Cheng399e1102008-03-13 00:44:09 +00001369 // Ignore stuff that we obviously can't move.
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001370 //
1371 // Treat volatile loads as stores. This is not strictly necessary for
Jakob Stoklund Olesend92e2bc2012-09-04 18:44:43 +00001372 // volatiles, but it is required for atomic loads. It is not allowed to move
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001373 // a load across an atomic load with Ordering > Monotonic.
1374 if (mayStore() || isCall() ||
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001375 (mayLoad() && hasOrderedMemoryRef())) {
Evan Cheng399e1102008-03-13 00:44:09 +00001376 SawStore = true;
1377 return false;
1378 }
Evan Cheng0638c202011-01-07 21:08:26 +00001379
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001380 if (isPosition() || isDebugValue() || isTerminator() ||
1381 hasUnmodeledSideEffects())
Evan Cheng399e1102008-03-13 00:44:09 +00001382 return false;
1383
1384 // See if this instruction does a load. If so, we have to guarantee that the
1385 // loaded value doesn't change between the load and the its intended
1386 // destination. The check for isInvariantLoad gives the targe the chance to
1387 // classify the load as always returning a constant, e.g. a constant pool
1388 // load.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001389 if (mayLoad() && !isInvariantLoad(AA))
Evan Cheng399e1102008-03-13 00:44:09 +00001390 // Otherwise, this is a real load. If there is a store between the load and
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001391 // end of block, we can't move it.
1392 return !SawStore;
Dan Gohman7c59ed62008-09-24 00:06:15 +00001393
Evan Cheng399e1102008-03-13 00:44:09 +00001394 return true;
1395}
1396
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001397/// hasOrderedMemoryRef - Return true if this instruction may have an ordered
1398/// or volatile memory reference, or if the information describing the memory
1399/// reference is not available. Return false if it is known to have no ordered
1400/// memory references.
1401bool MachineInstr::hasOrderedMemoryRef() const {
Dan Gohman7c59ed62008-09-24 00:06:15 +00001402 // An instruction known never to access memory won't have a volatile access.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001403 if (!mayStore() &&
1404 !mayLoad() &&
1405 !isCall() &&
Evan Cheng6eb516d2011-01-07 23:50:32 +00001406 !hasUnmodeledSideEffects())
Dan Gohman7c59ed62008-09-24 00:06:15 +00001407 return false;
1408
1409 // Otherwise, if the instruction has no memory reference information,
1410 // conservatively assume it wasn't preserved.
1411 if (memoperands_empty())
1412 return true;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001413
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001414 // Check the memory reference information for ordered references.
Dan Gohman48b185d2009-09-25 20:36:54 +00001415 for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I)
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001416 if (!(*I)->isUnordered())
Dan Gohman7c59ed62008-09-24 00:06:15 +00001417 return true;
1418
1419 return false;
1420}
1421
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001422/// isInvariantLoad - Return true if this instruction is loading from a
1423/// location whose value is invariant across the function. For example,
Dan Gohman4a618822010-02-10 16:03:48 +00001424/// loading a value from the constant pool or from the argument area
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001425/// of a function if it does not change. This should only return true of
1426/// *all* loads the instruction does are invariant (if it does multiple loads).
1427bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const {
1428 // If the instruction doesn't load at all, it isn't an invariant load.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001429 if (!mayLoad())
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001430 return false;
1431
1432 // If the instruction has lost its memoperands, conservatively assume that
1433 // it may not be an invariant load.
1434 if (memoperands_empty())
1435 return false;
1436
1437 const MachineFrameInfo *MFI = getParent()->getParent()->getFrameInfo();
1438
1439 for (mmo_iterator I = memoperands_begin(),
1440 E = memoperands_end(); I != E; ++I) {
1441 if ((*I)->isVolatile()) return false;
1442 if ((*I)->isStore()) return false;
Pete Cooper82cd9e82011-11-08 18:42:53 +00001443 if ((*I)->isInvariant()) return true;
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001444
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001445
1446 // A load from a constant PseudoSourceValue is invariant.
1447 if (const PseudoSourceValue *PSV = (*I)->getPseudoValue())
1448 if (PSV->isConstant(MFI))
1449 continue;
1450
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001451 if (const Value *V = (*I)->getValue()) {
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001452 // If we have an AliasAnalysis, ask it whether the memory is constant.
Dan Gohmana94cc6d2010-10-20 00:31:05 +00001453 if (AA && AA->pointsToConstantMemory(
1454 AliasAnalysis::Location(V, (*I)->getSize(),
Hal Finkelcc39b672014-07-24 12:16:19 +00001455 (*I)->getAAInfo())))
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001456 continue;
1457 }
1458
1459 // Otherwise assume conservatively.
1460 return false;
1461 }
1462
1463 // Everything checks out.
1464 return true;
1465}
1466
Evan Cheng71453822009-12-03 02:31:43 +00001467/// isConstantValuePHI - If the specified instruction is a PHI that always
1468/// merges together the same virtual register, return the register, otherwise
1469/// return 0.
1470unsigned MachineInstr::isConstantValuePHI() const {
Chris Lattnerb06015a2010-02-09 19:54:29 +00001471 if (!isPHI())
Evan Cheng71453822009-12-03 02:31:43 +00001472 return 0;
Evan Cheng5c668a22009-12-07 23:10:34 +00001473 assert(getNumOperands() >= 3 &&
1474 "It's illegal to have a PHI without source operands");
Evan Cheng71453822009-12-03 02:31:43 +00001475
1476 unsigned Reg = getOperand(1).getReg();
1477 for (unsigned i = 3, e = getNumOperands(); i < e; i += 2)
1478 if (getOperand(i).getReg() != Reg)
1479 return 0;
1480 return Reg;
1481}
1482
Evan Cheng6eb516d2011-01-07 23:50:32 +00001483bool MachineInstr::hasUnmodeledSideEffects() const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00001484 if (hasProperty(MCID::UnmodeledSideEffects))
Evan Cheng6eb516d2011-01-07 23:50:32 +00001485 return true;
1486 if (isInlineAsm()) {
1487 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1488 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1489 return true;
1490 }
1491
1492 return false;
1493}
1494
Evan Chengb083c472010-04-08 20:02:37 +00001495/// allDefsAreDead - Return true if all the defs of this instruction are dead.
1496///
1497bool MachineInstr::allDefsAreDead() const {
1498 for (unsigned i = 0, e = getNumOperands(); i < e; ++i) {
1499 const MachineOperand &MO = getOperand(i);
1500 if (!MO.isReg() || MO.isUse())
1501 continue;
1502 if (!MO.isDead())
1503 return false;
1504 }
1505 return true;
1506}
1507
Evan Cheng21eedfb2010-10-22 21:49:09 +00001508/// copyImplicitOps - Copy implicit register operands from specified
1509/// instruction to this instruction.
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001510void MachineInstr::copyImplicitOps(MachineFunction &MF,
1511 const MachineInstr *MI) {
Evan Cheng21eedfb2010-10-22 21:49:09 +00001512 for (unsigned i = MI->getDesc().getNumOperands(), e = MI->getNumOperands();
1513 i != e; ++i) {
1514 const MachineOperand &MO = MI->getOperand(i);
Lang Hames7c8189c2014-03-17 01:22:54 +00001515 if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask())
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001516 addOperand(MF, MO);
Evan Cheng21eedfb2010-10-22 21:49:09 +00001517 }
1518}
1519
Brian Gaekee8f7c2f2004-02-13 04:39:32 +00001520void MachineInstr::dump() const {
Manman Ren19f49ac2012-09-11 22:23:19 +00001521#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
David Greene29388d62010-01-04 23:48:20 +00001522 dbgs() << " " << *this;
Manman Ren742534c2012-09-06 19:06:06 +00001523#endif
Mon P Wangdfcc1ff2008-10-10 01:43:55 +00001524}
1525
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001526static void printDebugLoc(DebugLoc DL, const MachineFunction *MF,
Devang Patelc7285182010-06-29 21:51:32 +00001527 raw_ostream &CommentOS) {
1528 const LLVMContext &Ctx = MF->getFunction()->getContext();
Zinovy Nisda925c02014-05-07 09:51:22 +00001529 DL.print(Ctx, CommentOS);
Devang Patelc7285182010-06-29 21:51:32 +00001530}
1531
Andrew Trickb36388a2013-01-25 07:45:25 +00001532void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM,
1533 bool SkipOpers) const {
Dan Gohman2745d192009-11-09 19:38:45 +00001534 // We can be a bit tidier if we know the TargetMachine and/or MachineFunction.
Craig Topperc0196b12014-04-14 00:51:57 +00001535 const MachineFunction *MF = nullptr;
1536 const MachineRegisterInfo *MRI = nullptr;
Dan Gohman2745d192009-11-09 19:38:45 +00001537 if (const MachineBasicBlock *MBB = getParent()) {
1538 MF = MBB->getParent();
1539 if (!TM && MF)
1540 TM = &MF->getTarget();
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001541 if (MF)
1542 MRI = &MF->getRegInfo();
Dan Gohman2745d192009-11-09 19:38:45 +00001543 }
Dan Gohman34341e62009-10-31 20:19:03 +00001544
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001545 // Save a list of virtual registers.
1546 SmallVector<unsigned, 8> VirtRegs;
1547
Dan Gohman34341e62009-10-31 20:19:03 +00001548 // Print explicitly defined operands on the left of an assignment syntax.
Dan Gohman2745d192009-11-09 19:38:45 +00001549 unsigned StartOp = 0, e = getNumOperands();
Dan Gohman34341e62009-10-31 20:19:03 +00001550 for (; StartOp < e && getOperand(StartOp).isReg() &&
1551 getOperand(StartOp).isDef() &&
1552 !getOperand(StartOp).isImplicit();
1553 ++StartOp) {
1554 if (StartOp != 0) OS << ", ";
1555 getOperand(StartOp).print(OS, TM);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001556 unsigned Reg = getOperand(StartOp).getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001557 if (TargetRegisterInfo::isVirtualRegister(Reg))
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001558 VirtRegs.push_back(Reg);
Chris Lattnerac6e9742002-10-30 01:55:38 +00001559 }
Tanya Lattner23dbc812004-06-25 00:13:11 +00001560
Dan Gohman34341e62009-10-31 20:19:03 +00001561 if (StartOp != 0)
1562 OS << " = ";
1563
1564 // Print the opcode name.
Eric Christopherd9134482014-08-04 21:25:23 +00001565 if (TM && TM->getSubtargetImpl()->getInstrInfo())
1566 OS << TM->getSubtargetImpl()->getInstrInfo()->getName(getOpcode());
Benjamin Kramerbf152d52012-02-10 13:18:44 +00001567 else
1568 OS << "UNKNOWN";
Misha Brukman835702a2005-04-21 22:36:52 +00001569
Andrew Trickb36388a2013-01-25 07:45:25 +00001570 if (SkipOpers)
1571 return;
1572
Dan Gohman34341e62009-10-31 20:19:03 +00001573 // Print the rest of the operands.
Dan Gohman2745d192009-11-09 19:38:45 +00001574 bool OmittedAnyCallClobbers = false;
1575 bool FirstOp = true;
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001576 unsigned AsmDescOp = ~0u;
1577 unsigned AsmOpCount = 0;
Evan Cheng6eb516d2011-01-07 23:50:32 +00001578
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +00001579 if (isInlineAsm() && e >= InlineAsm::MIOp_FirstOperand) {
Evan Cheng6eb516d2011-01-07 23:50:32 +00001580 // Print asm string.
1581 OS << " ";
1582 getOperand(InlineAsm::MIOp_AsmString).print(OS, TM);
1583
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001584 // Print HasSideEffects, MayLoad, MayStore, IsAlignStack
Evan Cheng6eb516d2011-01-07 23:50:32 +00001585 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1586 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1587 OS << " [sideeffect]";
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001588 if (ExtraInfo & InlineAsm::Extra_MayLoad)
1589 OS << " [mayload]";
1590 if (ExtraInfo & InlineAsm::Extra_MayStore)
1591 OS << " [maystore]";
Evan Cheng6eb516d2011-01-07 23:50:32 +00001592 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
1593 OS << " [alignstack]";
Chad Rosiercbd2a192012-09-05 22:17:43 +00001594 if (getInlineAsmDialect() == InlineAsm::AD_ATT)
Chad Rosier994f4042012-09-05 21:00:58 +00001595 OS << " [attdialect]";
Chad Rosiercbd2a192012-09-05 22:17:43 +00001596 if (getInlineAsmDialect() == InlineAsm::AD_Intel)
Chad Rosier994f4042012-09-05 21:00:58 +00001597 OS << " [inteldialect]";
Evan Cheng6eb516d2011-01-07 23:50:32 +00001598
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001599 StartOp = AsmDescOp = InlineAsm::MIOp_FirstOperand;
Evan Cheng6eb516d2011-01-07 23:50:32 +00001600 FirstOp = false;
1601 }
1602
1603
Chris Lattnerac6e9742002-10-30 01:55:38 +00001604 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohman2745d192009-11-09 19:38:45 +00001605 const MachineOperand &MO = getOperand(i);
1606
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001607 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001608 VirtRegs.push_back(MO.getReg());
1609
Dan Gohman2745d192009-11-09 19:38:45 +00001610 // Omit call-clobbered registers which aren't used anywhere. This makes
1611 // call instructions much less noisy on targets where calls clobber lots
1612 // of registers. Don't rely on MO.isDead() because we may be called before
1613 // LiveVariables is run, or we may be looking at a non-allocatable reg.
Craig Toppercf0444b2014-11-17 05:50:14 +00001614 if (MRI && isCall() &&
Dan Gohman2745d192009-11-09 19:38:45 +00001615 MO.isReg() && MO.isImplicit() && MO.isDef()) {
1616 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001617 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Craig Toppercf0444b2014-11-17 05:50:14 +00001618 if (MRI->use_empty(Reg)) {
Dan Gohman2745d192009-11-09 19:38:45 +00001619 bool HasAliasLive = false;
Eric Christopherd9134482014-08-04 21:25:23 +00001620 for (MCRegAliasIterator AI(
1621 Reg, TM->getSubtargetImpl()->getRegisterInfo(), true);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001622 AI.isValid(); ++AI) {
1623 unsigned AliasReg = *AI;
Craig Toppercf0444b2014-11-17 05:50:14 +00001624 if (!MRI->use_empty(AliasReg)) {
Dan Gohman2745d192009-11-09 19:38:45 +00001625 HasAliasLive = true;
1626 break;
1627 }
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001628 }
Dan Gohman2745d192009-11-09 19:38:45 +00001629 if (!HasAliasLive) {
1630 OmittedAnyCallClobbers = true;
1631 continue;
1632 }
1633 }
1634 }
1635 }
1636
1637 if (FirstOp) FirstOp = false; else OS << ",";
Chris Lattnerac6e9742002-10-30 01:55:38 +00001638 OS << " ";
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001639 if (i < getDesc().NumOperands) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001640 const MCOperandInfo &MCOI = getDesc().OpInfo[i];
1641 if (MCOI.isPredicate())
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001642 OS << "pred:";
Evan Cheng6cc775f2011-06-28 19:10:37 +00001643 if (MCOI.isOptionalDef())
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001644 OS << "opt:";
1645 }
Evan Chengd4d1a512010-04-28 20:03:13 +00001646 if (isDebugValue() && MO.isMetadata()) {
1647 // Pretty print DBG_VALUE instructions.
1648 const MDNode *MD = MO.getMetadata();
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +00001649 DIDescriptor DI(MD);
1650 DIVariable DIV(MD);
1651
1652 if (DI.isVariable() && !DIV.getName().empty())
1653 OS << "!\"" << DIV.getName() << '\"';
Evan Chengd4d1a512010-04-28 20:03:13 +00001654 else
1655 MO.print(OS, TM);
Jakob Stoklund Olesenac0a2102010-07-04 23:24:23 +00001656 } else if (TM && (isInsertSubreg() || isRegSequence()) && MO.isImm()) {
Eric Christopherd9134482014-08-04 21:25:23 +00001657 OS << TM->getSubtargetImpl()->getRegisterInfo()->getSubRegIndexName(
1658 MO.getImm());
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001659 } else if (i == AsmDescOp && MO.isImm()) {
1660 // Pretty print the inline asm operand descriptor.
1661 OS << '$' << AsmOpCount++;
1662 unsigned Flag = MO.getImm();
1663 switch (InlineAsm::getKind(Flag)) {
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001664 case InlineAsm::Kind_RegUse: OS << ":[reguse"; break;
1665 case InlineAsm::Kind_RegDef: OS << ":[regdef"; break;
1666 case InlineAsm::Kind_RegDefEarlyClobber: OS << ":[regdef-ec"; break;
1667 case InlineAsm::Kind_Clobber: OS << ":[clobber"; break;
1668 case InlineAsm::Kind_Imm: OS << ":[imm"; break;
1669 case InlineAsm::Kind_Mem: OS << ":[mem"; break;
1670 default: OS << ":[??" << InlineAsm::getKind(Flag); break;
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001671 }
1672
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001673 unsigned RCID = 0;
Nick Lewycky84882252011-10-13 00:54:59 +00001674 if (InlineAsm::hasRegClassConstraint(Flag, RCID)) {
Craig Toppercf0444b2014-11-17 05:50:14 +00001675 if (TM) {
1676 const TargetRegisterInfo *TRI =
1677 TM->getSubtargetImpl()->getRegisterInfo();
Eric Christopherd9134482014-08-04 21:25:23 +00001678 OS << ':'
Craig Toppercf0444b2014-11-17 05:50:14 +00001679 << TRI->getRegClassName(TRI->getRegClass(RCID));
1680 } else
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001681 OS << ":RC" << RCID;
Nick Lewycky84882252011-10-13 00:54:59 +00001682 }
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001683
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001684 unsigned TiedTo = 0;
1685 if (InlineAsm::isUseOperandTiedToDef(Flag, TiedTo))
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001686 OS << " tiedto:$" << TiedTo;
1687
1688 OS << ']';
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001689
1690 // Compute the index of the next operand descriptor.
1691 AsmDescOp += 1 + InlineAsm::getNumOperandRegisters(Flag);
Evan Chengd4d1a512010-04-28 20:03:13 +00001692 } else
1693 MO.print(OS, TM);
Dan Gohman2745d192009-11-09 19:38:45 +00001694 }
1695
1696 // Briefly indicate whether any call clobbers were omitted.
1697 if (OmittedAnyCallClobbers) {
Bill Wendlingec030f22009-12-25 13:45:50 +00001698 if (!FirstOp) OS << ",";
Dan Gohman2745d192009-11-09 19:38:45 +00001699 OS << " ...";
Chris Lattner214808f2002-10-30 00:48:05 +00001700 }
Misha Brukman835702a2005-04-21 22:36:52 +00001701
Dan Gohman34341e62009-10-31 20:19:03 +00001702 bool HaveSemi = false;
Jakob Stoklund Olesen6922e9c2013-01-09 18:35:09 +00001703 const unsigned PrintableFlags = FrameSetup;
1704 if (Flags & PrintableFlags) {
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001705 if (!HaveSemi) OS << ";"; HaveSemi = true;
1706 OS << " flags: ";
1707
1708 if (Flags & FrameSetup)
1709 OS << "FrameSetup";
1710 }
1711
Dan Gohman3b460302008-07-07 23:14:23 +00001712 if (!memoperands_empty()) {
Dan Gohman34341e62009-10-31 20:19:03 +00001713 if (!HaveSemi) OS << ";"; HaveSemi = true;
1714
1715 OS << " mem:";
Dan Gohman48b185d2009-09-25 20:36:54 +00001716 for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
1717 i != e; ++i) {
1718 OS << **i;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001719 if (std::next(i) != e)
Dan Gohmanc0353bf2009-09-23 01:33:16 +00001720 OS << " ";
Dan Gohman2d489b52008-02-06 22:27:42 +00001721 }
1722 }
1723
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001724 // Print the regclass of any virtual registers encountered.
1725 if (MRI && !VirtRegs.empty()) {
1726 if (!HaveSemi) OS << ";"; HaveSemi = true;
1727 for (unsigned i = 0; i != VirtRegs.size(); ++i) {
1728 const TargetRegisterClass *RC = MRI->getRegClass(VirtRegs[i]);
Craig Toppercf0444b2014-11-17 05:50:14 +00001729 OS << " " << MRI->getTargetRegisterInfo()->getRegClassName(RC)
1730 << ':' << PrintReg(VirtRegs[i]);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001731 for (unsigned j = i+1; j != VirtRegs.size();) {
1732 if (MRI->getRegClass(VirtRegs[j]) != RC) {
1733 ++j;
1734 continue;
1735 }
1736 if (VirtRegs[i] != VirtRegs[j])
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +00001737 OS << "," << PrintReg(VirtRegs[j]);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001738 VirtRegs.erase(VirtRegs.begin()+j);
1739 }
1740 }
1741 }
1742
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001743 // Print debug location information.
Devang Pateld61b1d52011-08-04 20:44:26 +00001744 if (isDebugValue() && getOperand(e - 1).isMetadata()) {
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001745 if (!HaveSemi) OS << ";";
Devang Pateld61b1d52011-08-04 20:44:26 +00001746 DIVariable DV(getOperand(e - 1).getMetadata());
1747 OS << " line no:" << DV.getLineNumber();
1748 if (MDNode *InlinedAt = DV.getInlinedAt()) {
1749 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
Zinovy Nisda925c02014-05-07 09:51:22 +00001750 if (!InlinedAtDL.isUnknown() && MF) {
Devang Pateld61b1d52011-08-04 20:44:26 +00001751 OS << " inlined @[ ";
1752 printDebugLoc(InlinedAtDL, MF, OS);
1753 OS << " ]";
1754 }
1755 }
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001756 if (isIndirectDebugValue())
1757 OS << " indirect";
Devang Pateld61b1d52011-08-04 20:44:26 +00001758 } else if (!debugLoc.isUnknown() && MF) {
Arnaud A. de Grandmaison75c9e6d2014-03-15 22:13:15 +00001759 if (!HaveSemi) OS << ";";
Dan Gohman2e3f1872009-11-23 21:29:08 +00001760 OS << " dbg:";
Devang Patelc7285182010-06-29 21:51:32 +00001761 printDebugLoc(debugLoc, MF, OS);
Bill Wendling1a0a3d02009-02-19 21:44:55 +00001762 }
1763
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001764 OS << '\n';
Chris Lattner214808f2002-10-30 00:48:05 +00001765}
1766
Owen Anderson2a8a4852008-01-24 01:10:07 +00001767bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001768 const TargetRegisterInfo *RegInfo,
Owen Anderson2a8a4852008-01-24 01:10:07 +00001769 bool AddIfNotFound) {
Evan Cheng6c177732008-04-16 09:41:59 +00001770 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001771 bool hasAliases = isPhysReg &&
1772 MCRegAliasIterator(IncomingReg, RegInfo, false).isValid();
Dan Gohmanc7367b42008-09-03 15:56:16 +00001773 bool Found = false;
Evan Cheng6c177732008-04-16 09:41:59 +00001774 SmallVector<unsigned,4> DeadOps;
Bill Wendling7921ad02008-03-03 22:14:33 +00001775 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1776 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesenf465f062009-08-04 20:09:25 +00001777 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng6c177732008-04-16 09:41:59 +00001778 continue;
1779 unsigned Reg = MO.getReg();
1780 if (!Reg)
1781 continue;
Bill Wendling7921ad02008-03-03 22:14:33 +00001782
Evan Cheng6c177732008-04-16 09:41:59 +00001783 if (Reg == IncomingReg) {
Dan Gohmanc7367b42008-09-03 15:56:16 +00001784 if (!Found) {
1785 if (MO.isKill())
1786 // The register is already marked kill.
1787 return true;
Jakob Stoklund Olesenc59cd9b2009-08-02 19:13:03 +00001788 if (isPhysReg && isRegTiedToDefOperand(i))
1789 // Two-address uses of physregs must not be marked kill.
1790 return true;
Dan Gohmanc7367b42008-09-03 15:56:16 +00001791 MO.setIsKill();
1792 Found = true;
1793 }
1794 } else if (hasAliases && MO.isKill() &&
1795 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng6c177732008-04-16 09:41:59 +00001796 // A super-register kill already exists.
1797 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohmanb2612922008-07-03 01:18:51 +00001798 return true;
1799 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng6c177732008-04-16 09:41:59 +00001800 DeadOps.push_back(i);
Bill Wendling7921ad02008-03-03 22:14:33 +00001801 }
1802 }
1803
Evan Cheng6c177732008-04-16 09:41:59 +00001804 // Trim unneeded kill operands.
1805 while (!DeadOps.empty()) {
1806 unsigned OpIdx = DeadOps.back();
1807 if (getOperand(OpIdx).isImplicit())
1808 RemoveOperand(OpIdx);
1809 else
1810 getOperand(OpIdx).setIsKill(false);
1811 DeadOps.pop_back();
1812 }
1813
Bill Wendling7921ad02008-03-03 22:14:33 +00001814 // If not found, this means an alias of one of the operands is killed. Add a
Owen Anderson2a8a4852008-01-24 01:10:07 +00001815 // new implicit operand if required.
Dan Gohmanc7367b42008-09-03 15:56:16 +00001816 if (!Found && AddIfNotFound) {
Bill Wendling7921ad02008-03-03 22:14:33 +00001817 addOperand(MachineOperand::CreateReg(IncomingReg,
1818 false /*IsDef*/,
1819 true /*IsImp*/,
1820 true /*IsKill*/));
Owen Anderson2a8a4852008-01-24 01:10:07 +00001821 return true;
1822 }
Dan Gohmanc7367b42008-09-03 15:56:16 +00001823 return Found;
Owen Anderson2a8a4852008-01-24 01:10:07 +00001824}
1825
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00001826void MachineInstr::clearRegisterKills(unsigned Reg,
1827 const TargetRegisterInfo *RegInfo) {
1828 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topperc0196b12014-04-14 00:51:57 +00001829 RegInfo = nullptr;
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00001830 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1831 MachineOperand &MO = getOperand(i);
1832 if (!MO.isReg() || !MO.isUse() || !MO.isKill())
1833 continue;
1834 unsigned OpReg = MO.getReg();
1835 if (OpReg == Reg || (RegInfo && RegInfo->isSuperRegister(Reg, OpReg)))
1836 MO.setIsKill(false);
1837 }
1838}
1839
Matthias Braun1965bfa2013-10-10 21:28:38 +00001840bool MachineInstr::addRegisterDead(unsigned Reg,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001841 const TargetRegisterInfo *RegInfo,
Owen Anderson2a8a4852008-01-24 01:10:07 +00001842 bool AddIfNotFound) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00001843 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(Reg);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001844 bool hasAliases = isPhysReg &&
Matthias Braun1965bfa2013-10-10 21:28:38 +00001845 MCRegAliasIterator(Reg, RegInfo, false).isValid();
Dan Gohmanc7367b42008-09-03 15:56:16 +00001846 bool Found = false;
Evan Cheng6c177732008-04-16 09:41:59 +00001847 SmallVector<unsigned,4> DeadOps;
Owen Anderson2a8a4852008-01-24 01:10:07 +00001848 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1849 MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001850 if (!MO.isReg() || !MO.isDef())
Evan Cheng6c177732008-04-16 09:41:59 +00001851 continue;
Matthias Braun1965bfa2013-10-10 21:28:38 +00001852 unsigned MOReg = MO.getReg();
1853 if (!MOReg)
Dan Gohmanc7367b42008-09-03 15:56:16 +00001854 continue;
1855
Matthias Braun1965bfa2013-10-10 21:28:38 +00001856 if (MOReg == Reg) {
Jakob Stoklund Olesen76ad3de2011-04-05 16:53:50 +00001857 MO.setIsDead();
1858 Found = true;
Dan Gohmanc7367b42008-09-03 15:56:16 +00001859 } else if (hasAliases && MO.isDead() &&
Matthias Braun1965bfa2013-10-10 21:28:38 +00001860 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
Evan Cheng6c177732008-04-16 09:41:59 +00001861 // There exists a super-register that's marked dead.
Matthias Braun1965bfa2013-10-10 21:28:38 +00001862 if (RegInfo->isSuperRegister(Reg, MOReg))
Dan Gohmanb2612922008-07-03 01:18:51 +00001863 return true;
Matthias Braun1965bfa2013-10-10 21:28:38 +00001864 if (RegInfo->isSubRegister(Reg, MOReg))
Evan Cheng6c177732008-04-16 09:41:59 +00001865 DeadOps.push_back(i);
Owen Anderson2a8a4852008-01-24 01:10:07 +00001866 }
1867 }
1868
Evan Cheng6c177732008-04-16 09:41:59 +00001869 // Trim unneeded dead operands.
1870 while (!DeadOps.empty()) {
1871 unsigned OpIdx = DeadOps.back();
1872 if (getOperand(OpIdx).isImplicit())
1873 RemoveOperand(OpIdx);
1874 else
1875 getOperand(OpIdx).setIsDead(false);
1876 DeadOps.pop_back();
1877 }
1878
Dan Gohmanc7367b42008-09-03 15:56:16 +00001879 // If not found, this means an alias of one of the operands is dead. Add a
1880 // new implicit operand if required.
Chris Lattnerfd682802009-06-24 17:54:48 +00001881 if (Found || !AddIfNotFound)
1882 return Found;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001883
Matthias Braun1965bfa2013-10-10 21:28:38 +00001884 addOperand(MachineOperand::CreateReg(Reg,
Chris Lattnerfd682802009-06-24 17:54:48 +00001885 true /*IsDef*/,
1886 true /*IsImp*/,
1887 false /*IsKill*/,
1888 true /*IsDead*/));
1889 return true;
Owen Anderson2a8a4852008-01-24 01:10:07 +00001890}
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00001891
Matthias Braun1965bfa2013-10-10 21:28:38 +00001892void MachineInstr::addRegisterDefined(unsigned Reg,
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00001893 const TargetRegisterInfo *RegInfo) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00001894 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1895 MachineOperand *MO = findRegisterDefOperand(Reg, false, RegInfo);
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00001896 if (MO)
1897 return;
1898 } else {
1899 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1900 const MachineOperand &MO = getOperand(i);
Matthias Braun1965bfa2013-10-10 21:28:38 +00001901 if (MO.isReg() && MO.getReg() == Reg && MO.isDef() &&
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00001902 MO.getSubReg() == 0)
1903 return;
1904 }
1905 }
Matthias Braun1965bfa2013-10-10 21:28:38 +00001906 addOperand(MachineOperand::CreateReg(Reg,
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00001907 true /*IsDef*/,
1908 true /*IsImp*/));
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00001909}
Evan Cheng59d27fe2010-03-03 23:37:30 +00001910
Jakob Stoklund Olesen4290be42012-02-03 20:43:39 +00001911void MachineInstr::setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs,
Dan Gohman86936502010-06-18 23:28:01 +00001912 const TargetRegisterInfo &TRI) {
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00001913 bool HasRegMask = false;
Dan Gohman86936502010-06-18 23:28:01 +00001914 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1915 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00001916 if (MO.isRegMask()) {
1917 HasRegMask = true;
1918 continue;
1919 }
Dan Gohman86936502010-06-18 23:28:01 +00001920 if (!MO.isReg() || !MO.isDef()) continue;
1921 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenf6507322012-02-03 20:43:35 +00001922 if (!TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Dan Gohman86936502010-06-18 23:28:01 +00001923 bool Dead = true;
Jakob Stoklund Olesen4290be42012-02-03 20:43:39 +00001924 for (ArrayRef<unsigned>::iterator I = UsedRegs.begin(), E = UsedRegs.end();
1925 I != E; ++I)
Dan Gohman86936502010-06-18 23:28:01 +00001926 if (TRI.regsOverlap(*I, Reg)) {
1927 Dead = false;
1928 break;
1929 }
1930 // If there are no uses, including partial uses, the def is dead.
1931 if (Dead) MO.setIsDead();
1932 }
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00001933
1934 // This is a call with a register mask operand.
1935 // Mask clobbers are always dead, so add defs for the non-dead defines.
1936 if (HasRegMask)
1937 for (ArrayRef<unsigned>::iterator I = UsedRegs.begin(), E = UsedRegs.end();
1938 I != E; ++I)
1939 addRegisterDefined(*I, &TRI);
Dan Gohman86936502010-06-18 23:28:01 +00001940}
1941
Evan Cheng59d27fe2010-03-03 23:37:30 +00001942unsigned
1943MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) {
Chandler Carruth962152c2012-03-07 09:39:46 +00001944 // Build up a buffer of hash code components.
Chandler Carruth962152c2012-03-07 09:39:46 +00001945 SmallVector<size_t, 8> HashComponents;
1946 HashComponents.reserve(MI->getNumOperands() + 1);
1947 HashComponents.push_back(MI->getOpcode());
Evan Cheng59d27fe2010-03-03 23:37:30 +00001948 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1949 const MachineOperand &MO = MI->getOperand(i);
Chandler Carruth264854f2012-07-05 11:06:22 +00001950 if (MO.isReg() && MO.isDef() &&
1951 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
1952 continue; // Skip virtual register defs.
1953
1954 HashComponents.push_back(hash_value(MO));
Evan Cheng59d27fe2010-03-03 23:37:30 +00001955 }
Chandler Carruth962152c2012-03-07 09:39:46 +00001956 return hash_combine_range(HashComponents.begin(), HashComponents.end());
Evan Cheng59d27fe2010-03-03 23:37:30 +00001957}
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00001958
1959void MachineInstr::emitError(StringRef Msg) const {
1960 // Find the source location cookie.
1961 unsigned LocCookie = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00001962 const MDNode *LocMD = nullptr;
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00001963 for (unsigned i = getNumOperands(); i != 0; --i) {
1964 if (getOperand(i-1).isMetadata() &&
1965 (LocMD = getOperand(i-1).getMetadata()) &&
1966 LocMD->getNumOperands() != 0) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001967 if (const ConstantInt *CI =
1968 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00001969 LocCookie = CI->getZExtValue();
1970 break;
1971 }
1972 }
1973 }
1974
1975 if (const MachineBasicBlock *MBB = getParent())
1976 if (const MachineFunction *MF = MBB->getParent())
1977 return MF->getMMI().getModule()->getContext().emitError(LocCookie, Msg);
1978 report_fatal_error(Msg);
1979}