blob: 9ddf66cb9f0d7065103b7c1ee2e5f2f9f67cdd2d [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
Akira Hatanakadbe9a312011-08-18 20:07:42 +000038// If I is a shifted mask, set the size (Size) and the first bit of the
39// mask (Pos), and return true.
Akira Hatanaka854a7db2011-08-19 22:59:00 +000040// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
41static bool IsShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
42 if (!isUInt<32>(I) || !isShiftedMask_32(I))
43 return false;
Akira Hatanakabb15e112011-08-17 02:05:42 +000044
Akira Hatanaka854a7db2011-08-19 22:59:00 +000045 Size = CountPopulation_32(I);
46 Pos = CountTrailingZeros_32(I);
Akira Hatanakadbe9a312011-08-18 20:07:42 +000047 return true;
Akira Hatanakabb15e112011-08-17 02:05:42 +000048}
49
Chris Lattnerf0144122009-07-28 03:13:23 +000050const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
51 switch (Opcode) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000052 case MipsISD::JmpLink: return "MipsISD::JmpLink";
53 case MipsISD::Hi: return "MipsISD::Hi";
54 case MipsISD::Lo: return "MipsISD::Lo";
55 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +000056 case MipsISD::TlsGd: return "MipsISD::TlsGd";
57 case MipsISD::TprelHi: return "MipsISD::TprelHi";
58 case MipsISD::TprelLo: return "MipsISD::TprelLo";
59 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000060 case MipsISD::Ret: return "MipsISD::Ret";
61 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
62 case MipsISD::FPCmp: return "MipsISD::FPCmp";
63 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
64 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
65 case MipsISD::FPRound: return "MipsISD::FPRound";
66 case MipsISD::MAdd: return "MipsISD::MAdd";
67 case MipsISD::MAddu: return "MipsISD::MAddu";
68 case MipsISD::MSub: return "MipsISD::MSub";
69 case MipsISD::MSubu: return "MipsISD::MSubu";
70 case MipsISD::DivRem: return "MipsISD::DivRem";
71 case MipsISD::DivRemU: return "MipsISD::DivRemU";
72 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
73 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanaka342837d2011-05-28 01:07:07 +000074 case MipsISD::WrapperPIC: return "MipsISD::WrapperPIC";
Akira Hatanaka21afc632011-06-21 00:40:49 +000075 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
Akira Hatanakadb548262011-07-19 23:30:50 +000076 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanakabb15e112011-08-17 02:05:42 +000077 case MipsISD::Ext: return "MipsISD::Ext";
78 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanaka0f843822011-06-07 18:58:42 +000079 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000080 }
81}
82
83MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000084MipsTargetLowering(MipsTargetMachine &TM)
Chris Lattnerb71b9092009-08-13 06:28:06 +000085 : TargetLowering(TM, new MipsTargetObjectFile()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000086 Subtarget = &TM.getSubtarget<MipsSubtarget>();
87
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000088 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000089 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000090 setBooleanContents(ZeroOrOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000091
92 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000093 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
94 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000095
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000096 // When dealing with single precision only, use libcalls
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +000097 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000098 if (!Subtarget->isFP64bit())
Owen Anderson825b72b2009-08-11 20:47:22 +000099 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000100
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000101 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000102 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
103 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
104 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000105
Eli Friedman6055a6a2009-07-17 04:07:24 +0000106 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000107 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
108 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000109
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000110 // Used by legalize types to correctly generate the setcc result.
111 // Without this, every float setcc comes with a AND/OR with the result,
112 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000113 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000114 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000115
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000116 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +0000117 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000118 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000119 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
120 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
121 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
122 setOperationAction(ISD::SELECT, MVT::f32, Custom);
123 setOperationAction(ISD::SELECT, MVT::f64, Custom);
124 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
126 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000127 setOperationAction(ISD::VASTART, MVT::Other, Custom);
128
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000129 setOperationAction(ISD::SDIV, MVT::i32, Expand);
130 setOperationAction(ISD::SREM, MVT::i32, Expand);
131 setOperationAction(ISD::UDIV, MVT::i32, Expand);
132 setOperationAction(ISD::UREM, MVT::i32, Expand);
133
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000134 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
136 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
137 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
138 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
139 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
140 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
141 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
142 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
143 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000144
145 if (!Subtarget->isMips32r2())
146 setOperationAction(ISD::ROTR, MVT::i32, Expand);
147
Owen Anderson825b72b2009-08-11 20:47:22 +0000148 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
149 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
150 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000151 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
152 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000153 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000154 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000155 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000156 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000157 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
158 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000159 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 setOperationAction(ISD::FLOG, MVT::f32, Expand);
161 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
162 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
163 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000164 setOperationAction(ISD::FMA, MVT::f32, Expand);
165 setOperationAction(ISD::FMA, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000166
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000167 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
168 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Eric Christopher471e4222011-06-08 23:55:35 +0000169
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000170 setOperationAction(ISD::VAARG, MVT::Other, Expand);
171 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
172 setOperationAction(ISD::VAEND, MVT::Other, Expand);
173
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000174 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000175 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
176 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000177
Akira Hatanakadb548262011-07-19 23:30:50 +0000178 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000179 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000180
Eli Friedman26689ac2011-08-03 21:06:02 +0000181 setInsertFencesForAtomic(true);
182
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000183 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000184 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000185
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000186 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000187 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
188 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000189 }
190
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000191 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000193
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000194 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000195 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000196
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000197 setTargetDAGCombine(ISD::ADDE);
198 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000199 setTargetDAGCombine(ISD::SDIVREM);
200 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000201 setTargetDAGCombine(ISD::SETCC);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000202 setTargetDAGCombine(ISD::AND);
203 setTargetDAGCombine(ISD::OR);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000204
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000205 setMinFunctionAlignment(2);
206
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000207 setStackPointerRegisterToSaveRestore(Mips::SP);
208 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000209
210 setExceptionPointerRegister(Mips::A0);
211 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000212}
213
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000214bool MipsTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Akira Hatanaka511961a2011-08-17 18:49:18 +0000215 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
216 return SVT == MVT::i32 || SVT == MVT::i16;
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000217}
218
Owen Anderson825b72b2009-08-11 20:47:22 +0000219MVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
220 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000221}
222
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000223// SelectMadd -
224// Transforms a subgraph in CurDAG if the following pattern is found:
225// (addc multLo, Lo0), (adde multHi, Hi0),
226// where,
227// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000228// Lo0: initial value of Lo register
229// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000230// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000231static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000232 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000233 // for the matching to be successful.
234 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
235
236 if (ADDCNode->getOpcode() != ISD::ADDC)
237 return false;
238
239 SDValue MultHi = ADDENode->getOperand(0);
240 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000241 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000242 unsigned MultOpc = MultHi.getOpcode();
243
244 // MultHi and MultLo must be generated by the same node,
245 if (MultLo.getNode() != MultNode)
246 return false;
247
248 // and it must be a multiplication.
249 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
250 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000251
252 // MultLo amd MultHi must be the first and second output of MultNode
253 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000254 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
255 return false;
256
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000257 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000258 // of the values of MultNode, in which case MultNode will be removed in later
259 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000260 // If there exist users other than ADDENode or ADDCNode, this function returns
261 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000262 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000263 // produced.
264 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
265 return false;
266
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000267 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000268 DebugLoc dl = ADDENode->getDebugLoc();
269
270 // create MipsMAdd(u) node
271 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000272
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000273 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
274 MVT::Glue,
275 MultNode->getOperand(0),// Factor 0
276 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000277 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000278 ADDENode->getOperand(1));// Hi0
279
280 // create CopyFromReg nodes
281 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
282 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000283 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000284 Mips::HI, MVT::i32,
285 CopyFromLo.getValue(2));
286
287 // replace uses of adde and addc here
288 if (!SDValue(ADDCNode, 0).use_empty())
289 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
290
291 if (!SDValue(ADDENode, 0).use_empty())
292 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
293
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000294 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000295}
296
297// SelectMsub -
298// Transforms a subgraph in CurDAG if the following pattern is found:
299// (addc Lo0, multLo), (sube Hi0, multHi),
300// where,
301// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000302// Lo0: initial value of Lo register
303// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000304// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000305static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000306 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000307 // for the matching to be successful.
308 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
309
310 if (SUBCNode->getOpcode() != ISD::SUBC)
311 return false;
312
313 SDValue MultHi = SUBENode->getOperand(1);
314 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000315 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000316 unsigned MultOpc = MultHi.getOpcode();
317
318 // MultHi and MultLo must be generated by the same node,
319 if (MultLo.getNode() != MultNode)
320 return false;
321
322 // and it must be a multiplication.
323 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
324 return false;
325
326 // MultLo amd MultHi must be the first and second output of MultNode
327 // respectively.
328 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
329 return false;
330
331 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
332 // of the values of MultNode, in which case MultNode will be removed in later
333 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000334 // If there exist users other than SUBENode or SUBCNode, this function returns
335 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000336 // instruction node rather than a pair of MULT and MSUB instructions being
337 // produced.
338 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
339 return false;
340
341 SDValue Chain = CurDAG->getEntryNode();
342 DebugLoc dl = SUBENode->getDebugLoc();
343
344 // create MipsSub(u) node
345 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
346
347 SDValue MSub = CurDAG->getNode(MultOpc, dl,
348 MVT::Glue,
349 MultNode->getOperand(0),// Factor 0
350 MultNode->getOperand(1),// Factor 1
351 SUBCNode->getOperand(0),// Lo0
352 SUBENode->getOperand(0));// Hi0
353
354 // create CopyFromReg nodes
355 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
356 MSub);
357 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
358 Mips::HI, MVT::i32,
359 CopyFromLo.getValue(2));
360
361 // replace uses of sube and subc here
362 if (!SDValue(SUBCNode, 0).use_empty())
363 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
364
365 if (!SDValue(SUBENode, 0).use_empty())
366 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
367
368 return true;
369}
370
371static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
372 TargetLowering::DAGCombinerInfo &DCI,
373 const MipsSubtarget* Subtarget) {
374 if (DCI.isBeforeLegalize())
375 return SDValue();
376
377 if (Subtarget->isMips32() && SelectMadd(N, &DAG))
378 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000379
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000380 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000381}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000382
383static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
384 TargetLowering::DAGCombinerInfo &DCI,
385 const MipsSubtarget* Subtarget) {
386 if (DCI.isBeforeLegalize())
387 return SDValue();
388
389 if (Subtarget->isMips32() && SelectMsub(N, &DAG))
390 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000391
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000392 return SDValue();
393}
394
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000395static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
396 TargetLowering::DAGCombinerInfo &DCI,
397 const MipsSubtarget* Subtarget) {
398 if (DCI.isBeforeLegalizeOps())
399 return SDValue();
400
401 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
402 MipsISD::DivRemU;
403 DebugLoc dl = N->getDebugLoc();
404
405 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
406 N->getOperand(0), N->getOperand(1));
407 SDValue InChain = DAG.getEntryNode();
408 SDValue InGlue = DivRem;
409
410 // insert MFLO
411 if (N->hasAnyUseOfValue(0)) {
412 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, Mips::LO, MVT::i32,
413 InGlue);
414 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
415 InChain = CopyFromLo.getValue(1);
416 InGlue = CopyFromLo.getValue(2);
417 }
418
419 // insert MFHI
420 if (N->hasAnyUseOfValue(1)) {
421 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000422 Mips::HI, MVT::i32, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000423 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
424 }
425
426 return SDValue();
427}
428
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000429static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
430 switch (CC) {
431 default: llvm_unreachable("Unknown fp condition code!");
432 case ISD::SETEQ:
433 case ISD::SETOEQ: return Mips::FCOND_OEQ;
434 case ISD::SETUNE: return Mips::FCOND_UNE;
435 case ISD::SETLT:
436 case ISD::SETOLT: return Mips::FCOND_OLT;
437 case ISD::SETGT:
438 case ISD::SETOGT: return Mips::FCOND_OGT;
439 case ISD::SETLE:
440 case ISD::SETOLE: return Mips::FCOND_OLE;
441 case ISD::SETGE:
442 case ISD::SETOGE: return Mips::FCOND_OGE;
443 case ISD::SETULT: return Mips::FCOND_ULT;
444 case ISD::SETULE: return Mips::FCOND_ULE;
445 case ISD::SETUGT: return Mips::FCOND_UGT;
446 case ISD::SETUGE: return Mips::FCOND_UGE;
447 case ISD::SETUO: return Mips::FCOND_UN;
448 case ISD::SETO: return Mips::FCOND_OR;
449 case ISD::SETNE:
450 case ISD::SETONE: return Mips::FCOND_ONE;
451 case ISD::SETUEQ: return Mips::FCOND_UEQ;
452 }
453}
454
455
456// Returns true if condition code has to be inverted.
457static bool InvertFPCondCode(Mips::CondCode CC) {
458 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
459 return false;
460
461 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
462 return true;
463
464 assert(false && "Illegal Condition Code");
465 return false;
466}
467
468// Creates and returns an FPCmp node from a setcc node.
469// Returns Op if setcc is not a floating point comparison.
470static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
471 // must be a SETCC node
472 if (Op.getOpcode() != ISD::SETCC)
473 return Op;
474
475 SDValue LHS = Op.getOperand(0);
476
477 if (!LHS.getValueType().isFloatingPoint())
478 return Op;
479
480 SDValue RHS = Op.getOperand(1);
481 DebugLoc dl = Op.getDebugLoc();
482
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000483 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
484 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000485 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
486
487 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
488 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
489}
490
491// Creates and returns a CMovFPT/F node.
492static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
493 SDValue False, DebugLoc DL) {
494 bool invert = InvertFPCondCode((Mips::CondCode)
495 cast<ConstantSDNode>(Cond.getOperand(2))
496 ->getSExtValue());
497
498 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
499 True.getValueType(), True, False, Cond);
500}
501
502static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
503 TargetLowering::DAGCombinerInfo &DCI,
504 const MipsSubtarget* Subtarget) {
505 if (DCI.isBeforeLegalizeOps())
506 return SDValue();
507
508 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
509
510 if (Cond.getOpcode() != MipsISD::FPCmp)
511 return SDValue();
512
513 SDValue True = DAG.getConstant(1, MVT::i32);
514 SDValue False = DAG.getConstant(0, MVT::i32);
515
516 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
517}
518
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000519static SDValue PerformANDCombine(SDNode *N, SelectionDAG& DAG,
520 TargetLowering::DAGCombinerInfo &DCI,
521 const MipsSubtarget* Subtarget) {
522 // Pattern match EXT.
523 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
524 // => ext $dst, $src, size, pos
525 if (DCI.isBeforeLegalizeOps() || !Subtarget->isMips32r2())
526 return SDValue();
527
528 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
529
530 // Op's first operand must be a shift right.
531 if (ShiftRight.getOpcode() != ISD::SRA && ShiftRight.getOpcode() != ISD::SRL)
532 return SDValue();
533
534 // The second operand of the shift must be an immediate.
535 uint64_t Pos;
536 ConstantSDNode *CN;
537 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
538 return SDValue();
539
540 Pos = CN->getZExtValue();
541
542 uint64_t SMPos, SMSize;
543 // Op's second operand must be a shifted mask.
544 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000545 !IsShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000546 return SDValue();
547
548 // Return if the shifted mask does not start at bit 0 or the sum of its size
549 // and Pos exceeds the word's size.
550 if (SMPos != 0 || Pos + SMSize > 32)
551 return SDValue();
552
553 return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), MVT::i32,
554 ShiftRight.getOperand(0),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000555 DAG.getConstant(Pos, MVT::i32),
556 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000557}
558
559static SDValue PerformORCombine(SDNode *N, SelectionDAG& DAG,
560 TargetLowering::DAGCombinerInfo &DCI,
561 const MipsSubtarget* Subtarget) {
562 // Pattern match INS.
563 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
564 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
565 // => ins $dst, $src, size, pos, $src1
566 if (DCI.isBeforeLegalizeOps() || !Subtarget->isMips32r2())
567 return SDValue();
568
569 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
570 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
571 ConstantSDNode *CN;
572
573 // See if Op's first operand matches (and $src1 , mask0).
574 if (And0.getOpcode() != ISD::AND)
575 return SDValue();
576
577 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000578 !IsShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000579 return SDValue();
580
581 // See if Op's second operand matches (and (shl $src, pos), mask1).
582 if (And1.getOpcode() != ISD::AND)
583 return SDValue();
584
585 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000586 !IsShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000587 return SDValue();
588
589 // The shift masks must have the same position and size.
590 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
591 return SDValue();
592
593 SDValue Shl = And1.getOperand(0);
594 if (Shl.getOpcode() != ISD::SHL)
595 return SDValue();
596
597 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
598 return SDValue();
599
600 unsigned Shamt = CN->getZExtValue();
601
602 // Return if the shift amount and the first bit position of mask are not the
603 // same.
604 if (Shamt != SMPos0)
605 return SDValue();
606
607 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), MVT::i32,
608 Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000609 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000610 DAG.getConstant(SMSize0, MVT::i32),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000611 And0.getOperand(0));
612}
613
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000614SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000615 const {
616 SelectionDAG &DAG = DCI.DAG;
617 unsigned opc = N->getOpcode();
618
619 switch (opc) {
620 default: break;
621 case ISD::ADDE:
622 return PerformADDECombine(N, DAG, DCI, Subtarget);
623 case ISD::SUBE:
624 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000625 case ISD::SDIVREM:
626 case ISD::UDIVREM:
627 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000628 case ISD::SETCC:
629 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000630 case ISD::AND:
631 return PerformANDCombine(N, DAG, DCI, Subtarget);
632 case ISD::OR:
633 return PerformORCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000634 }
635
636 return SDValue();
637}
638
Dan Gohman475871a2008-07-27 21:46:04 +0000639SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000640LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000641{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000642 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000643 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000644 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000645 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
646 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000647 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000648 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000649 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
650 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000651 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000652 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000653 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000654 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Akira Hatanakadb548262011-07-19 23:30:50 +0000655 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +0000656 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000657 }
Dan Gohman475871a2008-07-27 21:46:04 +0000658 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000659}
660
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000661//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000662// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000663//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000664
665// AddLiveIn - This helper function adds the specified physical register to the
666// MachineFunction as a live in value. It also creates a corresponding
667// virtual register for it.
668static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000669AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000670{
671 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000672 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
673 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000674 return VReg;
675}
676
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000677// Get fp branch code (not opcode) from condition code.
678static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
679 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
680 return Mips::BRANCH_T;
681
682 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
683 return Mips::BRANCH_F;
684
685 return Mips::BRANCH_INVALID;
686}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000687
Akira Hatanaka14487d42011-06-07 19:28:39 +0000688static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
689 DebugLoc dl,
690 const MipsSubtarget* Subtarget,
691 const TargetInstrInfo *TII,
692 bool isFPCmp, unsigned Opc) {
693 // There is no need to expand CMov instructions if target has
694 // conditional moves.
695 if (Subtarget->hasCondMov())
696 return BB;
697
698 // To "insert" a SELECT_CC instruction, we actually have to insert the
699 // diamond control-flow pattern. The incoming instruction knows the
700 // destination vreg to set, the condition code register to branch on, the
701 // true/false values to select between, and a branch opcode to use.
702 const BasicBlock *LLVM_BB = BB->getBasicBlock();
703 MachineFunction::iterator It = BB;
704 ++It;
705
706 // thisMBB:
707 // ...
708 // TrueVal = ...
709 // setcc r1, r2, r3
710 // bNE r1, r0, copy1MBB
711 // fallthrough --> copy0MBB
712 MachineBasicBlock *thisMBB = BB;
713 MachineFunction *F = BB->getParent();
714 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
715 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
716 F->insert(It, copy0MBB);
717 F->insert(It, sinkMBB);
718
719 // Transfer the remainder of BB and its successor edges to sinkMBB.
720 sinkMBB->splice(sinkMBB->begin(), BB,
721 llvm::next(MachineBasicBlock::iterator(MI)),
722 BB->end());
723 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
724
725 // Next, add the true and fallthrough blocks as its successors.
726 BB->addSuccessor(copy0MBB);
727 BB->addSuccessor(sinkMBB);
728
729 // Emit the right instruction according to the type of the operands compared
730 if (isFPCmp)
731 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
732 else
733 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
734 .addReg(Mips::ZERO).addMBB(sinkMBB);
735
736 // copy0MBB:
737 // %FalseValue = ...
738 // # fallthrough to sinkMBB
739 BB = copy0MBB;
740
741 // Update machine-CFG edges
742 BB->addSuccessor(sinkMBB);
743
744 // sinkMBB:
745 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
746 // ...
747 BB = sinkMBB;
748
749 if (isFPCmp)
750 BuildMI(*BB, BB->begin(), dl,
751 TII->get(Mips::PHI), MI->getOperand(0).getReg())
752 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
753 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
754 else
755 BuildMI(*BB, BB->begin(), dl,
756 TII->get(Mips::PHI), MI->getOperand(0).getReg())
757 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
758 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
759
760 MI->eraseFromParent(); // The pseudo instruction is gone now.
761 return BB;
762}
763
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000764MachineBasicBlock *
765MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000766 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000767 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen94817572009-02-13 02:34:39 +0000768 DebugLoc dl = MI->getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000769
770 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000771 default:
772 assert(false && "Unexpected instr type to insert");
773 return NULL;
774 case Mips::MOVT:
775 case Mips::MOVT_S:
776 case Mips::MOVT_D:
777 return ExpandCondMov(MI, BB, dl, Subtarget, TII, true, Mips::BC1F);
778 case Mips::MOVF:
779 case Mips::MOVF_S:
780 case Mips::MOVF_D:
781 return ExpandCondMov(MI, BB, dl, Subtarget, TII, true, Mips::BC1T);
782 case Mips::MOVZ_I:
783 case Mips::MOVZ_S:
784 case Mips::MOVZ_D:
785 return ExpandCondMov(MI, BB, dl, Subtarget, TII, false, Mips::BNE);
786 case Mips::MOVN_I:
787 case Mips::MOVN_S:
788 case Mips::MOVN_D:
789 return ExpandCondMov(MI, BB, dl, Subtarget, TII, false, Mips::BEQ);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000790
791 case Mips::ATOMIC_LOAD_ADD_I8:
792 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
793 case Mips::ATOMIC_LOAD_ADD_I16:
794 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
795 case Mips::ATOMIC_LOAD_ADD_I32:
796 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
797
798 case Mips::ATOMIC_LOAD_AND_I8:
799 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
800 case Mips::ATOMIC_LOAD_AND_I16:
801 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
802 case Mips::ATOMIC_LOAD_AND_I32:
803 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
804
805 case Mips::ATOMIC_LOAD_OR_I8:
806 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
807 case Mips::ATOMIC_LOAD_OR_I16:
808 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
809 case Mips::ATOMIC_LOAD_OR_I32:
810 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
811
812 case Mips::ATOMIC_LOAD_XOR_I8:
813 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
814 case Mips::ATOMIC_LOAD_XOR_I16:
815 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
816 case Mips::ATOMIC_LOAD_XOR_I32:
817 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
818
819 case Mips::ATOMIC_LOAD_NAND_I8:
820 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
821 case Mips::ATOMIC_LOAD_NAND_I16:
822 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
823 case Mips::ATOMIC_LOAD_NAND_I32:
824 return EmitAtomicBinary(MI, BB, 4, 0, true);
825
826 case Mips::ATOMIC_LOAD_SUB_I8:
827 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
828 case Mips::ATOMIC_LOAD_SUB_I16:
829 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
830 case Mips::ATOMIC_LOAD_SUB_I32:
831 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
832
833 case Mips::ATOMIC_SWAP_I8:
834 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
835 case Mips::ATOMIC_SWAP_I16:
836 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
837 case Mips::ATOMIC_SWAP_I32:
838 return EmitAtomicBinary(MI, BB, 4, 0);
839
840 case Mips::ATOMIC_CMP_SWAP_I8:
841 return EmitAtomicCmpSwapPartword(MI, BB, 1);
842 case Mips::ATOMIC_CMP_SWAP_I16:
843 return EmitAtomicCmpSwapPartword(MI, BB, 2);
844 case Mips::ATOMIC_CMP_SWAP_I32:
845 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000846 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000847}
848
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000849// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
850// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
851MachineBasicBlock *
852MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000853 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000854 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000855 assert(Size == 4 && "Unsupported size for EmitAtomicBinary.");
856
857 MachineFunction *MF = BB->getParent();
858 MachineRegisterInfo &RegInfo = MF->getRegInfo();
859 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
860 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
861 DebugLoc dl = MI->getDebugLoc();
862
Akira Hatanaka4061da12011-07-19 20:11:17 +0000863 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000864 unsigned Ptr = MI->getOperand(1).getReg();
865 unsigned Incr = MI->getOperand(2).getReg();
866
Akira Hatanaka4061da12011-07-19 20:11:17 +0000867 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
868 unsigned AndRes = RegInfo.createVirtualRegister(RC);
869 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000870
871 // insert new blocks after the current block
872 const BasicBlock *LLVM_BB = BB->getBasicBlock();
873 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
874 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
875 MachineFunction::iterator It = BB;
876 ++It;
877 MF->insert(It, loopMBB);
878 MF->insert(It, exitMBB);
879
880 // Transfer the remainder of BB and its successor edges to exitMBB.
881 exitMBB->splice(exitMBB->begin(), BB,
882 llvm::next(MachineBasicBlock::iterator(MI)),
883 BB->end());
884 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
885
886 // thisMBB:
887 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000888 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000889 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000890 loopMBB->addSuccessor(loopMBB);
891 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000892
893 // loopMBB:
894 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000895 // <binop> storeval, oldval, incr
896 // sc success, storeval, 0(ptr)
897 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000898 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000899 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000900 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000901 // and andres, oldval, incr
902 // nor storeval, $0, andres
903 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr);
904 BuildMI(BB, dl, TII->get(Mips::NOR), StoreVal)
905 .addReg(Mips::ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000906 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000907 // <binop> storeval, oldval, incr
908 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000909 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000910 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000911 }
Akira Hatanaka4061da12011-07-19 20:11:17 +0000912 BuildMI(BB, dl, TII->get(Mips::SC), Success)
913 .addReg(StoreVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000914 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +0000915 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000916
917 MI->eraseFromParent(); // The instruction is gone now.
918
Akira Hatanaka939ece12011-07-19 03:42:13 +0000919 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000920}
921
922MachineBasicBlock *
923MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000924 MachineBasicBlock *BB,
925 unsigned Size, unsigned BinOpcode,
926 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000927 assert((Size == 1 || Size == 2) &&
928 "Unsupported size for EmitAtomicBinaryPartial.");
929
930 MachineFunction *MF = BB->getParent();
931 MachineRegisterInfo &RegInfo = MF->getRegInfo();
932 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
933 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
934 DebugLoc dl = MI->getDebugLoc();
935
936 unsigned Dest = MI->getOperand(0).getReg();
937 unsigned Ptr = MI->getOperand(1).getReg();
938 unsigned Incr = MI->getOperand(2).getReg();
939
Akira Hatanaka4061da12011-07-19 20:11:17 +0000940 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
941 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000942 unsigned Mask = RegInfo.createVirtualRegister(RC);
943 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000944 unsigned NewVal = RegInfo.createVirtualRegister(RC);
945 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000946 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000947 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
948 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
949 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
950 unsigned AndRes = RegInfo.createVirtualRegister(RC);
951 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +0000952 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000953 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
954 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
955 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
956 unsigned SllRes = RegInfo.createVirtualRegister(RC);
957 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000958
959 // insert new blocks after the current block
960 const BasicBlock *LLVM_BB = BB->getBasicBlock();
961 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000962 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000963 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
964 MachineFunction::iterator It = BB;
965 ++It;
966 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000967 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000968 MF->insert(It, exitMBB);
969
970 // Transfer the remainder of BB and its successor edges to exitMBB.
971 exitMBB->splice(exitMBB->begin(), BB,
972 llvm::next(MachineBasicBlock::iterator(MI)),
973 BB->end());
974 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
975
Akira Hatanaka81b44112011-07-19 17:09:53 +0000976 BB->addSuccessor(loopMBB);
977 loopMBB->addSuccessor(loopMBB);
978 loopMBB->addSuccessor(sinkMBB);
979 sinkMBB->addSuccessor(exitMBB);
980
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000981 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +0000982 // addiu masklsb2,$0,-4 # 0xfffffffc
983 // and alignedaddr,ptr,masklsb2
984 // andi ptrlsb2,ptr,3
985 // sll shiftamt,ptrlsb2,3
986 // ori maskupper,$0,255 # 0xff
987 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000988 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +0000989 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000990
991 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000992 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
993 .addReg(Mips::ZERO).addImm(-4);
994 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
995 .addReg(Ptr).addReg(MaskLSB2);
996 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
997 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
998 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
999 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001000 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1001 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001002 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001003 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001004
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001005
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001006 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001007 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001008 // ll oldval,0(alignedaddr)
1009 // binop binopres,oldval,incr2
1010 // and newval,binopres,mask
1011 // and maskedoldval0,oldval,mask2
1012 // or storeval,maskedoldval0,newval
1013 // sc success,storeval,0(alignedaddr)
1014 // beq success,$0,loopMBB
1015
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001016 // atomic.swap
1017 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001018 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001019 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001020 // and maskedoldval0,oldval,mask2
1021 // or storeval,maskedoldval0,newval
1022 // sc success,storeval,0(alignedaddr)
1023 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001024
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001025 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001026 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001027 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001028 // and andres, oldval, incr2
1029 // nor binopres, $0, andres
1030 // and newval, binopres, mask
1031 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1032 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1033 .addReg(Mips::ZERO).addReg(AndRes);
1034 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001035 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001036 // <binop> binopres, oldval, incr2
1037 // and newval, binopres, mask
1038 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1039 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001040 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001041 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001042 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001043 }
1044
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001045 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001046 .addReg(OldVal).addReg(Mask2);
1047 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001048 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001049 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1050 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001051 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001052 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001053
Akira Hatanaka939ece12011-07-19 03:42:13 +00001054 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001055 // and maskedoldval1,oldval,mask
1056 // srl srlres,maskedoldval1,shiftamt
1057 // sll sllres,srlres,24
1058 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001059 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001060 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001061
Akira Hatanaka4061da12011-07-19 20:11:17 +00001062 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1063 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001064 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1065 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001066 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1067 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001068 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001069 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001070
1071 MI->eraseFromParent(); // The instruction is gone now.
1072
Akira Hatanaka939ece12011-07-19 03:42:13 +00001073 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001074}
1075
1076MachineBasicBlock *
1077MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001078 MachineBasicBlock *BB,
1079 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001080 assert(Size == 4 && "Unsupported size for EmitAtomicCmpSwap.");
1081
1082 MachineFunction *MF = BB->getParent();
1083 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1084 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1085 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1086 DebugLoc dl = MI->getDebugLoc();
1087
1088 unsigned Dest = MI->getOperand(0).getReg();
1089 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001090 unsigned OldVal = MI->getOperand(2).getReg();
1091 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001092
Akira Hatanaka4061da12011-07-19 20:11:17 +00001093 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001094
1095 // insert new blocks after the current block
1096 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1097 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1098 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1099 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1100 MachineFunction::iterator It = BB;
1101 ++It;
1102 MF->insert(It, loop1MBB);
1103 MF->insert(It, loop2MBB);
1104 MF->insert(It, exitMBB);
1105
1106 // Transfer the remainder of BB and its successor edges to exitMBB.
1107 exitMBB->splice(exitMBB->begin(), BB,
1108 llvm::next(MachineBasicBlock::iterator(MI)),
1109 BB->end());
1110 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1111
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001112 // thisMBB:
1113 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001114 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001115 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001116 loop1MBB->addSuccessor(exitMBB);
1117 loop1MBB->addSuccessor(loop2MBB);
1118 loop2MBB->addSuccessor(loop1MBB);
1119 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001120
1121 // loop1MBB:
1122 // ll dest, 0(ptr)
1123 // bne dest, oldval, exitMBB
1124 BB = loop1MBB;
Akira Hatanakad3ac47f2011-07-07 18:57:00 +00001125 BuildMI(BB, dl, TII->get(Mips::LL), Dest).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001126 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001127 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001128
1129 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001130 // sc success, newval, 0(ptr)
1131 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001132 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001133 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1134 .addReg(NewVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001135 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001136 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001137
1138 MI->eraseFromParent(); // The instruction is gone now.
1139
Akira Hatanaka939ece12011-07-19 03:42:13 +00001140 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001141}
1142
1143MachineBasicBlock *
1144MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001145 MachineBasicBlock *BB,
1146 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001147 assert((Size == 1 || Size == 2) &&
1148 "Unsupported size for EmitAtomicCmpSwapPartial.");
1149
1150 MachineFunction *MF = BB->getParent();
1151 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1152 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1153 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1154 DebugLoc dl = MI->getDebugLoc();
1155
1156 unsigned Dest = MI->getOperand(0).getReg();
1157 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001158 unsigned CmpVal = MI->getOperand(2).getReg();
1159 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001160
Akira Hatanaka4061da12011-07-19 20:11:17 +00001161 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1162 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001163 unsigned Mask = RegInfo.createVirtualRegister(RC);
1164 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001165 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1166 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1167 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1168 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1169 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1170 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1171 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1172 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1173 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1174 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1175 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1176 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1177 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1178 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001179
1180 // insert new blocks after the current block
1181 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1182 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1183 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001184 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001185 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1186 MachineFunction::iterator It = BB;
1187 ++It;
1188 MF->insert(It, loop1MBB);
1189 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001190 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001191 MF->insert(It, exitMBB);
1192
1193 // Transfer the remainder of BB and its successor edges to exitMBB.
1194 exitMBB->splice(exitMBB->begin(), BB,
1195 llvm::next(MachineBasicBlock::iterator(MI)),
1196 BB->end());
1197 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1198
Akira Hatanaka81b44112011-07-19 17:09:53 +00001199 BB->addSuccessor(loop1MBB);
1200 loop1MBB->addSuccessor(sinkMBB);
1201 loop1MBB->addSuccessor(loop2MBB);
1202 loop2MBB->addSuccessor(loop1MBB);
1203 loop2MBB->addSuccessor(sinkMBB);
1204 sinkMBB->addSuccessor(exitMBB);
1205
Akira Hatanaka70564a92011-07-19 18:14:26 +00001206 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001207 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001208 // addiu masklsb2,$0,-4 # 0xfffffffc
1209 // and alignedaddr,ptr,masklsb2
1210 // andi ptrlsb2,ptr,3
1211 // sll shiftamt,ptrlsb2,3
1212 // ori maskupper,$0,255 # 0xff
1213 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001214 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001215 // andi maskedcmpval,cmpval,255
1216 // sll shiftedcmpval,maskedcmpval,shiftamt
1217 // andi maskednewval,newval,255
1218 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001219 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001220 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1221 .addReg(Mips::ZERO).addImm(-4);
1222 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1223 .addReg(Ptr).addReg(MaskLSB2);
1224 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1225 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1226 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1227 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001228 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1229 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001230 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001231 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1232 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001233 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1234 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001235 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1236 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001237 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1238 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001239
1240 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001241 // ll oldval,0(alginedaddr)
1242 // and maskedoldval0,oldval,mask
1243 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001244 BB = loop1MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001245 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
1246 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1247 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001248 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001249 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001250
1251 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001252 // and maskedoldval1,oldval,mask2
1253 // or storeval,maskedoldval1,shiftednewval
1254 // sc success,storeval,0(alignedaddr)
1255 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001256 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001257 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1258 .addReg(OldVal).addReg(Mask2);
1259 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1260 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1261 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1262 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001263 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001264 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001265
Akira Hatanaka939ece12011-07-19 03:42:13 +00001266 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001267 // srl srlres,maskedoldval0,shiftamt
1268 // sll sllres,srlres,24
1269 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001270 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001271 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001272
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001273 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1274 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001275 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1276 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001277 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001278 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001279
1280 MI->eraseFromParent(); // The instruction is gone now.
1281
Akira Hatanaka939ece12011-07-19 03:42:13 +00001282 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001283}
1284
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001285//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001286// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001287//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001288SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001289LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001290{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001291 MachineFunction &MF = DAG.getMachineFunction();
1292 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1293
1294 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001295 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1296 "Cannot lower if the alignment of the allocated space is larger than \
1297 that of the stack.");
1298
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001299 SDValue Chain = Op.getOperand(0);
1300 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001301 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001302
1303 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +00001304 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001305
1306 // Subtract the dynamic size from the actual stack size to
1307 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +00001308 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001309
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001310 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001311 // must be placed in the stack pointer register.
Akira Hatanaka053546c2011-05-25 02:20:00 +00001312 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub,
1313 SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001314
1315 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001316 // value and a chain
Akira Hatanaka21afc632011-06-21 00:40:49 +00001317 SDVTList VTLs = DAG.getVTList(MVT::i32, MVT::Other);
1318 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1319 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1320
1321 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001322}
1323
1324SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001325LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001326{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001327 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001328 // the block to branch to if the condition is true.
1329 SDValue Chain = Op.getOperand(0);
1330 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001331 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001332
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001333 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1334
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001335 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001336 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001337 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001338
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001339 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001340 Mips::CondCode CC =
1341 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001342 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001343
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001344 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001345 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001346}
1347
1348SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001349LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001350{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001351 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001352
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001353 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001354 if (Cond.getOpcode() != MipsISD::FPCmp)
1355 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001356
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001357 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1358 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001359}
1360
Dan Gohmand858e902010-04-17 15:26:15 +00001361SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1362 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001363 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001364 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001365 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001366
Eli Friedmane2c74082009-08-03 02:22:28 +00001367 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001368 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001369
Chris Lattnerb71b9092009-08-13 06:28:06 +00001370 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001371
Chris Lattnere3736f82009-08-13 05:41:27 +00001372 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001373 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1374 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001375 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001376 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1377 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001378 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001379 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001380 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001381 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1382 MipsII::MO_ABS_HI);
1383 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1384 MipsII::MO_ABS_LO);
1385 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1386 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001387 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001388 }
1389
Akira Hatanaka0f843822011-06-07 18:58:42 +00001390 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1391 MipsII::MO_GOT);
1392 GA = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, GA);
1393 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
1394 DAG.getEntryNode(), GA, MachinePointerInfo(),
1395 false, false, 0);
1396 // On functions and global targets not internal linked only
1397 // a load from got/GP is necessary for PIC to work.
1398 if (!GV->hasInternalLinkage() &&
1399 (!GV->hasLocalLinkage() || isa<Function>(GV)))
1400 return ResNode;
1401 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1402 MipsII::MO_ABS_LO);
1403 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
1404 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001405}
1406
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001407SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1408 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001409 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1410 // FIXME there isn't actually debug info here
1411 DebugLoc dl = Op.getDebugLoc();
1412
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001413 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001414 // %hi/%lo relocation
1415 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
1416 MipsII::MO_ABS_HI);
1417 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
1418 MipsII::MO_ABS_LO);
1419 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1420 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1421 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001422 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001423
1424 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1425 MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001426 BAGOTOffset = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, BAGOTOffset);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001427 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1428 MipsII::MO_ABS_LO);
1429 SDValue Load = DAG.getLoad(MVT::i32, dl,
1430 DAG.getEntryNode(), BAGOTOffset,
1431 MachinePointerInfo(), false, false, 0);
1432 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
1433 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001434}
1435
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001436SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001437LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001438{
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001439 // If the relocation model is PIC, use the General Dynamic TLS Model,
1440 // otherwise use the Initial Exec or Local Exec TLS Model.
1441 // TODO: implement Local Dynamic TLS model
1442
1443 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1444 DebugLoc dl = GA->getDebugLoc();
1445 const GlobalValue *GV = GA->getGlobal();
1446 EVT PtrVT = getPointerTy();
1447
1448 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1449 // General Dynamic TLS Model
1450 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001451 0, MipsII::MO_TLSGD);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001452 SDValue Tlsgd = DAG.getNode(MipsISD::TlsGd, dl, MVT::i32, TGA);
1453 SDValue GP = DAG.getRegister(Mips::GP, MVT::i32);
1454 SDValue Argument = DAG.getNode(ISD::ADD, dl, MVT::i32, GP, Tlsgd);
1455
1456 ArgListTy Args;
1457 ArgListEntry Entry;
1458 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001459 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001460 Args.push_back(Entry);
1461 std::pair<SDValue, SDValue> CallResult =
1462 LowerCallTo(DAG.getEntryNode(),
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001463 (Type *) Type::getInt32Ty(*DAG.getContext()),
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001464 false, false, false, false, 0, CallingConv::C, false, true,
1465 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG,
1466 dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001467
1468 return CallResult.first;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001469 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001470
1471 SDValue Offset;
1472 if (GV->isDeclaration()) {
1473 // Initial Exec TLS Model
1474 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1475 MipsII::MO_GOTTPREL);
1476 Offset = DAG.getLoad(MVT::i32, dl,
1477 DAG.getEntryNode(), TGA, MachinePointerInfo(),
1478 false, false, 0);
1479 } else {
1480 // Local Exec TLS Model
1481 SDVTList VTs = DAG.getVTList(MVT::i32);
1482 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1483 MipsII::MO_TPREL_HI);
1484 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1485 MipsII::MO_TPREL_LO);
1486 SDValue Hi = DAG.getNode(MipsISD::TprelHi, dl, VTs, &TGAHi, 1);
1487 SDValue Lo = DAG.getNode(MipsISD::TprelLo, dl, MVT::i32, TGALo);
1488 Offset = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1489 }
1490
1491 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1492 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001493}
1494
1495SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001496LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001497{
Dan Gohman475871a2008-07-27 21:46:04 +00001498 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001499 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +00001500 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001501 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001502 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001503 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001504
Owen Andersone50ed302009-08-10 22:56:29 +00001505 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001506 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001507
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001508 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
1509
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +00001510 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001511 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001512 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001513 } else {// Emit Load from Global Pointer
1514 JTI = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, JTI);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001515 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
1516 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001517 false, false, 0);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001518 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001519
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001520 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1521 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001522 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001523 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001524
1525 return ResNode;
1526}
1527
Dan Gohman475871a2008-07-27 21:46:04 +00001528SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001529LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001530{
Dan Gohman475871a2008-07-27 21:46:04 +00001531 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001532 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001533 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001534 // FIXME there isn't actually debug info here
1535 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001536
1537 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001538 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001539 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001540 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001541 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001542 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001543 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1544 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001545 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001546
1547 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001548 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001549 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001550 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001551 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001552 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1553 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001554 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001555 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001556 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001557 N->getOffset(), MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001558 CP = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001559 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001560 CP, MachinePointerInfo::getConstantPool(),
1561 false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001562 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001563 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001564 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001565 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
1566 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001567
1568 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001569}
1570
Dan Gohmand858e902010-04-17 15:26:15 +00001571SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001572 MachineFunction &MF = DAG.getMachineFunction();
1573 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1574
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001575 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001576 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1577 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001578
1579 // vastart just stores the address of the VarArgsFrameIndex slot into the
1580 // memory location argument.
1581 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001582 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1583 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +00001584 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001585}
1586
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001587static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
1588 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
1589 DebugLoc dl = Op.getDebugLoc();
1590 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
1591 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
1592 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
1593 DAG.getConstant(0x7fffffff, MVT::i32));
1594 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
1595 DAG.getConstant(0x80000000, MVT::i32));
1596 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1597 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
1598}
1599
1600static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001601 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001602 // Use ext/ins instructions if target architecture is Mips32r2.
1603 // Eliminate redundant mfc1 and mtc1 instructions.
1604 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001605
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001606 if (!isLittle)
1607 std::swap(LoIdx, HiIdx);
1608
1609 DebugLoc dl = Op.getDebugLoc();
1610 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1611 Op.getOperand(0),
1612 DAG.getConstant(LoIdx, MVT::i32));
1613 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1614 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1615 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1616 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1617 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1618 DAG.getConstant(0x7fffffff, MVT::i32));
1619 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1620 DAG.getConstant(0x80000000, MVT::i32));
1621 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1622
1623 if (!isLittle)
1624 std::swap(Word0, Word1);
1625
1626 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1627}
1628
1629SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1630 const {
1631 EVT Ty = Op.getValueType();
1632
1633 assert(Ty == MVT::f32 || Ty == MVT::f64);
1634
1635 if (Ty == MVT::f32)
1636 return LowerFCOPYSIGN32(Op, DAG);
1637 else
1638 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
1639}
1640
Akira Hatanaka2e591472011-06-02 00:24:44 +00001641SDValue MipsTargetLowering::
1642LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001643 // check the depth
1644 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001645 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001646
1647 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1648 MFI->setFrameAddressIsTaken(true);
1649 EVT VT = Op.getValueType();
1650 DebugLoc dl = Op.getDebugLoc();
1651 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Mips::FP, VT);
1652 return FrameAddr;
1653}
1654
Akira Hatanakadb548262011-07-19 23:30:50 +00001655// TODO: set SType according to the desired memory barrier behavior.
1656SDValue MipsTargetLowering::LowerMEMBARRIER(SDValue Op,
1657 SelectionDAG& DAG) const {
1658 unsigned SType = 0;
1659 DebugLoc dl = Op.getDebugLoc();
1660 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1661 DAG.getConstant(SType, MVT::i32));
1662}
1663
Eli Friedman14648462011-07-27 22:21:52 +00001664SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1665 SelectionDAG& DAG) const {
1666 // FIXME: Need pseudo-fence for 'singlethread' fences
1667 // FIXME: Set SType for weaker fences where supported/appropriate.
1668 unsigned SType = 0;
1669 DebugLoc dl = Op.getDebugLoc();
1670 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1671 DAG.getConstant(SType, MVT::i32));
1672}
1673
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001674//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001675// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001676//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001677
1678#include "MipsGenCallingConv.inc"
1679
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001680//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001681// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001682// Mips O32 ABI rules:
1683// ---
1684// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001685// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001686// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001687// f64 - Only passed in two aliased f32 registers if no int reg has been used
1688// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001689// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1690// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001691//
1692// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001693//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001694
Duncan Sands1e96bab2010-11-04 10:49:57 +00001695static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001696 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001697 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1698
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001699 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001700
1701 static const unsigned IntRegs[] = {
1702 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1703 };
1704 static const unsigned F32Regs[] = {
1705 Mips::F12, Mips::F14
1706 };
1707 static const unsigned F64Regs[] = {
1708 Mips::D6, Mips::D7
1709 };
1710
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001711 // ByVal Args
1712 if (ArgFlags.isByVal()) {
1713 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1714 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1715 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1716 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1717 r < std::min(IntRegsSize, NextReg); ++r)
1718 State.AllocateReg(IntRegs[r]);
1719 return false;
1720 }
1721
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001722 // Promote i8 and i16
1723 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1724 LocVT = MVT::i32;
1725 if (ArgFlags.isSExt())
1726 LocInfo = CCValAssign::SExt;
1727 else if (ArgFlags.isZExt())
1728 LocInfo = CCValAssign::ZExt;
1729 else
1730 LocInfo = CCValAssign::AExt;
1731 }
1732
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001733 unsigned Reg;
1734
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001735 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1736 // is true: function is vararg, argument is 3rd or higher, there is previous
1737 // argument which is not f32 or f64.
1738 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1739 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001740 unsigned OrigAlign = ArgFlags.getOrigAlign();
1741 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001742
1743 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001744 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001745 // If this is the first part of an i64 arg,
1746 // the allocated register must be either A0 or A2.
1747 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1748 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001749 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001750 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1751 // Allocate int register and shadow next int register. If first
1752 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001753 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1754 if (Reg == Mips::A1 || Reg == Mips::A3)
1755 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1756 State.AllocateReg(IntRegs, IntRegsSize);
1757 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001758 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1759 // we are guaranteed to find an available float register
1760 if (ValVT == MVT::f32) {
1761 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1762 // Shadow int register
1763 State.AllocateReg(IntRegs, IntRegsSize);
1764 } else {
1765 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1766 // Shadow int registers
1767 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1768 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1769 State.AllocateReg(IntRegs, IntRegsSize);
1770 State.AllocateReg(IntRegs, IntRegsSize);
1771 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001772 } else
1773 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001774
Akira Hatanakad37776d2011-05-20 21:39:54 +00001775 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1776 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1777
1778 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001779 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001780 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001781 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001782
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001783 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001784}
1785
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001786//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001787// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001788//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001789
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001790static const unsigned O32IntRegsSize = 4;
1791
1792static const unsigned O32IntRegs[] = {
1793 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1794};
1795
1796// Write ByVal Arg to arg registers and stack.
1797static void
1798WriteByValArg(SDValue& Chain, DebugLoc dl,
1799 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1800 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1801 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001802 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001803 MVT PtrType, bool isLittle) {
1804 unsigned LocMemOffset = VA.getLocMemOffset();
1805 unsigned Offset = 0;
1806 uint32_t RemainingSize = Flags.getByValSize();
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001807 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001808
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001809 // Copy the first 4 words of byval arg to registers A0 - A3.
1810 // FIXME: Use a stricter alignment if it enables better optimization in passes
1811 // run later.
1812 for (; RemainingSize >= 4 && LocMemOffset < 4 * 4;
1813 Offset += 4, RemainingSize -= 4, LocMemOffset += 4) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001814 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001815 DAG.getConstant(Offset, MVT::i32));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001816 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1817 MachinePointerInfo(),
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001818 false, false, std::min(ByValAlign,
1819 (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001820 MemOpChains.push_back(LoadVal.getValue(1));
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001821 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001822 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1823 }
1824
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001825 if (RemainingSize == 0)
1826 return;
1827
1828 // If there still is a register available for argument passing, write the
1829 // remaining part of the structure to it using subword loads and shifts.
1830 if (LocMemOffset < 4 * 4) {
1831 assert(RemainingSize <= 3 && RemainingSize >= 1 &&
1832 "There must be one to three bytes remaining.");
1833 unsigned LoadSize = (RemainingSize == 3 ? 2 : RemainingSize);
1834 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1835 DAG.getConstant(Offset, MVT::i32));
1836 unsigned Alignment = std::min(ByValAlign, (unsigned )4);
1837 SDValue LoadVal = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
1838 LoadPtr, MachinePointerInfo(),
1839 MVT::getIntegerVT(LoadSize * 8), false,
1840 false, Alignment);
1841 MemOpChains.push_back(LoadVal.getValue(1));
1842
1843 // If target is big endian, shift it to the most significant half-word or
1844 // byte.
1845 if (!isLittle)
1846 LoadVal = DAG.getNode(ISD::SHL, dl, MVT::i32, LoadVal,
1847 DAG.getConstant(32 - LoadSize * 8, MVT::i32));
1848
1849 Offset += LoadSize;
1850 RemainingSize -= LoadSize;
1851
1852 // Read second subword if necessary.
1853 if (RemainingSize != 0) {
1854 assert(RemainingSize == 1 && "There must be one byte remaining.");
1855 LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1856 DAG.getConstant(Offset, MVT::i32));
1857 unsigned Alignment = std::min(ByValAlign, (unsigned )2);
1858 SDValue Subword = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
1859 LoadPtr, MachinePointerInfo(),
1860 MVT::i8, false, false, Alignment);
1861 MemOpChains.push_back(Subword.getValue(1));
1862 // Insert the loaded byte to LoadVal.
1863 // FIXME: Use INS if supported by target.
1864 unsigned ShiftAmt = isLittle ? 16 : 8;
1865 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i32, Subword,
1866 DAG.getConstant(ShiftAmt, MVT::i32));
1867 LoadVal = DAG.getNode(ISD::OR, dl, MVT::i32, LoadVal, Shift);
1868 }
1869
1870 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
1871 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1872 return;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001873 }
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001874
1875 // Create a fixed object on stack at offset LocMemOffset and copy
1876 // remaining part of byval arg to it using memcpy.
1877 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1878 DAG.getConstant(Offset, MVT::i32));
1879 LastFI = MFI->CreateFixedObject(RemainingSize, LocMemOffset, true);
1880 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
1881 Chain = DAG.getMemcpy(Chain, dl, Dst, Src,
1882 DAG.getConstant(RemainingSize, MVT::i32),
1883 std::min(ByValAlign, (unsigned)4),
1884 /*isVolatile=*/false, /*AlwaysInline=*/false,
1885 MachinePointerInfo(0), MachinePointerInfo(0));
1886 MemOpChains.push_back(Chain);
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001887}
1888
Dan Gohman98ca4f22009-08-05 01:29:28 +00001889/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001890/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001891/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001892SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001893MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001894 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001895 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001896 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001897 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001898 const SmallVectorImpl<ISD::InputArg> &Ins,
1899 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001900 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001901 // MIPs target does not yet support tail call optimization.
1902 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001903
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001904 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001905 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00001906 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001907 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001908 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001909
1910 // Analyze operands of the call, assigning locations to each operand.
1911 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001912 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1913 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001914
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001915 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001916 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001917 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001918 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001919
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001920 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001921 unsigned NextStackOffset = CCInfo.getNextStackOffset();
1922
1923 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NextStackOffset,
1924 true));
1925
1926 // If this is the first call, create a stack frame object that points to
1927 // a location to which .cprestore saves $gp.
1928 if (IsPIC && !MipsFI->getGPFI())
1929 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
1930
Akira Hatanaka21afc632011-06-21 00:40:49 +00001931 // Get the frame index of the stack frame object that points to the location
1932 // of dynamically allocated area on the stack.
1933 int DynAllocFI = MipsFI->getDynAllocFI();
1934
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001935 // Update size of the maximum argument space.
1936 // For O32, a minimum of four words (16 bytes) of argument space is
1937 // allocated.
1938 if (Subtarget->isABI_O32())
1939 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
1940
1941 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1942
1943 if (MaxCallFrameSize < NextStackOffset) {
1944 MipsFI->setMaxCallFrameSize(NextStackOffset);
1945
Akira Hatanaka21afc632011-06-21 00:40:49 +00001946 // Set the offsets relative to $sp of the $gp restore slot and dynamically
1947 // allocated stack space. These offsets must be aligned to a boundary
1948 // determined by the stack alignment of the ABI.
1949 unsigned StackAlignment = TFL->getStackAlignment();
1950 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
1951 StackAlignment * StackAlignment;
1952
1953 if (IsPIC)
1954 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
1955
1956 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001957 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001958
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001959 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001960 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1961 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001962
Eric Christopher471e4222011-06-08 23:55:35 +00001963 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00001964
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001965 // Walk the register/memloc assignments, inserting copies/loads.
1966 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001967 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001968 CCValAssign &VA = ArgLocs[i];
1969
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001970 // Promote the value if needed.
1971 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001972 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001973 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001974 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001975 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001976 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001977 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001978 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1979 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001980 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1981 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00001982 if (!Subtarget->isLittle())
1983 std::swap(Lo, Hi);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001984 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
1985 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
1986 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001987 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001988 }
1989 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00001990 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001991 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001992 break;
1993 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001994 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001995 break;
1996 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001997 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001998 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001999 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002000
2001 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002002 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002003 if (VA.isRegLoc()) {
2004 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002005 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002006 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002007
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002008 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002009 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002010
Eric Christopher471e4222011-06-08 23:55:35 +00002011 // ByVal Arg.
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002012 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2013 if (Flags.isByVal()) {
2014 assert(Subtarget->isABI_O32() &&
2015 "No support for ByVal args by ABIs other than O32 yet.");
2016 assert(Flags.getByValSize() &&
2017 "ByVal args of size 0 should have been ignored by front-end.");
2018 WriteByValArg(Chain, dl, RegsToPass, MemOpChains, LastFI, MFI, DAG, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002019 VA, Flags, getPointerTy(), Subtarget->isLittle());
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002020 continue;
2021 }
2022
Chris Lattnere0b12152008-03-17 06:57:02 +00002023 // Create the frame index object for this incoming parameter
Eric Christopher471e4222011-06-08 23:55:35 +00002024 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002025 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00002026 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00002027
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002028 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002029 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00002030 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
2031 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00002032 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002033 }
2034
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002035 // Extend range of indices of frame objects for outgoing arguments that were
2036 // created during this function call. Skip this step if no such objects were
2037 // created.
2038 if (LastFI)
2039 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2040
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002041 // Transform all store nodes into one single node because all store
2042 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002043 if (!MemOpChains.empty())
2044 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002045 &MemOpChains[0], MemOpChains.size());
2046
Bill Wendling056292f2008-09-16 21:48:12 +00002047 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002048 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2049 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002050 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002051 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002052 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002053
2054 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002055 if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
2056 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2057 getPointerTy(), 0,MipsII:: MO_GOT);
2058 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
2059 0, MipsII::MO_ABS_LO);
2060 } else {
2061 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2062 getPointerTy(), 0, OpFlag);
2063 }
2064
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002065 LoadSymAddr = true;
2066 }
2067 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002068 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002069 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002070 LoadSymAddr = true;
2071 }
2072
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002073 SDValue InFlag;
2074
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002075 // Create nodes that load address of callee and copy it to T9
2076 if (IsPIC) {
2077 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002078 // Load callee address
Akira Hatanaka342837d2011-05-28 01:07:07 +00002079 Callee = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, Callee);
Akira Hatanaka25eba392011-06-24 19:01:25 +00002080 SDValue LoadValue = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), Callee,
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002081 MachinePointerInfo::getGOT(),
2082 false, false, 0);
2083
2084 // Use GOT+LO if callee has internal linkage.
2085 if (CalleeLo.getNode()) {
2086 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
2087 Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
2088 } else
2089 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002090 }
2091
2092 // copy to T9
2093 Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
2094 InFlag = Chain.getValue(1);
2095 Callee = DAG.getRegister(Mips::T9, MVT::i32);
2096 }
Bill Wendling056292f2008-09-16 21:48:12 +00002097
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002098 // Build a sequence of copy-to-reg nodes chained together with token
2099 // chain and flag operands which copy the outgoing args into registers.
2100 // The InFlag in necessary since all emitted instructions must be
2101 // stuck together.
2102 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2103 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2104 RegsToPass[i].second, InFlag);
2105 InFlag = Chain.getValue(1);
2106 }
2107
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002108 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002109 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002110 //
2111 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002112 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002113 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002114 Ops.push_back(Chain);
2115 Ops.push_back(Callee);
2116
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002117 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002118 // known live into the call.
2119 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2120 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2121 RegsToPass[i].second.getValueType()));
2122
Gabor Greifba36cb52008-08-28 21:40:38 +00002123 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002124 Ops.push_back(InFlag);
2125
Dale Johannesen33c960f2009-02-04 20:06:27 +00002126 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002127 InFlag = Chain.getValue(1);
2128
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002129 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002130 Chain = DAG.getCALLSEQ_END(Chain,
2131 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002132 DAG.getIntPtrConstant(0, true), InFlag);
2133 InFlag = Chain.getValue(1);
2134
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002135 // Handle result values, copying them out of physregs into vregs that we
2136 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002137 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2138 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002139}
2140
Dan Gohman98ca4f22009-08-05 01:29:28 +00002141/// LowerCallResult - Lower the result values of a call into the
2142/// appropriate copies out of appropriate physical registers.
2143SDValue
2144MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002145 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002146 const SmallVectorImpl<ISD::InputArg> &Ins,
2147 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002148 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002149 // Assign locations to each value returned by this call.
2150 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002151 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2152 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002153
Dan Gohman98ca4f22009-08-05 01:29:28 +00002154 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002155
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002156 // Copy all of the result registers out of their specified physreg.
2157 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002158 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002159 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002160 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002161 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002162 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002163
Dan Gohman98ca4f22009-08-05 01:29:28 +00002164 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002165}
2166
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002167//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002168// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002169//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002170static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2171 std::vector<SDValue>& OutChains,
2172 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2173 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2174 unsigned LocMem = VA.getLocMemOffset();
2175 unsigned FirstWord = LocMem / 4;
2176
2177 // copy register A0 - A3 to frame object
2178 for (unsigned i = 0; i < NumWords; ++i) {
2179 unsigned CurWord = FirstWord + i;
2180 if (CurWord >= O32IntRegsSize)
2181 break;
2182
2183 unsigned SrcReg = O32IntRegs[CurWord];
2184 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2185 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2186 DAG.getConstant(i * 4, MVT::i32));
2187 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2188 StorePtr, MachinePointerInfo(), false,
2189 false, 0);
2190 OutChains.push_back(Store);
2191 }
2192}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002193
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002194/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002195/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002196SDValue
2197MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002198 CallingConv::ID CallConv,
2199 bool isVarArg,
2200 const SmallVectorImpl<ISD::InputArg>
2201 &Ins,
2202 DebugLoc dl, SelectionDAG &DAG,
2203 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002204 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002205 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002206 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002207 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002208
Dan Gohman1e93df62010-04-17 14:41:14 +00002209 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002210
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002211 // Used with vargs to acumulate store chains.
2212 std::vector<SDValue> OutChains;
2213
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002214 // Assign locations to all of the incoming arguments.
2215 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002216 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2217 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002218
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002219 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002220 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002221 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002222 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002223
Akira Hatanaka43299772011-05-20 23:22:14 +00002224 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002225
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002226 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002227 CCValAssign &VA = ArgLocs[i];
2228
2229 // Arguments stored on registers
2230 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002231 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002232 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002233 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002234
Owen Anderson825b72b2009-08-11 20:47:22 +00002235 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002236 RC = Mips::CPURegsRegisterClass;
2237 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002238 RC = Mips::FGR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002239 else if (RegVT == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002240 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002241 RC = Mips::AFGR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002242 } else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002243 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002244
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002245 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002246 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002247 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002248 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002249
2250 // If this is an 8 or 16-bit value, it has been passed promoted
2251 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002252 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002253 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002254 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002255 if (VA.getLocInfo() == CCValAssign::SExt)
2256 Opcode = ISD::AssertSext;
2257 else if (VA.getLocInfo() == CCValAssign::ZExt)
2258 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002259 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002260 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00002261 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00002262 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002263 }
2264
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002265 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002266 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002267 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
2268 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00002269 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002270 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002271 VA.getLocReg()+1, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002272 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002273 if (!Subtarget->isLittle())
2274 std::swap(ArgValue, ArgValue2);
2275 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2276 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002277 }
2278 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002279
Dan Gohman98ca4f22009-08-05 01:29:28 +00002280 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002281 } else { // VA.isRegLoc()
2282
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002283 // sanity check
2284 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002285
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002286 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2287
2288 if (Flags.isByVal()) {
2289 assert(Subtarget->isABI_O32() &&
2290 "No support for ByVal args by ABIs other than O32 yet.");
2291 assert(Flags.getByValSize() &&
2292 "ByVal args of size 0 should have been ignored by front-end.");
2293 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2294 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2295 true);
2296 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2297 InVals.push_back(FIN);
2298 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2299
2300 continue;
2301 }
2302
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002303 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002304 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
2305 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002306
2307 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002308 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002309 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002310 MachinePointerInfo::getFixedStack(LastFI),
David Greenef6fa1862010-02-15 16:56:10 +00002311 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002312 }
2313 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002314
2315 // The mips ABIs for returning structs by value requires that we copy
2316 // the sret argument into $v0 for the return. Save the argument into
2317 // a virtual register so that we can access it from the return points.
2318 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2319 unsigned Reg = MipsFI->getSRetReturnReg();
2320 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002321 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002322 MipsFI->setSRetReturnReg(Reg);
2323 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002324 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002325 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002326 }
2327
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002328 if (isVarArg && Subtarget->isABI_O32()) {
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002329 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002330 // which is a value necessary to VASTART.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002331 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002332 assert(NextStackOffset % 4 == 0 &&
2333 "NextStackOffset must be aligned to 4-byte boundaries.");
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002334 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
2335 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002336
2337 // If NextStackOffset is smaller than o32's 16-byte reserved argument area,
2338 // copy the integer registers that have not been used for argument passing
2339 // to the caller's stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002340 for (; NextStackOffset < 16; NextStackOffset += 4) {
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002341 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002342 unsigned Idx = NextStackOffset / 4;
2343 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), O32IntRegs[Idx], RC);
2344 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
Akira Hatanaka69c19f72011-05-23 20:16:59 +00002345 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002346 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2347 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2348 MachinePointerInfo(),
2349 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002350 }
2351 }
2352
Akira Hatanaka43299772011-05-20 23:22:14 +00002353 MipsFI->setLastInArgFI(LastFI);
2354
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002355 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002356 // the size of Ins and InVals. This only happens when on varg functions
2357 if (!OutChains.empty()) {
2358 OutChains.push_back(Chain);
2359 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2360 &OutChains[0], OutChains.size());
2361 }
2362
Dan Gohman98ca4f22009-08-05 01:29:28 +00002363 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002364}
2365
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002366//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002367// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002368//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002369
Dan Gohman98ca4f22009-08-05 01:29:28 +00002370SDValue
2371MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002372 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002373 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002374 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002375 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002376
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002377 // CCValAssign - represent the assignment of
2378 // the return value to a location
2379 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002380
2381 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002382 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2383 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002384
Dan Gohman98ca4f22009-08-05 01:29:28 +00002385 // Analize return values.
2386 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002387
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002388 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002389 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002390 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002391 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002392 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002393 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002394 }
2395
Dan Gohman475871a2008-07-27 21:46:04 +00002396 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002397
2398 // Copy the result values into the output registers.
2399 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2400 CCValAssign &VA = RVLocs[i];
2401 assert(VA.isRegLoc() && "Can only return in registers!");
2402
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002403 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002404 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002405
2406 // guarantee that all emitted copies are
2407 // stuck together, avoiding something bad
2408 Flag = Chain.getValue(1);
2409 }
2410
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002411 // The mips ABIs for returning structs by value requires that we copy
2412 // the sret argument into $v0 for the return. We saved the argument into
2413 // a virtual register in the entry block, so now we copy the value out
2414 // and into $v0.
2415 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2416 MachineFunction &MF = DAG.getMachineFunction();
2417 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2418 unsigned Reg = MipsFI->getSRetReturnReg();
2419
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002420 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002421 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002422 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002423
Dale Johannesena05dca42009-02-04 23:02:30 +00002424 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002425 Flag = Chain.getValue(1);
2426 }
2427
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002428 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002429 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002430 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002431 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002432 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002433 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002434 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002435}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002436
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002437//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002438// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002439//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002440
2441/// getConstraintType - Given a constraint letter, return the type of
2442/// constraint it is for this target.
2443MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002444getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002445{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002446 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002447 // GCC config/mips/constraints.md
2448 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002449 // 'd' : An address register. Equivalent to r
2450 // unless generating MIPS16 code.
2451 // 'y' : Equivalent to r; retained for
2452 // backwards compatibility.
2453 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002454 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002455 switch (Constraint[0]) {
2456 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002457 case 'd':
2458 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002459 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002460 return C_RegisterClass;
2461 break;
2462 }
2463 }
2464 return TargetLowering::getConstraintType(Constraint);
2465}
2466
John Thompson44ab89e2010-10-29 17:29:13 +00002467/// Examine constraint type and operand type and determine a weight value.
2468/// This object must already have been set up with the operand type
2469/// and the current alternative constraint selected.
2470TargetLowering::ConstraintWeight
2471MipsTargetLowering::getSingleConstraintMatchWeight(
2472 AsmOperandInfo &info, const char *constraint) const {
2473 ConstraintWeight weight = CW_Invalid;
2474 Value *CallOperandVal = info.CallOperandVal;
2475 // If we don't have a value, we can't do a match,
2476 // but allow it at the lowest weight.
2477 if (CallOperandVal == NULL)
2478 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002479 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002480 // Look at the constraint type.
2481 switch (*constraint) {
2482 default:
2483 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2484 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002485 case 'd':
2486 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002487 if (type->isIntegerTy())
2488 weight = CW_Register;
2489 break;
2490 case 'f':
2491 if (type->isFloatTy())
2492 weight = CW_Register;
2493 break;
2494 }
2495 return weight;
2496}
2497
Eric Christopher38d64262011-06-29 19:33:04 +00002498/// Given a register class constraint, like 'r', if this corresponds directly
2499/// to an LLVM register class, return a register of 0 and the register class
2500/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002501std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002502getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002503{
2504 if (Constraint.size() == 1) {
2505 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002506 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2507 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002508 case 'r':
2509 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002510 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002511 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002512 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002513 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002514 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2515 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002516 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002517 }
2518 }
2519 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2520}
2521
Dan Gohman6520e202008-10-18 02:06:02 +00002522bool
2523MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2524 // The Mips target isn't yet aware of offsets.
2525 return false;
2526}
Evan Chengeb2f9692009-10-27 19:56:55 +00002527
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002528bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2529 if (VT != MVT::f32 && VT != MVT::f64)
2530 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002531 if (Imm.isNegZero())
2532 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002533 return Imm.isZero();
2534}