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