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" |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
| 16 | #include "llvm/InlineAsm.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 17 | #include "llvm/Value.h" |
Chris Lattner | 8517e1f | 2004-02-19 16:17:08 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetMachine.h" |
Evan Cheng | bb81d97 | 2008-01-31 09:59:15 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | f14cf85 | 2008-01-07 07:42:25 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetInstrDesc.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetRegisterInfo.h" |
Argyrios Kyrtzidis | a26eae6 | 2009-04-30 23:22:31 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/DebugInfo.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 27 | #include "llvm/Support/LeakDetector.h" |
Dan Gohman | ce42e40 | 2008-07-07 20:32:02 +0000 | [diff] [blame] | 28 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | edfb72c | 2008-08-24 20:37:32 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | b8d2f55 | 2008-08-20 15:58:01 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/FoldingSet.h" |
Chris Lattner | 0742b59 | 2004-02-23 18:38:20 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 32 | |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
| 34 | // MachineOperand Implementation |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 37 | /// AddRegOperandToRegInfo - Add this register operand to the specified |
| 38 | /// MachineRegisterInfo. If it is null, then the next/prev fields should be |
| 39 | /// explicitly nulled out. |
| 40 | void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 41 | assert(isReg() && "Can only add reg operand to use lists"); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 42 | |
| 43 | // If the reginfo pointer is null, just explicitly null out or next/prev |
| 44 | // pointers, to ensure they are not garbage. |
| 45 | if (RegInfo == 0) { |
| 46 | Contents.Reg.Prev = 0; |
| 47 | Contents.Reg.Next = 0; |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | // Otherwise, add this operand to the head of the registers use/def list. |
Chris Lattner | 80fe531 | 2008-01-01 21:08:22 +0000 | [diff] [blame] | 52 | MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg()); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 80fe531 | 2008-01-01 21:08:22 +0000 | [diff] [blame] | 54 | // For SSA values, we prefer to keep the definition at the start of the list. |
| 55 | // we do this by skipping over the definition if it is at the head of the |
| 56 | // list. |
| 57 | if (*Head && (*Head)->isDef()) |
| 58 | Head = &(*Head)->Contents.Reg.Next; |
| 59 | |
| 60 | Contents.Reg.Next = *Head; |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 61 | if (Contents.Reg.Next) { |
| 62 | assert(getReg() == Contents.Reg.Next->getReg() && |
| 63 | "Different regs on the same list!"); |
| 64 | Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next; |
| 65 | } |
| 66 | |
Chris Lattner | 80fe531 | 2008-01-01 21:08:22 +0000 | [diff] [blame] | 67 | Contents.Reg.Prev = Head; |
| 68 | *Head = this; |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Dan Gohman | 3bc1a37 | 2009-04-15 01:17:37 +0000 | [diff] [blame] | 71 | /// RemoveRegOperandFromRegInfo - Remove this register operand from the |
| 72 | /// MachineRegisterInfo it is linked with. |
| 73 | void MachineOperand::RemoveRegOperandFromRegInfo() { |
| 74 | assert(isOnRegUseList() && "Reg operand is not on a use list"); |
| 75 | // Unlink this from the doubly linked list of operands. |
| 76 | MachineOperand *NextOp = Contents.Reg.Next; |
| 77 | *Contents.Reg.Prev = NextOp; |
| 78 | if (NextOp) { |
| 79 | assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!"); |
| 80 | NextOp->Contents.Reg.Prev = Contents.Reg.Prev; |
| 81 | } |
| 82 | Contents.Reg.Prev = 0; |
| 83 | Contents.Reg.Next = 0; |
| 84 | } |
| 85 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 86 | void MachineOperand::setReg(unsigned Reg) { |
| 87 | if (getReg() == Reg) return; // No change. |
| 88 | |
| 89 | // Otherwise, we have to change the register. If this operand is embedded |
| 90 | // into a machine function, we need to update the old and new register's |
| 91 | // use/def lists. |
| 92 | if (MachineInstr *MI = getParent()) |
| 93 | if (MachineBasicBlock *MBB = MI->getParent()) |
| 94 | if (MachineFunction *MF = MBB->getParent()) { |
| 95 | RemoveRegOperandFromRegInfo(); |
| 96 | Contents.Reg.RegNo = Reg; |
| 97 | AddRegOperandToRegInfo(&MF->getRegInfo()); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | // Otherwise, just change the register, no problem. :) |
| 102 | Contents.Reg.RegNo = Reg; |
| 103 | } |
| 104 | |
| 105 | /// ChangeToImmediate - Replace this operand with a new immediate operand of |
| 106 | /// the specified value. If an operand is known to be an immediate already, |
| 107 | /// the setImm method should be used. |
| 108 | void MachineOperand::ChangeToImmediate(int64_t ImmVal) { |
| 109 | // If this operand is currently a register operand, and if this is in a |
| 110 | // function, deregister the operand from the register's use/def list. |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 111 | if (isReg() && getParent() && getParent()->getParent() && |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 112 | getParent()->getParent()->getParent()) |
| 113 | RemoveRegOperandFromRegInfo(); |
| 114 | |
| 115 | OpKind = MO_Immediate; |
| 116 | Contents.ImmVal = ImmVal; |
| 117 | } |
| 118 | |
| 119 | /// ChangeToRegister - Replace this operand with a new register operand of |
| 120 | /// the specified value. If an operand is known to be an register already, |
| 121 | /// the setReg method should be used. |
| 122 | void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp, |
Evan Cheng | 4784f1f | 2009-06-30 08:49:04 +0000 | [diff] [blame] | 123 | bool isKill, bool isDead, bool isUndef) { |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 124 | // If this operand is already a register operand, use setReg to update the |
| 125 | // register's use/def lists. |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 126 | if (isReg()) { |
Dale Johannesen | e009180 | 2008-09-14 01:44:36 +0000 | [diff] [blame] | 127 | assert(!isEarlyClobber()); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 128 | setReg(Reg); |
| 129 | } else { |
| 130 | // Otherwise, change this to a register and set the reg#. |
| 131 | OpKind = MO_Register; |
| 132 | Contents.Reg.RegNo = Reg; |
| 133 | |
| 134 | // If this operand is embedded in a function, add the operand to the |
| 135 | // register's use/def list. |
| 136 | if (MachineInstr *MI = getParent()) |
| 137 | if (MachineBasicBlock *MBB = MI->getParent()) |
| 138 | if (MachineFunction *MF = MBB->getParent()) |
| 139 | AddRegOperandToRegInfo(&MF->getRegInfo()); |
| 140 | } |
| 141 | |
| 142 | IsDef = isDef; |
| 143 | IsImp = isImp; |
| 144 | IsKill = isKill; |
| 145 | IsDead = isDead; |
Evan Cheng | 4784f1f | 2009-06-30 08:49:04 +0000 | [diff] [blame] | 146 | IsUndef = isUndef; |
Dale Johannesen | e009180 | 2008-09-14 01:44:36 +0000 | [diff] [blame] | 147 | IsEarlyClobber = false; |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 148 | SubReg = 0; |
| 149 | } |
| 150 | |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 151 | /// isIdenticalTo - Return true if this operand is identical to the specified |
| 152 | /// operand. |
| 153 | bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const { |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 154 | if (getType() != Other.getType() || |
| 155 | getTargetFlags() != Other.getTargetFlags()) |
| 156 | return false; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 157 | |
| 158 | switch (getType()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 159 | default: llvm_unreachable("Unrecognized operand type"); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 160 | case MachineOperand::MO_Register: |
| 161 | return getReg() == Other.getReg() && isDef() == Other.isDef() && |
| 162 | getSubReg() == Other.getSubReg(); |
| 163 | case MachineOperand::MO_Immediate: |
| 164 | return getImm() == Other.getImm(); |
Nate Begeman | e8b7ccf | 2008-02-14 07:39:30 +0000 | [diff] [blame] | 165 | case MachineOperand::MO_FPImmediate: |
| 166 | return getFPImm() == Other.getFPImm(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 167 | case MachineOperand::MO_MachineBasicBlock: |
| 168 | return getMBB() == Other.getMBB(); |
| 169 | case MachineOperand::MO_FrameIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 170 | return getIndex() == Other.getIndex(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 171 | case MachineOperand::MO_ConstantPoolIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 172 | return getIndex() == Other.getIndex() && getOffset() == Other.getOffset(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 173 | case MachineOperand::MO_JumpTableIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 174 | return getIndex() == Other.getIndex(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 175 | case MachineOperand::MO_GlobalAddress: |
| 176 | return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset(); |
| 177 | case MachineOperand::MO_ExternalSymbol: |
| 178 | return !strcmp(getSymbolName(), Other.getSymbolName()) && |
| 179 | getOffset() == Other.getOffset(); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /// print - Print the specified machine operand. |
| 184 | /// |
Mon P Wang | 5ca6bd1 | 2008-10-10 01:43:55 +0000 | [diff] [blame] | 185 | void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const { |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 186 | switch (getType()) { |
| 187 | case MachineOperand::MO_Register: |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 188 | if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) { |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 189 | OS << "%reg" << getReg(); |
| 190 | } else { |
| 191 | // 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] | 192 | // target info for the instruction. |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 193 | if (TM == 0) |
| 194 | if (const MachineInstr *MI = getParent()) |
| 195 | if (const MachineBasicBlock *MBB = MI->getParent()) |
| 196 | if (const MachineFunction *MF = MBB->getParent()) |
| 197 | TM = &MF->getTarget(); |
| 198 | |
| 199 | if (TM) |
Bill Wendling | e6d088a | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 200 | OS << "%" << TM->getRegisterInfo()->get(getReg()).Name; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 201 | else |
| 202 | OS << "%mreg" << getReg(); |
| 203 | } |
Dan Gohman | 2ccc839 | 2008-12-18 21:51:27 +0000 | [diff] [blame] | 204 | |
Evan Cheng | 4784f1f | 2009-06-30 08:49:04 +0000 | [diff] [blame] | 205 | if (getSubReg() != 0) |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 206 | OS << ':' << getSubReg(); |
Dan Gohman | 2ccc839 | 2008-12-18 21:51:27 +0000 | [diff] [blame] | 207 | |
Evan Cheng | 4784f1f | 2009-06-30 08:49:04 +0000 | [diff] [blame] | 208 | if (isDef() || isKill() || isDead() || isImplicit() || isUndef() || |
| 209 | isEarlyClobber()) { |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 210 | OS << '<'; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 211 | bool NeedComma = false; |
| 212 | if (isImplicit()) { |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 213 | if (NeedComma) OS << ','; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 214 | OS << (isDef() ? "imp-def" : "imp-use"); |
| 215 | NeedComma = true; |
| 216 | } else if (isDef()) { |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 217 | if (NeedComma) OS << ','; |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 218 | if (isEarlyClobber()) |
| 219 | OS << "earlyclobber,"; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 220 | OS << "def"; |
| 221 | NeedComma = true; |
| 222 | } |
Evan Cheng | 4784f1f | 2009-06-30 08:49:04 +0000 | [diff] [blame] | 223 | if (isKill() || isDead() || isUndef()) { |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 224 | if (NeedComma) OS << ','; |
Bill Wendling | 181eb73 | 2008-02-24 00:56:13 +0000 | [diff] [blame] | 225 | if (isKill()) OS << "kill"; |
| 226 | if (isDead()) OS << "dead"; |
Evan Cheng | 4784f1f | 2009-06-30 08:49:04 +0000 | [diff] [blame] | 227 | if (isUndef()) { |
| 228 | if (isKill() || isDead()) |
| 229 | OS << ','; |
| 230 | OS << "undef"; |
| 231 | } |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 232 | } |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 233 | OS << '>'; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 234 | } |
| 235 | break; |
| 236 | case MachineOperand::MO_Immediate: |
| 237 | OS << getImm(); |
| 238 | break; |
Nate Begeman | e8b7ccf | 2008-02-14 07:39:30 +0000 | [diff] [blame] | 239 | case MachineOperand::MO_FPImmediate: |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 240 | if (getFPImm()->getType() == Type::getFloatTy(getFPImm()->getContext())) |
Nate Begeman | e8b7ccf | 2008-02-14 07:39:30 +0000 | [diff] [blame] | 241 | OS << getFPImm()->getValueAPF().convertToFloat(); |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 242 | else |
Nate Begeman | e8b7ccf | 2008-02-14 07:39:30 +0000 | [diff] [blame] | 243 | OS << getFPImm()->getValueAPF().convertToDouble(); |
Nate Begeman | e8b7ccf | 2008-02-14 07:39:30 +0000 | [diff] [blame] | 244 | break; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 245 | case MachineOperand::MO_MachineBasicBlock: |
| 246 | OS << "mbb<" |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 247 | << ((Value*)getMBB()->getBasicBlock())->getName() |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 248 | << "," << (void*)getMBB() << '>'; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 249 | break; |
| 250 | case MachineOperand::MO_FrameIndex: |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 251 | OS << "<fi#" << getIndex() << '>'; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 252 | break; |
| 253 | case MachineOperand::MO_ConstantPoolIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 254 | OS << "<cp#" << getIndex(); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 255 | if (getOffset()) OS << "+" << getOffset(); |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 256 | OS << '>'; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 257 | break; |
| 258 | case MachineOperand::MO_JumpTableIndex: |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 259 | OS << "<jt#" << getIndex() << '>'; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 260 | break; |
| 261 | case MachineOperand::MO_GlobalAddress: |
| 262 | OS << "<ga:" << ((Value*)getGlobal())->getName(); |
| 263 | if (getOffset()) OS << "+" << getOffset(); |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 264 | OS << '>'; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 265 | break; |
| 266 | case MachineOperand::MO_ExternalSymbol: |
| 267 | OS << "<es:" << getSymbolName(); |
| 268 | if (getOffset()) OS << "+" << getOffset(); |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 269 | OS << '>'; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 270 | break; |
| 271 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 272 | llvm_unreachable("Unrecognized operand type"); |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 273 | } |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 274 | |
| 275 | if (unsigned TF = getTargetFlags()) |
| 276 | OS << "[TF=" << TF << ']'; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | //===----------------------------------------------------------------------===// |
Dan Gohman | ce42e40 | 2008-07-07 20:32:02 +0000 | [diff] [blame] | 280 | // MachineMemOperand Implementation |
| 281 | //===----------------------------------------------------------------------===// |
| 282 | |
| 283 | MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f, |
| 284 | int64_t o, uint64_t s, unsigned int a) |
| 285 | : Offset(o), Size(s), V(v), |
| 286 | Flags((f & 7) | ((Log2_32(a) + 1) << 3)) { |
Dan Gohman | f1bf29e | 2008-07-08 23:47:04 +0000 | [diff] [blame] | 287 | assert(isPowerOf2_32(a) && "Alignment is not a power of 2!"); |
Dan Gohman | c5e1f98 | 2008-07-16 15:56:42 +0000 | [diff] [blame] | 288 | assert((isLoad() || isStore()) && "Not a load/store!"); |
Dan Gohman | ce42e40 | 2008-07-07 20:32:02 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Dan Gohman | b8d2f55 | 2008-08-20 15:58:01 +0000 | [diff] [blame] | 291 | /// Profile - Gather unique data for the object. |
| 292 | /// |
| 293 | void MachineMemOperand::Profile(FoldingSetNodeID &ID) const { |
| 294 | ID.AddInteger(Offset); |
| 295 | ID.AddInteger(Size); |
| 296 | ID.AddPointer(V); |
| 297 | ID.AddInteger(Flags); |
| 298 | } |
| 299 | |
Dan Gohman | ce42e40 | 2008-07-07 20:32:02 +0000 | [diff] [blame] | 300 | //===----------------------------------------------------------------------===// |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 301 | // MachineInstr Implementation |
| 302 | //===----------------------------------------------------------------------===// |
| 303 | |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 304 | /// MachineInstr ctor - This constructor creates a dummy MachineInstr with |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 305 | /// TID NULL and no operands. |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 306 | MachineInstr::MachineInstr() |
Dale Johannesen | 06efc02 | 2009-01-27 23:20:29 +0000 | [diff] [blame] | 307 | : TID(0), NumImplicitOps(0), Parent(0), debugLoc(DebugLoc::getUnknownLoc()) { |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 308 | // Make sure that we get added to a machine basicblock |
| 309 | LeakDetector::addGarbageObject(this); |
Chris Lattner | 7279122 | 2002-10-28 20:59:49 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 312 | void MachineInstr::addImplicitDefUseOperands() { |
| 313 | if (TID->ImplicitDefs) |
Chris Lattner | a4161ee | 2007-12-30 00:12:25 +0000 | [diff] [blame] | 314 | for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs) |
Chris Lattner | 8019f41 | 2007-12-30 00:41:17 +0000 | [diff] [blame] | 315 | addOperand(MachineOperand::CreateReg(*ImpDefs, true, true)); |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 316 | if (TID->ImplicitUses) |
Chris Lattner | a4161ee | 2007-12-30 00:12:25 +0000 | [diff] [blame] | 317 | for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses) |
Chris Lattner | 8019f41 | 2007-12-30 00:41:17 +0000 | [diff] [blame] | 318 | addOperand(MachineOperand::CreateReg(*ImpUses, false, true)); |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /// MachineInstr ctor - This constructor create a MachineInstr and add the |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 322 | /// implicit operands. It reserves space for number of operands specified by |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 323 | /// TargetInstrDesc or the numOperands if it is not zero. (for |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 324 | /// instructions with variable number of operands). |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 325 | MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp) |
Dale Johannesen | 06efc02 | 2009-01-27 23:20:29 +0000 | [diff] [blame] | 326 | : TID(&tid), NumImplicitOps(0), Parent(0), |
| 327 | debugLoc(DebugLoc::getUnknownLoc()) { |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 328 | if (!NoImp && TID->getImplicitDefs()) |
| 329 | for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 330 | NumImplicitOps++; |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 331 | if (!NoImp && TID->getImplicitUses()) |
| 332 | for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 333 | NumImplicitOps++; |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 334 | Operands.reserve(NumImplicitOps + TID->getNumOperands()); |
Evan Cheng | fa94572 | 2007-10-13 02:23:01 +0000 | [diff] [blame] | 335 | if (!NoImp) |
| 336 | addImplicitDefUseOperands(); |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 337 | // Make sure that we get added to a machine basicblock |
| 338 | LeakDetector::addGarbageObject(this); |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Dale Johannesen | 06efc02 | 2009-01-27 23:20:29 +0000 | [diff] [blame] | 341 | /// MachineInstr ctor - As above, but with a DebugLoc. |
| 342 | MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl, |
| 343 | bool NoImp) |
| 344 | : TID(&tid), NumImplicitOps(0), Parent(0), debugLoc(dl) { |
| 345 | if (!NoImp && TID->getImplicitDefs()) |
| 346 | for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) |
| 347 | NumImplicitOps++; |
| 348 | if (!NoImp && TID->getImplicitUses()) |
| 349 | for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) |
| 350 | NumImplicitOps++; |
| 351 | Operands.reserve(NumImplicitOps + TID->getNumOperands()); |
| 352 | if (!NoImp) |
| 353 | addImplicitDefUseOperands(); |
| 354 | // Make sure that we get added to a machine basicblock |
| 355 | LeakDetector::addGarbageObject(this); |
| 356 | } |
| 357 | |
| 358 | /// MachineInstr ctor - Work exactly the same as the ctor two above, except |
| 359 | /// that the MachineInstr is created and added to the end of the specified |
| 360 | /// basic block. |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 361 | /// |
Dale Johannesen | 06efc02 | 2009-01-27 23:20:29 +0000 | [diff] [blame] | 362 | MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid) |
| 363 | : TID(&tid), NumImplicitOps(0), Parent(0), |
| 364 | debugLoc(DebugLoc::getUnknownLoc()) { |
| 365 | assert(MBB && "Cannot use inserting ctor with null basic block!"); |
| 366 | if (TID->ImplicitDefs) |
| 367 | for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) |
| 368 | NumImplicitOps++; |
| 369 | if (TID->ImplicitUses) |
| 370 | for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) |
| 371 | NumImplicitOps++; |
| 372 | Operands.reserve(NumImplicitOps + TID->getNumOperands()); |
| 373 | addImplicitDefUseOperands(); |
| 374 | // Make sure that we get added to a machine basicblock |
| 375 | LeakDetector::addGarbageObject(this); |
| 376 | MBB->push_back(this); // Add instruction to end of basic block! |
| 377 | } |
| 378 | |
| 379 | /// MachineInstr ctor - As above, but with a DebugLoc. |
| 380 | /// |
| 381 | MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl, |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 382 | const TargetInstrDesc &tid) |
Dale Johannesen | 06efc02 | 2009-01-27 23:20:29 +0000 | [diff] [blame] | 383 | : TID(&tid), NumImplicitOps(0), Parent(0), debugLoc(dl) { |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 384 | assert(MBB && "Cannot use inserting ctor with null basic block!"); |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 385 | if (TID->ImplicitDefs) |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 386 | for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 387 | NumImplicitOps++; |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 388 | if (TID->ImplicitUses) |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 389 | for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) |
Evan Cheng | d7de496 | 2006-11-13 23:34:06 +0000 | [diff] [blame] | 390 | NumImplicitOps++; |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 391 | Operands.reserve(NumImplicitOps + TID->getNumOperands()); |
Evan Cheng | 67f660c | 2006-11-30 07:08:44 +0000 | [diff] [blame] | 392 | addImplicitDefUseOperands(); |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 393 | // Make sure that we get added to a machine basicblock |
| 394 | LeakDetector::addGarbageObject(this); |
Chris Lattner | ddd7fcb | 2002-10-29 23:19:00 +0000 | [diff] [blame] | 395 | MBB->push_back(this); // Add instruction to end of basic block! |
| 396 | } |
| 397 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 398 | /// MachineInstr ctor - Copies MachineInstr arg exactly |
| 399 | /// |
Evan Cheng | 1ed9922 | 2008-07-19 00:37:25 +0000 | [diff] [blame] | 400 | MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI) |
Dale Johannesen | 06efc02 | 2009-01-27 23:20:29 +0000 | [diff] [blame] | 401 | : TID(&MI.getDesc()), NumImplicitOps(0), Parent(0), |
| 402 | debugLoc(MI.getDebugLoc()) { |
Chris Lattner | 943b5e1 | 2006-05-04 19:14:44 +0000 | [diff] [blame] | 403 | Operands.reserve(MI.getNumOperands()); |
Tanya Lattner | b5159ed | 2004-05-23 20:58:02 +0000 | [diff] [blame] | 404 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 405 | // Add operands |
Evan Cheng | 1ed9922 | 2008-07-19 00:37:25 +0000 | [diff] [blame] | 406 | for (unsigned i = 0; i != MI.getNumOperands(); ++i) |
| 407 | addOperand(MI.getOperand(i)); |
| 408 | NumImplicitOps = MI.NumImplicitOps; |
Tanya Lattner | 0c63e03 | 2004-05-24 03:14:18 +0000 | [diff] [blame] | 409 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 410 | // Add memory operands. |
Dan Gohman | fed90b6 | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 411 | for (std::list<MachineMemOperand>::const_iterator i = MI.memoperands_begin(), |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 412 | j = MI.memoperands_end(); i != j; ++i) |
| 413 | addMemOperand(MF, *i); |
| 414 | |
| 415 | // Set parent to null. |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 416 | Parent = 0; |
Dan Gohman | 6116a73 | 2008-07-21 18:47:29 +0000 | [diff] [blame] | 417 | |
| 418 | LeakDetector::addGarbageObject(this); |
Tanya Lattner | 466b534 | 2004-05-23 19:35:12 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Misha Brukman | ce22e76 | 2004-07-09 14:45:17 +0000 | [diff] [blame] | 421 | MachineInstr::~MachineInstr() { |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 422 | LeakDetector::removeGarbageObject(this); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 423 | assert(MemOperands.empty() && |
| 424 | "MachineInstr being deleted with live memoperands!"); |
Chris Lattner | e12d6ab | 2007-12-30 06:11:04 +0000 | [diff] [blame] | 425 | #ifndef NDEBUG |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 426 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
Chris Lattner | e12d6ab | 2007-12-30 06:11:04 +0000 | [diff] [blame] | 427 | assert(Operands[i].ParentMI == this && "ParentMI mismatch!"); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 428 | assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) && |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 429 | "Reg operand def/use list corrupted"); |
| 430 | } |
Chris Lattner | e12d6ab | 2007-12-30 06:11:04 +0000 | [diff] [blame] | 431 | #endif |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 434 | /// getRegInfo - If this instruction is embedded into a MachineFunction, |
| 435 | /// return the MachineRegisterInfo object for the current function, otherwise |
| 436 | /// return null. |
| 437 | MachineRegisterInfo *MachineInstr::getRegInfo() { |
| 438 | if (MachineBasicBlock *MBB = getParent()) |
Dan Gohman | 4e526b9 | 2008-07-08 23:59:09 +0000 | [diff] [blame] | 439 | return &MBB->getParent()->getRegInfo(); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in |
| 444 | /// this instruction from their respective use lists. This requires that the |
| 445 | /// operands already be on their use lists. |
| 446 | void MachineInstr::RemoveRegOperandsFromUseLists() { |
| 447 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 448 | if (Operands[i].isReg()) |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 449 | Operands[i].RemoveRegOperandFromRegInfo(); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /// AddRegOperandsToUseLists - Add all of the register operands in |
| 454 | /// this instruction from their respective use lists. This requires that the |
| 455 | /// operands not be on their use lists yet. |
| 456 | void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) { |
| 457 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 458 | if (Operands[i].isReg()) |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 459 | Operands[i].AddRegOperandToRegInfo(&RegInfo); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | |
| 464 | /// addOperand - Add the specified operand to the instruction. If it is an |
| 465 | /// implicit operand, it is added to the end of the operand list. If it is |
| 466 | /// an explicit operand it is added at the end of the explicit operand list |
| 467 | /// (before the first implicit operand). |
| 468 | void MachineInstr::addOperand(const MachineOperand &Op) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 469 | bool isImpReg = Op.isReg() && Op.isImplicit(); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 470 | assert((isImpReg || !OperandsComplete()) && |
| 471 | "Trying to add an operand to a machine instr that is already done!"); |
| 472 | |
Dan Gohman | bcf28c0 | 2008-12-09 22:45:08 +0000 | [diff] [blame] | 473 | MachineRegisterInfo *RegInfo = getRegInfo(); |
| 474 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 475 | // If we are adding the operand to the end of the list, our job is simpler. |
| 476 | // This is true most of the time, so this is a reasonable optimization. |
| 477 | if (isImpReg || NumImplicitOps == 0) { |
| 478 | // We can only do this optimization if we know that the operand list won't |
| 479 | // reallocate. |
| 480 | if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) { |
| 481 | Operands.push_back(Op); |
| 482 | |
| 483 | // Set the parent of the operand. |
| 484 | Operands.back().ParentMI = this; |
| 485 | |
| 486 | // If the operand is a register, update the operand's use list. |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 487 | if (Op.isReg()) |
Dan Gohman | bcf28c0 | 2008-12-09 22:45:08 +0000 | [diff] [blame] | 488 | Operands.back().AddRegOperandToRegInfo(RegInfo); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 489 | return; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | // Otherwise, we have to insert a real operand before any implicit ones. |
| 494 | unsigned OpNo = Operands.size()-NumImplicitOps; |
| 495 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 496 | // If this instruction isn't embedded into a function, then we don't need to |
| 497 | // update any operand lists. |
| 498 | if (RegInfo == 0) { |
| 499 | // Simple insertion, no reginfo update needed for other register operands. |
| 500 | Operands.insert(Operands.begin()+OpNo, Op); |
| 501 | Operands[OpNo].ParentMI = this; |
| 502 | |
| 503 | // Do explicitly set the reginfo for this operand though, to ensure the |
| 504 | // next/prev fields are properly nulled out. |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 505 | if (Operands[OpNo].isReg()) |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 506 | Operands[OpNo].AddRegOperandToRegInfo(0); |
| 507 | |
| 508 | } else if (Operands.size()+1 <= Operands.capacity()) { |
| 509 | // Otherwise, we have to remove register operands from their register use |
| 510 | // list, add the operand, then add the register operands back to their use |
| 511 | // list. This also must handle the case when the operand list reallocates |
| 512 | // to somewhere else. |
| 513 | |
| 514 | // If insertion of this operand won't cause reallocation of the operand |
| 515 | // list, just remove the implicit operands, add the operand, then re-add all |
| 516 | // the rest of the operands. |
| 517 | for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 518 | assert(Operands[i].isReg() && "Should only be an implicit reg!"); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 519 | Operands[i].RemoveRegOperandFromRegInfo(); |
| 520 | } |
| 521 | |
| 522 | // Add the operand. If it is a register, add it to the reg list. |
| 523 | Operands.insert(Operands.begin()+OpNo, Op); |
| 524 | Operands[OpNo].ParentMI = this; |
| 525 | |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 526 | if (Operands[OpNo].isReg()) |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 527 | Operands[OpNo].AddRegOperandToRegInfo(RegInfo); |
| 528 | |
| 529 | // Re-add all the implicit ops. |
| 530 | for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 531 | assert(Operands[i].isReg() && "Should only be an implicit reg!"); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 532 | Operands[i].AddRegOperandToRegInfo(RegInfo); |
| 533 | } |
| 534 | } else { |
| 535 | // Otherwise, we will be reallocating the operand list. Remove all reg |
| 536 | // operands from their list, then readd them after the operand list is |
| 537 | // reallocated. |
| 538 | RemoveRegOperandsFromUseLists(); |
| 539 | |
| 540 | Operands.insert(Operands.begin()+OpNo, Op); |
| 541 | Operands[OpNo].ParentMI = this; |
| 542 | |
| 543 | // Re-add all the operands. |
| 544 | AddRegOperandsToUseLists(*RegInfo); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | /// RemoveOperand - Erase an operand from an instruction, leaving it with one |
| 549 | /// fewer operand than it started with. |
| 550 | /// |
| 551 | void MachineInstr::RemoveOperand(unsigned OpNo) { |
| 552 | assert(OpNo < Operands.size() && "Invalid operand number"); |
| 553 | |
| 554 | // Special case removing the last one. |
| 555 | if (OpNo == Operands.size()-1) { |
| 556 | // If needed, remove from the reg def/use list. |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 557 | if (Operands.back().isReg() && Operands.back().isOnRegUseList()) |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 558 | Operands.back().RemoveRegOperandFromRegInfo(); |
| 559 | |
| 560 | Operands.pop_back(); |
| 561 | return; |
| 562 | } |
| 563 | |
| 564 | // Otherwise, we are removing an interior operand. If we have reginfo to |
| 565 | // update, remove all operands that will be shifted down from their reg lists, |
| 566 | // move everything down, then re-add them. |
| 567 | MachineRegisterInfo *RegInfo = getRegInfo(); |
| 568 | if (RegInfo) { |
| 569 | for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 570 | if (Operands[i].isReg()) |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 571 | Operands[i].RemoveRegOperandFromRegInfo(); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | Operands.erase(Operands.begin()+OpNo); |
| 576 | |
| 577 | if (RegInfo) { |
| 578 | for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 579 | if (Operands[i].isReg()) |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 580 | Operands[i].AddRegOperandToRegInfo(RegInfo); |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 585 | /// addMemOperand - Add a MachineMemOperand to the machine instruction, |
| 586 | /// referencing arbitrary storage. |
| 587 | void MachineInstr::addMemOperand(MachineFunction &MF, |
| 588 | const MachineMemOperand &MO) { |
Dan Gohman | fed90b6 | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 589 | MemOperands.push_back(MO); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | /// clearMemOperands - Erase all of this MachineInstr's MachineMemOperands. |
| 593 | void MachineInstr::clearMemOperands(MachineFunction &MF) { |
Dan Gohman | fed90b6 | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 594 | MemOperands.clear(); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 597 | |
Chris Lattner | 48d7c06 | 2006-04-17 21:35:41 +0000 | [diff] [blame] | 598 | /// removeFromParent - This method unlinks 'this' from the containing basic |
| 599 | /// block, and returns it, but does not delete it. |
| 600 | MachineInstr *MachineInstr::removeFromParent() { |
| 601 | assert(getParent() && "Not embedded in a basic block!"); |
| 602 | getParent()->remove(this); |
| 603 | return this; |
| 604 | } |
| 605 | |
| 606 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 607 | /// eraseFromParent - This method unlinks 'this' from the containing basic |
| 608 | /// block, and deletes it. |
| 609 | void MachineInstr::eraseFromParent() { |
| 610 | assert(getParent() && "Not embedded in a basic block!"); |
| 611 | getParent()->erase(this); |
| 612 | } |
| 613 | |
| 614 | |
Brian Gaeke | 21326fc | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 615 | /// OperandComplete - Return true if it's illegal to add a new operand |
| 616 | /// |
Chris Lattner | 2a90ba6 | 2004-02-12 16:09:53 +0000 | [diff] [blame] | 617 | bool MachineInstr::OperandsComplete() const { |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 618 | unsigned short NumOperands = TID->getNumOperands(); |
Chris Lattner | 8f707e1 | 2008-01-07 05:19:29 +0000 | [diff] [blame] | 619 | if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands) |
Vikram S. Adve | 3497782 | 2003-05-31 07:39:06 +0000 | [diff] [blame] | 620 | return true; // Broken: we have all the operands of this instruction! |
Chris Lattner | 413746e | 2002-10-28 20:48:39 +0000 | [diff] [blame] | 621 | return false; |
| 622 | } |
| 623 | |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 624 | /// getNumExplicitOperands - Returns the number of non-implicit operands. |
| 625 | /// |
| 626 | unsigned MachineInstr::getNumExplicitOperands() const { |
Chris Lattner | 349c495 | 2008-01-07 03:13:06 +0000 | [diff] [blame] | 627 | unsigned NumOperands = TID->getNumOperands(); |
Chris Lattner | 8f707e1 | 2008-01-07 05:19:29 +0000 | [diff] [blame] | 628 | if (!TID->isVariadic()) |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 629 | return NumOperands; |
| 630 | |
Dan Gohman | 9407cd4 | 2009-04-15 17:59:11 +0000 | [diff] [blame] | 631 | for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) { |
| 632 | const MachineOperand &MO = getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 633 | if (!MO.isReg() || !MO.isImplicit()) |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 634 | NumOperands++; |
| 635 | } |
| 636 | return NumOperands; |
| 637 | } |
| 638 | |
Chris Lattner | 8ace2cd | 2006-10-20 22:39:59 +0000 | [diff] [blame] | 639 | |
Dan Gohman | 4406604 | 2008-07-01 00:05:16 +0000 | [diff] [blame] | 640 | /// isLabel - Returns true if the MachineInstr represents a label. |
| 641 | /// |
| 642 | bool MachineInstr::isLabel() const { |
| 643 | return getOpcode() == TargetInstrInfo::DBG_LABEL || |
| 644 | getOpcode() == TargetInstrInfo::EH_LABEL || |
| 645 | getOpcode() == TargetInstrInfo::GC_LABEL; |
| 646 | } |
| 647 | |
Evan Cheng | bb81d97 | 2008-01-31 09:59:15 +0000 | [diff] [blame] | 648 | /// isDebugLabel - Returns true if the MachineInstr represents a debug label. |
| 649 | /// |
| 650 | bool MachineInstr::isDebugLabel() const { |
Dan Gohman | 4406604 | 2008-07-01 00:05:16 +0000 | [diff] [blame] | 651 | return getOpcode() == TargetInstrInfo::DBG_LABEL; |
Evan Cheng | bb81d97 | 2008-01-31 09:59:15 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Evan Cheng | faa5107 | 2007-04-26 19:00:32 +0000 | [diff] [blame] | 654 | /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of |
Jim Grosbach | f9ca50e | 2009-09-17 17:57:26 +0000 | [diff] [blame^] | 655 | /// the specific register or -1 if it is not found. It further tightens |
Evan Cheng | 76d7e76 | 2007-02-23 01:04:26 +0000 | [diff] [blame] | 656 | /// 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] | 657 | int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill, |
| 658 | const TargetRegisterInfo *TRI) const { |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 659 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 660 | const MachineOperand &MO = getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 661 | if (!MO.isReg() || !MO.isUse()) |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 662 | continue; |
| 663 | unsigned MOReg = MO.getReg(); |
| 664 | if (!MOReg) |
| 665 | continue; |
| 666 | if (MOReg == Reg || |
| 667 | (TRI && |
| 668 | TargetRegisterInfo::isPhysicalRegister(MOReg) && |
| 669 | TargetRegisterInfo::isPhysicalRegister(Reg) && |
| 670 | TRI->isSubRegister(MOReg, Reg))) |
Evan Cheng | 76d7e76 | 2007-02-23 01:04:26 +0000 | [diff] [blame] | 671 | if (!isKill || MO.isKill()) |
Evan Cheng | 32eb1f1 | 2007-03-26 22:37:45 +0000 | [diff] [blame] | 672 | return i; |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 673 | } |
Evan Cheng | 32eb1f1 | 2007-03-26 22:37:45 +0000 | [diff] [blame] | 674 | return -1; |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 677 | /// findRegisterDefOperandIdx() - Returns the operand index that is a def of |
Dan Gohman | 703bfe6 | 2008-05-06 00:20:10 +0000 | [diff] [blame] | 678 | /// the specified register or -1 if it is not found. If isDead is true, defs |
| 679 | /// that are not dead are skipped. If TargetRegisterInfo is non-null, then it |
| 680 | /// also checks if there is a def of a super-register. |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 681 | int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, |
| 682 | const TargetRegisterInfo *TRI) const { |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 683 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 684 | const MachineOperand &MO = getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 685 | if (!MO.isReg() || !MO.isDef()) |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 686 | continue; |
| 687 | unsigned MOReg = MO.getReg(); |
| 688 | if (MOReg == Reg || |
| 689 | (TRI && |
| 690 | TargetRegisterInfo::isPhysicalRegister(MOReg) && |
| 691 | TargetRegisterInfo::isPhysicalRegister(Reg) && |
| 692 | TRI->isSubRegister(MOReg, Reg))) |
| 693 | if (!isDead || MO.isDead()) |
| 694 | return i; |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 695 | } |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 696 | return -1; |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 697 | } |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 698 | |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 699 | /// findFirstPredOperandIdx() - Find the index of the first operand in the |
| 700 | /// operand list that is used to represent the predicate. It returns -1 if |
| 701 | /// none is found. |
| 702 | int MachineInstr::findFirstPredOperandIdx() const { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 703 | const TargetInstrDesc &TID = getDesc(); |
| 704 | if (TID.isPredicable()) { |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 705 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 706 | if (TID.OpInfo[i].isPredicate()) |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 707 | return i; |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Evan Cheng | f277ee4 | 2007-05-29 18:35:22 +0000 | [diff] [blame] | 710 | return -1; |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 711 | } |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 712 | |
Bob Wilson | d9df501 | 2009-04-09 17:16:43 +0000 | [diff] [blame] | 713 | /// isRegTiedToUseOperand - Given the index of a register def operand, |
| 714 | /// check if the register def is tied to a source operand, due to either |
| 715 | /// two-address elimination or inline assembly constraints. Returns the |
| 716 | /// first tied use operand index by reference is UseOpIdx is not null. |
Jakob Stoklund Olesen | ce9be2c | 2009-04-29 20:57:16 +0000 | [diff] [blame] | 717 | bool MachineInstr:: |
| 718 | isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const { |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 719 | if (getOpcode() == TargetInstrInfo::INLINEASM) { |
Bob Wilson | d9df501 | 2009-04-09 17:16:43 +0000 | [diff] [blame] | 720 | assert(DefOpIdx >= 2); |
| 721 | const MachineOperand &MO = getOperand(DefOpIdx); |
Chris Lattner | c30aa7b | 2009-04-09 23:33:34 +0000 | [diff] [blame] | 722 | if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0) |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 723 | return false; |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 724 | // Determine the actual operand index that corresponds to this index. |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 725 | unsigned DefNo = 0; |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 726 | unsigned DefPart = 0; |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 727 | for (unsigned i = 1, e = getNumOperands(); i < e; ) { |
| 728 | const MachineOperand &FMO = getOperand(i); |
Jakob Stoklund Olesen | 45d34fe | 2009-07-19 19:09:59 +0000 | [diff] [blame] | 729 | // After the normal asm operands there may be additional imp-def regs. |
| 730 | if (!FMO.isImm()) |
| 731 | return false; |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 732 | // Skip over this def. |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 733 | unsigned NumOps = InlineAsm::getNumOperandRegisters(FMO.getImm()); |
| 734 | unsigned PrevDef = i + 1; |
| 735 | i = PrevDef + NumOps; |
| 736 | if (i > DefOpIdx) { |
| 737 | DefPart = DefOpIdx - PrevDef; |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 738 | break; |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 739 | } |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 740 | ++DefNo; |
| 741 | } |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 742 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 743 | const MachineOperand &FMO = getOperand(i); |
| 744 | if (!FMO.isImm()) |
| 745 | continue; |
| 746 | if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse()) |
| 747 | continue; |
| 748 | unsigned Idx; |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 749 | if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) && |
Bob Wilson | d9df501 | 2009-04-09 17:16:43 +0000 | [diff] [blame] | 750 | Idx == DefNo) { |
| 751 | if (UseOpIdx) |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 752 | *UseOpIdx = (unsigned)i + 1 + DefPart; |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 753 | return true; |
Bob Wilson | d9df501 | 2009-04-09 17:16:43 +0000 | [diff] [blame] | 754 | } |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 755 | } |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 756 | return false; |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Bob Wilson | d9df501 | 2009-04-09 17:16:43 +0000 | [diff] [blame] | 759 | assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!"); |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 760 | const TargetInstrDesc &TID = getDesc(); |
Evan Cheng | ef0732d | 2008-07-10 07:35:43 +0000 | [diff] [blame] | 761 | for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) { |
| 762 | const MachineOperand &MO = getOperand(i); |
Dan Gohman | 2ce7f20 | 2008-12-05 05:45:42 +0000 | [diff] [blame] | 763 | if (MO.isReg() && MO.isUse() && |
Bob Wilson | d9df501 | 2009-04-09 17:16:43 +0000 | [diff] [blame] | 764 | TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) { |
| 765 | if (UseOpIdx) |
| 766 | *UseOpIdx = (unsigned)i; |
Evan Cheng | ef0732d | 2008-07-10 07:35:43 +0000 | [diff] [blame] | 767 | return true; |
Bob Wilson | d9df501 | 2009-04-09 17:16:43 +0000 | [diff] [blame] | 768 | } |
Evan Cheng | 32dfbea | 2007-10-12 08:50:34 +0000 | [diff] [blame] | 769 | } |
| 770 | return false; |
| 771 | } |
| 772 | |
Evan Cheng | a24752f | 2009-03-19 20:30:06 +0000 | [diff] [blame] | 773 | /// isRegTiedToDefOperand - Return true if the operand of the specified index |
| 774 | /// is a register use and it is tied to an def operand. It also returns the def |
| 775 | /// operand index by reference. |
Jakob Stoklund Olesen | ce9be2c | 2009-04-29 20:57:16 +0000 | [diff] [blame] | 776 | bool MachineInstr:: |
| 777 | isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const { |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 778 | if (getOpcode() == TargetInstrInfo::INLINEASM) { |
| 779 | const MachineOperand &MO = getOperand(UseOpIdx); |
Chris Lattner | 0c8382c | 2009-04-09 16:50:43 +0000 | [diff] [blame] | 780 | if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0) |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 781 | return false; |
Jakob Stoklund Olesen | 57e599a | 2009-07-16 20:58:34 +0000 | [diff] [blame] | 782 | |
| 783 | // Find the flag operand corresponding to UseOpIdx |
| 784 | unsigned FlagIdx, NumOps=0; |
| 785 | for (FlagIdx = 1; FlagIdx < UseOpIdx; FlagIdx += NumOps+1) { |
| 786 | const MachineOperand &UFMO = getOperand(FlagIdx); |
Jakob Stoklund Olesen | 45d34fe | 2009-07-19 19:09:59 +0000 | [diff] [blame] | 787 | // After the normal asm operands there may be additional imp-def regs. |
| 788 | if (!UFMO.isImm()) |
| 789 | return false; |
Jakob Stoklund Olesen | 57e599a | 2009-07-16 20:58:34 +0000 | [diff] [blame] | 790 | NumOps = InlineAsm::getNumOperandRegisters(UFMO.getImm()); |
| 791 | assert(NumOps < getNumOperands() && "Invalid inline asm flag"); |
| 792 | if (UseOpIdx < FlagIdx+NumOps+1) |
| 793 | break; |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 794 | } |
Jakob Stoklund Olesen | 57e599a | 2009-07-16 20:58:34 +0000 | [diff] [blame] | 795 | if (FlagIdx >= UseOpIdx) |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 796 | return false; |
Jakob Stoklund Olesen | 57e599a | 2009-07-16 20:58:34 +0000 | [diff] [blame] | 797 | const MachineOperand &UFMO = getOperand(FlagIdx); |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 798 | unsigned DefNo; |
| 799 | if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) { |
| 800 | if (!DefOpIdx) |
| 801 | return true; |
| 802 | |
| 803 | unsigned DefIdx = 1; |
| 804 | // Remember to adjust the index. First operand is asm string, then there |
| 805 | // is a flag for each. |
| 806 | while (DefNo) { |
| 807 | const MachineOperand &FMO = getOperand(DefIdx); |
| 808 | assert(FMO.isImm()); |
| 809 | // Skip over this def. |
| 810 | DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1; |
| 811 | --DefNo; |
| 812 | } |
Evan Cheng | ef5d070 | 2009-06-24 02:05:51 +0000 | [diff] [blame] | 813 | *DefOpIdx = DefIdx + UseOpIdx - FlagIdx; |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 814 | return true; |
| 815 | } |
| 816 | return false; |
| 817 | } |
| 818 | |
Evan Cheng | a24752f | 2009-03-19 20:30:06 +0000 | [diff] [blame] | 819 | const TargetInstrDesc &TID = getDesc(); |
| 820 | if (UseOpIdx >= TID.getNumOperands()) |
| 821 | return false; |
| 822 | const MachineOperand &MO = getOperand(UseOpIdx); |
| 823 | if (!MO.isReg() || !MO.isUse()) |
| 824 | return false; |
| 825 | int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO); |
| 826 | if (DefIdx == -1) |
| 827 | return false; |
| 828 | if (DefOpIdx) |
| 829 | *DefOpIdx = (unsigned)DefIdx; |
| 830 | return true; |
| 831 | } |
| 832 | |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 833 | /// copyKillDeadInfo - Copies kill / dead operand properties from MI. |
| 834 | /// |
| 835 | void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) { |
| 836 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 837 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 838 | if (!MO.isReg() || (!MO.isKill() && !MO.isDead())) |
Evan Cheng | 576d123 | 2006-12-06 08:27:42 +0000 | [diff] [blame] | 839 | continue; |
| 840 | for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) { |
| 841 | MachineOperand &MOp = getOperand(j); |
| 842 | if (!MOp.isIdenticalTo(MO)) |
| 843 | continue; |
| 844 | if (MO.isKill()) |
| 845 | MOp.setIsKill(); |
| 846 | else |
| 847 | MOp.setIsDead(); |
| 848 | break; |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 853 | /// copyPredicates - Copies predicate operand(s) from MI. |
| 854 | void MachineInstr::copyPredicates(const MachineInstr *MI) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 855 | const TargetInstrDesc &TID = MI->getDesc(); |
Evan Cheng | b27087f | 2008-03-13 00:44:09 +0000 | [diff] [blame] | 856 | if (!TID.isPredicable()) |
| 857 | return; |
| 858 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 859 | if (TID.OpInfo[i].isPredicate()) { |
| 860 | // Predicated operands must be last operands. |
| 861 | addOperand(MI->getOperand(i)); |
Evan Cheng | 19e3f31 | 2007-05-15 01:26:09 +0000 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | } |
| 865 | |
Evan Cheng | 9f1c831 | 2008-07-03 09:09:37 +0000 | [diff] [blame] | 866 | /// isSafeToMove - Return true if it is safe to move this instruction. If |
| 867 | /// SawStore is set to true, it means that there is a store (or call) between |
| 868 | /// the instruction's location and its intended destination. |
Dan Gohman | b3b930a | 2008-11-18 19:04:29 +0000 | [diff] [blame] | 869 | bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII, |
| 870 | bool &SawStore) const { |
Evan Cheng | b27087f | 2008-03-13 00:44:09 +0000 | [diff] [blame] | 871 | // Ignore stuff that we obviously can't move. |
| 872 | if (TID->mayStore() || TID->isCall()) { |
| 873 | SawStore = true; |
| 874 | return false; |
| 875 | } |
Dan Gohman | 237dee1 | 2008-12-23 17:28:50 +0000 | [diff] [blame] | 876 | if (TID->isTerminator() || TID->hasUnmodeledSideEffects()) |
Evan Cheng | b27087f | 2008-03-13 00:44:09 +0000 | [diff] [blame] | 877 | return false; |
| 878 | |
| 879 | // See if this instruction does a load. If so, we have to guarantee that the |
| 880 | // loaded value doesn't change between the load and the its intended |
| 881 | // destination. The check for isInvariantLoad gives the targe the chance to |
| 882 | // classify the load as always returning a constant, e.g. a constant pool |
| 883 | // load. |
Dan Gohman | 3e4fb70 | 2008-09-24 00:06:15 +0000 | [diff] [blame] | 884 | if (TID->mayLoad() && !TII->isInvariantLoad(this)) |
Evan Cheng | b27087f | 2008-03-13 00:44:09 +0000 | [diff] [blame] | 885 | // Otherwise, this is a real load. If there is a store between the load and |
Evan Cheng | 7cc2c40 | 2009-07-28 21:49:18 +0000 | [diff] [blame] | 886 | // end of block, or if the load is volatile, we can't move it. |
Dan Gohman | d790a5c | 2008-10-02 15:04:30 +0000 | [diff] [blame] | 887 | return !SawStore && !hasVolatileMemoryRef(); |
Dan Gohman | 3e4fb70 | 2008-09-24 00:06:15 +0000 | [diff] [blame] | 888 | |
Evan Cheng | b27087f | 2008-03-13 00:44:09 +0000 | [diff] [blame] | 889 | return true; |
| 890 | } |
| 891 | |
Evan Cheng | df3b993 | 2008-08-27 20:33:50 +0000 | [diff] [blame] | 892 | /// isSafeToReMat - Return true if it's safe to rematerialize the specified |
| 893 | /// instruction which defined the specified register instead of copying it. |
Dan Gohman | b3b930a | 2008-11-18 19:04:29 +0000 | [diff] [blame] | 894 | bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII, |
| 895 | unsigned DstReg) const { |
Evan Cheng | df3b993 | 2008-08-27 20:33:50 +0000 | [diff] [blame] | 896 | bool SawStore = false; |
Evan Cheng | 3689ff4 | 2008-08-30 09:07:18 +0000 | [diff] [blame] | 897 | if (!getDesc().isRematerializable() || |
| 898 | !TII->isTriviallyReMaterializable(this) || |
| 899 | !isSafeToMove(TII, SawStore)) |
Evan Cheng | df3b993 | 2008-08-27 20:33:50 +0000 | [diff] [blame] | 900 | return false; |
| 901 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Dan Gohman | cbad42c | 2008-11-18 19:49:32 +0000 | [diff] [blame] | 902 | const MachineOperand &MO = getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 903 | if (!MO.isReg()) |
Evan Cheng | df3b993 | 2008-08-27 20:33:50 +0000 | [diff] [blame] | 904 | continue; |
| 905 | // FIXME: For now, do not remat any instruction with register operands. |
| 906 | // Later on, we can loosen the restriction is the register operands have |
| 907 | // not been modified between the def and use. Note, this is different from |
Evan Cheng | 8763c1c | 2008-08-27 20:58:54 +0000 | [diff] [blame] | 908 | // MachineSink because the code is no longer in two-address form (at least |
Evan Cheng | df3b993 | 2008-08-27 20:33:50 +0000 | [diff] [blame] | 909 | // partially). |
| 910 | if (MO.isUse()) |
| 911 | return false; |
| 912 | else if (!MO.isDead() && MO.getReg() != DstReg) |
| 913 | return false; |
| 914 | } |
| 915 | return true; |
| 916 | } |
| 917 | |
Dan Gohman | 3e4fb70 | 2008-09-24 00:06:15 +0000 | [diff] [blame] | 918 | /// hasVolatileMemoryRef - Return true if this instruction may have a |
| 919 | /// volatile memory reference, or if the information describing the |
| 920 | /// memory reference is not available. Return false if it is known to |
| 921 | /// have no volatile memory references. |
| 922 | bool MachineInstr::hasVolatileMemoryRef() const { |
| 923 | // An instruction known never to access memory won't have a volatile access. |
| 924 | if (!TID->mayStore() && |
| 925 | !TID->mayLoad() && |
| 926 | !TID->isCall() && |
| 927 | !TID->hasUnmodeledSideEffects()) |
| 928 | return false; |
| 929 | |
| 930 | // Otherwise, if the instruction has no memory reference information, |
| 931 | // conservatively assume it wasn't preserved. |
| 932 | if (memoperands_empty()) |
| 933 | return true; |
| 934 | |
| 935 | // Check the memory reference information for volatile references. |
| 936 | for (std::list<MachineMemOperand>::const_iterator I = memoperands_begin(), |
| 937 | E = memoperands_end(); I != E; ++I) |
| 938 | if (I->isVolatile()) |
| 939 | return true; |
| 940 | |
| 941 | return false; |
| 942 | } |
| 943 | |
Brian Gaeke | 21326fc | 2004-02-13 04:39:32 +0000 | [diff] [blame] | 944 | void MachineInstr::dump() const { |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 945 | errs() << " " << *this; |
Mon P Wang | 5ca6bd1 | 2008-10-10 01:43:55 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const { |
Chris Lattner | e308789 | 2007-12-30 21:31:53 +0000 | [diff] [blame] | 949 | // Specialize printing if op#0 is definition |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 950 | unsigned StartOp = 0; |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 951 | if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) { |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 952 | getOperand(0).print(OS, TM); |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 953 | OS << " = "; |
| 954 | ++StartOp; // Don't print this operand again! |
| 955 | } |
Tanya Lattner | b140762 | 2004-06-25 00:13:11 +0000 | [diff] [blame] | 956 | |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 957 | OS << getDesc().getName(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 958 | |
Chris Lattner | 6a59227 | 2002-10-30 01:55:38 +0000 | [diff] [blame] | 959 | for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) { |
| 960 | if (i != StartOp) |
| 961 | OS << ","; |
| 962 | OS << " "; |
Chris Lattner | f738230 | 2007-12-30 21:56:09 +0000 | [diff] [blame] | 963 | getOperand(i).print(OS, TM); |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 964 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 965 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 966 | if (!memoperands_empty()) { |
Dan Gohman | 2bfe6ff | 2008-02-07 16:18:00 +0000 | [diff] [blame] | 967 | OS << ", Mem:"; |
Dan Gohman | fed90b6 | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 968 | for (std::list<MachineMemOperand>::const_iterator i = memoperands_begin(), |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 969 | e = memoperands_end(); i != e; ++i) { |
| 970 | const MachineMemOperand &MRO = *i; |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 971 | const Value *V = MRO.getValue(); |
| 972 | |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 973 | assert((MRO.isLoad() || MRO.isStore()) && |
| 974 | "SV has to be a load, store or both."); |
| 975 | |
| 976 | if (MRO.isVolatile()) |
| 977 | OS << "Volatile "; |
Dan Gohman | 2bfe6ff | 2008-02-07 16:18:00 +0000 | [diff] [blame] | 978 | |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 979 | if (MRO.isLoad()) |
Dan Gohman | 2bfe6ff | 2008-02-07 16:18:00 +0000 | [diff] [blame] | 980 | OS << "LD"; |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 981 | if (MRO.isStore()) |
Dan Gohman | 2bfe6ff | 2008-02-07 16:18:00 +0000 | [diff] [blame] | 982 | OS << "ST"; |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 983 | |
Evan Cheng | bbd8322 | 2008-02-08 22:05:07 +0000 | [diff] [blame] | 984 | OS << "(" << MRO.getSize() << "," << MRO.getAlignment() << ") ["; |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 985 | |
Dan Gohman | 2bfe6ff | 2008-02-07 16:18:00 +0000 | [diff] [blame] | 986 | if (!V) |
| 987 | OS << "<unknown>"; |
| 988 | else if (!V->getName().empty()) |
| 989 | OS << V->getName(); |
Chris Lattner | edfb72c | 2008-08-24 20:37:32 +0000 | [diff] [blame] | 990 | else if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) { |
Mon P Wang | 5ca6bd1 | 2008-10-10 01:43:55 +0000 | [diff] [blame] | 991 | PSV->print(OS); |
Chris Lattner | edfb72c | 2008-08-24 20:37:32 +0000 | [diff] [blame] | 992 | } else |
Dan Gohman | 2bfe6ff | 2008-02-07 16:18:00 +0000 | [diff] [blame] | 993 | OS << V; |
| 994 | |
| 995 | OS << " + " << MRO.getOffset() << "]"; |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | |
Bill Wendling | b5ef273 | 2009-02-19 21:44:55 +0000 | [diff] [blame] | 999 | if (!debugLoc.isUnknown()) { |
| 1000 | const MachineFunction *MF = getParent()->getParent(); |
| 1001 | DebugLocTuple DLT = MF->getDebugLocTuple(debugLoc); |
Argyrios Kyrtzidis | a26eae6 | 2009-04-30 23:22:31 +0000 | [diff] [blame] | 1002 | DICompileUnit CU(DLT.CompileUnit); |
| 1003 | std::string Dir, Fn; |
Bill Wendling | b5ef273 | 2009-02-19 21:44:55 +0000 | [diff] [blame] | 1004 | OS << " [dbg: " |
Argyrios Kyrtzidis | a26eae6 | 2009-04-30 23:22:31 +0000 | [diff] [blame] | 1005 | << CU.getDirectory(Dir) << '/' << CU.getFilename(Fn) << "," |
Bill Wendling | b5ef273 | 2009-02-19 21:44:55 +0000 | [diff] [blame] | 1006 | << DLT.Line << "," |
| 1007 | << DLT.Col << "]"; |
| 1008 | } |
| 1009 | |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 1010 | OS << "\n"; |
| 1011 | } |
| 1012 | |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1013 | bool MachineInstr::addRegisterKilled(unsigned IncomingReg, |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1014 | const TargetRegisterInfo *RegInfo, |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1015 | bool AddIfNotFound) { |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1016 | bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); |
Dan Gohman | 2ebc11a | 2008-07-03 01:18:51 +0000 | [diff] [blame] | 1017 | bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg); |
Dan Gohman | 3f62940 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1018 | bool Found = false; |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1019 | SmallVector<unsigned,4> DeadOps; |
Bill Wendling | 4a23d72 | 2008-03-03 22:14:33 +0000 | [diff] [blame] | 1020 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 1021 | MachineOperand &MO = getOperand(i); |
Jakob Stoklund Olesen | efb8e3e | 2009-08-04 20:09:25 +0000 | [diff] [blame] | 1022 | if (!MO.isReg() || !MO.isUse() || MO.isUndef()) |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1023 | continue; |
| 1024 | unsigned Reg = MO.getReg(); |
| 1025 | if (!Reg) |
| 1026 | continue; |
Bill Wendling | 4a23d72 | 2008-03-03 22:14:33 +0000 | [diff] [blame] | 1027 | |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1028 | if (Reg == IncomingReg) { |
Dan Gohman | 3f62940 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1029 | if (!Found) { |
| 1030 | if (MO.isKill()) |
| 1031 | // The register is already marked kill. |
| 1032 | return true; |
Jakob Stoklund Olesen | ece4818 | 2009-08-02 19:13:03 +0000 | [diff] [blame] | 1033 | if (isPhysReg && isRegTiedToDefOperand(i)) |
| 1034 | // Two-address uses of physregs must not be marked kill. |
| 1035 | return true; |
Dan Gohman | 3f62940 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1036 | MO.setIsKill(); |
| 1037 | Found = true; |
| 1038 | } |
| 1039 | } else if (hasAliases && MO.isKill() && |
| 1040 | TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1041 | // A super-register kill already exists. |
| 1042 | if (RegInfo->isSuperRegister(IncomingReg, Reg)) |
Dan Gohman | 2ebc11a | 2008-07-03 01:18:51 +0000 | [diff] [blame] | 1043 | return true; |
| 1044 | if (RegInfo->isSubRegister(IncomingReg, Reg)) |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1045 | DeadOps.push_back(i); |
Bill Wendling | 4a23d72 | 2008-03-03 22:14:33 +0000 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1049 | // Trim unneeded kill operands. |
| 1050 | while (!DeadOps.empty()) { |
| 1051 | unsigned OpIdx = DeadOps.back(); |
| 1052 | if (getOperand(OpIdx).isImplicit()) |
| 1053 | RemoveOperand(OpIdx); |
| 1054 | else |
| 1055 | getOperand(OpIdx).setIsKill(false); |
| 1056 | DeadOps.pop_back(); |
| 1057 | } |
| 1058 | |
Bill Wendling | 4a23d72 | 2008-03-03 22:14:33 +0000 | [diff] [blame] | 1059 | // 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] | 1060 | // new implicit operand if required. |
Dan Gohman | 3f62940 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1061 | if (!Found && AddIfNotFound) { |
Bill Wendling | 4a23d72 | 2008-03-03 22:14:33 +0000 | [diff] [blame] | 1062 | addOperand(MachineOperand::CreateReg(IncomingReg, |
| 1063 | false /*IsDef*/, |
| 1064 | true /*IsImp*/, |
| 1065 | true /*IsKill*/)); |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1066 | return true; |
| 1067 | } |
Dan Gohman | 3f62940 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1068 | return Found; |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | bool MachineInstr::addRegisterDead(unsigned IncomingReg, |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1072 | const TargetRegisterInfo *RegInfo, |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1073 | bool AddIfNotFound) { |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1074 | bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); |
Evan Cheng | 01b2e23 | 2008-06-27 22:11:49 +0000 | [diff] [blame] | 1075 | bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg); |
Dan Gohman | 3f62940 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1076 | bool Found = false; |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1077 | SmallVector<unsigned,4> DeadOps; |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1078 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 1079 | MachineOperand &MO = getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1080 | if (!MO.isReg() || !MO.isDef()) |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1081 | continue; |
| 1082 | unsigned Reg = MO.getReg(); |
Dan Gohman | 3f62940 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1083 | if (!Reg) |
| 1084 | continue; |
| 1085 | |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1086 | if (Reg == IncomingReg) { |
Dan Gohman | 3f62940 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1087 | if (!Found) { |
| 1088 | if (MO.isDead()) |
| 1089 | // The register is already marked dead. |
| 1090 | return true; |
| 1091 | MO.setIsDead(); |
| 1092 | Found = true; |
| 1093 | } |
| 1094 | } else if (hasAliases && MO.isDead() && |
| 1095 | TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1096 | // There exists a super-register that's marked dead. |
| 1097 | if (RegInfo->isSuperRegister(IncomingReg, Reg)) |
Dan Gohman | 2ebc11a | 2008-07-03 01:18:51 +0000 | [diff] [blame] | 1098 | return true; |
Owen Anderson | 22ae999 | 2008-08-14 18:34:18 +0000 | [diff] [blame] | 1099 | if (RegInfo->getSubRegisters(IncomingReg) && |
| 1100 | RegInfo->getSuperRegisters(Reg) && |
| 1101 | RegInfo->isSubRegister(IncomingReg, Reg)) |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1102 | DeadOps.push_back(i); |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1103 | } |
| 1104 | } |
| 1105 | |
Evan Cheng | 9b6d7b9 | 2008-04-16 09:41:59 +0000 | [diff] [blame] | 1106 | // Trim unneeded dead operands. |
| 1107 | while (!DeadOps.empty()) { |
| 1108 | unsigned OpIdx = DeadOps.back(); |
| 1109 | if (getOperand(OpIdx).isImplicit()) |
| 1110 | RemoveOperand(OpIdx); |
| 1111 | else |
| 1112 | getOperand(OpIdx).setIsDead(false); |
| 1113 | DeadOps.pop_back(); |
| 1114 | } |
| 1115 | |
Dan Gohman | 3f62940 | 2008-09-03 15:56:16 +0000 | [diff] [blame] | 1116 | // If not found, this means an alias of one of the operands is dead. Add a |
| 1117 | // new implicit operand if required. |
Chris Lattner | 3153061 | 2009-06-24 17:54:48 +0000 | [diff] [blame] | 1118 | if (Found || !AddIfNotFound) |
| 1119 | return Found; |
| 1120 | |
| 1121 | addOperand(MachineOperand::CreateReg(IncomingReg, |
| 1122 | true /*IsDef*/, |
| 1123 | true /*IsImp*/, |
| 1124 | false /*IsKill*/, |
| 1125 | true /*IsDead*/)); |
| 1126 | return true; |
Owen Anderson | b487e72 | 2008-01-24 01:10:07 +0000 | [diff] [blame] | 1127 | } |