blob: 486596928a47362787517d500ec95bcf63b8d40c [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.
83 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
84 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
85 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +000086 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000087
88 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
89 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
90 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
91 setOperationAction(ISD::ROTL , MVT::i32, Expand);
92 setOperationAction(ISD::ROTR , MVT::i32, Expand);
93 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
94
95 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
96 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
97 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
98
99 // We don't have line number support yet.
100 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
101 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
102 setOperationAction(ISD::LABEL, MVT::Other, Expand);
103
104 // Use the default for now
105 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
106 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
107
108 setStackPointerRegisterToSaveRestore(Mips::SP);
109 computeRegisterProperties();
110}
111
112
Scott Michel5b8f82e2008-03-10 15:42:14 +0000113MVT::ValueType
114MipsTargetLowering::getSetCCResultType(const SDOperand &) const {
115 return MVT::i32;
116}
117
118
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000119SDOperand MipsTargetLowering::
120LowerOperation(SDOperand Op, SelectionDAG &DAG)
121{
122 switch (Op.getOpcode())
123 {
124 case ISD::CALL: return LowerCALL(Op, DAG);
125 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
126 case ISD::RET: return LowerRET(Op, DAG);
127 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000128 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000129 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000130 }
131 return SDOperand();
132}
133
134//===----------------------------------------------------------------------===//
135// Lower helper functions
136//===----------------------------------------------------------------------===//
137
138// AddLiveIn - This helper function adds the specified physical register to the
139// MachineFunction as a live in value. It also creates a corresponding
140// virtual register for it.
141static unsigned
142AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
143{
144 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000145 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
146 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000147 return VReg;
148}
149
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000150//===----------------------------------------------------------------------===//
151// Misc Lower Operation implementation
152//===----------------------------------------------------------------------===//
153SDOperand MipsTargetLowering::
154LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG)
155{
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000156 SDOperand ResNode;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000157 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000158 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000159 bool isPIC = (getTargetMachine().getRelocationModel() == Reloc::PIC_);
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000160
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000161 SDOperand HiPart;
162 if (!isPIC) {
163 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::i32);
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000164 SDOperand Ops[] = { GA };
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000165 HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
166 } else // Emit Load from Global Pointer
167 HiPart = DAG.getLoad(MVT::i32, DAG.getEntryNode(), GA, NULL, 0);
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000168
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000169 // On functions and global targets not internal linked only
170 // a load from got/GP is necessary for PIC to work.
171 if ((isPIC) && ((!GV->hasInternalLinkage()) || (isa<Function>(GV))))
172 return HiPart;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000173
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000174 SDOperand Lo = DAG.getNode(MipsISD::Lo, MVT::i32, GA);
175 ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000176
177 return ResNode;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000178}
179
180SDOperand MipsTargetLowering::
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000181LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG)
182{
183 assert(0 && "TLS not implemented for MIPS.");
184}
185
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000186SDOperand MipsTargetLowering::
187LowerJumpTable(SDOperand Op, SelectionDAG &DAG)
188{
189 SDOperand ResNode;
190 SDOperand HiPart;
191
192 MVT::ValueType PtrVT = Op.getValueType();
193 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
194 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
195
196 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
197 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::i32);
198 SDOperand Ops[] = { JTI };
199 HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
200 } else // Emit Load from Global Pointer
201 HiPart = DAG.getLoad(MVT::i32, DAG.getEntryNode(), JTI, NULL, 0);
202
203 SDOperand Lo = DAG.getNode(MipsISD::Lo, MVT::i32, JTI);
204 ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
205
206 return ResNode;
207}
208
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000209//===----------------------------------------------------------------------===//
210// Calling Convention Implementation
211//
212// The lower operations present on calling convention works on this order:
213// LowerCALL (virt regs --> phys regs, virt regs --> stack)
214// LowerFORMAL_ARGUMENTS (phys --> virt regs, stack --> virt regs)
215// LowerRET (virt regs --> phys regs)
216// LowerCALL (phys regs --> virt regs)
217//
218//===----------------------------------------------------------------------===//
219
220#include "MipsGenCallingConv.inc"
221
222//===----------------------------------------------------------------------===//
223// CALL Calling Convention Implementation
224//===----------------------------------------------------------------------===//
225
226/// Mips custom CALL implementation
227SDOperand MipsTargetLowering::
228LowerCALL(SDOperand Op, SelectionDAG &DAG)
229{
230 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
231
232 // By now, only CallingConv::C implemented
233 switch (CallingConv)
234 {
235 default:
236 assert(0 && "Unsupported calling convention");
237 case CallingConv::Fast:
238 case CallingConv::C:
239 return LowerCCCCallTo(Op, DAG, CallingConv);
240 }
241}
242
243/// LowerCCCCallTo - functions arguments are copied from virtual
244/// regs to (physical regs)/(stack frame), CALLSEQ_START and
245/// CALLSEQ_END are emitted.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000246/// TODO: isVarArg, isTailCall, sret.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000247SDOperand MipsTargetLowering::
248LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG, unsigned CC)
249{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000250 MachineFunction &MF = DAG.getMachineFunction();
251 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
252
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000253 SDOperand Chain = Op.getOperand(0);
254 SDOperand Callee = Op.getOperand(4);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000255 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
256
257 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000258
259 // Analyze operands of the call, assigning locations to each operand.
260 SmallVector<CCValAssign, 16> ArgLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000261 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
262
263 // To meet ABI, Mips must always allocate 16 bytes on
264 // the stack (even if less than 4 are used as arguments)
265 int VTsize = MVT::getSizeInBits(MVT::i32)/8;
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000266 MFI->CreateFixedObject(VTsize, (VTsize*3));
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000267
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000268 CCInfo.AnalyzeCallOperands(Op.Val, CC_Mips);
269
270 // Get a count of how many bytes are to be pushed on the stack.
271 unsigned NumBytes = CCInfo.getNextStackOffset();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000272 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes,
273 getPointerTy()));
274
275 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
276 SmallVector<SDOperand, 8> MemOpChains;
277
278 SDOperand StackPtr;
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000279 int LastStackLoc=0;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000280
281 // Walk the register/memloc assignments, inserting copies/loads.
282 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
283 CCValAssign &VA = ArgLocs[i];
284
285 // Arguments start after the 5 first operands of ISD::CALL
286 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
287
288 // Promote the value if needed.
289 switch (VA.getLocInfo()) {
290 default: assert(0 && "Unknown loc info!");
291 case CCValAssign::Full: break;
292 case CCValAssign::SExt:
293 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
294 break;
295 case CCValAssign::ZExt:
296 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
297 break;
298 case CCValAssign::AExt:
299 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
300 break;
301 }
302
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000303 // Arguments that can be passed on register must be kept at
304 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000305 if (VA.isRegLoc()) {
306 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
307 } else {
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000308
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000309 assert(VA.isMemLoc());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000310
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000311 if (StackPtr.Val == 0)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000312 StackPtr = DAG.getRegister(StackReg, getPointerTy());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000313
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000314 // Create the frame index object for this incoming parameter
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000315 // This guarantees that when allocating Local Area the firsts
316 // 16 bytes which are alwayes reserved won't be overwritten.
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000317 LastStackLoc = (16 + VA.getLocMemOffset());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000318 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000319 LastStackLoc);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000320
321 SDOperand PtrOff = DAG.getFrameIndex(FI,getPointerTy());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000322
323 // emit ISD::STORE whichs stores the
324 // parameter value to a stack Location
325 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
326 }
327 }
328
329 // Transform all store nodes into one single node because
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000330 // all store nodes are independent of each other.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000331 if (!MemOpChains.empty())
332 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
333 &MemOpChains[0], MemOpChains.size());
334
335 // Build a sequence of copy-to-reg nodes chained together with token
336 // chain and flag operands which copy the outgoing args into registers.
337 // The InFlag in necessary since all emited instructions must be
338 // stuck together.
339 SDOperand InFlag;
340 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
341 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first,
342 RegsToPass[i].second, InFlag);
343 InFlag = Chain.getValue(1);
344 }
345
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000346 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
347 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000348 // node so that legalize doesn't hack it.
349 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000350 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000351 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000352 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
353
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000354
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000355 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
356 // = Chain, Callee, Reg#1, Reg#2, ...
357 //
358 // Returns a chain & a flag for retval copy to use.
359 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
360 SmallVector<SDOperand, 8> Ops;
361 Ops.push_back(Chain);
362 Ops.push_back(Callee);
363
364 // Add argument registers to the end of the list so that they are
365 // known live into the call.
366 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
367 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
368 RegsToPass[i].second.getValueType()));
369
370 if (InFlag.Val)
371 Ops.push_back(InFlag);
372
373 Chain = DAG.getNode(MipsISD::JmpLink, NodeTys, &Ops[0], Ops.size());
374 InFlag = Chain.getValue(1);
375
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000376 // Create a stack location to hold GP when PIC is used. This stack
377 // location is used on function prologue to save GP and also after all
378 // emited CALL's to restore GP.
379 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000380 // Function can have an arbitrary number of calls, so
381 // hold the LastStackLoc with the biggest offset.
382 int FI;
383 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
384 if (LastStackLoc >= MipsFI->getGPStackOffset()) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000385 LastStackLoc = (!LastStackLoc) ? (16) : (LastStackLoc+4);
386 // Create the frame index only once. SPOffset here can be anything
387 // (this will be fixed on processFunctionBeforeFrameFinalized)
388 if (MipsFI->getGPStackOffset() == -1) {
389 FI = MFI->CreateFixedObject(4, 0);
390 MipsFI->setGPFI(FI);
391 }
392 MipsFI->setGPStackOffset(LastStackLoc);
393 }
394
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000395 // Reload GP value.
396 FI = MipsFI->getGPFI();
397 SDOperand FIN = DAG.getFrameIndex(FI,getPointerTy());
398 SDOperand GPLoad = DAG.getLoad(MVT::i32, Chain, FIN, NULL, 0);
399 Chain = GPLoad.getValue(1);
400 Chain = DAG.getCopyToReg(Chain, DAG.getRegister(Mips::GP, MVT::i32),
401 GPLoad, SDOperand(0,0));
402 }
403
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000404 // Create the CALLSEQ_END node.
Bill Wendling0f8d9c02007-11-13 00:44:25 +0000405 Chain = DAG.getCALLSEQ_END(Chain,
406 DAG.getConstant(NumBytes, getPointerTy()),
407 DAG.getConstant(0, getPointerTy()),
408 InFlag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000409 InFlag = Chain.getValue(1);
410
411 // Handle result values, copying them out of physregs into vregs that we
412 // return.
413 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
414}
415
416/// LowerCallResult - Lower the result values of an ISD::CALL into the
417/// appropriate copies out of appropriate physical registers. This assumes that
418/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
419/// being lowered. Returns a SDNode with the same number of values as the
420/// ISD::CALL.
421SDNode *MipsTargetLowering::
422LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
423 unsigned CallingConv, SelectionDAG &DAG) {
424
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000425 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
426
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000427 // Assign locations to each value returned by this call.
428 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000429 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
430
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000431 CCInfo.AnalyzeCallResult(TheCall, RetCC_Mips);
432 SmallVector<SDOperand, 8> ResultVals;
433
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000434 // Copy all of the result registers out of their specified physreg.
435 for (unsigned i = 0; i != RVLocs.size(); ++i) {
436 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
437 RVLocs[i].getValVT(), InFlag).getValue(1);
438 InFlag = Chain.getValue(2);
439 ResultVals.push_back(Chain.getValue(0));
440 }
441
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000442 ResultVals.push_back(Chain);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000443
444 // Merge everything together with a MERGE_VALUES node.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000445 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000446 &ResultVals[0], ResultVals.size()).Val;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000447}
448
449//===----------------------------------------------------------------------===//
450// FORMAL_ARGUMENTS Calling Convention Implementation
451//===----------------------------------------------------------------------===//
452
453/// Mips custom FORMAL_ARGUMENTS implementation
454SDOperand MipsTargetLowering::
455LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG)
456{
457 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
458 switch(CC)
459 {
460 default:
461 assert(0 && "Unsupported calling convention");
462 case CallingConv::C:
463 return LowerCCCArguments(Op, DAG);
464 }
465}
466
467/// LowerCCCArguments - transform physical registers into
468/// virtual registers and generate load operations for
469/// arguments places on the stack.
470/// TODO: isVarArg, sret
471SDOperand MipsTargetLowering::
472LowerCCCArguments(SDOperand Op, SelectionDAG &DAG)
473{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000474 SDOperand Root = Op.getOperand(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000475 MachineFunction &MF = DAG.getMachineFunction();
476 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000477 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000478
479 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
480 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
481
482 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000483
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000484 // GP holds the GOT address on PIC calls.
485 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
486 AddLiveIn(MF, Mips::GP, Mips::CPURegsRegisterClass);
487
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000488 // Assign locations to all of the incoming arguments.
489 SmallVector<CCValAssign, 16> ArgLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000490 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
491
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000492 CCInfo.AnalyzeFormalArguments(Op.Val, CC_Mips);
493 SmallVector<SDOperand, 8> ArgValues;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000494 SDOperand StackPtr;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000495
496 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
497
498 CCValAssign &VA = ArgLocs[i];
499
500 // Arguments stored on registers
501 if (VA.isRegLoc()) {
502 MVT::ValueType RegVT = VA.getLocVT();
503 TargetRegisterClass *RC;
504
505 if (RegVT == MVT::i32)
506 RC = Mips::CPURegsRegisterClass;
507 else
508 assert(0 && "support only Mips::CPURegsRegisterClass");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000509
510 // Transform the arguments stored on
511 // physical registers into virtual ones
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000512 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000513 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
514
515 // If this is an 8 or 16-bit value, it is really passed promoted
516 // to 32 bits. Insert an assert[sz]ext to capture this, then
517 // truncate to the right size.
518 if (VA.getLocInfo() == CCValAssign::SExt)
519 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
520 DAG.getValueType(VA.getValVT()));
521 else if (VA.getLocInfo() == CCValAssign::ZExt)
522 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
523 DAG.getValueType(VA.getValVT()));
524
525 if (VA.getLocInfo() != CCValAssign::Full)
526 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
527
528 ArgValues.push_back(ArgValue);
529
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000530 // To meet ABI, when VARARGS are passed on registers, the registers
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000531 // must have their values written to the caller stack frame.
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000532 if (isVarArg) {
533
534 if (StackPtr.Val == 0)
535 StackPtr = DAG.getRegister(StackReg, getPointerTy());
536
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000537 // The stack pointer offset is relative to the caller stack frame.
538 // Since the real stack size is unknown here, a negative SPOffset
539 // is used so there's a way to adjust these offsets when the stack
540 // size get known (on EliminateFrameIndex). A dummy SPOffset is
541 // used instead of a direct negative address (which is recorded to
542 // be used on emitPrologue) to avoid mis-calc of the first stack
543 // offset on PEI::calculateFrameObjectOffsets.
544 // Arguments are always 32-bit.
545 int FI = MFI->CreateFixedObject(4, 0);
546 MipsFI->recordStoreVarArgsFI(FI, -(4+(i*4)));
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000547 SDOperand PtrOff = DAG.getFrameIndex(FI, getPointerTy());
548
549 // emit ISD::STORE whichs stores the
550 // parameter value to a stack Location
551 ArgValues.push_back(DAG.getStore(Root, ArgValue, PtrOff, NULL, 0));
552 }
553
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000554 } else {
555 // sanity check
556 assert(VA.isMemLoc());
557
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000558 // The stack pointer offset is relative to the caller stack frame.
559 // Since the real stack size is unknown here, a negative SPOffset
560 // is used so there's a way to adjust these offsets when the stack
561 // size get known (on EliminateFrameIndex). A dummy SPOffset is
562 // used instead of a direct negative address (which is recorded to
563 // be used on emitPrologue) to avoid mis-calc of the first stack
564 // offset on PEI::calculateFrameObjectOffsets.
565 // Arguments are always 32-bit.
566 int FI = MFI->CreateFixedObject(4, 0);
567 MipsFI->recordLoadArgsFI(FI, -(4+(16+VA.getLocMemOffset())));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000568
569 // Create load nodes to retrieve arguments from the stack
570 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
571 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
572 }
573 }
574 ArgValues.push_back(Root);
575
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000576 // Return the new list of results.
577 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
578 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
579}
580
581//===----------------------------------------------------------------------===//
582// Return Value Calling Convention Implementation
583//===----------------------------------------------------------------------===//
584
585SDOperand MipsTargetLowering::
586LowerRET(SDOperand Op, SelectionDAG &DAG)
587{
588 // CCValAssign - represent the assignment of
589 // the return value to a location
590 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000591 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
592 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000593
594 // CCState - Info about the registers and stack slot.
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000595 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000596
597 // Analize return values of ISD::RET
598 CCInfo.AnalyzeReturn(Op.Val, RetCC_Mips);
599
600 // If this is the first return lowered for this function, add
601 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000602 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000603 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000604 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000605 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000606 }
607
608 // The chain is always operand #0
609 SDOperand Chain = Op.getOperand(0);
610 SDOperand Flag;
611
612 // Copy the result values into the output registers.
613 for (unsigned i = 0; i != RVLocs.size(); ++i) {
614 CCValAssign &VA = RVLocs[i];
615 assert(VA.isRegLoc() && "Can only return in registers!");
616
617 // ISD::RET => ret chain, (regnum1,val1), ...
618 // So i*2+1 index only the regnums
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000619 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000620
621 // guarantee that all emitted copies are
622 // stuck together, avoiding something bad
623 Flag = Chain.getValue(1);
624 }
625
626 // Return on Mips is always a "jr $ra"
627 if (Flag.Val)
628 return DAG.getNode(MipsISD::Ret, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000629 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000630 else // Return Void
631 return DAG.getNode(MipsISD::Ret, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000632 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000633}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000634
635//===----------------------------------------------------------------------===//
636// Mips Inline Assembly Support
637//===----------------------------------------------------------------------===//
638
639/// getConstraintType - Given a constraint letter, return the type of
640/// constraint it is for this target.
641MipsTargetLowering::ConstraintType MipsTargetLowering::
642getConstraintType(const std::string &Constraint) const
643{
644 if (Constraint.size() == 1) {
645 // Mips specific constrainy
646 // GCC config/mips/constraints.md
647 //
648 // 'd' : An address register. Equivalent to r
649 // unless generating MIPS16 code.
650 // 'y' : Equivalent to r; retained for
651 // backwards compatibility.
652 //
653 switch (Constraint[0]) {
654 default : break;
655 case 'd':
656 case 'y':
657 return C_RegisterClass;
658 break;
659 }
660 }
661 return TargetLowering::getConstraintType(Constraint);
662}
663
664std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
665getRegForInlineAsmConstraint(const std::string &Constraint,
666 MVT::ValueType VT) const
667{
668 if (Constraint.size() == 1) {
669 switch (Constraint[0]) {
670 case 'r':
671 return std::make_pair(0U, Mips::CPURegsRegisterClass);
672 break;
673 }
674 }
675 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
676}
677
678std::vector<unsigned> MipsTargetLowering::
679getRegClassForInlineAsmConstraint(const std::string &Constraint,
680 MVT::ValueType VT) const
681{
682 if (Constraint.size() != 1)
683 return std::vector<unsigned>();
684
685 switch (Constraint[0]) {
686 default : break;
687 case 'r':
688 // GCC Mips Constraint Letters
689 case 'd':
690 case 'y':
691 return make_vector<unsigned>(Mips::V0, Mips::V1, Mips::A0,
692 Mips::A1, Mips::A2, Mips::A3,
693 Mips::T0, Mips::T1, Mips::T2,
694 Mips::T3, Mips::T4, Mips::T5,
695 Mips::T6, Mips::T7, Mips::S0,
696 Mips::S1, Mips::S2, Mips::S3,
697 Mips::S4, Mips::S5, Mips::S6,
698 Mips::S7, Mips::T8, Mips::T9, 0);
699 break;
700 }
701 return std::vector<unsigned>();
702}