blob: f73a5a362112f3701820b748b6a9a5164992a995 [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"
Dan Gohman91057512009-10-30 01:27:03 +000016#include "llvm/Function.h"
Evan Cheng2682ea02009-03-23 08:01:15 +000017#include "llvm/InlineAsm.h"
Chris Lattner1b989192007-12-31 04:13:23 +000018#include "llvm/Value.h"
Dan Gohman915d8722009-09-23 01:33:16 +000019#include "llvm/Assembly/Writer.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/CodeGen/MachineFunction.h"
Dan Gohman4e3bb1b2009-09-25 20:36:54 +000021#include "llvm/CodeGen/MachineMemOperand.h"
Chris Lattnere45742f2008-01-01 01:12:31 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000023#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/Target/TargetMachine.h"
Evan Cheng13d1c292008-01-31 09:59:15 +000025#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner8eaa5a92008-01-07 07:42:25 +000026#include "llvm/Target/TargetInstrDesc.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000027#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmaneef78172009-10-07 17:38:06 +000028#include "llvm/Analysis/AliasAnalysis.h"
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +000029#include "llvm/Analysis/DebugInfo.h"
Edwin Török675d5622009-07-11 20:10:48 +000030#include "llvm/Support/ErrorHandling.h"
Dan Gohman8b3b5172008-07-17 23:49:46 +000031#include "llvm/Support/LeakDetector.h"
Dan Gohmanac6f8922008-07-07 20:32:02 +000032#include "llvm/Support/MathExtras.h"
Chris Lattner24ae2a92008-08-24 20:37:32 +000033#include "llvm/Support/raw_ostream.h"
Dan Gohman98beebe2008-08-20 15:58:01 +000034#include "llvm/ADT/FoldingSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035using namespace llvm;
36
Chris Lattner7f2d3b82007-12-30 21:56:09 +000037//===----------------------------------------------------------------------===//
38// MachineOperand Implementation
39//===----------------------------------------------------------------------===//
40
Chris Lattnere45742f2008-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 Gohmanb9f4fa72008-10-03 15:45:36 +000045 assert(isReg() && "Can only add reg operand to use lists");
Chris Lattnere45742f2008-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 Lattner6fc812d2008-01-01 21:08:22 +000056 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg());
Chris Lattnere45742f2008-01-01 01:12:31 +000057
Chris Lattner6fc812d2008-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 Lattnere45742f2008-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 Lattner6fc812d2008-01-01 21:08:22 +000071 Contents.Reg.Prev = Head;
72 *Head = this;
Chris Lattnere45742f2008-01-01 01:12:31 +000073}
74
Dan Gohman8ff914c2009-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 Lattnere45742f2008-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 Gohmanb9f4fa72008-10-03 15:45:36 +0000115 if (isReg() && getParent() && getParent()->getParent() &&
Chris Lattnere45742f2008-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 Cheng9c73db12009-06-30 08:49:04 +0000127 bool isKill, bool isDead, bool isUndef) {
Chris Lattnere45742f2008-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 Gohmanb9f4fa72008-10-03 15:45:36 +0000130 if (isReg()) {
Dale Johannesen1b0c5782008-09-14 01:44:36 +0000131 assert(!isEarlyClobber());
Chris Lattnere45742f2008-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 Cheng9c73db12009-06-30 08:49:04 +0000150 IsUndef = isUndef;
Dale Johannesen1b0c5782008-09-14 01:44:36 +0000151 IsEarlyClobber = false;
Chris Lattnere45742f2008-01-01 01:12:31 +0000152 SubReg = 0;
153}
154
Chris Lattner7f2d3b82007-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 Lattnerf29b6dc2009-06-24 17:54:48 +0000158 if (getType() != Other.getType() ||
159 getTargetFlags() != Other.getTargetFlags())
160 return false;
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000161
162 switch (getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000163 default: llvm_unreachable("Unrecognized operand type");
Chris Lattner7f2d3b82007-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 Begeman6a38ec32008-02-14 07:39:30 +0000169 case MachineOperand::MO_FPImmediate:
170 return getFPImm() == Other.getFPImm();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000171 case MachineOperand::MO_MachineBasicBlock:
172 return getMBB() == Other.getMBB();
173 case MachineOperand::MO_FrameIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000174 return getIndex() == Other.getIndex();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000175 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000176 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000177 case MachineOperand::MO_JumpTableIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000178 return getIndex() == Other.getIndex();
Chris Lattner7f2d3b82007-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 Gohman91057512009-10-30 01:27:03 +0000184 case MachineOperand::MO_BlockAddress:
185 return getBlockAddress() == Other.getBlockAddress();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000186 }
187}
188
189/// print - Print the specified machine operand.
190///
Mon P Wang2f2cd302008-10-10 01:43:55 +0000191void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const {
Dan Gohman80164f22009-11-09 19:38:45 +0000192 // If the instruction is embedded into a basic block, we can find the
193 // target info for the instruction.
194 if (!TM)
195 if (const MachineInstr *MI = getParent())
196 if (const MachineBasicBlock *MBB = MI->getParent())
197 if (const MachineFunction *MF = MBB->getParent())
198 TM = &MF->getTarget();
199
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000200 switch (getType()) {
201 case MachineOperand::MO_Register:
Dan Gohman1e57df32008-02-10 18:45:23 +0000202 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000203 OS << "%reg" << getReg();
204 } else {
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000205 if (TM)
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000206 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000207 else
Dan Gohman57b31652009-10-31 20:19:03 +0000208 OS << "%physreg" << getReg();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000209 }
Dan Gohman4849d102008-12-18 21:51:27 +0000210
Evan Cheng9c73db12009-06-30 08:49:04 +0000211 if (getSubReg() != 0)
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000212 OS << ':' << getSubReg();
Dan Gohman4849d102008-12-18 21:51:27 +0000213
Evan Cheng9c73db12009-06-30 08:49:04 +0000214 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
215 isEarlyClobber()) {
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000216 OS << '<';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000217 bool NeedComma = false;
Evan Chengb71c46c2009-10-14 23:37:31 +0000218 if (isDef()) {
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000219 if (NeedComma) OS << ',';
Dale Johannesen38438f72008-09-12 17:49:03 +0000220 if (isEarlyClobber())
221 OS << "earlyclobber,";
Evan Chengb71c46c2009-10-14 23:37:31 +0000222 if (isImplicit())
223 OS << "imp-";
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000224 OS << "def";
225 NeedComma = true;
Evan Cheng8c56cfc2009-10-21 07:56:02 +0000226 } else if (isImplicit()) {
Evan Chengb71c46c2009-10-14 23:37:31 +0000227 OS << "imp-use";
Evan Cheng8c56cfc2009-10-21 07:56:02 +0000228 NeedComma = true;
229 }
Evan Chengb71c46c2009-10-14 23:37:31 +0000230
Evan Cheng9c73db12009-06-30 08:49:04 +0000231 if (isKill() || isDead() || isUndef()) {
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000232 if (NeedComma) OS << ',';
Bill Wendling733f0fd2008-02-24 00:56:13 +0000233 if (isKill()) OS << "kill";
234 if (isDead()) OS << "dead";
Evan Cheng9c73db12009-06-30 08:49:04 +0000235 if (isUndef()) {
236 if (isKill() || isDead())
237 OS << ',';
238 OS << "undef";
239 }
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000240 }
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000241 OS << '>';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000242 }
243 break;
244 case MachineOperand::MO_Immediate:
245 OS << getImm();
246 break;
Nate Begeman6a38ec32008-02-14 07:39:30 +0000247 case MachineOperand::MO_FPImmediate:
Chris Lattner82cdc062009-10-05 05:54:46 +0000248 if (getFPImm()->getType()->isFloatTy())
Nate Begeman6a38ec32008-02-14 07:39:30 +0000249 OS << getFPImm()->getValueAPF().convertToFloat();
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000250 else
Nate Begeman6a38ec32008-02-14 07:39:30 +0000251 OS << getFPImm()->getValueAPF().convertToDouble();
Nate Begeman6a38ec32008-02-14 07:39:30 +0000252 break;
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000253 case MachineOperand::MO_MachineBasicBlock:
Dan Gohman57b31652009-10-31 20:19:03 +0000254 OS << "<BB#" << getMBB()->getNumber() << ">";
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000255 break;
256 case MachineOperand::MO_FrameIndex:
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000257 OS << "<fi#" << getIndex() << '>';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000258 break;
259 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner6017d482007-12-30 23:10:15 +0000260 OS << "<cp#" << getIndex();
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000261 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000262 OS << '>';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000263 break;
264 case MachineOperand::MO_JumpTableIndex:
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000265 OS << "<jt#" << getIndex() << '>';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000266 break;
267 case MachineOperand::MO_GlobalAddress:
Dan Gohman40071872009-11-06 18:03:10 +0000268 OS << "<ga:";
269 WriteAsOperand(OS, getGlobal(), /*PrintType=*/false);
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000270 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000271 OS << '>';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000272 break;
273 case MachineOperand::MO_ExternalSymbol:
274 OS << "<es:" << getSymbolName();
275 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000276 OS << '>';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000277 break;
Dan Gohman91057512009-10-30 01:27:03 +0000278 case MachineOperand::MO_BlockAddress:
Dan Gohman57b31652009-10-31 20:19:03 +0000279 OS << "<";
280 WriteAsOperand(OS, getBlockAddress(), /*PrintType=*/false);
Dan Gohman91057512009-10-30 01:27:03 +0000281 OS << '>';
282 break;
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000283 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000284 llvm_unreachable("Unrecognized operand type");
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000285 }
Chris Lattnerf29b6dc2009-06-24 17:54:48 +0000286
287 if (unsigned TF = getTargetFlags())
288 OS << "[TF=" << TF << ']';
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000289}
290
291//===----------------------------------------------------------------------===//
Dan Gohmanac6f8922008-07-07 20:32:02 +0000292// MachineMemOperand Implementation
293//===----------------------------------------------------------------------===//
294
295MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f,
296 int64_t o, uint64_t s, unsigned int a)
297 : Offset(o), Size(s), V(v),
298 Flags((f & 7) | ((Log2_32(a) + 1) << 3)) {
Dan Gohman169948d2009-09-21 19:47:04 +0000299 assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
Dan Gohman78f9a462008-07-16 15:56:42 +0000300 assert((isLoad() || isStore()) && "Not a load/store!");
Dan Gohmanac6f8922008-07-07 20:32:02 +0000301}
302
Dan Gohman98beebe2008-08-20 15:58:01 +0000303/// Profile - Gather unique data for the object.
304///
305void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
306 ID.AddInteger(Offset);
307 ID.AddInteger(Size);
308 ID.AddPointer(V);
309 ID.AddInteger(Flags);
310}
311
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000312void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
313 // The Value and Offset may differ due to CSE. But the flags and size
314 // should be the same.
315 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
316 assert(MMO->getSize() == getSize() && "Size mismatch!");
317
318 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
319 // Update the alignment value.
320 Flags = (Flags & 7) | ((Log2_32(MMO->getBaseAlignment()) + 1) << 3);
321 // Also update the base and offset, because the new alignment may
322 // not be applicable with the old ones.
323 V = MMO->getValue();
324 Offset = MMO->getOffset();
325 }
326}
327
Dan Gohman232e4442009-09-25 23:33:20 +0000328/// getAlignment - Return the minimum known alignment in bytes of the
329/// actual memory reference.
330uint64_t MachineMemOperand::getAlignment() const {
331 return MinAlign(getBaseAlignment(), getOffset());
332}
333
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000334raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) {
335 assert((MMO.isLoad() || MMO.isStore()) &&
Dan Gohman915d8722009-09-23 01:33:16 +0000336 "SV has to be a load, store or both.");
337
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000338 if (MMO.isVolatile())
Dan Gohman915d8722009-09-23 01:33:16 +0000339 OS << "Volatile ";
340
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000341 if (MMO.isLoad())
Dan Gohman915d8722009-09-23 01:33:16 +0000342 OS << "LD";
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000343 if (MMO.isStore())
Dan Gohman915d8722009-09-23 01:33:16 +0000344 OS << "ST";
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000345 OS << MMO.getSize();
Dan Gohman915d8722009-09-23 01:33:16 +0000346
347 // Print the address information.
348 OS << "[";
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000349 if (!MMO.getValue())
Dan Gohman915d8722009-09-23 01:33:16 +0000350 OS << "<unknown>";
351 else
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000352 WriteAsOperand(OS, MMO.getValue(), /*PrintType=*/false);
Dan Gohman915d8722009-09-23 01:33:16 +0000353
354 // If the alignment of the memory reference itself differs from the alignment
355 // of the base pointer, print the base alignment explicitly, next to the base
356 // pointer.
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000357 if (MMO.getBaseAlignment() != MMO.getAlignment())
358 OS << "(align=" << MMO.getBaseAlignment() << ")";
Dan Gohman915d8722009-09-23 01:33:16 +0000359
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000360 if (MMO.getOffset() != 0)
361 OS << "+" << MMO.getOffset();
Dan Gohman915d8722009-09-23 01:33:16 +0000362 OS << "]";
363
364 // Print the alignment of the reference.
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000365 if (MMO.getBaseAlignment() != MMO.getAlignment() ||
366 MMO.getBaseAlignment() != MMO.getSize())
367 OS << "(align=" << MMO.getAlignment() << ")";
Dan Gohman915d8722009-09-23 01:33:16 +0000368
369 return OS;
370}
371
Dan Gohmanac6f8922008-07-07 20:32:02 +0000372//===----------------------------------------------------------------------===//
Chris Lattner7f2d3b82007-12-30 21:56:09 +0000373// MachineInstr Implementation
374//===----------------------------------------------------------------------===//
375
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
377/// TID NULL and no operands.
378MachineInstr::MachineInstr()
Dan Gohman16330ea2009-11-16 22:49:38 +0000379 : TID(0), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0),
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000380 Parent(0), debugLoc(DebugLoc::getUnknownLoc()) {
Dan Gohman8b3b5172008-07-17 23:49:46 +0000381 // Make sure that we get added to a machine basicblock
382 LeakDetector::addGarbageObject(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383}
384
385void MachineInstr::addImplicitDefUseOperands() {
386 if (TID->ImplicitDefs)
Chris Lattner720b6cf2007-12-30 00:12:25 +0000387 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Chris Lattner63ab1f22007-12-30 00:41:17 +0000388 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000389 if (TID->ImplicitUses)
Chris Lattner720b6cf2007-12-30 00:12:25 +0000390 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Chris Lattner63ab1f22007-12-30 00:41:17 +0000391 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392}
393
394/// MachineInstr ctor - This constructor create a MachineInstr and add the
395/// implicit operands. It reserves space for number of operands specified by
Chris Lattner5b930372008-01-07 07:27:27 +0000396/// TargetInstrDesc or the numOperands if it is not zero. (for
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397/// instructions with variable number of operands).
Chris Lattner5b930372008-01-07 07:27:27 +0000398MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp)
Dan Gohman16330ea2009-11-16 22:49:38 +0000399 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0),
400 MemRefs(0), MemRefsEnd(0), Parent(0),
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000401 debugLoc(DebugLoc::getUnknownLoc()) {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000402 if (!NoImp && TID->getImplicitDefs())
403 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 NumImplicitOps++;
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000405 if (!NoImp && TID->getImplicitUses())
406 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407 NumImplicitOps++;
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000408 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Chengbdf72b42007-10-13 02:23:01 +0000409 if (!NoImp)
410 addImplicitDefUseOperands();
Dan Gohman8b3b5172008-07-17 23:49:46 +0000411 // Make sure that we get added to a machine basicblock
412 LeakDetector::addGarbageObject(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413}
414
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000415/// MachineInstr ctor - As above, but with a DebugLoc.
416MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl,
417 bool NoImp)
Dan Gohman16330ea2009-11-16 22:49:38 +0000418 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0),
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000419 Parent(0), debugLoc(dl) {
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000420 if (!NoImp && TID->getImplicitDefs())
421 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
422 NumImplicitOps++;
423 if (!NoImp && TID->getImplicitUses())
424 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
425 NumImplicitOps++;
426 Operands.reserve(NumImplicitOps + TID->getNumOperands());
427 if (!NoImp)
428 addImplicitDefUseOperands();
429 // Make sure that we get added to a machine basicblock
430 LeakDetector::addGarbageObject(this);
431}
432
433/// MachineInstr ctor - Work exactly the same as the ctor two above, except
434/// that the MachineInstr is created and added to the end of the specified
435/// basic block.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436///
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000437MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid)
Dan Gohman16330ea2009-11-16 22:49:38 +0000438 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0),
439 MemRefs(0), MemRefsEnd(0), Parent(0),
Dale Johannesen7899a6d2009-01-27 23:20:29 +0000440 debugLoc(DebugLoc::getUnknownLoc()) {
441 assert(MBB && "Cannot use inserting ctor with null basic block!");
442 if (TID->ImplicitDefs)
443 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
444 NumImplicitOps++;
445 if (TID->ImplicitUses)
446 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
447 NumImplicitOps++;
448 Operands.reserve(NumImplicitOps + TID->getNumOperands());
449 addImplicitDefUseOperands();
450 // Make sure that we get added to a machine basicblock
451 LeakDetector::addGarbageObject(this);
452 MBB->push_back(this); // Add instruction to end of basic block!
453}
454
455/// MachineInstr ctor - As above, but with a DebugLoc.
456///
457MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl,
Chris Lattner5b930372008-01-07 07:27:27 +0000458 const TargetInstrDesc &tid)
Dan Gohman16330ea2009-11-16 22:49:38 +0000459 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0),
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000460 Parent(0), debugLoc(dl) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461 assert(MBB && "Cannot use inserting ctor with null basic block!");
462 if (TID->ImplicitDefs)
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000463 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 NumImplicitOps++;
465 if (TID->ImplicitUses)
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000466 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 NumImplicitOps++;
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000468 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469 addImplicitDefUseOperands();
Dan Gohman8b3b5172008-07-17 23:49:46 +0000470 // Make sure that we get added to a machine basicblock
471 LeakDetector::addGarbageObject(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000472 MBB->push_back(this); // Add instruction to end of basic block!
473}
474
475/// MachineInstr ctor - Copies MachineInstr arg exactly
476///
Evan Cheng4ce1a522008-07-19 00:37:25 +0000477MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Dan Gohman16330ea2009-11-16 22:49:38 +0000478 : TID(&MI.getDesc()), NumImplicitOps(0), AsmPrinterFlags(0),
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000479 MemRefs(MI.MemRefs), MemRefsEnd(MI.MemRefsEnd),
480 Parent(0), debugLoc(MI.getDebugLoc()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000481 Operands.reserve(MI.getNumOperands());
482
483 // Add operands
Evan Cheng4ce1a522008-07-19 00:37:25 +0000484 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
485 addOperand(MI.getOperand(i));
486 NumImplicitOps = MI.NumImplicitOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000487
Dan Gohman221a4372008-07-07 23:14:23 +0000488 // Set parent to null.
Chris Lattner7ce487f2007-12-31 04:56:33 +0000489 Parent = 0;
Dan Gohmance232952008-07-21 18:47:29 +0000490
491 LeakDetector::addGarbageObject(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492}
493
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000494MachineInstr::~MachineInstr() {
Dan Gohman8b3b5172008-07-17 23:49:46 +0000495 LeakDetector::removeGarbageObject(this);
Chris Lattnere722c3f2007-12-30 06:11:04 +0000496#ifndef NDEBUG
Chris Lattnere45742f2008-01-01 01:12:31 +0000497 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattnere722c3f2007-12-30 06:11:04 +0000498 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000499 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
Chris Lattnere45742f2008-01-01 01:12:31 +0000500 "Reg operand def/use list corrupted");
501 }
Chris Lattnere722c3f2007-12-30 06:11:04 +0000502#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000503}
504
Chris Lattnere45742f2008-01-01 01:12:31 +0000505/// getRegInfo - If this instruction is embedded into a MachineFunction,
506/// return the MachineRegisterInfo object for the current function, otherwise
507/// return null.
508MachineRegisterInfo *MachineInstr::getRegInfo() {
509 if (MachineBasicBlock *MBB = getParent())
Dan Gohman07368822008-07-08 23:59:09 +0000510 return &MBB->getParent()->getRegInfo();
Chris Lattnere45742f2008-01-01 01:12:31 +0000511 return 0;
512}
513
514/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
515/// this instruction from their respective use lists. This requires that the
516/// operands already be on their use lists.
517void MachineInstr::RemoveRegOperandsFromUseLists() {
518 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000519 if (Operands[i].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000520 Operands[i].RemoveRegOperandFromRegInfo();
521 }
522}
523
524/// AddRegOperandsToUseLists - Add all of the register operands in
525/// this instruction from their respective use lists. This requires that the
526/// operands not be on their use lists yet.
527void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
528 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000529 if (Operands[i].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000530 Operands[i].AddRegOperandToRegInfo(&RegInfo);
531 }
532}
533
534
535/// addOperand - Add the specified operand to the instruction. If it is an
536/// implicit operand, it is added to the end of the operand list. If it is
537/// an explicit operand it is added at the end of the explicit operand list
538/// (before the first implicit operand).
539void MachineInstr::addOperand(const MachineOperand &Op) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000540 bool isImpReg = Op.isReg() && Op.isImplicit();
Chris Lattnere45742f2008-01-01 01:12:31 +0000541 assert((isImpReg || !OperandsComplete()) &&
542 "Trying to add an operand to a machine instr that is already done!");
543
Dan Gohmana0dff432008-12-09 22:45:08 +0000544 MachineRegisterInfo *RegInfo = getRegInfo();
545
Chris Lattnere45742f2008-01-01 01:12:31 +0000546 // If we are adding the operand to the end of the list, our job is simpler.
547 // This is true most of the time, so this is a reasonable optimization.
548 if (isImpReg || NumImplicitOps == 0) {
549 // We can only do this optimization if we know that the operand list won't
550 // reallocate.
551 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
552 Operands.push_back(Op);
553
554 // Set the parent of the operand.
555 Operands.back().ParentMI = this;
556
557 // If the operand is a register, update the operand's use list.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000558 if (Op.isReg())
Dan Gohmana0dff432008-12-09 22:45:08 +0000559 Operands.back().AddRegOperandToRegInfo(RegInfo);
Chris Lattnere45742f2008-01-01 01:12:31 +0000560 return;
561 }
562 }
563
564 // Otherwise, we have to insert a real operand before any implicit ones.
565 unsigned OpNo = Operands.size()-NumImplicitOps;
566
Chris Lattnere45742f2008-01-01 01:12:31 +0000567 // If this instruction isn't embedded into a function, then we don't need to
568 // update any operand lists.
569 if (RegInfo == 0) {
570 // Simple insertion, no reginfo update needed for other register operands.
571 Operands.insert(Operands.begin()+OpNo, Op);
572 Operands[OpNo].ParentMI = this;
573
574 // Do explicitly set the reginfo for this operand though, to ensure the
575 // next/prev fields are properly nulled out.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000576 if (Operands[OpNo].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000577 Operands[OpNo].AddRegOperandToRegInfo(0);
578
579 } else if (Operands.size()+1 <= Operands.capacity()) {
580 // Otherwise, we have to remove register operands from their register use
581 // list, add the operand, then add the register operands back to their use
582 // list. This also must handle the case when the operand list reallocates
583 // to somewhere else.
584
585 // If insertion of this operand won't cause reallocation of the operand
586 // list, just remove the implicit operands, add the operand, then re-add all
587 // the rest of the operands.
588 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000589 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattnere45742f2008-01-01 01:12:31 +0000590 Operands[i].RemoveRegOperandFromRegInfo();
591 }
592
593 // Add the operand. If it is a register, add it to the reg list.
594 Operands.insert(Operands.begin()+OpNo, Op);
595 Operands[OpNo].ParentMI = this;
596
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000597 if (Operands[OpNo].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000598 Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
599
600 // Re-add all the implicit ops.
601 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000602 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattnere45742f2008-01-01 01:12:31 +0000603 Operands[i].AddRegOperandToRegInfo(RegInfo);
604 }
605 } else {
606 // Otherwise, we will be reallocating the operand list. Remove all reg
607 // operands from their list, then readd them after the operand list is
608 // reallocated.
609 RemoveRegOperandsFromUseLists();
610
611 Operands.insert(Operands.begin()+OpNo, Op);
612 Operands[OpNo].ParentMI = this;
613
614 // Re-add all the operands.
615 AddRegOperandsToUseLists(*RegInfo);
616 }
617}
618
619/// RemoveOperand - Erase an operand from an instruction, leaving it with one
620/// fewer operand than it started with.
621///
622void MachineInstr::RemoveOperand(unsigned OpNo) {
623 assert(OpNo < Operands.size() && "Invalid operand number");
624
625 // Special case removing the last one.
626 if (OpNo == Operands.size()-1) {
627 // If needed, remove from the reg def/use list.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000628 if (Operands.back().isReg() && Operands.back().isOnRegUseList())
Chris Lattnere45742f2008-01-01 01:12:31 +0000629 Operands.back().RemoveRegOperandFromRegInfo();
630
631 Operands.pop_back();
632 return;
633 }
634
635 // Otherwise, we are removing an interior operand. If we have reginfo to
636 // update, remove all operands that will be shifted down from their reg lists,
637 // move everything down, then re-add them.
638 MachineRegisterInfo *RegInfo = getRegInfo();
639 if (RegInfo) {
640 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000641 if (Operands[i].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000642 Operands[i].RemoveRegOperandFromRegInfo();
643 }
644 }
645
646 Operands.erase(Operands.begin()+OpNo);
647
648 if (RegInfo) {
649 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000650 if (Operands[i].isReg())
Chris Lattnere45742f2008-01-01 01:12:31 +0000651 Operands[i].AddRegOperandToRegInfo(RegInfo);
652 }
653 }
654}
655
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000656/// addMemOperand - Add a MachineMemOperand to the machine instruction.
657/// This function should be used only occasionally. The setMemRefs function
658/// is the primary method for setting up a MachineInstr's MemRefs list.
Dan Gohman221a4372008-07-07 23:14:23 +0000659void MachineInstr::addMemOperand(MachineFunction &MF,
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000660 MachineMemOperand *MO) {
661 mmo_iterator OldMemRefs = MemRefs;
662 mmo_iterator OldMemRefsEnd = MemRefsEnd;
Dan Gohman221a4372008-07-07 23:14:23 +0000663
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000664 size_t NewNum = (MemRefsEnd - MemRefs) + 1;
665 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
666 mmo_iterator NewMemRefsEnd = NewMemRefs + NewNum;
Dan Gohman221a4372008-07-07 23:14:23 +0000667
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000668 std::copy(OldMemRefs, OldMemRefsEnd, NewMemRefs);
669 NewMemRefs[NewNum - 1] = MO;
670
671 MemRefs = NewMemRefs;
672 MemRefsEnd = NewMemRefsEnd;
673}
Chris Lattnere45742f2008-01-01 01:12:31 +0000674
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000675/// removeFromParent - This method unlinks 'this' from the containing basic
676/// block, and returns it, but does not delete it.
677MachineInstr *MachineInstr::removeFromParent() {
678 assert(getParent() && "Not embedded in a basic block!");
679 getParent()->remove(this);
680 return this;
681}
682
683
Dan Gohman221a4372008-07-07 23:14:23 +0000684/// eraseFromParent - This method unlinks 'this' from the containing basic
685/// block, and deletes it.
686void MachineInstr::eraseFromParent() {
687 assert(getParent() && "Not embedded in a basic block!");
688 getParent()->erase(this);
689}
690
691
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692/// OperandComplete - Return true if it's illegal to add a new operand
693///
694bool MachineInstr::OperandsComplete() const {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000695 unsigned short NumOperands = TID->getNumOperands();
Chris Lattner2fb37c02008-01-07 05:19:29 +0000696 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697 return true; // Broken: we have all the operands of this instruction!
698 return false;
699}
700
701/// getNumExplicitOperands - Returns the number of non-implicit operands.
702///
703unsigned MachineInstr::getNumExplicitOperands() const {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000704 unsigned NumOperands = TID->getNumOperands();
Chris Lattner2fb37c02008-01-07 05:19:29 +0000705 if (!TID->isVariadic())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706 return NumOperands;
707
Dan Gohman3d880012009-04-15 17:59:11 +0000708 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
709 const MachineOperand &MO = getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000710 if (!MO.isReg() || !MO.isImplicit())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711 NumOperands++;
712 }
713 return NumOperands;
714}
715
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716
Dan Gohmanfa607c92008-07-01 00:05:16 +0000717/// isLabel - Returns true if the MachineInstr represents a label.
718///
719bool MachineInstr::isLabel() const {
720 return getOpcode() == TargetInstrInfo::DBG_LABEL ||
721 getOpcode() == TargetInstrInfo::EH_LABEL ||
722 getOpcode() == TargetInstrInfo::GC_LABEL;
723}
724
Evan Cheng13d1c292008-01-31 09:59:15 +0000725/// isDebugLabel - Returns true if the MachineInstr represents a debug label.
726///
727bool MachineInstr::isDebugLabel() const {
Dan Gohmanfa607c92008-07-01 00:05:16 +0000728 return getOpcode() == TargetInstrInfo::DBG_LABEL;
Evan Cheng13d1c292008-01-31 09:59:15 +0000729}
730
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Jim Grosbach47472be2009-09-17 17:57:26 +0000732/// the specific register or -1 if it is not found. It further tightens
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000733/// the search criteria to a use that kills the register if isKill is true.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000734int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
735 const TargetRegisterInfo *TRI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000736 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
737 const MachineOperand &MO = getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000738 if (!MO.isReg() || !MO.isUse())
Evan Chengc7daf1f2008-03-05 00:59:57 +0000739 continue;
740 unsigned MOReg = MO.getReg();
741 if (!MOReg)
742 continue;
743 if (MOReg == Reg ||
744 (TRI &&
745 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
746 TargetRegisterInfo::isPhysicalRegister(Reg) &&
747 TRI->isSubRegister(MOReg, Reg)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000748 if (!isKill || MO.isKill())
749 return i;
750 }
751 return -1;
752}
753
Evan Chengc7daf1f2008-03-05 00:59:57 +0000754/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman2f51e1f2008-05-06 00:20:10 +0000755/// the specified register or -1 if it is not found. If isDead is true, defs
756/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
757/// also checks if there is a def of a super-register.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000758int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead,
759 const TargetRegisterInfo *TRI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000760 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Chengc7daf1f2008-03-05 00:59:57 +0000761 const MachineOperand &MO = getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000762 if (!MO.isReg() || !MO.isDef())
Evan Chengc7daf1f2008-03-05 00:59:57 +0000763 continue;
764 unsigned MOReg = MO.getReg();
765 if (MOReg == Reg ||
766 (TRI &&
767 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
768 TargetRegisterInfo::isPhysicalRegister(Reg) &&
769 TRI->isSubRegister(MOReg, Reg)))
770 if (!isDead || MO.isDead())
771 return i;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000772 }
Evan Chengc7daf1f2008-03-05 00:59:57 +0000773 return -1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000774}
775
776/// findFirstPredOperandIdx() - Find the index of the first operand in the
777/// operand list that is used to represent the predicate. It returns -1 if
778/// none is found.
779int MachineInstr::findFirstPredOperandIdx() const {
Chris Lattner5b930372008-01-07 07:27:27 +0000780 const TargetInstrDesc &TID = getDesc();
781 if (TID.isPredicable()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner5b930372008-01-07 07:27:27 +0000783 if (TID.OpInfo[i].isPredicate())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000784 return i;
785 }
786
787 return -1;
788}
789
Bob Wilsonaded9952009-04-09 17:16:43 +0000790/// isRegTiedToUseOperand - Given the index of a register def operand,
791/// check if the register def is tied to a source operand, due to either
792/// two-address elimination or inline assembly constraints. Returns the
793/// first tied use operand index by reference is UseOpIdx is not null.
Jakob Stoklund Olesencb9f5b52009-04-29 20:57:16 +0000794bool MachineInstr::
795isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const {
Evan Cheng2682ea02009-03-23 08:01:15 +0000796 if (getOpcode() == TargetInstrInfo::INLINEASM) {
Bob Wilsonaded9952009-04-09 17:16:43 +0000797 assert(DefOpIdx >= 2);
798 const MachineOperand &MO = getOperand(DefOpIdx);
Chris Lattnerb1e84232009-04-09 23:33:34 +0000799 if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
Evan Cheng2682ea02009-03-23 08:01:15 +0000800 return false;
Evan Cheng3df52f72009-06-24 02:05:51 +0000801 // Determine the actual operand index that corresponds to this index.
Evan Cheng2682ea02009-03-23 08:01:15 +0000802 unsigned DefNo = 0;
Evan Cheng3df52f72009-06-24 02:05:51 +0000803 unsigned DefPart = 0;
Evan Cheng2682ea02009-03-23 08:01:15 +0000804 for (unsigned i = 1, e = getNumOperands(); i < e; ) {
805 const MachineOperand &FMO = getOperand(i);
Jakob Stoklund Olesen458bbfb2009-07-19 19:09:59 +0000806 // After the normal asm operands there may be additional imp-def regs.
807 if (!FMO.isImm())
808 return false;
Evan Cheng2682ea02009-03-23 08:01:15 +0000809 // Skip over this def.
Evan Cheng3df52f72009-06-24 02:05:51 +0000810 unsigned NumOps = InlineAsm::getNumOperandRegisters(FMO.getImm());
811 unsigned PrevDef = i + 1;
812 i = PrevDef + NumOps;
813 if (i > DefOpIdx) {
814 DefPart = DefOpIdx - PrevDef;
Evan Cheng2682ea02009-03-23 08:01:15 +0000815 break;
Evan Cheng3df52f72009-06-24 02:05:51 +0000816 }
Evan Cheng2682ea02009-03-23 08:01:15 +0000817 ++DefNo;
818 }
Evan Cheng3df52f72009-06-24 02:05:51 +0000819 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Evan Cheng2682ea02009-03-23 08:01:15 +0000820 const MachineOperand &FMO = getOperand(i);
821 if (!FMO.isImm())
822 continue;
823 if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse())
824 continue;
825 unsigned Idx;
Evan Cheng3df52f72009-06-24 02:05:51 +0000826 if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) &&
Bob Wilsonaded9952009-04-09 17:16:43 +0000827 Idx == DefNo) {
828 if (UseOpIdx)
Evan Cheng3df52f72009-06-24 02:05:51 +0000829 *UseOpIdx = (unsigned)i + 1 + DefPart;
Evan Cheng2682ea02009-03-23 08:01:15 +0000830 return true;
Bob Wilsonaded9952009-04-09 17:16:43 +0000831 }
Evan Cheng2682ea02009-03-23 08:01:15 +0000832 }
Evan Cheng3df52f72009-06-24 02:05:51 +0000833 return false;
Evan Cheng2682ea02009-03-23 08:01:15 +0000834 }
835
Bob Wilsonaded9952009-04-09 17:16:43 +0000836 assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!");
Chris Lattner5b930372008-01-07 07:27:27 +0000837 const TargetInstrDesc &TID = getDesc();
Evan Chengf1107fd2008-07-10 07:35:43 +0000838 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
839 const MachineOperand &MO = getOperand(i);
Dan Gohman4dbf8792008-12-05 05:45:42 +0000840 if (MO.isReg() && MO.isUse() &&
Bob Wilsonaded9952009-04-09 17:16:43 +0000841 TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) {
842 if (UseOpIdx)
843 *UseOpIdx = (unsigned)i;
Evan Chengf1107fd2008-07-10 07:35:43 +0000844 return true;
Bob Wilsonaded9952009-04-09 17:16:43 +0000845 }
Evan Cheng687d1082007-10-12 08:50:34 +0000846 }
847 return false;
848}
849
Evan Cheng48555e82009-03-19 20:30:06 +0000850/// isRegTiedToDefOperand - Return true if the operand of the specified index
851/// is a register use and it is tied to an def operand. It also returns the def
852/// operand index by reference.
Jakob Stoklund Olesencb9f5b52009-04-29 20:57:16 +0000853bool MachineInstr::
854isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const {
Evan Cheng2682ea02009-03-23 08:01:15 +0000855 if (getOpcode() == TargetInstrInfo::INLINEASM) {
856 const MachineOperand &MO = getOperand(UseOpIdx);
Chris Lattner0d0e8a92009-04-09 16:50:43 +0000857 if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0)
Evan Cheng2682ea02009-03-23 08:01:15 +0000858 return false;
Jakob Stoklund Olesen4c769c32009-07-16 20:58:34 +0000859
860 // Find the flag operand corresponding to UseOpIdx
861 unsigned FlagIdx, NumOps=0;
862 for (FlagIdx = 1; FlagIdx < UseOpIdx; FlagIdx += NumOps+1) {
863 const MachineOperand &UFMO = getOperand(FlagIdx);
Jakob Stoklund Olesen458bbfb2009-07-19 19:09:59 +0000864 // After the normal asm operands there may be additional imp-def regs.
865 if (!UFMO.isImm())
866 return false;
Jakob Stoklund Olesen4c769c32009-07-16 20:58:34 +0000867 NumOps = InlineAsm::getNumOperandRegisters(UFMO.getImm());
868 assert(NumOps < getNumOperands() && "Invalid inline asm flag");
869 if (UseOpIdx < FlagIdx+NumOps+1)
870 break;
Evan Cheng3df52f72009-06-24 02:05:51 +0000871 }
Jakob Stoklund Olesen4c769c32009-07-16 20:58:34 +0000872 if (FlagIdx >= UseOpIdx)
Evan Cheng3df52f72009-06-24 02:05:51 +0000873 return false;
Jakob Stoklund Olesen4c769c32009-07-16 20:58:34 +0000874 const MachineOperand &UFMO = getOperand(FlagIdx);
Evan Cheng2682ea02009-03-23 08:01:15 +0000875 unsigned DefNo;
876 if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) {
877 if (!DefOpIdx)
878 return true;
879
880 unsigned DefIdx = 1;
881 // Remember to adjust the index. First operand is asm string, then there
882 // is a flag for each.
883 while (DefNo) {
884 const MachineOperand &FMO = getOperand(DefIdx);
885 assert(FMO.isImm());
886 // Skip over this def.
887 DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
888 --DefNo;
889 }
Evan Cheng3df52f72009-06-24 02:05:51 +0000890 *DefOpIdx = DefIdx + UseOpIdx - FlagIdx;
Evan Cheng2682ea02009-03-23 08:01:15 +0000891 return true;
892 }
893 return false;
894 }
895
Evan Cheng48555e82009-03-19 20:30:06 +0000896 const TargetInstrDesc &TID = getDesc();
897 if (UseOpIdx >= TID.getNumOperands())
898 return false;
899 const MachineOperand &MO = getOperand(UseOpIdx);
900 if (!MO.isReg() || !MO.isUse())
901 return false;
902 int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO);
903 if (DefIdx == -1)
904 return false;
905 if (DefOpIdx)
906 *DefOpIdx = (unsigned)DefIdx;
907 return true;
908}
909
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000910/// copyKillDeadInfo - Copies kill / dead operand properties from MI.
911///
912void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
913 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
914 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000915 if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000916 continue;
917 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
918 MachineOperand &MOp = getOperand(j);
919 if (!MOp.isIdenticalTo(MO))
920 continue;
921 if (MO.isKill())
922 MOp.setIsKill();
923 else
924 MOp.setIsDead();
925 break;
926 }
927 }
928}
929
930/// copyPredicates - Copies predicate operand(s) from MI.
931void MachineInstr::copyPredicates(const MachineInstr *MI) {
Chris Lattner5b930372008-01-07 07:27:27 +0000932 const TargetInstrDesc &TID = MI->getDesc();
Evan Chengbe856622008-03-13 00:44:09 +0000933 if (!TID.isPredicable())
934 return;
935 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
936 if (TID.OpInfo[i].isPredicate()) {
937 // Predicated operands must be last operands.
938 addOperand(MI->getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000939 }
940 }
941}
942
Evan Chenge52c1912008-07-03 09:09:37 +0000943/// isSafeToMove - Return true if it is safe to move this instruction. If
944/// SawStore is set to true, it means that there is a store (or call) between
945/// the instruction's location and its intended destination.
Dan Gohman9ca19a32008-11-18 19:04:29 +0000946bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII,
Dan Gohman0a4c09e2009-10-09 23:27:56 +0000947 bool &SawStore,
948 AliasAnalysis *AA) const {
Evan Chengbe856622008-03-13 00:44:09 +0000949 // Ignore stuff that we obviously can't move.
950 if (TID->mayStore() || TID->isCall()) {
951 SawStore = true;
952 return false;
953 }
Dan Gohman64709cd2008-12-23 17:28:50 +0000954 if (TID->isTerminator() || TID->hasUnmodeledSideEffects())
Evan Chengbe856622008-03-13 00:44:09 +0000955 return false;
956
957 // See if this instruction does a load. If so, we have to guarantee that the
958 // loaded value doesn't change between the load and the its intended
959 // destination. The check for isInvariantLoad gives the targe the chance to
960 // classify the load as always returning a constant, e.g. a constant pool
961 // load.
Dan Gohman0a4c09e2009-10-09 23:27:56 +0000962 if (TID->mayLoad() && !isInvariantLoad(AA))
Evan Chengbe856622008-03-13 00:44:09 +0000963 // Otherwise, this is a real load. If there is a store between the load and
Evan Cheng79d87ec2009-07-28 21:49:18 +0000964 // end of block, or if the load is volatile, we can't move it.
Dan Gohman0ce00b82008-10-02 15:04:30 +0000965 return !SawStore && !hasVolatileMemoryRef();
Dan Gohman9ffbed82008-09-24 00:06:15 +0000966
Evan Chengbe856622008-03-13 00:44:09 +0000967 return true;
968}
969
Evan Cheng75e2cee2008-08-27 20:33:50 +0000970/// isSafeToReMat - Return true if it's safe to rematerialize the specified
971/// instruction which defined the specified register instead of copying it.
Dan Gohman9ca19a32008-11-18 19:04:29 +0000972bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII,
Dan Gohman0a4c09e2009-10-09 23:27:56 +0000973 unsigned DstReg,
974 AliasAnalysis *AA) const {
Evan Cheng75e2cee2008-08-27 20:33:50 +0000975 bool SawStore = false;
Dan Gohman0a4c09e2009-10-09 23:27:56 +0000976 if (!TII->isTriviallyReMaterializable(this, AA) ||
977 !isSafeToMove(TII, SawStore, AA))
Evan Cheng75e2cee2008-08-27 20:33:50 +0000978 return false;
979 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Dan Gohman90feee22008-11-18 19:49:32 +0000980 const MachineOperand &MO = getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000981 if (!MO.isReg())
Evan Cheng75e2cee2008-08-27 20:33:50 +0000982 continue;
983 // FIXME: For now, do not remat any instruction with register operands.
984 // Later on, we can loosen the restriction is the register operands have
985 // not been modified between the def and use. Note, this is different from
Evan Chenga02c6692008-08-27 20:58:54 +0000986 // MachineSink because the code is no longer in two-address form (at least
Evan Cheng75e2cee2008-08-27 20:33:50 +0000987 // partially).
988 if (MO.isUse())
989 return false;
990 else if (!MO.isDead() && MO.getReg() != DstReg)
991 return false;
992 }
993 return true;
994}
995
Dan Gohman9ffbed82008-09-24 00:06:15 +0000996/// hasVolatileMemoryRef - Return true if this instruction may have a
997/// volatile memory reference, or if the information describing the
998/// memory reference is not available. Return false if it is known to
999/// have no volatile memory references.
1000bool MachineInstr::hasVolatileMemoryRef() const {
1001 // An instruction known never to access memory won't have a volatile access.
1002 if (!TID->mayStore() &&
1003 !TID->mayLoad() &&
1004 !TID->isCall() &&
1005 !TID->hasUnmodeledSideEffects())
1006 return false;
1007
1008 // Otherwise, if the instruction has no memory reference information,
1009 // conservatively assume it wasn't preserved.
1010 if (memoperands_empty())
1011 return true;
1012
1013 // Check the memory reference information for volatile references.
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00001014 for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I)
1015 if ((*I)->isVolatile())
Dan Gohman9ffbed82008-09-24 00:06:15 +00001016 return true;
1017
1018 return false;
1019}
1020
Dan Gohmaneef78172009-10-07 17:38:06 +00001021/// isInvariantLoad - Return true if this instruction is loading from a
1022/// location whose value is invariant across the function. For example,
1023/// loading a value from the constant pool or from from the argument area
1024/// of a function if it does not change. This should only return true of
1025/// *all* loads the instruction does are invariant (if it does multiple loads).
1026bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const {
1027 // If the instruction doesn't load at all, it isn't an invariant load.
1028 if (!TID->mayLoad())
1029 return false;
1030
1031 // If the instruction has lost its memoperands, conservatively assume that
1032 // it may not be an invariant load.
1033 if (memoperands_empty())
1034 return false;
1035
1036 const MachineFrameInfo *MFI = getParent()->getParent()->getFrameInfo();
1037
1038 for (mmo_iterator I = memoperands_begin(),
1039 E = memoperands_end(); I != E; ++I) {
1040 if ((*I)->isVolatile()) return false;
1041 if ((*I)->isStore()) return false;
1042
1043 if (const Value *V = (*I)->getValue()) {
1044 // A load from a constant PseudoSourceValue is invariant.
1045 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V))
1046 if (PSV->isConstant(MFI))
1047 continue;
1048 // If we have an AliasAnalysis, ask it whether the memory is constant.
1049 if (AA && AA->pointsToConstantMemory(V))
1050 continue;
1051 }
1052
1053 // Otherwise assume conservatively.
1054 return false;
1055 }
1056
1057 // Everything checks out.
1058 return true;
1059}
1060
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001061void MachineInstr::dump() const {
Chris Lattnerd71b0b02009-08-23 03:41:05 +00001062 errs() << " " << *this;
Mon P Wang2f2cd302008-10-10 01:43:55 +00001063}
1064
1065void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const {
Dan Gohman80164f22009-11-09 19:38:45 +00001066 // We can be a bit tidier if we know the TargetMachine and/or MachineFunction.
1067 const MachineFunction *MF = 0;
1068 if (const MachineBasicBlock *MBB = getParent()) {
1069 MF = MBB->getParent();
1070 if (!TM && MF)
1071 TM = &MF->getTarget();
1072 }
Dan Gohman57b31652009-10-31 20:19:03 +00001073
1074 // Print explicitly defined operands on the left of an assignment syntax.
Dan Gohman80164f22009-11-09 19:38:45 +00001075 unsigned StartOp = 0, e = getNumOperands();
Dan Gohman57b31652009-10-31 20:19:03 +00001076 for (; StartOp < e && getOperand(StartOp).isReg() &&
1077 getOperand(StartOp).isDef() &&
1078 !getOperand(StartOp).isImplicit();
1079 ++StartOp) {
1080 if (StartOp != 0) OS << ", ";
1081 getOperand(StartOp).print(OS, TM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001082 }
1083
Dan Gohman57b31652009-10-31 20:19:03 +00001084 if (StartOp != 0)
1085 OS << " = ";
1086
1087 // Print the opcode name.
Chris Lattner5b930372008-01-07 07:27:27 +00001088 OS << getDesc().getName();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001089
Dan Gohman57b31652009-10-31 20:19:03 +00001090 // Print the rest of the operands.
Dan Gohman80164f22009-11-09 19:38:45 +00001091 bool OmittedAnyCallClobbers = false;
1092 bool FirstOp = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001093 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohman80164f22009-11-09 19:38:45 +00001094 const MachineOperand &MO = getOperand(i);
1095
1096 // Omit call-clobbered registers which aren't used anywhere. This makes
1097 // call instructions much less noisy on targets where calls clobber lots
1098 // of registers. Don't rely on MO.isDead() because we may be called before
1099 // LiveVariables is run, or we may be looking at a non-allocatable reg.
1100 if (MF && getDesc().isCall() &&
1101 MO.isReg() && MO.isImplicit() && MO.isDef()) {
1102 unsigned Reg = MO.getReg();
1103 if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) {
1104 const MachineRegisterInfo &MRI = MF->getRegInfo();
1105 if (MRI.use_empty(Reg) && !MRI.isLiveOut(Reg)) {
1106 bool HasAliasLive = false;
1107 for (const unsigned *Alias = TM->getRegisterInfo()->getAliasSet(Reg);
1108 unsigned AliasReg = *Alias; ++Alias)
1109 if (!MRI.use_empty(AliasReg) || MRI.isLiveOut(AliasReg)) {
1110 HasAliasLive = true;
1111 break;
1112 }
1113 if (!HasAliasLive) {
1114 OmittedAnyCallClobbers = true;
1115 continue;
1116 }
1117 }
1118 }
1119 }
1120
1121 if (FirstOp) FirstOp = false; else OS << ",";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001122 OS << " ";
Dan Gohman80164f22009-11-09 19:38:45 +00001123 MO.print(OS, TM);
1124 }
1125
1126 // Briefly indicate whether any call clobbers were omitted.
1127 if (OmittedAnyCallClobbers) {
1128 if (FirstOp) FirstOp = false; else OS << ",";
1129 OS << " ...";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001130 }
1131
Dan Gohman57b31652009-10-31 20:19:03 +00001132 bool HaveSemi = false;
Dan Gohman221a4372008-07-07 23:14:23 +00001133 if (!memoperands_empty()) {
Dan Gohman57b31652009-10-31 20:19:03 +00001134 if (!HaveSemi) OS << ";"; HaveSemi = true;
1135
1136 OS << " mem:";
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00001137 for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
1138 i != e; ++i) {
1139 OS << **i;
Dan Gohman915d8722009-09-23 01:33:16 +00001140 if (next(i) != e)
1141 OS << " ";
Dan Gohman12a9c082008-02-06 22:27:42 +00001142 }
1143 }
1144
Dan Gohman80164f22009-11-09 19:38:45 +00001145 if (!debugLoc.isUnknown() && MF) {
Dan Gohman57b31652009-10-31 20:19:03 +00001146 if (!HaveSemi) OS << ";"; HaveSemi = true;
1147
1148 // TODO: print InlinedAtLoc information
1149
Bill Wendlingb7596d22009-02-19 21:44:55 +00001150 DebugLocTuple DLT = MF->getDebugLocTuple(debugLoc);
Dan Gohmanaf20e7c2009-12-01 00:45:56 +00001151 DIScope Scope(DLT.Scope);
Dan Gohman3a271d82009-11-23 21:29:08 +00001152 OS << " dbg:";
Dan Gohmanaf20e7c2009-12-01 00:45:56 +00001153 if (!Scope.isNull())
1154 OS << Scope.getDirectory() << ':' << Scope.getFilename() << ':';
Dan Gohman3a271d82009-11-23 21:29:08 +00001155 OS << DLT.Line << ":" << DLT.Col;
Bill Wendlingb7596d22009-02-19 21:44:55 +00001156 }
1157
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001158 OS << "\n";
1159}
1160
Owen Anderson58060792008-01-24 01:10:07 +00001161bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman1e57df32008-02-10 18:45:23 +00001162 const TargetRegisterInfo *RegInfo,
Owen Anderson58060792008-01-24 01:10:07 +00001163 bool AddIfNotFound) {
Evan Cheng794d0f72008-04-16 09:41:59 +00001164 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Dan Gohman9d90c632008-07-03 01:18:51 +00001165 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman244b86a2008-09-03 15:56:16 +00001166 bool Found = false;
Evan Cheng794d0f72008-04-16 09:41:59 +00001167 SmallVector<unsigned,4> DeadOps;
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +00001168 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1169 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesenb409fff2009-08-04 20:09:25 +00001170 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng794d0f72008-04-16 09:41:59 +00001171 continue;
1172 unsigned Reg = MO.getReg();
1173 if (!Reg)
1174 continue;
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +00001175
Evan Cheng794d0f72008-04-16 09:41:59 +00001176 if (Reg == IncomingReg) {
Dan Gohman244b86a2008-09-03 15:56:16 +00001177 if (!Found) {
1178 if (MO.isKill())
1179 // The register is already marked kill.
1180 return true;
Jakob Stoklund Olesen7b8fe132009-08-02 19:13:03 +00001181 if (isPhysReg && isRegTiedToDefOperand(i))
1182 // Two-address uses of physregs must not be marked kill.
1183 return true;
Dan Gohman244b86a2008-09-03 15:56:16 +00001184 MO.setIsKill();
1185 Found = true;
1186 }
1187 } else if (hasAliases && MO.isKill() &&
1188 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng794d0f72008-04-16 09:41:59 +00001189 // A super-register kill already exists.
1190 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman9d90c632008-07-03 01:18:51 +00001191 return true;
1192 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng794d0f72008-04-16 09:41:59 +00001193 DeadOps.push_back(i);
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +00001194 }
1195 }
1196
Evan Cheng794d0f72008-04-16 09:41:59 +00001197 // Trim unneeded kill operands.
1198 while (!DeadOps.empty()) {
1199 unsigned OpIdx = DeadOps.back();
1200 if (getOperand(OpIdx).isImplicit())
1201 RemoveOperand(OpIdx);
1202 else
1203 getOperand(OpIdx).setIsKill(false);
1204 DeadOps.pop_back();
1205 }
1206
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +00001207 // If not found, this means an alias of one of the operands is killed. Add a
Owen Anderson58060792008-01-24 01:10:07 +00001208 // new implicit operand if required.
Dan Gohman244b86a2008-09-03 15:56:16 +00001209 if (!Found && AddIfNotFound) {
Bill Wendlingd0b7dfc2008-03-03 22:14:33 +00001210 addOperand(MachineOperand::CreateReg(IncomingReg,
1211 false /*IsDef*/,
1212 true /*IsImp*/,
1213 true /*IsKill*/));
Owen Anderson58060792008-01-24 01:10:07 +00001214 return true;
1215 }
Dan Gohman244b86a2008-09-03 15:56:16 +00001216 return Found;
Owen Anderson58060792008-01-24 01:10:07 +00001217}
1218
1219bool MachineInstr::addRegisterDead(unsigned IncomingReg,
Dan Gohman1e57df32008-02-10 18:45:23 +00001220 const TargetRegisterInfo *RegInfo,
Owen Anderson58060792008-01-24 01:10:07 +00001221 bool AddIfNotFound) {
Evan Cheng794d0f72008-04-16 09:41:59 +00001222 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Evan Chengdd562a02008-06-27 22:11:49 +00001223 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman244b86a2008-09-03 15:56:16 +00001224 bool Found = false;
Evan Cheng794d0f72008-04-16 09:41:59 +00001225 SmallVector<unsigned,4> DeadOps;
Owen Anderson58060792008-01-24 01:10:07 +00001226 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1227 MachineOperand &MO = getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001228 if (!MO.isReg() || !MO.isDef())
Evan Cheng794d0f72008-04-16 09:41:59 +00001229 continue;
1230 unsigned Reg = MO.getReg();
Dan Gohman244b86a2008-09-03 15:56:16 +00001231 if (!Reg)
1232 continue;
1233
Evan Cheng794d0f72008-04-16 09:41:59 +00001234 if (Reg == IncomingReg) {
Dan Gohman244b86a2008-09-03 15:56:16 +00001235 if (!Found) {
1236 if (MO.isDead())
1237 // The register is already marked dead.
1238 return true;
1239 MO.setIsDead();
1240 Found = true;
1241 }
1242 } else if (hasAliases && MO.isDead() &&
1243 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng794d0f72008-04-16 09:41:59 +00001244 // There exists a super-register that's marked dead.
1245 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman9d90c632008-07-03 01:18:51 +00001246 return true;
Owen Andersonc11fa052008-08-14 18:34:18 +00001247 if (RegInfo->getSubRegisters(IncomingReg) &&
1248 RegInfo->getSuperRegisters(Reg) &&
1249 RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng794d0f72008-04-16 09:41:59 +00001250 DeadOps.push_back(i);
Owen Anderson58060792008-01-24 01:10:07 +00001251 }
1252 }
1253
Evan Cheng794d0f72008-04-16 09:41:59 +00001254 // Trim unneeded dead operands.
1255 while (!DeadOps.empty()) {
1256 unsigned OpIdx = DeadOps.back();
1257 if (getOperand(OpIdx).isImplicit())
1258 RemoveOperand(OpIdx);
1259 else
1260 getOperand(OpIdx).setIsDead(false);
1261 DeadOps.pop_back();
1262 }
1263
Dan Gohman244b86a2008-09-03 15:56:16 +00001264 // If not found, this means an alias of one of the operands is dead. Add a
1265 // new implicit operand if required.
Chris Lattnerf29b6dc2009-06-24 17:54:48 +00001266 if (Found || !AddIfNotFound)
1267 return Found;
1268
1269 addOperand(MachineOperand::CreateReg(IncomingReg,
1270 true /*IsDef*/,
1271 true /*IsImp*/,
1272 false /*IsKill*/,
1273 true /*IsDead*/));
1274 return true;
Owen Anderson58060792008-01-24 01:10:07 +00001275}