Chris Lattner | e138b3d | 2008-01-01 20:36:19 +0000 | [diff] [blame] | 1 | //===-- lib/CodeGen/MachineInstr.cpp --------------------------------------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Brian Gaeke | 21326fc | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 9 | // |
| 10 | // Methods common to all machine instructions. |
| 11 | // |
Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 13 | |
Chris Lattner | 822b4fb | 2001-09-07 17:18:30 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 15 | #include "llvm/Value.h" |
Chris Lattner | 8517e1f | 2004-02-19 16:17:08 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | 2a79a09 | 2002-10-30 00:58:19 +0000 | [diff] [blame] | 20 | #include "llvm/Target/MRegisterInfo.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/LeakDetector.h" |
Bill Wendling | a09362e | 2006-11-28 22:48:48 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Streams.h" |
Jeff Cohen | c21c5ee | 2006-12-15 22:57:14 +0000 | [diff] [blame] | 23 | #include <ostream> |
Chris Lattner | 0742b59 | 2004-02-23 18:38:20 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | // MachineOperand Implementation |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 30 | /// AddRegOperandToRegInfo - Add this register operand to the specified |
| 31 | /// MachineRegisterInfo. If it is null, then the next/prev fields should be |
| 32 | /// explicitly nulled out. |
| 33 | void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) { |
| 34 | assert(isReg() && "Can only add reg operand to use lists"); |
| 35 | |
| 36 | // If the reginfo pointer is null, just explicitly null out or next/prev |
| 37 | // pointers, to ensure they are not garbage. |
| 38 | if (RegInfo == 0) { |
| 39 | Contents.Reg.Prev = 0; |
| 40 | Contents.Reg.Next = 0; |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | // Otherwise, add this operand to the head of the registers use/def list. |
Chris Lattner | 80fe531 | 2008-01-01 21:08:22 +0000 | [diff] [blame^] | 45 | MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg()); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 80fe531 | 2008-01-01 21:08:22 +0000 | [diff] [blame^] | 47 | // For SSA values, we prefer to keep the definition at the start of the list. |
| 48 | // we do this by skipping over the definition if it is at the head of the |
| 49 | // list. |
| 50 | if (*Head && (*Head)->isDef()) |
| 51 | Head = &(*Head)->Contents.Reg.Next; |
| 52 | |
| 53 | Contents.Reg.Next = *Head; |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 54 | if (Contents.Reg.Next) { |
| 55 | assert(getReg() == Contents.Reg.Next->getReg() && |
| 56 | "Different regs on the same list!"); |
| 57 | Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next; |
| 58 | } |
| 59 | |
Chris Lattner | 80fe531 | 2008-01-01 21:08:22 +0000 | [diff] [blame^] | 60 | Contents.Reg.Prev = Head; |
| 61 | *Head = this; |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void MachineOperand::setReg(unsigned Reg) { |
| 65 | if (getReg() == Reg) return; // No change. |
| 66 | |
| 67 | // Otherwise, we have to change the register. If this operand is embedded |
| 68 | // into a machine function, we need to update the old and new register's |
| 69 | // use/def lists. |
| 70 | if (MachineInstr *MI = getParent()) |
| 71 | if (MachineBasicBlock *MBB = MI->getParent()) |
| 72 | if (MachineFunction *MF = MBB->getParent()) { |
| 73 | RemoveRegOperandFromRegInfo(); |
| 74 | Contents.Reg.RegNo = Reg; |
| 75 | AddRegOperandToRegInfo(&MF->getRegInfo()); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | // Otherwise, just change the register, no problem. :) |
| 80 | Contents.Reg.RegNo = Reg; |
| 81 | } |
| 82 | |
| 83 | /// ChangeToImmediate - Replace this operand with a new immediate operand of |
| 84 | /// the specified value. If an operand is known to be an immediate already, |
| 85 | /// the setImm method should be used. |
| 86 | void MachineOperand::ChangeToImmediate(int64_t ImmVal) { |
| 87 | // If this operand is currently a register operand, and if this is in a |
| 88 | // function, deregister the operand from the register's use/def list. |
| 89 | if (isReg() && getParent() && getParent()->getParent() && |
| 90 | getParent()->getParent()->getParent()) |
| 91 | RemoveRegOperandFromRegInfo(); |
| 92 | |
| 93 | OpKind = MO_Immediate; |
| 94 | Contents.ImmVal = ImmVal; |
| 95 | } |
| 96 | |
| 97 | /// ChangeToRegister - Replace this operand with a new register operand of |
| 98 | /// the specified value. If an operand is known to be an register already, |
| 99 | /// the setReg method should be used. |
| 100 | void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp, |
| 101 | bool isKill, bool isDead) { |
| 102 | // If this operand is already a register operand, use setReg to update the |
| 103 | // register's use/def lists. |
| 104 | if (isReg()) { |
| 105 | setReg(Reg); |
| 106 | } else { |
| 107 | // Otherwise, change this to a register and set the reg#. |
| 108 | OpKind = MO_Register; |
| 109 | Contents.Reg.RegNo = Reg; |
| 110 | |
| 111 | // If this operand is embedded in a function, add the operand to the |
| 112 | // register's use/def list. |
| 113 | if (MachineInstr *MI = getParent()) |
| 114 | if (MachineBasicBlock *MBB = MI->getParent()) |
| 115 | if (MachineFunction *MF = MBB->getParent()) |
| 116 | AddRegOperandToRegInfo(&MF->getRegInfo()); |
| 117 | } |
| 118 | |
| 119 | IsDef = isDef; |
| 120 | IsImp = isImp; |
| 121 | IsKill = isKill; |
| 122 | IsDead = isDead; |
| 123 | SubReg = 0; |
| 124 | } |
| 125 | |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 126 | /// isIdenticalTo - Return true if this operand is identical to the specified |
| 127 | /// operand. |
| 128 | bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const { |
| 129 | if (getType() != Other.getType()) return false; |
| 130 | |
| 131 | switch (getType()) { |
| 132 | default: assert(0 && "Unrecognized operand type"); |
| 133 | case MachineOperand::MO_Register: |
| 134 | return getReg() == Other.getReg() && isDef() == Other.isDef() && |
| 135 | getSubReg() == Other.getSubReg(); |
| 136 | case MachineOperand::MO_Immediate: |
| 137 | return getImm() == Other.getImm(); |
| 138 | case MachineOperand::MO_MachineBasicBlock: |
| 139 | return getMBB() == Other.getMBB(); |
| 140 | case MachineOperand::MO_FrameIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 141 | return getIndex() == Other.getIndex(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 142 | case MachineOperand::MO_ConstantPoolIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 143 | return getIndex() == Other.getIndex() && getOffset() == Other.getOffset(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 144 | case MachineOperand::MO_JumpTableIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 145 | return getIndex() == Other.getIndex(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 146 | case MachineOperand::MO_GlobalAddress: |
| 147 | return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset(); |
| 148 | case MachineOperand::MO_ExternalSymbol: |
| 149 | return !strcmp(getSymbolName(), Other.getSymbolName()) && |
| 150 | getOffset() == Other.getOffset(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /// print - Print the specified machine operand. |
| 155 | /// |
| 156 | void MachineOperand::print(std::ostream &OS, const TargetMachine *TM) const { |
| 157 | switch (getType()) { |
| 158 | case MachineOperand::MO_Register: |
| 159 | if (getReg() == 0 || MRegisterInfo::isVirtualRegister(getReg())) { |
| 160 | OS << "%reg" << getReg(); |
| 161 | } else { |
| 162 | // If the instruction is embedded into a basic block, we can find the |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 163 | // target info for the instruction. |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 164 | if (TM == 0) |
| 165 | if (const MachineInstr *MI = getParent()) |
| 166 | if (const MachineBasicBlock *MBB = MI->getParent()) |
| 167 | if (const MachineFunction *MF = MBB->getParent()) |
| 168 | TM = &MF->getTarget(); |
| 169 | |
| 170 | if (TM) |
| 171 | OS << "%" << TM->getRegisterInfo()->get(getReg()).Name; |
| 172 | else |
| 173 | OS << "%mreg" << getReg(); |
| 174 | } |
| 175 | |
| 176 | if (isDef() || isKill() || isDead() || isImplicit()) { |
| 177 | OS << "<"; |
| 178 | bool NeedComma = false; |
| 179 | if (isImplicit()) { |
| 180 | OS << (isDef() ? "imp-def" : "imp-use"); |
| 181 | NeedComma = true; |
| 182 | } else if (isDef()) { |
| 183 | OS << "def"; |
| 184 | NeedComma = true; |
| 185 | } |
| 186 | if (isKill() || isDead()) { |
| 187 | if (NeedComma) OS << ","; |
| 188 | if (isKill()) OS << "kill"; |
| 189 | if (isDead()) OS << "dead"; |
| 190 | } |
| 191 | OS << ">"; |
| 192 | } |
| 193 | break; |
| 194 | case MachineOperand::MO_Immediate: |
| 195 | OS << getImm(); |
| 196 | break; |
| 197 | case MachineOperand::MO_MachineBasicBlock: |
| 198 | OS << "mbb<" |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 199 | << ((Value*)getMBB()->getBasicBlock())->getName() |
| 200 | << "," << (void*)getMBB() << ">"; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 201 | break; |
| 202 | case MachineOperand::MO_FrameIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 203 | OS << "<fi#" << getIndex() << ">"; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 204 | break; |
| 205 | case MachineOperand::MO_ConstantPoolIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 206 | OS << "<cp#" << getIndex(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 207 | if (getOffset()) OS << "+" << getOffset(); |
| 208 | OS << ">"; |
| 209 | break; |
| 210 | case MachineOperand::MO_JumpTableIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 211 | OS << "<jt#" << getIndex() << ">"; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 212 | break; |
| 213 | case MachineOperand::MO_GlobalAddress: |
| 214 | OS << "<ga:" << ((Value*)getGlobal())->getName(); |
| 215 | if (getOffset()) OS << "+" << getOffset(); |
| 216 | OS << ">"; |
| 217 | break; |
| 218 | case MachineOperand::MO_ExternalSymbol: |
| 219 | OS << "<es:" << getSymbolName(); |
| 220 | if (getOffset()) OS << "+" << getOffset(); |
| 221 | OS << ">"; |
| 222 | break; |
| 223 | default: |
| 224 | assert(0 && "Unrecognized operand type"); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | //===----------------------------------------------------------------------===// |
| 229 | // MachineInstr Implementation |
| 230 | //===----------------------------------------------------------------------===// |
| 231 | |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 232 | /// MachineInstr ctor - This constructor creates a dummy MachineInstr with |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 233 | /// TID NULL and no operands. |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 234 | MachineInstr::MachineInstr() |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 235 | : TID(0), NumImplicitOps(0), Parent(0) { |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 236 | // Make sure that we get added to a machine basicblock |
| 237 | LeakDetector::addGarbageObject(this); |
Chris Lattner | 7279122 | 2002-10-28 20:59:49 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 240 | void MachineInstr::addImplicitDefUseOperands() { |
| 241 | if (TID->ImplicitDefs) |
Chris Lattner | a4161ee | 2007-12-30 00:12:25 +0000 | [diff] [blame] | 242 | for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs) |
Chris Lattner | 8019f41 | 2007-12-30 00:41:17 +0000 | [diff] [blame] | 243 | addOperand(MachineOperand::CreateReg(*ImpDefs, true, true)); |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 244 | if (TID->ImplicitUses) |
Chris Lattner | a4161ee | 2007-12-30 00:12:25 +0000 | [diff] [blame] | 245 | for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses) |
Chris Lattner | 8019f41 | 2007-12-30 00:41:17 +0000 | [diff] [blame] | 246 | addOperand(MachineOperand::CreateReg(*ImpUses, false, true)); |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /// MachineInstr ctor - This constructor create a MachineInstr and add the |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 250 | /// implicit operands. It reserves space for number of operands specified by |
| 251 | /// TargetInstrDescriptor or the numOperands if it is not zero. (for |
| 252 | /// instructions with variable number of operands). |
Evan Cheng | fa94572 | 2007-10-13 02:23:01 +0000 | [diff] [blame] | 253 | MachineInstr::MachineInstr(const TargetInstrDescriptor &tid, bool NoImp) |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 254 | : TID(&tid), NumImplicitOps(0), Parent(0) { |
Evan Cheng | fa94572 | 2007-10-13 02:23:01 +0000 | [diff] [blame] | 255 | if (!NoImp && TID->ImplicitDefs) |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 256 | for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs) |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 257 | NumImplicitOps++; |
Evan Cheng | fa94572 | 2007-10-13 02:23:01 +0000 | [diff] [blame] | 258 | if (!NoImp && TID->ImplicitUses) |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 259 | for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses) |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 260 | NumImplicitOps++; |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 261 | Operands.reserve(NumImplicitOps + TID->numOperands); |
Evan Cheng | fa94572 | 2007-10-13 02:23:01 +0000 | [diff] [blame] | 262 | if (!NoImp) |
| 263 | addImplicitDefUseOperands(); |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 264 | // Make sure that we get added to a machine basicblock |
| 265 | LeakDetector::addGarbageObject(this); |
| 266 | } |
| 267 | |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 268 | /// MachineInstr ctor - Work exactly the same as the ctor above, except that the |
| 269 | /// MachineInstr is created and added to the end of the specified basic block. |
| 270 | /// |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 271 | MachineInstr::MachineInstr(MachineBasicBlock *MBB, |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 272 | const TargetInstrDescriptor &tid) |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 273 | : TID(&tid), NumImplicitOps(0), Parent(0) { |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 274 | assert(MBB && "Cannot use inserting ctor with null basic block!"); |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 275 | if (TID->ImplicitDefs) |
| 276 | for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs) |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 277 | NumImplicitOps++; |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 278 | if (TID->ImplicitUses) |
| 279 | for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses) |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 280 | NumImplicitOps++; |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 281 | Operands.reserve(NumImplicitOps + TID->numOperands); |
| 282 | addImplicitDefUseOperands(); |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 283 | // Make sure that we get added to a machine basicblock |
| 284 | LeakDetector::addGarbageObject(this); |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 285 | MBB->push_back(this); // Add instruction to end of basic block! |
| 286 | } |
| 287 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 288 | /// MachineInstr ctor - Copies MachineInstr arg exactly |
| 289 | /// |
Tanya Lattner | 466b534 | 2004-05-23 19:35:12 +0000 | [diff] [blame] | 290 | MachineInstr::MachineInstr(const MachineInstr &MI) { |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 291 | TID = MI.getInstrDescriptor(); |
Evan Cheng | 6b2c05f | 2006-11-15 20:54:29 +0000 | [diff] [blame] | 292 | NumImplicitOps = MI.NumImplicitOps; |
Chris Lattner | 943b5e1 | 2006-05-04 19:14:44 +0000 | [diff] [blame] | 293 | Operands.reserve(MI.getNumOperands()); |
Tanya Lattner | b5159ed | 2004-05-23 20:58:02 +0000 | [diff] [blame] | 294 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 295 | // Add operands |
Chris Lattner | e12d6ab | 2007-12-30 06:11:04 +0000 | [diff] [blame] | 296 | for (unsigned i = 0; i != MI.getNumOperands(); ++i) { |
Chris Lattner | 943b5e1 | 2006-05-04 19:14:44 +0000 | [diff] [blame] | 297 | Operands.push_back(MI.getOperand(i)); |
Chris Lattner | e12d6ab | 2007-12-30 06:11:04 +0000 | [diff] [blame] | 298 | Operands.back().ParentMI = this; |
| 299 | } |
Tanya Lattner | 0c63e03 | 2004-05-24 03:14:18 +0000 | [diff] [blame] | 300 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 301 | // Set parent, next, and prev to null |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 302 | Parent = 0; |
| 303 | Prev = 0; |
| 304 | Next = 0; |
Tanya Lattner | 466b534 | 2004-05-23 19:35:12 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 308 | MachineInstr::~MachineInstr() { |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 309 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | e12d6ab | 2007-12-30 06:11:04 +0000 | [diff] [blame] | 310 | #ifndef NDEBUG |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 311 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
Chris Lattner | e12d6ab | 2007-12-30 06:11:04 +0000 | [diff] [blame] | 312 | assert(Operands[i].ParentMI == this && "ParentMI mismatch!"); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 313 | assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) && |
| 314 | "Reg operand def/use list corrupted"); |
| 315 | } |
Chris Lattner | e12d6ab | 2007-12-30 06:11:04 +0000 | [diff] [blame] | 316 | #endif |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 319 | /// getOpcode - Returns the opcode of this MachineInstr. |
| 320 | /// |
Dan Gohman | cb648f9 | 2007-09-14 20:08:19 +0000 | [diff] [blame] | 321 | int MachineInstr::getOpcode() const { |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 322 | return TID->Opcode; |
| 323 | } |
| 324 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 325 | /// getRegInfo - If this instruction is embedded into a MachineFunction, |
| 326 | /// return the MachineRegisterInfo object for the current function, otherwise |
| 327 | /// return null. |
| 328 | MachineRegisterInfo *MachineInstr::getRegInfo() { |
| 329 | if (MachineBasicBlock *MBB = getParent()) |
| 330 | if (MachineFunction *MF = MBB->getParent()) |
| 331 | return &MF->getRegInfo(); |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in |
| 336 | /// this instruction from their respective use lists. This requires that the |
| 337 | /// operands already be on their use lists. |
| 338 | void MachineInstr::RemoveRegOperandsFromUseLists() { |
| 339 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
| 340 | if (Operands[i].isReg()) |
| 341 | Operands[i].RemoveRegOperandFromRegInfo(); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /// AddRegOperandsToUseLists - Add all of the register operands in |
| 346 | /// this instruction from their respective use lists. This requires that the |
| 347 | /// operands not be on their use lists yet. |
| 348 | void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) { |
| 349 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
| 350 | if (Operands[i].isReg()) |
| 351 | Operands[i].AddRegOperandToRegInfo(&RegInfo); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | |
| 356 | /// addOperand - Add the specified operand to the instruction. If it is an |
| 357 | /// implicit operand, it is added to the end of the operand list. If it is |
| 358 | /// an explicit operand it is added at the end of the explicit operand list |
| 359 | /// (before the first implicit operand). |
| 360 | void MachineInstr::addOperand(const MachineOperand &Op) { |
| 361 | bool isImpReg = Op.isReg() && Op.isImplicit(); |
| 362 | assert((isImpReg || !OperandsComplete()) && |
| 363 | "Trying to add an operand to a machine instr that is already done!"); |
| 364 | |
| 365 | // If we are adding the operand to the end of the list, our job is simpler. |
| 366 | // This is true most of the time, so this is a reasonable optimization. |
| 367 | if (isImpReg || NumImplicitOps == 0) { |
| 368 | // We can only do this optimization if we know that the operand list won't |
| 369 | // reallocate. |
| 370 | if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) { |
| 371 | Operands.push_back(Op); |
| 372 | |
| 373 | // Set the parent of the operand. |
| 374 | Operands.back().ParentMI = this; |
| 375 | |
| 376 | // If the operand is a register, update the operand's use list. |
| 377 | if (Op.isReg()) |
| 378 | Operands.back().AddRegOperandToRegInfo(getRegInfo()); |
| 379 | return; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | // Otherwise, we have to insert a real operand before any implicit ones. |
| 384 | unsigned OpNo = Operands.size()-NumImplicitOps; |
| 385 | |
| 386 | MachineRegisterInfo *RegInfo = getRegInfo(); |
| 387 | |
| 388 | // If this instruction isn't embedded into a function, then we don't need to |
| 389 | // update any operand lists. |
| 390 | if (RegInfo == 0) { |
| 391 | // Simple insertion, no reginfo update needed for other register operands. |
| 392 | Operands.insert(Operands.begin()+OpNo, Op); |
| 393 | Operands[OpNo].ParentMI = this; |
| 394 | |
| 395 | // Do explicitly set the reginfo for this operand though, to ensure the |
| 396 | // next/prev fields are properly nulled out. |
| 397 | if (Operands[OpNo].isReg()) |
| 398 | Operands[OpNo].AddRegOperandToRegInfo(0); |
| 399 | |
| 400 | } else if (Operands.size()+1 <= Operands.capacity()) { |
| 401 | // Otherwise, we have to remove register operands from their register use |
| 402 | // list, add the operand, then add the register operands back to their use |
| 403 | // list. This also must handle the case when the operand list reallocates |
| 404 | // to somewhere else. |
| 405 | |
| 406 | // If insertion of this operand won't cause reallocation of the operand |
| 407 | // list, just remove the implicit operands, add the operand, then re-add all |
| 408 | // the rest of the operands. |
| 409 | for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { |
| 410 | assert(Operands[i].isReg() && "Should only be an implicit reg!"); |
| 411 | Operands[i].RemoveRegOperandFromRegInfo(); |
| 412 | } |
| 413 | |
| 414 | // Add the operand. If it is a register, add it to the reg list. |
| 415 | Operands.insert(Operands.begin()+OpNo, Op); |
| 416 | Operands[OpNo].ParentMI = this; |
| 417 | |
| 418 | if (Operands[OpNo].isReg()) |
| 419 | Operands[OpNo].AddRegOperandToRegInfo(RegInfo); |
| 420 | |
| 421 | // Re-add all the implicit ops. |
| 422 | for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) { |
| 423 | assert(Operands[i].isReg() && "Should only be an implicit reg!"); |
| 424 | Operands[i].AddRegOperandToRegInfo(RegInfo); |
| 425 | } |
| 426 | } else { |
| 427 | // Otherwise, we will be reallocating the operand list. Remove all reg |
| 428 | // operands from their list, then readd them after the operand list is |
| 429 | // reallocated. |
| 430 | RemoveRegOperandsFromUseLists(); |
| 431 | |
| 432 | Operands.insert(Operands.begin()+OpNo, Op); |
| 433 | Operands[OpNo].ParentMI = this; |
| 434 | |
| 435 | // Re-add all the operands. |
| 436 | AddRegOperandsToUseLists(*RegInfo); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | /// RemoveOperand - Erase an operand from an instruction, leaving it with one |
| 441 | /// fewer operand than it started with. |
| 442 | /// |
| 443 | void MachineInstr::RemoveOperand(unsigned OpNo) { |
| 444 | assert(OpNo < Operands.size() && "Invalid operand number"); |
| 445 | |
| 446 | // Special case removing the last one. |
| 447 | if (OpNo == Operands.size()-1) { |
| 448 | // If needed, remove from the reg def/use list. |
| 449 | if (Operands.back().isReg() && Operands.back().isOnRegUseList()) |
| 450 | Operands.back().RemoveRegOperandFromRegInfo(); |
| 451 | |
| 452 | Operands.pop_back(); |
| 453 | return; |
| 454 | } |
| 455 | |
| 456 | // Otherwise, we are removing an interior operand. If we have reginfo to |
| 457 | // update, remove all operands that will be shifted down from their reg lists, |
| 458 | // move everything down, then re-add them. |
| 459 | MachineRegisterInfo *RegInfo = getRegInfo(); |
| 460 | if (RegInfo) { |
| 461 | for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { |
| 462 | if (Operands[i].isReg()) |
| 463 | Operands[i].RemoveRegOperandFromRegInfo(); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | Operands.erase(Operands.begin()+OpNo); |
| 468 | |
| 469 | if (RegInfo) { |
| 470 | for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { |
| 471 | if (Operands[i].isReg()) |
| 472 | Operands[i].AddRegOperandToRegInfo(RegInfo); |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | |
Chris Lattner | 48d7c06 | 2006-04-17 21:35:41 +0000 | [diff] [blame] | 478 | /// removeFromParent - This method unlinks 'this' from the containing basic |
| 479 | /// block, and returns it, but does not delete it. |
| 480 | MachineInstr *MachineInstr::removeFromParent() { |
| 481 | assert(getParent() && "Not embedded in a basic block!"); |
| 482 | getParent()->remove(this); |
| 483 | return this; |
| 484 | } |
| 485 | |
| 486 | |
Brian Gaeke | 21326fc | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 487 | /// OperandComplete - Return true if it's illegal to add a new operand |
| 488 | /// |
Chris Lattner | 2a90ba6 | 2004-02-12 16:09:53 +0000 | [diff] [blame] | 489 | bool MachineInstr::OperandsComplete() const { |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 490 | unsigned short NumOperands = TID->numOperands; |
| 491 | if ((TID->Flags & M_VARIABLE_OPS) == 0 && |
Evan Cheng | 8bcb042 | 2006-11-28 02:25:34 +0000 | [diff] [blame] | 492 | getNumOperands()-NumImplicitOps >= NumOperands) |
Vikram S. Adve | 3497782 | 2003-05-31 07:39:06 +0000 | [diff] [blame] | 493 | return true; // Broken: we have all the operands of this instruction! |
Chris Lattner | 413746e | 2002-10-28 20:48:39 +0000 | [diff] [blame] | 494 | return false; |
| 495 | } |
| 496 | |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 497 | /// getNumExplicitOperands - Returns the number of non-implicit operands. |
| 498 | /// |
| 499 | unsigned MachineInstr::getNumExplicitOperands() const { |
| 500 | unsigned NumOperands = TID->numOperands; |
| 501 | if ((TID->Flags & M_VARIABLE_OPS) == 0) |
| 502 | return NumOperands; |
| 503 | |
| 504 | for (unsigned e = getNumOperands(); NumOperands != e; ++NumOperands) { |
| 505 | const MachineOperand &MO = getOperand(NumOperands); |
| 506 | if (!MO.isRegister() || !MO.isImplicit()) |
| 507 | NumOperands++; |
| 508 | } |
| 509 | return NumOperands; |
| 510 | } |
| 511 | |
Chris Lattner | 8ace2cd | 2006-10-20 22:39:59 +0000 | [diff] [blame] | 512 | |
Evan Cheng | faa5107 | 2007-04-26 19:00:32 +0000 | [diff] [blame] | 513 | /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of |
Evan Cheng | 32eb1f1 | 2007-03-26 22:37:45 +0000 | [diff] [blame] | 514 | /// the specific register or -1 if it is not found. It further tightening |
Evan Cheng | 76d7e76 | 2007-02-23 01:04:26 +0000 | [diff] [blame] | 515 | /// the search criteria to a use that kills the register if isKill is true. |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 516 | int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill) const { |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 517 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 518 | const MachineOperand &MO = getOperand(i); |
Dan Gohman | 92dfe20 | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 519 | if (MO.isRegister() && MO.isUse() && MO.getReg() == Reg) |
Evan Cheng | 76d7e76 | 2007-02-23 01:04:26 +0000 | [diff] [blame] | 520 | if (!isKill || MO.isKill()) |
Evan Cheng | 32eb1f1 | 2007-03-26 22:37:45 +0000 | [diff] [blame] | 521 | return i; |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 522 | } |
Evan Cheng | 32eb1f1 | 2007-03-26 22:37:45 +0000 | [diff] [blame] | 523 | return -1; |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 526 | /// findRegisterDefOperand() - Returns the MachineOperand that is a def of |
| 527 | /// the specific register or NULL if it is not found. |
| 528 | MachineOperand *MachineInstr::findRegisterDefOperand(unsigned Reg) { |
| 529 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 530 | MachineOperand &MO = getOperand(i); |
Dan Gohman | 92dfe20 | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 531 | if (MO.isRegister() && MO.isDef() && MO.getReg() == Reg) |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 532 | return &MO; |
| 533 | } |
| 534 | return NULL; |
| 535 | } |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 536 | |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 537 | /// findFirstPredOperandIdx() - Find the index of the first operand in the |
| 538 | /// operand list that is used to represent the predicate. It returns -1 if |
| 539 | /// none is found. |
| 540 | int MachineInstr::findFirstPredOperandIdx() const { |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 541 | const TargetInstrDescriptor *TID = getInstrDescriptor(); |
Evan Cheng | c3a289c | 2007-05-16 20:56:08 +0000 | [diff] [blame] | 542 | if (TID->Flags & M_PREDICABLE) { |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 543 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 544 | if ((TID->OpInfo[i].Flags & M_PREDICATE_OPERAND)) |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 545 | return i; |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 548 | return -1; |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 549 | } |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 550 | |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 551 | /// isRegReDefinedByTwoAddr - Returns true if the Reg re-definition is due |
| 552 | /// to two addr elimination. |
| 553 | bool MachineInstr::isRegReDefinedByTwoAddr(unsigned Reg) const { |
| 554 | const TargetInstrDescriptor *TID = getInstrDescriptor(); |
| 555 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 556 | const MachineOperand &MO1 = getOperand(i); |
| 557 | if (MO1.isRegister() && MO1.isDef() && MO1.getReg() == Reg) { |
| 558 | for (unsigned j = i+1; j < e; ++j) { |
| 559 | const MachineOperand &MO2 = getOperand(j); |
| 560 | if (MO2.isRegister() && MO2.isUse() && MO2.getReg() == Reg && |
| 561 | TID->getOperandConstraint(j, TOI::TIED_TO) == (int)i) |
| 562 | return true; |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | return false; |
| 567 | } |
| 568 | |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 569 | /// copyKillDeadInfo - Copies kill / dead operand properties from MI. |
| 570 | /// |
| 571 | void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) { |
| 572 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 573 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | 92dfe20 | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 574 | if (!MO.isRegister() || (!MO.isKill() && !MO.isDead())) |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 575 | continue; |
| 576 | for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) { |
| 577 | MachineOperand &MOp = getOperand(j); |
| 578 | if (!MOp.isIdenticalTo(MO)) |
| 579 | continue; |
| 580 | if (MO.isKill()) |
| 581 | MOp.setIsKill(); |
| 582 | else |
| 583 | MOp.setIsDead(); |
| 584 | break; |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 589 | /// copyPredicates - Copies predicate operand(s) from MI. |
| 590 | void MachineInstr::copyPredicates(const MachineInstr *MI) { |
| 591 | const TargetInstrDescriptor *TID = MI->getInstrDescriptor(); |
Evan Cheng | c3a289c | 2007-05-16 20:56:08 +0000 | [diff] [blame] | 592 | if (TID->Flags & M_PREDICABLE) { |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 593 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 594 | if ((TID->OpInfo[i].Flags & M_PREDICATE_OPERAND)) { |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 595 | // Predicated operands must be last operands. |
Chris Lattner | 8019f41 | 2007-12-30 00:41:17 +0000 | [diff] [blame] | 596 | addOperand(MI->getOperand(i)); |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
Brian Gaeke | 21326fc | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 602 | void MachineInstr::dump() const { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 603 | cerr << " " << *this; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Tanya Lattner | b140762 | 2004-06-25 00:13:11 +0000 | [diff] [blame] | 606 | void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const { |
Chris Lattner | e308789 | 2007-12-30 21:31:53 +0000 | [diff] [blame] | 607 | // Specialize printing if op#0 is definition |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 608 | unsigned StartOp = 0; |
Dan Gohman | 92dfe20 | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 609 | if (getNumOperands() && getOperand(0).isRegister() && getOperand(0).isDef()) { |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 610 | getOperand(0).print(OS, TM); |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 611 | OS << " = "; |
| 612 | ++StartOp; // Don't print this operand again! |
| 613 | } |
Tanya Lattner | b140762 | 2004-06-25 00:13:11 +0000 | [diff] [blame] | 614 | |
Chris Lattner | e308789 | 2007-12-30 21:31:53 +0000 | [diff] [blame] | 615 | OS << getInstrDescriptor()->Name; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 616 | |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 617 | for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) { |
| 618 | if (i != StartOp) |
| 619 | OS << ","; |
| 620 | OS << " "; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 621 | getOperand(i).print(OS, TM); |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 622 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 623 | |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 624 | OS << "\n"; |
| 625 | } |
| 626 | |