blob: 2fb60839d63e100ec743d1f66c037ea4c23b3515 [file] [log] [blame]
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "mips-lower"
16
17#include "MipsISelLowering.h"
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +000018#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000019#include "MipsTargetMachine.h"
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000020#include "MipsSubtarget.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000021#include "llvm/DerivedTypes.h"
22#include "llvm/Function.h"
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +000023#include "llvm/GlobalVariable.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000024#include "llvm/Intrinsics.h"
25#include "llvm/CallingConv.h"
26#include "llvm/CodeGen/CallingConvLower.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000031#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000032#include "llvm/CodeGen/ValueTypes.h"
33#include "llvm/Support/Debug.h"
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
37const char *MipsTargetLowering::
38getTargetNodeName(unsigned Opcode) const
39{
40 switch (Opcode)
41 {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000042 case MipsISD::JmpLink : return "MipsISD::JmpLink";
43 case MipsISD::Hi : return "MipsISD::Hi";
44 case MipsISD::Lo : return "MipsISD::Lo";
45 case MipsISD::GPRel : return "MipsISD::GPRel";
46 case MipsISD::Ret : return "MipsISD::Ret";
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +000047 case MipsISD::CMov : return "MipsISD::CMov";
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000048 case MipsISD::SelectCC : return "MipsISD::SelectCC";
49 case MipsISD::FPSelectCC : return "MipsISD::FPSelectCC";
50 case MipsISD::FPBrcond : return "MipsISD::FPBrcond";
51 case MipsISD::FPCmp : return "MipsISD::FPCmp";
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +000052 case MipsISD::FPRound : return "MipsISD::FPRound";
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000053 default : return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000054 }
55}
56
57MipsTargetLowering::
58MipsTargetLowering(MipsTargetMachine &TM): TargetLowering(TM)
59{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000060 Subtarget = &TM.getSubtarget<MipsSubtarget>();
61
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000062 // Mips does not have i1 type, so use i32 for
63 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000064 setBooleanContents(ZeroOrOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000065
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000066 // JumpTable targets must use GOT when using PIC_
67 setUsesGlobalOffsetTable(true);
68
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000069 // Set up the register classes
70 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000071 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000072
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000073 // When dealing with single precision only, use libcalls
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000074 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000075 if (!Subtarget->isFP64bit())
76 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000077
Bruno Cardoso Lopes7030ae72008-07-30 19:00:31 +000078 // Legal fp constants
79 addLegalFPImmediate(APFloat(+0.0f));
80
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000081 // Load extented operations for i1 types must be promoted
Evan Cheng03294662008-10-14 21:26:46 +000082 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
83 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
84 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000085
Eli Friedman10a36592009-07-17 02:28:12 +000086 // MIPS doesn't have extending float->double load (?)
87 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
88
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000089 // Used by legalize types to correctly generate the setcc result.
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +000090 // Without this, every float setcc comes with a AND/OR with the result,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000091 // we don't want this, since the fpcmp result goes to a flag register,
92 // which is used implicitly by brcond and select operations.
93 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
94
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000095 // Mips Custom Operations
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +000096 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
97 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
98 setOperationAction(ISD::RET, MVT::Other, Custom);
99 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
100 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
101 setOperationAction(ISD::SELECT, MVT::f32, Custom);
Eli Friedman6314ac22009-06-16 06:40:59 +0000102 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000103 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000104 setOperationAction(ISD::SETCC, MVT::f32, Custom);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000105 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000106 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
107 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000108 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000109
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +0000110 // We custom lower AND/OR to handle the case where the DAG contain 'ands/ors'
111 // with operands comming from setcc fp comparions. This is necessary since
112 // the result from these setcc are in a flag registers (FCR31).
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000113 setOperationAction(ISD::AND, MVT::i32, Custom);
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +0000114 setOperationAction(ISD::OR, MVT::i32, Custom);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000115
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000116 // Operations not directly supported by Mips.
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000117 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
118 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
119 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000120 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
121 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000123 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
124 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000125 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000126 setOperationAction(ISD::ROTR, MVT::i32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000127 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
128 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
129 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Bruno Cardoso Lopes7bd71822008-07-31 18:50:54 +0000130 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Eli Friedman6314ac22009-06-16 06:40:59 +0000131 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000132 setOperationAction(ISD::FSIN, MVT::f32, Expand);
133 setOperationAction(ISD::FCOS, MVT::f32, Expand);
134 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
135 setOperationAction(ISD::FPOW, MVT::f32, Expand);
136 setOperationAction(ISD::FLOG, MVT::f32, Expand);
137 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
138 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
139 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000140
141 // We don't have line number support yet.
142 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
143 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
144 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
145 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
146
147 // Use the default for now
148 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
149 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
150 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000151
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000152 if (Subtarget->isSingleFloat())
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000153 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000154
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000155 if (!Subtarget->hasSEInReg()) {
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000156 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000157 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
158 }
159
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000160 if (!Subtarget->hasBitCount())
161 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
162
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000163 if (!Subtarget->hasSwap())
164 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
165
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000166 setStackPointerRegisterToSaveRestore(Mips::SP);
167 computeRegisterProperties();
168}
169
Duncan Sands5480c042009-01-01 15:52:00 +0000170MVT MipsTargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000171 return MVT::i32;
172}
173
Bill Wendlingb4202b82009-07-01 18:50:55 +0000174/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000175unsigned MipsTargetLowering::getFunctionAlignment(const Function *) const {
176 return 2;
177}
Scott Michel5b8f82e2008-03-10 15:42:14 +0000178
Dan Gohman475871a2008-07-27 21:46:04 +0000179SDValue MipsTargetLowering::
180LowerOperation(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000181{
182 switch (Op.getOpcode())
183 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000184 case ISD::AND: return LowerANDOR(Op, DAG);
185 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
186 case ISD::CALL: return LowerCALL(Op, DAG);
187 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
188 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
189 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000190 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000191 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
192 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
193 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
194 case ISD::OR: return LowerANDOR(Op, DAG);
195 case ISD::RET: return LowerRET(Op, DAG);
196 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000197 case ISD::SETCC: return LowerSETCC(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000198 }
Dan Gohman475871a2008-07-27 21:46:04 +0000199 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000200}
201
202//===----------------------------------------------------------------------===//
203// Lower helper functions
204//===----------------------------------------------------------------------===//
205
206// AddLiveIn - This helper function adds the specified physical register to the
207// MachineFunction as a live in value. It also creates a corresponding
208// virtual register for it.
209static unsigned
210AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
211{
212 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000213 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
214 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000215 return VReg;
216}
217
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000218// A address must be loaded from a small section if its size is less than the
219// small section size threshold. Data in this section must be addressed using
220// gp_rel operator.
221bool MipsTargetLowering::IsInSmallSection(unsigned Size) {
222 return (Size > 0 && (Size <= Subtarget->getSSectionThreshold()));
223}
224
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000225// Discover if this global address can be placed into small data/bss section.
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000226bool MipsTargetLowering::IsGlobalInSmallSection(GlobalValue *GV)
227{
228 const TargetData *TD = getTargetData();
Bruno Cardoso Lopesfeb95cc2008-07-22 15:34:27 +0000229 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
230
231 if (!GVA)
232 return false;
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000233
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000234 const Type *Ty = GV->getType()->getElementType();
Duncan Sands777d2302009-05-09 07:06:46 +0000235 unsigned Size = TD->getTypeAllocSize(Ty);
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000236
237 // if this is a internal constant string, there is a special
238 // section for it, but not in small data/bss.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000239 if (GVA->hasInitializer() && GV->hasLocalLinkage()) {
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000240 Constant *C = GVA->getInitializer();
241 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
Owen Anderson1ca29d32009-07-13 21:27:19 +0000242 if (CVA && CVA->isCString())
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000243 return false;
244 }
245
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000246 return IsInSmallSection(Size);
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000247}
248
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000249// Get fp branch code (not opcode) from condition code.
250static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
251 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
252 return Mips::BRANCH_T;
253
254 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
255 return Mips::BRANCH_F;
256
257 return Mips::BRANCH_INVALID;
258}
259
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000260static unsigned FPBranchCodeToOpc(Mips::FPBranchCode BC) {
261 switch(BC) {
262 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000263 llvm_unreachable("Unknown branch code");
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000264 case Mips::BRANCH_T : return Mips::BC1T;
265 case Mips::BRANCH_F : return Mips::BC1F;
266 case Mips::BRANCH_TL : return Mips::BC1TL;
267 case Mips::BRANCH_FL : return Mips::BC1FL;
268 }
269}
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000270
271static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
272 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000273 default: llvm_unreachable("Unknown fp condition code!");
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000274 case ISD::SETEQ:
275 case ISD::SETOEQ: return Mips::FCOND_EQ;
276 case ISD::SETUNE: return Mips::FCOND_OGL;
277 case ISD::SETLT:
278 case ISD::SETOLT: return Mips::FCOND_OLT;
279 case ISD::SETGT:
280 case ISD::SETOGT: return Mips::FCOND_OGT;
281 case ISD::SETLE:
282 case ISD::SETOLE: return Mips::FCOND_OLE;
283 case ISD::SETGE:
284 case ISD::SETOGE: return Mips::FCOND_OGE;
285 case ISD::SETULT: return Mips::FCOND_ULT;
286 case ISD::SETULE: return Mips::FCOND_ULE;
287 case ISD::SETUGT: return Mips::FCOND_UGT;
288 case ISD::SETUGE: return Mips::FCOND_UGE;
289 case ISD::SETUO: return Mips::FCOND_UN;
290 case ISD::SETO: return Mips::FCOND_OR;
291 case ISD::SETNE:
292 case ISD::SETONE: return Mips::FCOND_NEQ;
293 case ISD::SETUEQ: return Mips::FCOND_UEQ;
294 }
295}
296
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000297MachineBasicBlock *
298MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +0000299 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000300 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
301 bool isFPCmp = false;
Dale Johannesen94817572009-02-13 02:34:39 +0000302 DebugLoc dl = MI->getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000303
304 switch (MI->getOpcode()) {
305 default: assert(false && "Unexpected instr type to insert");
306 case Mips::Select_FCC:
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000307 case Mips::Select_FCC_S32:
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000308 case Mips::Select_FCC_D32:
309 isFPCmp = true; // FALL THROUGH
310 case Mips::Select_CC:
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000311 case Mips::Select_CC_S32:
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000312 case Mips::Select_CC_D32: {
313 // To "insert" a SELECT_CC instruction, we actually have to insert the
314 // diamond control-flow pattern. The incoming instruction knows the
315 // destination vreg to set, the condition code register to branch on, the
316 // true/false values to select between, and a branch opcode to use.
317 const BasicBlock *LLVM_BB = BB->getBasicBlock();
318 MachineFunction::iterator It = BB;
319 ++It;
320
321 // thisMBB:
322 // ...
323 // TrueVal = ...
324 // setcc r1, r2, r3
325 // bNE r1, r0, copy1MBB
326 // fallthrough --> copy0MBB
327 MachineBasicBlock *thisMBB = BB;
328 MachineFunction *F = BB->getParent();
329 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
330 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
331
332 // Emit the right instruction according to the type of the operands compared
333 if (isFPCmp) {
334 // Find the condiction code present in the setcc operation.
335 Mips::CondCode CC = (Mips::CondCode)MI->getOperand(4).getImm();
336 // Get the branch opcode from the branch code.
337 unsigned Opc = FPBranchCodeToOpc(GetFPBranchCodeFromCond(CC));
Dale Johannesen94817572009-02-13 02:34:39 +0000338 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000339 } else
Dale Johannesen94817572009-02-13 02:34:39 +0000340 BuildMI(BB, dl, TII->get(Mips::BNE)).addReg(MI->getOperand(1).getReg())
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000341 .addReg(Mips::ZERO).addMBB(sinkMBB);
342
343 F->insert(It, copy0MBB);
344 F->insert(It, sinkMBB);
345 // Update machine-CFG edges by first adding all successors of the current
346 // block to the new block which will contain the Phi node for the select.
347 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
348 e = BB->succ_end(); i != e; ++i)
349 sinkMBB->addSuccessor(*i);
350 // Next, remove all successors of the current block, and add the true
351 // and fallthrough blocks as its successors.
352 while(!BB->succ_empty())
353 BB->removeSuccessor(BB->succ_begin());
354 BB->addSuccessor(copy0MBB);
355 BB->addSuccessor(sinkMBB);
356
357 // copy0MBB:
358 // %FalseValue = ...
359 // # fallthrough to sinkMBB
360 BB = copy0MBB;
361
362 // Update machine-CFG edges
363 BB->addSuccessor(sinkMBB);
364
365 // sinkMBB:
366 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
367 // ...
368 BB = sinkMBB;
Dale Johannesen94817572009-02-13 02:34:39 +0000369 BuildMI(BB, dl, TII->get(Mips::PHI), MI->getOperand(0).getReg())
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000370 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
371 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB);
372
373 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
374 return BB;
375 }
376 }
377}
378
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000379//===----------------------------------------------------------------------===//
380// Misc Lower Operation implementation
381//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000382
Dan Gohman475871a2008-07-27 21:46:04 +0000383SDValue MipsTargetLowering::
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000384LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG)
385{
386 if (!Subtarget->isMips1())
387 return Op;
388
389 MachineFunction &MF = DAG.getMachineFunction();
390 unsigned CCReg = AddLiveIn(MF, Mips::FCR31, Mips::CCRRegisterClass);
391
392 SDValue Chain = DAG.getEntryNode();
393 DebugLoc dl = Op.getDebugLoc();
394 SDValue Src = Op.getOperand(0);
395
396 // Set the condition register
397 SDValue CondReg = DAG.getCopyFromReg(Chain, dl, CCReg, MVT::i32);
398 CondReg = DAG.getCopyToReg(Chain, dl, Mips::AT, CondReg);
399 CondReg = DAG.getCopyFromReg(CondReg, dl, Mips::AT, MVT::i32);
400
401 SDValue Cst = DAG.getConstant(3, MVT::i32);
402 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, CondReg, Cst);
403 Cst = DAG.getConstant(2, MVT::i32);
404 SDValue Xor = DAG.getNode(ISD::XOR, dl, MVT::i32, Or, Cst);
405
406 SDValue InFlag(0, 0);
407 CondReg = DAG.getCopyToReg(Chain, dl, Mips::FCR31, Xor, InFlag);
408
409 // Emit the round instruction and bit convert to integer
410 SDValue Trunc = DAG.getNode(MipsISD::FPRound, dl, MVT::f32,
411 Src, CondReg.getValue(1));
412 SDValue BitCvt = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Trunc);
413 return BitCvt;
414}
415
416SDValue MipsTargetLowering::
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000417LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG)
418{
419 SDValue Chain = Op.getOperand(0);
420 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +0000421 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000422
423 // Get a reference from Mips stack pointer
Dale Johannesena05dca42009-02-04 23:02:30 +0000424 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000425
426 // Subtract the dynamic size from the actual stack size to
427 // obtain the new stack size.
Dale Johannesena05dca42009-02-04 23:02:30 +0000428 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000429
430 // The Sub result contains the new stack start address, so it
431 // must be placed in the stack pointer register.
Dale Johannesena05dca42009-02-04 23:02:30 +0000432 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000433
434 // This node always has two return values: a new stack pointer
435 // value and a chain
436 SDValue Ops[2] = { Sub, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000437 return DAG.getMergeValues(Ops, 2, dl);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000438}
439
440SDValue MipsTargetLowering::
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +0000441LowerANDOR(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000442{
443 SDValue LHS = Op.getOperand(0);
444 SDValue RHS = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +0000445 DebugLoc dl = Op.getDebugLoc();
446
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000447 if (LHS.getOpcode() != MipsISD::FPCmp || RHS.getOpcode() != MipsISD::FPCmp)
448 return Op;
449
450 SDValue True = DAG.getConstant(1, MVT::i32);
451 SDValue False = DAG.getConstant(0, MVT::i32);
452
Dale Johannesende064702009-02-06 21:50:26 +0000453 SDValue LSEL = DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000454 LHS, True, False, LHS.getOperand(2));
Dale Johannesende064702009-02-06 21:50:26 +0000455 SDValue RSEL = DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000456 RHS, True, False, RHS.getOperand(2));
457
Dale Johannesende064702009-02-06 21:50:26 +0000458 return DAG.getNode(Op.getOpcode(), dl, MVT::i32, LSEL, RSEL);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000459}
460
461SDValue MipsTargetLowering::
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000462LowerBRCOND(SDValue Op, SelectionDAG &DAG)
463{
464 // The first operand is the chain, the second is the condition, the third is
465 // the block to branch to if the condition is true.
466 SDValue Chain = Op.getOperand(0);
467 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000468 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000469
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000470 if (Op.getOperand(1).getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000471 return Op;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000472
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000473 SDValue CondRes = Op.getOperand(1);
474 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000475 Mips::CondCode CC =
476 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000477 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
478
Dale Johannesende064702009-02-06 21:50:26 +0000479 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000480 Dest, CondRes);
481}
482
483SDValue MipsTargetLowering::
484LowerSETCC(SDValue Op, SelectionDAG &DAG)
485{
486 // The operands to this are the left and right operands to compare (ops #0,
487 // and #1) and the condition code to compare them with (op #2) as a
488 // CondCodeSDNode.
489 SDValue LHS = Op.getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +0000490 SDValue RHS = Op.getOperand(1);
491 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000492
493 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
494
Dale Johannesende064702009-02-06 21:50:26 +0000495 return DAG.getNode(MipsISD::FPCmp, dl, Op.getValueType(), LHS, RHS,
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000496 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
497}
498
499SDValue MipsTargetLowering::
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000500LowerSELECT(SDValue Op, SelectionDAG &DAG)
501{
502 SDValue Cond = Op.getOperand(0);
503 SDValue True = Op.getOperand(1);
504 SDValue False = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000505 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000506
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000507 // if the incomming condition comes from a integer compare, the select
508 // operation must be SelectCC or a conditional move if the subtarget
509 // supports it.
510 if (Cond.getOpcode() != MipsISD::FPCmp) {
511 if (Subtarget->hasCondMov() && !True.getValueType().isFloatingPoint())
512 return Op;
Dale Johannesende064702009-02-06 21:50:26 +0000513 return DAG.getNode(MipsISD::SelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000514 Cond, True, False);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000515 }
516
517 // if the incomming condition comes from fpcmp, the select
518 // operation must use FPSelectCC.
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000519 SDValue CCNode = Cond.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000520 return DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000521 Cond, True, False, CCNode);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000522}
523
524SDValue MipsTargetLowering::
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000525LowerGlobalAddress(SDValue Op, SelectionDAG &DAG)
526{
Dale Johannesende064702009-02-06 21:50:26 +0000527 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000528 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000529 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
530 SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
531
532 if (!Subtarget->hasABICall()) {
Dan Gohmanfc166572009-04-09 23:54:40 +0000533 SDVTList VTs = DAG.getVTList(MVT::i32);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000534 SDValue Ops[] = { GA };
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000535 // %gp_rel relocation
536 if (!isa<Function>(GV) && IsGlobalInSmallSection(GV)) {
Dan Gohmanfc166572009-04-09 23:54:40 +0000537 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, Ops, 1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000538 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000539 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000540 }
541 // %hi/%lo relocation
Dan Gohmanfc166572009-04-09 23:54:40 +0000542 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, Ops, 1);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000543 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GA);
544 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000545
546 } else { // Abicall relocations, TODO: make this cleaner.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000547 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
548 DAG.getEntryNode(), GA, NULL, 0);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000549 // On functions and global targets not internal linked only
550 // a load from got/GP is necessary for PIC to work.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000551 if (!GV->hasLocalLinkage() || isa<Function>(GV))
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000552 return ResNode;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000553 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GA);
554 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000555 }
556
Torok Edwinc23197a2009-07-14 16:55:14 +0000557 llvm_unreachable("Dont know how to handle GlobalAddress");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000558 return SDValue(0,0);
559}
560
561SDValue MipsTargetLowering::
562LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG)
563{
Torok Edwinc23197a2009-07-14 16:55:14 +0000564 llvm_unreachable("TLS not implemented for MIPS.");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000565 return SDValue(); // Not reached
566}
567
568SDValue MipsTargetLowering::
Dan Gohman475871a2008-07-27 21:46:04 +0000569LowerJumpTable(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000570{
Dan Gohman475871a2008-07-27 21:46:04 +0000571 SDValue ResNode;
572 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +0000573 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000574 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000575
Duncan Sands83ec4b62008-06-06 12:08:01 +0000576 MVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000577 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +0000578 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000579
580 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohmanfc166572009-04-09 23:54:40 +0000581 SDVTList VTs = DAG.getVTList(MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000582 SDValue Ops[] = { JTI };
Dan Gohmanfc166572009-04-09 23:54:40 +0000583 HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, Ops, 1);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000584 } else // Emit Load from Global Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +0000585 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI, NULL, 0);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000586
Dale Johannesen33c960f2009-02-04 20:06:27 +0000587 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTI);
588 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000589
590 return ResNode;
591}
592
Dan Gohman475871a2008-07-27 21:46:04 +0000593SDValue MipsTargetLowering::
594LowerConstantPool(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000595{
Dan Gohman475871a2008-07-27 21:46:04 +0000596 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000597 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
598 Constant *C = N->getConstVal();
Dan Gohman475871a2008-07-27 21:46:04 +0000599 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
Dale Johannesende064702009-02-06 21:50:26 +0000600 // FIXME there isn't actually debug info here
601 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000602
603 // gp_rel relocation
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000604 // FIXME: we should reference the constant pool using small data sections,
605 // but the asm printer currently doens't support this feature without
606 // hacking it. This feature should come soon so we can uncomment the
607 // stuff below.
608 //if (!Subtarget->hasABICall() &&
Duncan Sands777d2302009-05-09 07:06:46 +0000609 // IsInSmallSection(getTargetData()->getTypeAllocSize(C->getType()))) {
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000610 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000611 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000612 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
613 //} else { // %hi/%lo relocation
Dale Johannesende064702009-02-06 21:50:26 +0000614 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CP);
615 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CP);
616 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000617 //}
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000618
619 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000620}
621
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000622//===----------------------------------------------------------------------===//
623// Calling Convention Implementation
624//
625// The lower operations present on calling convention works on this order:
626// LowerCALL (virt regs --> phys regs, virt regs --> stack)
627// LowerFORMAL_ARGUMENTS (phys --> virt regs, stack --> virt regs)
628// LowerRET (virt regs --> phys regs)
629// LowerCALL (phys regs --> virt regs)
630//
631//===----------------------------------------------------------------------===//
632
633#include "MipsGenCallingConv.inc"
634
635//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000636// TODO: Implement a generic logic using tblgen that can support this.
637// Mips O32 ABI rules:
638// ---
639// i32 - Passed in A0, A1, A2, A3 and stack
640// f32 - Only passed in f32 registers if no int reg has been used yet to hold
641// an argument. Otherwise, passed in A1, A2, A3 and stack.
642// f64 - Only passed in two aliased f32 registers if no int reg has been used
643// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
644// not used, it must be shadowed. If only A3 is avaiable, shadow it and
645// go to stack.
646//===----------------------------------------------------------------------===//
647
648static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
649 MVT LocVT, CCValAssign::LocInfo LocInfo,
650 ISD::ArgFlagsTy ArgFlags, CCState &State) {
651
652 static const unsigned IntRegsSize=4, FloatRegsSize=2;
653
654 static const unsigned IntRegs[] = {
655 Mips::A0, Mips::A1, Mips::A2, Mips::A3
656 };
657 static const unsigned F32Regs[] = {
658 Mips::F12, Mips::F14
659 };
660 static const unsigned F64Regs[] = {
661 Mips::D6, Mips::D7
662 };
663
664 unsigned Reg=0;
665 unsigned UnallocIntReg = State.getFirstUnallocated(IntRegs, IntRegsSize);
666 bool IntRegUsed = (IntRegs[UnallocIntReg] != (unsigned (Mips::A0)));
667
668 // Promote i8 and i16
669 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
670 LocVT = MVT::i32;
671 if (ArgFlags.isSExt())
672 LocInfo = CCValAssign::SExt;
673 else if (ArgFlags.isZExt())
674 LocInfo = CCValAssign::ZExt;
675 else
676 LocInfo = CCValAssign::AExt;
677 }
678
679 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && IntRegUsed)) {
680 Reg = State.AllocateReg(IntRegs, IntRegsSize);
681 IntRegUsed = true;
682 LocVT = MVT::i32;
683 }
684
685 if (ValVT.isFloatingPoint() && !IntRegUsed) {
686 if (ValVT == MVT::f32)
687 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
688 else
689 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
690 }
691
692 if (ValVT == MVT::f64 && IntRegUsed) {
693 if (UnallocIntReg != IntRegsSize) {
694 // If we hit register A3 as the first not allocated, we must
695 // mark it as allocated (shadow) and use the stack instead.
696 if (IntRegs[UnallocIntReg] != (unsigned (Mips::A3)))
697 Reg = Mips::A2;
698 for (;UnallocIntReg < IntRegsSize; ++UnallocIntReg)
699 State.AllocateReg(UnallocIntReg);
700 }
701 LocVT = MVT::i32;
702 }
703
704 if (!Reg) {
705 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
706 unsigned Offset = State.AllocateStack(SizeInBytes, SizeInBytes);
707 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
708 } else
709 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
710
711 return false; // CC must always match
712}
713
714//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000715// CALL Calling Convention Implementation
716//===----------------------------------------------------------------------===//
717
Nate Begeman5bf4b752009-01-26 03:15:54 +0000718/// LowerCALL - functions arguments are copied from virtual regs to
719/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000720/// TODO: isVarArg, isTailCall.
Dan Gohman475871a2008-07-27 21:46:04 +0000721SDValue MipsTargetLowering::
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000722LowerCALL(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000723{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000724 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000725
Dan Gohman095cc292008-09-13 01:54:27 +0000726 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
727 SDValue Chain = TheCall->getChain();
728 SDValue Callee = TheCall->getCallee();
729 bool isVarArg = TheCall->isVarArg();
730 unsigned CC = TheCall->getCallingConv();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000731 DebugLoc dl = TheCall->getDebugLoc();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000732
733 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000734
735 // Analyze operands of the call, assigning locations to each operand.
736 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +0000737 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000738
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000739 // To meet O32 ABI, Mips must always allocate 16 bytes on
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000740 // the stack (even if less than 4 are used as arguments)
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000741 if (Subtarget->isABI_O32()) {
742 int VTsize = MVT(MVT::i32).getSizeInBits()/8;
743 MFI->CreateFixedObject(VTsize, (VTsize*3));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000744 CCInfo.AnalyzeCallOperands(TheCall, CC_MipsO32);
745 } else
746 CCInfo.AnalyzeCallOperands(TheCall, CC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000747
748 // Get a count of how many bytes are to be pushed on the stack.
749 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnere563bbc2008-10-11 22:08:30 +0000750 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000751
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000752 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +0000753 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
754 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000755
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000756 // First/LastArgStackLoc contains the first/last
757 // "at stack" argument location.
758 int LastArgStackLoc = 0;
759 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000760
761 // Walk the register/memloc assignments, inserting copies/loads.
762 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000763 SDValue Arg = TheCall->getArg(i);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000764 CCValAssign &VA = ArgLocs[i];
765
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000766 // Promote the value if needed.
767 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000768 default: llvm_unreachable("Unknown loc info!");
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000769 case CCValAssign::Full:
770 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
771 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
772 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Arg);
773 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
774 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
775 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
776 DAG.getConstant(0, getPointerTy()));
777 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
778 DAG.getConstant(1, getPointerTy()));
779 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
780 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
781 continue;
782 }
783 }
784 break;
Chris Lattnere0b12152008-03-17 06:57:02 +0000785 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000786 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000787 break;
788 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000789 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000790 break;
791 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000792 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000793 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000794 }
795
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000796 // Arguments that can be passed on register must be kept at
797 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000798 if (VA.isRegLoc()) {
799 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +0000800 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000801 }
Chris Lattnere0b12152008-03-17 06:57:02 +0000802
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000803 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +0000804 assert(VA.isMemLoc());
805
806 // Create the frame index object for this incoming parameter
807 // This guarantees that when allocating Local Area the firsts
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000808 // 16 bytes which are alwayes reserved won't be overwritten
809 // if O32 ABI is used. For EABI the first address is zero.
810 LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000811 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000812 LastArgStackLoc);
Chris Lattnere0b12152008-03-17 06:57:02 +0000813
Dan Gohman475871a2008-07-27 21:46:04 +0000814 SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +0000815
816 // emit ISD::STORE whichs stores the
817 // parameter value to a stack Location
Dale Johannesen33c960f2009-02-04 20:06:27 +0000818 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000819 }
820
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000821 // Transform all store nodes into one single node because all store
822 // nodes are independent of each other.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000823 if (!MemOpChains.empty())
Dale Johannesen33c960f2009-02-04 20:06:27 +0000824 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000825 &MemOpChains[0], MemOpChains.size());
826
827 // Build a sequence of copy-to-reg nodes chained together with token
828 // chain and flag operands which copy the outgoing args into registers.
829 // The InFlag in necessary since all emited instructions must be
830 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000831 SDValue InFlag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000832 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000833 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000834 RegsToPass[i].second, InFlag);
835 InFlag = Chain.getValue(1);
836 }
837
Bill Wendling056292f2008-09-16 21:48:12 +0000838 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
839 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
840 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000841 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000842 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +0000843 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
844 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
845
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000846 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
847 // = Chain, Callee, Reg#1, Reg#2, ...
848 //
849 // Returns a chain & a flag for retval copy to use.
850 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000851 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000852 Ops.push_back(Chain);
853 Ops.push_back(Callee);
854
855 // Add argument registers to the end of the list so that they are
856 // known live into the call.
857 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
858 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
859 RegsToPass[i].second.getValueType()));
860
Gabor Greifba36cb52008-08-28 21:40:38 +0000861 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000862 Ops.push_back(InFlag);
863
Dale Johannesen33c960f2009-02-04 20:06:27 +0000864 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000865 InFlag = Chain.getValue(1);
866
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000867 // Create the CALLSEQ_END node.
Chris Lattnere563bbc2008-10-11 22:08:30 +0000868 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
869 DAG.getIntPtrConstant(0, true), InFlag);
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000870 InFlag = Chain.getValue(1);
871
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000872 // Create a stack location to hold GP when PIC is used. This stack
873 // location is used on function prologue to save GP and also after all
874 // emited CALL's to restore GP.
875 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000876 // Function can have an arbitrary number of calls, so
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000877 // hold the LastArgStackLoc with the biggest offset.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000878 int FI;
879 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000880 if (LastArgStackLoc >= MipsFI->getGPStackOffset()) {
881 LastArgStackLoc = (!LastArgStackLoc) ? (16) : (LastArgStackLoc+4);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000882 // Create the frame index only once. SPOffset here can be anything
883 // (this will be fixed on processFunctionBeforeFrameFinalized)
884 if (MipsFI->getGPStackOffset() == -1) {
885 FI = MFI->CreateFixedObject(4, 0);
886 MipsFI->setGPFI(FI);
887 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000888 MipsFI->setGPStackOffset(LastArgStackLoc);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000889 }
890
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000891 // Reload GP value.
892 FI = MipsFI->getGPFI();
Dan Gohman475871a2008-07-27 21:46:04 +0000893 SDValue FIN = DAG.getFrameIndex(FI,getPointerTy());
Dale Johannesen33c960f2009-02-04 20:06:27 +0000894 SDValue GPLoad = DAG.getLoad(MVT::i32, dl, Chain, FIN, NULL, 0);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000895 Chain = GPLoad.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000896 Chain = DAG.getCopyToReg(Chain, dl, DAG.getRegister(Mips::GP, MVT::i32),
Dan Gohman475871a2008-07-27 21:46:04 +0000897 GPLoad, SDValue(0,0));
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000898 InFlag = Chain.getValue(1);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000899 }
900
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000901 // Handle result values, copying them out of physregs into vregs that we
902 // return.
Dan Gohman095cc292008-09-13 01:54:27 +0000903 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG), Op.getResNo());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000904}
905
906/// LowerCallResult - Lower the result values of an ISD::CALL into the
907/// appropriate copies out of appropriate physical registers. This assumes that
908/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
909/// being lowered. Returns a SDNode with the same number of values as the
910/// ISD::CALL.
911SDNode *MipsTargetLowering::
Dan Gohman095cc292008-09-13 01:54:27 +0000912LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000913 unsigned CallingConv, SelectionDAG &DAG) {
914
Dan Gohman095cc292008-09-13 01:54:27 +0000915 bool isVarArg = TheCall->isVarArg();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000916 DebugLoc dl = TheCall->getDebugLoc();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000917
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000918 // Assign locations to each value returned by this call.
919 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +0000920 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(),
921 RVLocs, DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000922
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000923 CCInfo.AnalyzeCallResult(TheCall, RetCC_Mips);
Dan Gohman475871a2008-07-27 21:46:04 +0000924 SmallVector<SDValue, 8> ResultVals;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000925
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000926 // Copy all of the result registers out of their specified physreg.
927 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000928 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000929 RVLocs[i].getValVT(), InFlag).getValue(1);
930 InFlag = Chain.getValue(2);
931 ResultVals.push_back(Chain.getValue(0));
932 }
933
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000934 ResultVals.push_back(Chain);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000935
936 // Merge everything together with a MERGE_VALUES node.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000937 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +0000938 &ResultVals[0], ResultVals.size()).getNode();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000939}
940
941//===----------------------------------------------------------------------===//
942// FORMAL_ARGUMENTS Calling Convention Implementation
943//===----------------------------------------------------------------------===//
944
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000945/// LowerFORMAL_ARGUMENTS - transform physical registers into
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000946/// virtual registers and generate load operations for
947/// arguments places on the stack.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000948/// TODO: isVarArg
Dan Gohman475871a2008-07-27 21:46:04 +0000949SDValue MipsTargetLowering::
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000950LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000951{
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000952 SDValue Root = Op.getOperand(0);
953 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000954 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000955 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000956 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000957
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000958 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000959 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000960
961 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000962
963 // Assign locations to all of the incoming arguments.
964 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +0000965 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000966
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000967 if (Subtarget->isABI_O32())
968 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_MipsO32);
969 else
970 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_Mips);
971
Dan Gohman475871a2008-07-27 21:46:04 +0000972 SmallVector<SDValue, 16> ArgValues;
973 SDValue StackPtr;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000974
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000975 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
976
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000977 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000978 CCValAssign &VA = ArgLocs[i];
979
980 // Arguments stored on registers
981 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000982 MVT RegVT = VA.getLocVT();
Bill Wendling06b8c192008-07-09 05:55:53 +0000983 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000984
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000985 if (RegVT == MVT::i32)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000986 RC = Mips::CPURegsRegisterClass;
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000987 else if (RegVT == MVT::f32)
988 RC = Mips::FGR32RegisterClass;
989 else if (RegVT == MVT::f64) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000990 if (!Subtarget->isSingleFloat())
991 RC = Mips::AFGR64RegisterClass;
992 } else
Torok Edwinc23197a2009-07-14 16:55:14 +0000993 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000994
995 // Transform the arguments stored on
996 // physical registers into virtual ones
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000997 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000998 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000999
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001000 // If this is an 8 or 16-bit value, it has been passed promoted
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001001 // to 32 bits. Insert an assert[sz]ext to capture this, then
1002 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001003 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00001004 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001005 if (VA.getLocInfo() == CCValAssign::SExt)
1006 Opcode = ISD::AssertSext;
1007 else if (VA.getLocInfo() == CCValAssign::ZExt)
1008 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00001009 if (Opcode)
1010 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
1011 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00001012 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001013 }
1014
1015 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
1016 if (Subtarget->isABI_O32()) {
1017 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
1018 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, ArgValue);
1019 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
1020 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
1021 VA.getLocReg()+1, RC);
1022 SDValue ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg2, RegVT);
1023 SDValue Hi = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, ArgValue);
1024 SDValue Lo = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, ArgValue2);
1025 ArgValue = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::f64, Lo, Hi);
1026 }
1027 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001028
1029 ArgValues.push_back(ArgValue);
1030
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001031 // To meet ABI, when VARARGS are passed on registers, the registers
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001032 // must have their values written to the caller stack frame.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001033 if ((isVarArg) && (Subtarget->isABI_O32())) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001034 if (StackPtr.getNode() == 0)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001035 StackPtr = DAG.getRegister(StackReg, getPointerTy());
1036
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001037 // The stack pointer offset is relative to the caller stack frame.
1038 // Since the real stack size is unknown here, a negative SPOffset
1039 // is used so there's a way to adjust these offsets when the stack
1040 // size get known (on EliminateFrameIndex). A dummy SPOffset is
1041 // used instead of a direct negative address (which is recorded to
1042 // be used on emitPrologue) to avoid mis-calc of the first stack
1043 // offset on PEI::calculateFrameObjectOffsets.
1044 // Arguments are always 32-bit.
1045 int FI = MFI->CreateFixedObject(4, 0);
1046 MipsFI->recordStoreVarArgsFI(FI, -(4+(i*4)));
Dan Gohman475871a2008-07-27 21:46:04 +00001047 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001048
1049 // emit ISD::STORE whichs stores the
1050 // parameter value to a stack Location
Dale Johannesen33c960f2009-02-04 20:06:27 +00001051 ArgValues.push_back(DAG.getStore(Root, dl, ArgValue, PtrOff, NULL, 0));
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001052 }
1053
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001054 } else { // VA.isRegLoc()
1055
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001056 // sanity check
1057 assert(VA.isMemLoc());
1058
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001059 // The stack pointer offset is relative to the caller stack frame.
1060 // Since the real stack size is unknown here, a negative SPOffset
1061 // is used so there's a way to adjust these offsets when the stack
1062 // size get known (on EliminateFrameIndex). A dummy SPOffset is
1063 // used instead of a direct negative address (which is recorded to
1064 // be used on emitPrologue) to avoid mis-calc of the first stack
1065 // offset on PEI::calculateFrameObjectOffsets.
1066 // Arguments are always 32-bit.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001067 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1068 int FI = MFI->CreateFixedObject(ArgSize, 0);
1069 MipsFI->recordLoadArgsFI(FI, -(ArgSize+
1070 (FirstStackArgLoc + VA.getLocMemOffset())));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001071
1072 // Create load nodes to retrieve arguments from the stack
Dan Gohman475871a2008-07-27 21:46:04 +00001073 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dale Johannesen33c960f2009-02-04 20:06:27 +00001074 ArgValues.push_back(DAG.getLoad(VA.getValVT(), dl, Root, FIN, NULL, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001075 }
1076 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001077
1078 // The mips ABIs for returning structs by value requires that we copy
1079 // the sret argument into $v0 for the return. Save the argument into
1080 // a virtual register so that we can access it from the return points.
1081 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1082 unsigned Reg = MipsFI->getSRetReturnReg();
1083 if (!Reg) {
1084 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
1085 MipsFI->setSRetReturnReg(Reg);
1086 }
Dale Johannesen33c960f2009-02-04 20:06:27 +00001087 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, ArgValues[0]);
1088 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Root);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001089 }
1090
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001091 ArgValues.push_back(Root);
1092
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001093 // Return the new list of results.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001094 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00001095 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001096}
1097
1098//===----------------------------------------------------------------------===//
1099// Return Value Calling Convention Implementation
1100//===----------------------------------------------------------------------===//
1101
Dan Gohman475871a2008-07-27 21:46:04 +00001102SDValue MipsTargetLowering::
1103LowerRET(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001104{
1105 // CCValAssign - represent the assignment of
1106 // the return value to a location
1107 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001108 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
1109 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Dale Johannesena05dca42009-02-04 23:02:30 +00001110 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001111
1112 // CCState - Info about the registers and stack slot.
Owen Andersond1474d02009-07-09 17:57:24 +00001113 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001114
1115 // Analize return values of ISD::RET
Gabor Greifba36cb52008-08-28 21:40:38 +00001116 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001117
1118 // If this is the first return lowered for this function, add
1119 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001120 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001121 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001122 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +00001123 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001124 }
1125
1126 // The chain is always operand #0
Dan Gohman475871a2008-07-27 21:46:04 +00001127 SDValue Chain = Op.getOperand(0);
1128 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001129
1130 // Copy the result values into the output registers.
1131 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1132 CCValAssign &VA = RVLocs[i];
1133 assert(VA.isRegLoc() && "Can only return in registers!");
1134
1135 // ISD::RET => ret chain, (regnum1,val1), ...
1136 // So i*2+1 index only the regnums
Dale Johannesena05dca42009-02-04 23:02:30 +00001137 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1138 Op.getOperand(i*2+1), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001139
1140 // guarantee that all emitted copies are
1141 // stuck together, avoiding something bad
1142 Flag = Chain.getValue(1);
1143 }
1144
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001145 // The mips ABIs for returning structs by value requires that we copy
1146 // the sret argument into $v0 for the return. We saved the argument into
1147 // a virtual register in the entry block, so now we copy the value out
1148 // and into $v0.
1149 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1150 MachineFunction &MF = DAG.getMachineFunction();
1151 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1152 unsigned Reg = MipsFI->getSRetReturnReg();
1153
1154 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00001155 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00001156 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001157
Dale Johannesena05dca42009-02-04 23:02:30 +00001158 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001159 Flag = Chain.getValue(1);
1160 }
1161
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001162 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00001163 if (Flag.getNode())
Dale Johannesena05dca42009-02-04 23:02:30 +00001164 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +00001165 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001166 else // Return Void
Dale Johannesena05dca42009-02-04 23:02:30 +00001167 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +00001168 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001169}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001170
1171//===----------------------------------------------------------------------===//
1172// Mips Inline Assembly Support
1173//===----------------------------------------------------------------------===//
1174
1175/// getConstraintType - Given a constraint letter, return the type of
1176/// constraint it is for this target.
1177MipsTargetLowering::ConstraintType MipsTargetLowering::
1178getConstraintType(const std::string &Constraint) const
1179{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001180 // Mips specific constrainy
1181 // GCC config/mips/constraints.md
1182 //
1183 // 'd' : An address register. Equivalent to r
1184 // unless generating MIPS16 code.
1185 // 'y' : Equivalent to r; retained for
1186 // backwards compatibility.
Bruno Cardoso Lopes7b76da12008-07-09 04:45:36 +00001187 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001188 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001189 switch (Constraint[0]) {
1190 default : break;
1191 case 'd':
1192 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001193 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001194 return C_RegisterClass;
1195 break;
1196 }
1197 }
1198 return TargetLowering::getConstraintType(Constraint);
1199}
1200
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001201/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
1202/// return a list of registers that can be used to satisfy the constraint.
1203/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001204std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00001205getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001206{
1207 if (Constraint.size() == 1) {
1208 switch (Constraint[0]) {
1209 case 'r':
1210 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001211 case 'f':
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001212 if (VT == MVT::f32)
1213 return std::make_pair(0U, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001214 if (VT == MVT::f64)
1215 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1216 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001217 }
1218 }
1219 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1220}
1221
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001222/// Given a register class constraint, like 'r', if this corresponds directly
1223/// to an LLVM register class, return a register of 0 and the register class
1224/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001225std::vector<unsigned> MipsTargetLowering::
1226getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001227 MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001228{
1229 if (Constraint.size() != 1)
1230 return std::vector<unsigned>();
1231
1232 switch (Constraint[0]) {
1233 default : break;
1234 case 'r':
1235 // GCC Mips Constraint Letters
1236 case 'd':
1237 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001238 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
1239 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
1240 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
1241 Mips::T8, 0);
1242
1243 case 'f':
Duncan Sands15126422008-07-08 09:33:14 +00001244 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001245 if (Subtarget->isSingleFloat())
1246 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
1247 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
1248 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
1249 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
1250 Mips::F30, Mips::F31, 0);
1251 else
1252 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
1253 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
1254 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +00001255 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001256
1257 if (VT == MVT::f64)
1258 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1259 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
1260 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
1261 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001262 }
1263 return std::vector<unsigned>();
1264}
Dan Gohman6520e202008-10-18 02:06:02 +00001265
1266bool
1267MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1268 // The Mips target isn't yet aware of offsets.
1269 return false;
1270}