blob: 8ba7b632bbe97879c41b4eb984add2259f36e249 [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);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000118
119 if (!Subtarget->isMips32r2())
120 setOperationAction(ISD::ROTR, MVT::i32, Expand);
121
Owen Anderson825b72b2009-08-11 20:47:22 +0000122 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
123 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
124 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
125 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
126 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
127 setOperationAction(ISD::FSIN, MVT::f32, Expand);
128 setOperationAction(ISD::FCOS, MVT::f32, Expand);
129 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
130 setOperationAction(ISD::FPOW, MVT::f32, Expand);
131 setOperationAction(ISD::FLOG, MVT::f32, Expand);
132 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
133 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
134 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000135
Owen Anderson825b72b2009-08-11 20:47:22 +0000136 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000137
138 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
140 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
141 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000142
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000143 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000144 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000145
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000146 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000147 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
148 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000149 }
150
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000151 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000152 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000153
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000154 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000155 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000156
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000157 setStackPointerRegisterToSaveRestore(Mips::SP);
158 computeRegisterProperties();
159}
160
Owen Anderson825b72b2009-08-11 20:47:22 +0000161MVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
162 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000163}
164
Bill Wendlingb4202b82009-07-01 18:50:55 +0000165/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000166unsigned MipsTargetLowering::getFunctionAlignment(const Function *) const {
167 return 2;
168}
Scott Michel5b8f82e2008-03-10 15:42:14 +0000169
Dan Gohman475871a2008-07-27 21:46:04 +0000170SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000171LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000172{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000173 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000174 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000175 case ISD::AND: return LowerANDOR(Op, DAG);
176 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000177 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
178 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000179 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000180 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
181 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
182 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
183 case ISD::OR: return LowerANDOR(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000184 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000185 case ISD::SETCC: return LowerSETCC(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000186 case ISD::VASTART: return LowerVASTART(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000187 }
Dan Gohman475871a2008-07-27 21:46:04 +0000188 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000189}
190
191//===----------------------------------------------------------------------===//
192// Lower helper functions
193//===----------------------------------------------------------------------===//
194
195// AddLiveIn - This helper function adds the specified physical register to the
196// MachineFunction as a live in value. It also creates a corresponding
197// virtual register for it.
198static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000199AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000200{
201 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000202 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
203 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000204 return VReg;
205}
206
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000207// Get fp branch code (not opcode) from condition code.
208static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
209 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
210 return Mips::BRANCH_T;
211
212 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
213 return Mips::BRANCH_F;
214
215 return Mips::BRANCH_INVALID;
216}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000217
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000218static unsigned FPBranchCodeToOpc(Mips::FPBranchCode BC) {
219 switch(BC) {
220 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000221 llvm_unreachable("Unknown branch code");
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000222 case Mips::BRANCH_T : return Mips::BC1T;
223 case Mips::BRANCH_F : return Mips::BC1F;
224 case Mips::BRANCH_TL : return Mips::BC1TL;
225 case Mips::BRANCH_FL : return Mips::BC1FL;
226 }
227}
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000228
229static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
230 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000231 default: llvm_unreachable("Unknown fp condition code!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000232 case ISD::SETEQ:
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000233 case ISD::SETOEQ: return Mips::FCOND_EQ;
234 case ISD::SETUNE: return Mips::FCOND_OGL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000235 case ISD::SETLT:
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000236 case ISD::SETOLT: return Mips::FCOND_OLT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000237 case ISD::SETGT:
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000238 case ISD::SETOGT: return Mips::FCOND_OGT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000239 case ISD::SETLE:
240 case ISD::SETOLE: return Mips::FCOND_OLE;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000241 case ISD::SETGE:
242 case ISD::SETOGE: return Mips::FCOND_OGE;
243 case ISD::SETULT: return Mips::FCOND_ULT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000244 case ISD::SETULE: return Mips::FCOND_ULE;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000245 case ISD::SETUGT: return Mips::FCOND_UGT;
246 case ISD::SETUGE: return Mips::FCOND_UGE;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000247 case ISD::SETUO: return Mips::FCOND_UN;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000248 case ISD::SETO: return Mips::FCOND_OR;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000249 case ISD::SETNE:
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000250 case ISD::SETONE: return Mips::FCOND_NEQ;
251 case ISD::SETUEQ: return Mips::FCOND_UEQ;
252 }
253}
254
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000255MachineBasicBlock *
256MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000257 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000258 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
259 bool isFPCmp = false;
Dale Johannesen94817572009-02-13 02:34:39 +0000260 DebugLoc dl = MI->getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000261
262 switch (MI->getOpcode()) {
263 default: assert(false && "Unexpected instr type to insert");
264 case Mips::Select_FCC:
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000265 case Mips::Select_FCC_S32:
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000266 case Mips::Select_FCC_D32:
267 isFPCmp = true; // FALL THROUGH
268 case Mips::Select_CC:
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000269 case Mips::Select_CC_S32:
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000270 case Mips::Select_CC_D32: {
271 // To "insert" a SELECT_CC instruction, we actually have to insert the
272 // diamond control-flow pattern. The incoming instruction knows the
273 // destination vreg to set, the condition code register to branch on, the
274 // true/false values to select between, and a branch opcode to use.
275 const BasicBlock *LLVM_BB = BB->getBasicBlock();
276 MachineFunction::iterator It = BB;
277 ++It;
278
279 // thisMBB:
280 // ...
281 // TrueVal = ...
282 // setcc r1, r2, r3
283 // bNE r1, r0, copy1MBB
284 // fallthrough --> copy0MBB
285 MachineBasicBlock *thisMBB = BB;
286 MachineFunction *F = BB->getParent();
287 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
288 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohman14152b42010-07-06 20:24:04 +0000289 F->insert(It, copy0MBB);
290 F->insert(It, sinkMBB);
291
292 // Transfer the remainder of BB and its successor edges to sinkMBB.
293 sinkMBB->splice(sinkMBB->begin(), BB,
294 llvm::next(MachineBasicBlock::iterator(MI)),
295 BB->end());
296 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
297
298 // Next, add the true and fallthrough blocks as its successors.
299 BB->addSuccessor(copy0MBB);
300 BB->addSuccessor(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000301
302 // Emit the right instruction according to the type of the operands compared
303 if (isFPCmp) {
304 // Find the condiction code present in the setcc operation.
305 Mips::CondCode CC = (Mips::CondCode)MI->getOperand(4).getImm();
306 // Get the branch opcode from the branch code.
307 unsigned Opc = FPBranchCodeToOpc(GetFPBranchCodeFromCond(CC));
Dale Johannesen94817572009-02-13 02:34:39 +0000308 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000309 } else
Dale Johannesen94817572009-02-13 02:34:39 +0000310 BuildMI(BB, dl, TII->get(Mips::BNE)).addReg(MI->getOperand(1).getReg())
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000311 .addReg(Mips::ZERO).addMBB(sinkMBB);
312
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000313 // copy0MBB:
314 // %FalseValue = ...
315 // # fallthrough to sinkMBB
316 BB = copy0MBB;
317
318 // Update machine-CFG edges
319 BB->addSuccessor(sinkMBB);
320
321 // sinkMBB:
Bruno Cardoso Lopes29e9daa2010-07-20 07:58:51 +0000322 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000323 // ...
324 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +0000325 BuildMI(*BB, BB->begin(), dl,
326 TII->get(Mips::PHI), MI->getOperand(0).getReg())
Bruno Cardoso Lopes29e9daa2010-07-20 07:58:51 +0000327 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
328 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000329
Dan Gohman14152b42010-07-06 20:24:04 +0000330 MI->eraseFromParent(); // The pseudo instruction is gone now.
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000331 return BB;
332 }
333 }
334}
335
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000336//===----------------------------------------------------------------------===//
337// Misc Lower Operation implementation
338//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000339
Dan Gohman475871a2008-07-27 21:46:04 +0000340SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000341LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000342{
343 if (!Subtarget->isMips1())
344 return Op;
345
346 MachineFunction &MF = DAG.getMachineFunction();
347 unsigned CCReg = AddLiveIn(MF, Mips::FCR31, Mips::CCRRegisterClass);
348
349 SDValue Chain = DAG.getEntryNode();
350 DebugLoc dl = Op.getDebugLoc();
351 SDValue Src = Op.getOperand(0);
352
353 // Set the condition register
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 SDValue CondReg = DAG.getCopyFromReg(Chain, dl, CCReg, MVT::i32);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000355 CondReg = DAG.getCopyToReg(Chain, dl, Mips::AT, CondReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000356 CondReg = DAG.getCopyFromReg(CondReg, dl, Mips::AT, MVT::i32);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000357
Owen Anderson825b72b2009-08-11 20:47:22 +0000358 SDValue Cst = DAG.getConstant(3, MVT::i32);
359 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, CondReg, Cst);
360 Cst = DAG.getConstant(2, MVT::i32);
361 SDValue Xor = DAG.getNode(ISD::XOR, dl, MVT::i32, Or, Cst);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000362
363 SDValue InFlag(0, 0);
364 CondReg = DAG.getCopyToReg(Chain, dl, Mips::FCR31, Xor, InFlag);
365
366 // Emit the round instruction and bit convert to integer
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 SDValue Trunc = DAG.getNode(MipsISD::FPRound, dl, MVT::f32,
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000368 Src, CondReg.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000369 SDValue BitCvt = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Trunc);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000370 return BitCvt;
371}
372
373SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000374LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000375{
376 SDValue Chain = Op.getOperand(0);
377 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +0000378 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000379
380 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +0000381 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000382
383 // Subtract the dynamic size from the actual stack size to
384 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000386
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000387 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000388 // must be placed in the stack pointer register.
Dale Johannesena05dca42009-02-04 23:02:30 +0000389 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000390
391 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000392 // value and a chain
393 SDValue Ops[2] = { Sub, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000394 return DAG.getMergeValues(Ops, 2, dl);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000395}
396
397SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000398LowerANDOR(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000399{
400 SDValue LHS = Op.getOperand(0);
401 SDValue RHS = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +0000402 DebugLoc dl = Op.getDebugLoc();
403
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000404 if (LHS.getOpcode() != MipsISD::FPCmp || RHS.getOpcode() != MipsISD::FPCmp)
405 return Op;
406
Owen Anderson825b72b2009-08-11 20:47:22 +0000407 SDValue True = DAG.getConstant(1, MVT::i32);
408 SDValue False = DAG.getConstant(0, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000409
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000410 SDValue LSEL = DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000411 LHS, True, False, LHS.getOperand(2));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000412 SDValue RSEL = DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000413 RHS, True, False, RHS.getOperand(2));
414
Owen Anderson825b72b2009-08-11 20:47:22 +0000415 return DAG.getNode(Op.getOpcode(), dl, MVT::i32, LSEL, RSEL);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000416}
417
418SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000419LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000420{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000421 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000422 // the block to branch to if the condition is true.
423 SDValue Chain = Op.getOperand(0);
424 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000425 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000426
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000427 if (Op.getOperand(1).getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000428 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000429
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000430 SDValue CondRes = Op.getOperand(1);
431 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000432 Mips::CondCode CC =
433 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000434 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000435
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000436 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000437 Dest, CondRes);
438}
439
440SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000441LowerSETCC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000442{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000443 // The operands to this are the left and right operands to compare (ops #0,
444 // and #1) and the condition code to compare them with (op #2) as a
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000445 // CondCodeSDNode.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000446 SDValue LHS = Op.getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +0000447 SDValue RHS = Op.getOperand(1);
448 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000449
450 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000451
452 return DAG.getNode(MipsISD::FPCmp, dl, Op.getValueType(), LHS, RHS,
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000454}
455
456SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000457LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000458{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000459 SDValue Cond = Op.getOperand(0);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000460 SDValue True = Op.getOperand(1);
461 SDValue False = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000462 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000463
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000464 // if the incomming condition comes from a integer compare, the select
465 // operation must be SelectCC or a conditional move if the subtarget
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000466 // supports it.
467 if (Cond.getOpcode() != MipsISD::FPCmp) {
468 if (Subtarget->hasCondMov() && !True.getValueType().isFloatingPoint())
469 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000470 return DAG.getNode(MipsISD::SelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000471 Cond, True, False);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000472 }
473
474 // if the incomming condition comes from fpcmp, the select
475 // operation must use FPSelectCC.
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000476 SDValue CCNode = Cond.getOperand(2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000477 return DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000478 Cond, True, False, CCNode);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000479}
480
Dan Gohmand858e902010-04-17 15:26:15 +0000481SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
482 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +0000483 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000484 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000485 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000486
Eli Friedmane2c74082009-08-03 02:22:28 +0000487 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Chris Lattnere3736f82009-08-13 05:41:27 +0000488 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000489
Chris Lattnerb71b9092009-08-13 06:28:06 +0000490 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000491
Chris Lattnere3736f82009-08-13 05:41:27 +0000492 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000493 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
494 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000495 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +0000496 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
497 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000498 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +0000499 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000500 // %hi/%lo relocation
Devang Patel0d881da2010-07-06 22:08:15 +0000501 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000502 MipsII::MO_ABS_HILO);
Chris Lattnere3736f82009-08-13 05:41:27 +0000503 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GA, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +0000504 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GA);
505 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000506
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000507 } else {
Devang Patel0d881da2010-07-06 22:08:15 +0000508 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000509 MipsII::MO_GOT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000510 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000511 DAG.getEntryNode(), GA, MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000512 false, false, 0);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000513 // On functions and global targets not internal linked only
514 // a load from got/GP is necessary for PIC to work.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000515 if (!GV->hasLocalLinkage() || isa<Function>(GV))
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000516 return ResNode;
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GA);
518 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000519 }
520
Torok Edwinc23197a2009-07-14 16:55:14 +0000521 llvm_unreachable("Dont know how to handle GlobalAddress");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000522 return SDValue(0,0);
523}
524
525SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000526LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000527{
Torok Edwinc23197a2009-07-14 16:55:14 +0000528 llvm_unreachable("TLS not implemented for MIPS.");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000529 return SDValue(); // Not reached
530}
531
532SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000533LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000534{
Dan Gohman475871a2008-07-27 21:46:04 +0000535 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000536 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +0000537 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000538 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000539 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
540 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HILO;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000541
Owen Andersone50ed302009-08-10 22:56:29 +0000542 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000543 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000544
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000545 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
546
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +0000547 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +0000548 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000549 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000550 } else // Emit Load from Global Pointer
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000551 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
552 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000553 false, false, 0);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000554
Owen Anderson825b72b2009-08-11 20:47:22 +0000555 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTI);
556 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000557
558 return ResNode;
559}
560
Dan Gohman475871a2008-07-27 21:46:04 +0000561SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000562LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000563{
Dan Gohman475871a2008-07-27 21:46:04 +0000564 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000565 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +0000566 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +0000567 // FIXME there isn't actually debug info here
568 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000569
570 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000571 // FIXME: we should reference the constant pool using small data sections,
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000572 // but the asm printer currently doens't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000573 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000574 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +0000575 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000576 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
577 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000578 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000579
580 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000581 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000582 N->getOffset(), MipsII::MO_ABS_HILO);
Owen Anderson825b72b2009-08-11 20:47:22 +0000583 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CP);
584 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CP);
585 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000586 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000587 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000588 N->getOffset(), MipsII::MO_GOT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000589 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000590 CP, MachinePointerInfo::getConstantPool(),
591 false, false, 0);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000592 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CP);
593 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
594 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000595
596 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000597}
598
Dan Gohmand858e902010-04-17 15:26:15 +0000599SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +0000600 MachineFunction &MF = DAG.getMachineFunction();
601 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
602
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000603 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +0000604 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
605 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000606
607 // vastart just stores the address of the VarArgsFrameIndex slot into the
608 // memory location argument.
609 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +0000610 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
611 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +0000612 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000613}
614
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000615//===----------------------------------------------------------------------===//
616// Calling Convention Implementation
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000617//===----------------------------------------------------------------------===//
618
619#include "MipsGenCallingConv.inc"
620
621//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000622// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000623// Mips O32 ABI rules:
624// ---
625// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000626// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000627// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000628// f64 - Only passed in two aliased f32 registers if no int reg has been used
629// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000630// not used, it must be shadowed. If only A3 is avaiable, shadow it and
631// go to stack.
632//===----------------------------------------------------------------------===//
633
Duncan Sands1e96bab2010-11-04 10:49:57 +0000634static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000635 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000636 ISD::ArgFlagsTy ArgFlags, CCState &State) {
637
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000638 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000639
640 static const unsigned IntRegs[] = {
641 Mips::A0, Mips::A1, Mips::A2, Mips::A3
642 };
643 static const unsigned F32Regs[] = {
644 Mips::F12, Mips::F14
645 };
646 static const unsigned F64Regs[] = {
647 Mips::D6, Mips::D7
648 };
649
650 unsigned Reg=0;
651 unsigned UnallocIntReg = State.getFirstUnallocated(IntRegs, IntRegsSize);
652 bool IntRegUsed = (IntRegs[UnallocIntReg] != (unsigned (Mips::A0)));
653
654 // Promote i8 and i16
Owen Anderson825b72b2009-08-11 20:47:22 +0000655 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
656 LocVT = MVT::i32;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000657 if (ArgFlags.isSExt())
658 LocInfo = CCValAssign::SExt;
659 else if (ArgFlags.isZExt())
660 LocInfo = CCValAssign::ZExt;
661 else
662 LocInfo = CCValAssign::AExt;
663 }
664
Owen Anderson825b72b2009-08-11 20:47:22 +0000665 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && IntRegUsed)) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000666 Reg = State.AllocateReg(IntRegs, IntRegsSize);
667 IntRegUsed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000668 LocVT = MVT::i32;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000669 }
670
671 if (ValVT.isFloatingPoint() && !IntRegUsed) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 if (ValVT == MVT::f32)
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000673 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
674 else
675 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
676 }
677
Owen Anderson825b72b2009-08-11 20:47:22 +0000678 if (ValVT == MVT::f64 && IntRegUsed) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000679 if (UnallocIntReg != IntRegsSize) {
680 // If we hit register A3 as the first not allocated, we must
681 // mark it as allocated (shadow) and use the stack instead.
682 if (IntRegs[UnallocIntReg] != (unsigned (Mips::A3)))
683 Reg = Mips::A2;
684 for (;UnallocIntReg < IntRegsSize; ++UnallocIntReg)
685 State.AllocateReg(UnallocIntReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000686 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000687 LocVT = MVT::i32;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000688 }
689
690 if (!Reg) {
691 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
692 unsigned Offset = State.AllocateStack(SizeInBytes, SizeInBytes);
693 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
694 } else
695 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
696
697 return false; // CC must always match
698}
699
Duncan Sands1e96bab2010-11-04 10:49:57 +0000700static bool CC_MipsO32_VarArgs(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +0000701 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +0000702 ISD::ArgFlagsTy ArgFlags, CCState &State) {
703
704 static const unsigned IntRegsSize=4;
705
706 static const unsigned IntRegs[] = {
707 Mips::A0, Mips::A1, Mips::A2, Mips::A3
708 };
709
710 // Promote i8 and i16
711 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
712 LocVT = MVT::i32;
713 if (ArgFlags.isSExt())
714 LocInfo = CCValAssign::SExt;
715 else if (ArgFlags.isZExt())
716 LocInfo = CCValAssign::ZExt;
717 else
718 LocInfo = CCValAssign::AExt;
719 }
720
721 if (ValVT == MVT::i32 || ValVT == MVT::f32) {
722 if (unsigned Reg = State.AllocateReg(IntRegs, IntRegsSize)) {
723 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, MVT::i32, LocInfo));
724 return false;
725 }
726 unsigned Off = State.AllocateStack(4, 4);
727 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Off, LocVT, LocInfo));
728 return false;
729 }
730
731 unsigned UnallocIntReg = State.getFirstUnallocated(IntRegs, IntRegsSize);
732 if (ValVT == MVT::f64) {
733 if (IntRegs[UnallocIntReg] == (unsigned (Mips::A1))) {
734 // A1 can't be used anymore, because 64 bit arguments
735 // must be aligned when copied back to the caller stack
736 State.AllocateReg(IntRegs, IntRegsSize);
737 UnallocIntReg++;
738 }
739
740 if (IntRegs[UnallocIntReg] == (unsigned (Mips::A0)) ||
741 IntRegs[UnallocIntReg] == (unsigned (Mips::A2))) {
742 unsigned Reg = State.AllocateReg(IntRegs, IntRegsSize);
743 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, MVT::i32, LocInfo));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000744 // Shadow the next register so it can be used
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +0000745 // later to get the other 32bit part.
746 State.AllocateReg(IntRegs, IntRegsSize);
747 return false;
748 }
749
750 // Register is shadowed to preserve alignment, and the
751 // argument goes to a stack location.
752 if (UnallocIntReg != IntRegsSize)
753 State.AllocateReg(IntRegs, IntRegsSize);
754
755 unsigned Off = State.AllocateStack(8, 8);
756 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Off, LocVT, LocInfo));
757 return false;
758 }
759
760 return true; // CC didn't match
761}
762
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000763//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +0000764// Call Calling Convention Implementation
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000765//===----------------------------------------------------------------------===//
766
Dan Gohman98ca4f22009-08-05 01:29:28 +0000767/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +0000768/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +0000769/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000770SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000771MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000772 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000773 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000774 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000775 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000776 const SmallVectorImpl<ISD::InputArg> &Ins,
777 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000778 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000779 // MIPs target does not yet support tail call optimization.
780 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000781
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000782 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000783 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000784 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000785
786 // Analyze operands of the call, assigning locations to each operand.
787 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000788 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
789 *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000790
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000791 // To meet O32 ABI, Mips must always allocate 16 bytes on
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000792 // the stack (even if less than 4 are used as arguments)
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000793 if (Subtarget->isABI_O32()) {
Duncan Sands1e96bab2010-11-04 10:49:57 +0000794 int VTsize = MVT(MVT::i32).getSizeInBits()/8;
Evan Chenged2ae132010-07-03 00:40:23 +0000795 MFI->CreateFixedObject(VTsize, (VTsize*3), true);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000796 CCInfo.AnalyzeCallOperands(Outs,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +0000797 isVarArg ? CC_MipsO32_VarArgs : CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000798 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +0000799 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000800
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000801 // Get a count of how many bytes are to be pushed on the stack.
802 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnere563bbc2008-10-11 22:08:30 +0000803 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000804
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000805 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +0000806 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
807 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000808
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000809 // First/LastArgStackLoc contains the first/last
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000810 // "at stack" argument location.
811 int LastArgStackLoc = 0;
812 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000813
814 // Walk the register/memloc assignments, inserting copies/loads.
815 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +0000816 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000817 CCValAssign &VA = ArgLocs[i];
818
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000819 // Promote the value if needed.
820 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000821 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000822 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000823 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000824 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000825 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000826 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000827 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000828 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000829 DAG.getConstant(0, getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +0000830 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000831 DAG.getConstant(1, getPointerTy()));
832 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
833 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
834 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000835 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000836 }
837 break;
Chris Lattnere0b12152008-03-17 06:57:02 +0000838 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000839 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000840 break;
841 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000842 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000843 break;
844 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000845 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000846 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000847 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000848
849 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000850 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000851 if (VA.isRegLoc()) {
852 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +0000853 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000854 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000855
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000856 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +0000857 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000858
Chris Lattnere0b12152008-03-17 06:57:02 +0000859 // Create the frame index object for this incoming parameter
860 // This guarantees that when allocating Local Area the firsts
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000861 // 16 bytes which are alwayes reserved won't be overwritten
862 // if O32 ABI is used. For EABI the first address is zero.
863 LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000864 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +0000865 LastArgStackLoc, true);
Chris Lattnere0b12152008-03-17 06:57:02 +0000866
Dan Gohman475871a2008-07-27 21:46:04 +0000867 SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +0000868
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000869 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +0000870 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +0000871 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
872 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +0000873 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000874 }
875
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000876 // Transform all store nodes into one single node because all store
877 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000878 if (!MemOpChains.empty())
879 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000880 &MemOpChains[0], MemOpChains.size());
881
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000882 // Build a sequence of copy-to-reg nodes chained together with token
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000883 // chain and flag operands which copy the outgoing args into registers.
884 // The InFlag in necessary since all emited instructions must be
885 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000886 SDValue InFlag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000887 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000888 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000889 RegsToPass[i].second, InFlag);
890 InFlag = Chain.getValue(1);
891 }
892
Bill Wendling056292f2008-09-16 21:48:12 +0000893 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000894 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
895 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000896 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000897 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
898 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000899 getPointerTy(), 0, OpFlag);
Bill Wendling056292f2008-09-16 21:48:12 +0000900 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000901 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000902 getPointerTy(), OpFlag);
Bill Wendling056292f2008-09-16 21:48:12 +0000903
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000904 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000905 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000906 //
907 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000909 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000910 Ops.push_back(Chain);
911 Ops.push_back(Callee);
912
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000913 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000914 // known live into the call.
915 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
916 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
917 RegsToPass[i].second.getValueType()));
918
Gabor Greifba36cb52008-08-28 21:40:38 +0000919 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000920 Ops.push_back(InFlag);
921
Dale Johannesen33c960f2009-02-04 20:06:27 +0000922 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000923 InFlag = Chain.getValue(1);
924
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000925 // Create a stack location to hold GP when PIC is used. This stack
926 // location is used on function prologue to save GP and also after all
927 // emited CALL's to restore GP.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +0000928 if (IsPIC) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000929 // Function can have an arbitrary number of calls, so
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000930 // hold the LastArgStackLoc with the biggest offset.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000931 int FI;
932 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000933 if (LastArgStackLoc >= MipsFI->getGPStackOffset()) {
934 LastArgStackLoc = (!LastArgStackLoc) ? (16) : (LastArgStackLoc+4);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000935 // Create the frame index only once. SPOffset here can be anything
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000936 // (this will be fixed on processFunctionBeforeFrameFinalized)
937 if (MipsFI->getGPStackOffset() == -1) {
Evan Chenged2ae132010-07-03 00:40:23 +0000938 FI = MFI->CreateFixedObject(4, 0, true);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000939 MipsFI->setGPFI(FI);
940 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000941 MipsFI->setGPStackOffset(LastArgStackLoc);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000942 }
943
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000944 // Reload GP value.
945 FI = MipsFI->getGPFI();
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000946 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
947 SDValue GPLoad = DAG.getLoad(MVT::i32, dl, Chain, FIN,
948 MachinePointerInfo::getFixedStack(FI),
David Greenef6fa1862010-02-15 16:56:10 +0000949 false, false, 0);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000950 Chain = GPLoad.getValue(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000951 Chain = DAG.getCopyToReg(Chain, dl, DAG.getRegister(Mips::GP, MVT::i32),
Dan Gohman475871a2008-07-27 21:46:04 +0000952 GPLoad, SDValue(0,0));
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000953 InFlag = Chain.getValue(1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000954 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000955
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +0000956 // Create the CALLSEQ_END node.
957 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
958 DAG.getIntPtrConstant(0, true), InFlag);
959 InFlag = Chain.getValue(1);
960
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000961 // Handle result values, copying them out of physregs into vregs that we
962 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000963 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
964 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000965}
966
Dan Gohman98ca4f22009-08-05 01:29:28 +0000967/// LowerCallResult - Lower the result values of a call into the
968/// appropriate copies out of appropriate physical registers.
969SDValue
970MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000971 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000972 const SmallVectorImpl<ISD::InputArg> &Ins,
973 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000974 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000975
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000976 // Assign locations to each value returned by this call.
977 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000978 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000979 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000980
Dan Gohman98ca4f22009-08-05 01:29:28 +0000981 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000982
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000983 // Copy all of the result registers out of their specified physreg.
984 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000985 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +0000986 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000987 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000988 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000989 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000990
Dan Gohman98ca4f22009-08-05 01:29:28 +0000991 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000992}
993
994//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +0000995// Formal Arguments Calling Convention Implementation
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000996//===----------------------------------------------------------------------===//
997
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000998/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +0000999/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001000SDValue
1001MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001002 CallingConv::ID CallConv, bool isVarArg,
1003 const SmallVectorImpl<ISD::InputArg>
1004 &Ins,
1005 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001006 SmallVectorImpl<SDValue> &InVals)
1007 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001008
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00001009 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001010 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001011 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001012
1013 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
Dan Gohman1e93df62010-04-17 14:41:14 +00001014 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001015
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001016 // Used with vargs to acumulate store chains.
1017 std::vector<SDValue> OutChains;
1018
1019 // Keep track of the last register used for arguments
1020 unsigned ArgRegEnd = 0;
1021
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001022 // Assign locations to all of the incoming arguments.
1023 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001024 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1025 ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001026
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001027 if (Subtarget->isABI_O32())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001028 CCInfo.AnalyzeFormalArguments(Ins,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001029 isVarArg ? CC_MipsO32_VarArgs : CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001030 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001031 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001032
Dan Gohman475871a2008-07-27 21:46:04 +00001033 SDValue StackPtr;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001034
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001035 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
1036
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001037 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001038 CCValAssign &VA = ArgLocs[i];
1039
1040 // Arguments stored on registers
1041 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001042 EVT RegVT = VA.getLocVT();
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001043 ArgRegEnd = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00001044 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001045
Owen Anderson825b72b2009-08-11 20:47:22 +00001046 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001047 RC = Mips::CPURegsRegisterClass;
1048 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001049 RC = Mips::FGR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001050 else if (RegVT == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001051 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001052 RC = Mips::AFGR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001053 } else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001054 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001055
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001056 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001057 // physical registers into virtual ones
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001058 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegEnd, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001059 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001060
1061 // If this is an 8 or 16-bit value, it has been passed promoted
1062 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001063 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001064 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00001065 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001066 if (VA.getLocInfo() == CCValAssign::SExt)
1067 Opcode = ISD::AssertSext;
1068 else if (VA.getLocInfo() == CCValAssign::ZExt)
1069 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00001070 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001071 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00001072 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00001073 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001074 }
1075
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001076 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001077 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001078 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
1079 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00001080 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001081 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001082 VA.getLocReg()+1, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001083 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001084 SDValue Hi = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
1085 SDValue Lo = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue2);
Owen Anderson825b72b2009-08-11 20:47:22 +00001086 ArgValue = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::f64, Lo, Hi);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001087 }
1088 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001089
Dan Gohman98ca4f22009-08-05 01:29:28 +00001090 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001091 } else { // VA.isRegLoc()
1092
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001093 // sanity check
1094 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001095
1096 // The last argument is not a register anymore
1097 ArgRegEnd = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001098
1099 // The stack pointer offset is relative to the caller stack frame.
1100 // Since the real stack size is unknown here, a negative SPOffset
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001101 // is used so there's a way to adjust these offsets when the stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001102 // size get known (on EliminateFrameIndex). A dummy SPOffset is
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001103 // used instead of a direct negative address (which is recorded to
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001104 // be used on emitPrologue) to avoid mis-calc of the first stack
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001105 // offset on PEI::calculateFrameObjectOffsets.
1106 // Arguments are always 32-bit.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001107 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
Evan Chenged2ae132010-07-03 00:40:23 +00001108 int FI = MFI->CreateFixedObject(ArgSize, 0, true);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001109 MipsFI->recordLoadArgsFI(FI, -(ArgSize+
1110 (FirstStackArgLoc + VA.getLocMemOffset())));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001111
1112 // Create load nodes to retrieve arguments from the stack
Dan Gohman475871a2008-07-27 21:46:04 +00001113 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001114 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
1115 MachinePointerInfo::getFixedStack(FI),
David Greenef6fa1862010-02-15 16:56:10 +00001116 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001117 }
1118 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001119
1120 // The mips ABIs for returning structs by value requires that we copy
1121 // the sret argument into $v0 for the return. Save the argument into
1122 // a virtual register so that we can access it from the return points.
1123 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1124 unsigned Reg = MipsFI->getSRetReturnReg();
1125 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001126 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001127 MipsFI->setSRetReturnReg(Reg);
1128 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001129 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001130 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001131 }
1132
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001133 // To meet ABI, when VARARGS are passed on registers, the registers
1134 // must have their values written to the caller stack frame. If the last
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001135 // argument was placed in the stack, there's no need to save any register.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001136 if ((isVarArg) && (Subtarget->isABI_O32() && ArgRegEnd)) {
1137 if (StackPtr.getNode() == 0)
1138 StackPtr = DAG.getRegister(StackReg, getPointerTy());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001139
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001140 // The last register argument that must be saved is Mips::A3
1141 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
1142 unsigned StackLoc = ArgLocs.size()-1;
1143
1144 for (++ArgRegEnd; ArgRegEnd <= Mips::A3; ++ArgRegEnd, ++StackLoc) {
1145 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegEnd, RC);
1146 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
1147
Evan Chenged2ae132010-07-03 00:40:23 +00001148 int FI = MFI->CreateFixedObject(4, 0, true);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001149 MipsFI->recordStoreVarArgsFI(FI, -(4+(StackLoc*4)));
1150 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00001151 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
1152 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001153 false, false, 0));
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001154
1155 // Record the frame index of the first variable argument
1156 // which is a value necessary to VASTART.
Dan Gohman1e93df62010-04-17 14:41:14 +00001157 if (!MipsFI->getVarArgsFrameIndex())
1158 MipsFI->setVarArgsFrameIndex(FI);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001159 }
1160 }
1161
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001162 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001163 // the size of Ins and InVals. This only happens when on varg functions
1164 if (!OutChains.empty()) {
1165 OutChains.push_back(Chain);
1166 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1167 &OutChains[0], OutChains.size());
1168 }
1169
Dan Gohman98ca4f22009-08-05 01:29:28 +00001170 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001171}
1172
1173//===----------------------------------------------------------------------===//
1174// Return Value Calling Convention Implementation
1175//===----------------------------------------------------------------------===//
1176
Dan Gohman98ca4f22009-08-05 01:29:28 +00001177SDValue
1178MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001179 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001180 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001181 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001182 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001183
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001184 // CCValAssign - represent the assignment of
1185 // the return value to a location
1186 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001187
1188 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001189 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1190 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001191
Dan Gohman98ca4f22009-08-05 01:29:28 +00001192 // Analize return values.
1193 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001194
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001195 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001196 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001197 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001198 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001199 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +00001200 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001201 }
1202
Dan Gohman475871a2008-07-27 21:46:04 +00001203 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001204
1205 // Copy the result values into the output registers.
1206 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1207 CCValAssign &VA = RVLocs[i];
1208 assert(VA.isRegLoc() && "Can only return in registers!");
1209
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001210 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001211 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001212
1213 // guarantee that all emitted copies are
1214 // stuck together, avoiding something bad
1215 Flag = Chain.getValue(1);
1216 }
1217
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001218 // The mips ABIs for returning structs by value requires that we copy
1219 // the sret argument into $v0 for the return. We saved the argument into
1220 // a virtual register in the entry block, so now we copy the value out
1221 // and into $v0.
1222 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1223 MachineFunction &MF = DAG.getMachineFunction();
1224 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1225 unsigned Reg = MipsFI->getSRetReturnReg();
1226
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001227 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00001228 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00001229 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001230
Dale Johannesena05dca42009-02-04 23:02:30 +00001231 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001232 Flag = Chain.getValue(1);
1233 }
1234
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001235 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00001236 if (Flag.getNode())
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), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001239 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001240 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00001241 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001242}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001243
1244//===----------------------------------------------------------------------===//
1245// Mips Inline Assembly Support
1246//===----------------------------------------------------------------------===//
1247
1248/// getConstraintType - Given a constraint letter, return the type of
1249/// constraint it is for this target.
1250MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001251getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001252{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001253 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001254 // GCC config/mips/constraints.md
1255 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001256 // 'd' : An address register. Equivalent to r
1257 // unless generating MIPS16 code.
1258 // 'y' : Equivalent to r; retained for
1259 // backwards compatibility.
1260 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001261 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001262 switch (Constraint[0]) {
1263 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001264 case 'd':
1265 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001266 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001267 return C_RegisterClass;
1268 break;
1269 }
1270 }
1271 return TargetLowering::getConstraintType(Constraint);
1272}
1273
John Thompson44ab89e2010-10-29 17:29:13 +00001274/// Examine constraint type and operand type and determine a weight value.
1275/// This object must already have been set up with the operand type
1276/// and the current alternative constraint selected.
1277TargetLowering::ConstraintWeight
1278MipsTargetLowering::getSingleConstraintMatchWeight(
1279 AsmOperandInfo &info, const char *constraint) const {
1280 ConstraintWeight weight = CW_Invalid;
1281 Value *CallOperandVal = info.CallOperandVal;
1282 // If we don't have a value, we can't do a match,
1283 // but allow it at the lowest weight.
1284 if (CallOperandVal == NULL)
1285 return CW_Default;
1286 const Type *type = CallOperandVal->getType();
1287 // Look at the constraint type.
1288 switch (*constraint) {
1289 default:
1290 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
1291 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001292 case 'd':
1293 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00001294 if (type->isIntegerTy())
1295 weight = CW_Register;
1296 break;
1297 case 'f':
1298 if (type->isFloatTy())
1299 weight = CW_Register;
1300 break;
1301 }
1302 return weight;
1303}
1304
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001305/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
1306/// return a list of registers that can be used to satisfy the constraint.
1307/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001308std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00001309getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001310{
1311 if (Constraint.size() == 1) {
1312 switch (Constraint[0]) {
1313 case 'r':
1314 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001315 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001316 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001317 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001318 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001319 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1320 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001321 }
1322 }
1323 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1324}
1325
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001326/// Given a register class constraint, like 'r', if this corresponds directly
1327/// to an LLVM register class, return a register of 0 and the register class
1328/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001329std::vector<unsigned> MipsTargetLowering::
1330getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001331 EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001332{
1333 if (Constraint.size() != 1)
1334 return std::vector<unsigned>();
1335
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001336 switch (Constraint[0]) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001337 default : break;
1338 case 'r':
1339 // GCC Mips Constraint Letters
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001340 case 'd':
1341 case 'y':
1342 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
1343 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
1344 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001345 Mips::T8, 0);
1346
1347 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00001348 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001349 if (Subtarget->isSingleFloat())
1350 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
1351 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
1352 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
1353 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
1354 Mips::F30, Mips::F31, 0);
1355 else
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001356 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
1357 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001358 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +00001359 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001360
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001361 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001362 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001363 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
1364 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001365 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001366 }
1367 return std::vector<unsigned>();
1368}
Dan Gohman6520e202008-10-18 02:06:02 +00001369
1370bool
1371MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1372 // The Mips target isn't yet aware of offsets.
1373 return false;
1374}
Evan Chengeb2f9692009-10-27 19:56:55 +00001375
Evan Chenga1eaa3c2009-10-28 01:43:28 +00001376bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
1377 if (VT != MVT::f32 && VT != MVT::f64)
1378 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00001379 return Imm.isZero();
1380}