blob: 2dc99f5bb7137abe7802b858c92d6a54445a5bfe [file] [log] [blame]
Chris Lattner85093632008-01-01 20:36:19 +00001//===-- lib/CodeGen/MachineInstr.cpp --------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Methods common to all machine instructions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng2682ea02009-03-23 08:01:15 +000015#include "llvm/Constants.h"
16#include "llvm/InlineAsm.h"
Chris Lattner1b989192007-12-31 04:13:23 +000017#include "llvm/Value.h"
Dan Gohman915d8722009-09-23 01:33:16 +000018#include "llvm/Assembly/Writer.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/CodeGen/MachineFunction.h"
Dan Gohman4e3bb1b2009-09-25 20:36:54 +000020#include "llvm/CodeGen/MachineMemOperand.h"
Chris Lattnere45742f2008-01-01 01:12:31 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000022#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/Target/TargetMachine.h"
Evan Cheng13d1c292008-01-31 09:59:15 +000024#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner8eaa5a92008-01-07 07:42:25 +000025#include "llvm/Target/TargetInstrDesc.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000026#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmaneef78172009-10-07 17:38:06 +000027#include "llvm/Analysis/AliasAnalysis.h"
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +000028#include "llvm/Analysis/DebugInfo.h"
Edwin Török675d5622009-07-11 20:10:48 +000029#include "llvm/Support/ErrorHandling.h"
Dan Gohman8b3b5172008-07-17 23:49:46 +000030#include "llvm/Support/LeakDetector.h"
Dan Gohmanac6f8922008-07-07 20:32:02 +000031#include "llvm/Support/MathExtras.h"
Chris Lattner24ae2a92008-08-24 20:37:32 +000032#include "llvm/Support/raw_ostream.h"
Dan Gohman98beebe2008-08-20 15:58:01 +000033#include "llvm/ADT/FoldingSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034using namespace llvm;
35
Chris Lattner7f2d3b82007-12-30 21:56:09 +000036//===----------------------------------------------------------------------===//
37// MachineOperand Implementation
38//===----------------------------------------------------------------------===//
39
Chris Lattnere45742f2008-01-01 01:12:31 +000040/// AddRegOperandToRegInfo - Add this register operand to the specified
41/// MachineRegisterInfo. If it is null, then the next/prev fields should be
42/// explicitly nulled out.
43void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000044 assert(isReg() && "Can only add reg operand to use lists");
Chris Lattnere45742f2008-01-01 01:12:31 +000045
46 // If the reginfo pointer is null, just explicitly null out or next/prev
47 // pointers, to ensure they are not garbage.
48 if (RegInfo == 0) {
49 Contents.Reg.Prev = 0;
50 Contents.Reg.Next = 0;
51 return;
52 }
53
54 // Otherwise, add this operand to the head of the registers use/def list.
Chris Lattner6fc812d2008-01-01 21:08:22 +000055 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg());
Chris Lattnere45742f2008-01-01 01:12:31 +000056
Chris Lattner6fc812d2008-01-01 21:08:22 +000057 // For SSA values, we prefer to keep the definition at the start of the list.
58 // we do this by skipping over the definition if it is at the head of the
59 // list.
60 if (*Head && (*Head)->isDef())
61 Head = &(*Head)->Contents.Reg.Next;
62
63 Contents.Reg.Next = *Head;
Chris Lattnere45742f2008-01-01 01:12:31 +000064 if (Contents.Reg.Next) {
65 assert(getReg() == Contents.Reg.Next->getReg() &&
66 "Different regs on the same list!");
67 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next;
68 }
69
Chris Lattner6fc812d2008-01-01 21:08:22 +000070 Contents.Reg.Prev = Head;
71 *Head = this;
Chris Lattnere45742f2008-01-01 01:12:31 +000072}
73
Dan Gohman8ff914c2009-04-15 01:17:37 +000074/// RemoveRegOperandFromRegInfo - Remove this register operand from the
75/// MachineRegisterInfo it is linked with.
76void MachineOperand::RemoveRegOperandFromRegInfo() {
77 assert(isOnRegUseList() && "Reg operand is not on a use list");
78 // Unlink this from the doubly linked list of operands.
79 MachineOperand *NextOp = Contents.Reg.Next;
80 *Contents.Reg.Prev = NextOp;
81 if (NextOp) {
82 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!");
83 NextOp->Contents.Reg.Prev = Contents.Reg.Prev;
84 }
85 Contents.Reg.Prev = 0;
86 Contents.Reg.Next = 0;
87}
88
Chris Lattnere45742f2008-01-01 01:12:31 +000089void MachineOperand::setReg(unsigned Reg) {
90 if (getReg() == Reg) return; // No change.
91
92 // Otherwise, we have to change the register. If this operand is embedded
93 // into a machine function, we need to update the old and new register's
94 // use/def lists.
95 if (MachineInstr *MI = getParent())
96 if (MachineBasicBlock *MBB = MI->getParent())
97 if (MachineFunction *MF = MBB->getParent()) {
98 RemoveRegOperandFromRegInfo();
99 Contents.Reg.RegNo = Reg;
100 AddRegOperandToRegInfo(&MF->getRegInfo());
101 return;
102 }
103
104 // Otherwise, just change the register, no problem. :)
105 Contents.Reg.RegNo = Reg;
106}
107
108/// ChangeToImmediate - Replace this operand with a new immediate operand of
109/// the specified value. If an operand is known to be an immediate already,
110/// the setImm method should be used.
111void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
112 // If this operand is currently a register operand, and if this is in a
113 // function, deregister the operand from the register's use/def list.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000114 if (isReg() && getParent() && getParent()->getParent() &&
Chris Lattnere45742f2008-01-01 01:12:31 +0000115 getParent()->getParent()->getParent())
116 RemoveRegOperandFromRegInfo();
117
118 OpKind = MO_Immediate;
119 Contents.ImmVal = ImmVal;
120}
121
122/// ChangeToRegister - Replace this operand with a new register operand of
123/// the specified value. If an operand is known to be an register already,
124/// the setReg method should be used.
125void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
Evan Cheng9c73db12009-06-30 08:49:04 +0000126 bool isKill, bool isDead, bool isUndef) {
Chris Lattnere45742f2008-01-01 01:12:31 +0000127 // If this operand is already a register operand, use setReg to update the
128 // register's use/def lists.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000129 if (isReg()) {
Dale Johannesen1b0c5782008-09-14 01:44:36 +0000130 assert(!isEarlyClobber());
Chris Lattnere45742f2008-01-01 01:12:31 +0000131 setReg(Reg);
132 } else {
133 // Otherwise, change this to a register and set the reg#.
134 OpKind = MO_Register;
135 Contents.Reg.RegNo = Reg;
136
137 // If this operand is embedded in a function, add the operand to the
138 // register's use/def list.
139 if (MachineInstr *MI = getParent())
140 if (MachineBasicBlock *MBB = MI->getParent())
141 if (MachineFunction *MF = MBB->getParent())
142 AddRegOperandToRegInfo(&MF->getRegInfo());
143 }
144
145 IsDef = isDef;
146 IsImp = isImp;
147 IsKill = isKill;
148 IsDead = isDead;
Evan Cheng9c73db12009-06-30 08:49:04 +0000149 IsUndef = isUndef;
Dale Johannesen1b0c5782008-09-14 01:44:36 +0000150 IsEarlyClobber = false;
Chris Lattnere45742f2008-01-01 01:12:31 +0000151 SubReg = 0;
152}
153
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000154/// isIdenticalTo - Return true if this operand is identical to the specified
155/// operand.
156bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000157 if (getType() != Other.getType() ||
158 getTargetFlags() != Other.getTargetFlags())
159 return false;
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000160
161 switch (getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000162 default: llvm_unreachable("Unrecognized operand type");
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000163 case MachineOperand::MO_Register:
164 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
165 getSubReg() == Other.getSubReg();
166 case MachineOperand::MO_Immediate:
167 return getImm() == Other.getImm();
Nate Begeman6a38ec32008-02-14 07:39:30 +0000168 case MachineOperand::MO_FPImmediate:
169 return getFPImm() == Other.getFPImm();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000170 case MachineOperand::MO_MachineBasicBlock:
171 return getMBB() == Other.getMBB();
172 case MachineOperand::MO_FrameIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000173 return getIndex() == Other.getIndex();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000174 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000175 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000176 case MachineOperand::MO_JumpTableIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000177 return getIndex() == Other.getIndex();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000178 case MachineOperand::MO_GlobalAddress:
179 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
180 case MachineOperand::MO_ExternalSymbol:
181 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
182 getOffset() == Other.getOffset();
183 }
184}
185
186/// print - Print the specified machine operand.
187///
Mon P Wang2f2cd302008-10-10 01:43:55 +0000188void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000189 switch (getType()) {
190 case MachineOperand::MO_Register:
Dan Gohman1e57df32008-02-10 18:45:23 +0000191 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000192 OS << "%reg" << getReg();
193 } else {
194 // If the instruction is embedded into a basic block, we can find the
Chris Lattnere45742f2008-01-01 01:12:31 +0000195 // target info for the instruction.
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000196 if (TM == 0)
197 if (const MachineInstr *MI = getParent())
198 if (const MachineBasicBlock *MBB = MI->getParent())
199 if (const MachineFunction *MF = MBB->getParent())
200 TM = &MF->getTarget();
201
202 if (TM)
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000203 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000204 else
205 OS << "%mreg" << getReg();
206 }
Dan Gohman4849d102008-12-18 21:51:27 +0000207
Evan Cheng9c73db12009-06-30 08:49:04 +0000208 if (getSubReg() != 0)
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000209 OS << ':' << getSubReg();
Dan Gohman4849d102008-12-18 21:51:27 +0000210
Evan Cheng9c73db12009-06-30 08:49:04 +0000211 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
212 isEarlyClobber()) {
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000213 OS << '<';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000214 bool NeedComma = false;
215 if (isImplicit()) {
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000216 if (NeedComma) OS << ',';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000217 OS << (isDef() ? "imp-def" : "imp-use");
218 NeedComma = true;
219 } else if (isDef()) {
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000220 if (NeedComma) OS << ',';
Dale Johannesen38438f72008-09-12 17:49:03 +0000221 if (isEarlyClobber())
222 OS << "earlyclobber,";
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000223 OS << "def";
224 NeedComma = true;
225 }
Evan Cheng9c73db12009-06-30 08:49:04 +0000226 if (isKill() || isDead() || isUndef()) {
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000227 if (NeedComma) OS << ',';
Bill Wendling733f0fd2008-02-24 00:56:13 +0000228 if (isKill()) OS << "kill";
229 if (isDead()) OS << "dead";
Evan Cheng9c73db12009-06-30 08:49:04 +0000230 if (isUndef()) {
231 if (isKill() || isDead())
232 OS << ',';
233 OS << "undef";
234 }
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000235 }
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000236 OS << '>';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000237 }
238 break;
239 case MachineOperand::MO_Immediate:
240 OS << getImm();
241 break;
Nate Begeman6a38ec32008-02-14 07:39:30 +0000242 case MachineOperand::MO_FPImmediate:
Chris Lattner82cdc062009-10-05 05:54:46 +0000243 if (getFPImm()->getType()->isFloatTy())
Nate Begeman6a38ec32008-02-14 07:39:30 +0000244 OS << getFPImm()->getValueAPF().convertToFloat();
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000245 else
Nate Begeman6a38ec32008-02-14 07:39:30 +0000246 OS << getFPImm()->getValueAPF().convertToDouble();
Nate Begeman6a38ec32008-02-14 07:39:30 +0000247 break;
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000248 case MachineOperand::MO_MachineBasicBlock:
249 OS << "mbb<"
Chris Lattner6017d482007-12-30 23:10:15 +0000250 << ((Value*)getMBB()->getBasicBlock())->getName()
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000251 << "," << (void*)getMBB() << '>';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000252 break;
253 case MachineOperand::MO_FrameIndex:
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000254 OS << "<fi#" << getIndex() << '>';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000255 break;
256 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000257 OS << "<cp#" << getIndex();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000258 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000259 OS << '>';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000260 break;
261 case MachineOperand::MO_JumpTableIndex:
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000262 OS << "<jt#" << getIndex() << '>';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000263 break;
264 case MachineOperand::MO_GlobalAddress:
265 OS << "<ga:" << ((Value*)getGlobal())->getName();
266 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000267 OS << '>';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000268 break;
269 case MachineOperand::MO_ExternalSymbol:
270 OS << "<es:" << getSymbolName();
271 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000272 OS << '>';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000273 break;
274 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000275 llvm_unreachable("Unrecognized operand type");
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000276 }
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000277
278 if (unsigned TF = getTargetFlags())
279 OS << "[TF=" << TF << ']';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000280}
281
282//===----------------------------------------------------------------------===//
Dan Gohmanac6f8922008-07-07 20:32:02 +0000283// MachineMemOperand Implementation
284//===----------------------------------------------------------------------===//
285
286MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f,
287 int64_t o, uint64_t s, unsigned int a)
288 : Offset(o), Size(s), V(v),
289 Flags((f & 7) | ((Log2_32(a) + 1) << 3)) {
Dan Gohman169948d2009-09-21 19:47:04 +0000290 assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
Dan Gohman78f9a462008-07-16 15:56:42 +0000291 assert((isLoad() || isStore()) && "Not a load/store!");
Dan Gohmanac6f8922008-07-07 20:32:02 +0000292}
293
Dan Gohman98beebe2008-08-20 15:58:01 +0000294/// Profile - Gather unique data for the object.
295///
296void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
297 ID.AddInteger(Offset);
298 ID.AddInteger(Size);
299 ID.AddPointer(V);
300 ID.AddInteger(Flags);
301}
302
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000303void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
304 // The Value and Offset may differ due to CSE. But the flags and size
305 // should be the same.
306 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
307 assert(MMO->getSize() == getSize() && "Size mismatch!");
308
309 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
310 // Update the alignment value.
311 Flags = (Flags & 7) | ((Log2_32(MMO->getBaseAlignment()) + 1) << 3);
312 // Also update the base and offset, because the new alignment may
313 // not be applicable with the old ones.
314 V = MMO->getValue();
315 Offset = MMO->getOffset();
316 }
317}
318
Dan Gohman232e4442009-09-25 23:33:20 +0000319/// getAlignment - Return the minimum known alignment in bytes of the
320/// actual memory reference.
321uint64_t MachineMemOperand::getAlignment() const {
322 return MinAlign(getBaseAlignment(), getOffset());
323}
324
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000325raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) {
326 assert((MMO.isLoad() || MMO.isStore()) &&
Dan Gohman915d8722009-09-23 01:33:16 +0000327 "SV has to be a load, store or both.");
328
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000329 if (MMO.isVolatile())
Dan Gohman915d8722009-09-23 01:33:16 +0000330 OS << "Volatile ";
331
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000332 if (MMO.isLoad())
Dan Gohman915d8722009-09-23 01:33:16 +0000333 OS << "LD";
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000334 if (MMO.isStore())
Dan Gohman915d8722009-09-23 01:33:16 +0000335 OS << "ST";
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000336 OS << MMO.getSize();
Dan Gohman915d8722009-09-23 01:33:16 +0000337
338 // Print the address information.
339 OS << "[";
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000340 if (!MMO.getValue())
Dan Gohman915d8722009-09-23 01:33:16 +0000341 OS << "<unknown>";
342 else
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000343 WriteAsOperand(OS, MMO.getValue(), /*PrintType=*/false);
Dan Gohman915d8722009-09-23 01:33:16 +0000344
345 // If the alignment of the memory reference itself differs from the alignment
346 // of the base pointer, print the base alignment explicitly, next to the base
347 // pointer.
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000348 if (MMO.getBaseAlignment() != MMO.getAlignment())
349 OS << "(align=" << MMO.getBaseAlignment() << ")";
Dan Gohman915d8722009-09-23 01:33:16 +0000350
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000351 if (MMO.getOffset() != 0)
352 OS << "+" << MMO.getOffset();
Dan Gohman915d8722009-09-23 01:33:16 +0000353 OS << "]";
354
355 // Print the alignment of the reference.
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000356 if (MMO.getBaseAlignment() != MMO.getAlignment() ||
357 MMO.getBaseAlignment() != MMO.getSize())
358 OS << "(align=" << MMO.getAlignment() << ")";
Dan Gohman915d8722009-09-23 01:33:16 +0000359
360 return OS;
361}
362
Dan Gohmanac6f8922008-07-07 20:32:02 +0000363//===----------------------------------------------------------------------===//
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000364// MachineInstr Implementation
365//===----------------------------------------------------------------------===//
366
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
368/// TID NULL and no operands.
369MachineInstr::MachineInstr()
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000370 : TID(0), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0),
371 Parent(0), debugLoc(DebugLoc::getUnknownLoc()) {
Dan Gohman8b3b5172008-07-17 23:49:46 +0000372 // Make sure that we get added to a machine basicblock
373 LeakDetector::addGarbageObject(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374}
375
376void MachineInstr::addImplicitDefUseOperands() {
377 if (TID->ImplicitDefs)
Chris Lattner720b6cf2007-12-30 00:12:25 +0000378 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Chris Lattner63ab1f22007-12-30 00:41:17 +0000379 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 if (TID->ImplicitUses)
Chris Lattner720b6cf2007-12-30 00:12:25 +0000381 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Chris Lattner63ab1f22007-12-30 00:41:17 +0000382 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383}
384
385/// MachineInstr ctor - This constructor create a MachineInstr and add the
386/// implicit operands. It reserves space for number of operands specified by
Chris Lattner5b930372008-01-07 07:27:27 +0000387/// TargetInstrDesc or the numOperands if it is not zero. (for
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000388/// instructions with variable number of operands).
Chris Lattner5b930372008-01-07 07:27:27 +0000389MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp)
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000390 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0), Parent(0),
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000391 debugLoc(DebugLoc::getUnknownLoc()) {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000392 if (!NoImp && TID->getImplicitDefs())
393 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 NumImplicitOps++;
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000395 if (!NoImp && TID->getImplicitUses())
396 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397 NumImplicitOps++;
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000398 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Chengbdf72b42007-10-13 02:23:01 +0000399 if (!NoImp)
400 addImplicitDefUseOperands();
Dan Gohman8b3b5172008-07-17 23:49:46 +0000401 // Make sure that we get added to a machine basicblock
402 LeakDetector::addGarbageObject(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403}
404
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000405/// MachineInstr ctor - As above, but with a DebugLoc.
406MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl,
407 bool NoImp)
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000408 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0),
409 Parent(0), debugLoc(dl) {
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000410 if (!NoImp && TID->getImplicitDefs())
411 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
412 NumImplicitOps++;
413 if (!NoImp && TID->getImplicitUses())
414 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
415 NumImplicitOps++;
416 Operands.reserve(NumImplicitOps + TID->getNumOperands());
417 if (!NoImp)
418 addImplicitDefUseOperands();
419 // Make sure that we get added to a machine basicblock
420 LeakDetector::addGarbageObject(this);
421}
422
423/// MachineInstr ctor - Work exactly the same as the ctor two above, except
424/// that the MachineInstr is created and added to the end of the specified
425/// basic block.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426///
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000427MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid)
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000428 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0), Parent(0),
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000429 debugLoc(DebugLoc::getUnknownLoc()) {
430 assert(MBB && "Cannot use inserting ctor with null basic block!");
431 if (TID->ImplicitDefs)
432 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
433 NumImplicitOps++;
434 if (TID->ImplicitUses)
435 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
436 NumImplicitOps++;
437 Operands.reserve(NumImplicitOps + TID->getNumOperands());
438 addImplicitDefUseOperands();
439 // Make sure that we get added to a machine basicblock
440 LeakDetector::addGarbageObject(this);
441 MBB->push_back(this); // Add instruction to end of basic block!
442}
443
444/// MachineInstr ctor - As above, but with a DebugLoc.
445///
446MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl,
Chris Lattner5b930372008-01-07 07:27:27 +0000447 const TargetInstrDesc &tid)
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000448 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0),
449 Parent(0), debugLoc(dl) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 assert(MBB && "Cannot use inserting ctor with null basic block!");
451 if (TID->ImplicitDefs)
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000452 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 NumImplicitOps++;
454 if (TID->ImplicitUses)
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000455 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 NumImplicitOps++;
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000457 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 addImplicitDefUseOperands();
Dan Gohman8b3b5172008-07-17 23:49:46 +0000459 // Make sure that we get added to a machine basicblock
460 LeakDetector::addGarbageObject(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461 MBB->push_back(this); // Add instruction to end of basic block!
462}
463
464/// MachineInstr ctor - Copies MachineInstr arg exactly
465///
Evan Cheng4ce1a522008-07-19 00:37:25 +0000466MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000467 : TID(&MI.getDesc()), NumImplicitOps(0),
468 MemRefs(MI.MemRefs), MemRefsEnd(MI.MemRefsEnd),
469 Parent(0), debugLoc(MI.getDebugLoc()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470 Operands.reserve(MI.getNumOperands());
471
472 // Add operands
Evan Cheng4ce1a522008-07-19 00:37:25 +0000473 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
474 addOperand(MI.getOperand(i));
475 NumImplicitOps = MI.NumImplicitOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000476
Dan Gohman221a4372008-07-07 23:14:23 +0000477 // Set parent to null.
Chris Lattner7ce487f2007-12-31 04:56:33 +0000478 Parent = 0;
Dan Gohmance232952008-07-21 18:47:29 +0000479
480 LeakDetector::addGarbageObject(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000481}
482
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483MachineInstr::~MachineInstr() {
Dan Gohman8b3b5172008-07-17 23:49:46 +0000484 LeakDetector::removeGarbageObject(this);
Chris Lattnere722c3f2007-12-30 06:11:04 +0000485#ifndef NDEBUG
Chris Lattnere45742f2008-01-01 01:12:31 +0000486 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattnere722c3f2007-12-30 06:11:04 +0000487 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000488 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
Chris Lattnere45742f2008-01-01 01:12:31 +0000489 "Reg operand def/use list corrupted");
490 }
Chris Lattnere722c3f2007-12-30 06:11:04 +0000491#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492}
493
Chris Lattnere45742f2008-01-01 01:12:31 +0000494/// getRegInfo - If this instruction is embedded into a MachineFunction,
495/// return the MachineRegisterInfo object for the current function, otherwise
496/// return null.
497MachineRegisterInfo *MachineInstr::getRegInfo() {
498 if (MachineBasicBlock *MBB = getParent())
Dan Gohman07368822008-07-08 23:59:09 +0000499 return &MBB->getParent()->getRegInfo();
Chris Lattnere45742f2008-01-01 01:12:31 +0000500 return 0;
501}
502
503/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
504/// this instruction from their respective use lists. This requires that the
505/// operands already be on their use lists.
506void MachineInstr::RemoveRegOperandsFromUseLists() {
507 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000508 if (Operands[i].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000509 Operands[i].RemoveRegOperandFromRegInfo();
510 }
511}
512
513/// AddRegOperandsToUseLists - Add all of the register operands in
514/// this instruction from their respective use lists. This requires that the
515/// operands not be on their use lists yet.
516void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
517 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000518 if (Operands[i].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000519 Operands[i].AddRegOperandToRegInfo(&RegInfo);
520 }
521}
522
523
524/// addOperand - Add the specified operand to the instruction. If it is an
525/// implicit operand, it is added to the end of the operand list. If it is
526/// an explicit operand it is added at the end of the explicit operand list
527/// (before the first implicit operand).
528void MachineInstr::addOperand(const MachineOperand &Op) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000529 bool isImpReg = Op.isReg() && Op.isImplicit();
Chris Lattnere45742f2008-01-01 01:12:31 +0000530 assert((isImpReg || !OperandsComplete()) &&
531 "Trying to add an operand to a machine instr that is already done!");
532
Dan Gohmana0dff432008-12-09 22:45:08 +0000533 MachineRegisterInfo *RegInfo = getRegInfo();
534
Chris Lattnere45742f2008-01-01 01:12:31 +0000535 // If we are adding the operand to the end of the list, our job is simpler.
536 // This is true most of the time, so this is a reasonable optimization.
537 if (isImpReg || NumImplicitOps == 0) {
538 // We can only do this optimization if we know that the operand list won't
539 // reallocate.
540 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
541 Operands.push_back(Op);
542
543 // Set the parent of the operand.
544 Operands.back().ParentMI = this;
545
546 // If the operand is a register, update the operand's use list.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000547 if (Op.isReg())
Dan Gohmana0dff432008-12-09 22:45:08 +0000548 Operands.back().AddRegOperandToRegInfo(RegInfo);
Chris Lattnere45742f2008-01-01 01:12:31 +0000549 return;
550 }
551 }
552
553 // Otherwise, we have to insert a real operand before any implicit ones.
554 unsigned OpNo = Operands.size()-NumImplicitOps;
555
Chris Lattnere45742f2008-01-01 01:12:31 +0000556 // If this instruction isn't embedded into a function, then we don't need to
557 // update any operand lists.
558 if (RegInfo == 0) {
559 // Simple insertion, no reginfo update needed for other register operands.
560 Operands.insert(Operands.begin()+OpNo, Op);
561 Operands[OpNo].ParentMI = this;
562
563 // Do explicitly set the reginfo for this operand though, to ensure the
564 // next/prev fields are properly nulled out.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000565 if (Operands[OpNo].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000566 Operands[OpNo].AddRegOperandToRegInfo(0);
567
568 } else if (Operands.size()+1 <= Operands.capacity()) {
569 // Otherwise, we have to remove register operands from their register use
570 // list, add the operand, then add the register operands back to their use
571 // list. This also must handle the case when the operand list reallocates
572 // to somewhere else.
573
574 // If insertion of this operand won't cause reallocation of the operand
575 // list, just remove the implicit operands, add the operand, then re-add all
576 // the rest of the operands.
577 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000578 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattnere45742f2008-01-01 01:12:31 +0000579 Operands[i].RemoveRegOperandFromRegInfo();
580 }
581
582 // Add the operand. If it is a register, add it to the reg list.
583 Operands.insert(Operands.begin()+OpNo, Op);
584 Operands[OpNo].ParentMI = this;
585
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000586 if (Operands[OpNo].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000587 Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
588
589 // Re-add all the implicit ops.
590 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000591 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattnere45742f2008-01-01 01:12:31 +0000592 Operands[i].AddRegOperandToRegInfo(RegInfo);
593 }
594 } else {
595 // Otherwise, we will be reallocating the operand list. Remove all reg
596 // operands from their list, then readd them after the operand list is
597 // reallocated.
598 RemoveRegOperandsFromUseLists();
599
600 Operands.insert(Operands.begin()+OpNo, Op);
601 Operands[OpNo].ParentMI = this;
602
603 // Re-add all the operands.
604 AddRegOperandsToUseLists(*RegInfo);
605 }
606}
607
608/// RemoveOperand - Erase an operand from an instruction, leaving it with one
609/// fewer operand than it started with.
610///
611void MachineInstr::RemoveOperand(unsigned OpNo) {
612 assert(OpNo < Operands.size() && "Invalid operand number");
613
614 // Special case removing the last one.
615 if (OpNo == Operands.size()-1) {
616 // If needed, remove from the reg def/use list.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000617 if (Operands.back().isReg() && Operands.back().isOnRegUseList())
Chris Lattnere45742f2008-01-01 01:12:31 +0000618 Operands.back().RemoveRegOperandFromRegInfo();
619
620 Operands.pop_back();
621 return;
622 }
623
624 // Otherwise, we are removing an interior operand. If we have reginfo to
625 // update, remove all operands that will be shifted down from their reg lists,
626 // move everything down, then re-add them.
627 MachineRegisterInfo *RegInfo = getRegInfo();
628 if (RegInfo) {
629 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000630 if (Operands[i].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000631 Operands[i].RemoveRegOperandFromRegInfo();
632 }
633 }
634
635 Operands.erase(Operands.begin()+OpNo);
636
637 if (RegInfo) {
638 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000639 if (Operands[i].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000640 Operands[i].AddRegOperandToRegInfo(RegInfo);
641 }
642 }
643}
644
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000645/// addMemOperand - Add a MachineMemOperand to the machine instruction.
646/// This function should be used only occasionally. The setMemRefs function
647/// is the primary method for setting up a MachineInstr's MemRefs list.
Dan Gohman221a4372008-07-07 23:14:23 +0000648void MachineInstr::addMemOperand(MachineFunction &MF,
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000649 MachineMemOperand *MO) {
650 mmo_iterator OldMemRefs = MemRefs;
651 mmo_iterator OldMemRefsEnd = MemRefsEnd;
Dan Gohman221a4372008-07-07 23:14:23 +0000652
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000653 size_t NewNum = (MemRefsEnd - MemRefs) + 1;
654 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
655 mmo_iterator NewMemRefsEnd = NewMemRefs + NewNum;
Dan Gohman221a4372008-07-07 23:14:23 +0000656
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000657 std::copy(OldMemRefs, OldMemRefsEnd, NewMemRefs);
658 NewMemRefs[NewNum - 1] = MO;
659
660 MemRefs = NewMemRefs;
661 MemRefsEnd = NewMemRefsEnd;
662}
Chris Lattnere45742f2008-01-01 01:12:31 +0000663
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664/// removeFromParent - This method unlinks 'this' from the containing basic
665/// block, and returns it, but does not delete it.
666MachineInstr *MachineInstr::removeFromParent() {
667 assert(getParent() && "Not embedded in a basic block!");
668 getParent()->remove(this);
669 return this;
670}
671
672
Dan Gohman221a4372008-07-07 23:14:23 +0000673/// eraseFromParent - This method unlinks 'this' from the containing basic
674/// block, and deletes it.
675void MachineInstr::eraseFromParent() {
676 assert(getParent() && "Not embedded in a basic block!");
677 getParent()->erase(this);
678}
679
680
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000681/// OperandComplete - Return true if it's illegal to add a new operand
682///
683bool MachineInstr::OperandsComplete() const {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000684 unsigned short NumOperands = TID->getNumOperands();
Chris Lattner2fb37c02008-01-07 05:19:29 +0000685 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686 return true; // Broken: we have all the operands of this instruction!
687 return false;
688}
689
690/// getNumExplicitOperands - Returns the number of non-implicit operands.
691///
692unsigned MachineInstr::getNumExplicitOperands() const {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000693 unsigned NumOperands = TID->getNumOperands();
Chris Lattner2fb37c02008-01-07 05:19:29 +0000694 if (!TID->isVariadic())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 return NumOperands;
696
Dan Gohman3d880012009-04-15 17:59:11 +0000697 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
698 const MachineOperand &MO = getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000699 if (!MO.isReg() || !MO.isImplicit())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700 NumOperands++;
701 }
702 return NumOperands;
703}
704
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705
Dan Gohmanfa607c92008-07-01 00:05:16 +0000706/// isLabel - Returns true if the MachineInstr represents a label.
707///
708bool MachineInstr::isLabel() const {
709 return getOpcode() == TargetInstrInfo::DBG_LABEL ||
710 getOpcode() == TargetInstrInfo::EH_LABEL ||
711 getOpcode() == TargetInstrInfo::GC_LABEL;
712}
713
Evan Cheng13d1c292008-01-31 09:59:15 +0000714/// isDebugLabel - Returns true if the MachineInstr represents a debug label.
715///
716bool MachineInstr::isDebugLabel() const {
Dan Gohmanfa607c92008-07-01 00:05:16 +0000717 return getOpcode() == TargetInstrInfo::DBG_LABEL;
Evan Cheng13d1c292008-01-31 09:59:15 +0000718}
719
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000720/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Jim Grosbach47472be2009-09-17 17:57:26 +0000721/// the specific register or -1 if it is not found. It further tightens
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000722/// the search criteria to a use that kills the register if isKill is true.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000723int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
724 const TargetRegisterInfo *TRI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000725 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
726 const MachineOperand &MO = getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000727 if (!MO.isReg() || !MO.isUse())
Evan Chengc7daf1f2008-03-05 00:59:57 +0000728 continue;
729 unsigned MOReg = MO.getReg();
730 if (!MOReg)
731 continue;
732 if (MOReg == Reg ||
733 (TRI &&
734 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
735 TargetRegisterInfo::isPhysicalRegister(Reg) &&
736 TRI->isSubRegister(MOReg, Reg)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000737 if (!isKill || MO.isKill())
738 return i;
739 }
740 return -1;
741}
742
Evan Chengc7daf1f2008-03-05 00:59:57 +0000743/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman2f51e1f2008-05-06 00:20:10 +0000744/// the specified register or -1 if it is not found. If isDead is true, defs
745/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
746/// also checks if there is a def of a super-register.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000747int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead,
748 const TargetRegisterInfo *TRI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000749 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Chengc7daf1f2008-03-05 00:59:57 +0000750 const MachineOperand &MO = getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000751 if (!MO.isReg() || !MO.isDef())
Evan Chengc7daf1f2008-03-05 00:59:57 +0000752 continue;
753 unsigned MOReg = MO.getReg();
754 if (MOReg == Reg ||
755 (TRI &&
756 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
757 TargetRegisterInfo::isPhysicalRegister(Reg) &&
758 TRI->isSubRegister(MOReg, Reg)))
759 if (!isDead || MO.isDead())
760 return i;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000761 }
Evan Chengc7daf1f2008-03-05 00:59:57 +0000762 return -1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000763}
764
765/// findFirstPredOperandIdx() - Find the index of the first operand in the
766/// operand list that is used to represent the predicate. It returns -1 if
767/// none is found.
768int MachineInstr::findFirstPredOperandIdx() const {
Chris Lattner5b930372008-01-07 07:27:27 +0000769 const TargetInstrDesc &TID = getDesc();
770 if (TID.isPredicable()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000771 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner5b930372008-01-07 07:27:27 +0000772 if (TID.OpInfo[i].isPredicate())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000773 return i;
774 }
775
776 return -1;
777}
778
Bob Wilsonaded9952009-04-09 17:16:43 +0000779/// isRegTiedToUseOperand - Given the index of a register def operand,
780/// check if the register def is tied to a source operand, due to either
781/// two-address elimination or inline assembly constraints. Returns the
782/// first tied use operand index by reference is UseOpIdx is not null.
Jakob Stoklund Olesencb9f5b52009-04-29 20:57:16 +0000783bool MachineInstr::
784isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const {
Evan Cheng2682ea02009-03-23 08:01:15 +0000785 if (getOpcode() == TargetInstrInfo::INLINEASM) {
Bob Wilsonaded9952009-04-09 17:16:43 +0000786 assert(DefOpIdx >= 2);
787 const MachineOperand &MO = getOperand(DefOpIdx);
Chris Lattnerb1e84232009-04-09 23:33:34 +0000788 if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
Evan Cheng2682ea02009-03-23 08:01:15 +0000789 return false;
Evan Cheng3df52f72009-06-24 02:05:51 +0000790 // Determine the actual operand index that corresponds to this index.
Evan Cheng2682ea02009-03-23 08:01:15 +0000791 unsigned DefNo = 0;
Evan Cheng3df52f72009-06-24 02:05:51 +0000792 unsigned DefPart = 0;
Evan Cheng2682ea02009-03-23 08:01:15 +0000793 for (unsigned i = 1, e = getNumOperands(); i < e; ) {
794 const MachineOperand &FMO = getOperand(i);
Jakob Stoklund Olesen458bbfb2009-07-19 19:09:59 +0000795 // After the normal asm operands there may be additional imp-def regs.
796 if (!FMO.isImm())
797 return false;
Evan Cheng2682ea02009-03-23 08:01:15 +0000798 // Skip over this def.
Evan Cheng3df52f72009-06-24 02:05:51 +0000799 unsigned NumOps = InlineAsm::getNumOperandRegisters(FMO.getImm());
800 unsigned PrevDef = i + 1;
801 i = PrevDef + NumOps;
802 if (i > DefOpIdx) {
803 DefPart = DefOpIdx - PrevDef;
Evan Cheng2682ea02009-03-23 08:01:15 +0000804 break;
Evan Cheng3df52f72009-06-24 02:05:51 +0000805 }
Evan Cheng2682ea02009-03-23 08:01:15 +0000806 ++DefNo;
807 }
Evan Cheng3df52f72009-06-24 02:05:51 +0000808 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Evan Cheng2682ea02009-03-23 08:01:15 +0000809 const MachineOperand &FMO = getOperand(i);
810 if (!FMO.isImm())
811 continue;
812 if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse())
813 continue;
814 unsigned Idx;
Evan Cheng3df52f72009-06-24 02:05:51 +0000815 if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) &&
Bob Wilsonaded9952009-04-09 17:16:43 +0000816 Idx == DefNo) {
817 if (UseOpIdx)
Evan Cheng3df52f72009-06-24 02:05:51 +0000818 *UseOpIdx = (unsigned)i + 1 + DefPart;
Evan Cheng2682ea02009-03-23 08:01:15 +0000819 return true;
Bob Wilsonaded9952009-04-09 17:16:43 +0000820 }
Evan Cheng2682ea02009-03-23 08:01:15 +0000821 }
Evan Cheng3df52f72009-06-24 02:05:51 +0000822 return false;
Evan Cheng2682ea02009-03-23 08:01:15 +0000823 }
824
Bob Wilsonaded9952009-04-09 17:16:43 +0000825 assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!");
Chris Lattner5b930372008-01-07 07:27:27 +0000826 const TargetInstrDesc &TID = getDesc();
Evan Chengf1107fd2008-07-10 07:35:43 +0000827 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
828 const MachineOperand &MO = getOperand(i);
Dan Gohman4dbf8792008-12-05 05:45:42 +0000829 if (MO.isReg() && MO.isUse() &&
Bob Wilsonaded9952009-04-09 17:16:43 +0000830 TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) {
831 if (UseOpIdx)
832 *UseOpIdx = (unsigned)i;
Evan Chengf1107fd2008-07-10 07:35:43 +0000833 return true;
Bob Wilsonaded9952009-04-09 17:16:43 +0000834 }
Evan Cheng687d1082007-10-12 08:50:34 +0000835 }
836 return false;
837}
838
Evan Cheng48555e82009-03-19 20:30:06 +0000839/// isRegTiedToDefOperand - Return true if the operand of the specified index
840/// is a register use and it is tied to an def operand. It also returns the def
841/// operand index by reference.
Jakob Stoklund Olesencb9f5b52009-04-29 20:57:16 +0000842bool MachineInstr::
843isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const {
Evan Cheng2682ea02009-03-23 08:01:15 +0000844 if (getOpcode() == TargetInstrInfo::INLINEASM) {
845 const MachineOperand &MO = getOperand(UseOpIdx);
Chris Lattner0d0e8a92009-04-09 16:50:43 +0000846 if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0)
Evan Cheng2682ea02009-03-23 08:01:15 +0000847 return false;
Jakob Stoklund Olesen4c769c32009-07-16 20:58:34 +0000848
849 // Find the flag operand corresponding to UseOpIdx
850 unsigned FlagIdx, NumOps=0;
851 for (FlagIdx = 1; FlagIdx < UseOpIdx; FlagIdx += NumOps+1) {
852 const MachineOperand &UFMO = getOperand(FlagIdx);
Jakob Stoklund Olesen458bbfb2009-07-19 19:09:59 +0000853 // After the normal asm operands there may be additional imp-def regs.
854 if (!UFMO.isImm())
855 return false;
Jakob Stoklund Olesen4c769c32009-07-16 20:58:34 +0000856 NumOps = InlineAsm::getNumOperandRegisters(UFMO.getImm());
857 assert(NumOps < getNumOperands() && "Invalid inline asm flag");
858 if (UseOpIdx < FlagIdx+NumOps+1)
859 break;
Evan Cheng3df52f72009-06-24 02:05:51 +0000860 }
Jakob Stoklund Olesen4c769c32009-07-16 20:58:34 +0000861 if (FlagIdx >= UseOpIdx)
Evan Cheng3df52f72009-06-24 02:05:51 +0000862 return false;
Jakob Stoklund Olesen4c769c32009-07-16 20:58:34 +0000863 const MachineOperand &UFMO = getOperand(FlagIdx);
Evan Cheng2682ea02009-03-23 08:01:15 +0000864 unsigned DefNo;
865 if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) {
866 if (!DefOpIdx)
867 return true;
868
869 unsigned DefIdx = 1;
870 // Remember to adjust the index. First operand is asm string, then there
871 // is a flag for each.
872 while (DefNo) {
873 const MachineOperand &FMO = getOperand(DefIdx);
874 assert(FMO.isImm());
875 // Skip over this def.
876 DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
877 --DefNo;
878 }
Evan Cheng3df52f72009-06-24 02:05:51 +0000879 *DefOpIdx = DefIdx + UseOpIdx - FlagIdx;
Evan Cheng2682ea02009-03-23 08:01:15 +0000880 return true;
881 }
882 return false;
883 }
884
Evan Cheng48555e82009-03-19 20:30:06 +0000885 const TargetInstrDesc &TID = getDesc();
886 if (UseOpIdx >= TID.getNumOperands())
887 return false;
888 const MachineOperand &MO = getOperand(UseOpIdx);
889 if (!MO.isReg() || !MO.isUse())
890 return false;
891 int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO);
892 if (DefIdx == -1)
893 return false;
894 if (DefOpIdx)
895 *DefOpIdx = (unsigned)DefIdx;
896 return true;
897}
898
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000899/// copyKillDeadInfo - Copies kill / dead operand properties from MI.
900///
901void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
902 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
903 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000904 if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000905 continue;
906 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
907 MachineOperand &MOp = getOperand(j);
908 if (!MOp.isIdenticalTo(MO))
909 continue;
910 if (MO.isKill())
911 MOp.setIsKill();
912 else
913 MOp.setIsDead();
914 break;
915 }
916 }
917}
918
919/// copyPredicates - Copies predicate operand(s) from MI.
920void MachineInstr::copyPredicates(const MachineInstr *MI) {
Chris Lattner5b930372008-01-07 07:27:27 +0000921 const TargetInstrDesc &TID = MI->getDesc();
Evan Chengbe856622008-03-13 00:44:09 +0000922 if (!TID.isPredicable())
923 return;
924 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
925 if (TID.OpInfo[i].isPredicate()) {
926 // Predicated operands must be last operands.
927 addOperand(MI->getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000928 }
929 }
930}
931
Evan Chenge52c1912008-07-03 09:09:37 +0000932/// isSafeToMove - Return true if it is safe to move this instruction. If
933/// SawStore is set to true, it means that there is a store (or call) between
934/// the instruction's location and its intended destination.
Dan Gohman9ca19a32008-11-18 19:04:29 +0000935bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII,
936 bool &SawStore) const {
Evan Chengbe856622008-03-13 00:44:09 +0000937 // Ignore stuff that we obviously can't move.
938 if (TID->mayStore() || TID->isCall()) {
939 SawStore = true;
940 return false;
941 }
Dan Gohman64709cd2008-12-23 17:28:50 +0000942 if (TID->isTerminator() || TID->hasUnmodeledSideEffects())
Evan Chengbe856622008-03-13 00:44:09 +0000943 return false;
944
945 // See if this instruction does a load. If so, we have to guarantee that the
946 // loaded value doesn't change between the load and the its intended
947 // destination. The check for isInvariantLoad gives the targe the chance to
948 // classify the load as always returning a constant, e.g. a constant pool
949 // load.
Dan Gohmaneef78172009-10-07 17:38:06 +0000950 if (TID->mayLoad() && !isInvariantLoad())
Evan Chengbe856622008-03-13 00:44:09 +0000951 // Otherwise, this is a real load. If there is a store between the load and
Evan Cheng79d87ec2009-07-28 21:49:18 +0000952 // end of block, or if the load is volatile, we can't move it.
Dan Gohman0ce00b82008-10-02 15:04:30 +0000953 return !SawStore && !hasVolatileMemoryRef();
Dan Gohman9ffbed82008-09-24 00:06:15 +0000954
Evan Chengbe856622008-03-13 00:44:09 +0000955 return true;
956}
957
Evan Cheng75e2cee2008-08-27 20:33:50 +0000958/// isSafeToReMat - Return true if it's safe to rematerialize the specified
959/// instruction which defined the specified register instead of copying it.
Dan Gohman9ca19a32008-11-18 19:04:29 +0000960bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII,
961 unsigned DstReg) const {
Evan Cheng75e2cee2008-08-27 20:33:50 +0000962 bool SawStore = false;
Evan Chenga07a9a22008-08-30 09:07:18 +0000963 if (!getDesc().isRematerializable() ||
964 !TII->isTriviallyReMaterializable(this) ||
965 !isSafeToMove(TII, SawStore))
Evan Cheng75e2cee2008-08-27 20:33:50 +0000966 return false;
967 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Dan Gohman90feee22008-11-18 19:49:32 +0000968 const MachineOperand &MO = getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000969 if (!MO.isReg())
Evan Cheng75e2cee2008-08-27 20:33:50 +0000970 continue;
971 // FIXME: For now, do not remat any instruction with register operands.
972 // Later on, we can loosen the restriction is the register operands have
973 // not been modified between the def and use. Note, this is different from
Evan Chenga02c6692008-08-27 20:58:54 +0000974 // MachineSink because the code is no longer in two-address form (at least
Evan Cheng75e2cee2008-08-27 20:33:50 +0000975 // partially).
976 if (MO.isUse())
977 return false;
978 else if (!MO.isDead() && MO.getReg() != DstReg)
979 return false;
980 }
981 return true;
982}
983
Dan Gohman9ffbed82008-09-24 00:06:15 +0000984/// hasVolatileMemoryRef - Return true if this instruction may have a
985/// volatile memory reference, or if the information describing the
986/// memory reference is not available. Return false if it is known to
987/// have no volatile memory references.
988bool MachineInstr::hasVolatileMemoryRef() const {
989 // An instruction known never to access memory won't have a volatile access.
990 if (!TID->mayStore() &&
991 !TID->mayLoad() &&
992 !TID->isCall() &&
993 !TID->hasUnmodeledSideEffects())
994 return false;
995
996 // Otherwise, if the instruction has no memory reference information,
997 // conservatively assume it wasn't preserved.
998 if (memoperands_empty())
999 return true;
1000
1001 // Check the memory reference information for volatile references.
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00001002 for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I)
1003 if ((*I)->isVolatile())
Dan Gohman9ffbed82008-09-24 00:06:15 +00001004 return true;
1005
1006 return false;
1007}
1008
Dan Gohmaneef78172009-10-07 17:38:06 +00001009/// isInvariantLoad - Return true if this instruction is loading from a
1010/// location whose value is invariant across the function. For example,
1011/// loading a value from the constant pool or from from the argument area
1012/// of a function if it does not change. This should only return true of
1013/// *all* loads the instruction does are invariant (if it does multiple loads).
1014bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const {
1015 // If the instruction doesn't load at all, it isn't an invariant load.
1016 if (!TID->mayLoad())
1017 return false;
1018
1019 // If the instruction has lost its memoperands, conservatively assume that
1020 // it may not be an invariant load.
1021 if (memoperands_empty())
1022 return false;
1023
1024 const MachineFrameInfo *MFI = getParent()->getParent()->getFrameInfo();
1025
1026 for (mmo_iterator I = memoperands_begin(),
1027 E = memoperands_end(); I != E; ++I) {
1028 if ((*I)->isVolatile()) return false;
1029 if ((*I)->isStore()) return false;
1030
1031 if (const Value *V = (*I)->getValue()) {
1032 // A load from a constant PseudoSourceValue is invariant.
1033 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V))
1034 if (PSV->isConstant(MFI))
1035 continue;
1036 // If we have an AliasAnalysis, ask it whether the memory is constant.
1037 if (AA && AA->pointsToConstantMemory(V))
1038 continue;
1039 }
1040
1041 // Otherwise assume conservatively.
1042 return false;
1043 }
1044
1045 // Everything checks out.
1046 return true;
1047}
1048
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001049void MachineInstr::dump() const {
Chris Lattnerd71b0b02009-08-23 03:41:05 +00001050 errs() << " " << *this;
Mon P Wang2f2cd302008-10-10 01:43:55 +00001051}
1052
1053void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const {
Chris Lattner9607bb82007-12-30 21:31:53 +00001054 // Specialize printing if op#0 is definition
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001055 unsigned StartOp = 0;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001056 if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +00001057 getOperand(0).print(OS, TM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058 OS << " = ";
1059 ++StartOp; // Don't print this operand again!
1060 }
1061
Chris Lattner5b930372008-01-07 07:27:27 +00001062 OS << getDesc().getName();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001063
1064 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001065 if (i != StartOp)
1066 OS << ",";
1067 OS << " ";
Chris Lattner7f2d3b82007-12-30 21:56:09 +00001068 getOperand(i).print(OS, TM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001069 }
1070
Dan Gohman221a4372008-07-07 23:14:23 +00001071 if (!memoperands_empty()) {
Dan Gohmanf738b652008-02-07 16:18:00 +00001072 OS << ", Mem:";
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00001073 for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
1074 i != e; ++i) {
1075 OS << **i;
Dan Gohman915d8722009-09-23 01:33:16 +00001076 if (next(i) != e)
1077 OS << " ";
Dan Gohman12a9c082008-02-06 22:27:42 +00001078 }
1079 }
1080
Bill Wendlingb7596d22009-02-19 21:44:55 +00001081 if (!debugLoc.isUnknown()) {
1082 const MachineFunction *MF = getParent()->getParent();
1083 DebugLocTuple DLT = MF->getDebugLocTuple(debugLoc);
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +00001084 DICompileUnit CU(DLT.CompileUnit);
Bill Wendlingb7596d22009-02-19 21:44:55 +00001085 OS << " [dbg: "
Devang Patelaaf012e2009-09-29 18:40:58 +00001086 << CU.getDirectory() << '/' << CU.getFilename() << ","
Bill Wendlingb7596d22009-02-19 21:44:55 +00001087 << DLT.Line << ","
1088 << DLT.Col << "]";
1089 }
1090
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001091 OS << "\n";
1092}
1093
Owen Anderson58060792008-01-24 01:10:07 +00001094bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman1e57df32008-02-10 18:45:23 +00001095 const TargetRegisterInfo *RegInfo,
Owen Anderson58060792008-01-24 01:10:07 +00001096 bool AddIfNotFound) {
Evan Cheng794d0f72008-04-16 09:41:59 +00001097 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Dan Gohman9d90c632008-07-03 01:18:51 +00001098 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman244b86a2008-09-03 15:56:16 +00001099 bool Found = false;
Evan Cheng794d0f72008-04-16 09:41:59 +00001100 SmallVector<unsigned,4> DeadOps;
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +00001101 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1102 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesenb409fff2009-08-04 20:09:25 +00001103 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng794d0f72008-04-16 09:41:59 +00001104 continue;
1105 unsigned Reg = MO.getReg();
1106 if (!Reg)
1107 continue;
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +00001108
Evan Cheng794d0f72008-04-16 09:41:59 +00001109 if (Reg == IncomingReg) {
Dan Gohman244b86a2008-09-03 15:56:16 +00001110 if (!Found) {
1111 if (MO.isKill())
1112 // The register is already marked kill.
1113 return true;
Jakob Stoklund Olesen7b8fe132009-08-02 19:13:03 +00001114 if (isPhysReg && isRegTiedToDefOperand(i))
1115 // Two-address uses of physregs must not be marked kill.
1116 return true;
Dan Gohman244b86a2008-09-03 15:56:16 +00001117 MO.setIsKill();
1118 Found = true;
1119 }
1120 } else if (hasAliases && MO.isKill() &&
1121 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng794d0f72008-04-16 09:41:59 +00001122 // A super-register kill already exists.
1123 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman9d90c632008-07-03 01:18:51 +00001124 return true;
1125 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng794d0f72008-04-16 09:41:59 +00001126 DeadOps.push_back(i);
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +00001127 }
1128 }
1129
Evan Cheng794d0f72008-04-16 09:41:59 +00001130 // Trim unneeded kill operands.
1131 while (!DeadOps.empty()) {
1132 unsigned OpIdx = DeadOps.back();
1133 if (getOperand(OpIdx).isImplicit())
1134 RemoveOperand(OpIdx);
1135 else
1136 getOperand(OpIdx).setIsKill(false);
1137 DeadOps.pop_back();
1138 }
1139
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +00001140 // If not found, this means an alias of one of the operands is killed. Add a
Owen Anderson58060792008-01-24 01:10:07 +00001141 // new implicit operand if required.
Dan Gohman244b86a2008-09-03 15:56:16 +00001142 if (!Found && AddIfNotFound) {
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +00001143 addOperand(MachineOperand::CreateReg(IncomingReg,
1144 false /*IsDef*/,
1145 true /*IsImp*/,
1146 true /*IsKill*/));
Owen Anderson58060792008-01-24 01:10:07 +00001147 return true;
1148 }
Dan Gohman244b86a2008-09-03 15:56:16 +00001149 return Found;
Owen Anderson58060792008-01-24 01:10:07 +00001150}
1151
1152bool MachineInstr::addRegisterDead(unsigned IncomingReg,
Dan Gohman1e57df32008-02-10 18:45:23 +00001153 const TargetRegisterInfo *RegInfo,
Owen Anderson58060792008-01-24 01:10:07 +00001154 bool AddIfNotFound) {
Evan Cheng794d0f72008-04-16 09:41:59 +00001155 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Evan Chengdd562a02008-06-27 22:11:49 +00001156 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman244b86a2008-09-03 15:56:16 +00001157 bool Found = false;
Evan Cheng794d0f72008-04-16 09:41:59 +00001158 SmallVector<unsigned,4> DeadOps;
Owen Anderson58060792008-01-24 01:10:07 +00001159 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1160 MachineOperand &MO = getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001161 if (!MO.isReg() || !MO.isDef())
Evan Cheng794d0f72008-04-16 09:41:59 +00001162 continue;
1163 unsigned Reg = MO.getReg();
Dan Gohman244b86a2008-09-03 15:56:16 +00001164 if (!Reg)
1165 continue;
1166
Evan Cheng794d0f72008-04-16 09:41:59 +00001167 if (Reg == IncomingReg) {
Dan Gohman244b86a2008-09-03 15:56:16 +00001168 if (!Found) {
1169 if (MO.isDead())
1170 // The register is already marked dead.
1171 return true;
1172 MO.setIsDead();
1173 Found = true;
1174 }
1175 } else if (hasAliases && MO.isDead() &&
1176 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng794d0f72008-04-16 09:41:59 +00001177 // There exists a super-register that's marked dead.
1178 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman9d90c632008-07-03 01:18:51 +00001179 return true;
Owen Andersonc11fa052008-08-14 18:34:18 +00001180 if (RegInfo->getSubRegisters(IncomingReg) &&
1181 RegInfo->getSuperRegisters(Reg) &&
1182 RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng794d0f72008-04-16 09:41:59 +00001183 DeadOps.push_back(i);
Owen Anderson58060792008-01-24 01:10:07 +00001184 }
1185 }
1186
Evan Cheng794d0f72008-04-16 09:41:59 +00001187 // Trim unneeded dead operands.
1188 while (!DeadOps.empty()) {
1189 unsigned OpIdx = DeadOps.back();
1190 if (getOperand(OpIdx).isImplicit())
1191 RemoveOperand(OpIdx);
1192 else
1193 getOperand(OpIdx).setIsDead(false);
1194 DeadOps.pop_back();
1195 }
1196
Dan Gohman244b86a2008-09-03 15:56:16 +00001197 // If not found, this means an alias of one of the operands is dead. Add a
1198 // new implicit operand if required.
Chris Lattnerf29b6dc2009-06-24 17:54:48 +00001199 if (Found || !AddIfNotFound)
1200 return Found;
1201
1202 addOperand(MachineOperand::CreateReg(IncomingReg,
1203 true /*IsDef*/,
1204 true /*IsImp*/,
1205 false /*IsKill*/,
1206 true /*IsDead*/));
1207 return true;
Owen Anderson58060792008-01-24 01:10:07 +00001208}