blob: d9916583abc44e00583b758672a063e238260831 [file] [log] [blame]
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "mips-lower"
16
17#include "MipsISelLowering.h"
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +000018#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000019#include "MipsTargetMachine.h"
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000020#include "MipsSubtarget.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000021#include "llvm/DerivedTypes.h"
22#include "llvm/Function.h"
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +000023#include "llvm/GlobalVariable.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000024#include "llvm/Intrinsics.h"
25#include "llvm/CallingConv.h"
26#include "llvm/CodeGen/CallingConvLower.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000031#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000032#include "llvm/CodeGen/ValueTypes.h"
33#include "llvm/Support/Debug.h"
34#include <queue>
35#include <set>
36
37using namespace llvm;
38
39const char *MipsTargetLowering::
40getTargetNodeName(unsigned Opcode) const
41{
42 switch (Opcode)
43 {
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +000044 case MipsISD::JmpLink : return "MipsISD::JmpLink";
45 case MipsISD::Hi : return "MipsISD::Hi";
46 case MipsISD::Lo : return "MipsISD::Lo";
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +000047 case MipsISD::GPRel : return "MipsISD::GPRel";
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +000048 case MipsISD::Ret : return "MipsISD::Ret";
49 case MipsISD::SelectCC : return "MipsISD::SelectCC";
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000050 case MipsISD::FPBrcond : return "MipsISD::FPBrcond";
51 case MipsISD::FPCmp : return "MipsISD::FPCmp";
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +000052 default : return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000053 }
54}
55
56MipsTargetLowering::
57MipsTargetLowering(MipsTargetMachine &TM): TargetLowering(TM)
58{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000059 Subtarget = &TM.getSubtarget<MipsSubtarget>();
60
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000061 // Mips does not have i1 type, so use i32 for
62 // setcc operations results (slt, sgt, ...).
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000063 setSetCCResultContents(ZeroOrOneSetCCResult);
64
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000065 // JumpTable targets must use GOT when using PIC_
66 setUsesGlobalOffsetTable(true);
67
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000068 // Set up the register classes
69 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
70
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000071 // When dealing with single precision only, use libcalls
72 if (!Subtarget->isSingleFloat()) {
73 addRegisterClass(MVT::f32, Mips::AFGR32RegisterClass);
74 if (!Subtarget->isFP64bit())
75 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
76 } else
77 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
78
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000079 // Load extented operations for i1 types must be promoted
80 setLoadXAction(ISD::EXTLOAD, MVT::i1, Promote);
81 setLoadXAction(ISD::ZEXTLOAD, MVT::i1, Promote);
82 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
83
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000084 // Mips Custom Operations
85 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
86 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
87 setOperationAction(ISD::RET, MVT::Other, Custom);
88 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
89 setOperationAction(ISD::ConstantPool, MVT::f32, Custom);
90 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
91 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
92
93 // Operations not directly supported by Mips.
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +000094 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
95 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
96 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
97 setOperationAction(ISD::SELECT, MVT::i32, Expand);
98 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
99 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
100 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000101 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
102 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
103 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
104 setOperationAction(ISD::ROTL, MVT::i32, Expand);
105 setOperationAction(ISD::ROTR, MVT::i32, Expand);
106 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
107 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
108 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
109 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
110
111 // We don't have line number support yet.
112 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
113 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
114 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
115 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
116
117 // Use the default for now
118 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
119 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
120 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000121
122 if (Subtarget->isSingleFloat())
123 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000124
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000125 if (!Subtarget->hasSEInReg()) {
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000126 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000127 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
128 }
129
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000130 setStackPointerRegisterToSaveRestore(Mips::SP);
131 computeRegisterProperties();
132}
133
134
Duncan Sands83ec4b62008-06-06 12:08:01 +0000135MVT MipsTargetLowering::getSetCCResultType(const SDOperand &) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000136 return MVT::i32;
137}
138
139
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000140SDOperand MipsTargetLowering::
141LowerOperation(SDOperand Op, SelectionDAG &DAG)
142{
143 switch (Op.getOpcode())
144 {
145 case ISD::CALL: return LowerCALL(Op, DAG);
146 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
147 case ISD::RET: return LowerRET(Op, DAG);
148 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000149 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000150 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000151 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000152 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000153 }
154 return SDOperand();
155}
156
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000157MachineBasicBlock *
158MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
159 MachineBasicBlock *BB)
160{
161 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
162 switch (MI->getOpcode()) {
163 default: assert(false && "Unexpected instr type to insert");
164 case Mips::Select_CC: {
165 // To "insert" a SELECT_CC instruction, we actually have to insert the
166 // diamond control-flow pattern. The incoming instruction knows the
167 // destination vreg to set, the condition code register to branch on, the
168 // true/false values to select between, and a branch opcode to use.
169 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000170 MachineFunction::iterator It = BB;
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000171 ++It;
172
173 // thisMBB:
174 // ...
175 // TrueVal = ...
176 // setcc r1, r2, r3
177 // bNE r1, r0, copy1MBB
178 // fallthrough --> copy0MBB
179 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000180 MachineFunction *F = BB->getParent();
181 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
182 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000183 BuildMI(BB, TII->get(Mips::BNE)).addReg(MI->getOperand(1).getReg())
184 .addReg(Mips::ZERO).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000185 F->insert(It, copy0MBB);
186 F->insert(It, sinkMBB);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000187 // Update machine-CFG edges by first adding all successors of the current
188 // block to the new block which will contain the Phi node for the select.
189 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
190 e = BB->succ_end(); i != e; ++i)
191 sinkMBB->addSuccessor(*i);
192 // Next, remove all successors of the current block, and add the true
193 // and fallthrough blocks as its successors.
194 while(!BB->succ_empty())
195 BB->removeSuccessor(BB->succ_begin());
196 BB->addSuccessor(copy0MBB);
197 BB->addSuccessor(sinkMBB);
198
199 // copy0MBB:
200 // %FalseValue = ...
201 // # fallthrough to sinkMBB
202 BB = copy0MBB;
203
204 // Update machine-CFG edges
205 BB->addSuccessor(sinkMBB);
206
207 // sinkMBB:
208 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
209 // ...
210 BB = sinkMBB;
211 BuildMI(BB, TII->get(Mips::PHI), MI->getOperand(0).getReg())
212 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
213 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB);
214
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000215 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000216 return BB;
217 }
218 }
219}
220
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000221//===----------------------------------------------------------------------===//
222// Lower helper functions
223//===----------------------------------------------------------------------===//
224
225// AddLiveIn - This helper function adds the specified physical register to the
226// MachineFunction as a live in value. It also creates a corresponding
227// virtual register for it.
228static unsigned
229AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
230{
231 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000232 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
233 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000234 return VReg;
235}
236
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000237// Discover if this global address can be placed into small data/bss section.
238// This should happen for globals with size less than small section size
239// threshold in no abicall environments. Data in this section must be addressed
240// using gp_rel operator.
241bool MipsTargetLowering::IsGlobalInSmallSection(GlobalValue *GV)
242{
243 const TargetData *TD = getTargetData();
Bruno Cardoso Lopesfeb95cc2008-07-22 15:34:27 +0000244 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
245
246 if (!GVA)
247 return false;
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000248
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000249 const Type *Ty = GV->getType()->getElementType();
250 unsigned Size = TD->getABITypeSize(Ty);
251
252 // if this is a internal constant string, there is a special
253 // section for it, but not in small data/bss.
254 if (GVA->hasInitializer() && GV->hasInternalLinkage()) {
255 Constant *C = GVA->getInitializer();
256 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
257 if (CVA && CVA->isCString())
258 return false;
259 }
260
261 if (Size > 0 && (Size <= Subtarget->getSSectionThreshold()))
262 return true;
263
264 return false;
265}
266
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000267//===----------------------------------------------------------------------===//
268// Misc Lower Operation implementation
269//===----------------------------------------------------------------------===//
270SDOperand MipsTargetLowering::
271LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG)
272{
273 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000274 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000275
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000276 if (!Subtarget->hasABICall()) {
277 if (isa<Function>(GV)) return GA;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000278 const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000279 SDOperand Ops[] = { GA };
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000280
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000281 if (IsGlobalInSmallSection(GV)) { // %gp_rel relocation
282 SDOperand GPRelNode = DAG.getNode(MipsISD::GPRel, VTs, 1, Ops, 1);
283 SDOperand GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, MVT::i32);
284 return DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
285 }
286 // %hi/%lo relocation
287 SDOperand HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
288 SDOperand Lo = DAG.getNode(MipsISD::Lo, MVT::i32, GA);
289 return DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000290
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000291 } else { // Abicall relocations, TODO: make this cleaner.
292 SDOperand ResNode = DAG.getLoad(MVT::i32, DAG.getEntryNode(), GA, NULL, 0);
293 // On functions and global targets not internal linked only
294 // a load from got/GP is necessary for PIC to work.
295 if (!GV->hasInternalLinkage() || isa<Function>(GV))
296 return ResNode;
297 SDOperand Lo = DAG.getNode(MipsISD::Lo, MVT::i32, GA);
298 return DAG.getNode(ISD::ADD, MVT::i32, ResNode, Lo);
299 }
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000300
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000301 assert(0 && "Dont know how to handle GlobalAddress");
302 return SDOperand(0,0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000303}
304
305SDOperand MipsTargetLowering::
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000306LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG)
307{
308 assert(0 && "TLS not implemented for MIPS.");
Chris Lattnerd27c9912008-03-30 18:22:13 +0000309 return SDOperand(); // Not reached
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000310}
311
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000312SDOperand MipsTargetLowering::
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000313LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG)
314{
315 SDOperand LHS = Op.getOperand(0);
316 SDOperand RHS = Op.getOperand(1);
317 SDOperand True = Op.getOperand(2);
318 SDOperand False = Op.getOperand(3);
319 SDOperand CC = Op.getOperand(4);
320
Duncan Sands83ec4b62008-06-06 12:08:01 +0000321 const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000322 SDOperand Ops[] = { LHS, RHS, CC };
323 SDOperand SetCCRes = DAG.getNode(ISD::SETCC, VTs, 1, Ops, 3);
324
325 return DAG.getNode(MipsISD::SelectCC, True.getValueType(),
326 SetCCRes, True, False);
327}
328
329SDOperand MipsTargetLowering::
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000330LowerJumpTable(SDOperand Op, SelectionDAG &DAG)
331{
332 SDOperand ResNode;
333 SDOperand HiPart;
334
Duncan Sands83ec4b62008-06-06 12:08:01 +0000335 MVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000336 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
337 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
338
339 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000340 const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000341 SDOperand Ops[] = { JTI };
342 HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
343 } else // Emit Load from Global Pointer
344 HiPart = DAG.getLoad(MVT::i32, DAG.getEntryNode(), JTI, NULL, 0);
345
346 SDOperand Lo = DAG.getNode(MipsISD::Lo, MVT::i32, JTI);
347 ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
348
349 return ResNode;
350}
351
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000352SDOperand MipsTargetLowering::
353LowerConstantPool(SDOperand Op, SelectionDAG &DAG)
354{
355 assert(0 && "ConstantPool not implemented for MIPS.");
356 return SDOperand(); // Not reached
357}
358
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000359//===----------------------------------------------------------------------===//
360// Calling Convention Implementation
361//
362// The lower operations present on calling convention works on this order:
363// LowerCALL (virt regs --> phys regs, virt regs --> stack)
364// LowerFORMAL_ARGUMENTS (phys --> virt regs, stack --> virt regs)
365// LowerRET (virt regs --> phys regs)
366// LowerCALL (phys regs --> virt regs)
367//
368//===----------------------------------------------------------------------===//
369
370#include "MipsGenCallingConv.inc"
371
372//===----------------------------------------------------------------------===//
373// CALL Calling Convention Implementation
374//===----------------------------------------------------------------------===//
375
376/// Mips custom CALL implementation
377SDOperand MipsTargetLowering::
378LowerCALL(SDOperand Op, SelectionDAG &DAG)
379{
Chris Lattnere0b12152008-03-17 06:57:02 +0000380 unsigned CallingConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000381
382 // By now, only CallingConv::C implemented
Chris Lattnere0b12152008-03-17 06:57:02 +0000383 switch (CallingConv) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000384 default:
385 assert(0 && "Unsupported calling convention");
386 case CallingConv::Fast:
387 case CallingConv::C:
388 return LowerCCCCallTo(Op, DAG, CallingConv);
389 }
390}
391
392/// LowerCCCCallTo - functions arguments are copied from virtual
393/// regs to (physical regs)/(stack frame), CALLSEQ_START and
394/// CALLSEQ_END are emitted.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000395/// TODO: isVarArg, isTailCall.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000396SDOperand MipsTargetLowering::
397LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG, unsigned CC)
398{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000399 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000400
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000401 SDOperand Chain = Op.getOperand(0);
402 SDOperand Callee = Op.getOperand(4);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000403 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
404
405 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000406
407 // Analyze operands of the call, assigning locations to each operand.
408 SmallVector<CCValAssign, 16> ArgLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000409 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
410
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000411 // To meet O32 ABI, Mips must always allocate 16 bytes on
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000412 // the stack (even if less than 4 are used as arguments)
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000413 if (Subtarget->isABI_O32()) {
414 int VTsize = MVT(MVT::i32).getSizeInBits()/8;
415 MFI->CreateFixedObject(VTsize, (VTsize*3));
416 }
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000417
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000418 CCInfo.AnalyzeCallOperands(Op.Val, CC_Mips);
419
420 // Get a count of how many bytes are to be pushed on the stack.
421 unsigned NumBytes = CCInfo.getNextStackOffset();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000422 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes,
423 getPointerTy()));
424
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000425 // With EABI is it possible to have 16 args on registers.
426 SmallVector<std::pair<unsigned, SDOperand>, 16> RegsToPass;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000427 SmallVector<SDOperand, 8> MemOpChains;
428
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000429 // First/LastArgStackLoc contains the first/last
430 // "at stack" argument location.
431 int LastArgStackLoc = 0;
432 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000433
434 // Walk the register/memloc assignments, inserting copies/loads.
435 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
436 CCValAssign &VA = ArgLocs[i];
437
438 // Arguments start after the 5 first operands of ISD::CALL
439 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
440
441 // Promote the value if needed.
442 switch (VA.getLocInfo()) {
Chris Lattnere0b12152008-03-17 06:57:02 +0000443 default: assert(0 && "Unknown loc info!");
444 case CCValAssign::Full: break;
445 case CCValAssign::SExt:
446 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
447 break;
448 case CCValAssign::ZExt:
449 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
450 break;
451 case CCValAssign::AExt:
452 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
453 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000454 }
455
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000456 // Arguments that can be passed on register must be kept at
457 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000458 if (VA.isRegLoc()) {
459 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +0000460 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000461 }
Chris Lattnere0b12152008-03-17 06:57:02 +0000462
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000463 // Register cant get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +0000464 assert(VA.isMemLoc());
465
466 // Create the frame index object for this incoming parameter
467 // This guarantees that when allocating Local Area the firsts
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000468 // 16 bytes which are alwayes reserved won't be overwritten
469 // if O32 ABI is used. For EABI the first address is zero.
470 LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000471 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000472 LastArgStackLoc);
Chris Lattnere0b12152008-03-17 06:57:02 +0000473
474 SDOperand PtrOff = DAG.getFrameIndex(FI,getPointerTy());
475
476 // emit ISD::STORE whichs stores the
477 // parameter value to a stack Location
478 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000479 }
480
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000481 // Transform all store nodes into one single node because all store
482 // nodes are independent of each other.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000483 if (!MemOpChains.empty())
484 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
485 &MemOpChains[0], MemOpChains.size());
486
487 // Build a sequence of copy-to-reg nodes chained together with token
488 // chain and flag operands which copy the outgoing args into registers.
489 // The InFlag in necessary since all emited instructions must be
490 // stuck together.
491 SDOperand InFlag;
492 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
493 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first,
494 RegsToPass[i].second, InFlag);
495 InFlag = Chain.getValue(1);
496 }
497
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000498 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
499 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000500 // node so that legalize doesn't hack it.
501 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000502 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000503 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000504 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
505
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000506
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000507 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
508 // = Chain, Callee, Reg#1, Reg#2, ...
509 //
510 // Returns a chain & a flag for retval copy to use.
511 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
512 SmallVector<SDOperand, 8> Ops;
513 Ops.push_back(Chain);
514 Ops.push_back(Callee);
515
516 // Add argument registers to the end of the list so that they are
517 // known live into the call.
518 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
519 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
520 RegsToPass[i].second.getValueType()));
521
522 if (InFlag.Val)
523 Ops.push_back(InFlag);
524
525 Chain = DAG.getNode(MipsISD::JmpLink, NodeTys, &Ops[0], Ops.size());
526 InFlag = Chain.getValue(1);
527
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000528 // Create the CALLSEQ_END node.
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000529 Chain = DAG.getCALLSEQ_END(Chain,
530 DAG.getConstant(NumBytes, getPointerTy()),
531 DAG.getConstant(0, getPointerTy()),
532 InFlag);
533 InFlag = Chain.getValue(1);
534
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000535 // Create a stack location to hold GP when PIC is used. This stack
536 // location is used on function prologue to save GP and also after all
537 // emited CALL's to restore GP.
538 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000539 // Function can have an arbitrary number of calls, so
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000540 // hold the LastArgStackLoc with the biggest offset.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000541 int FI;
542 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000543 if (LastArgStackLoc >= MipsFI->getGPStackOffset()) {
544 LastArgStackLoc = (!LastArgStackLoc) ? (16) : (LastArgStackLoc+4);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000545 // Create the frame index only once. SPOffset here can be anything
546 // (this will be fixed on processFunctionBeforeFrameFinalized)
547 if (MipsFI->getGPStackOffset() == -1) {
548 FI = MFI->CreateFixedObject(4, 0);
549 MipsFI->setGPFI(FI);
550 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000551 MipsFI->setGPStackOffset(LastArgStackLoc);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000552 }
553
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000554 // Reload GP value.
555 FI = MipsFI->getGPFI();
556 SDOperand FIN = DAG.getFrameIndex(FI,getPointerTy());
557 SDOperand GPLoad = DAG.getLoad(MVT::i32, Chain, FIN, NULL, 0);
558 Chain = GPLoad.getValue(1);
559 Chain = DAG.getCopyToReg(Chain, DAG.getRegister(Mips::GP, MVT::i32),
560 GPLoad, SDOperand(0,0));
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000561 InFlag = Chain.getValue(1);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000562 }
563
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000564 // Handle result values, copying them out of physregs into vregs that we
565 // return.
566 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
567}
568
569/// LowerCallResult - Lower the result values of an ISD::CALL into the
570/// appropriate copies out of appropriate physical registers. This assumes that
571/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
572/// being lowered. Returns a SDNode with the same number of values as the
573/// ISD::CALL.
574SDNode *MipsTargetLowering::
575LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
576 unsigned CallingConv, SelectionDAG &DAG) {
577
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000578 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
579
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000580 // Assign locations to each value returned by this call.
581 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000582 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
583
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000584 CCInfo.AnalyzeCallResult(TheCall, RetCC_Mips);
585 SmallVector<SDOperand, 8> ResultVals;
586
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000587 // Copy all of the result registers out of their specified physreg.
588 for (unsigned i = 0; i != RVLocs.size(); ++i) {
589 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
590 RVLocs[i].getValVT(), InFlag).getValue(1);
591 InFlag = Chain.getValue(2);
592 ResultVals.push_back(Chain.getValue(0));
593 }
594
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000595 ResultVals.push_back(Chain);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000596
597 // Merge everything together with a MERGE_VALUES node.
Duncan Sandsf9516202008-06-30 10:19:09 +0000598 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
599 ResultVals.size()).Val;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000600}
601
602//===----------------------------------------------------------------------===//
603// FORMAL_ARGUMENTS Calling Convention Implementation
604//===----------------------------------------------------------------------===//
605
606/// Mips custom FORMAL_ARGUMENTS implementation
607SDOperand MipsTargetLowering::
608LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG)
609{
610 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
611 switch(CC)
612 {
613 default:
614 assert(0 && "Unsupported calling convention");
615 case CallingConv::C:
616 return LowerCCCArguments(Op, DAG);
617 }
618}
619
620/// LowerCCCArguments - transform physical registers into
621/// virtual registers and generate load operations for
622/// arguments places on the stack.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000623/// TODO: isVarArg
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000624SDOperand MipsTargetLowering::
625LowerCCCArguments(SDOperand Op, SelectionDAG &DAG)
626{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000627 SDOperand Root = Op.getOperand(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000628 MachineFunction &MF = DAG.getMachineFunction();
629 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000630 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000631
632 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
633 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
634
635 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000636
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000637 // GP must be live into PIC and non-PIC call target.
638 AddLiveIn(MF, Mips::GP, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000639
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000640 // Assign locations to all of the incoming arguments.
641 SmallVector<CCValAssign, 16> ArgLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000642 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
643
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000644 CCInfo.AnalyzeFormalArguments(Op.Val, CC_Mips);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000645 SmallVector<SDOperand, 16> ArgValues;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000646 SDOperand StackPtr;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000647
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000648 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
649
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000650 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
651
652 CCValAssign &VA = ArgLocs[i];
653
654 // Arguments stored on registers
655 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000656 MVT RegVT = VA.getLocVT();
Bill Wendling06b8c192008-07-09 05:55:53 +0000657 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000658
659 if (RegVT == MVT::i32)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000660 RC = Mips::CPURegsRegisterClass;
661 else if (RegVT == MVT::f32) {
662 if (Subtarget->isSingleFloat())
663 RC = Mips::FGR32RegisterClass;
664 else
665 RC = Mips::AFGR32RegisterClass;
666 } else if (RegVT == MVT::f64) {
667 if (!Subtarget->isSingleFloat())
668 RC = Mips::AFGR64RegisterClass;
669 } else
670 assert(0 && "RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000671
672 // Transform the arguments stored on
673 // physical registers into virtual ones
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000674 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000675 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
676
677 // If this is an 8 or 16-bit value, it is really passed promoted
678 // to 32 bits. Insert an assert[sz]ext to capture this, then
679 // truncate to the right size.
680 if (VA.getLocInfo() == CCValAssign::SExt)
681 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
682 DAG.getValueType(VA.getValVT()));
683 else if (VA.getLocInfo() == CCValAssign::ZExt)
684 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
685 DAG.getValueType(VA.getValVT()));
686
687 if (VA.getLocInfo() != CCValAssign::Full)
688 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
689
690 ArgValues.push_back(ArgValue);
691
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000692 // To meet ABI, when VARARGS are passed on registers, the registers
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000693 // must have their values written to the caller stack frame.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000694 if ((isVarArg) && (Subtarget->isABI_O32())) {
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000695 if (StackPtr.Val == 0)
696 StackPtr = DAG.getRegister(StackReg, getPointerTy());
697
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000698 // The stack pointer offset is relative to the caller stack frame.
699 // Since the real stack size is unknown here, a negative SPOffset
700 // is used so there's a way to adjust these offsets when the stack
701 // size get known (on EliminateFrameIndex). A dummy SPOffset is
702 // used instead of a direct negative address (which is recorded to
703 // be used on emitPrologue) to avoid mis-calc of the first stack
704 // offset on PEI::calculateFrameObjectOffsets.
705 // Arguments are always 32-bit.
706 int FI = MFI->CreateFixedObject(4, 0);
707 MipsFI->recordStoreVarArgsFI(FI, -(4+(i*4)));
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000708 SDOperand PtrOff = DAG.getFrameIndex(FI, getPointerTy());
709
710 // emit ISD::STORE whichs stores the
711 // parameter value to a stack Location
712 ArgValues.push_back(DAG.getStore(Root, ArgValue, PtrOff, NULL, 0));
713 }
714
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000715 } else { // VA.isRegLoc()
716
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000717 // sanity check
718 assert(VA.isMemLoc());
719
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000720 // The stack pointer offset is relative to the caller stack frame.
721 // Since the real stack size is unknown here, a negative SPOffset
722 // is used so there's a way to adjust these offsets when the stack
723 // size get known (on EliminateFrameIndex). A dummy SPOffset is
724 // used instead of a direct negative address (which is recorded to
725 // be used on emitPrologue) to avoid mis-calc of the first stack
726 // offset on PEI::calculateFrameObjectOffsets.
727 // Arguments are always 32-bit.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000728 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
729 int FI = MFI->CreateFixedObject(ArgSize, 0);
730 MipsFI->recordLoadArgsFI(FI, -(ArgSize+
731 (FirstStackArgLoc + VA.getLocMemOffset())));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000732
733 // Create load nodes to retrieve arguments from the stack
734 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
735 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
736 }
737 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000738
739 // The mips ABIs for returning structs by value requires that we copy
740 // the sret argument into $v0 for the return. Save the argument into
741 // a virtual register so that we can access it from the return points.
742 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
743 unsigned Reg = MipsFI->getSRetReturnReg();
744 if (!Reg) {
745 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
746 MipsFI->setSRetReturnReg(Reg);
747 }
748 SDOperand Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
749 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
750 }
751
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000752 ArgValues.push_back(Root);
753
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000754 // Return the new list of results.
Duncan Sandsf9516202008-06-30 10:19:09 +0000755 return DAG.getMergeValues(Op.Val->getVTList(), &ArgValues[0],
756 ArgValues.size()).getValue(Op.ResNo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000757}
758
759//===----------------------------------------------------------------------===//
760// Return Value Calling Convention Implementation
761//===----------------------------------------------------------------------===//
762
763SDOperand MipsTargetLowering::
764LowerRET(SDOperand Op, SelectionDAG &DAG)
765{
766 // CCValAssign - represent the assignment of
767 // the return value to a location
768 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000769 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
770 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000771
772 // CCState - Info about the registers and stack slot.
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000773 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000774
775 // Analize return values of ISD::RET
776 CCInfo.AnalyzeReturn(Op.Val, RetCC_Mips);
777
778 // If this is the first return lowered for this function, add
779 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000780 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000781 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000782 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000783 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000784 }
785
786 // The chain is always operand #0
787 SDOperand Chain = Op.getOperand(0);
788 SDOperand Flag;
789
790 // Copy the result values into the output registers.
791 for (unsigned i = 0; i != RVLocs.size(); ++i) {
792 CCValAssign &VA = RVLocs[i];
793 assert(VA.isRegLoc() && "Can only return in registers!");
794
795 // ISD::RET => ret chain, (regnum1,val1), ...
796 // So i*2+1 index only the regnums
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000797 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000798
799 // guarantee that all emitted copies are
800 // stuck together, avoiding something bad
801 Flag = Chain.getValue(1);
802 }
803
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000804 // The mips ABIs for returning structs by value requires that we copy
805 // the sret argument into $v0 for the return. We saved the argument into
806 // a virtual register in the entry block, so now we copy the value out
807 // and into $v0.
808 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
809 MachineFunction &MF = DAG.getMachineFunction();
810 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
811 unsigned Reg = MipsFI->getSRetReturnReg();
812
813 if (!Reg)
814 assert(0 && "sret virtual register not created in the entry block");
815 SDOperand Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
816
817 Chain = DAG.getCopyToReg(Chain, Mips::V0, Val, Flag);
818 Flag = Chain.getValue(1);
819 }
820
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000821 // Return on Mips is always a "jr $ra"
822 if (Flag.Val)
823 return DAG.getNode(MipsISD::Ret, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000824 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000825 else // Return Void
826 return DAG.getNode(MipsISD::Ret, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000827 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000828}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000829
830//===----------------------------------------------------------------------===//
831// Mips Inline Assembly Support
832//===----------------------------------------------------------------------===//
833
834/// getConstraintType - Given a constraint letter, return the type of
835/// constraint it is for this target.
836MipsTargetLowering::ConstraintType MipsTargetLowering::
837getConstraintType(const std::string &Constraint) const
838{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000839 // Mips specific constrainy
840 // GCC config/mips/constraints.md
841 //
842 // 'd' : An address register. Equivalent to r
843 // unless generating MIPS16 code.
844 // 'y' : Equivalent to r; retained for
845 // backwards compatibility.
Bruno Cardoso Lopes7b76da12008-07-09 04:45:36 +0000846 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000847 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000848 switch (Constraint[0]) {
849 default : break;
850 case 'd':
851 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000852 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000853 return C_RegisterClass;
854 break;
855 }
856 }
857 return TargetLowering::getConstraintType(Constraint);
858}
859
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000860/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
861/// return a list of registers that can be used to satisfy the constraint.
862/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000863std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +0000864getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000865{
866 if (Constraint.size() == 1) {
867 switch (Constraint[0]) {
868 case 'r':
869 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000870 case 'f':
Duncan Sands15126422008-07-08 09:33:14 +0000871 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000872 if (Subtarget->isSingleFloat())
873 return std::make_pair(0U, Mips::FGR32RegisterClass);
874 else
875 return std::make_pair(0U, Mips::AFGR32RegisterClass);
Duncan Sands15126422008-07-08 09:33:14 +0000876 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000877 if (VT == MVT::f64)
878 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
879 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000880 }
881 }
882 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
883}
884
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000885/// Given a register class constraint, like 'r', if this corresponds directly
886/// to an LLVM register class, return a register of 0 and the register class
887/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000888std::vector<unsigned> MipsTargetLowering::
889getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000890 MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000891{
892 if (Constraint.size() != 1)
893 return std::vector<unsigned>();
894
895 switch (Constraint[0]) {
896 default : break;
897 case 'r':
898 // GCC Mips Constraint Letters
899 case 'd':
900 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000901 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
902 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
903 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
904 Mips::T8, 0);
905
906 case 'f':
Duncan Sands15126422008-07-08 09:33:14 +0000907 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000908 if (Subtarget->isSingleFloat())
909 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
910 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
911 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
912 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
913 Mips::F30, Mips::F31, 0);
914 else
915 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
916 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
917 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +0000918 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000919
920 if (VT == MVT::f64)
921 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
922 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
923 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
924 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000925 }
926 return std::vector<unsigned>();
927}