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 | |
Nate Begeman | e8b7ccf | 2008-02-14 07:39:30 +0000 | [diff] [blame] | 14 | #include "llvm/Constants.h" |
Chris Lattner | 822b4fb | 2001-09-07 17:18:30 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 16 | #include "llvm/Value.h" |
Chris Lattner | 8517e1f | 2004-02-19 16:17:08 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetMachine.h" |
Evan Cheng | bb81d97 | 2008-01-31 09:59:15 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | f14cf85 | 2008-01-07 07:42:25 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetInstrDesc.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetRegisterInfo.h" |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 24 | #include "llvm/Support/LeakDetector.h" |
Dan Gohman | ce42e40 | 2008-07-07 20:32:02 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MathExtras.h" |
Bill Wendling | a09362e | 2006-11-28 22:48:48 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Streams.h" |
Dan Gohman | b8d2f55 | 2008-08-20 15:58:01 +0000 | [diff] [blame^] | 27 | #include "llvm/ADT/FoldingSet.h" |
Jeff Cohen | c21c5ee | 2006-12-15 22:57:14 +0000 | [diff] [blame] | 28 | #include <ostream> |
Chris Lattner | 0742b59 | 2004-02-23 18:38:20 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // MachineOperand Implementation |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 35 | /// AddRegOperandToRegInfo - Add this register operand to the specified |
| 36 | /// MachineRegisterInfo. If it is null, then the next/prev fields should be |
| 37 | /// explicitly nulled out. |
| 38 | void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) { |
| 39 | assert(isReg() && "Can only add reg operand to use lists"); |
| 40 | |
| 41 | // If the reginfo pointer is null, just explicitly null out or next/prev |
| 42 | // pointers, to ensure they are not garbage. |
| 43 | if (RegInfo == 0) { |
| 44 | Contents.Reg.Prev = 0; |
| 45 | Contents.Reg.Next = 0; |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | // 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] | 50 | MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg()); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 80fe531 | 2008-01-01 21:08:22 +0000 | [diff] [blame] | 52 | // For SSA values, we prefer to keep the definition at the start of the list. |
| 53 | // we do this by skipping over the definition if it is at the head of the |
| 54 | // list. |
| 55 | if (*Head && (*Head)->isDef()) |
| 56 | Head = &(*Head)->Contents.Reg.Next; |
| 57 | |
| 58 | Contents.Reg.Next = *Head; |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 59 | if (Contents.Reg.Next) { |
| 60 | assert(getReg() == Contents.Reg.Next->getReg() && |
| 61 | "Different regs on the same list!"); |
| 62 | Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next; |
| 63 | } |
| 64 | |
Chris Lattner | 80fe531 | 2008-01-01 21:08:22 +0000 | [diff] [blame] | 65 | Contents.Reg.Prev = Head; |
| 66 | *Head = this; |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void MachineOperand::setReg(unsigned Reg) { |
| 70 | if (getReg() == Reg) return; // No change. |
| 71 | |
| 72 | // Otherwise, we have to change the register. If this operand is embedded |
| 73 | // into a machine function, we need to update the old and new register's |
| 74 | // use/def lists. |
| 75 | if (MachineInstr *MI = getParent()) |
| 76 | if (MachineBasicBlock *MBB = MI->getParent()) |
| 77 | if (MachineFunction *MF = MBB->getParent()) { |
| 78 | RemoveRegOperandFromRegInfo(); |
| 79 | Contents.Reg.RegNo = Reg; |
| 80 | AddRegOperandToRegInfo(&MF->getRegInfo()); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | // Otherwise, just change the register, no problem. :) |
| 85 | Contents.Reg.RegNo = Reg; |
| 86 | } |
| 87 | |
| 88 | /// ChangeToImmediate - Replace this operand with a new immediate operand of |
| 89 | /// the specified value. If an operand is known to be an immediate already, |
| 90 | /// the setImm method should be used. |
| 91 | void MachineOperand::ChangeToImmediate(int64_t ImmVal) { |
| 92 | // If this operand is currently a register operand, and if this is in a |
| 93 | // function, deregister the operand from the register's use/def list. |
| 94 | if (isReg() && getParent() && getParent()->getParent() && |
| 95 | getParent()->getParent()->getParent()) |
| 96 | RemoveRegOperandFromRegInfo(); |
| 97 | |
| 98 | OpKind = MO_Immediate; |
| 99 | Contents.ImmVal = ImmVal; |
| 100 | } |
| 101 | |
| 102 | /// ChangeToRegister - Replace this operand with a new register operand of |
| 103 | /// the specified value. If an operand is known to be an register already, |
| 104 | /// the setReg method should be used. |
| 105 | void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp, |
| 106 | bool isKill, bool isDead) { |
| 107 | // If this operand is already a register operand, use setReg to update the |
| 108 | // register's use/def lists. |
| 109 | if (isReg()) { |
| 110 | setReg(Reg); |
| 111 | } else { |
| 112 | // Otherwise, change this to a register and set the reg#. |
| 113 | OpKind = MO_Register; |
| 114 | Contents.Reg.RegNo = Reg; |
| 115 | |
| 116 | // If this operand is embedded in a function, add the operand to the |
| 117 | // register's use/def list. |
| 118 | if (MachineInstr *MI = getParent()) |
| 119 | if (MachineBasicBlock *MBB = MI->getParent()) |
| 120 | if (MachineFunction *MF = MBB->getParent()) |
| 121 | AddRegOperandToRegInfo(&MF->getRegInfo()); |
| 122 | } |
| 123 | |
| 124 | IsDef = isDef; |
| 125 | IsImp = isImp; |
| 126 | IsKill = isKill; |
| 127 | IsDead = isDead; |
| 128 | SubReg = 0; |
| 129 | } |
| 130 | |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 131 | /// isIdenticalTo - Return true if this operand is identical to the specified |
| 132 | /// operand. |
| 133 | bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const { |
| 134 | if (getType() != Other.getType()) return false; |
| 135 | |
| 136 | switch (getType()) { |
| 137 | default: assert(0 && "Unrecognized operand type"); |
| 138 | case MachineOperand::MO_Register: |
| 139 | return getReg() == Other.getReg() && isDef() == Other.isDef() && |
| 140 | getSubReg() == Other.getSubReg(); |
| 141 | case MachineOperand::MO_Immediate: |
| 142 | return getImm() == Other.getImm(); |
Nate Begeman | e8b7ccf | 2008-02-14 07:39:30 +0000 | [diff] [blame] | 143 | case MachineOperand::MO_FPImmediate: |
| 144 | return getFPImm() == Other.getFPImm(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 145 | case MachineOperand::MO_MachineBasicBlock: |
| 146 | return getMBB() == Other.getMBB(); |
| 147 | case MachineOperand::MO_FrameIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 148 | return getIndex() == Other.getIndex(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 149 | case MachineOperand::MO_ConstantPoolIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 150 | return getIndex() == Other.getIndex() && getOffset() == Other.getOffset(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 151 | case MachineOperand::MO_JumpTableIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 152 | return getIndex() == Other.getIndex(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 153 | case MachineOperand::MO_GlobalAddress: |
| 154 | return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset(); |
| 155 | case MachineOperand::MO_ExternalSymbol: |
| 156 | return !strcmp(getSymbolName(), Other.getSymbolName()) && |
| 157 | getOffset() == Other.getOffset(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /// print - Print the specified machine operand. |
| 162 | /// |
| 163 | void MachineOperand::print(std::ostream &OS, const TargetMachine *TM) const { |
| 164 | switch (getType()) { |
| 165 | case MachineOperand::MO_Register: |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 166 | if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) { |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 167 | OS << "%reg" << getReg(); |
| 168 | } else { |
| 169 | // 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] | 170 | // target info for the instruction. |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 171 | if (TM == 0) |
| 172 | if (const MachineInstr *MI = getParent()) |
| 173 | if (const MachineBasicBlock *MBB = MI->getParent()) |
| 174 | if (const MachineFunction *MF = MBB->getParent()) |
| 175 | TM = &MF->getTarget(); |
| 176 | |
| 177 | if (TM) |
Bill Wendling | e6d088a | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 178 | OS << "%" << TM->getRegisterInfo()->get(getReg()).Name; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 179 | else |
| 180 | OS << "%mreg" << getReg(); |
| 181 | } |
| 182 | |
| 183 | if (isDef() || isKill() || isDead() || isImplicit()) { |
| 184 | OS << "<"; |
| 185 | bool NeedComma = false; |
| 186 | if (isImplicit()) { |
| 187 | OS << (isDef() ? "imp-def" : "imp-use"); |
| 188 | NeedComma = true; |
| 189 | } else if (isDef()) { |
| 190 | OS << "def"; |
| 191 | NeedComma = true; |
| 192 | } |
| 193 | if (isKill() || isDead()) { |
Bill Wendling | 181eb73 | 2008-02-24 00:56:13 +0000 | [diff] [blame] | 194 | if (NeedComma) OS << ","; |
| 195 | if (isKill()) OS << "kill"; |
| 196 | if (isDead()) OS << "dead"; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 197 | } |
| 198 | OS << ">"; |
| 199 | } |
| 200 | break; |
| 201 | case MachineOperand::MO_Immediate: |
| 202 | OS << getImm(); |
| 203 | break; |
Nate Begeman | e8b7ccf | 2008-02-14 07:39:30 +0000 | [diff] [blame] | 204 | case MachineOperand::MO_FPImmediate: |
| 205 | if (getFPImm()->getType() == Type::FloatTy) { |
| 206 | OS << getFPImm()->getValueAPF().convertToFloat(); |
| 207 | } else { |
| 208 | OS << getFPImm()->getValueAPF().convertToDouble(); |
| 209 | } |
| 210 | break; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 211 | case MachineOperand::MO_MachineBasicBlock: |
| 212 | OS << "mbb<" |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 213 | << ((Value*)getMBB()->getBasicBlock())->getName() |
| 214 | << "," << (void*)getMBB() << ">"; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 215 | break; |
| 216 | case MachineOperand::MO_FrameIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 217 | OS << "<fi#" << getIndex() << ">"; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 218 | break; |
| 219 | case MachineOperand::MO_ConstantPoolIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 220 | OS << "<cp#" << getIndex(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 221 | if (getOffset()) OS << "+" << getOffset(); |
| 222 | OS << ">"; |
| 223 | break; |
| 224 | case MachineOperand::MO_JumpTableIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 225 | OS << "<jt#" << getIndex() << ">"; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 226 | break; |
| 227 | case MachineOperand::MO_GlobalAddress: |
| 228 | OS << "<ga:" << ((Value*)getGlobal())->getName(); |
| 229 | if (getOffset()) OS << "+" << getOffset(); |
| 230 | OS << ">"; |
| 231 | break; |
| 232 | case MachineOperand::MO_ExternalSymbol: |
| 233 | OS << "<es:" << getSymbolName(); |
| 234 | if (getOffset()) OS << "+" << getOffset(); |
| 235 | OS << ">"; |
| 236 | break; |
| 237 | default: |
| 238 | assert(0 && "Unrecognized operand type"); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | //===----------------------------------------------------------------------===// |
Dan Gohman | ce42e40 | 2008-07-07 20:32:02 +0000 | [diff] [blame] | 243 | // MachineMemOperand Implementation |
| 244 | //===----------------------------------------------------------------------===// |
| 245 | |
| 246 | MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f, |
| 247 | int64_t o, uint64_t s, unsigned int a) |
| 248 | : Offset(o), Size(s), V(v), |
| 249 | Flags((f & 7) | ((Log2_32(a) + 1) << 3)) { |
Dan Gohman | f1bf29e | 2008-07-08 23:47:04 +0000 | [diff] [blame] | 250 | assert(isPowerOf2_32(a) && "Alignment is not a power of 2!"); |
Dan Gohman | c5e1f98 | 2008-07-16 15:56:42 +0000 | [diff] [blame] | 251 | assert((isLoad() || isStore()) && "Not a load/store!"); |
Dan Gohman | ce42e40 | 2008-07-07 20:32:02 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Dan Gohman | b8d2f55 | 2008-08-20 15:58:01 +0000 | [diff] [blame^] | 254 | /// Profile - Gather unique data for the object. |
| 255 | /// |
| 256 | void MachineMemOperand::Profile(FoldingSetNodeID &ID) const { |
| 257 | ID.AddInteger(Offset); |
| 258 | ID.AddInteger(Size); |
| 259 | ID.AddPointer(V); |
| 260 | ID.AddInteger(Flags); |
| 261 | } |
| 262 | |
Dan Gohman | ce42e40 | 2008-07-07 20:32:02 +0000 | [diff] [blame] | 263 | //===----------------------------------------------------------------------===// |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 264 | // MachineInstr Implementation |
| 265 | //===----------------------------------------------------------------------===// |
| 266 | |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 267 | /// MachineInstr ctor - This constructor creates a dummy MachineInstr with |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 268 | /// TID NULL and no operands. |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 269 | MachineInstr::MachineInstr() |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 270 | : TID(0), NumImplicitOps(0), Parent(0) { |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 271 | // Make sure that we get added to a machine basicblock |
| 272 | LeakDetector::addGarbageObject(this); |
Chris Lattner | 7279122 | 2002-10-28 20:59:49 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 275 | void MachineInstr::addImplicitDefUseOperands() { |
| 276 | if (TID->ImplicitDefs) |
Chris Lattner | a4161ee | 2007-12-30 00:12:25 +0000 | [diff] [blame] | 277 | for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs) |
Chris Lattner | 8019f41 | 2007-12-30 00:41:17 +0000 | [diff] [blame] | 278 | addOperand(MachineOperand::CreateReg(*ImpDefs, true, true)); |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 279 | if (TID->ImplicitUses) |
Chris Lattner | a4161ee | 2007-12-30 00:12:25 +0000 | [diff] [blame] | 280 | for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses) |
Chris Lattner | 8019f41 | 2007-12-30 00:41:17 +0000 | [diff] [blame] | 281 | addOperand(MachineOperand::CreateReg(*ImpUses, false, true)); |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /// MachineInstr ctor - This constructor create a MachineInstr and add the |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 285 | /// implicit operands. It reserves space for number of operands specified by |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 286 | /// TargetInstrDesc or the numOperands if it is not zero. (for |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 287 | /// instructions with variable number of operands). |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 288 | MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp) |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 289 | : TID(&tid), NumImplicitOps(0), Parent(0) { |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 290 | if (!NoImp && TID->getImplicitDefs()) |
| 291 | for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 292 | NumImplicitOps++; |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 293 | if (!NoImp && TID->getImplicitUses()) |
| 294 | for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 295 | NumImplicitOps++; |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 296 | Operands.reserve(NumImplicitOps + TID->getNumOperands()); |
Evan Cheng | fa94572 | 2007-10-13 02:23:01 +0000 | [diff] [blame] | 297 | if (!NoImp) |
| 298 | addImplicitDefUseOperands(); |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 299 | // Make sure that we get added to a machine basicblock |
| 300 | LeakDetector::addGarbageObject(this); |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 303 | /// MachineInstr ctor - Work exactly the same as the ctor above, except that the |
| 304 | /// MachineInstr is created and added to the end of the specified basic block. |
| 305 | /// |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 306 | MachineInstr::MachineInstr(MachineBasicBlock *MBB, |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 307 | const TargetInstrDesc &tid) |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 308 | : TID(&tid), NumImplicitOps(0), Parent(0) { |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 309 | assert(MBB && "Cannot use inserting ctor with null basic block!"); |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 310 | if (TID->ImplicitDefs) |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 311 | for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 312 | NumImplicitOps++; |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 313 | if (TID->ImplicitUses) |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 314 | for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 315 | NumImplicitOps++; |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 316 | Operands.reserve(NumImplicitOps + TID->getNumOperands()); |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 317 | addImplicitDefUseOperands(); |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 318 | // Make sure that we get added to a machine basicblock |
| 319 | LeakDetector::addGarbageObject(this); |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 320 | MBB->push_back(this); // Add instruction to end of basic block! |
| 321 | } |
| 322 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 323 | /// MachineInstr ctor - Copies MachineInstr arg exactly |
| 324 | /// |
Evan Cheng | 1ed9922 | 2008-07-19 00:37:25 +0000 | [diff] [blame] | 325 | MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI) |
| 326 | : TID(&MI.getDesc()), NumImplicitOps(0), Parent(0) { |
Chris Lattner | 943b5e1 | 2006-05-04 19:14:44 +0000 | [diff] [blame] | 327 | Operands.reserve(MI.getNumOperands()); |
Tanya Lattner | b5159ed | 2004-05-23 20:58:02 +0000 | [diff] [blame] | 328 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 329 | // Add operands |
Evan Cheng | 1ed9922 | 2008-07-19 00:37:25 +0000 | [diff] [blame] | 330 | for (unsigned i = 0; i != MI.getNumOperands(); ++i) |
| 331 | addOperand(MI.getOperand(i)); |
| 332 | NumImplicitOps = MI.NumImplicitOps; |
Tanya Lattner | 0c63e03 | 2004-05-24 03:14:18 +0000 | [diff] [blame] | 333 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 334 | // Add memory operands. |
Dan Gohman | fed90b6 | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 335 | for (std::list<MachineMemOperand>::const_iterator i = MI.memoperands_begin(), |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 336 | j = MI.memoperands_end(); i != j; ++i) |
| 337 | addMemOperand(MF, *i); |
| 338 | |
| 339 | // Set parent to null. |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 340 | Parent = 0; |
Dan Gohman | 6116a73 | 2008-07-21 18:47:29 +0000 | [diff] [blame] | 341 | |
| 342 | LeakDetector::addGarbageObject(this); |
Tanya Lattner | 466b534 | 2004-05-23 19:35:12 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 345 | MachineInstr::~MachineInstr() { |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 346 | LeakDetector::removeGarbageObject(this); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 347 | assert(MemOperands.empty() && |
| 348 | "MachineInstr being deleted with live memoperands!"); |
Chris Lattner | e12d6ab | 2007-12-30 06:11:04 +0000 | [diff] [blame] | 349 | #ifndef NDEBUG |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 350 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
Chris Lattner | e12d6ab | 2007-12-30 06:11:04 +0000 | [diff] [blame] | 351 | assert(Operands[i].ParentMI == this && "ParentMI mismatch!"); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 352 | assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) && |
| 353 | "Reg operand def/use list corrupted"); |
| 354 | } |
Chris Lattner | e12d6ab | 2007-12-30 06:11:04 +0000 | [diff] [blame] | 355 | #endif |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 358 | /// getRegInfo - If this instruction is embedded into a MachineFunction, |
| 359 | /// return the MachineRegisterInfo object for the current function, otherwise |
| 360 | /// return null. |
| 361 | MachineRegisterInfo *MachineInstr::getRegInfo() { |
| 362 | if (MachineBasicBlock *MBB = getParent()) |
Dan Gohman | 4e526b9 | 2008-07-08 23:59:09 +0000 | [diff] [blame] | 363 | return &MBB->getParent()->getRegInfo(); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in |
| 368 | /// this instruction from their respective use lists. This requires that the |
| 369 | /// operands already be on their use lists. |
| 370 | void MachineInstr::RemoveRegOperandsFromUseLists() { |
| 371 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
| 372 | if (Operands[i].isReg()) |
| 373 | Operands[i].RemoveRegOperandFromRegInfo(); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /// AddRegOperandsToUseLists - Add all of the register operands in |
| 378 | /// this instruction from their respective use lists. This requires that the |
| 379 | /// operands not be on their use lists yet. |
| 380 | void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) { |
| 381 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
| 382 | if (Operands[i].isReg()) |
| 383 | Operands[i].AddRegOperandToRegInfo(&RegInfo); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | |
| 388 | /// addOperand - Add the specified operand to the instruction. If it is an |
| 389 | /// implicit operand, it is added to the end of the operand list. If it is |
| 390 | /// an explicit operand it is added at the end of the explicit operand list |
| 391 | /// (before the first implicit operand). |
| 392 | void MachineInstr::addOperand(const MachineOperand &Op) { |
| 393 | bool isImpReg = Op.isReg() && Op.isImplicit(); |
| 394 | assert((isImpReg || !OperandsComplete()) && |
| 395 | "Trying to add an operand to a machine instr that is already done!"); |
| 396 | |
| 397 | // If we are adding the operand to the end of the list, our job is simpler. |
| 398 | // This is true most of the time, so this is a reasonable optimization. |
| 399 | if (isImpReg || NumImplicitOps == 0) { |
| 400 | // We can only do this optimization if we know that the operand list won't |
| 401 | // reallocate. |
| 402 | if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) { |
| 403 | Operands.push_back(Op); |
| 404 | |
| 405 | // Set the parent of the operand. |
| 406 | Operands.back().ParentMI = this; |
| 407 | |
| 408 | // If the operand is a register, update the operand's use list. |
| 409 | if (Op.isReg()) |
| 410 | Operands.back().AddRegOperandToRegInfo(getRegInfo()); |
| 411 | return; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | // Otherwise, we have to insert a real operand before any implicit ones. |
| 416 | unsigned OpNo = Operands.size()-NumImplicitOps; |
| 417 | |
| 418 | MachineRegisterInfo *RegInfo = getRegInfo(); |
| 419 | |
| 420 | // If this instruction isn't embedded into a function, then we don't need to |
| 421 | // update any operand lists. |
| 422 | if (RegInfo == 0) { |
| 423 | // Simple insertion, no reginfo update needed for other register operands. |
| 424 | Operands.insert(Operands.begin()+OpNo, Op); |
| 425 | Operands[OpNo].ParentMI = this; |
| 426 | |
| 427 | // Do explicitly set the reginfo for this operand though, to ensure the |
| 428 | // next/prev fields are properly nulled out. |
| 429 | if (Operands[OpNo].isReg()) |
| 430 | Operands[OpNo].AddRegOperandToRegInfo(0); |
| 431 | |
| 432 | } else if (Operands.size()+1 <= Operands.capacity()) { |
| 433 | // Otherwise, we have to remove register operands from their register use |
| 434 | // list, add the operand, then add the register operands back to their use |
| 435 | // list. This also must handle the case when the operand list reallocates |
| 436 | // to somewhere else. |
| 437 | |
| 438 | // If insertion of this operand won't cause reallocation of the operand |
| 439 | // list, just remove the implicit operands, add the operand, then re-add all |
| 440 | // the rest of the operands. |
| 441 | for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { |
| 442 | assert(Operands[i].isReg() && "Should only be an implicit reg!"); |
| 443 | Operands[i].RemoveRegOperandFromRegInfo(); |
| 444 | } |
| 445 | |
| 446 | // Add the operand. If it is a register, add it to the reg list. |
| 447 | Operands.insert(Operands.begin()+OpNo, Op); |
| 448 | Operands[OpNo].ParentMI = this; |
| 449 | |
| 450 | if (Operands[OpNo].isReg()) |
| 451 | Operands[OpNo].AddRegOperandToRegInfo(RegInfo); |
| 452 | |
| 453 | // Re-add all the implicit ops. |
| 454 | for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) { |
| 455 | assert(Operands[i].isReg() && "Should only be an implicit reg!"); |
| 456 | Operands[i].AddRegOperandToRegInfo(RegInfo); |
| 457 | } |
| 458 | } else { |
| 459 | // Otherwise, we will be reallocating the operand list. Remove all reg |
| 460 | // operands from their list, then readd them after the operand list is |
| 461 | // reallocated. |
| 462 | RemoveRegOperandsFromUseLists(); |
| 463 | |
| 464 | Operands.insert(Operands.begin()+OpNo, Op); |
| 465 | Operands[OpNo].ParentMI = this; |
| 466 | |
| 467 | // Re-add all the operands. |
| 468 | AddRegOperandsToUseLists(*RegInfo); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | /// RemoveOperand - Erase an operand from an instruction, leaving it with one |
| 473 | /// fewer operand than it started with. |
| 474 | /// |
| 475 | void MachineInstr::RemoveOperand(unsigned OpNo) { |
| 476 | assert(OpNo < Operands.size() && "Invalid operand number"); |
| 477 | |
| 478 | // Special case removing the last one. |
| 479 | if (OpNo == Operands.size()-1) { |
| 480 | // If needed, remove from the reg def/use list. |
| 481 | if (Operands.back().isReg() && Operands.back().isOnRegUseList()) |
| 482 | Operands.back().RemoveRegOperandFromRegInfo(); |
| 483 | |
| 484 | Operands.pop_back(); |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | // Otherwise, we are removing an interior operand. If we have reginfo to |
| 489 | // update, remove all operands that will be shifted down from their reg lists, |
| 490 | // move everything down, then re-add them. |
| 491 | MachineRegisterInfo *RegInfo = getRegInfo(); |
| 492 | if (RegInfo) { |
| 493 | for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { |
| 494 | if (Operands[i].isReg()) |
| 495 | Operands[i].RemoveRegOperandFromRegInfo(); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | Operands.erase(Operands.begin()+OpNo); |
| 500 | |
| 501 | if (RegInfo) { |
| 502 | for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { |
| 503 | if (Operands[i].isReg()) |
| 504 | Operands[i].AddRegOperandToRegInfo(RegInfo); |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 509 | /// addMemOperand - Add a MachineMemOperand to the machine instruction, |
| 510 | /// referencing arbitrary storage. |
| 511 | void MachineInstr::addMemOperand(MachineFunction &MF, |
| 512 | const MachineMemOperand &MO) { |
Dan Gohman | fed90b6 | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 513 | MemOperands.push_back(MO); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | /// clearMemOperands - Erase all of this MachineInstr's MachineMemOperands. |
| 517 | void MachineInstr::clearMemOperands(MachineFunction &MF) { |
Dan Gohman | fed90b6 | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 518 | MemOperands.clear(); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 521 | |
Chris Lattner | 48d7c06 | 2006-04-17 21:35:41 +0000 | [diff] [blame] | 522 | /// removeFromParent - This method unlinks 'this' from the containing basic |
| 523 | /// block, and returns it, but does not delete it. |
| 524 | MachineInstr *MachineInstr::removeFromParent() { |
| 525 | assert(getParent() && "Not embedded in a basic block!"); |
| 526 | getParent()->remove(this); |
| 527 | return this; |
| 528 | } |
| 529 | |
| 530 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 531 | /// eraseFromParent - This method unlinks 'this' from the containing basic |
| 532 | /// block, and deletes it. |
| 533 | void MachineInstr::eraseFromParent() { |
| 534 | assert(getParent() && "Not embedded in a basic block!"); |
| 535 | getParent()->erase(this); |
| 536 | } |
| 537 | |
| 538 | |
Brian Gaeke | 21326fc | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 539 | /// OperandComplete - Return true if it's illegal to add a new operand |
| 540 | /// |
Chris Lattner | 2a90ba6 | 2004-02-12 16:09:53 +0000 | [diff] [blame] | 541 | bool MachineInstr::OperandsComplete() const { |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 542 | unsigned short NumOperands = TID->getNumOperands(); |
Chris Lattner | 8f707e1 | 2008-01-07 05:19:29 +0000 | [diff] [blame] | 543 | if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands) |
Vikram S. Adve | 3497782 | 2003-05-31 07:39:06 +0000 | [diff] [blame] | 544 | return true; // Broken: we have all the operands of this instruction! |
Chris Lattner | 413746e | 2002-10-28 20:48:39 +0000 | [diff] [blame] | 545 | return false; |
| 546 | } |
| 547 | |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 548 | /// getNumExplicitOperands - Returns the number of non-implicit operands. |
| 549 | /// |
| 550 | unsigned MachineInstr::getNumExplicitOperands() const { |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 551 | unsigned NumOperands = TID->getNumOperands(); |
Chris Lattner | 8f707e1 | 2008-01-07 05:19:29 +0000 | [diff] [blame] | 552 | if (!TID->isVariadic()) |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 553 | return NumOperands; |
| 554 | |
| 555 | for (unsigned e = getNumOperands(); NumOperands != e; ++NumOperands) { |
| 556 | const MachineOperand &MO = getOperand(NumOperands); |
| 557 | if (!MO.isRegister() || !MO.isImplicit()) |
| 558 | NumOperands++; |
| 559 | } |
| 560 | return NumOperands; |
| 561 | } |
| 562 | |
Chris Lattner | 8ace2cd | 2006-10-20 22:39:59 +0000 | [diff] [blame] | 563 | |
Dan Gohman | 4406604 | 2008-07-01 00:05:16 +0000 | [diff] [blame] | 564 | /// isLabel - Returns true if the MachineInstr represents a label. |
| 565 | /// |
| 566 | bool MachineInstr::isLabel() const { |
| 567 | return getOpcode() == TargetInstrInfo::DBG_LABEL || |
| 568 | getOpcode() == TargetInstrInfo::EH_LABEL || |
| 569 | getOpcode() == TargetInstrInfo::GC_LABEL; |
| 570 | } |
| 571 | |
Evan Cheng | bb81d97 | 2008-01-31 09:59:15 +0000 | [diff] [blame] | 572 | /// isDebugLabel - Returns true if the MachineInstr represents a debug label. |
| 573 | /// |
| 574 | bool MachineInstr::isDebugLabel() const { |
Dan Gohman | 4406604 | 2008-07-01 00:05:16 +0000 | [diff] [blame] | 575 | return getOpcode() == TargetInstrInfo::DBG_LABEL; |
Evan Cheng | bb81d97 | 2008-01-31 09:59:15 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Evan Cheng | faa5107 | 2007-04-26 19:00:32 +0000 | [diff] [blame] | 578 | /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of |
Evan Cheng | 32eb1f1 | 2007-03-26 22:37:45 +0000 | [diff] [blame] | 579 | /// 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] | 580 | /// the search criteria to a use that kills the register if isKill is true. |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 581 | int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill, |
| 582 | const TargetRegisterInfo *TRI) const { |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 583 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 584 | const MachineOperand &MO = getOperand(i); |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 585 | if (!MO.isRegister() || !MO.isUse()) |
| 586 | continue; |
| 587 | unsigned MOReg = MO.getReg(); |
| 588 | if (!MOReg) |
| 589 | continue; |
| 590 | if (MOReg == Reg || |
| 591 | (TRI && |
| 592 | TargetRegisterInfo::isPhysicalRegister(MOReg) && |
| 593 | TargetRegisterInfo::isPhysicalRegister(Reg) && |
| 594 | TRI->isSubRegister(MOReg, Reg))) |
Evan Cheng | 76d7e76 | 2007-02-23 01:04:26 +0000 | [diff] [blame] | 595 | if (!isKill || MO.isKill()) |
Evan Cheng | 32eb1f1 | 2007-03-26 22:37:45 +0000 | [diff] [blame] | 596 | return i; |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 597 | } |
Evan Cheng | 32eb1f1 | 2007-03-26 22:37:45 +0000 | [diff] [blame] | 598 | return -1; |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 601 | /// findRegisterDefOperandIdx() - Returns the operand index that is a def of |
Dan Gohman | 703bfe6 | 2008-05-06 00:20:10 +0000 | [diff] [blame] | 602 | /// the specified register or -1 if it is not found. If isDead is true, defs |
| 603 | /// that are not dead are skipped. If TargetRegisterInfo is non-null, then it |
| 604 | /// also checks if there is a def of a super-register. |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 605 | int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, |
| 606 | const TargetRegisterInfo *TRI) const { |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 607 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 608 | const MachineOperand &MO = getOperand(i); |
| 609 | if (!MO.isRegister() || !MO.isDef()) |
| 610 | continue; |
| 611 | unsigned MOReg = MO.getReg(); |
| 612 | if (MOReg == Reg || |
| 613 | (TRI && |
| 614 | TargetRegisterInfo::isPhysicalRegister(MOReg) && |
| 615 | TargetRegisterInfo::isPhysicalRegister(Reg) && |
| 616 | TRI->isSubRegister(MOReg, Reg))) |
| 617 | if (!isDead || MO.isDead()) |
| 618 | return i; |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 619 | } |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 620 | return -1; |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 621 | } |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 622 | |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 623 | /// findFirstPredOperandIdx() - Find the index of the first operand in the |
| 624 | /// operand list that is used to represent the predicate. It returns -1 if |
| 625 | /// none is found. |
| 626 | int MachineInstr::findFirstPredOperandIdx() const { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 627 | const TargetInstrDesc &TID = getDesc(); |
| 628 | if (TID.isPredicable()) { |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 629 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 630 | if (TID.OpInfo[i].isPredicate()) |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 631 | return i; |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 634 | return -1; |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 635 | } |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 636 | |
Evan Cheng | ef0732d | 2008-07-10 07:35:43 +0000 | [diff] [blame] | 637 | /// isRegReDefinedByTwoAddr - Given the defined register and the operand index, |
| 638 | /// check if the register def is a re-definition due to two addr elimination. |
| 639 | bool MachineInstr::isRegReDefinedByTwoAddr(unsigned Reg, unsigned DefIdx) const{ |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 640 | const TargetInstrDesc &TID = getDesc(); |
Evan Cheng | ef0732d | 2008-07-10 07:35:43 +0000 | [diff] [blame] | 641 | for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) { |
| 642 | const MachineOperand &MO = getOperand(i); |
| 643 | if (MO.isRegister() && MO.isUse() && MO.getReg() == Reg && |
| 644 | TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefIdx) |
| 645 | return true; |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 646 | } |
| 647 | return false; |
| 648 | } |
| 649 | |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 650 | /// copyKillDeadInfo - Copies kill / dead operand properties from MI. |
| 651 | /// |
| 652 | void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) { |
| 653 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 654 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | 92dfe20 | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 655 | if (!MO.isRegister() || (!MO.isKill() && !MO.isDead())) |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 656 | continue; |
| 657 | for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) { |
| 658 | MachineOperand &MOp = getOperand(j); |
| 659 | if (!MOp.isIdenticalTo(MO)) |
| 660 | continue; |
| 661 | if (MO.isKill()) |
| 662 | MOp.setIsKill(); |
| 663 | else |
| 664 | MOp.setIsDead(); |
| 665 | break; |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 670 | /// copyPredicates - Copies predicate operand(s) from MI. |
| 671 | void MachineInstr::copyPredicates(const MachineInstr *MI) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 672 | const TargetInstrDesc &TID = MI->getDesc(); |
Evan Cheng | b27087f | 2008-03-13 00:44:09 +0000 | [diff] [blame] | 673 | if (!TID.isPredicable()) |
| 674 | return; |
| 675 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 676 | if (TID.OpInfo[i].isPredicate()) { |
| 677 | // Predicated operands must be last operands. |
| 678 | addOperand(MI->getOperand(i)); |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | } |
| 682 | |
Evan Cheng | 9f1c831 | 2008-07-03 09:09:37 +0000 | [diff] [blame] | 683 | /// isSafeToMove - Return true if it is safe to move this instruction. If |
| 684 | /// SawStore is set to true, it means that there is a store (or call) between |
| 685 | /// the instruction's location and its intended destination. |
Evan Cheng | b27087f | 2008-03-13 00:44:09 +0000 | [diff] [blame] | 686 | bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII, bool &SawStore) { |
| 687 | // Ignore stuff that we obviously can't move. |
| 688 | if (TID->mayStore() || TID->isCall()) { |
| 689 | SawStore = true; |
| 690 | return false; |
| 691 | } |
| 692 | if (TID->isReturn() || TID->isBranch() || TID->hasUnmodeledSideEffects()) |
| 693 | return false; |
| 694 | |
| 695 | // See if this instruction does a load. If so, we have to guarantee that the |
| 696 | // loaded value doesn't change between the load and the its intended |
| 697 | // destination. The check for isInvariantLoad gives the targe the chance to |
| 698 | // classify the load as always returning a constant, e.g. a constant pool |
| 699 | // load. |
| 700 | if (TID->mayLoad() && !TII->isInvariantLoad(this)) { |
| 701 | // Otherwise, this is a real load. If there is a store between the load and |
| 702 | // end of block, we can't sink the load. |
| 703 | // |
| 704 | // FIXME: we can't do this transformation until we know that the load is |
| 705 | // not volatile, and machineinstrs don't keep this info. :( |
| 706 | // |
| 707 | //if (SawStore) |
| 708 | return false; |
| 709 | } |
| 710 | return true; |
| 711 | } |
| 712 | |
Brian Gaeke | 21326fc | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 713 | void MachineInstr::dump() const { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 714 | cerr << " " << *this; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Tanya Lattner | b140762 | 2004-06-25 00:13:11 +0000 | [diff] [blame] | 717 | void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const { |
Chris Lattner | e308789 | 2007-12-30 21:31:53 +0000 | [diff] [blame] | 718 | // Specialize printing if op#0 is definition |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 719 | unsigned StartOp = 0; |
Dan Gohman | 92dfe20 | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 720 | if (getNumOperands() && getOperand(0).isRegister() && getOperand(0).isDef()) { |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 721 | getOperand(0).print(OS, TM); |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 722 | OS << " = "; |
| 723 | ++StartOp; // Don't print this operand again! |
| 724 | } |
Tanya Lattner | b140762 | 2004-06-25 00:13:11 +0000 | [diff] [blame] | 725 | |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 726 | OS << getDesc().getName(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 727 | |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 728 | for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) { |
| 729 | if (i != StartOp) |
| 730 | OS << ","; |
| 731 | OS << " "; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 732 | getOperand(i).print(OS, TM); |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 733 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 734 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 735 | if (!memoperands_empty()) { |
Dan Gohman | 2bfe6ff | 2008-02-07 16:18:00 +0000 | [diff] [blame] | 736 | OS << ", Mem:"; |
Dan Gohman | fed90b6 | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 737 | for (std::list<MachineMemOperand>::const_iterator i = memoperands_begin(), |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 738 | e = memoperands_end(); i != e; ++i) { |
| 739 | const MachineMemOperand &MRO = *i; |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 740 | const Value *V = MRO.getValue(); |
| 741 | |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 742 | assert((MRO.isLoad() || MRO.isStore()) && |
| 743 | "SV has to be a load, store or both."); |
| 744 | |
| 745 | if (MRO.isVolatile()) |
| 746 | OS << "Volatile "; |
Dan Gohman | 2bfe6ff | 2008-02-07 16:18:00 +0000 | [diff] [blame] | 747 | |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 748 | if (MRO.isLoad()) |
Dan Gohman | 2bfe6ff | 2008-02-07 16:18:00 +0000 | [diff] [blame] | 749 | OS << "LD"; |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 750 | if (MRO.isStore()) |
Dan Gohman | 2bfe6ff | 2008-02-07 16:18:00 +0000 | [diff] [blame] | 751 | OS << "ST"; |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 752 | |
Evan Cheng | bbd8322 | 2008-02-08 22:05:07 +0000 | [diff] [blame] | 753 | OS << "(" << MRO.getSize() << "," << MRO.getAlignment() << ") ["; |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 754 | |
Dan Gohman | 2bfe6ff | 2008-02-07 16:18:00 +0000 | [diff] [blame] | 755 | if (!V) |
| 756 | OS << "<unknown>"; |
| 757 | else if (!V->getName().empty()) |
| 758 | OS << V->getName(); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 759 | else if (isa<PseudoSourceValue>(V)) |
Dan Gohman | 2bfe6ff | 2008-02-07 16:18:00 +0000 | [diff] [blame] | 760 | OS << *V; |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 761 | else |
Dan Gohman | 2bfe6ff | 2008-02-07 16:18:00 +0000 | [diff] [blame] | 762 | OS << V; |
| 763 | |
| 764 | OS << " + " << MRO.getOffset() << "]"; |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 765 | } |
| 766 | } |
| 767 | |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 768 | OS << "\n"; |
| 769 | } |
| 770 | |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 771 | bool MachineInstr::addRegisterKilled(unsigned IncomingReg, |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 772 | const TargetRegisterInfo *RegInfo, |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 773 | bool AddIfNotFound) { |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 774 | bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); |
Dan Gohman | 2ebc11a | 2008-07-03 01:18:51 +0000 | [diff] [blame] | 775 | bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg); |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 776 | SmallVector<unsigned,4> DeadOps; |
Bill Wendling | 4a23d72 | 2008-03-03 22:14:33 +0000 | [diff] [blame] | 777 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 778 | MachineOperand &MO = getOperand(i); |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 779 | if (!MO.isRegister() || !MO.isUse()) |
| 780 | continue; |
| 781 | unsigned Reg = MO.getReg(); |
| 782 | if (!Reg) |
| 783 | continue; |
Bill Wendling | 4a23d72 | 2008-03-03 22:14:33 +0000 | [diff] [blame] | 784 | |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 785 | if (Reg == IncomingReg) { |
Dan Gohman | 2ebc11a | 2008-07-03 01:18:51 +0000 | [diff] [blame] | 786 | MO.setIsKill(); |
| 787 | return true; |
| 788 | } |
| 789 | if (hasAliases && MO.isKill() && |
| 790 | TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 791 | // A super-register kill already exists. |
| 792 | if (RegInfo->isSuperRegister(IncomingReg, Reg)) |
Dan Gohman | 2ebc11a | 2008-07-03 01:18:51 +0000 | [diff] [blame] | 793 | return true; |
| 794 | if (RegInfo->isSubRegister(IncomingReg, Reg)) |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 795 | DeadOps.push_back(i); |
Bill Wendling | 4a23d72 | 2008-03-03 22:14:33 +0000 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 799 | // Trim unneeded kill operands. |
| 800 | while (!DeadOps.empty()) { |
| 801 | unsigned OpIdx = DeadOps.back(); |
| 802 | if (getOperand(OpIdx).isImplicit()) |
| 803 | RemoveOperand(OpIdx); |
| 804 | else |
| 805 | getOperand(OpIdx).setIsKill(false); |
| 806 | DeadOps.pop_back(); |
| 807 | } |
| 808 | |
Bill Wendling | 4a23d72 | 2008-03-03 22:14:33 +0000 | [diff] [blame] | 809 | // If not found, this means an alias of one of the operands is killed. Add a |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 810 | // new implicit operand if required. |
Dan Gohman | 2ebc11a | 2008-07-03 01:18:51 +0000 | [diff] [blame] | 811 | if (AddIfNotFound) { |
Bill Wendling | 4a23d72 | 2008-03-03 22:14:33 +0000 | [diff] [blame] | 812 | addOperand(MachineOperand::CreateReg(IncomingReg, |
| 813 | false /*IsDef*/, |
| 814 | true /*IsImp*/, |
| 815 | true /*IsKill*/)); |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 816 | return true; |
| 817 | } |
Dan Gohman | 2ebc11a | 2008-07-03 01:18:51 +0000 | [diff] [blame] | 818 | return false; |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | bool MachineInstr::addRegisterDead(unsigned IncomingReg, |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 822 | const TargetRegisterInfo *RegInfo, |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 823 | bool AddIfNotFound) { |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 824 | bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); |
Evan Cheng | 01b2e23 | 2008-06-27 22:11:49 +0000 | [diff] [blame] | 825 | bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg); |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 826 | SmallVector<unsigned,4> DeadOps; |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 827 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 828 | MachineOperand &MO = getOperand(i); |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 829 | if (!MO.isRegister() || !MO.isDef()) |
| 830 | continue; |
| 831 | unsigned Reg = MO.getReg(); |
| 832 | if (Reg == IncomingReg) { |
| 833 | MO.setIsDead(); |
Dan Gohman | 2ebc11a | 2008-07-03 01:18:51 +0000 | [diff] [blame] | 834 | return true; |
| 835 | } |
| 836 | if (hasAliases && MO.isDead() && |
| 837 | TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 838 | // There exists a super-register that's marked dead. |
| 839 | if (RegInfo->isSuperRegister(IncomingReg, Reg)) |
Dan Gohman | 2ebc11a | 2008-07-03 01:18:51 +0000 | [diff] [blame] | 840 | return true; |
Owen Anderson | 22ae999 | 2008-08-14 18:34:18 +0000 | [diff] [blame] | 841 | if (RegInfo->getSubRegisters(IncomingReg) && |
| 842 | RegInfo->getSuperRegisters(Reg) && |
| 843 | RegInfo->isSubRegister(IncomingReg, Reg)) |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 844 | DeadOps.push_back(i); |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 845 | } |
| 846 | } |
| 847 | |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 848 | // Trim unneeded dead operands. |
| 849 | while (!DeadOps.empty()) { |
| 850 | unsigned OpIdx = DeadOps.back(); |
| 851 | if (getOperand(OpIdx).isImplicit()) |
| 852 | RemoveOperand(OpIdx); |
| 853 | else |
| 854 | getOperand(OpIdx).setIsDead(false); |
| 855 | DeadOps.pop_back(); |
| 856 | } |
| 857 | |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 858 | // If not found, this means an alias of one of the operand is dead. Add a |
| 859 | // new implicit operand. |
Dan Gohman | 2ebc11a | 2008-07-03 01:18:51 +0000 | [diff] [blame] | 860 | if (AddIfNotFound) { |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 861 | addOperand(MachineOperand::CreateReg(IncomingReg, true/*IsDef*/, |
| 862 | true/*IsImp*/,false/*IsKill*/, |
| 863 | true/*IsDead*/)); |
| 864 | return true; |
| 865 | } |
Dan Gohman | 2ebc11a | 2008-07-03 01:18:51 +0000 | [diff] [blame] | 866 | return false; |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 867 | } |