blob: a3560a77b65554ba2c10cc802ae566e9e5918ef8 [file] [log] [blame]
Chris Lattnere138b3d2008-01-01 20:36:19 +00001//===-- lib/CodeGen/MachineInstr.cpp --------------------------------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Brian Gaeke21326fc2004-02-13 04:39:32 +00009//
10// Methods common to all machine instructions.
11//
Chris Lattner035dfbe2002-08-09 20:08:06 +000012//===----------------------------------------------------------------------===//
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000013
Chris Lattner822b4fb2001-09-07 17:18:30 +000014#include "llvm/CodeGen/MachineInstr.h"
Evan Chengfb112882009-03-23 08:01:15 +000015#include "llvm/Constants.h"
16#include "llvm/InlineAsm.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000017#include "llvm/Value.h"
Dan Gohmancd26ec52009-09-23 01:33:16 +000018#include "llvm/Assembly/Writer.h"
Chris Lattner8517e1f2004-02-19 16:17:08 +000019#include "llvm/CodeGen/MachineFunction.h"
Dan Gohmanc76909a2009-09-25 20:36:54 +000020#include "llvm/CodeGen/MachineMemOperand.h"
Chris Lattner62ed6b92008-01-01 01:12:31 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000022#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner10491642002-10-30 00:48:05 +000023#include "llvm/Target/TargetMachine.h"
Evan Chengbb81d972008-01-31 09:59:15 +000024#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerf14cf852008-01-07 07:42:25 +000025#include "llvm/Target/TargetInstrDesc.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000026#include "llvm/Target/TargetRegisterInfo.h"
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +000027#include "llvm/Analysis/DebugInfo.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000028#include "llvm/Support/ErrorHandling.h"
Dan Gohman2c3f7ae2008-07-17 23:49:46 +000029#include "llvm/Support/LeakDetector.h"
Dan Gohmance42e402008-07-07 20:32:02 +000030#include "llvm/Support/MathExtras.h"
Chris Lattneredfb72c2008-08-24 20:37:32 +000031#include "llvm/Support/raw_ostream.h"
Dan Gohmanb8d2f552008-08-20 15:58:01 +000032#include "llvm/ADT/FoldingSet.h"
Chris Lattner0742b592004-02-23 18:38:20 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattnerf7382302007-12-30 21:56:09 +000035//===----------------------------------------------------------------------===//
36// MachineOperand Implementation
37//===----------------------------------------------------------------------===//
38
Chris Lattner62ed6b92008-01-01 01:12:31 +000039/// AddRegOperandToRegInfo - Add this register operand to the specified
40/// MachineRegisterInfo. If it is null, then the next/prev fields should be
41/// explicitly nulled out.
42void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) {
Dan Gohmand735b802008-10-03 15:45:36 +000043 assert(isReg() && "Can only add reg operand to use lists");
Chris Lattner62ed6b92008-01-01 01:12:31 +000044
45 // If the reginfo pointer is null, just explicitly null out or next/prev
46 // pointers, to ensure they are not garbage.
47 if (RegInfo == 0) {
48 Contents.Reg.Prev = 0;
49 Contents.Reg.Next = 0;
50 return;
51 }
52
53 // Otherwise, add this operand to the head of the registers use/def list.
Chris Lattner80fe5312008-01-01 21:08:22 +000054 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg());
Chris Lattner62ed6b92008-01-01 01:12:31 +000055
Chris Lattner80fe5312008-01-01 21:08:22 +000056 // For SSA values, we prefer to keep the definition at the start of the list.
57 // we do this by skipping over the definition if it is at the head of the
58 // list.
59 if (*Head && (*Head)->isDef())
60 Head = &(*Head)->Contents.Reg.Next;
61
62 Contents.Reg.Next = *Head;
Chris Lattner62ed6b92008-01-01 01:12:31 +000063 if (Contents.Reg.Next) {
64 assert(getReg() == Contents.Reg.Next->getReg() &&
65 "Different regs on the same list!");
66 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next;
67 }
68
Chris Lattner80fe5312008-01-01 21:08:22 +000069 Contents.Reg.Prev = Head;
70 *Head = this;
Chris Lattner62ed6b92008-01-01 01:12:31 +000071}
72
Dan Gohman3bc1a372009-04-15 01:17:37 +000073/// RemoveRegOperandFromRegInfo - Remove this register operand from the
74/// MachineRegisterInfo it is linked with.
75void MachineOperand::RemoveRegOperandFromRegInfo() {
76 assert(isOnRegUseList() && "Reg operand is not on a use list");
77 // Unlink this from the doubly linked list of operands.
78 MachineOperand *NextOp = Contents.Reg.Next;
79 *Contents.Reg.Prev = NextOp;
80 if (NextOp) {
81 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!");
82 NextOp->Contents.Reg.Prev = Contents.Reg.Prev;
83 }
84 Contents.Reg.Prev = 0;
85 Contents.Reg.Next = 0;
86}
87
Chris Lattner62ed6b92008-01-01 01:12:31 +000088void MachineOperand::setReg(unsigned Reg) {
89 if (getReg() == Reg) return; // No change.
90
91 // Otherwise, we have to change the register. If this operand is embedded
92 // into a machine function, we need to update the old and new register's
93 // use/def lists.
94 if (MachineInstr *MI = getParent())
95 if (MachineBasicBlock *MBB = MI->getParent())
96 if (MachineFunction *MF = MBB->getParent()) {
97 RemoveRegOperandFromRegInfo();
98 Contents.Reg.RegNo = Reg;
99 AddRegOperandToRegInfo(&MF->getRegInfo());
100 return;
101 }
102
103 // Otherwise, just change the register, no problem. :)
104 Contents.Reg.RegNo = Reg;
105}
106
107/// ChangeToImmediate - Replace this operand with a new immediate operand of
108/// the specified value. If an operand is known to be an immediate already,
109/// the setImm method should be used.
110void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
111 // If this operand is currently a register operand, and if this is in a
112 // function, deregister the operand from the register's use/def list.
Dan Gohmand735b802008-10-03 15:45:36 +0000113 if (isReg() && getParent() && getParent()->getParent() &&
Chris Lattner62ed6b92008-01-01 01:12:31 +0000114 getParent()->getParent()->getParent())
115 RemoveRegOperandFromRegInfo();
116
117 OpKind = MO_Immediate;
118 Contents.ImmVal = ImmVal;
119}
120
121/// ChangeToRegister - Replace this operand with a new register operand of
122/// the specified value. If an operand is known to be an register already,
123/// the setReg method should be used.
124void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
Evan Cheng4784f1f2009-06-30 08:49:04 +0000125 bool isKill, bool isDead, bool isUndef) {
Chris Lattner62ed6b92008-01-01 01:12:31 +0000126 // If this operand is already a register operand, use setReg to update the
127 // register's use/def lists.
Dan Gohmand735b802008-10-03 15:45:36 +0000128 if (isReg()) {
Dale Johannesene0091802008-09-14 01:44:36 +0000129 assert(!isEarlyClobber());
Chris Lattner62ed6b92008-01-01 01:12:31 +0000130 setReg(Reg);
131 } else {
132 // Otherwise, change this to a register and set the reg#.
133 OpKind = MO_Register;
134 Contents.Reg.RegNo = Reg;
135
136 // If this operand is embedded in a function, add the operand to the
137 // register's use/def list.
138 if (MachineInstr *MI = getParent())
139 if (MachineBasicBlock *MBB = MI->getParent())
140 if (MachineFunction *MF = MBB->getParent())
141 AddRegOperandToRegInfo(&MF->getRegInfo());
142 }
143
144 IsDef = isDef;
145 IsImp = isImp;
146 IsKill = isKill;
147 IsDead = isDead;
Evan Cheng4784f1f2009-06-30 08:49:04 +0000148 IsUndef = isUndef;
Dale Johannesene0091802008-09-14 01:44:36 +0000149 IsEarlyClobber = false;
Chris Lattner62ed6b92008-01-01 01:12:31 +0000150 SubReg = 0;
151}
152
Chris Lattnerf7382302007-12-30 21:56:09 +0000153/// isIdenticalTo - Return true if this operand is identical to the specified
154/// operand.
155bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
Chris Lattner31530612009-06-24 17:54:48 +0000156 if (getType() != Other.getType() ||
157 getTargetFlags() != Other.getTargetFlags())
158 return false;
Chris Lattnerf7382302007-12-30 21:56:09 +0000159
160 switch (getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000161 default: llvm_unreachable("Unrecognized operand type");
Chris Lattnerf7382302007-12-30 21:56:09 +0000162 case MachineOperand::MO_Register:
163 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
164 getSubReg() == Other.getSubReg();
165 case MachineOperand::MO_Immediate:
166 return getImm() == Other.getImm();
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000167 case MachineOperand::MO_FPImmediate:
168 return getFPImm() == Other.getFPImm();
Chris Lattnerf7382302007-12-30 21:56:09 +0000169 case MachineOperand::MO_MachineBasicBlock:
170 return getMBB() == Other.getMBB();
171 case MachineOperand::MO_FrameIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000172 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000173 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000174 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattnerf7382302007-12-30 21:56:09 +0000175 case MachineOperand::MO_JumpTableIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000176 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000177 case MachineOperand::MO_GlobalAddress:
178 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
179 case MachineOperand::MO_ExternalSymbol:
180 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
181 getOffset() == Other.getOffset();
182 }
183}
184
185/// print - Print the specified machine operand.
186///
Mon P Wang5ca6bd12008-10-10 01:43:55 +0000187void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const {
Chris Lattnerf7382302007-12-30 21:56:09 +0000188 switch (getType()) {
189 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000190 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000191 OS << "%reg" << getReg();
192 } else {
193 // If the instruction is embedded into a basic block, we can find the
Chris Lattner62ed6b92008-01-01 01:12:31 +0000194 // target info for the instruction.
Chris Lattnerf7382302007-12-30 21:56:09 +0000195 if (TM == 0)
196 if (const MachineInstr *MI = getParent())
197 if (const MachineBasicBlock *MBB = MI->getParent())
198 if (const MachineFunction *MF = MBB->getParent())
199 TM = &MF->getTarget();
200
201 if (TM)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000202 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
Chris Lattnerf7382302007-12-30 21:56:09 +0000203 else
204 OS << "%mreg" << getReg();
205 }
Dan Gohman2ccc8392008-12-18 21:51:27 +0000206
Evan Cheng4784f1f2009-06-30 08:49:04 +0000207 if (getSubReg() != 0)
Chris Lattner31530612009-06-24 17:54:48 +0000208 OS << ':' << getSubReg();
Dan Gohman2ccc8392008-12-18 21:51:27 +0000209
Evan Cheng4784f1f2009-06-30 08:49:04 +0000210 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
211 isEarlyClobber()) {
Chris Lattner31530612009-06-24 17:54:48 +0000212 OS << '<';
Chris Lattnerf7382302007-12-30 21:56:09 +0000213 bool NeedComma = false;
214 if (isImplicit()) {
Chris Lattner31530612009-06-24 17:54:48 +0000215 if (NeedComma) OS << ',';
Chris Lattnerf7382302007-12-30 21:56:09 +0000216 OS << (isDef() ? "imp-def" : "imp-use");
217 NeedComma = true;
218 } else if (isDef()) {
Chris Lattner31530612009-06-24 17:54:48 +0000219 if (NeedComma) OS << ',';
Dale Johannesen913d3df2008-09-12 17:49:03 +0000220 if (isEarlyClobber())
221 OS << "earlyclobber,";
Chris Lattnerf7382302007-12-30 21:56:09 +0000222 OS << "def";
223 NeedComma = true;
224 }
Evan Cheng4784f1f2009-06-30 08:49:04 +0000225 if (isKill() || isDead() || isUndef()) {
Chris Lattner31530612009-06-24 17:54:48 +0000226 if (NeedComma) OS << ',';
Bill Wendling181eb732008-02-24 00:56:13 +0000227 if (isKill()) OS << "kill";
228 if (isDead()) OS << "dead";
Evan Cheng4784f1f2009-06-30 08:49:04 +0000229 if (isUndef()) {
230 if (isKill() || isDead())
231 OS << ',';
232 OS << "undef";
233 }
Chris Lattnerf7382302007-12-30 21:56:09 +0000234 }
Chris Lattner31530612009-06-24 17:54:48 +0000235 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000236 }
237 break;
238 case MachineOperand::MO_Immediate:
239 OS << getImm();
240 break;
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000241 case MachineOperand::MO_FPImmediate:
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000242 if (getFPImm()->getType()->isFloatTy())
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000243 OS << getFPImm()->getValueAPF().convertToFloat();
Chris Lattner31530612009-06-24 17:54:48 +0000244 else
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000245 OS << getFPImm()->getValueAPF().convertToDouble();
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000246 break;
Chris Lattnerf7382302007-12-30 21:56:09 +0000247 case MachineOperand::MO_MachineBasicBlock:
248 OS << "mbb<"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000249 << ((Value*)getMBB()->getBasicBlock())->getName()
Chris Lattner31530612009-06-24 17:54:48 +0000250 << "," << (void*)getMBB() << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000251 break;
252 case MachineOperand::MO_FrameIndex:
Chris Lattner31530612009-06-24 17:54:48 +0000253 OS << "<fi#" << getIndex() << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000254 break;
255 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000256 OS << "<cp#" << getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000257 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000258 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000259 break;
260 case MachineOperand::MO_JumpTableIndex:
Chris Lattner31530612009-06-24 17:54:48 +0000261 OS << "<jt#" << getIndex() << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000262 break;
263 case MachineOperand::MO_GlobalAddress:
264 OS << "<ga:" << ((Value*)getGlobal())->getName();
265 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000266 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000267 break;
268 case MachineOperand::MO_ExternalSymbol:
269 OS << "<es:" << getSymbolName();
270 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000271 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000272 break;
273 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000274 llvm_unreachable("Unrecognized operand type");
Chris Lattnerf7382302007-12-30 21:56:09 +0000275 }
Chris Lattner31530612009-06-24 17:54:48 +0000276
277 if (unsigned TF = getTargetFlags())
278 OS << "[TF=" << TF << ']';
Chris Lattnerf7382302007-12-30 21:56:09 +0000279}
280
281//===----------------------------------------------------------------------===//
Dan Gohmance42e402008-07-07 20:32:02 +0000282// MachineMemOperand Implementation
283//===----------------------------------------------------------------------===//
284
285MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f,
286 int64_t o, uint64_t s, unsigned int a)
287 : Offset(o), Size(s), V(v),
288 Flags((f & 7) | ((Log2_32(a) + 1) << 3)) {
Dan Gohman28f02fd2009-09-21 19:47:04 +0000289 assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
Dan Gohmanc5e1f982008-07-16 15:56:42 +0000290 assert((isLoad() || isStore()) && "Not a load/store!");
Dan Gohmance42e402008-07-07 20:32:02 +0000291}
292
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000293/// Profile - Gather unique data for the object.
294///
295void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
296 ID.AddInteger(Offset);
297 ID.AddInteger(Size);
298 ID.AddPointer(V);
299 ID.AddInteger(Flags);
300}
301
Dan Gohmanc76909a2009-09-25 20:36:54 +0000302void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
303 // The Value and Offset may differ due to CSE. But the flags and size
304 // should be the same.
305 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
306 assert(MMO->getSize() == getSize() && "Size mismatch!");
307
308 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
309 // Update the alignment value.
310 Flags = (Flags & 7) | ((Log2_32(MMO->getBaseAlignment()) + 1) << 3);
311 // Also update the base and offset, because the new alignment may
312 // not be applicable with the old ones.
313 V = MMO->getValue();
314 Offset = MMO->getOffset();
315 }
316}
317
Dan Gohman4b2ebc12009-09-25 23:33:20 +0000318/// getAlignment - Return the minimum known alignment in bytes of the
319/// actual memory reference.
320uint64_t MachineMemOperand::getAlignment() const {
321 return MinAlign(getBaseAlignment(), getOffset());
322}
323
Dan Gohmanc76909a2009-09-25 20:36:54 +0000324raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) {
325 assert((MMO.isLoad() || MMO.isStore()) &&
Dan Gohmancd26ec52009-09-23 01:33:16 +0000326 "SV has to be a load, store or both.");
327
Dan Gohmanc76909a2009-09-25 20:36:54 +0000328 if (MMO.isVolatile())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000329 OS << "Volatile ";
330
Dan Gohmanc76909a2009-09-25 20:36:54 +0000331 if (MMO.isLoad())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000332 OS << "LD";
Dan Gohmanc76909a2009-09-25 20:36:54 +0000333 if (MMO.isStore())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000334 OS << "ST";
Dan Gohmanc76909a2009-09-25 20:36:54 +0000335 OS << MMO.getSize();
Dan Gohmancd26ec52009-09-23 01:33:16 +0000336
337 // Print the address information.
338 OS << "[";
Dan Gohmanc76909a2009-09-25 20:36:54 +0000339 if (!MMO.getValue())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000340 OS << "<unknown>";
341 else
Dan Gohmanc76909a2009-09-25 20:36:54 +0000342 WriteAsOperand(OS, MMO.getValue(), /*PrintType=*/false);
Dan Gohmancd26ec52009-09-23 01:33:16 +0000343
344 // If the alignment of the memory reference itself differs from the alignment
345 // of the base pointer, print the base alignment explicitly, next to the base
346 // pointer.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000347 if (MMO.getBaseAlignment() != MMO.getAlignment())
348 OS << "(align=" << MMO.getBaseAlignment() << ")";
Dan Gohmancd26ec52009-09-23 01:33:16 +0000349
Dan Gohmanc76909a2009-09-25 20:36:54 +0000350 if (MMO.getOffset() != 0)
351 OS << "+" << MMO.getOffset();
Dan Gohmancd26ec52009-09-23 01:33:16 +0000352 OS << "]";
353
354 // Print the alignment of the reference.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000355 if (MMO.getBaseAlignment() != MMO.getAlignment() ||
356 MMO.getBaseAlignment() != MMO.getSize())
357 OS << "(align=" << MMO.getAlignment() << ")";
Dan Gohmancd26ec52009-09-23 01:33:16 +0000358
359 return OS;
360}
361
Dan Gohmance42e402008-07-07 20:32:02 +0000362//===----------------------------------------------------------------------===//
Chris Lattnerf7382302007-12-30 21:56:09 +0000363// MachineInstr Implementation
364//===----------------------------------------------------------------------===//
365
Evan Chengc0f64ff2006-11-27 23:37:22 +0000366/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
Evan Cheng67f660c2006-11-30 07:08:44 +0000367/// TID NULL and no operands.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000368MachineInstr::MachineInstr()
Dan Gohmanc76909a2009-09-25 20:36:54 +0000369 : TID(0), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0),
370 Parent(0), debugLoc(DebugLoc::getUnknownLoc()) {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000371 // Make sure that we get added to a machine basicblock
372 LeakDetector::addGarbageObject(this);
Chris Lattner72791222002-10-28 20:59:49 +0000373}
374
Evan Cheng67f660c2006-11-30 07:08:44 +0000375void MachineInstr::addImplicitDefUseOperands() {
376 if (TID->ImplicitDefs)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000377 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Chris Lattner8019f412007-12-30 00:41:17 +0000378 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng67f660c2006-11-30 07:08:44 +0000379 if (TID->ImplicitUses)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000380 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Chris Lattner8019f412007-12-30 00:41:17 +0000381 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
Evan Chengd7de4962006-11-13 23:34:06 +0000382}
383
384/// MachineInstr ctor - This constructor create a MachineInstr and add the
Evan Chengc0f64ff2006-11-27 23:37:22 +0000385/// implicit operands. It reserves space for number of operands specified by
Chris Lattner749c6f62008-01-07 07:27:27 +0000386/// TargetInstrDesc or the numOperands if it is not zero. (for
Evan Chengc0f64ff2006-11-27 23:37:22 +0000387/// instructions with variable number of operands).
Chris Lattner749c6f62008-01-07 07:27:27 +0000388MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp)
Dan Gohmanc76909a2009-09-25 20:36:54 +0000389 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0), Parent(0),
Dale Johannesen06efc022009-01-27 23:20:29 +0000390 debugLoc(DebugLoc::getUnknownLoc()) {
Chris Lattner349c4952008-01-07 03:13:06 +0000391 if (!NoImp && TID->getImplicitDefs())
392 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000393 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000394 if (!NoImp && TID->getImplicitUses())
395 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000396 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000397 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Chengfa945722007-10-13 02:23:01 +0000398 if (!NoImp)
399 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000400 // Make sure that we get added to a machine basicblock
401 LeakDetector::addGarbageObject(this);
Evan Chengd7de4962006-11-13 23:34:06 +0000402}
403
Dale Johannesen06efc022009-01-27 23:20:29 +0000404/// MachineInstr ctor - As above, but with a DebugLoc.
405MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl,
406 bool NoImp)
Dan Gohmanc76909a2009-09-25 20:36:54 +0000407 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0),
408 Parent(0), debugLoc(dl) {
Dale Johannesen06efc022009-01-27 23:20:29 +0000409 if (!NoImp && TID->getImplicitDefs())
410 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
411 NumImplicitOps++;
412 if (!NoImp && TID->getImplicitUses())
413 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
414 NumImplicitOps++;
415 Operands.reserve(NumImplicitOps + TID->getNumOperands());
416 if (!NoImp)
417 addImplicitDefUseOperands();
418 // Make sure that we get added to a machine basicblock
419 LeakDetector::addGarbageObject(this);
420}
421
422/// MachineInstr ctor - Work exactly the same as the ctor two above, except
423/// that the MachineInstr is created and added to the end of the specified
424/// basic block.
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000425///
Dale Johannesen06efc022009-01-27 23:20:29 +0000426MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid)
Dan Gohmanc76909a2009-09-25 20:36:54 +0000427 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0), Parent(0),
Dale Johannesen06efc022009-01-27 23:20:29 +0000428 debugLoc(DebugLoc::getUnknownLoc()) {
429 assert(MBB && "Cannot use inserting ctor with null basic block!");
430 if (TID->ImplicitDefs)
431 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
432 NumImplicitOps++;
433 if (TID->ImplicitUses)
434 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
435 NumImplicitOps++;
436 Operands.reserve(NumImplicitOps + TID->getNumOperands());
437 addImplicitDefUseOperands();
438 // Make sure that we get added to a machine basicblock
439 LeakDetector::addGarbageObject(this);
440 MBB->push_back(this); // Add instruction to end of basic block!
441}
442
443/// MachineInstr ctor - As above, but with a DebugLoc.
444///
445MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl,
Chris Lattner749c6f62008-01-07 07:27:27 +0000446 const TargetInstrDesc &tid)
Dan Gohmanc76909a2009-09-25 20:36:54 +0000447 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0),
448 Parent(0), debugLoc(dl) {
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000449 assert(MBB && "Cannot use inserting ctor with null basic block!");
Evan Cheng67f660c2006-11-30 07:08:44 +0000450 if (TID->ImplicitDefs)
Chris Lattner349c4952008-01-07 03:13:06 +0000451 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000452 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +0000453 if (TID->ImplicitUses)
Chris Lattner349c4952008-01-07 03:13:06 +0000454 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000455 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000456 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Cheng67f660c2006-11-30 07:08:44 +0000457 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000458 // Make sure that we get added to a machine basicblock
459 LeakDetector::addGarbageObject(this);
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000460 MBB->push_back(this); // Add instruction to end of basic block!
461}
462
Misha Brukmance22e762004-07-09 14:45:17 +0000463/// MachineInstr ctor - Copies MachineInstr arg exactly
464///
Evan Cheng1ed99222008-07-19 00:37:25 +0000465MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Dan Gohmanc76909a2009-09-25 20:36:54 +0000466 : TID(&MI.getDesc()), NumImplicitOps(0),
467 MemRefs(MI.MemRefs), MemRefsEnd(MI.MemRefsEnd),
468 Parent(0), debugLoc(MI.getDebugLoc()) {
Chris Lattner943b5e12006-05-04 19:14:44 +0000469 Operands.reserve(MI.getNumOperands());
Tanya Lattnerb5159ed2004-05-23 20:58:02 +0000470
Misha Brukmance22e762004-07-09 14:45:17 +0000471 // Add operands
Evan Cheng1ed99222008-07-19 00:37:25 +0000472 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
473 addOperand(MI.getOperand(i));
474 NumImplicitOps = MI.NumImplicitOps;
Tanya Lattner0c63e032004-05-24 03:14:18 +0000475
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000476 // Set parent to null.
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000477 Parent = 0;
Dan Gohman6116a732008-07-21 18:47:29 +0000478
479 LeakDetector::addGarbageObject(this);
Tanya Lattner466b5342004-05-23 19:35:12 +0000480}
481
Misha Brukmance22e762004-07-09 14:45:17 +0000482MachineInstr::~MachineInstr() {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000483 LeakDetector::removeGarbageObject(this);
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000484#ifndef NDEBUG
Chris Lattner62ed6b92008-01-01 01:12:31 +0000485 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000486 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
Dan Gohmand735b802008-10-03 15:45:36 +0000487 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
Chris Lattner62ed6b92008-01-01 01:12:31 +0000488 "Reg operand def/use list corrupted");
489 }
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000490#endif
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000491}
492
Chris Lattner62ed6b92008-01-01 01:12:31 +0000493/// getRegInfo - If this instruction is embedded into a MachineFunction,
494/// return the MachineRegisterInfo object for the current function, otherwise
495/// return null.
496MachineRegisterInfo *MachineInstr::getRegInfo() {
497 if (MachineBasicBlock *MBB = getParent())
Dan Gohman4e526b92008-07-08 23:59:09 +0000498 return &MBB->getParent()->getRegInfo();
Chris Lattner62ed6b92008-01-01 01:12:31 +0000499 return 0;
500}
501
502/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
503/// this instruction from their respective use lists. This requires that the
504/// operands already be on their use lists.
505void MachineInstr::RemoveRegOperandsFromUseLists() {
506 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000507 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000508 Operands[i].RemoveRegOperandFromRegInfo();
509 }
510}
511
512/// AddRegOperandsToUseLists - Add all of the register operands in
513/// this instruction from their respective use lists. This requires that the
514/// operands not be on their use lists yet.
515void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
516 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000517 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000518 Operands[i].AddRegOperandToRegInfo(&RegInfo);
519 }
520}
521
522
523/// addOperand - Add the specified operand to the instruction. If it is an
524/// implicit operand, it is added to the end of the operand list. If it is
525/// an explicit operand it is added at the end of the explicit operand list
526/// (before the first implicit operand).
527void MachineInstr::addOperand(const MachineOperand &Op) {
Dan Gohmand735b802008-10-03 15:45:36 +0000528 bool isImpReg = Op.isReg() && Op.isImplicit();
Chris Lattner62ed6b92008-01-01 01:12:31 +0000529 assert((isImpReg || !OperandsComplete()) &&
530 "Trying to add an operand to a machine instr that is already done!");
531
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000532 MachineRegisterInfo *RegInfo = getRegInfo();
533
Chris Lattner62ed6b92008-01-01 01:12:31 +0000534 // If we are adding the operand to the end of the list, our job is simpler.
535 // This is true most of the time, so this is a reasonable optimization.
536 if (isImpReg || NumImplicitOps == 0) {
537 // We can only do this optimization if we know that the operand list won't
538 // reallocate.
539 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
540 Operands.push_back(Op);
541
542 // Set the parent of the operand.
543 Operands.back().ParentMI = this;
544
545 // If the operand is a register, update the operand's use list.
Dan Gohmand735b802008-10-03 15:45:36 +0000546 if (Op.isReg())
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000547 Operands.back().AddRegOperandToRegInfo(RegInfo);
Chris Lattner62ed6b92008-01-01 01:12:31 +0000548 return;
549 }
550 }
551
552 // Otherwise, we have to insert a real operand before any implicit ones.
553 unsigned OpNo = Operands.size()-NumImplicitOps;
554
Chris Lattner62ed6b92008-01-01 01:12:31 +0000555 // If this instruction isn't embedded into a function, then we don't need to
556 // update any operand lists.
557 if (RegInfo == 0) {
558 // Simple insertion, no reginfo update needed for other register operands.
559 Operands.insert(Operands.begin()+OpNo, Op);
560 Operands[OpNo].ParentMI = this;
561
562 // Do explicitly set the reginfo for this operand though, to ensure the
563 // next/prev fields are properly nulled out.
Dan Gohmand735b802008-10-03 15:45:36 +0000564 if (Operands[OpNo].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000565 Operands[OpNo].AddRegOperandToRegInfo(0);
566
567 } else if (Operands.size()+1 <= Operands.capacity()) {
568 // Otherwise, we have to remove register operands from their register use
569 // list, add the operand, then add the register operands back to their use
570 // list. This also must handle the case when the operand list reallocates
571 // to somewhere else.
572
573 // If insertion of this operand won't cause reallocation of the operand
574 // list, just remove the implicit operands, add the operand, then re-add all
575 // the rest of the operands.
576 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000577 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000578 Operands[i].RemoveRegOperandFromRegInfo();
579 }
580
581 // Add the operand. If it is a register, add it to the reg list.
582 Operands.insert(Operands.begin()+OpNo, Op);
583 Operands[OpNo].ParentMI = this;
584
Dan Gohmand735b802008-10-03 15:45:36 +0000585 if (Operands[OpNo].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000586 Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
587
588 // Re-add all the implicit ops.
589 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000590 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000591 Operands[i].AddRegOperandToRegInfo(RegInfo);
592 }
593 } else {
594 // Otherwise, we will be reallocating the operand list. Remove all reg
595 // operands from their list, then readd them after the operand list is
596 // reallocated.
597 RemoveRegOperandsFromUseLists();
598
599 Operands.insert(Operands.begin()+OpNo, Op);
600 Operands[OpNo].ParentMI = this;
601
602 // Re-add all the operands.
603 AddRegOperandsToUseLists(*RegInfo);
604 }
605}
606
607/// RemoveOperand - Erase an operand from an instruction, leaving it with one
608/// fewer operand than it started with.
609///
610void MachineInstr::RemoveOperand(unsigned OpNo) {
611 assert(OpNo < Operands.size() && "Invalid operand number");
612
613 // Special case removing the last one.
614 if (OpNo == Operands.size()-1) {
615 // If needed, remove from the reg def/use list.
Dan Gohmand735b802008-10-03 15:45:36 +0000616 if (Operands.back().isReg() && Operands.back().isOnRegUseList())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000617 Operands.back().RemoveRegOperandFromRegInfo();
618
619 Operands.pop_back();
620 return;
621 }
622
623 // Otherwise, we are removing an interior operand. If we have reginfo to
624 // update, remove all operands that will be shifted down from their reg lists,
625 // move everything down, then re-add them.
626 MachineRegisterInfo *RegInfo = getRegInfo();
627 if (RegInfo) {
628 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000629 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000630 Operands[i].RemoveRegOperandFromRegInfo();
631 }
632 }
633
634 Operands.erase(Operands.begin()+OpNo);
635
636 if (RegInfo) {
637 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000638 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000639 Operands[i].AddRegOperandToRegInfo(RegInfo);
640 }
641 }
642}
643
Dan Gohmanc76909a2009-09-25 20:36:54 +0000644/// addMemOperand - Add a MachineMemOperand to the machine instruction.
645/// This function should be used only occasionally. The setMemRefs function
646/// is the primary method for setting up a MachineInstr's MemRefs list.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000647void MachineInstr::addMemOperand(MachineFunction &MF,
Dan Gohmanc76909a2009-09-25 20:36:54 +0000648 MachineMemOperand *MO) {
649 mmo_iterator OldMemRefs = MemRefs;
650 mmo_iterator OldMemRefsEnd = MemRefsEnd;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000651
Dan Gohmanc76909a2009-09-25 20:36:54 +0000652 size_t NewNum = (MemRefsEnd - MemRefs) + 1;
653 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
654 mmo_iterator NewMemRefsEnd = NewMemRefs + NewNum;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000655
Dan Gohmanc76909a2009-09-25 20:36:54 +0000656 std::copy(OldMemRefs, OldMemRefsEnd, NewMemRefs);
657 NewMemRefs[NewNum - 1] = MO;
658
659 MemRefs = NewMemRefs;
660 MemRefsEnd = NewMemRefsEnd;
661}
Chris Lattner62ed6b92008-01-01 01:12:31 +0000662
Chris Lattner48d7c062006-04-17 21:35:41 +0000663/// removeFromParent - This method unlinks 'this' from the containing basic
664/// block, and returns it, but does not delete it.
665MachineInstr *MachineInstr::removeFromParent() {
666 assert(getParent() && "Not embedded in a basic block!");
667 getParent()->remove(this);
668 return this;
669}
670
671
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000672/// eraseFromParent - This method unlinks 'this' from the containing basic
673/// block, and deletes it.
674void MachineInstr::eraseFromParent() {
675 assert(getParent() && "Not embedded in a basic block!");
676 getParent()->erase(this);
677}
678
679
Brian Gaeke21326fc2004-02-13 04:39:32 +0000680/// OperandComplete - Return true if it's illegal to add a new operand
681///
Chris Lattner2a90ba62004-02-12 16:09:53 +0000682bool MachineInstr::OperandsComplete() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000683 unsigned short NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000684 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands)
Vikram S. Adve34977822003-05-31 07:39:06 +0000685 return true; // Broken: we have all the operands of this instruction!
Chris Lattner413746e2002-10-28 20:48:39 +0000686 return false;
687}
688
Evan Cheng19e3f312007-05-15 01:26:09 +0000689/// getNumExplicitOperands - Returns the number of non-implicit operands.
690///
691unsigned MachineInstr::getNumExplicitOperands() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000692 unsigned NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000693 if (!TID->isVariadic())
Evan Cheng19e3f312007-05-15 01:26:09 +0000694 return NumOperands;
695
Dan Gohman9407cd42009-04-15 17:59:11 +0000696 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
697 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000698 if (!MO.isReg() || !MO.isImplicit())
Evan Cheng19e3f312007-05-15 01:26:09 +0000699 NumOperands++;
700 }
701 return NumOperands;
702}
703
Chris Lattner8ace2cd2006-10-20 22:39:59 +0000704
Dan Gohman44066042008-07-01 00:05:16 +0000705/// isLabel - Returns true if the MachineInstr represents a label.
706///
707bool MachineInstr::isLabel() const {
708 return getOpcode() == TargetInstrInfo::DBG_LABEL ||
709 getOpcode() == TargetInstrInfo::EH_LABEL ||
710 getOpcode() == TargetInstrInfo::GC_LABEL;
711}
712
Evan Chengbb81d972008-01-31 09:59:15 +0000713/// isDebugLabel - Returns true if the MachineInstr represents a debug label.
714///
715bool MachineInstr::isDebugLabel() const {
Dan Gohman44066042008-07-01 00:05:16 +0000716 return getOpcode() == TargetInstrInfo::DBG_LABEL;
Evan Chengbb81d972008-01-31 09:59:15 +0000717}
718
Evan Chengfaa51072007-04-26 19:00:32 +0000719/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Jim Grosbachf9ca50e2009-09-17 17:57:26 +0000720/// the specific register or -1 if it is not found. It further tightens
Evan Cheng76d7e762007-02-23 01:04:26 +0000721/// the search criteria to a use that kills the register if isKill is true.
Evan Cheng6130f662008-03-05 00:59:57 +0000722int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
723 const TargetRegisterInfo *TRI) const {
Evan Cheng576d1232006-12-06 08:27:42 +0000724 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Chengf277ee42007-05-29 18:35:22 +0000725 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000726 if (!MO.isReg() || !MO.isUse())
Evan Cheng6130f662008-03-05 00:59:57 +0000727 continue;
728 unsigned MOReg = MO.getReg();
729 if (!MOReg)
730 continue;
731 if (MOReg == Reg ||
732 (TRI &&
733 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
734 TargetRegisterInfo::isPhysicalRegister(Reg) &&
735 TRI->isSubRegister(MOReg, Reg)))
Evan Cheng76d7e762007-02-23 01:04:26 +0000736 if (!isKill || MO.isKill())
Evan Cheng32eb1f12007-03-26 22:37:45 +0000737 return i;
Evan Cheng576d1232006-12-06 08:27:42 +0000738 }
Evan Cheng32eb1f12007-03-26 22:37:45 +0000739 return -1;
Evan Cheng576d1232006-12-06 08:27:42 +0000740}
741
Evan Cheng6130f662008-03-05 00:59:57 +0000742/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman703bfe62008-05-06 00:20:10 +0000743/// the specified register or -1 if it is not found. If isDead is true, defs
744/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
745/// also checks if there is a def of a super-register.
Evan Cheng6130f662008-03-05 00:59:57 +0000746int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead,
747 const TargetRegisterInfo *TRI) const {
Evan Chengb371f452007-02-19 21:49:54 +0000748 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng6130f662008-03-05 00:59:57 +0000749 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000750 if (!MO.isReg() || !MO.isDef())
Evan Cheng6130f662008-03-05 00:59:57 +0000751 continue;
752 unsigned MOReg = MO.getReg();
753 if (MOReg == Reg ||
754 (TRI &&
755 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
756 TargetRegisterInfo::isPhysicalRegister(Reg) &&
757 TRI->isSubRegister(MOReg, Reg)))
758 if (!isDead || MO.isDead())
759 return i;
Evan Chengb371f452007-02-19 21:49:54 +0000760 }
Evan Cheng6130f662008-03-05 00:59:57 +0000761 return -1;
Evan Chengb371f452007-02-19 21:49:54 +0000762}
Evan Cheng19e3f312007-05-15 01:26:09 +0000763
Evan Chengf277ee42007-05-29 18:35:22 +0000764/// findFirstPredOperandIdx() - Find the index of the first operand in the
765/// operand list that is used to represent the predicate. It returns -1 if
766/// none is found.
767int MachineInstr::findFirstPredOperandIdx() const {
Chris Lattner749c6f62008-01-07 07:27:27 +0000768 const TargetInstrDesc &TID = getDesc();
769 if (TID.isPredicable()) {
Evan Cheng19e3f312007-05-15 01:26:09 +0000770 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner749c6f62008-01-07 07:27:27 +0000771 if (TID.OpInfo[i].isPredicate())
Evan Chengf277ee42007-05-29 18:35:22 +0000772 return i;
Evan Cheng19e3f312007-05-15 01:26:09 +0000773 }
774
Evan Chengf277ee42007-05-29 18:35:22 +0000775 return -1;
Evan Cheng19e3f312007-05-15 01:26:09 +0000776}
Evan Chengb371f452007-02-19 21:49:54 +0000777
Bob Wilsond9df5012009-04-09 17:16:43 +0000778/// isRegTiedToUseOperand - Given the index of a register def operand,
779/// check if the register def is tied to a source operand, due to either
780/// two-address elimination or inline assembly constraints. Returns the
781/// first tied use operand index by reference is UseOpIdx is not null.
Jakob Stoklund Olesence9be2c2009-04-29 20:57:16 +0000782bool MachineInstr::
783isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const {
Evan Chengfb112882009-03-23 08:01:15 +0000784 if (getOpcode() == TargetInstrInfo::INLINEASM) {
Bob Wilsond9df5012009-04-09 17:16:43 +0000785 assert(DefOpIdx >= 2);
786 const MachineOperand &MO = getOperand(DefOpIdx);
Chris Lattnerc30aa7b2009-04-09 23:33:34 +0000787 if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
Evan Chengfb112882009-03-23 08:01:15 +0000788 return false;
Evan Chengef5d0702009-06-24 02:05:51 +0000789 // Determine the actual operand index that corresponds to this index.
Evan Chengfb112882009-03-23 08:01:15 +0000790 unsigned DefNo = 0;
Evan Chengef5d0702009-06-24 02:05:51 +0000791 unsigned DefPart = 0;
Evan Chengfb112882009-03-23 08:01:15 +0000792 for (unsigned i = 1, e = getNumOperands(); i < e; ) {
793 const MachineOperand &FMO = getOperand(i);
Jakob Stoklund Olesen45d34fe2009-07-19 19:09:59 +0000794 // After the normal asm operands there may be additional imp-def regs.
795 if (!FMO.isImm())
796 return false;
Evan Chengfb112882009-03-23 08:01:15 +0000797 // Skip over this def.
Evan Chengef5d0702009-06-24 02:05:51 +0000798 unsigned NumOps = InlineAsm::getNumOperandRegisters(FMO.getImm());
799 unsigned PrevDef = i + 1;
800 i = PrevDef + NumOps;
801 if (i > DefOpIdx) {
802 DefPart = DefOpIdx - PrevDef;
Evan Chengfb112882009-03-23 08:01:15 +0000803 break;
Evan Chengef5d0702009-06-24 02:05:51 +0000804 }
Evan Chengfb112882009-03-23 08:01:15 +0000805 ++DefNo;
806 }
Evan Chengef5d0702009-06-24 02:05:51 +0000807 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Evan Chengfb112882009-03-23 08:01:15 +0000808 const MachineOperand &FMO = getOperand(i);
809 if (!FMO.isImm())
810 continue;
811 if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse())
812 continue;
813 unsigned Idx;
Evan Chengef5d0702009-06-24 02:05:51 +0000814 if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) &&
Bob Wilsond9df5012009-04-09 17:16:43 +0000815 Idx == DefNo) {
816 if (UseOpIdx)
Evan Chengef5d0702009-06-24 02:05:51 +0000817 *UseOpIdx = (unsigned)i + 1 + DefPart;
Evan Chengfb112882009-03-23 08:01:15 +0000818 return true;
Bob Wilsond9df5012009-04-09 17:16:43 +0000819 }
Evan Chengfb112882009-03-23 08:01:15 +0000820 }
Evan Chengef5d0702009-06-24 02:05:51 +0000821 return false;
Evan Chengfb112882009-03-23 08:01:15 +0000822 }
823
Bob Wilsond9df5012009-04-09 17:16:43 +0000824 assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!");
Chris Lattner749c6f62008-01-07 07:27:27 +0000825 const TargetInstrDesc &TID = getDesc();
Evan Chengef0732d2008-07-10 07:35:43 +0000826 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
827 const MachineOperand &MO = getOperand(i);
Dan Gohman2ce7f202008-12-05 05:45:42 +0000828 if (MO.isReg() && MO.isUse() &&
Bob Wilsond9df5012009-04-09 17:16:43 +0000829 TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) {
830 if (UseOpIdx)
831 *UseOpIdx = (unsigned)i;
Evan Chengef0732d2008-07-10 07:35:43 +0000832 return true;
Bob Wilsond9df5012009-04-09 17:16:43 +0000833 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000834 }
835 return false;
836}
837
Evan Chenga24752f2009-03-19 20:30:06 +0000838/// isRegTiedToDefOperand - Return true if the operand of the specified index
839/// is a register use and it is tied to an def operand. It also returns the def
840/// operand index by reference.
Jakob Stoklund Olesence9be2c2009-04-29 20:57:16 +0000841bool MachineInstr::
842isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const {
Evan Chengfb112882009-03-23 08:01:15 +0000843 if (getOpcode() == TargetInstrInfo::INLINEASM) {
844 const MachineOperand &MO = getOperand(UseOpIdx);
Chris Lattner0c8382c2009-04-09 16:50:43 +0000845 if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0)
Evan Chengfb112882009-03-23 08:01:15 +0000846 return false;
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000847
848 // Find the flag operand corresponding to UseOpIdx
849 unsigned FlagIdx, NumOps=0;
850 for (FlagIdx = 1; FlagIdx < UseOpIdx; FlagIdx += NumOps+1) {
851 const MachineOperand &UFMO = getOperand(FlagIdx);
Jakob Stoklund Olesen45d34fe2009-07-19 19:09:59 +0000852 // After the normal asm operands there may be additional imp-def regs.
853 if (!UFMO.isImm())
854 return false;
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000855 NumOps = InlineAsm::getNumOperandRegisters(UFMO.getImm());
856 assert(NumOps < getNumOperands() && "Invalid inline asm flag");
857 if (UseOpIdx < FlagIdx+NumOps+1)
858 break;
Evan Chengef5d0702009-06-24 02:05:51 +0000859 }
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000860 if (FlagIdx >= UseOpIdx)
Evan Chengef5d0702009-06-24 02:05:51 +0000861 return false;
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000862 const MachineOperand &UFMO = getOperand(FlagIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000863 unsigned DefNo;
864 if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) {
865 if (!DefOpIdx)
866 return true;
867
868 unsigned DefIdx = 1;
869 // Remember to adjust the index. First operand is asm string, then there
870 // is a flag for each.
871 while (DefNo) {
872 const MachineOperand &FMO = getOperand(DefIdx);
873 assert(FMO.isImm());
874 // Skip over this def.
875 DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
876 --DefNo;
877 }
Evan Chengef5d0702009-06-24 02:05:51 +0000878 *DefOpIdx = DefIdx + UseOpIdx - FlagIdx;
Evan Chengfb112882009-03-23 08:01:15 +0000879 return true;
880 }
881 return false;
882 }
883
Evan Chenga24752f2009-03-19 20:30:06 +0000884 const TargetInstrDesc &TID = getDesc();
885 if (UseOpIdx >= TID.getNumOperands())
886 return false;
887 const MachineOperand &MO = getOperand(UseOpIdx);
888 if (!MO.isReg() || !MO.isUse())
889 return false;
890 int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO);
891 if (DefIdx == -1)
892 return false;
893 if (DefOpIdx)
894 *DefOpIdx = (unsigned)DefIdx;
895 return true;
896}
897
Evan Cheng576d1232006-12-06 08:27:42 +0000898/// copyKillDeadInfo - Copies kill / dead operand properties from MI.
899///
900void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
901 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
902 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000903 if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
Evan Cheng576d1232006-12-06 08:27:42 +0000904 continue;
905 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
906 MachineOperand &MOp = getOperand(j);
907 if (!MOp.isIdenticalTo(MO))
908 continue;
909 if (MO.isKill())
910 MOp.setIsKill();
911 else
912 MOp.setIsDead();
913 break;
914 }
915 }
916}
917
Evan Cheng19e3f312007-05-15 01:26:09 +0000918/// copyPredicates - Copies predicate operand(s) from MI.
919void MachineInstr::copyPredicates(const MachineInstr *MI) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000920 const TargetInstrDesc &TID = MI->getDesc();
Evan Chengb27087f2008-03-13 00:44:09 +0000921 if (!TID.isPredicable())
922 return;
923 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
924 if (TID.OpInfo[i].isPredicate()) {
925 // Predicated operands must be last operands.
926 addOperand(MI->getOperand(i));
Evan Cheng19e3f312007-05-15 01:26:09 +0000927 }
928 }
929}
930
Evan Cheng9f1c8312008-07-03 09:09:37 +0000931/// isSafeToMove - Return true if it is safe to move this instruction. If
932/// SawStore is set to true, it means that there is a store (or call) between
933/// the instruction's location and its intended destination.
Dan Gohmanb3b930a2008-11-18 19:04:29 +0000934bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII,
935 bool &SawStore) const {
Evan Chengb27087f2008-03-13 00:44:09 +0000936 // Ignore stuff that we obviously can't move.
937 if (TID->mayStore() || TID->isCall()) {
938 SawStore = true;
939 return false;
940 }
Dan Gohman237dee12008-12-23 17:28:50 +0000941 if (TID->isTerminator() || TID->hasUnmodeledSideEffects())
Evan Chengb27087f2008-03-13 00:44:09 +0000942 return false;
943
944 // See if this instruction does a load. If so, we have to guarantee that the
945 // loaded value doesn't change between the load and the its intended
946 // destination. The check for isInvariantLoad gives the targe the chance to
947 // classify the load as always returning a constant, e.g. a constant pool
948 // load.
Dan Gohman3e4fb702008-09-24 00:06:15 +0000949 if (TID->mayLoad() && !TII->isInvariantLoad(this))
Evan Chengb27087f2008-03-13 00:44:09 +0000950 // Otherwise, this is a real load. If there is a store between the load and
Evan Cheng7cc2c402009-07-28 21:49:18 +0000951 // end of block, or if the load is volatile, we can't move it.
Dan Gohmand790a5c2008-10-02 15:04:30 +0000952 return !SawStore && !hasVolatileMemoryRef();
Dan Gohman3e4fb702008-09-24 00:06:15 +0000953
Evan Chengb27087f2008-03-13 00:44:09 +0000954 return true;
955}
956
Evan Chengdf3b9932008-08-27 20:33:50 +0000957/// isSafeToReMat - Return true if it's safe to rematerialize the specified
958/// instruction which defined the specified register instead of copying it.
Dan Gohmanb3b930a2008-11-18 19:04:29 +0000959bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII,
960 unsigned DstReg) const {
Evan Chengdf3b9932008-08-27 20:33:50 +0000961 bool SawStore = false;
Evan Cheng3689ff42008-08-30 09:07:18 +0000962 if (!getDesc().isRematerializable() ||
963 !TII->isTriviallyReMaterializable(this) ||
964 !isSafeToMove(TII, SawStore))
Evan Chengdf3b9932008-08-27 20:33:50 +0000965 return false;
966 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Dan Gohmancbad42c2008-11-18 19:49:32 +0000967 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000968 if (!MO.isReg())
Evan Chengdf3b9932008-08-27 20:33:50 +0000969 continue;
970 // FIXME: For now, do not remat any instruction with register operands.
971 // Later on, we can loosen the restriction is the register operands have
972 // not been modified between the def and use. Note, this is different from
Evan Cheng8763c1c2008-08-27 20:58:54 +0000973 // MachineSink because the code is no longer in two-address form (at least
Evan Chengdf3b9932008-08-27 20:33:50 +0000974 // partially).
975 if (MO.isUse())
976 return false;
977 else if (!MO.isDead() && MO.getReg() != DstReg)
978 return false;
979 }
980 return true;
981}
982
Dan Gohman3e4fb702008-09-24 00:06:15 +0000983/// hasVolatileMemoryRef - Return true if this instruction may have a
984/// volatile memory reference, or if the information describing the
985/// memory reference is not available. Return false if it is known to
986/// have no volatile memory references.
987bool MachineInstr::hasVolatileMemoryRef() const {
988 // An instruction known never to access memory won't have a volatile access.
989 if (!TID->mayStore() &&
990 !TID->mayLoad() &&
991 !TID->isCall() &&
992 !TID->hasUnmodeledSideEffects())
993 return false;
994
995 // Otherwise, if the instruction has no memory reference information,
996 // conservatively assume it wasn't preserved.
997 if (memoperands_empty())
998 return true;
999
1000 // Check the memory reference information for volatile references.
Dan Gohmanc76909a2009-09-25 20:36:54 +00001001 for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I)
1002 if ((*I)->isVolatile())
Dan Gohman3e4fb702008-09-24 00:06:15 +00001003 return true;
1004
1005 return false;
1006}
1007
Brian Gaeke21326fc2004-02-13 04:39:32 +00001008void MachineInstr::dump() const {
Chris Lattner705e07f2009-08-23 03:41:05 +00001009 errs() << " " << *this;
Mon P Wang5ca6bd12008-10-10 01:43:55 +00001010}
1011
1012void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const {
Chris Lattnere3087892007-12-30 21:31:53 +00001013 // Specialize printing if op#0 is definition
Chris Lattner6a592272002-10-30 01:55:38 +00001014 unsigned StartOp = 0;
Dan Gohmand735b802008-10-03 15:45:36 +00001015 if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
Chris Lattnerf7382302007-12-30 21:56:09 +00001016 getOperand(0).print(OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +00001017 OS << " = ";
1018 ++StartOp; // Don't print this operand again!
1019 }
Tanya Lattnerb1407622004-06-25 00:13:11 +00001020
Chris Lattner749c6f62008-01-07 07:27:27 +00001021 OS << getDesc().getName();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001022
Chris Lattner6a592272002-10-30 01:55:38 +00001023 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
1024 if (i != StartOp)
1025 OS << ",";
1026 OS << " ";
Chris Lattnerf7382302007-12-30 21:56:09 +00001027 getOperand(i).print(OS, TM);
Chris Lattner10491642002-10-30 00:48:05 +00001028 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001029
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001030 if (!memoperands_empty()) {
Dan Gohman2bfe6ff2008-02-07 16:18:00 +00001031 OS << ", Mem:";
Dan Gohmanc76909a2009-09-25 20:36:54 +00001032 for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
1033 i != e; ++i) {
1034 OS << **i;
Dan Gohmancd26ec52009-09-23 01:33:16 +00001035 if (next(i) != e)
1036 OS << " ";
Dan Gohman69de1932008-02-06 22:27:42 +00001037 }
1038 }
1039
Bill Wendlingb5ef2732009-02-19 21:44:55 +00001040 if (!debugLoc.isUnknown()) {
1041 const MachineFunction *MF = getParent()->getParent();
1042 DebugLocTuple DLT = MF->getDebugLocTuple(debugLoc);
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +00001043 DICompileUnit CU(DLT.CompileUnit);
Bill Wendlingb5ef2732009-02-19 21:44:55 +00001044 OS << " [dbg: "
Devang Patel5ccdd102009-09-29 18:40:58 +00001045 << CU.getDirectory() << '/' << CU.getFilename() << ","
Bill Wendlingb5ef2732009-02-19 21:44:55 +00001046 << DLT.Line << ","
1047 << DLT.Col << "]";
1048 }
1049
Chris Lattner10491642002-10-30 00:48:05 +00001050 OS << "\n";
1051}
1052
Owen Andersonb487e722008-01-24 01:10:07 +00001053bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001054 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +00001055 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001056 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001057 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +00001058 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001059 SmallVector<unsigned,4> DeadOps;
Bill Wendling4a23d722008-03-03 22:14:33 +00001060 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1061 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesenefb8e3e2009-08-04 20:09:25 +00001062 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001063 continue;
1064 unsigned Reg = MO.getReg();
1065 if (!Reg)
1066 continue;
Bill Wendling4a23d722008-03-03 22:14:33 +00001067
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001068 if (Reg == IncomingReg) {
Dan Gohman3f629402008-09-03 15:56:16 +00001069 if (!Found) {
1070 if (MO.isKill())
1071 // The register is already marked kill.
1072 return true;
Jakob Stoklund Olesenece48182009-08-02 19:13:03 +00001073 if (isPhysReg && isRegTiedToDefOperand(i))
1074 // Two-address uses of physregs must not be marked kill.
1075 return true;
Dan Gohman3f629402008-09-03 15:56:16 +00001076 MO.setIsKill();
1077 Found = true;
1078 }
1079 } else if (hasAliases && MO.isKill() &&
1080 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001081 // A super-register kill already exists.
1082 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001083 return true;
1084 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001085 DeadOps.push_back(i);
Bill Wendling4a23d722008-03-03 22:14:33 +00001086 }
1087 }
1088
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001089 // Trim unneeded kill operands.
1090 while (!DeadOps.empty()) {
1091 unsigned OpIdx = DeadOps.back();
1092 if (getOperand(OpIdx).isImplicit())
1093 RemoveOperand(OpIdx);
1094 else
1095 getOperand(OpIdx).setIsKill(false);
1096 DeadOps.pop_back();
1097 }
1098
Bill Wendling4a23d722008-03-03 22:14:33 +00001099 // If not found, this means an alias of one of the operands is killed. Add a
Owen Andersonb487e722008-01-24 01:10:07 +00001100 // new implicit operand if required.
Dan Gohman3f629402008-09-03 15:56:16 +00001101 if (!Found && AddIfNotFound) {
Bill Wendling4a23d722008-03-03 22:14:33 +00001102 addOperand(MachineOperand::CreateReg(IncomingReg,
1103 false /*IsDef*/,
1104 true /*IsImp*/,
1105 true /*IsKill*/));
Owen Andersonb487e722008-01-24 01:10:07 +00001106 return true;
1107 }
Dan Gohman3f629402008-09-03 15:56:16 +00001108 return Found;
Owen Andersonb487e722008-01-24 01:10:07 +00001109}
1110
1111bool MachineInstr::addRegisterDead(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001112 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +00001113 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001114 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Evan Cheng01b2e232008-06-27 22:11:49 +00001115 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +00001116 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001117 SmallVector<unsigned,4> DeadOps;
Owen Andersonb487e722008-01-24 01:10:07 +00001118 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1119 MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001120 if (!MO.isReg() || !MO.isDef())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001121 continue;
1122 unsigned Reg = MO.getReg();
Dan Gohman3f629402008-09-03 15:56:16 +00001123 if (!Reg)
1124 continue;
1125
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001126 if (Reg == IncomingReg) {
Dan Gohman3f629402008-09-03 15:56:16 +00001127 if (!Found) {
1128 if (MO.isDead())
1129 // The register is already marked dead.
1130 return true;
1131 MO.setIsDead();
1132 Found = true;
1133 }
1134 } else if (hasAliases && MO.isDead() &&
1135 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001136 // There exists a super-register that's marked dead.
1137 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001138 return true;
Owen Anderson22ae9992008-08-14 18:34:18 +00001139 if (RegInfo->getSubRegisters(IncomingReg) &&
1140 RegInfo->getSuperRegisters(Reg) &&
1141 RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001142 DeadOps.push_back(i);
Owen Andersonb487e722008-01-24 01:10:07 +00001143 }
1144 }
1145
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001146 // Trim unneeded dead operands.
1147 while (!DeadOps.empty()) {
1148 unsigned OpIdx = DeadOps.back();
1149 if (getOperand(OpIdx).isImplicit())
1150 RemoveOperand(OpIdx);
1151 else
1152 getOperand(OpIdx).setIsDead(false);
1153 DeadOps.pop_back();
1154 }
1155
Dan Gohman3f629402008-09-03 15:56:16 +00001156 // If not found, this means an alias of one of the operands is dead. Add a
1157 // new implicit operand if required.
Chris Lattner31530612009-06-24 17:54:48 +00001158 if (Found || !AddIfNotFound)
1159 return Found;
1160
1161 addOperand(MachineOperand::CreateReg(IncomingReg,
1162 true /*IsDef*/,
1163 true /*IsImp*/,
1164 false /*IsKill*/,
1165 true /*IsDead*/));
1166 return true;
Owen Andersonb487e722008-01-24 01:10:07 +00001167}