blob: a0e5f30d78760da7981a0939cac7f3f9632d79cb [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"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000034using namespace llvm;
35
36const char *MipsTargetLowering::
37getTargetNodeName(unsigned Opcode) const
38{
39 switch (Opcode)
40 {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000041 case MipsISD::JmpLink : return "MipsISD::JmpLink";
42 case MipsISD::Hi : return "MipsISD::Hi";
43 case MipsISD::Lo : return "MipsISD::Lo";
44 case MipsISD::GPRel : return "MipsISD::GPRel";
45 case MipsISD::Ret : return "MipsISD::Ret";
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +000046 case MipsISD::CMov : return "MipsISD::CMov";
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000047 case MipsISD::SelectCC : return "MipsISD::SelectCC";
48 case MipsISD::FPSelectCC : return "MipsISD::FPSelectCC";
49 case MipsISD::FPBrcond : return "MipsISD::FPBrcond";
50 case MipsISD::FPCmp : return "MipsISD::FPCmp";
51 default : return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000052 }
53}
54
55MipsTargetLowering::
56MipsTargetLowering(MipsTargetMachine &TM): TargetLowering(TM)
57{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000058 Subtarget = &TM.getSubtarget<MipsSubtarget>();
59
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000060 // Mips does not have i1 type, so use i32 for
61 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000062 setBooleanContents(ZeroOrOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000063
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000064 // JumpTable targets must use GOT when using PIC_
65 setUsesGlobalOffsetTable(true);
66
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000067 // Set up the register classes
68 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000069 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000070
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000071 // When dealing with single precision only, use libcalls
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000072 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000073 if (!Subtarget->isFP64bit())
74 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000075
Bruno Cardoso Lopes7030ae72008-07-30 19:00:31 +000076 // Legal fp constants
77 addLegalFPImmediate(APFloat(+0.0f));
78
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000079 // Load extented operations for i1 types must be promoted
Evan Cheng03294662008-10-14 21:26:46 +000080 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
81 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
82 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000083
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000084 // Used by legalize types to correctly generate the setcc result.
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +000085 // Without this, every float setcc comes with a AND/OR with the result,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000086 // we don't want this, since the fpcmp result goes to a flag register,
87 // which is used implicitly by brcond and select operations.
88 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
89
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000090 // Mips Custom Operations
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +000091 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
92 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
93 setOperationAction(ISD::RET, MVT::Other, Custom);
94 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
95 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
96 setOperationAction(ISD::SELECT, MVT::f32, Custom);
97 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +000098 setOperationAction(ISD::SETCC, MVT::f32, Custom);
99 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
100 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000101
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +0000102 // We custom lower AND/OR to handle the case where the DAG contain 'ands/ors'
103 // with operands comming from setcc fp comparions. This is necessary since
104 // the result from these setcc are in a flag registers (FCR31).
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000105 setOperationAction(ISD::AND, MVT::i32, Custom);
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +0000106 setOperationAction(ISD::OR, MVT::i32, Custom);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000107
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000108 // Operations not directly supported by Mips.
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000109 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
110 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
111 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000112 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
113 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
114 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000115 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
116 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000117 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000118 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
119 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
120 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Bruno Cardoso Lopes7bd71822008-07-31 18:50:54 +0000121 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000122
123 // We don't have line number support yet.
124 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
125 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
126 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
127 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
128
129 // Use the default for now
130 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
131 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
132 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000133
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000134 if (Subtarget->isSingleFloat())
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000135 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000136
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000137 if (!Subtarget->hasSEInReg()) {
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000138 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000139 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
140 }
141
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000142 if (!Subtarget->hasBitCount())
143 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
144
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000145 if (!Subtarget->hasSwap())
146 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
147
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000148 setStackPointerRegisterToSaveRestore(Mips::SP);
149 computeRegisterProperties();
150}
151
152
Duncan Sands5480c042009-01-01 15:52:00 +0000153MVT MipsTargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000154 return MVT::i32;
155}
156
157
Dan Gohman475871a2008-07-27 21:46:04 +0000158SDValue MipsTargetLowering::
159LowerOperation(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000160{
161 switch (Op.getOpcode())
162 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000163 case ISD::AND: return LowerANDOR(Op, DAG);
164 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
165 case ISD::CALL: return LowerCALL(Op, DAG);
166 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
167 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
168 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
169 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
170 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
171 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
172 case ISD::OR: return LowerANDOR(Op, DAG);
173 case ISD::RET: return LowerRET(Op, DAG);
174 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000175 case ISD::SETCC: return LowerSETCC(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000176 }
Dan Gohman475871a2008-07-27 21:46:04 +0000177 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000178}
179
180//===----------------------------------------------------------------------===//
181// Lower helper functions
182//===----------------------------------------------------------------------===//
183
184// AddLiveIn - This helper function adds the specified physical register to the
185// MachineFunction as a live in value. It also creates a corresponding
186// virtual register for it.
187static unsigned
188AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
189{
190 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000191 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
192 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000193 return VReg;
194}
195
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000196// A address must be loaded from a small section if its size is less than the
197// small section size threshold. Data in this section must be addressed using
198// gp_rel operator.
199bool MipsTargetLowering::IsInSmallSection(unsigned Size) {
200 return (Size > 0 && (Size <= Subtarget->getSSectionThreshold()));
201}
202
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000203// Discover if this global address can be placed into small data/bss section.
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000204bool MipsTargetLowering::IsGlobalInSmallSection(GlobalValue *GV)
205{
206 const TargetData *TD = getTargetData();
Bruno Cardoso Lopesfeb95cc2008-07-22 15:34:27 +0000207 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
208
209 if (!GVA)
210 return false;
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000211
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000212 const Type *Ty = GV->getType()->getElementType();
Duncan Sands777d2302009-05-09 07:06:46 +0000213 unsigned Size = TD->getTypeAllocSize(Ty);
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000214
215 // if this is a internal constant string, there is a special
216 // section for it, but not in small data/bss.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000217 if (GVA->hasInitializer() && GV->hasLocalLinkage()) {
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000218 Constant *C = GVA->getInitializer();
219 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
220 if (CVA && CVA->isCString())
221 return false;
222 }
223
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000224 return IsInSmallSection(Size);
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000225}
226
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000227// Get fp branch code (not opcode) from condition code.
228static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
229 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
230 return Mips::BRANCH_T;
231
232 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
233 return Mips::BRANCH_F;
234
235 return Mips::BRANCH_INVALID;
236}
237
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000238static unsigned FPBranchCodeToOpc(Mips::FPBranchCode BC) {
239 switch(BC) {
240 default:
241 assert(0 && "Unknown branch code");
242 case Mips::BRANCH_T : return Mips::BC1T;
243 case Mips::BRANCH_F : return Mips::BC1F;
244 case Mips::BRANCH_TL : return Mips::BC1TL;
245 case Mips::BRANCH_FL : return Mips::BC1FL;
246 }
247}
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000248
249static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
250 switch (CC) {
251 default: assert(0 && "Unknown fp condition code!");
252 case ISD::SETEQ:
253 case ISD::SETOEQ: return Mips::FCOND_EQ;
254 case ISD::SETUNE: return Mips::FCOND_OGL;
255 case ISD::SETLT:
256 case ISD::SETOLT: return Mips::FCOND_OLT;
257 case ISD::SETGT:
258 case ISD::SETOGT: return Mips::FCOND_OGT;
259 case ISD::SETLE:
260 case ISD::SETOLE: return Mips::FCOND_OLE;
261 case ISD::SETGE:
262 case ISD::SETOGE: return Mips::FCOND_OGE;
263 case ISD::SETULT: return Mips::FCOND_ULT;
264 case ISD::SETULE: return Mips::FCOND_ULE;
265 case ISD::SETUGT: return Mips::FCOND_UGT;
266 case ISD::SETUGE: return Mips::FCOND_UGE;
267 case ISD::SETUO: return Mips::FCOND_UN;
268 case ISD::SETO: return Mips::FCOND_OR;
269 case ISD::SETNE:
270 case ISD::SETONE: return Mips::FCOND_NEQ;
271 case ISD::SETUEQ: return Mips::FCOND_UEQ;
272 }
273}
274
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000275MachineBasicBlock *
276MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +0000277 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000278 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
279 bool isFPCmp = false;
Dale Johannesen94817572009-02-13 02:34:39 +0000280 DebugLoc dl = MI->getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000281
282 switch (MI->getOpcode()) {
283 default: assert(false && "Unexpected instr type to insert");
284 case Mips::Select_FCC:
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000285 case Mips::Select_FCC_S32:
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000286 case Mips::Select_FCC_D32:
287 isFPCmp = true; // FALL THROUGH
288 case Mips::Select_CC:
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000289 case Mips::Select_CC_S32:
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000290 case Mips::Select_CC_D32: {
291 // To "insert" a SELECT_CC instruction, we actually have to insert the
292 // diamond control-flow pattern. The incoming instruction knows the
293 // destination vreg to set, the condition code register to branch on, the
294 // true/false values to select between, and a branch opcode to use.
295 const BasicBlock *LLVM_BB = BB->getBasicBlock();
296 MachineFunction::iterator It = BB;
297 ++It;
298
299 // thisMBB:
300 // ...
301 // TrueVal = ...
302 // setcc r1, r2, r3
303 // bNE r1, r0, copy1MBB
304 // fallthrough --> copy0MBB
305 MachineBasicBlock *thisMBB = BB;
306 MachineFunction *F = BB->getParent();
307 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
308 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
309
310 // Emit the right instruction according to the type of the operands compared
311 if (isFPCmp) {
312 // Find the condiction code present in the setcc operation.
313 Mips::CondCode CC = (Mips::CondCode)MI->getOperand(4).getImm();
314 // Get the branch opcode from the branch code.
315 unsigned Opc = FPBranchCodeToOpc(GetFPBranchCodeFromCond(CC));
Dale Johannesen94817572009-02-13 02:34:39 +0000316 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000317 } else
Dale Johannesen94817572009-02-13 02:34:39 +0000318 BuildMI(BB, dl, TII->get(Mips::BNE)).addReg(MI->getOperand(1).getReg())
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000319 .addReg(Mips::ZERO).addMBB(sinkMBB);
320
321 F->insert(It, copy0MBB);
322 F->insert(It, sinkMBB);
323 // Update machine-CFG edges by first adding all successors of the current
324 // block to the new block which will contain the Phi node for the select.
325 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
326 e = BB->succ_end(); i != e; ++i)
327 sinkMBB->addSuccessor(*i);
328 // Next, remove all successors of the current block, and add the true
329 // and fallthrough blocks as its successors.
330 while(!BB->succ_empty())
331 BB->removeSuccessor(BB->succ_begin());
332 BB->addSuccessor(copy0MBB);
333 BB->addSuccessor(sinkMBB);
334
335 // copy0MBB:
336 // %FalseValue = ...
337 // # fallthrough to sinkMBB
338 BB = copy0MBB;
339
340 // Update machine-CFG edges
341 BB->addSuccessor(sinkMBB);
342
343 // sinkMBB:
344 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
345 // ...
346 BB = sinkMBB;
Dale Johannesen94817572009-02-13 02:34:39 +0000347 BuildMI(BB, dl, TII->get(Mips::PHI), MI->getOperand(0).getReg())
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000348 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
349 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB);
350
351 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
352 return BB;
353 }
354 }
355}
356
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000357//===----------------------------------------------------------------------===//
358// Misc Lower Operation implementation
359//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000360
Dan Gohman475871a2008-07-27 21:46:04 +0000361SDValue MipsTargetLowering::
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000362LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG)
363{
364 SDValue Chain = Op.getOperand(0);
365 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +0000366 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000367
368 // Get a reference from Mips stack pointer
Dale Johannesena05dca42009-02-04 23:02:30 +0000369 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000370
371 // Subtract the dynamic size from the actual stack size to
372 // obtain the new stack size.
Dale Johannesena05dca42009-02-04 23:02:30 +0000373 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000374
375 // The Sub result contains the new stack start address, so it
376 // must be placed in the stack pointer register.
Dale Johannesena05dca42009-02-04 23:02:30 +0000377 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000378
379 // This node always has two return values: a new stack pointer
380 // value and a chain
381 SDValue Ops[2] = { Sub, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000382 return DAG.getMergeValues(Ops, 2, dl);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000383}
384
385SDValue MipsTargetLowering::
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +0000386LowerANDOR(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000387{
388 SDValue LHS = Op.getOperand(0);
389 SDValue RHS = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +0000390 DebugLoc dl = Op.getDebugLoc();
391
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000392 if (LHS.getOpcode() != MipsISD::FPCmp || RHS.getOpcode() != MipsISD::FPCmp)
393 return Op;
394
395 SDValue True = DAG.getConstant(1, MVT::i32);
396 SDValue False = DAG.getConstant(0, MVT::i32);
397
Dale Johannesende064702009-02-06 21:50:26 +0000398 SDValue LSEL = DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000399 LHS, True, False, LHS.getOperand(2));
Dale Johannesende064702009-02-06 21:50:26 +0000400 SDValue RSEL = DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000401 RHS, True, False, RHS.getOperand(2));
402
Dale Johannesende064702009-02-06 21:50:26 +0000403 return DAG.getNode(Op.getOpcode(), dl, MVT::i32, LSEL, RSEL);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000404}
405
406SDValue MipsTargetLowering::
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000407LowerBRCOND(SDValue Op, SelectionDAG &DAG)
408{
409 // The first operand is the chain, the second is the condition, the third is
410 // the block to branch to if the condition is true.
411 SDValue Chain = Op.getOperand(0);
412 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000413 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000414
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000415 if (Op.getOperand(1).getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000416 return Op;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000417
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000418 SDValue CondRes = Op.getOperand(1);
419 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000420 Mips::CondCode CC =
421 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000422 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
423
Dale Johannesende064702009-02-06 21:50:26 +0000424 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000425 Dest, CondRes);
426}
427
428SDValue MipsTargetLowering::
429LowerSETCC(SDValue Op, SelectionDAG &DAG)
430{
431 // The operands to this are the left and right operands to compare (ops #0,
432 // and #1) and the condition code to compare them with (op #2) as a
433 // CondCodeSDNode.
434 SDValue LHS = Op.getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +0000435 SDValue RHS = Op.getOperand(1);
436 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000437
438 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
439
Dale Johannesende064702009-02-06 21:50:26 +0000440 return DAG.getNode(MipsISD::FPCmp, dl, Op.getValueType(), LHS, RHS,
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000441 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
442}
443
444SDValue MipsTargetLowering::
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000445LowerSELECT(SDValue Op, SelectionDAG &DAG)
446{
447 SDValue Cond = Op.getOperand(0);
448 SDValue True = Op.getOperand(1);
449 SDValue False = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000450 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000451
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000452 // if the incomming condition comes from a integer compare, the select
453 // operation must be SelectCC or a conditional move if the subtarget
454 // supports it.
455 if (Cond.getOpcode() != MipsISD::FPCmp) {
456 if (Subtarget->hasCondMov() && !True.getValueType().isFloatingPoint())
457 return Op;
Dale Johannesende064702009-02-06 21:50:26 +0000458 return DAG.getNode(MipsISD::SelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000459 Cond, True, False);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000460 }
461
462 // if the incomming condition comes from fpcmp, the select
463 // operation must use FPSelectCC.
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000464 SDValue CCNode = Cond.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000465 return DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000466 Cond, True, False, CCNode);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000467}
468
469SDValue MipsTargetLowering::
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000470LowerGlobalAddress(SDValue Op, SelectionDAG &DAG)
471{
Dale Johannesende064702009-02-06 21:50:26 +0000472 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000473 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000474 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
475 SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
476
477 if (!Subtarget->hasABICall()) {
Dan Gohmanfc166572009-04-09 23:54:40 +0000478 SDVTList VTs = DAG.getVTList(MVT::i32);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000479 SDValue Ops[] = { GA };
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000480 // %gp_rel relocation
481 if (!isa<Function>(GV) && IsGlobalInSmallSection(GV)) {
Dan Gohmanfc166572009-04-09 23:54:40 +0000482 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, Ops, 1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000483 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000484 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000485 }
486 // %hi/%lo relocation
Dan Gohmanfc166572009-04-09 23:54:40 +0000487 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, Ops, 1);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000488 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GA);
489 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000490
491 } else { // Abicall relocations, TODO: make this cleaner.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000492 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
493 DAG.getEntryNode(), GA, NULL, 0);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000494 // On functions and global targets not internal linked only
495 // a load from got/GP is necessary for PIC to work.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000496 if (!GV->hasLocalLinkage() || isa<Function>(GV))
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000497 return ResNode;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000498 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GA);
499 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000500 }
501
502 assert(0 && "Dont know how to handle GlobalAddress");
503 return SDValue(0,0);
504}
505
506SDValue MipsTargetLowering::
507LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG)
508{
509 assert(0 && "TLS not implemented for MIPS.");
510 return SDValue(); // Not reached
511}
512
513SDValue MipsTargetLowering::
Dan Gohman475871a2008-07-27 21:46:04 +0000514LowerJumpTable(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000515{
Dan Gohman475871a2008-07-27 21:46:04 +0000516 SDValue ResNode;
517 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +0000518 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000519 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000520
Duncan Sands83ec4b62008-06-06 12:08:01 +0000521 MVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000522 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +0000523 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000524
525 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohmanfc166572009-04-09 23:54:40 +0000526 SDVTList VTs = DAG.getVTList(MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000527 SDValue Ops[] = { JTI };
Dan Gohmanfc166572009-04-09 23:54:40 +0000528 HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, Ops, 1);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000529 } else // Emit Load from Global Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +0000530 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI, NULL, 0);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000531
Dale Johannesen33c960f2009-02-04 20:06:27 +0000532 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTI);
533 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000534
535 return ResNode;
536}
537
Dan Gohman475871a2008-07-27 21:46:04 +0000538SDValue MipsTargetLowering::
539LowerConstantPool(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000540{
Dan Gohman475871a2008-07-27 21:46:04 +0000541 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000542 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
543 Constant *C = N->getConstVal();
Dan Gohman475871a2008-07-27 21:46:04 +0000544 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
Dale Johannesende064702009-02-06 21:50:26 +0000545 // FIXME there isn't actually debug info here
546 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000547
548 // gp_rel relocation
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000549 // FIXME: we should reference the constant pool using small data sections,
550 // but the asm printer currently doens't support this feature without
551 // hacking it. This feature should come soon so we can uncomment the
552 // stuff below.
553 //if (!Subtarget->hasABICall() &&
Duncan Sands777d2302009-05-09 07:06:46 +0000554 // IsInSmallSection(getTargetData()->getTypeAllocSize(C->getType()))) {
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000555 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000556 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000557 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
558 //} else { // %hi/%lo relocation
Dale Johannesende064702009-02-06 21:50:26 +0000559 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CP);
560 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CP);
561 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000562 //}
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000563
564 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000565}
566
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000567//===----------------------------------------------------------------------===//
568// Calling Convention Implementation
569//
570// The lower operations present on calling convention works on this order:
571// LowerCALL (virt regs --> phys regs, virt regs --> stack)
572// LowerFORMAL_ARGUMENTS (phys --> virt regs, stack --> virt regs)
573// LowerRET (virt regs --> phys regs)
574// LowerCALL (phys regs --> virt regs)
575//
576//===----------------------------------------------------------------------===//
577
578#include "MipsGenCallingConv.inc"
579
580//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000581// TODO: Implement a generic logic using tblgen that can support this.
582// Mips O32 ABI rules:
583// ---
584// i32 - Passed in A0, A1, A2, A3 and stack
585// f32 - Only passed in f32 registers if no int reg has been used yet to hold
586// an argument. Otherwise, passed in A1, A2, A3 and stack.
587// f64 - Only passed in two aliased f32 registers if no int reg has been used
588// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
589// not used, it must be shadowed. If only A3 is avaiable, shadow it and
590// go to stack.
591//===----------------------------------------------------------------------===//
592
593static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
594 MVT LocVT, CCValAssign::LocInfo LocInfo,
595 ISD::ArgFlagsTy ArgFlags, CCState &State) {
596
597 static const unsigned IntRegsSize=4, FloatRegsSize=2;
598
599 static const unsigned IntRegs[] = {
600 Mips::A0, Mips::A1, Mips::A2, Mips::A3
601 };
602 static const unsigned F32Regs[] = {
603 Mips::F12, Mips::F14
604 };
605 static const unsigned F64Regs[] = {
606 Mips::D6, Mips::D7
607 };
608
609 unsigned Reg=0;
610 unsigned UnallocIntReg = State.getFirstUnallocated(IntRegs, IntRegsSize);
611 bool IntRegUsed = (IntRegs[UnallocIntReg] != (unsigned (Mips::A0)));
612
613 // Promote i8 and i16
614 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
615 LocVT = MVT::i32;
616 if (ArgFlags.isSExt())
617 LocInfo = CCValAssign::SExt;
618 else if (ArgFlags.isZExt())
619 LocInfo = CCValAssign::ZExt;
620 else
621 LocInfo = CCValAssign::AExt;
622 }
623
624 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && IntRegUsed)) {
625 Reg = State.AllocateReg(IntRegs, IntRegsSize);
626 IntRegUsed = true;
627 LocVT = MVT::i32;
628 }
629
630 if (ValVT.isFloatingPoint() && !IntRegUsed) {
631 if (ValVT == MVT::f32)
632 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
633 else
634 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
635 }
636
637 if (ValVT == MVT::f64 && IntRegUsed) {
638 if (UnallocIntReg != IntRegsSize) {
639 // If we hit register A3 as the first not allocated, we must
640 // mark it as allocated (shadow) and use the stack instead.
641 if (IntRegs[UnallocIntReg] != (unsigned (Mips::A3)))
642 Reg = Mips::A2;
643 for (;UnallocIntReg < IntRegsSize; ++UnallocIntReg)
644 State.AllocateReg(UnallocIntReg);
645 }
646 LocVT = MVT::i32;
647 }
648
649 if (!Reg) {
650 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
651 unsigned Offset = State.AllocateStack(SizeInBytes, SizeInBytes);
652 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
653 } else
654 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
655
656 return false; // CC must always match
657}
658
659//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000660// CALL Calling Convention Implementation
661//===----------------------------------------------------------------------===//
662
Nate Begeman5bf4b752009-01-26 03:15:54 +0000663/// LowerCALL - functions arguments are copied from virtual regs to
664/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000665/// TODO: isVarArg, isTailCall.
Dan Gohman475871a2008-07-27 21:46:04 +0000666SDValue MipsTargetLowering::
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000667LowerCALL(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000668{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000669 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000670
Dan Gohman095cc292008-09-13 01:54:27 +0000671 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
672 SDValue Chain = TheCall->getChain();
673 SDValue Callee = TheCall->getCallee();
674 bool isVarArg = TheCall->isVarArg();
675 unsigned CC = TheCall->getCallingConv();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000676 DebugLoc dl = TheCall->getDebugLoc();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000677
678 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000679
680 // Analyze operands of the call, assigning locations to each operand.
681 SmallVector<CCValAssign, 16> ArgLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000682 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
683
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000684 // To meet O32 ABI, Mips must always allocate 16 bytes on
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000685 // the stack (even if less than 4 are used as arguments)
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000686 if (Subtarget->isABI_O32()) {
687 int VTsize = MVT(MVT::i32).getSizeInBits()/8;
688 MFI->CreateFixedObject(VTsize, (VTsize*3));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000689 CCInfo.AnalyzeCallOperands(TheCall, CC_MipsO32);
690 } else
691 CCInfo.AnalyzeCallOperands(TheCall, CC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000692
693 // Get a count of how many bytes are to be pushed on the stack.
694 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnere563bbc2008-10-11 22:08:30 +0000695 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000696
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000697 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +0000698 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
699 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000700
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000701 // First/LastArgStackLoc contains the first/last
702 // "at stack" argument location.
703 int LastArgStackLoc = 0;
704 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000705
706 // Walk the register/memloc assignments, inserting copies/loads.
707 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000708 SDValue Arg = TheCall->getArg(i);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000709 CCValAssign &VA = ArgLocs[i];
710
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000711 // Promote the value if needed.
712 switch (VA.getLocInfo()) {
Chris Lattnere0b12152008-03-17 06:57:02 +0000713 default: assert(0 && "Unknown loc info!");
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000714 case CCValAssign::Full:
715 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
716 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
717 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Arg);
718 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
719 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
720 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
721 DAG.getConstant(0, getPointerTy()));
722 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
723 DAG.getConstant(1, getPointerTy()));
724 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
725 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
726 continue;
727 }
728 }
729 break;
Chris Lattnere0b12152008-03-17 06:57:02 +0000730 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000731 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000732 break;
733 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000734 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000735 break;
736 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000737 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000738 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000739 }
740
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000741 // Arguments that can be passed on register must be kept at
742 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000743 if (VA.isRegLoc()) {
744 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +0000745 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000746 }
Chris Lattnere0b12152008-03-17 06:57:02 +0000747
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000748 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +0000749 assert(VA.isMemLoc());
750
751 // Create the frame index object for this incoming parameter
752 // This guarantees that when allocating Local Area the firsts
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000753 // 16 bytes which are alwayes reserved won't be overwritten
754 // if O32 ABI is used. For EABI the first address is zero.
755 LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000756 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000757 LastArgStackLoc);
Chris Lattnere0b12152008-03-17 06:57:02 +0000758
Dan Gohman475871a2008-07-27 21:46:04 +0000759 SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +0000760
761 // emit ISD::STORE whichs stores the
762 // parameter value to a stack Location
Dale Johannesen33c960f2009-02-04 20:06:27 +0000763 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000764 }
765
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000766 // Transform all store nodes into one single node because all store
767 // nodes are independent of each other.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000768 if (!MemOpChains.empty())
Dale Johannesen33c960f2009-02-04 20:06:27 +0000769 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000770 &MemOpChains[0], MemOpChains.size());
771
772 // Build a sequence of copy-to-reg nodes chained together with token
773 // chain and flag operands which copy the outgoing args into registers.
774 // The InFlag in necessary since all emited instructions must be
775 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000776 SDValue InFlag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000777 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000778 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000779 RegsToPass[i].second, InFlag);
780 InFlag = Chain.getValue(1);
781 }
782
Bill Wendling056292f2008-09-16 21:48:12 +0000783 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
784 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
785 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000786 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000787 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +0000788 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
789 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
790
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000791 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
792 // = Chain, Callee, Reg#1, Reg#2, ...
793 //
794 // Returns a chain & a flag for retval copy to use.
795 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000796 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000797 Ops.push_back(Chain);
798 Ops.push_back(Callee);
799
800 // Add argument registers to the end of the list so that they are
801 // known live into the call.
802 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
803 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
804 RegsToPass[i].second.getValueType()));
805
Gabor Greifba36cb52008-08-28 21:40:38 +0000806 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000807 Ops.push_back(InFlag);
808
Dale Johannesen33c960f2009-02-04 20:06:27 +0000809 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000810 InFlag = Chain.getValue(1);
811
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000812 // Create the CALLSEQ_END node.
Chris Lattnere563bbc2008-10-11 22:08:30 +0000813 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
814 DAG.getIntPtrConstant(0, true), InFlag);
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000815 InFlag = Chain.getValue(1);
816
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000817 // Create a stack location to hold GP when PIC is used. This stack
818 // location is used on function prologue to save GP and also after all
819 // emited CALL's to restore GP.
820 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000821 // Function can have an arbitrary number of calls, so
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000822 // hold the LastArgStackLoc with the biggest offset.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000823 int FI;
824 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000825 if (LastArgStackLoc >= MipsFI->getGPStackOffset()) {
826 LastArgStackLoc = (!LastArgStackLoc) ? (16) : (LastArgStackLoc+4);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000827 // Create the frame index only once. SPOffset here can be anything
828 // (this will be fixed on processFunctionBeforeFrameFinalized)
829 if (MipsFI->getGPStackOffset() == -1) {
830 FI = MFI->CreateFixedObject(4, 0);
831 MipsFI->setGPFI(FI);
832 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000833 MipsFI->setGPStackOffset(LastArgStackLoc);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000834 }
835
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000836 // Reload GP value.
837 FI = MipsFI->getGPFI();
Dan Gohman475871a2008-07-27 21:46:04 +0000838 SDValue FIN = DAG.getFrameIndex(FI,getPointerTy());
Dale Johannesen33c960f2009-02-04 20:06:27 +0000839 SDValue GPLoad = DAG.getLoad(MVT::i32, dl, Chain, FIN, NULL, 0);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000840 Chain = GPLoad.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000841 Chain = DAG.getCopyToReg(Chain, dl, DAG.getRegister(Mips::GP, MVT::i32),
Dan Gohman475871a2008-07-27 21:46:04 +0000842 GPLoad, SDValue(0,0));
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000843 InFlag = Chain.getValue(1);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000844 }
845
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000846 // Handle result values, copying them out of physregs into vregs that we
847 // return.
Dan Gohman095cc292008-09-13 01:54:27 +0000848 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG), Op.getResNo());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000849}
850
851/// LowerCallResult - Lower the result values of an ISD::CALL into the
852/// appropriate copies out of appropriate physical registers. This assumes that
853/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
854/// being lowered. Returns a SDNode with the same number of values as the
855/// ISD::CALL.
856SDNode *MipsTargetLowering::
Dan Gohman095cc292008-09-13 01:54:27 +0000857LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000858 unsigned CallingConv, SelectionDAG &DAG) {
859
Dan Gohman095cc292008-09-13 01:54:27 +0000860 bool isVarArg = TheCall->isVarArg();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000861 DebugLoc dl = TheCall->getDebugLoc();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000862
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000863 // Assign locations to each value returned by this call.
864 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000865 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
866
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000867 CCInfo.AnalyzeCallResult(TheCall, RetCC_Mips);
Dan Gohman475871a2008-07-27 21:46:04 +0000868 SmallVector<SDValue, 8> ResultVals;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000869
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000870 // Copy all of the result registers out of their specified physreg.
871 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000872 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000873 RVLocs[i].getValVT(), InFlag).getValue(1);
874 InFlag = Chain.getValue(2);
875 ResultVals.push_back(Chain.getValue(0));
876 }
877
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000878 ResultVals.push_back(Chain);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000879
880 // Merge everything together with a MERGE_VALUES node.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000881 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +0000882 &ResultVals[0], ResultVals.size()).getNode();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000883}
884
885//===----------------------------------------------------------------------===//
886// FORMAL_ARGUMENTS Calling Convention Implementation
887//===----------------------------------------------------------------------===//
888
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000889/// LowerFORMAL_ARGUMENTS - transform physical registers into
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000890/// virtual registers and generate load operations for
891/// arguments places on the stack.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000892/// TODO: isVarArg
Dan Gohman475871a2008-07-27 21:46:04 +0000893SDValue MipsTargetLowering::
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000894LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000895{
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000896 SDValue Root = Op.getOperand(0);
897 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000898 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000899 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000900 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000901
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000902 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000903 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000904
905 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000906
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000907 // GP must be live into PIC and non-PIC call target.
908 AddLiveIn(MF, Mips::GP, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000909
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000910 // Assign locations to all of the incoming arguments.
911 SmallVector<CCValAssign, 16> ArgLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000912 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
913
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000914 if (Subtarget->isABI_O32())
915 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_MipsO32);
916 else
917 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_Mips);
918
Dan Gohman475871a2008-07-27 21:46:04 +0000919 SmallVector<SDValue, 16> ArgValues;
920 SDValue StackPtr;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000921
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000922 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
923
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000924 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000925 CCValAssign &VA = ArgLocs[i];
926
927 // Arguments stored on registers
928 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000929 MVT RegVT = VA.getLocVT();
Bill Wendling06b8c192008-07-09 05:55:53 +0000930 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000931
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000932 if (RegVT == MVT::i32)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000933 RC = Mips::CPURegsRegisterClass;
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000934 else if (RegVT == MVT::f32)
935 RC = Mips::FGR32RegisterClass;
936 else if (RegVT == MVT::f64) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000937 if (!Subtarget->isSingleFloat())
938 RC = Mips::AFGR64RegisterClass;
939 } else
940 assert(0 && "RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000941
942 // Transform the arguments stored on
943 // physical registers into virtual ones
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000944 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000945 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000946
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000947 // If this is an 8 or 16-bit value, it has been passed promoted
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000948 // to 32 bits. Insert an assert[sz]ext to capture this, then
949 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000950 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +0000951 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000952 if (VA.getLocInfo() == CCValAssign::SExt)
953 Opcode = ISD::AssertSext;
954 else if (VA.getLocInfo() == CCValAssign::ZExt)
955 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +0000956 if (Opcode)
957 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
958 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +0000959 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000960 }
961
962 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
963 if (Subtarget->isABI_O32()) {
964 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
965 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, ArgValue);
966 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
967 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
968 VA.getLocReg()+1, RC);
969 SDValue ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg2, RegVT);
970 SDValue Hi = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, ArgValue);
971 SDValue Lo = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, ArgValue2);
972 ArgValue = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::f64, Lo, Hi);
973 }
974 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000975
976 ArgValues.push_back(ArgValue);
977
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000978 // To meet ABI, when VARARGS are passed on registers, the registers
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000979 // must have their values written to the caller stack frame.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000980 if ((isVarArg) && (Subtarget->isABI_O32())) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000981 if (StackPtr.getNode() == 0)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000982 StackPtr = DAG.getRegister(StackReg, getPointerTy());
983
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000984 // The stack pointer offset is relative to the caller stack frame.
985 // Since the real stack size is unknown here, a negative SPOffset
986 // is used so there's a way to adjust these offsets when the stack
987 // size get known (on EliminateFrameIndex). A dummy SPOffset is
988 // used instead of a direct negative address (which is recorded to
989 // be used on emitPrologue) to avoid mis-calc of the first stack
990 // offset on PEI::calculateFrameObjectOffsets.
991 // Arguments are always 32-bit.
992 int FI = MFI->CreateFixedObject(4, 0);
993 MipsFI->recordStoreVarArgsFI(FI, -(4+(i*4)));
Dan Gohman475871a2008-07-27 21:46:04 +0000994 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000995
996 // emit ISD::STORE whichs stores the
997 // parameter value to a stack Location
Dale Johannesen33c960f2009-02-04 20:06:27 +0000998 ArgValues.push_back(DAG.getStore(Root, dl, ArgValue, PtrOff, NULL, 0));
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000999 }
1000
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001001 } else { // VA.isRegLoc()
1002
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001003 // sanity check
1004 assert(VA.isMemLoc());
1005
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001006 // The stack pointer offset is relative to the caller stack frame.
1007 // Since the real stack size is unknown here, a negative SPOffset
1008 // is used so there's a way to adjust these offsets when the stack
1009 // size get known (on EliminateFrameIndex). A dummy SPOffset is
1010 // used instead of a direct negative address (which is recorded to
1011 // be used on emitPrologue) to avoid mis-calc of the first stack
1012 // offset on PEI::calculateFrameObjectOffsets.
1013 // Arguments are always 32-bit.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001014 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1015 int FI = MFI->CreateFixedObject(ArgSize, 0);
1016 MipsFI->recordLoadArgsFI(FI, -(ArgSize+
1017 (FirstStackArgLoc + VA.getLocMemOffset())));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001018
1019 // Create load nodes to retrieve arguments from the stack
Dan Gohman475871a2008-07-27 21:46:04 +00001020 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dale Johannesen33c960f2009-02-04 20:06:27 +00001021 ArgValues.push_back(DAG.getLoad(VA.getValVT(), dl, Root, FIN, NULL, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001022 }
1023 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001024
1025 // The mips ABIs for returning structs by value requires that we copy
1026 // the sret argument into $v0 for the return. Save the argument into
1027 // a virtual register so that we can access it from the return points.
1028 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1029 unsigned Reg = MipsFI->getSRetReturnReg();
1030 if (!Reg) {
1031 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
1032 MipsFI->setSRetReturnReg(Reg);
1033 }
Dale Johannesen33c960f2009-02-04 20:06:27 +00001034 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, ArgValues[0]);
1035 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Root);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001036 }
1037
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001038 ArgValues.push_back(Root);
1039
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001040 // Return the new list of results.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001041 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00001042 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001043}
1044
1045//===----------------------------------------------------------------------===//
1046// Return Value Calling Convention Implementation
1047//===----------------------------------------------------------------------===//
1048
Dan Gohman475871a2008-07-27 21:46:04 +00001049SDValue MipsTargetLowering::
1050LowerRET(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001051{
1052 // CCValAssign - represent the assignment of
1053 // the return value to a location
1054 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001055 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
1056 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Dale Johannesena05dca42009-02-04 23:02:30 +00001057 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001058
1059 // CCState - Info about the registers and stack slot.
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001060 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001061
1062 // Analize return values of ISD::RET
Gabor Greifba36cb52008-08-28 21:40:38 +00001063 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001064
1065 // If this is the first return lowered for this function, add
1066 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001067 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001068 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001069 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +00001070 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001071 }
1072
1073 // The chain is always operand #0
Dan Gohman475871a2008-07-27 21:46:04 +00001074 SDValue Chain = Op.getOperand(0);
1075 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001076
1077 // Copy the result values into the output registers.
1078 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1079 CCValAssign &VA = RVLocs[i];
1080 assert(VA.isRegLoc() && "Can only return in registers!");
1081
1082 // ISD::RET => ret chain, (regnum1,val1), ...
1083 // So i*2+1 index only the regnums
Dale Johannesena05dca42009-02-04 23:02:30 +00001084 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1085 Op.getOperand(i*2+1), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001086
1087 // guarantee that all emitted copies are
1088 // stuck together, avoiding something bad
1089 Flag = Chain.getValue(1);
1090 }
1091
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001092 // The mips ABIs for returning structs by value requires that we copy
1093 // the sret argument into $v0 for the return. We saved the argument into
1094 // a virtual register in the entry block, so now we copy the value out
1095 // and into $v0.
1096 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1097 MachineFunction &MF = DAG.getMachineFunction();
1098 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1099 unsigned Reg = MipsFI->getSRetReturnReg();
1100
1101 if (!Reg)
1102 assert(0 && "sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00001103 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001104
Dale Johannesena05dca42009-02-04 23:02:30 +00001105 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001106 Flag = Chain.getValue(1);
1107 }
1108
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001109 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00001110 if (Flag.getNode())
Dale Johannesena05dca42009-02-04 23:02:30 +00001111 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +00001112 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001113 else // Return Void
Dale Johannesena05dca42009-02-04 23:02:30 +00001114 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +00001115 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001116}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001117
1118//===----------------------------------------------------------------------===//
1119// Mips Inline Assembly Support
1120//===----------------------------------------------------------------------===//
1121
1122/// getConstraintType - Given a constraint letter, return the type of
1123/// constraint it is for this target.
1124MipsTargetLowering::ConstraintType MipsTargetLowering::
1125getConstraintType(const std::string &Constraint) const
1126{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001127 // Mips specific constrainy
1128 // GCC config/mips/constraints.md
1129 //
1130 // 'd' : An address register. Equivalent to r
1131 // unless generating MIPS16 code.
1132 // 'y' : Equivalent to r; retained for
1133 // backwards compatibility.
Bruno Cardoso Lopes7b76da12008-07-09 04:45:36 +00001134 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001135 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001136 switch (Constraint[0]) {
1137 default : break;
1138 case 'd':
1139 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001140 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001141 return C_RegisterClass;
1142 break;
1143 }
1144 }
1145 return TargetLowering::getConstraintType(Constraint);
1146}
1147
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001148/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
1149/// return a list of registers that can be used to satisfy the constraint.
1150/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001151std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00001152getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001153{
1154 if (Constraint.size() == 1) {
1155 switch (Constraint[0]) {
1156 case 'r':
1157 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001158 case 'f':
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001159 if (VT == MVT::f32)
1160 return std::make_pair(0U, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001161 if (VT == MVT::f64)
1162 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1163 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001164 }
1165 }
1166 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1167}
1168
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001169/// Given a register class constraint, like 'r', if this corresponds directly
1170/// to an LLVM register class, return a register of 0 and the register class
1171/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001172std::vector<unsigned> MipsTargetLowering::
1173getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001174 MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001175{
1176 if (Constraint.size() != 1)
1177 return std::vector<unsigned>();
1178
1179 switch (Constraint[0]) {
1180 default : break;
1181 case 'r':
1182 // GCC Mips Constraint Letters
1183 case 'd':
1184 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001185 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
1186 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
1187 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
1188 Mips::T8, 0);
1189
1190 case 'f':
Duncan Sands15126422008-07-08 09:33:14 +00001191 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001192 if (Subtarget->isSingleFloat())
1193 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
1194 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
1195 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
1196 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
1197 Mips::F30, Mips::F31, 0);
1198 else
1199 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
1200 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
1201 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +00001202 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001203
1204 if (VT == MVT::f64)
1205 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1206 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
1207 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
1208 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001209 }
1210 return std::vector<unsigned>();
1211}
Dan Gohman6520e202008-10-18 02:06:02 +00001212
1213bool
1214MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1215 // The Mips target isn't yet aware of offsets.
1216 return false;
1217}