blob: 7fb5390c90ae46240d309b0fca3d0895383d91d1 [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"
23#include "llvm/Intrinsics.h"
24#include "llvm/CallingConv.h"
25#include "llvm/CodeGen/CallingConvLower.h"
26#include "llvm/CodeGen/MachineFrameInfo.h"
27#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000030#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000031#include "llvm/CodeGen/ValueTypes.h"
32#include "llvm/Support/Debug.h"
33#include <queue>
34#include <set>
35
36using namespace llvm;
37
38const char *MipsTargetLowering::
39getTargetNodeName(unsigned Opcode) const
40{
41 switch (Opcode)
42 {
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +000043 case MipsISD::JmpLink : return "MipsISD::JmpLink";
44 case MipsISD::Hi : return "MipsISD::Hi";
45 case MipsISD::Lo : return "MipsISD::Lo";
46 case MipsISD::Ret : return "MipsISD::Ret";
47 case MipsISD::SelectCC : return "MipsISD::SelectCC";
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000048 case MipsISD::FPBrcond : return "MipsISD::FPBrcond";
49 case MipsISD::FPCmp : return "MipsISD::FPCmp";
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +000050 default : return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000051 }
52}
53
54MipsTargetLowering::
55MipsTargetLowering(MipsTargetMachine &TM): TargetLowering(TM)
56{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000057 Subtarget = &TM.getSubtarget<MipsSubtarget>();
58
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000059 // Mips does not have i1 type, so use i32 for
60 // setcc operations results (slt, sgt, ...).
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000061 setSetCCResultContents(ZeroOrOneSetCCResult);
62
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000063 // JumpTable targets must use GOT when using PIC_
64 setUsesGlobalOffsetTable(true);
65
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000066 // Set up the register classes
67 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
68
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000069 // When dealing with single precision only, use libcalls
70 if (!Subtarget->isSingleFloat()) {
71 addRegisterClass(MVT::f32, Mips::AFGR32RegisterClass);
72 if (!Subtarget->isFP64bit())
73 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
74 } else
75 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
76
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000077 // Custom
78 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +000079 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000080 setOperationAction(ISD::RET, MVT::Other, Custom);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000081 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +000082 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000083 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
84
85 if (Subtarget->isSingleFloat())
86 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000087
88 // Load extented operations for i1 types must be promoted
89 setLoadXAction(ISD::EXTLOAD, MVT::i1, Promote);
90 setLoadXAction(ISD::ZEXTLOAD, MVT::i1, Promote);
91 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
92
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000093 // Mips does not have these NodeTypes below.
94 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
95 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
96 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +000097 setOperationAction(ISD::SELECT, MVT::i32, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000098 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000099
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000100 if (!Subtarget->isAllegrex()) {
101 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
102 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
103 }
104
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000105 // Mips not supported intrinsics.
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000106 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000107
108 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
109 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
110 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
111 setOperationAction(ISD::ROTL , MVT::i32, Expand);
112 setOperationAction(ISD::ROTR , MVT::i32, Expand);
113 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
114
115 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
116 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
117 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
118
119 // We don't have line number support yet.
Dan Gohman7f460202008-06-30 20:59:49 +0000120 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000121 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000122 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
123 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000124
125 // Use the default for now
126 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
127 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
128
129 setStackPointerRegisterToSaveRestore(Mips::SP);
130 computeRegisterProperties();
131}
132
133
Duncan Sands83ec4b62008-06-06 12:08:01 +0000134MVT MipsTargetLowering::getSetCCResultType(const SDOperand &) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000135 return MVT::i32;
136}
137
138
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000139SDOperand MipsTargetLowering::
140LowerOperation(SDOperand Op, SelectionDAG &DAG)
141{
142 switch (Op.getOpcode())
143 {
144 case ISD::CALL: return LowerCALL(Op, DAG);
145 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
146 case ISD::RET: return LowerRET(Op, DAG);
147 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000148 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000149 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000150 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000151 }
152 return SDOperand();
153}
154
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000155MachineBasicBlock *
156MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
157 MachineBasicBlock *BB)
158{
159 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
160 switch (MI->getOpcode()) {
161 default: assert(false && "Unexpected instr type to insert");
162 case Mips::Select_CC: {
163 // To "insert" a SELECT_CC instruction, we actually have to insert the
164 // diamond control-flow pattern. The incoming instruction knows the
165 // destination vreg to set, the condition code register to branch on, the
166 // true/false values to select between, and a branch opcode to use.
167 const BasicBlock *LLVM_BB = BB->getBasicBlock();
168 ilist<MachineBasicBlock>::iterator It = BB;
169 ++It;
170
171 // thisMBB:
172 // ...
173 // TrueVal = ...
174 // setcc r1, r2, r3
175 // bNE r1, r0, copy1MBB
176 // fallthrough --> copy0MBB
177 MachineBasicBlock *thisMBB = BB;
178 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
179 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
180 BuildMI(BB, TII->get(Mips::BNE)).addReg(MI->getOperand(1).getReg())
181 .addReg(Mips::ZERO).addMBB(sinkMBB);
182 MachineFunction *F = BB->getParent();
183 F->getBasicBlockList().insert(It, copy0MBB);
184 F->getBasicBlockList().insert(It, sinkMBB);
185 // Update machine-CFG edges by first adding all successors of the current
186 // block to the new block which will contain the Phi node for the select.
187 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
188 e = BB->succ_end(); i != e; ++i)
189 sinkMBB->addSuccessor(*i);
190 // Next, remove all successors of the current block, and add the true
191 // and fallthrough blocks as its successors.
192 while(!BB->succ_empty())
193 BB->removeSuccessor(BB->succ_begin());
194 BB->addSuccessor(copy0MBB);
195 BB->addSuccessor(sinkMBB);
196
197 // copy0MBB:
198 // %FalseValue = ...
199 // # fallthrough to sinkMBB
200 BB = copy0MBB;
201
202 // Update machine-CFG edges
203 BB->addSuccessor(sinkMBB);
204
205 // sinkMBB:
206 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
207 // ...
208 BB = sinkMBB;
209 BuildMI(BB, TII->get(Mips::PHI), MI->getOperand(0).getReg())
210 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
211 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB);
212
213 delete MI; // The pseudo instruction is gone now.
214 return BB;
215 }
216 }
217}
218
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000219//===----------------------------------------------------------------------===//
220// Lower helper functions
221//===----------------------------------------------------------------------===//
222
223// AddLiveIn - This helper function adds the specified physical register to the
224// MachineFunction as a live in value. It also creates a corresponding
225// virtual register for it.
226static unsigned
227AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
228{
229 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000230 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
231 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000232 return VReg;
233}
234
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000235//===----------------------------------------------------------------------===//
236// Misc Lower Operation implementation
237//===----------------------------------------------------------------------===//
238SDOperand MipsTargetLowering::
239LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG)
240{
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000241 SDOperand ResNode;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000242 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000243 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000244 bool isPIC = (getTargetMachine().getRelocationModel() == Reloc::PIC_);
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000245
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000246 SDOperand HiPart;
247 if (!isPIC) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000248 const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000249 SDOperand Ops[] = { GA };
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000250 HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
251 } else // Emit Load from Global Pointer
252 HiPart = DAG.getLoad(MVT::i32, DAG.getEntryNode(), GA, NULL, 0);
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000253
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000254 // On functions and global targets not internal linked only
255 // a load from got/GP is necessary for PIC to work.
256 if ((isPIC) && ((!GV->hasInternalLinkage()) || (isa<Function>(GV))))
257 return HiPart;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000258
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000259 SDOperand Lo = DAG.getNode(MipsISD::Lo, MVT::i32, GA);
260 ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000261
262 return ResNode;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000263}
264
265SDOperand MipsTargetLowering::
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000266LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG)
267{
268 assert(0 && "TLS not implemented for MIPS.");
Chris Lattnerd27c9912008-03-30 18:22:13 +0000269 return SDOperand(); // Not reached
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000270}
271
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000272SDOperand MipsTargetLowering::
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000273LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG)
274{
275 SDOperand LHS = Op.getOperand(0);
276 SDOperand RHS = Op.getOperand(1);
277 SDOperand True = Op.getOperand(2);
278 SDOperand False = Op.getOperand(3);
279 SDOperand CC = Op.getOperand(4);
280
Duncan Sands83ec4b62008-06-06 12:08:01 +0000281 const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000282 SDOperand Ops[] = { LHS, RHS, CC };
283 SDOperand SetCCRes = DAG.getNode(ISD::SETCC, VTs, 1, Ops, 3);
284
285 return DAG.getNode(MipsISD::SelectCC, True.getValueType(),
286 SetCCRes, True, False);
287}
288
289SDOperand MipsTargetLowering::
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000290LowerJumpTable(SDOperand Op, SelectionDAG &DAG)
291{
292 SDOperand ResNode;
293 SDOperand HiPart;
294
Duncan Sands83ec4b62008-06-06 12:08:01 +0000295 MVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000296 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
297 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
298
299 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000300 const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000301 SDOperand Ops[] = { JTI };
302 HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
303 } else // Emit Load from Global Pointer
304 HiPart = DAG.getLoad(MVT::i32, DAG.getEntryNode(), JTI, NULL, 0);
305
306 SDOperand Lo = DAG.getNode(MipsISD::Lo, MVT::i32, JTI);
307 ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
308
309 return ResNode;
310}
311
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000312//===----------------------------------------------------------------------===//
313// Calling Convention Implementation
314//
315// The lower operations present on calling convention works on this order:
316// LowerCALL (virt regs --> phys regs, virt regs --> stack)
317// LowerFORMAL_ARGUMENTS (phys --> virt regs, stack --> virt regs)
318// LowerRET (virt regs --> phys regs)
319// LowerCALL (phys regs --> virt regs)
320//
321//===----------------------------------------------------------------------===//
322
323#include "MipsGenCallingConv.inc"
324
325//===----------------------------------------------------------------------===//
326// CALL Calling Convention Implementation
327//===----------------------------------------------------------------------===//
328
329/// Mips custom CALL implementation
330SDOperand MipsTargetLowering::
331LowerCALL(SDOperand Op, SelectionDAG &DAG)
332{
Chris Lattnere0b12152008-03-17 06:57:02 +0000333 unsigned CallingConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000334
335 // By now, only CallingConv::C implemented
Chris Lattnere0b12152008-03-17 06:57:02 +0000336 switch (CallingConv) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000337 default:
338 assert(0 && "Unsupported calling convention");
339 case CallingConv::Fast:
340 case CallingConv::C:
341 return LowerCCCCallTo(Op, DAG, CallingConv);
342 }
343}
344
345/// LowerCCCCallTo - functions arguments are copied from virtual
346/// regs to (physical regs)/(stack frame), CALLSEQ_START and
347/// CALLSEQ_END are emitted.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000348/// TODO: isVarArg, isTailCall.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000349SDOperand MipsTargetLowering::
350LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG, unsigned CC)
351{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000352 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000353
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000354 SDOperand Chain = Op.getOperand(0);
355 SDOperand Callee = Op.getOperand(4);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000356 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
357
358 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000359
360 // Analyze operands of the call, assigning locations to each operand.
361 SmallVector<CCValAssign, 16> ArgLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000362 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
363
364 // To meet ABI, Mips must always allocate 16 bytes on
365 // the stack (even if less than 4 are used as arguments)
Duncan Sands83ec4b62008-06-06 12:08:01 +0000366 int VTsize = MVT(MVT::i32).getSizeInBits()/8;
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000367 MFI->CreateFixedObject(VTsize, (VTsize*3));
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000368
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000369 CCInfo.AnalyzeCallOperands(Op.Val, CC_Mips);
370
371 // Get a count of how many bytes are to be pushed on the stack.
372 unsigned NumBytes = CCInfo.getNextStackOffset();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000373 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes,
374 getPointerTy()));
375
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000376 // With EABI is it possible to have 16 args on registers.
377 SmallVector<std::pair<unsigned, SDOperand>, 16> RegsToPass;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000378 SmallVector<SDOperand, 8> MemOpChains;
379
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000380 // First/LastArgStackLoc contains the first/last
381 // "at stack" argument location.
382 int LastArgStackLoc = 0;
383 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000384
385 // Walk the register/memloc assignments, inserting copies/loads.
386 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
387 CCValAssign &VA = ArgLocs[i];
388
389 // Arguments start after the 5 first operands of ISD::CALL
390 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
391
392 // Promote the value if needed.
393 switch (VA.getLocInfo()) {
Chris Lattnere0b12152008-03-17 06:57:02 +0000394 default: assert(0 && "Unknown loc info!");
395 case CCValAssign::Full: break;
396 case CCValAssign::SExt:
397 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
398 break;
399 case CCValAssign::ZExt:
400 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
401 break;
402 case CCValAssign::AExt:
403 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
404 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000405 }
406
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000407 // Arguments that can be passed on register must be kept at
408 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000409 if (VA.isRegLoc()) {
410 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +0000411 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000412 }
Chris Lattnere0b12152008-03-17 06:57:02 +0000413
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000414 // Register cant get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +0000415 assert(VA.isMemLoc());
416
417 // Create the frame index object for this incoming parameter
418 // This guarantees that when allocating Local Area the firsts
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000419 // 16 bytes which are alwayes reserved won't be overwritten
420 // if O32 ABI is used. For EABI the first address is zero.
421 LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000422 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000423 LastArgStackLoc);
Chris Lattnere0b12152008-03-17 06:57:02 +0000424
425 SDOperand PtrOff = DAG.getFrameIndex(FI,getPointerTy());
426
427 // emit ISD::STORE whichs stores the
428 // parameter value to a stack Location
429 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000430 }
431
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000432 // Transform all store nodes into one single node because all store
433 // nodes are independent of each other.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000434 if (!MemOpChains.empty())
435 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
436 &MemOpChains[0], MemOpChains.size());
437
438 // Build a sequence of copy-to-reg nodes chained together with token
439 // chain and flag operands which copy the outgoing args into registers.
440 // The InFlag in necessary since all emited instructions must be
441 // stuck together.
442 SDOperand InFlag;
443 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
444 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first,
445 RegsToPass[i].second, InFlag);
446 InFlag = Chain.getValue(1);
447 }
448
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000449 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
450 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000451 // node so that legalize doesn't hack it.
452 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000453 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000454 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000455 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
456
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000457
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000458 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
459 // = Chain, Callee, Reg#1, Reg#2, ...
460 //
461 // Returns a chain & a flag for retval copy to use.
462 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
463 SmallVector<SDOperand, 8> Ops;
464 Ops.push_back(Chain);
465 Ops.push_back(Callee);
466
467 // Add argument registers to the end of the list so that they are
468 // known live into the call.
469 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
470 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
471 RegsToPass[i].second.getValueType()));
472
473 if (InFlag.Val)
474 Ops.push_back(InFlag);
475
476 Chain = DAG.getNode(MipsISD::JmpLink, NodeTys, &Ops[0], Ops.size());
477 InFlag = Chain.getValue(1);
478
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000479 // Create the CALLSEQ_END node.
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000480 Chain = DAG.getCALLSEQ_END(Chain,
481 DAG.getConstant(NumBytes, getPointerTy()),
482 DAG.getConstant(0, getPointerTy()),
483 InFlag);
484 InFlag = Chain.getValue(1);
485
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000486 // Create a stack location to hold GP when PIC is used. This stack
487 // location is used on function prologue to save GP and also after all
488 // emited CALL's to restore GP.
489 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000490 // Function can have an arbitrary number of calls, so
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000491 // hold the LastArgStackLoc with the biggest offset.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000492 int FI;
493 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000494 if (LastArgStackLoc >= MipsFI->getGPStackOffset()) {
495 LastArgStackLoc = (!LastArgStackLoc) ? (16) : (LastArgStackLoc+4);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000496 // Create the frame index only once. SPOffset here can be anything
497 // (this will be fixed on processFunctionBeforeFrameFinalized)
498 if (MipsFI->getGPStackOffset() == -1) {
499 FI = MFI->CreateFixedObject(4, 0);
500 MipsFI->setGPFI(FI);
501 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000502 MipsFI->setGPStackOffset(LastArgStackLoc);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000503 }
504
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000505 // Reload GP value.
506 FI = MipsFI->getGPFI();
507 SDOperand FIN = DAG.getFrameIndex(FI,getPointerTy());
508 SDOperand GPLoad = DAG.getLoad(MVT::i32, Chain, FIN, NULL, 0);
509 Chain = GPLoad.getValue(1);
510 Chain = DAG.getCopyToReg(Chain, DAG.getRegister(Mips::GP, MVT::i32),
511 GPLoad, SDOperand(0,0));
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000512 InFlag = Chain.getValue(1);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000513 }
514
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000515 // Handle result values, copying them out of physregs into vregs that we
516 // return.
517 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
518}
519
520/// LowerCallResult - Lower the result values of an ISD::CALL into the
521/// appropriate copies out of appropriate physical registers. This assumes that
522/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
523/// being lowered. Returns a SDNode with the same number of values as the
524/// ISD::CALL.
525SDNode *MipsTargetLowering::
526LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
527 unsigned CallingConv, SelectionDAG &DAG) {
528
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000529 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
530
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000531 // Assign locations to each value returned by this call.
532 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000533 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
534
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000535 CCInfo.AnalyzeCallResult(TheCall, RetCC_Mips);
536 SmallVector<SDOperand, 8> ResultVals;
537
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000538 // Copy all of the result registers out of their specified physreg.
539 for (unsigned i = 0; i != RVLocs.size(); ++i) {
540 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
541 RVLocs[i].getValVT(), InFlag).getValue(1);
542 InFlag = Chain.getValue(2);
543 ResultVals.push_back(Chain.getValue(0));
544 }
545
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000546 ResultVals.push_back(Chain);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000547
548 // Merge everything together with a MERGE_VALUES node.
Duncan Sandsf9516202008-06-30 10:19:09 +0000549 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
550 ResultVals.size()).Val;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000551}
552
553//===----------------------------------------------------------------------===//
554// FORMAL_ARGUMENTS Calling Convention Implementation
555//===----------------------------------------------------------------------===//
556
557/// Mips custom FORMAL_ARGUMENTS implementation
558SDOperand MipsTargetLowering::
559LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG)
560{
561 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
562 switch(CC)
563 {
564 default:
565 assert(0 && "Unsupported calling convention");
566 case CallingConv::C:
567 return LowerCCCArguments(Op, DAG);
568 }
569}
570
571/// LowerCCCArguments - transform physical registers into
572/// virtual registers and generate load operations for
573/// arguments places on the stack.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000574/// TODO: isVarArg
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000575SDOperand MipsTargetLowering::
576LowerCCCArguments(SDOperand Op, SelectionDAG &DAG)
577{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000578 SDOperand Root = Op.getOperand(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000579 MachineFunction &MF = DAG.getMachineFunction();
580 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000581 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000582
583 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
584 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
585
586 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000587
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000588 // GP holds the GOT address on PIC calls.
589 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
590 AddLiveIn(MF, Mips::GP, Mips::CPURegsRegisterClass);
591
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000592 // Assign locations to all of the incoming arguments.
593 SmallVector<CCValAssign, 16> ArgLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000594 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
595
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000596 CCInfo.AnalyzeFormalArguments(Op.Val, CC_Mips);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000597 SmallVector<SDOperand, 16> ArgValues;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000598 SDOperand StackPtr;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000599
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000600 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
601
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000602 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
603
604 CCValAssign &VA = ArgLocs[i];
605
606 // Arguments stored on registers
607 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000608 MVT RegVT = VA.getLocVT();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000609 TargetRegisterClass *RC;
610
611 if (RegVT == MVT::i32)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000612 RC = Mips::CPURegsRegisterClass;
613 else if (RegVT == MVT::f32) {
614 if (Subtarget->isSingleFloat())
615 RC = Mips::FGR32RegisterClass;
616 else
617 RC = Mips::AFGR32RegisterClass;
618 } else if (RegVT == MVT::f64) {
619 if (!Subtarget->isSingleFloat())
620 RC = Mips::AFGR64RegisterClass;
621 } else
622 assert(0 && "RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000623
624 // Transform the arguments stored on
625 // physical registers into virtual ones
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000626 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000627 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
628
629 // If this is an 8 or 16-bit value, it is really passed promoted
630 // to 32 bits. Insert an assert[sz]ext to capture this, then
631 // truncate to the right size.
632 if (VA.getLocInfo() == CCValAssign::SExt)
633 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
634 DAG.getValueType(VA.getValVT()));
635 else if (VA.getLocInfo() == CCValAssign::ZExt)
636 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
637 DAG.getValueType(VA.getValVT()));
638
639 if (VA.getLocInfo() != CCValAssign::Full)
640 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
641
642 ArgValues.push_back(ArgValue);
643
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000644 // To meet ABI, when VARARGS are passed on registers, the registers
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000645 // must have their values written to the caller stack frame.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000646 if ((isVarArg) && (Subtarget->isABI_O32())) {
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000647 if (StackPtr.Val == 0)
648 StackPtr = DAG.getRegister(StackReg, getPointerTy());
649
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000650 // The stack pointer offset is relative to the caller stack frame.
651 // Since the real stack size is unknown here, a negative SPOffset
652 // is used so there's a way to adjust these offsets when the stack
653 // size get known (on EliminateFrameIndex). A dummy SPOffset is
654 // used instead of a direct negative address (which is recorded to
655 // be used on emitPrologue) to avoid mis-calc of the first stack
656 // offset on PEI::calculateFrameObjectOffsets.
657 // Arguments are always 32-bit.
658 int FI = MFI->CreateFixedObject(4, 0);
659 MipsFI->recordStoreVarArgsFI(FI, -(4+(i*4)));
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000660 SDOperand PtrOff = DAG.getFrameIndex(FI, getPointerTy());
661
662 // emit ISD::STORE whichs stores the
663 // parameter value to a stack Location
664 ArgValues.push_back(DAG.getStore(Root, ArgValue, PtrOff, NULL, 0));
665 }
666
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000667 } else { // VA.isRegLoc()
668
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000669 // sanity check
670 assert(VA.isMemLoc());
671
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000672 // The stack pointer offset is relative to the caller stack frame.
673 // Since the real stack size is unknown here, a negative SPOffset
674 // is used so there's a way to adjust these offsets when the stack
675 // size get known (on EliminateFrameIndex). A dummy SPOffset is
676 // used instead of a direct negative address (which is recorded to
677 // be used on emitPrologue) to avoid mis-calc of the first stack
678 // offset on PEI::calculateFrameObjectOffsets.
679 // Arguments are always 32-bit.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000680 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
681 int FI = MFI->CreateFixedObject(ArgSize, 0);
682 MipsFI->recordLoadArgsFI(FI, -(ArgSize+
683 (FirstStackArgLoc + VA.getLocMemOffset())));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000684
685 // Create load nodes to retrieve arguments from the stack
686 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
687 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
688 }
689 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000690
691 // The mips ABIs for returning structs by value requires that we copy
692 // the sret argument into $v0 for the return. Save the argument into
693 // a virtual register so that we can access it from the return points.
694 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
695 unsigned Reg = MipsFI->getSRetReturnReg();
696 if (!Reg) {
697 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
698 MipsFI->setSRetReturnReg(Reg);
699 }
700 SDOperand Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
701 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
702 }
703
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000704 ArgValues.push_back(Root);
705
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000706 // Return the new list of results.
Duncan Sandsf9516202008-06-30 10:19:09 +0000707 return DAG.getMergeValues(Op.Val->getVTList(), &ArgValues[0],
708 ArgValues.size()).getValue(Op.ResNo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000709}
710
711//===----------------------------------------------------------------------===//
712// Return Value Calling Convention Implementation
713//===----------------------------------------------------------------------===//
714
715SDOperand MipsTargetLowering::
716LowerRET(SDOperand Op, SelectionDAG &DAG)
717{
718 // CCValAssign - represent the assignment of
719 // the return value to a location
720 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000721 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
722 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000723
724 // CCState - Info about the registers and stack slot.
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000725 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000726
727 // Analize return values of ISD::RET
728 CCInfo.AnalyzeReturn(Op.Val, RetCC_Mips);
729
730 // If this is the first return lowered for this function, add
731 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000732 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000733 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000734 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000735 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000736 }
737
738 // The chain is always operand #0
739 SDOperand Chain = Op.getOperand(0);
740 SDOperand Flag;
741
742 // Copy the result values into the output registers.
743 for (unsigned i = 0; i != RVLocs.size(); ++i) {
744 CCValAssign &VA = RVLocs[i];
745 assert(VA.isRegLoc() && "Can only return in registers!");
746
747 // ISD::RET => ret chain, (regnum1,val1), ...
748 // So i*2+1 index only the regnums
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000749 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000750
751 // guarantee that all emitted copies are
752 // stuck together, avoiding something bad
753 Flag = Chain.getValue(1);
754 }
755
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000756 // The mips ABIs for returning structs by value requires that we copy
757 // the sret argument into $v0 for the return. We saved the argument into
758 // a virtual register in the entry block, so now we copy the value out
759 // and into $v0.
760 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
761 MachineFunction &MF = DAG.getMachineFunction();
762 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
763 unsigned Reg = MipsFI->getSRetReturnReg();
764
765 if (!Reg)
766 assert(0 && "sret virtual register not created in the entry block");
767 SDOperand Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
768
769 Chain = DAG.getCopyToReg(Chain, Mips::V0, Val, Flag);
770 Flag = Chain.getValue(1);
771 }
772
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000773 // Return on Mips is always a "jr $ra"
774 if (Flag.Val)
775 return DAG.getNode(MipsISD::Ret, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000776 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000777 else // Return Void
778 return DAG.getNode(MipsISD::Ret, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000779 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000780}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000781
782//===----------------------------------------------------------------------===//
783// Mips Inline Assembly Support
784//===----------------------------------------------------------------------===//
785
786/// getConstraintType - Given a constraint letter, return the type of
787/// constraint it is for this target.
788MipsTargetLowering::ConstraintType MipsTargetLowering::
789getConstraintType(const std::string &Constraint) const
790{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000791 // Mips specific constrainy
792 // GCC config/mips/constraints.md
793 //
794 // 'd' : An address register. Equivalent to r
795 // unless generating MIPS16 code.
796 // 'y' : Equivalent to r; retained for
797 // backwards compatibility.
798 // 'f' : Float Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000799 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000800 switch (Constraint[0]) {
801 default : break;
802 case 'd':
803 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000804 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000805 return C_RegisterClass;
806 break;
807 }
808 }
809 return TargetLowering::getConstraintType(Constraint);
810}
811
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000812/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
813/// return a list of registers that can be used to satisfy the constraint.
814/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000815std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +0000816getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000817{
818 if (Constraint.size() == 1) {
819 switch (Constraint[0]) {
820 case 'r':
821 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000822 case 'f':
823 if (VT == MVT::f32)
824 if (Subtarget->isSingleFloat())
825 return std::make_pair(0U, Mips::FGR32RegisterClass);
826 else
827 return std::make_pair(0U, Mips::AFGR32RegisterClass);
828 if (VT == MVT::f64)
829 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
830 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000831 }
832 }
833 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
834}
835
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000836/// Given a register class constraint, like 'r', if this corresponds directly
837/// to an LLVM register class, return a register of 0 and the register class
838/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000839std::vector<unsigned> MipsTargetLowering::
840getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000841 MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000842{
843 if (Constraint.size() != 1)
844 return std::vector<unsigned>();
845
846 switch (Constraint[0]) {
847 default : break;
848 case 'r':
849 // GCC Mips Constraint Letters
850 case 'd':
851 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000852 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
853 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
854 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
855 Mips::T8, 0);
856
857 case 'f':
858 if (VT == MVT::f32)
859 if (Subtarget->isSingleFloat())
860 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
861 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
862 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
863 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
864 Mips::F30, Mips::F31, 0);
865 else
866 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
867 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
868 Mips::F28, Mips::F30, 0);
869
870 if (VT == MVT::f64)
871 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
872 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
873 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
874 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000875 }
876 return std::vector<unsigned>();
877}