blob: 5ef5470d41fd704598f885063a07cc63a8fac3e0 [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/// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an
32/// implicit physical register output.
Chris Lattner52023122009-06-26 05:39:02 +000033void ScheduleDAGSDNodes::
34EmitCopyFromReg(SDNode *Node, unsigned ResNo, bool IsClone, bool IsCloned,
35 unsigned SrcReg, DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +000036 unsigned VRBase = 0;
37 if (TargetRegisterInfo::isVirtualRegister(SrcReg)) {
38 // Just use the input register directly!
39 SDValue Op(Node, ResNo);
40 if (IsClone)
41 VRBaseMap.erase(Op);
42 bool isNew = VRBaseMap.insert(std::make_pair(Op, SrcReg)).second;
43 isNew = isNew; // Silence compiler warning.
44 assert(isNew && "Node emitted out of order - early");
45 return;
46 }
47
48 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
49 // the CopyToReg'd destination register instead of creating a new vreg.
50 bool MatchReg = true;
Evan Cheng1cd33272008-09-16 23:12:11 +000051 const TargetRegisterClass *UseRC = NULL;
Evan Chenge57187c2009-01-16 20:57:18 +000052 if (!IsClone && !IsCloned)
53 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
54 UI != E; ++UI) {
55 SDNode *User = *UI;
56 bool Match = true;
57 if (User->getOpcode() == ISD::CopyToReg &&
58 User->getOperand(2).getNode() == Node &&
59 User->getOperand(2).getResNo() == ResNo) {
60 unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
61 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
62 VRBase = DestReg;
63 Match = false;
64 } else if (DestReg != SrcReg)
65 Match = false;
66 } else {
67 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
68 SDValue Op = User->getOperand(i);
69 if (Op.getNode() != Node || Op.getResNo() != ResNo)
70 continue;
71 MVT VT = Node->getValueType(Op.getResNo());
72 if (VT == MVT::Other || VT == MVT::Flag)
73 continue;
74 Match = false;
75 if (User->isMachineOpcode()) {
76 const TargetInstrDesc &II = TII->get(User->getMachineOpcode());
77 const TargetRegisterClass *RC =
Evan Cheng770bcc72009-02-06 17:43:24 +000078 getInstrOperandRegClass(TRI, II, i+II.getNumDefs());
Evan Chenge57187c2009-01-16 20:57:18 +000079 if (!UseRC)
80 UseRC = RC;
Dan Gohmanf8c73942009-04-13 15:38:05 +000081 else if (RC) {
82 if (UseRC->hasSuperClass(RC))
83 UseRC = RC;
84 else
85 assert((UseRC == RC || RC->hasSuperClass(UseRC)) &&
86 "Multiple uses expecting different register classes!");
87 }
Evan Chenge57187c2009-01-16 20:57:18 +000088 }
Evan Cheng1cd33272008-09-16 23:12:11 +000089 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +000090 }
Evan Chenge57187c2009-01-16 20:57:18 +000091 MatchReg &= Match;
92 if (VRBase)
93 break;
Dan Gohman94b8d7e2008-09-03 16:01:59 +000094 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +000095
Evan Cheng1cd33272008-09-16 23:12:11 +000096 MVT VT = Node->getValueType(ResNo);
Dan Gohman94b8d7e2008-09-03 16:01:59 +000097 const TargetRegisterClass *SrcRC = 0, *DstRC = 0;
Evan Cheng1cd33272008-09-16 23:12:11 +000098 SrcRC = TRI->getPhysicalRegisterRegClass(SrcReg, VT);
Dan Gohman94b8d7e2008-09-03 16:01:59 +000099
100 // Figure out the register class to create for the destreg.
101 if (VRBase) {
102 DstRC = MRI.getRegClass(VRBase);
Evan Cheng1cd33272008-09-16 23:12:11 +0000103 } else if (UseRC) {
104 assert(UseRC->hasType(VT) && "Incompatible phys register def and uses!");
105 DstRC = UseRC;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000106 } else {
Evan Cheng1cd33272008-09-16 23:12:11 +0000107 DstRC = TLI->getRegClassFor(VT);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000108 }
109
110 // If all uses are reading from the src physical register and copying the
111 // register is either impossible or very expensive, then don't create a copy.
112 if (MatchReg && SrcRC->getCopyCost() < 0) {
113 VRBase = SrcReg;
114 } else {
115 // Create the reg, emit the copy.
116 VRBase = MRI.createVirtualRegister(DstRC);
Dan Gohman47ac0f02009-02-11 04:27:20 +0000117 bool Emitted = TII->copyRegToReg(*BB, InsertPos, VRBase, SrcReg,
118 DstRC, SrcRC);
Dan Gohmanf8c73942009-04-13 15:38:05 +0000119
120 assert(Emitted && "Unable to issue a copy instruction!\n");
Daniel Dunbar8c562e22009-05-18 16:43:04 +0000121 (void) Emitted;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000122 }
123
124 SDValue Op(Node, ResNo);
125 if (IsClone)
126 VRBaseMap.erase(Op);
127 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
128 isNew = isNew; // Silence compiler warning.
129 assert(isNew && "Node emitted out of order - early");
130}
131
132/// getDstOfCopyToRegUse - If the only use of the specified result number of
133/// node is a CopyToReg, return its destination register. Return 0 otherwise.
Dan Gohman343f0c02008-11-19 23:18:57 +0000134unsigned ScheduleDAGSDNodes::getDstOfOnlyCopyToRegUse(SDNode *Node,
135 unsigned ResNo) const {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000136 if (!Node->hasOneUse())
137 return 0;
138
139 SDNode *User = *Node->use_begin();
140 if (User->getOpcode() == ISD::CopyToReg &&
141 User->getOperand(2).getNode() == Node &&
142 User->getOperand(2).getResNo() == ResNo) {
143 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
144 if (TargetRegisterInfo::isVirtualRegister(Reg))
145 return Reg;
146 }
147 return 0;
148}
149
Dan Gohman343f0c02008-11-19 23:18:57 +0000150void ScheduleDAGSDNodes::CreateVirtualRegisters(SDNode *Node, MachineInstr *MI,
Evan Chenge57187c2009-01-16 20:57:18 +0000151 const TargetInstrDesc &II,
152 bool IsClone, bool IsCloned,
Evan Cheng5c3c5a42009-01-09 22:44:02 +0000153 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000154 assert(Node->getMachineOpcode() != TargetInstrInfo::IMPLICIT_DEF &&
155 "IMPLICIT_DEF should have been handled as a special case elsewhere!");
156
157 for (unsigned i = 0; i < II.getNumDefs(); ++i) {
158 // If the specific node value is only used by a CopyToReg and the dest reg
Dan Gohmanf8c73942009-04-13 15:38:05 +0000159 // is a vreg in the same register class, use the CopyToReg'd destination
160 // register instead of creating a new vreg.
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000161 unsigned VRBase = 0;
Dan Gohmanf8c73942009-04-13 15:38:05 +0000162 const TargetRegisterClass *RC = getInstrOperandRegClass(TRI, II, i);
Evan Chenge57187c2009-01-16 20:57:18 +0000163
164 if (!IsClone && !IsCloned)
165 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
166 UI != E; ++UI) {
167 SDNode *User = *UI;
168 if (User->getOpcode() == ISD::CopyToReg &&
169 User->getOperand(2).getNode() == Node &&
170 User->getOperand(2).getResNo() == i) {
171 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
172 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Dan Gohmanf8c73942009-04-13 15:38:05 +0000173 const TargetRegisterClass *RegRC = MRI.getRegClass(Reg);
174 if (RegRC == RC) {
175 VRBase = Reg;
176 MI->addOperand(MachineOperand::CreateReg(Reg, true));
177 break;
178 }
Evan Chenge57187c2009-01-16 20:57:18 +0000179 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000180 }
181 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000182
183 // Create the result registers for this node and add the result regs to
184 // the machine instruction.
185 if (VRBase == 0) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000186 assert(RC && "Isn't a register operand!");
187 VRBase = MRI.createVirtualRegister(RC);
188 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
189 }
190
191 SDValue Op(Node, i);
Evan Cheng5c3c5a42009-01-09 22:44:02 +0000192 if (IsClone)
193 VRBaseMap.erase(Op);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000194 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
195 isNew = isNew; // Silence compiler warning.
196 assert(isNew && "Node emitted out of order - early");
197 }
198}
199
200/// getVR - Return the virtual register corresponding to the specified result
201/// of the specified node.
Dan Gohman343f0c02008-11-19 23:18:57 +0000202unsigned ScheduleDAGSDNodes::getVR(SDValue Op,
203 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000204 if (Op.isMachineOpcode() &&
205 Op.getMachineOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
206 // Add an IMPLICIT_DEF instruction before every use.
207 unsigned VReg = getDstOfOnlyCopyToRegUse(Op.getNode(), Op.getResNo());
208 // IMPLICIT_DEF can produce any type of result so its TargetInstrDesc
209 // does not include operand register class info.
210 if (!VReg) {
211 const TargetRegisterClass *RC = TLI->getRegClassFor(Op.getValueType());
212 VReg = MRI.createVirtualRegister(RC);
213 }
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000214 BuildMI(BB, Op.getDebugLoc(), TII->get(TargetInstrInfo::IMPLICIT_DEF),VReg);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000215 return VReg;
216 }
217
218 DenseMap<SDValue, unsigned>::iterator I = VRBaseMap.find(Op);
219 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
220 return I->second;
221}
222
223
Dan Gohmanf8c73942009-04-13 15:38:05 +0000224/// AddRegisterOperand - Add the specified register as an operand to the
225/// specified machine instr. Insert register copies if the register is
226/// not in the required register class.
227void
228ScheduleDAGSDNodes::AddRegisterOperand(MachineInstr *MI, SDValue Op,
229 unsigned IIOpNum,
230 const TargetInstrDesc *II,
231 DenseMap<SDValue, unsigned> &VRBaseMap) {
232 assert(Op.getValueType() != MVT::Other &&
233 Op.getValueType() != MVT::Flag &&
234 "Chain and flag operands should occur at end of operand list!");
235 // Get/emit the operand.
236 unsigned VReg = getVR(Op, VRBaseMap);
237 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
238
239 const TargetInstrDesc &TID = MI->getDesc();
240 bool isOptDef = IIOpNum < TID.getNumOperands() &&
241 TID.OpInfo[IIOpNum].isOptionalDef();
242
243 // If the instruction requires a register in a different class, create
244 // a new virtual register and copy the value into it.
245 if (II) {
246 const TargetRegisterClass *SrcRC =
247 MRI.getRegClass(VReg);
248 const TargetRegisterClass *DstRC =
249 getInstrOperandRegClass(TRI, *II, IIOpNum);
250 assert((DstRC || (TID.isVariadic() && IIOpNum >= TID.getNumOperands())) &&
251 "Don't have operand info for this instruction!");
252 if (DstRC && SrcRC != DstRC && !SrcRC->hasSuperClass(DstRC)) {
253 unsigned NewVReg = MRI.createVirtualRegister(DstRC);
254 bool Emitted = TII->copyRegToReg(*BB, InsertPos, NewVReg, VReg,
255 DstRC, SrcRC);
Dan Gohmanf8c73942009-04-13 15:38:05 +0000256 assert(Emitted && "Unable to issue a copy instruction!\n");
Daniel Dunbar8c562e22009-05-18 16:43:04 +0000257 (void) Emitted;
Dan Gohmanf8c73942009-04-13 15:38:05 +0000258 VReg = NewVReg;
259 }
260 }
261
262 MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
263}
264
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000265/// AddOperand - Add the specified operand to the specified machine instr. II
266/// specifies the instruction information for the node, and IIOpNum is the
267/// operand number (in the II) that we are adding. IIOpNum and II are used for
268/// assertions only.
Dan Gohman343f0c02008-11-19 23:18:57 +0000269void ScheduleDAGSDNodes::AddOperand(MachineInstr *MI, SDValue Op,
270 unsigned IIOpNum,
271 const TargetInstrDesc *II,
272 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000273 if (Op.isMachineOpcode()) {
Dan Gohmanf8c73942009-04-13 15:38:05 +0000274 AddRegisterOperand(MI, Op, IIOpNum, II, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000275 } 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()));
Dan Gohmanf8c73942009-04-13 15:38:05 +0000284 } else if (BasicBlockSDNode *BBNode = dyn_cast<BasicBlockSDNode>(Op)) {
285 MI->addOperand(MachineOperand::CreateMBB(BBNode->getBasicBlock()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000286 } 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) {
Evan Cheng1606e8e2009-03-13 07:51:59 +0000296 Align = TM.getTargetData()->getPrefTypeAlignment(Type);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000297 if (Align == 0) {
298 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +0000299 Align = TM.getTargetData()->getTypeAllocSize(Type);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000300 }
301 }
302
303 unsigned Idx;
304 if (CP->isMachineConstantPoolEntry())
305 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
306 else
307 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
308 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
Bill Wendling056292f2008-09-16 21:48:12 +0000309 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000310 MI->addOperand(MachineOperand::CreateES(ES->getSymbol()));
311 } else {
312 assert(Op.getValueType() != MVT::Other &&
313 Op.getValueType() != MVT::Flag &&
314 "Chain and flag operands should occur at end of operand list!");
Dan Gohmanf8c73942009-04-13 15:38:05 +0000315 AddRegisterOperand(MI, Op, IIOpNum, II, VRBaseMap);
316 }
317}
318
Dan Gohmanf8c73942009-04-13 15:38:05 +0000319/// getSuperRegisterRegClass - Returns the register class of a superreg A whose
320/// "SubIdx"'th sub-register class is the specified register class and whose
321/// type matches the specified type.
322static const TargetRegisterClass*
323getSuperRegisterRegClass(const TargetRegisterClass *TRC,
324 unsigned SubIdx, MVT VT) {
325 // Pick the register class of the superegister for this type
326 for (TargetRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
327 E = TRC->superregclasses_end(); I != E; ++I)
Jakob Stoklund Olesenfa4677b2009-04-28 16:34:09 +0000328 if ((*I)->hasType(VT) && (*I)->getSubRegisterRegClass(SubIdx) == TRC)
Dan Gohmanf8c73942009-04-13 15:38:05 +0000329 return *I;
330 assert(false && "Couldn't find the register class");
331 return 0;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000332}
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,
Chris Lattner52023122009-06-26 05:39:02 +0000337 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.
Dan Gohmanf8c73942009-04-13 15:38:05 +0000364 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
365 const TargetRegisterClass *TRC = MRI.getRegClass(VReg);
Jakob Stoklund Olesenfa4677b2009-04-28 16:34:09 +0000366 const TargetRegisterClass *SRC = TRC->getSubRegisterRegClass(SubIdx);
367 assert(SRC && "Invalid subregister index in EXTRACT_SUBREG");
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000368
Dan Gohman5ec3b422009-04-14 22:17:14 +0000369 // Figure out the register class to create for the destreg.
370 // Note that if we're going to directly use an existing register,
371 // it must be precisely the required class, and not a subclass
372 // thereof.
373 if (VRBase == 0 || SRC != MRI.getRegClass(VRBase)) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000374 // Create the reg
375 assert(SRC && "Couldn't find source register class");
376 VRBase = MRI.createVirtualRegister(SRC);
377 }
Dan Gohman5ec3b422009-04-14 22:17:14 +0000378
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000379 // 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 Gohman47ac0f02009-02-11 04:27:20 +0000383 BB->insert(InsertPos, 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 Gohmanf8c73942009-04-13 15:38:05 +0000389 unsigned SubReg = getVR(N1, VRBaseMap);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000390 unsigned SubIdx = cast<ConstantSDNode>(N2)->getZExtValue();
Dan Gohman5ec3b422009-04-14 22:17:14 +0000391 const TargetRegisterClass *TRC = MRI.getRegClass(SubReg);
392 const TargetRegisterClass *SRC =
393 getSuperRegisterRegClass(TRC, SubIdx,
394 Node->getValueType(0));
395
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000396 // Figure out the register class to create for the destreg.
Dan Gohman5ec3b422009-04-14 22:17:14 +0000397 // Note that if we're going to directly use an existing register,
398 // it must be precisely the required class, and not a subclass
399 // thereof.
400 if (VRBase == 0 || SRC != MRI.getRegClass(VRBase)) {
401 // Create the reg
402 assert(SRC && "Couldn't find source register class");
403 VRBase = MRI.createVirtualRegister(SRC);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000404 }
Dan Gohman5ec3b422009-04-14 22:17:14 +0000405
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000406 // Create the insert_subreg or subreg_to_reg machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000407 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(), TII->get(Opc));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000408 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
409
410 // If creating a subreg_to_reg, then the first input operand
411 // is an implicit value immediate, otherwise it's a register
412 if (Opc == TargetInstrInfo::SUBREG_TO_REG) {
413 const ConstantSDNode *SD = cast<ConstantSDNode>(N0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000414 MI->addOperand(MachineOperand::CreateImm(SD->getZExtValue()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000415 } else
416 AddOperand(MI, N0, 0, 0, VRBaseMap);
417 // Add the subregster being inserted
418 AddOperand(MI, N1, 0, 0, VRBaseMap);
419 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Dan Gohman47ac0f02009-02-11 04:27:20 +0000420 BB->insert(InsertPos, MI);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000421 } else
422 assert(0 && "Node is not insert_subreg, extract_subreg, or subreg_to_reg");
423
424 SDValue Op(Node, 0);
425 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
426 isNew = isNew; // Silence compiler warning.
427 assert(isNew && "Node emitted out of order - early");
428}
429
Dan Gohman88c7af02009-04-13 21:06:25 +0000430/// EmitCopyToRegClassNode - Generate machine code for COPY_TO_REGCLASS nodes.
431/// COPY_TO_REGCLASS is just a normal copy, except that the destination
Dan Gohmanf8c73942009-04-13 15:38:05 +0000432/// register is constrained to be in a particular register class.
433///
434void
Dan Gohman88c7af02009-04-13 21:06:25 +0000435ScheduleDAGSDNodes::EmitCopyToRegClassNode(SDNode *Node,
Dan Gohmanf8c73942009-04-13 15:38:05 +0000436 DenseMap<SDValue, unsigned> &VRBaseMap) {
437 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
438 const TargetRegisterClass *SrcRC = MRI.getRegClass(VReg);
439
440 unsigned DstRCIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
441 const TargetRegisterClass *DstRC = TRI->getRegClass(DstRCIdx);
442
Dan Gohmanf8c73942009-04-13 15:38:05 +0000443 // Create the new VReg in the destination class and emit a copy.
444 unsigned NewVReg = MRI.createVirtualRegister(DstRC);
445 bool Emitted = TII->copyRegToReg(*BB, InsertPos, NewVReg, VReg,
446 DstRC, SrcRC);
Dan Gohmanf8c73942009-04-13 15:38:05 +0000447 assert(Emitted &&
Dan Gohman88c7af02009-04-13 21:06:25 +0000448 "Unable to issue a copy instruction for a COPY_TO_REGCLASS node!\n");
Daniel Dunbar8c562e22009-05-18 16:43:04 +0000449 (void) Emitted;
Dan Gohmanf8c73942009-04-13 15:38:05 +0000450
451 SDValue Op(Node, 0);
452 bool isNew = VRBaseMap.insert(std::make_pair(Op, NewVReg)).second;
453 isNew = isNew; // Silence compiler warning.
454 assert(isNew && "Node emitted out of order - early");
455}
456
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000457/// EmitNode - Generate machine code for an node and needed dependencies.
458///
Evan Chenge57187c2009-01-16 20:57:18 +0000459void ScheduleDAGSDNodes::EmitNode(SDNode *Node, bool IsClone, bool IsCloned,
Dan Gohman343f0c02008-11-19 23:18:57 +0000460 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000461 // If machine instruction
462 if (Node->isMachineOpcode()) {
463 unsigned Opc = Node->getMachineOpcode();
464
465 // Handle subreg insert/extract specially
466 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
467 Opc == TargetInstrInfo::INSERT_SUBREG ||
468 Opc == TargetInstrInfo::SUBREG_TO_REG) {
469 EmitSubregNode(Node, VRBaseMap);
470 return;
471 }
472
Dan Gohman88c7af02009-04-13 21:06:25 +0000473 // Handle COPY_TO_REGCLASS specially.
474 if (Opc == TargetInstrInfo::COPY_TO_REGCLASS) {
475 EmitCopyToRegClassNode(Node, VRBaseMap);
Dan Gohmanf8c73942009-04-13 15:38:05 +0000476 return;
477 }
478
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000479 if (Opc == TargetInstrInfo::IMPLICIT_DEF)
480 // We want a unique VR for each IMPLICIT_DEF use.
481 return;
482
483 const TargetInstrDesc &II = TII->get(Opc);
484 unsigned NumResults = CountResults(Node);
485 unsigned NodeOperands = CountOperands(Node);
486 unsigned MemOperandsEnd = ComputeMemOperandsEnd(Node);
487 bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
488 II.getImplicitDefs() != 0;
489#ifndef NDEBUG
490 unsigned NumMIOperands = NodeOperands + NumResults;
491 assert((II.getNumOperands() == NumMIOperands ||
492 HasPhysRegOuts || II.isVariadic()) &&
493 "#operands for dag node doesn't match .td file!");
494#endif
495
496 // Create the new machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000497 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(), II);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000498
499 // Add result register values for things that are defined by this
500 // instruction.
501 if (NumResults)
Evan Chenge57187c2009-01-16 20:57:18 +0000502 CreateVirtualRegisters(Node, MI, II, IsClone, IsCloned, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000503
504 // Emit all of the actual operands of this instruction, adding them to the
505 // instruction as appropriate.
506 for (unsigned i = 0; i != NodeOperands; ++i)
507 AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap);
508
509 // Emit all of the memory operands of this instruction
510 for (unsigned i = NodeOperands; i != MemOperandsEnd; ++i)
511 AddMemOperand(MI, cast<MemOperandSDNode>(Node->getOperand(i))->MO);
512
Dan Gohmanf7119392009-01-16 22:10:20 +0000513 if (II.usesCustomDAGSchedInsertionHook()) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000514 // Insert this instruction into the basic block using a target
515 // specific inserter which may returns a new basic block.
516 BB = TLI->EmitInstrWithCustomInserter(MI, BB);
Dan Gohman47ac0f02009-02-11 04:27:20 +0000517 InsertPos = BB->end();
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000518 } else {
Dan Gohman47ac0f02009-02-11 04:27:20 +0000519 BB->insert(InsertPos, MI);
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000520 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000521
522 // Additional results must be an physical register def.
523 if (HasPhysRegOuts) {
524 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
525 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
526 if (Node->hasAnyUseOfValue(i))
Evan Chenge57187c2009-01-16 20:57:18 +0000527 EmitCopyFromReg(Node, i, IsClone, IsCloned, Reg, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000528 }
529 }
530 return;
531 }
532
533 switch (Node->getOpcode()) {
534 default:
535#ifndef NDEBUG
Dan Gohmana23b3b82008-11-13 21:21:28 +0000536 Node->dump(DAG);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000537#endif
538 assert(0 && "This target-independent node should have been selected!");
539 break;
540 case ISD::EntryToken:
541 assert(0 && "EntryToken should have been excluded from the schedule!");
542 break;
543 case ISD::TokenFactor: // fall thru
544 break;
545 case ISD::CopyToReg: {
546 unsigned SrcReg;
547 SDValue SrcVal = Node->getOperand(2);
548 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
549 SrcReg = R->getReg();
550 else
551 SrcReg = getVR(SrcVal, VRBaseMap);
552
553 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
554 if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
555 break;
556
557 const TargetRegisterClass *SrcTRC = 0, *DstTRC = 0;
558 // Get the register classes of the src/dst.
559 if (TargetRegisterInfo::isVirtualRegister(SrcReg))
560 SrcTRC = MRI.getRegClass(SrcReg);
561 else
562 SrcTRC = TRI->getPhysicalRegisterRegClass(SrcReg,SrcVal.getValueType());
563
564 if (TargetRegisterInfo::isVirtualRegister(DestReg))
565 DstTRC = MRI.getRegClass(DestReg);
566 else
567 DstTRC = TRI->getPhysicalRegisterRegClass(DestReg,
568 Node->getOperand(1).getValueType());
Dan Gohmanf8c73942009-04-13 15:38:05 +0000569
Dan Gohman47ac0f02009-02-11 04:27:20 +0000570 bool Emitted = TII->copyRegToReg(*BB, InsertPos, DestReg, SrcReg,
571 DstTRC, SrcTRC);
Dan Gohmanf8c73942009-04-13 15:38:05 +0000572 assert(Emitted && "Unable to issue a copy instruction!\n");
Daniel Dunbar8c562e22009-05-18 16:43:04 +0000573 (void) Emitted;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000574 break;
575 }
576 case ISD::CopyFromReg: {
577 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Evan Chenge57187c2009-01-16 20:57:18 +0000578 EmitCopyFromReg(Node, 0, IsClone, IsCloned, SrcReg, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000579 break;
580 }
581 case ISD::INLINEASM: {
582 unsigned NumOps = Node->getNumOperands();
583 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
584 --NumOps; // Ignore the flag operand.
585
586 // Create the inline asm machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000587 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(),
588 TII->get(TargetInstrInfo::INLINEASM));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000589
590 // Add the asm string as an external symbol operand.
Bill Wendling056292f2008-09-16 21:48:12 +0000591 const char *AsmStr =
592 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000593 MI->addOperand(MachineOperand::CreateES(AsmStr));
594
595 // Add all of the operand registers to the instruction.
596 for (unsigned i = 2; i != NumOps;) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000597 unsigned Flags =
598 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +0000599 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000600
601 MI->addOperand(MachineOperand::CreateImm(Flags));
602 ++i; // Skip the ID value.
603
604 switch (Flags & 7) {
605 default: assert(0 && "Bad flags!");
606 case 2: // Def of register.
607 for (; NumVals; --NumVals, ++i) {
608 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
609 MI->addOperand(MachineOperand::CreateReg(Reg, true));
610 }
611 break;
Dale Johannesen913d3df2008-09-12 17:49:03 +0000612 case 6: // Def of earlyclobber register.
613 for (; NumVals; --NumVals, ++i) {
614 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
615 MI->addOperand(MachineOperand::CreateReg(Reg, true, false, false,
616 false, 0, true));
617 }
618 break;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000619 case 1: // Use of register.
620 case 3: // Immediate.
621 case 4: // Addressing mode.
622 // The addressing mode has been selected, just add all of the
623 // operands to the machine instruction.
624 for (; NumVals; --NumVals, ++i)
Dale Johannesen86b49f82008-09-24 01:07:17 +0000625 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000626 break;
627 }
628 }
Dan Gohman47ac0f02009-02-11 04:27:20 +0000629 BB->insert(InsertPos, MI);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000630 break;
631 }
632 }
633}
634
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000635/// EmitSchedule - Emit the machine code in scheduled order.
Dan Gohman343f0c02008-11-19 23:18:57 +0000636MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000637 DenseMap<SDValue, unsigned> VRBaseMap;
638 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
639 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
640 SUnit *SU = Sequence[i];
641 if (!SU) {
642 // Null SUnit* is a noop.
643 EmitNoop();
644 continue;
645 }
Dan Gohmanf449bf32008-11-14 00:06:09 +0000646
Dan Gohmanf449bf32008-11-14 00:06:09 +0000647 // For pre-regalloc scheduling, create instructions corresponding to the
648 // SDNode and any flagged SDNodes and append them to the block.
Evan Chengc29a56d2009-01-12 03:19:55 +0000649 if (!SU->getNode()) {
650 // Emit a copy.
651 EmitPhysRegCopy(SU, CopyVRBaseMap);
652 continue;
653 }
654
Dan Gohmand23e0f82008-11-13 23:24:17 +0000655 SmallVector<SDNode *, 4> FlaggedNodes;
Evan Chenge57187c2009-01-16 20:57:18 +0000656 for (SDNode *N = SU->getNode()->getFlaggedNode(); N;
657 N = N->getFlaggedNode())
Dan Gohmand23e0f82008-11-13 23:24:17 +0000658 FlaggedNodes.push_back(N);
659 while (!FlaggedNodes.empty()) {
Evan Chenge57187c2009-01-16 20:57:18 +0000660 EmitNode(FlaggedNodes.back(), SU->OrigNode != SU, SU->isCloned,VRBaseMap);
Dan Gohmand23e0f82008-11-13 23:24:17 +0000661 FlaggedNodes.pop_back();
662 }
Evan Chenge57187c2009-01-16 20:57:18 +0000663 EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000664 }
665
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000666 return BB;
667}