Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 1 | //===---- ScheduleDAGEmit.cpp - Emit routines for the ScheduleDAG class ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements the Emit routines for the ScheduleDAG class, which creates |
| 11 | // MachineInstrs according to the computed schedule. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "pre-RA-sched" |
Dan Gohman | 84fbac5 | 2009-02-06 17:22:58 +0000 | [diff] [blame] | 16 | #include "ScheduleDAGSDNodes.h" |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 18 | #include "llvm/CodeGen/MachineFunction.h" |
| 19 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 21 | #include "llvm/Target/TargetData.h" |
| 22 | #include "llvm/Target/TargetMachine.h" |
| 23 | #include "llvm/Target/TargetInstrInfo.h" |
| 24 | #include "llvm/Target/TargetLowering.h" |
| 25 | #include "llvm/ADT/Statistic.h" |
| 26 | #include "llvm/Support/CommandLine.h" |
| 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Support/MathExtras.h" |
| 29 | using namespace llvm; |
| 30 | |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 31 | /// getInstrOperandRegClass - Return register class of the operand of an |
| 32 | /// instruction of the specified TargetInstrDesc. |
| 33 | static const TargetRegisterClass* |
| 34 | getInstrOperandRegClass(const TargetRegisterInfo *TRI, |
Evan Cheng | 770bcc7 | 2009-02-06 17:43:24 +0000 | [diff] [blame] | 35 | const TargetInstrDesc &II, unsigned Op) { |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 36 | if (Op >= II.getNumOperands()) { |
| 37 | assert(II.isVariadic() && "Invalid operand # of instruction"); |
| 38 | return NULL; |
| 39 | } |
| 40 | if (II.OpInfo[Op].isLookupPtrRegClass()) |
Evan Cheng | 770bcc7 | 2009-02-06 17:43:24 +0000 | [diff] [blame] | 41 | return TRI->getPointerRegClass(); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 42 | return TRI->getRegClass(II.OpInfo[Op].RegClass); |
| 43 | } |
| 44 | |
| 45 | /// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an |
| 46 | /// implicit physical register output. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 47 | void ScheduleDAGSDNodes::EmitCopyFromReg(SDNode *Node, unsigned ResNo, |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame] | 48 | bool IsClone, bool IsCloned, |
| 49 | unsigned SrcReg, |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 50 | DenseMap<SDValue, unsigned> &VRBaseMap) { |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 51 | unsigned VRBase = 0; |
| 52 | if (TargetRegisterInfo::isVirtualRegister(SrcReg)) { |
| 53 | // Just use the input register directly! |
| 54 | SDValue Op(Node, ResNo); |
| 55 | if (IsClone) |
| 56 | VRBaseMap.erase(Op); |
| 57 | bool isNew = VRBaseMap.insert(std::make_pair(Op, SrcReg)).second; |
| 58 | isNew = isNew; // Silence compiler warning. |
| 59 | assert(isNew && "Node emitted out of order - early"); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | // If the node is only used by a CopyToReg and the dest reg is a vreg, use |
| 64 | // the CopyToReg'd destination register instead of creating a new vreg. |
| 65 | bool MatchReg = true; |
Evan Cheng | 1cd3327 | 2008-09-16 23:12:11 +0000 | [diff] [blame] | 66 | const TargetRegisterClass *UseRC = NULL; |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame] | 67 | if (!IsClone && !IsCloned) |
| 68 | for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end(); |
| 69 | UI != E; ++UI) { |
| 70 | SDNode *User = *UI; |
| 71 | bool Match = true; |
| 72 | if (User->getOpcode() == ISD::CopyToReg && |
| 73 | User->getOperand(2).getNode() == Node && |
| 74 | User->getOperand(2).getResNo() == ResNo) { |
| 75 | unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg(); |
| 76 | if (TargetRegisterInfo::isVirtualRegister(DestReg)) { |
| 77 | VRBase = DestReg; |
| 78 | Match = false; |
| 79 | } else if (DestReg != SrcReg) |
| 80 | Match = false; |
| 81 | } else { |
| 82 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) { |
| 83 | SDValue Op = User->getOperand(i); |
| 84 | if (Op.getNode() != Node || Op.getResNo() != ResNo) |
| 85 | continue; |
| 86 | MVT VT = Node->getValueType(Op.getResNo()); |
| 87 | if (VT == MVT::Other || VT == MVT::Flag) |
| 88 | continue; |
| 89 | Match = false; |
| 90 | if (User->isMachineOpcode()) { |
| 91 | const TargetInstrDesc &II = TII->get(User->getMachineOpcode()); |
| 92 | const TargetRegisterClass *RC = |
Evan Cheng | 770bcc7 | 2009-02-06 17:43:24 +0000 | [diff] [blame] | 93 | getInstrOperandRegClass(TRI, II, i+II.getNumDefs()); |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame] | 94 | if (!UseRC) |
| 95 | UseRC = RC; |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 96 | else if (RC) { |
| 97 | if (UseRC->hasSuperClass(RC)) |
| 98 | UseRC = RC; |
| 99 | else |
| 100 | assert((UseRC == RC || RC->hasSuperClass(UseRC)) && |
| 101 | "Multiple uses expecting different register classes!"); |
| 102 | } |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame] | 103 | } |
Evan Cheng | 1cd3327 | 2008-09-16 23:12:11 +0000 | [diff] [blame] | 104 | } |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 105 | } |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame] | 106 | MatchReg &= Match; |
| 107 | if (VRBase) |
| 108 | break; |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 109 | } |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 110 | |
Evan Cheng | 1cd3327 | 2008-09-16 23:12:11 +0000 | [diff] [blame] | 111 | MVT VT = Node->getValueType(ResNo); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 112 | const TargetRegisterClass *SrcRC = 0, *DstRC = 0; |
Evan Cheng | 1cd3327 | 2008-09-16 23:12:11 +0000 | [diff] [blame] | 113 | SrcRC = TRI->getPhysicalRegisterRegClass(SrcReg, VT); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 114 | |
| 115 | // Figure out the register class to create for the destreg. |
| 116 | if (VRBase) { |
| 117 | DstRC = MRI.getRegClass(VRBase); |
Evan Cheng | 1cd3327 | 2008-09-16 23:12:11 +0000 | [diff] [blame] | 118 | } else if (UseRC) { |
| 119 | assert(UseRC->hasType(VT) && "Incompatible phys register def and uses!"); |
| 120 | DstRC = UseRC; |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 121 | } else { |
Evan Cheng | 1cd3327 | 2008-09-16 23:12:11 +0000 | [diff] [blame] | 122 | DstRC = TLI->getRegClassFor(VT); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // If all uses are reading from the src physical register and copying the |
| 126 | // register is either impossible or very expensive, then don't create a copy. |
| 127 | if (MatchReg && SrcRC->getCopyCost() < 0) { |
| 128 | VRBase = SrcReg; |
| 129 | } else { |
| 130 | // Create the reg, emit the copy. |
| 131 | VRBase = MRI.createVirtualRegister(DstRC); |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 132 | bool Emitted = TII->copyRegToReg(*BB, InsertPos, VRBase, SrcReg, |
| 133 | DstRC, SrcRC); |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 134 | // If the target didn't handle the copy with different register |
| 135 | // classes and the destination is a subset of the source, |
| 136 | // try a normal same-RC copy. |
| 137 | if (!Emitted && DstRC->hasSuperClass(SrcRC)) |
| 138 | Emitted = TII->copyRegToReg(*BB, InsertPos, VRBase, SrcReg, |
| 139 | SrcRC, SrcRC); |
| 140 | |
| 141 | assert(Emitted && "Unable to issue a copy instruction!\n"); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | SDValue Op(Node, ResNo); |
| 145 | if (IsClone) |
| 146 | VRBaseMap.erase(Op); |
| 147 | bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second; |
| 148 | isNew = isNew; // Silence compiler warning. |
| 149 | assert(isNew && "Node emitted out of order - early"); |
| 150 | } |
| 151 | |
| 152 | /// getDstOfCopyToRegUse - If the only use of the specified result number of |
| 153 | /// node is a CopyToReg, return its destination register. Return 0 otherwise. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 154 | unsigned ScheduleDAGSDNodes::getDstOfOnlyCopyToRegUse(SDNode *Node, |
| 155 | unsigned ResNo) const { |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 156 | if (!Node->hasOneUse()) |
| 157 | return 0; |
| 158 | |
| 159 | SDNode *User = *Node->use_begin(); |
| 160 | if (User->getOpcode() == ISD::CopyToReg && |
| 161 | User->getOperand(2).getNode() == Node && |
| 162 | User->getOperand(2).getResNo() == ResNo) { |
| 163 | unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg(); |
| 164 | if (TargetRegisterInfo::isVirtualRegister(Reg)) |
| 165 | return Reg; |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 170 | void ScheduleDAGSDNodes::CreateVirtualRegisters(SDNode *Node, MachineInstr *MI, |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame] | 171 | const TargetInstrDesc &II, |
| 172 | bool IsClone, bool IsCloned, |
Evan Cheng | 5c3c5a4 | 2009-01-09 22:44:02 +0000 | [diff] [blame] | 173 | DenseMap<SDValue, unsigned> &VRBaseMap) { |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 174 | assert(Node->getMachineOpcode() != TargetInstrInfo::IMPLICIT_DEF && |
| 175 | "IMPLICIT_DEF should have been handled as a special case elsewhere!"); |
| 176 | |
| 177 | for (unsigned i = 0; i < II.getNumDefs(); ++i) { |
| 178 | // If the specific node value is only used by a CopyToReg and the dest reg |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 179 | // is a vreg in the same register class, use the CopyToReg'd destination |
| 180 | // register instead of creating a new vreg. |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 181 | unsigned VRBase = 0; |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 182 | const TargetRegisterClass *RC = getInstrOperandRegClass(TRI, II, i); |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame] | 183 | |
| 184 | if (!IsClone && !IsCloned) |
| 185 | for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end(); |
| 186 | UI != E; ++UI) { |
| 187 | SDNode *User = *UI; |
| 188 | if (User->getOpcode() == ISD::CopyToReg && |
| 189 | User->getOperand(2).getNode() == Node && |
| 190 | User->getOperand(2).getResNo() == i) { |
| 191 | unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg(); |
| 192 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 193 | const TargetRegisterClass *RegRC = MRI.getRegClass(Reg); |
| 194 | if (RegRC == RC) { |
| 195 | VRBase = Reg; |
| 196 | MI->addOperand(MachineOperand::CreateReg(Reg, true)); |
| 197 | break; |
| 198 | } |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame] | 199 | } |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 202 | |
| 203 | // Create the result registers for this node and add the result regs to |
| 204 | // the machine instruction. |
| 205 | if (VRBase == 0) { |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 206 | assert(RC && "Isn't a register operand!"); |
| 207 | VRBase = MRI.createVirtualRegister(RC); |
| 208 | MI->addOperand(MachineOperand::CreateReg(VRBase, true)); |
| 209 | } |
| 210 | |
| 211 | SDValue Op(Node, i); |
Evan Cheng | 5c3c5a4 | 2009-01-09 22:44:02 +0000 | [diff] [blame] | 212 | if (IsClone) |
| 213 | VRBaseMap.erase(Op); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 214 | bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second; |
| 215 | isNew = isNew; // Silence compiler warning. |
| 216 | assert(isNew && "Node emitted out of order - early"); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /// getVR - Return the virtual register corresponding to the specified result |
| 221 | /// of the specified node. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 222 | unsigned ScheduleDAGSDNodes::getVR(SDValue Op, |
| 223 | DenseMap<SDValue, unsigned> &VRBaseMap) { |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 224 | if (Op.isMachineOpcode() && |
| 225 | Op.getMachineOpcode() == TargetInstrInfo::IMPLICIT_DEF) { |
| 226 | // Add an IMPLICIT_DEF instruction before every use. |
| 227 | unsigned VReg = getDstOfOnlyCopyToRegUse(Op.getNode(), Op.getResNo()); |
| 228 | // IMPLICIT_DEF can produce any type of result so its TargetInstrDesc |
| 229 | // does not include operand register class info. |
| 230 | if (!VReg) { |
| 231 | const TargetRegisterClass *RC = TLI->getRegClassFor(Op.getValueType()); |
| 232 | VReg = MRI.createVirtualRegister(RC); |
| 233 | } |
Bill Wendling | f2ad58d | 2009-02-03 01:02:39 +0000 | [diff] [blame] | 234 | BuildMI(BB, Op.getDebugLoc(), TII->get(TargetInstrInfo::IMPLICIT_DEF),VReg); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 235 | return VReg; |
| 236 | } |
| 237 | |
| 238 | DenseMap<SDValue, unsigned>::iterator I = VRBaseMap.find(Op); |
| 239 | assert(I != VRBaseMap.end() && "Node emitted out of order - late"); |
| 240 | return I->second; |
| 241 | } |
| 242 | |
| 243 | |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 244 | /// AddRegisterOperand - Add the specified register as an operand to the |
| 245 | /// specified machine instr. Insert register copies if the register is |
| 246 | /// not in the required register class. |
| 247 | void |
| 248 | ScheduleDAGSDNodes::AddRegisterOperand(MachineInstr *MI, SDValue Op, |
| 249 | unsigned IIOpNum, |
| 250 | const TargetInstrDesc *II, |
| 251 | DenseMap<SDValue, unsigned> &VRBaseMap) { |
| 252 | assert(Op.getValueType() != MVT::Other && |
| 253 | Op.getValueType() != MVT::Flag && |
| 254 | "Chain and flag operands should occur at end of operand list!"); |
| 255 | // Get/emit the operand. |
| 256 | unsigned VReg = getVR(Op, VRBaseMap); |
| 257 | assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?"); |
| 258 | |
| 259 | const TargetInstrDesc &TID = MI->getDesc(); |
| 260 | bool isOptDef = IIOpNum < TID.getNumOperands() && |
| 261 | TID.OpInfo[IIOpNum].isOptionalDef(); |
| 262 | |
| 263 | // If the instruction requires a register in a different class, create |
| 264 | // a new virtual register and copy the value into it. |
| 265 | if (II) { |
| 266 | const TargetRegisterClass *SrcRC = |
| 267 | MRI.getRegClass(VReg); |
| 268 | const TargetRegisterClass *DstRC = |
| 269 | getInstrOperandRegClass(TRI, *II, IIOpNum); |
| 270 | assert((DstRC || (TID.isVariadic() && IIOpNum >= TID.getNumOperands())) && |
| 271 | "Don't have operand info for this instruction!"); |
| 272 | if (DstRC && SrcRC != DstRC && !SrcRC->hasSuperClass(DstRC)) { |
| 273 | unsigned NewVReg = MRI.createVirtualRegister(DstRC); |
| 274 | bool Emitted = TII->copyRegToReg(*BB, InsertPos, NewVReg, VReg, |
| 275 | DstRC, SrcRC); |
| 276 | // If the target didn't handle the copy with different register |
| 277 | // classes and the destination is a subset of the source, |
| 278 | // try a normal same-RC copy. |
| 279 | if (!Emitted && DstRC->hasSuperClass(SrcRC)) |
| 280 | Emitted = TII->copyRegToReg(*BB, InsertPos, NewVReg, VReg, |
| 281 | SrcRC, SrcRC); |
| 282 | assert(Emitted && "Unable to issue a copy instruction!\n"); |
| 283 | VReg = NewVReg; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef)); |
| 288 | } |
| 289 | |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 290 | /// AddOperand - Add the specified operand to the specified machine instr. II |
| 291 | /// specifies the instruction information for the node, and IIOpNum is the |
| 292 | /// operand number (in the II) that we are adding. IIOpNum and II are used for |
| 293 | /// assertions only. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 294 | void ScheduleDAGSDNodes::AddOperand(MachineInstr *MI, SDValue Op, |
| 295 | unsigned IIOpNum, |
| 296 | const TargetInstrDesc *II, |
| 297 | DenseMap<SDValue, unsigned> &VRBaseMap) { |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 298 | if (Op.isMachineOpcode()) { |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 299 | AddRegisterOperand(MI, Op, IIOpNum, II, VRBaseMap); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 300 | } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 301 | MI->addOperand(MachineOperand::CreateImm(C->getZExtValue())); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 302 | } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) { |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 303 | const ConstantFP *CFP = F->getConstantFPValue(); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 304 | MI->addOperand(MachineOperand::CreateFPImm(CFP)); |
| 305 | } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) { |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 306 | MI->addOperand(MachineOperand::CreateReg(R->getReg(), false)); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 307 | } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) { |
| 308 | MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(),TGA->getOffset())); |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 309 | } else if (BasicBlockSDNode *BBNode = dyn_cast<BasicBlockSDNode>(Op)) { |
| 310 | MI->addOperand(MachineOperand::CreateMBB(BBNode->getBasicBlock())); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 311 | } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) { |
| 312 | MI->addOperand(MachineOperand::CreateFI(FI->getIndex())); |
| 313 | } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) { |
| 314 | MI->addOperand(MachineOperand::CreateJTI(JT->getIndex())); |
| 315 | } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) { |
| 316 | int Offset = CP->getOffset(); |
| 317 | unsigned Align = CP->getAlignment(); |
| 318 | const Type *Type = CP->getType(); |
| 319 | // MachineConstantPool wants an explicit alignment. |
| 320 | if (Align == 0) { |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 321 | Align = TM.getTargetData()->getPrefTypeAlignment(Type); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 322 | if (Align == 0) { |
| 323 | // Alignment of vector types. FIXME! |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 324 | Align = TM.getTargetData()->getTypePaddedSize(Type); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
| 328 | unsigned Idx; |
| 329 | if (CP->isMachineConstantPoolEntry()) |
| 330 | Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align); |
| 331 | else |
| 332 | Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align); |
| 333 | MI->addOperand(MachineOperand::CreateCPI(Idx, Offset)); |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 334 | } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) { |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 335 | MI->addOperand(MachineOperand::CreateES(ES->getSymbol())); |
| 336 | } else { |
| 337 | assert(Op.getValueType() != MVT::Other && |
| 338 | Op.getValueType() != MVT::Flag && |
| 339 | "Chain and flag operands should occur at end of operand list!"); |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 340 | AddRegisterOperand(MI, Op, IIOpNum, II, VRBaseMap); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /// getSubRegisterRegClass - Returns the register class of specified register |
| 345 | /// class' "SubIdx"'th sub-register class. |
| 346 | static const TargetRegisterClass* |
| 347 | getSubRegisterRegClass(const TargetRegisterClass *TRC, unsigned SubIdx) { |
| 348 | // Pick the register class of the subregister |
| 349 | TargetRegisterInfo::regclass_iterator I = |
| 350 | TRC->subregclasses_begin() + SubIdx-1; |
| 351 | assert(I < TRC->subregclasses_end() && |
| 352 | "Invalid subregister index for register class"); |
| 353 | return *I; |
| 354 | } |
| 355 | |
| 356 | /// getSuperRegisterRegClass - Returns the register class of a superreg A whose |
| 357 | /// "SubIdx"'th sub-register class is the specified register class and whose |
| 358 | /// type matches the specified type. |
| 359 | static const TargetRegisterClass* |
| 360 | getSuperRegisterRegClass(const TargetRegisterClass *TRC, |
| 361 | unsigned SubIdx, MVT VT) { |
| 362 | // Pick the register class of the superegister for this type |
| 363 | for (TargetRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(), |
| 364 | E = TRC->superregclasses_end(); I != E; ++I) |
| 365 | if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC) |
| 366 | return *I; |
| 367 | assert(false && "Couldn't find the register class"); |
| 368 | return 0; |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 371 | /// EmitSubregNode - Generate machine code for subreg nodes. |
| 372 | /// |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 373 | void ScheduleDAGSDNodes::EmitSubregNode(SDNode *Node, |
| 374 | DenseMap<SDValue, unsigned> &VRBaseMap) { |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 375 | unsigned VRBase = 0; |
| 376 | unsigned Opc = Node->getMachineOpcode(); |
| 377 | |
| 378 | // If the node is only used by a CopyToReg and the dest reg is a vreg, use |
| 379 | // the CopyToReg'd destination register instead of creating a new vreg. |
| 380 | for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end(); |
| 381 | UI != E; ++UI) { |
| 382 | SDNode *User = *UI; |
| 383 | if (User->getOpcode() == ISD::CopyToReg && |
| 384 | User->getOperand(2).getNode() == Node) { |
| 385 | unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg(); |
| 386 | if (TargetRegisterInfo::isVirtualRegister(DestReg)) { |
| 387 | VRBase = DestReg; |
| 388 | break; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | if (Opc == TargetInstrInfo::EXTRACT_SUBREG) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 394 | unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue(); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 395 | |
| 396 | // Create the extract_subreg machine instruction. |
Bill Wendling | f2ad58d | 2009-02-03 01:02:39 +0000 | [diff] [blame] | 397 | MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(), |
| 398 | TII->get(TargetInstrInfo::EXTRACT_SUBREG)); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 399 | |
| 400 | // Figure out the register class to create for the destreg. |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 401 | unsigned VReg = getVR(Node->getOperand(0), VRBaseMap); |
| 402 | const TargetRegisterClass *TRC = MRI.getRegClass(VReg); |
| 403 | const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 404 | |
| 405 | if (VRBase) { |
| 406 | // Grab the destination register |
| 407 | #ifndef NDEBUG |
| 408 | const TargetRegisterClass *DRC = MRI.getRegClass(VRBase); |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 409 | assert(SRC && DRC && (SRC == DRC || DRC->hasSubClass(SRC)) && |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 410 | "Source subregister and destination must have the same class"); |
| 411 | #endif |
| 412 | } else { |
| 413 | // Create the reg |
| 414 | assert(SRC && "Couldn't find source register class"); |
| 415 | VRBase = MRI.createVirtualRegister(SRC); |
| 416 | } |
| 417 | |
| 418 | // Add def, source, and subreg index |
| 419 | MI->addOperand(MachineOperand::CreateReg(VRBase, true)); |
| 420 | AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap); |
| 421 | MI->addOperand(MachineOperand::CreateImm(SubIdx)); |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 422 | BB->insert(InsertPos, MI); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 423 | } else if (Opc == TargetInstrInfo::INSERT_SUBREG || |
| 424 | Opc == TargetInstrInfo::SUBREG_TO_REG) { |
| 425 | SDValue N0 = Node->getOperand(0); |
| 426 | SDValue N1 = Node->getOperand(1); |
| 427 | SDValue N2 = Node->getOperand(2); |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 428 | unsigned SubReg = getVR(N1, VRBaseMap); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 429 | unsigned SubIdx = cast<ConstantSDNode>(N2)->getZExtValue(); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 430 | |
| 431 | |
| 432 | // Figure out the register class to create for the destreg. |
| 433 | const TargetRegisterClass *TRC = 0; |
| 434 | if (VRBase) { |
| 435 | TRC = MRI.getRegClass(VRBase); |
| 436 | } else { |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 437 | TRC = getSuperRegisterRegClass(MRI.getRegClass(SubReg), SubIdx, |
| 438 | Node->getValueType(0)); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 439 | assert(TRC && "Couldn't determine register class for insert_subreg"); |
| 440 | VRBase = MRI.createVirtualRegister(TRC); // Create the reg |
| 441 | } |
| 442 | |
| 443 | // Create the insert_subreg or subreg_to_reg machine instruction. |
Bill Wendling | f2ad58d | 2009-02-03 01:02:39 +0000 | [diff] [blame] | 444 | MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(), TII->get(Opc)); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 445 | MI->addOperand(MachineOperand::CreateReg(VRBase, true)); |
| 446 | |
| 447 | // If creating a subreg_to_reg, then the first input operand |
| 448 | // is an implicit value immediate, otherwise it's a register |
| 449 | if (Opc == TargetInstrInfo::SUBREG_TO_REG) { |
| 450 | const ConstantSDNode *SD = cast<ConstantSDNode>(N0); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 451 | MI->addOperand(MachineOperand::CreateImm(SD->getZExtValue())); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 452 | } else |
| 453 | AddOperand(MI, N0, 0, 0, VRBaseMap); |
| 454 | // Add the subregster being inserted |
| 455 | AddOperand(MI, N1, 0, 0, VRBaseMap); |
| 456 | MI->addOperand(MachineOperand::CreateImm(SubIdx)); |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 457 | BB->insert(InsertPos, MI); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 458 | } else |
| 459 | assert(0 && "Node is not insert_subreg, extract_subreg, or subreg_to_reg"); |
| 460 | |
| 461 | SDValue Op(Node, 0); |
| 462 | bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second; |
| 463 | isNew = isNew; // Silence compiler warning. |
| 464 | assert(isNew && "Node emitted out of order - early"); |
| 465 | } |
| 466 | |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 467 | /// EmitCopyToSubclassNode - Generate machine code for COPY_TO_SUBCLASS nodes. |
| 468 | /// COPY_TO_SUBCLASS is just a normal copy, except that the destination |
| 469 | /// register is constrained to be in a particular register class. |
| 470 | /// |
| 471 | void |
| 472 | ScheduleDAGSDNodes::EmitCopyToSubclassNode(SDNode *Node, |
| 473 | DenseMap<SDValue, unsigned> &VRBaseMap) { |
| 474 | unsigned VReg = getVR(Node->getOperand(0), VRBaseMap); |
| 475 | const TargetRegisterClass *SrcRC = MRI.getRegClass(VReg); |
| 476 | |
| 477 | unsigned DstRCIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue(); |
| 478 | const TargetRegisterClass *DstRC = TRI->getRegClass(DstRCIdx); |
| 479 | |
| 480 | assert(SrcRC->hasSubClass(DstRC) && |
| 481 | "COPY_TO_SUBCLASS destination class is not a proper subclass!"); |
| 482 | |
| 483 | // Create the new VReg in the destination class and emit a copy. |
| 484 | unsigned NewVReg = MRI.createVirtualRegister(DstRC); |
| 485 | bool Emitted = TII->copyRegToReg(*BB, InsertPos, NewVReg, VReg, |
| 486 | DstRC, SrcRC); |
| 487 | // If the target didn't handle that, emit a plain copy. |
| 488 | if (!Emitted) |
| 489 | Emitted = TII->copyRegToReg(*BB, InsertPos, NewVReg, VReg, |
| 490 | SrcRC, SrcRC); |
| 491 | assert(Emitted && |
| 492 | "Unable to issue a copy instruction for a COPY_TO_SUBCLASS node!\n"); |
| 493 | |
| 494 | SDValue Op(Node, 0); |
| 495 | bool isNew = VRBaseMap.insert(std::make_pair(Op, NewVReg)).second; |
| 496 | isNew = isNew; // Silence compiler warning. |
| 497 | assert(isNew && "Node emitted out of order - early"); |
| 498 | } |
| 499 | |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 500 | /// EmitNode - Generate machine code for an node and needed dependencies. |
| 501 | /// |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame] | 502 | void ScheduleDAGSDNodes::EmitNode(SDNode *Node, bool IsClone, bool IsCloned, |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 503 | DenseMap<SDValue, unsigned> &VRBaseMap) { |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 504 | // If machine instruction |
| 505 | if (Node->isMachineOpcode()) { |
| 506 | unsigned Opc = Node->getMachineOpcode(); |
| 507 | |
| 508 | // Handle subreg insert/extract specially |
| 509 | if (Opc == TargetInstrInfo::EXTRACT_SUBREG || |
| 510 | Opc == TargetInstrInfo::INSERT_SUBREG || |
| 511 | Opc == TargetInstrInfo::SUBREG_TO_REG) { |
| 512 | EmitSubregNode(Node, VRBaseMap); |
| 513 | return; |
| 514 | } |
| 515 | |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 516 | // Handle COPY_TO_SUBCLASS specially. |
| 517 | if (Opc == TargetInstrInfo::COPY_TO_SUBCLASS) { |
| 518 | EmitCopyToSubclassNode(Node, VRBaseMap); |
| 519 | return; |
| 520 | } |
| 521 | |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 522 | if (Opc == TargetInstrInfo::IMPLICIT_DEF) |
| 523 | // We want a unique VR for each IMPLICIT_DEF use. |
| 524 | return; |
| 525 | |
| 526 | const TargetInstrDesc &II = TII->get(Opc); |
| 527 | unsigned NumResults = CountResults(Node); |
| 528 | unsigned NodeOperands = CountOperands(Node); |
| 529 | unsigned MemOperandsEnd = ComputeMemOperandsEnd(Node); |
| 530 | bool HasPhysRegOuts = (NumResults > II.getNumDefs()) && |
| 531 | II.getImplicitDefs() != 0; |
| 532 | #ifndef NDEBUG |
| 533 | unsigned NumMIOperands = NodeOperands + NumResults; |
| 534 | assert((II.getNumOperands() == NumMIOperands || |
| 535 | HasPhysRegOuts || II.isVariadic()) && |
| 536 | "#operands for dag node doesn't match .td file!"); |
| 537 | #endif |
| 538 | |
| 539 | // Create the new machine instruction. |
Bill Wendling | f2ad58d | 2009-02-03 01:02:39 +0000 | [diff] [blame] | 540 | MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(), II); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 541 | |
| 542 | // Add result register values for things that are defined by this |
| 543 | // instruction. |
| 544 | if (NumResults) |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame] | 545 | CreateVirtualRegisters(Node, MI, II, IsClone, IsCloned, VRBaseMap); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 546 | |
| 547 | // Emit all of the actual operands of this instruction, adding them to the |
| 548 | // instruction as appropriate. |
| 549 | for (unsigned i = 0; i != NodeOperands; ++i) |
| 550 | AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap); |
| 551 | |
| 552 | // Emit all of the memory operands of this instruction |
| 553 | for (unsigned i = NodeOperands; i != MemOperandsEnd; ++i) |
| 554 | AddMemOperand(MI, cast<MemOperandSDNode>(Node->getOperand(i))->MO); |
| 555 | |
Dan Gohman | f711939 | 2009-01-16 22:10:20 +0000 | [diff] [blame] | 556 | if (II.usesCustomDAGSchedInsertionHook()) { |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 557 | // Insert this instruction into the basic block using a target |
| 558 | // specific inserter which may returns a new basic block. |
| 559 | BB = TLI->EmitInstrWithCustomInserter(MI, BB); |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 560 | InsertPos = BB->end(); |
Bill Wendling | f2ad58d | 2009-02-03 01:02:39 +0000 | [diff] [blame] | 561 | } else { |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 562 | BB->insert(InsertPos, MI); |
Bill Wendling | f2ad58d | 2009-02-03 01:02:39 +0000 | [diff] [blame] | 563 | } |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 564 | |
| 565 | // Additional results must be an physical register def. |
| 566 | if (HasPhysRegOuts) { |
| 567 | for (unsigned i = II.getNumDefs(); i < NumResults; ++i) { |
| 568 | unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()]; |
| 569 | if (Node->hasAnyUseOfValue(i)) |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame] | 570 | EmitCopyFromReg(Node, i, IsClone, IsCloned, Reg, VRBaseMap); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | return; |
| 574 | } |
| 575 | |
| 576 | switch (Node->getOpcode()) { |
| 577 | default: |
| 578 | #ifndef NDEBUG |
Dan Gohman | a23b3b8 | 2008-11-13 21:21:28 +0000 | [diff] [blame] | 579 | Node->dump(DAG); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 580 | #endif |
| 581 | assert(0 && "This target-independent node should have been selected!"); |
| 582 | break; |
| 583 | case ISD::EntryToken: |
| 584 | assert(0 && "EntryToken should have been excluded from the schedule!"); |
| 585 | break; |
| 586 | case ISD::TokenFactor: // fall thru |
| 587 | break; |
| 588 | case ISD::CopyToReg: { |
| 589 | unsigned SrcReg; |
| 590 | SDValue SrcVal = Node->getOperand(2); |
| 591 | if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal)) |
| 592 | SrcReg = R->getReg(); |
| 593 | else |
| 594 | SrcReg = getVR(SrcVal, VRBaseMap); |
| 595 | |
| 596 | unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg(); |
| 597 | if (SrcReg == DestReg) // Coalesced away the copy? Ignore. |
| 598 | break; |
| 599 | |
| 600 | const TargetRegisterClass *SrcTRC = 0, *DstTRC = 0; |
| 601 | // Get the register classes of the src/dst. |
| 602 | if (TargetRegisterInfo::isVirtualRegister(SrcReg)) |
| 603 | SrcTRC = MRI.getRegClass(SrcReg); |
| 604 | else |
| 605 | SrcTRC = TRI->getPhysicalRegisterRegClass(SrcReg,SrcVal.getValueType()); |
| 606 | |
| 607 | if (TargetRegisterInfo::isVirtualRegister(DestReg)) |
| 608 | DstTRC = MRI.getRegClass(DestReg); |
| 609 | else |
| 610 | DstTRC = TRI->getPhysicalRegisterRegClass(DestReg, |
| 611 | Node->getOperand(1).getValueType()); |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 612 | |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 613 | bool Emitted = TII->copyRegToReg(*BB, InsertPos, DestReg, SrcReg, |
| 614 | DstTRC, SrcTRC); |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 615 | // If the target didn't handle the copy with different register |
| 616 | // classes and the destination is a subset of the source, |
| 617 | // try a normal same-RC copy. |
| 618 | if (!Emitted && DstTRC->hasSubClass(SrcTRC)) |
| 619 | Emitted = TII->copyRegToReg(*BB, InsertPos, DestReg, SrcReg, |
| 620 | DstTRC, DstTRC); |
| 621 | |
| 622 | assert(Emitted && "Unable to issue a copy instruction!\n"); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 623 | break; |
| 624 | } |
| 625 | case ISD::CopyFromReg: { |
| 626 | unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg(); |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame] | 627 | EmitCopyFromReg(Node, 0, IsClone, IsCloned, SrcReg, VRBaseMap); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 628 | break; |
| 629 | } |
| 630 | case ISD::INLINEASM: { |
| 631 | unsigned NumOps = Node->getNumOperands(); |
| 632 | if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag) |
| 633 | --NumOps; // Ignore the flag operand. |
| 634 | |
| 635 | // Create the inline asm machine instruction. |
Bill Wendling | f2ad58d | 2009-02-03 01:02:39 +0000 | [diff] [blame] | 636 | MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(), |
| 637 | TII->get(TargetInstrInfo::INLINEASM)); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 638 | |
| 639 | // Add the asm string as an external symbol operand. |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 640 | const char *AsmStr = |
| 641 | cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol(); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 642 | MI->addOperand(MachineOperand::CreateES(AsmStr)); |
| 643 | |
| 644 | // Add all of the operand registers to the instruction. |
| 645 | for (unsigned i = 2; i != NumOps;) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 646 | unsigned Flags = |
| 647 | cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue(); |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 648 | unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 649 | |
| 650 | MI->addOperand(MachineOperand::CreateImm(Flags)); |
| 651 | ++i; // Skip the ID value. |
| 652 | |
| 653 | switch (Flags & 7) { |
| 654 | default: assert(0 && "Bad flags!"); |
| 655 | case 2: // Def of register. |
| 656 | for (; NumVals; --NumVals, ++i) { |
| 657 | unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg(); |
| 658 | MI->addOperand(MachineOperand::CreateReg(Reg, true)); |
| 659 | } |
| 660 | break; |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 661 | case 6: // Def of earlyclobber register. |
| 662 | for (; NumVals; --NumVals, ++i) { |
| 663 | unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg(); |
| 664 | MI->addOperand(MachineOperand::CreateReg(Reg, true, false, false, |
| 665 | false, 0, true)); |
| 666 | } |
| 667 | break; |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 668 | case 1: // Use of register. |
| 669 | case 3: // Immediate. |
| 670 | case 4: // Addressing mode. |
| 671 | // The addressing mode has been selected, just add all of the |
| 672 | // operands to the machine instruction. |
| 673 | for (; NumVals; --NumVals, ++i) |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 674 | AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 675 | break; |
| 676 | } |
| 677 | } |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 678 | BB->insert(InsertPos, MI); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 679 | break; |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 684 | /// EmitSchedule - Emit the machine code in scheduled order. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 685 | MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() { |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 686 | DenseMap<SDValue, unsigned> VRBaseMap; |
| 687 | DenseMap<SUnit*, unsigned> CopyVRBaseMap; |
| 688 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 689 | SUnit *SU = Sequence[i]; |
| 690 | if (!SU) { |
| 691 | // Null SUnit* is a noop. |
| 692 | EmitNoop(); |
| 693 | continue; |
| 694 | } |
Dan Gohman | f449bf3 | 2008-11-14 00:06:09 +0000 | [diff] [blame] | 695 | |
Dan Gohman | f449bf3 | 2008-11-14 00:06:09 +0000 | [diff] [blame] | 696 | // For pre-regalloc scheduling, create instructions corresponding to the |
| 697 | // SDNode and any flagged SDNodes and append them to the block. |
Evan Cheng | c29a56d | 2009-01-12 03:19:55 +0000 | [diff] [blame] | 698 | if (!SU->getNode()) { |
| 699 | // Emit a copy. |
| 700 | EmitPhysRegCopy(SU, CopyVRBaseMap); |
| 701 | continue; |
| 702 | } |
| 703 | |
Dan Gohman | d23e0f8 | 2008-11-13 23:24:17 +0000 | [diff] [blame] | 704 | SmallVector<SDNode *, 4> FlaggedNodes; |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame] | 705 | for (SDNode *N = SU->getNode()->getFlaggedNode(); N; |
| 706 | N = N->getFlaggedNode()) |
Dan Gohman | d23e0f8 | 2008-11-13 23:24:17 +0000 | [diff] [blame] | 707 | FlaggedNodes.push_back(N); |
| 708 | while (!FlaggedNodes.empty()) { |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame] | 709 | EmitNode(FlaggedNodes.back(), SU->OrigNode != SU, SU->isCloned,VRBaseMap); |
Dan Gohman | d23e0f8 | 2008-11-13 23:24:17 +0000 | [diff] [blame] | 710 | FlaggedNodes.pop_back(); |
| 711 | } |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame] | 712 | EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned, VRBaseMap); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 715 | return BB; |
| 716 | } |