blob: aa29d3efa77828f760b361ebf519c3914281639d [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 Cheng1cd33272008-09-16 23:12:11 +0000128 bool Emitted =
Dan Gohmanf7119392009-01-16 22:10:20 +0000129 TII->copyRegToReg(*BB, End, VRBase, SrcReg, DstRC, SrcRC);
Evan Cheng1cd33272008-09-16 23:12:11 +0000130 Emitted = Emitted; // Silence compiler warning.
131 assert(Emitted && "Unable to issue a copy instruction!");
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000132 }
133
134 SDValue Op(Node, ResNo);
135 if (IsClone)
136 VRBaseMap.erase(Op);
137 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
138 isNew = isNew; // Silence compiler warning.
139 assert(isNew && "Node emitted out of order - early");
140}
141
142/// getDstOfCopyToRegUse - If the only use of the specified result number of
143/// node is a CopyToReg, return its destination register. Return 0 otherwise.
Dan Gohman343f0c02008-11-19 23:18:57 +0000144unsigned ScheduleDAGSDNodes::getDstOfOnlyCopyToRegUse(SDNode *Node,
145 unsigned ResNo) const {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000146 if (!Node->hasOneUse())
147 return 0;
148
149 SDNode *User = *Node->use_begin();
150 if (User->getOpcode() == ISD::CopyToReg &&
151 User->getOperand(2).getNode() == Node &&
152 User->getOperand(2).getResNo() == ResNo) {
153 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
154 if (TargetRegisterInfo::isVirtualRegister(Reg))
155 return Reg;
156 }
157 return 0;
158}
159
Dan Gohman343f0c02008-11-19 23:18:57 +0000160void ScheduleDAGSDNodes::CreateVirtualRegisters(SDNode *Node, MachineInstr *MI,
Evan Chenge57187c2009-01-16 20:57:18 +0000161 const TargetInstrDesc &II,
162 bool IsClone, bool IsCloned,
Evan Cheng5c3c5a42009-01-09 22:44:02 +0000163 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000164 assert(Node->getMachineOpcode() != TargetInstrInfo::IMPLICIT_DEF &&
165 "IMPLICIT_DEF should have been handled as a special case elsewhere!");
166
167 for (unsigned i = 0; i < II.getNumDefs(); ++i) {
168 // If the specific node value is only used by a CopyToReg and the dest reg
169 // is a vreg, use the CopyToReg'd destination register instead of creating
170 // a new vreg.
171 unsigned VRBase = 0;
Evan Chenge57187c2009-01-16 20:57:18 +0000172
173 if (!IsClone && !IsCloned)
174 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
175 UI != E; ++UI) {
176 SDNode *User = *UI;
177 if (User->getOpcode() == ISD::CopyToReg &&
178 User->getOperand(2).getNode() == Node &&
179 User->getOperand(2).getResNo() == i) {
180 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
181 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
182 VRBase = Reg;
183 MI->addOperand(MachineOperand::CreateReg(Reg, true));
184 break;
185 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000186 }
187 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000188
189 // Create the result registers for this node and add the result regs to
190 // the machine instruction.
191 if (VRBase == 0) {
Evan Cheng770bcc72009-02-06 17:43:24 +0000192 const TargetRegisterClass *RC = getInstrOperandRegClass(TRI, II, i);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000193 assert(RC && "Isn't a register operand!");
194 VRBase = MRI.createVirtualRegister(RC);
195 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
196 }
197
198 SDValue Op(Node, i);
Evan Cheng5c3c5a42009-01-09 22:44:02 +0000199 if (IsClone)
200 VRBaseMap.erase(Op);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000201 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
202 isNew = isNew; // Silence compiler warning.
203 assert(isNew && "Node emitted out of order - early");
204 }
205}
206
207/// getVR - Return the virtual register corresponding to the specified result
208/// of the specified node.
Dan Gohman343f0c02008-11-19 23:18:57 +0000209unsigned ScheduleDAGSDNodes::getVR(SDValue Op,
210 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000211 if (Op.isMachineOpcode() &&
212 Op.getMachineOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
213 // Add an IMPLICIT_DEF instruction before every use.
214 unsigned VReg = getDstOfOnlyCopyToRegUse(Op.getNode(), Op.getResNo());
215 // IMPLICIT_DEF can produce any type of result so its TargetInstrDesc
216 // does not include operand register class info.
217 if (!VReg) {
218 const TargetRegisterClass *RC = TLI->getRegClassFor(Op.getValueType());
219 VReg = MRI.createVirtualRegister(RC);
220 }
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000221 BuildMI(BB, Op.getDebugLoc(), TII->get(TargetInstrInfo::IMPLICIT_DEF),VReg);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000222 return VReg;
223 }
224
225 DenseMap<SDValue, unsigned>::iterator I = VRBaseMap.find(Op);
226 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
227 return I->second;
228}
229
230
231/// AddOperand - Add the specified operand to the specified machine instr. II
232/// specifies the instruction information for the node, and IIOpNum is the
233/// operand number (in the II) that we are adding. IIOpNum and II are used for
234/// assertions only.
Dan Gohman343f0c02008-11-19 23:18:57 +0000235void ScheduleDAGSDNodes::AddOperand(MachineInstr *MI, SDValue Op,
236 unsigned IIOpNum,
237 const TargetInstrDesc *II,
238 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000239 if (Op.isMachineOpcode()) {
240 // Note that this case is redundant with the final else block, but we
241 // include it because it is the most common and it makes the logic
242 // simpler here.
243 assert(Op.getValueType() != MVT::Other &&
244 Op.getValueType() != MVT::Flag &&
245 "Chain and flag operands should occur at end of operand list!");
246 // Get/emit the operand.
247 unsigned VReg = getVR(Op, VRBaseMap);
248 const TargetInstrDesc &TID = MI->getDesc();
249 bool isOptDef = IIOpNum < TID.getNumOperands() &&
250 TID.OpInfo[IIOpNum].isOptionalDef();
Dale Johannesen86b49f82008-09-24 01:07:17 +0000251 MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000252
253 // Verify that it is right.
254 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
255#ifndef NDEBUG
256 if (II) {
257 // There may be no register class for this operand if it is a variadic
258 // argument (RC will be NULL in this case). In this case, we just assume
259 // the regclass is ok.
Evan Cheng770bcc72009-02-06 17:43:24 +0000260 const TargetRegisterClass *RC= getInstrOperandRegClass(TRI, *II, IIOpNum);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000261 assert((RC || II->isVariadic()) && "Expected reg class info!");
262 const TargetRegisterClass *VRC = MRI.getRegClass(VReg);
263 if (RC && VRC != RC) {
264 cerr << "Register class of operand and regclass of use don't agree!\n";
265 cerr << "Operand = " << IIOpNum << "\n";
Dan Gohmana23b3b82008-11-13 21:21:28 +0000266 cerr << "Op->Val = "; Op.getNode()->dump(DAG); cerr << "\n";
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000267 cerr << "MI = "; MI->print(cerr);
268 cerr << "VReg = " << VReg << "\n";
269 cerr << "VReg RegClass size = " << VRC->getSize()
270 << ", align = " << VRC->getAlignment() << "\n";
271 cerr << "Expected RegClass size = " << RC->getSize()
272 << ", align = " << RC->getAlignment() << "\n";
273 cerr << "Fatal error, aborting.\n";
274 abort();
275 }
276 }
277#endif
278 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000279 MI->addOperand(MachineOperand::CreateImm(C->getZExtValue()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000280 } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000281 const ConstantFP *CFP = F->getConstantFPValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000282 MI->addOperand(MachineOperand::CreateFPImm(CFP));
283 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
Dale Johannesen86b49f82008-09-24 01:07:17 +0000284 MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000285 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
286 MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(),TGA->getOffset()));
287 } else if (BasicBlockSDNode *BB = dyn_cast<BasicBlockSDNode>(Op)) {
288 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
289 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
290 MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
291 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
292 MI->addOperand(MachineOperand::CreateJTI(JT->getIndex()));
293 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
294 int Offset = CP->getOffset();
295 unsigned Align = CP->getAlignment();
296 const Type *Type = CP->getType();
297 // MachineConstantPool wants an explicit alignment.
298 if (Align == 0) {
299 Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
300 if (Align == 0) {
301 // Alignment of vector types. FIXME!
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000302 Align = TM.getTargetData()->getTypePaddedSize(Type);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000303 Align = Log2_64(Align);
304 }
305 }
306
307 unsigned Idx;
308 if (CP->isMachineConstantPoolEntry())
309 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
310 else
311 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
312 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
Bill Wendling056292f2008-09-16 21:48:12 +0000313 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000314 MI->addOperand(MachineOperand::CreateES(ES->getSymbol()));
315 } else {
316 assert(Op.getValueType() != MVT::Other &&
317 Op.getValueType() != MVT::Flag &&
318 "Chain and flag operands should occur at end of operand list!");
319 unsigned VReg = getVR(Op, VRBaseMap);
Dale Johannesen86b49f82008-09-24 01:07:17 +0000320 MI->addOperand(MachineOperand::CreateReg(VReg, false));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000321
322 // Verify that it is right. Note that the reg class of the physreg and the
323 // vreg don't necessarily need to match, but the target copy insertion has
324 // to be able to handle it. This handles things like copies from ST(0) to
325 // an FP vreg on x86.
326 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
327 if (II && !II->isVariadic()) {
Evan Cheng770bcc72009-02-06 17:43:24 +0000328 assert(getInstrOperandRegClass(TRI, *II, IIOpNum) &&
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000329 "Don't have operand info for this instruction!");
330 }
331 }
332}
333
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000334/// EmitSubregNode - Generate machine code for subreg nodes.
335///
Dan Gohman343f0c02008-11-19 23:18:57 +0000336void ScheduleDAGSDNodes::EmitSubregNode(SDNode *Node,
337 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000338 unsigned VRBase = 0;
339 unsigned Opc = Node->getMachineOpcode();
340
341 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
342 // the CopyToReg'd destination register instead of creating a new vreg.
343 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
344 UI != E; ++UI) {
345 SDNode *User = *UI;
346 if (User->getOpcode() == ISD::CopyToReg &&
347 User->getOperand(2).getNode() == Node) {
348 unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
349 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
350 VRBase = DestReg;
351 break;
352 }
353 }
354 }
355
356 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000357 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000358
359 // Create the extract_subreg machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000360 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(),
361 TII->get(TargetInstrInfo::EXTRACT_SUBREG));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000362
363 // Figure out the register class to create for the destreg.
Evan Cheng536ab132009-01-22 09:10:11 +0000364 const TargetRegisterClass *SRC = TLI->getRegClassFor(Node->getValueType(0));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000365
366 if (VRBase) {
367 // Grab the destination register
368#ifndef NDEBUG
369 const TargetRegisterClass *DRC = MRI.getRegClass(VRBase);
370 assert(SRC && DRC && SRC == DRC &&
371 "Source subregister and destination must have the same class");
372#endif
373 } else {
374 // Create the reg
375 assert(SRC && "Couldn't find source register class");
376 VRBase = MRI.createVirtualRegister(SRC);
377 }
378
379 // Add def, source, and subreg index
380 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
381 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
382 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Dan Gohmanf7119392009-01-16 22:10:20 +0000383 BB->insert(End, MI);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000384 } else if (Opc == TargetInstrInfo::INSERT_SUBREG ||
385 Opc == TargetInstrInfo::SUBREG_TO_REG) {
386 SDValue N0 = Node->getOperand(0);
387 SDValue N1 = Node->getOperand(1);
388 SDValue N2 = Node->getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000389 unsigned SubIdx = cast<ConstantSDNode>(N2)->getZExtValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000390
391
392 // Figure out the register class to create for the destreg.
393 const TargetRegisterClass *TRC = 0;
394 if (VRBase) {
395 TRC = MRI.getRegClass(VRBase);
396 } else {
Evan Cheng536ab132009-01-22 09:10:11 +0000397 TRC = TLI->getRegClassFor(Node->getValueType(0));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000398 assert(TRC && "Couldn't determine register class for insert_subreg");
399 VRBase = MRI.createVirtualRegister(TRC); // Create the reg
400 }
401
402 // Create the insert_subreg or subreg_to_reg machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000403 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(), TII->get(Opc));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000404 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
405
406 // If creating a subreg_to_reg, then the first input operand
407 // is an implicit value immediate, otherwise it's a register
408 if (Opc == TargetInstrInfo::SUBREG_TO_REG) {
409 const ConstantSDNode *SD = cast<ConstantSDNode>(N0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000410 MI->addOperand(MachineOperand::CreateImm(SD->getZExtValue()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000411 } else
412 AddOperand(MI, N0, 0, 0, VRBaseMap);
413 // Add the subregster being inserted
414 AddOperand(MI, N1, 0, 0, VRBaseMap);
415 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Dan Gohmanf7119392009-01-16 22:10:20 +0000416 BB->insert(End, MI);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000417 } else
418 assert(0 && "Node is not insert_subreg, extract_subreg, or subreg_to_reg");
419
420 SDValue Op(Node, 0);
421 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
422 isNew = isNew; // Silence compiler warning.
423 assert(isNew && "Node emitted out of order - early");
424}
425
426/// EmitNode - Generate machine code for an node and needed dependencies.
427///
Evan Chenge57187c2009-01-16 20:57:18 +0000428void ScheduleDAGSDNodes::EmitNode(SDNode *Node, bool IsClone, bool IsCloned,
Dan Gohman343f0c02008-11-19 23:18:57 +0000429 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000430 // If machine instruction
431 if (Node->isMachineOpcode()) {
432 unsigned Opc = Node->getMachineOpcode();
433
434 // Handle subreg insert/extract specially
435 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
436 Opc == TargetInstrInfo::INSERT_SUBREG ||
437 Opc == TargetInstrInfo::SUBREG_TO_REG) {
438 EmitSubregNode(Node, VRBaseMap);
439 return;
440 }
441
442 if (Opc == TargetInstrInfo::IMPLICIT_DEF)
443 // We want a unique VR for each IMPLICIT_DEF use.
444 return;
445
446 const TargetInstrDesc &II = TII->get(Opc);
447 unsigned NumResults = CountResults(Node);
448 unsigned NodeOperands = CountOperands(Node);
449 unsigned MemOperandsEnd = ComputeMemOperandsEnd(Node);
450 bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
451 II.getImplicitDefs() != 0;
452#ifndef NDEBUG
453 unsigned NumMIOperands = NodeOperands + NumResults;
454 assert((II.getNumOperands() == NumMIOperands ||
455 HasPhysRegOuts || II.isVariadic()) &&
456 "#operands for dag node doesn't match .td file!");
457#endif
458
459 // Create the new machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000460 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(), II);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000461
462 // Add result register values for things that are defined by this
463 // instruction.
464 if (NumResults)
Evan Chenge57187c2009-01-16 20:57:18 +0000465 CreateVirtualRegisters(Node, MI, II, IsClone, IsCloned, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000466
467 // Emit all of the actual operands of this instruction, adding them to the
468 // instruction as appropriate.
469 for (unsigned i = 0; i != NodeOperands; ++i)
470 AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap);
471
472 // Emit all of the memory operands of this instruction
473 for (unsigned i = NodeOperands; i != MemOperandsEnd; ++i)
474 AddMemOperand(MI, cast<MemOperandSDNode>(Node->getOperand(i))->MO);
475
Dan Gohmanf7119392009-01-16 22:10:20 +0000476 if (II.usesCustomDAGSchedInsertionHook()) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000477 // Insert this instruction into the basic block using a target
478 // specific inserter which may returns a new basic block.
479 BB = TLI->EmitInstrWithCustomInserter(MI, BB);
Dan Gohmanf7119392009-01-16 22:10:20 +0000480 Begin = End = BB->end();
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000481 } else {
Dan Gohmanf7119392009-01-16 22:10:20 +0000482 BB->insert(End, MI);
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000483 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000484
485 // Additional results must be an physical register def.
486 if (HasPhysRegOuts) {
487 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
488 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
489 if (Node->hasAnyUseOfValue(i))
Evan Chenge57187c2009-01-16 20:57:18 +0000490 EmitCopyFromReg(Node, i, IsClone, IsCloned, Reg, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000491 }
492 }
493 return;
494 }
495
496 switch (Node->getOpcode()) {
497 default:
498#ifndef NDEBUG
Dan Gohmana23b3b82008-11-13 21:21:28 +0000499 Node->dump(DAG);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000500#endif
501 assert(0 && "This target-independent node should have been selected!");
502 break;
503 case ISD::EntryToken:
504 assert(0 && "EntryToken should have been excluded from the schedule!");
505 break;
506 case ISD::TokenFactor: // fall thru
507 break;
508 case ISD::CopyToReg: {
509 unsigned SrcReg;
510 SDValue SrcVal = Node->getOperand(2);
511 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
512 SrcReg = R->getReg();
513 else
514 SrcReg = getVR(SrcVal, VRBaseMap);
515
516 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
517 if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
518 break;
519
520 const TargetRegisterClass *SrcTRC = 0, *DstTRC = 0;
521 // Get the register classes of the src/dst.
522 if (TargetRegisterInfo::isVirtualRegister(SrcReg))
523 SrcTRC = MRI.getRegClass(SrcReg);
524 else
525 SrcTRC = TRI->getPhysicalRegisterRegClass(SrcReg,SrcVal.getValueType());
526
527 if (TargetRegisterInfo::isVirtualRegister(DestReg))
528 DstTRC = MRI.getRegClass(DestReg);
529 else
530 DstTRC = TRI->getPhysicalRegisterRegClass(DestReg,
531 Node->getOperand(1).getValueType());
Dan Gohmanf7119392009-01-16 22:10:20 +0000532 TII->copyRegToReg(*BB, End, DestReg, SrcReg, DstTRC, SrcTRC);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000533 break;
534 }
535 case ISD::CopyFromReg: {
536 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Evan Chenge57187c2009-01-16 20:57:18 +0000537 EmitCopyFromReg(Node, 0, IsClone, IsCloned, SrcReg, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000538 break;
539 }
540 case ISD::INLINEASM: {
541 unsigned NumOps = Node->getNumOperands();
542 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
543 --NumOps; // Ignore the flag operand.
544
545 // Create the inline asm machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000546 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(),
547 TII->get(TargetInstrInfo::INLINEASM));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000548
549 // Add the asm string as an external symbol operand.
Bill Wendling056292f2008-09-16 21:48:12 +0000550 const char *AsmStr =
551 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000552 MI->addOperand(MachineOperand::CreateES(AsmStr));
553
554 // Add all of the operand registers to the instruction.
555 for (unsigned i = 2; i != NumOps;) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000556 unsigned Flags =
557 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000558 unsigned NumVals = Flags >> 3;
559
560 MI->addOperand(MachineOperand::CreateImm(Flags));
561 ++i; // Skip the ID value.
562
563 switch (Flags & 7) {
564 default: assert(0 && "Bad flags!");
565 case 2: // Def of register.
566 for (; NumVals; --NumVals, ++i) {
567 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
568 MI->addOperand(MachineOperand::CreateReg(Reg, true));
569 }
570 break;
Dale Johannesen913d3df2008-09-12 17:49:03 +0000571 case 6: // Def of earlyclobber register.
572 for (; NumVals; --NumVals, ++i) {
573 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
574 MI->addOperand(MachineOperand::CreateReg(Reg, true, false, false,
575 false, 0, true));
576 }
577 break;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000578 case 1: // Use of register.
579 case 3: // Immediate.
580 case 4: // Addressing mode.
581 // The addressing mode has been selected, just add all of the
582 // operands to the machine instruction.
583 for (; NumVals; --NumVals, ++i)
Dale Johannesen86b49f82008-09-24 01:07:17 +0000584 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000585 break;
586 }
587 }
Dan Gohmanf7119392009-01-16 22:10:20 +0000588 BB->insert(End, MI);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000589 break;
590 }
591 }
592}
593
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000594/// EmitSchedule - Emit the machine code in scheduled order.
Dan Gohman343f0c02008-11-19 23:18:57 +0000595MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000596 DenseMap<SDValue, unsigned> VRBaseMap;
597 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
598 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
599 SUnit *SU = Sequence[i];
600 if (!SU) {
601 // Null SUnit* is a noop.
602 EmitNoop();
603 continue;
604 }
Dan Gohmanf449bf32008-11-14 00:06:09 +0000605
Dan Gohmanf449bf32008-11-14 00:06:09 +0000606 // For pre-regalloc scheduling, create instructions corresponding to the
607 // SDNode and any flagged SDNodes and append them to the block.
Evan Chengc29a56d2009-01-12 03:19:55 +0000608 if (!SU->getNode()) {
609 // Emit a copy.
610 EmitPhysRegCopy(SU, CopyVRBaseMap);
611 continue;
612 }
613
Dan Gohmand23e0f82008-11-13 23:24:17 +0000614 SmallVector<SDNode *, 4> FlaggedNodes;
Evan Chenge57187c2009-01-16 20:57:18 +0000615 for (SDNode *N = SU->getNode()->getFlaggedNode(); N;
616 N = N->getFlaggedNode())
Dan Gohmand23e0f82008-11-13 23:24:17 +0000617 FlaggedNodes.push_back(N);
618 while (!FlaggedNodes.empty()) {
Evan Chenge57187c2009-01-16 20:57:18 +0000619 EmitNode(FlaggedNodes.back(), SU->OrigNode != SU, SU->isCloned,VRBaseMap);
Dan Gohmand23e0f82008-11-13 23:24:17 +0000620 FlaggedNodes.pop_back();
621 }
Evan Chenge57187c2009-01-16 20:57:18 +0000622 EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000623 }
624
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000625 return BB;
626}