blob: dc9313bb0c10e6900d1aaae0790807a10201a3f7 [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"
Dan Gohman343f0c02008-11-19 23:18:57 +000016#include "llvm/CodeGen/ScheduleDAGSDNodes.h"
Dan Gohman94b8d7e2008-09-03 16:01:59 +000017#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.
Dan Gohman343f0c02008-11-19 23:18:57 +000050void ScheduleDAGSDNodes::EmitCopyFromReg(SDNode *Node, unsigned ResNo,
51 bool IsClone, unsigned SrcReg,
52 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +000053 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.
Dan Gohman343f0c02008-11-19 23:18:57 +0000145unsigned ScheduleDAGSDNodes::getDstOfOnlyCopyToRegUse(SDNode *Node,
146 unsigned ResNo) const {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000147 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 Gohman343f0c02008-11-19 23:18:57 +0000161void ScheduleDAGSDNodes::CreateVirtualRegisters(SDNode *Node, MachineInstr *MI,
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000162 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.
Dan Gohman343f0c02008-11-19 23:18:57 +0000205unsigned ScheduleDAGSDNodes::getVR(SDValue Op,
206 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000207 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.
Dan Gohman343f0c02008-11-19 23:18:57 +0000231void ScheduleDAGSDNodes::AddOperand(MachineInstr *MI, SDValue Op,
232 unsigned IIOpNum,
233 const TargetInstrDesc *II,
234 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";
Dan Gohmana23b3b82008-11-13 21:21:28 +0000263 cerr << "Op->Val = "; Op.getNode()->dump(DAG); cerr << "\n";
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000264 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
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000331/// getSubRegisterRegClass - Returns the register class of specified register
332/// class' "SubIdx"'th sub-register class.
333static const TargetRegisterClass*
334getSubRegisterRegClass(const TargetRegisterClass *TRC, unsigned SubIdx) {
335 // Pick the register class of the subregister
336 TargetRegisterInfo::regclass_iterator I =
337 TRC->subregclasses_begin() + SubIdx-1;
338 assert(I < TRC->subregclasses_end() &&
339 "Invalid subregister index for register class");
340 return *I;
341}
342
343/// getSuperRegisterRegClass - Returns the register class of a superreg A whose
344/// "SubIdx"'th sub-register class is the specified register class and whose
345/// type matches the specified type.
346static const TargetRegisterClass*
347getSuperRegisterRegClass(const TargetRegisterClass *TRC,
348 unsigned SubIdx, MVT VT) {
349 // Pick the register class of the superegister for this type
350 for (TargetRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
351 E = TRC->superregclasses_end(); I != E; ++I)
352 if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC)
353 return *I;
354 assert(false && "Couldn't find the register class");
355 return 0;
356}
357
358/// EmitSubregNode - Generate machine code for subreg nodes.
359///
Dan Gohman343f0c02008-11-19 23:18:57 +0000360void ScheduleDAGSDNodes::EmitSubregNode(SDNode *Node,
361 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000362 unsigned VRBase = 0;
363 unsigned Opc = Node->getMachineOpcode();
364
365 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
366 // the CopyToReg'd destination register instead of creating a new vreg.
367 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
368 UI != E; ++UI) {
369 SDNode *User = *UI;
370 if (User->getOpcode() == ISD::CopyToReg &&
371 User->getOperand(2).getNode() == Node) {
372 unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
373 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
374 VRBase = DestReg;
375 break;
376 }
377 }
378 }
379
380 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000381 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000382
383 // Create the extract_subreg machine instruction.
384 MachineInstr *MI = BuildMI(*MF, TII->get(TargetInstrInfo::EXTRACT_SUBREG));
385
386 // Figure out the register class to create for the destreg.
387 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
388 const TargetRegisterClass *TRC = MRI.getRegClass(VReg);
389 const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx);
390
391 if (VRBase) {
392 // Grab the destination register
393#ifndef NDEBUG
394 const TargetRegisterClass *DRC = MRI.getRegClass(VRBase);
395 assert(SRC && DRC && SRC == DRC &&
396 "Source subregister and destination must have the same class");
397#endif
398 } else {
399 // Create the reg
400 assert(SRC && "Couldn't find source register class");
401 VRBase = MRI.createVirtualRegister(SRC);
402 }
403
404 // Add def, source, and subreg index
405 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
406 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
407 MI->addOperand(MachineOperand::CreateImm(SubIdx));
408 BB->push_back(MI);
409 } else if (Opc == TargetInstrInfo::INSERT_SUBREG ||
410 Opc == TargetInstrInfo::SUBREG_TO_REG) {
411 SDValue N0 = Node->getOperand(0);
412 SDValue N1 = Node->getOperand(1);
413 SDValue N2 = Node->getOperand(2);
414 unsigned SubReg = getVR(N1, VRBaseMap);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000415 unsigned SubIdx = cast<ConstantSDNode>(N2)->getZExtValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000416
417
418 // Figure out the register class to create for the destreg.
419 const TargetRegisterClass *TRC = 0;
420 if (VRBase) {
421 TRC = MRI.getRegClass(VRBase);
422 } else {
423 TRC = getSuperRegisterRegClass(MRI.getRegClass(SubReg), SubIdx,
424 Node->getValueType(0));
425 assert(TRC && "Couldn't determine register class for insert_subreg");
426 VRBase = MRI.createVirtualRegister(TRC); // Create the reg
427 }
428
429 // Create the insert_subreg or subreg_to_reg machine instruction.
430 MachineInstr *MI = BuildMI(*MF, TII->get(Opc));
431 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
432
433 // If creating a subreg_to_reg, then the first input operand
434 // is an implicit value immediate, otherwise it's a register
435 if (Opc == TargetInstrInfo::SUBREG_TO_REG) {
436 const ConstantSDNode *SD = cast<ConstantSDNode>(N0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000437 MI->addOperand(MachineOperand::CreateImm(SD->getZExtValue()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000438 } else
439 AddOperand(MI, N0, 0, 0, VRBaseMap);
440 // Add the subregster being inserted
441 AddOperand(MI, N1, 0, 0, VRBaseMap);
442 MI->addOperand(MachineOperand::CreateImm(SubIdx));
443 BB->push_back(MI);
444 } else
445 assert(0 && "Node is not insert_subreg, extract_subreg, or subreg_to_reg");
446
447 SDValue Op(Node, 0);
448 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
449 isNew = isNew; // Silence compiler warning.
450 assert(isNew && "Node emitted out of order - early");
451}
452
453/// EmitNode - Generate machine code for an node and needed dependencies.
454///
Dan Gohman343f0c02008-11-19 23:18:57 +0000455void ScheduleDAGSDNodes::EmitNode(SDNode *Node, bool IsClone,
456 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000457 // If machine instruction
458 if (Node->isMachineOpcode()) {
459 unsigned Opc = Node->getMachineOpcode();
460
461 // Handle subreg insert/extract specially
462 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
463 Opc == TargetInstrInfo::INSERT_SUBREG ||
464 Opc == TargetInstrInfo::SUBREG_TO_REG) {
465 EmitSubregNode(Node, VRBaseMap);
466 return;
467 }
468
469 if (Opc == TargetInstrInfo::IMPLICIT_DEF)
470 // We want a unique VR for each IMPLICIT_DEF use.
471 return;
472
473 const TargetInstrDesc &II = TII->get(Opc);
474 unsigned NumResults = CountResults(Node);
475 unsigned NodeOperands = CountOperands(Node);
476 unsigned MemOperandsEnd = ComputeMemOperandsEnd(Node);
477 bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
478 II.getImplicitDefs() != 0;
479#ifndef NDEBUG
480 unsigned NumMIOperands = NodeOperands + NumResults;
481 assert((II.getNumOperands() == NumMIOperands ||
482 HasPhysRegOuts || II.isVariadic()) &&
483 "#operands for dag node doesn't match .td file!");
484#endif
485
486 // Create the new machine instruction.
487 MachineInstr *MI = BuildMI(*MF, II);
488
489 // Add result register values for things that are defined by this
490 // instruction.
491 if (NumResults)
492 CreateVirtualRegisters(Node, MI, II, VRBaseMap);
493
494 // Emit all of the actual operands of this instruction, adding them to the
495 // instruction as appropriate.
496 for (unsigned i = 0; i != NodeOperands; ++i)
497 AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap);
498
499 // Emit all of the memory operands of this instruction
500 for (unsigned i = NodeOperands; i != MemOperandsEnd; ++i)
501 AddMemOperand(MI, cast<MemOperandSDNode>(Node->getOperand(i))->MO);
502
503 // Commute node if it has been determined to be profitable.
504 if (CommuteSet.count(Node)) {
505 MachineInstr *NewMI = TII->commuteInstruction(MI);
506 if (NewMI == 0)
507 DOUT << "Sched: COMMUTING FAILED!\n";
508 else {
509 DOUT << "Sched: COMMUTED TO: " << *NewMI;
510 if (MI != NewMI) {
511 MF->DeleteMachineInstr(MI);
512 MI = NewMI;
513 }
514 ++NumCommutes;
515 }
516 }
517
518 if (II.usesCustomDAGSchedInsertionHook())
519 // Insert this instruction into the basic block using a target
520 // specific inserter which may returns a new basic block.
521 BB = TLI->EmitInstrWithCustomInserter(MI, BB);
522 else
523 BB->push_back(MI);
524
525 // Additional results must be an physical register def.
526 if (HasPhysRegOuts) {
527 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
528 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
529 if (Node->hasAnyUseOfValue(i))
530 EmitCopyFromReg(Node, i, IsClone, Reg, VRBaseMap);
531 }
532 }
533 return;
534 }
535
536 switch (Node->getOpcode()) {
537 default:
538#ifndef NDEBUG
Dan Gohmana23b3b82008-11-13 21:21:28 +0000539 Node->dump(DAG);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000540#endif
541 assert(0 && "This target-independent node should have been selected!");
542 break;
543 case ISD::EntryToken:
544 assert(0 && "EntryToken should have been excluded from the schedule!");
545 break;
546 case ISD::TokenFactor: // fall thru
547 break;
548 case ISD::CopyToReg: {
549 unsigned SrcReg;
550 SDValue SrcVal = Node->getOperand(2);
551 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
552 SrcReg = R->getReg();
553 else
554 SrcReg = getVR(SrcVal, VRBaseMap);
555
556 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
557 if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
558 break;
559
560 const TargetRegisterClass *SrcTRC = 0, *DstTRC = 0;
561 // Get the register classes of the src/dst.
562 if (TargetRegisterInfo::isVirtualRegister(SrcReg))
563 SrcTRC = MRI.getRegClass(SrcReg);
564 else
565 SrcTRC = TRI->getPhysicalRegisterRegClass(SrcReg,SrcVal.getValueType());
566
567 if (TargetRegisterInfo::isVirtualRegister(DestReg))
568 DstTRC = MRI.getRegClass(DestReg);
569 else
570 DstTRC = TRI->getPhysicalRegisterRegClass(DestReg,
571 Node->getOperand(1).getValueType());
572 TII->copyRegToReg(*BB, BB->end(), DestReg, SrcReg, DstTRC, SrcTRC);
573 break;
574 }
575 case ISD::CopyFromReg: {
576 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
577 EmitCopyFromReg(Node, 0, IsClone, SrcReg, VRBaseMap);
578 break;
579 }
580 case ISD::INLINEASM: {
581 unsigned NumOps = Node->getNumOperands();
582 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
583 --NumOps; // Ignore the flag operand.
584
585 // Create the inline asm machine instruction.
586 MachineInstr *MI = BuildMI(*MF, TII->get(TargetInstrInfo::INLINEASM));
587
588 // Add the asm string as an external symbol operand.
Bill Wendling056292f2008-09-16 21:48:12 +0000589 const char *AsmStr =
590 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000591 MI->addOperand(MachineOperand::CreateES(AsmStr));
592
593 // Add all of the operand registers to the instruction.
594 for (unsigned i = 2; i != NumOps;) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000595 unsigned Flags =
596 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000597 unsigned NumVals = Flags >> 3;
598
599 MI->addOperand(MachineOperand::CreateImm(Flags));
600 ++i; // Skip the ID value.
601
602 switch (Flags & 7) {
603 default: assert(0 && "Bad flags!");
604 case 2: // Def of register.
605 for (; NumVals; --NumVals, ++i) {
606 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
607 MI->addOperand(MachineOperand::CreateReg(Reg, true));
608 }
609 break;
Dale Johannesen913d3df2008-09-12 17:49:03 +0000610 case 6: // Def of earlyclobber register.
611 for (; NumVals; --NumVals, ++i) {
612 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
613 MI->addOperand(MachineOperand::CreateReg(Reg, true, false, false,
614 false, 0, true));
615 }
616 break;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000617 case 1: // Use of register.
618 case 3: // Immediate.
619 case 4: // Addressing mode.
620 // The addressing mode has been selected, just add all of the
621 // operands to the machine instruction.
622 for (; NumVals; --NumVals, ++i)
Dale Johannesen86b49f82008-09-24 01:07:17 +0000623 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000624 break;
625 }
626 }
627 BB->push_back(MI);
628 break;
629 }
630 }
631}
632
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000633/// EmitSchedule - Emit the machine code in scheduled order.
Dan Gohman343f0c02008-11-19 23:18:57 +0000634MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000635 DenseMap<SDValue, unsigned> VRBaseMap;
636 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
637 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
638 SUnit *SU = Sequence[i];
639 if (!SU) {
640 // Null SUnit* is a noop.
641 EmitNoop();
642 continue;
643 }
Dan Gohmanf449bf32008-11-14 00:06:09 +0000644
Dan Gohmanf449bf32008-11-14 00:06:09 +0000645 // For pre-regalloc scheduling, create instructions corresponding to the
646 // SDNode and any flagged SDNodes and append them to the block.
Dan Gohmand23e0f82008-11-13 23:24:17 +0000647 SmallVector<SDNode *, 4> FlaggedNodes;
648 for (SDNode *N = SU->getNode()->getFlaggedNode(); N; N = N->getFlaggedNode())
649 FlaggedNodes.push_back(N);
650 while (!FlaggedNodes.empty()) {
651 EmitNode(FlaggedNodes.back(), SU->OrigNode != SU, VRBaseMap);
652 FlaggedNodes.pop_back();
653 }
Dan Gohman550f5af2008-11-13 21:36:12 +0000654 if (!SU->getNode())
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000655 EmitCrossRCCopy(SU, CopyVRBaseMap);
656 else
Dan Gohman550f5af2008-11-13 21:36:12 +0000657 EmitNode(SU->getNode(), SU->OrigNode != SU, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000658 }
659
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000660 return BB;
661}