blob: efc5d9bf8d62c62bc6e54fbdb7dbaecb2b1783bd [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 // Load extented operations for i1 types must be promoted
78 setLoadXAction(ISD::EXTLOAD, MVT::i1, Promote);
79 setLoadXAction(ISD::ZEXTLOAD, MVT::i1, Promote);
80 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
81
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000082 // Mips Custom Operations
83 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
84 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
85 setOperationAction(ISD::RET, MVT::Other, Custom);
86 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
87 setOperationAction(ISD::ConstantPool, MVT::f32, Custom);
88 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
89 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
90
91 // Operations not directly supported by Mips.
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +000092 setConvertAction(MVT::f64, MVT::f32, Expand);
93
94 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 Lopes972f5892007-06-06 07:42:06 +0000237//===----------------------------------------------------------------------===//
238// Misc Lower Operation implementation
239//===----------------------------------------------------------------------===//
240SDOperand MipsTargetLowering::
241LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG)
242{
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000243 SDOperand ResNode;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000244 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000245 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000246 bool isPIC = (getTargetMachine().getRelocationModel() == Reloc::PIC_);
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000247
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000248 SDOperand HiPart;
249 if (!isPIC) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000250 const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000251 SDOperand Ops[] = { GA };
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000252 HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
253 } else // Emit Load from Global Pointer
254 HiPart = DAG.getLoad(MVT::i32, DAG.getEntryNode(), GA, NULL, 0);
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000255
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000256 // On functions and global targets not internal linked only
257 // a load from got/GP is necessary for PIC to work.
258 if ((isPIC) && ((!GV->hasInternalLinkage()) || (isa<Function>(GV))))
259 return HiPart;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000260
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000261 SDOperand Lo = DAG.getNode(MipsISD::Lo, MVT::i32, GA);
262 ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000263
264 return ResNode;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000265}
266
267SDOperand MipsTargetLowering::
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000268LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG)
269{
270 assert(0 && "TLS not implemented for MIPS.");
Chris Lattnerd27c9912008-03-30 18:22:13 +0000271 return SDOperand(); // Not reached
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000272}
273
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000274SDOperand MipsTargetLowering::
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000275LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG)
276{
277 SDOperand LHS = Op.getOperand(0);
278 SDOperand RHS = Op.getOperand(1);
279 SDOperand True = Op.getOperand(2);
280 SDOperand False = Op.getOperand(3);
281 SDOperand CC = Op.getOperand(4);
282
Duncan Sands83ec4b62008-06-06 12:08:01 +0000283 const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000284 SDOperand Ops[] = { LHS, RHS, CC };
285 SDOperand SetCCRes = DAG.getNode(ISD::SETCC, VTs, 1, Ops, 3);
286
287 return DAG.getNode(MipsISD::SelectCC, True.getValueType(),
288 SetCCRes, True, False);
289}
290
291SDOperand MipsTargetLowering::
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000292LowerJumpTable(SDOperand Op, SelectionDAG &DAG)
293{
294 SDOperand ResNode;
295 SDOperand HiPart;
296
Duncan Sands83ec4b62008-06-06 12:08:01 +0000297 MVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000298 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
299 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
300
301 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000302 const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000303 SDOperand Ops[] = { JTI };
304 HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
305 } else // Emit Load from Global Pointer
306 HiPart = DAG.getLoad(MVT::i32, DAG.getEntryNode(), JTI, NULL, 0);
307
308 SDOperand Lo = DAG.getNode(MipsISD::Lo, MVT::i32, JTI);
309 ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
310
311 return ResNode;
312}
313
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000314SDOperand MipsTargetLowering::
315LowerConstantPool(SDOperand Op, SelectionDAG &DAG)
316{
317 assert(0 && "ConstantPool not implemented for MIPS.");
318 return SDOperand(); // Not reached
319}
320
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000321//===----------------------------------------------------------------------===//
322// Calling Convention Implementation
323//
324// The lower operations present on calling convention works on this order:
325// LowerCALL (virt regs --> phys regs, virt regs --> stack)
326// LowerFORMAL_ARGUMENTS (phys --> virt regs, stack --> virt regs)
327// LowerRET (virt regs --> phys regs)
328// LowerCALL (phys regs --> virt regs)
329//
330//===----------------------------------------------------------------------===//
331
332#include "MipsGenCallingConv.inc"
333
334//===----------------------------------------------------------------------===//
335// CALL Calling Convention Implementation
336//===----------------------------------------------------------------------===//
337
338/// Mips custom CALL implementation
339SDOperand MipsTargetLowering::
340LowerCALL(SDOperand Op, SelectionDAG &DAG)
341{
Chris Lattnere0b12152008-03-17 06:57:02 +0000342 unsigned CallingConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000343
344 // By now, only CallingConv::C implemented
Chris Lattnere0b12152008-03-17 06:57:02 +0000345 switch (CallingConv) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000346 default:
347 assert(0 && "Unsupported calling convention");
348 case CallingConv::Fast:
349 case CallingConv::C:
350 return LowerCCCCallTo(Op, DAG, CallingConv);
351 }
352}
353
354/// LowerCCCCallTo - functions arguments are copied from virtual
355/// regs to (physical regs)/(stack frame), CALLSEQ_START and
356/// CALLSEQ_END are emitted.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000357/// TODO: isVarArg, isTailCall.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000358SDOperand MipsTargetLowering::
359LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG, unsigned CC)
360{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000361 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000362
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000363 SDOperand Chain = Op.getOperand(0);
364 SDOperand Callee = Op.getOperand(4);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000365 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
366
367 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000368
369 // Analyze operands of the call, assigning locations to each operand.
370 SmallVector<CCValAssign, 16> ArgLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000371 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
372
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000373 // To meet O32 ABI, Mips must always allocate 16 bytes on
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000374 // the stack (even if less than 4 are used as arguments)
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000375 if (Subtarget->isABI_O32()) {
376 int VTsize = MVT(MVT::i32).getSizeInBits()/8;
377 MFI->CreateFixedObject(VTsize, (VTsize*3));
378 }
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000379
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000380 CCInfo.AnalyzeCallOperands(Op.Val, CC_Mips);
381
382 // Get a count of how many bytes are to be pushed on the stack.
383 unsigned NumBytes = CCInfo.getNextStackOffset();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000384 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes,
385 getPointerTy()));
386
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000387 // With EABI is it possible to have 16 args on registers.
388 SmallVector<std::pair<unsigned, SDOperand>, 16> RegsToPass;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000389 SmallVector<SDOperand, 8> MemOpChains;
390
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000391 // First/LastArgStackLoc contains the first/last
392 // "at stack" argument location.
393 int LastArgStackLoc = 0;
394 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000395
396 // Walk the register/memloc assignments, inserting copies/loads.
397 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
398 CCValAssign &VA = ArgLocs[i];
399
400 // Arguments start after the 5 first operands of ISD::CALL
401 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
402
403 // Promote the value if needed.
404 switch (VA.getLocInfo()) {
Chris Lattnere0b12152008-03-17 06:57:02 +0000405 default: assert(0 && "Unknown loc info!");
406 case CCValAssign::Full: break;
407 case CCValAssign::SExt:
408 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
409 break;
410 case CCValAssign::ZExt:
411 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
412 break;
413 case CCValAssign::AExt:
414 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
415 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000416 }
417
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000418 // Arguments that can be passed on register must be kept at
419 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000420 if (VA.isRegLoc()) {
421 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +0000422 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000423 }
Chris Lattnere0b12152008-03-17 06:57:02 +0000424
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000425 // Register cant get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +0000426 assert(VA.isMemLoc());
427
428 // Create the frame index object for this incoming parameter
429 // This guarantees that when allocating Local Area the firsts
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000430 // 16 bytes which are alwayes reserved won't be overwritten
431 // if O32 ABI is used. For EABI the first address is zero.
432 LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000433 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000434 LastArgStackLoc);
Chris Lattnere0b12152008-03-17 06:57:02 +0000435
436 SDOperand PtrOff = DAG.getFrameIndex(FI,getPointerTy());
437
438 // emit ISD::STORE whichs stores the
439 // parameter value to a stack Location
440 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000441 }
442
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000443 // Transform all store nodes into one single node because all store
444 // nodes are independent of each other.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000445 if (!MemOpChains.empty())
446 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
447 &MemOpChains[0], MemOpChains.size());
448
449 // Build a sequence of copy-to-reg nodes chained together with token
450 // chain and flag operands which copy the outgoing args into registers.
451 // The InFlag in necessary since all emited instructions must be
452 // stuck together.
453 SDOperand InFlag;
454 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
455 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first,
456 RegsToPass[i].second, InFlag);
457 InFlag = Chain.getValue(1);
458 }
459
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000460 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
461 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000462 // node so that legalize doesn't hack it.
463 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000464 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000465 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000466 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
467
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000468
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000469 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
470 // = Chain, Callee, Reg#1, Reg#2, ...
471 //
472 // Returns a chain & a flag for retval copy to use.
473 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
474 SmallVector<SDOperand, 8> Ops;
475 Ops.push_back(Chain);
476 Ops.push_back(Callee);
477
478 // Add argument registers to the end of the list so that they are
479 // known live into the call.
480 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
481 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
482 RegsToPass[i].second.getValueType()));
483
484 if (InFlag.Val)
485 Ops.push_back(InFlag);
486
487 Chain = DAG.getNode(MipsISD::JmpLink, NodeTys, &Ops[0], Ops.size());
488 InFlag = Chain.getValue(1);
489
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000490 // Create the CALLSEQ_END node.
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000491 Chain = DAG.getCALLSEQ_END(Chain,
492 DAG.getConstant(NumBytes, getPointerTy()),
493 DAG.getConstant(0, getPointerTy()),
494 InFlag);
495 InFlag = Chain.getValue(1);
496
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000497 // Create a stack location to hold GP when PIC is used. This stack
498 // location is used on function prologue to save GP and also after all
499 // emited CALL's to restore GP.
500 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000501 // Function can have an arbitrary number of calls, so
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000502 // hold the LastArgStackLoc with the biggest offset.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000503 int FI;
504 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000505 if (LastArgStackLoc >= MipsFI->getGPStackOffset()) {
506 LastArgStackLoc = (!LastArgStackLoc) ? (16) : (LastArgStackLoc+4);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000507 // Create the frame index only once. SPOffset here can be anything
508 // (this will be fixed on processFunctionBeforeFrameFinalized)
509 if (MipsFI->getGPStackOffset() == -1) {
510 FI = MFI->CreateFixedObject(4, 0);
511 MipsFI->setGPFI(FI);
512 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000513 MipsFI->setGPStackOffset(LastArgStackLoc);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000514 }
515
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000516 // Reload GP value.
517 FI = MipsFI->getGPFI();
518 SDOperand FIN = DAG.getFrameIndex(FI,getPointerTy());
519 SDOperand GPLoad = DAG.getLoad(MVT::i32, Chain, FIN, NULL, 0);
520 Chain = GPLoad.getValue(1);
521 Chain = DAG.getCopyToReg(Chain, DAG.getRegister(Mips::GP, MVT::i32),
522 GPLoad, SDOperand(0,0));
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000523 InFlag = Chain.getValue(1);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000524 }
525
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000526 // Handle result values, copying them out of physregs into vregs that we
527 // return.
528 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
529}
530
531/// LowerCallResult - Lower the result values of an ISD::CALL into the
532/// appropriate copies out of appropriate physical registers. This assumes that
533/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
534/// being lowered. Returns a SDNode with the same number of values as the
535/// ISD::CALL.
536SDNode *MipsTargetLowering::
537LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
538 unsigned CallingConv, SelectionDAG &DAG) {
539
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000540 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
541
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000542 // Assign locations to each value returned by this call.
543 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000544 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
545
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000546 CCInfo.AnalyzeCallResult(TheCall, RetCC_Mips);
547 SmallVector<SDOperand, 8> ResultVals;
548
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000549 // Copy all of the result registers out of their specified physreg.
550 for (unsigned i = 0; i != RVLocs.size(); ++i) {
551 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
552 RVLocs[i].getValVT(), InFlag).getValue(1);
553 InFlag = Chain.getValue(2);
554 ResultVals.push_back(Chain.getValue(0));
555 }
556
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000557 ResultVals.push_back(Chain);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000558
559 // Merge everything together with a MERGE_VALUES node.
Duncan Sandsf9516202008-06-30 10:19:09 +0000560 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
561 ResultVals.size()).Val;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000562}
563
564//===----------------------------------------------------------------------===//
565// FORMAL_ARGUMENTS Calling Convention Implementation
566//===----------------------------------------------------------------------===//
567
568/// Mips custom FORMAL_ARGUMENTS implementation
569SDOperand MipsTargetLowering::
570LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG)
571{
572 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
573 switch(CC)
574 {
575 default:
576 assert(0 && "Unsupported calling convention");
577 case CallingConv::C:
578 return LowerCCCArguments(Op, DAG);
579 }
580}
581
582/// LowerCCCArguments - transform physical registers into
583/// virtual registers and generate load operations for
584/// arguments places on the stack.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000585/// TODO: isVarArg
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000586SDOperand MipsTargetLowering::
587LowerCCCArguments(SDOperand Op, SelectionDAG &DAG)
588{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000589 SDOperand Root = Op.getOperand(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000590 MachineFunction &MF = DAG.getMachineFunction();
591 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000592 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000593
594 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
595 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
596
597 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000598
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000599 // GP holds the GOT address on PIC calls.
600 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
601 AddLiveIn(MF, Mips::GP, Mips::CPURegsRegisterClass);
602
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000603 // Assign locations to all of the incoming arguments.
604 SmallVector<CCValAssign, 16> ArgLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000605 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
606
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000607 CCInfo.AnalyzeFormalArguments(Op.Val, CC_Mips);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000608 SmallVector<SDOperand, 16> ArgValues;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000609 SDOperand StackPtr;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000610
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000611 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
612
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000613 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
614
615 CCValAssign &VA = ArgLocs[i];
616
617 // Arguments stored on registers
618 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000619 MVT RegVT = VA.getLocVT();
Bill Wendling06b8c192008-07-09 05:55:53 +0000620 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000621
622 if (RegVT == MVT::i32)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000623 RC = Mips::CPURegsRegisterClass;
624 else if (RegVT == MVT::f32) {
625 if (Subtarget->isSingleFloat())
626 RC = Mips::FGR32RegisterClass;
627 else
628 RC = Mips::AFGR32RegisterClass;
629 } else if (RegVT == MVT::f64) {
630 if (!Subtarget->isSingleFloat())
631 RC = Mips::AFGR64RegisterClass;
632 } else
633 assert(0 && "RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000634
635 // Transform the arguments stored on
636 // physical registers into virtual ones
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000637 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000638 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
639
640 // If this is an 8 or 16-bit value, it is really passed promoted
641 // to 32 bits. Insert an assert[sz]ext to capture this, then
642 // truncate to the right size.
643 if (VA.getLocInfo() == CCValAssign::SExt)
644 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
645 DAG.getValueType(VA.getValVT()));
646 else if (VA.getLocInfo() == CCValAssign::ZExt)
647 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
648 DAG.getValueType(VA.getValVT()));
649
650 if (VA.getLocInfo() != CCValAssign::Full)
651 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
652
653 ArgValues.push_back(ArgValue);
654
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000655 // To meet ABI, when VARARGS are passed on registers, the registers
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000656 // must have their values written to the caller stack frame.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000657 if ((isVarArg) && (Subtarget->isABI_O32())) {
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000658 if (StackPtr.Val == 0)
659 StackPtr = DAG.getRegister(StackReg, getPointerTy());
660
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000661 // The stack pointer offset is relative to the caller stack frame.
662 // Since the real stack size is unknown here, a negative SPOffset
663 // is used so there's a way to adjust these offsets when the stack
664 // size get known (on EliminateFrameIndex). A dummy SPOffset is
665 // used instead of a direct negative address (which is recorded to
666 // be used on emitPrologue) to avoid mis-calc of the first stack
667 // offset on PEI::calculateFrameObjectOffsets.
668 // Arguments are always 32-bit.
669 int FI = MFI->CreateFixedObject(4, 0);
670 MipsFI->recordStoreVarArgsFI(FI, -(4+(i*4)));
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000671 SDOperand PtrOff = DAG.getFrameIndex(FI, getPointerTy());
672
673 // emit ISD::STORE whichs stores the
674 // parameter value to a stack Location
675 ArgValues.push_back(DAG.getStore(Root, ArgValue, PtrOff, NULL, 0));
676 }
677
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000678 } else { // VA.isRegLoc()
679
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000680 // sanity check
681 assert(VA.isMemLoc());
682
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000683 // The stack pointer offset is relative to the caller stack frame.
684 // Since the real stack size is unknown here, a negative SPOffset
685 // is used so there's a way to adjust these offsets when the stack
686 // size get known (on EliminateFrameIndex). A dummy SPOffset is
687 // used instead of a direct negative address (which is recorded to
688 // be used on emitPrologue) to avoid mis-calc of the first stack
689 // offset on PEI::calculateFrameObjectOffsets.
690 // Arguments are always 32-bit.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000691 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
692 int FI = MFI->CreateFixedObject(ArgSize, 0);
693 MipsFI->recordLoadArgsFI(FI, -(ArgSize+
694 (FirstStackArgLoc + VA.getLocMemOffset())));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000695
696 // Create load nodes to retrieve arguments from the stack
697 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
698 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
699 }
700 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000701
702 // The mips ABIs for returning structs by value requires that we copy
703 // the sret argument into $v0 for the return. Save the argument into
704 // a virtual register so that we can access it from the return points.
705 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
706 unsigned Reg = MipsFI->getSRetReturnReg();
707 if (!Reg) {
708 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
709 MipsFI->setSRetReturnReg(Reg);
710 }
711 SDOperand Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
712 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
713 }
714
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000715 ArgValues.push_back(Root);
716
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000717 // Return the new list of results.
Duncan Sandsf9516202008-06-30 10:19:09 +0000718 return DAG.getMergeValues(Op.Val->getVTList(), &ArgValues[0],
719 ArgValues.size()).getValue(Op.ResNo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000720}
721
722//===----------------------------------------------------------------------===//
723// Return Value Calling Convention Implementation
724//===----------------------------------------------------------------------===//
725
726SDOperand MipsTargetLowering::
727LowerRET(SDOperand Op, SelectionDAG &DAG)
728{
729 // CCValAssign - represent the assignment of
730 // the return value to a location
731 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000732 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
733 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000734
735 // CCState - Info about the registers and stack slot.
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000736 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000737
738 // Analize return values of ISD::RET
739 CCInfo.AnalyzeReturn(Op.Val, RetCC_Mips);
740
741 // If this is the first return lowered for this function, add
742 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000743 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000744 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000745 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000746 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000747 }
748
749 // The chain is always operand #0
750 SDOperand Chain = Op.getOperand(0);
751 SDOperand Flag;
752
753 // Copy the result values into the output registers.
754 for (unsigned i = 0; i != RVLocs.size(); ++i) {
755 CCValAssign &VA = RVLocs[i];
756 assert(VA.isRegLoc() && "Can only return in registers!");
757
758 // ISD::RET => ret chain, (regnum1,val1), ...
759 // So i*2+1 index only the regnums
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000760 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000761
762 // guarantee that all emitted copies are
763 // stuck together, avoiding something bad
764 Flag = Chain.getValue(1);
765 }
766
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000767 // The mips ABIs for returning structs by value requires that we copy
768 // the sret argument into $v0 for the return. We saved the argument into
769 // a virtual register in the entry block, so now we copy the value out
770 // and into $v0.
771 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
772 MachineFunction &MF = DAG.getMachineFunction();
773 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
774 unsigned Reg = MipsFI->getSRetReturnReg();
775
776 if (!Reg)
777 assert(0 && "sret virtual register not created in the entry block");
778 SDOperand Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
779
780 Chain = DAG.getCopyToReg(Chain, Mips::V0, Val, Flag);
781 Flag = Chain.getValue(1);
782 }
783
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000784 // Return on Mips is always a "jr $ra"
785 if (Flag.Val)
786 return DAG.getNode(MipsISD::Ret, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000787 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000788 else // Return Void
789 return DAG.getNode(MipsISD::Ret, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000790 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000791}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000792
793//===----------------------------------------------------------------------===//
794// Mips Inline Assembly Support
795//===----------------------------------------------------------------------===//
796
797/// getConstraintType - Given a constraint letter, return the type of
798/// constraint it is for this target.
799MipsTargetLowering::ConstraintType MipsTargetLowering::
800getConstraintType(const std::string &Constraint) const
801{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000802 // Mips specific constrainy
803 // GCC config/mips/constraints.md
804 //
805 // 'd' : An address register. Equivalent to r
806 // unless generating MIPS16 code.
807 // 'y' : Equivalent to r; retained for
808 // backwards compatibility.
Bruno Cardoso Lopes7b76da12008-07-09 04:45:36 +0000809 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000810 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000811 switch (Constraint[0]) {
812 default : break;
813 case 'd':
814 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000815 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000816 return C_RegisterClass;
817 break;
818 }
819 }
820 return TargetLowering::getConstraintType(Constraint);
821}
822
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000823/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
824/// return a list of registers that can be used to satisfy the constraint.
825/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000826std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +0000827getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000828{
829 if (Constraint.size() == 1) {
830 switch (Constraint[0]) {
831 case 'r':
832 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000833 case 'f':
Duncan Sands15126422008-07-08 09:33:14 +0000834 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000835 if (Subtarget->isSingleFloat())
836 return std::make_pair(0U, Mips::FGR32RegisterClass);
837 else
838 return std::make_pair(0U, Mips::AFGR32RegisterClass);
Duncan Sands15126422008-07-08 09:33:14 +0000839 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000840 if (VT == MVT::f64)
841 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
842 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000843 }
844 }
845 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
846}
847
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000848/// Given a register class constraint, like 'r', if this corresponds directly
849/// to an LLVM register class, return a register of 0 and the register class
850/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000851std::vector<unsigned> MipsTargetLowering::
852getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000853 MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000854{
855 if (Constraint.size() != 1)
856 return std::vector<unsigned>();
857
858 switch (Constraint[0]) {
859 default : break;
860 case 'r':
861 // GCC Mips Constraint Letters
862 case 'd':
863 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000864 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
865 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
866 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
867 Mips::T8, 0);
868
869 case 'f':
Duncan Sands15126422008-07-08 09:33:14 +0000870 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000871 if (Subtarget->isSingleFloat())
872 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
873 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
874 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
875 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
876 Mips::F30, Mips::F31, 0);
877 else
878 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
879 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
880 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +0000881 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000882
883 if (VT == MVT::f64)
884 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
885 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
886 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
887 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000888 }
889 return std::vector<unsigned>();
890}