blob: 005827266d63cb5fad18be6dafe9bd034c54473e [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"
Chris Lattnerf0144122009-07-28 03:13:23 +000033#include "llvm/Target/TargetLoweringObjectFile.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000034#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000036using namespace llvm;
37
Chris Lattnerf0144122009-07-28 03:13:23 +000038const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
39 switch (Opcode) {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000040 case MipsISD::JmpLink : return "MipsISD::JmpLink";
41 case MipsISD::Hi : return "MipsISD::Hi";
42 case MipsISD::Lo : return "MipsISD::Lo";
43 case MipsISD::GPRel : return "MipsISD::GPRel";
44 case MipsISD::Ret : return "MipsISD::Ret";
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +000045 case MipsISD::CMov : return "MipsISD::CMov";
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000046 case MipsISD::SelectCC : return "MipsISD::SelectCC";
47 case MipsISD::FPSelectCC : return "MipsISD::FPSelectCC";
48 case MipsISD::FPBrcond : return "MipsISD::FPBrcond";
49 case MipsISD::FPCmp : return "MipsISD::FPCmp";
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +000050 case MipsISD::FPRound : return "MipsISD::FPRound";
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +000051 default : return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000052 }
53}
54
55MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000056MipsTargetLowering(MipsTargetMachine &TM)
57 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000058 Subtarget = &TM.getSubtarget<MipsSubtarget>();
59
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000060 // Mips does not have i1 type, so use i32 for
61 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000062 setBooleanContents(ZeroOrOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000063
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +000064 // JumpTable targets must use GOT when using PIC_
65 setUsesGlobalOffsetTable(true);
66
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000067 // Set up the register classes
Owen Andersone50ed302009-08-10 22:56:29 +000068 addRegisterClass(EVT::i32, Mips::CPURegsRegisterClass);
69 addRegisterClass(EVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000070
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000071 // When dealing with single precision only, use libcalls
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000072 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000073 if (!Subtarget->isFP64bit())
Owen Andersone50ed302009-08-10 22:56:29 +000074 addRegisterClass(EVT::f64, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000075
Bruno Cardoso Lopes7030ae72008-07-30 19:00:31 +000076 // Legal fp constants
77 addLegalFPImmediate(APFloat(+0.0f));
78
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000079 // Load extented operations for i1 types must be promoted
Owen Andersone50ed302009-08-10 22:56:29 +000080 setLoadExtAction(ISD::EXTLOAD, EVT::i1, Promote);
81 setLoadExtAction(ISD::ZEXTLOAD, EVT::i1, Promote);
82 setLoadExtAction(ISD::SEXTLOAD, EVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000083
Eli Friedman6055a6a2009-07-17 04:07:24 +000084 // MIPS doesn't have extending float->double load/store
Owen Andersone50ed302009-08-10 22:56:29 +000085 setLoadExtAction(ISD::EXTLOAD, EVT::f32, Expand);
86 setTruncStoreAction(EVT::f64, EVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +000087
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000088 // Used by legalize types to correctly generate the setcc result.
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +000089 // Without this, every float setcc comes with a AND/OR with the result,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000090 // we don't want this, since the fpcmp result goes to a flag register,
91 // which is used implicitly by brcond and select operations.
Owen Andersone50ed302009-08-10 22:56:29 +000092 AddPromotedToType(ISD::SETCC, EVT::i1, EVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000093
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000094 // Mips Custom Operations
Owen Andersone50ed302009-08-10 22:56:29 +000095 setOperationAction(ISD::GlobalAddress, EVT::i32, Custom);
96 setOperationAction(ISD::GlobalTLSAddress, EVT::i32, Custom);
97 setOperationAction(ISD::JumpTable, EVT::i32, Custom);
98 setOperationAction(ISD::ConstantPool, EVT::i32, Custom);
99 setOperationAction(ISD::SELECT, EVT::f32, Custom);
100 setOperationAction(ISD::SELECT, EVT::f64, Custom);
101 setOperationAction(ISD::SELECT, EVT::i32, Custom);
102 setOperationAction(ISD::SETCC, EVT::f32, Custom);
103 setOperationAction(ISD::SETCC, EVT::f64, Custom);
104 setOperationAction(ISD::BRCOND, EVT::Other, Custom);
105 setOperationAction(ISD::DYNAMIC_STACKALLOC, EVT::i32, Custom);
106 setOperationAction(ISD::FP_TO_SINT, EVT::i32, Custom);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000107
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +0000108 // We custom lower AND/OR to handle the case where the DAG contain 'ands/ors'
109 // with operands comming from setcc fp comparions. This is necessary since
110 // the result from these setcc are in a flag registers (FCR31).
Owen Andersone50ed302009-08-10 22:56:29 +0000111 setOperationAction(ISD::AND, EVT::i32, Custom);
112 setOperationAction(ISD::OR, EVT::i32, Custom);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000113
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000114 // Operations not directly supported by Mips.
Owen Andersone50ed302009-08-10 22:56:29 +0000115 setOperationAction(ISD::BR_JT, EVT::Other, Expand);
116 setOperationAction(ISD::BR_CC, EVT::Other, Expand);
117 setOperationAction(ISD::SELECT_CC, EVT::Other, Expand);
118 setOperationAction(ISD::UINT_TO_FP, EVT::i32, Expand);
119 setOperationAction(ISD::FP_TO_UINT, EVT::i32, Expand);
120 setOperationAction(ISD::SIGN_EXTEND_INREG, EVT::i1, Expand);
121 setOperationAction(ISD::CTPOP, EVT::i32, Expand);
122 setOperationAction(ISD::CTTZ, EVT::i32, Expand);
123 setOperationAction(ISD::ROTL, EVT::i32, Expand);
124 setOperationAction(ISD::ROTR, EVT::i32, Expand);
125 setOperationAction(ISD::SHL_PARTS, EVT::i32, Expand);
126 setOperationAction(ISD::SRA_PARTS, EVT::i32, Expand);
127 setOperationAction(ISD::SRL_PARTS, EVT::i32, Expand);
128 setOperationAction(ISD::FCOPYSIGN, EVT::f32, Expand);
129 setOperationAction(ISD::FCOPYSIGN, EVT::f64, Expand);
130 setOperationAction(ISD::FSIN, EVT::f32, Expand);
131 setOperationAction(ISD::FCOS, EVT::f32, Expand);
132 setOperationAction(ISD::FPOWI, EVT::f32, Expand);
133 setOperationAction(ISD::FPOW, EVT::f32, Expand);
134 setOperationAction(ISD::FLOG, EVT::f32, Expand);
135 setOperationAction(ISD::FLOG2, EVT::f32, Expand);
136 setOperationAction(ISD::FLOG10, EVT::f32, Expand);
137 setOperationAction(ISD::FEXP, EVT::f32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000138
139 // We don't have line number support yet.
Owen Andersone50ed302009-08-10 22:56:29 +0000140 setOperationAction(ISD::DBG_STOPPOINT, EVT::Other, Expand);
141 setOperationAction(ISD::DEBUG_LOC, EVT::Other, Expand);
142 setOperationAction(ISD::DBG_LABEL, EVT::Other, Expand);
143 setOperationAction(ISD::EH_LABEL, EVT::Other, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000144
145 // Use the default for now
Owen Andersone50ed302009-08-10 22:56:29 +0000146 setOperationAction(ISD::STACKSAVE, EVT::Other, Expand);
147 setOperationAction(ISD::STACKRESTORE, EVT::Other, Expand);
148 setOperationAction(ISD::MEMBARRIER, EVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000149
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000150 if (Subtarget->isSingleFloat())
Owen Andersone50ed302009-08-10 22:56:29 +0000151 setOperationAction(ISD::SELECT_CC, EVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000152
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000153 if (!Subtarget->hasSEInReg()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000154 setOperationAction(ISD::SIGN_EXTEND_INREG, EVT::i8, Expand);
155 setOperationAction(ISD::SIGN_EXTEND_INREG, EVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000156 }
157
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000158 if (!Subtarget->hasBitCount())
Owen Andersone50ed302009-08-10 22:56:29 +0000159 setOperationAction(ISD::CTLZ, EVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000160
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000161 if (!Subtarget->hasSwap())
Owen Andersone50ed302009-08-10 22:56:29 +0000162 setOperationAction(ISD::BSWAP, EVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000163
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000164 setStackPointerRegisterToSaveRestore(Mips::SP);
165 computeRegisterProperties();
166}
167
Owen Andersone50ed302009-08-10 22:56:29 +0000168EVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
169 return EVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000170}
171
Bill Wendlingb4202b82009-07-01 18:50:55 +0000172/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000173unsigned MipsTargetLowering::getFunctionAlignment(const Function *) const {
174 return 2;
175}
Scott Michel5b8f82e2008-03-10 15:42:14 +0000176
Dan Gohman475871a2008-07-27 21:46:04 +0000177SDValue MipsTargetLowering::
178LowerOperation(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000179{
180 switch (Op.getOpcode())
181 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000182 case ISD::AND: return LowerANDOR(Op, DAG);
183 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000184 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
185 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000186 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000187 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
188 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
189 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
190 case ISD::OR: return LowerANDOR(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000191 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000192 case ISD::SETCC: return LowerSETCC(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000193 }
Dan Gohman475871a2008-07-27 21:46:04 +0000194 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000195}
196
197//===----------------------------------------------------------------------===//
198// Lower helper functions
199//===----------------------------------------------------------------------===//
200
201// AddLiveIn - This helper function adds the specified physical register to the
202// MachineFunction as a live in value. It also creates a corresponding
203// virtual register for it.
204static unsigned
205AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
206{
207 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000208 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
209 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000210 return VReg;
211}
212
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000213// Get fp branch code (not opcode) from condition code.
214static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
215 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
216 return Mips::BRANCH_T;
217
218 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
219 return Mips::BRANCH_F;
220
221 return Mips::BRANCH_INVALID;
222}
223
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000224static unsigned FPBranchCodeToOpc(Mips::FPBranchCode BC) {
225 switch(BC) {
226 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000227 llvm_unreachable("Unknown branch code");
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000228 case Mips::BRANCH_T : return Mips::BC1T;
229 case Mips::BRANCH_F : return Mips::BC1F;
230 case Mips::BRANCH_TL : return Mips::BC1TL;
231 case Mips::BRANCH_FL : return Mips::BC1FL;
232 }
233}
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000234
235static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
236 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000237 default: llvm_unreachable("Unknown fp condition code!");
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000238 case ISD::SETEQ:
239 case ISD::SETOEQ: return Mips::FCOND_EQ;
240 case ISD::SETUNE: return Mips::FCOND_OGL;
241 case ISD::SETLT:
242 case ISD::SETOLT: return Mips::FCOND_OLT;
243 case ISD::SETGT:
244 case ISD::SETOGT: return Mips::FCOND_OGT;
245 case ISD::SETLE:
246 case ISD::SETOLE: return Mips::FCOND_OLE;
247 case ISD::SETGE:
248 case ISD::SETOGE: return Mips::FCOND_OGE;
249 case ISD::SETULT: return Mips::FCOND_ULT;
250 case ISD::SETULE: return Mips::FCOND_ULE;
251 case ISD::SETUGT: return Mips::FCOND_UGT;
252 case ISD::SETUGE: return Mips::FCOND_UGE;
253 case ISD::SETUO: return Mips::FCOND_UN;
254 case ISD::SETO: return Mips::FCOND_OR;
255 case ISD::SETNE:
256 case ISD::SETONE: return Mips::FCOND_NEQ;
257 case ISD::SETUEQ: return Mips::FCOND_UEQ;
258 }
259}
260
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000261MachineBasicBlock *
262MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +0000263 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000264 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
265 bool isFPCmp = false;
Dale Johannesen94817572009-02-13 02:34:39 +0000266 DebugLoc dl = MI->getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000267
268 switch (MI->getOpcode()) {
269 default: assert(false && "Unexpected instr type to insert");
270 case Mips::Select_FCC:
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000271 case Mips::Select_FCC_S32:
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000272 case Mips::Select_FCC_D32:
273 isFPCmp = true; // FALL THROUGH
274 case Mips::Select_CC:
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000275 case Mips::Select_CC_S32:
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000276 case Mips::Select_CC_D32: {
277 // To "insert" a SELECT_CC instruction, we actually have to insert the
278 // diamond control-flow pattern. The incoming instruction knows the
279 // destination vreg to set, the condition code register to branch on, the
280 // true/false values to select between, and a branch opcode to use.
281 const BasicBlock *LLVM_BB = BB->getBasicBlock();
282 MachineFunction::iterator It = BB;
283 ++It;
284
285 // thisMBB:
286 // ...
287 // TrueVal = ...
288 // setcc r1, r2, r3
289 // bNE r1, r0, copy1MBB
290 // fallthrough --> copy0MBB
291 MachineBasicBlock *thisMBB = BB;
292 MachineFunction *F = BB->getParent();
293 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
294 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
295
296 // Emit the right instruction according to the type of the operands compared
297 if (isFPCmp) {
298 // Find the condiction code present in the setcc operation.
299 Mips::CondCode CC = (Mips::CondCode)MI->getOperand(4).getImm();
300 // Get the branch opcode from the branch code.
301 unsigned Opc = FPBranchCodeToOpc(GetFPBranchCodeFromCond(CC));
Dale Johannesen94817572009-02-13 02:34:39 +0000302 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000303 } else
Dale Johannesen94817572009-02-13 02:34:39 +0000304 BuildMI(BB, dl, TII->get(Mips::BNE)).addReg(MI->getOperand(1).getReg())
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000305 .addReg(Mips::ZERO).addMBB(sinkMBB);
306
307 F->insert(It, copy0MBB);
308 F->insert(It, sinkMBB);
309 // Update machine-CFG edges by first adding all successors of the current
310 // block to the new block which will contain the Phi node for the select.
311 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
312 e = BB->succ_end(); i != e; ++i)
313 sinkMBB->addSuccessor(*i);
314 // Next, remove all successors of the current block, and add the true
315 // and fallthrough blocks as its successors.
316 while(!BB->succ_empty())
317 BB->removeSuccessor(BB->succ_begin());
318 BB->addSuccessor(copy0MBB);
319 BB->addSuccessor(sinkMBB);
320
321 // copy0MBB:
322 // %FalseValue = ...
323 // # fallthrough to sinkMBB
324 BB = copy0MBB;
325
326 // Update machine-CFG edges
327 BB->addSuccessor(sinkMBB);
328
329 // sinkMBB:
330 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
331 // ...
332 BB = sinkMBB;
Dale Johannesen94817572009-02-13 02:34:39 +0000333 BuildMI(BB, dl, TII->get(Mips::PHI), MI->getOperand(0).getReg())
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000334 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
335 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB);
336
337 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
338 return BB;
339 }
340 }
341}
342
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000343//===----------------------------------------------------------------------===//
344// Misc Lower Operation implementation
345//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000346
Dan Gohman475871a2008-07-27 21:46:04 +0000347SDValue MipsTargetLowering::
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000348LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG)
349{
350 if (!Subtarget->isMips1())
351 return Op;
352
353 MachineFunction &MF = DAG.getMachineFunction();
354 unsigned CCReg = AddLiveIn(MF, Mips::FCR31, Mips::CCRRegisterClass);
355
356 SDValue Chain = DAG.getEntryNode();
357 DebugLoc dl = Op.getDebugLoc();
358 SDValue Src = Op.getOperand(0);
359
360 // Set the condition register
Owen Andersone50ed302009-08-10 22:56:29 +0000361 SDValue CondReg = DAG.getCopyFromReg(Chain, dl, CCReg, EVT::i32);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000362 CondReg = DAG.getCopyToReg(Chain, dl, Mips::AT, CondReg);
Owen Andersone50ed302009-08-10 22:56:29 +0000363 CondReg = DAG.getCopyFromReg(CondReg, dl, Mips::AT, EVT::i32);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000364
Owen Andersone50ed302009-08-10 22:56:29 +0000365 SDValue Cst = DAG.getConstant(3, EVT::i32);
366 SDValue Or = DAG.getNode(ISD::OR, dl, EVT::i32, CondReg, Cst);
367 Cst = DAG.getConstant(2, EVT::i32);
368 SDValue Xor = DAG.getNode(ISD::XOR, dl, EVT::i32, Or, Cst);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000369
370 SDValue InFlag(0, 0);
371 CondReg = DAG.getCopyToReg(Chain, dl, Mips::FCR31, Xor, InFlag);
372
373 // Emit the round instruction and bit convert to integer
Owen Andersone50ed302009-08-10 22:56:29 +0000374 SDValue Trunc = DAG.getNode(MipsISD::FPRound, dl, EVT::f32,
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000375 Src, CondReg.getValue(1));
Owen Andersone50ed302009-08-10 22:56:29 +0000376 SDValue BitCvt = DAG.getNode(ISD::BIT_CONVERT, dl, EVT::i32, Trunc);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000377 return BitCvt;
378}
379
380SDValue MipsTargetLowering::
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000381LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG)
382{
383 SDValue Chain = Op.getOperand(0);
384 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +0000385 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000386
387 // Get a reference from Mips stack pointer
Owen Andersone50ed302009-08-10 22:56:29 +0000388 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, EVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000389
390 // Subtract the dynamic size from the actual stack size to
391 // obtain the new stack size.
Owen Andersone50ed302009-08-10 22:56:29 +0000392 SDValue Sub = DAG.getNode(ISD::SUB, dl, EVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000393
394 // The Sub result contains the new stack start address, so it
395 // must be placed in the stack pointer register.
Dale Johannesena05dca42009-02-04 23:02:30 +0000396 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000397
398 // This node always has two return values: a new stack pointer
399 // value and a chain
400 SDValue Ops[2] = { Sub, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000401 return DAG.getMergeValues(Ops, 2, dl);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000402}
403
404SDValue MipsTargetLowering::
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +0000405LowerANDOR(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000406{
407 SDValue LHS = Op.getOperand(0);
408 SDValue RHS = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +0000409 DebugLoc dl = Op.getDebugLoc();
410
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000411 if (LHS.getOpcode() != MipsISD::FPCmp || RHS.getOpcode() != MipsISD::FPCmp)
412 return Op;
413
Owen Andersone50ed302009-08-10 22:56:29 +0000414 SDValue True = DAG.getConstant(1, EVT::i32);
415 SDValue False = DAG.getConstant(0, EVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000416
Dale Johannesende064702009-02-06 21:50:26 +0000417 SDValue LSEL = DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000418 LHS, True, False, LHS.getOperand(2));
Dale Johannesende064702009-02-06 21:50:26 +0000419 SDValue RSEL = DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000420 RHS, True, False, RHS.getOperand(2));
421
Owen Andersone50ed302009-08-10 22:56:29 +0000422 return DAG.getNode(Op.getOpcode(), dl, EVT::i32, LSEL, RSEL);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000423}
424
425SDValue MipsTargetLowering::
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000426LowerBRCOND(SDValue Op, SelectionDAG &DAG)
427{
428 // The first operand is the chain, the second is the condition, the third is
429 // the block to branch to if the condition is true.
430 SDValue Chain = Op.getOperand(0);
431 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000432 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000433
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000434 if (Op.getOperand(1).getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000435 return Op;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000436
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000437 SDValue CondRes = Op.getOperand(1);
438 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000439 Mips::CondCode CC =
440 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Owen Andersone50ed302009-08-10 22:56:29 +0000441 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), EVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000442
Dale Johannesende064702009-02-06 21:50:26 +0000443 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000444 Dest, CondRes);
445}
446
447SDValue MipsTargetLowering::
448LowerSETCC(SDValue Op, SelectionDAG &DAG)
449{
450 // The operands to this are the left and right operands to compare (ops #0,
451 // and #1) and the condition code to compare them with (op #2) as a
452 // CondCodeSDNode.
453 SDValue LHS = Op.getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +0000454 SDValue RHS = Op.getOperand(1);
455 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000456
457 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
458
Dale Johannesende064702009-02-06 21:50:26 +0000459 return DAG.getNode(MipsISD::FPCmp, dl, Op.getValueType(), LHS, RHS,
Owen Andersone50ed302009-08-10 22:56:29 +0000460 DAG.getConstant(FPCondCCodeToFCC(CC), EVT::i32));
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000461}
462
463SDValue MipsTargetLowering::
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000464LowerSELECT(SDValue Op, SelectionDAG &DAG)
465{
466 SDValue Cond = Op.getOperand(0);
467 SDValue True = Op.getOperand(1);
468 SDValue False = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000469 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000470
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000471 // if the incomming condition comes from a integer compare, the select
472 // operation must be SelectCC or a conditional move if the subtarget
473 // supports it.
474 if (Cond.getOpcode() != MipsISD::FPCmp) {
475 if (Subtarget->hasCondMov() && !True.getValueType().isFloatingPoint())
476 return Op;
Dale Johannesende064702009-02-06 21:50:26 +0000477 return DAG.getNode(MipsISD::SelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000478 Cond, True, False);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000479 }
480
481 // if the incomming condition comes from fpcmp, the select
482 // operation must use FPSelectCC.
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000483 SDValue CCNode = Cond.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000484 return DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000485 Cond, True, False, CCNode);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000486}
487
488SDValue MipsTargetLowering::
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000489LowerGlobalAddress(SDValue Op, SelectionDAG &DAG)
490{
Dale Johannesende064702009-02-06 21:50:26 +0000491 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000492 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000493 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Owen Andersone50ed302009-08-10 22:56:29 +0000494 SDValue GA = DAG.getTargetGlobalAddress(GV, EVT::i32);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000495
Eli Friedmane2c74082009-08-03 02:22:28 +0000496 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000497 // %hi/%lo relocation
Owen Andersone50ed302009-08-10 22:56:29 +0000498 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, EVT::i32, GA);
499 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, EVT::i32, GA);
500 return DAG.getNode(ISD::ADD, dl, EVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000501
502 } else { // Abicall relocations, TODO: make this cleaner.
Owen Andersone50ed302009-08-10 22:56:29 +0000503 SDValue ResNode = DAG.getLoad(EVT::i32, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +0000504 DAG.getEntryNode(), GA, NULL, 0);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000505 // On functions and global targets not internal linked only
506 // a load from got/GP is necessary for PIC to work.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000507 if (!GV->hasLocalLinkage() || isa<Function>(GV))
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000508 return ResNode;
Owen Andersone50ed302009-08-10 22:56:29 +0000509 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, EVT::i32, GA);
510 return DAG.getNode(ISD::ADD, dl, EVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000511 }
512
Torok Edwinc23197a2009-07-14 16:55:14 +0000513 llvm_unreachable("Dont know how to handle GlobalAddress");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000514 return SDValue(0,0);
515}
516
517SDValue MipsTargetLowering::
518LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG)
519{
Torok Edwinc23197a2009-07-14 16:55:14 +0000520 llvm_unreachable("TLS not implemented for MIPS.");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000521 return SDValue(); // Not reached
522}
523
524SDValue MipsTargetLowering::
Dan Gohman475871a2008-07-27 21:46:04 +0000525LowerJumpTable(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000526{
Dan Gohman475871a2008-07-27 21:46:04 +0000527 SDValue ResNode;
528 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +0000529 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000530 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000531
Owen Andersone50ed302009-08-10 22:56:29 +0000532 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000533 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +0000534 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000535
536 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Owen Andersone50ed302009-08-10 22:56:29 +0000537 SDVTList VTs = DAG.getVTList(EVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000538 SDValue Ops[] = { JTI };
Dan Gohmanfc166572009-04-09 23:54:40 +0000539 HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, Ops, 1);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000540 } else // Emit Load from Global Pointer
Owen Andersone50ed302009-08-10 22:56:29 +0000541 HiPart = DAG.getLoad(EVT::i32, dl, DAG.getEntryNode(), JTI, NULL, 0);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000542
Owen Andersone50ed302009-08-10 22:56:29 +0000543 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, EVT::i32, JTI);
544 ResNode = DAG.getNode(ISD::ADD, dl, EVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000545
546 return ResNode;
547}
548
Dan Gohman475871a2008-07-27 21:46:04 +0000549SDValue MipsTargetLowering::
550LowerConstantPool(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000551{
Dan Gohman475871a2008-07-27 21:46:04 +0000552 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000553 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
554 Constant *C = N->getConstVal();
Owen Andersone50ed302009-08-10 22:56:29 +0000555 SDValue CP = DAG.getTargetConstantPool(C, EVT::i32, N->getAlignment());
Dale Johannesende064702009-02-06 21:50:26 +0000556 // FIXME there isn't actually debug info here
557 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000558
559 // gp_rel relocation
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000560 // FIXME: we should reference the constant pool using small data sections,
561 // but the asm printer currently doens't support this feature without
562 // hacking it. This feature should come soon so we can uncomment the
563 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +0000564 //if (IsInSmallSection(C->getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +0000565 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, EVT::i32, CP);
566 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(EVT::i32);
567 // ResNode = DAG.getNode(ISD::ADD, EVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000568 //} else { // %hi/%lo relocation
Owen Andersone50ed302009-08-10 22:56:29 +0000569 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, EVT::i32, CP);
570 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, EVT::i32, CP);
571 ResNode = DAG.getNode(ISD::ADD, dl, EVT::i32, HiPart, Lo);
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000572 //}
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000573
574 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000575}
576
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000577//===----------------------------------------------------------------------===//
578// Calling Convention Implementation
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000579//===----------------------------------------------------------------------===//
580
581#include "MipsGenCallingConv.inc"
582
583//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000584// TODO: Implement a generic logic using tblgen that can support this.
585// Mips O32 ABI rules:
586// ---
587// i32 - Passed in A0, A1, A2, A3 and stack
588// f32 - Only passed in f32 registers if no int reg has been used yet to hold
589// an argument. Otherwise, passed in A1, A2, A3 and stack.
590// f64 - Only passed in two aliased f32 registers if no int reg has been used
591// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
592// not used, it must be shadowed. If only A3 is avaiable, shadow it and
593// go to stack.
594//===----------------------------------------------------------------------===//
595
Owen Andersone50ed302009-08-10 22:56:29 +0000596static bool CC_MipsO32(unsigned ValNo, EVT ValVT,
597 EVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000598 ISD::ArgFlagsTy ArgFlags, CCState &State) {
599
600 static const unsigned IntRegsSize=4, FloatRegsSize=2;
601
602 static const unsigned IntRegs[] = {
603 Mips::A0, Mips::A1, Mips::A2, Mips::A3
604 };
605 static const unsigned F32Regs[] = {
606 Mips::F12, Mips::F14
607 };
608 static const unsigned F64Regs[] = {
609 Mips::D6, Mips::D7
610 };
611
612 unsigned Reg=0;
613 unsigned UnallocIntReg = State.getFirstUnallocated(IntRegs, IntRegsSize);
614 bool IntRegUsed = (IntRegs[UnallocIntReg] != (unsigned (Mips::A0)));
615
616 // Promote i8 and i16
Owen Andersone50ed302009-08-10 22:56:29 +0000617 if (LocVT == EVT::i8 || LocVT == EVT::i16) {
618 LocVT = EVT::i32;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000619 if (ArgFlags.isSExt())
620 LocInfo = CCValAssign::SExt;
621 else if (ArgFlags.isZExt())
622 LocInfo = CCValAssign::ZExt;
623 else
624 LocInfo = CCValAssign::AExt;
625 }
626
Owen Andersone50ed302009-08-10 22:56:29 +0000627 if (ValVT == EVT::i32 || (ValVT == EVT::f32 && IntRegUsed)) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000628 Reg = State.AllocateReg(IntRegs, IntRegsSize);
629 IntRegUsed = true;
Owen Andersone50ed302009-08-10 22:56:29 +0000630 LocVT = EVT::i32;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000631 }
632
633 if (ValVT.isFloatingPoint() && !IntRegUsed) {
Owen Andersone50ed302009-08-10 22:56:29 +0000634 if (ValVT == EVT::f32)
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000635 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
636 else
637 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
638 }
639
Owen Andersone50ed302009-08-10 22:56:29 +0000640 if (ValVT == EVT::f64 && IntRegUsed) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000641 if (UnallocIntReg != IntRegsSize) {
642 // If we hit register A3 as the first not allocated, we must
643 // mark it as allocated (shadow) and use the stack instead.
644 if (IntRegs[UnallocIntReg] != (unsigned (Mips::A3)))
645 Reg = Mips::A2;
646 for (;UnallocIntReg < IntRegsSize; ++UnallocIntReg)
647 State.AllocateReg(UnallocIntReg);
648 }
Owen Andersone50ed302009-08-10 22:56:29 +0000649 LocVT = EVT::i32;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000650 }
651
652 if (!Reg) {
653 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
654 unsigned Offset = State.AllocateStack(SizeInBytes, SizeInBytes);
655 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
656 } else
657 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
658
659 return false; // CC must always match
660}
661
662//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +0000663// Call Calling Convention Implementation
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000664//===----------------------------------------------------------------------===//
665
Dan Gohman98ca4f22009-08-05 01:29:28 +0000666/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +0000667/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000668/// TODO: isVarArg, isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000669SDValue
670MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
671 unsigned CallConv, bool isVarArg,
672 bool isTailCall,
673 const SmallVectorImpl<ISD::OutputArg> &Outs,
674 const SmallVectorImpl<ISD::InputArg> &Ins,
675 DebugLoc dl, SelectionDAG &DAG,
676 SmallVectorImpl<SDValue> &InVals) {
677
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000678 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000679 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000680
681 // Analyze operands of the call, assigning locations to each operand.
682 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000683 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
684 *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000685
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000686 // To meet O32 ABI, Mips must always allocate 16 bytes on
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000687 // the stack (even if less than 4 are used as arguments)
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000688 if (Subtarget->isABI_O32()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000689 int VTsize = EVT(EVT::i32).getSizeInBits()/8;
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000690 MFI->CreateFixedObject(VTsize, (VTsize*3));
Dan Gohman98ca4f22009-08-05 01:29:28 +0000691 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000692 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +0000693 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000694
695 // Get a count of how many bytes are to be pushed on the stack.
696 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnere563bbc2008-10-11 22:08:30 +0000697 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000698
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000699 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +0000700 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
701 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000702
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000703 // First/LastArgStackLoc contains the first/last
704 // "at stack" argument location.
705 int LastArgStackLoc = 0;
706 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000707
708 // Walk the register/memloc assignments, inserting copies/loads.
709 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000710 SDValue Arg = Outs[i].Val;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000711 CCValAssign &VA = ArgLocs[i];
712
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000713 // Promote the value if needed.
714 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000715 default: llvm_unreachable("Unknown loc info!");
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000716 case CCValAssign::Full:
717 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000718 if (VA.getValVT() == EVT::f32 && VA.getLocVT() == EVT::i32)
719 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, EVT::i32, Arg);
720 if (VA.getValVT() == EVT::f64 && VA.getLocVT() == EVT::i32) {
721 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, EVT::i64, Arg);
722 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, EVT::i32, Arg,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000723 DAG.getConstant(0, getPointerTy()));
Owen Andersone50ed302009-08-10 22:56:29 +0000724 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, EVT::i32, Arg,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000725 DAG.getConstant(1, getPointerTy()));
726 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
727 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
728 continue;
729 }
730 }
731 break;
Chris Lattnere0b12152008-03-17 06:57:02 +0000732 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000733 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000734 break;
735 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000736 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000737 break;
738 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000739 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000740 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000741 }
742
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000743 // Arguments that can be passed on register must be kept at
744 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000745 if (VA.isRegLoc()) {
746 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +0000747 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000748 }
Chris Lattnere0b12152008-03-17 06:57:02 +0000749
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000750 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +0000751 assert(VA.isMemLoc());
752
753 // Create the frame index object for this incoming parameter
754 // This guarantees that when allocating Local Area the firsts
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000755 // 16 bytes which are alwayes reserved won't be overwritten
756 // if O32 ABI is used. For EABI the first address is zero.
757 LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000758 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000759 LastArgStackLoc);
Chris Lattnere0b12152008-03-17 06:57:02 +0000760
Dan Gohman475871a2008-07-27 21:46:04 +0000761 SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +0000762
763 // emit ISD::STORE whichs stores the
764 // parameter value to a stack Location
Dale Johannesen33c960f2009-02-04 20:06:27 +0000765 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000766 }
767
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000768 // Transform all store nodes into one single node because all store
769 // nodes are independent of each other.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000770 if (!MemOpChains.empty())
Owen Andersone50ed302009-08-10 22:56:29 +0000771 Chain = DAG.getNode(ISD::TokenFactor, dl, EVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000772 &MemOpChains[0], MemOpChains.size());
773
774 // Build a sequence of copy-to-reg nodes chained together with token
775 // chain and flag operands which copy the outgoing args into registers.
776 // The InFlag in necessary since all emited instructions must be
777 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000778 SDValue InFlag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000779 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000780 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000781 RegsToPass[i].second, InFlag);
782 InFlag = Chain.getValue(1);
783 }
784
Bill Wendling056292f2008-09-16 21:48:12 +0000785 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
786 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
787 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000788 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000789 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +0000790 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
791 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
792
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000793 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
794 // = Chain, Callee, Reg#1, Reg#2, ...
795 //
796 // Returns a chain & a flag for retval copy to use.
Owen Andersone50ed302009-08-10 22:56:29 +0000797 SDVTList NodeTys = DAG.getVTList(EVT::Other, EVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000798 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000799 Ops.push_back(Chain);
800 Ops.push_back(Callee);
801
802 // Add argument registers to the end of the list so that they are
803 // known live into the call.
804 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
805 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
806 RegsToPass[i].second.getValueType()));
807
Gabor Greifba36cb52008-08-28 21:40:38 +0000808 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000809 Ops.push_back(InFlag);
810
Dale Johannesen33c960f2009-02-04 20:06:27 +0000811 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000812 InFlag = Chain.getValue(1);
813
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000814 // Create the CALLSEQ_END node.
Chris Lattnere563bbc2008-10-11 22:08:30 +0000815 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
816 DAG.getIntPtrConstant(0, true), InFlag);
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000817 InFlag = Chain.getValue(1);
818
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000819 // Create a stack location to hold GP when PIC is used. This stack
820 // location is used on function prologue to save GP and also after all
821 // emited CALL's to restore GP.
822 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000823 // Function can have an arbitrary number of calls, so
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000824 // hold the LastArgStackLoc with the biggest offset.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000825 int FI;
826 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000827 if (LastArgStackLoc >= MipsFI->getGPStackOffset()) {
828 LastArgStackLoc = (!LastArgStackLoc) ? (16) : (LastArgStackLoc+4);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000829 // Create the frame index only once. SPOffset here can be anything
830 // (this will be fixed on processFunctionBeforeFrameFinalized)
831 if (MipsFI->getGPStackOffset() == -1) {
832 FI = MFI->CreateFixedObject(4, 0);
833 MipsFI->setGPFI(FI);
834 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000835 MipsFI->setGPStackOffset(LastArgStackLoc);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000836 }
837
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000838 // Reload GP value.
839 FI = MipsFI->getGPFI();
Dan Gohman475871a2008-07-27 21:46:04 +0000840 SDValue FIN = DAG.getFrameIndex(FI,getPointerTy());
Owen Andersone50ed302009-08-10 22:56:29 +0000841 SDValue GPLoad = DAG.getLoad(EVT::i32, dl, Chain, FIN, NULL, 0);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000842 Chain = GPLoad.getValue(1);
Owen Andersone50ed302009-08-10 22:56:29 +0000843 Chain = DAG.getCopyToReg(Chain, dl, DAG.getRegister(Mips::GP, EVT::i32),
Dan Gohman475871a2008-07-27 21:46:04 +0000844 GPLoad, SDValue(0,0));
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000845 InFlag = Chain.getValue(1);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000846 }
847
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000848 // Handle result values, copying them out of physregs into vregs that we
849 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000850 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
851 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000852}
853
Dan Gohman98ca4f22009-08-05 01:29:28 +0000854/// LowerCallResult - Lower the result values of a call into the
855/// appropriate copies out of appropriate physical registers.
856SDValue
857MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
858 unsigned CallConv, bool isVarArg,
859 const SmallVectorImpl<ISD::InputArg> &Ins,
860 DebugLoc dl, SelectionDAG &DAG,
861 SmallVectorImpl<SDValue> &InVals) {
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000862
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000863 // Assign locations to each value returned by this call.
864 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000865 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000866 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000867
Dan Gohman98ca4f22009-08-05 01:29:28 +0000868 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000869
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000870 // Copy all of the result registers out of their specified physreg.
871 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000872 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +0000873 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000874 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000875 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000876 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000877
Dan Gohman98ca4f22009-08-05 01:29:28 +0000878 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000879}
880
881//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +0000882// Formal Arguments Calling Convention Implementation
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000883//===----------------------------------------------------------------------===//
884
Dan Gohman98ca4f22009-08-05 01:29:28 +0000885/// LowerFormalArguments - transform physical registers into
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000886/// virtual registers and generate load operations for
887/// arguments places on the stack.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000888/// TODO: isVarArg
Dan Gohman98ca4f22009-08-05 01:29:28 +0000889SDValue
890MipsTargetLowering::LowerFormalArguments(SDValue Chain,
891 unsigned CallConv, bool isVarArg,
892 const SmallVectorImpl<ISD::InputArg>
893 &Ins,
894 DebugLoc dl, SelectionDAG &DAG,
895 SmallVectorImpl<SDValue> &InVals) {
896
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000897 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000898 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000899 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000900
901 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000902
903 // Assign locations to all of the incoming arguments.
904 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000905 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
906 ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000907
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000908 if (Subtarget->isABI_O32())
Dan Gohman98ca4f22009-08-05 01:29:28 +0000909 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000910 else
Dan Gohman98ca4f22009-08-05 01:29:28 +0000911 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000912
Dan Gohman475871a2008-07-27 21:46:04 +0000913 SDValue StackPtr;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000914
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000915 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
916
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000917 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000918 CCValAssign &VA = ArgLocs[i];
919
920 // Arguments stored on registers
921 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000922 EVT RegVT = VA.getLocVT();
Bill Wendling06b8c192008-07-09 05:55:53 +0000923 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000924
Owen Andersone50ed302009-08-10 22:56:29 +0000925 if (RegVT == EVT::i32)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000926 RC = Mips::CPURegsRegisterClass;
Owen Andersone50ed302009-08-10 22:56:29 +0000927 else if (RegVT == EVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000928 RC = Mips::FGR32RegisterClass;
Owen Andersone50ed302009-08-10 22:56:29 +0000929 else if (RegVT == EVT::f64) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000930 if (!Subtarget->isSingleFloat())
931 RC = Mips::AFGR64RegisterClass;
932 } else
Dan Gohman98ca4f22009-08-05 01:29:28 +0000933 llvm_unreachable("RegVT not supported by LowerFormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000934
935 // Transform the arguments stored on
936 // physical registers into virtual ones
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000937 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000938 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000939
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000940 // If this is an 8 or 16-bit value, it has been passed promoted
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000941 // to 32 bits. Insert an assert[sz]ext to capture this, then
942 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000943 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +0000944 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000945 if (VA.getLocInfo() == CCValAssign::SExt)
946 Opcode = ISD::AssertSext;
947 else if (VA.getLocInfo() == CCValAssign::ZExt)
948 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +0000949 if (Opcode)
950 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
951 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +0000952 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000953 }
954
955 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
956 if (Subtarget->isABI_O32()) {
Owen Andersone50ed302009-08-10 22:56:29 +0000957 if (RegVT == EVT::i32 && VA.getValVT() == EVT::f32)
958 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, EVT::f32, ArgValue);
959 if (RegVT == EVT::i32 && VA.getValVT() == EVT::f64) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000960 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
961 VA.getLocReg()+1, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000962 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Owen Andersone50ed302009-08-10 22:56:29 +0000963 SDValue Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT::f32, ArgValue);
964 SDValue Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT::f32, ArgValue2);
965 ArgValue = DAG.getNode(ISD::BUILD_PAIR, dl, EVT::f64, Lo, Hi);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000966 }
967 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000968
Dan Gohman98ca4f22009-08-05 01:29:28 +0000969 InVals.push_back(ArgValue);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000970
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000971 // To meet ABI, when VARARGS are passed on registers, the registers
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000972 // must have their values written to the caller stack frame.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000973 if ((isVarArg) && (Subtarget->isABI_O32())) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000974 if (StackPtr.getNode() == 0)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000975 StackPtr = DAG.getRegister(StackReg, getPointerTy());
976
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000977 // The stack pointer offset is relative to the caller stack frame.
978 // Since the real stack size is unknown here, a negative SPOffset
979 // is used so there's a way to adjust these offsets when the stack
980 // size get known (on EliminateFrameIndex). A dummy SPOffset is
981 // used instead of a direct negative address (which is recorded to
982 // be used on emitPrologue) to avoid mis-calc of the first stack
983 // offset on PEI::calculateFrameObjectOffsets.
984 // Arguments are always 32-bit.
985 int FI = MFI->CreateFixedObject(4, 0);
986 MipsFI->recordStoreVarArgsFI(FI, -(4+(i*4)));
Dan Gohman475871a2008-07-27 21:46:04 +0000987 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000988
989 // emit ISD::STORE whichs stores the
990 // parameter value to a stack Location
Dan Gohman98ca4f22009-08-05 01:29:28 +0000991 InVals.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff, NULL, 0));
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000992 }
993
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000994 } else { // VA.isRegLoc()
995
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000996 // sanity check
997 assert(VA.isMemLoc());
998
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000999 // The stack pointer offset is relative to the caller stack frame.
1000 // Since the real stack size is unknown here, a negative SPOffset
1001 // is used so there's a way to adjust these offsets when the stack
1002 // size get known (on EliminateFrameIndex). A dummy SPOffset is
1003 // used instead of a direct negative address (which is recorded to
1004 // be used on emitPrologue) to avoid mis-calc of the first stack
1005 // offset on PEI::calculateFrameObjectOffsets.
1006 // Arguments are always 32-bit.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001007 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1008 int FI = MFI->CreateFixedObject(ArgSize, 0);
1009 MipsFI->recordLoadArgsFI(FI, -(ArgSize+
1010 (FirstStackArgLoc + VA.getLocMemOffset())));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001011
1012 // Create load nodes to retrieve arguments from the stack
Dan Gohman475871a2008-07-27 21:46:04 +00001013 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001014 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, NULL, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001015 }
1016 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001017
1018 // The mips ABIs for returning structs by value requires that we copy
1019 // the sret argument into $v0 for the return. Save the argument into
1020 // a virtual register so that we can access it from the return points.
1021 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1022 unsigned Reg = MipsFI->getSRetReturnReg();
1023 if (!Reg) {
Owen Andersone50ed302009-08-10 22:56:29 +00001024 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(EVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001025 MipsFI->setSRetReturnReg(Reg);
1026 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001027 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Andersone50ed302009-08-10 22:56:29 +00001028 Chain = DAG.getNode(ISD::TokenFactor, dl, EVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001029 }
1030
Dan Gohman98ca4f22009-08-05 01:29:28 +00001031 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001032}
1033
1034//===----------------------------------------------------------------------===//
1035// Return Value Calling Convention Implementation
1036//===----------------------------------------------------------------------===//
1037
Dan Gohman98ca4f22009-08-05 01:29:28 +00001038SDValue
1039MipsTargetLowering::LowerReturn(SDValue Chain,
1040 unsigned CallConv, bool isVarArg,
1041 const SmallVectorImpl<ISD::OutputArg> &Outs,
1042 DebugLoc dl, SelectionDAG &DAG) {
1043
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001044 // CCValAssign - represent the assignment of
1045 // the return value to a location
1046 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001047
1048 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001049 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1050 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001051
Dan Gohman98ca4f22009-08-05 01:29:28 +00001052 // Analize return values.
1053 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001054
1055 // If this is the first return lowered for this function, add
1056 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001057 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001058 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001059 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +00001060 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001061 }
1062
Dan Gohman475871a2008-07-27 21:46:04 +00001063 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001064
1065 // Copy the result values into the output registers.
1066 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1067 CCValAssign &VA = RVLocs[i];
1068 assert(VA.isRegLoc() && "Can only return in registers!");
1069
Dale Johannesena05dca42009-02-04 23:02:30 +00001070 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001071 Outs[i].Val, Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001072
1073 // guarantee that all emitted copies are
1074 // stuck together, avoiding something bad
1075 Flag = Chain.getValue(1);
1076 }
1077
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001078 // The mips ABIs for returning structs by value requires that we copy
1079 // the sret argument into $v0 for the return. We saved the argument into
1080 // a virtual register in the entry block, so now we copy the value out
1081 // and into $v0.
1082 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1083 MachineFunction &MF = DAG.getMachineFunction();
1084 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1085 unsigned Reg = MipsFI->getSRetReturnReg();
1086
1087 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00001088 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00001089 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001090
Dale Johannesena05dca42009-02-04 23:02:30 +00001091 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001092 Flag = Chain.getValue(1);
1093 }
1094
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001095 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00001096 if (Flag.getNode())
Owen Andersone50ed302009-08-10 22:56:29 +00001097 return DAG.getNode(MipsISD::Ret, dl, EVT::Other,
1098 Chain, DAG.getRegister(Mips::RA, EVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001099 else // Return Void
Owen Andersone50ed302009-08-10 22:56:29 +00001100 return DAG.getNode(MipsISD::Ret, dl, EVT::Other,
1101 Chain, DAG.getRegister(Mips::RA, EVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001102}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001103
1104//===----------------------------------------------------------------------===//
1105// Mips Inline Assembly Support
1106//===----------------------------------------------------------------------===//
1107
1108/// getConstraintType - Given a constraint letter, return the type of
1109/// constraint it is for this target.
1110MipsTargetLowering::ConstraintType MipsTargetLowering::
1111getConstraintType(const std::string &Constraint) const
1112{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001113 // Mips specific constrainy
1114 // GCC config/mips/constraints.md
1115 //
1116 // 'd' : An address register. Equivalent to r
1117 // unless generating MIPS16 code.
1118 // 'y' : Equivalent to r; retained for
1119 // backwards compatibility.
Bruno Cardoso Lopes7b76da12008-07-09 04:45:36 +00001120 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001121 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001122 switch (Constraint[0]) {
1123 default : break;
1124 case 'd':
1125 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001126 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001127 return C_RegisterClass;
1128 break;
1129 }
1130 }
1131 return TargetLowering::getConstraintType(Constraint);
1132}
1133
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001134/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
1135/// return a list of registers that can be used to satisfy the constraint.
1136/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001137std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00001138getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001139{
1140 if (Constraint.size() == 1) {
1141 switch (Constraint[0]) {
1142 case 'r':
1143 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001144 case 'f':
Owen Andersone50ed302009-08-10 22:56:29 +00001145 if (VT == EVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001146 return std::make_pair(0U, Mips::FGR32RegisterClass);
Owen Andersone50ed302009-08-10 22:56:29 +00001147 if (VT == EVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001148 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1149 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001150 }
1151 }
1152 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1153}
1154
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001155/// Given a register class constraint, like 'r', if this corresponds directly
1156/// to an LLVM register class, return a register of 0 and the register class
1157/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001158std::vector<unsigned> MipsTargetLowering::
1159getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001160 EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001161{
1162 if (Constraint.size() != 1)
1163 return std::vector<unsigned>();
1164
1165 switch (Constraint[0]) {
1166 default : break;
1167 case 'r':
1168 // GCC Mips Constraint Letters
1169 case 'd':
1170 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001171 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
1172 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
1173 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
1174 Mips::T8, 0);
1175
1176 case 'f':
Owen Andersone50ed302009-08-10 22:56:29 +00001177 if (VT == EVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001178 if (Subtarget->isSingleFloat())
1179 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
1180 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
1181 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
1182 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
1183 Mips::F30, Mips::F31, 0);
1184 else
1185 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
1186 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
1187 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +00001188 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001189
Owen Andersone50ed302009-08-10 22:56:29 +00001190 if (VT == EVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001191 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1192 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
1193 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
1194 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001195 }
1196 return std::vector<unsigned>();
1197}
Dan Gohman6520e202008-10-18 02:06:02 +00001198
1199bool
1200MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1201 // The Mips target isn't yet aware of offsets.
1202 return false;
1203}