blob: 1d369c1bc44413e5ba96da375ba71ebeefeca6c6 [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);
Evan Cheng4ded02f2009-02-09 22:47:36 +0000128 bool Emitted = TII->copyRegToReg(*BB, End, VRBase, SrcReg, DstRC, SrcRC);
129 if (!Emitted) {
130 cerr << "Unable to issue a copy instruction!\n";
131 abort();
132 }
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,
Evan Chenge57187c2009-01-16 20:57:18 +0000162 const TargetInstrDesc &II,
163 bool IsClone, bool IsCloned,
Evan Cheng5c3c5a42009-01-09 22:44:02 +0000164 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000165 assert(Node->getMachineOpcode() != TargetInstrInfo::IMPLICIT_DEF &&
166 "IMPLICIT_DEF should have been handled as a special case elsewhere!");
167
168 for (unsigned i = 0; i < II.getNumDefs(); ++i) {
169 // If the specific node value is only used by a CopyToReg and the dest reg
170 // is a vreg, use the CopyToReg'd destination register instead of creating
171 // a new vreg.
172 unsigned VRBase = 0;
Evan Chenge57187c2009-01-16 20:57:18 +0000173
174 if (!IsClone && !IsCloned)
175 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
176 UI != E; ++UI) {
177 SDNode *User = *UI;
178 if (User->getOpcode() == ISD::CopyToReg &&
179 User->getOperand(2).getNode() == Node &&
180 User->getOperand(2).getResNo() == i) {
181 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
182 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
183 VRBase = Reg;
184 MI->addOperand(MachineOperand::CreateReg(Reg, true));
185 break;
186 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000187 }
188 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000189
190 // Create the result registers for this node and add the result regs to
191 // the machine instruction.
192 if (VRBase == 0) {
Evan Cheng770bcc72009-02-06 17:43:24 +0000193 const TargetRegisterClass *RC = getInstrOperandRegClass(TRI, II, i);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000194 assert(RC && "Isn't a register operand!");
195 VRBase = MRI.createVirtualRegister(RC);
196 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
197 }
198
199 SDValue Op(Node, i);
Evan Cheng5c3c5a42009-01-09 22:44:02 +0000200 if (IsClone)
201 VRBaseMap.erase(Op);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000202 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
203 isNew = isNew; // Silence compiler warning.
204 assert(isNew && "Node emitted out of order - early");
205 }
206}
207
208/// getVR - Return the virtual register corresponding to the specified result
209/// of the specified node.
Dan Gohman343f0c02008-11-19 23:18:57 +0000210unsigned ScheduleDAGSDNodes::getVR(SDValue Op,
211 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000212 if (Op.isMachineOpcode() &&
213 Op.getMachineOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
214 // Add an IMPLICIT_DEF instruction before every use.
215 unsigned VReg = getDstOfOnlyCopyToRegUse(Op.getNode(), Op.getResNo());
216 // IMPLICIT_DEF can produce any type of result so its TargetInstrDesc
217 // does not include operand register class info.
218 if (!VReg) {
219 const TargetRegisterClass *RC = TLI->getRegClassFor(Op.getValueType());
220 VReg = MRI.createVirtualRegister(RC);
221 }
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000222 BuildMI(BB, Op.getDebugLoc(), TII->get(TargetInstrInfo::IMPLICIT_DEF),VReg);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000223 return VReg;
224 }
225
226 DenseMap<SDValue, unsigned>::iterator I = VRBaseMap.find(Op);
227 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
228 return I->second;
229}
230
231
232/// AddOperand - Add the specified operand to the specified machine instr. II
233/// specifies the instruction information for the node, and IIOpNum is the
234/// operand number (in the II) that we are adding. IIOpNum and II are used for
235/// assertions only.
Dan Gohman343f0c02008-11-19 23:18:57 +0000236void ScheduleDAGSDNodes::AddOperand(MachineInstr *MI, SDValue Op,
237 unsigned IIOpNum,
238 const TargetInstrDesc *II,
239 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000240 if (Op.isMachineOpcode()) {
241 // Note that this case is redundant with the final else block, but we
242 // include it because it is the most common and it makes the logic
243 // simpler here.
244 assert(Op.getValueType() != MVT::Other &&
245 Op.getValueType() != MVT::Flag &&
246 "Chain and flag operands should occur at end of operand list!");
247 // Get/emit the operand.
248 unsigned VReg = getVR(Op, VRBaseMap);
249 const TargetInstrDesc &TID = MI->getDesc();
250 bool isOptDef = IIOpNum < TID.getNumOperands() &&
251 TID.OpInfo[IIOpNum].isOptionalDef();
Dale Johannesen86b49f82008-09-24 01:07:17 +0000252 MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000253
254 // Verify that it is right.
255 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
256#ifndef NDEBUG
257 if (II) {
258 // There may be no register class for this operand if it is a variadic
259 // argument (RC will be NULL in this case). In this case, we just assume
260 // the regclass is ok.
Evan Cheng770bcc72009-02-06 17:43:24 +0000261 const TargetRegisterClass *RC= getInstrOperandRegClass(TRI, *II, IIOpNum);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000262 assert((RC || II->isVariadic()) && "Expected reg class info!");
263 const TargetRegisterClass *VRC = MRI.getRegClass(VReg);
264 if (RC && VRC != RC) {
265 cerr << "Register class of operand and regclass of use don't agree!\n";
266 cerr << "Operand = " << IIOpNum << "\n";
Dan Gohmana23b3b82008-11-13 21:21:28 +0000267 cerr << "Op->Val = "; Op.getNode()->dump(DAG); cerr << "\n";
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000268 cerr << "MI = "; MI->print(cerr);
269 cerr << "VReg = " << VReg << "\n";
270 cerr << "VReg RegClass size = " << VRC->getSize()
271 << ", align = " << VRC->getAlignment() << "\n";
272 cerr << "Expected RegClass size = " << RC->getSize()
273 << ", align = " << RC->getAlignment() << "\n";
274 cerr << "Fatal error, aborting.\n";
275 abort();
276 }
277 }
278#endif
279 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000280 MI->addOperand(MachineOperand::CreateImm(C->getZExtValue()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000281 } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000282 const ConstantFP *CFP = F->getConstantFPValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000283 MI->addOperand(MachineOperand::CreateFPImm(CFP));
284 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
Dale Johannesen86b49f82008-09-24 01:07:17 +0000285 MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000286 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
287 MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(),TGA->getOffset()));
288 } else if (BasicBlockSDNode *BB = dyn_cast<BasicBlockSDNode>(Op)) {
289 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
290 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
291 MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
292 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
293 MI->addOperand(MachineOperand::CreateJTI(JT->getIndex()));
294 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
295 int Offset = CP->getOffset();
296 unsigned Align = CP->getAlignment();
297 const Type *Type = CP->getType();
298 // MachineConstantPool wants an explicit alignment.
299 if (Align == 0) {
300 Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
301 if (Align == 0) {
302 // Alignment of vector types. FIXME!
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000303 Align = TM.getTargetData()->getTypePaddedSize(Type);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000304 Align = Log2_64(Align);
305 }
306 }
307
308 unsigned Idx;
309 if (CP->isMachineConstantPoolEntry())
310 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
311 else
312 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
313 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
Bill Wendling056292f2008-09-16 21:48:12 +0000314 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000315 MI->addOperand(MachineOperand::CreateES(ES->getSymbol()));
316 } else {
317 assert(Op.getValueType() != MVT::Other &&
318 Op.getValueType() != MVT::Flag &&
319 "Chain and flag operands should occur at end of operand list!");
320 unsigned VReg = getVR(Op, VRBaseMap);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000321 MI->addOperand(MachineOperand::CreateReg(VReg, false));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000322
323 // Verify that it is right. Note that the reg class of the physreg and the
324 // vreg don't necessarily need to match, but the target copy insertion has
325 // to be able to handle it. This handles things like copies from ST(0) to
326 // an FP vreg on x86.
327 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
328 if (II && !II->isVariadic()) {
Evan Cheng770bcc72009-02-06 17:43:24 +0000329 assert(getInstrOperandRegClass(TRI, *II, IIOpNum) &&
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000330 "Don't have operand info for this instruction!");
331 }
332 }
333}
334
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000335/// EmitSubregNode - Generate machine code for subreg nodes.
336///
Dan Gohman343f0c02008-11-19 23:18:57 +0000337void ScheduleDAGSDNodes::EmitSubregNode(SDNode *Node,
338 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000339 unsigned VRBase = 0;
340 unsigned Opc = Node->getMachineOpcode();
341
342 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
343 // the CopyToReg'd destination register instead of creating a new vreg.
344 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
345 UI != E; ++UI) {
346 SDNode *User = *UI;
347 if (User->getOpcode() == ISD::CopyToReg &&
348 User->getOperand(2).getNode() == Node) {
349 unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
350 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
351 VRBase = DestReg;
352 break;
353 }
354 }
355 }
356
357 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000358 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000359
360 // Create the extract_subreg machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000361 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(),
362 TII->get(TargetInstrInfo::EXTRACT_SUBREG));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000363
364 // Figure out the register class to create for the destreg.
Evan Cheng536ab132009-01-22 09:10:11 +0000365 const TargetRegisterClass *SRC = TLI->getRegClassFor(Node->getValueType(0));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000366
367 if (VRBase) {
368 // Grab the destination register
369#ifndef NDEBUG
370 const TargetRegisterClass *DRC = MRI.getRegClass(VRBase);
371 assert(SRC && DRC && SRC == DRC &&
372 "Source subregister and destination must have the same class");
373#endif
374 } else {
375 // Create the reg
376 assert(SRC && "Couldn't find source register class");
377 VRBase = MRI.createVirtualRegister(SRC);
378 }
379
380 // Add def, source, and subreg index
381 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
382 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
383 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Dan Gohmanf7119392009-01-16 22:10:20 +0000384 BB->insert(End, MI);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000385 } else if (Opc == TargetInstrInfo::INSERT_SUBREG ||
386 Opc == TargetInstrInfo::SUBREG_TO_REG) {
387 SDValue N0 = Node->getOperand(0);
388 SDValue N1 = Node->getOperand(1);
389 SDValue N2 = Node->getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000390 unsigned SubIdx = cast<ConstantSDNode>(N2)->getZExtValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000391
392
393 // Figure out the register class to create for the destreg.
394 const TargetRegisterClass *TRC = 0;
395 if (VRBase) {
396 TRC = MRI.getRegClass(VRBase);
397 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000398 TRC = TLI->getRegClassFor(Node->getValueType(0));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000399 assert(TRC && "Couldn't determine register class for insert_subreg");
400 VRBase = MRI.createVirtualRegister(TRC); // Create the reg
401 }
402
403 // Create the insert_subreg or subreg_to_reg machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000404 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(), TII->get(Opc));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000405 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
406
407 // If creating a subreg_to_reg, then the first input operand
408 // is an implicit value immediate, otherwise it's a register
409 if (Opc == TargetInstrInfo::SUBREG_TO_REG) {
410 const ConstantSDNode *SD = cast<ConstantSDNode>(N0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000411 MI->addOperand(MachineOperand::CreateImm(SD->getZExtValue()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000412 } else
413 AddOperand(MI, N0, 0, 0, VRBaseMap);
414 // Add the subregster being inserted
415 AddOperand(MI, N1, 0, 0, VRBaseMap);
416 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Dan Gohmanf7119392009-01-16 22:10:20 +0000417 BB->insert(End, MI);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000418 } else
419 assert(0 && "Node is not insert_subreg, extract_subreg, or subreg_to_reg");
420
421 SDValue Op(Node, 0);
422 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
423 isNew = isNew; // Silence compiler warning.
424 assert(isNew && "Node emitted out of order - early");
425}
426
427/// EmitNode - Generate machine code for an node and needed dependencies.
428///
Evan Chenge57187c2009-01-16 20:57:18 +0000429void ScheduleDAGSDNodes::EmitNode(SDNode *Node, bool IsClone, bool IsCloned,
Dan Gohman343f0c02008-11-19 23:18:57 +0000430 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000431 // If machine instruction
432 if (Node->isMachineOpcode()) {
433 unsigned Opc = Node->getMachineOpcode();
434
435 // Handle subreg insert/extract specially
436 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
437 Opc == TargetInstrInfo::INSERT_SUBREG ||
438 Opc == TargetInstrInfo::SUBREG_TO_REG) {
439 EmitSubregNode(Node, VRBaseMap);
440 return;
441 }
442
443 if (Opc == TargetInstrInfo::IMPLICIT_DEF)
444 // We want a unique VR for each IMPLICIT_DEF use.
445 return;
446
447 const TargetInstrDesc &II = TII->get(Opc);
448 unsigned NumResults = CountResults(Node);
449 unsigned NodeOperands = CountOperands(Node);
450 unsigned MemOperandsEnd = ComputeMemOperandsEnd(Node);
451 bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
452 II.getImplicitDefs() != 0;
453#ifndef NDEBUG
454 unsigned NumMIOperands = NodeOperands + NumResults;
455 assert((II.getNumOperands() == NumMIOperands ||
456 HasPhysRegOuts || II.isVariadic()) &&
457 "#operands for dag node doesn't match .td file!");
458#endif
459
460 // Create the new machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000461 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(), II);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000462
463 // Add result register values for things that are defined by this
464 // instruction.
465 if (NumResults)
Evan Chenge57187c2009-01-16 20:57:18 +0000466 CreateVirtualRegisters(Node, MI, II, IsClone, IsCloned, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000467
468 // Emit all of the actual operands of this instruction, adding them to the
469 // instruction as appropriate.
470 for (unsigned i = 0; i != NodeOperands; ++i)
471 AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap);
472
473 // Emit all of the memory operands of this instruction
474 for (unsigned i = NodeOperands; i != MemOperandsEnd; ++i)
475 AddMemOperand(MI, cast<MemOperandSDNode>(Node->getOperand(i))->MO);
476
Dan Gohmanf7119392009-01-16 22:10:20 +0000477 if (II.usesCustomDAGSchedInsertionHook()) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000478 // Insert this instruction into the basic block using a target
479 // specific inserter which may returns a new basic block.
480 BB = TLI->EmitInstrWithCustomInserter(MI, BB);
Dan Gohmanf7119392009-01-16 22:10:20 +0000481 Begin = End = BB->end();
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000482 } else {
Dan Gohmanf7119392009-01-16 22:10:20 +0000483 BB->insert(End, MI);
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000484 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000485
486 // Additional results must be an physical register def.
487 if (HasPhysRegOuts) {
488 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
489 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
490 if (Node->hasAnyUseOfValue(i))
Evan Chenge57187c2009-01-16 20:57:18 +0000491 EmitCopyFromReg(Node, i, IsClone, IsCloned, Reg, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000492 }
493 }
494 return;
495 }
496
497 switch (Node->getOpcode()) {
498 default:
499#ifndef NDEBUG
Dan Gohmana23b3b82008-11-13 21:21:28 +0000500 Node->dump(DAG);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000501#endif
502 assert(0 && "This target-independent node should have been selected!");
503 break;
504 case ISD::EntryToken:
505 assert(0 && "EntryToken should have been excluded from the schedule!");
506 break;
507 case ISD::TokenFactor: // fall thru
508 break;
509 case ISD::CopyToReg: {
510 unsigned SrcReg;
511 SDValue SrcVal = Node->getOperand(2);
512 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
513 SrcReg = R->getReg();
514 else
515 SrcReg = getVR(SrcVal, VRBaseMap);
516
517 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
518 if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
519 break;
520
521 const TargetRegisterClass *SrcTRC = 0, *DstTRC = 0;
522 // Get the register classes of the src/dst.
523 if (TargetRegisterInfo::isVirtualRegister(SrcReg))
524 SrcTRC = MRI.getRegClass(SrcReg);
525 else
526 SrcTRC = TRI->getPhysicalRegisterRegClass(SrcReg,SrcVal.getValueType());
527
528 if (TargetRegisterInfo::isVirtualRegister(DestReg))
529 DstTRC = MRI.getRegClass(DestReg);
530 else
531 DstTRC = TRI->getPhysicalRegisterRegClass(DestReg,
532 Node->getOperand(1).getValueType());
Evan Cheng4ded02f2009-02-09 22:47:36 +0000533 bool Emitted = TII->copyRegToReg(*BB, End, DestReg, SrcReg, DstTRC, SrcTRC);
534 if (!Emitted) {
535 cerr << "Unable to issue a copy instruction!\n";
536 abort();
537 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000538 break;
539 }
540 case ISD::CopyFromReg: {
541 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Evan Chenge57187c2009-01-16 20:57:18 +0000542 EmitCopyFromReg(Node, 0, IsClone, IsCloned, SrcReg, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000543 break;
544 }
545 case ISD::INLINEASM: {
546 unsigned NumOps = Node->getNumOperands();
547 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
548 --NumOps; // Ignore the flag operand.
549
550 // Create the inline asm machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000551 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(),
552 TII->get(TargetInstrInfo::INLINEASM));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000553
554 // Add the asm string as an external symbol operand.
Bill Wendling056292f2008-09-16 21:48:12 +0000555 const char *AsmStr =
556 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000557 MI->addOperand(MachineOperand::CreateES(AsmStr));
558
559 // Add all of the operand registers to the instruction.
560 for (unsigned i = 2; i != NumOps;) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000561 unsigned Flags =
562 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000563 unsigned NumVals = Flags >> 3;
564
565 MI->addOperand(MachineOperand::CreateImm(Flags));
566 ++i; // Skip the ID value.
567
568 switch (Flags & 7) {
569 default: assert(0 && "Bad flags!");
570 case 2: // Def of register.
571 for (; NumVals; --NumVals, ++i) {
572 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
573 MI->addOperand(MachineOperand::CreateReg(Reg, true));
574 }
575 break;
Dale Johannesen913d3df2008-09-12 17:49:03 +0000576 case 6: // Def of earlyclobber register.
577 for (; NumVals; --NumVals, ++i) {
578 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
579 MI->addOperand(MachineOperand::CreateReg(Reg, true, false, false,
580 false, 0, true));
581 }
582 break;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000583 case 1: // Use of register.
584 case 3: // Immediate.
585 case 4: // Addressing mode.
586 // The addressing mode has been selected, just add all of the
587 // operands to the machine instruction.
588 for (; NumVals; --NumVals, ++i)
Dale Johannesen86b49f82008-09-24 01:07:17 +0000589 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000590 break;
591 }
592 }
Dan Gohmanf7119392009-01-16 22:10:20 +0000593 BB->insert(End, MI);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000594 break;
595 }
596 }
597}
598
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000599/// EmitSchedule - Emit the machine code in scheduled order.
Dan Gohman343f0c02008-11-19 23:18:57 +0000600MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000601 DenseMap<SDValue, unsigned> VRBaseMap;
602 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
603 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
604 SUnit *SU = Sequence[i];
605 if (!SU) {
606 // Null SUnit* is a noop.
607 EmitNoop();
608 continue;
609 }
Dan Gohmanf449bf32008-11-14 00:06:09 +0000610
Dan Gohmanf449bf32008-11-14 00:06:09 +0000611 // For pre-regalloc scheduling, create instructions corresponding to the
612 // SDNode and any flagged SDNodes and append them to the block.
Evan Chengc29a56d2009-01-12 03:19:55 +0000613 if (!SU->getNode()) {
614 // Emit a copy.
615 EmitPhysRegCopy(SU, CopyVRBaseMap);
616 continue;
617 }
618
Dan Gohmand23e0f82008-11-13 23:24:17 +0000619 SmallVector<SDNode *, 4> FlaggedNodes;
Evan Chenge57187c2009-01-16 20:57:18 +0000620 for (SDNode *N = SU->getNode()->getFlaggedNode(); N;
621 N = N->getFlaggedNode())
Dan Gohmand23e0f82008-11-13 23:24:17 +0000622 FlaggedNodes.push_back(N);
623 while (!FlaggedNodes.empty()) {
Evan Chenge57187c2009-01-16 20:57:18 +0000624 EmitNode(FlaggedNodes.back(), SU->OrigNode != SU, SU->isCloned,VRBaseMap);
Dan Gohmand23e0f82008-11-13 23:24:17 +0000625 FlaggedNodes.pop_back();
626 }
Evan Chenge57187c2009-01-16 20:57:18 +0000627 EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000628 }
629
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000630 return BB;
631}