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