blob: 29dc7a32acb59795d643f24892c71a92deea0c50 [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 Lopes6d399bd2008-07-29 19:05:28 +000044 case MipsISD::JmpLink : return "MipsISD::JmpLink";
45 case MipsISD::Hi : return "MipsISD::Hi";
46 case MipsISD::Lo : return "MipsISD::Lo";
47 case MipsISD::GPRel : return "MipsISD::GPRel";
48 case MipsISD::Ret : return "MipsISD::Ret";
49 case MipsISD::SelectCC : return "MipsISD::SelectCC";
50 case MipsISD::FPSelectCC : return "MipsISD::FPSelectCC";
51 case MipsISD::FPBrcond : return "MipsISD::FPBrcond";
52 case MipsISD::FPCmp : return "MipsISD::FPCmp";
53 default : return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000054 }
55}
56
57MipsTargetLowering::
58MipsTargetLowering(MipsTargetMachine &TM): TargetLowering(TM)
59{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000060 Subtarget = &TM.getSubtarget<MipsSubtarget>();
61
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000062 // Mips does not have i1 type, so use i32 for
63 // setcc operations results (slt, sgt, ...).
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000064 setSetCCResultContents(ZeroOrOneSetCCResult);
65
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000066 // JumpTable targets must use GOT when using PIC_
67 setUsesGlobalOffsetTable(true);
68
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000069 // Set up the register classes
70 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
71
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000072 // When dealing with single precision only, use libcalls
73 if (!Subtarget->isSingleFloat()) {
74 addRegisterClass(MVT::f32, Mips::AFGR32RegisterClass);
75 if (!Subtarget->isFP64bit())
76 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
77 } else
78 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
79
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000080 // Load extented operations for i1 types must be promoted
81 setLoadXAction(ISD::EXTLOAD, MVT::i1, Promote);
82 setLoadXAction(ISD::ZEXTLOAD, MVT::i1, Promote);
83 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
84
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000085 // Mips Custom Operations
86 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
87 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
88 setOperationAction(ISD::RET, MVT::Other, Custom);
89 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +000090 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000091 setOperationAction(ISD::SELECT, MVT::f32, Custom);
92 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000093 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +000094 setOperationAction(ISD::SETCC, MVT::f32, Custom);
95 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000096
97 // Operations not directly supported by Mips.
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +000098 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
99 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
100 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000101 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
102 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
103 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000104 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
105 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
106 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
107 setOperationAction(ISD::ROTL, MVT::i32, Expand);
108 setOperationAction(ISD::ROTR, MVT::i32, Expand);
109 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
110 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
111 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
112 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
113
114 // We don't have line number support yet.
115 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
116 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
117 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
118 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
119
120 // Use the default for now
121 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
122 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
123 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000124
125 if (Subtarget->isSingleFloat())
126 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000127
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000128 if (!Subtarget->hasSEInReg()) {
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000129 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000130 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
131 }
132
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000133 setStackPointerRegisterToSaveRestore(Mips::SP);
134 computeRegisterProperties();
135}
136
137
Dan Gohman475871a2008-07-27 21:46:04 +0000138MVT MipsTargetLowering::getSetCCResultType(const SDValue &) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000139 return MVT::i32;
140}
141
142
Dan Gohman475871a2008-07-27 21:46:04 +0000143SDValue MipsTargetLowering::
144LowerOperation(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000145{
146 switch (Op.getOpcode())
147 {
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000148 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000149 case ISD::CALL: return LowerCALL(Op, DAG);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000150 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000151 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000152 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000153 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000154 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000155 case ISD::RET: return LowerRET(Op, DAG);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000156 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000157 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000158 case ISD::SETCC: return LowerSETCC(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000159 }
Dan Gohman475871a2008-07-27 21:46:04 +0000160 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000161}
162
163//===----------------------------------------------------------------------===//
164// Lower helper functions
165//===----------------------------------------------------------------------===//
166
167// AddLiveIn - This helper function adds the specified physical register to the
168// MachineFunction as a live in value. It also creates a corresponding
169// virtual register for it.
170static unsigned
171AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
172{
173 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000174 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
175 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000176 return VReg;
177}
178
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000179// A address must be loaded from a small section if its size is less than the
180// small section size threshold. Data in this section must be addressed using
181// gp_rel operator.
182bool MipsTargetLowering::IsInSmallSection(unsigned Size) {
183 return (Size > 0 && (Size <= Subtarget->getSSectionThreshold()));
184}
185
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000186// Discover if this global address can be placed into small data/bss section.
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000187bool MipsTargetLowering::IsGlobalInSmallSection(GlobalValue *GV)
188{
189 const TargetData *TD = getTargetData();
Bruno Cardoso Lopesfeb95cc2008-07-22 15:34:27 +0000190 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
191
192 if (!GVA)
193 return false;
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000194
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000195 const Type *Ty = GV->getType()->getElementType();
196 unsigned Size = TD->getABITypeSize(Ty);
197
198 // if this is a internal constant string, there is a special
199 // section for it, but not in small data/bss.
200 if (GVA->hasInitializer() && GV->hasInternalLinkage()) {
201 Constant *C = GVA->getInitializer();
202 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
203 if (CVA && CVA->isCString())
204 return false;
205 }
206
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000207 return IsInSmallSection(Size);
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000208}
209
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000210// Get fp branch code (not opcode) from condition code.
211static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
212 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
213 return Mips::BRANCH_T;
214
215 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
216 return Mips::BRANCH_F;
217
218 return Mips::BRANCH_INVALID;
219}
220
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000221static unsigned FPBranchCodeToOpc(Mips::FPBranchCode BC) {
222 switch(BC) {
223 default:
224 assert(0 && "Unknown branch code");
225 case Mips::BRANCH_T : return Mips::BC1T;
226 case Mips::BRANCH_F : return Mips::BC1F;
227 case Mips::BRANCH_TL : return Mips::BC1TL;
228 case Mips::BRANCH_FL : return Mips::BC1FL;
229 }
230}
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000231
232static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
233 switch (CC) {
234 default: assert(0 && "Unknown fp condition code!");
235 case ISD::SETEQ:
236 case ISD::SETOEQ: return Mips::FCOND_EQ;
237 case ISD::SETUNE: return Mips::FCOND_OGL;
238 case ISD::SETLT:
239 case ISD::SETOLT: return Mips::FCOND_OLT;
240 case ISD::SETGT:
241 case ISD::SETOGT: return Mips::FCOND_OGT;
242 case ISD::SETLE:
243 case ISD::SETOLE: return Mips::FCOND_OLE;
244 case ISD::SETGE:
245 case ISD::SETOGE: return Mips::FCOND_OGE;
246 case ISD::SETULT: return Mips::FCOND_ULT;
247 case ISD::SETULE: return Mips::FCOND_ULE;
248 case ISD::SETUGT: return Mips::FCOND_UGT;
249 case ISD::SETUGE: return Mips::FCOND_UGE;
250 case ISD::SETUO: return Mips::FCOND_UN;
251 case ISD::SETO: return Mips::FCOND_OR;
252 case ISD::SETNE:
253 case ISD::SETONE: return Mips::FCOND_NEQ;
254 case ISD::SETUEQ: return Mips::FCOND_UEQ;
255 }
256}
257
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000258MachineBasicBlock *
259MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
260 MachineBasicBlock *BB)
261{
262 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
263 bool isFPCmp = false;
264
265 switch (MI->getOpcode()) {
266 default: assert(false && "Unexpected instr type to insert");
267 case Mips::Select_FCC:
268 case Mips::Select_FCC_SO32:
269 case Mips::Select_FCC_AS32:
270 case Mips::Select_FCC_D32:
271 isFPCmp = true; // FALL THROUGH
272 case Mips::Select_CC:
273 case Mips::Select_CC_SO32:
274 case Mips::Select_CC_AS32:
275 case Mips::Select_CC_D32: {
276 // To "insert" a SELECT_CC instruction, we actually have to insert the
277 // diamond control-flow pattern. The incoming instruction knows the
278 // destination vreg to set, the condition code register to branch on, the
279 // true/false values to select between, and a branch opcode to use.
280 const BasicBlock *LLVM_BB = BB->getBasicBlock();
281 MachineFunction::iterator It = BB;
282 ++It;
283
284 // thisMBB:
285 // ...
286 // TrueVal = ...
287 // setcc r1, r2, r3
288 // bNE r1, r0, copy1MBB
289 // fallthrough --> copy0MBB
290 MachineBasicBlock *thisMBB = BB;
291 MachineFunction *F = BB->getParent();
292 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
293 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
294
295 // Emit the right instruction according to the type of the operands compared
296 if (isFPCmp) {
297 // Find the condiction code present in the setcc operation.
298 Mips::CondCode CC = (Mips::CondCode)MI->getOperand(4).getImm();
299 // Get the branch opcode from the branch code.
300 unsigned Opc = FPBranchCodeToOpc(GetFPBranchCodeFromCond(CC));
301 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
302 } else
303 BuildMI(BB, TII->get(Mips::BNE)).addReg(MI->getOperand(1).getReg())
304 .addReg(Mips::ZERO).addMBB(sinkMBB);
305
306 F->insert(It, copy0MBB);
307 F->insert(It, sinkMBB);
308 // Update machine-CFG edges by first adding all successors of the current
309 // block to the new block which will contain the Phi node for the select.
310 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
311 e = BB->succ_end(); i != e; ++i)
312 sinkMBB->addSuccessor(*i);
313 // Next, remove all successors of the current block, and add the true
314 // and fallthrough blocks as its successors.
315 while(!BB->succ_empty())
316 BB->removeSuccessor(BB->succ_begin());
317 BB->addSuccessor(copy0MBB);
318 BB->addSuccessor(sinkMBB);
319
320 // copy0MBB:
321 // %FalseValue = ...
322 // # fallthrough to sinkMBB
323 BB = copy0MBB;
324
325 // Update machine-CFG edges
326 BB->addSuccessor(sinkMBB);
327
328 // sinkMBB:
329 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
330 // ...
331 BB = sinkMBB;
332 BuildMI(BB, TII->get(Mips::PHI), MI->getOperand(0).getReg())
333 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
334 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB);
335
336 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
337 return BB;
338 }
339 }
340}
341
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000342//===----------------------------------------------------------------------===//
343// Misc Lower Operation implementation
344//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000345
Dan Gohman475871a2008-07-27 21:46:04 +0000346SDValue MipsTargetLowering::
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000347LowerBRCOND(SDValue Op, SelectionDAG &DAG)
348{
349 // The first operand is the chain, the second is the condition, the third is
350 // the block to branch to if the condition is true.
351 SDValue Chain = Op.getOperand(0);
352 SDValue Dest = Op.getOperand(2);
353 SDValue CondRes;
354
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000355 if (Op.getOperand(1).getOpcode() == ISD::AND) {
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000356 CondRes = Op.getOperand(1).getOperand(0);
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000357 if (CondRes.getOpcode() != MipsISD::FPCmp)
358 return Op;
359 } else if (Op.getOperand(1).getOpcode() == MipsISD::FPCmp)
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000360 CondRes = Op.getOperand(1);
361 else
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000362 return Op;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000363
364 SDValue CCNode = CondRes.getOperand(2);
365 Mips::CondCode CC = (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getValue();
366 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
367
368 return DAG.getNode(MipsISD::FPBrcond, Op.getValueType(), Chain, BrCode,
369 Dest, CondRes);
370}
371
372SDValue MipsTargetLowering::
373LowerSETCC(SDValue Op, SelectionDAG &DAG)
374{
375 // The operands to this are the left and right operands to compare (ops #0,
376 // and #1) and the condition code to compare them with (op #2) as a
377 // CondCodeSDNode.
378 SDValue LHS = Op.getOperand(0);
379 SDValue RHS = Op.getOperand(1);
380
381 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
382
383 return DAG.getNode(MipsISD::FPCmp, Op.getValueType(), LHS, RHS,
384 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
385}
386
387SDValue MipsTargetLowering::
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000388LowerSELECT(SDValue Op, SelectionDAG &DAG)
389{
390 SDValue Cond = Op.getOperand(0);
391 SDValue True = Op.getOperand(1);
392 SDValue False = Op.getOperand(2);
393
394 // this can be a fp select but with a setcc comming from a
395 // integer compare.
396 if (Cond.getOpcode() == ISD::SETCC)
397 if (Cond.getOperand(0).getValueType().isInteger())
398 return DAG.getNode(MipsISD::SelectCC, True.getValueType(),
399 Cond, True, False);
400
401 // Otherwise we're dealing with floating point compare.
402 SDValue CondRes;
403 if (Cond.getOpcode() == ISD::AND)
404 CondRes = Cond.getOperand(0);
405 else if (Cond.getOpcode() == MipsISD::FPCmp)
406 CondRes = Cond;
407 else
408 assert(0 && "Incoming condition flag unknown");
409
410 SDValue CCNode = CondRes.getOperand(2);
411 return DAG.getNode(MipsISD::FPSelectCC, True.getValueType(),
412 CondRes, True, False, CCNode);
413}
414
415SDValue MipsTargetLowering::
Dan Gohman475871a2008-07-27 21:46:04 +0000416LowerSELECT_CC(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000417{
Dan Gohman475871a2008-07-27 21:46:04 +0000418 SDValue LHS = Op.getOperand(0);
419 SDValue RHS = Op.getOperand(1);
420 SDValue True = Op.getOperand(2);
421 SDValue False = Op.getOperand(3);
422 SDValue CC = Op.getOperand(4);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000423
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000424 SDValue SetCCRes = DAG.getNode(ISD::SETCC, LHS.getValueType(), LHS, RHS, CC);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000425 return DAG.getNode(MipsISD::SelectCC, True.getValueType(),
426 SetCCRes, True, False);
427}
428
Dan Gohman475871a2008-07-27 21:46:04 +0000429SDValue MipsTargetLowering::
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000430LowerGlobalAddress(SDValue Op, SelectionDAG &DAG)
431{
432 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
433 SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
434
435 if (!Subtarget->hasABICall()) {
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000436 const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
437 SDValue Ops[] = { GA };
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000438 // %gp_rel relocation
439 if (!isa<Function>(GV) && IsGlobalInSmallSection(GV)) {
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000440 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, VTs, 1, Ops, 1);
441 SDValue GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, MVT::i32);
442 return DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
443 }
444 // %hi/%lo relocation
445 SDValue HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
446 SDValue Lo = DAG.getNode(MipsISD::Lo, MVT::i32, GA);
447 return DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
448
449 } else { // Abicall relocations, TODO: make this cleaner.
450 SDValue ResNode = DAG.getLoad(MVT::i32, DAG.getEntryNode(), GA, NULL, 0);
451 // On functions and global targets not internal linked only
452 // a load from got/GP is necessary for PIC to work.
453 if (!GV->hasInternalLinkage() || isa<Function>(GV))
454 return ResNode;
455 SDValue Lo = DAG.getNode(MipsISD::Lo, MVT::i32, GA);
456 return DAG.getNode(ISD::ADD, MVT::i32, ResNode, Lo);
457 }
458
459 assert(0 && "Dont know how to handle GlobalAddress");
460 return SDValue(0,0);
461}
462
463SDValue MipsTargetLowering::
464LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG)
465{
466 assert(0 && "TLS not implemented for MIPS.");
467 return SDValue(); // Not reached
468}
469
470SDValue MipsTargetLowering::
Dan Gohman475871a2008-07-27 21:46:04 +0000471LowerJumpTable(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000472{
Dan Gohman475871a2008-07-27 21:46:04 +0000473 SDValue ResNode;
474 SDValue HiPart;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000475
Duncan Sands83ec4b62008-06-06 12:08:01 +0000476 MVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000477 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +0000478 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000479
480 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000481 const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000482 SDValue Ops[] = { JTI };
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000483 HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
484 } else // Emit Load from Global Pointer
485 HiPart = DAG.getLoad(MVT::i32, DAG.getEntryNode(), JTI, NULL, 0);
486
Dan Gohman475871a2008-07-27 21:46:04 +0000487 SDValue Lo = DAG.getNode(MipsISD::Lo, MVT::i32, JTI);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000488 ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
489
490 return ResNode;
491}
492
Dan Gohman475871a2008-07-27 21:46:04 +0000493SDValue MipsTargetLowering::
494LowerConstantPool(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000495{
Dan Gohman475871a2008-07-27 21:46:04 +0000496 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000497 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
498 Constant *C = N->getConstVal();
Dan Gohman475871a2008-07-27 21:46:04 +0000499 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000500
501 // gp_rel relocation
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000502 // FIXME: we should reference the constant pool using small data sections,
503 // but the asm printer currently doens't support this feature without
504 // hacking it. This feature should come soon so we can uncomment the
505 // stuff below.
506 //if (!Subtarget->hasABICall() &&
507 // IsInSmallSection(getTargetData()->getABITypeSize(C->getType()))) {
508 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
509 // SDValue GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, MVT::i32);
510 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
511 //} else { // %hi/%lo relocation
Dan Gohman475871a2008-07-27 21:46:04 +0000512 SDValue HiPart = DAG.getNode(MipsISD::Hi, MVT::i32, CP);
513 SDValue Lo = DAG.getNode(MipsISD::Lo, MVT::i32, CP);
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000514 ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000515 //}
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000516
517 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000518}
519
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000520//===----------------------------------------------------------------------===//
521// Calling Convention Implementation
522//
523// The lower operations present on calling convention works on this order:
524// LowerCALL (virt regs --> phys regs, virt regs --> stack)
525// LowerFORMAL_ARGUMENTS (phys --> virt regs, stack --> virt regs)
526// LowerRET (virt regs --> phys regs)
527// LowerCALL (phys regs --> virt regs)
528//
529//===----------------------------------------------------------------------===//
530
531#include "MipsGenCallingConv.inc"
532
533//===----------------------------------------------------------------------===//
534// CALL Calling Convention Implementation
535//===----------------------------------------------------------------------===//
536
537/// Mips custom CALL implementation
Dan Gohman475871a2008-07-27 21:46:04 +0000538SDValue MipsTargetLowering::
539LowerCALL(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000540{
Chris Lattnere0b12152008-03-17 06:57:02 +0000541 unsigned CallingConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000542
543 // By now, only CallingConv::C implemented
Chris Lattnere0b12152008-03-17 06:57:02 +0000544 switch (CallingConv) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000545 default:
546 assert(0 && "Unsupported calling convention");
547 case CallingConv::Fast:
548 case CallingConv::C:
549 return LowerCCCCallTo(Op, DAG, CallingConv);
550 }
551}
552
553/// LowerCCCCallTo - functions arguments are copied from virtual
554/// regs to (physical regs)/(stack frame), CALLSEQ_START and
555/// CALLSEQ_END are emitted.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000556/// TODO: isVarArg, isTailCall.
Dan Gohman475871a2008-07-27 21:46:04 +0000557SDValue MipsTargetLowering::
558LowerCCCCallTo(SDValue Op, SelectionDAG &DAG, unsigned CC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000559{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000560 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000561
Dan Gohman475871a2008-07-27 21:46:04 +0000562 SDValue Chain = Op.getOperand(0);
563 SDValue Callee = Op.getOperand(4);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000564 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
565
566 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000567
568 // Analyze operands of the call, assigning locations to each operand.
569 SmallVector<CCValAssign, 16> ArgLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000570 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
571
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000572 // To meet O32 ABI, Mips must always allocate 16 bytes on
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000573 // the stack (even if less than 4 are used as arguments)
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000574 if (Subtarget->isABI_O32()) {
575 int VTsize = MVT(MVT::i32).getSizeInBits()/8;
576 MFI->CreateFixedObject(VTsize, (VTsize*3));
577 }
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000578
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000579 CCInfo.AnalyzeCallOperands(Op.Val, CC_Mips);
580
581 // Get a count of how many bytes are to be pushed on the stack.
582 unsigned NumBytes = CCInfo.getNextStackOffset();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000583 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes,
584 getPointerTy()));
585
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000586 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +0000587 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
588 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000589
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000590 // First/LastArgStackLoc contains the first/last
591 // "at stack" argument location.
592 int LastArgStackLoc = 0;
593 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000594
595 // Walk the register/memloc assignments, inserting copies/loads.
596 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
597 CCValAssign &VA = ArgLocs[i];
598
599 // Arguments start after the 5 first operands of ISD::CALL
Dan Gohman475871a2008-07-27 21:46:04 +0000600 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000601
602 // Promote the value if needed.
603 switch (VA.getLocInfo()) {
Chris Lattnere0b12152008-03-17 06:57:02 +0000604 default: assert(0 && "Unknown loc info!");
605 case CCValAssign::Full: break;
606 case CCValAssign::SExt:
607 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
608 break;
609 case CCValAssign::ZExt:
610 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
611 break;
612 case CCValAssign::AExt:
613 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
614 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000615 }
616
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000617 // Arguments that can be passed on register must be kept at
618 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000619 if (VA.isRegLoc()) {
620 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +0000621 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000622 }
Chris Lattnere0b12152008-03-17 06:57:02 +0000623
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000624 // Register cant get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +0000625 assert(VA.isMemLoc());
626
627 // Create the frame index object for this incoming parameter
628 // This guarantees that when allocating Local Area the firsts
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000629 // 16 bytes which are alwayes reserved won't be overwritten
630 // if O32 ABI is used. For EABI the first address is zero.
631 LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000632 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000633 LastArgStackLoc);
Chris Lattnere0b12152008-03-17 06:57:02 +0000634
Dan Gohman475871a2008-07-27 21:46:04 +0000635 SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +0000636
637 // emit ISD::STORE whichs stores the
638 // parameter value to a stack Location
639 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000640 }
641
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000642 // Transform all store nodes into one single node because all store
643 // nodes are independent of each other.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000644 if (!MemOpChains.empty())
645 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
646 &MemOpChains[0], MemOpChains.size());
647
648 // Build a sequence of copy-to-reg nodes chained together with token
649 // chain and flag operands which copy the outgoing args into registers.
650 // The InFlag in necessary since all emited instructions must be
651 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000652 SDValue InFlag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000653 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
654 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first,
655 RegsToPass[i].second, InFlag);
656 InFlag = Chain.getValue(1);
657 }
658
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000659 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
660 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000661 // node so that legalize doesn't hack it.
662 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000663 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000664 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000665 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
666
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000667
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000668 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
669 // = Chain, Callee, Reg#1, Reg#2, ...
670 //
671 // Returns a chain & a flag for retval copy to use.
672 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000673 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000674 Ops.push_back(Chain);
675 Ops.push_back(Callee);
676
677 // Add argument registers to the end of the list so that they are
678 // known live into the call.
679 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
680 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
681 RegsToPass[i].second.getValueType()));
682
683 if (InFlag.Val)
684 Ops.push_back(InFlag);
685
686 Chain = DAG.getNode(MipsISD::JmpLink, NodeTys, &Ops[0], Ops.size());
687 InFlag = Chain.getValue(1);
688
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000689 // Create the CALLSEQ_END node.
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000690 Chain = DAG.getCALLSEQ_END(Chain,
691 DAG.getConstant(NumBytes, getPointerTy()),
692 DAG.getConstant(0, getPointerTy()),
693 InFlag);
694 InFlag = Chain.getValue(1);
695
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000696 // Create a stack location to hold GP when PIC is used. This stack
697 // location is used on function prologue to save GP and also after all
698 // emited CALL's to restore GP.
699 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000700 // Function can have an arbitrary number of calls, so
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000701 // hold the LastArgStackLoc with the biggest offset.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000702 int FI;
703 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000704 if (LastArgStackLoc >= MipsFI->getGPStackOffset()) {
705 LastArgStackLoc = (!LastArgStackLoc) ? (16) : (LastArgStackLoc+4);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000706 // Create the frame index only once. SPOffset here can be anything
707 // (this will be fixed on processFunctionBeforeFrameFinalized)
708 if (MipsFI->getGPStackOffset() == -1) {
709 FI = MFI->CreateFixedObject(4, 0);
710 MipsFI->setGPFI(FI);
711 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000712 MipsFI->setGPStackOffset(LastArgStackLoc);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000713 }
714
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000715 // Reload GP value.
716 FI = MipsFI->getGPFI();
Dan Gohman475871a2008-07-27 21:46:04 +0000717 SDValue FIN = DAG.getFrameIndex(FI,getPointerTy());
718 SDValue GPLoad = DAG.getLoad(MVT::i32, Chain, FIN, NULL, 0);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000719 Chain = GPLoad.getValue(1);
720 Chain = DAG.getCopyToReg(Chain, DAG.getRegister(Mips::GP, MVT::i32),
Dan Gohman475871a2008-07-27 21:46:04 +0000721 GPLoad, SDValue(0,0));
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000722 InFlag = Chain.getValue(1);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000723 }
724
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000725 // Handle result values, copying them out of physregs into vregs that we
726 // return.
Dan Gohman475871a2008-07-27 21:46:04 +0000727 return SDValue(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000728}
729
730/// LowerCallResult - Lower the result values of an ISD::CALL into the
731/// appropriate copies out of appropriate physical registers. This assumes that
732/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
733/// being lowered. Returns a SDNode with the same number of values as the
734/// ISD::CALL.
735SDNode *MipsTargetLowering::
Dan Gohman475871a2008-07-27 21:46:04 +0000736LowerCallResult(SDValue Chain, SDValue InFlag, SDNode *TheCall,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000737 unsigned CallingConv, SelectionDAG &DAG) {
738
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000739 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
740
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000741 // Assign locations to each value returned by this call.
742 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000743 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
744
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000745 CCInfo.AnalyzeCallResult(TheCall, RetCC_Mips);
Dan Gohman475871a2008-07-27 21:46:04 +0000746 SmallVector<SDValue, 8> ResultVals;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000747
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000748 // Copy all of the result registers out of their specified physreg.
749 for (unsigned i = 0; i != RVLocs.size(); ++i) {
750 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
751 RVLocs[i].getValVT(), InFlag).getValue(1);
752 InFlag = Chain.getValue(2);
753 ResultVals.push_back(Chain.getValue(0));
754 }
755
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000756 ResultVals.push_back(Chain);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000757
758 // Merge everything together with a MERGE_VALUES node.
Duncan Sandsf9516202008-06-30 10:19:09 +0000759 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
760 ResultVals.size()).Val;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000761}
762
763//===----------------------------------------------------------------------===//
764// FORMAL_ARGUMENTS Calling Convention Implementation
765//===----------------------------------------------------------------------===//
766
767/// Mips custom FORMAL_ARGUMENTS implementation
Dan Gohman475871a2008-07-27 21:46:04 +0000768SDValue MipsTargetLowering::
769LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000770{
771 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
772 switch(CC)
773 {
774 default:
775 assert(0 && "Unsupported calling convention");
776 case CallingConv::C:
777 return LowerCCCArguments(Op, DAG);
778 }
779}
780
781/// LowerCCCArguments - transform physical registers into
782/// virtual registers and generate load operations for
783/// arguments places on the stack.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000784/// TODO: isVarArg
Dan Gohman475871a2008-07-27 21:46:04 +0000785SDValue MipsTargetLowering::
786LowerCCCArguments(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000787{
Dan Gohman475871a2008-07-27 21:46:04 +0000788 SDValue Root = Op.getOperand(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000789 MachineFunction &MF = DAG.getMachineFunction();
790 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000791 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000792
793 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
794 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
795
796 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000797
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000798 // GP must be live into PIC and non-PIC call target.
799 AddLiveIn(MF, Mips::GP, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000800
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000801 // Assign locations to all of the incoming arguments.
802 SmallVector<CCValAssign, 16> ArgLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000803 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
804
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000805 CCInfo.AnalyzeFormalArguments(Op.Val, CC_Mips);
Dan Gohman475871a2008-07-27 21:46:04 +0000806 SmallVector<SDValue, 16> ArgValues;
807 SDValue StackPtr;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000808
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000809 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
810
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000811 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
812
813 CCValAssign &VA = ArgLocs[i];
814
815 // Arguments stored on registers
816 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000817 MVT RegVT = VA.getLocVT();
Bill Wendling06b8c192008-07-09 05:55:53 +0000818 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000819
820 if (RegVT == MVT::i32)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000821 RC = Mips::CPURegsRegisterClass;
822 else if (RegVT == MVT::f32) {
823 if (Subtarget->isSingleFloat())
824 RC = Mips::FGR32RegisterClass;
825 else
826 RC = Mips::AFGR32RegisterClass;
827 } else if (RegVT == MVT::f64) {
828 if (!Subtarget->isSingleFloat())
829 RC = Mips::AFGR64RegisterClass;
830 } else
831 assert(0 && "RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000832
833 // Transform the arguments stored on
834 // physical registers into virtual ones
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000835 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman475871a2008-07-27 21:46:04 +0000836 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000837
838 // If this is an 8 or 16-bit value, it is really passed promoted
839 // to 32 bits. Insert an assert[sz]ext to capture this, then
840 // truncate to the right size.
841 if (VA.getLocInfo() == CCValAssign::SExt)
842 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
843 DAG.getValueType(VA.getValVT()));
844 else if (VA.getLocInfo() == CCValAssign::ZExt)
845 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
846 DAG.getValueType(VA.getValVT()));
847
848 if (VA.getLocInfo() != CCValAssign::Full)
849 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
850
851 ArgValues.push_back(ArgValue);
852
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000853 // To meet ABI, when VARARGS are passed on registers, the registers
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000854 // must have their values written to the caller stack frame.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000855 if ((isVarArg) && (Subtarget->isABI_O32())) {
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000856 if (StackPtr.Val == 0)
857 StackPtr = DAG.getRegister(StackReg, getPointerTy());
858
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000859 // The stack pointer offset is relative to the caller stack frame.
860 // Since the real stack size is unknown here, a negative SPOffset
861 // is used so there's a way to adjust these offsets when the stack
862 // size get known (on EliminateFrameIndex). A dummy SPOffset is
863 // used instead of a direct negative address (which is recorded to
864 // be used on emitPrologue) to avoid mis-calc of the first stack
865 // offset on PEI::calculateFrameObjectOffsets.
866 // Arguments are always 32-bit.
867 int FI = MFI->CreateFixedObject(4, 0);
868 MipsFI->recordStoreVarArgsFI(FI, -(4+(i*4)));
Dan Gohman475871a2008-07-27 21:46:04 +0000869 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000870
871 // emit ISD::STORE whichs stores the
872 // parameter value to a stack Location
873 ArgValues.push_back(DAG.getStore(Root, ArgValue, PtrOff, NULL, 0));
874 }
875
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000876 } else { // VA.isRegLoc()
877
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000878 // sanity check
879 assert(VA.isMemLoc());
880
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000881 // The stack pointer offset is relative to the caller stack frame.
882 // Since the real stack size is unknown here, a negative SPOffset
883 // is used so there's a way to adjust these offsets when the stack
884 // size get known (on EliminateFrameIndex). A dummy SPOffset is
885 // used instead of a direct negative address (which is recorded to
886 // be used on emitPrologue) to avoid mis-calc of the first stack
887 // offset on PEI::calculateFrameObjectOffsets.
888 // Arguments are always 32-bit.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000889 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
890 int FI = MFI->CreateFixedObject(ArgSize, 0);
891 MipsFI->recordLoadArgsFI(FI, -(ArgSize+
892 (FirstStackArgLoc + VA.getLocMemOffset())));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000893
894 // Create load nodes to retrieve arguments from the stack
Dan Gohman475871a2008-07-27 21:46:04 +0000895 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000896 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
897 }
898 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000899
900 // The mips ABIs for returning structs by value requires that we copy
901 // the sret argument into $v0 for the return. Save the argument into
902 // a virtual register so that we can access it from the return points.
903 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
904 unsigned Reg = MipsFI->getSRetReturnReg();
905 if (!Reg) {
906 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
907 MipsFI->setSRetReturnReg(Reg);
908 }
Dan Gohman475871a2008-07-27 21:46:04 +0000909 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000910 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
911 }
912
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000913 ArgValues.push_back(Root);
914
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000915 // Return the new list of results.
Duncan Sandsf9516202008-06-30 10:19:09 +0000916 return DAG.getMergeValues(Op.Val->getVTList(), &ArgValues[0],
917 ArgValues.size()).getValue(Op.ResNo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000918}
919
920//===----------------------------------------------------------------------===//
921// Return Value Calling Convention Implementation
922//===----------------------------------------------------------------------===//
923
Dan Gohman475871a2008-07-27 21:46:04 +0000924SDValue MipsTargetLowering::
925LowerRET(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000926{
927 // CCValAssign - represent the assignment of
928 // the return value to a location
929 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000930 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
931 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000932
933 // CCState - Info about the registers and stack slot.
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000934 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000935
936 // Analize return values of ISD::RET
937 CCInfo.AnalyzeReturn(Op.Val, RetCC_Mips);
938
939 // If this is the first return lowered for this function, add
940 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000941 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000942 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000943 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000944 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000945 }
946
947 // The chain is always operand #0
Dan Gohman475871a2008-07-27 21:46:04 +0000948 SDValue Chain = Op.getOperand(0);
949 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000950
951 // Copy the result values into the output registers.
952 for (unsigned i = 0; i != RVLocs.size(); ++i) {
953 CCValAssign &VA = RVLocs[i];
954 assert(VA.isRegLoc() && "Can only return in registers!");
955
956 // ISD::RET => ret chain, (regnum1,val1), ...
957 // So i*2+1 index only the regnums
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000958 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000959
960 // guarantee that all emitted copies are
961 // stuck together, avoiding something bad
962 Flag = Chain.getValue(1);
963 }
964
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000965 // The mips ABIs for returning structs by value requires that we copy
966 // the sret argument into $v0 for the return. We saved the argument into
967 // a virtual register in the entry block, so now we copy the value out
968 // and into $v0.
969 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
970 MachineFunction &MF = DAG.getMachineFunction();
971 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
972 unsigned Reg = MipsFI->getSRetReturnReg();
973
974 if (!Reg)
975 assert(0 && "sret virtual register not created in the entry block");
Dan Gohman475871a2008-07-27 21:46:04 +0000976 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000977
978 Chain = DAG.getCopyToReg(Chain, Mips::V0, Val, Flag);
979 Flag = Chain.getValue(1);
980 }
981
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000982 // Return on Mips is always a "jr $ra"
983 if (Flag.Val)
984 return DAG.getNode(MipsISD::Ret, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000985 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000986 else // Return Void
987 return DAG.getNode(MipsISD::Ret, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +0000988 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000989}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +0000990
991//===----------------------------------------------------------------------===//
992// Mips Inline Assembly Support
993//===----------------------------------------------------------------------===//
994
995/// getConstraintType - Given a constraint letter, return the type of
996/// constraint it is for this target.
997MipsTargetLowering::ConstraintType MipsTargetLowering::
998getConstraintType(const std::string &Constraint) const
999{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001000 // Mips specific constrainy
1001 // GCC config/mips/constraints.md
1002 //
1003 // 'd' : An address register. Equivalent to r
1004 // unless generating MIPS16 code.
1005 // 'y' : Equivalent to r; retained for
1006 // backwards compatibility.
Bruno Cardoso Lopes7b76da12008-07-09 04:45:36 +00001007 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001008 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001009 switch (Constraint[0]) {
1010 default : break;
1011 case 'd':
1012 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001013 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001014 return C_RegisterClass;
1015 break;
1016 }
1017 }
1018 return TargetLowering::getConstraintType(Constraint);
1019}
1020
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001021/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
1022/// return a list of registers that can be used to satisfy the constraint.
1023/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001024std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00001025getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001026{
1027 if (Constraint.size() == 1) {
1028 switch (Constraint[0]) {
1029 case 'r':
1030 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001031 case 'f':
Duncan Sands15126422008-07-08 09:33:14 +00001032 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001033 if (Subtarget->isSingleFloat())
1034 return std::make_pair(0U, Mips::FGR32RegisterClass);
1035 else
1036 return std::make_pair(0U, Mips::AFGR32RegisterClass);
Duncan Sands15126422008-07-08 09:33:14 +00001037 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001038 if (VT == MVT::f64)
1039 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1040 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001041 }
1042 }
1043 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1044}
1045
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001046/// Given a register class constraint, like 'r', if this corresponds directly
1047/// to an LLVM register class, return a register of 0 and the register class
1048/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001049std::vector<unsigned> MipsTargetLowering::
1050getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001051 MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001052{
1053 if (Constraint.size() != 1)
1054 return std::vector<unsigned>();
1055
1056 switch (Constraint[0]) {
1057 default : break;
1058 case 'r':
1059 // GCC Mips Constraint Letters
1060 case 'd':
1061 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001062 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
1063 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
1064 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
1065 Mips::T8, 0);
1066
1067 case 'f':
Duncan Sands15126422008-07-08 09:33:14 +00001068 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001069 if (Subtarget->isSingleFloat())
1070 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
1071 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
1072 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
1073 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
1074 Mips::F30, Mips::F31, 0);
1075 else
1076 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
1077 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
1078 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +00001079 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001080
1081 if (VT == MVT::f64)
1082 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1083 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
1084 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
1085 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001086 }
1087 return std::vector<unsigned>();
1088}