blob: f9bfe003ed6c225ee7747896fd7a09b2ca456265 [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 Cheng8955e932009-07-11 01:06:50 +0000163 if (II.OpInfo[i].isOptionalDef()) {
164 // Optional def must be a physical register.
165 unsigned NumResults = CountResults(Node);
166 VRBase = cast<RegisterSDNode>(Node->getOperand(i-NumResults))->getReg();
167 assert(TargetRegisterInfo::isPhysicalRegister(VRBase));
168 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
169 }
Evan Chenge57187c2009-01-16 20:57:18 +0000170
Evan Cheng8955e932009-07-11 01:06:50 +0000171 if (!VRBase && !IsClone && !IsCloned)
Evan Chenge57187c2009-01-16 20:57:18 +0000172 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
173 UI != E; ++UI) {
174 SDNode *User = *UI;
175 if (User->getOpcode() == ISD::CopyToReg &&
176 User->getOperand(2).getNode() == Node &&
177 User->getOperand(2).getResNo() == i) {
178 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
179 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Dan Gohmanf8c73942009-04-13 15:38:05 +0000180 const TargetRegisterClass *RegRC = MRI.getRegClass(Reg);
181 if (RegRC == RC) {
182 VRBase = Reg;
183 MI->addOperand(MachineOperand::CreateReg(Reg, true));
184 break;
185 }
Evan Chenge57187c2009-01-16 20:57:18 +0000186 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000187 }
188 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000189
190 // Create the result registers for this node and add the result regs to
191 // the machine instruction.
192 if (VRBase == 0) {
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
Dan Gohmanf8c73942009-04-13 15:38:05 +0000231/// AddRegisterOperand - Add the specified register as an operand to the
232/// specified machine instr. Insert register copies if the register is
233/// not in the required register class.
234void
235ScheduleDAGSDNodes::AddRegisterOperand(MachineInstr *MI, SDValue Op,
236 unsigned IIOpNum,
237 const TargetInstrDesc *II,
238 DenseMap<SDValue, unsigned> &VRBaseMap) {
239 assert(Op.getValueType() != MVT::Other &&
240 Op.getValueType() != MVT::Flag &&
241 "Chain and flag operands should occur at end of operand list!");
242 // Get/emit the operand.
243 unsigned VReg = getVR(Op, VRBaseMap);
244 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
245
246 const TargetInstrDesc &TID = MI->getDesc();
247 bool isOptDef = IIOpNum < TID.getNumOperands() &&
248 TID.OpInfo[IIOpNum].isOptionalDef();
249
250 // If the instruction requires a register in a different class, create
251 // a new virtual register and copy the value into it.
252 if (II) {
253 const TargetRegisterClass *SrcRC =
254 MRI.getRegClass(VReg);
255 const TargetRegisterClass *DstRC =
256 getInstrOperandRegClass(TRI, *II, IIOpNum);
257 assert((DstRC || (TID.isVariadic() && IIOpNum >= TID.getNumOperands())) &&
258 "Don't have operand info for this instruction!");
259 if (DstRC && SrcRC != DstRC && !SrcRC->hasSuperClass(DstRC)) {
260 unsigned NewVReg = MRI.createVirtualRegister(DstRC);
261 bool Emitted = TII->copyRegToReg(*BB, InsertPos, NewVReg, VReg,
262 DstRC, SrcRC);
Dan Gohmanf8c73942009-04-13 15:38:05 +0000263 assert(Emitted && "Unable to issue a copy instruction!\n");
Daniel Dunbar8c562e22009-05-18 16:43:04 +0000264 (void) Emitted;
Dan Gohmanf8c73942009-04-13 15:38:05 +0000265 VReg = NewVReg;
266 }
267 }
268
269 MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
270}
271
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000272/// AddOperand - Add the specified operand to the specified machine instr. II
273/// specifies the instruction information for the node, and IIOpNum is the
274/// operand number (in the II) that we are adding. IIOpNum and II are used for
275/// assertions only.
Dan Gohman343f0c02008-11-19 23:18:57 +0000276void ScheduleDAGSDNodes::AddOperand(MachineInstr *MI, SDValue Op,
277 unsigned IIOpNum,
278 const TargetInstrDesc *II,
279 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000280 if (Op.isMachineOpcode()) {
Dan Gohmanf8c73942009-04-13 15:38:05 +0000281 AddRegisterOperand(MI, Op, IIOpNum, II, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000282 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000283 MI->addOperand(MachineOperand::CreateImm(C->getZExtValue()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000284 } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000285 const ConstantFP *CFP = F->getConstantFPValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000286 MI->addOperand(MachineOperand::CreateFPImm(CFP));
287 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
Dale Johannesen86b49f82008-09-24 01:07:17 +0000288 MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000289 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
Chris Lattner6ec66db2009-06-26 05:52:14 +0000290 MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(), TGA->getOffset(),
291 TGA->getTargetFlags()));
Dan Gohmanf8c73942009-04-13 15:38:05 +0000292 } else if (BasicBlockSDNode *BBNode = dyn_cast<BasicBlockSDNode>(Op)) {
293 MI->addOperand(MachineOperand::CreateMBB(BBNode->getBasicBlock()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000294 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
295 MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
296 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
Chris Lattner6ec66db2009-06-26 05:52:14 +0000297 MI->addOperand(MachineOperand::CreateJTI(JT->getIndex(),
298 JT->getTargetFlags()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000299 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
300 int Offset = CP->getOffset();
301 unsigned Align = CP->getAlignment();
302 const Type *Type = CP->getType();
303 // MachineConstantPool wants an explicit alignment.
304 if (Align == 0) {
Evan Cheng1606e8e2009-03-13 07:51:59 +0000305 Align = TM.getTargetData()->getPrefTypeAlignment(Type);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000306 if (Align == 0) {
307 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +0000308 Align = TM.getTargetData()->getTypeAllocSize(Type);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000309 }
310 }
311
312 unsigned Idx;
313 if (CP->isMachineConstantPoolEntry())
314 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
315 else
316 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
Chris Lattner6ec66db2009-06-26 05:52:14 +0000317 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset,
318 CP->getTargetFlags()));
Bill Wendling056292f2008-09-16 21:48:12 +0000319 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
Chris Lattner6ec66db2009-06-26 05:52:14 +0000320 MI->addOperand(MachineOperand::CreateES(ES->getSymbol(), 0,
321 ES->getTargetFlags()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000322 } else {
323 assert(Op.getValueType() != MVT::Other &&
324 Op.getValueType() != MVT::Flag &&
325 "Chain and flag operands should occur at end of operand list!");
Dan Gohmanf8c73942009-04-13 15:38:05 +0000326 AddRegisterOperand(MI, Op, IIOpNum, II, VRBaseMap);
327 }
328}
329
Dan Gohmanf8c73942009-04-13 15:38:05 +0000330/// getSuperRegisterRegClass - Returns the register class of a superreg A whose
331/// "SubIdx"'th sub-register class is the specified register class and whose
332/// type matches the specified type.
333static const TargetRegisterClass*
334getSuperRegisterRegClass(const TargetRegisterClass *TRC,
335 unsigned SubIdx, MVT VT) {
336 // Pick the register class of the superegister for this type
337 for (TargetRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
338 E = TRC->superregclasses_end(); I != E; ++I)
Jakob Stoklund Olesenfa4677b2009-04-28 16:34:09 +0000339 if ((*I)->hasType(VT) && (*I)->getSubRegisterRegClass(SubIdx) == TRC)
Dan Gohmanf8c73942009-04-13 15:38:05 +0000340 return *I;
341 assert(false && "Couldn't find the register class");
342 return 0;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000343}
344
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000345/// EmitSubregNode - Generate machine code for subreg nodes.
346///
Dan Gohman343f0c02008-11-19 23:18:57 +0000347void ScheduleDAGSDNodes::EmitSubregNode(SDNode *Node,
Chris Lattner52023122009-06-26 05:39:02 +0000348 DenseMap<SDValue, unsigned> &VRBaseMap){
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000349 unsigned VRBase = 0;
350 unsigned Opc = Node->getMachineOpcode();
351
352 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
353 // the CopyToReg'd destination register instead of creating a new vreg.
354 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
355 UI != E; ++UI) {
356 SDNode *User = *UI;
357 if (User->getOpcode() == ISD::CopyToReg &&
358 User->getOperand(2).getNode() == Node) {
359 unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
360 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
361 VRBase = DestReg;
362 break;
363 }
364 }
365 }
366
367 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000368 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000369
370 // Create the extract_subreg machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000371 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(),
372 TII->get(TargetInstrInfo::EXTRACT_SUBREG));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000373
374 // Figure out the register class to create for the destreg.
Dan Gohmanf8c73942009-04-13 15:38:05 +0000375 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
376 const TargetRegisterClass *TRC = MRI.getRegClass(VReg);
Jakob Stoklund Olesenfa4677b2009-04-28 16:34:09 +0000377 const TargetRegisterClass *SRC = TRC->getSubRegisterRegClass(SubIdx);
378 assert(SRC && "Invalid subregister index in EXTRACT_SUBREG");
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000379
Dan Gohman5ec3b422009-04-14 22:17:14 +0000380 // Figure out the register class to create for the destreg.
381 // Note that if we're going to directly use an existing register,
382 // it must be precisely the required class, and not a subclass
383 // thereof.
384 if (VRBase == 0 || SRC != MRI.getRegClass(VRBase)) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000385 // Create the reg
386 assert(SRC && "Couldn't find source register class");
387 VRBase = MRI.createVirtualRegister(SRC);
388 }
Dan Gohman5ec3b422009-04-14 22:17:14 +0000389
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000390 // Add def, source, and subreg index
391 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
392 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
393 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Dan Gohman47ac0f02009-02-11 04:27:20 +0000394 BB->insert(InsertPos, MI);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000395 } else if (Opc == TargetInstrInfo::INSERT_SUBREG ||
396 Opc == TargetInstrInfo::SUBREG_TO_REG) {
397 SDValue N0 = Node->getOperand(0);
398 SDValue N1 = Node->getOperand(1);
399 SDValue N2 = Node->getOperand(2);
Dan Gohmanf8c73942009-04-13 15:38:05 +0000400 unsigned SubReg = getVR(N1, VRBaseMap);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000401 unsigned SubIdx = cast<ConstantSDNode>(N2)->getZExtValue();
Dan Gohman5ec3b422009-04-14 22:17:14 +0000402 const TargetRegisterClass *TRC = MRI.getRegClass(SubReg);
403 const TargetRegisterClass *SRC =
404 getSuperRegisterRegClass(TRC, SubIdx,
405 Node->getValueType(0));
406
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000407 // Figure out the register class to create for the destreg.
Dan Gohman5ec3b422009-04-14 22:17:14 +0000408 // Note that if we're going to directly use an existing register,
409 // it must be precisely the required class, and not a subclass
410 // thereof.
411 if (VRBase == 0 || SRC != MRI.getRegClass(VRBase)) {
412 // Create the reg
413 assert(SRC && "Couldn't find source register class");
414 VRBase = MRI.createVirtualRegister(SRC);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000415 }
Dan Gohman5ec3b422009-04-14 22:17:14 +0000416
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000417 // Create the insert_subreg or subreg_to_reg machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000418 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(), TII->get(Opc));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000419 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
420
421 // If creating a subreg_to_reg, then the first input operand
422 // is an implicit value immediate, otherwise it's a register
423 if (Opc == TargetInstrInfo::SUBREG_TO_REG) {
424 const ConstantSDNode *SD = cast<ConstantSDNode>(N0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000425 MI->addOperand(MachineOperand::CreateImm(SD->getZExtValue()));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000426 } else
427 AddOperand(MI, N0, 0, 0, VRBaseMap);
428 // Add the subregster being inserted
429 AddOperand(MI, N1, 0, 0, VRBaseMap);
430 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Dan Gohman47ac0f02009-02-11 04:27:20 +0000431 BB->insert(InsertPos, MI);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000432 } else
433 assert(0 && "Node is not insert_subreg, extract_subreg, or subreg_to_reg");
434
435 SDValue Op(Node, 0);
436 bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
437 isNew = isNew; // Silence compiler warning.
438 assert(isNew && "Node emitted out of order - early");
439}
440
Dan Gohman88c7af02009-04-13 21:06:25 +0000441/// EmitCopyToRegClassNode - Generate machine code for COPY_TO_REGCLASS nodes.
442/// COPY_TO_REGCLASS is just a normal copy, except that the destination
Dan Gohmanf8c73942009-04-13 15:38:05 +0000443/// register is constrained to be in a particular register class.
444///
445void
Dan Gohman88c7af02009-04-13 21:06:25 +0000446ScheduleDAGSDNodes::EmitCopyToRegClassNode(SDNode *Node,
Dan Gohmanf8c73942009-04-13 15:38:05 +0000447 DenseMap<SDValue, unsigned> &VRBaseMap) {
448 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
449 const TargetRegisterClass *SrcRC = MRI.getRegClass(VReg);
450
451 unsigned DstRCIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
452 const TargetRegisterClass *DstRC = TRI->getRegClass(DstRCIdx);
453
Dan Gohmanf8c73942009-04-13 15:38:05 +0000454 // Create the new VReg in the destination class and emit a copy.
455 unsigned NewVReg = MRI.createVirtualRegister(DstRC);
456 bool Emitted = TII->copyRegToReg(*BB, InsertPos, NewVReg, VReg,
457 DstRC, SrcRC);
Dan Gohmanf8c73942009-04-13 15:38:05 +0000458 assert(Emitted &&
Dan Gohman88c7af02009-04-13 21:06:25 +0000459 "Unable to issue a copy instruction for a COPY_TO_REGCLASS node!\n");
Daniel Dunbar8c562e22009-05-18 16:43:04 +0000460 (void) Emitted;
Dan Gohmanf8c73942009-04-13 15:38:05 +0000461
462 SDValue Op(Node, 0);
463 bool isNew = VRBaseMap.insert(std::make_pair(Op, NewVReg)).second;
464 isNew = isNew; // Silence compiler warning.
465 assert(isNew && "Node emitted out of order - early");
466}
467
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000468/// EmitNode - Generate machine code for an node and needed dependencies.
469///
Evan Chenge57187c2009-01-16 20:57:18 +0000470void ScheduleDAGSDNodes::EmitNode(SDNode *Node, bool IsClone, bool IsCloned,
Dan Gohman343f0c02008-11-19 23:18:57 +0000471 DenseMap<SDValue, unsigned> &VRBaseMap) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000472 // If machine instruction
473 if (Node->isMachineOpcode()) {
474 unsigned Opc = Node->getMachineOpcode();
475
476 // Handle subreg insert/extract specially
477 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
478 Opc == TargetInstrInfo::INSERT_SUBREG ||
479 Opc == TargetInstrInfo::SUBREG_TO_REG) {
480 EmitSubregNode(Node, VRBaseMap);
481 return;
482 }
483
Dan Gohman88c7af02009-04-13 21:06:25 +0000484 // Handle COPY_TO_REGCLASS specially.
485 if (Opc == TargetInstrInfo::COPY_TO_REGCLASS) {
486 EmitCopyToRegClassNode(Node, VRBaseMap);
Dan Gohmanf8c73942009-04-13 15:38:05 +0000487 return;
488 }
489
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000490 if (Opc == TargetInstrInfo::IMPLICIT_DEF)
491 // We want a unique VR for each IMPLICIT_DEF use.
492 return;
493
494 const TargetInstrDesc &II = TII->get(Opc);
495 unsigned NumResults = CountResults(Node);
496 unsigned NodeOperands = CountOperands(Node);
497 unsigned MemOperandsEnd = ComputeMemOperandsEnd(Node);
498 bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
499 II.getImplicitDefs() != 0;
500#ifndef NDEBUG
501 unsigned NumMIOperands = NodeOperands + NumResults;
502 assert((II.getNumOperands() == NumMIOperands ||
503 HasPhysRegOuts || II.isVariadic()) &&
504 "#operands for dag node doesn't match .td file!");
505#endif
506
507 // Create the new machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000508 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(), II);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000509
510 // Add result register values for things that are defined by this
511 // instruction.
512 if (NumResults)
Evan Chenge57187c2009-01-16 20:57:18 +0000513 CreateVirtualRegisters(Node, MI, II, IsClone, IsCloned, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000514
515 // Emit all of the actual operands of this instruction, adding them to the
516 // instruction as appropriate.
Evan Cheng8955e932009-07-11 01:06:50 +0000517 bool HasOptPRefs = II.getNumDefs() > NumResults;
518 assert((!HasOptPRefs || !HasPhysRegOuts) &&
519 "Unable to cope with optional defs and phys regs defs!");
520 unsigned NumSkip = HasOptPRefs ? II.getNumDefs() - NumResults : 0;
521 for (unsigned i = NumSkip; i != NodeOperands; ++i)
522 AddOperand(MI, Node->getOperand(i), i-NumSkip+II.getNumDefs(), &II,
523 VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000524
525 // Emit all of the memory operands of this instruction
526 for (unsigned i = NodeOperands; i != MemOperandsEnd; ++i)
Evan Cheng8955e932009-07-11 01:06:50 +0000527 AddMemOperand(MI,cast<MemOperandSDNode>(Node->getOperand(i+NumSkip))->MO);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000528
Dan Gohmanf7119392009-01-16 22:10:20 +0000529 if (II.usesCustomDAGSchedInsertionHook()) {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000530 // Insert this instruction into the basic block using a target
531 // specific inserter which may returns a new basic block.
532 BB = TLI->EmitInstrWithCustomInserter(MI, BB);
Dan Gohman47ac0f02009-02-11 04:27:20 +0000533 InsertPos = BB->end();
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000534 } else {
Dan Gohman47ac0f02009-02-11 04:27:20 +0000535 BB->insert(InsertPos, MI);
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000536 }
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000537
538 // Additional results must be an physical register def.
539 if (HasPhysRegOuts) {
540 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
541 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
542 if (Node->hasAnyUseOfValue(i))
Evan Chenge57187c2009-01-16 20:57:18 +0000543 EmitCopyFromReg(Node, i, IsClone, IsCloned, Reg, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000544 }
545 }
546 return;
547 }
548
549 switch (Node->getOpcode()) {
550 default:
551#ifndef NDEBUG
Dan Gohmana23b3b82008-11-13 21:21:28 +0000552 Node->dump(DAG);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000553#endif
554 assert(0 && "This target-independent node should have been selected!");
555 break;
556 case ISD::EntryToken:
557 assert(0 && "EntryToken should have been excluded from the schedule!");
558 break;
559 case ISD::TokenFactor: // fall thru
560 break;
561 case ISD::CopyToReg: {
562 unsigned SrcReg;
563 SDValue SrcVal = Node->getOperand(2);
564 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
565 SrcReg = R->getReg();
566 else
567 SrcReg = getVR(SrcVal, VRBaseMap);
568
569 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
570 if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
571 break;
572
573 const TargetRegisterClass *SrcTRC = 0, *DstTRC = 0;
574 // Get the register classes of the src/dst.
575 if (TargetRegisterInfo::isVirtualRegister(SrcReg))
576 SrcTRC = MRI.getRegClass(SrcReg);
577 else
578 SrcTRC = TRI->getPhysicalRegisterRegClass(SrcReg,SrcVal.getValueType());
579
580 if (TargetRegisterInfo::isVirtualRegister(DestReg))
581 DstTRC = MRI.getRegClass(DestReg);
582 else
583 DstTRC = TRI->getPhysicalRegisterRegClass(DestReg,
584 Node->getOperand(1).getValueType());
Dan Gohmanf8c73942009-04-13 15:38:05 +0000585
Dan Gohman47ac0f02009-02-11 04:27:20 +0000586 bool Emitted = TII->copyRegToReg(*BB, InsertPos, DestReg, SrcReg,
587 DstTRC, SrcTRC);
Dan Gohmanf8c73942009-04-13 15:38:05 +0000588 assert(Emitted && "Unable to issue a copy instruction!\n");
Daniel Dunbar8c562e22009-05-18 16:43:04 +0000589 (void) Emitted;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000590 break;
591 }
592 case ISD::CopyFromReg: {
593 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Evan Chenge57187c2009-01-16 20:57:18 +0000594 EmitCopyFromReg(Node, 0, IsClone, IsCloned, SrcReg, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000595 break;
596 }
597 case ISD::INLINEASM: {
598 unsigned NumOps = Node->getNumOperands();
599 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
600 --NumOps; // Ignore the flag operand.
601
602 // Create the inline asm machine instruction.
Bill Wendlingf2ad58d2009-02-03 01:02:39 +0000603 MachineInstr *MI = BuildMI(MF, Node->getDebugLoc(),
604 TII->get(TargetInstrInfo::INLINEASM));
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000605
606 // Add the asm string as an external symbol operand.
Bill Wendling056292f2008-09-16 21:48:12 +0000607 const char *AsmStr =
608 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000609 MI->addOperand(MachineOperand::CreateES(AsmStr));
610
611 // Add all of the operand registers to the instruction.
612 for (unsigned i = 2; i != NumOps;) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000613 unsigned Flags =
614 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +0000615 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000616
617 MI->addOperand(MachineOperand::CreateImm(Flags));
618 ++i; // Skip the ID value.
619
620 switch (Flags & 7) {
621 default: assert(0 && "Bad flags!");
622 case 2: // Def of register.
623 for (; NumVals; --NumVals, ++i) {
624 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
625 MI->addOperand(MachineOperand::CreateReg(Reg, true));
626 }
627 break;
Dale Johannesen913d3df2008-09-12 17:49:03 +0000628 case 6: // Def of earlyclobber register.
629 for (; NumVals; --NumVals, ++i) {
630 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
631 MI->addOperand(MachineOperand::CreateReg(Reg, true, false, false,
Evan Cheng4784f1f2009-06-30 08:49:04 +0000632 false, false, true));
Dale Johannesen913d3df2008-09-12 17:49:03 +0000633 }
634 break;
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000635 case 1: // Use of register.
636 case 3: // Immediate.
637 case 4: // Addressing mode.
638 // The addressing mode has been selected, just add all of the
639 // operands to the machine instruction.
640 for (; NumVals; --NumVals, ++i)
Dale Johannesen86b49f82008-09-24 01:07:17 +0000641 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000642 break;
643 }
644 }
Dan Gohman47ac0f02009-02-11 04:27:20 +0000645 BB->insert(InsertPos, MI);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000646 break;
647 }
648 }
649}
650
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000651/// EmitSchedule - Emit the machine code in scheduled order.
Dan Gohman343f0c02008-11-19 23:18:57 +0000652MachineBasicBlock *ScheduleDAGSDNodes::EmitSchedule() {
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000653 DenseMap<SDValue, unsigned> VRBaseMap;
654 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
655 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
656 SUnit *SU = Sequence[i];
657 if (!SU) {
658 // Null SUnit* is a noop.
659 EmitNoop();
660 continue;
661 }
Dan Gohmanf449bf32008-11-14 00:06:09 +0000662
Dan Gohmanf449bf32008-11-14 00:06:09 +0000663 // For pre-regalloc scheduling, create instructions corresponding to the
664 // SDNode and any flagged SDNodes and append them to the block.
Evan Chengc29a56d2009-01-12 03:19:55 +0000665 if (!SU->getNode()) {
666 // Emit a copy.
667 EmitPhysRegCopy(SU, CopyVRBaseMap);
668 continue;
669 }
670
Dan Gohmand23e0f82008-11-13 23:24:17 +0000671 SmallVector<SDNode *, 4> FlaggedNodes;
Evan Chenge57187c2009-01-16 20:57:18 +0000672 for (SDNode *N = SU->getNode()->getFlaggedNode(); N;
673 N = N->getFlaggedNode())
Dan Gohmand23e0f82008-11-13 23:24:17 +0000674 FlaggedNodes.push_back(N);
675 while (!FlaggedNodes.empty()) {
Evan Chenge57187c2009-01-16 20:57:18 +0000676 EmitNode(FlaggedNodes.back(), SU->OrigNode != SU, SU->isCloned,VRBaseMap);
Dan Gohmand23e0f82008-11-13 23:24:17 +0000677 FlaggedNodes.pop_back();
678 }
Evan Chenge57187c2009-01-16 20:57:18 +0000679 EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned, VRBaseMap);
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000680 }
681
Dan Gohman94b8d7e2008-09-03 16:01:59 +0000682 return BB;
683}