blob: e352f88afc4f50d3b7321da817baac203ef8278e [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"
Torok Edwinc25e7582009-07-11 20:10:48 +000028#include "llvm/Support/ErrorHandling.h"
Dan Gohman94b8d7e2008-09-03 16:01:59 +000029#include "llvm/Support/MathExtras.h"
30using namespace llvm;
31
Dan Gohman94b8d7e2008-09-03 16:01:59 +000032/// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an
33/// implicit physical register output.
Chris Lattner52023122009-06-26 05:39:02 +000034void ScheduleDAGSDNodes::
35EmitCopyFromReg(SDNode *Node, unsigned ResNo, bool IsClone, bool IsCloned,
36 unsigned SrcReg, DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +000037 unsigned VRBase = 0;
38 if (TargetRegisterInfo::isVirtualRegister(SrcReg)) {
39 // Just use the input register directly!
40 SDValue Op(Node, ResNo);
41 if (IsClone)
42 VRBaseMap.erase(Op);
43 bool isNew = VRBaseMap.insert(std::make_pair(Op, SrcReg)).second;
44 isNew = isNew; // Silence compiler warning.
45 assert(isNew && "Node emitted out of order - early");
46 return;
47 }
48
49 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
50 // the CopyToReg'd destination register instead of creating a new vreg.
51 bool MatchReg = true;
Evan Cheng1cd33272008-09-16 23:12:11 +000052 const TargetRegisterClass *UseRC = NULL;
Evan Chenge57187c2009-01-16 20:57:18 +000053 if (!IsClone && !IsCloned)
54 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
55 UI != E; ++UI) {
56 SDNode *User = *UI;
57 bool Match = true;
58 if (User->getOpcode() == ISD::CopyToReg &&
59 User->getOperand(2).getNode() == Node &&
60 User->getOperand(2).getResNo() == ResNo) {
61 unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
62 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
63 VRBase = DestReg;
64 Match = false;
65 } else if (DestReg != SrcReg)
66 Match = false;
67 } else {
68 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
69 SDValue Op = User->getOperand(i);
70 if (Op.getNode() != Node || Op.getResNo() != ResNo)
71 continue;
72 MVT VT = Node->getValueType(Op.getResNo());
73 if (VT == MVT::Other || VT == MVT::Flag)
74 continue;
75 Match = false;
76 if (User->isMachineOpcode()) {
77 const TargetInstrDesc &II = TII->get(User->getMachineOpcode());
Chris Lattner2a386882009-07-29 21:36:49 +000078 const TargetRegisterClass *RC = 0;
79 if (i+II.getNumDefs() < II.getNumOperands())
80 RC = II.OpInfo[i+II.getNumDefs()].getRegClass(TRI);
Evan Chenge57187c2009-01-16 20:57:18 +000081 if (!UseRC)
82 UseRC = RC;
Dan Gohmanf8c73942009-04-13 15:38:05 +000083 else if (RC) {
84 if (UseRC->hasSuperClass(RC))
85 UseRC = RC;
86 else
87 assert((UseRC == RC || RC->hasSuperClass(UseRC)) &&
88 "Multiple uses expecting different register classes!");
89 }
Evan Chenge57187c2009-01-16 20:57:18 +000090 }
Evan Cheng1cd33272008-09-16 23:12:11 +000091 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +000092 }
Evan Chenge57187c2009-01-16 20:57:18 +000093 MatchReg &= Match;
94 if (VRBase)
95 break;
Dan Gohman94b8d7e2008-09-03 16:01:59 +000096 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +000097
Evan Cheng1cd33272008-09-16 23:12:11 +000098 MVT VT = Node->getValueType(ResNo);
Dan Gohman94b8d7e2008-09-03 16:01:59 +000099 const TargetRegisterClass *SrcRC = 0, *DstRC = 0;
Evan Cheng1cd33272008-09-16 23:12:11 +0000100 SrcRC = TRI->getPhysicalRegisterRegClass(SrcReg, VT);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000101
102 // Figure out the register class to create for the destreg.
103 if (VRBase) {
104 DstRC = MRI.getRegClass(VRBase);
Evan Cheng1cd33272008-09-16 23:12:11 +0000105 } else if (UseRC) {
106 assert(UseRC->hasType(VT) && "Incompatible phys register def and uses!");
107 DstRC = UseRC;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000108 } else {
Evan Cheng1cd33272008-09-16 23:12:11 +0000109 DstRC = TLI->getRegClassFor(VT);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000110 }
111
112 // If all uses are reading from the src physical register and copying the
113 // register is either impossible or very expensive, then don't create a copy.
114 if (MatchReg && SrcRC->getCopyCost() < 0) {
115 VRBase = SrcReg;
116 } else {
117 // Create the reg, emit the copy.
118 VRBase = MRI.createVirtualRegister(DstRC);
Dan Gohman47ac0f02009-02-11 04:27:20 +0000119 bool Emitted = TII->copyRegToReg(*BB, InsertPos, VRBase, SrcReg,
120 DstRC, SrcRC);
Dan Gohmanf8c73942009-04-13 15:38:05 +0000121
122 assert(Emitted && "Unable to issue a copy instruction!\n");
Daniel Dunbar8c562e22009-05-18 16:43:04 +0000123 (void) Emitted;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000124 }
125
126 SDValue Op(Node, ResNo);
127 if (IsClone)
128 VRBaseMap.erase(Op);
129 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
130 isNew = isNew; // Silence compiler warning.
131 assert(isNew && "Node emitted out of order - early");
132}
133
134/// getDstOfCopyToRegUse - If the only use of the specified result number of
135/// node is a CopyToReg, return its destination register. Return 0 otherwise.
Dan Gohman343f0c02008-11-19 23:18:57 +0000136unsigned ScheduleDAGSDNodes::getDstOfOnlyCopyToRegUse(SDNode *Node,
137 unsigned ResNo) const {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000138 if (!Node->hasOneUse())
139 return 0;
140
141 SDNode *User = *Node->use_begin();
142 if (User->getOpcode() == ISD::CopyToReg &&
143 User->getOperand(2).getNode() == Node &&
144 User->getOperand(2).getResNo() == ResNo) {
145 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
146 if (TargetRegisterInfo::isVirtualRegister(Reg))
147 return Reg;
148 }
149 return 0;
150}
151
Dan Gohman343f0c02008-11-19 23:18:57 +0000152void ScheduleDAGSDNodes::CreateVirtualRegisters(SDNode *Node, MachineInstr *MI,
Evan Chenge57187c2009-01-16 20:57:18 +0000153 const TargetInstrDesc &II,
154 bool IsClone, bool IsCloned,
Evan Cheng5c3c5a42009-01-09 22:44:02 +0000155 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000156 assert(Node->getMachineOpcode() != TargetInstrInfo::IMPLICIT_DEF &&
157 "IMPLICIT_DEF should have been handled as a special case elsewhere!");
158
159 for (unsigned i = 0; i < II.getNumDefs(); ++i) {
160 // If the specific node value is only used by a CopyToReg and the dest reg
Dan Gohmanf8c73942009-04-13 15:38:05 +0000161 // is a vreg in the same register class, use the CopyToReg'd destination
162 // register instead of creating a new vreg.
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000163 unsigned VRBase = 0;
Chris Lattner2a386882009-07-29 21:36:49 +0000164 const TargetRegisterClass *RC = II.OpInfo[i].getRegClass(TRI);
Evan Cheng8955e932009-07-11 01:06:50 +0000165 if (II.OpInfo[i].isOptionalDef()) {
166 // Optional def must be a physical register.
167 unsigned NumResults = CountResults(Node);
168 VRBase = cast<RegisterSDNode>(Node->getOperand(i-NumResults))->getReg();
169 assert(TargetRegisterInfo::isPhysicalRegister(VRBase));
170 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
171 }
Evan Chenge57187c2009-01-16 20:57:18 +0000172
Evan Cheng8955e932009-07-11 01:06:50 +0000173 if (!VRBase && !IsClone && !IsCloned)
Evan Chenge57187c2009-01-16 20:57:18 +0000174 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)) {
Dan Gohmanf8c73942009-04-13 15:38:05 +0000182 const TargetRegisterClass *RegRC = MRI.getRegClass(Reg);
183 if (RegRC == RC) {
184 VRBase = Reg;
185 MI->addOperand(MachineOperand::CreateReg(Reg, true));
186 break;
187 }
Evan Chenge57187c2009-01-16 20:57:18 +0000188 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000189 }
190 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000191
192 // Create the result registers for this node and add the result regs to
193 // the machine instruction.
194 if (VRBase == 0) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000195 assert(RC && "Isn't a register operand!");
196 VRBase = MRI.createVirtualRegister(RC);
197 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
198 }
199
200 SDValue Op(Node, i);
Evan Cheng5c3c5a42009-01-09 22:44:02 +0000201 if (IsClone)
202 VRBaseMap.erase(Op);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000203 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
204 isNew = isNew; // Silence compiler warning.
205 assert(isNew && "Node emitted out of order - early");
206 }
207}
208
209/// getVR - Return the virtual register corresponding to the specified result
210/// of the specified node.
Dan Gohman343f0c02008-11-19 23:18:57 +0000211unsigned ScheduleDAGSDNodes::getVR(SDValue Op,
212 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000213 if (Op.isMachineOpcode() &&
214 Op.getMachineOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
215 // Add an IMPLICIT_DEF instruction before every use.
216 unsigned VReg = getDstOfOnlyCopyToRegUse(Op.getNode(), Op.getResNo());
217 // IMPLICIT_DEF can produce any type of result so its TargetInstrDesc
218 // does not include operand register class info.
219 if (!VReg) {
220 const TargetRegisterClass *RC = TLI->getRegClassFor(Op.getValueType());
221 VReg = MRI.createVirtualRegister(RC);
222 }
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000223 BuildMI(BB, Op.getDebugLoc(), TII->get(TargetInstrInfo::IMPLICIT_DEF),VReg);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000224 return VReg;
225 }
226
227 DenseMap<SDValue, unsigned>::iterator I = VRBaseMap.find(Op);
228 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
229 return I->second;
230}
231
232
Dan Gohmanf8c73942009-04-13 15:38:05 +0000233/// AddRegisterOperand - Add the specified register as an operand to the
234/// specified machine instr. Insert register copies if the register is
235/// not in the required register class.
236void
237ScheduleDAGSDNodes::AddRegisterOperand(MachineInstr *MI, SDValue Op,
238 unsigned IIOpNum,
239 const TargetInstrDesc *II,
240 DenseMap<SDValue, unsigned> &VRBaseMap) {
241 assert(Op.getValueType() != MVT::Other &&
242 Op.getValueType() != MVT::Flag &&
243 "Chain and flag operands should occur at end of operand list!");
244 // Get/emit the operand.
245 unsigned VReg = getVR(Op, VRBaseMap);
246 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
247
248 const TargetInstrDesc &TID = MI->getDesc();
249 bool isOptDef = IIOpNum < TID.getNumOperands() &&
250 TID.OpInfo[IIOpNum].isOptionalDef();
251
252 // If the instruction requires a register in a different class, create
253 // a new virtual register and copy the value into it.
254 if (II) {
Chris Lattner2a386882009-07-29 21:36:49 +0000255 const TargetRegisterClass *SrcRC = MRI.getRegClass(VReg);
256 const TargetRegisterClass *DstRC = 0;
257 if (IIOpNum < II->getNumOperands())
258 DstRC = II->OpInfo[IIOpNum].getRegClass(TRI);
Dan Gohmanf8c73942009-04-13 15:38:05 +0000259 assert((DstRC || (TID.isVariadic() && IIOpNum >= TID.getNumOperands())) &&
260 "Don't have operand info for this instruction!");
261 if (DstRC && SrcRC != DstRC && !SrcRC->hasSuperClass(DstRC)) {
262 unsigned NewVReg = MRI.createVirtualRegister(DstRC);
263 bool Emitted = TII->copyRegToReg(*BB, InsertPos, NewVReg, VReg,
264 DstRC, SrcRC);
Dan Gohmanf8c73942009-04-13 15:38:05 +0000265 assert(Emitted && "Unable to issue a copy instruction!\n");
Daniel Dunbar8c562e22009-05-18 16:43:04 +0000266 (void) Emitted;
Dan Gohmanf8c73942009-04-13 15:38:05 +0000267 VReg = NewVReg;
268 }
269 }
270
271 MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
272}
273
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000274/// AddOperand - Add the specified operand to the specified machine instr. II
275/// specifies the instruction information for the node, and IIOpNum is the
276/// operand number (in the II) that we are adding. IIOpNum and II are used for
277/// assertions only.
Dan Gohman343f0c02008-11-19 23:18:57 +0000278void ScheduleDAGSDNodes::AddOperand(MachineInstr *MI, SDValue Op,
279 unsigned IIOpNum,
280 const TargetInstrDesc *II,
281 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000282 if (Op.isMachineOpcode()) {
Dan Gohmanf8c73942009-04-13 15:38:05 +0000283 AddRegisterOperand(MI, Op, IIOpNum, II, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000284 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000285 MI->addOperand(MachineOperand::CreateImm(C->getZExtValue()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000286 } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000287 const ConstantFP *CFP = F->getConstantFPValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000288 MI->addOperand(MachineOperand::CreateFPImm(CFP));
289 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
Dale Johannesen86b49f82008-09-24 01:07:17 +0000290 MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000291 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
Chris Lattner6ec66db2009-06-26 05:52:14 +0000292 MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(), TGA->getOffset(),
293 TGA->getTargetFlags()));
Dan Gohmanf8c73942009-04-13 15:38:05 +0000294 } else if (BasicBlockSDNode *BBNode = dyn_cast<BasicBlockSDNode>(Op)) {
295 MI->addOperand(MachineOperand::CreateMBB(BBNode->getBasicBlock()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000296 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
297 MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
298 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
Chris Lattner6ec66db2009-06-26 05:52:14 +0000299 MI->addOperand(MachineOperand::CreateJTI(JT->getIndex(),
300 JT->getTargetFlags()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000301 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
302 int Offset = CP->getOffset();
303 unsigned Align = CP->getAlignment();
304 const Type *Type = CP->getType();
305 // MachineConstantPool wants an explicit alignment.
306 if (Align == 0) {
Evan Cheng1606e8e2009-03-13 07:51:59 +0000307 Align = TM.getTargetData()->getPrefTypeAlignment(Type);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000308 if (Align == 0) {
309 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +0000310 Align = TM.getTargetData()->getTypeAllocSize(Type);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000311 }
312 }
313
314 unsigned Idx;
315 if (CP->isMachineConstantPoolEntry())
316 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
317 else
318 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
Chris Lattner6ec66db2009-06-26 05:52:14 +0000319 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset,
320 CP->getTargetFlags()));
Bill Wendling056292f2008-09-16 21:48:12 +0000321 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
Chris Lattner6ec66db2009-06-26 05:52:14 +0000322 MI->addOperand(MachineOperand::CreateES(ES->getSymbol(), 0,
323 ES->getTargetFlags()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000324 } else {
325 assert(Op.getValueType() != MVT::Other &&
326 Op.getValueType() != MVT::Flag &&
327 "Chain and flag operands should occur at end of operand list!");
Dan Gohmanf8c73942009-04-13 15:38:05 +0000328 AddRegisterOperand(MI, Op, IIOpNum, II, VRBaseMap);
329 }
330}
331
Dan Gohmanf8c73942009-04-13 15:38:05 +0000332/// getSuperRegisterRegClass - Returns the register class of a superreg A whose
333/// "SubIdx"'th sub-register class is the specified register class and whose
334/// type matches the specified type.
335static const TargetRegisterClass*
336getSuperRegisterRegClass(const TargetRegisterClass *TRC,
337 unsigned SubIdx, MVT VT) {
338 // Pick the register class of the superegister for this type
339 for (TargetRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
340 E = TRC->superregclasses_end(); I != E; ++I)
Jakob Stoklund Olesenfa4677b2009-04-28 16:34:09 +0000341 if ((*I)->hasType(VT) && (*I)->getSubRegisterRegClass(SubIdx) == TRC)
Dan Gohmanf8c73942009-04-13 15:38:05 +0000342 return *I;
343 assert(false && "Couldn't find the register class");
344 return 0;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000345}
346
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000347/// EmitSubregNode - Generate machine code for subreg nodes.
348///
Dan Gohman343f0c02008-11-19 23:18:57 +0000349void ScheduleDAGSDNodes::EmitSubregNode(SDNode *Node,
Chris Lattner52023122009-06-26 05:39:02 +0000350 DenseMap<SDValue, unsigned> &VRBaseMap){
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000351 unsigned VRBase = 0;
352 unsigned Opc = Node->getMachineOpcode();
353
354 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
355 // the CopyToReg'd destination register instead of creating a new vreg.
356 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
357 UI != E; ++UI) {
358 SDNode *User = *UI;
359 if (User->getOpcode() == ISD::CopyToReg &&
360 User->getOperand(2).getNode() == Node) {
361 unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
362 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
363 VRBase = DestReg;
364 break;
365 }
366 }
367 }
368
369 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000370 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000371
372 // Create the extract_subreg machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000373 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(),
374 TII->get(TargetInstrInfo::EXTRACT_SUBREG));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000375
376 // Figure out the register class to create for the destreg.
Dan Gohmanf8c73942009-04-13 15:38:05 +0000377 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
378 const TargetRegisterClass *TRC = MRI.getRegClass(VReg);
Jakob Stoklund Olesenfa4677b2009-04-28 16:34:09 +0000379 const TargetRegisterClass *SRC = TRC->getSubRegisterRegClass(SubIdx);
380 assert(SRC && "Invalid subregister index in EXTRACT_SUBREG");
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000381
Dan Gohman5ec3b422009-04-14 22:17:14 +0000382 // Figure out the register class to create for the destreg.
383 // Note that if we're going to directly use an existing register,
384 // it must be precisely the required class, and not a subclass
385 // thereof.
386 if (VRBase == 0 || SRC != MRI.getRegClass(VRBase)) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000387 // Create the reg
388 assert(SRC && "Couldn't find source register class");
389 VRBase = MRI.createVirtualRegister(SRC);
390 }
Dan Gohman5ec3b422009-04-14 22:17:14 +0000391
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000392 // Add def, source, and subreg index
393 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
394 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
395 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Dan Gohman47ac0f02009-02-11 04:27:20 +0000396 BB->insert(InsertPos, MI);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000397 } else if (Opc == TargetInstrInfo::INSERT_SUBREG ||
398 Opc == TargetInstrInfo::SUBREG_TO_REG) {
399 SDValue N0 = Node->getOperand(0);
400 SDValue N1 = Node->getOperand(1);
401 SDValue N2 = Node->getOperand(2);
Dan Gohmanf8c73942009-04-13 15:38:05 +0000402 unsigned SubReg = getVR(N1, VRBaseMap);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000403 unsigned SubIdx = cast<ConstantSDNode>(N2)->getZExtValue();
Dan Gohman5ec3b422009-04-14 22:17:14 +0000404 const TargetRegisterClass *TRC = MRI.getRegClass(SubReg);
405 const TargetRegisterClass *SRC =
406 getSuperRegisterRegClass(TRC, SubIdx,
407 Node->getValueType(0));
408
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000409 // Figure out the register class to create for the destreg.
Dan Gohman5ec3b422009-04-14 22:17:14 +0000410 // Note that if we're going to directly use an existing register,
411 // it must be precisely the required class, and not a subclass
412 // thereof.
413 if (VRBase == 0 || SRC != MRI.getRegClass(VRBase)) {
414 // Create the reg
415 assert(SRC && "Couldn't find source register class");
416 VRBase = MRI.createVirtualRegister(SRC);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000417 }
Dan Gohman5ec3b422009-04-14 22:17:14 +0000418
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000419 // Create the insert_subreg or subreg_to_reg machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000420 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(), TII->get(Opc));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000421 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
422
423 // If creating a subreg_to_reg, then the first input operand
424 // is an implicit value immediate, otherwise it's a register
425 if (Opc == TargetInstrInfo::SUBREG_TO_REG) {
426 const ConstantSDNode *SD = cast<ConstantSDNode>(N0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000427 MI->addOperand(MachineOperand::CreateImm(SD->getZExtValue()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000428 } else
429 AddOperand(MI, N0, 0, 0, VRBaseMap);
430 // Add the subregster being inserted
431 AddOperand(MI, N1, 0, 0, VRBaseMap);
432 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Dan Gohman47ac0f02009-02-11 04:27:20 +0000433 BB->insert(InsertPos, MI);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000434 } else
Torok Edwinc23197a2009-07-14 16:55:14 +0000435 llvm_unreachable("Node is not insert_subreg, extract_subreg, or subreg_to_reg");
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000436
437 SDValue Op(Node, 0);
438 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
439 isNew = isNew; // Silence compiler warning.
440 assert(isNew && "Node emitted out of order - early");
441}
442
Dan Gohman88c7af02009-04-13 21:06:25 +0000443/// EmitCopyToRegClassNode - Generate machine code for COPY_TO_REGCLASS nodes.
444/// COPY_TO_REGCLASS is just a normal copy, except that the destination
Dan Gohmanf8c73942009-04-13 15:38:05 +0000445/// register is constrained to be in a particular register class.
446///
447void
Dan Gohman88c7af02009-04-13 21:06:25 +0000448ScheduleDAGSDNodes::EmitCopyToRegClassNode(SDNode *Node,
Dan Gohmanf8c73942009-04-13 15:38:05 +0000449 DenseMap<SDValue, unsigned> &VRBaseMap) {
450 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
451 const TargetRegisterClass *SrcRC = MRI.getRegClass(VReg);
452
453 unsigned DstRCIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
454 const TargetRegisterClass *DstRC = TRI->getRegClass(DstRCIdx);
455
Dan Gohmanf8c73942009-04-13 15:38:05 +0000456 // Create the new VReg in the destination class and emit a copy.
457 unsigned NewVReg = MRI.createVirtualRegister(DstRC);
458 bool Emitted = TII->copyRegToReg(*BB, InsertPos, NewVReg, VReg,
459 DstRC, SrcRC);
Dan Gohmanf8c73942009-04-13 15:38:05 +0000460 assert(Emitted &&
Dan Gohman88c7af02009-04-13 21:06:25 +0000461 "Unable to issue a copy instruction for a COPY_TO_REGCLASS node!\n");
Daniel Dunbar8c562e22009-05-18 16:43:04 +0000462 (void) Emitted;
Dan Gohmanf8c73942009-04-13 15:38:05 +0000463
464 SDValue Op(Node, 0);
465 bool isNew = VRBaseMap.insert(std::make_pair(Op, NewVReg)).second;
466 isNew = isNew; // Silence compiler warning.
467 assert(isNew && "Node emitted out of order - early");
468}
469
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000470/// EmitNode - Generate machine code for an node and needed dependencies.
471///
Evan Chenge57187c2009-01-16 20:57:18 +0000472void ScheduleDAGSDNodes::EmitNode(SDNode *Node, bool IsClone, bool IsCloned,
Dan Gohman343f0c02008-11-19 23:18:57 +0000473 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000474 // If machine instruction
475 if (Node->isMachineOpcode()) {
476 unsigned Opc = Node->getMachineOpcode();
477
478 // Handle subreg insert/extract specially
479 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
480 Opc == TargetInstrInfo::INSERT_SUBREG ||
481 Opc == TargetInstrInfo::SUBREG_TO_REG) {
482 EmitSubregNode(Node, VRBaseMap);
483 return;
484 }
485
Dan Gohman88c7af02009-04-13 21:06:25 +0000486 // Handle COPY_TO_REGCLASS specially.
487 if (Opc == TargetInstrInfo::COPY_TO_REGCLASS) {
488 EmitCopyToRegClassNode(Node, VRBaseMap);
Dan Gohmanf8c73942009-04-13 15:38:05 +0000489 return;
490 }
491
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000492 if (Opc == TargetInstrInfo::IMPLICIT_DEF)
493 // We want a unique VR for each IMPLICIT_DEF use.
494 return;
495
496 const TargetInstrDesc &II = TII->get(Opc);
497 unsigned NumResults = CountResults(Node);
498 unsigned NodeOperands = CountOperands(Node);
499 unsigned MemOperandsEnd = ComputeMemOperandsEnd(Node);
500 bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
501 II.getImplicitDefs() != 0;
502#ifndef NDEBUG
503 unsigned NumMIOperands = NodeOperands + NumResults;
504 assert((II.getNumOperands() == NumMIOperands ||
505 HasPhysRegOuts || II.isVariadic()) &&
506 "#operands for dag node doesn't match .td file!");
507#endif
508
509 // Create the new machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000510 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(), II);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000511
512 // Add result register values for things that are defined by this
513 // instruction.
514 if (NumResults)
Evan Chenge57187c2009-01-16 20:57:18 +0000515 CreateVirtualRegisters(Node, MI, II, IsClone, IsCloned, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000516
517 // Emit all of the actual operands of this instruction, adding them to the
518 // instruction as appropriate.
Evan Cheng8955e932009-07-11 01:06:50 +0000519 bool HasOptPRefs = II.getNumDefs() > NumResults;
520 assert((!HasOptPRefs || !HasPhysRegOuts) &&
521 "Unable to cope with optional defs and phys regs defs!");
522 unsigned NumSkip = HasOptPRefs ? II.getNumDefs() - NumResults : 0;
523 for (unsigned i = NumSkip; i != NodeOperands; ++i)
524 AddOperand(MI, Node->getOperand(i), i-NumSkip+II.getNumDefs(), &II,
525 VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000526
527 // Emit all of the memory operands of this instruction
528 for (unsigned i = NodeOperands; i != MemOperandsEnd; ++i)
Evan Cheng8955e932009-07-11 01:06:50 +0000529 AddMemOperand(MI,cast<MemOperandSDNode>(Node->getOperand(i+NumSkip))->MO);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000530
Dan Gohmanf7119392009-01-16 22:10:20 +0000531 if (II.usesCustomDAGSchedInsertionHook()) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000532 // Insert this instruction into the basic block using a target
533 // specific inserter which may returns a new basic block.
534 BB = TLI->EmitInstrWithCustomInserter(MI, BB);
Dan Gohman47ac0f02009-02-11 04:27:20 +0000535 InsertPos = BB->end();
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000536 } else {
Dan Gohman47ac0f02009-02-11 04:27:20 +0000537 BB->insert(InsertPos, MI);
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000538 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000539
540 // Additional results must be an physical register def.
541 if (HasPhysRegOuts) {
542 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
543 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
544 if (Node->hasAnyUseOfValue(i))
Evan Chenge57187c2009-01-16 20:57:18 +0000545 EmitCopyFromReg(Node, i, IsClone, IsCloned, Reg, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000546 }
547 }
548 return;
549 }
550
551 switch (Node->getOpcode()) {
552 default:
553#ifndef NDEBUG
Dan Gohmana23b3b82008-11-13 21:21:28 +0000554 Node->dump(DAG);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000555#endif
Torok Edwinc23197a2009-07-14 16:55:14 +0000556 llvm_unreachable("This target-independent node should have been selected!");
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000557 break;
558 case ISD::EntryToken:
Torok Edwinc23197a2009-07-14 16:55:14 +0000559 llvm_unreachable("EntryToken should have been excluded from the schedule!");
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000560 break;
561 case ISD::TokenFactor: // fall thru
562 break;
563 case ISD::CopyToReg: {
564 unsigned SrcReg;
565 SDValue SrcVal = Node->getOperand(2);
566 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
567 SrcReg = R->getReg();
568 else
569 SrcReg = getVR(SrcVal, VRBaseMap);
570
571 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
572 if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
573 break;
574
575 const TargetRegisterClass *SrcTRC = 0, *DstTRC = 0;
576 // Get the register classes of the src/dst.
577 if (TargetRegisterInfo::isVirtualRegister(SrcReg))
578 SrcTRC = MRI.getRegClass(SrcReg);
579 else
580 SrcTRC = TRI->getPhysicalRegisterRegClass(SrcReg,SrcVal.getValueType());
581
582 if (TargetRegisterInfo::isVirtualRegister(DestReg))
583 DstTRC = MRI.getRegClass(DestReg);
584 else
585 DstTRC = TRI->getPhysicalRegisterRegClass(DestReg,
586 Node->getOperand(1).getValueType());
Dan Gohmanf8c73942009-04-13 15:38:05 +0000587
Dan Gohman47ac0f02009-02-11 04:27:20 +0000588 bool Emitted = TII->copyRegToReg(*BB, InsertPos, DestReg, SrcReg,
589 DstTRC, SrcTRC);
Dan Gohmanf8c73942009-04-13 15:38:05 +0000590 assert(Emitted && "Unable to issue a copy instruction!\n");
Daniel Dunbar8c562e22009-05-18 16:43:04 +0000591 (void) Emitted;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000592 break;
593 }
594 case ISD::CopyFromReg: {
595 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Evan Chenge57187c2009-01-16 20:57:18 +0000596 EmitCopyFromReg(Node, 0, IsClone, IsCloned, SrcReg, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000597 break;
598 }
599 case ISD::INLINEASM: {
600 unsigned NumOps = Node->getNumOperands();
601 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
602 --NumOps; // Ignore the flag operand.
603
604 // Create the inline asm machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000605 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(),
606 TII->get(TargetInstrInfo::INLINEASM));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000607
608 // Add the asm string as an external symbol operand.
Bill Wendling056292f2008-09-16 21:48:12 +0000609 const char *AsmStr =
610 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000611 MI->addOperand(MachineOperand::CreateES(AsmStr));
612
613 // Add all of the operand registers to the instruction.
614 for (unsigned i = 2; i != NumOps;) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000615 unsigned Flags =
616 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +0000617 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000618
619 MI->addOperand(MachineOperand::CreateImm(Flags));
620 ++i; // Skip the ID value.
621
622 switch (Flags & 7) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000623 default: llvm_unreachable("Bad flags!");
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000624 case 2: // Def of register.
625 for (; NumVals; --NumVals, ++i) {
626 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
627 MI->addOperand(MachineOperand::CreateReg(Reg, true));
628 }
629 break;
Dale Johannesen913d3df2008-09-12 17:49:03 +0000630 case 6: // Def of earlyclobber register.
631 for (; NumVals; --NumVals, ++i) {
632 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
633 MI->addOperand(MachineOperand::CreateReg(Reg, true, false, false,
Evan Cheng4784f1f2009-06-30 08:49:04 +0000634 false, false, true));
Dale Johannesen913d3df2008-09-12 17:49:03 +0000635 }
636 break;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000637 case 1: // Use of register.
638 case 3: // Immediate.
639 case 4: // Addressing mode.
640 // The addressing mode has been selected, just add all of the
641 // operands to the machine instruction.
642 for (; NumVals; --NumVals, ++i)
Dale Johannesen86b49f82008-09-24 01:07:17 +0000643 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000644 break;
645 }
646 }
Dan Gohman47ac0f02009-02-11 04:27:20 +0000647 BB->insert(InsertPos, MI);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000648 break;
649 }
650 }
651}
652
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000653/// EmitSchedule - Emit the machine code in scheduled order.
Dan Gohman343f0c02008-11-19 23:18:57 +0000654MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000655 DenseMap<SDValue, unsigned> VRBaseMap;
656 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
657 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
658 SUnit *SU = Sequence[i];
659 if (!SU) {
660 // Null SUnit* is a noop.
661 EmitNoop();
662 continue;
663 }
Dan Gohmanf449bf32008-11-14 00:06:09 +0000664
Dan Gohmanf449bf32008-11-14 00:06:09 +0000665 // For pre-regalloc scheduling, create instructions corresponding to the
666 // SDNode and any flagged SDNodes and append them to the block.
Evan Chengc29a56d2009-01-12 03:19:55 +0000667 if (!SU->getNode()) {
668 // Emit a copy.
669 EmitPhysRegCopy(SU, CopyVRBaseMap);
670 continue;
671 }
672
Dan Gohmand23e0f82008-11-13 23:24:17 +0000673 SmallVector<SDNode *, 4> FlaggedNodes;
Evan Chenge57187c2009-01-16 20:57:18 +0000674 for (SDNode *N = SU->getNode()->getFlaggedNode(); N;
675 N = N->getFlaggedNode())
Dan Gohmand23e0f82008-11-13 23:24:17 +0000676 FlaggedNodes.push_back(N);
677 while (!FlaggedNodes.empty()) {
Evan Chenge57187c2009-01-16 20:57:18 +0000678 EmitNode(FlaggedNodes.back(), SU->OrigNode != SU, SU->isCloned,VRBaseMap);
Dan Gohmand23e0f82008-11-13 23:24:17 +0000679 FlaggedNodes.pop_back();
680 }
Evan Chenge57187c2009-01-16 20:57:18 +0000681 EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000682 }
683
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000684 return BB;
685}