blob: ade8683ec794f92b9481cf6640d6a23f9dc4e89b [file] [log] [blame]
Chris Lattnere138b3d2008-01-01 20:36:19 +00001//===-- lib/CodeGen/MachineInstr.cpp --------------------------------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Brian Gaeke21326fc2004-02-13 04:39:32 +00009//
10// Methods common to all machine instructions.
11//
Chris Lattner035dfbe2002-08-09 20:08:06 +000012//===----------------------------------------------------------------------===//
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000013
Chris Lattner822b4fb2001-09-07 17:18:30 +000014#include "llvm/CodeGen/MachineInstr.h"
Evan Chengfb112882009-03-23 08:01:15 +000015#include "llvm/Constants.h"
16#include "llvm/InlineAsm.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000017#include "llvm/Value.h"
Chris Lattner8517e1f2004-02-19 16:17:08 +000018#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner62ed6b92008-01-01 01:12:31 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000020#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner10491642002-10-30 00:48:05 +000021#include "llvm/Target/TargetMachine.h"
Evan Chengbb81d972008-01-31 09:59:15 +000022#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerf14cf852008-01-07 07:42:25 +000023#include "llvm/Target/TargetInstrDesc.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000024#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohman2c3f7ae2008-07-17 23:49:46 +000025#include "llvm/Support/LeakDetector.h"
Dan Gohmance42e402008-07-07 20:32:02 +000026#include "llvm/Support/MathExtras.h"
Bill Wendlinga09362e2006-11-28 22:48:48 +000027#include "llvm/Support/Streams.h"
Chris Lattneredfb72c2008-08-24 20:37:32 +000028#include "llvm/Support/raw_ostream.h"
Dan Gohmanb8d2f552008-08-20 15:58:01 +000029#include "llvm/ADT/FoldingSet.h"
Jeff Cohenc21c5ee2006-12-15 22:57:14 +000030#include <ostream>
Chris Lattner0742b592004-02-23 18:38:20 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattnerf7382302007-12-30 21:56:09 +000033//===----------------------------------------------------------------------===//
34// MachineOperand Implementation
35//===----------------------------------------------------------------------===//
36
Chris Lattner62ed6b92008-01-01 01:12:31 +000037/// AddRegOperandToRegInfo - Add this register operand to the specified
38/// MachineRegisterInfo. If it is null, then the next/prev fields should be
39/// explicitly nulled out.
40void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) {
Dan Gohmand735b802008-10-03 15:45:36 +000041 assert(isReg() && "Can only add reg operand to use lists");
Chris Lattner62ed6b92008-01-01 01:12:31 +000042
43 // If the reginfo pointer is null, just explicitly null out or next/prev
44 // pointers, to ensure they are not garbage.
45 if (RegInfo == 0) {
46 Contents.Reg.Prev = 0;
47 Contents.Reg.Next = 0;
48 return;
49 }
50
51 // Otherwise, add this operand to the head of the registers use/def list.
Chris Lattner80fe5312008-01-01 21:08:22 +000052 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg());
Chris Lattner62ed6b92008-01-01 01:12:31 +000053
Chris Lattner80fe5312008-01-01 21:08:22 +000054 // For SSA values, we prefer to keep the definition at the start of the list.
55 // we do this by skipping over the definition if it is at the head of the
56 // list.
57 if (*Head && (*Head)->isDef())
58 Head = &(*Head)->Contents.Reg.Next;
59
60 Contents.Reg.Next = *Head;
Chris Lattner62ed6b92008-01-01 01:12:31 +000061 if (Contents.Reg.Next) {
62 assert(getReg() == Contents.Reg.Next->getReg() &&
63 "Different regs on the same list!");
64 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next;
65 }
66
Chris Lattner80fe5312008-01-01 21:08:22 +000067 Contents.Reg.Prev = Head;
68 *Head = this;
Chris Lattner62ed6b92008-01-01 01:12:31 +000069}
70
71void MachineOperand::setReg(unsigned Reg) {
72 if (getReg() == Reg) return; // No change.
73
74 // Otherwise, we have to change the register. If this operand is embedded
75 // into a machine function, we need to update the old and new register's
76 // use/def lists.
77 if (MachineInstr *MI = getParent())
78 if (MachineBasicBlock *MBB = MI->getParent())
79 if (MachineFunction *MF = MBB->getParent()) {
80 RemoveRegOperandFromRegInfo();
81 Contents.Reg.RegNo = Reg;
82 AddRegOperandToRegInfo(&MF->getRegInfo());
83 return;
84 }
85
86 // Otherwise, just change the register, no problem. :)
87 Contents.Reg.RegNo = Reg;
88}
89
90/// ChangeToImmediate - Replace this operand with a new immediate operand of
91/// the specified value. If an operand is known to be an immediate already,
92/// the setImm method should be used.
93void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
94 // If this operand is currently a register operand, and if this is in a
95 // function, deregister the operand from the register's use/def list.
Dan Gohmand735b802008-10-03 15:45:36 +000096 if (isReg() && getParent() && getParent()->getParent() &&
Chris Lattner62ed6b92008-01-01 01:12:31 +000097 getParent()->getParent()->getParent())
98 RemoveRegOperandFromRegInfo();
99
100 OpKind = MO_Immediate;
101 Contents.ImmVal = ImmVal;
102}
103
104/// ChangeToRegister - Replace this operand with a new register operand of
105/// the specified value. If an operand is known to be an register already,
106/// the setReg method should be used.
107void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
Dale Johannesene0091802008-09-14 01:44:36 +0000108 bool isKill, bool isDead) {
Chris Lattner62ed6b92008-01-01 01:12:31 +0000109 // If this operand is already a register operand, use setReg to update the
110 // register's use/def lists.
Dan Gohmand735b802008-10-03 15:45:36 +0000111 if (isReg()) {
Dale Johannesene0091802008-09-14 01:44:36 +0000112 assert(!isEarlyClobber());
Chris Lattner62ed6b92008-01-01 01:12:31 +0000113 setReg(Reg);
114 } else {
115 // Otherwise, change this to a register and set the reg#.
116 OpKind = MO_Register;
117 Contents.Reg.RegNo = Reg;
118
119 // If this operand is embedded in a function, add the operand to the
120 // register's use/def list.
121 if (MachineInstr *MI = getParent())
122 if (MachineBasicBlock *MBB = MI->getParent())
123 if (MachineFunction *MF = MBB->getParent())
124 AddRegOperandToRegInfo(&MF->getRegInfo());
125 }
126
127 IsDef = isDef;
128 IsImp = isImp;
129 IsKill = isKill;
130 IsDead = isDead;
Dale Johannesene0091802008-09-14 01:44:36 +0000131 IsEarlyClobber = false;
Chris Lattner62ed6b92008-01-01 01:12:31 +0000132 SubReg = 0;
133}
134
Chris Lattnerf7382302007-12-30 21:56:09 +0000135/// isIdenticalTo - Return true if this operand is identical to the specified
136/// operand.
137bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
138 if (getType() != Other.getType()) return false;
139
140 switch (getType()) {
141 default: assert(0 && "Unrecognized operand type");
142 case MachineOperand::MO_Register:
143 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
144 getSubReg() == Other.getSubReg();
145 case MachineOperand::MO_Immediate:
146 return getImm() == Other.getImm();
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000147 case MachineOperand::MO_FPImmediate:
148 return getFPImm() == Other.getFPImm();
Chris Lattnerf7382302007-12-30 21:56:09 +0000149 case MachineOperand::MO_MachineBasicBlock:
150 return getMBB() == Other.getMBB();
151 case MachineOperand::MO_FrameIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000152 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000153 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000154 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattnerf7382302007-12-30 21:56:09 +0000155 case MachineOperand::MO_JumpTableIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000156 return getIndex() == Other.getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000157 case MachineOperand::MO_GlobalAddress:
158 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
159 case MachineOperand::MO_ExternalSymbol:
160 return !strcmp(getSymbolName(), Other.getSymbolName()) &&
161 getOffset() == Other.getOffset();
162 }
163}
164
165/// print - Print the specified machine operand.
166///
167void MachineOperand::print(std::ostream &OS, const TargetMachine *TM) const {
Mon P Wang5ca6bd12008-10-10 01:43:55 +0000168 raw_os_ostream RawOS(OS);
169 print(RawOS, TM);
170}
171
172void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const {
Chris Lattnerf7382302007-12-30 21:56:09 +0000173 switch (getType()) {
174 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000175 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000176 OS << "%reg" << getReg();
177 } else {
178 // If the instruction is embedded into a basic block, we can find the
Chris Lattner62ed6b92008-01-01 01:12:31 +0000179 // target info for the instruction.
Chris Lattnerf7382302007-12-30 21:56:09 +0000180 if (TM == 0)
181 if (const MachineInstr *MI = getParent())
182 if (const MachineBasicBlock *MBB = MI->getParent())
183 if (const MachineFunction *MF = MBB->getParent())
184 TM = &MF->getTarget();
185
186 if (TM)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000187 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
Chris Lattnerf7382302007-12-30 21:56:09 +0000188 else
189 OS << "%mreg" << getReg();
190 }
Dan Gohman2ccc8392008-12-18 21:51:27 +0000191
192 if (getSubReg() != 0) {
193 OS << ":" << getSubReg();
194 }
195
Dale Johannesen86b49f82008-09-24 01:07:17 +0000196 if (isDef() || isKill() || isDead() || isImplicit() || isEarlyClobber()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000197 OS << "<";
198 bool NeedComma = false;
199 if (isImplicit()) {
Dale Johannesen91aac102008-09-17 21:13:11 +0000200 if (NeedComma) OS << ",";
Chris Lattnerf7382302007-12-30 21:56:09 +0000201 OS << (isDef() ? "imp-def" : "imp-use");
202 NeedComma = true;
203 } else if (isDef()) {
Dale Johannesen91aac102008-09-17 21:13:11 +0000204 if (NeedComma) OS << ",";
Dale Johannesen913d3df2008-09-12 17:49:03 +0000205 if (isEarlyClobber())
206 OS << "earlyclobber,";
Chris Lattnerf7382302007-12-30 21:56:09 +0000207 OS << "def";
208 NeedComma = true;
209 }
210 if (isKill() || isDead()) {
Bill Wendling181eb732008-02-24 00:56:13 +0000211 if (NeedComma) OS << ",";
212 if (isKill()) OS << "kill";
213 if (isDead()) OS << "dead";
Chris Lattnerf7382302007-12-30 21:56:09 +0000214 }
215 OS << ">";
216 }
217 break;
218 case MachineOperand::MO_Immediate:
219 OS << getImm();
220 break;
Nate Begemane8b7ccf2008-02-14 07:39:30 +0000221 case MachineOperand::MO_FPImmediate:
222 if (getFPImm()->getType() == Type::FloatTy) {
223 OS << getFPImm()->getValueAPF().convertToFloat();
224 } else {
225 OS << getFPImm()->getValueAPF().convertToDouble();
226 }
227 break;
Chris Lattnerf7382302007-12-30 21:56:09 +0000228 case MachineOperand::MO_MachineBasicBlock:
229 OS << "mbb<"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000230 << ((Value*)getMBB()->getBasicBlock())->getName()
231 << "," << (void*)getMBB() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000232 break;
233 case MachineOperand::MO_FrameIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000234 OS << "<fi#" << getIndex() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000235 break;
236 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000237 OS << "<cp#" << getIndex();
Chris Lattnerf7382302007-12-30 21:56:09 +0000238 if (getOffset()) OS << "+" << getOffset();
239 OS << ">";
240 break;
241 case MachineOperand::MO_JumpTableIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000242 OS << "<jt#" << getIndex() << ">";
Chris Lattnerf7382302007-12-30 21:56:09 +0000243 break;
244 case MachineOperand::MO_GlobalAddress:
245 OS << "<ga:" << ((Value*)getGlobal())->getName();
246 if (getOffset()) OS << "+" << getOffset();
247 OS << ">";
248 break;
249 case MachineOperand::MO_ExternalSymbol:
250 OS << "<es:" << getSymbolName();
251 if (getOffset()) OS << "+" << getOffset();
252 OS << ">";
253 break;
254 default:
255 assert(0 && "Unrecognized operand type");
256 }
257}
258
259//===----------------------------------------------------------------------===//
Dan Gohmance42e402008-07-07 20:32:02 +0000260// MachineMemOperand Implementation
261//===----------------------------------------------------------------------===//
262
263MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f,
264 int64_t o, uint64_t s, unsigned int a)
265 : Offset(o), Size(s), V(v),
266 Flags((f & 7) | ((Log2_32(a) + 1) << 3)) {
Dan Gohmanf1bf29e2008-07-08 23:47:04 +0000267 assert(isPowerOf2_32(a) && "Alignment is not a power of 2!");
Dan Gohmanc5e1f982008-07-16 15:56:42 +0000268 assert((isLoad() || isStore()) && "Not a load/store!");
Dan Gohmance42e402008-07-07 20:32:02 +0000269}
270
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000271/// Profile - Gather unique data for the object.
272///
273void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
274 ID.AddInteger(Offset);
275 ID.AddInteger(Size);
276 ID.AddPointer(V);
277 ID.AddInteger(Flags);
278}
279
Dan Gohmance42e402008-07-07 20:32:02 +0000280//===----------------------------------------------------------------------===//
Chris Lattnerf7382302007-12-30 21:56:09 +0000281// MachineInstr Implementation
282//===----------------------------------------------------------------------===//
283
Evan Chengc0f64ff2006-11-27 23:37:22 +0000284/// MachineInstr ctor - This constructor creates a dummy MachineInstr with
Evan Cheng67f660c2006-11-30 07:08:44 +0000285/// TID NULL and no operands.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000286MachineInstr::MachineInstr()
Dale Johannesen06efc022009-01-27 23:20:29 +0000287 : TID(0), NumImplicitOps(0), Parent(0), debugLoc(DebugLoc::getUnknownLoc()) {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000288 // Make sure that we get added to a machine basicblock
289 LeakDetector::addGarbageObject(this);
Chris Lattner72791222002-10-28 20:59:49 +0000290}
291
Evan Cheng67f660c2006-11-30 07:08:44 +0000292void MachineInstr::addImplicitDefUseOperands() {
293 if (TID->ImplicitDefs)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000294 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
Chris Lattner8019f412007-12-30 00:41:17 +0000295 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng67f660c2006-11-30 07:08:44 +0000296 if (TID->ImplicitUses)
Chris Lattnera4161ee2007-12-30 00:12:25 +0000297 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
Chris Lattner8019f412007-12-30 00:41:17 +0000298 addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
Evan Chengd7de4962006-11-13 23:34:06 +0000299}
300
301/// MachineInstr ctor - This constructor create a MachineInstr and add the
Evan Chengc0f64ff2006-11-27 23:37:22 +0000302/// implicit operands. It reserves space for number of operands specified by
Chris Lattner749c6f62008-01-07 07:27:27 +0000303/// TargetInstrDesc or the numOperands if it is not zero. (for
Evan Chengc0f64ff2006-11-27 23:37:22 +0000304/// instructions with variable number of operands).
Chris Lattner749c6f62008-01-07 07:27:27 +0000305MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp)
Dale Johannesen06efc022009-01-27 23:20:29 +0000306 : TID(&tid), NumImplicitOps(0), Parent(0),
307 debugLoc(DebugLoc::getUnknownLoc()) {
Chris Lattner349c4952008-01-07 03:13:06 +0000308 if (!NoImp && TID->getImplicitDefs())
309 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000310 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000311 if (!NoImp && TID->getImplicitUses())
312 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000313 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000314 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Chengfa945722007-10-13 02:23:01 +0000315 if (!NoImp)
316 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000317 // Make sure that we get added to a machine basicblock
318 LeakDetector::addGarbageObject(this);
Evan Chengd7de4962006-11-13 23:34:06 +0000319}
320
Dale Johannesen06efc022009-01-27 23:20:29 +0000321/// MachineInstr ctor - As above, but with a DebugLoc.
322MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl,
323 bool NoImp)
324 : TID(&tid), NumImplicitOps(0), Parent(0), debugLoc(dl) {
325 if (!NoImp && TID->getImplicitDefs())
326 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
327 NumImplicitOps++;
328 if (!NoImp && TID->getImplicitUses())
329 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
330 NumImplicitOps++;
331 Operands.reserve(NumImplicitOps + TID->getNumOperands());
332 if (!NoImp)
333 addImplicitDefUseOperands();
334 // Make sure that we get added to a machine basicblock
335 LeakDetector::addGarbageObject(this);
336}
337
338/// MachineInstr ctor - Work exactly the same as the ctor two above, except
339/// that the MachineInstr is created and added to the end of the specified
340/// basic block.
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000341///
Dale Johannesen06efc022009-01-27 23:20:29 +0000342MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid)
343 : TID(&tid), NumImplicitOps(0), Parent(0),
344 debugLoc(DebugLoc::getUnknownLoc()) {
345 assert(MBB && "Cannot use inserting ctor with null basic block!");
346 if (TID->ImplicitDefs)
347 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
348 NumImplicitOps++;
349 if (TID->ImplicitUses)
350 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
351 NumImplicitOps++;
352 Operands.reserve(NumImplicitOps + TID->getNumOperands());
353 addImplicitDefUseOperands();
354 // Make sure that we get added to a machine basicblock
355 LeakDetector::addGarbageObject(this);
356 MBB->push_back(this); // Add instruction to end of basic block!
357}
358
359/// MachineInstr ctor - As above, but with a DebugLoc.
360///
361MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl,
Chris Lattner749c6f62008-01-07 07:27:27 +0000362 const TargetInstrDesc &tid)
Dale Johannesen06efc022009-01-27 23:20:29 +0000363 : TID(&tid), NumImplicitOps(0), Parent(0), debugLoc(dl) {
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000364 assert(MBB && "Cannot use inserting ctor with null basic block!");
Evan Cheng67f660c2006-11-30 07:08:44 +0000365 if (TID->ImplicitDefs)
Chris Lattner349c4952008-01-07 03:13:06 +0000366 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
Evan Chengd7de4962006-11-13 23:34:06 +0000367 NumImplicitOps++;
Evan Cheng67f660c2006-11-30 07:08:44 +0000368 if (TID->ImplicitUses)
Chris Lattner349c4952008-01-07 03:13:06 +0000369 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
Evan Chengd7de4962006-11-13 23:34:06 +0000370 NumImplicitOps++;
Chris Lattner349c4952008-01-07 03:13:06 +0000371 Operands.reserve(NumImplicitOps + TID->getNumOperands());
Evan Cheng67f660c2006-11-30 07:08:44 +0000372 addImplicitDefUseOperands();
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000373 // Make sure that we get added to a machine basicblock
374 LeakDetector::addGarbageObject(this);
Chris Lattnerddd7fcb2002-10-29 23:19:00 +0000375 MBB->push_back(this); // Add instruction to end of basic block!
376}
377
Misha Brukmance22e762004-07-09 14:45:17 +0000378/// MachineInstr ctor - Copies MachineInstr arg exactly
379///
Evan Cheng1ed99222008-07-19 00:37:25 +0000380MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Dale Johannesen06efc022009-01-27 23:20:29 +0000381 : TID(&MI.getDesc()), NumImplicitOps(0), Parent(0),
382 debugLoc(MI.getDebugLoc()) {
Chris Lattner943b5e12006-05-04 19:14:44 +0000383 Operands.reserve(MI.getNumOperands());
Tanya Lattnerb5159ed2004-05-23 20:58:02 +0000384
Misha Brukmance22e762004-07-09 14:45:17 +0000385 // Add operands
Evan Cheng1ed99222008-07-19 00:37:25 +0000386 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
387 addOperand(MI.getOperand(i));
388 NumImplicitOps = MI.NumImplicitOps;
Tanya Lattner0c63e032004-05-24 03:14:18 +0000389
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000390 // Add memory operands.
Dan Gohmanfed90b62008-07-28 21:51:04 +0000391 for (std::list<MachineMemOperand>::const_iterator i = MI.memoperands_begin(),
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000392 j = MI.memoperands_end(); i != j; ++i)
393 addMemOperand(MF, *i);
394
395 // Set parent to null.
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000396 Parent = 0;
Dan Gohman6116a732008-07-21 18:47:29 +0000397
398 LeakDetector::addGarbageObject(this);
Tanya Lattner466b5342004-05-23 19:35:12 +0000399}
400
Misha Brukmance22e762004-07-09 14:45:17 +0000401MachineInstr::~MachineInstr() {
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000402 LeakDetector::removeGarbageObject(this);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000403 assert(MemOperands.empty() &&
404 "MachineInstr being deleted with live memoperands!");
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000405#ifndef NDEBUG
Chris Lattner62ed6b92008-01-01 01:12:31 +0000406 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000407 assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
Dan Gohmand735b802008-10-03 15:45:36 +0000408 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
Chris Lattner62ed6b92008-01-01 01:12:31 +0000409 "Reg operand def/use list corrupted");
410 }
Chris Lattnere12d6ab2007-12-30 06:11:04 +0000411#endif
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000412}
413
Chris Lattner62ed6b92008-01-01 01:12:31 +0000414/// getRegInfo - If this instruction is embedded into a MachineFunction,
415/// return the MachineRegisterInfo object for the current function, otherwise
416/// return null.
417MachineRegisterInfo *MachineInstr::getRegInfo() {
418 if (MachineBasicBlock *MBB = getParent())
Dan Gohman4e526b92008-07-08 23:59:09 +0000419 return &MBB->getParent()->getRegInfo();
Chris Lattner62ed6b92008-01-01 01:12:31 +0000420 return 0;
421}
422
423/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
424/// this instruction from their respective use lists. This requires that the
425/// operands already be on their use lists.
426void MachineInstr::RemoveRegOperandsFromUseLists() {
427 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000428 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000429 Operands[i].RemoveRegOperandFromRegInfo();
430 }
431}
432
433/// AddRegOperandsToUseLists - Add all of the register operands in
434/// this instruction from their respective use lists. This requires that the
435/// operands not be on their use lists yet.
436void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
437 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000438 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000439 Operands[i].AddRegOperandToRegInfo(&RegInfo);
440 }
441}
442
443
444/// addOperand - Add the specified operand to the instruction. If it is an
445/// implicit operand, it is added to the end of the operand list. If it is
446/// an explicit operand it is added at the end of the explicit operand list
447/// (before the first implicit operand).
448void MachineInstr::addOperand(const MachineOperand &Op) {
Dan Gohmand735b802008-10-03 15:45:36 +0000449 bool isImpReg = Op.isReg() && Op.isImplicit();
Chris Lattner62ed6b92008-01-01 01:12:31 +0000450 assert((isImpReg || !OperandsComplete()) &&
451 "Trying to add an operand to a machine instr that is already done!");
452
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000453 MachineRegisterInfo *RegInfo = getRegInfo();
454
Chris Lattner62ed6b92008-01-01 01:12:31 +0000455 // If we are adding the operand to the end of the list, our job is simpler.
456 // This is true most of the time, so this is a reasonable optimization.
457 if (isImpReg || NumImplicitOps == 0) {
458 // We can only do this optimization if we know that the operand list won't
459 // reallocate.
460 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
461 Operands.push_back(Op);
462
463 // Set the parent of the operand.
464 Operands.back().ParentMI = this;
465
466 // If the operand is a register, update the operand's use list.
Dan Gohmand735b802008-10-03 15:45:36 +0000467 if (Op.isReg())
Dan Gohmanbcf28c02008-12-09 22:45:08 +0000468 Operands.back().AddRegOperandToRegInfo(RegInfo);
Chris Lattner62ed6b92008-01-01 01:12:31 +0000469 return;
470 }
471 }
472
473 // Otherwise, we have to insert a real operand before any implicit ones.
474 unsigned OpNo = Operands.size()-NumImplicitOps;
475
Chris Lattner62ed6b92008-01-01 01:12:31 +0000476 // If this instruction isn't embedded into a function, then we don't need to
477 // update any operand lists.
478 if (RegInfo == 0) {
479 // Simple insertion, no reginfo update needed for other register operands.
480 Operands.insert(Operands.begin()+OpNo, Op);
481 Operands[OpNo].ParentMI = this;
482
483 // Do explicitly set the reginfo for this operand though, to ensure the
484 // next/prev fields are properly nulled out.
Dan Gohmand735b802008-10-03 15:45:36 +0000485 if (Operands[OpNo].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000486 Operands[OpNo].AddRegOperandToRegInfo(0);
487
488 } else if (Operands.size()+1 <= Operands.capacity()) {
489 // Otherwise, we have to remove register operands from their register use
490 // list, add the operand, then add the register operands back to their use
491 // list. This also must handle the case when the operand list reallocates
492 // to somewhere else.
493
494 // If insertion of this operand won't cause reallocation of the operand
495 // list, just remove the implicit operands, add the operand, then re-add all
496 // the rest of the operands.
497 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000498 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000499 Operands[i].RemoveRegOperandFromRegInfo();
500 }
501
502 // Add the operand. If it is a register, add it to the reg list.
503 Operands.insert(Operands.begin()+OpNo, Op);
504 Operands[OpNo].ParentMI = this;
505
Dan Gohmand735b802008-10-03 15:45:36 +0000506 if (Operands[OpNo].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000507 Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
508
509 // Re-add all the implicit ops.
510 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000511 assert(Operands[i].isReg() && "Should only be an implicit reg!");
Chris Lattner62ed6b92008-01-01 01:12:31 +0000512 Operands[i].AddRegOperandToRegInfo(RegInfo);
513 }
514 } else {
515 // Otherwise, we will be reallocating the operand list. Remove all reg
516 // operands from their list, then readd them after the operand list is
517 // reallocated.
518 RemoveRegOperandsFromUseLists();
519
520 Operands.insert(Operands.begin()+OpNo, Op);
521 Operands[OpNo].ParentMI = this;
522
523 // Re-add all the operands.
524 AddRegOperandsToUseLists(*RegInfo);
525 }
526}
527
528/// RemoveOperand - Erase an operand from an instruction, leaving it with one
529/// fewer operand than it started with.
530///
531void MachineInstr::RemoveOperand(unsigned OpNo) {
532 assert(OpNo < Operands.size() && "Invalid operand number");
533
534 // Special case removing the last one.
535 if (OpNo == Operands.size()-1) {
536 // If needed, remove from the reg def/use list.
Dan Gohmand735b802008-10-03 15:45:36 +0000537 if (Operands.back().isReg() && Operands.back().isOnRegUseList())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000538 Operands.back().RemoveRegOperandFromRegInfo();
539
540 Operands.pop_back();
541 return;
542 }
543
544 // Otherwise, we are removing an interior operand. If we have reginfo to
545 // update, remove all operands that will be shifted down from their reg lists,
546 // move everything down, then re-add them.
547 MachineRegisterInfo *RegInfo = getRegInfo();
548 if (RegInfo) {
549 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000550 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000551 Operands[i].RemoveRegOperandFromRegInfo();
552 }
553 }
554
555 Operands.erase(Operands.begin()+OpNo);
556
557 if (RegInfo) {
558 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000559 if (Operands[i].isReg())
Chris Lattner62ed6b92008-01-01 01:12:31 +0000560 Operands[i].AddRegOperandToRegInfo(RegInfo);
561 }
562 }
563}
564
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000565/// addMemOperand - Add a MachineMemOperand to the machine instruction,
566/// referencing arbitrary storage.
567void MachineInstr::addMemOperand(MachineFunction &MF,
568 const MachineMemOperand &MO) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000569 MemOperands.push_back(MO);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000570}
571
572/// clearMemOperands - Erase all of this MachineInstr's MachineMemOperands.
573void MachineInstr::clearMemOperands(MachineFunction &MF) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000574 MemOperands.clear();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000575}
576
Chris Lattner62ed6b92008-01-01 01:12:31 +0000577
Chris Lattner48d7c062006-04-17 21:35:41 +0000578/// removeFromParent - This method unlinks 'this' from the containing basic
579/// block, and returns it, but does not delete it.
580MachineInstr *MachineInstr::removeFromParent() {
581 assert(getParent() && "Not embedded in a basic block!");
582 getParent()->remove(this);
583 return this;
584}
585
586
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000587/// eraseFromParent - This method unlinks 'this' from the containing basic
588/// block, and deletes it.
589void MachineInstr::eraseFromParent() {
590 assert(getParent() && "Not embedded in a basic block!");
591 getParent()->erase(this);
592}
593
594
Brian Gaeke21326fc2004-02-13 04:39:32 +0000595/// OperandComplete - Return true if it's illegal to add a new operand
596///
Chris Lattner2a90ba62004-02-12 16:09:53 +0000597bool MachineInstr::OperandsComplete() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000598 unsigned short NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000599 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands)
Vikram S. Adve34977822003-05-31 07:39:06 +0000600 return true; // Broken: we have all the operands of this instruction!
Chris Lattner413746e2002-10-28 20:48:39 +0000601 return false;
602}
603
Evan Cheng19e3f312007-05-15 01:26:09 +0000604/// getNumExplicitOperands - Returns the number of non-implicit operands.
605///
606unsigned MachineInstr::getNumExplicitOperands() const {
Chris Lattner349c4952008-01-07 03:13:06 +0000607 unsigned NumOperands = TID->getNumOperands();
Chris Lattner8f707e12008-01-07 05:19:29 +0000608 if (!TID->isVariadic())
Evan Cheng19e3f312007-05-15 01:26:09 +0000609 return NumOperands;
610
611 for (unsigned e = getNumOperands(); NumOperands != e; ++NumOperands) {
612 const MachineOperand &MO = getOperand(NumOperands);
Dan Gohmand735b802008-10-03 15:45:36 +0000613 if (!MO.isReg() || !MO.isImplicit())
Evan Cheng19e3f312007-05-15 01:26:09 +0000614 NumOperands++;
615 }
616 return NumOperands;
617}
618
Chris Lattner8ace2cd2006-10-20 22:39:59 +0000619
Dan Gohman44066042008-07-01 00:05:16 +0000620/// isLabel - Returns true if the MachineInstr represents a label.
621///
622bool MachineInstr::isLabel() const {
623 return getOpcode() == TargetInstrInfo::DBG_LABEL ||
624 getOpcode() == TargetInstrInfo::EH_LABEL ||
625 getOpcode() == TargetInstrInfo::GC_LABEL;
626}
627
Evan Chengbb81d972008-01-31 09:59:15 +0000628/// isDebugLabel - Returns true if the MachineInstr represents a debug label.
629///
630bool MachineInstr::isDebugLabel() const {
Dan Gohman44066042008-07-01 00:05:16 +0000631 return getOpcode() == TargetInstrInfo::DBG_LABEL;
Evan Chengbb81d972008-01-31 09:59:15 +0000632}
633
Evan Chengfaa51072007-04-26 19:00:32 +0000634/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Evan Cheng32eb1f12007-03-26 22:37:45 +0000635/// the specific register or -1 if it is not found. It further tightening
Evan Cheng76d7e762007-02-23 01:04:26 +0000636/// the search criteria to a use that kills the register if isKill is true.
Evan Cheng6130f662008-03-05 00:59:57 +0000637int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
638 const TargetRegisterInfo *TRI) const {
Evan Cheng576d1232006-12-06 08:27:42 +0000639 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Chengf277ee42007-05-29 18:35:22 +0000640 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000641 if (!MO.isReg() || !MO.isUse())
Evan Cheng6130f662008-03-05 00:59:57 +0000642 continue;
643 unsigned MOReg = MO.getReg();
644 if (!MOReg)
645 continue;
646 if (MOReg == Reg ||
647 (TRI &&
648 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
649 TargetRegisterInfo::isPhysicalRegister(Reg) &&
650 TRI->isSubRegister(MOReg, Reg)))
Evan Cheng76d7e762007-02-23 01:04:26 +0000651 if (!isKill || MO.isKill())
Evan Cheng32eb1f12007-03-26 22:37:45 +0000652 return i;
Evan Cheng576d1232006-12-06 08:27:42 +0000653 }
Evan Cheng32eb1f12007-03-26 22:37:45 +0000654 return -1;
Evan Cheng576d1232006-12-06 08:27:42 +0000655}
656
Evan Cheng6130f662008-03-05 00:59:57 +0000657/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman703bfe62008-05-06 00:20:10 +0000658/// the specified register or -1 if it is not found. If isDead is true, defs
659/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
660/// also checks if there is a def of a super-register.
Evan Cheng6130f662008-03-05 00:59:57 +0000661int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead,
662 const TargetRegisterInfo *TRI) const {
Evan Chengb371f452007-02-19 21:49:54 +0000663 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng6130f662008-03-05 00:59:57 +0000664 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000665 if (!MO.isReg() || !MO.isDef())
Evan Cheng6130f662008-03-05 00:59:57 +0000666 continue;
667 unsigned MOReg = MO.getReg();
668 if (MOReg == Reg ||
669 (TRI &&
670 TargetRegisterInfo::isPhysicalRegister(MOReg) &&
671 TargetRegisterInfo::isPhysicalRegister(Reg) &&
672 TRI->isSubRegister(MOReg, Reg)))
673 if (!isDead || MO.isDead())
674 return i;
Evan Chengb371f452007-02-19 21:49:54 +0000675 }
Evan Cheng6130f662008-03-05 00:59:57 +0000676 return -1;
Evan Chengb371f452007-02-19 21:49:54 +0000677}
Evan Cheng19e3f312007-05-15 01:26:09 +0000678
Evan Chengf277ee42007-05-29 18:35:22 +0000679/// findFirstPredOperandIdx() - Find the index of the first operand in the
680/// operand list that is used to represent the predicate. It returns -1 if
681/// none is found.
682int MachineInstr::findFirstPredOperandIdx() const {
Chris Lattner749c6f62008-01-07 07:27:27 +0000683 const TargetInstrDesc &TID = getDesc();
684 if (TID.isPredicable()) {
Evan Cheng19e3f312007-05-15 01:26:09 +0000685 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Chris Lattner749c6f62008-01-07 07:27:27 +0000686 if (TID.OpInfo[i].isPredicate())
Evan Chengf277ee42007-05-29 18:35:22 +0000687 return i;
Evan Cheng19e3f312007-05-15 01:26:09 +0000688 }
689
Evan Chengf277ee42007-05-29 18:35:22 +0000690 return -1;
Evan Cheng19e3f312007-05-15 01:26:09 +0000691}
Evan Chengb371f452007-02-19 21:49:54 +0000692
Evan Chenga24752f2009-03-19 20:30:06 +0000693/// isRegReDefinedByTwoAddr - Given the index of a register operand,
Evan Chengef0732d2008-07-10 07:35:43 +0000694/// check if the register def is a re-definition due to two addr elimination.
Dan Gohman2ce7f202008-12-05 05:45:42 +0000695bool MachineInstr::isRegReDefinedByTwoAddr(unsigned DefIdx) const{
Evan Chengfb112882009-03-23 08:01:15 +0000696 if (getOpcode() == TargetInstrInfo::INLINEASM) {
697 assert(DefIdx >= 2);
698 const MachineOperand &MO = getOperand(DefIdx);
699 if (!MO.isReg() || !MO.isDef())
700 return false;
701 // Determine the actual operand no corresponding to this index.
702 unsigned DefNo = 0;
703 for (unsigned i = 1, e = getNumOperands(); i < e; ) {
704 const MachineOperand &FMO = getOperand(i);
705 assert(FMO.isImm());
706 // Skip over this def.
707 i += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
708 if (i > DefIdx)
709 break;
710 ++DefNo;
711 }
712 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
713 const MachineOperand &FMO = getOperand(i);
714 if (!FMO.isImm())
715 continue;
716 if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse())
717 continue;
718 unsigned Idx;
719 if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) &&
720 Idx == DefNo)
721 return true;
722 }
723 }
724
Dan Gohman2ce7f202008-12-05 05:45:42 +0000725 assert(getOperand(DefIdx).isDef() && "DefIdx is not a def!");
Chris Lattner749c6f62008-01-07 07:27:27 +0000726 const TargetInstrDesc &TID = getDesc();
Evan Chengef0732d2008-07-10 07:35:43 +0000727 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
728 const MachineOperand &MO = getOperand(i);
Dan Gohman2ce7f202008-12-05 05:45:42 +0000729 if (MO.isReg() && MO.isUse() &&
Evan Chengef0732d2008-07-10 07:35:43 +0000730 TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefIdx)
731 return true;
Evan Cheng32dfbea2007-10-12 08:50:34 +0000732 }
733 return false;
734}
735
Evan Chenga24752f2009-03-19 20:30:06 +0000736/// isRegTiedToDefOperand - Return true if the operand of the specified index
737/// is a register use and it is tied to an def operand. It also returns the def
738/// operand index by reference.
739bool MachineInstr::isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx){
Evan Chengfb112882009-03-23 08:01:15 +0000740 if (getOpcode() == TargetInstrInfo::INLINEASM) {
741 const MachineOperand &MO = getOperand(UseOpIdx);
Chris Lattner0c8382c2009-04-09 16:50:43 +0000742 if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0)
Evan Chengfb112882009-03-23 08:01:15 +0000743 return false;
744 assert(UseOpIdx > 0);
745 const MachineOperand &UFMO = getOperand(UseOpIdx-1);
746 if (!UFMO.isImm())
747 return false; // Must be physreg uses.
748 unsigned DefNo;
749 if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) {
750 if (!DefOpIdx)
751 return true;
752
753 unsigned DefIdx = 1;
754 // Remember to adjust the index. First operand is asm string, then there
755 // is a flag for each.
756 while (DefNo) {
757 const MachineOperand &FMO = getOperand(DefIdx);
758 assert(FMO.isImm());
759 // Skip over this def.
760 DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
761 --DefNo;
762 }
763 *DefOpIdx = DefIdx+1;
764 return true;
765 }
766 return false;
767 }
768
Evan Chenga24752f2009-03-19 20:30:06 +0000769 const TargetInstrDesc &TID = getDesc();
770 if (UseOpIdx >= TID.getNumOperands())
771 return false;
772 const MachineOperand &MO = getOperand(UseOpIdx);
773 if (!MO.isReg() || !MO.isUse())
774 return false;
775 int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO);
776 if (DefIdx == -1)
777 return false;
778 if (DefOpIdx)
779 *DefOpIdx = (unsigned)DefIdx;
780 return true;
781}
782
Evan Cheng576d1232006-12-06 08:27:42 +0000783/// copyKillDeadInfo - Copies kill / dead operand properties from MI.
784///
785void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
786 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
787 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000788 if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
Evan Cheng576d1232006-12-06 08:27:42 +0000789 continue;
790 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
791 MachineOperand &MOp = getOperand(j);
792 if (!MOp.isIdenticalTo(MO))
793 continue;
794 if (MO.isKill())
795 MOp.setIsKill();
796 else
797 MOp.setIsDead();
798 break;
799 }
800 }
801}
802
Evan Cheng19e3f312007-05-15 01:26:09 +0000803/// copyPredicates - Copies predicate operand(s) from MI.
804void MachineInstr::copyPredicates(const MachineInstr *MI) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000805 const TargetInstrDesc &TID = MI->getDesc();
Evan Chengb27087f2008-03-13 00:44:09 +0000806 if (!TID.isPredicable())
807 return;
808 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
809 if (TID.OpInfo[i].isPredicate()) {
810 // Predicated operands must be last operands.
811 addOperand(MI->getOperand(i));
Evan Cheng19e3f312007-05-15 01:26:09 +0000812 }
813 }
814}
815
Evan Cheng9f1c8312008-07-03 09:09:37 +0000816/// isSafeToMove - Return true if it is safe to move this instruction. If
817/// SawStore is set to true, it means that there is a store (or call) between
818/// the instruction's location and its intended destination.
Dan Gohmanb3b930a2008-11-18 19:04:29 +0000819bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII,
820 bool &SawStore) const {
Evan Chengb27087f2008-03-13 00:44:09 +0000821 // Ignore stuff that we obviously can't move.
822 if (TID->mayStore() || TID->isCall()) {
823 SawStore = true;
824 return false;
825 }
Dan Gohman237dee12008-12-23 17:28:50 +0000826 if (TID->isTerminator() || TID->hasUnmodeledSideEffects())
Evan Chengb27087f2008-03-13 00:44:09 +0000827 return false;
828
829 // See if this instruction does a load. If so, we have to guarantee that the
830 // loaded value doesn't change between the load and the its intended
831 // destination. The check for isInvariantLoad gives the targe the chance to
832 // classify the load as always returning a constant, e.g. a constant pool
833 // load.
Dan Gohman3e4fb702008-09-24 00:06:15 +0000834 if (TID->mayLoad() && !TII->isInvariantLoad(this))
Evan Chengb27087f2008-03-13 00:44:09 +0000835 // Otherwise, this is a real load. If there is a store between the load and
Dan Gohman3e4fb702008-09-24 00:06:15 +0000836 // end of block, or if the laod is volatile, we can't move it.
Dan Gohmand790a5c2008-10-02 15:04:30 +0000837 return !SawStore && !hasVolatileMemoryRef();
Dan Gohman3e4fb702008-09-24 00:06:15 +0000838
Evan Chengb27087f2008-03-13 00:44:09 +0000839 return true;
840}
841
Evan Chengdf3b9932008-08-27 20:33:50 +0000842/// isSafeToReMat - Return true if it's safe to rematerialize the specified
843/// instruction which defined the specified register instead of copying it.
Dan Gohmanb3b930a2008-11-18 19:04:29 +0000844bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII,
845 unsigned DstReg) const {
Evan Chengdf3b9932008-08-27 20:33:50 +0000846 bool SawStore = false;
Evan Cheng3689ff42008-08-30 09:07:18 +0000847 if (!getDesc().isRematerializable() ||
848 !TII->isTriviallyReMaterializable(this) ||
849 !isSafeToMove(TII, SawStore))
Evan Chengdf3b9932008-08-27 20:33:50 +0000850 return false;
851 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Dan Gohmancbad42c2008-11-18 19:49:32 +0000852 const MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000853 if (!MO.isReg())
Evan Chengdf3b9932008-08-27 20:33:50 +0000854 continue;
855 // FIXME: For now, do not remat any instruction with register operands.
856 // Later on, we can loosen the restriction is the register operands have
857 // not been modified between the def and use. Note, this is different from
Evan Cheng8763c1c2008-08-27 20:58:54 +0000858 // MachineSink because the code is no longer in two-address form (at least
Evan Chengdf3b9932008-08-27 20:33:50 +0000859 // partially).
860 if (MO.isUse())
861 return false;
862 else if (!MO.isDead() && MO.getReg() != DstReg)
863 return false;
864 }
865 return true;
866}
867
Dan Gohman3e4fb702008-09-24 00:06:15 +0000868/// hasVolatileMemoryRef - Return true if this instruction may have a
869/// volatile memory reference, or if the information describing the
870/// memory reference is not available. Return false if it is known to
871/// have no volatile memory references.
872bool MachineInstr::hasVolatileMemoryRef() const {
873 // An instruction known never to access memory won't have a volatile access.
874 if (!TID->mayStore() &&
875 !TID->mayLoad() &&
876 !TID->isCall() &&
877 !TID->hasUnmodeledSideEffects())
878 return false;
879
880 // Otherwise, if the instruction has no memory reference information,
881 // conservatively assume it wasn't preserved.
882 if (memoperands_empty())
883 return true;
884
885 // Check the memory reference information for volatile references.
886 for (std::list<MachineMemOperand>::const_iterator I = memoperands_begin(),
887 E = memoperands_end(); I != E; ++I)
888 if (I->isVolatile())
889 return true;
890
891 return false;
892}
893
Brian Gaeke21326fc2004-02-13 04:39:32 +0000894void MachineInstr::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000895 cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000896}
897
Tanya Lattnerb1407622004-06-25 00:13:11 +0000898void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
Mon P Wang5ca6bd12008-10-10 01:43:55 +0000899 raw_os_ostream RawOS(OS);
900 print(RawOS, TM);
901}
902
903void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const {
Chris Lattnere3087892007-12-30 21:31:53 +0000904 // Specialize printing if op#0 is definition
Chris Lattner6a592272002-10-30 01:55:38 +0000905 unsigned StartOp = 0;
Dan Gohmand735b802008-10-03 15:45:36 +0000906 if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
Chris Lattnerf7382302007-12-30 21:56:09 +0000907 getOperand(0).print(OS, TM);
Chris Lattner6a592272002-10-30 01:55:38 +0000908 OS << " = ";
909 ++StartOp; // Don't print this operand again!
910 }
Tanya Lattnerb1407622004-06-25 00:13:11 +0000911
Chris Lattner749c6f62008-01-07 07:27:27 +0000912 OS << getDesc().getName();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000913
Chris Lattner6a592272002-10-30 01:55:38 +0000914 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
915 if (i != StartOp)
916 OS << ",";
917 OS << " ";
Chris Lattnerf7382302007-12-30 21:56:09 +0000918 getOperand(i).print(OS, TM);
Chris Lattner10491642002-10-30 00:48:05 +0000919 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000920
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000921 if (!memoperands_empty()) {
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000922 OS << ", Mem:";
Dan Gohmanfed90b62008-07-28 21:51:04 +0000923 for (std::list<MachineMemOperand>::const_iterator i = memoperands_begin(),
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000924 e = memoperands_end(); i != e; ++i) {
925 const MachineMemOperand &MRO = *i;
Dan Gohman69de1932008-02-06 22:27:42 +0000926 const Value *V = MRO.getValue();
927
Dan Gohman69de1932008-02-06 22:27:42 +0000928 assert((MRO.isLoad() || MRO.isStore()) &&
929 "SV has to be a load, store or both.");
930
931 if (MRO.isVolatile())
932 OS << "Volatile ";
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000933
Dan Gohman69de1932008-02-06 22:27:42 +0000934 if (MRO.isLoad())
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000935 OS << "LD";
Dan Gohman69de1932008-02-06 22:27:42 +0000936 if (MRO.isStore())
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000937 OS << "ST";
Dan Gohman69de1932008-02-06 22:27:42 +0000938
Evan Chengbbd83222008-02-08 22:05:07 +0000939 OS << "(" << MRO.getSize() << "," << MRO.getAlignment() << ") [";
Dan Gohman69de1932008-02-06 22:27:42 +0000940
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000941 if (!V)
942 OS << "<unknown>";
943 else if (!V->getName().empty())
944 OS << V->getName();
Chris Lattneredfb72c2008-08-24 20:37:32 +0000945 else if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
Mon P Wang5ca6bd12008-10-10 01:43:55 +0000946 PSV->print(OS);
Chris Lattneredfb72c2008-08-24 20:37:32 +0000947 } else
Dan Gohman2bfe6ff2008-02-07 16:18:00 +0000948 OS << V;
949
950 OS << " + " << MRO.getOffset() << "]";
Dan Gohman69de1932008-02-06 22:27:42 +0000951 }
952 }
953
Bill Wendlingb5ef2732009-02-19 21:44:55 +0000954 if (!debugLoc.isUnknown()) {
955 const MachineFunction *MF = getParent()->getParent();
956 DebugLocTuple DLT = MF->getDebugLocTuple(debugLoc);
957 OS << " [dbg: "
958 << DLT.Src << ","
959 << DLT.Line << ","
960 << DLT.Col << "]";
961 }
962
Chris Lattner10491642002-10-30 00:48:05 +0000963 OS << "\n";
964}
965
Owen Andersonb487e722008-01-24 01:10:07 +0000966bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +0000967 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +0000968 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +0000969 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Dan Gohman2ebc11a2008-07-03 01:18:51 +0000970 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +0000971 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +0000972 SmallVector<unsigned,4> DeadOps;
Bill Wendling4a23d722008-03-03 22:14:33 +0000973 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
974 MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000975 if (!MO.isReg() || !MO.isUse())
Evan Cheng9b6d7b92008-04-16 09:41:59 +0000976 continue;
977 unsigned Reg = MO.getReg();
978 if (!Reg)
979 continue;
Bill Wendling4a23d722008-03-03 22:14:33 +0000980
Evan Cheng9b6d7b92008-04-16 09:41:59 +0000981 if (Reg == IncomingReg) {
Dan Gohman3f629402008-09-03 15:56:16 +0000982 if (!Found) {
983 if (MO.isKill())
984 // The register is already marked kill.
985 return true;
986 MO.setIsKill();
987 Found = true;
988 }
989 } else if (hasAliases && MO.isKill() &&
990 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +0000991 // A super-register kill already exists.
992 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +0000993 return true;
994 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +0000995 DeadOps.push_back(i);
Bill Wendling4a23d722008-03-03 22:14:33 +0000996 }
997 }
998
Evan Cheng9b6d7b92008-04-16 09:41:59 +0000999 // Trim unneeded kill operands.
1000 while (!DeadOps.empty()) {
1001 unsigned OpIdx = DeadOps.back();
1002 if (getOperand(OpIdx).isImplicit())
1003 RemoveOperand(OpIdx);
1004 else
1005 getOperand(OpIdx).setIsKill(false);
1006 DeadOps.pop_back();
1007 }
1008
Bill Wendling4a23d722008-03-03 22:14:33 +00001009 // If not found, this means an alias of one of the operands is killed. Add a
Owen Andersonb487e722008-01-24 01:10:07 +00001010 // new implicit operand if required.
Dan Gohman3f629402008-09-03 15:56:16 +00001011 if (!Found && AddIfNotFound) {
Bill Wendling4a23d722008-03-03 22:14:33 +00001012 addOperand(MachineOperand::CreateReg(IncomingReg,
1013 false /*IsDef*/,
1014 true /*IsImp*/,
1015 true /*IsKill*/));
Owen Andersonb487e722008-01-24 01:10:07 +00001016 return true;
1017 }
Dan Gohman3f629402008-09-03 15:56:16 +00001018 return Found;
Owen Andersonb487e722008-01-24 01:10:07 +00001019}
1020
1021bool MachineInstr::addRegisterDead(unsigned IncomingReg,
Dan Gohman6f0d0242008-02-10 18:45:23 +00001022 const TargetRegisterInfo *RegInfo,
Owen Andersonb487e722008-01-24 01:10:07 +00001023 bool AddIfNotFound) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001024 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Evan Cheng01b2e232008-06-27 22:11:49 +00001025 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
Dan Gohman3f629402008-09-03 15:56:16 +00001026 bool Found = false;
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001027 SmallVector<unsigned,4> DeadOps;
Owen Andersonb487e722008-01-24 01:10:07 +00001028 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1029 MachineOperand &MO = getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00001030 if (!MO.isReg() || !MO.isDef())
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001031 continue;
1032 unsigned Reg = MO.getReg();
Dan Gohman3f629402008-09-03 15:56:16 +00001033 if (!Reg)
1034 continue;
1035
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001036 if (Reg == IncomingReg) {
Dan Gohman3f629402008-09-03 15:56:16 +00001037 if (!Found) {
1038 if (MO.isDead())
1039 // The register is already marked dead.
1040 return true;
1041 MO.setIsDead();
1042 Found = true;
1043 }
1044 } else if (hasAliases && MO.isDead() &&
1045 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001046 // There exists a super-register that's marked dead.
1047 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohman2ebc11a2008-07-03 01:18:51 +00001048 return true;
Owen Anderson22ae9992008-08-14 18:34:18 +00001049 if (RegInfo->getSubRegisters(IncomingReg) &&
1050 RegInfo->getSuperRegisters(Reg) &&
1051 RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001052 DeadOps.push_back(i);
Owen Andersonb487e722008-01-24 01:10:07 +00001053 }
1054 }
1055
Evan Cheng9b6d7b92008-04-16 09:41:59 +00001056 // Trim unneeded dead operands.
1057 while (!DeadOps.empty()) {
1058 unsigned OpIdx = DeadOps.back();
1059 if (getOperand(OpIdx).isImplicit())
1060 RemoveOperand(OpIdx);
1061 else
1062 getOperand(OpIdx).setIsDead(false);
1063 DeadOps.pop_back();
1064 }
1065
Dan Gohman3f629402008-09-03 15:56:16 +00001066 // If not found, this means an alias of one of the operands is dead. Add a
1067 // new implicit operand if required.
1068 if (!Found && AddIfNotFound) {
1069 addOperand(MachineOperand::CreateReg(IncomingReg,
1070 true /*IsDef*/,
1071 true /*IsImp*/,
1072 false /*IsKill*/,
1073 true /*IsDead*/));
Owen Andersonb487e722008-01-24 01:10:07 +00001074 return true;
1075 }
Dan Gohman3f629402008-09-03 15:56:16 +00001076 return Found;
Owen Andersonb487e722008-01-24 01:10:07 +00001077}