blob: d6e5ecc5c68c1f0478483afae94f115a0d686c29 [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"
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +000023#include "llvm/GlobalVariable.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000024#include "llvm/Intrinsics.h"
25#include "llvm/CallingConv.h"
26#include "llvm/CodeGen/CallingConvLower.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000031#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000032#include "llvm/CodeGen/ValueTypes.h"
33#include "llvm/Support/Debug.h"
34#include <queue>
35#include <set>
36
37using namespace llvm;
38
39const char *MipsTargetLowering::
40getTargetNodeName(unsigned Opcode) const
41{
42 switch (Opcode)
43 {
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +000044 case MipsISD::JmpLink : return "MipsISD::JmpLink";
45 case MipsISD::Hi : return "MipsISD::Hi";
46 case MipsISD::Lo : return "MipsISD::Lo";
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +000047 case MipsISD::GPRel : return "MipsISD::GPRel";
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +000048 case MipsISD::Ret : return "MipsISD::Ret";
49 case MipsISD::SelectCC : return "MipsISD::SelectCC";
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000050 case MipsISD::FPBrcond : return "MipsISD::FPBrcond";
51 case MipsISD::FPCmp : return "MipsISD::FPCmp";
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +000052 default : return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000053 }
54}
55
56MipsTargetLowering::
57MipsTargetLowering(MipsTargetMachine &TM): TargetLowering(TM)
58{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000059 Subtarget = &TM.getSubtarget<MipsSubtarget>();
60
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000061 // Mips does not have i1 type, so use i32 for
62 // setcc operations results (slt, sgt, ...).
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000063 setSetCCResultContents(ZeroOrOneSetCCResult);
64
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000065 // JumpTable targets must use GOT when using PIC_
66 setUsesGlobalOffsetTable(true);
67
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000068 // Set up the register classes
69 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
70
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000071 // When dealing with single precision only, use libcalls
72 if (!Subtarget->isSingleFloat()) {
73 addRegisterClass(MVT::f32, Mips::AFGR32RegisterClass);
74 if (!Subtarget->isFP64bit())
75 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
76 } else
77 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
78
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000079 // Load extented operations for i1 types must be promoted
80 setLoadXAction(ISD::EXTLOAD, MVT::i1, Promote);
81 setLoadXAction(ISD::ZEXTLOAD, MVT::i1, Promote);
82 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
83
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000084 // Mips Custom Operations
85 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
86 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
87 setOperationAction(ISD::RET, MVT::Other, Custom);
88 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +000089 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000090 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
91 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
92
93 // Operations not directly supported by Mips.
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +000094 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
95 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
96 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
97 setOperationAction(ISD::SELECT, MVT::i32, Expand);
98 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
99 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
100 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000101 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
102 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
103 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
104 setOperationAction(ISD::ROTL, MVT::i32, Expand);
105 setOperationAction(ISD::ROTR, MVT::i32, Expand);
106 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
107 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
108 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
109 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
110
111 // We don't have line number support yet.
112 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
113 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
114 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
115 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
116
117 // Use the default for now
118 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
119 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
120 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000121
122 if (Subtarget->isSingleFloat())
123 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000124
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000125 if (!Subtarget->hasSEInReg()) {
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000126 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000127 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
128 }
129
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000130 setStackPointerRegisterToSaveRestore(Mips::SP);
131 computeRegisterProperties();
132}
133
134
Duncan Sands83ec4b62008-06-06 12:08:01 +0000135MVT MipsTargetLowering::getSetCCResultType(const SDOperand &) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000136 return MVT::i32;
137}
138
139
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000140SDOperand MipsTargetLowering::
141LowerOperation(SDOperand Op, SelectionDAG &DAG)
142{
143 switch (Op.getOpcode())
144 {
145 case ISD::CALL: return LowerCALL(Op, DAG);
146 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
147 case ISD::RET: return LowerRET(Op, DAG);
148 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000149 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000150 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000151 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000152 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000153 }
154 return SDOperand();
155}
156
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000157MachineBasicBlock *
158MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
159 MachineBasicBlock *BB)
160{
161 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
162 switch (MI->getOpcode()) {
163 default: assert(false && "Unexpected instr type to insert");
164 case Mips::Select_CC: {
165 // To "insert" a SELECT_CC instruction, we actually have to insert the
166 // diamond control-flow pattern. The incoming instruction knows the
167 // destination vreg to set, the condition code register to branch on, the
168 // true/false values to select between, and a branch opcode to use.
169 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000170 MachineFunction::iterator It = BB;
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000171 ++It;
172
173 // thisMBB:
174 // ...
175 // TrueVal = ...
176 // setcc r1, r2, r3
177 // bNE r1, r0, copy1MBB
178 // fallthrough --> copy0MBB
179 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000180 MachineFunction *F = BB->getParent();
181 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
182 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000183 BuildMI(BB, TII->get(Mips::BNE)).addReg(MI->getOperand(1).getReg())
184 .addReg(Mips::ZERO).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000185 F->insert(It, copy0MBB);
186 F->insert(It, sinkMBB);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000187 // Update machine-CFG edges by first adding all successors of the current
188 // block to the new block which will contain the Phi node for the select.
189 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
190 e = BB->succ_end(); i != e; ++i)
191 sinkMBB->addSuccessor(*i);
192 // Next, remove all successors of the current block, and add the true
193 // and fallthrough blocks as its successors.
194 while(!BB->succ_empty())
195 BB->removeSuccessor(BB->succ_begin());
196 BB->addSuccessor(copy0MBB);
197 BB->addSuccessor(sinkMBB);
198
199 // copy0MBB:
200 // %FalseValue = ...
201 // # fallthrough to sinkMBB
202 BB = copy0MBB;
203
204 // Update machine-CFG edges
205 BB->addSuccessor(sinkMBB);
206
207 // sinkMBB:
208 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
209 // ...
210 BB = sinkMBB;
211 BuildMI(BB, TII->get(Mips::PHI), MI->getOperand(0).getReg())
212 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
213 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB);
214
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000215 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000216 return BB;
217 }
218 }
219}
220
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000221//===----------------------------------------------------------------------===//
222// Lower helper functions
223//===----------------------------------------------------------------------===//
224
225// AddLiveIn - This helper function adds the specified physical register to the
226// MachineFunction as a live in value. It also creates a corresponding
227// virtual register for it.
228static unsigned
229AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
230{
231 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000232 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
233 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000234 return VReg;
235}
236
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000237// A address must be loaded from a small section if its size is less than the
238// small section size threshold. Data in this section must be addressed using
239// gp_rel operator.
240bool MipsTargetLowering::IsInSmallSection(unsigned Size) {
241 return (Size > 0 && (Size <= Subtarget->getSSectionThreshold()));
242}
243
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000244// Discover if this global address can be placed into small data/bss section.
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000245bool MipsTargetLowering::IsGlobalInSmallSection(GlobalValue *GV)
246{
247 const TargetData *TD = getTargetData();
Bruno Cardoso Lopesfeb95cc2008-07-22 15:34:27 +0000248 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
249
250 if (!GVA)
251 return false;
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000252
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000253 const Type *Ty = GV->getType()->getElementType();
254 unsigned Size = TD->getABITypeSize(Ty);
255
256 // if this is a internal constant string, there is a special
257 // section for it, but not in small data/bss.
258 if (GVA->hasInitializer() && GV->hasInternalLinkage()) {
259 Constant *C = GVA->getInitializer();
260 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
261 if (CVA && CVA->isCString())
262 return false;
263 }
264
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000265 return IsInSmallSection(Size);
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000266}
267
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000268//===----------------------------------------------------------------------===//
269// Misc Lower Operation implementation
270//===----------------------------------------------------------------------===//
271SDOperand MipsTargetLowering::
272LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG)
273{
274 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000275 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000276
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000277 if (!Subtarget->hasABICall()) {
278 if (isa<Function>(GV)) return GA;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000279 const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000280 SDOperand Ops[] = { GA };
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000281
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000282 if (IsGlobalInSmallSection(GV)) { // %gp_rel relocation
283 SDOperand GPRelNode = DAG.getNode(MipsISD::GPRel, VTs, 1, Ops, 1);
284 SDOperand GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, MVT::i32);
285 return DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
286 }
287 // %hi/%lo relocation
288 SDOperand HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
289 SDOperand Lo = DAG.getNode(MipsISD::Lo, MVT::i32, GA);
290 return DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000291
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000292 } else { // Abicall relocations, TODO: make this cleaner.
293 SDOperand ResNode = DAG.getLoad(MVT::i32, DAG.getEntryNode(), GA, NULL, 0);
294 // On functions and global targets not internal linked only
295 // a load from got/GP is necessary for PIC to work.
296 if (!GV->hasInternalLinkage() || isa<Function>(GV))
297 return ResNode;
298 SDOperand Lo = DAG.getNode(MipsISD::Lo, MVT::i32, GA);
299 return DAG.getNode(ISD::ADD, MVT::i32, ResNode, Lo);
300 }
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000301
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000302 assert(0 && "Dont know how to handle GlobalAddress");
303 return SDOperand(0,0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000304}
305
306SDOperand MipsTargetLowering::
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000307LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG)
308{
309 assert(0 && "TLS not implemented for MIPS.");
Chris Lattnerd27c9912008-03-30 18:22:13 +0000310 return SDOperand(); // Not reached
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000311}
312
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000313SDOperand MipsTargetLowering::
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000314LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG)
315{
316 SDOperand LHS = Op.getOperand(0);
317 SDOperand RHS = Op.getOperand(1);
318 SDOperand True = Op.getOperand(2);
319 SDOperand False = Op.getOperand(3);
320 SDOperand CC = Op.getOperand(4);
321
Duncan Sands83ec4b62008-06-06 12:08:01 +0000322 const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000323 SDOperand Ops[] = { LHS, RHS, CC };
324 SDOperand SetCCRes = DAG.getNode(ISD::SETCC, VTs, 1, Ops, 3);
325
326 return DAG.getNode(MipsISD::SelectCC, True.getValueType(),
327 SetCCRes, True, False);
328}
329
330SDOperand MipsTargetLowering::
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000331LowerJumpTable(SDOperand Op, SelectionDAG &DAG)
332{
333 SDOperand ResNode;
334 SDOperand HiPart;
335
Duncan Sands83ec4b62008-06-06 12:08:01 +0000336 MVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000337 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
338 SDOperand JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
339
340 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000341 const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000342 SDOperand Ops[] = { JTI };
343 HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
344 } else // Emit Load from Global Pointer
345 HiPart = DAG.getLoad(MVT::i32, DAG.getEntryNode(), JTI, NULL, 0);
346
347 SDOperand Lo = DAG.getNode(MipsISD::Lo, MVT::i32, JTI);
348 ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
349
350 return ResNode;
351}
352
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000353SDOperand MipsTargetLowering::
354LowerConstantPool(SDOperand Op, SelectionDAG &DAG)
355{
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000356 SDOperand ResNode;
357 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
358 Constant *C = N->getConstVal();
359 SDOperand CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
360
361 // gp_rel relocation
362 if (!Subtarget->hasABICall() &&
363 IsInSmallSection(getTargetData()->getABITypeSize(C->getType()))) {
364 SDOperand GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
365 SDOperand GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, MVT::i32);
366 ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
367 } else { // %hi/%lo relocation
368 SDOperand HiPart = DAG.getNode(MipsISD::Hi, MVT::i32, CP);
369 SDOperand Lo = DAG.getNode(MipsISD::Lo, MVT::i32, CP);
370 ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
371 }
372
373 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000374}
375
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000376//===----------------------------------------------------------------------===//
377// Calling Convention Implementation
378//
379// The lower operations present on calling convention works on this order:
380// LowerCALL (virt regs --> phys regs, virt regs --> stack)
381// LowerFORMAL_ARGUMENTS (phys --> virt regs, stack --> virt regs)
382// LowerRET (virt regs --> phys regs)
383// LowerCALL (phys regs --> virt regs)
384//
385//===----------------------------------------------------------------------===//
386
387#include "MipsGenCallingConv.inc"
388
389//===----------------------------------------------------------------------===//
390// CALL Calling Convention Implementation
391//===----------------------------------------------------------------------===//
392
393/// Mips custom CALL implementation
394SDOperand MipsTargetLowering::
395LowerCALL(SDOperand Op, SelectionDAG &DAG)
396{
Chris Lattnere0b12152008-03-17 06:57:02 +0000397 unsigned CallingConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000398
399 // By now, only CallingConv::C implemented
Chris Lattnere0b12152008-03-17 06:57:02 +0000400 switch (CallingConv) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000401 default:
402 assert(0 && "Unsupported calling convention");
403 case CallingConv::Fast:
404 case CallingConv::C:
405 return LowerCCCCallTo(Op, DAG, CallingConv);
406 }
407}
408
409/// LowerCCCCallTo - functions arguments are copied from virtual
410/// regs to (physical regs)/(stack frame), CALLSEQ_START and
411/// CALLSEQ_END are emitted.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000412/// TODO: isVarArg, isTailCall.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000413SDOperand MipsTargetLowering::
414LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG, unsigned CC)
415{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000416 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000417
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000418 SDOperand Chain = Op.getOperand(0);
419 SDOperand Callee = Op.getOperand(4);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000420 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
421
422 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000423
424 // Analyze operands of the call, assigning locations to each operand.
425 SmallVector<CCValAssign, 16> ArgLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000426 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
427
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000428 // To meet O32 ABI, Mips must always allocate 16 bytes on
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000429 // the stack (even if less than 4 are used as arguments)
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000430 if (Subtarget->isABI_O32()) {
431 int VTsize = MVT(MVT::i32).getSizeInBits()/8;
432 MFI->CreateFixedObject(VTsize, (VTsize*3));
433 }
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000434
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000435 CCInfo.AnalyzeCallOperands(Op.Val, CC_Mips);
436
437 // Get a count of how many bytes are to be pushed on the stack.
438 unsigned NumBytes = CCInfo.getNextStackOffset();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000439 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes,
440 getPointerTy()));
441
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000442 // With EABI is it possible to have 16 args on registers.
443 SmallVector<std::pair<unsigned, SDOperand>, 16> RegsToPass;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000444 SmallVector<SDOperand, 8> MemOpChains;
445
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000446 // First/LastArgStackLoc contains the first/last
447 // "at stack" argument location.
448 int LastArgStackLoc = 0;
449 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000450
451 // Walk the register/memloc assignments, inserting copies/loads.
452 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
453 CCValAssign &VA = ArgLocs[i];
454
455 // Arguments start after the 5 first operands of ISD::CALL
456 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
457
458 // Promote the value if needed.
459 switch (VA.getLocInfo()) {
Chris Lattnere0b12152008-03-17 06:57:02 +0000460 default: assert(0 && "Unknown loc info!");
461 case CCValAssign::Full: break;
462 case CCValAssign::SExt:
463 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
464 break;
465 case CCValAssign::ZExt:
466 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
467 break;
468 case CCValAssign::AExt:
469 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
470 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000471 }
472
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000473 // Arguments that can be passed on register must be kept at
474 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000475 if (VA.isRegLoc()) {
476 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +0000477 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000478 }
Chris Lattnere0b12152008-03-17 06:57:02 +0000479
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000480 // Register cant get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +0000481 assert(VA.isMemLoc());
482
483 // Create the frame index object for this incoming parameter
484 // This guarantees that when allocating Local Area the firsts
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000485 // 16 bytes which are alwayes reserved won't be overwritten
486 // if O32 ABI is used. For EABI the first address is zero.
487 LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000488 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000489 LastArgStackLoc);
Chris Lattnere0b12152008-03-17 06:57:02 +0000490
491 SDOperand PtrOff = DAG.getFrameIndex(FI,getPointerTy());
492
493 // emit ISD::STORE whichs stores the
494 // parameter value to a stack Location
495 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000496 }
497
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000498 // Transform all store nodes into one single node because all store
499 // nodes are independent of each other.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000500 if (!MemOpChains.empty())
501 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
502 &MemOpChains[0], MemOpChains.size());
503
504 // Build a sequence of copy-to-reg nodes chained together with token
505 // chain and flag operands which copy the outgoing args into registers.
506 // The InFlag in necessary since all emited instructions must be
507 // stuck together.
508 SDOperand InFlag;
509 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
510 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first,
511 RegsToPass[i].second, InFlag);
512 InFlag = Chain.getValue(1);
513 }
514
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000515 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
516 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000517 // node so that legalize doesn't hack it.
518 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000519 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000520 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000521 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
522
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000523
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000524 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
525 // = Chain, Callee, Reg#1, Reg#2, ...
526 //
527 // Returns a chain & a flag for retval copy to use.
528 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
529 SmallVector<SDOperand, 8> Ops;
530 Ops.push_back(Chain);
531 Ops.push_back(Callee);
532
533 // Add argument registers to the end of the list so that they are
534 // known live into the call.
535 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
536 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
537 RegsToPass[i].second.getValueType()));
538
539 if (InFlag.Val)
540 Ops.push_back(InFlag);
541
542 Chain = DAG.getNode(MipsISD::JmpLink, NodeTys, &Ops[0], Ops.size());
543 InFlag = Chain.getValue(1);
544
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000545 // Create the CALLSEQ_END node.
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000546 Chain = DAG.getCALLSEQ_END(Chain,
547 DAG.getConstant(NumBytes, getPointerTy()),
548 DAG.getConstant(0, getPointerTy()),
549 InFlag);
550 InFlag = Chain.getValue(1);
551
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000552 // Create a stack location to hold GP when PIC is used. This stack
553 // location is used on function prologue to save GP and also after all
554 // emited CALL's to restore GP.
555 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000556 // Function can have an arbitrary number of calls, so
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000557 // hold the LastArgStackLoc with the biggest offset.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000558 int FI;
559 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000560 if (LastArgStackLoc >= MipsFI->getGPStackOffset()) {
561 LastArgStackLoc = (!LastArgStackLoc) ? (16) : (LastArgStackLoc+4);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000562 // Create the frame index only once. SPOffset here can be anything
563 // (this will be fixed on processFunctionBeforeFrameFinalized)
564 if (MipsFI->getGPStackOffset() == -1) {
565 FI = MFI->CreateFixedObject(4, 0);
566 MipsFI->setGPFI(FI);
567 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000568 MipsFI->setGPStackOffset(LastArgStackLoc);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000569 }
570
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000571 // Reload GP value.
572 FI = MipsFI->getGPFI();
573 SDOperand FIN = DAG.getFrameIndex(FI,getPointerTy());
574 SDOperand GPLoad = DAG.getLoad(MVT::i32, Chain, FIN, NULL, 0);
575 Chain = GPLoad.getValue(1);
576 Chain = DAG.getCopyToReg(Chain, DAG.getRegister(Mips::GP, MVT::i32),
577 GPLoad, SDOperand(0,0));
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000578 InFlag = Chain.getValue(1);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000579 }
580
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000581 // Handle result values, copying them out of physregs into vregs that we
582 // return.
583 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
584}
585
586/// LowerCallResult - Lower the result values of an ISD::CALL into the
587/// appropriate copies out of appropriate physical registers. This assumes that
588/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
589/// being lowered. Returns a SDNode with the same number of values as the
590/// ISD::CALL.
591SDNode *MipsTargetLowering::
592LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
593 unsigned CallingConv, SelectionDAG &DAG) {
594
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000595 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
596
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000597 // Assign locations to each value returned by this call.
598 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000599 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
600
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000601 CCInfo.AnalyzeCallResult(TheCall, RetCC_Mips);
602 SmallVector<SDOperand, 8> ResultVals;
603
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000604 // Copy all of the result registers out of their specified physreg.
605 for (unsigned i = 0; i != RVLocs.size(); ++i) {
606 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
607 RVLocs[i].getValVT(), InFlag).getValue(1);
608 InFlag = Chain.getValue(2);
609 ResultVals.push_back(Chain.getValue(0));
610 }
611
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000612 ResultVals.push_back(Chain);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000613
614 // Merge everything together with a MERGE_VALUES node.
Duncan Sandsf9516202008-06-30 10:19:09 +0000615 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
616 ResultVals.size()).Val;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000617}
618
619//===----------------------------------------------------------------------===//
620// FORMAL_ARGUMENTS Calling Convention Implementation
621//===----------------------------------------------------------------------===//
622
623/// Mips custom FORMAL_ARGUMENTS implementation
624SDOperand MipsTargetLowering::
625LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG)
626{
627 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
628 switch(CC)
629 {
630 default:
631 assert(0 && "Unsupported calling convention");
632 case CallingConv::C:
633 return LowerCCCArguments(Op, DAG);
634 }
635}
636
637/// LowerCCCArguments - transform physical registers into
638/// virtual registers and generate load operations for
639/// arguments places on the stack.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000640/// TODO: isVarArg
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000641SDOperand MipsTargetLowering::
642LowerCCCArguments(SDOperand Op, SelectionDAG &DAG)
643{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000644 SDOperand Root = Op.getOperand(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000645 MachineFunction &MF = DAG.getMachineFunction();
646 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000647 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000648
649 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
650 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
651
652 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000653
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000654 // GP must be live into PIC and non-PIC call target.
655 AddLiveIn(MF, Mips::GP, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000656
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000657 // Assign locations to all of the incoming arguments.
658 SmallVector<CCValAssign, 16> ArgLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000659 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
660
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000661 CCInfo.AnalyzeFormalArguments(Op.Val, CC_Mips);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000662 SmallVector<SDOperand, 16> ArgValues;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000663 SDOperand StackPtr;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000664
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000665 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
666
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000667 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
668
669 CCValAssign &VA = ArgLocs[i];
670
671 // Arguments stored on registers
672 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000673 MVT RegVT = VA.getLocVT();
Bill Wendling06b8c192008-07-09 05:55:53 +0000674 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000675
676 if (RegVT == MVT::i32)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000677 RC = Mips::CPURegsRegisterClass;
678 else if (RegVT == MVT::f32) {
679 if (Subtarget->isSingleFloat())
680 RC = Mips::FGR32RegisterClass;
681 else
682 RC = Mips::AFGR32RegisterClass;
683 } else if (RegVT == MVT::f64) {
684 if (!Subtarget->isSingleFloat())
685 RC = Mips::AFGR64RegisterClass;
686 } else
687 assert(0 && "RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000688
689 // Transform the arguments stored on
690 // physical registers into virtual ones
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000691 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000692 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
693
694 // If this is an 8 or 16-bit value, it is really passed promoted
695 // to 32 bits. Insert an assert[sz]ext to capture this, then
696 // truncate to the right size.
697 if (VA.getLocInfo() == CCValAssign::SExt)
698 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
699 DAG.getValueType(VA.getValVT()));
700 else if (VA.getLocInfo() == CCValAssign::ZExt)
701 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
702 DAG.getValueType(VA.getValVT()));
703
704 if (VA.getLocInfo() != CCValAssign::Full)
705 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
706
707 ArgValues.push_back(ArgValue);
708
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000709 // To meet ABI, when VARARGS are passed on registers, the registers
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000710 // must have their values written to the caller stack frame.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000711 if ((isVarArg) && (Subtarget->isABI_O32())) {
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000712 if (StackPtr.Val == 0)
713 StackPtr = DAG.getRegister(StackReg, getPointerTy());
714
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000715 // The stack pointer offset is relative to the caller stack frame.
716 // Since the real stack size is unknown here, a negative SPOffset
717 // is used so there's a way to adjust these offsets when the stack
718 // size get known (on EliminateFrameIndex). A dummy SPOffset is
719 // used instead of a direct negative address (which is recorded to
720 // be used on emitPrologue) to avoid mis-calc of the first stack
721 // offset on PEI::calculateFrameObjectOffsets.
722 // Arguments are always 32-bit.
723 int FI = MFI->CreateFixedObject(4, 0);
724 MipsFI->recordStoreVarArgsFI(FI, -(4+(i*4)));
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000725 SDOperand PtrOff = DAG.getFrameIndex(FI, getPointerTy());
726
727 // emit ISD::STORE whichs stores the
728 // parameter value to a stack Location
729 ArgValues.push_back(DAG.getStore(Root, ArgValue, PtrOff, NULL, 0));
730 }
731
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000732 } else { // VA.isRegLoc()
733
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000734 // sanity check
735 assert(VA.isMemLoc());
736
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000737 // The stack pointer offset is relative to the caller stack frame.
738 // Since the real stack size is unknown here, a negative SPOffset
739 // is used so there's a way to adjust these offsets when the stack
740 // size get known (on EliminateFrameIndex). A dummy SPOffset is
741 // used instead of a direct negative address (which is recorded to
742 // be used on emitPrologue) to avoid mis-calc of the first stack
743 // offset on PEI::calculateFrameObjectOffsets.
744 // Arguments are always 32-bit.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000745 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
746 int FI = MFI->CreateFixedObject(ArgSize, 0);
747 MipsFI->recordLoadArgsFI(FI, -(ArgSize+
748 (FirstStackArgLoc + VA.getLocMemOffset())));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000749
750 // Create load nodes to retrieve arguments from the stack
751 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
752 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
753 }
754 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000755
756 // The mips ABIs for returning structs by value requires that we copy
757 // the sret argument into $v0 for the return. Save the argument into
758 // a virtual register so that we can access it from the return points.
759 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
760 unsigned Reg = MipsFI->getSRetReturnReg();
761 if (!Reg) {
762 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
763 MipsFI->setSRetReturnReg(Reg);
764 }
765 SDOperand Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
766 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
767 }
768
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000769 ArgValues.push_back(Root);
770
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000771 // Return the new list of results.
Duncan Sandsf9516202008-06-30 10:19:09 +0000772 return DAG.getMergeValues(Op.Val->getVTList(), &ArgValues[0],
773 ArgValues.size()).getValue(Op.ResNo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000774}
775
776//===----------------------------------------------------------------------===//
777// Return Value Calling Convention Implementation
778//===----------------------------------------------------------------------===//
779
780SDOperand MipsTargetLowering::
781LowerRET(SDOperand Op, SelectionDAG &DAG)
782{
783 // CCValAssign - represent the assignment of
784 // the return value to a location
785 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000786 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
787 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000788
789 // CCState - Info about the registers and stack slot.
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000790 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000791
792 // Analize return values of ISD::RET
793 CCInfo.AnalyzeReturn(Op.Val, RetCC_Mips);
794
795 // If this is the first return lowered for this function, add
796 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000797 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000798 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000799 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000800 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000801 }
802
803 // The chain is always operand #0
804 SDOperand Chain = Op.getOperand(0);
805 SDOperand Flag;
806
807 // Copy the result values into the output registers.
808 for (unsigned i = 0; i != RVLocs.size(); ++i) {
809 CCValAssign &VA = RVLocs[i];
810 assert(VA.isRegLoc() && "Can only return in registers!");
811
812 // ISD::RET => ret chain, (regnum1,val1), ...
813 // So i*2+1 index only the regnums
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000814 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000815
816 // guarantee that all emitted copies are
817 // stuck together, avoiding something bad
818 Flag = Chain.getValue(1);
819 }
820
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000821 // The mips ABIs for returning structs by value requires that we copy
822 // the sret argument into $v0 for the return. We saved the argument into
823 // a virtual register in the entry block, so now we copy the value out
824 // and into $v0.
825 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
826 MachineFunction &MF = DAG.getMachineFunction();
827 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
828 unsigned Reg = MipsFI->getSRetReturnReg();
829
830 if (!Reg)
831 assert(0 && "sret virtual register not created in the entry block");
832 SDOperand Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
833
834 Chain = DAG.getCopyToReg(Chain, Mips::V0, Val, Flag);
835 Flag = Chain.getValue(1);
836 }
837
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000838 // Return on Mips is always a "jr $ra"
839 if (Flag.Val)
840 return DAG.getNode(MipsISD::Ret, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000841 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000842 else // Return Void
843 return DAG.getNode(MipsISD::Ret, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000844 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000845}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000846
847//===----------------------------------------------------------------------===//
848// Mips Inline Assembly Support
849//===----------------------------------------------------------------------===//
850
851/// getConstraintType - Given a constraint letter, return the type of
852/// constraint it is for this target.
853MipsTargetLowering::ConstraintType MipsTargetLowering::
854getConstraintType(const std::string &Constraint) const
855{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000856 // Mips specific constrainy
857 // GCC config/mips/constraints.md
858 //
859 // 'd' : An address register. Equivalent to r
860 // unless generating MIPS16 code.
861 // 'y' : Equivalent to r; retained for
862 // backwards compatibility.
Bruno Cardoso Lopes7b76da12008-07-09 04:45:36 +0000863 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000864 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000865 switch (Constraint[0]) {
866 default : break;
867 case 'd':
868 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000869 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000870 return C_RegisterClass;
871 break;
872 }
873 }
874 return TargetLowering::getConstraintType(Constraint);
875}
876
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000877/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
878/// return a list of registers that can be used to satisfy the constraint.
879/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000880std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +0000881getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000882{
883 if (Constraint.size() == 1) {
884 switch (Constraint[0]) {
885 case 'r':
886 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000887 case 'f':
Duncan Sands15126422008-07-08 09:33:14 +0000888 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000889 if (Subtarget->isSingleFloat())
890 return std::make_pair(0U, Mips::FGR32RegisterClass);
891 else
892 return std::make_pair(0U, Mips::AFGR32RegisterClass);
Duncan Sands15126422008-07-08 09:33:14 +0000893 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000894 if (VT == MVT::f64)
895 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
896 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000897 }
898 }
899 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
900}
901
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000902/// Given a register class constraint, like 'r', if this corresponds directly
903/// to an LLVM register class, return a register of 0 and the register class
904/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000905std::vector<unsigned> MipsTargetLowering::
906getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000907 MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000908{
909 if (Constraint.size() != 1)
910 return std::vector<unsigned>();
911
912 switch (Constraint[0]) {
913 default : break;
914 case 'r':
915 // GCC Mips Constraint Letters
916 case 'd':
917 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000918 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
919 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
920 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
921 Mips::T8, 0);
922
923 case 'f':
Duncan Sands15126422008-07-08 09:33:14 +0000924 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000925 if (Subtarget->isSingleFloat())
926 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
927 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
928 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
929 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
930 Mips::F30, Mips::F31, 0);
931 else
932 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
933 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
934 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +0000935 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000936
937 if (VT == MVT::f64)
938 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
939 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
940 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
941 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000942 }
943 return std::vector<unsigned>();
944}