blob: abeb7f4dbf7ce4436f422a80d3f6b97ef87b1365 [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 Gohman84fbac52009-02-06 17:22:58 +000016#include "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,
Evan Cheng770bcc72009-02-06 17:43:24 +000035 const TargetInstrDesc &II, unsigned Op) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +000036 if (Op >= II.getNumOperands()) {
37 assert(II.isVariadic() && "Invalid operand # of instruction");
38 return NULL;
39 }
40 if (II.OpInfo[Op].isLookupPtrRegClass())
Evan Cheng770bcc72009-02-06 17:43:24 +000041 return TRI->getPointerRegClass();
Dan Gohman94b8d7e2008-09-03 16:01:59 +000042 return TRI->getRegClass(II.OpInfo[Op].RegClass);
43}
44
45/// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an
46/// implicit physical register output.
Dan Gohman343f0c02008-11-19 23:18:57 +000047void ScheduleDAGSDNodes::EmitCopyFromReg(SDNode *Node, unsigned ResNo,
Evan Chenge57187c2009-01-16 20:57:18 +000048 bool IsClone, bool IsCloned,
49 unsigned SrcReg,
Dan Gohman343f0c02008-11-19 23:18:57 +000050 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;
Evan Chenge57187c2009-01-16 20:57:18 +000067 if (!IsClone && !IsCloned)
68 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
69 UI != E; ++UI) {
70 SDNode *User = *UI;
71 bool Match = true;
72 if (User->getOpcode() == ISD::CopyToReg &&
73 User->getOperand(2).getNode() == Node &&
74 User->getOperand(2).getResNo() == ResNo) {
75 unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
76 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
77 VRBase = DestReg;
78 Match = false;
79 } else if (DestReg != SrcReg)
80 Match = false;
81 } else {
82 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
83 SDValue Op = User->getOperand(i);
84 if (Op.getNode() != Node || Op.getResNo() != ResNo)
85 continue;
86 MVT VT = Node->getValueType(Op.getResNo());
87 if (VT == MVT::Other || VT == MVT::Flag)
88 continue;
89 Match = false;
90 if (User->isMachineOpcode()) {
91 const TargetInstrDesc &II = TII->get(User->getMachineOpcode());
92 const TargetRegisterClass *RC =
Evan Cheng770bcc72009-02-06 17:43:24 +000093 getInstrOperandRegClass(TRI, II, i+II.getNumDefs());
Evan Chenge57187c2009-01-16 20:57:18 +000094 if (!UseRC)
95 UseRC = RC;
96 else if (RC)
97 assert(UseRC == RC &&
98 "Multiple uses expecting different register classes!");
99 }
Evan Cheng1cd33272008-09-16 23:12:11 +0000100 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000101 }
Evan Chenge57187c2009-01-16 20:57:18 +0000102 MatchReg &= Match;
103 if (VRBase)
104 break;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000105 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000106
Evan Cheng1cd33272008-09-16 23:12:11 +0000107 MVT VT = Node->getValueType(ResNo);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000108 const TargetRegisterClass *SrcRC = 0, *DstRC = 0;
Evan Cheng1cd33272008-09-16 23:12:11 +0000109 SrcRC = TRI->getPhysicalRegisterRegClass(SrcReg, VT);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000110
111 // Figure out the register class to create for the destreg.
112 if (VRBase) {
113 DstRC = MRI.getRegClass(VRBase);
Evan Cheng1cd33272008-09-16 23:12:11 +0000114 } else if (UseRC) {
115 assert(UseRC->hasType(VT) && "Incompatible phys register def and uses!");
116 DstRC = UseRC;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000117 } else {
Evan Cheng1cd33272008-09-16 23:12:11 +0000118 DstRC = TLI->getRegClassFor(VT);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000119 }
120
121 // If all uses are reading from the src physical register and copying the
122 // register is either impossible or very expensive, then don't create a copy.
123 if (MatchReg && SrcRC->getCopyCost() < 0) {
124 VRBase = SrcReg;
125 } else {
126 // Create the reg, emit the copy.
127 VRBase = MRI.createVirtualRegister(DstRC);
Dan Gohman47ac0f02009-02-11 04:27:20 +0000128 bool Emitted = TII->copyRegToReg(*BB, InsertPos, VRBase, SrcReg,
129 DstRC, SrcRC);
Evan Cheng4ded02f2009-02-09 22:47:36 +0000130 if (!Emitted) {
131 cerr << "Unable to issue a copy instruction!\n";
132 abort();
133 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000134 }
135
136 SDValue Op(Node, ResNo);
137 if (IsClone)
138 VRBaseMap.erase(Op);
139 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
140 isNew = isNew; // Silence compiler warning.
141 assert(isNew && "Node emitted out of order - early");
142}
143
144/// getDstOfCopyToRegUse - If the only use of the specified result number of
145/// node is a CopyToReg, return its destination register. Return 0 otherwise.
Dan Gohman343f0c02008-11-19 23:18:57 +0000146unsigned ScheduleDAGSDNodes::getDstOfOnlyCopyToRegUse(SDNode *Node,
147 unsigned ResNo) const {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000148 if (!Node->hasOneUse())
149 return 0;
150
151 SDNode *User = *Node->use_begin();
152 if (User->getOpcode() == ISD::CopyToReg &&
153 User->getOperand(2).getNode() == Node &&
154 User->getOperand(2).getResNo() == ResNo) {
155 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
156 if (TargetRegisterInfo::isVirtualRegister(Reg))
157 return Reg;
158 }
159 return 0;
160}
161
Dan Gohman343f0c02008-11-19 23:18:57 +0000162void ScheduleDAGSDNodes::CreateVirtualRegisters(SDNode *Node, MachineInstr *MI,
Evan Chenge57187c2009-01-16 20:57:18 +0000163 const TargetInstrDesc &II,
164 bool IsClone, bool IsCloned,
Evan Cheng5c3c5a42009-01-09 22:44:02 +0000165 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000166 assert(Node->getMachineOpcode() != TargetInstrInfo::IMPLICIT_DEF &&
167 "IMPLICIT_DEF should have been handled as a special case elsewhere!");
168
169 for (unsigned i = 0; i < II.getNumDefs(); ++i) {
170 // If the specific node value is only used by a CopyToReg and the dest reg
171 // is a vreg, use the CopyToReg'd destination register instead of creating
172 // a new vreg.
173 unsigned VRBase = 0;
Evan Chenge57187c2009-01-16 20:57:18 +0000174
175 if (!IsClone && !IsCloned)
176 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
177 UI != E; ++UI) {
178 SDNode *User = *UI;
179 if (User->getOpcode() == ISD::CopyToReg &&
180 User->getOperand(2).getNode() == Node &&
181 User->getOperand(2).getResNo() == i) {
182 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
183 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
184 VRBase = Reg;
185 MI->addOperand(MachineOperand::CreateReg(Reg, true));
186 break;
187 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000188 }
189 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000190
191 // Create the result registers for this node and add the result regs to
192 // the machine instruction.
193 if (VRBase == 0) {
Evan Cheng770bcc72009-02-06 17:43:24 +0000194 const TargetRegisterClass *RC = getInstrOperandRegClass(TRI, II, i);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000195 assert(RC && "Isn't a register operand!");
196 VRBase = MRI.createVirtualRegister(RC);
197 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
198 }
199
200 SDValue Op(Node, i);
Evan Cheng5c3c5a42009-01-09 22:44:02 +0000201 if (IsClone)
202 VRBaseMap.erase(Op);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000203 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
204 isNew = isNew; // Silence compiler warning.
205 assert(isNew && "Node emitted out of order - early");
206 }
207}
208
209/// getVR - Return the virtual register corresponding to the specified result
210/// of the specified node.
Dan Gohman343f0c02008-11-19 23:18:57 +0000211unsigned ScheduleDAGSDNodes::getVR(SDValue Op,
212 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000213 if (Op.isMachineOpcode() &&
214 Op.getMachineOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
215 // Add an IMPLICIT_DEF instruction before every use.
216 unsigned VReg = getDstOfOnlyCopyToRegUse(Op.getNode(), Op.getResNo());
217 // IMPLICIT_DEF can produce any type of result so its TargetInstrDesc
218 // does not include operand register class info.
219 if (!VReg) {
220 const TargetRegisterClass *RC = TLI->getRegClassFor(Op.getValueType());
221 VReg = MRI.createVirtualRegister(RC);
222 }
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000223 BuildMI(BB, Op.getDebugLoc(), TII->get(TargetInstrInfo::IMPLICIT_DEF),VReg);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000224 return VReg;
225 }
226
227 DenseMap<SDValue, unsigned>::iterator I = VRBaseMap.find(Op);
228 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
229 return I->second;
230}
231
232
233/// AddOperand - Add the specified operand to the specified machine instr. II
234/// specifies the instruction information for the node, and IIOpNum is the
235/// operand number (in the II) that we are adding. IIOpNum and II are used for
236/// assertions only.
Dan Gohman343f0c02008-11-19 23:18:57 +0000237void ScheduleDAGSDNodes::AddOperand(MachineInstr *MI, SDValue Op,
238 unsigned IIOpNum,
239 const TargetInstrDesc *II,
240 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000241 if (Op.isMachineOpcode()) {
242 // Note that this case is redundant with the final else block, but we
243 // include it because it is the most common and it makes the logic
244 // simpler here.
245 assert(Op.getValueType() != MVT::Other &&
246 Op.getValueType() != MVT::Flag &&
247 "Chain and flag operands should occur at end of operand list!");
248 // Get/emit the operand.
249 unsigned VReg = getVR(Op, VRBaseMap);
250 const TargetInstrDesc &TID = MI->getDesc();
251 bool isOptDef = IIOpNum < TID.getNumOperands() &&
252 TID.OpInfo[IIOpNum].isOptionalDef();
Dale Johannesen86b49f82008-09-24 01:07:17 +0000253 MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000254
255 // Verify that it is right.
256 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
257#ifndef NDEBUG
258 if (II) {
259 // There may be no register class for this operand if it is a variadic
260 // argument (RC will be NULL in this case). In this case, we just assume
261 // the regclass is ok.
Evan Cheng770bcc72009-02-06 17:43:24 +0000262 const TargetRegisterClass *RC= getInstrOperandRegClass(TRI, *II, IIOpNum);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000263 assert((RC || II->isVariadic()) && "Expected reg class info!");
264 const TargetRegisterClass *VRC = MRI.getRegClass(VReg);
265 if (RC && VRC != RC) {
266 cerr << "Register class of operand and regclass of use don't agree!\n";
267 cerr << "Operand = " << IIOpNum << "\n";
Dan Gohmana23b3b82008-11-13 21:21:28 +0000268 cerr << "Op->Val = "; Op.getNode()->dump(DAG); cerr << "\n";
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000269 cerr << "MI = "; MI->print(cerr);
270 cerr << "VReg = " << VReg << "\n";
Dan Gohman03558622009-04-10 15:59:38 +0000271 cerr << "VReg RegClass " << VRC->getName()
272 << " size = " << VRC->getSize()
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000273 << ", align = " << VRC->getAlignment() << "\n";
Dan Gohman03558622009-04-10 15:59:38 +0000274 cerr << "Expected RegClass " << RC->getName()
275 << " size = " << RC->getSize()
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000276 << ", align = " << RC->getAlignment() << "\n";
277 cerr << "Fatal error, aborting.\n";
278 abort();
279 }
280 }
281#endif
282 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000283 MI->addOperand(MachineOperand::CreateImm(C->getZExtValue()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000284 } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000285 const ConstantFP *CFP = F->getConstantFPValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000286 MI->addOperand(MachineOperand::CreateFPImm(CFP));
287 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
Dale Johannesen86b49f82008-09-24 01:07:17 +0000288 MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000289 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
290 MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(),TGA->getOffset()));
291 } else if (BasicBlockSDNode *BB = dyn_cast<BasicBlockSDNode>(Op)) {
292 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
293 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
294 MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
295 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
296 MI->addOperand(MachineOperand::CreateJTI(JT->getIndex()));
297 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
298 int Offset = CP->getOffset();
299 unsigned Align = CP->getAlignment();
300 const Type *Type = CP->getType();
301 // MachineConstantPool wants an explicit alignment.
302 if (Align == 0) {
Evan Cheng1606e8e2009-03-13 07:51:59 +0000303 Align = TM.getTargetData()->getPrefTypeAlignment(Type);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000304 if (Align == 0) {
305 // Alignment of vector types. FIXME!
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000306 Align = TM.getTargetData()->getTypePaddedSize(Type);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000307 }
308 }
309
310 unsigned Idx;
311 if (CP->isMachineConstantPoolEntry())
312 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
313 else
314 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
315 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
Bill Wendling056292f2008-09-16 21:48:12 +0000316 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000317 MI->addOperand(MachineOperand::CreateES(ES->getSymbol()));
318 } else {
319 assert(Op.getValueType() != MVT::Other &&
320 Op.getValueType() != MVT::Flag &&
321 "Chain and flag operands should occur at end of operand list!");
322 unsigned VReg = getVR(Op, VRBaseMap);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000323 MI->addOperand(MachineOperand::CreateReg(VReg, false));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000324
325 // Verify that it is right. Note that the reg class of the physreg and the
326 // vreg don't necessarily need to match, but the target copy insertion has
327 // to be able to handle it. This handles things like copies from ST(0) to
328 // an FP vreg on x86.
329 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
330 if (II && !II->isVariadic()) {
Evan Cheng770bcc72009-02-06 17:43:24 +0000331 assert(getInstrOperandRegClass(TRI, *II, IIOpNum) &&
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000332 "Don't have operand info for this instruction!");
333 }
334 }
335}
336
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000337/// EmitSubregNode - Generate machine code for subreg nodes.
338///
Dan Gohman343f0c02008-11-19 23:18:57 +0000339void ScheduleDAGSDNodes::EmitSubregNode(SDNode *Node,
340 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000341 unsigned VRBase = 0;
342 unsigned Opc = Node->getMachineOpcode();
343
344 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
345 // the CopyToReg'd destination register instead of creating a new vreg.
346 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
347 UI != E; ++UI) {
348 SDNode *User = *UI;
349 if (User->getOpcode() == ISD::CopyToReg &&
350 User->getOperand(2).getNode() == Node) {
351 unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
352 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
353 VRBase = DestReg;
354 break;
355 }
356 }
357 }
358
359 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000360 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000361
362 // Create the extract_subreg machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000363 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(),
364 TII->get(TargetInstrInfo::EXTRACT_SUBREG));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000365
366 // Figure out the register class to create for the destreg.
Evan Cheng536ab132009-01-22 09:10:11 +0000367 const TargetRegisterClass *SRC = TLI->getRegClassFor(Node->getValueType(0));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000368
369 if (VRBase) {
370 // Grab the destination register
371#ifndef NDEBUG
372 const TargetRegisterClass *DRC = MRI.getRegClass(VRBase);
373 assert(SRC && DRC && SRC == DRC &&
374 "Source subregister and destination must have the same class");
375#endif
376 } else {
377 // Create the reg
378 assert(SRC && "Couldn't find source register class");
379 VRBase = MRI.createVirtualRegister(SRC);
380 }
381
382 // Add def, source, and subreg index
383 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
384 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
385 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Dan Gohman47ac0f02009-02-11 04:27:20 +0000386 BB->insert(InsertPos, MI);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000387 } else if (Opc == TargetInstrInfo::INSERT_SUBREG ||
388 Opc == TargetInstrInfo::SUBREG_TO_REG) {
389 SDValue N0 = Node->getOperand(0);
390 SDValue N1 = Node->getOperand(1);
391 SDValue N2 = Node->getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000392 unsigned SubIdx = cast<ConstantSDNode>(N2)->getZExtValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000393
394
395 // Figure out the register class to create for the destreg.
396 const TargetRegisterClass *TRC = 0;
397 if (VRBase) {
398 TRC = MRI.getRegClass(VRBase);
399 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000400 TRC = TLI->getRegClassFor(Node->getValueType(0));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000401 assert(TRC && "Couldn't determine register class for insert_subreg");
402 VRBase = MRI.createVirtualRegister(TRC); // Create the reg
403 }
404
405 // Create the insert_subreg or subreg_to_reg machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000406 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(), TII->get(Opc));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000407 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
408
409 // If creating a subreg_to_reg, then the first input operand
410 // is an implicit value immediate, otherwise it's a register
411 if (Opc == TargetInstrInfo::SUBREG_TO_REG) {
412 const ConstantSDNode *SD = cast<ConstantSDNode>(N0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000413 MI->addOperand(MachineOperand::CreateImm(SD->getZExtValue()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000414 } else
415 AddOperand(MI, N0, 0, 0, VRBaseMap);
416 // Add the subregster being inserted
417 AddOperand(MI, N1, 0, 0, VRBaseMap);
418 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Dan Gohman47ac0f02009-02-11 04:27:20 +0000419 BB->insert(InsertPos, MI);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000420 } else
421 assert(0 && "Node is not insert_subreg, extract_subreg, or subreg_to_reg");
422
423 SDValue Op(Node, 0);
424 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
425 isNew = isNew; // Silence compiler warning.
426 assert(isNew && "Node emitted out of order - early");
427}
428
429/// EmitNode - Generate machine code for an node and needed dependencies.
430///
Evan Chenge57187c2009-01-16 20:57:18 +0000431void ScheduleDAGSDNodes::EmitNode(SDNode *Node, bool IsClone, bool IsCloned,
Dan Gohman343f0c02008-11-19 23:18:57 +0000432 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000433 // If machine instruction
434 if (Node->isMachineOpcode()) {
435 unsigned Opc = Node->getMachineOpcode();
436
437 // Handle subreg insert/extract specially
438 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
439 Opc == TargetInstrInfo::INSERT_SUBREG ||
440 Opc == TargetInstrInfo::SUBREG_TO_REG) {
441 EmitSubregNode(Node, VRBaseMap);
442 return;
443 }
444
445 if (Opc == TargetInstrInfo::IMPLICIT_DEF)
446 // We want a unique VR for each IMPLICIT_DEF use.
447 return;
448
449 const TargetInstrDesc &II = TII->get(Opc);
450 unsigned NumResults = CountResults(Node);
451 unsigned NodeOperands = CountOperands(Node);
452 unsigned MemOperandsEnd = ComputeMemOperandsEnd(Node);
453 bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
454 II.getImplicitDefs() != 0;
455#ifndef NDEBUG
456 unsigned NumMIOperands = NodeOperands + NumResults;
457 assert((II.getNumOperands() == NumMIOperands ||
458 HasPhysRegOuts || II.isVariadic()) &&
459 "#operands for dag node doesn't match .td file!");
460#endif
461
462 // Create the new machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000463 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(), II);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000464
465 // Add result register values for things that are defined by this
466 // instruction.
467 if (NumResults)
Evan Chenge57187c2009-01-16 20:57:18 +0000468 CreateVirtualRegisters(Node, MI, II, IsClone, IsCloned, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000469
470 // Emit all of the actual operands of this instruction, adding them to the
471 // instruction as appropriate.
472 for (unsigned i = 0; i != NodeOperands; ++i)
473 AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap);
474
475 // Emit all of the memory operands of this instruction
476 for (unsigned i = NodeOperands; i != MemOperandsEnd; ++i)
477 AddMemOperand(MI, cast<MemOperandSDNode>(Node->getOperand(i))->MO);
478
Dan Gohmanf7119392009-01-16 22:10:20 +0000479 if (II.usesCustomDAGSchedInsertionHook()) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000480 // Insert this instruction into the basic block using a target
481 // specific inserter which may returns a new basic block.
482 BB = TLI->EmitInstrWithCustomInserter(MI, BB);
Dan Gohman47ac0f02009-02-11 04:27:20 +0000483 InsertPos = BB->end();
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000484 } else {
Dan Gohman47ac0f02009-02-11 04:27:20 +0000485 BB->insert(InsertPos, MI);
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000486 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000487
488 // Additional results must be an physical register def.
489 if (HasPhysRegOuts) {
490 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
491 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
492 if (Node->hasAnyUseOfValue(i))
Evan Chenge57187c2009-01-16 20:57:18 +0000493 EmitCopyFromReg(Node, i, IsClone, IsCloned, Reg, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000494 }
495 }
496 return;
497 }
498
499 switch (Node->getOpcode()) {
500 default:
501#ifndef NDEBUG
Dan Gohmana23b3b82008-11-13 21:21:28 +0000502 Node->dump(DAG);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000503#endif
504 assert(0 && "This target-independent node should have been selected!");
505 break;
506 case ISD::EntryToken:
507 assert(0 && "EntryToken should have been excluded from the schedule!");
508 break;
509 case ISD::TokenFactor: // fall thru
510 break;
511 case ISD::CopyToReg: {
512 unsigned SrcReg;
513 SDValue SrcVal = Node->getOperand(2);
514 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
515 SrcReg = R->getReg();
516 else
517 SrcReg = getVR(SrcVal, VRBaseMap);
518
519 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
520 if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
521 break;
522
523 const TargetRegisterClass *SrcTRC = 0, *DstTRC = 0;
524 // Get the register classes of the src/dst.
525 if (TargetRegisterInfo::isVirtualRegister(SrcReg))
526 SrcTRC = MRI.getRegClass(SrcReg);
527 else
528 SrcTRC = TRI->getPhysicalRegisterRegClass(SrcReg,SrcVal.getValueType());
529
530 if (TargetRegisterInfo::isVirtualRegister(DestReg))
531 DstTRC = MRI.getRegClass(DestReg);
532 else
533 DstTRC = TRI->getPhysicalRegisterRegClass(DestReg,
534 Node->getOperand(1).getValueType());
Dan Gohman47ac0f02009-02-11 04:27:20 +0000535 bool Emitted = TII->copyRegToReg(*BB, InsertPos, DestReg, SrcReg,
536 DstTRC, SrcTRC);
Evan Cheng4ded02f2009-02-09 22:47:36 +0000537 if (!Emitted) {
538 cerr << "Unable to issue a copy instruction!\n";
539 abort();
540 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000541 break;
542 }
543 case ISD::CopyFromReg: {
544 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Evan Chenge57187c2009-01-16 20:57:18 +0000545 EmitCopyFromReg(Node, 0, IsClone, IsCloned, SrcReg, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000546 break;
547 }
548 case ISD::INLINEASM: {
549 unsigned NumOps = Node->getNumOperands();
550 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
551 --NumOps; // Ignore the flag operand.
552
553 // Create the inline asm machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000554 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(),
555 TII->get(TargetInstrInfo::INLINEASM));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000556
557 // Add the asm string as an external symbol operand.
Bill Wendling056292f2008-09-16 21:48:12 +0000558 const char *AsmStr =
559 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000560 MI->addOperand(MachineOperand::CreateES(AsmStr));
561
562 // Add all of the operand registers to the instruction.
563 for (unsigned i = 2; i != NumOps;) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000564 unsigned Flags =
565 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +0000566 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000567
568 MI->addOperand(MachineOperand::CreateImm(Flags));
569 ++i; // Skip the ID value.
570
571 switch (Flags & 7) {
572 default: assert(0 && "Bad flags!");
573 case 2: // Def of register.
574 for (; NumVals; --NumVals, ++i) {
575 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
576 MI->addOperand(MachineOperand::CreateReg(Reg, true));
577 }
578 break;
Dale Johannesen913d3df2008-09-12 17:49:03 +0000579 case 6: // Def of earlyclobber register.
580 for (; NumVals; --NumVals, ++i) {
581 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
582 MI->addOperand(MachineOperand::CreateReg(Reg, true, false, false,
583 false, 0, true));
584 }
585 break;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000586 case 1: // Use of register.
587 case 3: // Immediate.
588 case 4: // Addressing mode.
589 // The addressing mode has been selected, just add all of the
590 // operands to the machine instruction.
591 for (; NumVals; --NumVals, ++i)
Dale Johannesen86b49f82008-09-24 01:07:17 +0000592 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000593 break;
594 }
595 }
Dan Gohman47ac0f02009-02-11 04:27:20 +0000596 BB->insert(InsertPos, MI);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000597 break;
598 }
599 }
600}
601
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000602/// EmitSchedule - Emit the machine code in scheduled order.
Dan Gohman343f0c02008-11-19 23:18:57 +0000603MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000604 DenseMap<SDValue, unsigned> VRBaseMap;
605 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
606 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
607 SUnit *SU = Sequence[i];
608 if (!SU) {
609 // Null SUnit* is a noop.
610 EmitNoop();
611 continue;
612 }
Dan Gohmanf449bf32008-11-14 00:06:09 +0000613
Dan Gohmanf449bf32008-11-14 00:06:09 +0000614 // For pre-regalloc scheduling, create instructions corresponding to the
615 // SDNode and any flagged SDNodes and append them to the block.
Evan Chengc29a56d2009-01-12 03:19:55 +0000616 if (!SU->getNode()) {
617 // Emit a copy.
618 EmitPhysRegCopy(SU, CopyVRBaseMap);
619 continue;
620 }
621
Dan Gohmand23e0f82008-11-13 23:24:17 +0000622 SmallVector<SDNode *, 4> FlaggedNodes;
Evan Chenge57187c2009-01-16 20:57:18 +0000623 for (SDNode *N = SU->getNode()->getFlaggedNode(); N;
624 N = N->getFlaggedNode())
Dan Gohmand23e0f82008-11-13 23:24:17 +0000625 FlaggedNodes.push_back(N);
626 while (!FlaggedNodes.empty()) {
Evan Chenge57187c2009-01-16 20:57:18 +0000627 EmitNode(FlaggedNodes.back(), SU->OrigNode != SU, SU->isCloned,VRBaseMap);
Dan Gohmand23e0f82008-11-13 23:24:17 +0000628 FlaggedNodes.pop_back();
629 }
Evan Chenge57187c2009-01-16 20:57:18 +0000630 EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000631 }
632
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000633 return BB;
634}