blob: 1f199efaafea07764ac424e0ab8a13a422a19381 [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"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000016#include "MipsISelLowering.h"
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +000017#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000018#include "MipsTargetMachine.h"
Chris Lattnerb71b9092009-08-13 06:28:06 +000019#include "MipsTargetObjectFile.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"
Torok Edwinc25e7582009-07-11 20:10:48 +000034#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000035using namespace llvm;
36
Chris Lattnerf0144122009-07-28 03:13:23 +000037const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
38 switch (Opcode) {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000039 case MipsISD::JmpLink : return "MipsISD::JmpLink";
40 case MipsISD::Hi : return "MipsISD::Hi";
41 case MipsISD::Lo : return "MipsISD::Lo";
42 case MipsISD::GPRel : return "MipsISD::GPRel";
43 case MipsISD::Ret : return "MipsISD::Ret";
44 case MipsISD::SelectCC : return "MipsISD::SelectCC";
45 case MipsISD::FPSelectCC : return "MipsISD::FPSelectCC";
46 case MipsISD::FPBrcond : return "MipsISD::FPBrcond";
47 case MipsISD::FPCmp : return "MipsISD::FPCmp";
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +000048 case MipsISD::FPRound : return "MipsISD::FPRound";
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000049 default : return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000050 }
51}
52
53MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000054MipsTargetLowering(MipsTargetMachine &TM)
Chris Lattnerb71b9092009-08-13 06:28:06 +000055 : TargetLowering(TM, new MipsTargetObjectFile()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000056 Subtarget = &TM.getSubtarget<MipsSubtarget>();
57
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000058 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000059 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000060 setBooleanContents(ZeroOrOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000061
62 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000063 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
64 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000065
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000066 // When dealing with single precision only, use libcalls
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000067 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000068 if (!Subtarget->isFP64bit())
Owen Anderson825b72b2009-08-11 20:47:22 +000069 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000070
Wesley Peckbf17cfa2010-11-23 03:31:01 +000071 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +000072 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
73 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
74 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000075
Eli Friedman6055a6a2009-07-17 04:07:24 +000076 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +000077 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
78 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +000079
Wesley Peckbf17cfa2010-11-23 03:31:01 +000080 // Used by legalize types to correctly generate the setcc result.
81 // Without this, every float setcc comes with a AND/OR with the result,
82 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000083 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +000084 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000085
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000086 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +000087 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
88 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
89 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
90 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
91 setOperationAction(ISD::SELECT, MVT::f32, Custom);
92 setOperationAction(ISD::SELECT, MVT::f64, Custom);
93 setOperationAction(ISD::SELECT, MVT::i32, Custom);
94 setOperationAction(ISD::SETCC, MVT::f32, Custom);
95 setOperationAction(ISD::SETCC, MVT::f64, Custom);
96 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
97 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
98 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +000099 setOperationAction(ISD::VASTART, MVT::Other, Custom);
100
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000101
Wesley Peckbf17cfa2010-11-23 03:31:01 +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
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +0000104 // the result from these setcc are in a flag registers (FCR31).
Owen Anderson825b72b2009-08-11 20:47:22 +0000105 setOperationAction(ISD::AND, MVT::i32, Custom);
106 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.
Owen Anderson825b72b2009-08-11 20:47:22 +0000109 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
110 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
111 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
112 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);
115 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
116 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
117 setOperationAction(ISD::ROTL, MVT::i32, Expand);
118 setOperationAction(ISD::ROTR, MVT::i32, Expand);
119 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
120 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
121 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
122 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
123 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
124 setOperationAction(ISD::FSIN, MVT::f32, Expand);
125 setOperationAction(ISD::FCOS, MVT::f32, Expand);
126 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
127 setOperationAction(ISD::FPOW, MVT::f32, Expand);
128 setOperationAction(ISD::FLOG, MVT::f32, Expand);
129 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
130 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
131 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000132
Owen Anderson825b72b2009-08-11 20:47:22 +0000133 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000134
135 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000136 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
137 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
138 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000139
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000140 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000141 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000142
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000143 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000144 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
145 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000146 }
147
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000148 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000149 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000150
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000151 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000152 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000153
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000154 setStackPointerRegisterToSaveRestore(Mips::SP);
155 computeRegisterProperties();
156}
157
Owen Anderson825b72b2009-08-11 20:47:22 +0000158MVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
159 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000160}
161
Bill Wendlingb4202b82009-07-01 18:50:55 +0000162/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000163unsigned MipsTargetLowering::getFunctionAlignment(const Function *) const {
164 return 2;
165}
Scott Michel5b8f82e2008-03-10 15:42:14 +0000166
Dan Gohman475871a2008-07-27 21:46:04 +0000167SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000168LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000169{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000170 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000171 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000172 case ISD::AND: return LowerANDOR(Op, DAG);
173 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000174 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
175 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000176 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000177 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
178 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
179 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
180 case ISD::OR: return LowerANDOR(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000181 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000182 case ISD::SETCC: return LowerSETCC(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000183 case ISD::VASTART: return LowerVASTART(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000184 }
Dan Gohman475871a2008-07-27 21:46:04 +0000185 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000186}
187
188//===----------------------------------------------------------------------===//
189// Lower helper functions
190//===----------------------------------------------------------------------===//
191
192// AddLiveIn - This helper function adds the specified physical register to the
193// MachineFunction as a live in value. It also creates a corresponding
194// virtual register for it.
195static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000196AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000197{
198 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000199 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
200 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000201 return VReg;
202}
203
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000204// Get fp branch code (not opcode) from condition code.
205static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
206 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
207 return Mips::BRANCH_T;
208
209 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
210 return Mips::BRANCH_F;
211
212 return Mips::BRANCH_INVALID;
213}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000214
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000215static unsigned FPBranchCodeToOpc(Mips::FPBranchCode BC) {
216 switch(BC) {
217 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000218 llvm_unreachable("Unknown branch code");
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000219 case Mips::BRANCH_T : return Mips::BC1T;
220 case Mips::BRANCH_F : return Mips::BC1F;
221 case Mips::BRANCH_TL : return Mips::BC1TL;
222 case Mips::BRANCH_FL : return Mips::BC1FL;
223 }
224}
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000225
226static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
227 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000228 default: llvm_unreachable("Unknown fp condition code!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000229 case ISD::SETEQ:
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000230 case ISD::SETOEQ: return Mips::FCOND_EQ;
231 case ISD::SETUNE: return Mips::FCOND_OGL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000232 case ISD::SETLT:
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000233 case ISD::SETOLT: return Mips::FCOND_OLT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000234 case ISD::SETGT:
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000235 case ISD::SETOGT: return Mips::FCOND_OGT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000236 case ISD::SETLE:
237 case ISD::SETOLE: return Mips::FCOND_OLE;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000238 case ISD::SETGE:
239 case ISD::SETOGE: return Mips::FCOND_OGE;
240 case ISD::SETULT: return Mips::FCOND_ULT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000241 case ISD::SETULE: return Mips::FCOND_ULE;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000242 case ISD::SETUGT: return Mips::FCOND_UGT;
243 case ISD::SETUGE: return Mips::FCOND_UGE;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000244 case ISD::SETUO: return Mips::FCOND_UN;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000245 case ISD::SETO: return Mips::FCOND_OR;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000246 case ISD::SETNE:
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000247 case ISD::SETONE: return Mips::FCOND_NEQ;
248 case ISD::SETUEQ: return Mips::FCOND_UEQ;
249 }
250}
251
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000252MachineBasicBlock *
253MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000254 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000255 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
256 bool isFPCmp = false;
Dale Johannesen94817572009-02-13 02:34:39 +0000257 DebugLoc dl = MI->getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000258
259 switch (MI->getOpcode()) {
260 default: assert(false && "Unexpected instr type to insert");
261 case Mips::Select_FCC:
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000262 case Mips::Select_FCC_S32:
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000263 case Mips::Select_FCC_D32:
264 isFPCmp = true; // FALL THROUGH
265 case Mips::Select_CC:
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000266 case Mips::Select_CC_S32:
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000267 case Mips::Select_CC_D32: {
268 // To "insert" a SELECT_CC instruction, we actually have to insert the
269 // diamond control-flow pattern. The incoming instruction knows the
270 // destination vreg to set, the condition code register to branch on, the
271 // true/false values to select between, and a branch opcode to use.
272 const BasicBlock *LLVM_BB = BB->getBasicBlock();
273 MachineFunction::iterator It = BB;
274 ++It;
275
276 // thisMBB:
277 // ...
278 // TrueVal = ...
279 // setcc r1, r2, r3
280 // bNE r1, r0, copy1MBB
281 // fallthrough --> copy0MBB
282 MachineBasicBlock *thisMBB = BB;
283 MachineFunction *F = BB->getParent();
284 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
285 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman14152b42010-07-06 20:24:04 +0000286 F->insert(It, copy0MBB);
287 F->insert(It, sinkMBB);
288
289 // Transfer the remainder of BB and its successor edges to sinkMBB.
290 sinkMBB->splice(sinkMBB->begin(), BB,
291 llvm::next(MachineBasicBlock::iterator(MI)),
292 BB->end());
293 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
294
295 // Next, add the true and fallthrough blocks as its successors.
296 BB->addSuccessor(copy0MBB);
297 BB->addSuccessor(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000298
299 // Emit the right instruction according to the type of the operands compared
300 if (isFPCmp) {
301 // Find the condiction code present in the setcc operation.
302 Mips::CondCode CC = (Mips::CondCode)MI->getOperand(4).getImm();
303 // Get the branch opcode from the branch code.
304 unsigned Opc = FPBranchCodeToOpc(GetFPBranchCodeFromCond(CC));
Dale Johannesen94817572009-02-13 02:34:39 +0000305 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000306 } else
Dale Johannesen94817572009-02-13 02:34:39 +0000307 BuildMI(BB, dl, TII->get(Mips::BNE)).addReg(MI->getOperand(1).getReg())
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000308 .addReg(Mips::ZERO).addMBB(sinkMBB);
309
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000310 // copy0MBB:
311 // %FalseValue = ...
312 // # fallthrough to sinkMBB
313 BB = copy0MBB;
314
315 // Update machine-CFG edges
316 BB->addSuccessor(sinkMBB);
317
318 // sinkMBB:
Bruno Cardoso Lopes29e9daa2010-07-20 07:58:51 +0000319 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000320 // ...
321 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +0000322 BuildMI(*BB, BB->begin(), dl,
323 TII->get(Mips::PHI), MI->getOperand(0).getReg())
Bruno Cardoso Lopes29e9daa2010-07-20 07:58:51 +0000324 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
325 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000326
Dan Gohman14152b42010-07-06 20:24:04 +0000327 MI->eraseFromParent(); // The pseudo instruction is gone now.
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000328 return BB;
329 }
330 }
331}
332
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000333//===----------------------------------------------------------------------===//
334// Misc Lower Operation implementation
335//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000336
Dan Gohman475871a2008-07-27 21:46:04 +0000337SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000338LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000339{
340 if (!Subtarget->isMips1())
341 return Op;
342
343 MachineFunction &MF = DAG.getMachineFunction();
344 unsigned CCReg = AddLiveIn(MF, Mips::FCR31, Mips::CCRRegisterClass);
345
346 SDValue Chain = DAG.getEntryNode();
347 DebugLoc dl = Op.getDebugLoc();
348 SDValue Src = Op.getOperand(0);
349
350 // Set the condition register
Owen Anderson825b72b2009-08-11 20:47:22 +0000351 SDValue CondReg = DAG.getCopyFromReg(Chain, dl, CCReg, MVT::i32);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000352 CondReg = DAG.getCopyToReg(Chain, dl, Mips::AT, CondReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000353 CondReg = DAG.getCopyFromReg(CondReg, dl, Mips::AT, MVT::i32);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000354
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 SDValue Cst = DAG.getConstant(3, MVT::i32);
356 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, CondReg, Cst);
357 Cst = DAG.getConstant(2, MVT::i32);
358 SDValue Xor = DAG.getNode(ISD::XOR, dl, MVT::i32, Or, Cst);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000359
360 SDValue InFlag(0, 0);
361 CondReg = DAG.getCopyToReg(Chain, dl, Mips::FCR31, Xor, InFlag);
362
363 // Emit the round instruction and bit convert to integer
Owen Anderson825b72b2009-08-11 20:47:22 +0000364 SDValue Trunc = DAG.getNode(MipsISD::FPRound, dl, MVT::f32,
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000365 Src, CondReg.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000366 SDValue BitCvt = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Trunc);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000367 return BitCvt;
368}
369
370SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000371LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000372{
373 SDValue Chain = Op.getOperand(0);
374 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +0000375 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000376
377 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +0000378 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000379
380 // Subtract the dynamic size from the actual stack size to
381 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000383
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000384 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000385 // must be placed in the stack pointer register.
Dale Johannesena05dca42009-02-04 23:02:30 +0000386 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000387
388 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000389 // value and a chain
390 SDValue Ops[2] = { Sub, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000391 return DAG.getMergeValues(Ops, 2, dl);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000392}
393
394SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000395LowerANDOR(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000396{
397 SDValue LHS = Op.getOperand(0);
398 SDValue RHS = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +0000399 DebugLoc dl = Op.getDebugLoc();
400
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000401 if (LHS.getOpcode() != MipsISD::FPCmp || RHS.getOpcode() != MipsISD::FPCmp)
402 return Op;
403
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 SDValue True = DAG.getConstant(1, MVT::i32);
405 SDValue False = DAG.getConstant(0, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000406
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000407 SDValue LSEL = DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000408 LHS, True, False, LHS.getOperand(2));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000409 SDValue RSEL = DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000410 RHS, True, False, RHS.getOperand(2));
411
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 return DAG.getNode(Op.getOpcode(), dl, MVT::i32, LSEL, RSEL);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000413}
414
415SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000416LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000417{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000418 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000419 // the block to branch to if the condition is true.
420 SDValue Chain = Op.getOperand(0);
421 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000422 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000423
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000424 if (Op.getOperand(1).getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000425 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000426
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000427 SDValue CondRes = Op.getOperand(1);
428 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000429 Mips::CondCode CC =
430 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000431 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000432
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000433 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000434 Dest, CondRes);
435}
436
437SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000438LowerSETCC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000439{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000440 // The operands to this are the left and right operands to compare (ops #0,
441 // and #1) and the condition code to compare them with (op #2) as a
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000442 // CondCodeSDNode.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000443 SDValue LHS = Op.getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +0000444 SDValue RHS = Op.getOperand(1);
445 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000446
447 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000448
449 return DAG.getNode(MipsISD::FPCmp, dl, Op.getValueType(), LHS, RHS,
Owen Anderson825b72b2009-08-11 20:47:22 +0000450 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000451}
452
453SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000454LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000455{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000456 SDValue Cond = Op.getOperand(0);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000457 SDValue True = Op.getOperand(1);
458 SDValue False = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000459 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000460
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000461 // if the incomming condition comes from a integer compare, the select
462 // operation must be SelectCC or a conditional move if the subtarget
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000463 // supports it.
464 if (Cond.getOpcode() != MipsISD::FPCmp) {
465 if (Subtarget->hasCondMov() && !True.getValueType().isFloatingPoint())
466 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000467 return DAG.getNode(MipsISD::SelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000468 Cond, True, False);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000469 }
470
471 // if the incomming condition comes from fpcmp, the select
472 // operation must use FPSelectCC.
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000473 SDValue CCNode = Cond.getOperand(2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000474 return DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000475 Cond, True, False, CCNode);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000476}
477
Dan Gohmand858e902010-04-17 15:26:15 +0000478SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
479 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +0000480 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000481 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000482 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000483
Eli Friedmane2c74082009-08-03 02:22:28 +0000484 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Chris Lattnere3736f82009-08-13 05:41:27 +0000485 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000486
Chris Lattnerb71b9092009-08-13 06:28:06 +0000487 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000488
Chris Lattnere3736f82009-08-13 05:41:27 +0000489 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000490 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
491 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000492 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +0000493 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
494 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000495 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +0000496 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000497 // %hi/%lo relocation
Devang Patel0d881da2010-07-06 22:08:15 +0000498 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000499 MipsII::MO_ABS_HILO);
Chris Lattnere3736f82009-08-13 05:41:27 +0000500 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GA, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000501 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GA);
502 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000503
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000504 } else {
Devang Patel0d881da2010-07-06 22:08:15 +0000505 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000506 MipsII::MO_GOT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000507 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000508 DAG.getEntryNode(), GA, MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000509 false, false, 0);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000510 // On functions and global targets not internal linked only
511 // a load from got/GP is necessary for PIC to work.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000512 if (!GV->hasLocalLinkage() || isa<Function>(GV))
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000513 return ResNode;
Owen Anderson825b72b2009-08-11 20:47:22 +0000514 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GA);
515 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000516 }
517
Torok Edwinc23197a2009-07-14 16:55:14 +0000518 llvm_unreachable("Dont know how to handle GlobalAddress");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000519 return SDValue(0,0);
520}
521
522SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000523LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000524{
Torok Edwinc23197a2009-07-14 16:55:14 +0000525 llvm_unreachable("TLS not implemented for MIPS.");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000526 return SDValue(); // Not reached
527}
528
529SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000530LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000531{
Dan Gohman475871a2008-07-27 21:46:04 +0000532 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000533 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +0000534 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000535 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000536 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
537 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HILO;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000538
Owen Andersone50ed302009-08-10 22:56:29 +0000539 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000540 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000541
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000542 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
543
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +0000544 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +0000545 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000546 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000547 } else // Emit Load from Global Pointer
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000548 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
549 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000550 false, false, 0);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000551
Owen Anderson825b72b2009-08-11 20:47:22 +0000552 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTI);
553 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000554
555 return ResNode;
556}
557
Dan Gohman475871a2008-07-27 21:46:04 +0000558SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000559LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000560{
Dan Gohman475871a2008-07-27 21:46:04 +0000561 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000562 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +0000563 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +0000564 // FIXME there isn't actually debug info here
565 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000566
567 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000568 // FIXME: we should reference the constant pool using small data sections,
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000569 // but the asm printer currently doens't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000570 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000571 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +0000572 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
574 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000575 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000576
577 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000578 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000579 N->getOffset(), MipsII::MO_ABS_HILO);
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CP);
581 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CP);
582 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000583 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000584 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000585 N->getOffset(), MipsII::MO_GOT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000586 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000587 CP, MachinePointerInfo::getConstantPool(),
588 false, false, 0);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000589 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CP);
590 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
591 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000592
593 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000594}
595
Dan Gohmand858e902010-04-17 15:26:15 +0000596SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +0000597 MachineFunction &MF = DAG.getMachineFunction();
598 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
599
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000600 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +0000601 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
602 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000603
604 // vastart just stores the address of the VarArgsFrameIndex slot into the
605 // memory location argument.
606 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +0000607 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
608 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +0000609 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000610}
611
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000612//===----------------------------------------------------------------------===//
613// Calling Convention Implementation
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000614//===----------------------------------------------------------------------===//
615
616#include "MipsGenCallingConv.inc"
617
618//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000619// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000620// Mips O32 ABI rules:
621// ---
622// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000623// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000624// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000625// f64 - Only passed in two aliased f32 registers if no int reg has been used
626// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000627// not used, it must be shadowed. If only A3 is avaiable, shadow it and
628// go to stack.
629//===----------------------------------------------------------------------===//
630
Duncan Sands1e96bab2010-11-04 10:49:57 +0000631static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000632 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000633 ISD::ArgFlagsTy ArgFlags, CCState &State) {
634
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000635 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000636
637 static const unsigned IntRegs[] = {
638 Mips::A0, Mips::A1, Mips::A2, Mips::A3
639 };
640 static const unsigned F32Regs[] = {
641 Mips::F12, Mips::F14
642 };
643 static const unsigned F64Regs[] = {
644 Mips::D6, Mips::D7
645 };
646
647 unsigned Reg=0;
648 unsigned UnallocIntReg = State.getFirstUnallocated(IntRegs, IntRegsSize);
649 bool IntRegUsed = (IntRegs[UnallocIntReg] != (unsigned (Mips::A0)));
650
651 // Promote i8 and i16
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
653 LocVT = MVT::i32;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000654 if (ArgFlags.isSExt())
655 LocInfo = CCValAssign::SExt;
656 else if (ArgFlags.isZExt())
657 LocInfo = CCValAssign::ZExt;
658 else
659 LocInfo = CCValAssign::AExt;
660 }
661
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && IntRegUsed)) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000663 Reg = State.AllocateReg(IntRegs, IntRegsSize);
664 IntRegUsed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 LocVT = MVT::i32;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000666 }
667
668 if (ValVT.isFloatingPoint() && !IntRegUsed) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000669 if (ValVT == MVT::f32)
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000670 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
671 else
672 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
673 }
674
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 if (ValVT == MVT::f64 && IntRegUsed) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000676 if (UnallocIntReg != IntRegsSize) {
677 // If we hit register A3 as the first not allocated, we must
678 // mark it as allocated (shadow) and use the stack instead.
679 if (IntRegs[UnallocIntReg] != (unsigned (Mips::A3)))
680 Reg = Mips::A2;
681 for (;UnallocIntReg < IntRegsSize; ++UnallocIntReg)
682 State.AllocateReg(UnallocIntReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000683 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 LocVT = MVT::i32;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000685 }
686
687 if (!Reg) {
688 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
689 unsigned Offset = State.AllocateStack(SizeInBytes, SizeInBytes);
690 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
691 } else
692 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
693
694 return false; // CC must always match
695}
696
Duncan Sands1e96bab2010-11-04 10:49:57 +0000697static bool CC_MipsO32_VarArgs(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000698 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +0000699 ISD::ArgFlagsTy ArgFlags, CCState &State) {
700
701 static const unsigned IntRegsSize=4;
702
703 static const unsigned IntRegs[] = {
704 Mips::A0, Mips::A1, Mips::A2, Mips::A3
705 };
706
707 // Promote i8 and i16
708 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
709 LocVT = MVT::i32;
710 if (ArgFlags.isSExt())
711 LocInfo = CCValAssign::SExt;
712 else if (ArgFlags.isZExt())
713 LocInfo = CCValAssign::ZExt;
714 else
715 LocInfo = CCValAssign::AExt;
716 }
717
718 if (ValVT == MVT::i32 || ValVT == MVT::f32) {
719 if (unsigned Reg = State.AllocateReg(IntRegs, IntRegsSize)) {
720 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, MVT::i32, LocInfo));
721 return false;
722 }
723 unsigned Off = State.AllocateStack(4, 4);
724 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Off, LocVT, LocInfo));
725 return false;
726 }
727
728 unsigned UnallocIntReg = State.getFirstUnallocated(IntRegs, IntRegsSize);
729 if (ValVT == MVT::f64) {
730 if (IntRegs[UnallocIntReg] == (unsigned (Mips::A1))) {
731 // A1 can't be used anymore, because 64 bit arguments
732 // must be aligned when copied back to the caller stack
733 State.AllocateReg(IntRegs, IntRegsSize);
734 UnallocIntReg++;
735 }
736
737 if (IntRegs[UnallocIntReg] == (unsigned (Mips::A0)) ||
738 IntRegs[UnallocIntReg] == (unsigned (Mips::A2))) {
739 unsigned Reg = State.AllocateReg(IntRegs, IntRegsSize);
740 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, MVT::i32, LocInfo));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000741 // Shadow the next register so it can be used
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +0000742 // later to get the other 32bit part.
743 State.AllocateReg(IntRegs, IntRegsSize);
744 return false;
745 }
746
747 // Register is shadowed to preserve alignment, and the
748 // argument goes to a stack location.
749 if (UnallocIntReg != IntRegsSize)
750 State.AllocateReg(IntRegs, IntRegsSize);
751
752 unsigned Off = State.AllocateStack(8, 8);
753 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Off, LocVT, LocInfo));
754 return false;
755 }
756
757 return true; // CC didn't match
758}
759
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000760//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +0000761// Call Calling Convention Implementation
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000762//===----------------------------------------------------------------------===//
763
Dan Gohman98ca4f22009-08-05 01:29:28 +0000764/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +0000765/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +0000766/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000767SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000768MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000769 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000770 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000771 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000772 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000773 const SmallVectorImpl<ISD::InputArg> &Ins,
774 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000775 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000776 // MIPs target does not yet support tail call optimization.
777 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000778
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000779 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000780 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000781 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000782
783 // Analyze operands of the call, assigning locations to each operand.
784 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000785 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
786 *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000787
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000788 // To meet O32 ABI, Mips must always allocate 16 bytes on
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000789 // the stack (even if less than 4 are used as arguments)
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000790 if (Subtarget->isABI_O32()) {
Duncan Sands1e96bab2010-11-04 10:49:57 +0000791 int VTsize = MVT(MVT::i32).getSizeInBits()/8;
Evan Chenged2ae132010-07-03 00:40:23 +0000792 MFI->CreateFixedObject(VTsize, (VTsize*3), true);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000793 CCInfo.AnalyzeCallOperands(Outs,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +0000794 isVarArg ? CC_MipsO32_VarArgs : CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000795 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +0000796 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000797
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000798 // Get a count of how many bytes are to be pushed on the stack.
799 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnere563bbc2008-10-11 22:08:30 +0000800 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000801
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000802 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +0000803 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
804 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000805
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000806 // First/LastArgStackLoc contains the first/last
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000807 // "at stack" argument location.
808 int LastArgStackLoc = 0;
809 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000810
811 // Walk the register/memloc assignments, inserting copies/loads.
812 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +0000813 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000814 CCValAssign &VA = ArgLocs[i];
815
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000816 // Promote the value if needed.
817 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000818 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000819 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000820 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000821 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000822 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000823 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000824 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000825 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000826 DAG.getConstant(0, getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +0000827 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000828 DAG.getConstant(1, getPointerTy()));
829 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
830 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
831 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000832 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000833 }
834 break;
Chris Lattnere0b12152008-03-17 06:57:02 +0000835 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000836 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000837 break;
838 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000839 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000840 break;
841 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000842 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000843 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000844 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000845
846 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000847 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000848 if (VA.isRegLoc()) {
849 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +0000850 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000851 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000852
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000853 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +0000854 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000855
Chris Lattnere0b12152008-03-17 06:57:02 +0000856 // Create the frame index object for this incoming parameter
857 // This guarantees that when allocating Local Area the firsts
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000858 // 16 bytes which are alwayes reserved won't be overwritten
859 // if O32 ABI is used. For EABI the first address is zero.
860 LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000861 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +0000862 LastArgStackLoc, true);
Chris Lattnere0b12152008-03-17 06:57:02 +0000863
Dan Gohman475871a2008-07-27 21:46:04 +0000864 SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +0000865
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000866 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +0000867 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +0000868 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
869 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000870 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000871 }
872
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000873 // Transform all store nodes into one single node because all store
874 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000875 if (!MemOpChains.empty())
876 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000877 &MemOpChains[0], MemOpChains.size());
878
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000879 // Build a sequence of copy-to-reg nodes chained together with token
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000880 // chain and flag operands which copy the outgoing args into registers.
881 // The InFlag in necessary since all emited instructions must be
882 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000883 SDValue InFlag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000884 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000885 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000886 RegsToPass[i].second, InFlag);
887 InFlag = Chain.getValue(1);
888 }
889
Bill Wendling056292f2008-09-16 21:48:12 +0000890 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000891 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
892 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000893 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000894 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
895 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000896 getPointerTy(), 0, OpFlag);
Bill Wendling056292f2008-09-16 21:48:12 +0000897 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000898 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000899 getPointerTy(), OpFlag);
Bill Wendling056292f2008-09-16 21:48:12 +0000900
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000901 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000902 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000903 //
904 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +0000905 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000906 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000907 Ops.push_back(Chain);
908 Ops.push_back(Callee);
909
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000910 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000911 // known live into the call.
912 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
913 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
914 RegsToPass[i].second.getValueType()));
915
Gabor Greifba36cb52008-08-28 21:40:38 +0000916 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000917 Ops.push_back(InFlag);
918
Dale Johannesen33c960f2009-02-04 20:06:27 +0000919 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000920 InFlag = Chain.getValue(1);
921
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000922 // Create a stack location to hold GP when PIC is used. This stack
923 // location is used on function prologue to save GP and also after all
924 // emited CALL's to restore GP.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000925 if (IsPIC) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000926 // Function can have an arbitrary number of calls, so
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000927 // hold the LastArgStackLoc with the biggest offset.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000928 int FI;
929 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000930 if (LastArgStackLoc >= MipsFI->getGPStackOffset()) {
931 LastArgStackLoc = (!LastArgStackLoc) ? (16) : (LastArgStackLoc+4);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000932 // Create the frame index only once. SPOffset here can be anything
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000933 // (this will be fixed on processFunctionBeforeFrameFinalized)
934 if (MipsFI->getGPStackOffset() == -1) {
Evan Chenged2ae132010-07-03 00:40:23 +0000935 FI = MFI->CreateFixedObject(4, 0, true);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000936 MipsFI->setGPFI(FI);
937 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000938 MipsFI->setGPStackOffset(LastArgStackLoc);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000939 }
940
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000941 // Reload GP value.
942 FI = MipsFI->getGPFI();
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000943 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
944 SDValue GPLoad = DAG.getLoad(MVT::i32, dl, Chain, FIN,
945 MachinePointerInfo::getFixedStack(FI),
David Greenef6fa1862010-02-15 16:56:10 +0000946 false, false, 0);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000947 Chain = GPLoad.getValue(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000948 Chain = DAG.getCopyToReg(Chain, dl, DAG.getRegister(Mips::GP, MVT::i32),
Dan Gohman475871a2008-07-27 21:46:04 +0000949 GPLoad, SDValue(0,0));
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000950 InFlag = Chain.getValue(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000951 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000952
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +0000953 // Create the CALLSEQ_END node.
954 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
955 DAG.getIntPtrConstant(0, true), InFlag);
956 InFlag = Chain.getValue(1);
957
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000958 // Handle result values, copying them out of physregs into vregs that we
959 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000960 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
961 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000962}
963
Dan Gohman98ca4f22009-08-05 01:29:28 +0000964/// LowerCallResult - Lower the result values of a call into the
965/// appropriate copies out of appropriate physical registers.
966SDValue
967MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000968 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000969 const SmallVectorImpl<ISD::InputArg> &Ins,
970 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000971 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000972
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000973 // Assign locations to each value returned by this call.
974 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000975 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000976 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000977
Dan Gohman98ca4f22009-08-05 01:29:28 +0000978 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000979
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000980 // Copy all of the result registers out of their specified physreg.
981 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000982 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +0000983 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000984 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000985 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000986 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000987
Dan Gohman98ca4f22009-08-05 01:29:28 +0000988 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000989}
990
991//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +0000992// Formal Arguments Calling Convention Implementation
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000993//===----------------------------------------------------------------------===//
994
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000995/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +0000996/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000997SDValue
998MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +0000999 CallingConv::ID CallConv, bool isVarArg,
1000 const SmallVectorImpl<ISD::InputArg>
1001 &Ins,
1002 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001003 SmallVectorImpl<SDValue> &InVals)
1004 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001005
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00001006 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001007 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001008 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001009
1010 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
Dan Gohman1e93df62010-04-17 14:41:14 +00001011 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001012
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001013 // Used with vargs to acumulate store chains.
1014 std::vector<SDValue> OutChains;
1015
1016 // Keep track of the last register used for arguments
1017 unsigned ArgRegEnd = 0;
1018
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001019 // Assign locations to all of the incoming arguments.
1020 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001021 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1022 ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001023
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001024 if (Subtarget->isABI_O32())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001025 CCInfo.AnalyzeFormalArguments(Ins,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001026 isVarArg ? CC_MipsO32_VarArgs : CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001027 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001028 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001029
Dan Gohman475871a2008-07-27 21:46:04 +00001030 SDValue StackPtr;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001031
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001032 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
1033
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001034 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001035 CCValAssign &VA = ArgLocs[i];
1036
1037 // Arguments stored on registers
1038 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001039 EVT RegVT = VA.getLocVT();
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001040 ArgRegEnd = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00001041 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001042
Owen Anderson825b72b2009-08-11 20:47:22 +00001043 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001044 RC = Mips::CPURegsRegisterClass;
1045 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001046 RC = Mips::FGR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001047 else if (RegVT == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001048 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001049 RC = Mips::AFGR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001050 } else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001051 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001052
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001053 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001054 // physical registers into virtual ones
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001055 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegEnd, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001056 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001057
1058 // If this is an 8 or 16-bit value, it has been passed promoted
1059 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001060 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001061 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00001062 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001063 if (VA.getLocInfo() == CCValAssign::SExt)
1064 Opcode = ISD::AssertSext;
1065 else if (VA.getLocInfo() == CCValAssign::ZExt)
1066 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00001067 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001068 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00001069 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00001070 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001071 }
1072
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001073 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001074 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001075 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
1076 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00001077 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001078 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001079 VA.getLocReg()+1, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001080 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001081 SDValue Hi = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
1082 SDValue Lo = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue2);
Owen Anderson825b72b2009-08-11 20:47:22 +00001083 ArgValue = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::f64, Lo, Hi);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001084 }
1085 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001086
Dan Gohman98ca4f22009-08-05 01:29:28 +00001087 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001088 } else { // VA.isRegLoc()
1089
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001090 // sanity check
1091 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001092
1093 // The last argument is not a register anymore
1094 ArgRegEnd = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001095
1096 // The stack pointer offset is relative to the caller stack frame.
1097 // Since the real stack size is unknown here, a negative SPOffset
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001098 // is used so there's a way to adjust these offsets when the stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001099 // size get known (on EliminateFrameIndex). A dummy SPOffset is
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001100 // used instead of a direct negative address (which is recorded to
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001101 // be used on emitPrologue) to avoid mis-calc of the first stack
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001102 // offset on PEI::calculateFrameObjectOffsets.
1103 // Arguments are always 32-bit.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001104 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
Evan Chenged2ae132010-07-03 00:40:23 +00001105 int FI = MFI->CreateFixedObject(ArgSize, 0, true);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001106 MipsFI->recordLoadArgsFI(FI, -(ArgSize+
1107 (FirstStackArgLoc + VA.getLocMemOffset())));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001108
1109 // Create load nodes to retrieve arguments from the stack
Dan Gohman475871a2008-07-27 21:46:04 +00001110 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001111 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
1112 MachinePointerInfo::getFixedStack(FI),
David Greenef6fa1862010-02-15 16:56:10 +00001113 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001114 }
1115 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001116
1117 // The mips ABIs for returning structs by value requires that we copy
1118 // the sret argument into $v0 for the return. Save the argument into
1119 // a virtual register so that we can access it from the return points.
1120 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1121 unsigned Reg = MipsFI->getSRetReturnReg();
1122 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001123 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001124 MipsFI->setSRetReturnReg(Reg);
1125 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001126 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001127 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001128 }
1129
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001130 // To meet ABI, when VARARGS are passed on registers, the registers
1131 // must have their values written to the caller stack frame. If the last
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001132 // argument was placed in the stack, there's no need to save any register.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001133 if ((isVarArg) && (Subtarget->isABI_O32() && ArgRegEnd)) {
1134 if (StackPtr.getNode() == 0)
1135 StackPtr = DAG.getRegister(StackReg, getPointerTy());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001136
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001137 // The last register argument that must be saved is Mips::A3
1138 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
1139 unsigned StackLoc = ArgLocs.size()-1;
1140
1141 for (++ArgRegEnd; ArgRegEnd <= Mips::A3; ++ArgRegEnd, ++StackLoc) {
1142 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegEnd, RC);
1143 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
1144
Evan Chenged2ae132010-07-03 00:40:23 +00001145 int FI = MFI->CreateFixedObject(4, 0, true);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001146 MipsFI->recordStoreVarArgsFI(FI, -(4+(StackLoc*4)));
1147 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00001148 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
1149 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001150 false, false, 0));
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001151
1152 // Record the frame index of the first variable argument
1153 // which is a value necessary to VASTART.
Dan Gohman1e93df62010-04-17 14:41:14 +00001154 if (!MipsFI->getVarArgsFrameIndex())
1155 MipsFI->setVarArgsFrameIndex(FI);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001156 }
1157 }
1158
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001159 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001160 // the size of Ins and InVals. This only happens when on varg functions
1161 if (!OutChains.empty()) {
1162 OutChains.push_back(Chain);
1163 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1164 &OutChains[0], OutChains.size());
1165 }
1166
Dan Gohman98ca4f22009-08-05 01:29:28 +00001167 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001168}
1169
1170//===----------------------------------------------------------------------===//
1171// Return Value Calling Convention Implementation
1172//===----------------------------------------------------------------------===//
1173
Dan Gohman98ca4f22009-08-05 01:29:28 +00001174SDValue
1175MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001176 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001177 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001178 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001179 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001180
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001181 // CCValAssign - represent the assignment of
1182 // the return value to a location
1183 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001184
1185 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001186 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1187 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001188
Dan Gohman98ca4f22009-08-05 01:29:28 +00001189 // Analize return values.
1190 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001191
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001192 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001193 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001194 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001195 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001196 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +00001197 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001198 }
1199
Dan Gohman475871a2008-07-27 21:46:04 +00001200 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001201
1202 // Copy the result values into the output registers.
1203 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1204 CCValAssign &VA = RVLocs[i];
1205 assert(VA.isRegLoc() && "Can only return in registers!");
1206
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001207 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001208 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001209
1210 // guarantee that all emitted copies are
1211 // stuck together, avoiding something bad
1212 Flag = Chain.getValue(1);
1213 }
1214
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001215 // The mips ABIs for returning structs by value requires that we copy
1216 // the sret argument into $v0 for the return. We saved the argument into
1217 // a virtual register in the entry block, so now we copy the value out
1218 // and into $v0.
1219 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1220 MachineFunction &MF = DAG.getMachineFunction();
1221 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1222 unsigned Reg = MipsFI->getSRetReturnReg();
1223
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001224 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00001225 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00001226 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001227
Dale Johannesena05dca42009-02-04 23:02:30 +00001228 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001229 Flag = Chain.getValue(1);
1230 }
1231
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001232 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00001233 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001234 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001235 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001236 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001237 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001238 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001239}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001240
1241//===----------------------------------------------------------------------===//
1242// Mips Inline Assembly Support
1243//===----------------------------------------------------------------------===//
1244
1245/// getConstraintType - Given a constraint letter, return the type of
1246/// constraint it is for this target.
1247MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001248getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001249{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001250 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001251 // GCC config/mips/constraints.md
1252 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001253 // 'd' : An address register. Equivalent to r
1254 // unless generating MIPS16 code.
1255 // 'y' : Equivalent to r; retained for
1256 // backwards compatibility.
1257 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001258 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001259 switch (Constraint[0]) {
1260 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001261 case 'd':
1262 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001263 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001264 return C_RegisterClass;
1265 break;
1266 }
1267 }
1268 return TargetLowering::getConstraintType(Constraint);
1269}
1270
John Thompson44ab89e2010-10-29 17:29:13 +00001271/// Examine constraint type and operand type and determine a weight value.
1272/// This object must already have been set up with the operand type
1273/// and the current alternative constraint selected.
1274TargetLowering::ConstraintWeight
1275MipsTargetLowering::getSingleConstraintMatchWeight(
1276 AsmOperandInfo &info, const char *constraint) const {
1277 ConstraintWeight weight = CW_Invalid;
1278 Value *CallOperandVal = info.CallOperandVal;
1279 // If we don't have a value, we can't do a match,
1280 // but allow it at the lowest weight.
1281 if (CallOperandVal == NULL)
1282 return CW_Default;
1283 const Type *type = CallOperandVal->getType();
1284 // Look at the constraint type.
1285 switch (*constraint) {
1286 default:
1287 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
1288 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001289 case 'd':
1290 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00001291 if (type->isIntegerTy())
1292 weight = CW_Register;
1293 break;
1294 case 'f':
1295 if (type->isFloatTy())
1296 weight = CW_Register;
1297 break;
1298 }
1299 return weight;
1300}
1301
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001302/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
1303/// return a list of registers that can be used to satisfy the constraint.
1304/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001305std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00001306getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001307{
1308 if (Constraint.size() == 1) {
1309 switch (Constraint[0]) {
1310 case 'r':
1311 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001312 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001313 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001314 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001315 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001316 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1317 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001318 }
1319 }
1320 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1321}
1322
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001323/// Given a register class constraint, like 'r', if this corresponds directly
1324/// to an LLVM register class, return a register of 0 and the register class
1325/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001326std::vector<unsigned> MipsTargetLowering::
1327getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001328 EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001329{
1330 if (Constraint.size() != 1)
1331 return std::vector<unsigned>();
1332
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001333 switch (Constraint[0]) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001334 default : break;
1335 case 'r':
1336 // GCC Mips Constraint Letters
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001337 case 'd':
1338 case 'y':
1339 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
1340 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
1341 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001342 Mips::T8, 0);
1343
1344 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001345 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001346 if (Subtarget->isSingleFloat())
1347 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
1348 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
1349 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
1350 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
1351 Mips::F30, Mips::F31, 0);
1352 else
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001353 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
1354 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001355 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +00001356 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001357
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001358 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001359 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001360 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
1361 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001362 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001363 }
1364 return std::vector<unsigned>();
1365}
Dan Gohman6520e202008-10-18 02:06:02 +00001366
1367bool
1368MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1369 // The Mips target isn't yet aware of offsets.
1370 return false;
1371}
Evan Chengeb2f9692009-10-27 19:56:55 +00001372
Evan Chenga1eaa3c2009-10-28 01:43:28 +00001373bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
1374 if (VT != MVT::f32 && VT != MVT::f64)
1375 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00001376 return Imm.isZero();
1377}