blob: d6179651589c1b72faefae1f5362a3dcf7bd57e3 [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,
Evan Cheng5c3c5a42009-01-09 22:44:02 +0000160 const TargetInstrDesc &II, bool IsClone,
161 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000162 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);
Evan Cheng5c3c5a42009-01-09 22:44:02 +0000195 if (IsClone)
196 VRBaseMap.erase(Op);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000197 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)
Evan Cheng5c3c5a42009-01-09 22:44:02 +0000492 CreateVirtualRegisters(Node, MI, II, IsClone, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000493
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
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000503 if (II.usesCustomDAGSchedInsertionHook())
504 // Insert this instruction into the basic block using a target
505 // specific inserter which may returns a new basic block.
506 BB = TLI->EmitInstrWithCustomInserter(MI, BB);
507 else
508 BB->push_back(MI);
509
510 // Additional results must be an physical register def.
511 if (HasPhysRegOuts) {
512 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
513 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
514 if (Node->hasAnyUseOfValue(i))
515 EmitCopyFromReg(Node, i, IsClone, Reg, VRBaseMap);
516 }
517 }
518 return;
519 }
520
521 switch (Node->getOpcode()) {
522 default:
523#ifndef NDEBUG
Dan Gohmana23b3b82008-11-13 21:21:28 +0000524 Node->dump(DAG);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000525#endif
526 assert(0 && "This target-independent node should have been selected!");
527 break;
528 case ISD::EntryToken:
529 assert(0 && "EntryToken should have been excluded from the schedule!");
530 break;
531 case ISD::TokenFactor: // fall thru
532 break;
533 case ISD::CopyToReg: {
534 unsigned SrcReg;
535 SDValue SrcVal = Node->getOperand(2);
536 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
537 SrcReg = R->getReg();
538 else
539 SrcReg = getVR(SrcVal, VRBaseMap);
540
541 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
542 if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
543 break;
544
545 const TargetRegisterClass *SrcTRC = 0, *DstTRC = 0;
546 // Get the register classes of the src/dst.
547 if (TargetRegisterInfo::isVirtualRegister(SrcReg))
548 SrcTRC = MRI.getRegClass(SrcReg);
549 else
550 SrcTRC = TRI->getPhysicalRegisterRegClass(SrcReg,SrcVal.getValueType());
551
552 if (TargetRegisterInfo::isVirtualRegister(DestReg))
553 DstTRC = MRI.getRegClass(DestReg);
554 else
555 DstTRC = TRI->getPhysicalRegisterRegClass(DestReg,
556 Node->getOperand(1).getValueType());
557 TII->copyRegToReg(*BB, BB->end(), DestReg, SrcReg, DstTRC, SrcTRC);
558 break;
559 }
560 case ISD::CopyFromReg: {
561 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
562 EmitCopyFromReg(Node, 0, IsClone, SrcReg, VRBaseMap);
563 break;
564 }
565 case ISD::INLINEASM: {
566 unsigned NumOps = Node->getNumOperands();
567 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
568 --NumOps; // Ignore the flag operand.
569
570 // Create the inline asm machine instruction.
571 MachineInstr *MI = BuildMI(*MF, TII->get(TargetInstrInfo::INLINEASM));
572
573 // Add the asm string as an external symbol operand.
Bill Wendling056292f2008-09-16 21:48:12 +0000574 const char *AsmStr =
575 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000576 MI->addOperand(MachineOperand::CreateES(AsmStr));
577
578 // Add all of the operand registers to the instruction.
579 for (unsigned i = 2; i != NumOps;) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000580 unsigned Flags =
581 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000582 unsigned NumVals = Flags >> 3;
583
584 MI->addOperand(MachineOperand::CreateImm(Flags));
585 ++i; // Skip the ID value.
586
587 switch (Flags & 7) {
588 default: assert(0 && "Bad flags!");
589 case 2: // Def of register.
590 for (; NumVals; --NumVals, ++i) {
591 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
592 MI->addOperand(MachineOperand::CreateReg(Reg, true));
593 }
594 break;
Dale Johannesen913d3df2008-09-12 17:49:03 +0000595 case 6: // Def of earlyclobber register.
596 for (; NumVals; --NumVals, ++i) {
597 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
598 MI->addOperand(MachineOperand::CreateReg(Reg, true, false, false,
599 false, 0, true));
600 }
601 break;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000602 case 1: // Use of register.
603 case 3: // Immediate.
604 case 4: // Addressing mode.
605 // The addressing mode has been selected, just add all of the
606 // operands to the machine instruction.
607 for (; NumVals; --NumVals, ++i)
Dale Johannesen86b49f82008-09-24 01:07:17 +0000608 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000609 break;
610 }
611 }
612 BB->push_back(MI);
613 break;
614 }
615 }
616}
617
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000618/// EmitSchedule - Emit the machine code in scheduled order.
Dan Gohman343f0c02008-11-19 23:18:57 +0000619MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000620 DenseMap<SDValue, unsigned> VRBaseMap;
621 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
622 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
623 SUnit *SU = Sequence[i];
624 if (!SU) {
625 // Null SUnit* is a noop.
626 EmitNoop();
627 continue;
628 }
Dan Gohmanf449bf32008-11-14 00:06:09 +0000629
Dan Gohmanf449bf32008-11-14 00:06:09 +0000630 // For pre-regalloc scheduling, create instructions corresponding to the
631 // SDNode and any flagged SDNodes and append them to the block.
Evan Chengc29a56d2009-01-12 03:19:55 +0000632 if (!SU->getNode()) {
633 // Emit a copy.
634 EmitPhysRegCopy(SU, CopyVRBaseMap);
635 continue;
636 }
637
Dan Gohmand23e0f82008-11-13 23:24:17 +0000638 SmallVector<SDNode *, 4> FlaggedNodes;
639 for (SDNode *N = SU->getNode()->getFlaggedNode(); N; N = N->getFlaggedNode())
640 FlaggedNodes.push_back(N);
641 while (!FlaggedNodes.empty()) {
642 EmitNode(FlaggedNodes.back(), SU->OrigNode != SU, VRBaseMap);
643 FlaggedNodes.pop_back();
644 }
Evan Chengc29a56d2009-01-12 03:19:55 +0000645 EmitNode(SU->getNode(), SU->OrigNode != SU, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000646 }
647
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000648 return BB;
649}