blob: 54fa2d40b01cbde433b9c1371e895f9597fbe8d1 [file] [log] [blame]
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
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//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000014
15#define DEBUG_TYPE "mips-lower"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000016#include "MipsISelLowering.h"
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +000017#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000018#include "MipsTargetMachine.h"
Chris Lattnerb71b9092009-08-13 06:28:06 +000019#include "MipsTargetObjectFile.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"
Akira Hatanaka794bf172011-07-07 23:56:50 +000026#include "InstPrinter/MipsInstPrinter.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000027#include "llvm/CodeGen/CallingConvLower.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000032#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033#include "llvm/CodeGen/ValueTypes.h"
34#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) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +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";
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +000044 case MipsISD::TlsGd: return "MipsISD::TlsGd";
45 case MipsISD::TprelHi: return "MipsISD::TprelHi";
46 case MipsISD::TprelLo: return "MipsISD::TprelLo";
47 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000048 case MipsISD::Ret: return "MipsISD::Ret";
49 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
50 case MipsISD::FPCmp: return "MipsISD::FPCmp";
51 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
52 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
53 case MipsISD::FPRound: return "MipsISD::FPRound";
54 case MipsISD::MAdd: return "MipsISD::MAdd";
55 case MipsISD::MAddu: return "MipsISD::MAddu";
56 case MipsISD::MSub: return "MipsISD::MSub";
57 case MipsISD::MSubu: return "MipsISD::MSubu";
58 case MipsISD::DivRem: return "MipsISD::DivRem";
59 case MipsISD::DivRemU: return "MipsISD::DivRemU";
60 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
61 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanaka342837d2011-05-28 01:07:07 +000062 case MipsISD::WrapperPIC: return "MipsISD::WrapperPIC";
Akira Hatanaka21afc632011-06-21 00:40:49 +000063 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
Akira Hatanakadb548262011-07-19 23:30:50 +000064 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanaka0f843822011-06-07 18:58:42 +000065 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000066 }
67}
68
69MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000070MipsTargetLowering(MipsTargetMachine &TM)
Chris Lattnerb71b9092009-08-13 06:28:06 +000071 : TargetLowering(TM, new MipsTargetObjectFile()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000072 Subtarget = &TM.getSubtarget<MipsSubtarget>();
73
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000074 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000075 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000076 setBooleanContents(ZeroOrOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000077
78 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000079 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
80 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000081
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000082 // When dealing with single precision only, use libcalls
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000083 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000084 if (!Subtarget->isFP64bit())
Owen Anderson825b72b2009-08-11 20:47:22 +000085 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000086
Wesley Peckbf17cfa2010-11-23 03:31:01 +000087 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +000088 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
89 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
90 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000091
Eli Friedman6055a6a2009-07-17 04:07:24 +000092 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +000093 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
94 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +000095
Wesley Peckbf17cfa2010-11-23 03:31:01 +000096 // Used by legalize types to correctly generate the setcc result.
97 // Without this, every float setcc comes with a AND/OR with the result,
98 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +000099 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000100 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000101
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000102 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +0000103 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000104 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000105 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
106 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
107 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
108 setOperationAction(ISD::SELECT, MVT::f32, Custom);
109 setOperationAction(ISD::SELECT, MVT::f64, Custom);
110 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000111 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
112 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000113 setOperationAction(ISD::VASTART, MVT::Other, Custom);
114
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000115 setOperationAction(ISD::SDIV, MVT::i32, Expand);
116 setOperationAction(ISD::SREM, MVT::i32, Expand);
117 setOperationAction(ISD::UDIV, MVT::i32, Expand);
118 setOperationAction(ISD::UREM, MVT::i32, Expand);
119
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000120 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000121 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
122 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
123 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
124 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
125 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
126 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
127 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
128 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
129 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000130
131 if (!Subtarget->isMips32r2())
132 setOperationAction(ISD::ROTR, MVT::i32, Expand);
133
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
135 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
136 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000137 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
138 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000140 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000141 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000142 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000143 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
144 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000145 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000146 setOperationAction(ISD::FLOG, MVT::f32, Expand);
147 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
148 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
149 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000150 setOperationAction(ISD::FMA, MVT::f32, Expand);
151 setOperationAction(ISD::FMA, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000152
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000153 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
154 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Eric Christopher471e4222011-06-08 23:55:35 +0000155
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000156 setOperationAction(ISD::VAARG, MVT::Other, Expand);
157 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
158 setOperationAction(ISD::VAEND, MVT::Other, Expand);
159
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000160 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000161 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
162 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000163
Akira Hatanakadb548262011-07-19 23:30:50 +0000164 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000165 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000166
Eli Friedman26689ac2011-08-03 21:06:02 +0000167 setInsertFencesForAtomic(true);
168
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000169 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000170 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000171
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000172 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000173 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
174 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000175 }
176
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000177 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000178 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000179
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000180 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000181 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000182
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000183 setTargetDAGCombine(ISD::ADDE);
184 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000185 setTargetDAGCombine(ISD::SDIVREM);
186 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000187 setTargetDAGCombine(ISD::SETCC);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000188
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000189 setMinFunctionAlignment(2);
190
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000191 setStackPointerRegisterToSaveRestore(Mips::SP);
192 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000193
194 setExceptionPointerRegister(Mips::A0);
195 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000196}
197
Owen Anderson825b72b2009-08-11 20:47:22 +0000198MVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
199 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000200}
201
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000202// SelectMadd -
203// Transforms a subgraph in CurDAG if the following pattern is found:
204// (addc multLo, Lo0), (adde multHi, Hi0),
205// where,
206// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000207// Lo0: initial value of Lo register
208// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000209// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000210static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000211 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000212 // for the matching to be successful.
213 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
214
215 if (ADDCNode->getOpcode() != ISD::ADDC)
216 return false;
217
218 SDValue MultHi = ADDENode->getOperand(0);
219 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000220 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000221 unsigned MultOpc = MultHi.getOpcode();
222
223 // MultHi and MultLo must be generated by the same node,
224 if (MultLo.getNode() != MultNode)
225 return false;
226
227 // and it must be a multiplication.
228 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
229 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000230
231 // MultLo amd MultHi must be the first and second output of MultNode
232 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000233 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
234 return false;
235
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000236 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000237 // of the values of MultNode, in which case MultNode will be removed in later
238 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000239 // If there exist users other than ADDENode or ADDCNode, this function returns
240 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000241 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000242 // produced.
243 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
244 return false;
245
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000246 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000247 DebugLoc dl = ADDENode->getDebugLoc();
248
249 // create MipsMAdd(u) node
250 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000251
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000252 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
253 MVT::Glue,
254 MultNode->getOperand(0),// Factor 0
255 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000256 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000257 ADDENode->getOperand(1));// Hi0
258
259 // create CopyFromReg nodes
260 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
261 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000262 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000263 Mips::HI, MVT::i32,
264 CopyFromLo.getValue(2));
265
266 // replace uses of adde and addc here
267 if (!SDValue(ADDCNode, 0).use_empty())
268 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
269
270 if (!SDValue(ADDENode, 0).use_empty())
271 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
272
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000273 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000274}
275
276// SelectMsub -
277// Transforms a subgraph in CurDAG if the following pattern is found:
278// (addc Lo0, multLo), (sube Hi0, multHi),
279// where,
280// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000281// Lo0: initial value of Lo register
282// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000283// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000284static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000285 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000286 // for the matching to be successful.
287 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
288
289 if (SUBCNode->getOpcode() != ISD::SUBC)
290 return false;
291
292 SDValue MultHi = SUBENode->getOperand(1);
293 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000294 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000295 unsigned MultOpc = MultHi.getOpcode();
296
297 // MultHi and MultLo must be generated by the same node,
298 if (MultLo.getNode() != MultNode)
299 return false;
300
301 // and it must be a multiplication.
302 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
303 return false;
304
305 // MultLo amd MultHi must be the first and second output of MultNode
306 // respectively.
307 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
308 return false;
309
310 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
311 // of the values of MultNode, in which case MultNode will be removed in later
312 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000313 // If there exist users other than SUBENode or SUBCNode, this function returns
314 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000315 // instruction node rather than a pair of MULT and MSUB instructions being
316 // produced.
317 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
318 return false;
319
320 SDValue Chain = CurDAG->getEntryNode();
321 DebugLoc dl = SUBENode->getDebugLoc();
322
323 // create MipsSub(u) node
324 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
325
326 SDValue MSub = CurDAG->getNode(MultOpc, dl,
327 MVT::Glue,
328 MultNode->getOperand(0),// Factor 0
329 MultNode->getOperand(1),// Factor 1
330 SUBCNode->getOperand(0),// Lo0
331 SUBENode->getOperand(0));// Hi0
332
333 // create CopyFromReg nodes
334 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
335 MSub);
336 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
337 Mips::HI, MVT::i32,
338 CopyFromLo.getValue(2));
339
340 // replace uses of sube and subc here
341 if (!SDValue(SUBCNode, 0).use_empty())
342 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
343
344 if (!SDValue(SUBENode, 0).use_empty())
345 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
346
347 return true;
348}
349
350static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
351 TargetLowering::DAGCombinerInfo &DCI,
352 const MipsSubtarget* Subtarget) {
353 if (DCI.isBeforeLegalize())
354 return SDValue();
355
356 if (Subtarget->isMips32() && SelectMadd(N, &DAG))
357 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000358
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000359 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000360}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000361
362static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
363 TargetLowering::DAGCombinerInfo &DCI,
364 const MipsSubtarget* Subtarget) {
365 if (DCI.isBeforeLegalize())
366 return SDValue();
367
368 if (Subtarget->isMips32() && SelectMsub(N, &DAG))
369 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000370
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000371 return SDValue();
372}
373
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000374static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
375 TargetLowering::DAGCombinerInfo &DCI,
376 const MipsSubtarget* Subtarget) {
377 if (DCI.isBeforeLegalizeOps())
378 return SDValue();
379
380 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
381 MipsISD::DivRemU;
382 DebugLoc dl = N->getDebugLoc();
383
384 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
385 N->getOperand(0), N->getOperand(1));
386 SDValue InChain = DAG.getEntryNode();
387 SDValue InGlue = DivRem;
388
389 // insert MFLO
390 if (N->hasAnyUseOfValue(0)) {
391 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, Mips::LO, MVT::i32,
392 InGlue);
393 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
394 InChain = CopyFromLo.getValue(1);
395 InGlue = CopyFromLo.getValue(2);
396 }
397
398 // insert MFHI
399 if (N->hasAnyUseOfValue(1)) {
400 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000401 Mips::HI, MVT::i32, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000402 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
403 }
404
405 return SDValue();
406}
407
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000408static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
409 switch (CC) {
410 default: llvm_unreachable("Unknown fp condition code!");
411 case ISD::SETEQ:
412 case ISD::SETOEQ: return Mips::FCOND_OEQ;
413 case ISD::SETUNE: return Mips::FCOND_UNE;
414 case ISD::SETLT:
415 case ISD::SETOLT: return Mips::FCOND_OLT;
416 case ISD::SETGT:
417 case ISD::SETOGT: return Mips::FCOND_OGT;
418 case ISD::SETLE:
419 case ISD::SETOLE: return Mips::FCOND_OLE;
420 case ISD::SETGE:
421 case ISD::SETOGE: return Mips::FCOND_OGE;
422 case ISD::SETULT: return Mips::FCOND_ULT;
423 case ISD::SETULE: return Mips::FCOND_ULE;
424 case ISD::SETUGT: return Mips::FCOND_UGT;
425 case ISD::SETUGE: return Mips::FCOND_UGE;
426 case ISD::SETUO: return Mips::FCOND_UN;
427 case ISD::SETO: return Mips::FCOND_OR;
428 case ISD::SETNE:
429 case ISD::SETONE: return Mips::FCOND_ONE;
430 case ISD::SETUEQ: return Mips::FCOND_UEQ;
431 }
432}
433
434
435// Returns true if condition code has to be inverted.
436static bool InvertFPCondCode(Mips::CondCode CC) {
437 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
438 return false;
439
440 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
441 return true;
442
443 assert(false && "Illegal Condition Code");
444 return false;
445}
446
447// Creates and returns an FPCmp node from a setcc node.
448// Returns Op if setcc is not a floating point comparison.
449static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
450 // must be a SETCC node
451 if (Op.getOpcode() != ISD::SETCC)
452 return Op;
453
454 SDValue LHS = Op.getOperand(0);
455
456 if (!LHS.getValueType().isFloatingPoint())
457 return Op;
458
459 SDValue RHS = Op.getOperand(1);
460 DebugLoc dl = Op.getDebugLoc();
461
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000462 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
463 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000464 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
465
466 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
467 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
468}
469
470// Creates and returns a CMovFPT/F node.
471static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
472 SDValue False, DebugLoc DL) {
473 bool invert = InvertFPCondCode((Mips::CondCode)
474 cast<ConstantSDNode>(Cond.getOperand(2))
475 ->getSExtValue());
476
477 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
478 True.getValueType(), True, False, Cond);
479}
480
481static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
482 TargetLowering::DAGCombinerInfo &DCI,
483 const MipsSubtarget* Subtarget) {
484 if (DCI.isBeforeLegalizeOps())
485 return SDValue();
486
487 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
488
489 if (Cond.getOpcode() != MipsISD::FPCmp)
490 return SDValue();
491
492 SDValue True = DAG.getConstant(1, MVT::i32);
493 SDValue False = DAG.getConstant(0, MVT::i32);
494
495 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
496}
497
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000498SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000499 const {
500 SelectionDAG &DAG = DCI.DAG;
501 unsigned opc = N->getOpcode();
502
503 switch (opc) {
504 default: break;
505 case ISD::ADDE:
506 return PerformADDECombine(N, DAG, DCI, Subtarget);
507 case ISD::SUBE:
508 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000509 case ISD::SDIVREM:
510 case ISD::UDIVREM:
511 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000512 case ISD::SETCC:
513 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000514 }
515
516 return SDValue();
517}
518
Dan Gohman475871a2008-07-27 21:46:04 +0000519SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000520LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000521{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000522 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000523 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000524 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000525 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
526 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000527 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000528 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000529 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
530 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000531 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000532 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000533 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000534 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Akira Hatanakadb548262011-07-19 23:30:50 +0000535 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +0000536 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000537 }
Dan Gohman475871a2008-07-27 21:46:04 +0000538 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000539}
540
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000541//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000542// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000543//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000544
545// AddLiveIn - This helper function adds the specified physical register to the
546// MachineFunction as a live in value. It also creates a corresponding
547// virtual register for it.
548static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000549AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000550{
551 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000552 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
553 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000554 return VReg;
555}
556
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000557// Get fp branch code (not opcode) from condition code.
558static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
559 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
560 return Mips::BRANCH_T;
561
562 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
563 return Mips::BRANCH_F;
564
565 return Mips::BRANCH_INVALID;
566}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000567
Akira Hatanaka14487d42011-06-07 19:28:39 +0000568static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
569 DebugLoc dl,
570 const MipsSubtarget* Subtarget,
571 const TargetInstrInfo *TII,
572 bool isFPCmp, unsigned Opc) {
573 // There is no need to expand CMov instructions if target has
574 // conditional moves.
575 if (Subtarget->hasCondMov())
576 return BB;
577
578 // To "insert" a SELECT_CC instruction, we actually have to insert the
579 // diamond control-flow pattern. The incoming instruction knows the
580 // destination vreg to set, the condition code register to branch on, the
581 // true/false values to select between, and a branch opcode to use.
582 const BasicBlock *LLVM_BB = BB->getBasicBlock();
583 MachineFunction::iterator It = BB;
584 ++It;
585
586 // thisMBB:
587 // ...
588 // TrueVal = ...
589 // setcc r1, r2, r3
590 // bNE r1, r0, copy1MBB
591 // fallthrough --> copy0MBB
592 MachineBasicBlock *thisMBB = BB;
593 MachineFunction *F = BB->getParent();
594 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
595 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
596 F->insert(It, copy0MBB);
597 F->insert(It, sinkMBB);
598
599 // Transfer the remainder of BB and its successor edges to sinkMBB.
600 sinkMBB->splice(sinkMBB->begin(), BB,
601 llvm::next(MachineBasicBlock::iterator(MI)),
602 BB->end());
603 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
604
605 // Next, add the true and fallthrough blocks as its successors.
606 BB->addSuccessor(copy0MBB);
607 BB->addSuccessor(sinkMBB);
608
609 // Emit the right instruction according to the type of the operands compared
610 if (isFPCmp)
611 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
612 else
613 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
614 .addReg(Mips::ZERO).addMBB(sinkMBB);
615
616 // copy0MBB:
617 // %FalseValue = ...
618 // # fallthrough to sinkMBB
619 BB = copy0MBB;
620
621 // Update machine-CFG edges
622 BB->addSuccessor(sinkMBB);
623
624 // sinkMBB:
625 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
626 // ...
627 BB = sinkMBB;
628
629 if (isFPCmp)
630 BuildMI(*BB, BB->begin(), dl,
631 TII->get(Mips::PHI), MI->getOperand(0).getReg())
632 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
633 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
634 else
635 BuildMI(*BB, BB->begin(), dl,
636 TII->get(Mips::PHI), MI->getOperand(0).getReg())
637 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
638 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
639
640 MI->eraseFromParent(); // The pseudo instruction is gone now.
641 return BB;
642}
643
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000644MachineBasicBlock *
645MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000646 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000647 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen94817572009-02-13 02:34:39 +0000648 DebugLoc dl = MI->getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000649
650 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000651 default:
652 assert(false && "Unexpected instr type to insert");
653 return NULL;
654 case Mips::MOVT:
655 case Mips::MOVT_S:
656 case Mips::MOVT_D:
657 return ExpandCondMov(MI, BB, dl, Subtarget, TII, true, Mips::BC1F);
658 case Mips::MOVF:
659 case Mips::MOVF_S:
660 case Mips::MOVF_D:
661 return ExpandCondMov(MI, BB, dl, Subtarget, TII, true, Mips::BC1T);
662 case Mips::MOVZ_I:
663 case Mips::MOVZ_S:
664 case Mips::MOVZ_D:
665 return ExpandCondMov(MI, BB, dl, Subtarget, TII, false, Mips::BNE);
666 case Mips::MOVN_I:
667 case Mips::MOVN_S:
668 case Mips::MOVN_D:
669 return ExpandCondMov(MI, BB, dl, Subtarget, TII, false, Mips::BEQ);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000670
671 case Mips::ATOMIC_LOAD_ADD_I8:
672 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
673 case Mips::ATOMIC_LOAD_ADD_I16:
674 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
675 case Mips::ATOMIC_LOAD_ADD_I32:
676 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
677
678 case Mips::ATOMIC_LOAD_AND_I8:
679 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
680 case Mips::ATOMIC_LOAD_AND_I16:
681 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
682 case Mips::ATOMIC_LOAD_AND_I32:
683 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
684
685 case Mips::ATOMIC_LOAD_OR_I8:
686 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
687 case Mips::ATOMIC_LOAD_OR_I16:
688 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
689 case Mips::ATOMIC_LOAD_OR_I32:
690 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
691
692 case Mips::ATOMIC_LOAD_XOR_I8:
693 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
694 case Mips::ATOMIC_LOAD_XOR_I16:
695 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
696 case Mips::ATOMIC_LOAD_XOR_I32:
697 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
698
699 case Mips::ATOMIC_LOAD_NAND_I8:
700 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
701 case Mips::ATOMIC_LOAD_NAND_I16:
702 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
703 case Mips::ATOMIC_LOAD_NAND_I32:
704 return EmitAtomicBinary(MI, BB, 4, 0, true);
705
706 case Mips::ATOMIC_LOAD_SUB_I8:
707 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
708 case Mips::ATOMIC_LOAD_SUB_I16:
709 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
710 case Mips::ATOMIC_LOAD_SUB_I32:
711 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
712
713 case Mips::ATOMIC_SWAP_I8:
714 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
715 case Mips::ATOMIC_SWAP_I16:
716 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
717 case Mips::ATOMIC_SWAP_I32:
718 return EmitAtomicBinary(MI, BB, 4, 0);
719
720 case Mips::ATOMIC_CMP_SWAP_I8:
721 return EmitAtomicCmpSwapPartword(MI, BB, 1);
722 case Mips::ATOMIC_CMP_SWAP_I16:
723 return EmitAtomicCmpSwapPartword(MI, BB, 2);
724 case Mips::ATOMIC_CMP_SWAP_I32:
725 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000726 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000727}
728
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000729// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
730// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
731MachineBasicBlock *
732MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000733 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000734 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000735 assert(Size == 4 && "Unsupported size for EmitAtomicBinary.");
736
737 MachineFunction *MF = BB->getParent();
738 MachineRegisterInfo &RegInfo = MF->getRegInfo();
739 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
740 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
741 DebugLoc dl = MI->getDebugLoc();
742
Akira Hatanaka4061da12011-07-19 20:11:17 +0000743 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000744 unsigned Ptr = MI->getOperand(1).getReg();
745 unsigned Incr = MI->getOperand(2).getReg();
746
Akira Hatanaka4061da12011-07-19 20:11:17 +0000747 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
748 unsigned AndRes = RegInfo.createVirtualRegister(RC);
749 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000750
751 // insert new blocks after the current block
752 const BasicBlock *LLVM_BB = BB->getBasicBlock();
753 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
754 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
755 MachineFunction::iterator It = BB;
756 ++It;
757 MF->insert(It, loopMBB);
758 MF->insert(It, exitMBB);
759
760 // Transfer the remainder of BB and its successor edges to exitMBB.
761 exitMBB->splice(exitMBB->begin(), BB,
762 llvm::next(MachineBasicBlock::iterator(MI)),
763 BB->end());
764 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
765
766 // thisMBB:
767 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000768 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000769 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000770 loopMBB->addSuccessor(loopMBB);
771 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000772
773 // loopMBB:
774 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000775 // <binop> storeval, oldval, incr
776 // sc success, storeval, 0(ptr)
777 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000778 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000779 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000780 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000781 // and andres, oldval, incr
782 // nor storeval, $0, andres
783 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr);
784 BuildMI(BB, dl, TII->get(Mips::NOR), StoreVal)
785 .addReg(Mips::ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000786 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000787 // <binop> storeval, oldval, incr
788 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000789 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000790 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000791 }
Akira Hatanaka4061da12011-07-19 20:11:17 +0000792 BuildMI(BB, dl, TII->get(Mips::SC), Success)
793 .addReg(StoreVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000794 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +0000795 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000796
797 MI->eraseFromParent(); // The instruction is gone now.
798
Akira Hatanaka939ece12011-07-19 03:42:13 +0000799 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000800}
801
802MachineBasicBlock *
803MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000804 MachineBasicBlock *BB,
805 unsigned Size, unsigned BinOpcode,
806 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000807 assert((Size == 1 || Size == 2) &&
808 "Unsupported size for EmitAtomicBinaryPartial.");
809
810 MachineFunction *MF = BB->getParent();
811 MachineRegisterInfo &RegInfo = MF->getRegInfo();
812 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
813 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
814 DebugLoc dl = MI->getDebugLoc();
815
816 unsigned Dest = MI->getOperand(0).getReg();
817 unsigned Ptr = MI->getOperand(1).getReg();
818 unsigned Incr = MI->getOperand(2).getReg();
819
Akira Hatanaka4061da12011-07-19 20:11:17 +0000820 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
821 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000822 unsigned Mask = RegInfo.createVirtualRegister(RC);
823 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000824 unsigned NewVal = RegInfo.createVirtualRegister(RC);
825 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000826 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000827 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
828 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
829 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
830 unsigned AndRes = RegInfo.createVirtualRegister(RC);
831 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +0000832 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000833 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
834 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
835 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
836 unsigned SllRes = RegInfo.createVirtualRegister(RC);
837 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000838
839 // insert new blocks after the current block
840 const BasicBlock *LLVM_BB = BB->getBasicBlock();
841 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000842 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000843 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
844 MachineFunction::iterator It = BB;
845 ++It;
846 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000847 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000848 MF->insert(It, exitMBB);
849
850 // Transfer the remainder of BB and its successor edges to exitMBB.
851 exitMBB->splice(exitMBB->begin(), BB,
852 llvm::next(MachineBasicBlock::iterator(MI)),
853 BB->end());
854 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
855
Akira Hatanaka81b44112011-07-19 17:09:53 +0000856 BB->addSuccessor(loopMBB);
857 loopMBB->addSuccessor(loopMBB);
858 loopMBB->addSuccessor(sinkMBB);
859 sinkMBB->addSuccessor(exitMBB);
860
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000861 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +0000862 // addiu masklsb2,$0,-4 # 0xfffffffc
863 // and alignedaddr,ptr,masklsb2
864 // andi ptrlsb2,ptr,3
865 // sll shiftamt,ptrlsb2,3
866 // ori maskupper,$0,255 # 0xff
867 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000868 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +0000869 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000870
871 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000872 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
873 .addReg(Mips::ZERO).addImm(-4);
874 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
875 .addReg(Ptr).addReg(MaskLSB2);
876 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
877 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
878 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
879 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +0000880 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
881 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000882 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +0000883 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +0000884
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000885
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +0000886 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000887 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +0000888 // ll oldval,0(alignedaddr)
889 // binop binopres,oldval,incr2
890 // and newval,binopres,mask
891 // and maskedoldval0,oldval,mask2
892 // or storeval,maskedoldval0,newval
893 // sc success,storeval,0(alignedaddr)
894 // beq success,$0,loopMBB
895
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +0000896 // atomic.swap
897 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +0000898 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +0000899 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +0000900 // and maskedoldval0,oldval,mask2
901 // or storeval,maskedoldval0,newval
902 // sc success,storeval,0(alignedaddr)
903 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +0000904
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000905 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000906 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000907 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000908 // and andres, oldval, incr2
909 // nor binopres, $0, andres
910 // and newval, binopres, mask
911 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
912 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
913 .addReg(Mips::ZERO).addReg(AndRes);
914 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000915 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000916 // <binop> binopres, oldval, incr2
917 // and newval, binopres, mask
918 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
919 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +0000920 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +0000921 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +0000922 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +0000923 }
924
Akira Hatanakabdd83fe2011-07-19 20:56:53 +0000925 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000926 .addReg(OldVal).addReg(Mask2);
927 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +0000928 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000929 BuildMI(BB, dl, TII->get(Mips::SC), Success)
930 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000931 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +0000932 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000933
Akira Hatanaka939ece12011-07-19 03:42:13 +0000934 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +0000935 // and maskedoldval1,oldval,mask
936 // srl srlres,maskedoldval1,shiftamt
937 // sll sllres,srlres,24
938 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +0000939 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000940 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +0000941
Akira Hatanaka4061da12011-07-19 20:11:17 +0000942 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
943 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +0000944 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
945 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000946 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
947 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000948 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000949 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000950
951 MI->eraseFromParent(); // The instruction is gone now.
952
Akira Hatanaka939ece12011-07-19 03:42:13 +0000953 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000954}
955
956MachineBasicBlock *
957MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000958 MachineBasicBlock *BB,
959 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000960 assert(Size == 4 && "Unsupported size for EmitAtomicCmpSwap.");
961
962 MachineFunction *MF = BB->getParent();
963 MachineRegisterInfo &RegInfo = MF->getRegInfo();
964 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
965 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
966 DebugLoc dl = MI->getDebugLoc();
967
968 unsigned Dest = MI->getOperand(0).getReg();
969 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +0000970 unsigned OldVal = MI->getOperand(2).getReg();
971 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000972
Akira Hatanaka4061da12011-07-19 20:11:17 +0000973 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000974
975 // insert new blocks after the current block
976 const BasicBlock *LLVM_BB = BB->getBasicBlock();
977 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
978 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
979 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
980 MachineFunction::iterator It = BB;
981 ++It;
982 MF->insert(It, loop1MBB);
983 MF->insert(It, loop2MBB);
984 MF->insert(It, exitMBB);
985
986 // Transfer the remainder of BB and its successor edges to exitMBB.
987 exitMBB->splice(exitMBB->begin(), BB,
988 llvm::next(MachineBasicBlock::iterator(MI)),
989 BB->end());
990 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
991
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000992 // thisMBB:
993 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000994 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000995 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000996 loop1MBB->addSuccessor(exitMBB);
997 loop1MBB->addSuccessor(loop2MBB);
998 loop2MBB->addSuccessor(loop1MBB);
999 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001000
1001 // loop1MBB:
1002 // ll dest, 0(ptr)
1003 // bne dest, oldval, exitMBB
1004 BB = loop1MBB;
Akira Hatanakad3ac47f2011-07-07 18:57:00 +00001005 BuildMI(BB, dl, TII->get(Mips::LL), Dest).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001006 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001007 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001008
1009 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001010 // sc success, newval, 0(ptr)
1011 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001012 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001013 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1014 .addReg(NewVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001015 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001016 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001017
1018 MI->eraseFromParent(); // The instruction is gone now.
1019
Akira Hatanaka939ece12011-07-19 03:42:13 +00001020 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001021}
1022
1023MachineBasicBlock *
1024MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001025 MachineBasicBlock *BB,
1026 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001027 assert((Size == 1 || Size == 2) &&
1028 "Unsupported size for EmitAtomicCmpSwapPartial.");
1029
1030 MachineFunction *MF = BB->getParent();
1031 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1032 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1033 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1034 DebugLoc dl = MI->getDebugLoc();
1035
1036 unsigned Dest = MI->getOperand(0).getReg();
1037 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001038 unsigned CmpVal = MI->getOperand(2).getReg();
1039 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001040
Akira Hatanaka4061da12011-07-19 20:11:17 +00001041 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1042 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001043 unsigned Mask = RegInfo.createVirtualRegister(RC);
1044 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001045 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1046 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1047 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1048 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1049 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1050 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1051 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1052 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1053 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1054 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1055 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1056 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1057 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1058 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001059
1060 // insert new blocks after the current block
1061 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1062 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1063 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001064 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001065 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1066 MachineFunction::iterator It = BB;
1067 ++It;
1068 MF->insert(It, loop1MBB);
1069 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001070 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001071 MF->insert(It, exitMBB);
1072
1073 // Transfer the remainder of BB and its successor edges to exitMBB.
1074 exitMBB->splice(exitMBB->begin(), BB,
1075 llvm::next(MachineBasicBlock::iterator(MI)),
1076 BB->end());
1077 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1078
Akira Hatanaka81b44112011-07-19 17:09:53 +00001079 BB->addSuccessor(loop1MBB);
1080 loop1MBB->addSuccessor(sinkMBB);
1081 loop1MBB->addSuccessor(loop2MBB);
1082 loop2MBB->addSuccessor(loop1MBB);
1083 loop2MBB->addSuccessor(sinkMBB);
1084 sinkMBB->addSuccessor(exitMBB);
1085
Akira Hatanaka70564a92011-07-19 18:14:26 +00001086 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001087 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001088 // addiu masklsb2,$0,-4 # 0xfffffffc
1089 // and alignedaddr,ptr,masklsb2
1090 // andi ptrlsb2,ptr,3
1091 // sll shiftamt,ptrlsb2,3
1092 // ori maskupper,$0,255 # 0xff
1093 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001094 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001095 // andi maskedcmpval,cmpval,255
1096 // sll shiftedcmpval,maskedcmpval,shiftamt
1097 // andi maskednewval,newval,255
1098 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001099 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001100 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1101 .addReg(Mips::ZERO).addImm(-4);
1102 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1103 .addReg(Ptr).addReg(MaskLSB2);
1104 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1105 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1106 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1107 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001108 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1109 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001110 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001111 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1112 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001113 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1114 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001115 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1116 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001117 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1118 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001119
1120 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001121 // ll oldval,0(alginedaddr)
1122 // and maskedoldval0,oldval,mask
1123 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001124 BB = loop1MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001125 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
1126 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1127 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001128 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001129 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001130
1131 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001132 // and maskedoldval1,oldval,mask2
1133 // or storeval,maskedoldval1,shiftednewval
1134 // sc success,storeval,0(alignedaddr)
1135 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001136 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001137 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1138 .addReg(OldVal).addReg(Mask2);
1139 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1140 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1141 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1142 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001143 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001144 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001145
Akira Hatanaka939ece12011-07-19 03:42:13 +00001146 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001147 // srl srlres,maskedoldval0,shiftamt
1148 // sll sllres,srlres,24
1149 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001150 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001151 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001152
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001153 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1154 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001155 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1156 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001157 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001158 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001159
1160 MI->eraseFromParent(); // The instruction is gone now.
1161
Akira Hatanaka939ece12011-07-19 03:42:13 +00001162 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001163}
1164
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001165//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001166// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001167//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001168SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001169LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001170{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001171 MachineFunction &MF = DAG.getMachineFunction();
1172 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1173
1174 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001175 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1176 "Cannot lower if the alignment of the allocated space is larger than \
1177 that of the stack.");
1178
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001179 SDValue Chain = Op.getOperand(0);
1180 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001181 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001182
1183 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +00001184 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001185
1186 // Subtract the dynamic size from the actual stack size to
1187 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +00001188 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001189
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001190 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001191 // must be placed in the stack pointer register.
Akira Hatanaka053546c2011-05-25 02:20:00 +00001192 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub,
1193 SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001194
1195 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001196 // value and a chain
Akira Hatanaka21afc632011-06-21 00:40:49 +00001197 SDVTList VTLs = DAG.getVTList(MVT::i32, MVT::Other);
1198 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1199 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1200
1201 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001202}
1203
1204SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001205LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001206{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001207 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001208 // the block to branch to if the condition is true.
1209 SDValue Chain = Op.getOperand(0);
1210 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001211 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001212
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001213 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1214
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001215 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001216 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001217 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001218
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001219 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001220 Mips::CondCode CC =
1221 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001222 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001223
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001224 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001225 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001226}
1227
1228SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001229LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001230{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001231 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001232
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001233 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001234 if (Cond.getOpcode() != MipsISD::FPCmp)
1235 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001236
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001237 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1238 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001239}
1240
Dan Gohmand858e902010-04-17 15:26:15 +00001241SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1242 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001243 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001244 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001245 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001246
Eli Friedmane2c74082009-08-03 02:22:28 +00001247 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001248 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001249
Chris Lattnerb71b9092009-08-13 06:28:06 +00001250 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001251
Chris Lattnere3736f82009-08-13 05:41:27 +00001252 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001253 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1254 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001255 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001256 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1257 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001258 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001259 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001260 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001261 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1262 MipsII::MO_ABS_HI);
1263 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1264 MipsII::MO_ABS_LO);
1265 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1266 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001267 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001268 }
1269
Akira Hatanaka0f843822011-06-07 18:58:42 +00001270 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1271 MipsII::MO_GOT);
1272 GA = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, GA);
1273 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
1274 DAG.getEntryNode(), GA, MachinePointerInfo(),
1275 false, false, 0);
1276 // On functions and global targets not internal linked only
1277 // a load from got/GP is necessary for PIC to work.
1278 if (!GV->hasInternalLinkage() &&
1279 (!GV->hasLocalLinkage() || isa<Function>(GV)))
1280 return ResNode;
1281 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1282 MipsII::MO_ABS_LO);
1283 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
1284 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001285}
1286
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001287SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1288 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001289 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1290 // FIXME there isn't actually debug info here
1291 DebugLoc dl = Op.getDebugLoc();
1292
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001293 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001294 // %hi/%lo relocation
1295 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
1296 MipsII::MO_ABS_HI);
1297 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
1298 MipsII::MO_ABS_LO);
1299 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1300 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1301 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001302 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001303
1304 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1305 MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001306 BAGOTOffset = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, BAGOTOffset);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001307 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1308 MipsII::MO_ABS_LO);
1309 SDValue Load = DAG.getLoad(MVT::i32, dl,
1310 DAG.getEntryNode(), BAGOTOffset,
1311 MachinePointerInfo(), false, false, 0);
1312 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
1313 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001314}
1315
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001316SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001317LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001318{
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001319 // If the relocation model is PIC, use the General Dynamic TLS Model,
1320 // otherwise use the Initial Exec or Local Exec TLS Model.
1321 // TODO: implement Local Dynamic TLS model
1322
1323 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1324 DebugLoc dl = GA->getDebugLoc();
1325 const GlobalValue *GV = GA->getGlobal();
1326 EVT PtrVT = getPointerTy();
1327
1328 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1329 // General Dynamic TLS Model
1330 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001331 0, MipsII::MO_TLSGD);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001332 SDValue Tlsgd = DAG.getNode(MipsISD::TlsGd, dl, MVT::i32, TGA);
1333 SDValue GP = DAG.getRegister(Mips::GP, MVT::i32);
1334 SDValue Argument = DAG.getNode(ISD::ADD, dl, MVT::i32, GP, Tlsgd);
1335
1336 ArgListTy Args;
1337 ArgListEntry Entry;
1338 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001339 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001340 Args.push_back(Entry);
1341 std::pair<SDValue, SDValue> CallResult =
1342 LowerCallTo(DAG.getEntryNode(),
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001343 (Type *) Type::getInt32Ty(*DAG.getContext()),
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001344 false, false, false, false, 0, CallingConv::C, false, true,
1345 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG,
1346 dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001347
1348 return CallResult.first;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001349 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001350
1351 SDValue Offset;
1352 if (GV->isDeclaration()) {
1353 // Initial Exec TLS Model
1354 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1355 MipsII::MO_GOTTPREL);
1356 Offset = DAG.getLoad(MVT::i32, dl,
1357 DAG.getEntryNode(), TGA, MachinePointerInfo(),
1358 false, false, 0);
1359 } else {
1360 // Local Exec TLS Model
1361 SDVTList VTs = DAG.getVTList(MVT::i32);
1362 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1363 MipsII::MO_TPREL_HI);
1364 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1365 MipsII::MO_TPREL_LO);
1366 SDValue Hi = DAG.getNode(MipsISD::TprelHi, dl, VTs, &TGAHi, 1);
1367 SDValue Lo = DAG.getNode(MipsISD::TprelLo, dl, MVT::i32, TGALo);
1368 Offset = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1369 }
1370
1371 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1372 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001373}
1374
1375SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001376LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001377{
Dan Gohman475871a2008-07-27 21:46:04 +00001378 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001379 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +00001380 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001381 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001382 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001383 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001384
Owen Andersone50ed302009-08-10 22:56:29 +00001385 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001386 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001387
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001388 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
1389
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +00001390 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001391 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001392 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001393 } else {// Emit Load from Global Pointer
1394 JTI = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, JTI);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001395 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
1396 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001397 false, false, 0);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001398 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001399
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001400 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1401 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001402 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001403 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001404
1405 return ResNode;
1406}
1407
Dan Gohman475871a2008-07-27 21:46:04 +00001408SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001409LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001410{
Dan Gohman475871a2008-07-27 21:46:04 +00001411 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001412 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001413 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001414 // FIXME there isn't actually debug info here
1415 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001416
1417 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001418 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001419 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001420 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001421 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001422 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001423 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1424 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001425 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001426
1427 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001428 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001429 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001430 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001431 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001432 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1433 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001434 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001435 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001436 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001437 N->getOffset(), MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001438 CP = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001439 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001440 CP, MachinePointerInfo::getConstantPool(),
1441 false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001442 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001443 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001444 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001445 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
1446 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001447
1448 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001449}
1450
Dan Gohmand858e902010-04-17 15:26:15 +00001451SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001452 MachineFunction &MF = DAG.getMachineFunction();
1453 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1454
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001455 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001456 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1457 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001458
1459 // vastart just stores the address of the VarArgsFrameIndex slot into the
1460 // memory location argument.
1461 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001462 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1463 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +00001464 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001465}
1466
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001467static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
1468 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
1469 DebugLoc dl = Op.getDebugLoc();
1470 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
1471 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
1472 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
1473 DAG.getConstant(0x7fffffff, MVT::i32));
1474 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
1475 DAG.getConstant(0x80000000, MVT::i32));
1476 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1477 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
1478}
1479
1480static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001481 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001482 // Use ext/ins instructions if target architecture is Mips32r2.
1483 // Eliminate redundant mfc1 and mtc1 instructions.
1484 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001485
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001486 if (!isLittle)
1487 std::swap(LoIdx, HiIdx);
1488
1489 DebugLoc dl = Op.getDebugLoc();
1490 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1491 Op.getOperand(0),
1492 DAG.getConstant(LoIdx, MVT::i32));
1493 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1494 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1495 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1496 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1497 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1498 DAG.getConstant(0x7fffffff, MVT::i32));
1499 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1500 DAG.getConstant(0x80000000, MVT::i32));
1501 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1502
1503 if (!isLittle)
1504 std::swap(Word0, Word1);
1505
1506 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1507}
1508
1509SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1510 const {
1511 EVT Ty = Op.getValueType();
1512
1513 assert(Ty == MVT::f32 || Ty == MVT::f64);
1514
1515 if (Ty == MVT::f32)
1516 return LowerFCOPYSIGN32(Op, DAG);
1517 else
1518 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
1519}
1520
Akira Hatanaka2e591472011-06-02 00:24:44 +00001521SDValue MipsTargetLowering::
1522LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001523 // check the depth
1524 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001525 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001526
1527 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1528 MFI->setFrameAddressIsTaken(true);
1529 EVT VT = Op.getValueType();
1530 DebugLoc dl = Op.getDebugLoc();
1531 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Mips::FP, VT);
1532 return FrameAddr;
1533}
1534
Akira Hatanakadb548262011-07-19 23:30:50 +00001535// TODO: set SType according to the desired memory barrier behavior.
1536SDValue MipsTargetLowering::LowerMEMBARRIER(SDValue Op,
1537 SelectionDAG& DAG) const {
1538 unsigned SType = 0;
1539 DebugLoc dl = Op.getDebugLoc();
1540 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1541 DAG.getConstant(SType, MVT::i32));
1542}
1543
Eli Friedman14648462011-07-27 22:21:52 +00001544SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1545 SelectionDAG& DAG) const {
1546 // FIXME: Need pseudo-fence for 'singlethread' fences
1547 // FIXME: Set SType for weaker fences where supported/appropriate.
1548 unsigned SType = 0;
1549 DebugLoc dl = Op.getDebugLoc();
1550 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1551 DAG.getConstant(SType, MVT::i32));
1552}
1553
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001554//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001555// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001556//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001557
1558#include "MipsGenCallingConv.inc"
1559
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001560//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001561// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001562// Mips O32 ABI rules:
1563// ---
1564// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001565// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001566// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001567// f64 - Only passed in two aliased f32 registers if no int reg has been used
1568// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001569// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1570// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001571//
1572// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001573//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001574
Duncan Sands1e96bab2010-11-04 10:49:57 +00001575static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001576 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001577 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1578
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001579 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001580
1581 static const unsigned IntRegs[] = {
1582 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1583 };
1584 static const unsigned F32Regs[] = {
1585 Mips::F12, Mips::F14
1586 };
1587 static const unsigned F64Regs[] = {
1588 Mips::D6, Mips::D7
1589 };
1590
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001591 // ByVal Args
1592 if (ArgFlags.isByVal()) {
1593 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1594 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1595 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1596 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1597 r < std::min(IntRegsSize, NextReg); ++r)
1598 State.AllocateReg(IntRegs[r]);
1599 return false;
1600 }
1601
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001602 // Promote i8 and i16
1603 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1604 LocVT = MVT::i32;
1605 if (ArgFlags.isSExt())
1606 LocInfo = CCValAssign::SExt;
1607 else if (ArgFlags.isZExt())
1608 LocInfo = CCValAssign::ZExt;
1609 else
1610 LocInfo = CCValAssign::AExt;
1611 }
1612
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001613 unsigned Reg;
1614
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001615 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1616 // is true: function is vararg, argument is 3rd or higher, there is previous
1617 // argument which is not f32 or f64.
1618 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1619 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001620 unsigned OrigAlign = ArgFlags.getOrigAlign();
1621 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001622
1623 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001624 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001625 // If this is the first part of an i64 arg,
1626 // the allocated register must be either A0 or A2.
1627 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1628 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001629 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001630 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1631 // Allocate int register and shadow next int register. If first
1632 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001633 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1634 if (Reg == Mips::A1 || Reg == Mips::A3)
1635 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1636 State.AllocateReg(IntRegs, IntRegsSize);
1637 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001638 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1639 // we are guaranteed to find an available float register
1640 if (ValVT == MVT::f32) {
1641 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1642 // Shadow int register
1643 State.AllocateReg(IntRegs, IntRegsSize);
1644 } else {
1645 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1646 // Shadow int registers
1647 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1648 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1649 State.AllocateReg(IntRegs, IntRegsSize);
1650 State.AllocateReg(IntRegs, IntRegsSize);
1651 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001652 } else
1653 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001654
Akira Hatanakad37776d2011-05-20 21:39:54 +00001655 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1656 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1657
1658 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001659 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001660 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001661 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001662
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001663 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001664}
1665
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001666//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001667// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001668//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001669
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001670static const unsigned O32IntRegsSize = 4;
1671
1672static const unsigned O32IntRegs[] = {
1673 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1674};
1675
1676// Write ByVal Arg to arg registers and stack.
1677static void
1678WriteByValArg(SDValue& Chain, DebugLoc dl,
1679 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1680 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1681 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001682 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
1683 MVT PtrType) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001684 unsigned FirstWord = VA.getLocMemOffset() / 4;
1685 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
1686 unsigned LastWord = FirstWord + NumWords;
1687 unsigned CurWord;
1688
1689 // copy the first 4 words of byval arg to registers A0 - A3
1690 for (CurWord = FirstWord; CurWord < std::min(LastWord, O32IntRegsSize);
1691 ++CurWord) {
1692 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1693 DAG.getConstant((CurWord - FirstWord) * 4,
1694 MVT::i32));
1695 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1696 MachinePointerInfo(),
1697 false, false, 0);
1698 MemOpChains.push_back(LoadVal.getValue(1));
1699 unsigned DstReg = O32IntRegs[CurWord];
1700 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1701 }
1702
1703 // copy remaining part of byval arg to stack.
1704 if (CurWord < LastWord) {
Eric Christopher471e4222011-06-08 23:55:35 +00001705 unsigned SizeInBytes = (LastWord - CurWord) * 4;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001706 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1707 DAG.getConstant((CurWord - FirstWord) * 4,
1708 MVT::i32));
1709 LastFI = MFI->CreateFixedObject(SizeInBytes, CurWord * 4, true);
1710 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
1711 Chain = DAG.getMemcpy(Chain, dl, Dst, Src,
1712 DAG.getConstant(SizeInBytes, MVT::i32),
1713 /*Align*/4,
1714 /*isVolatile=*/false, /*AlwaysInline=*/false,
1715 MachinePointerInfo(0), MachinePointerInfo(0));
1716 MemOpChains.push_back(Chain);
1717 }
1718}
1719
Dan Gohman98ca4f22009-08-05 01:29:28 +00001720/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001721/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001722/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001723SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001724MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001725 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001726 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001727 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001728 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001729 const SmallVectorImpl<ISD::InputArg> &Ins,
1730 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001731 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001732 // MIPs target does not yet support tail call optimization.
1733 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001734
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001735 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001736 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00001737 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001738 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001739 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001740
1741 // Analyze operands of the call, assigning locations to each operand.
1742 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001743 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1744 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001745
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001746 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001747 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001748 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001749 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001750
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001751 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001752 unsigned NextStackOffset = CCInfo.getNextStackOffset();
1753
1754 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NextStackOffset,
1755 true));
1756
1757 // If this is the first call, create a stack frame object that points to
1758 // a location to which .cprestore saves $gp.
1759 if (IsPIC && !MipsFI->getGPFI())
1760 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
1761
Akira Hatanaka21afc632011-06-21 00:40:49 +00001762 // Get the frame index of the stack frame object that points to the location
1763 // of dynamically allocated area on the stack.
1764 int DynAllocFI = MipsFI->getDynAllocFI();
1765
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001766 // Update size of the maximum argument space.
1767 // For O32, a minimum of four words (16 bytes) of argument space is
1768 // allocated.
1769 if (Subtarget->isABI_O32())
1770 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
1771
1772 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1773
1774 if (MaxCallFrameSize < NextStackOffset) {
1775 MipsFI->setMaxCallFrameSize(NextStackOffset);
1776
Akira Hatanaka21afc632011-06-21 00:40:49 +00001777 // Set the offsets relative to $sp of the $gp restore slot and dynamically
1778 // allocated stack space. These offsets must be aligned to a boundary
1779 // determined by the stack alignment of the ABI.
1780 unsigned StackAlignment = TFL->getStackAlignment();
1781 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
1782 StackAlignment * StackAlignment;
1783
1784 if (IsPIC)
1785 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
1786
1787 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001788 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001789
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001790 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001791 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1792 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001793
Eric Christopher471e4222011-06-08 23:55:35 +00001794 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00001795
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001796 // Walk the register/memloc assignments, inserting copies/loads.
1797 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001798 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001799 CCValAssign &VA = ArgLocs[i];
1800
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001801 // Promote the value if needed.
1802 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001803 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001804 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001805 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001806 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001807 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001808 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001809 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1810 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001811 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1812 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00001813 if (!Subtarget->isLittle())
1814 std::swap(Lo, Hi);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001815 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
1816 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
1817 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001818 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001819 }
1820 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00001821 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001822 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001823 break;
1824 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001825 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001826 break;
1827 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001828 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001829 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001830 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001831
1832 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001833 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001834 if (VA.isRegLoc()) {
1835 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00001836 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001837 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001838
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001839 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00001840 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001841
Eric Christopher471e4222011-06-08 23:55:35 +00001842 // ByVal Arg.
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001843 ISD::ArgFlagsTy Flags = Outs[i].Flags;
1844 if (Flags.isByVal()) {
1845 assert(Subtarget->isABI_O32() &&
1846 "No support for ByVal args by ABIs other than O32 yet.");
1847 assert(Flags.getByValSize() &&
1848 "ByVal args of size 0 should have been ignored by front-end.");
1849 WriteByValArg(Chain, dl, RegsToPass, MemOpChains, LastFI, MFI, DAG, Arg,
1850 VA, Flags, getPointerTy());
1851 continue;
1852 }
1853
Chris Lattnere0b12152008-03-17 06:57:02 +00001854 // Create the frame index object for this incoming parameter
Eric Christopher471e4222011-06-08 23:55:35 +00001855 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00001856 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00001857 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00001858
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001859 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00001860 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00001861 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
1862 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001863 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001864 }
1865
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001866 // Extend range of indices of frame objects for outgoing arguments that were
1867 // created during this function call. Skip this step if no such objects were
1868 // created.
1869 if (LastFI)
1870 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
1871
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001872 // Transform all store nodes into one single node because all store
1873 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001874 if (!MemOpChains.empty())
1875 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001876 &MemOpChains[0], MemOpChains.size());
1877
Bill Wendling056292f2008-09-16 21:48:12 +00001878 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001879 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1880 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001881 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001882 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001883 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001884
1885 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001886 if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
1887 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1888 getPointerTy(), 0,MipsII:: MO_GOT);
1889 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
1890 0, MipsII::MO_ABS_LO);
1891 } else {
1892 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1893 getPointerTy(), 0, OpFlag);
1894 }
1895
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001896 LoadSymAddr = true;
1897 }
1898 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001899 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001900 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001901 LoadSymAddr = true;
1902 }
1903
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00001904 SDValue InFlag;
1905
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001906 // Create nodes that load address of callee and copy it to T9
1907 if (IsPIC) {
1908 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001909 // Load callee address
Akira Hatanaka342837d2011-05-28 01:07:07 +00001910 Callee = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, Callee);
Akira Hatanaka25eba392011-06-24 19:01:25 +00001911 SDValue LoadValue = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), Callee,
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00001912 MachinePointerInfo::getGOT(),
1913 false, false, 0);
1914
1915 // Use GOT+LO if callee has internal linkage.
1916 if (CalleeLo.getNode()) {
1917 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
1918 Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
1919 } else
1920 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00001921 }
1922
1923 // copy to T9
1924 Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
1925 InFlag = Chain.getValue(1);
1926 Callee = DAG.getRegister(Mips::T9, MVT::i32);
1927 }
Bill Wendling056292f2008-09-16 21:48:12 +00001928
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00001929 // Build a sequence of copy-to-reg nodes chained together with token
1930 // chain and flag operands which copy the outgoing args into registers.
1931 // The InFlag in necessary since all emitted instructions must be
1932 // stuck together.
1933 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1934 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1935 RegsToPass[i].second, InFlag);
1936 InFlag = Chain.getValue(1);
1937 }
1938
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001939 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001940 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001941 //
1942 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001943 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00001944 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001945 Ops.push_back(Chain);
1946 Ops.push_back(Callee);
1947
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001948 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001949 // known live into the call.
1950 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1951 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1952 RegsToPass[i].second.getValueType()));
1953
Gabor Greifba36cb52008-08-28 21:40:38 +00001954 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001955 Ops.push_back(InFlag);
1956
Dale Johannesen33c960f2009-02-04 20:06:27 +00001957 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001958 InFlag = Chain.getValue(1);
1959
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00001960 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001961 Chain = DAG.getCALLSEQ_END(Chain,
1962 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00001963 DAG.getIntPtrConstant(0, true), InFlag);
1964 InFlag = Chain.getValue(1);
1965
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001966 // Handle result values, copying them out of physregs into vregs that we
1967 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001968 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
1969 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001970}
1971
Dan Gohman98ca4f22009-08-05 01:29:28 +00001972/// LowerCallResult - Lower the result values of a call into the
1973/// appropriate copies out of appropriate physical registers.
1974SDValue
1975MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001976 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001977 const SmallVectorImpl<ISD::InputArg> &Ins,
1978 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001979 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001980 // Assign locations to each value returned by this call.
1981 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001982 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1983 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001984
Dan Gohman98ca4f22009-08-05 01:29:28 +00001985 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001986
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001987 // Copy all of the result registers out of their specified physreg.
1988 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00001989 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00001990 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001991 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001992 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001993 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00001994
Dan Gohman98ca4f22009-08-05 01:29:28 +00001995 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001996}
1997
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001998//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001999// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002000//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002001static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2002 std::vector<SDValue>& OutChains,
2003 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2004 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2005 unsigned LocMem = VA.getLocMemOffset();
2006 unsigned FirstWord = LocMem / 4;
2007
2008 // copy register A0 - A3 to frame object
2009 for (unsigned i = 0; i < NumWords; ++i) {
2010 unsigned CurWord = FirstWord + i;
2011 if (CurWord >= O32IntRegsSize)
2012 break;
2013
2014 unsigned SrcReg = O32IntRegs[CurWord];
2015 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2016 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2017 DAG.getConstant(i * 4, MVT::i32));
2018 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2019 StorePtr, MachinePointerInfo(), false,
2020 false, 0);
2021 OutChains.push_back(Store);
2022 }
2023}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002024
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002025/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002026/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002027SDValue
2028MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002029 CallingConv::ID CallConv,
2030 bool isVarArg,
2031 const SmallVectorImpl<ISD::InputArg>
2032 &Ins,
2033 DebugLoc dl, SelectionDAG &DAG,
2034 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002035 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002036 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002037 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002038 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002039
Dan Gohman1e93df62010-04-17 14:41:14 +00002040 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002041
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002042 // Used with vargs to acumulate store chains.
2043 std::vector<SDValue> OutChains;
2044
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002045 // Assign locations to all of the incoming arguments.
2046 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002047 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2048 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002049
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002050 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002051 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002052 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002053 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002054
Akira Hatanaka43299772011-05-20 23:22:14 +00002055 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002056
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002057 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002058 CCValAssign &VA = ArgLocs[i];
2059
2060 // Arguments stored on registers
2061 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002062 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002063 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002064 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002065
Owen Anderson825b72b2009-08-11 20:47:22 +00002066 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002067 RC = Mips::CPURegsRegisterClass;
2068 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002069 RC = Mips::FGR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002070 else if (RegVT == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002071 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002072 RC = Mips::AFGR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002073 } else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002074 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002075
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002076 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002077 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002078 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002079 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002080
2081 // If this is an 8 or 16-bit value, it has been passed promoted
2082 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002083 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002084 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002085 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002086 if (VA.getLocInfo() == CCValAssign::SExt)
2087 Opcode = ISD::AssertSext;
2088 else if (VA.getLocInfo() == CCValAssign::ZExt)
2089 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002090 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002091 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00002092 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00002093 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002094 }
2095
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002096 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002097 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002098 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
2099 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00002100 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002101 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002102 VA.getLocReg()+1, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002103 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002104 if (!Subtarget->isLittle())
2105 std::swap(ArgValue, ArgValue2);
2106 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2107 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002108 }
2109 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002110
Dan Gohman98ca4f22009-08-05 01:29:28 +00002111 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002112 } else { // VA.isRegLoc()
2113
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002114 // sanity check
2115 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002116
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002117 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2118
2119 if (Flags.isByVal()) {
2120 assert(Subtarget->isABI_O32() &&
2121 "No support for ByVal args by ABIs other than O32 yet.");
2122 assert(Flags.getByValSize() &&
2123 "ByVal args of size 0 should have been ignored by front-end.");
2124 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2125 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2126 true);
2127 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2128 InVals.push_back(FIN);
2129 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2130
2131 continue;
2132 }
2133
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002134 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002135 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
2136 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002137
2138 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002139 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002140 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002141 MachinePointerInfo::getFixedStack(LastFI),
David Greenef6fa1862010-02-15 16:56:10 +00002142 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002143 }
2144 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002145
2146 // The mips ABIs for returning structs by value requires that we copy
2147 // the sret argument into $v0 for the return. Save the argument into
2148 // a virtual register so that we can access it from the return points.
2149 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2150 unsigned Reg = MipsFI->getSRetReturnReg();
2151 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002152 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002153 MipsFI->setSRetReturnReg(Reg);
2154 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002155 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002156 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002157 }
2158
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002159 if (isVarArg && Subtarget->isABI_O32()) {
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002160 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002161 // which is a value necessary to VASTART.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002162 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002163 assert(NextStackOffset % 4 == 0 &&
2164 "NextStackOffset must be aligned to 4-byte boundaries.");
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002165 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
2166 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002167
2168 // If NextStackOffset is smaller than o32's 16-byte reserved argument area,
2169 // copy the integer registers that have not been used for argument passing
2170 // to the caller's stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002171 for (; NextStackOffset < 16; NextStackOffset += 4) {
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002172 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002173 unsigned Idx = NextStackOffset / 4;
2174 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), O32IntRegs[Idx], RC);
2175 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
Akira Hatanaka69c19f72011-05-23 20:16:59 +00002176 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002177 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2178 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2179 MachinePointerInfo(),
2180 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002181 }
2182 }
2183
Akira Hatanaka43299772011-05-20 23:22:14 +00002184 MipsFI->setLastInArgFI(LastFI);
2185
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002186 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002187 // the size of Ins and InVals. This only happens when on varg functions
2188 if (!OutChains.empty()) {
2189 OutChains.push_back(Chain);
2190 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2191 &OutChains[0], OutChains.size());
2192 }
2193
Dan Gohman98ca4f22009-08-05 01:29:28 +00002194 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002195}
2196
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002197//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002198// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002199//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002200
Dan Gohman98ca4f22009-08-05 01:29:28 +00002201SDValue
2202MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002203 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002204 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002205 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002206 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002207
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002208 // CCValAssign - represent the assignment of
2209 // the return value to a location
2210 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002211
2212 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002213 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2214 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002215
Dan Gohman98ca4f22009-08-05 01:29:28 +00002216 // Analize return values.
2217 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002218
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002219 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002220 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002221 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002222 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002223 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002224 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002225 }
2226
Dan Gohman475871a2008-07-27 21:46:04 +00002227 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002228
2229 // Copy the result values into the output registers.
2230 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2231 CCValAssign &VA = RVLocs[i];
2232 assert(VA.isRegLoc() && "Can only return in registers!");
2233
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002234 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002235 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002236
2237 // guarantee that all emitted copies are
2238 // stuck together, avoiding something bad
2239 Flag = Chain.getValue(1);
2240 }
2241
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002242 // The mips ABIs for returning structs by value requires that we copy
2243 // the sret argument into $v0 for the return. We saved the argument into
2244 // a virtual register in the entry block, so now we copy the value out
2245 // and into $v0.
2246 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2247 MachineFunction &MF = DAG.getMachineFunction();
2248 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2249 unsigned Reg = MipsFI->getSRetReturnReg();
2250
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002251 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002252 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002253 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002254
Dale Johannesena05dca42009-02-04 23:02:30 +00002255 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002256 Flag = Chain.getValue(1);
2257 }
2258
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002259 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002260 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002261 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002262 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002263 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002264 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002265 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002266}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002267
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002268//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002269// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002270//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002271
2272/// getConstraintType - Given a constraint letter, return the type of
2273/// constraint it is for this target.
2274MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002275getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002276{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002277 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002278 // GCC config/mips/constraints.md
2279 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002280 // 'd' : An address register. Equivalent to r
2281 // unless generating MIPS16 code.
2282 // 'y' : Equivalent to r; retained for
2283 // backwards compatibility.
2284 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002285 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002286 switch (Constraint[0]) {
2287 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002288 case 'd':
2289 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002290 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002291 return C_RegisterClass;
2292 break;
2293 }
2294 }
2295 return TargetLowering::getConstraintType(Constraint);
2296}
2297
John Thompson44ab89e2010-10-29 17:29:13 +00002298/// Examine constraint type and operand type and determine a weight value.
2299/// This object must already have been set up with the operand type
2300/// and the current alternative constraint selected.
2301TargetLowering::ConstraintWeight
2302MipsTargetLowering::getSingleConstraintMatchWeight(
2303 AsmOperandInfo &info, const char *constraint) const {
2304 ConstraintWeight weight = CW_Invalid;
2305 Value *CallOperandVal = info.CallOperandVal;
2306 // If we don't have a value, we can't do a match,
2307 // but allow it at the lowest weight.
2308 if (CallOperandVal == NULL)
2309 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002310 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002311 // Look at the constraint type.
2312 switch (*constraint) {
2313 default:
2314 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2315 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002316 case 'd':
2317 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002318 if (type->isIntegerTy())
2319 weight = CW_Register;
2320 break;
2321 case 'f':
2322 if (type->isFloatTy())
2323 weight = CW_Register;
2324 break;
2325 }
2326 return weight;
2327}
2328
Eric Christopher38d64262011-06-29 19:33:04 +00002329/// Given a register class constraint, like 'r', if this corresponds directly
2330/// to an LLVM register class, return a register of 0 and the register class
2331/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002332std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002333getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002334{
2335 if (Constraint.size() == 1) {
2336 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002337 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2338 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002339 case 'r':
2340 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002341 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002342 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002343 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002344 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002345 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2346 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002347 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002348 }
2349 }
2350 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2351}
2352
Dan Gohman6520e202008-10-18 02:06:02 +00002353bool
2354MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2355 // The Mips target isn't yet aware of offsets.
2356 return false;
2357}
Evan Chengeb2f9692009-10-27 19:56:55 +00002358
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002359bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2360 if (VT != MVT::f32 && VT != MVT::f64)
2361 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002362 if (Imm.isNegZero())
2363 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002364 return Imm.isZero();
2365}