blob: 5f05a6f1e7272c7dfec145e24c36226254bd05ee [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"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Function.h"
22#include "llvm/Intrinsics.h"
23#include "llvm/CallingConv.h"
24#include "llvm/CodeGen/CallingConvLower.h"
25#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000029#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000030#include "llvm/CodeGen/ValueTypes.h"
31#include "llvm/Support/Debug.h"
32#include <queue>
33#include <set>
34
35using namespace llvm;
36
37const char *MipsTargetLowering::
38getTargetNodeName(unsigned Opcode) const
39{
40 switch (Opcode)
41 {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +000042 case MipsISD::JmpLink : return "MipsISD::JmpLink";
43 case MipsISD::Hi : return "MipsISD::Hi";
44 case MipsISD::Lo : return "MipsISD::Lo";
45 case MipsISD::Ret : return "MipsISD::Ret";
46 default : return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000047 }
48}
49
50MipsTargetLowering::
51MipsTargetLowering(MipsTargetMachine &TM): TargetLowering(TM)
52{
53 // Mips does not have i1 type, so use i32 for
54 // setcc operations results (slt, sgt, ...).
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000055 setSetCCResultContents(ZeroOrOneSetCCResult);
56
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000057 // JumpTable targets must use GOT when using PIC_
58 setUsesGlobalOffsetTable(true);
59
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000060 // Set up the register classes
61 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
62
63 // Custom
64 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +000065 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000066 setOperationAction(ISD::RET, MVT::Other, Custom);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000067 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000068
69 // Load extented operations for i1 types must be promoted
70 setLoadXAction(ISD::EXTLOAD, MVT::i1, Promote);
71 setLoadXAction(ISD::ZEXTLOAD, MVT::i1, Promote);
72 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
73
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000074 // Mips does not have these NodeTypes below.
75 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
76 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
77 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +000078 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
79 setOperationAction(ISD::SELECT, MVT::i32, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000080 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000081
82 // Mips not supported intrinsics.
Andrew Lenharthd497d9f2008-02-16 14:46:26 +000083 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000084
85 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
86 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
87 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
88 setOperationAction(ISD::ROTL , MVT::i32, Expand);
89 setOperationAction(ISD::ROTR , MVT::i32, Expand);
90 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
91
92 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
93 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
94 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
95
96 // We don't have line number support yet.
97 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
98 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
99 setOperationAction(ISD::LABEL, MVT::Other, Expand);
100
101 // Use the default for now
102 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
103 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
104
105 setStackPointerRegisterToSaveRestore(Mips::SP);
106 computeRegisterProperties();
107}
108
109
Scott Michel5b8f82e2008-03-10 15:42:14 +0000110MVT::ValueType
111MipsTargetLowering::getSetCCResultType(const SDOperand &) const {
112 return MVT::i32;
113}
114
115
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000116SDOperand MipsTargetLowering::
117LowerOperation(SDOperand Op, SelectionDAG &DAG)
118{
119 switch (Op.getOpcode())
120 {
121 case ISD::CALL: return LowerCALL(Op, DAG);
122 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
123 case ISD::RET: return LowerRET(Op, DAG);
124 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000125 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000126 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000127 }
128 return SDOperand();
129}
130
131//===----------------------------------------------------------------------===//
132// Lower helper functions
133//===----------------------------------------------------------------------===//
134
135// AddLiveIn - This helper function adds the specified physical register to the
136// MachineFunction as a live in value. It also creates a corresponding
137// virtual register for it.
138static unsigned
139AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
140{
141 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000142 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
143 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000144 return VReg;
145}
146
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000147//===----------------------------------------------------------------------===//
148// Misc Lower Operation implementation
149//===----------------------------------------------------------------------===//
150SDOperand MipsTargetLowering::
151LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG)
152{
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000153 SDOperand ResNode;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000154 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000155 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000156 bool isPIC = (getTargetMachine().getRelocationModel() == Reloc::PIC_);
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000157
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000158 SDOperand HiPart;
159 if (!isPIC) {
160 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::i32);
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000161 SDOperand Ops[] = { GA };
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000162 HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
163 } else // Emit Load from Global Pointer
164 HiPart = DAG.getLoad(MVT::i32, DAG.getEntryNode(), GA, NULL, 0);
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000165
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000166 // On functions and global targets not internal linked only
167 // a load from got/GP is necessary for PIC to work.
168 if ((isPIC) && ((!GV->hasInternalLinkage()) || (isa<Function>(GV))))
169 return HiPart;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000170
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000171 SDOperand Lo = DAG.getNode(MipsISD::Lo, MVT::i32, GA);
172 ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000173
174 return ResNode;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000175}
176
177SDOperand MipsTargetLowering::
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000178LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG)
179{
180 assert(0 && "TLS not implemented for MIPS.");
Chris Lattnerd27c9912008-03-30 18:22:13 +0000181 return SDOperand(); // Not reached
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000182}
183
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000184SDOperand MipsTargetLowering::
185LowerJumpTable(SDOperand Op, SelectionDAG &DAG)
186{
187 SDOperand ResNode;
188 SDOperand HiPart;
189
190 MVT::ValueType PtrVT = Op.getValueType();
191 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
192 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
193
194 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
195 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::i32);
196 SDOperand Ops[] = { JTI };
197 HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
198 } else // Emit Load from Global Pointer
199 HiPart = DAG.getLoad(MVT::i32, DAG.getEntryNode(), JTI, NULL, 0);
200
201 SDOperand Lo = DAG.getNode(MipsISD::Lo, MVT::i32, JTI);
202 ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
203
204 return ResNode;
205}
206
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000207//===----------------------------------------------------------------------===//
208// Calling Convention Implementation
209//
210// The lower operations present on calling convention works on this order:
211// LowerCALL (virt regs --> phys regs, virt regs --> stack)
212// LowerFORMAL_ARGUMENTS (phys --> virt regs, stack --> virt regs)
213// LowerRET (virt regs --> phys regs)
214// LowerCALL (phys regs --> virt regs)
215//
216//===----------------------------------------------------------------------===//
217
218#include "MipsGenCallingConv.inc"
219
220//===----------------------------------------------------------------------===//
221// CALL Calling Convention Implementation
222//===----------------------------------------------------------------------===//
223
224/// Mips custom CALL implementation
225SDOperand MipsTargetLowering::
226LowerCALL(SDOperand Op, SelectionDAG &DAG)
227{
Chris Lattnere0b12152008-03-17 06:57:02 +0000228 unsigned CallingConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000229
230 // By now, only CallingConv::C implemented
Chris Lattnere0b12152008-03-17 06:57:02 +0000231 switch (CallingConv) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000232 default:
233 assert(0 && "Unsupported calling convention");
234 case CallingConv::Fast:
235 case CallingConv::C:
236 return LowerCCCCallTo(Op, DAG, CallingConv);
237 }
238}
239
240/// LowerCCCCallTo - functions arguments are copied from virtual
241/// regs to (physical regs)/(stack frame), CALLSEQ_START and
242/// CALLSEQ_END are emitted.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000243/// TODO: isVarArg, isTailCall, sret.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000244SDOperand MipsTargetLowering::
245LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG, unsigned CC)
246{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000247 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000248
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000249 SDOperand Chain = Op.getOperand(0);
250 SDOperand Callee = Op.getOperand(4);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000251 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
252
253 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000254
255 // Analyze operands of the call, assigning locations to each operand.
256 SmallVector<CCValAssign, 16> ArgLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000257 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
258
259 // To meet ABI, Mips must always allocate 16 bytes on
260 // the stack (even if less than 4 are used as arguments)
261 int VTsize = MVT::getSizeInBits(MVT::i32)/8;
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000262 MFI->CreateFixedObject(VTsize, (VTsize*3));
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000263
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000264 CCInfo.AnalyzeCallOperands(Op.Val, CC_Mips);
265
266 // Get a count of how many bytes are to be pushed on the stack.
267 unsigned NumBytes = CCInfo.getNextStackOffset();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000268 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes,
269 getPointerTy()));
270
271 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
272 SmallVector<SDOperand, 8> MemOpChains;
273
Chris Lattnere0b12152008-03-17 06:57:02 +0000274 int LastStackLoc = 0;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000275
276 // Walk the register/memloc assignments, inserting copies/loads.
277 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
278 CCValAssign &VA = ArgLocs[i];
279
280 // Arguments start after the 5 first operands of ISD::CALL
281 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
282
283 // Promote the value if needed.
284 switch (VA.getLocInfo()) {
Chris Lattnere0b12152008-03-17 06:57:02 +0000285 default: assert(0 && "Unknown loc info!");
286 case CCValAssign::Full: break;
287 case CCValAssign::SExt:
288 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
289 break;
290 case CCValAssign::ZExt:
291 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
292 break;
293 case CCValAssign::AExt:
294 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
295 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000296 }
297
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000298 // Arguments that can be passed on register must be kept at
299 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000300 if (VA.isRegLoc()) {
301 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +0000302 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000303 }
Chris Lattnere0b12152008-03-17 06:57:02 +0000304
305 assert(VA.isMemLoc());
306
307 // Create the frame index object for this incoming parameter
308 // This guarantees that when allocating Local Area the firsts
309 // 16 bytes which are alwayes reserved won't be overwritten.
310 LastStackLoc = (16 + VA.getLocMemOffset());
311 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
312 LastStackLoc);
313
314 SDOperand PtrOff = DAG.getFrameIndex(FI,getPointerTy());
315
316 // emit ISD::STORE whichs stores the
317 // parameter value to a stack Location
318 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000319 }
320
321 // Transform all store nodes into one single node because
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000322 // all store nodes are independent of each other.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000323 if (!MemOpChains.empty())
324 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
325 &MemOpChains[0], MemOpChains.size());
326
327 // Build a sequence of copy-to-reg nodes chained together with token
328 // chain and flag operands which copy the outgoing args into registers.
329 // The InFlag in necessary since all emited instructions must be
330 // stuck together.
331 SDOperand InFlag;
332 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
333 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first,
334 RegsToPass[i].second, InFlag);
335 InFlag = Chain.getValue(1);
336 }
337
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000338 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
339 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000340 // node so that legalize doesn't hack it.
341 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000342 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000343 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000344 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
345
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000346
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000347 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
348 // = Chain, Callee, Reg#1, Reg#2, ...
349 //
350 // Returns a chain & a flag for retval copy to use.
351 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
352 SmallVector<SDOperand, 8> Ops;
353 Ops.push_back(Chain);
354 Ops.push_back(Callee);
355
356 // Add argument registers to the end of the list so that they are
357 // known live into the call.
358 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
359 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
360 RegsToPass[i].second.getValueType()));
361
362 if (InFlag.Val)
363 Ops.push_back(InFlag);
364
365 Chain = DAG.getNode(MipsISD::JmpLink, NodeTys, &Ops[0], Ops.size());
366 InFlag = Chain.getValue(1);
367
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000368 Chain = DAG.getCALLSEQ_END(Chain,
369 DAG.getConstant(NumBytes, getPointerTy()),
370 DAG.getConstant(0, getPointerTy()),
371 InFlag);
372 InFlag = Chain.getValue(1);
373
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000374 // Create a stack location to hold GP when PIC is used. This stack
375 // location is used on function prologue to save GP and also after all
376 // emited CALL's to restore GP.
377 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000378 // Function can have an arbitrary number of calls, so
379 // hold the LastStackLoc with the biggest offset.
380 int FI;
381 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
382 if (LastStackLoc >= MipsFI->getGPStackOffset()) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000383 LastStackLoc = (!LastStackLoc) ? (16) : (LastStackLoc+4);
384 // Create the frame index only once. SPOffset here can be anything
385 // (this will be fixed on processFunctionBeforeFrameFinalized)
386 if (MipsFI->getGPStackOffset() == -1) {
387 FI = MFI->CreateFixedObject(4, 0);
388 MipsFI->setGPFI(FI);
389 }
390 MipsFI->setGPStackOffset(LastStackLoc);
391 }
392
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000393 // Reload GP value.
394 FI = MipsFI->getGPFI();
395 SDOperand FIN = DAG.getFrameIndex(FI,getPointerTy());
396 SDOperand GPLoad = DAG.getLoad(MVT::i32, Chain, FIN, NULL, 0);
397 Chain = GPLoad.getValue(1);
398 Chain = DAG.getCopyToReg(Chain, DAG.getRegister(Mips::GP, MVT::i32),
399 GPLoad, SDOperand(0,0));
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000400 InFlag = Chain.getValue(1);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000401 }
402
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000403 // Create the CALLSEQ_END node.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000404
405 // Handle result values, copying them out of physregs into vregs that we
406 // return.
407 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
408}
409
410/// LowerCallResult - Lower the result values of an ISD::CALL into the
411/// appropriate copies out of appropriate physical registers. This assumes that
412/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
413/// being lowered. Returns a SDNode with the same number of values as the
414/// ISD::CALL.
415SDNode *MipsTargetLowering::
416LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
417 unsigned CallingConv, SelectionDAG &DAG) {
418
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000419 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
420
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000421 // Assign locations to each value returned by this call.
422 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000423 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
424
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000425 CCInfo.AnalyzeCallResult(TheCall, RetCC_Mips);
426 SmallVector<SDOperand, 8> ResultVals;
427
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000428 // Copy all of the result registers out of their specified physreg.
429 for (unsigned i = 0; i != RVLocs.size(); ++i) {
430 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
431 RVLocs[i].getValVT(), InFlag).getValue(1);
432 InFlag = Chain.getValue(2);
433 ResultVals.push_back(Chain.getValue(0));
434 }
435
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000436 ResultVals.push_back(Chain);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000437
438 // Merge everything together with a MERGE_VALUES node.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000439 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000440 &ResultVals[0], ResultVals.size()).Val;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000441}
442
443//===----------------------------------------------------------------------===//
444// FORMAL_ARGUMENTS Calling Convention Implementation
445//===----------------------------------------------------------------------===//
446
447/// Mips custom FORMAL_ARGUMENTS implementation
448SDOperand MipsTargetLowering::
449LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG)
450{
451 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
452 switch(CC)
453 {
454 default:
455 assert(0 && "Unsupported calling convention");
456 case CallingConv::C:
457 return LowerCCCArguments(Op, DAG);
458 }
459}
460
461/// LowerCCCArguments - transform physical registers into
462/// virtual registers and generate load operations for
463/// arguments places on the stack.
464/// TODO: isVarArg, sret
465SDOperand MipsTargetLowering::
466LowerCCCArguments(SDOperand Op, SelectionDAG &DAG)
467{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000468 SDOperand Root = Op.getOperand(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000469 MachineFunction &MF = DAG.getMachineFunction();
470 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000471 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000472
473 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
474 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
475
476 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000477
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000478 // GP holds the GOT address on PIC calls.
479 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
480 AddLiveIn(MF, Mips::GP, Mips::CPURegsRegisterClass);
481
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000482 // Assign locations to all of the incoming arguments.
483 SmallVector<CCValAssign, 16> ArgLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000484 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
485
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000486 CCInfo.AnalyzeFormalArguments(Op.Val, CC_Mips);
487 SmallVector<SDOperand, 8> ArgValues;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000488 SDOperand StackPtr;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000489
490 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
491
492 CCValAssign &VA = ArgLocs[i];
493
494 // Arguments stored on registers
495 if (VA.isRegLoc()) {
496 MVT::ValueType RegVT = VA.getLocVT();
497 TargetRegisterClass *RC;
498
499 if (RegVT == MVT::i32)
500 RC = Mips::CPURegsRegisterClass;
501 else
502 assert(0 && "support only Mips::CPURegsRegisterClass");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000503
504 // Transform the arguments stored on
505 // physical registers into virtual ones
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000506 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000507 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
508
509 // If this is an 8 or 16-bit value, it is really passed promoted
510 // to 32 bits. Insert an assert[sz]ext to capture this, then
511 // truncate to the right size.
512 if (VA.getLocInfo() == CCValAssign::SExt)
513 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
514 DAG.getValueType(VA.getValVT()));
515 else if (VA.getLocInfo() == CCValAssign::ZExt)
516 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
517 DAG.getValueType(VA.getValVT()));
518
519 if (VA.getLocInfo() != CCValAssign::Full)
520 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
521
522 ArgValues.push_back(ArgValue);
523
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000524 // To meet ABI, when VARARGS are passed on registers, the registers
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000525 // must have their values written to the caller stack frame.
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000526 if (isVarArg) {
527
528 if (StackPtr.Val == 0)
529 StackPtr = DAG.getRegister(StackReg, getPointerTy());
530
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000531 // The stack pointer offset is relative to the caller stack frame.
532 // Since the real stack size is unknown here, a negative SPOffset
533 // is used so there's a way to adjust these offsets when the stack
534 // size get known (on EliminateFrameIndex). A dummy SPOffset is
535 // used instead of a direct negative address (which is recorded to
536 // be used on emitPrologue) to avoid mis-calc of the first stack
537 // offset on PEI::calculateFrameObjectOffsets.
538 // Arguments are always 32-bit.
539 int FI = MFI->CreateFixedObject(4, 0);
540 MipsFI->recordStoreVarArgsFI(FI, -(4+(i*4)));
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000541 SDOperand PtrOff = DAG.getFrameIndex(FI, getPointerTy());
542
543 // emit ISD::STORE whichs stores the
544 // parameter value to a stack Location
545 ArgValues.push_back(DAG.getStore(Root, ArgValue, PtrOff, NULL, 0));
546 }
547
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000548 } else {
549 // sanity check
550 assert(VA.isMemLoc());
551
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000552 // The stack pointer offset is relative to the caller stack frame.
553 // Since the real stack size is unknown here, a negative SPOffset
554 // is used so there's a way to adjust these offsets when the stack
555 // size get known (on EliminateFrameIndex). A dummy SPOffset is
556 // used instead of a direct negative address (which is recorded to
557 // be used on emitPrologue) to avoid mis-calc of the first stack
558 // offset on PEI::calculateFrameObjectOffsets.
559 // Arguments are always 32-bit.
560 int FI = MFI->CreateFixedObject(4, 0);
561 MipsFI->recordLoadArgsFI(FI, -(4+(16+VA.getLocMemOffset())));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000562
563 // Create load nodes to retrieve arguments from the stack
564 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
565 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
566 }
567 }
568 ArgValues.push_back(Root);
569
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000570 // Return the new list of results.
571 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
572 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
573}
574
575//===----------------------------------------------------------------------===//
576// Return Value Calling Convention Implementation
577//===----------------------------------------------------------------------===//
578
579SDOperand MipsTargetLowering::
580LowerRET(SDOperand Op, SelectionDAG &DAG)
581{
582 // CCValAssign - represent the assignment of
583 // the return value to a location
584 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000585 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
586 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000587
588 // CCState - Info about the registers and stack slot.
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000589 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000590
591 // Analize return values of ISD::RET
592 CCInfo.AnalyzeReturn(Op.Val, RetCC_Mips);
593
594 // If this is the first return lowered for this function, add
595 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000596 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000597 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000598 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000599 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000600 }
601
602 // The chain is always operand #0
603 SDOperand Chain = Op.getOperand(0);
604 SDOperand Flag;
605
606 // Copy the result values into the output registers.
607 for (unsigned i = 0; i != RVLocs.size(); ++i) {
608 CCValAssign &VA = RVLocs[i];
609 assert(VA.isRegLoc() && "Can only return in registers!");
610
611 // ISD::RET => ret chain, (regnum1,val1), ...
612 // So i*2+1 index only the regnums
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000613 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000614
615 // guarantee that all emitted copies are
616 // stuck together, avoiding something bad
617 Flag = Chain.getValue(1);
618 }
619
620 // Return on Mips is always a "jr $ra"
621 if (Flag.Val)
622 return DAG.getNode(MipsISD::Ret, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000623 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000624 else // Return Void
625 return DAG.getNode(MipsISD::Ret, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000626 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000627}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000628
629//===----------------------------------------------------------------------===//
630// Mips Inline Assembly Support
631//===----------------------------------------------------------------------===//
632
633/// getConstraintType - Given a constraint letter, return the type of
634/// constraint it is for this target.
635MipsTargetLowering::ConstraintType MipsTargetLowering::
636getConstraintType(const std::string &Constraint) const
637{
638 if (Constraint.size() == 1) {
639 // Mips specific constrainy
640 // GCC config/mips/constraints.md
641 //
642 // 'd' : An address register. Equivalent to r
643 // unless generating MIPS16 code.
644 // 'y' : Equivalent to r; retained for
645 // backwards compatibility.
646 //
647 switch (Constraint[0]) {
648 default : break;
649 case 'd':
650 case 'y':
651 return C_RegisterClass;
652 break;
653 }
654 }
655 return TargetLowering::getConstraintType(Constraint);
656}
657
658std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
659getRegForInlineAsmConstraint(const std::string &Constraint,
660 MVT::ValueType VT) const
661{
662 if (Constraint.size() == 1) {
663 switch (Constraint[0]) {
664 case 'r':
665 return std::make_pair(0U, Mips::CPURegsRegisterClass);
666 break;
667 }
668 }
669 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
670}
671
672std::vector<unsigned> MipsTargetLowering::
673getRegClassForInlineAsmConstraint(const std::string &Constraint,
674 MVT::ValueType VT) const
675{
676 if (Constraint.size() != 1)
677 return std::vector<unsigned>();
678
679 switch (Constraint[0]) {
680 default : break;
681 case 'r':
682 // GCC Mips Constraint Letters
683 case 'd':
684 case 'y':
685 return make_vector<unsigned>(Mips::V0, Mips::V1, Mips::A0,
686 Mips::A1, Mips::A2, Mips::A3,
687 Mips::T0, Mips::T1, Mips::T2,
688 Mips::T3, Mips::T4, Mips::T5,
689 Mips::T6, Mips::T7, Mips::S0,
690 Mips::S1, Mips::S2, Mips::S3,
691 Mips::S4, Mips::S5, Mips::S6,
692 Mips::S7, Mips::T8, Mips::T9, 0);
693 break;
694 }
695 return std::vector<unsigned>();
696}