blob: 5744c8a54552947c7115c39b418d0ede33094a61 [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"
Dan Gohman8c2b5252009-10-30 01:27:03 +000016#include "llvm/Function.h"
Evan Chengfb112882009-03-23 08:01:15 +000017#include "llvm/InlineAsm.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000018#include "llvm/Value.h"
Dan Gohmancd26ec52009-09-23 01:33:16 +000019#include "llvm/Assembly/Writer.h"
Chris Lattner8517e1f2004-02-19 16:17:08 +000020#include "llvm/CodeGen/MachineFunction.h"
Dan Gohmanc76909a2009-09-25 20:36:54 +000021#include "llvm/CodeGen/MachineMemOperand.h"
Chris Lattner62ed6b92008-01-01 01:12:31 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000023#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner10491642002-10-30 00:48:05 +000024#include "llvm/Target/TargetMachine.h"
Evan Chengbb81d972008-01-31 09:59:15 +000025#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerf14cf852008-01-07 07:42:25 +000026#include "llvm/Target/TargetInstrDesc.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000027#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmane33f44c2009-10-07 17:38:06 +000028#include "llvm/Analysis/AliasAnalysis.h"
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +000029#include "llvm/Analysis/DebugInfo.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000030#include "llvm/Support/ErrorHandling.h"
Dan Gohman2c3f7ae2008-07-17 23:49:46 +000031#include "llvm/Support/LeakDetector.h"
Dan Gohmance42e402008-07-07 20:32:02 +000032#include "llvm/Support/MathExtras.h"
Chris Lattneredfb72c2008-08-24 20:37:32 +000033#include "llvm/Support/raw_ostream.h"
Dan Gohmanb8d2f552008-08-20 15:58:01 +000034#include "llvm/ADT/FoldingSet.h"
Chris Lattner0742b592004-02-23 18:38:20 +000035using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000036
Chris Lattnerf7382302007-12-30 21:56:09 +000037//===----------------------------------------------------------------------===//
38// MachineOperand Implementation
39//===----------------------------------------------------------------------===//
40
Chris Lattner62ed6b92008-01-01 01:12:31 +000041/// AddRegOperandToRegInfo - Add this register operand to the specified
42/// MachineRegisterInfo. If it is null, then the next/prev fields should be
43/// explicitly nulled out.
44void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) {
Dan Gohmand735b802008-10-03 15:45:36 +000045 assert(isReg() && "Can only add reg operand to use lists");
Chris Lattner62ed6b92008-01-01 01:12:31 +000046
47 // If the reginfo pointer is null, just explicitly null out or next/prev
48 // pointers, to ensure they are not garbage.
49 if (RegInfo == 0) {
50 Contents.Reg.Prev = 0;
51 Contents.Reg.Next = 0;
52 return;
53 }
54
55 // Otherwise, add this operand to the head of the registers use/def list.
Chris Lattner80fe5312008-01-01 21:08:22 +000056 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg());
Chris Lattner62ed6b92008-01-01 01:12:31 +000057
Chris Lattner80fe5312008-01-01 21:08:22 +000058 // For SSA values, we prefer to keep the definition at the start of the list.
59 // we do this by skipping over the definition if it is at the head of the
60 // list.
61 if (*Head && (*Head)->isDef())
62 Head = &(*Head)->Contents.Reg.Next;
63
64 Contents.Reg.Next = *Head;
Chris Lattner62ed6b92008-01-01 01:12:31 +000065 if (Contents.Reg.Next) {
66 assert(getReg() == Contents.Reg.Next->getReg() &&
67 "Different regs on the same list!");
68 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next;
69 }
70
Chris Lattner80fe5312008-01-01 21:08:22 +000071 Contents.Reg.Prev = Head;
72 *Head = this;
Chris Lattner62ed6b92008-01-01 01:12:31 +000073}
74
Dan Gohman3bc1a372009-04-15 01:17:37 +000075/// RemoveRegOperandFromRegInfo - Remove this register operand from the
76/// MachineRegisterInfo it is linked with.
77void MachineOperand::RemoveRegOperandFromRegInfo() {
78 assert(isOnRegUseList() && "Reg operand is not on a use list");
79 // Unlink this from the doubly linked list of operands.
80 MachineOperand *NextOp = Contents.Reg.Next;
81 *Contents.Reg.Prev = NextOp;
82 if (NextOp) {
83 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!");
84 NextOp->Contents.Reg.Prev = Contents.Reg.Prev;
85 }
86 Contents.Reg.Prev = 0;
87 Contents.Reg.Next = 0;
88}
89
Chris Lattner62ed6b92008-01-01 01:12:31 +000090void MachineOperand::setReg(unsigned Reg) {
91 if (getReg() == Reg) return; // No change.
92
93 // Otherwise, we have to change the register. If this operand is embedded
94 // into a machine function, we need to update the old and new register's
95 // use/def lists.
96 if (MachineInstr *MI = getParent())
97 if (MachineBasicBlock *MBB = MI->getParent())
98 if (MachineFunction *MF = MBB->getParent()) {
99 RemoveRegOperandFromRegInfo();
100 Contents.Reg.RegNo = Reg;
101 AddRegOperandToRegInfo(&MF->getRegInfo());
102 return;
103 }
104
105 // Otherwise, just change the register, no problem. :)
106 Contents.Reg.RegNo = Reg;
107}
108
109/// ChangeToImmediate - Replace this operand with a new immediate operand of
110/// the specified value. If an operand is known to be an immediate already,
111/// the setImm method should be used.
112void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
113 // If this operand is currently a register operand, and if this is in a
114 // function, deregister the operand from the register's use/def list.
Dan Gohmand735b802008-10-03 15:45:36 +0000115 if (isReg() && getParent() && getParent()->getParent() &&
Chris Lattner62ed6b92008-01-01 01:12:31 +0000116 getParent()->getParent()->getParent())
117 RemoveRegOperandFromRegInfo();
118
119 OpKind = MO_Immediate;
120 Contents.ImmVal = ImmVal;
121}
122
123/// ChangeToRegister - Replace this operand with a new register operand of
124/// the specified value. If an operand is known to be an register already,
125/// the setReg method should be used.
126void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
Evan Cheng4784f1f2009-06-30 08:49:04 +0000127 bool isKill, bool isDead, bool isUndef) {
Chris Lattner62ed6b92008-01-01 01:12:31 +0000128 // If this operand is already a register operand, use setReg to update the
129 // register's use/def lists.
Dan Gohmand735b802008-10-03 15:45:36 +0000130 if (isReg()) {
Dale Johannesene0091802008-09-14 01:44:36 +0000131 assert(!isEarlyClobber());
Chris Lattner62ed6b92008-01-01 01:12:31 +0000132 setReg(Reg);
133 } else {
134 // Otherwise, change this to a register and set the reg#.
135 OpKind = MO_Register;
136 Contents.Reg.RegNo = Reg;
137
138 // If this operand is embedded in a function, add the operand to the
139 // register's use/def list.
140 if (MachineInstr *MI = getParent())
141 if (MachineBasicBlock *MBB = MI->getParent())
142 if (MachineFunction *MF = MBB->getParent())
143 AddRegOperandToRegInfo(&MF->getRegInfo());
144 }
145
146 IsDef = isDef;
147 IsImp = isImp;
148 IsKill = isKill;
149 IsDead = isDead;
Evan Cheng4784f1f2009-06-30 08:49:04 +0000150 IsUndef = isUndef;
Dale Johannesene0091802008-09-14 01:44:36 +0000151 IsEarlyClobber = false;
Chris Lattner62ed6b92008-01-01 01:12:31 +0000152 SubReg = 0;
153}
154
Chris Lattnerf7382302007-12-30 21:56:09 +0000155/// isIdenticalTo - Return true if this operand is identical to the specified
156/// operand.
157bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
Chris Lattner31530612009-06-24 17:54:48 +0000158 if (getType() != Other.getType() ||
159 getTargetFlags() != Other.getTargetFlags())
160 return false;
Chris Lattnerf7382302007-12-30 21:56:09 +0000161
162 switch (getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000163 default: llvm_unreachable("Unrecognized operand type");
Chris Lattnerf7382302007-12-30 21:56:09 +0000164 case MachineOperand::MO_Register:
165 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
166 getSubReg() == Other.getSubReg();
167 case MachineOperand::MO_Immediate:
168 return getImm() == Other.getImm();
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000169 case MachineOperand::MO_FPImmediate:
170 return getFPImm() == Other.getFPImm();
Chris Lattnerf7382302007-12-30 21:56:09 +0000171 case MachineOperand::MO_MachineBasicBlock:
172 return getMBB() == Other.getMBB();
173 case MachineOperand::MO_FrameIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000174 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000175 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000176 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattnerf7382302007-12-30 21:56:09 +0000177 case MachineOperand::MO_JumpTableIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000178 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000179 case MachineOperand::MO_GlobalAddress:
180 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
181 case MachineOperand::MO_ExternalSymbol:
182 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
183 getOffset() == Other.getOffset();
Dan Gohman8c2b5252009-10-30 01:27:03 +0000184 case MachineOperand::MO_BlockAddress:
185 return getBlockAddress() == Other.getBlockAddress();
Chris Lattnerf7382302007-12-30 21:56:09 +0000186 }
187}
188
189/// print - Print the specified machine operand.
190///
Mon P Wang5ca6bd12008-10-10 01:43:55 +0000191void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const {
Chris Lattnerf7382302007-12-30 21:56:09 +0000192 switch (getType()) {
193 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000194 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000195 OS << "%reg" << getReg();
196 } else {
197 // If the instruction is embedded into a basic block, we can find the
Chris Lattner62ed6b92008-01-01 01:12:31 +0000198 // target info for the instruction.
Chris Lattnerf7382302007-12-30 21:56:09 +0000199 if (TM == 0)
200 if (const MachineInstr *MI = getParent())
201 if (const MachineBasicBlock *MBB = MI->getParent())
202 if (const MachineFunction *MF = MBB->getParent())
203 TM = &MF->getTarget();
204
205 if (TM)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000206 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
Chris Lattnerf7382302007-12-30 21:56:09 +0000207 else
Dan Gohman0ba90f32009-10-31 20:19:03 +0000208 OS << "%physreg" << getReg();
Chris Lattnerf7382302007-12-30 21:56:09 +0000209 }
Dan Gohman2ccc8392008-12-18 21:51:27 +0000210
Evan Cheng4784f1f2009-06-30 08:49:04 +0000211 if (getSubReg() != 0)
Chris Lattner31530612009-06-24 17:54:48 +0000212 OS << ':' << getSubReg();
Dan Gohman2ccc8392008-12-18 21:51:27 +0000213
Evan Cheng4784f1f2009-06-30 08:49:04 +0000214 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
215 isEarlyClobber()) {
Chris Lattner31530612009-06-24 17:54:48 +0000216 OS << '<';
Chris Lattnerf7382302007-12-30 21:56:09 +0000217 bool NeedComma = false;
Evan Cheng07897072009-10-14 23:37:31 +0000218 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,";
Evan Cheng07897072009-10-14 23:37:31 +0000222 if (isImplicit())
223 OS << "imp-";
Chris Lattnerf7382302007-12-30 21:56:09 +0000224 OS << "def";
225 NeedComma = true;
Evan Cheng5affca02009-10-21 07:56:02 +0000226 } else if (isImplicit()) {
Evan Cheng07897072009-10-14 23:37:31 +0000227 OS << "imp-use";
Evan Cheng5affca02009-10-21 07:56:02 +0000228 NeedComma = true;
229 }
Evan Cheng07897072009-10-14 23:37:31 +0000230
Evan Cheng4784f1f2009-06-30 08:49:04 +0000231 if (isKill() || isDead() || isUndef()) {
Chris Lattner31530612009-06-24 17:54:48 +0000232 if (NeedComma) OS << ',';
Bill Wendling181eb732008-02-24 00:56:13 +0000233 if (isKill()) OS << "kill";
234 if (isDead()) OS << "dead";
Evan Cheng4784f1f2009-06-30 08:49:04 +0000235 if (isUndef()) {
236 if (isKill() || isDead())
237 OS << ',';
238 OS << "undef";
239 }
Chris Lattnerf7382302007-12-30 21:56:09 +0000240 }
Chris Lattner31530612009-06-24 17:54:48 +0000241 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000242 }
243 break;
244 case MachineOperand::MO_Immediate:
245 OS << getImm();
246 break;
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000247 case MachineOperand::MO_FPImmediate:
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000248 if (getFPImm()->getType()->isFloatTy())
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000249 OS << getFPImm()->getValueAPF().convertToFloat();
Chris Lattner31530612009-06-24 17:54:48 +0000250 else
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000251 OS << getFPImm()->getValueAPF().convertToDouble();
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000252 break;
Chris Lattnerf7382302007-12-30 21:56:09 +0000253 case MachineOperand::MO_MachineBasicBlock:
Dan Gohman0ba90f32009-10-31 20:19:03 +0000254 OS << "<BB#" << getMBB()->getNumber() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000255 break;
256 case MachineOperand::MO_FrameIndex:
Chris Lattner31530612009-06-24 17:54:48 +0000257 OS << "<fi#" << getIndex() << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000258 break;
259 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000260 OS << "<cp#" << getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000261 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000262 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000263 break;
264 case MachineOperand::MO_JumpTableIndex:
Chris Lattner31530612009-06-24 17:54:48 +0000265 OS << "<jt#" << getIndex() << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000266 break;
267 case MachineOperand::MO_GlobalAddress:
268 OS << "<ga:" << ((Value*)getGlobal())->getName();
269 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000270 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000271 break;
272 case MachineOperand::MO_ExternalSymbol:
273 OS << "<es:" << getSymbolName();
274 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000275 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000276 break;
Dan Gohman8c2b5252009-10-30 01:27:03 +0000277 case MachineOperand::MO_BlockAddress:
Dan Gohman0ba90f32009-10-31 20:19:03 +0000278 OS << "<";
279 WriteAsOperand(OS, getBlockAddress(), /*PrintType=*/false);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000280 OS << '>';
281 break;
Chris Lattnerf7382302007-12-30 21:56:09 +0000282 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000283 llvm_unreachable("Unrecognized operand type");
Chris Lattnerf7382302007-12-30 21:56:09 +0000284 }
Chris Lattner31530612009-06-24 17:54:48 +0000285
286 if (unsigned TF = getTargetFlags())
287 OS << "[TF=" << TF << ']';
Chris Lattnerf7382302007-12-30 21:56:09 +0000288}
289
290//===----------------------------------------------------------------------===//
Dan Gohmance42e402008-07-07 20:32:02 +0000291// MachineMemOperand Implementation
292//===----------------------------------------------------------------------===//
293
294MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f,
295 int64_t o, uint64_t s, unsigned int a)
296 : Offset(o), Size(s), V(v),
297 Flags((f & 7) | ((Log2_32(a) + 1) << 3)) {
Dan Gohman28f02fd2009-09-21 19:47:04 +0000298 assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
Dan Gohmanc5e1f982008-07-16 15:56:42 +0000299 assert((isLoad() || isStore()) && "Not a load/store!");
Dan Gohmance42e402008-07-07 20:32:02 +0000300}
301
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000302/// Profile - Gather unique data for the object.
303///
304void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
305 ID.AddInteger(Offset);
306 ID.AddInteger(Size);
307 ID.AddPointer(V);
308 ID.AddInteger(Flags);
309}
310
Dan Gohmanc76909a2009-09-25 20:36:54 +0000311void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
312 // The Value and Offset may differ due to CSE. But the flags and size
313 // should be the same.
314 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
315 assert(MMO->getSize() == getSize() && "Size mismatch!");
316
317 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
318 // Update the alignment value.
319 Flags = (Flags & 7) | ((Log2_32(MMO->getBaseAlignment()) + 1) << 3);
320 // Also update the base and offset, because the new alignment may
321 // not be applicable with the old ones.
322 V = MMO->getValue();
323 Offset = MMO->getOffset();
324 }
325}
326
Dan Gohman4b2ebc12009-09-25 23:33:20 +0000327/// getAlignment - Return the minimum known alignment in bytes of the
328/// actual memory reference.
329uint64_t MachineMemOperand::getAlignment() const {
330 return MinAlign(getBaseAlignment(), getOffset());
331}
332
Dan Gohmanc76909a2009-09-25 20:36:54 +0000333raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) {
334 assert((MMO.isLoad() || MMO.isStore()) &&
Dan Gohmancd26ec52009-09-23 01:33:16 +0000335 "SV has to be a load, store or both.");
336
Dan Gohmanc76909a2009-09-25 20:36:54 +0000337 if (MMO.isVolatile())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000338 OS << "Volatile ";
339
Dan Gohmanc76909a2009-09-25 20:36:54 +0000340 if (MMO.isLoad())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000341 OS << "LD";
Dan Gohmanc76909a2009-09-25 20:36:54 +0000342 if (MMO.isStore())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000343 OS << "ST";
Dan Gohmanc76909a2009-09-25 20:36:54 +0000344 OS << MMO.getSize();
Dan Gohmancd26ec52009-09-23 01:33:16 +0000345
346 // Print the address information.
347 OS << "[";
Dan Gohmanc76909a2009-09-25 20:36:54 +0000348 if (!MMO.getValue())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000349 OS << "<unknown>";
350 else
Dan Gohmanc76909a2009-09-25 20:36:54 +0000351 WriteAsOperand(OS, MMO.getValue(), /*PrintType=*/false);
Dan Gohmancd26ec52009-09-23 01:33:16 +0000352
353 // If the alignment of the memory reference itself differs from the alignment
354 // of the base pointer, print the base alignment explicitly, next to the base
355 // pointer.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000356 if (MMO.getBaseAlignment() != MMO.getAlignment())
357 OS << "(align=" << MMO.getBaseAlignment() << ")";
Dan Gohmancd26ec52009-09-23 01:33:16 +0000358
Dan Gohmanc76909a2009-09-25 20:36:54 +0000359 if (MMO.getOffset() != 0)
360 OS << "+" << MMO.getOffset();
Dan Gohmancd26ec52009-09-23 01:33:16 +0000361 OS << "]";
362
363 // Print the alignment of the reference.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000364 if (MMO.getBaseAlignment() != MMO.getAlignment() ||
365 MMO.getBaseAlignment() != MMO.getSize())
366 OS << "(align=" << MMO.getAlignment() << ")";
Dan Gohmancd26ec52009-09-23 01:33:16 +0000367
368 return OS;
369}
370
Dan Gohmance42e402008-07-07 20:32:02 +0000371//===----------------------------------------------------------------------===//
Chris Lattnerf7382302007-12-30 21:56:09 +0000372// MachineInstr Implementation
373//===----------------------------------------------------------------------===//
374
Evan Chengc0f64ff2006-11-27 23:37:22 +0000375/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
Evan Cheng67f660c2006-11-30 07:08:44 +0000376/// TID NULL and no operands.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000377MachineInstr::MachineInstr()
Dan Gohmanc76909a2009-09-25 20:36:54 +0000378 : TID(0), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0),
379 Parent(0), debugLoc(DebugLoc::getUnknownLoc()) {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000380 // Make sure that we get added to a machine basicblock
381 LeakDetector::addGarbageObject(this);
Chris Lattner72791222002-10-28 20:59:49 +0000382}
383
Evan Cheng67f660c2006-11-30 07:08:44 +0000384void MachineInstr::addImplicitDefUseOperands() {
385 if (TID->ImplicitDefs)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000386 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Chris Lattner8019f412007-12-30 00:41:17 +0000387 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng67f660c2006-11-30 07:08:44 +0000388 if (TID->ImplicitUses)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000389 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Chris Lattner8019f412007-12-30 00:41:17 +0000390 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
Evan Chengd7de4962006-11-13 23:34:06 +0000391}
392
393/// MachineInstr ctor - This constructor create a MachineInstr and add the
Evan Chengc0f64ff2006-11-27 23:37:22 +0000394/// implicit operands. It reserves space for number of operands specified by
Chris Lattner749c6f62008-01-07 07:27:27 +0000395/// TargetInstrDesc or the numOperands if it is not zero. (for
Evan Chengc0f64ff2006-11-27 23:37:22 +0000396/// instructions with variable number of operands).
Chris Lattner749c6f62008-01-07 07:27:27 +0000397MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp)
Dan Gohmanc76909a2009-09-25 20:36:54 +0000398 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0), Parent(0),
Dale Johannesen06efc022009-01-27 23:20:29 +0000399 debugLoc(DebugLoc::getUnknownLoc()) {
Chris Lattner349c4952008-01-07 03:13:06 +0000400 if (!NoImp && TID->getImplicitDefs())
401 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000402 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000403 if (!NoImp && TID->getImplicitUses())
404 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000405 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000406 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Chengfa945722007-10-13 02:23:01 +0000407 if (!NoImp)
408 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000409 // Make sure that we get added to a machine basicblock
410 LeakDetector::addGarbageObject(this);
Evan Chengd7de4962006-11-13 23:34:06 +0000411}
412
Dale Johannesen06efc022009-01-27 23:20:29 +0000413/// MachineInstr ctor - As above, but with a DebugLoc.
414MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl,
415 bool NoImp)
Dan Gohmanc76909a2009-09-25 20:36:54 +0000416 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0),
417 Parent(0), debugLoc(dl) {
Dale Johannesen06efc022009-01-27 23:20:29 +0000418 if (!NoImp && TID->getImplicitDefs())
419 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
420 NumImplicitOps++;
421 if (!NoImp && TID->getImplicitUses())
422 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
423 NumImplicitOps++;
424 Operands.reserve(NumImplicitOps + TID->getNumOperands());
425 if (!NoImp)
426 addImplicitDefUseOperands();
427 // Make sure that we get added to a machine basicblock
428 LeakDetector::addGarbageObject(this);
429}
430
431/// MachineInstr ctor - Work exactly the same as the ctor two above, except
432/// that the MachineInstr is created and added to the end of the specified
433/// basic block.
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000434///
Dale Johannesen06efc022009-01-27 23:20:29 +0000435MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid)
Dan Gohmanc76909a2009-09-25 20:36:54 +0000436 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0), Parent(0),
Dale Johannesen06efc022009-01-27 23:20:29 +0000437 debugLoc(DebugLoc::getUnknownLoc()) {
438 assert(MBB && "Cannot use inserting ctor with null basic block!");
439 if (TID->ImplicitDefs)
440 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
441 NumImplicitOps++;
442 if (TID->ImplicitUses)
443 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
444 NumImplicitOps++;
445 Operands.reserve(NumImplicitOps + TID->getNumOperands());
446 addImplicitDefUseOperands();
447 // Make sure that we get added to a machine basicblock
448 LeakDetector::addGarbageObject(this);
449 MBB->push_back(this); // Add instruction to end of basic block!
450}
451
452/// MachineInstr ctor - As above, but with a DebugLoc.
453///
454MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl,
Chris Lattner749c6f62008-01-07 07:27:27 +0000455 const TargetInstrDesc &tid)
Dan Gohmanc76909a2009-09-25 20:36:54 +0000456 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0),
457 Parent(0), debugLoc(dl) {
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000458 assert(MBB && "Cannot use inserting ctor with null basic block!");
Evan Cheng67f660c2006-11-30 07:08:44 +0000459 if (TID->ImplicitDefs)
Chris Lattner349c4952008-01-07 03:13:06 +0000460 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000461 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +0000462 if (TID->ImplicitUses)
Chris Lattner349c4952008-01-07 03:13:06 +0000463 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000464 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000465 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Cheng67f660c2006-11-30 07:08:44 +0000466 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000467 // Make sure that we get added to a machine basicblock
468 LeakDetector::addGarbageObject(this);
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000469 MBB->push_back(this); // Add instruction to end of basic block!
470}
471
Misha Brukmance22e762004-07-09 14:45:17 +0000472/// MachineInstr ctor - Copies MachineInstr arg exactly
473///
Evan Cheng1ed99222008-07-19 00:37:25 +0000474MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Dan Gohmanc76909a2009-09-25 20:36:54 +0000475 : TID(&MI.getDesc()), NumImplicitOps(0),
476 MemRefs(MI.MemRefs), MemRefsEnd(MI.MemRefsEnd),
477 Parent(0), debugLoc(MI.getDebugLoc()) {
Chris Lattner943b5e12006-05-04 19:14:44 +0000478 Operands.reserve(MI.getNumOperands());
Tanya Lattnerb5159ed2004-05-23 20:58:02 +0000479
Misha Brukmance22e762004-07-09 14:45:17 +0000480 // Add operands
Evan Cheng1ed99222008-07-19 00:37:25 +0000481 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
482 addOperand(MI.getOperand(i));
483 NumImplicitOps = MI.NumImplicitOps;
Tanya Lattner0c63e032004-05-24 03:14:18 +0000484
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000485 // Set parent to null.
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000486 Parent = 0;
Dan Gohman6116a732008-07-21 18:47:29 +0000487
488 LeakDetector::addGarbageObject(this);
Tanya Lattner466b5342004-05-23 19:35:12 +0000489}
490
Misha Brukmance22e762004-07-09 14:45:17 +0000491MachineInstr::~MachineInstr() {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000492 LeakDetector::removeGarbageObject(this);
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000493#ifndef NDEBUG
Chris Lattner62ed6b92008-01-01 01:12:31 +0000494 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000495 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
Dan Gohmand735b802008-10-03 15:45:36 +0000496 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
Chris Lattner62ed6b92008-01-01 01:12:31 +0000497 "Reg operand def/use list corrupted");
498 }
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000499#endif
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000500}
501
Chris Lattner62ed6b92008-01-01 01:12:31 +0000502/// getRegInfo - If this instruction is embedded into a MachineFunction,
503/// return the MachineRegisterInfo object for the current function, otherwise
504/// return null.
505MachineRegisterInfo *MachineInstr::getRegInfo() {
506 if (MachineBasicBlock *MBB = getParent())
Dan Gohman4e526b92008-07-08 23:59:09 +0000507 return &MBB->getParent()->getRegInfo();
Chris Lattner62ed6b92008-01-01 01:12:31 +0000508 return 0;
509}
510
511/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
512/// this instruction from their respective use lists. This requires that the
513/// operands already be on their use lists.
514void MachineInstr::RemoveRegOperandsFromUseLists() {
515 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000516 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000517 Operands[i].RemoveRegOperandFromRegInfo();
518 }
519}
520
521/// AddRegOperandsToUseLists - Add all of the register operands in
522/// this instruction from their respective use lists. This requires that the
523/// operands not be on their use lists yet.
524void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
525 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000526 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000527 Operands[i].AddRegOperandToRegInfo(&RegInfo);
528 }
529}
530
531
532/// addOperand - Add the specified operand to the instruction. If it is an
533/// implicit operand, it is added to the end of the operand list. If it is
534/// an explicit operand it is added at the end of the explicit operand list
535/// (before the first implicit operand).
536void MachineInstr::addOperand(const MachineOperand &Op) {
Dan Gohmand735b802008-10-03 15:45:36 +0000537 bool isImpReg = Op.isReg() && Op.isImplicit();
Chris Lattner62ed6b92008-01-01 01:12:31 +0000538 assert((isImpReg || !OperandsComplete()) &&
539 "Trying to add an operand to a machine instr that is already done!");
540
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000541 MachineRegisterInfo *RegInfo = getRegInfo();
542
Chris Lattner62ed6b92008-01-01 01:12:31 +0000543 // If we are adding the operand to the end of the list, our job is simpler.
544 // This is true most of the time, so this is a reasonable optimization.
545 if (isImpReg || NumImplicitOps == 0) {
546 // We can only do this optimization if we know that the operand list won't
547 // reallocate.
548 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
549 Operands.push_back(Op);
550
551 // Set the parent of the operand.
552 Operands.back().ParentMI = this;
553
554 // If the operand is a register, update the operand's use list.
Dan Gohmand735b802008-10-03 15:45:36 +0000555 if (Op.isReg())
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000556 Operands.back().AddRegOperandToRegInfo(RegInfo);
Chris Lattner62ed6b92008-01-01 01:12:31 +0000557 return;
558 }
559 }
560
561 // Otherwise, we have to insert a real operand before any implicit ones.
562 unsigned OpNo = Operands.size()-NumImplicitOps;
563
Chris Lattner62ed6b92008-01-01 01:12:31 +0000564 // If this instruction isn't embedded into a function, then we don't need to
565 // update any operand lists.
566 if (RegInfo == 0) {
567 // Simple insertion, no reginfo update needed for other register operands.
568 Operands.insert(Operands.begin()+OpNo, Op);
569 Operands[OpNo].ParentMI = this;
570
571 // Do explicitly set the reginfo for this operand though, to ensure the
572 // next/prev fields are properly nulled out.
Dan Gohmand735b802008-10-03 15:45:36 +0000573 if (Operands[OpNo].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000574 Operands[OpNo].AddRegOperandToRegInfo(0);
575
576 } else if (Operands.size()+1 <= Operands.capacity()) {
577 // Otherwise, we have to remove register operands from their register use
578 // list, add the operand, then add the register operands back to their use
579 // list. This also must handle the case when the operand list reallocates
580 // to somewhere else.
581
582 // If insertion of this operand won't cause reallocation of the operand
583 // list, just remove the implicit operands, add the operand, then re-add all
584 // the rest of the operands.
585 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000586 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000587 Operands[i].RemoveRegOperandFromRegInfo();
588 }
589
590 // Add the operand. If it is a register, add it to the reg list.
591 Operands.insert(Operands.begin()+OpNo, Op);
592 Operands[OpNo].ParentMI = this;
593
Dan Gohmand735b802008-10-03 15:45:36 +0000594 if (Operands[OpNo].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000595 Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
596
597 // Re-add all the implicit ops.
598 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000599 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000600 Operands[i].AddRegOperandToRegInfo(RegInfo);
601 }
602 } else {
603 // Otherwise, we will be reallocating the operand list. Remove all reg
604 // operands from their list, then readd them after the operand list is
605 // reallocated.
606 RemoveRegOperandsFromUseLists();
607
608 Operands.insert(Operands.begin()+OpNo, Op);
609 Operands[OpNo].ParentMI = this;
610
611 // Re-add all the operands.
612 AddRegOperandsToUseLists(*RegInfo);
613 }
614}
615
616/// RemoveOperand - Erase an operand from an instruction, leaving it with one
617/// fewer operand than it started with.
618///
619void MachineInstr::RemoveOperand(unsigned OpNo) {
620 assert(OpNo < Operands.size() && "Invalid operand number");
621
622 // Special case removing the last one.
623 if (OpNo == Operands.size()-1) {
624 // If needed, remove from the reg def/use list.
Dan Gohmand735b802008-10-03 15:45:36 +0000625 if (Operands.back().isReg() && Operands.back().isOnRegUseList())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000626 Operands.back().RemoveRegOperandFromRegInfo();
627
628 Operands.pop_back();
629 return;
630 }
631
632 // Otherwise, we are removing an interior operand. If we have reginfo to
633 // update, remove all operands that will be shifted down from their reg lists,
634 // move everything down, then re-add them.
635 MachineRegisterInfo *RegInfo = getRegInfo();
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].RemoveRegOperandFromRegInfo();
640 }
641 }
642
643 Operands.erase(Operands.begin()+OpNo);
644
645 if (RegInfo) {
646 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000647 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000648 Operands[i].AddRegOperandToRegInfo(RegInfo);
649 }
650 }
651}
652
Dan Gohmanc76909a2009-09-25 20:36:54 +0000653/// addMemOperand - Add a MachineMemOperand to the machine instruction.
654/// This function should be used only occasionally. The setMemRefs function
655/// is the primary method for setting up a MachineInstr's MemRefs list.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000656void MachineInstr::addMemOperand(MachineFunction &MF,
Dan Gohmanc76909a2009-09-25 20:36:54 +0000657 MachineMemOperand *MO) {
658 mmo_iterator OldMemRefs = MemRefs;
659 mmo_iterator OldMemRefsEnd = MemRefsEnd;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000660
Dan Gohmanc76909a2009-09-25 20:36:54 +0000661 size_t NewNum = (MemRefsEnd - MemRefs) + 1;
662 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
663 mmo_iterator NewMemRefsEnd = NewMemRefs + NewNum;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000664
Dan Gohmanc76909a2009-09-25 20:36:54 +0000665 std::copy(OldMemRefs, OldMemRefsEnd, NewMemRefs);
666 NewMemRefs[NewNum - 1] = MO;
667
668 MemRefs = NewMemRefs;
669 MemRefsEnd = NewMemRefsEnd;
670}
Chris Lattner62ed6b92008-01-01 01:12:31 +0000671
Chris Lattner48d7c062006-04-17 21:35:41 +0000672/// removeFromParent - This method unlinks 'this' from the containing basic
673/// block, and returns it, but does not delete it.
674MachineInstr *MachineInstr::removeFromParent() {
675 assert(getParent() && "Not embedded in a basic block!");
676 getParent()->remove(this);
677 return this;
678}
679
680
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000681/// eraseFromParent - This method unlinks 'this' from the containing basic
682/// block, and deletes it.
683void MachineInstr::eraseFromParent() {
684 assert(getParent() && "Not embedded in a basic block!");
685 getParent()->erase(this);
686}
687
688
Brian Gaeke21326fc2004-02-13 04:39:32 +0000689/// OperandComplete - Return true if it's illegal to add a new operand
690///
Chris Lattner2a90ba62004-02-12 16:09:53 +0000691bool MachineInstr::OperandsComplete() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000692 unsigned short NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000693 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands)
Vikram S. Adve34977822003-05-31 07:39:06 +0000694 return true; // Broken: we have all the operands of this instruction!
Chris Lattner413746e2002-10-28 20:48:39 +0000695 return false;
696}
697
Evan Cheng19e3f312007-05-15 01:26:09 +0000698/// getNumExplicitOperands - Returns the number of non-implicit operands.
699///
700unsigned MachineInstr::getNumExplicitOperands() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000701 unsigned NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000702 if (!TID->isVariadic())
Evan Cheng19e3f312007-05-15 01:26:09 +0000703 return NumOperands;
704
Dan Gohman9407cd42009-04-15 17:59:11 +0000705 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
706 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000707 if (!MO.isReg() || !MO.isImplicit())
Evan Cheng19e3f312007-05-15 01:26:09 +0000708 NumOperands++;
709 }
710 return NumOperands;
711}
712
Chris Lattner8ace2cd2006-10-20 22:39:59 +0000713
Dan Gohman44066042008-07-01 00:05:16 +0000714/// isLabel - Returns true if the MachineInstr represents a label.
715///
716bool MachineInstr::isLabel() const {
717 return getOpcode() == TargetInstrInfo::DBG_LABEL ||
718 getOpcode() == TargetInstrInfo::EH_LABEL ||
719 getOpcode() == TargetInstrInfo::GC_LABEL;
720}
721
Evan Chengbb81d972008-01-31 09:59:15 +0000722/// isDebugLabel - Returns true if the MachineInstr represents a debug label.
723///
724bool MachineInstr::isDebugLabel() const {
Dan Gohman44066042008-07-01 00:05:16 +0000725 return getOpcode() == TargetInstrInfo::DBG_LABEL;
Evan Chengbb81d972008-01-31 09:59:15 +0000726}
727
Evan Chengfaa51072007-04-26 19:00:32 +0000728/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Jim Grosbachf9ca50e2009-09-17 17:57:26 +0000729/// the specific register or -1 if it is not found. It further tightens
Evan Cheng76d7e762007-02-23 01:04:26 +0000730/// the search criteria to a use that kills the register if isKill is true.
Evan Cheng6130f662008-03-05 00:59:57 +0000731int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
732 const TargetRegisterInfo *TRI) const {
Evan Cheng576d1232006-12-06 08:27:42 +0000733 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Chengf277ee42007-05-29 18:35:22 +0000734 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000735 if (!MO.isReg() || !MO.isUse())
Evan Cheng6130f662008-03-05 00:59:57 +0000736 continue;
737 unsigned MOReg = MO.getReg();
738 if (!MOReg)
739 continue;
740 if (MOReg == Reg ||
741 (TRI &&
742 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
743 TargetRegisterInfo::isPhysicalRegister(Reg) &&
744 TRI->isSubRegister(MOReg, Reg)))
Evan Cheng76d7e762007-02-23 01:04:26 +0000745 if (!isKill || MO.isKill())
Evan Cheng32eb1f12007-03-26 22:37:45 +0000746 return i;
Evan Cheng576d1232006-12-06 08:27:42 +0000747 }
Evan Cheng32eb1f12007-03-26 22:37:45 +0000748 return -1;
Evan Cheng576d1232006-12-06 08:27:42 +0000749}
750
Evan Cheng6130f662008-03-05 00:59:57 +0000751/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman703bfe62008-05-06 00:20:10 +0000752/// the specified register or -1 if it is not found. If isDead is true, defs
753/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
754/// also checks if there is a def of a super-register.
Evan Cheng6130f662008-03-05 00:59:57 +0000755int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead,
756 const TargetRegisterInfo *TRI) const {
Evan Chengb371f452007-02-19 21:49:54 +0000757 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng6130f662008-03-05 00:59:57 +0000758 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000759 if (!MO.isReg() || !MO.isDef())
Evan Cheng6130f662008-03-05 00:59:57 +0000760 continue;
761 unsigned MOReg = MO.getReg();
762 if (MOReg == Reg ||
763 (TRI &&
764 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
765 TargetRegisterInfo::isPhysicalRegister(Reg) &&
766 TRI->isSubRegister(MOReg, Reg)))
767 if (!isDead || MO.isDead())
768 return i;
Evan Chengb371f452007-02-19 21:49:54 +0000769 }
Evan Cheng6130f662008-03-05 00:59:57 +0000770 return -1;
Evan Chengb371f452007-02-19 21:49:54 +0000771}
Evan Cheng19e3f312007-05-15 01:26:09 +0000772
Evan Chengf277ee42007-05-29 18:35:22 +0000773/// findFirstPredOperandIdx() - Find the index of the first operand in the
774/// operand list that is used to represent the predicate. It returns -1 if
775/// none is found.
776int MachineInstr::findFirstPredOperandIdx() const {
Chris Lattner749c6f62008-01-07 07:27:27 +0000777 const TargetInstrDesc &TID = getDesc();
778 if (TID.isPredicable()) {
Evan Cheng19e3f312007-05-15 01:26:09 +0000779 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner749c6f62008-01-07 07:27:27 +0000780 if (TID.OpInfo[i].isPredicate())
Evan Chengf277ee42007-05-29 18:35:22 +0000781 return i;
Evan Cheng19e3f312007-05-15 01:26:09 +0000782 }
783
Evan Chengf277ee42007-05-29 18:35:22 +0000784 return -1;
Evan Cheng19e3f312007-05-15 01:26:09 +0000785}
Evan Chengb371f452007-02-19 21:49:54 +0000786
Bob Wilsond9df5012009-04-09 17:16:43 +0000787/// isRegTiedToUseOperand - Given the index of a register def operand,
788/// check if the register def is tied to a source operand, due to either
789/// two-address elimination or inline assembly constraints. Returns the
790/// first tied use operand index by reference is UseOpIdx is not null.
Jakob Stoklund Olesence9be2c2009-04-29 20:57:16 +0000791bool MachineInstr::
792isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const {
Evan Chengfb112882009-03-23 08:01:15 +0000793 if (getOpcode() == TargetInstrInfo::INLINEASM) {
Bob Wilsond9df5012009-04-09 17:16:43 +0000794 assert(DefOpIdx >= 2);
795 const MachineOperand &MO = getOperand(DefOpIdx);
Chris Lattnerc30aa7b2009-04-09 23:33:34 +0000796 if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
Evan Chengfb112882009-03-23 08:01:15 +0000797 return false;
Evan Chengef5d0702009-06-24 02:05:51 +0000798 // Determine the actual operand index that corresponds to this index.
Evan Chengfb112882009-03-23 08:01:15 +0000799 unsigned DefNo = 0;
Evan Chengef5d0702009-06-24 02:05:51 +0000800 unsigned DefPart = 0;
Evan Chengfb112882009-03-23 08:01:15 +0000801 for (unsigned i = 1, e = getNumOperands(); i < e; ) {
802 const MachineOperand &FMO = getOperand(i);
Jakob Stoklund Olesen45d34fe2009-07-19 19:09:59 +0000803 // After the normal asm operands there may be additional imp-def regs.
804 if (!FMO.isImm())
805 return false;
Evan Chengfb112882009-03-23 08:01:15 +0000806 // Skip over this def.
Evan Chengef5d0702009-06-24 02:05:51 +0000807 unsigned NumOps = InlineAsm::getNumOperandRegisters(FMO.getImm());
808 unsigned PrevDef = i + 1;
809 i = PrevDef + NumOps;
810 if (i > DefOpIdx) {
811 DefPart = DefOpIdx - PrevDef;
Evan Chengfb112882009-03-23 08:01:15 +0000812 break;
Evan Chengef5d0702009-06-24 02:05:51 +0000813 }
Evan Chengfb112882009-03-23 08:01:15 +0000814 ++DefNo;
815 }
Evan Chengef5d0702009-06-24 02:05:51 +0000816 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Evan Chengfb112882009-03-23 08:01:15 +0000817 const MachineOperand &FMO = getOperand(i);
818 if (!FMO.isImm())
819 continue;
820 if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse())
821 continue;
822 unsigned Idx;
Evan Chengef5d0702009-06-24 02:05:51 +0000823 if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) &&
Bob Wilsond9df5012009-04-09 17:16:43 +0000824 Idx == DefNo) {
825 if (UseOpIdx)
Evan Chengef5d0702009-06-24 02:05:51 +0000826 *UseOpIdx = (unsigned)i + 1 + DefPart;
Evan Chengfb112882009-03-23 08:01:15 +0000827 return true;
Bob Wilsond9df5012009-04-09 17:16:43 +0000828 }
Evan Chengfb112882009-03-23 08:01:15 +0000829 }
Evan Chengef5d0702009-06-24 02:05:51 +0000830 return false;
Evan Chengfb112882009-03-23 08:01:15 +0000831 }
832
Bob Wilsond9df5012009-04-09 17:16:43 +0000833 assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!");
Chris Lattner749c6f62008-01-07 07:27:27 +0000834 const TargetInstrDesc &TID = getDesc();
Evan Chengef0732d2008-07-10 07:35:43 +0000835 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
836 const MachineOperand &MO = getOperand(i);
Dan Gohman2ce7f202008-12-05 05:45:42 +0000837 if (MO.isReg() && MO.isUse() &&
Bob Wilsond9df5012009-04-09 17:16:43 +0000838 TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) {
839 if (UseOpIdx)
840 *UseOpIdx = (unsigned)i;
Evan Chengef0732d2008-07-10 07:35:43 +0000841 return true;
Bob Wilsond9df5012009-04-09 17:16:43 +0000842 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000843 }
844 return false;
845}
846
Evan Chenga24752f2009-03-19 20:30:06 +0000847/// isRegTiedToDefOperand - Return true if the operand of the specified index
848/// is a register use and it is tied to an def operand. It also returns the def
849/// operand index by reference.
Jakob Stoklund Olesence9be2c2009-04-29 20:57:16 +0000850bool MachineInstr::
851isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const {
Evan Chengfb112882009-03-23 08:01:15 +0000852 if (getOpcode() == TargetInstrInfo::INLINEASM) {
853 const MachineOperand &MO = getOperand(UseOpIdx);
Chris Lattner0c8382c2009-04-09 16:50:43 +0000854 if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0)
Evan Chengfb112882009-03-23 08:01:15 +0000855 return false;
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000856
857 // Find the flag operand corresponding to UseOpIdx
858 unsigned FlagIdx, NumOps=0;
859 for (FlagIdx = 1; FlagIdx < UseOpIdx; FlagIdx += NumOps+1) {
860 const MachineOperand &UFMO = getOperand(FlagIdx);
Jakob Stoklund Olesen45d34fe2009-07-19 19:09:59 +0000861 // After the normal asm operands there may be additional imp-def regs.
862 if (!UFMO.isImm())
863 return false;
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000864 NumOps = InlineAsm::getNumOperandRegisters(UFMO.getImm());
865 assert(NumOps < getNumOperands() && "Invalid inline asm flag");
866 if (UseOpIdx < FlagIdx+NumOps+1)
867 break;
Evan Chengef5d0702009-06-24 02:05:51 +0000868 }
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000869 if (FlagIdx >= UseOpIdx)
Evan Chengef5d0702009-06-24 02:05:51 +0000870 return false;
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000871 const MachineOperand &UFMO = getOperand(FlagIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000872 unsigned DefNo;
873 if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) {
874 if (!DefOpIdx)
875 return true;
876
877 unsigned DefIdx = 1;
878 // Remember to adjust the index. First operand is asm string, then there
879 // is a flag for each.
880 while (DefNo) {
881 const MachineOperand &FMO = getOperand(DefIdx);
882 assert(FMO.isImm());
883 // Skip over this def.
884 DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
885 --DefNo;
886 }
Evan Chengef5d0702009-06-24 02:05:51 +0000887 *DefOpIdx = DefIdx + UseOpIdx - FlagIdx;
Evan Chengfb112882009-03-23 08:01:15 +0000888 return true;
889 }
890 return false;
891 }
892
Evan Chenga24752f2009-03-19 20:30:06 +0000893 const TargetInstrDesc &TID = getDesc();
894 if (UseOpIdx >= TID.getNumOperands())
895 return false;
896 const MachineOperand &MO = getOperand(UseOpIdx);
897 if (!MO.isReg() || !MO.isUse())
898 return false;
899 int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO);
900 if (DefIdx == -1)
901 return false;
902 if (DefOpIdx)
903 *DefOpIdx = (unsigned)DefIdx;
904 return true;
905}
906
Evan Cheng576d1232006-12-06 08:27:42 +0000907/// copyKillDeadInfo - Copies kill / dead operand properties from MI.
908///
909void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
910 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
911 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000912 if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
Evan Cheng576d1232006-12-06 08:27:42 +0000913 continue;
914 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
915 MachineOperand &MOp = getOperand(j);
916 if (!MOp.isIdenticalTo(MO))
917 continue;
918 if (MO.isKill())
919 MOp.setIsKill();
920 else
921 MOp.setIsDead();
922 break;
923 }
924 }
925}
926
Evan Cheng19e3f312007-05-15 01:26:09 +0000927/// copyPredicates - Copies predicate operand(s) from MI.
928void MachineInstr::copyPredicates(const MachineInstr *MI) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000929 const TargetInstrDesc &TID = MI->getDesc();
Evan Chengb27087f2008-03-13 00:44:09 +0000930 if (!TID.isPredicable())
931 return;
932 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
933 if (TID.OpInfo[i].isPredicate()) {
934 // Predicated operands must be last operands.
935 addOperand(MI->getOperand(i));
Evan Cheng19e3f312007-05-15 01:26:09 +0000936 }
937 }
938}
939
Evan Cheng9f1c8312008-07-03 09:09:37 +0000940/// isSafeToMove - Return true if it is safe to move this instruction. If
941/// SawStore is set to true, it means that there is a store (or call) between
942/// the instruction's location and its intended destination.
Dan Gohmanb3b930a2008-11-18 19:04:29 +0000943bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII,
Dan Gohmana70dca12009-10-09 23:27:56 +0000944 bool &SawStore,
945 AliasAnalysis *AA) const {
Evan Chengb27087f2008-03-13 00:44:09 +0000946 // Ignore stuff that we obviously can't move.
947 if (TID->mayStore() || TID->isCall()) {
948 SawStore = true;
949 return false;
950 }
Dan Gohman237dee12008-12-23 17:28:50 +0000951 if (TID->isTerminator() || TID->hasUnmodeledSideEffects())
Evan Chengb27087f2008-03-13 00:44:09 +0000952 return false;
953
954 // See if this instruction does a load. If so, we have to guarantee that the
955 // loaded value doesn't change between the load and the its intended
956 // destination. The check for isInvariantLoad gives the targe the chance to
957 // classify the load as always returning a constant, e.g. a constant pool
958 // load.
Dan Gohmana70dca12009-10-09 23:27:56 +0000959 if (TID->mayLoad() && !isInvariantLoad(AA))
Evan Chengb27087f2008-03-13 00:44:09 +0000960 // Otherwise, this is a real load. If there is a store between the load and
Evan Cheng7cc2c402009-07-28 21:49:18 +0000961 // end of block, or if the load is volatile, we can't move it.
Dan Gohmand790a5c2008-10-02 15:04:30 +0000962 return !SawStore && !hasVolatileMemoryRef();
Dan Gohman3e4fb702008-09-24 00:06:15 +0000963
Evan Chengb27087f2008-03-13 00:44:09 +0000964 return true;
965}
966
Evan Chengdf3b9932008-08-27 20:33:50 +0000967/// isSafeToReMat - Return true if it's safe to rematerialize the specified
968/// instruction which defined the specified register instead of copying it.
Dan Gohmanb3b930a2008-11-18 19:04:29 +0000969bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII,
Dan Gohmana70dca12009-10-09 23:27:56 +0000970 unsigned DstReg,
971 AliasAnalysis *AA) const {
Evan Chengdf3b9932008-08-27 20:33:50 +0000972 bool SawStore = false;
Dan Gohmana70dca12009-10-09 23:27:56 +0000973 if (!TII->isTriviallyReMaterializable(this, AA) ||
974 !isSafeToMove(TII, SawStore, AA))
Evan Chengdf3b9932008-08-27 20:33:50 +0000975 return false;
976 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Dan Gohmancbad42c2008-11-18 19:49:32 +0000977 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000978 if (!MO.isReg())
Evan Chengdf3b9932008-08-27 20:33:50 +0000979 continue;
980 // FIXME: For now, do not remat any instruction with register operands.
981 // Later on, we can loosen the restriction is the register operands have
982 // not been modified between the def and use. Note, this is different from
Evan Cheng8763c1c2008-08-27 20:58:54 +0000983 // MachineSink because the code is no longer in two-address form (at least
Evan Chengdf3b9932008-08-27 20:33:50 +0000984 // partially).
985 if (MO.isUse())
986 return false;
987 else if (!MO.isDead() && MO.getReg() != DstReg)
988 return false;
989 }
990 return true;
991}
992
Dan Gohman3e4fb702008-09-24 00:06:15 +0000993/// hasVolatileMemoryRef - Return true if this instruction may have a
994/// volatile memory reference, or if the information describing the
995/// memory reference is not available. Return false if it is known to
996/// have no volatile memory references.
997bool MachineInstr::hasVolatileMemoryRef() const {
998 // An instruction known never to access memory won't have a volatile access.
999 if (!TID->mayStore() &&
1000 !TID->mayLoad() &&
1001 !TID->isCall() &&
1002 !TID->hasUnmodeledSideEffects())
1003 return false;
1004
1005 // Otherwise, if the instruction has no memory reference information,
1006 // conservatively assume it wasn't preserved.
1007 if (memoperands_empty())
1008 return true;
1009
1010 // Check the memory reference information for volatile references.
Dan Gohmanc76909a2009-09-25 20:36:54 +00001011 for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I)
1012 if ((*I)->isVolatile())
Dan Gohman3e4fb702008-09-24 00:06:15 +00001013 return true;
1014
1015 return false;
1016}
1017
Dan Gohmane33f44c2009-10-07 17:38:06 +00001018/// isInvariantLoad - Return true if this instruction is loading from a
1019/// location whose value is invariant across the function. For example,
1020/// loading a value from the constant pool or from from the argument area
1021/// of a function if it does not change. This should only return true of
1022/// *all* loads the instruction does are invariant (if it does multiple loads).
1023bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const {
1024 // If the instruction doesn't load at all, it isn't an invariant load.
1025 if (!TID->mayLoad())
1026 return false;
1027
1028 // If the instruction has lost its memoperands, conservatively assume that
1029 // it may not be an invariant load.
1030 if (memoperands_empty())
1031 return false;
1032
1033 const MachineFrameInfo *MFI = getParent()->getParent()->getFrameInfo();
1034
1035 for (mmo_iterator I = memoperands_begin(),
1036 E = memoperands_end(); I != E; ++I) {
1037 if ((*I)->isVolatile()) return false;
1038 if ((*I)->isStore()) return false;
1039
1040 if (const Value *V = (*I)->getValue()) {
1041 // A load from a constant PseudoSourceValue is invariant.
1042 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V))
1043 if (PSV->isConstant(MFI))
1044 continue;
1045 // If we have an AliasAnalysis, ask it whether the memory is constant.
1046 if (AA && AA->pointsToConstantMemory(V))
1047 continue;
1048 }
1049
1050 // Otherwise assume conservatively.
1051 return false;
1052 }
1053
1054 // Everything checks out.
1055 return true;
1056}
1057
Brian Gaeke21326fc2004-02-13 04:39:32 +00001058void MachineInstr::dump() const {
Chris Lattner705e07f2009-08-23 03:41:05 +00001059 errs() << " " << *this;
Mon P Wang5ca6bd12008-10-10 01:43:55 +00001060}
1061
1062void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const {
Dan Gohman0ba90f32009-10-31 20:19:03 +00001063 unsigned StartOp = 0, e = getNumOperands();
1064
1065 // Print explicitly defined operands on the left of an assignment syntax.
1066 for (; StartOp < e && getOperand(StartOp).isReg() &&
1067 getOperand(StartOp).isDef() &&
1068 !getOperand(StartOp).isImplicit();
1069 ++StartOp) {
1070 if (StartOp != 0) OS << ", ";
1071 getOperand(StartOp).print(OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +00001072 }
Tanya Lattnerb1407622004-06-25 00:13:11 +00001073
Dan Gohman0ba90f32009-10-31 20:19:03 +00001074 if (StartOp != 0)
1075 OS << " = ";
1076
1077 // Print the opcode name.
Chris Lattner749c6f62008-01-07 07:27:27 +00001078 OS << getDesc().getName();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001079
Dan Gohman0ba90f32009-10-31 20:19:03 +00001080 // Print the rest of the operands.
Chris Lattner6a592272002-10-30 01:55:38 +00001081 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
1082 if (i != StartOp)
1083 OS << ",";
1084 OS << " ";
Chris Lattnerf7382302007-12-30 21:56:09 +00001085 getOperand(i).print(OS, TM);
Chris Lattner10491642002-10-30 00:48:05 +00001086 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001087
Dan Gohman0ba90f32009-10-31 20:19:03 +00001088 bool HaveSemi = false;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001089 if (!memoperands_empty()) {
Dan Gohman0ba90f32009-10-31 20:19:03 +00001090 if (!HaveSemi) OS << ";"; HaveSemi = true;
1091
1092 OS << " mem:";
Dan Gohmanc76909a2009-09-25 20:36:54 +00001093 for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
1094 i != e; ++i) {
1095 OS << **i;
Dan Gohmancd26ec52009-09-23 01:33:16 +00001096 if (next(i) != e)
1097 OS << " ";
Dan Gohman69de1932008-02-06 22:27:42 +00001098 }
1099 }
1100
Bill Wendlingb5ef2732009-02-19 21:44:55 +00001101 if (!debugLoc.isUnknown()) {
Dan Gohman0ba90f32009-10-31 20:19:03 +00001102 if (!HaveSemi) OS << ";"; HaveSemi = true;
1103
1104 // TODO: print InlinedAtLoc information
1105
Bill Wendlingb5ef2732009-02-19 21:44:55 +00001106 const MachineFunction *MF = getParent()->getParent();
1107 DebugLocTuple DLT = MF->getDebugLocTuple(debugLoc);
Devang Patel1619dc32009-10-13 23:28:53 +00001108 DICompileUnit CU(DLT.Scope);
1109 if (!CU.isNull())
Dan Gohman0ba90f32009-10-31 20:19:03 +00001110 OS << " dbg:" << CU.getDirectory() << '/' << CU.getFilename() << ":"
1111 << DLT.Line << ":" << DLT.Col;
Bill Wendlingb5ef2732009-02-19 21:44:55 +00001112 }
1113
Chris Lattner10491642002-10-30 00:48:05 +00001114 OS << "\n";
1115}
1116
Owen Andersonb487e722008-01-24 01:10:07 +00001117bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001118 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +00001119 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001120 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001121 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +00001122 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001123 SmallVector<unsigned,4> DeadOps;
Bill Wendling4a23d722008-03-03 22:14:33 +00001124 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1125 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesenefb8e3e2009-08-04 20:09:25 +00001126 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001127 continue;
1128 unsigned Reg = MO.getReg();
1129 if (!Reg)
1130 continue;
Bill Wendling4a23d722008-03-03 22:14:33 +00001131
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001132 if (Reg == IncomingReg) {
Dan Gohman3f629402008-09-03 15:56:16 +00001133 if (!Found) {
1134 if (MO.isKill())
1135 // The register is already marked kill.
1136 return true;
Jakob Stoklund Olesenece48182009-08-02 19:13:03 +00001137 if (isPhysReg && isRegTiedToDefOperand(i))
1138 // Two-address uses of physregs must not be marked kill.
1139 return true;
Dan Gohman3f629402008-09-03 15:56:16 +00001140 MO.setIsKill();
1141 Found = true;
1142 }
1143 } else if (hasAliases && MO.isKill() &&
1144 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001145 // A super-register kill already exists.
1146 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001147 return true;
1148 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001149 DeadOps.push_back(i);
Bill Wendling4a23d722008-03-03 22:14:33 +00001150 }
1151 }
1152
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001153 // Trim unneeded kill operands.
1154 while (!DeadOps.empty()) {
1155 unsigned OpIdx = DeadOps.back();
1156 if (getOperand(OpIdx).isImplicit())
1157 RemoveOperand(OpIdx);
1158 else
1159 getOperand(OpIdx).setIsKill(false);
1160 DeadOps.pop_back();
1161 }
1162
Bill Wendling4a23d722008-03-03 22:14:33 +00001163 // If not found, this means an alias of one of the operands is killed. Add a
Owen Andersonb487e722008-01-24 01:10:07 +00001164 // new implicit operand if required.
Dan Gohman3f629402008-09-03 15:56:16 +00001165 if (!Found && AddIfNotFound) {
Bill Wendling4a23d722008-03-03 22:14:33 +00001166 addOperand(MachineOperand::CreateReg(IncomingReg,
1167 false /*IsDef*/,
1168 true /*IsImp*/,
1169 true /*IsKill*/));
Owen Andersonb487e722008-01-24 01:10:07 +00001170 return true;
1171 }
Dan Gohman3f629402008-09-03 15:56:16 +00001172 return Found;
Owen Andersonb487e722008-01-24 01:10:07 +00001173}
1174
1175bool MachineInstr::addRegisterDead(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001176 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +00001177 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001178 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Evan Cheng01b2e232008-06-27 22:11:49 +00001179 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +00001180 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001181 SmallVector<unsigned,4> DeadOps;
Owen Andersonb487e722008-01-24 01:10:07 +00001182 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1183 MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001184 if (!MO.isReg() || !MO.isDef())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001185 continue;
1186 unsigned Reg = MO.getReg();
Dan Gohman3f629402008-09-03 15:56:16 +00001187 if (!Reg)
1188 continue;
1189
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001190 if (Reg == IncomingReg) {
Dan Gohman3f629402008-09-03 15:56:16 +00001191 if (!Found) {
1192 if (MO.isDead())
1193 // The register is already marked dead.
1194 return true;
1195 MO.setIsDead();
1196 Found = true;
1197 }
1198 } else if (hasAliases && MO.isDead() &&
1199 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001200 // There exists a super-register that's marked dead.
1201 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001202 return true;
Owen Anderson22ae9992008-08-14 18:34:18 +00001203 if (RegInfo->getSubRegisters(IncomingReg) &&
1204 RegInfo->getSuperRegisters(Reg) &&
1205 RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001206 DeadOps.push_back(i);
Owen Andersonb487e722008-01-24 01:10:07 +00001207 }
1208 }
1209
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001210 // Trim unneeded dead operands.
1211 while (!DeadOps.empty()) {
1212 unsigned OpIdx = DeadOps.back();
1213 if (getOperand(OpIdx).isImplicit())
1214 RemoveOperand(OpIdx);
1215 else
1216 getOperand(OpIdx).setIsDead(false);
1217 DeadOps.pop_back();
1218 }
1219
Dan Gohman3f629402008-09-03 15:56:16 +00001220 // If not found, this means an alias of one of the operands is dead. Add a
1221 // new implicit operand if required.
Chris Lattner31530612009-06-24 17:54:48 +00001222 if (Found || !AddIfNotFound)
1223 return Found;
1224
1225 addOperand(MachineOperand::CreateReg(IncomingReg,
1226 true /*IsDef*/,
1227 true /*IsImp*/,
1228 false /*IsKill*/,
1229 true /*IsDead*/));
1230 return true;
Owen Andersonb487e722008-01-24 01:10:07 +00001231}