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 | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/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, |
| 35 | const TargetInstrInfo *TII, const TargetInstrDesc &II, |
| 36 | unsigned Op) { |
| 37 | if (Op >= II.getNumOperands()) { |
| 38 | assert(II.isVariadic() && "Invalid operand # of instruction"); |
| 39 | return NULL; |
| 40 | } |
| 41 | if (II.OpInfo[Op].isLookupPtrRegClass()) |
| 42 | return TII->getPointerRegClass(); |
| 43 | return TRI->getRegClass(II.OpInfo[Op].RegClass); |
| 44 | } |
| 45 | |
| 46 | /// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an |
| 47 | /// implicit physical register output. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 48 | void ScheduleDAGSDNodes::EmitCopyFromReg(SDNode *Node, unsigned ResNo, |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame^] | 49 | bool IsClone, bool IsCloned, |
| 50 | unsigned SrcReg, |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 51 | DenseMap<SDValue, unsigned> &VRBaseMap) { |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 52 | unsigned VRBase = 0; |
| 53 | if (TargetRegisterInfo::isVirtualRegister(SrcReg)) { |
| 54 | // Just use the input register directly! |
| 55 | SDValue Op(Node, ResNo); |
| 56 | if (IsClone) |
| 57 | VRBaseMap.erase(Op); |
| 58 | bool isNew = VRBaseMap.insert(std::make_pair(Op, SrcReg)).second; |
| 59 | isNew = isNew; // Silence compiler warning. |
| 60 | assert(isNew && "Node emitted out of order - early"); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | // If the node is only used by a CopyToReg and the dest reg is a vreg, use |
| 65 | // the CopyToReg'd destination register instead of creating a new vreg. |
| 66 | bool MatchReg = true; |
Evan Cheng | 1cd3327 | 2008-09-16 23:12:11 +0000 | [diff] [blame] | 67 | const TargetRegisterClass *UseRC = NULL; |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame^] | 68 | if (!IsClone && !IsCloned) |
| 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()); |
| 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 | } |
Evan Cheng | 1cd3327 | 2008-09-16 23:12:11 +0000 | [diff] [blame] | 101 | } |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 102 | } |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame^] | 103 | MatchReg &= Match; |
| 104 | if (VRBase) |
| 105 | break; |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 106 | } |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 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. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 145 | unsigned ScheduleDAGSDNodes::getDstOfOnlyCopyToRegUse(SDNode *Node, |
| 146 | unsigned ResNo) const { |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 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 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 161 | void ScheduleDAGSDNodes::CreateVirtualRegisters(SDNode *Node, MachineInstr *MI, |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame^] | 162 | const TargetInstrDesc &II, |
| 163 | bool IsClone, bool IsCloned, |
Evan Cheng | 5c3c5a4 | 2009-01-09 22:44:02 +0000 | [diff] [blame] | 164 | DenseMap<SDValue, unsigned> &VRBaseMap) { |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 165 | assert(Node->getMachineOpcode() != TargetInstrInfo::IMPLICIT_DEF && |
| 166 | "IMPLICIT_DEF should have been handled as a special case elsewhere!"); |
| 167 | |
| 168 | for (unsigned i = 0; i < II.getNumDefs(); ++i) { |
| 169 | // If the specific node value is only used by a CopyToReg and the dest reg |
| 170 | // is a vreg, use the CopyToReg'd destination register instead of creating |
| 171 | // a new vreg. |
| 172 | unsigned VRBase = 0; |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame^] | 173 | |
| 174 | if (!IsClone && !IsCloned) |
| 175 | for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end(); |
| 176 | UI != E; ++UI) { |
| 177 | SDNode *User = *UI; |
| 178 | if (User->getOpcode() == ISD::CopyToReg && |
| 179 | User->getOperand(2).getNode() == Node && |
| 180 | User->getOperand(2).getResNo() == i) { |
| 181 | unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg(); |
| 182 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 183 | VRBase = Reg; |
| 184 | MI->addOperand(MachineOperand::CreateReg(Reg, true)); |
| 185 | break; |
| 186 | } |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 189 | |
| 190 | // Create the result registers for this node and add the result regs to |
| 191 | // the machine instruction. |
| 192 | if (VRBase == 0) { |
| 193 | const TargetRegisterClass *RC = getInstrOperandRegClass(TRI, TII, II, i); |
| 194 | assert(RC && "Isn't a register operand!"); |
| 195 | VRBase = MRI.createVirtualRegister(RC); |
| 196 | MI->addOperand(MachineOperand::CreateReg(VRBase, true)); |
| 197 | } |
| 198 | |
| 199 | SDValue Op(Node, i); |
Evan Cheng | 5c3c5a4 | 2009-01-09 22:44:02 +0000 | [diff] [blame] | 200 | if (IsClone) |
| 201 | VRBaseMap.erase(Op); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 202 | bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second; |
| 203 | isNew = isNew; // Silence compiler warning. |
| 204 | assert(isNew && "Node emitted out of order - early"); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /// getVR - Return the virtual register corresponding to the specified result |
| 209 | /// of the specified node. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 210 | unsigned ScheduleDAGSDNodes::getVR(SDValue Op, |
| 211 | DenseMap<SDValue, unsigned> &VRBaseMap) { |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 212 | if (Op.isMachineOpcode() && |
| 213 | Op.getMachineOpcode() == TargetInstrInfo::IMPLICIT_DEF) { |
| 214 | // Add an IMPLICIT_DEF instruction before every use. |
| 215 | unsigned VReg = getDstOfOnlyCopyToRegUse(Op.getNode(), Op.getResNo()); |
| 216 | // IMPLICIT_DEF can produce any type of result so its TargetInstrDesc |
| 217 | // does not include operand register class info. |
| 218 | if (!VReg) { |
| 219 | const TargetRegisterClass *RC = TLI->getRegClassFor(Op.getValueType()); |
| 220 | VReg = MRI.createVirtualRegister(RC); |
| 221 | } |
| 222 | BuildMI(BB, TII->get(TargetInstrInfo::IMPLICIT_DEF), VReg); |
| 223 | return VReg; |
| 224 | } |
| 225 | |
| 226 | DenseMap<SDValue, unsigned>::iterator I = VRBaseMap.find(Op); |
| 227 | assert(I != VRBaseMap.end() && "Node emitted out of order - late"); |
| 228 | return I->second; |
| 229 | } |
| 230 | |
| 231 | |
| 232 | /// AddOperand - Add the specified operand to the specified machine instr. II |
| 233 | /// specifies the instruction information for the node, and IIOpNum is the |
| 234 | /// operand number (in the II) that we are adding. IIOpNum and II are used for |
| 235 | /// assertions only. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 236 | void ScheduleDAGSDNodes::AddOperand(MachineInstr *MI, SDValue Op, |
| 237 | unsigned IIOpNum, |
| 238 | const TargetInstrDesc *II, |
| 239 | DenseMap<SDValue, unsigned> &VRBaseMap) { |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 240 | if (Op.isMachineOpcode()) { |
| 241 | // Note that this case is redundant with the final else block, but we |
| 242 | // include it because it is the most common and it makes the logic |
| 243 | // simpler here. |
| 244 | assert(Op.getValueType() != MVT::Other && |
| 245 | Op.getValueType() != MVT::Flag && |
| 246 | "Chain and flag operands should occur at end of operand list!"); |
| 247 | // Get/emit the operand. |
| 248 | unsigned VReg = getVR(Op, VRBaseMap); |
| 249 | const TargetInstrDesc &TID = MI->getDesc(); |
| 250 | bool isOptDef = IIOpNum < TID.getNumOperands() && |
| 251 | TID.OpInfo[IIOpNum].isOptionalDef(); |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 252 | MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef)); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 253 | |
| 254 | // Verify that it is right. |
| 255 | assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?"); |
| 256 | #ifndef NDEBUG |
| 257 | if (II) { |
| 258 | // There may be no register class for this operand if it is a variadic |
| 259 | // argument (RC will be NULL in this case). In this case, we just assume |
| 260 | // the regclass is ok. |
| 261 | const TargetRegisterClass *RC = |
| 262 | getInstrOperandRegClass(TRI, TII, *II, IIOpNum); |
| 263 | assert((RC || II->isVariadic()) && "Expected reg class info!"); |
| 264 | const TargetRegisterClass *VRC = MRI.getRegClass(VReg); |
| 265 | if (RC && VRC != RC) { |
| 266 | cerr << "Register class of operand and regclass of use don't agree!\n"; |
| 267 | cerr << "Operand = " << IIOpNum << "\n"; |
Dan Gohman | a23b3b8 | 2008-11-13 21:21:28 +0000 | [diff] [blame] | 268 | cerr << "Op->Val = "; Op.getNode()->dump(DAG); cerr << "\n"; |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 269 | cerr << "MI = "; MI->print(cerr); |
| 270 | cerr << "VReg = " << VReg << "\n"; |
| 271 | cerr << "VReg RegClass size = " << VRC->getSize() |
| 272 | << ", align = " << VRC->getAlignment() << "\n"; |
| 273 | cerr << "Expected RegClass size = " << RC->getSize() |
| 274 | << ", align = " << RC->getAlignment() << "\n"; |
| 275 | cerr << "Fatal error, aborting.\n"; |
| 276 | abort(); |
| 277 | } |
| 278 | } |
| 279 | #endif |
| 280 | } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 281 | MI->addOperand(MachineOperand::CreateImm(C->getZExtValue())); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 282 | } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) { |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 283 | const ConstantFP *CFP = F->getConstantFPValue(); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 284 | MI->addOperand(MachineOperand::CreateFPImm(CFP)); |
| 285 | } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) { |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 286 | MI->addOperand(MachineOperand::CreateReg(R->getReg(), false)); |
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! |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 304 | Align = TM.getTargetData()->getTypePaddedSize(Type); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 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 | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 322 | MI->addOperand(MachineOperand::CreateReg(VReg, false)); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 323 | |
| 324 | // Verify that it is right. Note that the reg class of the physreg and the |
| 325 | // vreg don't necessarily need to match, but the target copy insertion has |
| 326 | // to be able to handle it. This handles things like copies from ST(0) to |
| 327 | // an FP vreg on x86. |
| 328 | assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?"); |
| 329 | if (II && !II->isVariadic()) { |
| 330 | assert(getInstrOperandRegClass(TRI, TII, *II, IIOpNum) && |
| 331 | "Don't have operand info for this instruction!"); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 336 | /// getSubRegisterRegClass - Returns the register class of specified register |
| 337 | /// class' "SubIdx"'th sub-register class. |
| 338 | static const TargetRegisterClass* |
| 339 | getSubRegisterRegClass(const TargetRegisterClass *TRC, unsigned SubIdx) { |
| 340 | // Pick the register class of the subregister |
| 341 | TargetRegisterInfo::regclass_iterator I = |
| 342 | TRC->subregclasses_begin() + SubIdx-1; |
| 343 | assert(I < TRC->subregclasses_end() && |
| 344 | "Invalid subregister index for register class"); |
| 345 | return *I; |
| 346 | } |
| 347 | |
| 348 | /// getSuperRegisterRegClass - Returns the register class of a superreg A whose |
| 349 | /// "SubIdx"'th sub-register class is the specified register class and whose |
| 350 | /// type matches the specified type. |
| 351 | static const TargetRegisterClass* |
| 352 | getSuperRegisterRegClass(const TargetRegisterClass *TRC, |
| 353 | unsigned SubIdx, MVT VT) { |
| 354 | // Pick the register class of the superegister for this type |
| 355 | for (TargetRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(), |
| 356 | E = TRC->superregclasses_end(); I != E; ++I) |
| 357 | if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC) |
| 358 | return *I; |
| 359 | assert(false && "Couldn't find the register class"); |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | /// EmitSubregNode - Generate machine code for subreg nodes. |
| 364 | /// |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 365 | void ScheduleDAGSDNodes::EmitSubregNode(SDNode *Node, |
| 366 | DenseMap<SDValue, unsigned> &VRBaseMap) { |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 367 | unsigned VRBase = 0; |
| 368 | unsigned Opc = Node->getMachineOpcode(); |
| 369 | |
| 370 | // If the node is only used by a CopyToReg and the dest reg is a vreg, use |
| 371 | // the CopyToReg'd destination register instead of creating a new vreg. |
| 372 | for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end(); |
| 373 | UI != E; ++UI) { |
| 374 | SDNode *User = *UI; |
| 375 | if (User->getOpcode() == ISD::CopyToReg && |
| 376 | User->getOperand(2).getNode() == Node) { |
| 377 | unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg(); |
| 378 | if (TargetRegisterInfo::isVirtualRegister(DestReg)) { |
| 379 | VRBase = DestReg; |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | if (Opc == TargetInstrInfo::EXTRACT_SUBREG) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 386 | unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue(); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 387 | |
| 388 | // Create the extract_subreg machine instruction. |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 389 | MachineInstr *MI = BuildMI(MF, TII->get(TargetInstrInfo::EXTRACT_SUBREG)); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 390 | |
| 391 | // Figure out the register class to create for the destreg. |
| 392 | unsigned VReg = getVR(Node->getOperand(0), VRBaseMap); |
| 393 | const TargetRegisterClass *TRC = MRI.getRegClass(VReg); |
| 394 | const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx); |
| 395 | |
| 396 | if (VRBase) { |
| 397 | // Grab the destination register |
| 398 | #ifndef NDEBUG |
| 399 | const TargetRegisterClass *DRC = MRI.getRegClass(VRBase); |
| 400 | assert(SRC && DRC && SRC == DRC && |
| 401 | "Source subregister and destination must have the same class"); |
| 402 | #endif |
| 403 | } else { |
| 404 | // Create the reg |
| 405 | assert(SRC && "Couldn't find source register class"); |
| 406 | VRBase = MRI.createVirtualRegister(SRC); |
| 407 | } |
| 408 | |
| 409 | // Add def, source, and subreg index |
| 410 | MI->addOperand(MachineOperand::CreateReg(VRBase, true)); |
| 411 | AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap); |
| 412 | MI->addOperand(MachineOperand::CreateImm(SubIdx)); |
| 413 | BB->push_back(MI); |
| 414 | } else if (Opc == TargetInstrInfo::INSERT_SUBREG || |
| 415 | Opc == TargetInstrInfo::SUBREG_TO_REG) { |
| 416 | SDValue N0 = Node->getOperand(0); |
| 417 | SDValue N1 = Node->getOperand(1); |
| 418 | SDValue N2 = Node->getOperand(2); |
| 419 | unsigned SubReg = getVR(N1, VRBaseMap); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 420 | unsigned SubIdx = cast<ConstantSDNode>(N2)->getZExtValue(); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 421 | |
| 422 | |
| 423 | // Figure out the register class to create for the destreg. |
| 424 | const TargetRegisterClass *TRC = 0; |
| 425 | if (VRBase) { |
| 426 | TRC = MRI.getRegClass(VRBase); |
| 427 | } else { |
| 428 | TRC = getSuperRegisterRegClass(MRI.getRegClass(SubReg), SubIdx, |
| 429 | Node->getValueType(0)); |
| 430 | assert(TRC && "Couldn't determine register class for insert_subreg"); |
| 431 | VRBase = MRI.createVirtualRegister(TRC); // Create the reg |
| 432 | } |
| 433 | |
| 434 | // Create the insert_subreg or subreg_to_reg machine instruction. |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 435 | MachineInstr *MI = BuildMI(MF, TII->get(Opc)); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 436 | MI->addOperand(MachineOperand::CreateReg(VRBase, true)); |
| 437 | |
| 438 | // If creating a subreg_to_reg, then the first input operand |
| 439 | // is an implicit value immediate, otherwise it's a register |
| 440 | if (Opc == TargetInstrInfo::SUBREG_TO_REG) { |
| 441 | const ConstantSDNode *SD = cast<ConstantSDNode>(N0); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 442 | MI->addOperand(MachineOperand::CreateImm(SD->getZExtValue())); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 443 | } else |
| 444 | AddOperand(MI, N0, 0, 0, VRBaseMap); |
| 445 | // Add the subregster being inserted |
| 446 | AddOperand(MI, N1, 0, 0, VRBaseMap); |
| 447 | MI->addOperand(MachineOperand::CreateImm(SubIdx)); |
| 448 | BB->push_back(MI); |
| 449 | } else |
| 450 | assert(0 && "Node is not insert_subreg, extract_subreg, or subreg_to_reg"); |
| 451 | |
| 452 | SDValue Op(Node, 0); |
| 453 | bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second; |
| 454 | isNew = isNew; // Silence compiler warning. |
| 455 | assert(isNew && "Node emitted out of order - early"); |
| 456 | } |
| 457 | |
| 458 | /// EmitNode - Generate machine code for an node and needed dependencies. |
| 459 | /// |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame^] | 460 | void ScheduleDAGSDNodes::EmitNode(SDNode *Node, bool IsClone, bool IsCloned, |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 461 | DenseMap<SDValue, unsigned> &VRBaseMap) { |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 462 | // If machine instruction |
| 463 | if (Node->isMachineOpcode()) { |
| 464 | unsigned Opc = Node->getMachineOpcode(); |
| 465 | |
| 466 | // Handle subreg insert/extract specially |
| 467 | if (Opc == TargetInstrInfo::EXTRACT_SUBREG || |
| 468 | Opc == TargetInstrInfo::INSERT_SUBREG || |
| 469 | Opc == TargetInstrInfo::SUBREG_TO_REG) { |
| 470 | EmitSubregNode(Node, VRBaseMap); |
| 471 | return; |
| 472 | } |
| 473 | |
| 474 | if (Opc == TargetInstrInfo::IMPLICIT_DEF) |
| 475 | // We want a unique VR for each IMPLICIT_DEF use. |
| 476 | return; |
| 477 | |
| 478 | const TargetInstrDesc &II = TII->get(Opc); |
| 479 | unsigned NumResults = CountResults(Node); |
| 480 | unsigned NodeOperands = CountOperands(Node); |
| 481 | unsigned MemOperandsEnd = ComputeMemOperandsEnd(Node); |
| 482 | bool HasPhysRegOuts = (NumResults > II.getNumDefs()) && |
| 483 | II.getImplicitDefs() != 0; |
| 484 | #ifndef NDEBUG |
| 485 | unsigned NumMIOperands = NodeOperands + NumResults; |
| 486 | assert((II.getNumOperands() == NumMIOperands || |
| 487 | HasPhysRegOuts || II.isVariadic()) && |
| 488 | "#operands for dag node doesn't match .td file!"); |
| 489 | #endif |
| 490 | |
| 491 | // Create the new machine instruction. |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 492 | MachineInstr *MI = BuildMI(MF, II); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 493 | |
| 494 | // Add result register values for things that are defined by this |
| 495 | // instruction. |
| 496 | if (NumResults) |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame^] | 497 | CreateVirtualRegisters(Node, MI, II, IsClone, IsCloned, VRBaseMap); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 498 | |
| 499 | // Emit all of the actual operands of this instruction, adding them to the |
| 500 | // instruction as appropriate. |
| 501 | for (unsigned i = 0; i != NodeOperands; ++i) |
| 502 | AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap); |
| 503 | |
| 504 | // Emit all of the memory operands of this instruction |
| 505 | for (unsigned i = NodeOperands; i != MemOperandsEnd; ++i) |
| 506 | AddMemOperand(MI, cast<MemOperandSDNode>(Node->getOperand(i))->MO); |
| 507 | |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 508 | if (II.usesCustomDAGSchedInsertionHook()) |
| 509 | // Insert this instruction into the basic block using a target |
| 510 | // specific inserter which may returns a new basic block. |
| 511 | BB = TLI->EmitInstrWithCustomInserter(MI, BB); |
| 512 | else |
| 513 | BB->push_back(MI); |
| 514 | |
| 515 | // Additional results must be an physical register def. |
| 516 | if (HasPhysRegOuts) { |
| 517 | for (unsigned i = II.getNumDefs(); i < NumResults; ++i) { |
| 518 | unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()]; |
| 519 | if (Node->hasAnyUseOfValue(i)) |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame^] | 520 | EmitCopyFromReg(Node, i, IsClone, IsCloned, Reg, VRBaseMap); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | switch (Node->getOpcode()) { |
| 527 | default: |
| 528 | #ifndef NDEBUG |
Dan Gohman | a23b3b8 | 2008-11-13 21:21:28 +0000 | [diff] [blame] | 529 | Node->dump(DAG); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 530 | #endif |
| 531 | assert(0 && "This target-independent node should have been selected!"); |
| 532 | break; |
| 533 | case ISD::EntryToken: |
| 534 | assert(0 && "EntryToken should have been excluded from the schedule!"); |
| 535 | break; |
| 536 | case ISD::TokenFactor: // fall thru |
| 537 | break; |
| 538 | case ISD::CopyToReg: { |
| 539 | unsigned SrcReg; |
| 540 | SDValue SrcVal = Node->getOperand(2); |
| 541 | if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal)) |
| 542 | SrcReg = R->getReg(); |
| 543 | else |
| 544 | SrcReg = getVR(SrcVal, VRBaseMap); |
| 545 | |
| 546 | unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg(); |
| 547 | if (SrcReg == DestReg) // Coalesced away the copy? Ignore. |
| 548 | break; |
| 549 | |
| 550 | const TargetRegisterClass *SrcTRC = 0, *DstTRC = 0; |
| 551 | // Get the register classes of the src/dst. |
| 552 | if (TargetRegisterInfo::isVirtualRegister(SrcReg)) |
| 553 | SrcTRC = MRI.getRegClass(SrcReg); |
| 554 | else |
| 555 | SrcTRC = TRI->getPhysicalRegisterRegClass(SrcReg,SrcVal.getValueType()); |
| 556 | |
| 557 | if (TargetRegisterInfo::isVirtualRegister(DestReg)) |
| 558 | DstTRC = MRI.getRegClass(DestReg); |
| 559 | else |
| 560 | DstTRC = TRI->getPhysicalRegisterRegClass(DestReg, |
| 561 | Node->getOperand(1).getValueType()); |
| 562 | TII->copyRegToReg(*BB, BB->end(), DestReg, SrcReg, DstTRC, SrcTRC); |
| 563 | break; |
| 564 | } |
| 565 | case ISD::CopyFromReg: { |
| 566 | unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg(); |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame^] | 567 | EmitCopyFromReg(Node, 0, IsClone, IsCloned, SrcReg, VRBaseMap); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 568 | break; |
| 569 | } |
| 570 | case ISD::INLINEASM: { |
| 571 | unsigned NumOps = Node->getNumOperands(); |
| 572 | if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag) |
| 573 | --NumOps; // Ignore the flag operand. |
| 574 | |
| 575 | // Create the inline asm machine instruction. |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 576 | MachineInstr *MI = BuildMI(MF, TII->get(TargetInstrInfo::INLINEASM)); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 577 | |
| 578 | // Add the asm string as an external symbol operand. |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 579 | const char *AsmStr = |
| 580 | cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol(); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 581 | MI->addOperand(MachineOperand::CreateES(AsmStr)); |
| 582 | |
| 583 | // Add all of the operand registers to the instruction. |
| 584 | for (unsigned i = 2; i != NumOps;) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 585 | unsigned Flags = |
| 586 | cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue(); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 587 | unsigned NumVals = Flags >> 3; |
| 588 | |
| 589 | MI->addOperand(MachineOperand::CreateImm(Flags)); |
| 590 | ++i; // Skip the ID value. |
| 591 | |
| 592 | switch (Flags & 7) { |
| 593 | default: assert(0 && "Bad flags!"); |
| 594 | case 2: // Def of register. |
| 595 | for (; NumVals; --NumVals, ++i) { |
| 596 | unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg(); |
| 597 | MI->addOperand(MachineOperand::CreateReg(Reg, true)); |
| 598 | } |
| 599 | break; |
Dale Johannesen | 913d3df | 2008-09-12 17:49:03 +0000 | [diff] [blame] | 600 | case 6: // Def of earlyclobber register. |
| 601 | for (; NumVals; --NumVals, ++i) { |
| 602 | unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg(); |
| 603 | MI->addOperand(MachineOperand::CreateReg(Reg, true, false, false, |
| 604 | false, 0, true)); |
| 605 | } |
| 606 | break; |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 607 | case 1: // Use of register. |
| 608 | case 3: // Immediate. |
| 609 | case 4: // Addressing mode. |
| 610 | // The addressing mode has been selected, just add all of the |
| 611 | // operands to the machine instruction. |
| 612 | for (; NumVals; --NumVals, ++i) |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 613 | AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 614 | break; |
| 615 | } |
| 616 | } |
| 617 | BB->push_back(MI); |
| 618 | break; |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 623 | /// EmitSchedule - Emit the machine code in scheduled order. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 624 | MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() { |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 625 | DenseMap<SDValue, unsigned> VRBaseMap; |
| 626 | DenseMap<SUnit*, unsigned> CopyVRBaseMap; |
| 627 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 628 | SUnit *SU = Sequence[i]; |
| 629 | if (!SU) { |
| 630 | // Null SUnit* is a noop. |
| 631 | EmitNoop(); |
| 632 | continue; |
| 633 | } |
Dan Gohman | f449bf3 | 2008-11-14 00:06:09 +0000 | [diff] [blame] | 634 | |
Dan Gohman | f449bf3 | 2008-11-14 00:06:09 +0000 | [diff] [blame] | 635 | // For pre-regalloc scheduling, create instructions corresponding to the |
| 636 | // SDNode and any flagged SDNodes and append them to the block. |
Evan Cheng | c29a56d | 2009-01-12 03:19:55 +0000 | [diff] [blame] | 637 | if (!SU->getNode()) { |
| 638 | // Emit a copy. |
| 639 | EmitPhysRegCopy(SU, CopyVRBaseMap); |
| 640 | continue; |
| 641 | } |
| 642 | |
Dan Gohman | d23e0f8 | 2008-11-13 23:24:17 +0000 | [diff] [blame] | 643 | SmallVector<SDNode *, 4> FlaggedNodes; |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame^] | 644 | for (SDNode *N = SU->getNode()->getFlaggedNode(); N; |
| 645 | N = N->getFlaggedNode()) |
Dan Gohman | d23e0f8 | 2008-11-13 23:24:17 +0000 | [diff] [blame] | 646 | FlaggedNodes.push_back(N); |
| 647 | while (!FlaggedNodes.empty()) { |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame^] | 648 | EmitNode(FlaggedNodes.back(), SU->OrigNode != SU, SU->isCloned,VRBaseMap); |
Dan Gohman | d23e0f8 | 2008-11-13 23:24:17 +0000 | [diff] [blame] | 649 | FlaggedNodes.pop_back(); |
| 650 | } |
Evan Cheng | e57187c | 2009-01-16 20:57:18 +0000 | [diff] [blame^] | 651 | EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned, VRBaseMap); |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Dan Gohman | 94b8d7e | 2008-09-03 16:01:59 +0000 | [diff] [blame] | 654 | return BB; |
| 655 | } |