blob: 0b10c9aee247b4ffd1c43edd74d5d2e41eff3868 [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
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000086 // Used by legalize types to correctly generate the setcc result.
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +000087 // Without this, every float setcc comes with a AND/OR with the result,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000088 // we don't want this, since the fpcmp result goes to a flag register,
89 // which is used implicitly by brcond and select operations.
90 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
91
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000092 // Mips Custom Operations
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +000093 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
94 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
95 setOperationAction(ISD::RET, MVT::Other, Custom);
96 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
97 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
98 setOperationAction(ISD::SELECT, MVT::f32, Custom);
Eli Friedman6314ac22009-06-16 06:40:59 +000099 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000100 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000101 setOperationAction(ISD::SETCC, MVT::f32, Custom);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000102 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000103 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
104 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000105 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000106
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +0000107 // We custom lower AND/OR to handle the case where the DAG contain 'ands/ors'
108 // with operands comming from setcc fp comparions. This is necessary since
109 // the result from these setcc are in a flag registers (FCR31).
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000110 setOperationAction(ISD::AND, MVT::i32, Custom);
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +0000111 setOperationAction(ISD::OR, MVT::i32, Custom);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000112
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000113 // Operations not directly supported by Mips.
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000114 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
115 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
116 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000117 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
118 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000120 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
121 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000122 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000123 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
124 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
125 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Bruno Cardoso Lopes7bd71822008-07-31 18:50:54 +0000126 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Eli Friedman6314ac22009-06-16 06:40:59 +0000127 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000128
129 // We don't have line number support yet.
130 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
131 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
132 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
133 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
134
135 // Use the default for now
136 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
137 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
138 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000139
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000140 if (Subtarget->isSingleFloat())
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000141 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000142
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000143 if (!Subtarget->hasSEInReg()) {
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000144 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000145 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
146 }
147
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000148 if (!Subtarget->hasBitCount())
149 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
150
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000151 if (!Subtarget->hasSwap())
152 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
153
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000154 setStackPointerRegisterToSaveRestore(Mips::SP);
155 computeRegisterProperties();
156}
157
Duncan Sands5480c042009-01-01 15:52:00 +0000158MVT MipsTargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000159 return MVT::i32;
160}
161
Bill Wendlingb4202b82009-07-01 18:50:55 +0000162/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000163unsigned MipsTargetLowering::getFunctionAlignment(const Function *) const {
164 return 2;
165}
Scott Michel5b8f82e2008-03-10 15:42:14 +0000166
Dan Gohman475871a2008-07-27 21:46:04 +0000167SDValue MipsTargetLowering::
168LowerOperation(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000169{
170 switch (Op.getOpcode())
171 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000172 case ISD::AND: return LowerANDOR(Op, DAG);
173 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
174 case ISD::CALL: return LowerCALL(Op, DAG);
175 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
176 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
177 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000178 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000179 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
180 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
181 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
182 case ISD::OR: return LowerANDOR(Op, DAG);
183 case ISD::RET: return LowerRET(Op, DAG);
184 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 Lopes972f5892007-06-06 07:42:06 +0000186 }
Dan Gohman475871a2008-07-27 21:46:04 +0000187 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000188}
189
190//===----------------------------------------------------------------------===//
191// Lower helper functions
192//===----------------------------------------------------------------------===//
193
194// AddLiveIn - This helper function adds the specified physical register to the
195// MachineFunction as a live in value. It also creates a corresponding
196// virtual register for it.
197static unsigned
198AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
199{
200 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000201 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
202 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000203 return VReg;
204}
205
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000206// A address must be loaded from a small section if its size is less than the
207// small section size threshold. Data in this section must be addressed using
208// gp_rel operator.
209bool MipsTargetLowering::IsInSmallSection(unsigned Size) {
210 return (Size > 0 && (Size <= Subtarget->getSSectionThreshold()));
211}
212
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000213// Discover if this global address can be placed into small data/bss section.
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000214bool MipsTargetLowering::IsGlobalInSmallSection(GlobalValue *GV)
215{
216 const TargetData *TD = getTargetData();
Bruno Cardoso Lopesfeb95cc2008-07-22 15:34:27 +0000217 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
218
219 if (!GVA)
220 return false;
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000221
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000222 const Type *Ty = GV->getType()->getElementType();
Duncan Sands777d2302009-05-09 07:06:46 +0000223 unsigned Size = TD->getTypeAllocSize(Ty);
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000224
225 // if this is a internal constant string, there is a special
226 // section for it, but not in small data/bss.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000227 if (GVA->hasInitializer() && GV->hasLocalLinkage()) {
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000228 Constant *C = GVA->getInitializer();
229 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
Owen Anderson1ca29d32009-07-13 21:27:19 +0000230 if (CVA && CVA->isCString())
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000231 return false;
232 }
233
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000234 return IsInSmallSection(Size);
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000235}
236
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000237// Get fp branch code (not opcode) from condition code.
238static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
239 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
240 return Mips::BRANCH_T;
241
242 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
243 return Mips::BRANCH_F;
244
245 return Mips::BRANCH_INVALID;
246}
247
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000248static unsigned FPBranchCodeToOpc(Mips::FPBranchCode BC) {
249 switch(BC) {
250 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000251 llvm_unreachable("Unknown branch code");
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000252 case Mips::BRANCH_T : return Mips::BC1T;
253 case Mips::BRANCH_F : return Mips::BC1F;
254 case Mips::BRANCH_TL : return Mips::BC1TL;
255 case Mips::BRANCH_FL : return Mips::BC1FL;
256 }
257}
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000258
259static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
260 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000261 default: llvm_unreachable("Unknown fp condition code!");
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000262 case ISD::SETEQ:
263 case ISD::SETOEQ: return Mips::FCOND_EQ;
264 case ISD::SETUNE: return Mips::FCOND_OGL;
265 case ISD::SETLT:
266 case ISD::SETOLT: return Mips::FCOND_OLT;
267 case ISD::SETGT:
268 case ISD::SETOGT: return Mips::FCOND_OGT;
269 case ISD::SETLE:
270 case ISD::SETOLE: return Mips::FCOND_OLE;
271 case ISD::SETGE:
272 case ISD::SETOGE: return Mips::FCOND_OGE;
273 case ISD::SETULT: return Mips::FCOND_ULT;
274 case ISD::SETULE: return Mips::FCOND_ULE;
275 case ISD::SETUGT: return Mips::FCOND_UGT;
276 case ISD::SETUGE: return Mips::FCOND_UGE;
277 case ISD::SETUO: return Mips::FCOND_UN;
278 case ISD::SETO: return Mips::FCOND_OR;
279 case ISD::SETNE:
280 case ISD::SETONE: return Mips::FCOND_NEQ;
281 case ISD::SETUEQ: return Mips::FCOND_UEQ;
282 }
283}
284
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000285MachineBasicBlock *
286MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +0000287 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000288 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
289 bool isFPCmp = false;
Dale Johannesen94817572009-02-13 02:34:39 +0000290 DebugLoc dl = MI->getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000291
292 switch (MI->getOpcode()) {
293 default: assert(false && "Unexpected instr type to insert");
294 case Mips::Select_FCC:
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000295 case Mips::Select_FCC_S32:
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000296 case Mips::Select_FCC_D32:
297 isFPCmp = true; // FALL THROUGH
298 case Mips::Select_CC:
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000299 case Mips::Select_CC_S32:
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000300 case Mips::Select_CC_D32: {
301 // To "insert" a SELECT_CC instruction, we actually have to insert the
302 // diamond control-flow pattern. The incoming instruction knows the
303 // destination vreg to set, the condition code register to branch on, the
304 // true/false values to select between, and a branch opcode to use.
305 const BasicBlock *LLVM_BB = BB->getBasicBlock();
306 MachineFunction::iterator It = BB;
307 ++It;
308
309 // thisMBB:
310 // ...
311 // TrueVal = ...
312 // setcc r1, r2, r3
313 // bNE r1, r0, copy1MBB
314 // fallthrough --> copy0MBB
315 MachineBasicBlock *thisMBB = BB;
316 MachineFunction *F = BB->getParent();
317 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
318 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
319
320 // Emit the right instruction according to the type of the operands compared
321 if (isFPCmp) {
322 // Find the condiction code present in the setcc operation.
323 Mips::CondCode CC = (Mips::CondCode)MI->getOperand(4).getImm();
324 // Get the branch opcode from the branch code.
325 unsigned Opc = FPBranchCodeToOpc(GetFPBranchCodeFromCond(CC));
Dale Johannesen94817572009-02-13 02:34:39 +0000326 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000327 } else
Dale Johannesen94817572009-02-13 02:34:39 +0000328 BuildMI(BB, dl, TII->get(Mips::BNE)).addReg(MI->getOperand(1).getReg())
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000329 .addReg(Mips::ZERO).addMBB(sinkMBB);
330
331 F->insert(It, copy0MBB);
332 F->insert(It, sinkMBB);
333 // Update machine-CFG edges by first adding all successors of the current
334 // block to the new block which will contain the Phi node for the select.
335 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
336 e = BB->succ_end(); i != e; ++i)
337 sinkMBB->addSuccessor(*i);
338 // Next, remove all successors of the current block, and add the true
339 // and fallthrough blocks as its successors.
340 while(!BB->succ_empty())
341 BB->removeSuccessor(BB->succ_begin());
342 BB->addSuccessor(copy0MBB);
343 BB->addSuccessor(sinkMBB);
344
345 // copy0MBB:
346 // %FalseValue = ...
347 // # fallthrough to sinkMBB
348 BB = copy0MBB;
349
350 // Update machine-CFG edges
351 BB->addSuccessor(sinkMBB);
352
353 // sinkMBB:
354 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
355 // ...
356 BB = sinkMBB;
Dale Johannesen94817572009-02-13 02:34:39 +0000357 BuildMI(BB, dl, TII->get(Mips::PHI), MI->getOperand(0).getReg())
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000358 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
359 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB);
360
361 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
362 return BB;
363 }
364 }
365}
366
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000367//===----------------------------------------------------------------------===//
368// Misc Lower Operation implementation
369//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000370
Dan Gohman475871a2008-07-27 21:46:04 +0000371SDValue MipsTargetLowering::
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000372LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG)
373{
374 if (!Subtarget->isMips1())
375 return Op;
376
377 MachineFunction &MF = DAG.getMachineFunction();
378 unsigned CCReg = AddLiveIn(MF, Mips::FCR31, Mips::CCRRegisterClass);
379
380 SDValue Chain = DAG.getEntryNode();
381 DebugLoc dl = Op.getDebugLoc();
382 SDValue Src = Op.getOperand(0);
383
384 // Set the condition register
385 SDValue CondReg = DAG.getCopyFromReg(Chain, dl, CCReg, MVT::i32);
386 CondReg = DAG.getCopyToReg(Chain, dl, Mips::AT, CondReg);
387 CondReg = DAG.getCopyFromReg(CondReg, dl, Mips::AT, MVT::i32);
388
389 SDValue Cst = DAG.getConstant(3, MVT::i32);
390 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, CondReg, Cst);
391 Cst = DAG.getConstant(2, MVT::i32);
392 SDValue Xor = DAG.getNode(ISD::XOR, dl, MVT::i32, Or, Cst);
393
394 SDValue InFlag(0, 0);
395 CondReg = DAG.getCopyToReg(Chain, dl, Mips::FCR31, Xor, InFlag);
396
397 // Emit the round instruction and bit convert to integer
398 SDValue Trunc = DAG.getNode(MipsISD::FPRound, dl, MVT::f32,
399 Src, CondReg.getValue(1));
400 SDValue BitCvt = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Trunc);
401 return BitCvt;
402}
403
404SDValue MipsTargetLowering::
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000405LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG)
406{
407 SDValue Chain = Op.getOperand(0);
408 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +0000409 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000410
411 // Get a reference from Mips stack pointer
Dale Johannesena05dca42009-02-04 23:02:30 +0000412 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000413
414 // Subtract the dynamic size from the actual stack size to
415 // obtain the new stack size.
Dale Johannesena05dca42009-02-04 23:02:30 +0000416 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000417
418 // The Sub result contains the new stack start address, so it
419 // must be placed in the stack pointer register.
Dale Johannesena05dca42009-02-04 23:02:30 +0000420 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000421
422 // This node always has two return values: a new stack pointer
423 // value and a chain
424 SDValue Ops[2] = { Sub, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000425 return DAG.getMergeValues(Ops, 2, dl);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000426}
427
428SDValue MipsTargetLowering::
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +0000429LowerANDOR(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000430{
431 SDValue LHS = Op.getOperand(0);
432 SDValue RHS = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +0000433 DebugLoc dl = Op.getDebugLoc();
434
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000435 if (LHS.getOpcode() != MipsISD::FPCmp || RHS.getOpcode() != MipsISD::FPCmp)
436 return Op;
437
438 SDValue True = DAG.getConstant(1, MVT::i32);
439 SDValue False = DAG.getConstant(0, MVT::i32);
440
Dale Johannesende064702009-02-06 21:50:26 +0000441 SDValue LSEL = DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000442 LHS, True, False, LHS.getOperand(2));
Dale Johannesende064702009-02-06 21:50:26 +0000443 SDValue RSEL = DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000444 RHS, True, False, RHS.getOperand(2));
445
Dale Johannesende064702009-02-06 21:50:26 +0000446 return DAG.getNode(Op.getOpcode(), dl, MVT::i32, LSEL, RSEL);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000447}
448
449SDValue MipsTargetLowering::
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000450LowerBRCOND(SDValue Op, SelectionDAG &DAG)
451{
452 // The first operand is the chain, the second is the condition, the third is
453 // the block to branch to if the condition is true.
454 SDValue Chain = Op.getOperand(0);
455 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000456 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000457
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000458 if (Op.getOperand(1).getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000459 return Op;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000460
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000461 SDValue CondRes = Op.getOperand(1);
462 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000463 Mips::CondCode CC =
464 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000465 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
466
Dale Johannesende064702009-02-06 21:50:26 +0000467 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000468 Dest, CondRes);
469}
470
471SDValue MipsTargetLowering::
472LowerSETCC(SDValue Op, SelectionDAG &DAG)
473{
474 // The operands to this are the left and right operands to compare (ops #0,
475 // and #1) and the condition code to compare them with (op #2) as a
476 // CondCodeSDNode.
477 SDValue LHS = Op.getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +0000478 SDValue RHS = Op.getOperand(1);
479 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000480
481 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
482
Dale Johannesende064702009-02-06 21:50:26 +0000483 return DAG.getNode(MipsISD::FPCmp, dl, Op.getValueType(), LHS, RHS,
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000484 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
485}
486
487SDValue MipsTargetLowering::
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000488LowerSELECT(SDValue Op, SelectionDAG &DAG)
489{
490 SDValue Cond = Op.getOperand(0);
491 SDValue True = Op.getOperand(1);
492 SDValue False = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000493 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000494
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000495 // if the incomming condition comes from a integer compare, the select
496 // operation must be SelectCC or a conditional move if the subtarget
497 // supports it.
498 if (Cond.getOpcode() != MipsISD::FPCmp) {
499 if (Subtarget->hasCondMov() && !True.getValueType().isFloatingPoint())
500 return Op;
Dale Johannesende064702009-02-06 21:50:26 +0000501 return DAG.getNode(MipsISD::SelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000502 Cond, True, False);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000503 }
504
505 // if the incomming condition comes from fpcmp, the select
506 // operation must use FPSelectCC.
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000507 SDValue CCNode = Cond.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000508 return DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000509 Cond, True, False, CCNode);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000510}
511
512SDValue MipsTargetLowering::
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000513LowerGlobalAddress(SDValue Op, SelectionDAG &DAG)
514{
Dale Johannesende064702009-02-06 21:50:26 +0000515 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000516 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000517 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
518 SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
519
520 if (!Subtarget->hasABICall()) {
Dan Gohmanfc166572009-04-09 23:54:40 +0000521 SDVTList VTs = DAG.getVTList(MVT::i32);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000522 SDValue Ops[] = { GA };
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000523 // %gp_rel relocation
524 if (!isa<Function>(GV) && IsGlobalInSmallSection(GV)) {
Dan Gohmanfc166572009-04-09 23:54:40 +0000525 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, Ops, 1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000526 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000527 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000528 }
529 // %hi/%lo relocation
Dan Gohmanfc166572009-04-09 23:54:40 +0000530 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, Ops, 1);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000531 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GA);
532 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000533
534 } else { // Abicall relocations, TODO: make this cleaner.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000535 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
536 DAG.getEntryNode(), GA, NULL, 0);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000537 // On functions and global targets not internal linked only
538 // a load from got/GP is necessary for PIC to work.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000539 if (!GV->hasLocalLinkage() || isa<Function>(GV))
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000540 return ResNode;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000541 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GA);
542 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000543 }
544
Torok Edwinc23197a2009-07-14 16:55:14 +0000545 llvm_unreachable("Dont know how to handle GlobalAddress");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000546 return SDValue(0,0);
547}
548
549SDValue MipsTargetLowering::
550LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG)
551{
Torok Edwinc23197a2009-07-14 16:55:14 +0000552 llvm_unreachable("TLS not implemented for MIPS.");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000553 return SDValue(); // Not reached
554}
555
556SDValue MipsTargetLowering::
Dan Gohman475871a2008-07-27 21:46:04 +0000557LowerJumpTable(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000558{
Dan Gohman475871a2008-07-27 21:46:04 +0000559 SDValue ResNode;
560 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +0000561 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000562 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000563
Duncan Sands83ec4b62008-06-06 12:08:01 +0000564 MVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000565 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +0000566 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000567
568 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohmanfc166572009-04-09 23:54:40 +0000569 SDVTList VTs = DAG.getVTList(MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000570 SDValue Ops[] = { JTI };
Dan Gohmanfc166572009-04-09 23:54:40 +0000571 HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, Ops, 1);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000572 } else // Emit Load from Global Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +0000573 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI, NULL, 0);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000574
Dale Johannesen33c960f2009-02-04 20:06:27 +0000575 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTI);
576 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000577
578 return ResNode;
579}
580
Dan Gohman475871a2008-07-27 21:46:04 +0000581SDValue MipsTargetLowering::
582LowerConstantPool(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000583{
Dan Gohman475871a2008-07-27 21:46:04 +0000584 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000585 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
586 Constant *C = N->getConstVal();
Dan Gohman475871a2008-07-27 21:46:04 +0000587 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
Dale Johannesende064702009-02-06 21:50:26 +0000588 // FIXME there isn't actually debug info here
589 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000590
591 // gp_rel relocation
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000592 // FIXME: we should reference the constant pool using small data sections,
593 // but the asm printer currently doens't support this feature without
594 // hacking it. This feature should come soon so we can uncomment the
595 // stuff below.
596 //if (!Subtarget->hasABICall() &&
Duncan Sands777d2302009-05-09 07:06:46 +0000597 // IsInSmallSection(getTargetData()->getTypeAllocSize(C->getType()))) {
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000598 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000599 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000600 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
601 //} else { // %hi/%lo relocation
Dale Johannesende064702009-02-06 21:50:26 +0000602 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CP);
603 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CP);
604 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000605 //}
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000606
607 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000608}
609
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000610//===----------------------------------------------------------------------===//
611// Calling Convention Implementation
612//
613// The lower operations present on calling convention works on this order:
614// LowerCALL (virt regs --> phys regs, virt regs --> stack)
615// LowerFORMAL_ARGUMENTS (phys --> virt regs, stack --> virt regs)
616// LowerRET (virt regs --> phys regs)
617// LowerCALL (phys regs --> virt regs)
618//
619//===----------------------------------------------------------------------===//
620
621#include "MipsGenCallingConv.inc"
622
623//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000624// TODO: Implement a generic logic using tblgen that can support this.
625// Mips O32 ABI rules:
626// ---
627// i32 - Passed in A0, A1, A2, A3 and stack
628// f32 - Only passed in f32 registers if no int reg has been used yet to hold
629// an argument. Otherwise, passed in A1, A2, A3 and stack.
630// f64 - Only passed in two aliased f32 registers if no int reg has been used
631// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
632// not used, it must be shadowed. If only A3 is avaiable, shadow it and
633// go to stack.
634//===----------------------------------------------------------------------===//
635
636static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
637 MVT LocVT, CCValAssign::LocInfo LocInfo,
638 ISD::ArgFlagsTy ArgFlags, CCState &State) {
639
640 static const unsigned IntRegsSize=4, FloatRegsSize=2;
641
642 static const unsigned IntRegs[] = {
643 Mips::A0, Mips::A1, Mips::A2, Mips::A3
644 };
645 static const unsigned F32Regs[] = {
646 Mips::F12, Mips::F14
647 };
648 static const unsigned F64Regs[] = {
649 Mips::D6, Mips::D7
650 };
651
652 unsigned Reg=0;
653 unsigned UnallocIntReg = State.getFirstUnallocated(IntRegs, IntRegsSize);
654 bool IntRegUsed = (IntRegs[UnallocIntReg] != (unsigned (Mips::A0)));
655
656 // Promote i8 and i16
657 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
658 LocVT = MVT::i32;
659 if (ArgFlags.isSExt())
660 LocInfo = CCValAssign::SExt;
661 else if (ArgFlags.isZExt())
662 LocInfo = CCValAssign::ZExt;
663 else
664 LocInfo = CCValAssign::AExt;
665 }
666
667 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && IntRegUsed)) {
668 Reg = State.AllocateReg(IntRegs, IntRegsSize);
669 IntRegUsed = true;
670 LocVT = MVT::i32;
671 }
672
673 if (ValVT.isFloatingPoint() && !IntRegUsed) {
674 if (ValVT == MVT::f32)
675 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
676 else
677 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
678 }
679
680 if (ValVT == MVT::f64 && IntRegUsed) {
681 if (UnallocIntReg != IntRegsSize) {
682 // If we hit register A3 as the first not allocated, we must
683 // mark it as allocated (shadow) and use the stack instead.
684 if (IntRegs[UnallocIntReg] != (unsigned (Mips::A3)))
685 Reg = Mips::A2;
686 for (;UnallocIntReg < IntRegsSize; ++UnallocIntReg)
687 State.AllocateReg(UnallocIntReg);
688 }
689 LocVT = MVT::i32;
690 }
691
692 if (!Reg) {
693 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
694 unsigned Offset = State.AllocateStack(SizeInBytes, SizeInBytes);
695 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
696 } else
697 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
698
699 return false; // CC must always match
700}
701
702//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000703// CALL Calling Convention Implementation
704//===----------------------------------------------------------------------===//
705
Nate Begeman5bf4b752009-01-26 03:15:54 +0000706/// LowerCALL - functions arguments are copied from virtual regs to
707/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000708/// TODO: isVarArg, isTailCall.
Dan Gohman475871a2008-07-27 21:46:04 +0000709SDValue MipsTargetLowering::
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000710LowerCALL(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000711{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000712 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000713
Dan Gohman095cc292008-09-13 01:54:27 +0000714 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
715 SDValue Chain = TheCall->getChain();
716 SDValue Callee = TheCall->getCallee();
717 bool isVarArg = TheCall->isVarArg();
718 unsigned CC = TheCall->getCallingConv();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000719 DebugLoc dl = TheCall->getDebugLoc();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000720
721 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000722
723 // Analyze operands of the call, assigning locations to each operand.
724 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +0000725 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000726
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000727 // To meet O32 ABI, Mips must always allocate 16 bytes on
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000728 // the stack (even if less than 4 are used as arguments)
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000729 if (Subtarget->isABI_O32()) {
730 int VTsize = MVT(MVT::i32).getSizeInBits()/8;
731 MFI->CreateFixedObject(VTsize, (VTsize*3));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000732 CCInfo.AnalyzeCallOperands(TheCall, CC_MipsO32);
733 } else
734 CCInfo.AnalyzeCallOperands(TheCall, CC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000735
736 // Get a count of how many bytes are to be pushed on the stack.
737 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnere563bbc2008-10-11 22:08:30 +0000738 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000739
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000740 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +0000741 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
742 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000743
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000744 // First/LastArgStackLoc contains the first/last
745 // "at stack" argument location.
746 int LastArgStackLoc = 0;
747 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000748
749 // Walk the register/memloc assignments, inserting copies/loads.
750 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000751 SDValue Arg = TheCall->getArg(i);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000752 CCValAssign &VA = ArgLocs[i];
753
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000754 // Promote the value if needed.
755 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000756 default: llvm_unreachable("Unknown loc info!");
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000757 case CCValAssign::Full:
758 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
759 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
760 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Arg);
761 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
762 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
763 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
764 DAG.getConstant(0, getPointerTy()));
765 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
766 DAG.getConstant(1, getPointerTy()));
767 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
768 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
769 continue;
770 }
771 }
772 break;
Chris Lattnere0b12152008-03-17 06:57:02 +0000773 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000774 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000775 break;
776 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000777 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000778 break;
779 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000780 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000781 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000782 }
783
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000784 // Arguments that can be passed on register must be kept at
785 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000786 if (VA.isRegLoc()) {
787 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +0000788 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000789 }
Chris Lattnere0b12152008-03-17 06:57:02 +0000790
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000791 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +0000792 assert(VA.isMemLoc());
793
794 // Create the frame index object for this incoming parameter
795 // This guarantees that when allocating Local Area the firsts
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000796 // 16 bytes which are alwayes reserved won't be overwritten
797 // if O32 ABI is used. For EABI the first address is zero.
798 LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000799 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000800 LastArgStackLoc);
Chris Lattnere0b12152008-03-17 06:57:02 +0000801
Dan Gohman475871a2008-07-27 21:46:04 +0000802 SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +0000803
804 // emit ISD::STORE whichs stores the
805 // parameter value to a stack Location
Dale Johannesen33c960f2009-02-04 20:06:27 +0000806 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000807 }
808
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000809 // Transform all store nodes into one single node because all store
810 // nodes are independent of each other.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000811 if (!MemOpChains.empty())
Dale Johannesen33c960f2009-02-04 20:06:27 +0000812 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000813 &MemOpChains[0], MemOpChains.size());
814
815 // Build a sequence of copy-to-reg nodes chained together with token
816 // chain and flag operands which copy the outgoing args into registers.
817 // The InFlag in necessary since all emited instructions must be
818 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000819 SDValue InFlag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000820 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000821 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000822 RegsToPass[i].second, InFlag);
823 InFlag = Chain.getValue(1);
824 }
825
Bill Wendling056292f2008-09-16 21:48:12 +0000826 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
827 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
828 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000829 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000830 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +0000831 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
832 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
833
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000834 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
835 // = Chain, Callee, Reg#1, Reg#2, ...
836 //
837 // Returns a chain & a flag for retval copy to use.
838 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000839 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000840 Ops.push_back(Chain);
841 Ops.push_back(Callee);
842
843 // Add argument registers to the end of the list so that they are
844 // known live into the call.
845 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
846 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
847 RegsToPass[i].second.getValueType()));
848
Gabor Greifba36cb52008-08-28 21:40:38 +0000849 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000850 Ops.push_back(InFlag);
851
Dale Johannesen33c960f2009-02-04 20:06:27 +0000852 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000853 InFlag = Chain.getValue(1);
854
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000855 // Create the CALLSEQ_END node.
Chris Lattnere563bbc2008-10-11 22:08:30 +0000856 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
857 DAG.getIntPtrConstant(0, true), InFlag);
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000858 InFlag = Chain.getValue(1);
859
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000860 // Create a stack location to hold GP when PIC is used. This stack
861 // location is used on function prologue to save GP and also after all
862 // emited CALL's to restore GP.
863 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000864 // Function can have an arbitrary number of calls, so
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000865 // hold the LastArgStackLoc with the biggest offset.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000866 int FI;
867 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000868 if (LastArgStackLoc >= MipsFI->getGPStackOffset()) {
869 LastArgStackLoc = (!LastArgStackLoc) ? (16) : (LastArgStackLoc+4);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000870 // Create the frame index only once. SPOffset here can be anything
871 // (this will be fixed on processFunctionBeforeFrameFinalized)
872 if (MipsFI->getGPStackOffset() == -1) {
873 FI = MFI->CreateFixedObject(4, 0);
874 MipsFI->setGPFI(FI);
875 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000876 MipsFI->setGPStackOffset(LastArgStackLoc);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000877 }
878
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000879 // Reload GP value.
880 FI = MipsFI->getGPFI();
Dan Gohman475871a2008-07-27 21:46:04 +0000881 SDValue FIN = DAG.getFrameIndex(FI,getPointerTy());
Dale Johannesen33c960f2009-02-04 20:06:27 +0000882 SDValue GPLoad = DAG.getLoad(MVT::i32, dl, Chain, FIN, NULL, 0);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000883 Chain = GPLoad.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000884 Chain = DAG.getCopyToReg(Chain, dl, DAG.getRegister(Mips::GP, MVT::i32),
Dan Gohman475871a2008-07-27 21:46:04 +0000885 GPLoad, SDValue(0,0));
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000886 InFlag = Chain.getValue(1);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000887 }
888
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000889 // Handle result values, copying them out of physregs into vregs that we
890 // return.
Dan Gohman095cc292008-09-13 01:54:27 +0000891 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG), Op.getResNo());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000892}
893
894/// LowerCallResult - Lower the result values of an ISD::CALL into the
895/// appropriate copies out of appropriate physical registers. This assumes that
896/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
897/// being lowered. Returns a SDNode with the same number of values as the
898/// ISD::CALL.
899SDNode *MipsTargetLowering::
Dan Gohman095cc292008-09-13 01:54:27 +0000900LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000901 unsigned CallingConv, SelectionDAG &DAG) {
902
Dan Gohman095cc292008-09-13 01:54:27 +0000903 bool isVarArg = TheCall->isVarArg();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000904 DebugLoc dl = TheCall->getDebugLoc();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000905
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000906 // Assign locations to each value returned by this call.
907 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +0000908 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(),
909 RVLocs, DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000910
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000911 CCInfo.AnalyzeCallResult(TheCall, RetCC_Mips);
Dan Gohman475871a2008-07-27 21:46:04 +0000912 SmallVector<SDValue, 8> ResultVals;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000913
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000914 // Copy all of the result registers out of their specified physreg.
915 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000916 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000917 RVLocs[i].getValVT(), InFlag).getValue(1);
918 InFlag = Chain.getValue(2);
919 ResultVals.push_back(Chain.getValue(0));
920 }
921
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000922 ResultVals.push_back(Chain);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000923
924 // Merge everything together with a MERGE_VALUES node.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000925 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +0000926 &ResultVals[0], ResultVals.size()).getNode();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000927}
928
929//===----------------------------------------------------------------------===//
930// FORMAL_ARGUMENTS Calling Convention Implementation
931//===----------------------------------------------------------------------===//
932
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000933/// LowerFORMAL_ARGUMENTS - transform physical registers into
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000934/// virtual registers and generate load operations for
935/// arguments places on the stack.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000936/// TODO: isVarArg
Dan Gohman475871a2008-07-27 21:46:04 +0000937SDValue MipsTargetLowering::
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000938LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000939{
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000940 SDValue Root = Op.getOperand(0);
941 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000942 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000943 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000944 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000945
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000946 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000947 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000948
949 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000950
951 // Assign locations to all of the incoming arguments.
952 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +0000953 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000954
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000955 if (Subtarget->isABI_O32())
956 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_MipsO32);
957 else
958 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_Mips);
959
Dan Gohman475871a2008-07-27 21:46:04 +0000960 SmallVector<SDValue, 16> ArgValues;
961 SDValue StackPtr;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000962
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000963 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
964
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000965 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000966 CCValAssign &VA = ArgLocs[i];
967
968 // Arguments stored on registers
969 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000970 MVT RegVT = VA.getLocVT();
Bill Wendling06b8c192008-07-09 05:55:53 +0000971 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000972
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000973 if (RegVT == MVT::i32)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000974 RC = Mips::CPURegsRegisterClass;
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000975 else if (RegVT == MVT::f32)
976 RC = Mips::FGR32RegisterClass;
977 else if (RegVT == MVT::f64) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000978 if (!Subtarget->isSingleFloat())
979 RC = Mips::AFGR64RegisterClass;
980 } else
Torok Edwinc23197a2009-07-14 16:55:14 +0000981 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000982
983 // Transform the arguments stored on
984 // physical registers into virtual ones
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000985 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000986 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000987
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000988 // If this is an 8 or 16-bit value, it has been passed promoted
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000989 // to 32 bits. Insert an assert[sz]ext to capture this, then
990 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000991 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +0000992 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000993 if (VA.getLocInfo() == CCValAssign::SExt)
994 Opcode = ISD::AssertSext;
995 else if (VA.getLocInfo() == CCValAssign::ZExt)
996 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +0000997 if (Opcode)
998 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
999 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00001000 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001001 }
1002
1003 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
1004 if (Subtarget->isABI_O32()) {
1005 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
1006 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, ArgValue);
1007 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
1008 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
1009 VA.getLocReg()+1, RC);
1010 SDValue ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg2, RegVT);
1011 SDValue Hi = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, ArgValue);
1012 SDValue Lo = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, ArgValue2);
1013 ArgValue = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::f64, Lo, Hi);
1014 }
1015 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001016
1017 ArgValues.push_back(ArgValue);
1018
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001019 // To meet ABI, when VARARGS are passed on registers, the registers
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001020 // must have their values written to the caller stack frame.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001021 if ((isVarArg) && (Subtarget->isABI_O32())) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001022 if (StackPtr.getNode() == 0)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001023 StackPtr = DAG.getRegister(StackReg, getPointerTy());
1024
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001025 // The stack pointer offset is relative to the caller stack frame.
1026 // Since the real stack size is unknown here, a negative SPOffset
1027 // is used so there's a way to adjust these offsets when the stack
1028 // size get known (on EliminateFrameIndex). A dummy SPOffset is
1029 // used instead of a direct negative address (which is recorded to
1030 // be used on emitPrologue) to avoid mis-calc of the first stack
1031 // offset on PEI::calculateFrameObjectOffsets.
1032 // Arguments are always 32-bit.
1033 int FI = MFI->CreateFixedObject(4, 0);
1034 MipsFI->recordStoreVarArgsFI(FI, -(4+(i*4)));
Dan Gohman475871a2008-07-27 21:46:04 +00001035 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001036
1037 // emit ISD::STORE whichs stores the
1038 // parameter value to a stack Location
Dale Johannesen33c960f2009-02-04 20:06:27 +00001039 ArgValues.push_back(DAG.getStore(Root, dl, ArgValue, PtrOff, NULL, 0));
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001040 }
1041
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001042 } else { // VA.isRegLoc()
1043
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001044 // sanity check
1045 assert(VA.isMemLoc());
1046
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001047 // The stack pointer offset is relative to the caller stack frame.
1048 // Since the real stack size is unknown here, a negative SPOffset
1049 // is used so there's a way to adjust these offsets when the stack
1050 // size get known (on EliminateFrameIndex). A dummy SPOffset is
1051 // used instead of a direct negative address (which is recorded to
1052 // be used on emitPrologue) to avoid mis-calc of the first stack
1053 // offset on PEI::calculateFrameObjectOffsets.
1054 // Arguments are always 32-bit.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001055 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1056 int FI = MFI->CreateFixedObject(ArgSize, 0);
1057 MipsFI->recordLoadArgsFI(FI, -(ArgSize+
1058 (FirstStackArgLoc + VA.getLocMemOffset())));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001059
1060 // Create load nodes to retrieve arguments from the stack
Dan Gohman475871a2008-07-27 21:46:04 +00001061 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dale Johannesen33c960f2009-02-04 20:06:27 +00001062 ArgValues.push_back(DAG.getLoad(VA.getValVT(), dl, Root, FIN, NULL, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001063 }
1064 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001065
1066 // The mips ABIs for returning structs by value requires that we copy
1067 // the sret argument into $v0 for the return. Save the argument into
1068 // a virtual register so that we can access it from the return points.
1069 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1070 unsigned Reg = MipsFI->getSRetReturnReg();
1071 if (!Reg) {
1072 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
1073 MipsFI->setSRetReturnReg(Reg);
1074 }
Dale Johannesen33c960f2009-02-04 20:06:27 +00001075 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, ArgValues[0]);
1076 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Root);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001077 }
1078
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001079 ArgValues.push_back(Root);
1080
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001081 // Return the new list of results.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001082 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00001083 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001084}
1085
1086//===----------------------------------------------------------------------===//
1087// Return Value Calling Convention Implementation
1088//===----------------------------------------------------------------------===//
1089
Dan Gohman475871a2008-07-27 21:46:04 +00001090SDValue MipsTargetLowering::
1091LowerRET(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001092{
1093 // CCValAssign - represent the assignment of
1094 // the return value to a location
1095 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001096 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
1097 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Dale Johannesena05dca42009-02-04 23:02:30 +00001098 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001099
1100 // CCState - Info about the registers and stack slot.
Owen Andersond1474d02009-07-09 17:57:24 +00001101 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001102
1103 // Analize return values of ISD::RET
Gabor Greifba36cb52008-08-28 21:40:38 +00001104 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001105
1106 // If this is the first return lowered for this function, add
1107 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001108 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001109 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001110 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +00001111 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001112 }
1113
1114 // The chain is always operand #0
Dan Gohman475871a2008-07-27 21:46:04 +00001115 SDValue Chain = Op.getOperand(0);
1116 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001117
1118 // Copy the result values into the output registers.
1119 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1120 CCValAssign &VA = RVLocs[i];
1121 assert(VA.isRegLoc() && "Can only return in registers!");
1122
1123 // ISD::RET => ret chain, (regnum1,val1), ...
1124 // So i*2+1 index only the regnums
Dale Johannesena05dca42009-02-04 23:02:30 +00001125 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1126 Op.getOperand(i*2+1), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001127
1128 // guarantee that all emitted copies are
1129 // stuck together, avoiding something bad
1130 Flag = Chain.getValue(1);
1131 }
1132
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001133 // The mips ABIs for returning structs by value requires that we copy
1134 // the sret argument into $v0 for the return. We saved the argument into
1135 // a virtual register in the entry block, so now we copy the value out
1136 // and into $v0.
1137 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1138 MachineFunction &MF = DAG.getMachineFunction();
1139 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1140 unsigned Reg = MipsFI->getSRetReturnReg();
1141
1142 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00001143 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00001144 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001145
Dale Johannesena05dca42009-02-04 23:02:30 +00001146 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001147 Flag = Chain.getValue(1);
1148 }
1149
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001150 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00001151 if (Flag.getNode())
Dale Johannesena05dca42009-02-04 23:02:30 +00001152 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +00001153 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001154 else // Return Void
Dale Johannesena05dca42009-02-04 23:02:30 +00001155 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +00001156 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001157}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001158
1159//===----------------------------------------------------------------------===//
1160// Mips Inline Assembly Support
1161//===----------------------------------------------------------------------===//
1162
1163/// getConstraintType - Given a constraint letter, return the type of
1164/// constraint it is for this target.
1165MipsTargetLowering::ConstraintType MipsTargetLowering::
1166getConstraintType(const std::string &Constraint) const
1167{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001168 // Mips specific constrainy
1169 // GCC config/mips/constraints.md
1170 //
1171 // 'd' : An address register. Equivalent to r
1172 // unless generating MIPS16 code.
1173 // 'y' : Equivalent to r; retained for
1174 // backwards compatibility.
Bruno Cardoso Lopes7b76da12008-07-09 04:45:36 +00001175 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001176 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001177 switch (Constraint[0]) {
1178 default : break;
1179 case 'd':
1180 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001181 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001182 return C_RegisterClass;
1183 break;
1184 }
1185 }
1186 return TargetLowering::getConstraintType(Constraint);
1187}
1188
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001189/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
1190/// return a list of registers that can be used to satisfy the constraint.
1191/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001192std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00001193getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001194{
1195 if (Constraint.size() == 1) {
1196 switch (Constraint[0]) {
1197 case 'r':
1198 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001199 case 'f':
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001200 if (VT == MVT::f32)
1201 return std::make_pair(0U, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001202 if (VT == MVT::f64)
1203 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1204 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001205 }
1206 }
1207 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1208}
1209
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001210/// Given a register class constraint, like 'r', if this corresponds directly
1211/// to an LLVM register class, return a register of 0 and the register class
1212/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001213std::vector<unsigned> MipsTargetLowering::
1214getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001215 MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001216{
1217 if (Constraint.size() != 1)
1218 return std::vector<unsigned>();
1219
1220 switch (Constraint[0]) {
1221 default : break;
1222 case 'r':
1223 // GCC Mips Constraint Letters
1224 case 'd':
1225 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001226 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
1227 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
1228 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
1229 Mips::T8, 0);
1230
1231 case 'f':
Duncan Sands15126422008-07-08 09:33:14 +00001232 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001233 if (Subtarget->isSingleFloat())
1234 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
1235 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
1236 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
1237 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
1238 Mips::F30, Mips::F31, 0);
1239 else
1240 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
1241 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
1242 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +00001243 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001244
1245 if (VT == MVT::f64)
1246 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1247 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
1248 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
1249 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001250 }
1251 return std::vector<unsigned>();
1252}
Dan Gohman6520e202008-10-18 02:06:02 +00001253
1254bool
1255MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1256 // The Mips target isn't yet aware of offsets.
1257 return false;
1258}