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