blob: 4ea29063ab58eb1163aed05427129d42dd3fb6db [file] [log] [blame]
Dan Gohman94b8d7e2008-09-03 16:01:59 +00001//===---- 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"
29using namespace llvm;
30
31STATISTIC(NumCommutes, "Number of instructions commuted");
32
Dan Gohman94b8d7e2008-09-03 16:01:59 +000033/// getInstrOperandRegClass - Return register class of the operand of an
34/// instruction of the specified TargetInstrDesc.
35static const TargetRegisterClass*
36getInstrOperandRegClass(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.
50void 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 Cheng1cd33272008-09-16 23:12:11 +000068 const TargetRegisterClass *UseRC = NULL;
Dan Gohman94b8d7e2008-09-03 16:01:59 +000069 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 Cheng1cd33272008-09-16 23:12:11 +000088 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 Gohman94b8d7e2008-09-03 16:01:59 +0000101 }
102 }
103 MatchReg &= Match;
104 if (VRBase)
105 break;
106 }
107
Evan Cheng1cd33272008-09-16 23:12:11 +0000108 MVT VT = Node->getValueType(ResNo);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000109 const TargetRegisterClass *SrcRC = 0, *DstRC = 0;
Evan Cheng1cd33272008-09-16 23:12:11 +0000110 SrcRC = TRI->getPhysicalRegisterRegClass(SrcReg, VT);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000111
112 // Figure out the register class to create for the destreg.
113 if (VRBase) {
114 DstRC = MRI.getRegClass(VRBase);
Evan Cheng1cd33272008-09-16 23:12:11 +0000115 } else if (UseRC) {
116 assert(UseRC->hasType(VT) && "Incompatible phys register def and uses!");
117 DstRC = UseRC;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000118 } else {
Evan Cheng1cd33272008-09-16 23:12:11 +0000119 DstRC = TLI->getRegClassFor(VT);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000120 }
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 Cheng1cd33272008-09-16 23:12:11 +0000129 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 Gohman94b8d7e2008-09-03 16:01:59 +0000133 }
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.
145unsigned 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
161void 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.
205unsigned 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.
231void ScheduleDAG::AddOperand(MachineInstr *MI, SDValue Op,
232 unsigned IIOpNum,
233 const TargetInstrDesc *II,
Dale Johannesen86b49f82008-09-24 01:07:17 +0000234 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000235 if (Op.isMachineOpcode()) {
236 // Note that this case is redundant with the final else block, but we
237 // include it because it is the most common and it makes the logic
238 // simpler here.
239 assert(Op.getValueType() != MVT::Other &&
240 Op.getValueType() != MVT::Flag &&
241 "Chain and flag operands should occur at end of operand list!");
242 // Get/emit the operand.
243 unsigned VReg = getVR(Op, VRBaseMap);
244 const TargetInstrDesc &TID = MI->getDesc();
245 bool isOptDef = IIOpNum < TID.getNumOperands() &&
246 TID.OpInfo[IIOpNum].isOptionalDef();
Dale Johannesen86b49f82008-09-24 01:07:17 +0000247 MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000248
249 // Verify that it is right.
250 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
251#ifndef NDEBUG
252 if (II) {
253 // There may be no register class for this operand if it is a variadic
254 // argument (RC will be NULL in this case). In this case, we just assume
255 // the regclass is ok.
256 const TargetRegisterClass *RC =
257 getInstrOperandRegClass(TRI, TII, *II, IIOpNum);
258 assert((RC || II->isVariadic()) && "Expected reg class info!");
259 const TargetRegisterClass *VRC = MRI.getRegClass(VReg);
260 if (RC && VRC != RC) {
261 cerr << "Register class of operand and regclass of use don't agree!\n";
262 cerr << "Operand = " << IIOpNum << "\n";
263 cerr << "Op->Val = "; Op.getNode()->dump(&DAG); cerr << "\n";
264 cerr << "MI = "; MI->print(cerr);
265 cerr << "VReg = " << VReg << "\n";
266 cerr << "VReg RegClass size = " << VRC->getSize()
267 << ", align = " << VRC->getAlignment() << "\n";
268 cerr << "Expected RegClass size = " << RC->getSize()
269 << ", align = " << RC->getAlignment() << "\n";
270 cerr << "Fatal error, aborting.\n";
271 abort();
272 }
273 }
274#endif
275 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000276 MI->addOperand(MachineOperand::CreateImm(C->getZExtValue()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000277 } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000278 const ConstantFP *CFP = F->getConstantFPValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000279 MI->addOperand(MachineOperand::CreateFPImm(CFP));
280 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
Dale Johannesen86b49f82008-09-24 01:07:17 +0000281 MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000282 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
283 MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(),TGA->getOffset()));
284 } else if (BasicBlockSDNode *BB = dyn_cast<BasicBlockSDNode>(Op)) {
285 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
286 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
287 MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
288 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
289 MI->addOperand(MachineOperand::CreateJTI(JT->getIndex()));
290 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
291 int Offset = CP->getOffset();
292 unsigned Align = CP->getAlignment();
293 const Type *Type = CP->getType();
294 // MachineConstantPool wants an explicit alignment.
295 if (Align == 0) {
296 Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
297 if (Align == 0) {
298 // Alignment of vector types. FIXME!
299 Align = TM.getTargetData()->getABITypeSize(Type);
300 Align = Log2_64(Align);
301 }
302 }
303
304 unsigned Idx;
305 if (CP->isMachineConstantPoolEntry())
306 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
307 else
308 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
309 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
Bill Wendling056292f2008-09-16 21:48:12 +0000310 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000311 MI->addOperand(MachineOperand::CreateES(ES->getSymbol()));
312 } else {
313 assert(Op.getValueType() != MVT::Other &&
314 Op.getValueType() != MVT::Flag &&
315 "Chain and flag operands should occur at end of operand list!");
316 unsigned VReg = getVR(Op, VRBaseMap);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000317 MI->addOperand(MachineOperand::CreateReg(VReg, false));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000318
319 // Verify that it is right. Note that the reg class of the physreg and the
320 // vreg don't necessarily need to match, but the target copy insertion has
321 // to be able to handle it. This handles things like copies from ST(0) to
322 // an FP vreg on x86.
323 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
324 if (II && !II->isVariadic()) {
325 assert(getInstrOperandRegClass(TRI, TII, *II, IIOpNum) &&
326 "Don't have operand info for this instruction!");
327 }
328 }
329}
330
331void ScheduleDAG::AddMemOperand(MachineInstr *MI, const MachineMemOperand &MO) {
332 MI->addMemOperand(*MF, MO);
333}
334
335/// getSubRegisterRegClass - Returns the register class of specified register
336/// class' "SubIdx"'th sub-register class.
337static const TargetRegisterClass*
338getSubRegisterRegClass(const TargetRegisterClass *TRC, unsigned SubIdx) {
339 // Pick the register class of the subregister
340 TargetRegisterInfo::regclass_iterator I =
341 TRC->subregclasses_begin() + SubIdx-1;
342 assert(I < TRC->subregclasses_end() &&
343 "Invalid subregister index for register class");
344 return *I;
345}
346
347/// getSuperRegisterRegClass - Returns the register class of a superreg A whose
348/// "SubIdx"'th sub-register class is the specified register class and whose
349/// type matches the specified type.
350static const TargetRegisterClass*
351getSuperRegisterRegClass(const TargetRegisterClass *TRC,
352 unsigned SubIdx, MVT VT) {
353 // Pick the register class of the superegister for this type
354 for (TargetRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
355 E = TRC->superregclasses_end(); I != E; ++I)
356 if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC)
357 return *I;
358 assert(false && "Couldn't find the register class");
359 return 0;
360}
361
362/// EmitSubregNode - Generate machine code for subreg nodes.
363///
364void ScheduleDAG::EmitSubregNode(SDNode *Node,
365 DenseMap<SDValue, unsigned> &VRBaseMap) {
366 unsigned VRBase = 0;
367 unsigned Opc = Node->getMachineOpcode();
368
369 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
370 // the CopyToReg'd destination register instead of creating a new vreg.
371 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
372 UI != E; ++UI) {
373 SDNode *User = *UI;
374 if (User->getOpcode() == ISD::CopyToReg &&
375 User->getOperand(2).getNode() == Node) {
376 unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
377 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
378 VRBase = DestReg;
379 break;
380 }
381 }
382 }
383
384 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000385 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000386
387 // Create the extract_subreg machine instruction.
388 MachineInstr *MI = BuildMI(*MF, TII->get(TargetInstrInfo::EXTRACT_SUBREG));
389
390 // Figure out the register class to create for the destreg.
391 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
392 const TargetRegisterClass *TRC = MRI.getRegClass(VReg);
393 const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx);
394
395 if (VRBase) {
396 // Grab the destination register
397#ifndef NDEBUG
398 const TargetRegisterClass *DRC = MRI.getRegClass(VRBase);
399 assert(SRC && DRC && SRC == DRC &&
400 "Source subregister and destination must have the same class");
401#endif
402 } else {
403 // Create the reg
404 assert(SRC && "Couldn't find source register class");
405 VRBase = MRI.createVirtualRegister(SRC);
406 }
407
408 // Add def, source, and subreg index
409 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
410 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
411 MI->addOperand(MachineOperand::CreateImm(SubIdx));
412 BB->push_back(MI);
413 } else if (Opc == TargetInstrInfo::INSERT_SUBREG ||
414 Opc == TargetInstrInfo::SUBREG_TO_REG) {
415 SDValue N0 = Node->getOperand(0);
416 SDValue N1 = Node->getOperand(1);
417 SDValue N2 = Node->getOperand(2);
418 unsigned SubReg = getVR(N1, VRBaseMap);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000419 unsigned SubIdx = cast<ConstantSDNode>(N2)->getZExtValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000420
421
422 // Figure out the register class to create for the destreg.
423 const TargetRegisterClass *TRC = 0;
424 if (VRBase) {
425 TRC = MRI.getRegClass(VRBase);
426 } else {
427 TRC = getSuperRegisterRegClass(MRI.getRegClass(SubReg), SubIdx,
428 Node->getValueType(0));
429 assert(TRC && "Couldn't determine register class for insert_subreg");
430 VRBase = MRI.createVirtualRegister(TRC); // Create the reg
431 }
432
433 // Create the insert_subreg or subreg_to_reg machine instruction.
434 MachineInstr *MI = BuildMI(*MF, TII->get(Opc));
435 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
436
437 // If creating a subreg_to_reg, then the first input operand
438 // is an implicit value immediate, otherwise it's a register
439 if (Opc == TargetInstrInfo::SUBREG_TO_REG) {
440 const ConstantSDNode *SD = cast<ConstantSDNode>(N0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000441 MI->addOperand(MachineOperand::CreateImm(SD->getZExtValue()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000442 } else
443 AddOperand(MI, N0, 0, 0, VRBaseMap);
444 // Add the subregster being inserted
445 AddOperand(MI, N1, 0, 0, VRBaseMap);
446 MI->addOperand(MachineOperand::CreateImm(SubIdx));
447 BB->push_back(MI);
448 } else
449 assert(0 && "Node is not insert_subreg, extract_subreg, or subreg_to_reg");
450
451 SDValue Op(Node, 0);
452 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
453 isNew = isNew; // Silence compiler warning.
454 assert(isNew && "Node emitted out of order - early");
455}
456
457/// EmitNode - Generate machine code for an node and needed dependencies.
458///
459void ScheduleDAG::EmitNode(SDNode *Node, bool IsClone,
460 DenseMap<SDValue, unsigned> &VRBaseMap) {
461 // If machine instruction
462 if (Node->isMachineOpcode()) {
463 unsigned Opc = Node->getMachineOpcode();
464
465 // Handle subreg insert/extract specially
466 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
467 Opc == TargetInstrInfo::INSERT_SUBREG ||
468 Opc == TargetInstrInfo::SUBREG_TO_REG) {
469 EmitSubregNode(Node, VRBaseMap);
470 return;
471 }
472
473 if (Opc == TargetInstrInfo::IMPLICIT_DEF)
474 // We want a unique VR for each IMPLICIT_DEF use.
475 return;
476
477 const TargetInstrDesc &II = TII->get(Opc);
478 unsigned NumResults = CountResults(Node);
479 unsigned NodeOperands = CountOperands(Node);
480 unsigned MemOperandsEnd = ComputeMemOperandsEnd(Node);
481 bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
482 II.getImplicitDefs() != 0;
483#ifndef NDEBUG
484 unsigned NumMIOperands = NodeOperands + NumResults;
485 assert((II.getNumOperands() == NumMIOperands ||
486 HasPhysRegOuts || II.isVariadic()) &&
487 "#operands for dag node doesn't match .td file!");
488#endif
489
490 // Create the new machine instruction.
491 MachineInstr *MI = BuildMI(*MF, II);
492
493 // Add result register values for things that are defined by this
494 // instruction.
495 if (NumResults)
496 CreateVirtualRegisters(Node, MI, II, VRBaseMap);
497
498 // Emit all of the actual operands of this instruction, adding them to the
499 // instruction as appropriate.
500 for (unsigned i = 0; i != NodeOperands; ++i)
501 AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap);
502
503 // Emit all of the memory operands of this instruction
504 for (unsigned i = NodeOperands; i != MemOperandsEnd; ++i)
505 AddMemOperand(MI, cast<MemOperandSDNode>(Node->getOperand(i))->MO);
506
507 // Commute node if it has been determined to be profitable.
508 if (CommuteSet.count(Node)) {
509 MachineInstr *NewMI = TII->commuteInstruction(MI);
510 if (NewMI == 0)
511 DOUT << "Sched: COMMUTING FAILED!\n";
512 else {
513 DOUT << "Sched: COMMUTED TO: " << *NewMI;
514 if (MI != NewMI) {
515 MF->DeleteMachineInstr(MI);
516 MI = NewMI;
517 }
518 ++NumCommutes;
519 }
520 }
521
522 if (II.usesCustomDAGSchedInsertionHook())
523 // Insert this instruction into the basic block using a target
524 // specific inserter which may returns a new basic block.
525 BB = TLI->EmitInstrWithCustomInserter(MI, BB);
526 else
527 BB->push_back(MI);
528
529 // Additional results must be an physical register def.
530 if (HasPhysRegOuts) {
531 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
532 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
533 if (Node->hasAnyUseOfValue(i))
534 EmitCopyFromReg(Node, i, IsClone, Reg, VRBaseMap);
535 }
536 }
537 return;
538 }
539
540 switch (Node->getOpcode()) {
541 default:
542#ifndef NDEBUG
543 Node->dump(&DAG);
544#endif
545 assert(0 && "This target-independent node should have been selected!");
546 break;
547 case ISD::EntryToken:
548 assert(0 && "EntryToken should have been excluded from the schedule!");
549 break;
550 case ISD::TokenFactor: // fall thru
551 break;
552 case ISD::CopyToReg: {
553 unsigned SrcReg;
554 SDValue SrcVal = Node->getOperand(2);
555 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
556 SrcReg = R->getReg();
557 else
558 SrcReg = getVR(SrcVal, VRBaseMap);
559
560 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
561 if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
562 break;
563
564 const TargetRegisterClass *SrcTRC = 0, *DstTRC = 0;
565 // Get the register classes of the src/dst.
566 if (TargetRegisterInfo::isVirtualRegister(SrcReg))
567 SrcTRC = MRI.getRegClass(SrcReg);
568 else
569 SrcTRC = TRI->getPhysicalRegisterRegClass(SrcReg,SrcVal.getValueType());
570
571 if (TargetRegisterInfo::isVirtualRegister(DestReg))
572 DstTRC = MRI.getRegClass(DestReg);
573 else
574 DstTRC = TRI->getPhysicalRegisterRegClass(DestReg,
575 Node->getOperand(1).getValueType());
576 TII->copyRegToReg(*BB, BB->end(), DestReg, SrcReg, DstTRC, SrcTRC);
577 break;
578 }
579 case ISD::CopyFromReg: {
580 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
581 EmitCopyFromReg(Node, 0, IsClone, SrcReg, VRBaseMap);
582 break;
583 }
584 case ISD::INLINEASM: {
585 unsigned NumOps = Node->getNumOperands();
586 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
587 --NumOps; // Ignore the flag operand.
588
589 // Create the inline asm machine instruction.
590 MachineInstr *MI = BuildMI(*MF, TII->get(TargetInstrInfo::INLINEASM));
591
592 // Add the asm string as an external symbol operand.
Bill Wendling056292f2008-09-16 21:48:12 +0000593 const char *AsmStr =
594 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000595 MI->addOperand(MachineOperand::CreateES(AsmStr));
596
597 // Add all of the operand registers to the instruction.
598 for (unsigned i = 2; i != NumOps;) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000599 unsigned Flags =
600 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000601 unsigned NumVals = Flags >> 3;
602
603 MI->addOperand(MachineOperand::CreateImm(Flags));
604 ++i; // Skip the ID value.
605
606 switch (Flags & 7) {
607 default: assert(0 && "Bad flags!");
608 case 2: // Def of register.
609 for (; NumVals; --NumVals, ++i) {
610 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
611 MI->addOperand(MachineOperand::CreateReg(Reg, true));
612 }
613 break;
Dale Johannesen913d3df2008-09-12 17:49:03 +0000614 case 6: // Def of earlyclobber register.
615 for (; NumVals; --NumVals, ++i) {
616 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
617 MI->addOperand(MachineOperand::CreateReg(Reg, true, false, false,
618 false, 0, true));
619 }
620 break;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000621 case 1: // Use of register.
622 case 3: // Immediate.
623 case 4: // Addressing mode.
624 // The addressing mode has been selected, just add all of the
625 // operands to the machine instruction.
626 for (; NumVals; --NumVals, ++i)
Dale Johannesen86b49f82008-09-24 01:07:17 +0000627 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000628 break;
629 }
630 }
631 BB->push_back(MI);
632 break;
633 }
634 }
635}
636
637void ScheduleDAG::EmitNoop() {
638 TII->insertNoop(*BB, BB->end());
639}
640
641void ScheduleDAG::EmitCrossRCCopy(SUnit *SU,
642 DenseMap<SUnit*, unsigned> &VRBaseMap) {
643 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
644 I != E; ++I) {
645 if (I->isCtrl) continue; // ignore chain preds
646 if (!I->Dep->Node) {
647 // Copy to physical register.
648 DenseMap<SUnit*, unsigned>::iterator VRI = VRBaseMap.find(I->Dep);
649 assert(VRI != VRBaseMap.end() && "Node emitted out of order - late");
650 // Find the destination physical register.
651 unsigned Reg = 0;
652 for (SUnit::const_succ_iterator II = SU->Succs.begin(),
653 EE = SU->Succs.end(); II != EE; ++II) {
654 if (I->Reg) {
655 Reg = I->Reg;
656 break;
657 }
658 }
659 assert(I->Reg && "Unknown physical register!");
660 TII->copyRegToReg(*BB, BB->end(), Reg, VRI->second,
661 SU->CopyDstRC, SU->CopySrcRC);
662 } else {
663 // Copy from physical register.
664 assert(I->Reg && "Unknown physical register!");
665 unsigned VRBase = MRI.createVirtualRegister(SU->CopyDstRC);
666 bool isNew = VRBaseMap.insert(std::make_pair(SU, VRBase)).second;
667 isNew = isNew; // Silence compiler warning.
668 assert(isNew && "Node emitted out of order - early");
669 TII->copyRegToReg(*BB, BB->end(), VRBase, I->Reg,
670 SU->CopyDstRC, SU->CopySrcRC);
671 }
672 break;
673 }
674}
675
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000676/// EmitSchedule - Emit the machine code in scheduled order.
677MachineBasicBlock *ScheduleDAG::EmitSchedule() {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000678 DenseMap<SDValue, unsigned> VRBaseMap;
679 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
680 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
681 SUnit *SU = Sequence[i];
682 if (!SU) {
683 // Null SUnit* is a noop.
684 EmitNoop();
685 continue;
686 }
687 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; ++j)
688 EmitNode(SU->FlaggedNodes[j], SU->OrigNode != SU, VRBaseMap);
689 if (!SU->Node)
690 EmitCrossRCCopy(SU, CopyVRBaseMap);
691 else
692 EmitNode(SU->Node, SU->OrigNode != SU, VRBaseMap);
693 }
694
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000695 return BB;
696}