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