blob: ab3f144df2a4ca7463de546540437f4a64cb8c78 [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
68 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000069 addRegisterClass(MVT::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())
74 addRegisterClass(MVT::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
Evan Cheng03294662008-10-14 21:26:46 +000080 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
81 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
82 setLoadExtAction(ISD::SEXTLOAD, MVT::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
Eli Friedman10a36592009-07-17 02:28:12 +000085 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Eli Friedman6055a6a2009-07-17 04:07:24 +000086 setTruncStoreAction(MVT::f64, MVT::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.
92 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
93
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +000094 // Mips Custom Operations
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +000095 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
96 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
97 setOperationAction(ISD::RET, MVT::Other, Custom);
98 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
99 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
100 setOperationAction(ISD::SELECT, MVT::f32, Custom);
Eli Friedman6314ac22009-06-16 06:40:59 +0000101 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000102 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000103 setOperationAction(ISD::SETCC, MVT::f32, Custom);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000104 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000105 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
106 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000107 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000108
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +0000109 // We custom lower AND/OR to handle the case where the DAG contain 'ands/ors'
110 // with operands comming from setcc fp comparions. This is necessary since
111 // the result from these setcc are in a flag registers (FCR31).
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000112 setOperationAction(ISD::AND, MVT::i32, Custom);
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +0000113 setOperationAction(ISD::OR, MVT::i32, Custom);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000114
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000115 // Operations not directly supported by Mips.
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000116 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
117 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
118 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000119 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
120 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
121 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000122 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
123 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000124 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000125 setOperationAction(ISD::ROTR, MVT::i32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000126 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
127 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
128 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Bruno Cardoso Lopes7bd71822008-07-31 18:50:54 +0000129 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Eli Friedman6314ac22009-06-16 06:40:59 +0000130 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000131 setOperationAction(ISD::FSIN, MVT::f32, Expand);
132 setOperationAction(ISD::FCOS, MVT::f32, Expand);
133 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
134 setOperationAction(ISD::FPOW, MVT::f32, Expand);
135 setOperationAction(ISD::FLOG, MVT::f32, Expand);
136 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
137 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
138 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000139
140 // We don't have line number support yet.
141 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
142 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
143 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
144 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
145
146 // Use the default for now
147 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
148 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
149 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000150
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000151 if (Subtarget->isSingleFloat())
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000152 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000153
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000154 if (!Subtarget->hasSEInReg()) {
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000155 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000156 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
157 }
158
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000159 if (!Subtarget->hasBitCount())
160 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
161
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000162 if (!Subtarget->hasSwap())
163 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
164
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000165 setStackPointerRegisterToSaveRestore(Mips::SP);
166 computeRegisterProperties();
167}
168
Duncan Sands5480c042009-01-01 15:52:00 +0000169MVT MipsTargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000170 return MVT::i32;
171}
172
Bill Wendlingb4202b82009-07-01 18:50:55 +0000173/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000174unsigned MipsTargetLowering::getFunctionAlignment(const Function *) const {
175 return 2;
176}
Scott Michel5b8f82e2008-03-10 15:42:14 +0000177
Dan Gohman475871a2008-07-27 21:46:04 +0000178SDValue MipsTargetLowering::
179LowerOperation(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000180{
181 switch (Op.getOpcode())
182 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000183 case ISD::AND: return LowerANDOR(Op, DAG);
184 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
185 case ISD::CALL: return LowerCALL(Op, DAG);
186 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
187 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
188 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000189 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000190 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
191 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
192 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
193 case ISD::OR: return LowerANDOR(Op, DAG);
194 case ISD::RET: return LowerRET(Op, DAG);
195 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000196 case ISD::SETCC: return LowerSETCC(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000197 }
Dan Gohman475871a2008-07-27 21:46:04 +0000198 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000199}
200
201//===----------------------------------------------------------------------===//
202// Lower helper functions
203//===----------------------------------------------------------------------===//
204
205// AddLiveIn - This helper function adds the specified physical register to the
206// MachineFunction as a live in value. It also creates a corresponding
207// virtual register for it.
208static unsigned
209AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
210{
211 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000212 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
213 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000214 return VReg;
215}
216
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000217// Get fp branch code (not opcode) from condition code.
218static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
219 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
220 return Mips::BRANCH_T;
221
222 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
223 return Mips::BRANCH_F;
224
225 return Mips::BRANCH_INVALID;
226}
227
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000228static unsigned FPBranchCodeToOpc(Mips::FPBranchCode BC) {
229 switch(BC) {
230 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000231 llvm_unreachable("Unknown branch code");
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000232 case Mips::BRANCH_T : return Mips::BC1T;
233 case Mips::BRANCH_F : return Mips::BC1F;
234 case Mips::BRANCH_TL : return Mips::BC1TL;
235 case Mips::BRANCH_FL : return Mips::BC1FL;
236 }
237}
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000238
239static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
240 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000241 default: llvm_unreachable("Unknown fp condition code!");
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000242 case ISD::SETEQ:
243 case ISD::SETOEQ: return Mips::FCOND_EQ;
244 case ISD::SETUNE: return Mips::FCOND_OGL;
245 case ISD::SETLT:
246 case ISD::SETOLT: return Mips::FCOND_OLT;
247 case ISD::SETGT:
248 case ISD::SETOGT: return Mips::FCOND_OGT;
249 case ISD::SETLE:
250 case ISD::SETOLE: return Mips::FCOND_OLE;
251 case ISD::SETGE:
252 case ISD::SETOGE: return Mips::FCOND_OGE;
253 case ISD::SETULT: return Mips::FCOND_ULT;
254 case ISD::SETULE: return Mips::FCOND_ULE;
255 case ISD::SETUGT: return Mips::FCOND_UGT;
256 case ISD::SETUGE: return Mips::FCOND_UGE;
257 case ISD::SETUO: return Mips::FCOND_UN;
258 case ISD::SETO: return Mips::FCOND_OR;
259 case ISD::SETNE:
260 case ISD::SETONE: return Mips::FCOND_NEQ;
261 case ISD::SETUEQ: return Mips::FCOND_UEQ;
262 }
263}
264
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000265MachineBasicBlock *
266MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +0000267 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000268 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
269 bool isFPCmp = false;
Dale Johannesen94817572009-02-13 02:34:39 +0000270 DebugLoc dl = MI->getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000271
272 switch (MI->getOpcode()) {
273 default: assert(false && "Unexpected instr type to insert");
274 case Mips::Select_FCC:
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000275 case Mips::Select_FCC_S32:
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000276 case Mips::Select_FCC_D32:
277 isFPCmp = true; // FALL THROUGH
278 case Mips::Select_CC:
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000279 case Mips::Select_CC_S32:
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000280 case Mips::Select_CC_D32: {
281 // To "insert" a SELECT_CC instruction, we actually have to insert the
282 // diamond control-flow pattern. The incoming instruction knows the
283 // destination vreg to set, the condition code register to branch on, the
284 // true/false values to select between, and a branch opcode to use.
285 const BasicBlock *LLVM_BB = BB->getBasicBlock();
286 MachineFunction::iterator It = BB;
287 ++It;
288
289 // thisMBB:
290 // ...
291 // TrueVal = ...
292 // setcc r1, r2, r3
293 // bNE r1, r0, copy1MBB
294 // fallthrough --> copy0MBB
295 MachineBasicBlock *thisMBB = BB;
296 MachineFunction *F = BB->getParent();
297 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
298 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
299
300 // Emit the right instruction according to the type of the operands compared
301 if (isFPCmp) {
302 // Find the condiction code present in the setcc operation.
303 Mips::CondCode CC = (Mips::CondCode)MI->getOperand(4).getImm();
304 // Get the branch opcode from the branch code.
305 unsigned Opc = FPBranchCodeToOpc(GetFPBranchCodeFromCond(CC));
Dale Johannesen94817572009-02-13 02:34:39 +0000306 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000307 } else
Dale Johannesen94817572009-02-13 02:34:39 +0000308 BuildMI(BB, dl, TII->get(Mips::BNE)).addReg(MI->getOperand(1).getReg())
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000309 .addReg(Mips::ZERO).addMBB(sinkMBB);
310
311 F->insert(It, copy0MBB);
312 F->insert(It, sinkMBB);
313 // Update machine-CFG edges by first adding all successors of the current
314 // block to the new block which will contain the Phi node for the select.
315 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
316 e = BB->succ_end(); i != e; ++i)
317 sinkMBB->addSuccessor(*i);
318 // Next, remove all successors of the current block, and add the true
319 // and fallthrough blocks as its successors.
320 while(!BB->succ_empty())
321 BB->removeSuccessor(BB->succ_begin());
322 BB->addSuccessor(copy0MBB);
323 BB->addSuccessor(sinkMBB);
324
325 // copy0MBB:
326 // %FalseValue = ...
327 // # fallthrough to sinkMBB
328 BB = copy0MBB;
329
330 // Update machine-CFG edges
331 BB->addSuccessor(sinkMBB);
332
333 // sinkMBB:
334 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
335 // ...
336 BB = sinkMBB;
Dale Johannesen94817572009-02-13 02:34:39 +0000337 BuildMI(BB, dl, TII->get(Mips::PHI), MI->getOperand(0).getReg())
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000338 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
339 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB);
340
341 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
342 return BB;
343 }
344 }
345}
346
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000347//===----------------------------------------------------------------------===//
348// Misc Lower Operation implementation
349//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000350
Dan Gohman475871a2008-07-27 21:46:04 +0000351SDValue MipsTargetLowering::
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +0000352LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG)
353{
354 if (!Subtarget->isMips1())
355 return Op;
356
357 MachineFunction &MF = DAG.getMachineFunction();
358 unsigned CCReg = AddLiveIn(MF, Mips::FCR31, Mips::CCRRegisterClass);
359
360 SDValue Chain = DAG.getEntryNode();
361 DebugLoc dl = Op.getDebugLoc();
362 SDValue Src = Op.getOperand(0);
363
364 // Set the condition register
365 SDValue CondReg = DAG.getCopyFromReg(Chain, dl, CCReg, MVT::i32);
366 CondReg = DAG.getCopyToReg(Chain, dl, Mips::AT, CondReg);
367 CondReg = DAG.getCopyFromReg(CondReg, dl, Mips::AT, MVT::i32);
368
369 SDValue Cst = DAG.getConstant(3, MVT::i32);
370 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, CondReg, Cst);
371 Cst = DAG.getConstant(2, MVT::i32);
372 SDValue Xor = DAG.getNode(ISD::XOR, dl, MVT::i32, Or, Cst);
373
374 SDValue InFlag(0, 0);
375 CondReg = DAG.getCopyToReg(Chain, dl, Mips::FCR31, Xor, InFlag);
376
377 // Emit the round instruction and bit convert to integer
378 SDValue Trunc = DAG.getNode(MipsISD::FPRound, dl, MVT::f32,
379 Src, CondReg.getValue(1));
380 SDValue BitCvt = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Trunc);
381 return BitCvt;
382}
383
384SDValue MipsTargetLowering::
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000385LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG)
386{
387 SDValue Chain = Op.getOperand(0);
388 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +0000389 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000390
391 // Get a reference from Mips stack pointer
Dale Johannesena05dca42009-02-04 23:02:30 +0000392 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000393
394 // Subtract the dynamic size from the actual stack size to
395 // obtain the new stack size.
Dale Johannesena05dca42009-02-04 23:02:30 +0000396 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000397
398 // The Sub result contains the new stack start address, so it
399 // must be placed in the stack pointer register.
Dale Johannesena05dca42009-02-04 23:02:30 +0000400 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000401
402 // This node always has two return values: a new stack pointer
403 // value and a chain
404 SDValue Ops[2] = { Sub, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000405 return DAG.getMergeValues(Ops, 2, dl);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000406}
407
408SDValue MipsTargetLowering::
Bruno Cardoso Lopes1906c5a2008-08-02 19:37:33 +0000409LowerANDOR(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000410{
411 SDValue LHS = Op.getOperand(0);
412 SDValue RHS = Op.getOperand(1);
Dale Johannesende064702009-02-06 21:50:26 +0000413 DebugLoc dl = Op.getDebugLoc();
414
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000415 if (LHS.getOpcode() != MipsISD::FPCmp || RHS.getOpcode() != MipsISD::FPCmp)
416 return Op;
417
418 SDValue True = DAG.getConstant(1, MVT::i32);
419 SDValue False = DAG.getConstant(0, MVT::i32);
420
Dale Johannesende064702009-02-06 21:50:26 +0000421 SDValue LSEL = DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000422 LHS, True, False, LHS.getOperand(2));
Dale Johannesende064702009-02-06 21:50:26 +0000423 SDValue RSEL = DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000424 RHS, True, False, RHS.getOperand(2));
425
Dale Johannesende064702009-02-06 21:50:26 +0000426 return DAG.getNode(Op.getOpcode(), dl, MVT::i32, LSEL, RSEL);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000427}
428
429SDValue MipsTargetLowering::
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000430LowerBRCOND(SDValue Op, SelectionDAG &DAG)
431{
432 // The first operand is the chain, the second is the condition, the third is
433 // the block to branch to if the condition is true.
434 SDValue Chain = Op.getOperand(0);
435 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000436 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000437
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000438 if (Op.getOperand(1).getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +0000439 return Op;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000440
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000441 SDValue CondRes = Op.getOperand(1);
442 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000443 Mips::CondCode CC =
444 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000445 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
446
Dale Johannesende064702009-02-06 21:50:26 +0000447 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000448 Dest, CondRes);
449}
450
451SDValue MipsTargetLowering::
452LowerSETCC(SDValue Op, SelectionDAG &DAG)
453{
454 // The operands to this are the left and right operands to compare (ops #0,
455 // and #1) and the condition code to compare them with (op #2) as a
456 // CondCodeSDNode.
457 SDValue LHS = Op.getOperand(0);
Dale Johannesende064702009-02-06 21:50:26 +0000458 SDValue RHS = Op.getOperand(1);
459 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000460
461 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
462
Dale Johannesende064702009-02-06 21:50:26 +0000463 return DAG.getNode(MipsISD::FPCmp, dl, Op.getValueType(), LHS, RHS,
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000464 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
465}
466
467SDValue MipsTargetLowering::
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000468LowerSELECT(SDValue Op, SelectionDAG &DAG)
469{
470 SDValue Cond = Op.getOperand(0);
471 SDValue True = Op.getOperand(1);
472 SDValue False = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000473 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000474
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000475 // if the incomming condition comes from a integer compare, the select
476 // operation must be SelectCC or a conditional move if the subtarget
477 // supports it.
478 if (Cond.getOpcode() != MipsISD::FPCmp) {
479 if (Subtarget->hasCondMov() && !True.getValueType().isFloatingPoint())
480 return Op;
Dale Johannesende064702009-02-06 21:50:26 +0000481 return DAG.getNode(MipsISD::SelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000482 Cond, True, False);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000483 }
484
485 // if the incomming condition comes from fpcmp, the select
486 // operation must use FPSelectCC.
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000487 SDValue CCNode = Cond.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +0000488 return DAG.getNode(MipsISD::FPSelectCC, dl, True.getValueType(),
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000489 Cond, True, False, CCNode);
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000490}
491
492SDValue MipsTargetLowering::
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000493LowerGlobalAddress(SDValue Op, SelectionDAG &DAG)
494{
Dale Johannesende064702009-02-06 21:50:26 +0000495 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000496 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000497 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
498 SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
499
Eli Friedmane2c74082009-08-03 02:22:28 +0000500 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000501 // %hi/%lo relocation
Eli Friedmane2c74082009-08-03 02:22:28 +0000502 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, GA);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000503 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GA);
504 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000505
506 } else { // Abicall relocations, TODO: make this cleaner.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000507 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
508 DAG.getEntryNode(), GA, NULL, 0);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000509 // On functions and global targets not internal linked only
510 // a load from got/GP is necessary for PIC to work.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000511 if (!GV->hasLocalLinkage() || isa<Function>(GV))
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000512 return ResNode;
Dale Johannesen33c960f2009-02-04 20:06:27 +0000513 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GA);
514 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000515 }
516
Torok Edwinc23197a2009-07-14 16:55:14 +0000517 llvm_unreachable("Dont know how to handle GlobalAddress");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000518 return SDValue(0,0);
519}
520
521SDValue MipsTargetLowering::
522LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG)
523{
Torok Edwinc23197a2009-07-14 16:55:14 +0000524 llvm_unreachable("TLS not implemented for MIPS.");
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +0000525 return SDValue(); // Not reached
526}
527
528SDValue MipsTargetLowering::
Dan Gohman475871a2008-07-27 21:46:04 +0000529LowerJumpTable(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000530{
Dan Gohman475871a2008-07-27 21:46:04 +0000531 SDValue ResNode;
532 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +0000533 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +0000534 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000535
Duncan Sands83ec4b62008-06-06 12:08:01 +0000536 MVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000537 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +0000538 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000539
540 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohmanfc166572009-04-09 23:54:40 +0000541 SDVTList VTs = DAG.getVTList(MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000542 SDValue Ops[] = { JTI };
Dan Gohmanfc166572009-04-09 23:54:40 +0000543 HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, Ops, 1);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000544 } else // Emit Load from Global Pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +0000545 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI, NULL, 0);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000546
Dale Johannesen33c960f2009-02-04 20:06:27 +0000547 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTI);
548 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000549
550 return ResNode;
551}
552
Dan Gohman475871a2008-07-27 21:46:04 +0000553SDValue MipsTargetLowering::
554LowerConstantPool(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000555{
Dan Gohman475871a2008-07-27 21:46:04 +0000556 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000557 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
558 Constant *C = N->getConstVal();
Dan Gohman475871a2008-07-27 21:46:04 +0000559 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
Dale Johannesende064702009-02-06 21:50:26 +0000560 // FIXME there isn't actually debug info here
561 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000562
563 // gp_rel relocation
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000564 // FIXME: we should reference the constant pool using small data sections,
565 // but the asm printer currently doens't support this feature without
566 // hacking it. This feature should come soon so we can uncomment the
567 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +0000568 //if (IsInSmallSection(C->getType())) {
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000569 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000570 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000571 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
572 //} else { // %hi/%lo relocation
Dale Johannesende064702009-02-06 21:50:26 +0000573 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CP);
574 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CP);
575 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +0000576 //}
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +0000577
578 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000579}
580
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000581//===----------------------------------------------------------------------===//
582// Calling Convention Implementation
583//
584// The lower operations present on calling convention works on this order:
585// LowerCALL (virt regs --> phys regs, virt regs --> stack)
586// LowerFORMAL_ARGUMENTS (phys --> virt regs, stack --> virt regs)
587// LowerRET (virt regs --> phys regs)
588// LowerCALL (phys regs --> virt regs)
589//
590//===----------------------------------------------------------------------===//
591
592#include "MipsGenCallingConv.inc"
593
594//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000595// TODO: Implement a generic logic using tblgen that can support this.
596// Mips O32 ABI rules:
597// ---
598// i32 - Passed in A0, A1, A2, A3 and stack
599// f32 - Only passed in f32 registers if no int reg has been used yet to hold
600// an argument. Otherwise, passed in A1, A2, A3 and stack.
601// f64 - Only passed in two aliased f32 registers if no int reg has been used
602// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
603// not used, it must be shadowed. If only A3 is avaiable, shadow it and
604// go to stack.
605//===----------------------------------------------------------------------===//
606
607static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
608 MVT LocVT, CCValAssign::LocInfo LocInfo,
609 ISD::ArgFlagsTy ArgFlags, CCState &State) {
610
611 static const unsigned IntRegsSize=4, FloatRegsSize=2;
612
613 static const unsigned IntRegs[] = {
614 Mips::A0, Mips::A1, Mips::A2, Mips::A3
615 };
616 static const unsigned F32Regs[] = {
617 Mips::F12, Mips::F14
618 };
619 static const unsigned F64Regs[] = {
620 Mips::D6, Mips::D7
621 };
622
623 unsigned Reg=0;
624 unsigned UnallocIntReg = State.getFirstUnallocated(IntRegs, IntRegsSize);
625 bool IntRegUsed = (IntRegs[UnallocIntReg] != (unsigned (Mips::A0)));
626
627 // Promote i8 and i16
628 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
629 LocVT = MVT::i32;
630 if (ArgFlags.isSExt())
631 LocInfo = CCValAssign::SExt;
632 else if (ArgFlags.isZExt())
633 LocInfo = CCValAssign::ZExt;
634 else
635 LocInfo = CCValAssign::AExt;
636 }
637
638 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && IntRegUsed)) {
639 Reg = State.AllocateReg(IntRegs, IntRegsSize);
640 IntRegUsed = true;
641 LocVT = MVT::i32;
642 }
643
644 if (ValVT.isFloatingPoint() && !IntRegUsed) {
645 if (ValVT == MVT::f32)
646 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
647 else
648 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
649 }
650
651 if (ValVT == MVT::f64 && IntRegUsed) {
652 if (UnallocIntReg != IntRegsSize) {
653 // If we hit register A3 as the first not allocated, we must
654 // mark it as allocated (shadow) and use the stack instead.
655 if (IntRegs[UnallocIntReg] != (unsigned (Mips::A3)))
656 Reg = Mips::A2;
657 for (;UnallocIntReg < IntRegsSize; ++UnallocIntReg)
658 State.AllocateReg(UnallocIntReg);
659 }
660 LocVT = MVT::i32;
661 }
662
663 if (!Reg) {
664 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
665 unsigned Offset = State.AllocateStack(SizeInBytes, SizeInBytes);
666 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
667 } else
668 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
669
670 return false; // CC must always match
671}
672
673//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000674// CALL Calling Convention Implementation
675//===----------------------------------------------------------------------===//
676
Nate Begeman5bf4b752009-01-26 03:15:54 +0000677/// LowerCALL - functions arguments are copied from virtual regs to
678/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000679/// TODO: isVarArg, isTailCall.
Dan Gohman475871a2008-07-27 21:46:04 +0000680SDValue MipsTargetLowering::
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000681LowerCALL(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000682{
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000683 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000684
Dan Gohman095cc292008-09-13 01:54:27 +0000685 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
686 SDValue Chain = TheCall->getChain();
687 SDValue Callee = TheCall->getCallee();
688 bool isVarArg = TheCall->isVarArg();
689 unsigned CC = TheCall->getCallingConv();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000690 DebugLoc dl = TheCall->getDebugLoc();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000691
692 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000693
694 // Analyze operands of the call, assigning locations to each operand.
695 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersone922c022009-07-22 00:24:57 +0000696 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000697
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000698 // To meet O32 ABI, Mips must always allocate 16 bytes on
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000699 // the stack (even if less than 4 are used as arguments)
Bruno Cardoso Lopesb27cb552008-07-15 02:03:36 +0000700 if (Subtarget->isABI_O32()) {
701 int VTsize = MVT(MVT::i32).getSizeInBits()/8;
702 MFI->CreateFixedObject(VTsize, (VTsize*3));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000703 CCInfo.AnalyzeCallOperands(TheCall, CC_MipsO32);
704 } else
705 CCInfo.AnalyzeCallOperands(TheCall, CC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000706
707 // Get a count of how many bytes are to be pushed on the stack.
708 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnere563bbc2008-10-11 22:08:30 +0000709 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000710
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000711 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +0000712 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
713 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000714
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000715 // First/LastArgStackLoc contains the first/last
716 // "at stack" argument location.
717 int LastArgStackLoc = 0;
718 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000719
720 // Walk the register/memloc assignments, inserting copies/loads.
721 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000722 SDValue Arg = TheCall->getArg(i);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000723 CCValAssign &VA = ArgLocs[i];
724
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000725 // Promote the value if needed.
726 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000727 default: llvm_unreachable("Unknown loc info!");
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000728 case CCValAssign::Full:
729 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
730 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
731 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Arg);
732 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
733 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
734 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
735 DAG.getConstant(0, getPointerTy()));
736 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Arg,
737 DAG.getConstant(1, getPointerTy()));
738 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
739 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
740 continue;
741 }
742 }
743 break;
Chris Lattnere0b12152008-03-17 06:57:02 +0000744 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000745 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000746 break;
747 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000748 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000749 break;
750 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +0000751 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +0000752 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000753 }
754
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000755 // Arguments that can be passed on register must be kept at
756 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000757 if (VA.isRegLoc()) {
758 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +0000759 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000760 }
Chris Lattnere0b12152008-03-17 06:57:02 +0000761
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000762 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +0000763 assert(VA.isMemLoc());
764
765 // Create the frame index object for this incoming parameter
766 // This guarantees that when allocating Local Area the firsts
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000767 // 16 bytes which are alwayes reserved won't be overwritten
768 // if O32 ABI is used. For EABI the first address is zero.
769 LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset());
Duncan Sands83ec4b62008-06-06 12:08:01 +0000770 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000771 LastArgStackLoc);
Chris Lattnere0b12152008-03-17 06:57:02 +0000772
Dan Gohman475871a2008-07-27 21:46:04 +0000773 SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +0000774
775 // emit ISD::STORE whichs stores the
776 // parameter value to a stack Location
Dale Johannesen33c960f2009-02-04 20:06:27 +0000777 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000778 }
779
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000780 // Transform all store nodes into one single node because all store
781 // nodes are independent of each other.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000782 if (!MemOpChains.empty())
Dale Johannesen33c960f2009-02-04 20:06:27 +0000783 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000784 &MemOpChains[0], MemOpChains.size());
785
786 // Build a sequence of copy-to-reg nodes chained together with token
787 // chain and flag operands which copy the outgoing args into registers.
788 // The InFlag in necessary since all emited instructions must be
789 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000790 SDValue InFlag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000791 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000792 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000793 RegsToPass[i].second, InFlag);
794 InFlag = Chain.getValue(1);
795 }
796
Bill Wendling056292f2008-09-16 21:48:12 +0000797 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
798 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
799 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000800 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000801 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +0000802 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
803 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
804
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000805 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
806 // = Chain, Callee, Reg#1, Reg#2, ...
807 //
808 // Returns a chain & a flag for retval copy to use.
809 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000810 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000811 Ops.push_back(Chain);
812 Ops.push_back(Callee);
813
814 // Add argument registers to the end of the list so that they are
815 // known live into the call.
816 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
817 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
818 RegsToPass[i].second.getValueType()));
819
Gabor Greifba36cb52008-08-28 21:40:38 +0000820 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000821 Ops.push_back(InFlag);
822
Dale Johannesen33c960f2009-02-04 20:06:27 +0000823 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000824 InFlag = Chain.getValue(1);
825
Bruno Cardoso Lopesd2947ee2008-06-04 01:45:25 +0000826 // Create the CALLSEQ_END node.
Chris Lattnere563bbc2008-10-11 22:08:30 +0000827 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
828 DAG.getIntPtrConstant(0, true), InFlag);
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000829 InFlag = Chain.getValue(1);
830
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000831 // Create a stack location to hold GP when PIC is used. This stack
832 // location is used on function prologue to save GP and also after all
833 // emited CALL's to restore GP.
834 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000835 // Function can have an arbitrary number of calls, so
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000836 // hold the LastArgStackLoc with the biggest offset.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000837 int FI;
838 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000839 if (LastArgStackLoc >= MipsFI->getGPStackOffset()) {
840 LastArgStackLoc = (!LastArgStackLoc) ? (16) : (LastArgStackLoc+4);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000841 // Create the frame index only once. SPOffset here can be anything
842 // (this will be fixed on processFunctionBeforeFrameFinalized)
843 if (MipsFI->getGPStackOffset() == -1) {
844 FI = MFI->CreateFixedObject(4, 0);
845 MipsFI->setGPFI(FI);
846 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000847 MipsFI->setGPStackOffset(LastArgStackLoc);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000848 }
849
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000850 // Reload GP value.
851 FI = MipsFI->getGPFI();
Dan Gohman475871a2008-07-27 21:46:04 +0000852 SDValue FIN = DAG.getFrameIndex(FI,getPointerTy());
Dale Johannesen33c960f2009-02-04 20:06:27 +0000853 SDValue GPLoad = DAG.getLoad(MVT::i32, dl, Chain, FIN, NULL, 0);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000854 Chain = GPLoad.getValue(1);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000855 Chain = DAG.getCopyToReg(Chain, dl, DAG.getRegister(Mips::GP, MVT::i32),
Dan Gohman475871a2008-07-27 21:46:04 +0000856 GPLoad, SDValue(0,0));
Bruno Cardoso Lopesbdbb7502008-06-01 03:49:39 +0000857 InFlag = Chain.getValue(1);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000858 }
859
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000860 // Handle result values, copying them out of physregs into vregs that we
861 // return.
Dan Gohman095cc292008-09-13 01:54:27 +0000862 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG), Op.getResNo());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000863}
864
865/// LowerCallResult - Lower the result values of an ISD::CALL into the
866/// appropriate copies out of appropriate physical registers. This assumes that
867/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
868/// being lowered. Returns a SDNode with the same number of values as the
869/// ISD::CALL.
870SDNode *MipsTargetLowering::
Dan Gohman095cc292008-09-13 01:54:27 +0000871LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000872 unsigned CallingConv, SelectionDAG &DAG) {
873
Dan Gohman095cc292008-09-13 01:54:27 +0000874 bool isVarArg = TheCall->isVarArg();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000875 DebugLoc dl = TheCall->getDebugLoc();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000876
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000877 // Assign locations to each value returned by this call.
878 SmallVector<CCValAssign, 16> RVLocs;
Owen Andersond1474d02009-07-09 17:57:24 +0000879 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000880 RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000881
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000882 CCInfo.AnalyzeCallResult(TheCall, RetCC_Mips);
Dan Gohman475871a2008-07-27 21:46:04 +0000883 SmallVector<SDValue, 8> ResultVals;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000884
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000885 // Copy all of the result registers out of their specified physreg.
886 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000887 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000888 RVLocs[i].getValVT(), InFlag).getValue(1);
889 InFlag = Chain.getValue(2);
890 ResultVals.push_back(Chain.getValue(0));
891 }
892
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000893 ResultVals.push_back(Chain);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000894
895 // Merge everything together with a MERGE_VALUES node.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000896 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +0000897 &ResultVals[0], ResultVals.size()).getNode();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000898}
899
900//===----------------------------------------------------------------------===//
901// FORMAL_ARGUMENTS Calling Convention Implementation
902//===----------------------------------------------------------------------===//
903
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000904/// LowerFORMAL_ARGUMENTS - transform physical registers into
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000905/// virtual registers and generate load operations for
906/// arguments places on the stack.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000907/// TODO: isVarArg
Dan Gohman475871a2008-07-27 21:46:04 +0000908SDValue MipsTargetLowering::
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000909LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000910{
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000911 SDValue Root = Op.getOperand(0);
912 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000913 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000914 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000915 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000916
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000917 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +0000918 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000919
920 unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000921
922 // Assign locations to all of the incoming arguments.
923 SmallVector<CCValAssign, 16> ArgLocs;
Owen Andersone922c022009-07-22 00:24:57 +0000924 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000925
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000926 if (Subtarget->isABI_O32())
927 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_MipsO32);
928 else
929 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_Mips);
930
Dan Gohman475871a2008-07-27 21:46:04 +0000931 SmallVector<SDValue, 16> ArgValues;
932 SDValue StackPtr;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000933
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000934 unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
935
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000936 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000937 CCValAssign &VA = ArgLocs[i];
938
939 // Arguments stored on registers
940 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000941 MVT RegVT = VA.getLocVT();
Bill Wendling06b8c192008-07-09 05:55:53 +0000942 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000943
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000944 if (RegVT == MVT::i32)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000945 RC = Mips::CPURegsRegisterClass;
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000946 else if (RegVT == MVT::f32)
947 RC = Mips::FGR32RegisterClass;
948 else if (RegVT == MVT::f64) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000949 if (!Subtarget->isSingleFloat())
950 RC = Mips::AFGR64RegisterClass;
951 } else
Torok Edwinc23197a2009-07-14 16:55:14 +0000952 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000953
954 // Transform the arguments stored on
955 // physical registers into virtual ones
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000956 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000957 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000958
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000959 // If this is an 8 or 16-bit value, it has been passed promoted
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000960 // to 32 bits. Insert an assert[sz]ext to capture this, then
961 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000962 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +0000963 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000964 if (VA.getLocInfo() == CCValAssign::SExt)
965 Opcode = ISD::AssertSext;
966 else if (VA.getLocInfo() == CCValAssign::ZExt)
967 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +0000968 if (Opcode)
969 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
970 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +0000971 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +0000972 }
973
974 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
975 if (Subtarget->isABI_O32()) {
976 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
977 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, ArgValue);
978 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
979 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
980 VA.getLocReg()+1, RC);
981 SDValue ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg2, RegVT);
982 SDValue Hi = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, ArgValue);
983 SDValue Lo = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, ArgValue2);
984 ArgValue = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::f64, Lo, Hi);
985 }
986 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000987
988 ArgValues.push_back(ArgValue);
989
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000990 // To meet ABI, when VARARGS are passed on registers, the registers
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000991 // must have their values written to the caller stack frame.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000992 if ((isVarArg) && (Subtarget->isABI_O32())) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000993 if (StackPtr.getNode() == 0)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +0000994 StackPtr = DAG.getRegister(StackReg, getPointerTy());
995
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +0000996 // The stack pointer offset is relative to the caller stack frame.
997 // Since the real stack size is unknown here, a negative SPOffset
998 // is used so there's a way to adjust these offsets when the stack
999 // size get known (on EliminateFrameIndex). A dummy SPOffset is
1000 // used instead of a direct negative address (which is recorded to
1001 // be used on emitPrologue) to avoid mis-calc of the first stack
1002 // offset on PEI::calculateFrameObjectOffsets.
1003 // Arguments are always 32-bit.
1004 int FI = MFI->CreateFixedObject(4, 0);
1005 MipsFI->recordStoreVarArgsFI(FI, -(4+(i*4)));
Dan Gohman475871a2008-07-27 21:46:04 +00001006 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001007
1008 // emit ISD::STORE whichs stores the
1009 // parameter value to a stack Location
Dale Johannesen33c960f2009-02-04 20:06:27 +00001010 ArgValues.push_back(DAG.getStore(Root, dl, ArgValue, PtrOff, NULL, 0));
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001011 }
1012
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001013 } else { // VA.isRegLoc()
1014
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001015 // sanity check
1016 assert(VA.isMemLoc());
1017
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00001018 // The stack pointer offset is relative to the caller stack frame.
1019 // Since the real stack size is unknown here, a negative SPOffset
1020 // is used so there's a way to adjust these offsets when the stack
1021 // size get known (on EliminateFrameIndex). A dummy SPOffset is
1022 // used instead of a direct negative address (which is recorded to
1023 // be used on emitPrologue) to avoid mis-calc of the first stack
1024 // offset on PEI::calculateFrameObjectOffsets.
1025 // Arguments are always 32-bit.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001026 unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
1027 int FI = MFI->CreateFixedObject(ArgSize, 0);
1028 MipsFI->recordLoadArgsFI(FI, -(ArgSize+
1029 (FirstStackArgLoc + VA.getLocMemOffset())));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001030
1031 // Create load nodes to retrieve arguments from the stack
Dan Gohman475871a2008-07-27 21:46:04 +00001032 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Dale Johannesen33c960f2009-02-04 20:06:27 +00001033 ArgValues.push_back(DAG.getLoad(VA.getValVT(), dl, Root, FIN, NULL, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001034 }
1035 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001036
1037 // The mips ABIs for returning structs by value requires that we copy
1038 // the sret argument into $v0 for the return. Save the argument into
1039 // a virtual register so that we can access it from the return points.
1040 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1041 unsigned Reg = MipsFI->getSRetReturnReg();
1042 if (!Reg) {
1043 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
1044 MipsFI->setSRetReturnReg(Reg);
1045 }
Dale Johannesen33c960f2009-02-04 20:06:27 +00001046 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, ArgValues[0]);
1047 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Root);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001048 }
1049
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001050 ArgValues.push_back(Root);
1051
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001052 // Return the new list of results.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001053 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00001054 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001055}
1056
1057//===----------------------------------------------------------------------===//
1058// Return Value Calling Convention Implementation
1059//===----------------------------------------------------------------------===//
1060
Dan Gohman475871a2008-07-27 21:46:04 +00001061SDValue MipsTargetLowering::
1062LowerRET(SDValue Op, SelectionDAG &DAG)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001063{
1064 // CCValAssign - represent the assignment of
1065 // the return value to a location
1066 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001067 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
1068 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Dale Johannesena05dca42009-02-04 23:02:30 +00001069 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001070
1071 // CCState - Info about the registers and stack slot.
Owen Andersone922c022009-07-22 00:24:57 +00001072 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001073
1074 // Analize return values of ISD::RET
Gabor Greifba36cb52008-08-28 21:40:38 +00001075 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001076
1077 // If this is the first return lowered for this function, add
1078 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001079 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001080 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001081 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +00001082 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001083 }
1084
1085 // The chain is always operand #0
Dan Gohman475871a2008-07-27 21:46:04 +00001086 SDValue Chain = Op.getOperand(0);
1087 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001088
1089 // Copy the result values into the output registers.
1090 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1091 CCValAssign &VA = RVLocs[i];
1092 assert(VA.isRegLoc() && "Can only return in registers!");
1093
1094 // ISD::RET => ret chain, (regnum1,val1), ...
1095 // So i*2+1 index only the regnums
Dale Johannesena05dca42009-02-04 23:02:30 +00001096 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1097 Op.getOperand(i*2+1), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001098
1099 // guarantee that all emitted copies are
1100 // stuck together, avoiding something bad
1101 Flag = Chain.getValue(1);
1102 }
1103
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001104 // The mips ABIs for returning structs by value requires that we copy
1105 // the sret argument into $v0 for the return. We saved the argument into
1106 // a virtual register in the entry block, so now we copy the value out
1107 // and into $v0.
1108 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1109 MachineFunction &MF = DAG.getMachineFunction();
1110 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1111 unsigned Reg = MipsFI->getSRetReturnReg();
1112
1113 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00001114 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00001115 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001116
Dale Johannesena05dca42009-02-04 23:02:30 +00001117 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001118 Flag = Chain.getValue(1);
1119 }
1120
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001121 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00001122 if (Flag.getNode())
Dale Johannesena05dca42009-02-04 23:02:30 +00001123 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +00001124 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001125 else // Return Void
Dale Johannesena05dca42009-02-04 23:02:30 +00001126 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Bruno Cardoso Lopes8262df32007-10-09 03:15:11 +00001127 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001128}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001129
1130//===----------------------------------------------------------------------===//
1131// Mips Inline Assembly Support
1132//===----------------------------------------------------------------------===//
1133
1134/// getConstraintType - Given a constraint letter, return the type of
1135/// constraint it is for this target.
1136MipsTargetLowering::ConstraintType MipsTargetLowering::
1137getConstraintType(const std::string &Constraint) const
1138{
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001139 // Mips specific constrainy
1140 // GCC config/mips/constraints.md
1141 //
1142 // 'd' : An address register. Equivalent to r
1143 // unless generating MIPS16 code.
1144 // 'y' : Equivalent to r; retained for
1145 // backwards compatibility.
Bruno Cardoso Lopes7b76da12008-07-09 04:45:36 +00001146 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001147 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001148 switch (Constraint[0]) {
1149 default : break;
1150 case 'd':
1151 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001152 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001153 return C_RegisterClass;
1154 break;
1155 }
1156 }
1157 return TargetLowering::getConstraintType(Constraint);
1158}
1159
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001160/// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
1161/// return a list of registers that can be used to satisfy the constraint.
1162/// This should only be used for C_RegisterClass constraints.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001163std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00001164getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001165{
1166 if (Constraint.size() == 1) {
1167 switch (Constraint[0]) {
1168 case 'r':
1169 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001170 case 'f':
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00001171 if (VT == MVT::f32)
1172 return std::make_pair(0U, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001173 if (VT == MVT::f64)
1174 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1175 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001176 }
1177 }
1178 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1179}
1180
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001181/// Given a register class constraint, like 'r', if this corresponds directly
1182/// to an LLVM register class, return a register of 0 and the register class
1183/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001184std::vector<unsigned> MipsTargetLowering::
1185getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001186 MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001187{
1188 if (Constraint.size() != 1)
1189 return std::vector<unsigned>();
1190
1191 switch (Constraint[0]) {
1192 default : break;
1193 case 'r':
1194 // GCC Mips Constraint Letters
1195 case 'd':
1196 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001197 return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
1198 Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
1199 Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
1200 Mips::T8, 0);
1201
1202 case 'f':
Duncan Sands15126422008-07-08 09:33:14 +00001203 if (VT == MVT::f32) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001204 if (Subtarget->isSingleFloat())
1205 return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
1206 Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
1207 Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
1208 Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
1209 Mips::F30, Mips::F31, 0);
1210 else
1211 return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
1212 Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
1213 Mips::F28, Mips::F30, 0);
Duncan Sands15126422008-07-08 09:33:14 +00001214 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001215
1216 if (VT == MVT::f64)
1217 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1218 return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
1219 Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
1220 Mips::D14, Mips::D15, 0);
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00001221 }
1222 return std::vector<unsigned>();
1223}
Dan Gohman6520e202008-10-18 02:06:02 +00001224
1225bool
1226MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1227 // The Mips target isn't yet aware of offsets.
1228 return false;
1229}