blob: 323af2afc77ed8192db2bc7542300928f55a43b3 [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
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000085 // Load extented operations for i1 types must be promoted
86 setLoadXAction(ISD::EXTLOAD, MVT::i1, Promote);
87 setLoadXAction(ISD::ZEXTLOAD, MVT::i1, Promote);
88 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
89
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000090 // Mips does not have these NodeTypes below.
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +000091 setConvertAction(MVT::f64, MVT::f32, Expand);
92
93 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
94 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
95 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
96 setOperationAction(ISD::SELECT, MVT::i32, Expand);
97 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
98 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
99 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
100
101 if (Subtarget->isSingleFloat())
102 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000103
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000104 if (!Subtarget->isAllegrex()) {
105 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
106 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
107 }
108
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000109 // Mips not supported intrinsics.
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000110 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000111
112 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
113 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
114 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
115 setOperationAction(ISD::ROTL , MVT::i32, Expand);
116 setOperationAction(ISD::ROTR , MVT::i32, Expand);
117 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
118
119 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
120 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
121 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
122
123 // We don't have line number support yet.
Dan Gohman7f460202008-06-30 20:59:49 +0000124 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000125 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000126 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
127 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000128
129 // Use the default for now
130 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
131 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
132
133 setStackPointerRegisterToSaveRestore(Mips::SP);
134 computeRegisterProperties();
135}
136
137
Duncan Sands83ec4b62008-06-06 12:08:01 +0000138MVT MipsTargetLowering::getSetCCResultType(const SDOperand &) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000139 return MVT::i32;
140}
141
142
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000143SDOperand MipsTargetLowering::
144LowerOperation(SDOperand Op, SelectionDAG &DAG)
145{
146 switch (Op.getOpcode())
147 {
148 case ISD::CALL: return LowerCALL(Op, DAG);
149 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
150 case ISD::RET: return LowerRET(Op, DAG);
151 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000152 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000153 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000154 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000155 }
156 return SDOperand();
157}
158
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000159MachineBasicBlock *
160MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
161 MachineBasicBlock *BB)
162{
163 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
164 switch (MI->getOpcode()) {
165 default: assert(false && "Unexpected instr type to insert");
166 case Mips::Select_CC: {
167 // To "insert" a SELECT_CC instruction, we actually have to insert the
168 // diamond control-flow pattern. The incoming instruction knows the
169 // destination vreg to set, the condition code register to branch on, the
170 // true/false values to select between, and a branch opcode to use.
171 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000172 MachineFunction::iterator It = BB;
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000173 ++It;
174
175 // thisMBB:
176 // ...
177 // TrueVal = ...
178 // setcc r1, r2, r3
179 // bNE r1, r0, copy1MBB
180 // fallthrough --> copy0MBB
181 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000182 MachineFunction *F = BB->getParent();
183 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
184 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000185 BuildMI(BB, TII->get(Mips::BNE)).addReg(MI->getOperand(1).getReg())
186 .addReg(Mips::ZERO).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000187 F->insert(It, copy0MBB);
188 F->insert(It, sinkMBB);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000189 // Update machine-CFG edges by first adding all successors of the current
190 // block to the new block which will contain the Phi node for the select.
191 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
192 e = BB->succ_end(); i != e; ++i)
193 sinkMBB->addSuccessor(*i);
194 // Next, remove all successors of the current block, and add the true
195 // and fallthrough blocks as its successors.
196 while(!BB->succ_empty())
197 BB->removeSuccessor(BB->succ_begin());
198 BB->addSuccessor(copy0MBB);
199 BB->addSuccessor(sinkMBB);
200
201 // copy0MBB:
202 // %FalseValue = ...
203 // # fallthrough to sinkMBB
204 BB = copy0MBB;
205
206 // Update machine-CFG edges
207 BB->addSuccessor(sinkMBB);
208
209 // sinkMBB:
210 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
211 // ...
212 BB = sinkMBB;
213 BuildMI(BB, TII->get(Mips::PHI), MI->getOperand(0).getReg())
214 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
215 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB);
216
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000217 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000218 return BB;
219 }
220 }
221}
222
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000223//===----------------------------------------------------------------------===//
224// Lower helper functions
225//===----------------------------------------------------------------------===//
226
227// AddLiveIn - This helper function adds the specified physical register to the
228// MachineFunction as a live in value. It also creates a corresponding
229// virtual register for it.
230static unsigned
231AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
232{
233 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000234 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
235 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000236 return VReg;
237}
238
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000239//===----------------------------------------------------------------------===//
240// Misc Lower Operation implementation
241//===----------------------------------------------------------------------===//
242SDOperand MipsTargetLowering::
243LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG)
244{
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000245 SDOperand ResNode;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000246 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000247 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000248 bool isPIC = (getTargetMachine().getRelocationModel() == Reloc::PIC_);
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000249
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000250 SDOperand HiPart;
251 if (!isPIC) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000252 const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000253 SDOperand Ops[] = { GA };
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000254 HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
255 } else // Emit Load from Global Pointer
256 HiPart = DAG.getLoad(MVT::i32, DAG.getEntryNode(), GA, NULL, 0);
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000257
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000258 // On functions and global targets not internal linked only
259 // a load from got/GP is necessary for PIC to work.
260 if ((isPIC) && ((!GV->hasInternalLinkage()) || (isa<Function>(GV))))
261 return HiPart;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000262
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000263 SDOperand Lo = DAG.getNode(MipsISD::Lo, MVT::i32, GA);
264 ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000265
266 return ResNode;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000267}
268
269SDOperand MipsTargetLowering::
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000270LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG)
271{
272 assert(0 && "TLS not implemented for MIPS.");
Chris Lattnerd27c9912008-03-30 18:22:13 +0000273 return SDOperand(); // Not reached
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000274}
275
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000276SDOperand MipsTargetLowering::
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000277LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG)
278{
279 SDOperand LHS = Op.getOperand(0);
280 SDOperand RHS = Op.getOperand(1);
281 SDOperand True = Op.getOperand(2);
282 SDOperand False = Op.getOperand(3);
283 SDOperand CC = Op.getOperand(4);
284
Duncan Sands83ec4b62008-06-06 12:08:01 +0000285 const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000286 SDOperand Ops[] = { LHS, RHS, CC };
287 SDOperand SetCCRes = DAG.getNode(ISD::SETCC, VTs, 1, Ops, 3);
288
289 return DAG.getNode(MipsISD::SelectCC, True.getValueType(),
290 SetCCRes, True, False);
291}
292
293SDOperand MipsTargetLowering::
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000294LowerJumpTable(SDOperand Op, SelectionDAG &DAG)
295{
296 SDOperand ResNode;
297 SDOperand HiPart;
298
Duncan Sands83ec4b62008-06-06 12:08:01 +0000299 MVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000300 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
301 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
302
303 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000304 const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000305 SDOperand Ops[] = { JTI };
306 HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
307 } else // Emit Load from Global Pointer
308 HiPart = DAG.getLoad(MVT::i32, DAG.getEntryNode(), JTI, NULL, 0);
309
310 SDOperand Lo = DAG.getNode(MipsISD::Lo, MVT::i32, JTI);
311 ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
312
313 return ResNode;
314}
315
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000316//===----------------------------------------------------------------------===//
317// Calling Convention Implementation
318//
319// The lower operations present on calling convention works on this order:
320// LowerCALL (virt regs --> phys regs, virt regs --> stack)
321// LowerFORMAL_ARGUMENTS (phys --> virt regs, stack --> virt regs)
322// LowerRET (virt regs --> phys regs)
323// LowerCALL (phys regs --> virt regs)
324//
325//===----------------------------------------------------------------------===//
326
327#include "MipsGenCallingConv.inc"
328
329//===----------------------------------------------------------------------===//
330// CALL Calling Convention Implementation
331//===----------------------------------------------------------------------===//
332
333/// Mips custom CALL implementation
334SDOperand MipsTargetLowering::
335LowerCALL(SDOperand Op, SelectionDAG &DAG)
336{
Chris Lattnere0b12152008-03-17 06:57:02 +0000337 unsigned CallingConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000338
339 // By now, only CallingConv::C implemented
Chris Lattnere0b12152008-03-17 06:57:02 +0000340 switch (CallingConv) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000341 default:
342 assert(0 && "Unsupported calling convention");
343 case CallingConv::Fast:
344 case CallingConv::C:
345 return LowerCCCCallTo(Op, DAG, CallingConv);
346 }
347}
348
349/// LowerCCCCallTo - functions arguments are copied from virtual
350/// regs to (physical regs)/(stack frame), CALLSEQ_START and
351/// CALLSEQ_END are emitted.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000352/// TODO: isVarArg, isTailCall.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000353SDOperand MipsTargetLowering::
354LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG, unsigned CC)
355{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000356 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000357
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000358 SDOperand Chain = Op.getOperand(0);
359 SDOperand Callee = Op.getOperand(4);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000360 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
361
362 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000363
364 // Analyze operands of the call, assigning locations to each operand.
365 SmallVector<CCValAssign, 16> ArgLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000366 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
367
368 // To meet ABI, Mips must always allocate 16 bytes on
369 // the stack (even if less than 4 are used as arguments)
Duncan Sands83ec4b62008-06-06 12:08:01 +0000370 int VTsize = MVT(MVT::i32).getSizeInBits()/8;
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000371 MFI->CreateFixedObject(VTsize, (VTsize*3));
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000372
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000373 CCInfo.AnalyzeCallOperands(Op.Val, CC_Mips);
374
375 // Get a count of how many bytes are to be pushed on the stack.
376 unsigned NumBytes = CCInfo.getNextStackOffset();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000377 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes,
378 getPointerTy()));
379
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000380 // With EABI is it possible to have 16 args on registers.
381 SmallVector<std::pair<unsigned, SDOperand>, 16> RegsToPass;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000382 SmallVector<SDOperand, 8> MemOpChains;
383
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000384 // First/LastArgStackLoc contains the first/last
385 // "at stack" argument location.
386 int LastArgStackLoc = 0;
387 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000388
389 // Walk the register/memloc assignments, inserting copies/loads.
390 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
391 CCValAssign &VA = ArgLocs[i];
392
393 // Arguments start after the 5 first operands of ISD::CALL
394 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
395
396 // Promote the value if needed.
397 switch (VA.getLocInfo()) {
Chris Lattnere0b12152008-03-17 06:57:02 +0000398 default: assert(0 && "Unknown loc info!");
399 case CCValAssign::Full: break;
400 case CCValAssign::SExt:
401 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
402 break;
403 case CCValAssign::ZExt:
404 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
405 break;
406 case CCValAssign::AExt:
407 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
408 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000409 }
410
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000411 // Arguments that can be passed on register must be kept at
412 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000413 if (VA.isRegLoc()) {
414 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +0000415 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000416 }
Chris Lattnere0b12152008-03-17 06:57:02 +0000417
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000418 // Register cant get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +0000419 assert(VA.isMemLoc());
420
421 // Create the frame index object for this incoming parameter
422 // This guarantees that when allocating Local Area the firsts
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000423 // 16 bytes which are alwayes reserved won't be overwritten
424 // if O32 ABI is used. For EABI the first address is zero.
425 LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000426 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000427 LastArgStackLoc);
Chris Lattnere0b12152008-03-17 06:57:02 +0000428
429 SDOperand PtrOff = DAG.getFrameIndex(FI,getPointerTy());
430
431 // emit ISD::STORE whichs stores the
432 // parameter value to a stack Location
433 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000434 }
435
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000436 // Transform all store nodes into one single node because all store
437 // nodes are independent of each other.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000438 if (!MemOpChains.empty())
439 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
440 &MemOpChains[0], MemOpChains.size());
441
442 // Build a sequence of copy-to-reg nodes chained together with token
443 // chain and flag operands which copy the outgoing args into registers.
444 // The InFlag in necessary since all emited instructions must be
445 // stuck together.
446 SDOperand InFlag;
447 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
448 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first,
449 RegsToPass[i].second, InFlag);
450 InFlag = Chain.getValue(1);
451 }
452
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000453 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
454 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000455 // node so that legalize doesn't hack it.
456 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000457 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000458 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000459 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
460
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000461
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000462 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
463 // = Chain, Callee, Reg#1, Reg#2, ...
464 //
465 // Returns a chain & a flag for retval copy to use.
466 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
467 SmallVector<SDOperand, 8> Ops;
468 Ops.push_back(Chain);
469 Ops.push_back(Callee);
470
471 // Add argument registers to the end of the list so that they are
472 // known live into the call.
473 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
474 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
475 RegsToPass[i].second.getValueType()));
476
477 if (InFlag.Val)
478 Ops.push_back(InFlag);
479
480 Chain = DAG.getNode(MipsISD::JmpLink, NodeTys, &Ops[0], Ops.size());
481 InFlag = Chain.getValue(1);
482
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000483 // Create the CALLSEQ_END node.
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000484 Chain = DAG.getCALLSEQ_END(Chain,
485 DAG.getConstant(NumBytes, getPointerTy()),
486 DAG.getConstant(0, getPointerTy()),
487 InFlag);
488 InFlag = Chain.getValue(1);
489
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000490 // Create a stack location to hold GP when PIC is used. This stack
491 // location is used on function prologue to save GP and also after all
492 // emited CALL's to restore GP.
493 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000494 // Function can have an arbitrary number of calls, so
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000495 // hold the LastArgStackLoc with the biggest offset.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000496 int FI;
497 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000498 if (LastArgStackLoc >= MipsFI->getGPStackOffset()) {
499 LastArgStackLoc = (!LastArgStackLoc) ? (16) : (LastArgStackLoc+4);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000500 // Create the frame index only once. SPOffset here can be anything
501 // (this will be fixed on processFunctionBeforeFrameFinalized)
502 if (MipsFI->getGPStackOffset() == -1) {
503 FI = MFI->CreateFixedObject(4, 0);
504 MipsFI->setGPFI(FI);
505 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000506 MipsFI->setGPStackOffset(LastArgStackLoc);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000507 }
508
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000509 // Reload GP value.
510 FI = MipsFI->getGPFI();
511 SDOperand FIN = DAG.getFrameIndex(FI,getPointerTy());
512 SDOperand GPLoad = DAG.getLoad(MVT::i32, Chain, FIN, NULL, 0);
513 Chain = GPLoad.getValue(1);
514 Chain = DAG.getCopyToReg(Chain, DAG.getRegister(Mips::GP, MVT::i32),
515 GPLoad, SDOperand(0,0));
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000516 InFlag = Chain.getValue(1);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000517 }
518
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000519 // Handle result values, copying them out of physregs into vregs that we
520 // return.
521 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
522}
523
524/// LowerCallResult - Lower the result values of an ISD::CALL into the
525/// appropriate copies out of appropriate physical registers. This assumes that
526/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
527/// being lowered. Returns a SDNode with the same number of values as the
528/// ISD::CALL.
529SDNode *MipsTargetLowering::
530LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
531 unsigned CallingConv, SelectionDAG &DAG) {
532
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000533 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
534
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000535 // Assign locations to each value returned by this call.
536 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000537 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
538
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000539 CCInfo.AnalyzeCallResult(TheCall, RetCC_Mips);
540 SmallVector<SDOperand, 8> ResultVals;
541
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000542 // Copy all of the result registers out of their specified physreg.
543 for (unsigned i = 0; i != RVLocs.size(); ++i) {
544 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
545 RVLocs[i].getValVT(), InFlag).getValue(1);
546 InFlag = Chain.getValue(2);
547 ResultVals.push_back(Chain.getValue(0));
548 }
549
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000550 ResultVals.push_back(Chain);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000551
552 // Merge everything together with a MERGE_VALUES node.
Duncan Sandsf9516202008-06-30 10:19:09 +0000553 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
554 ResultVals.size()).Val;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000555}
556
557//===----------------------------------------------------------------------===//
558// FORMAL_ARGUMENTS Calling Convention Implementation
559//===----------------------------------------------------------------------===//
560
561/// Mips custom FORMAL_ARGUMENTS implementation
562SDOperand MipsTargetLowering::
563LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG)
564{
565 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
566 switch(CC)
567 {
568 default:
569 assert(0 && "Unsupported calling convention");
570 case CallingConv::C:
571 return LowerCCCArguments(Op, DAG);
572 }
573}
574
575/// LowerCCCArguments - transform physical registers into
576/// virtual registers and generate load operations for
577/// arguments places on the stack.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000578/// TODO: isVarArg
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000579SDOperand MipsTargetLowering::
580LowerCCCArguments(SDOperand Op, SelectionDAG &DAG)
581{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000582 SDOperand Root = Op.getOperand(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000583 MachineFunction &MF = DAG.getMachineFunction();
584 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000585 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000586
587 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
588 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
589
590 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000591
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000592 // GP holds the GOT address on PIC calls.
593 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
594 AddLiveIn(MF, Mips::GP, Mips::CPURegsRegisterClass);
595
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000596 // Assign locations to all of the incoming arguments.
597 SmallVector<CCValAssign, 16> ArgLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000598 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
599
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000600 CCInfo.AnalyzeFormalArguments(Op.Val, CC_Mips);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000601 SmallVector<SDOperand, 16> ArgValues;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000602 SDOperand StackPtr;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000603
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000604 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
605
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000606 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
607
608 CCValAssign &VA = ArgLocs[i];
609
610 // Arguments stored on registers
611 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000612 MVT RegVT = VA.getLocVT();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000613 TargetRegisterClass *RC;
614
615 if (RegVT == MVT::i32)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000616 RC = Mips::CPURegsRegisterClass;
617 else if (RegVT == MVT::f32) {
618 if (Subtarget->isSingleFloat())
619 RC = Mips::FGR32RegisterClass;
620 else
621 RC = Mips::AFGR32RegisterClass;
622 } else if (RegVT == MVT::f64) {
623 if (!Subtarget->isSingleFloat())
624 RC = Mips::AFGR64RegisterClass;
625 } else
626 assert(0 && "RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000627
628 // Transform the arguments stored on
629 // physical registers into virtual ones
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000630 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000631 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
632
633 // If this is an 8 or 16-bit value, it is really passed promoted
634 // to 32 bits. Insert an assert[sz]ext to capture this, then
635 // truncate to the right size.
636 if (VA.getLocInfo() == CCValAssign::SExt)
637 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
638 DAG.getValueType(VA.getValVT()));
639 else if (VA.getLocInfo() == CCValAssign::ZExt)
640 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
641 DAG.getValueType(VA.getValVT()));
642
643 if (VA.getLocInfo() != CCValAssign::Full)
644 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
645
646 ArgValues.push_back(ArgValue);
647
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000648 // To meet ABI, when VARARGS are passed on registers, the registers
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000649 // must have their values written to the caller stack frame.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000650 if ((isVarArg) && (Subtarget->isABI_O32())) {
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000651 if (StackPtr.Val == 0)
652 StackPtr = DAG.getRegister(StackReg, getPointerTy());
653
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000654 // The stack pointer offset is relative to the caller stack frame.
655 // Since the real stack size is unknown here, a negative SPOffset
656 // is used so there's a way to adjust these offsets when the stack
657 // size get known (on EliminateFrameIndex). A dummy SPOffset is
658 // used instead of a direct negative address (which is recorded to
659 // be used on emitPrologue) to avoid mis-calc of the first stack
660 // offset on PEI::calculateFrameObjectOffsets.
661 // Arguments are always 32-bit.
662 int FI = MFI->CreateFixedObject(4, 0);
663 MipsFI->recordStoreVarArgsFI(FI, -(4+(i*4)));
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000664 SDOperand PtrOff = DAG.getFrameIndex(FI, getPointerTy());
665
666 // emit ISD::STORE whichs stores the
667 // parameter value to a stack Location
668 ArgValues.push_back(DAG.getStore(Root, ArgValue, PtrOff, NULL, 0));
669 }
670
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000671 } else { // VA.isRegLoc()
672
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000673 // sanity check
674 assert(VA.isMemLoc());
675
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000676 // The stack pointer offset is relative to the caller stack frame.
677 // Since the real stack size is unknown here, a negative SPOffset
678 // is used so there's a way to adjust these offsets when the stack
679 // size get known (on EliminateFrameIndex). A dummy SPOffset is
680 // used instead of a direct negative address (which is recorded to
681 // be used on emitPrologue) to avoid mis-calc of the first stack
682 // offset on PEI::calculateFrameObjectOffsets.
683 // Arguments are always 32-bit.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000684 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
685 int FI = MFI->CreateFixedObject(ArgSize, 0);
686 MipsFI->recordLoadArgsFI(FI, -(ArgSize+
687 (FirstStackArgLoc + VA.getLocMemOffset())));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000688
689 // Create load nodes to retrieve arguments from the stack
690 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
691 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
692 }
693 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000694
695 // The mips ABIs for returning structs by value requires that we copy
696 // the sret argument into $v0 for the return. Save the argument into
697 // a virtual register so that we can access it from the return points.
698 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
699 unsigned Reg = MipsFI->getSRetReturnReg();
700 if (!Reg) {
701 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
702 MipsFI->setSRetReturnReg(Reg);
703 }
704 SDOperand Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
705 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
706 }
707
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000708 ArgValues.push_back(Root);
709
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000710 // Return the new list of results.
Duncan Sandsf9516202008-06-30 10:19:09 +0000711 return DAG.getMergeValues(Op.Val->getVTList(), &ArgValues[0],
712 ArgValues.size()).getValue(Op.ResNo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000713}
714
715//===----------------------------------------------------------------------===//
716// Return Value Calling Convention Implementation
717//===----------------------------------------------------------------------===//
718
719SDOperand MipsTargetLowering::
720LowerRET(SDOperand Op, SelectionDAG &DAG)
721{
722 // CCValAssign - represent the assignment of
723 // the return value to a location
724 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000725 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
726 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000727
728 // CCState - Info about the registers and stack slot.
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000729 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000730
731 // Analize return values of ISD::RET
732 CCInfo.AnalyzeReturn(Op.Val, RetCC_Mips);
733
734 // If this is the first return lowered for this function, add
735 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000736 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000737 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000738 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000739 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000740 }
741
742 // The chain is always operand #0
743 SDOperand Chain = Op.getOperand(0);
744 SDOperand Flag;
745
746 // Copy the result values into the output registers.
747 for (unsigned i = 0; i != RVLocs.size(); ++i) {
748 CCValAssign &VA = RVLocs[i];
749 assert(VA.isRegLoc() && "Can only return in registers!");
750
751 // ISD::RET => ret chain, (regnum1,val1), ...
752 // So i*2+1 index only the regnums
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000753 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000754
755 // guarantee that all emitted copies are
756 // stuck together, avoiding something bad
757 Flag = Chain.getValue(1);
758 }
759
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000760 // The mips ABIs for returning structs by value requires that we copy
761 // the sret argument into $v0 for the return. We saved the argument into
762 // a virtual register in the entry block, so now we copy the value out
763 // and into $v0.
764 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
765 MachineFunction &MF = DAG.getMachineFunction();
766 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
767 unsigned Reg = MipsFI->getSRetReturnReg();
768
769 if (!Reg)
770 assert(0 && "sret virtual register not created in the entry block");
771 SDOperand Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
772
773 Chain = DAG.getCopyToReg(Chain, Mips::V0, Val, Flag);
774 Flag = Chain.getValue(1);
775 }
776
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000777 // Return on Mips is always a "jr $ra"
778 if (Flag.Val)
779 return DAG.getNode(MipsISD::Ret, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000780 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000781 else // Return Void
782 return DAG.getNode(MipsISD::Ret, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000783 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000784}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000785
786//===----------------------------------------------------------------------===//
787// Mips Inline Assembly Support
788//===----------------------------------------------------------------------===//
789
790/// getConstraintType - Given a constraint letter, return the type of
791/// constraint it is for this target.
792MipsTargetLowering::ConstraintType MipsTargetLowering::
793getConstraintType(const std::string &Constraint) const
794{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000795 // Mips specific constrainy
796 // GCC config/mips/constraints.md
797 //
798 // 'd' : An address register. Equivalent to r
799 // unless generating MIPS16 code.
800 // 'y' : Equivalent to r; retained for
801 // backwards compatibility.
802 // 'f' : Float Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000803 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000804 switch (Constraint[0]) {
805 default : break;
806 case 'd':
807 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000808 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000809 return C_RegisterClass;
810 break;
811 }
812 }
813 return TargetLowering::getConstraintType(Constraint);
814}
815
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000816/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
817/// return a list of registers that can be used to satisfy the constraint.
818/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000819std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +0000820getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000821{
822 if (Constraint.size() == 1) {
823 switch (Constraint[0]) {
824 case 'r':
825 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000826 case 'f':
827 if (VT == MVT::f32)
828 if (Subtarget->isSingleFloat())
829 return std::make_pair(0U, Mips::FGR32RegisterClass);
830 else
831 return std::make_pair(0U, Mips::AFGR32RegisterClass);
832 if (VT == MVT::f64)
833 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
834 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000835 }
836 }
837 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
838}
839
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000840/// Given a register class constraint, like 'r', if this corresponds directly
841/// to an LLVM register class, return a register of 0 and the register class
842/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000843std::vector<unsigned> MipsTargetLowering::
844getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000845 MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000846{
847 if (Constraint.size() != 1)
848 return std::vector<unsigned>();
849
850 switch (Constraint[0]) {
851 default : break;
852 case 'r':
853 // GCC Mips Constraint Letters
854 case 'd':
855 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000856 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
857 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
858 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
859 Mips::T8, 0);
860
861 case 'f':
862 if (VT == MVT::f32)
863 if (Subtarget->isSingleFloat())
864 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
865 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
866 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
867 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
868 Mips::F30, Mips::F31, 0);
869 else
870 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
871 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
872 Mips::F28, Mips::F30, 0);
873
874 if (VT == MVT::f64)
875 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
876 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
877 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
878 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000879 }
880 return std::vector<unsigned>();
881}