blob: 4019ec1303f49b22d23ad1df4b88cde9790a2ee2 [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
Dan Gohman94b8d7e2008-09-03 16:01:59 +000031/// getInstrOperandRegClass - Return register class of the operand of an
32/// instruction of the specified TargetInstrDesc.
33static const TargetRegisterClass*
34getInstrOperandRegClass(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 Gohman343f0c02008-11-19 23:18:57 +000048void ScheduleDAGSDNodes::EmitCopyFromReg(SDNode *Node, unsigned ResNo,
49 bool IsClone, unsigned SrcReg,
50 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +000051 unsigned VRBase = 0;
52 if (TargetRegisterInfo::isVirtualRegister(SrcReg)) {
53 // Just use the input register directly!
54 SDValue Op(Node, ResNo);
55 if (IsClone)
56 VRBaseMap.erase(Op);
57 bool isNew = VRBaseMap.insert(std::make_pair(Op, SrcReg)).second;
58 isNew = isNew; // Silence compiler warning.
59 assert(isNew && "Node emitted out of order - early");
60 return;
61 }
62
63 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
64 // the CopyToReg'd destination register instead of creating a new vreg.
65 bool MatchReg = true;
Evan Cheng1cd33272008-09-16 23:12:11 +000066 const TargetRegisterClass *UseRC = NULL;
Dan Gohman94b8d7e2008-09-03 16:01:59 +000067 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
68 UI != E; ++UI) {
69 SDNode *User = *UI;
70 bool Match = true;
71 if (User->getOpcode() == ISD::CopyToReg &&
72 User->getOperand(2).getNode() == Node &&
73 User->getOperand(2).getResNo() == ResNo) {
74 unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
75 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
76 VRBase = DestReg;
77 Match = false;
78 } else if (DestReg != SrcReg)
79 Match = false;
80 } else {
81 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
82 SDValue Op = User->getOperand(i);
83 if (Op.getNode() != Node || Op.getResNo() != ResNo)
84 continue;
85 MVT VT = Node->getValueType(Op.getResNo());
Evan Cheng1cd33272008-09-16 23:12:11 +000086 if (VT == MVT::Other || VT == MVT::Flag)
87 continue;
88 Match = false;
89 if (User->isMachineOpcode()) {
90 const TargetInstrDesc &II = TII->get(User->getMachineOpcode());
91 const TargetRegisterClass *RC =
92 getInstrOperandRegClass(TRI,TII,II,i+II.getNumDefs());
93 if (!UseRC)
94 UseRC = RC;
95 else if (RC)
96 assert(UseRC == RC &&
97 "Multiple uses expecting different register classes!");
98 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +000099 }
100 }
101 MatchReg &= Match;
102 if (VRBase)
103 break;
104 }
105
Evan Cheng1cd33272008-09-16 23:12:11 +0000106 MVT VT = Node->getValueType(ResNo);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000107 const TargetRegisterClass *SrcRC = 0, *DstRC = 0;
Evan Cheng1cd33272008-09-16 23:12:11 +0000108 SrcRC = TRI->getPhysicalRegisterRegClass(SrcReg, VT);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000109
110 // Figure out the register class to create for the destreg.
111 if (VRBase) {
112 DstRC = MRI.getRegClass(VRBase);
Evan Cheng1cd33272008-09-16 23:12:11 +0000113 } else if (UseRC) {
114 assert(UseRC->hasType(VT) && "Incompatible phys register def and uses!");
115 DstRC = UseRC;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000116 } else {
Evan Cheng1cd33272008-09-16 23:12:11 +0000117 DstRC = TLI->getRegClassFor(VT);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000118 }
119
120 // If all uses are reading from the src physical register and copying the
121 // register is either impossible or very expensive, then don't create a copy.
122 if (MatchReg && SrcRC->getCopyCost() < 0) {
123 VRBase = SrcReg;
124 } else {
125 // Create the reg, emit the copy.
126 VRBase = MRI.createVirtualRegister(DstRC);
Evan Cheng1cd33272008-09-16 23:12:11 +0000127 bool Emitted =
128 TII->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, DstRC, SrcRC);
129 Emitted = Emitted; // Silence compiler warning.
130 assert(Emitted && "Unable to issue a copy instruction!");
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000131 }
132
133 SDValue Op(Node, ResNo);
134 if (IsClone)
135 VRBaseMap.erase(Op);
136 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
137 isNew = isNew; // Silence compiler warning.
138 assert(isNew && "Node emitted out of order - early");
139}
140
141/// getDstOfCopyToRegUse - If the only use of the specified result number of
142/// node is a CopyToReg, return its destination register. Return 0 otherwise.
Dan Gohman343f0c02008-11-19 23:18:57 +0000143unsigned ScheduleDAGSDNodes::getDstOfOnlyCopyToRegUse(SDNode *Node,
144 unsigned ResNo) const {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000145 if (!Node->hasOneUse())
146 return 0;
147
148 SDNode *User = *Node->use_begin();
149 if (User->getOpcode() == ISD::CopyToReg &&
150 User->getOperand(2).getNode() == Node &&
151 User->getOperand(2).getResNo() == ResNo) {
152 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
153 if (TargetRegisterInfo::isVirtualRegister(Reg))
154 return Reg;
155 }
156 return 0;
157}
158
Dan Gohman343f0c02008-11-19 23:18:57 +0000159void ScheduleDAGSDNodes::CreateVirtualRegisters(SDNode *Node, MachineInstr *MI,
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000160 const TargetInstrDesc &II,
161 DenseMap<SDValue, unsigned> &VRBaseMap) {
162 assert(Node->getMachineOpcode() != TargetInstrInfo::IMPLICIT_DEF &&
163 "IMPLICIT_DEF should have been handled as a special case elsewhere!");
164
165 for (unsigned i = 0; i < II.getNumDefs(); ++i) {
166 // If the specific node value is only used by a CopyToReg and the dest reg
167 // is a vreg, use the CopyToReg'd destination register instead of creating
168 // a new vreg.
169 unsigned VRBase = 0;
170 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
171 UI != E; ++UI) {
172 SDNode *User = *UI;
173 if (User->getOpcode() == ISD::CopyToReg &&
174 User->getOperand(2).getNode() == Node &&
175 User->getOperand(2).getResNo() == i) {
176 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
177 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
178 VRBase = Reg;
179 MI->addOperand(MachineOperand::CreateReg(Reg, true));
180 break;
181 }
182 }
183 }
184
185 // Create the result registers for this node and add the result regs to
186 // the machine instruction.
187 if (VRBase == 0) {
188 const TargetRegisterClass *RC = getInstrOperandRegClass(TRI, TII, II, i);
189 assert(RC && "Isn't a register operand!");
190 VRBase = MRI.createVirtualRegister(RC);
191 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
192 }
193
194 SDValue Op(Node, i);
195 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
196 isNew = isNew; // Silence compiler warning.
197 assert(isNew && "Node emitted out of order - early");
198 }
199}
200
201/// getVR - Return the virtual register corresponding to the specified result
202/// of the specified node.
Dan Gohman343f0c02008-11-19 23:18:57 +0000203unsigned ScheduleDAGSDNodes::getVR(SDValue Op,
204 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000205 if (Op.isMachineOpcode() &&
206 Op.getMachineOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
207 // Add an IMPLICIT_DEF instruction before every use.
208 unsigned VReg = getDstOfOnlyCopyToRegUse(Op.getNode(), Op.getResNo());
209 // IMPLICIT_DEF can produce any type of result so its TargetInstrDesc
210 // does not include operand register class info.
211 if (!VReg) {
212 const TargetRegisterClass *RC = TLI->getRegClassFor(Op.getValueType());
213 VReg = MRI.createVirtualRegister(RC);
214 }
215 BuildMI(BB, TII->get(TargetInstrInfo::IMPLICIT_DEF), VReg);
216 return VReg;
217 }
218
219 DenseMap<SDValue, unsigned>::iterator I = VRBaseMap.find(Op);
220 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
221 return I->second;
222}
223
224
225/// AddOperand - Add the specified operand to the specified machine instr. II
226/// specifies the instruction information for the node, and IIOpNum is the
227/// operand number (in the II) that we are adding. IIOpNum and II are used for
228/// assertions only.
Dan Gohman343f0c02008-11-19 23:18:57 +0000229void ScheduleDAGSDNodes::AddOperand(MachineInstr *MI, SDValue Op,
230 unsigned IIOpNum,
231 const TargetInstrDesc *II,
232 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000233 if (Op.isMachineOpcode()) {
234 // Note that this case is redundant with the final else block, but we
235 // include it because it is the most common and it makes the logic
236 // simpler here.
237 assert(Op.getValueType() != MVT::Other &&
238 Op.getValueType() != MVT::Flag &&
239 "Chain and flag operands should occur at end of operand list!");
240 // Get/emit the operand.
241 unsigned VReg = getVR(Op, VRBaseMap);
242 const TargetInstrDesc &TID = MI->getDesc();
243 bool isOptDef = IIOpNum < TID.getNumOperands() &&
244 TID.OpInfo[IIOpNum].isOptionalDef();
Dale Johannesen86b49f82008-09-24 01:07:17 +0000245 MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000246
247 // Verify that it is right.
248 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
249#ifndef NDEBUG
250 if (II) {
251 // There may be no register class for this operand if it is a variadic
252 // argument (RC will be NULL in this case). In this case, we just assume
253 // the regclass is ok.
254 const TargetRegisterClass *RC =
255 getInstrOperandRegClass(TRI, TII, *II, IIOpNum);
256 assert((RC || II->isVariadic()) && "Expected reg class info!");
257 const TargetRegisterClass *VRC = MRI.getRegClass(VReg);
258 if (RC && VRC != RC) {
259 cerr << "Register class of operand and regclass of use don't agree!\n";
260 cerr << "Operand = " << IIOpNum << "\n";
Dan Gohmana23b3b82008-11-13 21:21:28 +0000261 cerr << "Op->Val = "; Op.getNode()->dump(DAG); cerr << "\n";
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000262 cerr << "MI = "; MI->print(cerr);
263 cerr << "VReg = " << VReg << "\n";
264 cerr << "VReg RegClass size = " << VRC->getSize()
265 << ", align = " << VRC->getAlignment() << "\n";
266 cerr << "Expected RegClass size = " << RC->getSize()
267 << ", align = " << RC->getAlignment() << "\n";
268 cerr << "Fatal error, aborting.\n";
269 abort();
270 }
271 }
272#endif
273 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000274 MI->addOperand(MachineOperand::CreateImm(C->getZExtValue()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000275 } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000276 const ConstantFP *CFP = F->getConstantFPValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000277 MI->addOperand(MachineOperand::CreateFPImm(CFP));
278 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
Dale Johannesen86b49f82008-09-24 01:07:17 +0000279 MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000280 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
281 MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(),TGA->getOffset()));
282 } else if (BasicBlockSDNode *BB = dyn_cast<BasicBlockSDNode>(Op)) {
283 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
284 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
285 MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
286 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
287 MI->addOperand(MachineOperand::CreateJTI(JT->getIndex()));
288 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
289 int Offset = CP->getOffset();
290 unsigned Align = CP->getAlignment();
291 const Type *Type = CP->getType();
292 // MachineConstantPool wants an explicit alignment.
293 if (Align == 0) {
294 Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
295 if (Align == 0) {
296 // Alignment of vector types. FIXME!
297 Align = TM.getTargetData()->getABITypeSize(Type);
298 Align = Log2_64(Align);
299 }
300 }
301
302 unsigned Idx;
303 if (CP->isMachineConstantPoolEntry())
304 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
305 else
306 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
307 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
Bill Wendling056292f2008-09-16 21:48:12 +0000308 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000309 MI->addOperand(MachineOperand::CreateES(ES->getSymbol()));
310 } else {
311 assert(Op.getValueType() != MVT::Other &&
312 Op.getValueType() != MVT::Flag &&
313 "Chain and flag operands should occur at end of operand list!");
314 unsigned VReg = getVR(Op, VRBaseMap);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000315 MI->addOperand(MachineOperand::CreateReg(VReg, false));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000316
317 // Verify that it is right. Note that the reg class of the physreg and the
318 // vreg don't necessarily need to match, but the target copy insertion has
319 // to be able to handle it. This handles things like copies from ST(0) to
320 // an FP vreg on x86.
321 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
322 if (II && !II->isVariadic()) {
323 assert(getInstrOperandRegClass(TRI, TII, *II, IIOpNum) &&
324 "Don't have operand info for this instruction!");
325 }
326 }
327}
328
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000329/// getSubRegisterRegClass - Returns the register class of specified register
330/// class' "SubIdx"'th sub-register class.
331static const TargetRegisterClass*
332getSubRegisterRegClass(const TargetRegisterClass *TRC, unsigned SubIdx) {
333 // Pick the register class of the subregister
334 TargetRegisterInfo::regclass_iterator I =
335 TRC->subregclasses_begin() + SubIdx-1;
336 assert(I < TRC->subregclasses_end() &&
337 "Invalid subregister index for register class");
338 return *I;
339}
340
341/// getSuperRegisterRegClass - Returns the register class of a superreg A whose
342/// "SubIdx"'th sub-register class is the specified register class and whose
343/// type matches the specified type.
344static const TargetRegisterClass*
345getSuperRegisterRegClass(const TargetRegisterClass *TRC,
346 unsigned SubIdx, MVT VT) {
347 // Pick the register class of the superegister for this type
348 for (TargetRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
349 E = TRC->superregclasses_end(); I != E; ++I)
350 if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC)
351 return *I;
352 assert(false && "Couldn't find the register class");
353 return 0;
354}
355
356/// EmitSubregNode - Generate machine code for subreg nodes.
357///
Dan Gohman343f0c02008-11-19 23:18:57 +0000358void ScheduleDAGSDNodes::EmitSubregNode(SDNode *Node,
359 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000360 unsigned VRBase = 0;
361 unsigned Opc = Node->getMachineOpcode();
362
363 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
364 // the CopyToReg'd destination register instead of creating a new vreg.
365 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
366 UI != E; ++UI) {
367 SDNode *User = *UI;
368 if (User->getOpcode() == ISD::CopyToReg &&
369 User->getOperand(2).getNode() == Node) {
370 unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
371 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
372 VRBase = DestReg;
373 break;
374 }
375 }
376 }
377
378 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000379 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000380
381 // Create the extract_subreg machine instruction.
382 MachineInstr *MI = BuildMI(*MF, TII->get(TargetInstrInfo::EXTRACT_SUBREG));
383
384 // Figure out the register class to create for the destreg.
385 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
386 const TargetRegisterClass *TRC = MRI.getRegClass(VReg);
387 const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx);
388
389 if (VRBase) {
390 // Grab the destination register
391#ifndef NDEBUG
392 const TargetRegisterClass *DRC = MRI.getRegClass(VRBase);
393 assert(SRC && DRC && SRC == DRC &&
394 "Source subregister and destination must have the same class");
395#endif
396 } else {
397 // Create the reg
398 assert(SRC && "Couldn't find source register class");
399 VRBase = MRI.createVirtualRegister(SRC);
400 }
401
402 // Add def, source, and subreg index
403 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
404 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
405 MI->addOperand(MachineOperand::CreateImm(SubIdx));
406 BB->push_back(MI);
407 } else if (Opc == TargetInstrInfo::INSERT_SUBREG ||
408 Opc == TargetInstrInfo::SUBREG_TO_REG) {
409 SDValue N0 = Node->getOperand(0);
410 SDValue N1 = Node->getOperand(1);
411 SDValue N2 = Node->getOperand(2);
412 unsigned SubReg = getVR(N1, VRBaseMap);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000413 unsigned SubIdx = cast<ConstantSDNode>(N2)->getZExtValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000414
415
416 // Figure out the register class to create for the destreg.
417 const TargetRegisterClass *TRC = 0;
418 if (VRBase) {
419 TRC = MRI.getRegClass(VRBase);
420 } else {
421 TRC = getSuperRegisterRegClass(MRI.getRegClass(SubReg), SubIdx,
422 Node->getValueType(0));
423 assert(TRC && "Couldn't determine register class for insert_subreg");
424 VRBase = MRI.createVirtualRegister(TRC); // Create the reg
425 }
426
427 // Create the insert_subreg or subreg_to_reg machine instruction.
428 MachineInstr *MI = BuildMI(*MF, TII->get(Opc));
429 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
430
431 // If creating a subreg_to_reg, then the first input operand
432 // is an implicit value immediate, otherwise it's a register
433 if (Opc == TargetInstrInfo::SUBREG_TO_REG) {
434 const ConstantSDNode *SD = cast<ConstantSDNode>(N0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000435 MI->addOperand(MachineOperand::CreateImm(SD->getZExtValue()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000436 } else
437 AddOperand(MI, N0, 0, 0, VRBaseMap);
438 // Add the subregster being inserted
439 AddOperand(MI, N1, 0, 0, VRBaseMap);
440 MI->addOperand(MachineOperand::CreateImm(SubIdx));
441 BB->push_back(MI);
442 } else
443 assert(0 && "Node is not insert_subreg, extract_subreg, or subreg_to_reg");
444
445 SDValue Op(Node, 0);
446 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
447 isNew = isNew; // Silence compiler warning.
448 assert(isNew && "Node emitted out of order - early");
449}
450
451/// EmitNode - Generate machine code for an node and needed dependencies.
452///
Dan Gohman343f0c02008-11-19 23:18:57 +0000453void ScheduleDAGSDNodes::EmitNode(SDNode *Node, bool IsClone,
454 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000455 // If machine instruction
456 if (Node->isMachineOpcode()) {
457 unsigned Opc = Node->getMachineOpcode();
458
459 // Handle subreg insert/extract specially
460 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
461 Opc == TargetInstrInfo::INSERT_SUBREG ||
462 Opc == TargetInstrInfo::SUBREG_TO_REG) {
463 EmitSubregNode(Node, VRBaseMap);
464 return;
465 }
466
467 if (Opc == TargetInstrInfo::IMPLICIT_DEF)
468 // We want a unique VR for each IMPLICIT_DEF use.
469 return;
470
471 const TargetInstrDesc &II = TII->get(Opc);
472 unsigned NumResults = CountResults(Node);
473 unsigned NodeOperands = CountOperands(Node);
474 unsigned MemOperandsEnd = ComputeMemOperandsEnd(Node);
475 bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
476 II.getImplicitDefs() != 0;
477#ifndef NDEBUG
478 unsigned NumMIOperands = NodeOperands + NumResults;
479 assert((II.getNumOperands() == NumMIOperands ||
480 HasPhysRegOuts || II.isVariadic()) &&
481 "#operands for dag node doesn't match .td file!");
482#endif
483
484 // Create the new machine instruction.
485 MachineInstr *MI = BuildMI(*MF, II);
486
487 // Add result register values for things that are defined by this
488 // instruction.
489 if (NumResults)
490 CreateVirtualRegisters(Node, MI, II, VRBaseMap);
491
492 // Emit all of the actual operands of this instruction, adding them to the
493 // instruction as appropriate.
494 for (unsigned i = 0; i != NodeOperands; ++i)
495 AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap);
496
497 // Emit all of the memory operands of this instruction
498 for (unsigned i = NodeOperands; i != MemOperandsEnd; ++i)
499 AddMemOperand(MI, cast<MemOperandSDNode>(Node->getOperand(i))->MO);
500
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000501 if (II.usesCustomDAGSchedInsertionHook())
502 // Insert this instruction into the basic block using a target
503 // specific inserter which may returns a new basic block.
504 BB = TLI->EmitInstrWithCustomInserter(MI, BB);
505 else
506 BB->push_back(MI);
507
508 // Additional results must be an physical register def.
509 if (HasPhysRegOuts) {
510 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
511 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
512 if (Node->hasAnyUseOfValue(i))
513 EmitCopyFromReg(Node, i, IsClone, Reg, VRBaseMap);
514 }
515 }
516 return;
517 }
518
519 switch (Node->getOpcode()) {
520 default:
521#ifndef NDEBUG
Dan Gohmana23b3b82008-11-13 21:21:28 +0000522 Node->dump(DAG);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000523#endif
524 assert(0 && "This target-independent node should have been selected!");
525 break;
526 case ISD::EntryToken:
527 assert(0 && "EntryToken should have been excluded from the schedule!");
528 break;
529 case ISD::TokenFactor: // fall thru
530 break;
531 case ISD::CopyToReg: {
532 unsigned SrcReg;
533 SDValue SrcVal = Node->getOperand(2);
534 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
535 SrcReg = R->getReg();
536 else
537 SrcReg = getVR(SrcVal, VRBaseMap);
538
539 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
540 if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
541 break;
542
543 const TargetRegisterClass *SrcTRC = 0, *DstTRC = 0;
544 // Get the register classes of the src/dst.
545 if (TargetRegisterInfo::isVirtualRegister(SrcReg))
546 SrcTRC = MRI.getRegClass(SrcReg);
547 else
548 SrcTRC = TRI->getPhysicalRegisterRegClass(SrcReg,SrcVal.getValueType());
549
550 if (TargetRegisterInfo::isVirtualRegister(DestReg))
551 DstTRC = MRI.getRegClass(DestReg);
552 else
553 DstTRC = TRI->getPhysicalRegisterRegClass(DestReg,
554 Node->getOperand(1).getValueType());
555 TII->copyRegToReg(*BB, BB->end(), DestReg, SrcReg, DstTRC, SrcTRC);
556 break;
557 }
558 case ISD::CopyFromReg: {
559 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
560 EmitCopyFromReg(Node, 0, IsClone, SrcReg, VRBaseMap);
561 break;
562 }
563 case ISD::INLINEASM: {
564 unsigned NumOps = Node->getNumOperands();
565 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
566 --NumOps; // Ignore the flag operand.
567
568 // Create the inline asm machine instruction.
569 MachineInstr *MI = BuildMI(*MF, TII->get(TargetInstrInfo::INLINEASM));
570
571 // Add the asm string as an external symbol operand.
Bill Wendling056292f2008-09-16 21:48:12 +0000572 const char *AsmStr =
573 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000574 MI->addOperand(MachineOperand::CreateES(AsmStr));
575
576 // Add all of the operand registers to the instruction.
577 for (unsigned i = 2; i != NumOps;) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000578 unsigned Flags =
579 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000580 unsigned NumVals = Flags >> 3;
581
582 MI->addOperand(MachineOperand::CreateImm(Flags));
583 ++i; // Skip the ID value.
584
585 switch (Flags & 7) {
586 default: assert(0 && "Bad flags!");
587 case 2: // Def of register.
588 for (; NumVals; --NumVals, ++i) {
589 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
590 MI->addOperand(MachineOperand::CreateReg(Reg, true));
591 }
592 break;
Dale Johannesen913d3df2008-09-12 17:49:03 +0000593 case 6: // Def of earlyclobber register.
594 for (; NumVals; --NumVals, ++i) {
595 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
596 MI->addOperand(MachineOperand::CreateReg(Reg, true, false, false,
597 false, 0, true));
598 }
599 break;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000600 case 1: // Use of register.
601 case 3: // Immediate.
602 case 4: // Addressing mode.
603 // The addressing mode has been selected, just add all of the
604 // operands to the machine instruction.
605 for (; NumVals; --NumVals, ++i)
Dale Johannesen86b49f82008-09-24 01:07:17 +0000606 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000607 break;
608 }
609 }
610 BB->push_back(MI);
611 break;
612 }
613 }
614}
615
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000616/// EmitSchedule - Emit the machine code in scheduled order.
Dan Gohman343f0c02008-11-19 23:18:57 +0000617MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000618 DenseMap<SDValue, unsigned> VRBaseMap;
619 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
620 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
621 SUnit *SU = Sequence[i];
622 if (!SU) {
623 // Null SUnit* is a noop.
624 EmitNoop();
625 continue;
626 }
Dan Gohmanf449bf32008-11-14 00:06:09 +0000627
Dan Gohmanf449bf32008-11-14 00:06:09 +0000628 // For pre-regalloc scheduling, create instructions corresponding to the
629 // SDNode and any flagged SDNodes and append them to the block.
Dan Gohmand23e0f82008-11-13 23:24:17 +0000630 SmallVector<SDNode *, 4> FlaggedNodes;
631 for (SDNode *N = SU->getNode()->getFlaggedNode(); N; N = N->getFlaggedNode())
632 FlaggedNodes.push_back(N);
633 while (!FlaggedNodes.empty()) {
634 EmitNode(FlaggedNodes.back(), SU->OrigNode != SU, VRBaseMap);
635 FlaggedNodes.pop_back();
636 }
Dan Gohman550f5af2008-11-13 21:36:12 +0000637 if (!SU->getNode())
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000638 EmitCrossRCCopy(SU, CopyVRBaseMap);
639 else
Dan Gohman550f5af2008-11-13 21:36:12 +0000640 EmitNode(SU->getNode(), SU->OrigNode != SU, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000641 }
642
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000643 return BB;
644}