blob: f132d2de8b83a27e8b7698264f6cf0c4eab4ef09 [file] [log] [blame]
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "mips-lower"
16
17#include "MipsISelLowering.h"
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +000018#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000019#include "MipsTargetMachine.h"
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000020#include "MipsSubtarget.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000021#include "llvm/DerivedTypes.h"
22#include "llvm/Function.h"
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +000023#include "llvm/GlobalVariable.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000024#include "llvm/Intrinsics.h"
25#include "llvm/CallingConv.h"
26#include "llvm/CodeGen/CallingConvLower.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000031#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000032#include "llvm/CodeGen/ValueTypes.h"
33#include "llvm/Support/Debug.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000034using namespace llvm;
35
36const char *MipsTargetLowering::
37getTargetNodeName(unsigned Opcode) const
38{
39 switch (Opcode)
40 {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000041 case MipsISD::JmpLink : return "MipsISD::JmpLink";
42 case MipsISD::Hi : return "MipsISD::Hi";
43 case MipsISD::Lo : return "MipsISD::Lo";
44 case MipsISD::GPRel : return "MipsISD::GPRel";
45 case MipsISD::Ret : return "MipsISD::Ret";
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +000046 case MipsISD::CMov : return "MipsISD::CMov";
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000047 case MipsISD::SelectCC : return "MipsISD::SelectCC";
48 case MipsISD::FPSelectCC : return "MipsISD::FPSelectCC";
49 case MipsISD::FPBrcond : return "MipsISD::FPBrcond";
50 case MipsISD::FPCmp : return "MipsISD::FPCmp";
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +000051 case MipsISD::FPRound : return "MipsISD::FPRound";
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000052 default : return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000053 }
54}
55
56MipsTargetLowering::
57MipsTargetLowering(MipsTargetMachine &TM): TargetLowering(TM)
58{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000059 Subtarget = &TM.getSubtarget<MipsSubtarget>();
60
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000061 // Mips does not have i1 type, so use i32 for
62 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000063 setBooleanContents(ZeroOrOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000064
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000065 // JumpTable targets must use GOT when using PIC_
66 setUsesGlobalOffsetTable(true);
67
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000068 // Set up the register classes
69 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000070 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000071
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000072 // When dealing with single precision only, use libcalls
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000073 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000074 if (!Subtarget->isFP64bit())
75 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000076
Bruno Cardoso Lopes7030ae72008-07-30 19:00:31 +000077 // Legal fp constants
78 addLegalFPImmediate(APFloat(+0.0f));
79
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000080 // Load extented operations for i1 types must be promoted
Evan Cheng03294662008-10-14 21:26:46 +000081 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
82 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
83 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000084
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000085 // Used by legalize types to correctly generate the setcc result.
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +000086 // Without this, every float setcc comes with a AND/OR with the result,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000087 // we don't want this, since the fpcmp result goes to a flag register,
88 // which is used implicitly by brcond and select operations.
89 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
90
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000091 // Mips Custom Operations
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +000092 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
93 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
94 setOperationAction(ISD::RET, MVT::Other, Custom);
95 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
96 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
97 setOperationAction(ISD::SELECT, MVT::f32, Custom);
Eli Friedman6314ac22009-06-16 06:40:59 +000098 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +000099 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000100 setOperationAction(ISD::SETCC, MVT::f32, Custom);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000101 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000102 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
103 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000104 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000105
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +0000106 // We custom lower AND/OR to handle the case where the DAG contain 'ands/ors'
107 // with operands comming from setcc fp comparions. This is necessary since
108 // the result from these setcc are in a flag registers (FCR31).
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000109 setOperationAction(ISD::AND, MVT::i32, Custom);
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +0000110 setOperationAction(ISD::OR, MVT::i32, Custom);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000111
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000112 // Operations not directly supported by Mips.
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000113 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
114 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
115 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000116 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
117 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000119 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
120 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000121 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000122 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
123 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
124 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Bruno Cardoso Lopes7bd71822008-07-31 18:50:54 +0000125 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Eli Friedman6314ac22009-06-16 06:40:59 +0000126 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000127
128 // We don't have line number support yet.
129 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
130 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
131 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
132 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
133
134 // Use the default for now
135 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
136 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
137 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000138
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000139 if (Subtarget->isSingleFloat())
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000140 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000141
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000142 if (!Subtarget->hasSEInReg()) {
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000143 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000144 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
145 }
146
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000147 if (!Subtarget->hasBitCount())
148 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
149
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000150 if (!Subtarget->hasSwap())
151 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
152
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000153 setStackPointerRegisterToSaveRestore(Mips::SP);
154 computeRegisterProperties();
155}
156
Duncan Sands5480c042009-01-01 15:52:00 +0000157MVT MipsTargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000158 return MVT::i32;
159}
160
Bill Wendlingb4202b82009-07-01 18:50:55 +0000161/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000162unsigned MipsTargetLowering::getFunctionAlignment(const Function *) const {
163 return 2;
164}
Scott Michel5b8f82e2008-03-10 15:42:14 +0000165
Dan Gohman475871a2008-07-27 21:46:04 +0000166SDValue MipsTargetLowering::
167LowerOperation(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000168{
169 switch (Op.getOpcode())
170 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000171 case ISD::AND: return LowerANDOR(Op, DAG);
172 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
173 case ISD::CALL: return LowerCALL(Op, DAG);
174 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
175 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
176 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000177 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000178 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
179 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
180 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
181 case ISD::OR: return LowerANDOR(Op, DAG);
182 case ISD::RET: return LowerRET(Op, DAG);
183 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000184 case ISD::SETCC: return LowerSETCC(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000185 }
Dan Gohman475871a2008-07-27 21:46:04 +0000186 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000187}
188
189//===----------------------------------------------------------------------===//
190// Lower helper functions
191//===----------------------------------------------------------------------===//
192
193// AddLiveIn - This helper function adds the specified physical register to the
194// MachineFunction as a live in value. It also creates a corresponding
195// virtual register for it.
196static unsigned
197AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
198{
199 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000200 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
201 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000202 return VReg;
203}
204
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000205// A address must be loaded from a small section if its size is less than the
206// small section size threshold. Data in this section must be addressed using
207// gp_rel operator.
208bool MipsTargetLowering::IsInSmallSection(unsigned Size) {
209 return (Size > 0 && (Size <= Subtarget->getSSectionThreshold()));
210}
211
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000212// Discover if this global address can be placed into small data/bss section.
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000213bool MipsTargetLowering::IsGlobalInSmallSection(GlobalValue *GV)
214{
215 const TargetData *TD = getTargetData();
Bruno Cardoso Lopesfeb95cc2008-07-22 15:34:27 +0000216 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
217
218 if (!GVA)
219 return false;
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000220
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000221 const Type *Ty = GV->getType()->getElementType();
Duncan Sands777d2302009-05-09 07:06:46 +0000222 unsigned Size = TD->getTypeAllocSize(Ty);
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000223
224 // if this is a internal constant string, there is a special
225 // section for it, but not in small data/bss.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000226 if (GVA->hasInitializer() && GV->hasLocalLinkage()) {
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000227 Constant *C = GVA->getInitializer();
228 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
229 if (CVA && CVA->isCString())
230 return false;
231 }
232
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000233 return IsInSmallSection(Size);
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +0000234}
235
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000236// Get fp branch code (not opcode) from condition code.
237static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
238 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
239 return Mips::BRANCH_T;
240
241 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
242 return Mips::BRANCH_F;
243
244 return Mips::BRANCH_INVALID;
245}
246
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000247static unsigned FPBranchCodeToOpc(Mips::FPBranchCode BC) {
248 switch(BC) {
249 default:
250 assert(0 && "Unknown branch code");
251 case Mips::BRANCH_T : return Mips::BC1T;
252 case Mips::BRANCH_F : return Mips::BC1F;
253 case Mips::BRANCH_TL : return Mips::BC1TL;
254 case Mips::BRANCH_FL : return Mips::BC1FL;
255 }
256}
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000257
258static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
259 switch (CC) {
260 default: assert(0 && "Unknown fp condition code!");
261 case ISD::SETEQ:
262 case ISD::SETOEQ: return Mips::FCOND_EQ;
263 case ISD::SETUNE: return Mips::FCOND_OGL;
264 case ISD::SETLT:
265 case ISD::SETOLT: return Mips::FCOND_OLT;
266 case ISD::SETGT:
267 case ISD::SETOGT: return Mips::FCOND_OGT;
268 case ISD::SETLE:
269 case ISD::SETOLE: return Mips::FCOND_OLE;
270 case ISD::SETGE:
271 case ISD::SETOGE: return Mips::FCOND_OGE;
272 case ISD::SETULT: return Mips::FCOND_ULT;
273 case ISD::SETULE: return Mips::FCOND_ULE;
274 case ISD::SETUGT: return Mips::FCOND_UGT;
275 case ISD::SETUGE: return Mips::FCOND_UGE;
276 case ISD::SETUO: return Mips::FCOND_UN;
277 case ISD::SETO: return Mips::FCOND_OR;
278 case ISD::SETNE:
279 case ISD::SETONE: return Mips::FCOND_NEQ;
280 case ISD::SETUEQ: return Mips::FCOND_UEQ;
281 }
282}
283
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000284MachineBasicBlock *
285MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +0000286 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000287 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
288 bool isFPCmp = false;
Dale Johannesen94817572009-02-13 02:34:39 +0000289 DebugLoc dl = MI->getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000290
291 switch (MI->getOpcode()) {
292 default: assert(false && "Unexpected instr type to insert");
293 case Mips::Select_FCC:
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000294 case Mips::Select_FCC_S32:
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000295 case Mips::Select_FCC_D32:
296 isFPCmp = true; // FALL THROUGH
297 case Mips::Select_CC:
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000298 case Mips::Select_CC_S32:
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000299 case Mips::Select_CC_D32: {
300 // To "insert" a SELECT_CC instruction, we actually have to insert the
301 // diamond control-flow pattern. The incoming instruction knows the
302 // destination vreg to set, the condition code register to branch on, the
303 // true/false values to select between, and a branch opcode to use.
304 const BasicBlock *LLVM_BB = BB->getBasicBlock();
305 MachineFunction::iterator It = BB;
306 ++It;
307
308 // thisMBB:
309 // ...
310 // TrueVal = ...
311 // setcc r1, r2, r3
312 // bNE r1, r0, copy1MBB
313 // fallthrough --> copy0MBB
314 MachineBasicBlock *thisMBB = BB;
315 MachineFunction *F = BB->getParent();
316 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
317 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
318
319 // Emit the right instruction according to the type of the operands compared
320 if (isFPCmp) {
321 // Find the condiction code present in the setcc operation.
322 Mips::CondCode CC = (Mips::CondCode)MI->getOperand(4).getImm();
323 // Get the branch opcode from the branch code.
324 unsigned Opc = FPBranchCodeToOpc(GetFPBranchCodeFromCond(CC));
Dale Johannesen94817572009-02-13 02:34:39 +0000325 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000326 } else
Dale Johannesen94817572009-02-13 02:34:39 +0000327 BuildMI(BB, dl, TII->get(Mips::BNE)).addReg(MI->getOperand(1).getReg())
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000328 .addReg(Mips::ZERO).addMBB(sinkMBB);
329
330 F->insert(It, copy0MBB);
331 F->insert(It, sinkMBB);
332 // Update machine-CFG edges by first adding all successors of the current
333 // block to the new block which will contain the Phi node for the select.
334 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
335 e = BB->succ_end(); i != e; ++i)
336 sinkMBB->addSuccessor(*i);
337 // Next, remove all successors of the current block, and add the true
338 // and fallthrough blocks as its successors.
339 while(!BB->succ_empty())
340 BB->removeSuccessor(BB->succ_begin());
341 BB->addSuccessor(copy0MBB);
342 BB->addSuccessor(sinkMBB);
343
344 // copy0MBB:
345 // %FalseValue = ...
346 // # fallthrough to sinkMBB
347 BB = copy0MBB;
348
349 // Update machine-CFG edges
350 BB->addSuccessor(sinkMBB);
351
352 // sinkMBB:
353 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
354 // ...
355 BB = sinkMBB;
Dale Johannesen94817572009-02-13 02:34:39 +0000356 BuildMI(BB, dl, TII->get(Mips::PHI), MI->getOperand(0).getReg())
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000357 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
358 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB);
359
360 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
361 return BB;
362 }
363 }
364}
365
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000366//===----------------------------------------------------------------------===//
367// Misc Lower Operation implementation
368//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000369
Dan Gohman475871a2008-07-27 21:46:04 +0000370SDValue MipsTargetLowering::
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000371LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG)
372{
373 if (!Subtarget->isMips1())
374 return Op;
375
376 MachineFunction &MF = DAG.getMachineFunction();
377 unsigned CCReg = AddLiveIn(MF, Mips::FCR31, Mips::CCRRegisterClass);
378
379 SDValue Chain = DAG.getEntryNode();
380 DebugLoc dl = Op.getDebugLoc();
381 SDValue Src = Op.getOperand(0);
382
383 // Set the condition register
384 SDValue CondReg = DAG.getCopyFromReg(Chain, dl, CCReg, MVT::i32);
385 CondReg = DAG.getCopyToReg(Chain, dl, Mips::AT, CondReg);
386 CondReg = DAG.getCopyFromReg(CondReg, dl, Mips::AT, MVT::i32);
387
388 SDValue Cst = DAG.getConstant(3, MVT::i32);
389 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, CondReg, Cst);
390 Cst = DAG.getConstant(2, MVT::i32);
391 SDValue Xor = DAG.getNode(ISD::XOR, dl, MVT::i32, Or, Cst);
392
393 SDValue InFlag(0, 0);
394 CondReg = DAG.getCopyToReg(Chain, dl, Mips::FCR31, Xor, InFlag);
395
396 // Emit the round instruction and bit convert to integer
397 SDValue Trunc = DAG.getNode(MipsISD::FPRound, dl, MVT::f32,
398 Src, CondReg.getValue(1));
399 SDValue BitCvt = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Trunc);
400 return BitCvt;
401}
402
403SDValue MipsTargetLowering::
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000404LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG)
405{
406 SDValue Chain = Op.getOperand(0);
407 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +0000408 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000409
410 // Get a reference from Mips stack pointer
Dale Johannesena05dca42009-02-04 23:02:30 +0000411 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000412
413 // Subtract the dynamic size from the actual stack size to
414 // obtain the new stack size.
Dale Johannesena05dca42009-02-04 23:02:30 +0000415 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000416
417 // The Sub result contains the new stack start address, so it
418 // must be placed in the stack pointer register.
Dale Johannesena05dca42009-02-04 23:02:30 +0000419 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000420
421 // This node always has two return values: a new stack pointer
422 // value and a chain
423 SDValue Ops[2] = { Sub, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000424 return DAG.getMergeValues(Ops, 2, dl);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000425}
426
427SDValue MipsTargetLowering::
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +0000428LowerANDOR(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000429{
430 SDValue LHS = Op.getOperand(0);
431 SDValue RHS = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +0000432 DebugLoc dl = Op.getDebugLoc();
433
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000434 if (LHS.getOpcode() != MipsISD::FPCmp || RHS.getOpcode() != MipsISD::FPCmp)
435 return Op;
436
437 SDValue True = DAG.getConstant(1, MVT::i32);
438 SDValue False = DAG.getConstant(0, MVT::i32);
439
Dale Johannesende064702009-02-06 21:50:26 +0000440 SDValue LSEL = DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000441 LHS, True, False, LHS.getOperand(2));
Dale Johannesende064702009-02-06 21:50:26 +0000442 SDValue RSEL = DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000443 RHS, True, False, RHS.getOperand(2));
444
Dale Johannesende064702009-02-06 21:50:26 +0000445 return DAG.getNode(Op.getOpcode(), dl, MVT::i32, LSEL, RSEL);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000446}
447
448SDValue MipsTargetLowering::
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000449LowerBRCOND(SDValue Op, SelectionDAG &DAG)
450{
451 // The first operand is the chain, the second is the condition, the third is
452 // the block to branch to if the condition is true.
453 SDValue Chain = Op.getOperand(0);
454 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000455 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000456
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000457 if (Op.getOperand(1).getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000458 return Op;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000459
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000460 SDValue CondRes = Op.getOperand(1);
461 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000462 Mips::CondCode CC =
463 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000464 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
465
Dale Johannesende064702009-02-06 21:50:26 +0000466 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000467 Dest, CondRes);
468}
469
470SDValue MipsTargetLowering::
471LowerSETCC(SDValue Op, SelectionDAG &DAG)
472{
473 // The operands to this are the left and right operands to compare (ops #0,
474 // and #1) and the condition code to compare them with (op #2) as a
475 // CondCodeSDNode.
476 SDValue LHS = Op.getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +0000477 SDValue RHS = Op.getOperand(1);
478 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000479
480 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
481
Dale Johannesende064702009-02-06 21:50:26 +0000482 return DAG.getNode(MipsISD::FPCmp, dl, Op.getValueType(), LHS, RHS,
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000483 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
484}
485
486SDValue MipsTargetLowering::
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000487LowerSELECT(SDValue Op, SelectionDAG &DAG)
488{
489 SDValue Cond = Op.getOperand(0);
490 SDValue True = Op.getOperand(1);
491 SDValue False = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000492 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000493
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000494 // if the incomming condition comes from a integer compare, the select
495 // operation must be SelectCC or a conditional move if the subtarget
496 // supports it.
497 if (Cond.getOpcode() != MipsISD::FPCmp) {
498 if (Subtarget->hasCondMov() && !True.getValueType().isFloatingPoint())
499 return Op;
Dale Johannesende064702009-02-06 21:50:26 +0000500 return DAG.getNode(MipsISD::SelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000501 Cond, True, False);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000502 }
503
504 // if the incomming condition comes from fpcmp, the select
505 // operation must use FPSelectCC.
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000506 SDValue CCNode = Cond.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000507 return DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000508 Cond, True, False, CCNode);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000509}
510
511SDValue MipsTargetLowering::
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000512LowerGlobalAddress(SDValue Op, SelectionDAG &DAG)
513{
Dale Johannesende064702009-02-06 21:50:26 +0000514 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000515 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000516 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
517 SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
518
519 if (!Subtarget->hasABICall()) {
Dan Gohmanfc166572009-04-09 23:54:40 +0000520 SDVTList VTs = DAG.getVTList(MVT::i32);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000521 SDValue Ops[] = { GA };
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000522 // %gp_rel relocation
523 if (!isa<Function>(GV) && IsGlobalInSmallSection(GV)) {
Dan Gohmanfc166572009-04-09 23:54:40 +0000524 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, Ops, 1);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000525 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000526 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000527 }
528 // %hi/%lo relocation
Dan Gohmanfc166572009-04-09 23:54:40 +0000529 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, Ops, 1);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000530 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GA);
531 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000532
533 } else { // Abicall relocations, TODO: make this cleaner.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000534 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
535 DAG.getEntryNode(), GA, NULL, 0);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000536 // On functions and global targets not internal linked only
537 // a load from got/GP is necessary for PIC to work.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000538 if (!GV->hasLocalLinkage() || isa<Function>(GV))
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000539 return ResNode;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000540 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GA);
541 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000542 }
543
544 assert(0 && "Dont know how to handle GlobalAddress");
545 return SDValue(0,0);
546}
547
548SDValue MipsTargetLowering::
549LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG)
550{
551 assert(0 && "TLS not implemented for MIPS.");
552 return SDValue(); // Not reached
553}
554
555SDValue MipsTargetLowering::
Dan Gohman475871a2008-07-27 21:46:04 +0000556LowerJumpTable(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000557{
Dan Gohman475871a2008-07-27 21:46:04 +0000558 SDValue ResNode;
559 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +0000560 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000561 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000562
Duncan Sands83ec4b62008-06-06 12:08:01 +0000563 MVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000564 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +0000565 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000566
567 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohmanfc166572009-04-09 23:54:40 +0000568 SDVTList VTs = DAG.getVTList(MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000569 SDValue Ops[] = { JTI };
Dan Gohmanfc166572009-04-09 23:54:40 +0000570 HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, Ops, 1);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000571 } else // Emit Load from Global Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +0000572 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI, NULL, 0);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000573
Dale Johannesen33c960f2009-02-04 20:06:27 +0000574 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTI);
575 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000576
577 return ResNode;
578}
579
Dan Gohman475871a2008-07-27 21:46:04 +0000580SDValue MipsTargetLowering::
581LowerConstantPool(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000582{
Dan Gohman475871a2008-07-27 21:46:04 +0000583 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000584 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
585 Constant *C = N->getConstVal();
Dan Gohman475871a2008-07-27 21:46:04 +0000586 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
Dale Johannesende064702009-02-06 21:50:26 +0000587 // FIXME there isn't actually debug info here
588 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000589
590 // gp_rel relocation
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000591 // FIXME: we should reference the constant pool using small data sections,
592 // but the asm printer currently doens't support this feature without
593 // hacking it. This feature should come soon so we can uncomment the
594 // stuff below.
595 //if (!Subtarget->hasABICall() &&
Duncan Sands777d2302009-05-09 07:06:46 +0000596 // IsInSmallSection(getTargetData()->getTypeAllocSize(C->getType()))) {
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000597 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000598 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000599 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
600 //} else { // %hi/%lo relocation
Dale Johannesende064702009-02-06 21:50:26 +0000601 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CP);
602 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CP);
603 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000604 //}
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000605
606 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000607}
608
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000609//===----------------------------------------------------------------------===//
610// Calling Convention Implementation
611//
612// The lower operations present on calling convention works on this order:
613// LowerCALL (virt regs --> phys regs, virt regs --> stack)
614// LowerFORMAL_ARGUMENTS (phys --> virt regs, stack --> virt regs)
615// LowerRET (virt regs --> phys regs)
616// LowerCALL (phys regs --> virt regs)
617//
618//===----------------------------------------------------------------------===//
619
620#include "MipsGenCallingConv.inc"
621
622//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000623// TODO: Implement a generic logic using tblgen that can support this.
624// Mips O32 ABI rules:
625// ---
626// i32 - Passed in A0, A1, A2, A3 and stack
627// f32 - Only passed in f32 registers if no int reg has been used yet to hold
628// an argument. Otherwise, passed in A1, A2, A3 and stack.
629// f64 - Only passed in two aliased f32 registers if no int reg has been used
630// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
631// not used, it must be shadowed. If only A3 is avaiable, shadow it and
632// go to stack.
633//===----------------------------------------------------------------------===//
634
635static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
636 MVT LocVT, CCValAssign::LocInfo LocInfo,
637 ISD::ArgFlagsTy ArgFlags, CCState &State) {
638
639 static const unsigned IntRegsSize=4, FloatRegsSize=2;
640
641 static const unsigned IntRegs[] = {
642 Mips::A0, Mips::A1, Mips::A2, Mips::A3
643 };
644 static const unsigned F32Regs[] = {
645 Mips::F12, Mips::F14
646 };
647 static const unsigned F64Regs[] = {
648 Mips::D6, Mips::D7
649 };
650
651 unsigned Reg=0;
652 unsigned UnallocIntReg = State.getFirstUnallocated(IntRegs, IntRegsSize);
653 bool IntRegUsed = (IntRegs[UnallocIntReg] != (unsigned (Mips::A0)));
654
655 // Promote i8 and i16
656 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
657 LocVT = MVT::i32;
658 if (ArgFlags.isSExt())
659 LocInfo = CCValAssign::SExt;
660 else if (ArgFlags.isZExt())
661 LocInfo = CCValAssign::ZExt;
662 else
663 LocInfo = CCValAssign::AExt;
664 }
665
666 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && IntRegUsed)) {
667 Reg = State.AllocateReg(IntRegs, IntRegsSize);
668 IntRegUsed = true;
669 LocVT = MVT::i32;
670 }
671
672 if (ValVT.isFloatingPoint() && !IntRegUsed) {
673 if (ValVT == MVT::f32)
674 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
675 else
676 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
677 }
678
679 if (ValVT == MVT::f64 && IntRegUsed) {
680 if (UnallocIntReg != IntRegsSize) {
681 // If we hit register A3 as the first not allocated, we must
682 // mark it as allocated (shadow) and use the stack instead.
683 if (IntRegs[UnallocIntReg] != (unsigned (Mips::A3)))
684 Reg = Mips::A2;
685 for (;UnallocIntReg < IntRegsSize; ++UnallocIntReg)
686 State.AllocateReg(UnallocIntReg);
687 }
688 LocVT = MVT::i32;
689 }
690
691 if (!Reg) {
692 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
693 unsigned Offset = State.AllocateStack(SizeInBytes, SizeInBytes);
694 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
695 } else
696 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
697
698 return false; // CC must always match
699}
700
701//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000702// CALL Calling Convention Implementation
703//===----------------------------------------------------------------------===//
704
Nate Begeman5bf4b752009-01-26 03:15:54 +0000705/// LowerCALL - functions arguments are copied from virtual regs to
706/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000707/// TODO: isVarArg, isTailCall.
Dan Gohman475871a2008-07-27 21:46:04 +0000708SDValue MipsTargetLowering::
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000709LowerCALL(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000710{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000711 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000712
Dan Gohman095cc292008-09-13 01:54:27 +0000713 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
714 SDValue Chain = TheCall->getChain();
715 SDValue Callee = TheCall->getCallee();
716 bool isVarArg = TheCall->isVarArg();
717 unsigned CC = TheCall->getCallingConv();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000718 DebugLoc dl = TheCall->getDebugLoc();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000719
720 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000721
722 // Analyze operands of the call, assigning locations to each operand.
723 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +0000724 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000725
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000726 // To meet O32 ABI, Mips must always allocate 16 bytes on
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000727 // the stack (even if less than 4 are used as arguments)
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000728 if (Subtarget->isABI_O32()) {
729 int VTsize = MVT(MVT::i32).getSizeInBits()/8;
730 MFI->CreateFixedObject(VTsize, (VTsize*3));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000731 CCInfo.AnalyzeCallOperands(TheCall, CC_MipsO32);
732 } else
733 CCInfo.AnalyzeCallOperands(TheCall, CC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000734
735 // Get a count of how many bytes are to be pushed on the stack.
736 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnere563bbc2008-10-11 22:08:30 +0000737 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000738
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000739 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +0000740 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
741 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000742
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000743 // First/LastArgStackLoc contains the first/last
744 // "at stack" argument location.
745 int LastArgStackLoc = 0;
746 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000747
748 // Walk the register/memloc assignments, inserting copies/loads.
749 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000750 SDValue Arg = TheCall->getArg(i);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000751 CCValAssign &VA = ArgLocs[i];
752
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000753 // Promote the value if needed.
754 switch (VA.getLocInfo()) {
Chris Lattnere0b12152008-03-17 06:57:02 +0000755 default: assert(0 && "Unknown loc info!");
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000756 case CCValAssign::Full:
757 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
758 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
759 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Arg);
760 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
761 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
762 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
763 DAG.getConstant(0, getPointerTy()));
764 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
765 DAG.getConstant(1, getPointerTy()));
766 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
767 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
768 continue;
769 }
770 }
771 break;
Chris Lattnere0b12152008-03-17 06:57:02 +0000772 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000773 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000774 break;
775 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000776 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000777 break;
778 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000779 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000780 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000781 }
782
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000783 // Arguments that can be passed on register must be kept at
784 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000785 if (VA.isRegLoc()) {
786 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +0000787 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000788 }
Chris Lattnere0b12152008-03-17 06:57:02 +0000789
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000790 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +0000791 assert(VA.isMemLoc());
792
793 // Create the frame index object for this incoming parameter
794 // This guarantees that when allocating Local Area the firsts
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000795 // 16 bytes which are alwayes reserved won't be overwritten
796 // if O32 ABI is used. For EABI the first address is zero.
797 LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000798 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000799 LastArgStackLoc);
Chris Lattnere0b12152008-03-17 06:57:02 +0000800
Dan Gohman475871a2008-07-27 21:46:04 +0000801 SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +0000802
803 // emit ISD::STORE whichs stores the
804 // parameter value to a stack Location
Dale Johannesen33c960f2009-02-04 20:06:27 +0000805 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000806 }
807
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000808 // Transform all store nodes into one single node because all store
809 // nodes are independent of each other.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000810 if (!MemOpChains.empty())
Dale Johannesen33c960f2009-02-04 20:06:27 +0000811 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000812 &MemOpChains[0], MemOpChains.size());
813
814 // Build a sequence of copy-to-reg nodes chained together with token
815 // chain and flag operands which copy the outgoing args into registers.
816 // The InFlag in necessary since all emited instructions must be
817 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000818 SDValue InFlag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000819 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000820 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000821 RegsToPass[i].second, InFlag);
822 InFlag = Chain.getValue(1);
823 }
824
Bill Wendling056292f2008-09-16 21:48:12 +0000825 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
826 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
827 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000828 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000829 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +0000830 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
831 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
832
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000833 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
834 // = Chain, Callee, Reg#1, Reg#2, ...
835 //
836 // Returns a chain & a flag for retval copy to use.
837 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000838 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000839 Ops.push_back(Chain);
840 Ops.push_back(Callee);
841
842 // Add argument registers to the end of the list so that they are
843 // known live into the call.
844 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
845 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
846 RegsToPass[i].second.getValueType()));
847
Gabor Greifba36cb52008-08-28 21:40:38 +0000848 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000849 Ops.push_back(InFlag);
850
Dale Johannesen33c960f2009-02-04 20:06:27 +0000851 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000852 InFlag = Chain.getValue(1);
853
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000854 // Create the CALLSEQ_END node.
Chris Lattnere563bbc2008-10-11 22:08:30 +0000855 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
856 DAG.getIntPtrConstant(0, true), InFlag);
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000857 InFlag = Chain.getValue(1);
858
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000859 // Create a stack location to hold GP when PIC is used. This stack
860 // location is used on function prologue to save GP and also after all
861 // emited CALL's to restore GP.
862 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000863 // Function can have an arbitrary number of calls, so
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000864 // hold the LastArgStackLoc with the biggest offset.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000865 int FI;
866 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000867 if (LastArgStackLoc >= MipsFI->getGPStackOffset()) {
868 LastArgStackLoc = (!LastArgStackLoc) ? (16) : (LastArgStackLoc+4);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000869 // Create the frame index only once. SPOffset here can be anything
870 // (this will be fixed on processFunctionBeforeFrameFinalized)
871 if (MipsFI->getGPStackOffset() == -1) {
872 FI = MFI->CreateFixedObject(4, 0);
873 MipsFI->setGPFI(FI);
874 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000875 MipsFI->setGPStackOffset(LastArgStackLoc);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000876 }
877
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000878 // Reload GP value.
879 FI = MipsFI->getGPFI();
Dan Gohman475871a2008-07-27 21:46:04 +0000880 SDValue FIN = DAG.getFrameIndex(FI,getPointerTy());
Dale Johannesen33c960f2009-02-04 20:06:27 +0000881 SDValue GPLoad = DAG.getLoad(MVT::i32, dl, Chain, FIN, NULL, 0);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000882 Chain = GPLoad.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000883 Chain = DAG.getCopyToReg(Chain, dl, DAG.getRegister(Mips::GP, MVT::i32),
Dan Gohman475871a2008-07-27 21:46:04 +0000884 GPLoad, SDValue(0,0));
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000885 InFlag = Chain.getValue(1);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000886 }
887
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000888 // Handle result values, copying them out of physregs into vregs that we
889 // return.
Dan Gohman095cc292008-09-13 01:54:27 +0000890 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG), Op.getResNo());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000891}
892
893/// LowerCallResult - Lower the result values of an ISD::CALL into the
894/// appropriate copies out of appropriate physical registers. This assumes that
895/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
896/// being lowered. Returns a SDNode with the same number of values as the
897/// ISD::CALL.
898SDNode *MipsTargetLowering::
Dan Gohman095cc292008-09-13 01:54:27 +0000899LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000900 unsigned CallingConv, SelectionDAG &DAG) {
901
Dan Gohman095cc292008-09-13 01:54:27 +0000902 bool isVarArg = TheCall->isVarArg();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000903 DebugLoc dl = TheCall->getDebugLoc();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000904
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000905 // Assign locations to each value returned by this call.
906 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +0000907 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(),
908 RVLocs, DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000909
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000910 CCInfo.AnalyzeCallResult(TheCall, RetCC_Mips);
Dan Gohman475871a2008-07-27 21:46:04 +0000911 SmallVector<SDValue, 8> ResultVals;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000912
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000913 // Copy all of the result registers out of their specified physreg.
914 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000915 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000916 RVLocs[i].getValVT(), InFlag).getValue(1);
917 InFlag = Chain.getValue(2);
918 ResultVals.push_back(Chain.getValue(0));
919 }
920
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000921 ResultVals.push_back(Chain);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000922
923 // Merge everything together with a MERGE_VALUES node.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000924 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +0000925 &ResultVals[0], ResultVals.size()).getNode();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000926}
927
928//===----------------------------------------------------------------------===//
929// FORMAL_ARGUMENTS Calling Convention Implementation
930//===----------------------------------------------------------------------===//
931
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000932/// LowerFORMAL_ARGUMENTS - transform physical registers into
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000933/// virtual registers and generate load operations for
934/// arguments places on the stack.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000935/// TODO: isVarArg
Dan Gohman475871a2008-07-27 21:46:04 +0000936SDValue MipsTargetLowering::
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000937LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000938{
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000939 SDValue Root = Op.getOperand(0);
940 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000941 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000942 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000943 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000944
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000945 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000946 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000947
948 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000949
950 // Assign locations to all of the incoming arguments.
951 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersond1474d02009-07-09 17:57:24 +0000952 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000953
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000954 if (Subtarget->isABI_O32())
955 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_MipsO32);
956 else
957 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_Mips);
958
Dan Gohman475871a2008-07-27 21:46:04 +0000959 SmallVector<SDValue, 16> ArgValues;
960 SDValue StackPtr;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000961
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000962 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
963
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000964 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000965 CCValAssign &VA = ArgLocs[i];
966
967 // Arguments stored on registers
968 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000969 MVT RegVT = VA.getLocVT();
Bill Wendling06b8c192008-07-09 05:55:53 +0000970 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000971
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000972 if (RegVT == MVT::i32)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000973 RC = Mips::CPURegsRegisterClass;
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000974 else if (RegVT == MVT::f32)
975 RC = Mips::FGR32RegisterClass;
976 else if (RegVT == MVT::f64) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000977 if (!Subtarget->isSingleFloat())
978 RC = Mips::AFGR64RegisterClass;
979 } else
980 assert(0 && "RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000981
982 // Transform the arguments stored on
983 // physical registers into virtual ones
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000984 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000985 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000986
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000987 // If this is an 8 or 16-bit value, it has been passed promoted
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000988 // to 32 bits. Insert an assert[sz]ext to capture this, then
989 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000990 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +0000991 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000992 if (VA.getLocInfo() == CCValAssign::SExt)
993 Opcode = ISD::AssertSext;
994 else if (VA.getLocInfo() == CCValAssign::ZExt)
995 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +0000996 if (Opcode)
997 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
998 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +0000999 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001000 }
1001
1002 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
1003 if (Subtarget->isABI_O32()) {
1004 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
1005 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, ArgValue);
1006 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
1007 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
1008 VA.getLocReg()+1, RC);
1009 SDValue ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg2, RegVT);
1010 SDValue Hi = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, ArgValue);
1011 SDValue Lo = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, ArgValue2);
1012 ArgValue = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::f64, Lo, Hi);
1013 }
1014 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001015
1016 ArgValues.push_back(ArgValue);
1017
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001018 // To meet ABI, when VARARGS are passed on registers, the registers
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001019 // must have their values written to the caller stack frame.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001020 if ((isVarArg) && (Subtarget->isABI_O32())) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001021 if (StackPtr.getNode() == 0)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001022 StackPtr = DAG.getRegister(StackReg, getPointerTy());
1023
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001024 // The stack pointer offset is relative to the caller stack frame.
1025 // Since the real stack size is unknown here, a negative SPOffset
1026 // is used so there's a way to adjust these offsets when the stack
1027 // size get known (on EliminateFrameIndex). A dummy SPOffset is
1028 // used instead of a direct negative address (which is recorded to
1029 // be used on emitPrologue) to avoid mis-calc of the first stack
1030 // offset on PEI::calculateFrameObjectOffsets.
1031 // Arguments are always 32-bit.
1032 int FI = MFI->CreateFixedObject(4, 0);
1033 MipsFI->recordStoreVarArgsFI(FI, -(4+(i*4)));
Dan Gohman475871a2008-07-27 21:46:04 +00001034 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001035
1036 // emit ISD::STORE whichs stores the
1037 // parameter value to a stack Location
Dale Johannesen33c960f2009-02-04 20:06:27 +00001038 ArgValues.push_back(DAG.getStore(Root, dl, ArgValue, PtrOff, NULL, 0));
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001039 }
1040
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001041 } else { // VA.isRegLoc()
1042
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001043 // sanity check
1044 assert(VA.isMemLoc());
1045
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001046 // The stack pointer offset is relative to the caller stack frame.
1047 // Since the real stack size is unknown here, a negative SPOffset
1048 // is used so there's a way to adjust these offsets when the stack
1049 // size get known (on EliminateFrameIndex). A dummy SPOffset is
1050 // used instead of a direct negative address (which is recorded to
1051 // be used on emitPrologue) to avoid mis-calc of the first stack
1052 // offset on PEI::calculateFrameObjectOffsets.
1053 // Arguments are always 32-bit.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001054 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1055 int FI = MFI->CreateFixedObject(ArgSize, 0);
1056 MipsFI->recordLoadArgsFI(FI, -(ArgSize+
1057 (FirstStackArgLoc + VA.getLocMemOffset())));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001058
1059 // Create load nodes to retrieve arguments from the stack
Dan Gohman475871a2008-07-27 21:46:04 +00001060 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dale Johannesen33c960f2009-02-04 20:06:27 +00001061 ArgValues.push_back(DAG.getLoad(VA.getValVT(), dl, Root, FIN, NULL, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001062 }
1063 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001064
1065 // The mips ABIs for returning structs by value requires that we copy
1066 // the sret argument into $v0 for the return. Save the argument into
1067 // a virtual register so that we can access it from the return points.
1068 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1069 unsigned Reg = MipsFI->getSRetReturnReg();
1070 if (!Reg) {
1071 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
1072 MipsFI->setSRetReturnReg(Reg);
1073 }
Dale Johannesen33c960f2009-02-04 20:06:27 +00001074 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, ArgValues[0]);
1075 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Root);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001076 }
1077
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001078 ArgValues.push_back(Root);
1079
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001080 // Return the new list of results.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001081 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00001082 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001083}
1084
1085//===----------------------------------------------------------------------===//
1086// Return Value Calling Convention Implementation
1087//===----------------------------------------------------------------------===//
1088
Dan Gohman475871a2008-07-27 21:46:04 +00001089SDValue MipsTargetLowering::
1090LowerRET(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001091{
1092 // CCValAssign - represent the assignment of
1093 // the return value to a location
1094 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001095 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
1096 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Dale Johannesena05dca42009-02-04 23:02:30 +00001097 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001098
1099 // CCState - Info about the registers and stack slot.
Owen Andersond1474d02009-07-09 17:57:24 +00001100 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001101
1102 // Analize return values of ISD::RET
Gabor Greifba36cb52008-08-28 21:40:38 +00001103 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001104
1105 // If this is the first return lowered for this function, add
1106 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001107 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001108 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001109 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +00001110 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001111 }
1112
1113 // The chain is always operand #0
Dan Gohman475871a2008-07-27 21:46:04 +00001114 SDValue Chain = Op.getOperand(0);
1115 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001116
1117 // Copy the result values into the output registers.
1118 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1119 CCValAssign &VA = RVLocs[i];
1120 assert(VA.isRegLoc() && "Can only return in registers!");
1121
1122 // ISD::RET => ret chain, (regnum1,val1), ...
1123 // So i*2+1 index only the regnums
Dale Johannesena05dca42009-02-04 23:02:30 +00001124 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1125 Op.getOperand(i*2+1), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001126
1127 // guarantee that all emitted copies are
1128 // stuck together, avoiding something bad
1129 Flag = Chain.getValue(1);
1130 }
1131
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001132 // The mips ABIs for returning structs by value requires that we copy
1133 // the sret argument into $v0 for the return. We saved the argument into
1134 // a virtual register in the entry block, so now we copy the value out
1135 // and into $v0.
1136 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1137 MachineFunction &MF = DAG.getMachineFunction();
1138 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1139 unsigned Reg = MipsFI->getSRetReturnReg();
1140
1141 if (!Reg)
1142 assert(0 && "sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00001143 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001144
Dale Johannesena05dca42009-02-04 23:02:30 +00001145 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001146 Flag = Chain.getValue(1);
1147 }
1148
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001149 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00001150 if (Flag.getNode())
Dale Johannesena05dca42009-02-04 23:02:30 +00001151 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +00001152 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001153 else // Return Void
Dale Johannesena05dca42009-02-04 23:02:30 +00001154 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +00001155 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001156}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001157
1158//===----------------------------------------------------------------------===//
1159// Mips Inline Assembly Support
1160//===----------------------------------------------------------------------===//
1161
1162/// getConstraintType - Given a constraint letter, return the type of
1163/// constraint it is for this target.
1164MipsTargetLowering::ConstraintType MipsTargetLowering::
1165getConstraintType(const std::string &Constraint) const
1166{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001167 // Mips specific constrainy
1168 // GCC config/mips/constraints.md
1169 //
1170 // 'd' : An address register. Equivalent to r
1171 // unless generating MIPS16 code.
1172 // 'y' : Equivalent to r; retained for
1173 // backwards compatibility.
Bruno Cardoso Lopes7b76da12008-07-09 04:45:36 +00001174 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001175 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001176 switch (Constraint[0]) {
1177 default : break;
1178 case 'd':
1179 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001180 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001181 return C_RegisterClass;
1182 break;
1183 }
1184 }
1185 return TargetLowering::getConstraintType(Constraint);
1186}
1187
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001188/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
1189/// return a list of registers that can be used to satisfy the constraint.
1190/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001191std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00001192getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001193{
1194 if (Constraint.size() == 1) {
1195 switch (Constraint[0]) {
1196 case 'r':
1197 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001198 case 'f':
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001199 if (VT == MVT::f32)
1200 return std::make_pair(0U, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001201 if (VT == MVT::f64)
1202 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1203 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001204 }
1205 }
1206 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1207}
1208
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001209/// Given a register class constraint, like 'r', if this corresponds directly
1210/// to an LLVM register class, return a register of 0 and the register class
1211/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001212std::vector<unsigned> MipsTargetLowering::
1213getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001214 MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001215{
1216 if (Constraint.size() != 1)
1217 return std::vector<unsigned>();
1218
1219 switch (Constraint[0]) {
1220 default : break;
1221 case 'r':
1222 // GCC Mips Constraint Letters
1223 case 'd':
1224 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001225 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
1226 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
1227 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
1228 Mips::T8, 0);
1229
1230 case 'f':
Duncan Sands15126422008-07-08 09:33:14 +00001231 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001232 if (Subtarget->isSingleFloat())
1233 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
1234 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
1235 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
1236 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
1237 Mips::F30, Mips::F31, 0);
1238 else
1239 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
1240 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
1241 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +00001242 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001243
1244 if (VT == MVT::f64)
1245 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1246 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
1247 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
1248 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001249 }
1250 return std::vector<unsigned>();
1251}
Dan Gohman6520e202008-10-18 02:06:02 +00001252
1253bool
1254MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1255 // The Mips target isn't yet aware of offsets.
1256 return false;
1257}