blob: a761c2daa96b648ee3a7ee16336f16be9814d887 [file] [log] [blame]
Chris Lattnere138b3d2008-01-01 20:36:19 +00001//===-- lib/CodeGen/MachineInstr.cpp --------------------------------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Brian Gaeke21326fc2004-02-13 04:39:32 +00009//
10// Methods common to all machine instructions.
11//
Chris Lattner035dfbe2002-08-09 20:08:06 +000012//===----------------------------------------------------------------------===//
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000013
Chris Lattner822b4fb2001-09-07 17:18:30 +000014#include "llvm/CodeGen/MachineInstr.h"
Evan Chengfb112882009-03-23 08:01:15 +000015#include "llvm/Constants.h"
Dan Gohman8c2b5252009-10-30 01:27:03 +000016#include "llvm/Function.h"
Evan Chengfb112882009-03-23 08:01:15 +000017#include "llvm/InlineAsm.h"
Chris Lattner5e9cd432009-12-28 08:30:43 +000018#include "llvm/Type.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000019#include "llvm/Value.h"
Dan Gohmancd26ec52009-09-23 01:33:16 +000020#include "llvm/Assembly/Writer.h"
Chris Lattner8517e1f2004-02-19 16:17:08 +000021#include "llvm/CodeGen/MachineFunction.h"
Dan Gohmanc76909a2009-09-25 20:36:54 +000022#include "llvm/CodeGen/MachineMemOperand.h"
Chris Lattner62ed6b92008-01-01 01:12:31 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000024#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner10491642002-10-30 00:48:05 +000025#include "llvm/Target/TargetMachine.h"
Evan Chengbb81d972008-01-31 09:59:15 +000026#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerf14cf852008-01-07 07:42:25 +000027#include "llvm/Target/TargetInstrDesc.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmane33f44c2009-10-07 17:38:06 +000029#include "llvm/Analysis/AliasAnalysis.h"
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +000030#include "llvm/Analysis/DebugInfo.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000031#include "llvm/Support/ErrorHandling.h"
Dan Gohman2c3f7ae2008-07-17 23:49:46 +000032#include "llvm/Support/LeakDetector.h"
Dan Gohmance42e402008-07-07 20:32:02 +000033#include "llvm/Support/MathExtras.h"
Chris Lattneredfb72c2008-08-24 20:37:32 +000034#include "llvm/Support/raw_ostream.h"
Dan Gohmanb8d2f552008-08-20 15:58:01 +000035#include "llvm/ADT/FoldingSet.h"
Chris Lattner0742b592004-02-23 18:38:20 +000036using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000037
Chris Lattnerf7382302007-12-30 21:56:09 +000038//===----------------------------------------------------------------------===//
39// MachineOperand Implementation
40//===----------------------------------------------------------------------===//
41
Chris Lattner62ed6b92008-01-01 01:12:31 +000042/// AddRegOperandToRegInfo - Add this register operand to the specified
43/// MachineRegisterInfo. If it is null, then the next/prev fields should be
44/// explicitly nulled out.
45void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) {
Dan Gohmand735b802008-10-03 15:45:36 +000046 assert(isReg() && "Can only add reg operand to use lists");
Chris Lattner62ed6b92008-01-01 01:12:31 +000047
48 // If the reginfo pointer is null, just explicitly null out or next/prev
49 // pointers, to ensure they are not garbage.
50 if (RegInfo == 0) {
51 Contents.Reg.Prev = 0;
52 Contents.Reg.Next = 0;
53 return;
54 }
55
56 // Otherwise, add this operand to the head of the registers use/def list.
Chris Lattner80fe5312008-01-01 21:08:22 +000057 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg());
Chris Lattner62ed6b92008-01-01 01:12:31 +000058
Chris Lattner80fe5312008-01-01 21:08:22 +000059 // For SSA values, we prefer to keep the definition at the start of the list.
60 // we do this by skipping over the definition if it is at the head of the
61 // list.
62 if (*Head && (*Head)->isDef())
63 Head = &(*Head)->Contents.Reg.Next;
64
65 Contents.Reg.Next = *Head;
Chris Lattner62ed6b92008-01-01 01:12:31 +000066 if (Contents.Reg.Next) {
67 assert(getReg() == Contents.Reg.Next->getReg() &&
68 "Different regs on the same list!");
69 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next;
70 }
71
Chris Lattner80fe5312008-01-01 21:08:22 +000072 Contents.Reg.Prev = Head;
73 *Head = this;
Chris Lattner62ed6b92008-01-01 01:12:31 +000074}
75
Dan Gohman3bc1a372009-04-15 01:17:37 +000076/// RemoveRegOperandFromRegInfo - Remove this register operand from the
77/// MachineRegisterInfo it is linked with.
78void MachineOperand::RemoveRegOperandFromRegInfo() {
79 assert(isOnRegUseList() && "Reg operand is not on a use list");
80 // Unlink this from the doubly linked list of operands.
81 MachineOperand *NextOp = Contents.Reg.Next;
82 *Contents.Reg.Prev = NextOp;
83 if (NextOp) {
84 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!");
85 NextOp->Contents.Reg.Prev = Contents.Reg.Prev;
86 }
87 Contents.Reg.Prev = 0;
88 Contents.Reg.Next = 0;
89}
90
Chris Lattner62ed6b92008-01-01 01:12:31 +000091void MachineOperand::setReg(unsigned Reg) {
92 if (getReg() == Reg) return; // No change.
93
94 // Otherwise, we have to change the register. If this operand is embedded
95 // into a machine function, we need to update the old and new register's
96 // use/def lists.
97 if (MachineInstr *MI = getParent())
98 if (MachineBasicBlock *MBB = MI->getParent())
99 if (MachineFunction *MF = MBB->getParent()) {
100 RemoveRegOperandFromRegInfo();
101 Contents.Reg.RegNo = Reg;
102 AddRegOperandToRegInfo(&MF->getRegInfo());
103 return;
104 }
105
106 // Otherwise, just change the register, no problem. :)
107 Contents.Reg.RegNo = Reg;
108}
109
110/// ChangeToImmediate - Replace this operand with a new immediate operand of
111/// the specified value. If an operand is known to be an immediate already,
112/// the setImm method should be used.
113void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
114 // If this operand is currently a register operand, and if this is in a
115 // function, deregister the operand from the register's use/def list.
Dan Gohmand735b802008-10-03 15:45:36 +0000116 if (isReg() && getParent() && getParent()->getParent() &&
Chris Lattner62ed6b92008-01-01 01:12:31 +0000117 getParent()->getParent()->getParent())
118 RemoveRegOperandFromRegInfo();
119
120 OpKind = MO_Immediate;
121 Contents.ImmVal = ImmVal;
122}
123
124/// ChangeToRegister - Replace this operand with a new register operand of
125/// the specified value. If an operand is known to be an register already,
126/// the setReg method should be used.
127void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
Evan Cheng4784f1f2009-06-30 08:49:04 +0000128 bool isKill, bool isDead, bool isUndef) {
Chris Lattner62ed6b92008-01-01 01:12:31 +0000129 // If this operand is already a register operand, use setReg to update the
130 // register's use/def lists.
Dan Gohmand735b802008-10-03 15:45:36 +0000131 if (isReg()) {
Dale Johannesene0091802008-09-14 01:44:36 +0000132 assert(!isEarlyClobber());
Chris Lattner62ed6b92008-01-01 01:12:31 +0000133 setReg(Reg);
134 } else {
135 // Otherwise, change this to a register and set the reg#.
136 OpKind = MO_Register;
137 Contents.Reg.RegNo = Reg;
138
139 // If this operand is embedded in a function, add the operand to the
140 // register's use/def list.
141 if (MachineInstr *MI = getParent())
142 if (MachineBasicBlock *MBB = MI->getParent())
143 if (MachineFunction *MF = MBB->getParent())
144 AddRegOperandToRegInfo(&MF->getRegInfo());
145 }
146
147 IsDef = isDef;
148 IsImp = isImp;
149 IsKill = isKill;
150 IsDead = isDead;
Evan Cheng4784f1f2009-06-30 08:49:04 +0000151 IsUndef = isUndef;
Dale Johannesene0091802008-09-14 01:44:36 +0000152 IsEarlyClobber = false;
Chris Lattner62ed6b92008-01-01 01:12:31 +0000153 SubReg = 0;
154}
155
Chris Lattnerf7382302007-12-30 21:56:09 +0000156/// isIdenticalTo - Return true if this operand is identical to the specified
157/// operand.
158bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
Chris Lattner31530612009-06-24 17:54:48 +0000159 if (getType() != Other.getType() ||
160 getTargetFlags() != Other.getTargetFlags())
161 return false;
Chris Lattnerf7382302007-12-30 21:56:09 +0000162
163 switch (getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000164 default: llvm_unreachable("Unrecognized operand type");
Chris Lattnerf7382302007-12-30 21:56:09 +0000165 case MachineOperand::MO_Register:
166 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
167 getSubReg() == Other.getSubReg();
168 case MachineOperand::MO_Immediate:
169 return getImm() == Other.getImm();
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000170 case MachineOperand::MO_FPImmediate:
171 return getFPImm() == Other.getFPImm();
Chris Lattnerf7382302007-12-30 21:56:09 +0000172 case MachineOperand::MO_MachineBasicBlock:
173 return getMBB() == Other.getMBB();
174 case MachineOperand::MO_FrameIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000175 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000176 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000177 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattnerf7382302007-12-30 21:56:09 +0000178 case MachineOperand::MO_JumpTableIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000179 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000180 case MachineOperand::MO_GlobalAddress:
181 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
182 case MachineOperand::MO_ExternalSymbol:
183 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
184 getOffset() == Other.getOffset();
Dan Gohman8c2b5252009-10-30 01:27:03 +0000185 case MachineOperand::MO_BlockAddress:
186 return getBlockAddress() == Other.getBlockAddress();
Chris Lattnerf7382302007-12-30 21:56:09 +0000187 }
188}
189
190/// print - Print the specified machine operand.
191///
Mon P Wang5ca6bd12008-10-10 01:43:55 +0000192void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const {
Dan Gohman80f6c582009-11-09 19:38:45 +0000193 // If the instruction is embedded into a basic block, we can find the
194 // target info for the instruction.
195 if (!TM)
196 if (const MachineInstr *MI = getParent())
197 if (const MachineBasicBlock *MBB = MI->getParent())
198 if (const MachineFunction *MF = MBB->getParent())
199 TM = &MF->getTarget();
200
Chris Lattnerf7382302007-12-30 21:56:09 +0000201 switch (getType()) {
202 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000203 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000204 OS << "%reg" << getReg();
205 } else {
Chris Lattnerf7382302007-12-30 21:56:09 +0000206 if (TM)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000207 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
Chris Lattnerf7382302007-12-30 21:56:09 +0000208 else
Dan Gohman0ba90f32009-10-31 20:19:03 +0000209 OS << "%physreg" << getReg();
Chris Lattnerf7382302007-12-30 21:56:09 +0000210 }
Dan Gohman2ccc8392008-12-18 21:51:27 +0000211
Evan Cheng4784f1f2009-06-30 08:49:04 +0000212 if (getSubReg() != 0)
Chris Lattner31530612009-06-24 17:54:48 +0000213 OS << ':' << getSubReg();
Dan Gohman2ccc8392008-12-18 21:51:27 +0000214
Evan Cheng4784f1f2009-06-30 08:49:04 +0000215 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
216 isEarlyClobber()) {
Chris Lattner31530612009-06-24 17:54:48 +0000217 OS << '<';
Chris Lattnerf7382302007-12-30 21:56:09 +0000218 bool NeedComma = false;
Evan Cheng07897072009-10-14 23:37:31 +0000219 if (isDef()) {
Chris Lattner31530612009-06-24 17:54:48 +0000220 if (NeedComma) OS << ',';
Dale Johannesen913d3df2008-09-12 17:49:03 +0000221 if (isEarlyClobber())
222 OS << "earlyclobber,";
Evan Cheng07897072009-10-14 23:37:31 +0000223 if (isImplicit())
224 OS << "imp-";
Chris Lattnerf7382302007-12-30 21:56:09 +0000225 OS << "def";
226 NeedComma = true;
Evan Cheng5affca02009-10-21 07:56:02 +0000227 } else if (isImplicit()) {
Evan Cheng07897072009-10-14 23:37:31 +0000228 OS << "imp-use";
Evan Cheng5affca02009-10-21 07:56:02 +0000229 NeedComma = true;
230 }
Evan Cheng07897072009-10-14 23:37:31 +0000231
Evan Cheng4784f1f2009-06-30 08:49:04 +0000232 if (isKill() || isDead() || isUndef()) {
Chris Lattner31530612009-06-24 17:54:48 +0000233 if (NeedComma) OS << ',';
Bill Wendling181eb732008-02-24 00:56:13 +0000234 if (isKill()) OS << "kill";
235 if (isDead()) OS << "dead";
Evan Cheng4784f1f2009-06-30 08:49:04 +0000236 if (isUndef()) {
237 if (isKill() || isDead())
238 OS << ',';
239 OS << "undef";
240 }
Chris Lattnerf7382302007-12-30 21:56:09 +0000241 }
Chris Lattner31530612009-06-24 17:54:48 +0000242 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000243 }
244 break;
245 case MachineOperand::MO_Immediate:
246 OS << getImm();
247 break;
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000248 case MachineOperand::MO_FPImmediate:
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000249 if (getFPImm()->getType()->isFloatTy())
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000250 OS << getFPImm()->getValueAPF().convertToFloat();
Chris Lattner31530612009-06-24 17:54:48 +0000251 else
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000252 OS << getFPImm()->getValueAPF().convertToDouble();
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000253 break;
Chris Lattnerf7382302007-12-30 21:56:09 +0000254 case MachineOperand::MO_MachineBasicBlock:
Dan Gohman0ba90f32009-10-31 20:19:03 +0000255 OS << "<BB#" << getMBB()->getNumber() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000256 break;
257 case MachineOperand::MO_FrameIndex:
Chris Lattner31530612009-06-24 17:54:48 +0000258 OS << "<fi#" << getIndex() << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000259 break;
260 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000261 OS << "<cp#" << getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000262 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000263 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000264 break;
265 case MachineOperand::MO_JumpTableIndex:
Chris Lattner31530612009-06-24 17:54:48 +0000266 OS << "<jt#" << getIndex() << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000267 break;
268 case MachineOperand::MO_GlobalAddress:
Dan Gohman8d4e3b52009-11-06 18:03:10 +0000269 OS << "<ga:";
270 WriteAsOperand(OS, getGlobal(), /*PrintType=*/false);
Chris Lattnerf7382302007-12-30 21:56:09 +0000271 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000272 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000273 break;
274 case MachineOperand::MO_ExternalSymbol:
275 OS << "<es:" << getSymbolName();
276 if (getOffset()) OS << "+" << getOffset();
Chris Lattner31530612009-06-24 17:54:48 +0000277 OS << '>';
Chris Lattnerf7382302007-12-30 21:56:09 +0000278 break;
Dan Gohman8c2b5252009-10-30 01:27:03 +0000279 case MachineOperand::MO_BlockAddress:
Dan Gohman0ba90f32009-10-31 20:19:03 +0000280 OS << "<";
281 WriteAsOperand(OS, getBlockAddress(), /*PrintType=*/false);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000282 OS << '>';
283 break;
Chris Lattnerf7382302007-12-30 21:56:09 +0000284 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000285 llvm_unreachable("Unrecognized operand type");
Chris Lattnerf7382302007-12-30 21:56:09 +0000286 }
Chris Lattner31530612009-06-24 17:54:48 +0000287
288 if (unsigned TF = getTargetFlags())
289 OS << "[TF=" << TF << ']';
Chris Lattnerf7382302007-12-30 21:56:09 +0000290}
291
292//===----------------------------------------------------------------------===//
Dan Gohmance42e402008-07-07 20:32:02 +0000293// MachineMemOperand Implementation
294//===----------------------------------------------------------------------===//
295
296MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f,
297 int64_t o, uint64_t s, unsigned int a)
298 : Offset(o), Size(s), V(v),
299 Flags((f & 7) | ((Log2_32(a) + 1) << 3)) {
Dan Gohman28f02fd2009-09-21 19:47:04 +0000300 assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
Dan Gohmanc5e1f982008-07-16 15:56:42 +0000301 assert((isLoad() || isStore()) && "Not a load/store!");
Dan Gohmance42e402008-07-07 20:32:02 +0000302}
303
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000304/// Profile - Gather unique data for the object.
305///
306void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
307 ID.AddInteger(Offset);
308 ID.AddInteger(Size);
309 ID.AddPointer(V);
310 ID.AddInteger(Flags);
311}
312
Dan Gohmanc76909a2009-09-25 20:36:54 +0000313void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
314 // The Value and Offset may differ due to CSE. But the flags and size
315 // should be the same.
316 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
317 assert(MMO->getSize() == getSize() && "Size mismatch!");
318
319 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
320 // Update the alignment value.
321 Flags = (Flags & 7) | ((Log2_32(MMO->getBaseAlignment()) + 1) << 3);
322 // Also update the base and offset, because the new alignment may
323 // not be applicable with the old ones.
324 V = MMO->getValue();
325 Offset = MMO->getOffset();
326 }
327}
328
Dan Gohman4b2ebc12009-09-25 23:33:20 +0000329/// getAlignment - Return the minimum known alignment in bytes of the
330/// actual memory reference.
331uint64_t MachineMemOperand::getAlignment() const {
332 return MinAlign(getBaseAlignment(), getOffset());
333}
334
Dan Gohmanc76909a2009-09-25 20:36:54 +0000335raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) {
336 assert((MMO.isLoad() || MMO.isStore()) &&
Dan Gohmancd26ec52009-09-23 01:33:16 +0000337 "SV has to be a load, store or both.");
338
Dan Gohmanc76909a2009-09-25 20:36:54 +0000339 if (MMO.isVolatile())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000340 OS << "Volatile ";
341
Dan Gohmanc76909a2009-09-25 20:36:54 +0000342 if (MMO.isLoad())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000343 OS << "LD";
Dan Gohmanc76909a2009-09-25 20:36:54 +0000344 if (MMO.isStore())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000345 OS << "ST";
Dan Gohmanc76909a2009-09-25 20:36:54 +0000346 OS << MMO.getSize();
Dan Gohmancd26ec52009-09-23 01:33:16 +0000347
348 // Print the address information.
349 OS << "[";
Dan Gohmanc76909a2009-09-25 20:36:54 +0000350 if (!MMO.getValue())
Dan Gohmancd26ec52009-09-23 01:33:16 +0000351 OS << "<unknown>";
352 else
Dan Gohmanc76909a2009-09-25 20:36:54 +0000353 WriteAsOperand(OS, MMO.getValue(), /*PrintType=*/false);
Dan Gohmancd26ec52009-09-23 01:33:16 +0000354
355 // If the alignment of the memory reference itself differs from the alignment
356 // of the base pointer, print the base alignment explicitly, next to the base
357 // pointer.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000358 if (MMO.getBaseAlignment() != MMO.getAlignment())
359 OS << "(align=" << MMO.getBaseAlignment() << ")";
Dan Gohmancd26ec52009-09-23 01:33:16 +0000360
Dan Gohmanc76909a2009-09-25 20:36:54 +0000361 if (MMO.getOffset() != 0)
362 OS << "+" << MMO.getOffset();
Dan Gohmancd26ec52009-09-23 01:33:16 +0000363 OS << "]";
364
365 // Print the alignment of the reference.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000366 if (MMO.getBaseAlignment() != MMO.getAlignment() ||
367 MMO.getBaseAlignment() != MMO.getSize())
368 OS << "(align=" << MMO.getAlignment() << ")";
Dan Gohmancd26ec52009-09-23 01:33:16 +0000369
370 return OS;
371}
372
Dan Gohmance42e402008-07-07 20:32:02 +0000373//===----------------------------------------------------------------------===//
Chris Lattnerf7382302007-12-30 21:56:09 +0000374// MachineInstr Implementation
375//===----------------------------------------------------------------------===//
376
Evan Chengc0f64ff2006-11-27 23:37:22 +0000377/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
Evan Cheng67f660c2006-11-30 07:08:44 +0000378/// TID NULL and no operands.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000379MachineInstr::MachineInstr()
Dan Gohman834651c2009-11-16 22:49:38 +0000380 : TID(0), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0),
Dan Gohmanc76909a2009-09-25 20:36:54 +0000381 Parent(0), debugLoc(DebugLoc::getUnknownLoc()) {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000382 // Make sure that we get added to a machine basicblock
383 LeakDetector::addGarbageObject(this);
Chris Lattner72791222002-10-28 20:59:49 +0000384}
385
Evan Cheng67f660c2006-11-30 07:08:44 +0000386void MachineInstr::addImplicitDefUseOperands() {
387 if (TID->ImplicitDefs)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000388 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Chris Lattner8019f412007-12-30 00:41:17 +0000389 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng67f660c2006-11-30 07:08:44 +0000390 if (TID->ImplicitUses)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000391 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Chris Lattner8019f412007-12-30 00:41:17 +0000392 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
Evan Chengd7de4962006-11-13 23:34:06 +0000393}
394
395/// MachineInstr ctor - This constructor create a MachineInstr and add the
Evan Chengc0f64ff2006-11-27 23:37:22 +0000396/// implicit operands. It reserves space for number of operands specified by
Chris Lattner749c6f62008-01-07 07:27:27 +0000397/// TargetInstrDesc or the numOperands if it is not zero. (for
Evan Chengc0f64ff2006-11-27 23:37:22 +0000398/// instructions with variable number of operands).
Chris Lattner749c6f62008-01-07 07:27:27 +0000399MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp)
Dan Gohman834651c2009-11-16 22:49:38 +0000400 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0),
401 MemRefs(0), MemRefsEnd(0), Parent(0),
Dale Johannesen06efc022009-01-27 23:20:29 +0000402 debugLoc(DebugLoc::getUnknownLoc()) {
Chris Lattner349c4952008-01-07 03:13:06 +0000403 if (!NoImp && TID->getImplicitDefs())
404 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000405 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000406 if (!NoImp && TID->getImplicitUses())
407 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000408 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000409 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Chengfa945722007-10-13 02:23:01 +0000410 if (!NoImp)
411 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000412 // Make sure that we get added to a machine basicblock
413 LeakDetector::addGarbageObject(this);
Evan Chengd7de4962006-11-13 23:34:06 +0000414}
415
Dale Johannesen06efc022009-01-27 23:20:29 +0000416/// MachineInstr ctor - As above, but with a DebugLoc.
417MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl,
418 bool NoImp)
Dan Gohman834651c2009-11-16 22:49:38 +0000419 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0),
Dan Gohmanc76909a2009-09-25 20:36:54 +0000420 Parent(0), debugLoc(dl) {
Dale Johannesen06efc022009-01-27 23:20:29 +0000421 if (!NoImp && TID->getImplicitDefs())
422 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
423 NumImplicitOps++;
424 if (!NoImp && TID->getImplicitUses())
425 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
426 NumImplicitOps++;
427 Operands.reserve(NumImplicitOps + TID->getNumOperands());
428 if (!NoImp)
429 addImplicitDefUseOperands();
430 // Make sure that we get added to a machine basicblock
431 LeakDetector::addGarbageObject(this);
432}
433
434/// MachineInstr ctor - Work exactly the same as the ctor two above, except
435/// that the MachineInstr is created and added to the end of the specified
436/// basic block.
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000437///
Dale Johannesen06efc022009-01-27 23:20:29 +0000438MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid)
Dan Gohman834651c2009-11-16 22:49:38 +0000439 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0),
440 MemRefs(0), MemRefsEnd(0), Parent(0),
Dale Johannesen06efc022009-01-27 23:20:29 +0000441 debugLoc(DebugLoc::getUnknownLoc()) {
442 assert(MBB && "Cannot use inserting ctor with null basic block!");
443 if (TID->ImplicitDefs)
444 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
445 NumImplicitOps++;
446 if (TID->ImplicitUses)
447 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
448 NumImplicitOps++;
449 Operands.reserve(NumImplicitOps + TID->getNumOperands());
450 addImplicitDefUseOperands();
451 // Make sure that we get added to a machine basicblock
452 LeakDetector::addGarbageObject(this);
453 MBB->push_back(this); // Add instruction to end of basic block!
454}
455
456/// MachineInstr ctor - As above, but with a DebugLoc.
457///
458MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl,
Chris Lattner749c6f62008-01-07 07:27:27 +0000459 const TargetInstrDesc &tid)
Dan Gohman834651c2009-11-16 22:49:38 +0000460 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0),
Dan Gohmanc76909a2009-09-25 20:36:54 +0000461 Parent(0), debugLoc(dl) {
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000462 assert(MBB && "Cannot use inserting ctor with null basic block!");
Evan Cheng67f660c2006-11-30 07:08:44 +0000463 if (TID->ImplicitDefs)
Chris Lattner349c4952008-01-07 03:13:06 +0000464 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000465 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +0000466 if (TID->ImplicitUses)
Chris Lattner349c4952008-01-07 03:13:06 +0000467 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000468 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000469 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Cheng67f660c2006-11-30 07:08:44 +0000470 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000471 // Make sure that we get added to a machine basicblock
472 LeakDetector::addGarbageObject(this);
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000473 MBB->push_back(this); // Add instruction to end of basic block!
474}
475
Misha Brukmance22e762004-07-09 14:45:17 +0000476/// MachineInstr ctor - Copies MachineInstr arg exactly
477///
Evan Cheng1ed99222008-07-19 00:37:25 +0000478MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Dan Gohman834651c2009-11-16 22:49:38 +0000479 : TID(&MI.getDesc()), NumImplicitOps(0), AsmPrinterFlags(0),
Dan Gohmanc76909a2009-09-25 20:36:54 +0000480 MemRefs(MI.MemRefs), MemRefsEnd(MI.MemRefsEnd),
481 Parent(0), debugLoc(MI.getDebugLoc()) {
Chris Lattner943b5e12006-05-04 19:14:44 +0000482 Operands.reserve(MI.getNumOperands());
Tanya Lattnerb5159ed2004-05-23 20:58:02 +0000483
Misha Brukmance22e762004-07-09 14:45:17 +0000484 // Add operands
Evan Cheng1ed99222008-07-19 00:37:25 +0000485 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
486 addOperand(MI.getOperand(i));
487 NumImplicitOps = MI.NumImplicitOps;
Tanya Lattner0c63e032004-05-24 03:14:18 +0000488
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000489 // Set parent to null.
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000490 Parent = 0;
Dan Gohman6116a732008-07-21 18:47:29 +0000491
492 LeakDetector::addGarbageObject(this);
Tanya Lattner466b5342004-05-23 19:35:12 +0000493}
494
Misha Brukmance22e762004-07-09 14:45:17 +0000495MachineInstr::~MachineInstr() {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000496 LeakDetector::removeGarbageObject(this);
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000497#ifndef NDEBUG
Chris Lattner62ed6b92008-01-01 01:12:31 +0000498 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000499 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
Dan Gohmand735b802008-10-03 15:45:36 +0000500 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
Chris Lattner62ed6b92008-01-01 01:12:31 +0000501 "Reg operand def/use list corrupted");
502 }
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000503#endif
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000504}
505
Chris Lattner62ed6b92008-01-01 01:12:31 +0000506/// getRegInfo - If this instruction is embedded into a MachineFunction,
507/// return the MachineRegisterInfo object for the current function, otherwise
508/// return null.
509MachineRegisterInfo *MachineInstr::getRegInfo() {
510 if (MachineBasicBlock *MBB = getParent())
Dan Gohman4e526b92008-07-08 23:59:09 +0000511 return &MBB->getParent()->getRegInfo();
Chris Lattner62ed6b92008-01-01 01:12:31 +0000512 return 0;
513}
514
515/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
516/// this instruction from their respective use lists. This requires that the
517/// operands already be on their use lists.
518void MachineInstr::RemoveRegOperandsFromUseLists() {
519 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000520 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000521 Operands[i].RemoveRegOperandFromRegInfo();
522 }
523}
524
525/// AddRegOperandsToUseLists - Add all of the register operands in
526/// this instruction from their respective use lists. This requires that the
527/// operands not be on their use lists yet.
528void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
529 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000530 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000531 Operands[i].AddRegOperandToRegInfo(&RegInfo);
532 }
533}
534
535
536/// addOperand - Add the specified operand to the instruction. If it is an
537/// implicit operand, it is added to the end of the operand list. If it is
538/// an explicit operand it is added at the end of the explicit operand list
539/// (before the first implicit operand).
540void MachineInstr::addOperand(const MachineOperand &Op) {
Dan Gohmand735b802008-10-03 15:45:36 +0000541 bool isImpReg = Op.isReg() && Op.isImplicit();
Chris Lattner62ed6b92008-01-01 01:12:31 +0000542 assert((isImpReg || !OperandsComplete()) &&
543 "Trying to add an operand to a machine instr that is already done!");
544
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000545 MachineRegisterInfo *RegInfo = getRegInfo();
546
Chris Lattner62ed6b92008-01-01 01:12:31 +0000547 // If we are adding the operand to the end of the list, our job is simpler.
548 // This is true most of the time, so this is a reasonable optimization.
549 if (isImpReg || NumImplicitOps == 0) {
550 // We can only do this optimization if we know that the operand list won't
551 // reallocate.
552 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
553 Operands.push_back(Op);
554
555 // Set the parent of the operand.
556 Operands.back().ParentMI = this;
557
558 // If the operand is a register, update the operand's use list.
Jim Grosbach06801722009-12-16 19:43:02 +0000559 if (Op.isReg()) {
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000560 Operands.back().AddRegOperandToRegInfo(RegInfo);
Jim Grosbach06801722009-12-16 19:43:02 +0000561 // If the register operand is flagged as early, mark the operand as such
562 unsigned OpNo = Operands.size() - 1;
563 if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
564 Operands[OpNo].setIsEarlyClobber(true);
565 }
Chris Lattner62ed6b92008-01-01 01:12:31 +0000566 return;
567 }
568 }
569
570 // Otherwise, we have to insert a real operand before any implicit ones.
571 unsigned OpNo = Operands.size()-NumImplicitOps;
572
Chris Lattner62ed6b92008-01-01 01:12:31 +0000573 // If this instruction isn't embedded into a function, then we don't need to
574 // update any operand lists.
575 if (RegInfo == 0) {
576 // Simple insertion, no reginfo update needed for other register operands.
577 Operands.insert(Operands.begin()+OpNo, Op);
578 Operands[OpNo].ParentMI = this;
579
580 // Do explicitly set the reginfo for this operand though, to ensure the
581 // next/prev fields are properly nulled out.
Jim Grosbach06801722009-12-16 19:43:02 +0000582 if (Operands[OpNo].isReg()) {
Chris Lattner62ed6b92008-01-01 01:12:31 +0000583 Operands[OpNo].AddRegOperandToRegInfo(0);
Jim Grosbach06801722009-12-16 19:43:02 +0000584 // If the register operand is flagged as early, mark the operand as such
585 if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
586 Operands[OpNo].setIsEarlyClobber(true);
587 }
Chris Lattner62ed6b92008-01-01 01:12:31 +0000588
589 } else if (Operands.size()+1 <= Operands.capacity()) {
590 // Otherwise, we have to remove register operands from their register use
591 // list, add the operand, then add the register operands back to their use
592 // list. This also must handle the case when the operand list reallocates
593 // to somewhere else.
594
595 // If insertion of this operand won't cause reallocation of the operand
596 // list, just remove the implicit operands, add the operand, then re-add all
597 // the rest of the operands.
598 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000599 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000600 Operands[i].RemoveRegOperandFromRegInfo();
601 }
602
603 // Add the operand. If it is a register, add it to the reg list.
604 Operands.insert(Operands.begin()+OpNo, Op);
605 Operands[OpNo].ParentMI = this;
606
Jim Grosbach06801722009-12-16 19:43:02 +0000607 if (Operands[OpNo].isReg()) {
Chris Lattner62ed6b92008-01-01 01:12:31 +0000608 Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
Jim Grosbach06801722009-12-16 19:43:02 +0000609 // If the register operand is flagged as early, mark the operand as such
610 if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
611 Operands[OpNo].setIsEarlyClobber(true);
612 }
Chris Lattner62ed6b92008-01-01 01:12:31 +0000613
614 // Re-add all the implicit ops.
615 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000616 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000617 Operands[i].AddRegOperandToRegInfo(RegInfo);
618 }
619 } else {
620 // Otherwise, we will be reallocating the operand list. Remove all reg
621 // operands from their list, then readd them after the operand list is
622 // reallocated.
623 RemoveRegOperandsFromUseLists();
624
625 Operands.insert(Operands.begin()+OpNo, Op);
626 Operands[OpNo].ParentMI = this;
627
628 // Re-add all the operands.
629 AddRegOperandsToUseLists(*RegInfo);
Jim Grosbach06801722009-12-16 19:43:02 +0000630
631 // If the register operand is flagged as early, mark the operand as such
632 if (Operands[OpNo].isReg()
633 && TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
634 Operands[OpNo].setIsEarlyClobber(true);
Chris Lattner62ed6b92008-01-01 01:12:31 +0000635 }
636}
637
638/// RemoveOperand - Erase an operand from an instruction, leaving it with one
639/// fewer operand than it started with.
640///
641void MachineInstr::RemoveOperand(unsigned OpNo) {
642 assert(OpNo < Operands.size() && "Invalid operand number");
643
644 // Special case removing the last one.
645 if (OpNo == Operands.size()-1) {
646 // If needed, remove from the reg def/use list.
Dan Gohmand735b802008-10-03 15:45:36 +0000647 if (Operands.back().isReg() && Operands.back().isOnRegUseList())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000648 Operands.back().RemoveRegOperandFromRegInfo();
649
650 Operands.pop_back();
651 return;
652 }
653
654 // Otherwise, we are removing an interior operand. If we have reginfo to
655 // update, remove all operands that will be shifted down from their reg lists,
656 // move everything down, then re-add them.
657 MachineRegisterInfo *RegInfo = getRegInfo();
658 if (RegInfo) {
659 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000660 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000661 Operands[i].RemoveRegOperandFromRegInfo();
662 }
663 }
664
665 Operands.erase(Operands.begin()+OpNo);
666
667 if (RegInfo) {
668 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000669 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000670 Operands[i].AddRegOperandToRegInfo(RegInfo);
671 }
672 }
673}
674
Dan Gohmanc76909a2009-09-25 20:36:54 +0000675/// addMemOperand - Add a MachineMemOperand to the machine instruction.
676/// This function should be used only occasionally. The setMemRefs function
677/// is the primary method for setting up a MachineInstr's MemRefs list.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000678void MachineInstr::addMemOperand(MachineFunction &MF,
Dan Gohmanc76909a2009-09-25 20:36:54 +0000679 MachineMemOperand *MO) {
680 mmo_iterator OldMemRefs = MemRefs;
681 mmo_iterator OldMemRefsEnd = MemRefsEnd;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000682
Dan Gohmanc76909a2009-09-25 20:36:54 +0000683 size_t NewNum = (MemRefsEnd - MemRefs) + 1;
684 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
685 mmo_iterator NewMemRefsEnd = NewMemRefs + NewNum;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000686
Dan Gohmanc76909a2009-09-25 20:36:54 +0000687 std::copy(OldMemRefs, OldMemRefsEnd, NewMemRefs);
688 NewMemRefs[NewNum - 1] = MO;
689
690 MemRefs = NewMemRefs;
691 MemRefsEnd = NewMemRefsEnd;
692}
Chris Lattner62ed6b92008-01-01 01:12:31 +0000693
Chris Lattner48d7c062006-04-17 21:35:41 +0000694/// removeFromParent - This method unlinks 'this' from the containing basic
695/// block, and returns it, but does not delete it.
696MachineInstr *MachineInstr::removeFromParent() {
697 assert(getParent() && "Not embedded in a basic block!");
698 getParent()->remove(this);
699 return this;
700}
701
702
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000703/// eraseFromParent - This method unlinks 'this' from the containing basic
704/// block, and deletes it.
705void MachineInstr::eraseFromParent() {
706 assert(getParent() && "Not embedded in a basic block!");
707 getParent()->erase(this);
708}
709
710
Brian Gaeke21326fc2004-02-13 04:39:32 +0000711/// OperandComplete - Return true if it's illegal to add a new operand
712///
Chris Lattner2a90ba62004-02-12 16:09:53 +0000713bool MachineInstr::OperandsComplete() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000714 unsigned short NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000715 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands)
Vikram S. Adve34977822003-05-31 07:39:06 +0000716 return true; // Broken: we have all the operands of this instruction!
Chris Lattner413746e2002-10-28 20:48:39 +0000717 return false;
718}
719
Evan Cheng19e3f312007-05-15 01:26:09 +0000720/// getNumExplicitOperands - Returns the number of non-implicit operands.
721///
722unsigned MachineInstr::getNumExplicitOperands() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000723 unsigned NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000724 if (!TID->isVariadic())
Evan Cheng19e3f312007-05-15 01:26:09 +0000725 return NumOperands;
726
Dan Gohman9407cd42009-04-15 17:59:11 +0000727 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
728 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000729 if (!MO.isReg() || !MO.isImplicit())
Evan Cheng19e3f312007-05-15 01:26:09 +0000730 NumOperands++;
731 }
732 return NumOperands;
733}
734
Chris Lattner8ace2cd2006-10-20 22:39:59 +0000735
Dan Gohman44066042008-07-01 00:05:16 +0000736/// isLabel - Returns true if the MachineInstr represents a label.
737///
738bool MachineInstr::isLabel() const {
739 return getOpcode() == TargetInstrInfo::DBG_LABEL ||
740 getOpcode() == TargetInstrInfo::EH_LABEL ||
741 getOpcode() == TargetInstrInfo::GC_LABEL;
742}
743
Evan Chengbb81d972008-01-31 09:59:15 +0000744/// isDebugLabel - Returns true if the MachineInstr represents a debug label.
745///
746bool MachineInstr::isDebugLabel() const {
Dan Gohman44066042008-07-01 00:05:16 +0000747 return getOpcode() == TargetInstrInfo::DBG_LABEL;
Evan Chengbb81d972008-01-31 09:59:15 +0000748}
749
Evan Chengfaa51072007-04-26 19:00:32 +0000750/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Jim Grosbachf9ca50e2009-09-17 17:57:26 +0000751/// the specific register or -1 if it is not found. It further tightens
Evan Cheng76d7e762007-02-23 01:04:26 +0000752/// the search criteria to a use that kills the register if isKill is true.
Evan Cheng6130f662008-03-05 00:59:57 +0000753int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
754 const TargetRegisterInfo *TRI) const {
Evan Cheng576d1232006-12-06 08:27:42 +0000755 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Chengf277ee42007-05-29 18:35:22 +0000756 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000757 if (!MO.isReg() || !MO.isUse())
Evan Cheng6130f662008-03-05 00:59:57 +0000758 continue;
759 unsigned MOReg = MO.getReg();
760 if (!MOReg)
761 continue;
762 if (MOReg == Reg ||
763 (TRI &&
764 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
765 TargetRegisterInfo::isPhysicalRegister(Reg) &&
766 TRI->isSubRegister(MOReg, Reg)))
Evan Cheng76d7e762007-02-23 01:04:26 +0000767 if (!isKill || MO.isKill())
Evan Cheng32eb1f12007-03-26 22:37:45 +0000768 return i;
Evan Cheng576d1232006-12-06 08:27:42 +0000769 }
Evan Cheng32eb1f12007-03-26 22:37:45 +0000770 return -1;
Evan Cheng576d1232006-12-06 08:27:42 +0000771}
772
Evan Cheng6130f662008-03-05 00:59:57 +0000773/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman703bfe62008-05-06 00:20:10 +0000774/// the specified register or -1 if it is not found. If isDead is true, defs
775/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
776/// also checks if there is a def of a super-register.
Evan Cheng6130f662008-03-05 00:59:57 +0000777int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead,
778 const TargetRegisterInfo *TRI) const {
Evan Chengb371f452007-02-19 21:49:54 +0000779 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng6130f662008-03-05 00:59:57 +0000780 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000781 if (!MO.isReg() || !MO.isDef())
Evan Cheng6130f662008-03-05 00:59:57 +0000782 continue;
783 unsigned MOReg = MO.getReg();
784 if (MOReg == Reg ||
785 (TRI &&
786 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
787 TargetRegisterInfo::isPhysicalRegister(Reg) &&
788 TRI->isSubRegister(MOReg, Reg)))
789 if (!isDead || MO.isDead())
790 return i;
Evan Chengb371f452007-02-19 21:49:54 +0000791 }
Evan Cheng6130f662008-03-05 00:59:57 +0000792 return -1;
Evan Chengb371f452007-02-19 21:49:54 +0000793}
Evan Cheng19e3f312007-05-15 01:26:09 +0000794
Evan Chengf277ee42007-05-29 18:35:22 +0000795/// findFirstPredOperandIdx() - Find the index of the first operand in the
796/// operand list that is used to represent the predicate. It returns -1 if
797/// none is found.
798int MachineInstr::findFirstPredOperandIdx() const {
Chris Lattner749c6f62008-01-07 07:27:27 +0000799 const TargetInstrDesc &TID = getDesc();
800 if (TID.isPredicable()) {
Evan Cheng19e3f312007-05-15 01:26:09 +0000801 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner749c6f62008-01-07 07:27:27 +0000802 if (TID.OpInfo[i].isPredicate())
Evan Chengf277ee42007-05-29 18:35:22 +0000803 return i;
Evan Cheng19e3f312007-05-15 01:26:09 +0000804 }
805
Evan Chengf277ee42007-05-29 18:35:22 +0000806 return -1;
Evan Cheng19e3f312007-05-15 01:26:09 +0000807}
Evan Chengb371f452007-02-19 21:49:54 +0000808
Bob Wilsond9df5012009-04-09 17:16:43 +0000809/// isRegTiedToUseOperand - Given the index of a register def operand,
810/// check if the register def is tied to a source operand, due to either
811/// two-address elimination or inline assembly constraints. Returns the
812/// first tied use operand index by reference is UseOpIdx is not null.
Jakob Stoklund Olesence9be2c2009-04-29 20:57:16 +0000813bool MachineInstr::
814isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const {
Evan Chengfb112882009-03-23 08:01:15 +0000815 if (getOpcode() == TargetInstrInfo::INLINEASM) {
Bob Wilsond9df5012009-04-09 17:16:43 +0000816 assert(DefOpIdx >= 2);
817 const MachineOperand &MO = getOperand(DefOpIdx);
Chris Lattnerc30aa7b2009-04-09 23:33:34 +0000818 if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
Evan Chengfb112882009-03-23 08:01:15 +0000819 return false;
Evan Chengef5d0702009-06-24 02:05:51 +0000820 // Determine the actual operand index that corresponds to this index.
Evan Chengfb112882009-03-23 08:01:15 +0000821 unsigned DefNo = 0;
Evan Chengef5d0702009-06-24 02:05:51 +0000822 unsigned DefPart = 0;
Evan Chengfb112882009-03-23 08:01:15 +0000823 for (unsigned i = 1, e = getNumOperands(); i < e; ) {
824 const MachineOperand &FMO = getOperand(i);
Jakob Stoklund Olesen45d34fe2009-07-19 19:09:59 +0000825 // After the normal asm operands there may be additional imp-def regs.
826 if (!FMO.isImm())
827 return false;
Evan Chengfb112882009-03-23 08:01:15 +0000828 // Skip over this def.
Evan Chengef5d0702009-06-24 02:05:51 +0000829 unsigned NumOps = InlineAsm::getNumOperandRegisters(FMO.getImm());
830 unsigned PrevDef = i + 1;
831 i = PrevDef + NumOps;
832 if (i > DefOpIdx) {
833 DefPart = DefOpIdx - PrevDef;
Evan Chengfb112882009-03-23 08:01:15 +0000834 break;
Evan Chengef5d0702009-06-24 02:05:51 +0000835 }
Evan Chengfb112882009-03-23 08:01:15 +0000836 ++DefNo;
837 }
Evan Chengef5d0702009-06-24 02:05:51 +0000838 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Evan Chengfb112882009-03-23 08:01:15 +0000839 const MachineOperand &FMO = getOperand(i);
840 if (!FMO.isImm())
841 continue;
842 if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse())
843 continue;
844 unsigned Idx;
Evan Chengef5d0702009-06-24 02:05:51 +0000845 if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) &&
Bob Wilsond9df5012009-04-09 17:16:43 +0000846 Idx == DefNo) {
847 if (UseOpIdx)
Evan Chengef5d0702009-06-24 02:05:51 +0000848 *UseOpIdx = (unsigned)i + 1 + DefPart;
Evan Chengfb112882009-03-23 08:01:15 +0000849 return true;
Bob Wilsond9df5012009-04-09 17:16:43 +0000850 }
Evan Chengfb112882009-03-23 08:01:15 +0000851 }
Evan Chengef5d0702009-06-24 02:05:51 +0000852 return false;
Evan Chengfb112882009-03-23 08:01:15 +0000853 }
854
Bob Wilsond9df5012009-04-09 17:16:43 +0000855 assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!");
Chris Lattner749c6f62008-01-07 07:27:27 +0000856 const TargetInstrDesc &TID = getDesc();
Evan Chengef0732d2008-07-10 07:35:43 +0000857 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
858 const MachineOperand &MO = getOperand(i);
Dan Gohman2ce7f202008-12-05 05:45:42 +0000859 if (MO.isReg() && MO.isUse() &&
Bob Wilsond9df5012009-04-09 17:16:43 +0000860 TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) {
861 if (UseOpIdx)
862 *UseOpIdx = (unsigned)i;
Evan Chengef0732d2008-07-10 07:35:43 +0000863 return true;
Bob Wilsond9df5012009-04-09 17:16:43 +0000864 }
Evan Cheng32dfbea2007-10-12 08:50:34 +0000865 }
866 return false;
867}
868
Evan Chenga24752f2009-03-19 20:30:06 +0000869/// isRegTiedToDefOperand - Return true if the operand of the specified index
870/// is a register use and it is tied to an def operand. It also returns the def
871/// operand index by reference.
Jakob Stoklund Olesence9be2c2009-04-29 20:57:16 +0000872bool MachineInstr::
873isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const {
Evan Chengfb112882009-03-23 08:01:15 +0000874 if (getOpcode() == TargetInstrInfo::INLINEASM) {
875 const MachineOperand &MO = getOperand(UseOpIdx);
Chris Lattner0c8382c2009-04-09 16:50:43 +0000876 if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0)
Evan Chengfb112882009-03-23 08:01:15 +0000877 return false;
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000878
879 // Find the flag operand corresponding to UseOpIdx
880 unsigned FlagIdx, NumOps=0;
881 for (FlagIdx = 1; FlagIdx < UseOpIdx; FlagIdx += NumOps+1) {
882 const MachineOperand &UFMO = getOperand(FlagIdx);
Jakob Stoklund Olesen45d34fe2009-07-19 19:09:59 +0000883 // After the normal asm operands there may be additional imp-def regs.
884 if (!UFMO.isImm())
885 return false;
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000886 NumOps = InlineAsm::getNumOperandRegisters(UFMO.getImm());
887 assert(NumOps < getNumOperands() && "Invalid inline asm flag");
888 if (UseOpIdx < FlagIdx+NumOps+1)
889 break;
Evan Chengef5d0702009-06-24 02:05:51 +0000890 }
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000891 if (FlagIdx >= UseOpIdx)
Evan Chengef5d0702009-06-24 02:05:51 +0000892 return false;
Jakob Stoklund Olesen57e599a2009-07-16 20:58:34 +0000893 const MachineOperand &UFMO = getOperand(FlagIdx);
Evan Chengfb112882009-03-23 08:01:15 +0000894 unsigned DefNo;
895 if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) {
896 if (!DefOpIdx)
897 return true;
898
899 unsigned DefIdx = 1;
900 // Remember to adjust the index. First operand is asm string, then there
901 // is a flag for each.
902 while (DefNo) {
903 const MachineOperand &FMO = getOperand(DefIdx);
904 assert(FMO.isImm());
905 // Skip over this def.
906 DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
907 --DefNo;
908 }
Evan Chengef5d0702009-06-24 02:05:51 +0000909 *DefOpIdx = DefIdx + UseOpIdx - FlagIdx;
Evan Chengfb112882009-03-23 08:01:15 +0000910 return true;
911 }
912 return false;
913 }
914
Evan Chenga24752f2009-03-19 20:30:06 +0000915 const TargetInstrDesc &TID = getDesc();
916 if (UseOpIdx >= TID.getNumOperands())
917 return false;
918 const MachineOperand &MO = getOperand(UseOpIdx);
919 if (!MO.isReg() || !MO.isUse())
920 return false;
921 int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO);
922 if (DefIdx == -1)
923 return false;
924 if (DefOpIdx)
925 *DefOpIdx = (unsigned)DefIdx;
926 return true;
927}
928
Evan Cheng576d1232006-12-06 08:27:42 +0000929/// copyKillDeadInfo - Copies kill / dead operand properties from MI.
930///
931void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
932 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
933 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000934 if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
Evan Cheng576d1232006-12-06 08:27:42 +0000935 continue;
936 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
937 MachineOperand &MOp = getOperand(j);
938 if (!MOp.isIdenticalTo(MO))
939 continue;
940 if (MO.isKill())
941 MOp.setIsKill();
942 else
943 MOp.setIsDead();
944 break;
945 }
946 }
947}
948
Evan Cheng19e3f312007-05-15 01:26:09 +0000949/// copyPredicates - Copies predicate operand(s) from MI.
950void MachineInstr::copyPredicates(const MachineInstr *MI) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000951 const TargetInstrDesc &TID = MI->getDesc();
Evan Chengb27087f2008-03-13 00:44:09 +0000952 if (!TID.isPredicable())
953 return;
954 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
955 if (TID.OpInfo[i].isPredicate()) {
956 // Predicated operands must be last operands.
957 addOperand(MI->getOperand(i));
Evan Cheng19e3f312007-05-15 01:26:09 +0000958 }
959 }
960}
961
Evan Cheng9f1c8312008-07-03 09:09:37 +0000962/// isSafeToMove - Return true if it is safe to move this instruction. If
963/// SawStore is set to true, it means that there is a store (or call) between
964/// the instruction's location and its intended destination.
Dan Gohmanb3b930a2008-11-18 19:04:29 +0000965bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII,
Dan Gohmana70dca12009-10-09 23:27:56 +0000966 bool &SawStore,
967 AliasAnalysis *AA) const {
Evan Chengb27087f2008-03-13 00:44:09 +0000968 // Ignore stuff that we obviously can't move.
969 if (TID->mayStore() || TID->isCall()) {
970 SawStore = true;
971 return false;
972 }
Dan Gohman237dee12008-12-23 17:28:50 +0000973 if (TID->isTerminator() || TID->hasUnmodeledSideEffects())
Evan Chengb27087f2008-03-13 00:44:09 +0000974 return false;
975
976 // See if this instruction does a load. If so, we have to guarantee that the
977 // loaded value doesn't change between the load and the its intended
978 // destination. The check for isInvariantLoad gives the targe the chance to
979 // classify the load as always returning a constant, e.g. a constant pool
980 // load.
Dan Gohmana70dca12009-10-09 23:27:56 +0000981 if (TID->mayLoad() && !isInvariantLoad(AA))
Evan Chengb27087f2008-03-13 00:44:09 +0000982 // Otherwise, this is a real load. If there is a store between the load and
Evan Cheng7cc2c402009-07-28 21:49:18 +0000983 // end of block, or if the load is volatile, we can't move it.
Dan Gohmand790a5c2008-10-02 15:04:30 +0000984 return !SawStore && !hasVolatileMemoryRef();
Dan Gohman3e4fb702008-09-24 00:06:15 +0000985
Evan Chengb27087f2008-03-13 00:44:09 +0000986 return true;
987}
988
Evan Chengdf3b9932008-08-27 20:33:50 +0000989/// isSafeToReMat - Return true if it's safe to rematerialize the specified
990/// instruction which defined the specified register instead of copying it.
Dan Gohmanb3b930a2008-11-18 19:04:29 +0000991bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII,
Dan Gohmana70dca12009-10-09 23:27:56 +0000992 unsigned DstReg,
993 AliasAnalysis *AA) const {
Evan Chengdf3b9932008-08-27 20:33:50 +0000994 bool SawStore = false;
Dan Gohmana70dca12009-10-09 23:27:56 +0000995 if (!TII->isTriviallyReMaterializable(this, AA) ||
996 !isSafeToMove(TII, SawStore, AA))
Evan Chengdf3b9932008-08-27 20:33:50 +0000997 return false;
998 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Dan Gohmancbad42c2008-11-18 19:49:32 +0000999 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001000 if (!MO.isReg())
Evan Chengdf3b9932008-08-27 20:33:50 +00001001 continue;
1002 // FIXME: For now, do not remat any instruction with register operands.
1003 // Later on, we can loosen the restriction is the register operands have
1004 // not been modified between the def and use. Note, this is different from
Evan Cheng8763c1c2008-08-27 20:58:54 +00001005 // MachineSink because the code is no longer in two-address form (at least
Evan Chengdf3b9932008-08-27 20:33:50 +00001006 // partially).
1007 if (MO.isUse())
1008 return false;
1009 else if (!MO.isDead() && MO.getReg() != DstReg)
1010 return false;
1011 }
1012 return true;
1013}
1014
Dan Gohman3e4fb702008-09-24 00:06:15 +00001015/// hasVolatileMemoryRef - Return true if this instruction may have a
1016/// volatile memory reference, or if the information describing the
1017/// memory reference is not available. Return false if it is known to
1018/// have no volatile memory references.
1019bool MachineInstr::hasVolatileMemoryRef() const {
1020 // An instruction known never to access memory won't have a volatile access.
1021 if (!TID->mayStore() &&
1022 !TID->mayLoad() &&
1023 !TID->isCall() &&
1024 !TID->hasUnmodeledSideEffects())
1025 return false;
1026
1027 // Otherwise, if the instruction has no memory reference information,
1028 // conservatively assume it wasn't preserved.
1029 if (memoperands_empty())
1030 return true;
1031
1032 // Check the memory reference information for volatile references.
Dan Gohmanc76909a2009-09-25 20:36:54 +00001033 for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I)
1034 if ((*I)->isVolatile())
Dan Gohman3e4fb702008-09-24 00:06:15 +00001035 return true;
1036
1037 return false;
1038}
1039
Dan Gohmane33f44c2009-10-07 17:38:06 +00001040/// isInvariantLoad - Return true if this instruction is loading from a
1041/// location whose value is invariant across the function. For example,
1042/// loading a value from the constant pool or from from the argument area
1043/// of a function if it does not change. This should only return true of
1044/// *all* loads the instruction does are invariant (if it does multiple loads).
1045bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const {
1046 // If the instruction doesn't load at all, it isn't an invariant load.
1047 if (!TID->mayLoad())
1048 return false;
1049
1050 // If the instruction has lost its memoperands, conservatively assume that
1051 // it may not be an invariant load.
1052 if (memoperands_empty())
1053 return false;
1054
1055 const MachineFrameInfo *MFI = getParent()->getParent()->getFrameInfo();
1056
1057 for (mmo_iterator I = memoperands_begin(),
1058 E = memoperands_end(); I != E; ++I) {
1059 if ((*I)->isVolatile()) return false;
1060 if ((*I)->isStore()) return false;
1061
1062 if (const Value *V = (*I)->getValue()) {
1063 // A load from a constant PseudoSourceValue is invariant.
1064 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V))
1065 if (PSV->isConstant(MFI))
1066 continue;
1067 // If we have an AliasAnalysis, ask it whether the memory is constant.
1068 if (AA && AA->pointsToConstantMemory(V))
1069 continue;
1070 }
1071
1072 // Otherwise assume conservatively.
1073 return false;
1074 }
1075
1076 // Everything checks out.
1077 return true;
1078}
1079
Evan Cheng229694f2009-12-03 02:31:43 +00001080/// isConstantValuePHI - If the specified instruction is a PHI that always
1081/// merges together the same virtual register, return the register, otherwise
1082/// return 0.
1083unsigned MachineInstr::isConstantValuePHI() const {
1084 if (getOpcode() != TargetInstrInfo::PHI)
1085 return 0;
Evan Chengd8f079c2009-12-07 23:10:34 +00001086 assert(getNumOperands() >= 3 &&
1087 "It's illegal to have a PHI without source operands");
Evan Cheng229694f2009-12-03 02:31:43 +00001088
1089 unsigned Reg = getOperand(1).getReg();
1090 for (unsigned i = 3, e = getNumOperands(); i < e; i += 2)
1091 if (getOperand(i).getReg() != Reg)
1092 return 0;
1093 return Reg;
1094}
1095
Brian Gaeke21326fc2004-02-13 04:39:32 +00001096void MachineInstr::dump() const {
Chris Lattner705e07f2009-08-23 03:41:05 +00001097 errs() << " " << *this;
Mon P Wang5ca6bd12008-10-10 01:43:55 +00001098}
1099
1100void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const {
Dan Gohman80f6c582009-11-09 19:38:45 +00001101 // We can be a bit tidier if we know the TargetMachine and/or MachineFunction.
1102 const MachineFunction *MF = 0;
1103 if (const MachineBasicBlock *MBB = getParent()) {
1104 MF = MBB->getParent();
1105 if (!TM && MF)
1106 TM = &MF->getTarget();
1107 }
Dan Gohman0ba90f32009-10-31 20:19:03 +00001108
1109 // Print explicitly defined operands on the left of an assignment syntax.
Dan Gohman80f6c582009-11-09 19:38:45 +00001110 unsigned StartOp = 0, e = getNumOperands();
Dan Gohman0ba90f32009-10-31 20:19:03 +00001111 for (; StartOp < e && getOperand(StartOp).isReg() &&
1112 getOperand(StartOp).isDef() &&
1113 !getOperand(StartOp).isImplicit();
1114 ++StartOp) {
1115 if (StartOp != 0) OS << ", ";
1116 getOperand(StartOp).print(OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +00001117 }
Tanya Lattnerb1407622004-06-25 00:13:11 +00001118
Dan Gohman0ba90f32009-10-31 20:19:03 +00001119 if (StartOp != 0)
1120 OS << " = ";
1121
1122 // Print the opcode name.
Chris Lattner749c6f62008-01-07 07:27:27 +00001123 OS << getDesc().getName();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001124
Dan Gohman0ba90f32009-10-31 20:19:03 +00001125 // Print the rest of the operands.
Dan Gohman80f6c582009-11-09 19:38:45 +00001126 bool OmittedAnyCallClobbers = false;
1127 bool FirstOp = true;
Chris Lattner6a592272002-10-30 01:55:38 +00001128 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohman80f6c582009-11-09 19:38:45 +00001129 const MachineOperand &MO = getOperand(i);
1130
1131 // Omit call-clobbered registers which aren't used anywhere. This makes
1132 // call instructions much less noisy on targets where calls clobber lots
1133 // of registers. Don't rely on MO.isDead() because we may be called before
1134 // LiveVariables is run, or we may be looking at a non-allocatable reg.
1135 if (MF && getDesc().isCall() &&
1136 MO.isReg() && MO.isImplicit() && MO.isDef()) {
1137 unsigned Reg = MO.getReg();
1138 if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) {
1139 const MachineRegisterInfo &MRI = MF->getRegInfo();
1140 if (MRI.use_empty(Reg) && !MRI.isLiveOut(Reg)) {
1141 bool HasAliasLive = false;
1142 for (const unsigned *Alias = TM->getRegisterInfo()->getAliasSet(Reg);
1143 unsigned AliasReg = *Alias; ++Alias)
1144 if (!MRI.use_empty(AliasReg) || MRI.isLiveOut(AliasReg)) {
1145 HasAliasLive = true;
1146 break;
1147 }
1148 if (!HasAliasLive) {
1149 OmittedAnyCallClobbers = true;
1150 continue;
1151 }
1152 }
1153 }
1154 }
1155
1156 if (FirstOp) FirstOp = false; else OS << ",";
Chris Lattner6a592272002-10-30 01:55:38 +00001157 OS << " ";
Dan Gohman80f6c582009-11-09 19:38:45 +00001158 MO.print(OS, TM);
1159 }
1160
1161 // Briefly indicate whether any call clobbers were omitted.
1162 if (OmittedAnyCallClobbers) {
Bill Wendling164558e2009-12-25 13:45:50 +00001163 if (!FirstOp) OS << ",";
Dan Gohman80f6c582009-11-09 19:38:45 +00001164 OS << " ...";
Chris Lattner10491642002-10-30 00:48:05 +00001165 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001166
Dan Gohman0ba90f32009-10-31 20:19:03 +00001167 bool HaveSemi = false;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001168 if (!memoperands_empty()) {
Dan Gohman0ba90f32009-10-31 20:19:03 +00001169 if (!HaveSemi) OS << ";"; HaveSemi = true;
1170
1171 OS << " mem:";
Dan Gohmanc76909a2009-09-25 20:36:54 +00001172 for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
1173 i != e; ++i) {
1174 OS << **i;
Dan Gohmancd26ec52009-09-23 01:33:16 +00001175 if (next(i) != e)
1176 OS << " ";
Dan Gohman69de1932008-02-06 22:27:42 +00001177 }
1178 }
1179
Dan Gohman80f6c582009-11-09 19:38:45 +00001180 if (!debugLoc.isUnknown() && MF) {
Bill Wendlingad2cf9d2009-12-25 13:44:36 +00001181 if (!HaveSemi) OS << ";";
Dan Gohman0ba90f32009-10-31 20:19:03 +00001182
1183 // TODO: print InlinedAtLoc information
1184
Bill Wendlingb5ef2732009-02-19 21:44:55 +00001185 DebugLocTuple DLT = MF->getDebugLocTuple(debugLoc);
Dan Gohman261a7d92009-12-01 00:45:56 +00001186 DIScope Scope(DLT.Scope);
Dan Gohman75ae5932009-11-23 21:29:08 +00001187 OS << " dbg:";
Dan Gohman4b808b02009-12-05 00:20:51 +00001188 // Omit the directory, since it's usually long and uninteresting.
Dan Gohman261a7d92009-12-01 00:45:56 +00001189 if (!Scope.isNull())
Dan Gohman4b808b02009-12-05 00:20:51 +00001190 OS << Scope.getFilename();
1191 else
1192 OS << "<unknown>";
1193 OS << ':' << DLT.Line;
1194 if (DLT.Col != 0)
1195 OS << ':' << DLT.Col;
Bill Wendlingb5ef2732009-02-19 21:44:55 +00001196 }
1197
Chris Lattner10491642002-10-30 00:48:05 +00001198 OS << "\n";
1199}
1200
Owen Andersonb487e722008-01-24 01:10:07 +00001201bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001202 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +00001203 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001204 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001205 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +00001206 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001207 SmallVector<unsigned,4> DeadOps;
Bill Wendling4a23d722008-03-03 22:14:33 +00001208 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1209 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesenefb8e3e2009-08-04 20:09:25 +00001210 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001211 continue;
1212 unsigned Reg = MO.getReg();
1213 if (!Reg)
1214 continue;
Bill Wendling4a23d722008-03-03 22:14:33 +00001215
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001216 if (Reg == IncomingReg) {
Dan Gohman3f629402008-09-03 15:56:16 +00001217 if (!Found) {
1218 if (MO.isKill())
1219 // The register is already marked kill.
1220 return true;
Jakob Stoklund Olesenece48182009-08-02 19:13:03 +00001221 if (isPhysReg && isRegTiedToDefOperand(i))
1222 // Two-address uses of physregs must not be marked kill.
1223 return true;
Dan Gohman3f629402008-09-03 15:56:16 +00001224 MO.setIsKill();
1225 Found = true;
1226 }
1227 } else if (hasAliases && MO.isKill() &&
1228 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001229 // A super-register kill already exists.
1230 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001231 return true;
1232 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001233 DeadOps.push_back(i);
Bill Wendling4a23d722008-03-03 22:14:33 +00001234 }
1235 }
1236
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001237 // Trim unneeded kill operands.
1238 while (!DeadOps.empty()) {
1239 unsigned OpIdx = DeadOps.back();
1240 if (getOperand(OpIdx).isImplicit())
1241 RemoveOperand(OpIdx);
1242 else
1243 getOperand(OpIdx).setIsKill(false);
1244 DeadOps.pop_back();
1245 }
1246
Bill Wendling4a23d722008-03-03 22:14:33 +00001247 // If not found, this means an alias of one of the operands is killed. Add a
Owen Andersonb487e722008-01-24 01:10:07 +00001248 // new implicit operand if required.
Dan Gohman3f629402008-09-03 15:56:16 +00001249 if (!Found && AddIfNotFound) {
Bill Wendling4a23d722008-03-03 22:14:33 +00001250 addOperand(MachineOperand::CreateReg(IncomingReg,
1251 false /*IsDef*/,
1252 true /*IsImp*/,
1253 true /*IsKill*/));
Owen Andersonb487e722008-01-24 01:10:07 +00001254 return true;
1255 }
Dan Gohman3f629402008-09-03 15:56:16 +00001256 return Found;
Owen Andersonb487e722008-01-24 01:10:07 +00001257}
1258
1259bool MachineInstr::addRegisterDead(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001260 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +00001261 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001262 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Evan Cheng01b2e232008-06-27 22:11:49 +00001263 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +00001264 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001265 SmallVector<unsigned,4> DeadOps;
Owen Andersonb487e722008-01-24 01:10:07 +00001266 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1267 MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001268 if (!MO.isReg() || !MO.isDef())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001269 continue;
1270 unsigned Reg = MO.getReg();
Dan Gohman3f629402008-09-03 15:56:16 +00001271 if (!Reg)
1272 continue;
1273
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001274 if (Reg == IncomingReg) {
Dan Gohman3f629402008-09-03 15:56:16 +00001275 if (!Found) {
1276 if (MO.isDead())
1277 // The register is already marked dead.
1278 return true;
1279 MO.setIsDead();
1280 Found = true;
1281 }
1282 } else if (hasAliases && MO.isDead() &&
1283 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001284 // There exists a super-register that's marked dead.
1285 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001286 return true;
Owen Anderson22ae9992008-08-14 18:34:18 +00001287 if (RegInfo->getSubRegisters(IncomingReg) &&
1288 RegInfo->getSuperRegisters(Reg) &&
1289 RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001290 DeadOps.push_back(i);
Owen Andersonb487e722008-01-24 01:10:07 +00001291 }
1292 }
1293
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001294 // Trim unneeded dead operands.
1295 while (!DeadOps.empty()) {
1296 unsigned OpIdx = DeadOps.back();
1297 if (getOperand(OpIdx).isImplicit())
1298 RemoveOperand(OpIdx);
1299 else
1300 getOperand(OpIdx).setIsDead(false);
1301 DeadOps.pop_back();
1302 }
1303
Dan Gohman3f629402008-09-03 15:56:16 +00001304 // If not found, this means an alias of one of the operands is dead. Add a
1305 // new implicit operand if required.
Chris Lattner31530612009-06-24 17:54:48 +00001306 if (Found || !AddIfNotFound)
1307 return Found;
1308
1309 addOperand(MachineOperand::CreateReg(IncomingReg,
1310 true /*IsDef*/,
1311 true /*IsImp*/,
1312 false /*IsKill*/,
1313 true /*IsDead*/));
1314 return true;
Owen Andersonb487e722008-01-24 01:10:07 +00001315}