blob: 45f00aeb5bb63b67822e2938aee525d958b962fa [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)
Akira Hatanaka8b4198d2011-09-26 21:47:02 +000085 : TargetLowering(TM, new MipsTargetObjectFile()),
86 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
87 HasMips64(Subtarget->hasMips64()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000088
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000089 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000090 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000091 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +000092 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000093
94 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000095 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
96 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000097
Akira Hatanaka95934842011-09-24 01:34:44 +000098 if (HasMips64)
99 addRegisterClass(MVT::i64, Mips::CPU64RegsRegisterClass);
100
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000101 // When dealing with single precision only, use libcalls
Akira Hatanaka792016b2011-09-23 18:28:39 +0000102 if (!Subtarget->isSingleFloat()) {
103 if (HasMips64)
104 addRegisterClass(MVT::f64, Mips::FGR64RegisterClass);
105 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000106 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Akira Hatanaka792016b2011-09-23 18:28:39 +0000107 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000108
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000109 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000110 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
111 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
112 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000113
Eli Friedman6055a6a2009-07-17 04:07:24 +0000114 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000115 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
116 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000117
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000118 // Used by legalize types to correctly generate the setcc result.
119 // Without this, every float setcc comes with a AND/OR with the result,
120 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000121 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000122 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000123
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000124 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000126 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
128 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
129 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
130 setOperationAction(ISD::SELECT, MVT::f32, Custom);
131 setOperationAction(ISD::SELECT, MVT::f64, Custom);
132 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000133 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
134 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000135 setOperationAction(ISD::VASTART, MVT::Other, Custom);
136
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000137 setOperationAction(ISD::SDIV, MVT::i32, Expand);
138 setOperationAction(ISD::SREM, MVT::i32, Expand);
139 setOperationAction(ISD::UDIV, MVT::i32, Expand);
140 setOperationAction(ISD::UREM, MVT::i32, Expand);
141
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000142 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000143 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
144 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
145 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
146 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
147 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
148 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
149 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
150 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
151 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000152 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000153
Akira Hatanaka56633442011-09-20 23:53:09 +0000154 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000155 setOperationAction(ISD::ROTR, MVT::i32, Expand);
156
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000157 if (!Subtarget->hasMips64r2())
158 setOperationAction(ISD::ROTR, MVT::i64, Expand);
159
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
161 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
162 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000163 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
164 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000166 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000167 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000168 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000169 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
170 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000171 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000172 setOperationAction(ISD::FLOG, MVT::f32, Expand);
173 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
174 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
175 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000176 setOperationAction(ISD::FMA, MVT::f32, Expand);
177 setOperationAction(ISD::FMA, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000178
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000179 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
180 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Eric Christopher471e4222011-06-08 23:55:35 +0000181
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000182 setOperationAction(ISD::VAARG, MVT::Other, Expand);
183 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
184 setOperationAction(ISD::VAEND, MVT::Other, Expand);
185
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000186 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000187 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
188 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000189
Akira Hatanakadb548262011-07-19 23:30:50 +0000190 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000191 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000192
Eli Friedman4db5aca2011-08-29 18:23:02 +0000193 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
194 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
195
Eli Friedman26689ac2011-08-03 21:06:02 +0000196 setInsertFencesForAtomic(true);
197
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000198 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000199 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000200
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000201 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000202 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
203 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000204 }
205
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000206 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000207 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000208
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000209 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000211
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000212 setTargetDAGCombine(ISD::ADDE);
213 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000214 setTargetDAGCombine(ISD::SDIVREM);
215 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000216 setTargetDAGCombine(ISD::SETCC);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000217 setTargetDAGCombine(ISD::AND);
218 setTargetDAGCombine(ISD::OR);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000219
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000220 setMinFunctionAlignment(2);
221
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000222 setStackPointerRegisterToSaveRestore(Mips::SP);
223 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000224
225 setExceptionPointerRegister(Mips::A0);
226 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000227}
228
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000229bool MipsTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Akira Hatanaka511961a2011-08-17 18:49:18 +0000230 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
231 return SVT == MVT::i32 || SVT == MVT::i16;
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000232}
233
Duncan Sands28b77e92011-09-06 19:07:46 +0000234EVT MipsTargetLowering::getSetCCResultType(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000236}
237
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000238// SelectMadd -
239// Transforms a subgraph in CurDAG if the following pattern is found:
240// (addc multLo, Lo0), (adde multHi, Hi0),
241// where,
242// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000243// Lo0: initial value of Lo register
244// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000245// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000246static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000247 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000248 // for the matching to be successful.
249 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
250
251 if (ADDCNode->getOpcode() != ISD::ADDC)
252 return false;
253
254 SDValue MultHi = ADDENode->getOperand(0);
255 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000256 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000257 unsigned MultOpc = MultHi.getOpcode();
258
259 // MultHi and MultLo must be generated by the same node,
260 if (MultLo.getNode() != MultNode)
261 return false;
262
263 // and it must be a multiplication.
264 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
265 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000266
267 // MultLo amd MultHi must be the first and second output of MultNode
268 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000269 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
270 return false;
271
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000272 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000273 // of the values of MultNode, in which case MultNode will be removed in later
274 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000275 // If there exist users other than ADDENode or ADDCNode, this function returns
276 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000277 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000278 // produced.
279 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
280 return false;
281
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000282 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000283 DebugLoc dl = ADDENode->getDebugLoc();
284
285 // create MipsMAdd(u) node
286 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000287
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000288 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
289 MVT::Glue,
290 MultNode->getOperand(0),// Factor 0
291 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000292 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000293 ADDENode->getOperand(1));// Hi0
294
295 // create CopyFromReg nodes
296 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
297 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000298 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000299 Mips::HI, MVT::i32,
300 CopyFromLo.getValue(2));
301
302 // replace uses of adde and addc here
303 if (!SDValue(ADDCNode, 0).use_empty())
304 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
305
306 if (!SDValue(ADDENode, 0).use_empty())
307 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
308
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000309 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000310}
311
312// SelectMsub -
313// Transforms a subgraph in CurDAG if the following pattern is found:
314// (addc Lo0, multLo), (sube Hi0, multHi),
315// where,
316// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000317// Lo0: initial value of Lo register
318// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000319// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000320static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000321 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000322 // for the matching to be successful.
323 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
324
325 if (SUBCNode->getOpcode() != ISD::SUBC)
326 return false;
327
328 SDValue MultHi = SUBENode->getOperand(1);
329 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000330 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000331 unsigned MultOpc = MultHi.getOpcode();
332
333 // MultHi and MultLo must be generated by the same node,
334 if (MultLo.getNode() != MultNode)
335 return false;
336
337 // and it must be a multiplication.
338 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
339 return false;
340
341 // MultLo amd MultHi must be the first and second output of MultNode
342 // respectively.
343 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
344 return false;
345
346 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
347 // of the values of MultNode, in which case MultNode will be removed in later
348 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000349 // If there exist users other than SUBENode or SUBCNode, this function returns
350 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000351 // instruction node rather than a pair of MULT and MSUB instructions being
352 // produced.
353 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
354 return false;
355
356 SDValue Chain = CurDAG->getEntryNode();
357 DebugLoc dl = SUBENode->getDebugLoc();
358
359 // create MipsSub(u) node
360 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
361
362 SDValue MSub = CurDAG->getNode(MultOpc, dl,
363 MVT::Glue,
364 MultNode->getOperand(0),// Factor 0
365 MultNode->getOperand(1),// Factor 1
366 SUBCNode->getOperand(0),// Lo0
367 SUBENode->getOperand(0));// Hi0
368
369 // create CopyFromReg nodes
370 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
371 MSub);
372 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
373 Mips::HI, MVT::i32,
374 CopyFromLo.getValue(2));
375
376 // replace uses of sube and subc here
377 if (!SDValue(SUBCNode, 0).use_empty())
378 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
379
380 if (!SDValue(SUBENode, 0).use_empty())
381 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
382
383 return true;
384}
385
386static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
387 TargetLowering::DAGCombinerInfo &DCI,
388 const MipsSubtarget* Subtarget) {
389 if (DCI.isBeforeLegalize())
390 return SDValue();
391
Akira Hatanaka56633442011-09-20 23:53:09 +0000392 if (Subtarget->hasMips32() && SelectMadd(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000393 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000394
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000395 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000396}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000397
398static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
399 TargetLowering::DAGCombinerInfo &DCI,
400 const MipsSubtarget* Subtarget) {
401 if (DCI.isBeforeLegalize())
402 return SDValue();
403
Akira Hatanaka56633442011-09-20 23:53:09 +0000404 if (Subtarget->hasMips32() && SelectMsub(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000405 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000406
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000407 return SDValue();
408}
409
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000410static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
411 TargetLowering::DAGCombinerInfo &DCI,
412 const MipsSubtarget* Subtarget) {
413 if (DCI.isBeforeLegalizeOps())
414 return SDValue();
415
416 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
417 MipsISD::DivRemU;
418 DebugLoc dl = N->getDebugLoc();
419
420 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
421 N->getOperand(0), N->getOperand(1));
422 SDValue InChain = DAG.getEntryNode();
423 SDValue InGlue = DivRem;
424
425 // insert MFLO
426 if (N->hasAnyUseOfValue(0)) {
427 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, Mips::LO, MVT::i32,
428 InGlue);
429 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
430 InChain = CopyFromLo.getValue(1);
431 InGlue = CopyFromLo.getValue(2);
432 }
433
434 // insert MFHI
435 if (N->hasAnyUseOfValue(1)) {
436 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000437 Mips::HI, MVT::i32, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000438 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
439 }
440
441 return SDValue();
442}
443
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000444static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
445 switch (CC) {
446 default: llvm_unreachable("Unknown fp condition code!");
447 case ISD::SETEQ:
448 case ISD::SETOEQ: return Mips::FCOND_OEQ;
449 case ISD::SETUNE: return Mips::FCOND_UNE;
450 case ISD::SETLT:
451 case ISD::SETOLT: return Mips::FCOND_OLT;
452 case ISD::SETGT:
453 case ISD::SETOGT: return Mips::FCOND_OGT;
454 case ISD::SETLE:
455 case ISD::SETOLE: return Mips::FCOND_OLE;
456 case ISD::SETGE:
457 case ISD::SETOGE: return Mips::FCOND_OGE;
458 case ISD::SETULT: return Mips::FCOND_ULT;
459 case ISD::SETULE: return Mips::FCOND_ULE;
460 case ISD::SETUGT: return Mips::FCOND_UGT;
461 case ISD::SETUGE: return Mips::FCOND_UGE;
462 case ISD::SETUO: return Mips::FCOND_UN;
463 case ISD::SETO: return Mips::FCOND_OR;
464 case ISD::SETNE:
465 case ISD::SETONE: return Mips::FCOND_ONE;
466 case ISD::SETUEQ: return Mips::FCOND_UEQ;
467 }
468}
469
470
471// Returns true if condition code has to be inverted.
472static bool InvertFPCondCode(Mips::CondCode CC) {
473 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
474 return false;
475
476 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
477 return true;
478
479 assert(false && "Illegal Condition Code");
480 return false;
481}
482
483// Creates and returns an FPCmp node from a setcc node.
484// Returns Op if setcc is not a floating point comparison.
485static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
486 // must be a SETCC node
487 if (Op.getOpcode() != ISD::SETCC)
488 return Op;
489
490 SDValue LHS = Op.getOperand(0);
491
492 if (!LHS.getValueType().isFloatingPoint())
493 return Op;
494
495 SDValue RHS = Op.getOperand(1);
496 DebugLoc dl = Op.getDebugLoc();
497
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000498 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
499 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000500 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
501
502 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
503 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
504}
505
506// Creates and returns a CMovFPT/F node.
507static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
508 SDValue False, DebugLoc DL) {
509 bool invert = InvertFPCondCode((Mips::CondCode)
510 cast<ConstantSDNode>(Cond.getOperand(2))
511 ->getSExtValue());
512
513 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
514 True.getValueType(), True, False, Cond);
515}
516
517static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
518 TargetLowering::DAGCombinerInfo &DCI,
519 const MipsSubtarget* Subtarget) {
520 if (DCI.isBeforeLegalizeOps())
521 return SDValue();
522
523 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
524
525 if (Cond.getOpcode() != MipsISD::FPCmp)
526 return SDValue();
527
528 SDValue True = DAG.getConstant(1, MVT::i32);
529 SDValue False = DAG.getConstant(0, MVT::i32);
530
531 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
532}
533
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000534static SDValue PerformANDCombine(SDNode *N, SelectionDAG& DAG,
535 TargetLowering::DAGCombinerInfo &DCI,
536 const MipsSubtarget* Subtarget) {
537 // Pattern match EXT.
538 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
539 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000540 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000541 return SDValue();
542
543 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
544
545 // Op's first operand must be a shift right.
546 if (ShiftRight.getOpcode() != ISD::SRA && ShiftRight.getOpcode() != ISD::SRL)
547 return SDValue();
548
549 // The second operand of the shift must be an immediate.
550 uint64_t Pos;
551 ConstantSDNode *CN;
552 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
553 return SDValue();
554
555 Pos = CN->getZExtValue();
556
557 uint64_t SMPos, SMSize;
558 // Op's second operand must be a shifted mask.
559 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000560 !IsShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000561 return SDValue();
562
563 // Return if the shifted mask does not start at bit 0 or the sum of its size
564 // and Pos exceeds the word's size.
565 if (SMPos != 0 || Pos + SMSize > 32)
566 return SDValue();
567
568 return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), MVT::i32,
569 ShiftRight.getOperand(0),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000570 DAG.getConstant(Pos, MVT::i32),
571 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000572}
573
574static SDValue PerformORCombine(SDNode *N, SelectionDAG& DAG,
575 TargetLowering::DAGCombinerInfo &DCI,
576 const MipsSubtarget* Subtarget) {
577 // Pattern match INS.
578 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
579 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
580 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000581 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000582 return SDValue();
583
584 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
585 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
586 ConstantSDNode *CN;
587
588 // See if Op's first operand matches (and $src1 , mask0).
589 if (And0.getOpcode() != ISD::AND)
590 return SDValue();
591
592 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000593 !IsShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000594 return SDValue();
595
596 // See if Op's second operand matches (and (shl $src, pos), mask1).
597 if (And1.getOpcode() != ISD::AND)
598 return SDValue();
599
600 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000601 !IsShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000602 return SDValue();
603
604 // The shift masks must have the same position and size.
605 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
606 return SDValue();
607
608 SDValue Shl = And1.getOperand(0);
609 if (Shl.getOpcode() != ISD::SHL)
610 return SDValue();
611
612 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
613 return SDValue();
614
615 unsigned Shamt = CN->getZExtValue();
616
617 // Return if the shift amount and the first bit position of mask are not the
618 // same.
619 if (Shamt != SMPos0)
620 return SDValue();
621
622 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), MVT::i32,
623 Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000624 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000625 DAG.getConstant(SMSize0, MVT::i32),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000626 And0.getOperand(0));
627}
628
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000629SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000630 const {
631 SelectionDAG &DAG = DCI.DAG;
632 unsigned opc = N->getOpcode();
633
634 switch (opc) {
635 default: break;
636 case ISD::ADDE:
637 return PerformADDECombine(N, DAG, DCI, Subtarget);
638 case ISD::SUBE:
639 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000640 case ISD::SDIVREM:
641 case ISD::UDIVREM:
642 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000643 case ISD::SETCC:
644 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000645 case ISD::AND:
646 return PerformANDCombine(N, DAG, DCI, Subtarget);
647 case ISD::OR:
648 return PerformORCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000649 }
650
651 return SDValue();
652}
653
Dan Gohman475871a2008-07-27 21:46:04 +0000654SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000655LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000656{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000657 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000658 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000659 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000660 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
661 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000662 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000663 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000664 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
665 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000666 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000667 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000668 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000669 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Akira Hatanakadb548262011-07-19 23:30:50 +0000670 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +0000671 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000672 }
Dan Gohman475871a2008-07-27 21:46:04 +0000673 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000674}
675
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000676//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000677// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000678//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000679
680// AddLiveIn - This helper function adds the specified physical register to the
681// MachineFunction as a live in value. It also creates a corresponding
682// virtual register for it.
683static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000684AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000685{
686 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000687 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
688 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000689 return VReg;
690}
691
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000692// Get fp branch code (not opcode) from condition code.
693static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
694 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
695 return Mips::BRANCH_T;
696
697 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
698 return Mips::BRANCH_F;
699
700 return Mips::BRANCH_INVALID;
701}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000702
Akira Hatanaka14487d42011-06-07 19:28:39 +0000703static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
704 DebugLoc dl,
705 const MipsSubtarget* Subtarget,
706 const TargetInstrInfo *TII,
707 bool isFPCmp, unsigned Opc) {
708 // There is no need to expand CMov instructions if target has
709 // conditional moves.
710 if (Subtarget->hasCondMov())
711 return BB;
712
713 // To "insert" a SELECT_CC instruction, we actually have to insert the
714 // diamond control-flow pattern. The incoming instruction knows the
715 // destination vreg to set, the condition code register to branch on, the
716 // true/false values to select between, and a branch opcode to use.
717 const BasicBlock *LLVM_BB = BB->getBasicBlock();
718 MachineFunction::iterator It = BB;
719 ++It;
720
721 // thisMBB:
722 // ...
723 // TrueVal = ...
724 // setcc r1, r2, r3
725 // bNE r1, r0, copy1MBB
726 // fallthrough --> copy0MBB
727 MachineBasicBlock *thisMBB = BB;
728 MachineFunction *F = BB->getParent();
729 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
730 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
731 F->insert(It, copy0MBB);
732 F->insert(It, sinkMBB);
733
734 // Transfer the remainder of BB and its successor edges to sinkMBB.
735 sinkMBB->splice(sinkMBB->begin(), BB,
736 llvm::next(MachineBasicBlock::iterator(MI)),
737 BB->end());
738 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
739
740 // Next, add the true and fallthrough blocks as its successors.
741 BB->addSuccessor(copy0MBB);
742 BB->addSuccessor(sinkMBB);
743
744 // Emit the right instruction according to the type of the operands compared
745 if (isFPCmp)
746 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
747 else
748 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
749 .addReg(Mips::ZERO).addMBB(sinkMBB);
750
751 // copy0MBB:
752 // %FalseValue = ...
753 // # fallthrough to sinkMBB
754 BB = copy0MBB;
755
756 // Update machine-CFG edges
757 BB->addSuccessor(sinkMBB);
758
759 // sinkMBB:
760 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
761 // ...
762 BB = sinkMBB;
763
764 if (isFPCmp)
765 BuildMI(*BB, BB->begin(), dl,
766 TII->get(Mips::PHI), MI->getOperand(0).getReg())
767 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
768 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
769 else
770 BuildMI(*BB, BB->begin(), dl,
771 TII->get(Mips::PHI), MI->getOperand(0).getReg())
772 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
773 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
774
775 MI->eraseFromParent(); // The pseudo instruction is gone now.
776 return BB;
777}
778
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000779MachineBasicBlock *
780MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000781 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000782 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen94817572009-02-13 02:34:39 +0000783 DebugLoc dl = MI->getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000784
785 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000786 default:
787 assert(false && "Unexpected instr type to insert");
788 return NULL;
789 case Mips::MOVT:
790 case Mips::MOVT_S:
791 case Mips::MOVT_D:
792 return ExpandCondMov(MI, BB, dl, Subtarget, TII, true, Mips::BC1F);
793 case Mips::MOVF:
794 case Mips::MOVF_S:
795 case Mips::MOVF_D:
796 return ExpandCondMov(MI, BB, dl, Subtarget, TII, true, Mips::BC1T);
797 case Mips::MOVZ_I:
798 case Mips::MOVZ_S:
799 case Mips::MOVZ_D:
800 return ExpandCondMov(MI, BB, dl, Subtarget, TII, false, Mips::BNE);
801 case Mips::MOVN_I:
802 case Mips::MOVN_S:
803 case Mips::MOVN_D:
804 return ExpandCondMov(MI, BB, dl, Subtarget, TII, false, Mips::BEQ);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000805
806 case Mips::ATOMIC_LOAD_ADD_I8:
807 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
808 case Mips::ATOMIC_LOAD_ADD_I16:
809 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
810 case Mips::ATOMIC_LOAD_ADD_I32:
811 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
812
813 case Mips::ATOMIC_LOAD_AND_I8:
814 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
815 case Mips::ATOMIC_LOAD_AND_I16:
816 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
817 case Mips::ATOMIC_LOAD_AND_I32:
818 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
819
820 case Mips::ATOMIC_LOAD_OR_I8:
821 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
822 case Mips::ATOMIC_LOAD_OR_I16:
823 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
824 case Mips::ATOMIC_LOAD_OR_I32:
825 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
826
827 case Mips::ATOMIC_LOAD_XOR_I8:
828 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
829 case Mips::ATOMIC_LOAD_XOR_I16:
830 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
831 case Mips::ATOMIC_LOAD_XOR_I32:
832 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
833
834 case Mips::ATOMIC_LOAD_NAND_I8:
835 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
836 case Mips::ATOMIC_LOAD_NAND_I16:
837 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
838 case Mips::ATOMIC_LOAD_NAND_I32:
839 return EmitAtomicBinary(MI, BB, 4, 0, true);
840
841 case Mips::ATOMIC_LOAD_SUB_I8:
842 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
843 case Mips::ATOMIC_LOAD_SUB_I16:
844 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
845 case Mips::ATOMIC_LOAD_SUB_I32:
846 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
847
848 case Mips::ATOMIC_SWAP_I8:
849 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
850 case Mips::ATOMIC_SWAP_I16:
851 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
852 case Mips::ATOMIC_SWAP_I32:
853 return EmitAtomicBinary(MI, BB, 4, 0);
854
855 case Mips::ATOMIC_CMP_SWAP_I8:
856 return EmitAtomicCmpSwapPartword(MI, BB, 1);
857 case Mips::ATOMIC_CMP_SWAP_I16:
858 return EmitAtomicCmpSwapPartword(MI, BB, 2);
859 case Mips::ATOMIC_CMP_SWAP_I32:
860 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000861 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000862}
863
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000864// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
865// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
866MachineBasicBlock *
867MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000868 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000869 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000870 assert(Size == 4 && "Unsupported size for EmitAtomicBinary.");
871
872 MachineFunction *MF = BB->getParent();
873 MachineRegisterInfo &RegInfo = MF->getRegInfo();
874 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
875 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
876 DebugLoc dl = MI->getDebugLoc();
877
Akira Hatanaka4061da12011-07-19 20:11:17 +0000878 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000879 unsigned Ptr = MI->getOperand(1).getReg();
880 unsigned Incr = MI->getOperand(2).getReg();
881
Akira Hatanaka4061da12011-07-19 20:11:17 +0000882 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
883 unsigned AndRes = RegInfo.createVirtualRegister(RC);
884 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000885
886 // insert new blocks after the current block
887 const BasicBlock *LLVM_BB = BB->getBasicBlock();
888 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
889 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
890 MachineFunction::iterator It = BB;
891 ++It;
892 MF->insert(It, loopMBB);
893 MF->insert(It, exitMBB);
894
895 // Transfer the remainder of BB and its successor edges to exitMBB.
896 exitMBB->splice(exitMBB->begin(), BB,
897 llvm::next(MachineBasicBlock::iterator(MI)),
898 BB->end());
899 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
900
901 // thisMBB:
902 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000903 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000904 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000905 loopMBB->addSuccessor(loopMBB);
906 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000907
908 // loopMBB:
909 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000910 // <binop> storeval, oldval, incr
911 // sc success, storeval, 0(ptr)
912 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000913 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000914 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000915 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000916 // and andres, oldval, incr
917 // nor storeval, $0, andres
918 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr);
919 BuildMI(BB, dl, TII->get(Mips::NOR), StoreVal)
920 .addReg(Mips::ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000921 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000922 // <binop> storeval, oldval, incr
923 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000924 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000925 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000926 }
Akira Hatanaka4061da12011-07-19 20:11:17 +0000927 BuildMI(BB, dl, TII->get(Mips::SC), Success)
928 .addReg(StoreVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000929 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +0000930 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000931
932 MI->eraseFromParent(); // The instruction is gone now.
933
Akira Hatanaka939ece12011-07-19 03:42:13 +0000934 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000935}
936
937MachineBasicBlock *
938MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000939 MachineBasicBlock *BB,
940 unsigned Size, unsigned BinOpcode,
941 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000942 assert((Size == 1 || Size == 2) &&
943 "Unsupported size for EmitAtomicBinaryPartial.");
944
945 MachineFunction *MF = BB->getParent();
946 MachineRegisterInfo &RegInfo = MF->getRegInfo();
947 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
948 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
949 DebugLoc dl = MI->getDebugLoc();
950
951 unsigned Dest = MI->getOperand(0).getReg();
952 unsigned Ptr = MI->getOperand(1).getReg();
953 unsigned Incr = MI->getOperand(2).getReg();
954
Akira Hatanaka4061da12011-07-19 20:11:17 +0000955 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
956 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000957 unsigned Mask = RegInfo.createVirtualRegister(RC);
958 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000959 unsigned NewVal = RegInfo.createVirtualRegister(RC);
960 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000961 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000962 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
963 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
964 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
965 unsigned AndRes = RegInfo.createVirtualRegister(RC);
966 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +0000967 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000968 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
969 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
970 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
971 unsigned SllRes = RegInfo.createVirtualRegister(RC);
972 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000973
974 // insert new blocks after the current block
975 const BasicBlock *LLVM_BB = BB->getBasicBlock();
976 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000977 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000978 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
979 MachineFunction::iterator It = BB;
980 ++It;
981 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000982 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000983 MF->insert(It, exitMBB);
984
985 // Transfer the remainder of BB and its successor edges to exitMBB.
986 exitMBB->splice(exitMBB->begin(), BB,
987 llvm::next(MachineBasicBlock::iterator(MI)),
988 BB->end());
989 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
990
Akira Hatanaka81b44112011-07-19 17:09:53 +0000991 BB->addSuccessor(loopMBB);
992 loopMBB->addSuccessor(loopMBB);
993 loopMBB->addSuccessor(sinkMBB);
994 sinkMBB->addSuccessor(exitMBB);
995
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000996 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +0000997 // addiu masklsb2,$0,-4 # 0xfffffffc
998 // and alignedaddr,ptr,masklsb2
999 // andi ptrlsb2,ptr,3
1000 // sll shiftamt,ptrlsb2,3
1001 // ori maskupper,$0,255 # 0xff
1002 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001003 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001004 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001005
1006 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001007 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1008 .addReg(Mips::ZERO).addImm(-4);
1009 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1010 .addReg(Ptr).addReg(MaskLSB2);
1011 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1012 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1013 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1014 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001015 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1016 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001017 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001018 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001019
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001020
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001021 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001022 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001023 // ll oldval,0(alignedaddr)
1024 // binop binopres,oldval,incr2
1025 // and newval,binopres,mask
1026 // and maskedoldval0,oldval,mask2
1027 // or storeval,maskedoldval0,newval
1028 // sc success,storeval,0(alignedaddr)
1029 // beq success,$0,loopMBB
1030
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001031 // atomic.swap
1032 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001033 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001034 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001035 // and maskedoldval0,oldval,mask2
1036 // or storeval,maskedoldval0,newval
1037 // sc success,storeval,0(alignedaddr)
1038 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001039
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001040 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001041 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001042 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001043 // and andres, oldval, incr2
1044 // nor binopres, $0, andres
1045 // and newval, binopres, mask
1046 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1047 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1048 .addReg(Mips::ZERO).addReg(AndRes);
1049 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001050 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001051 // <binop> binopres, oldval, incr2
1052 // and newval, binopres, mask
1053 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1054 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001055 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001056 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001057 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001058 }
1059
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001060 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001061 .addReg(OldVal).addReg(Mask2);
1062 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001063 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001064 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1065 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001066 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001067 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001068
Akira Hatanaka939ece12011-07-19 03:42:13 +00001069 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001070 // and maskedoldval1,oldval,mask
1071 // srl srlres,maskedoldval1,shiftamt
1072 // sll sllres,srlres,24
1073 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001074 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001075 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001076
Akira Hatanaka4061da12011-07-19 20:11:17 +00001077 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1078 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001079 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1080 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001081 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1082 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001083 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001084 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001085
1086 MI->eraseFromParent(); // The instruction is gone now.
1087
Akira Hatanaka939ece12011-07-19 03:42:13 +00001088 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001089}
1090
1091MachineBasicBlock *
1092MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001093 MachineBasicBlock *BB,
1094 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001095 assert(Size == 4 && "Unsupported size for EmitAtomicCmpSwap.");
1096
1097 MachineFunction *MF = BB->getParent();
1098 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1099 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1100 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1101 DebugLoc dl = MI->getDebugLoc();
1102
1103 unsigned Dest = MI->getOperand(0).getReg();
1104 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001105 unsigned OldVal = MI->getOperand(2).getReg();
1106 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001107
Akira Hatanaka4061da12011-07-19 20:11:17 +00001108 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001109
1110 // insert new blocks after the current block
1111 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1112 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1113 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1114 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1115 MachineFunction::iterator It = BB;
1116 ++It;
1117 MF->insert(It, loop1MBB);
1118 MF->insert(It, loop2MBB);
1119 MF->insert(It, exitMBB);
1120
1121 // Transfer the remainder of BB and its successor edges to exitMBB.
1122 exitMBB->splice(exitMBB->begin(), BB,
1123 llvm::next(MachineBasicBlock::iterator(MI)),
1124 BB->end());
1125 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1126
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001127 // thisMBB:
1128 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001129 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001130 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001131 loop1MBB->addSuccessor(exitMBB);
1132 loop1MBB->addSuccessor(loop2MBB);
1133 loop2MBB->addSuccessor(loop1MBB);
1134 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001135
1136 // loop1MBB:
1137 // ll dest, 0(ptr)
1138 // bne dest, oldval, exitMBB
1139 BB = loop1MBB;
Akira Hatanakad3ac47f2011-07-07 18:57:00 +00001140 BuildMI(BB, dl, TII->get(Mips::LL), Dest).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001141 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001142 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001143
1144 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001145 // sc success, newval, 0(ptr)
1146 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001147 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001148 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1149 .addReg(NewVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001150 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001151 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001152
1153 MI->eraseFromParent(); // The instruction is gone now.
1154
Akira Hatanaka939ece12011-07-19 03:42:13 +00001155 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001156}
1157
1158MachineBasicBlock *
1159MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001160 MachineBasicBlock *BB,
1161 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001162 assert((Size == 1 || Size == 2) &&
1163 "Unsupported size for EmitAtomicCmpSwapPartial.");
1164
1165 MachineFunction *MF = BB->getParent();
1166 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1167 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1168 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1169 DebugLoc dl = MI->getDebugLoc();
1170
1171 unsigned Dest = MI->getOperand(0).getReg();
1172 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001173 unsigned CmpVal = MI->getOperand(2).getReg();
1174 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001175
Akira Hatanaka4061da12011-07-19 20:11:17 +00001176 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1177 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001178 unsigned Mask = RegInfo.createVirtualRegister(RC);
1179 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001180 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1181 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1182 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1183 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1184 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1185 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1186 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1187 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1188 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1189 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1190 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1191 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1192 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1193 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001194
1195 // insert new blocks after the current block
1196 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1197 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1198 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001199 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001200 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1201 MachineFunction::iterator It = BB;
1202 ++It;
1203 MF->insert(It, loop1MBB);
1204 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001205 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001206 MF->insert(It, exitMBB);
1207
1208 // Transfer the remainder of BB and its successor edges to exitMBB.
1209 exitMBB->splice(exitMBB->begin(), BB,
1210 llvm::next(MachineBasicBlock::iterator(MI)),
1211 BB->end());
1212 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1213
Akira Hatanaka81b44112011-07-19 17:09:53 +00001214 BB->addSuccessor(loop1MBB);
1215 loop1MBB->addSuccessor(sinkMBB);
1216 loop1MBB->addSuccessor(loop2MBB);
1217 loop2MBB->addSuccessor(loop1MBB);
1218 loop2MBB->addSuccessor(sinkMBB);
1219 sinkMBB->addSuccessor(exitMBB);
1220
Akira Hatanaka70564a92011-07-19 18:14:26 +00001221 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001222 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001223 // addiu masklsb2,$0,-4 # 0xfffffffc
1224 // and alignedaddr,ptr,masklsb2
1225 // andi ptrlsb2,ptr,3
1226 // sll shiftamt,ptrlsb2,3
1227 // ori maskupper,$0,255 # 0xff
1228 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001229 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001230 // andi maskedcmpval,cmpval,255
1231 // sll shiftedcmpval,maskedcmpval,shiftamt
1232 // andi maskednewval,newval,255
1233 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001234 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001235 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1236 .addReg(Mips::ZERO).addImm(-4);
1237 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1238 .addReg(Ptr).addReg(MaskLSB2);
1239 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1240 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1241 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1242 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001243 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1244 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001245 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001246 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1247 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001248 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1249 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001250 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1251 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001252 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1253 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001254
1255 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001256 // ll oldval,0(alginedaddr)
1257 // and maskedoldval0,oldval,mask
1258 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001259 BB = loop1MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001260 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
1261 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1262 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001263 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001264 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001265
1266 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001267 // and maskedoldval1,oldval,mask2
1268 // or storeval,maskedoldval1,shiftednewval
1269 // sc success,storeval,0(alignedaddr)
1270 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001271 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001272 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1273 .addReg(OldVal).addReg(Mask2);
1274 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1275 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1276 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1277 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001278 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001279 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001280
Akira Hatanaka939ece12011-07-19 03:42:13 +00001281 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001282 // srl srlres,maskedoldval0,shiftamt
1283 // sll sllres,srlres,24
1284 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001285 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001286 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001287
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001288 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1289 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001290 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1291 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001292 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001293 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001294
1295 MI->eraseFromParent(); // The instruction is gone now.
1296
Akira Hatanaka939ece12011-07-19 03:42:13 +00001297 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001298}
1299
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001300//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001301// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001302//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001303SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001304LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001305{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001306 MachineFunction &MF = DAG.getMachineFunction();
1307 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1308
1309 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001310 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1311 "Cannot lower if the alignment of the allocated space is larger than \
1312 that of the stack.");
1313
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001314 SDValue Chain = Op.getOperand(0);
1315 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001316 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001317
1318 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +00001319 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001320
1321 // Subtract the dynamic size from the actual stack size to
1322 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +00001323 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001324
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001325 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001326 // must be placed in the stack pointer register.
Akira Hatanaka053546c2011-05-25 02:20:00 +00001327 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub,
1328 SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001329
1330 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001331 // value and a chain
Akira Hatanaka21afc632011-06-21 00:40:49 +00001332 SDVTList VTLs = DAG.getVTList(MVT::i32, MVT::Other);
1333 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1334 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1335
1336 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001337}
1338
1339SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001340LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001341{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001342 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001343 // the block to branch to if the condition is true.
1344 SDValue Chain = Op.getOperand(0);
1345 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001346 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001347
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001348 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1349
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001350 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001351 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001352 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001353
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001354 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001355 Mips::CondCode CC =
1356 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001357 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001358
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001359 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001360 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001361}
1362
1363SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001364LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001365{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001366 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001367
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001368 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001369 if (Cond.getOpcode() != MipsISD::FPCmp)
1370 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001371
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001372 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1373 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001374}
1375
Dan Gohmand858e902010-04-17 15:26:15 +00001376SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1377 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001378 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001379 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001380 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001381
Eli Friedmane2c74082009-08-03 02:22:28 +00001382 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001383 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001384
Chris Lattnerb71b9092009-08-13 06:28:06 +00001385 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001386
Chris Lattnere3736f82009-08-13 05:41:27 +00001387 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001388 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1389 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001390 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001391 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1392 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001393 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001394 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001395 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001396 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1397 MipsII::MO_ABS_HI);
1398 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1399 MipsII::MO_ABS_LO);
1400 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1401 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001402 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001403 }
1404
Akira Hatanaka0f843822011-06-07 18:58:42 +00001405 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1406 MipsII::MO_GOT);
1407 GA = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, GA);
1408 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
1409 DAG.getEntryNode(), GA, MachinePointerInfo(),
1410 false, false, 0);
1411 // On functions and global targets not internal linked only
1412 // a load from got/GP is necessary for PIC to work.
1413 if (!GV->hasInternalLinkage() &&
1414 (!GV->hasLocalLinkage() || isa<Function>(GV)))
1415 return ResNode;
1416 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1417 MipsII::MO_ABS_LO);
1418 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
1419 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001420}
1421
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001422SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1423 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001424 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1425 // FIXME there isn't actually debug info here
1426 DebugLoc dl = Op.getDebugLoc();
1427
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001428 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001429 // %hi/%lo relocation
1430 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
1431 MipsII::MO_ABS_HI);
1432 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
1433 MipsII::MO_ABS_LO);
1434 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1435 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1436 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001437 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001438
1439 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1440 MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001441 BAGOTOffset = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, BAGOTOffset);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001442 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1443 MipsII::MO_ABS_LO);
1444 SDValue Load = DAG.getLoad(MVT::i32, dl,
1445 DAG.getEntryNode(), BAGOTOffset,
1446 MachinePointerInfo(), false, false, 0);
1447 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
1448 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001449}
1450
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001451SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001452LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001453{
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001454 // If the relocation model is PIC, use the General Dynamic TLS Model,
1455 // otherwise use the Initial Exec or Local Exec TLS Model.
1456 // TODO: implement Local Dynamic TLS model
1457
1458 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1459 DebugLoc dl = GA->getDebugLoc();
1460 const GlobalValue *GV = GA->getGlobal();
1461 EVT PtrVT = getPointerTy();
1462
1463 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1464 // General Dynamic TLS Model
1465 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001466 0, MipsII::MO_TLSGD);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001467 SDValue Tlsgd = DAG.getNode(MipsISD::TlsGd, dl, MVT::i32, TGA);
1468 SDValue GP = DAG.getRegister(Mips::GP, MVT::i32);
1469 SDValue Argument = DAG.getNode(ISD::ADD, dl, MVT::i32, GP, Tlsgd);
1470
1471 ArgListTy Args;
1472 ArgListEntry Entry;
1473 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001474 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001475 Args.push_back(Entry);
1476 std::pair<SDValue, SDValue> CallResult =
1477 LowerCallTo(DAG.getEntryNode(),
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001478 (Type *) Type::getInt32Ty(*DAG.getContext()),
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001479 false, false, false, false, 0, CallingConv::C, false, true,
1480 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG,
1481 dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001482
1483 return CallResult.first;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001484 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001485
1486 SDValue Offset;
1487 if (GV->isDeclaration()) {
1488 // Initial Exec TLS Model
1489 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1490 MipsII::MO_GOTTPREL);
1491 Offset = DAG.getLoad(MVT::i32, dl,
1492 DAG.getEntryNode(), TGA, MachinePointerInfo(),
1493 false, false, 0);
1494 } else {
1495 // Local Exec TLS Model
1496 SDVTList VTs = DAG.getVTList(MVT::i32);
1497 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1498 MipsII::MO_TPREL_HI);
1499 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1500 MipsII::MO_TPREL_LO);
1501 SDValue Hi = DAG.getNode(MipsISD::TprelHi, dl, VTs, &TGAHi, 1);
1502 SDValue Lo = DAG.getNode(MipsISD::TprelLo, dl, MVT::i32, TGALo);
1503 Offset = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1504 }
1505
1506 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1507 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001508}
1509
1510SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001511LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001512{
Dan Gohman475871a2008-07-27 21:46:04 +00001513 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001514 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +00001515 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001516 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001517 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001518 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001519
Owen Andersone50ed302009-08-10 22:56:29 +00001520 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001521 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001522
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001523 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
1524
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +00001525 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001526 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001527 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001528 } else {// Emit Load from Global Pointer
1529 JTI = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, JTI);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001530 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
1531 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001532 false, false, 0);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001533 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001534
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001535 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1536 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001537 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001538 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001539
1540 return ResNode;
1541}
1542
Dan Gohman475871a2008-07-27 21:46:04 +00001543SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001544LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001545{
Dan Gohman475871a2008-07-27 21:46:04 +00001546 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001547 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001548 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001549 // FIXME there isn't actually debug info here
1550 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001551
1552 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001553 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001554 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001555 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001556 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001557 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001558 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1559 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001560 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001561
1562 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001563 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001564 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001565 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001566 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001567 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1568 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001569 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001570 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001571 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001572 N->getOffset(), MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001573 CP = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001574 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001575 CP, MachinePointerInfo::getConstantPool(),
1576 false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001577 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001578 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001579 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001580 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
1581 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001582
1583 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001584}
1585
Dan Gohmand858e902010-04-17 15:26:15 +00001586SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001587 MachineFunction &MF = DAG.getMachineFunction();
1588 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1589
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001590 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001591 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1592 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001593
1594 // vastart just stores the address of the VarArgsFrameIndex slot into the
1595 // memory location argument.
1596 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001597 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1598 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +00001599 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001600}
1601
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001602static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
1603 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
1604 DebugLoc dl = Op.getDebugLoc();
1605 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
1606 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
1607 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
1608 DAG.getConstant(0x7fffffff, MVT::i32));
1609 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
1610 DAG.getConstant(0x80000000, MVT::i32));
1611 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1612 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
1613}
1614
1615static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001616 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001617 // Use ext/ins instructions if target architecture is Mips32r2.
1618 // Eliminate redundant mfc1 and mtc1 instructions.
1619 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001620
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001621 if (!isLittle)
1622 std::swap(LoIdx, HiIdx);
1623
1624 DebugLoc dl = Op.getDebugLoc();
1625 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1626 Op.getOperand(0),
1627 DAG.getConstant(LoIdx, MVT::i32));
1628 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1629 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1630 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1631 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1632 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1633 DAG.getConstant(0x7fffffff, MVT::i32));
1634 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1635 DAG.getConstant(0x80000000, MVT::i32));
1636 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1637
1638 if (!isLittle)
1639 std::swap(Word0, Word1);
1640
1641 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1642}
1643
1644SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1645 const {
1646 EVT Ty = Op.getValueType();
1647
1648 assert(Ty == MVT::f32 || Ty == MVT::f64);
1649
1650 if (Ty == MVT::f32)
1651 return LowerFCOPYSIGN32(Op, DAG);
1652 else
1653 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
1654}
1655
Akira Hatanaka2e591472011-06-02 00:24:44 +00001656SDValue MipsTargetLowering::
1657LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001658 // check the depth
1659 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001660 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001661
1662 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1663 MFI->setFrameAddressIsTaken(true);
1664 EVT VT = Op.getValueType();
1665 DebugLoc dl = Op.getDebugLoc();
1666 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Mips::FP, VT);
1667 return FrameAddr;
1668}
1669
Akira Hatanakadb548262011-07-19 23:30:50 +00001670// TODO: set SType according to the desired memory barrier behavior.
1671SDValue MipsTargetLowering::LowerMEMBARRIER(SDValue Op,
1672 SelectionDAG& DAG) const {
1673 unsigned SType = 0;
1674 DebugLoc dl = Op.getDebugLoc();
1675 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1676 DAG.getConstant(SType, MVT::i32));
1677}
1678
Eli Friedman14648462011-07-27 22:21:52 +00001679SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1680 SelectionDAG& DAG) const {
1681 // FIXME: Need pseudo-fence for 'singlethread' fences
1682 // FIXME: Set SType for weaker fences where supported/appropriate.
1683 unsigned SType = 0;
1684 DebugLoc dl = Op.getDebugLoc();
1685 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1686 DAG.getConstant(SType, MVT::i32));
1687}
1688
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001689//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001690// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001691//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001692
1693#include "MipsGenCallingConv.inc"
1694
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001695//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001696// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001697// Mips O32 ABI rules:
1698// ---
1699// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001700// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001701// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001702// f64 - Only passed in two aliased f32 registers if no int reg has been used
1703// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001704// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1705// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001706//
1707// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001708//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001709
Duncan Sands1e96bab2010-11-04 10:49:57 +00001710static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001711 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001712 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1713
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001714 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001715
1716 static const unsigned IntRegs[] = {
1717 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1718 };
1719 static const unsigned F32Regs[] = {
1720 Mips::F12, Mips::F14
1721 };
1722 static const unsigned F64Regs[] = {
1723 Mips::D6, Mips::D7
1724 };
1725
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001726 // ByVal Args
1727 if (ArgFlags.isByVal()) {
1728 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1729 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1730 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1731 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1732 r < std::min(IntRegsSize, NextReg); ++r)
1733 State.AllocateReg(IntRegs[r]);
1734 return false;
1735 }
1736
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001737 // Promote i8 and i16
1738 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1739 LocVT = MVT::i32;
1740 if (ArgFlags.isSExt())
1741 LocInfo = CCValAssign::SExt;
1742 else if (ArgFlags.isZExt())
1743 LocInfo = CCValAssign::ZExt;
1744 else
1745 LocInfo = CCValAssign::AExt;
1746 }
1747
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001748 unsigned Reg;
1749
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001750 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1751 // is true: function is vararg, argument is 3rd or higher, there is previous
1752 // argument which is not f32 or f64.
1753 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1754 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001755 unsigned OrigAlign = ArgFlags.getOrigAlign();
1756 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001757
1758 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001759 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001760 // If this is the first part of an i64 arg,
1761 // the allocated register must be either A0 or A2.
1762 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1763 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001764 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001765 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1766 // Allocate int register and shadow next int register. If first
1767 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001768 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1769 if (Reg == Mips::A1 || Reg == Mips::A3)
1770 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1771 State.AllocateReg(IntRegs, IntRegsSize);
1772 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001773 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1774 // we are guaranteed to find an available float register
1775 if (ValVT == MVT::f32) {
1776 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1777 // Shadow int register
1778 State.AllocateReg(IntRegs, IntRegsSize);
1779 } else {
1780 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1781 // Shadow int registers
1782 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1783 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1784 State.AllocateReg(IntRegs, IntRegsSize);
1785 State.AllocateReg(IntRegs, IntRegsSize);
1786 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001787 } else
1788 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001789
Akira Hatanakad37776d2011-05-20 21:39:54 +00001790 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1791 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1792
1793 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001794 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001795 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001796 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001797
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001798 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001799}
1800
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001801//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001802// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001803//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001804
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001805static const unsigned O32IntRegsSize = 4;
1806
1807static const unsigned O32IntRegs[] = {
1808 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1809};
1810
Akira Hatanaka373e3a42011-09-23 00:58:33 +00001811// Return next O32 integer argument register.
1812static unsigned getNextIntArgReg(unsigned Reg) {
1813 assert((Reg == Mips::A0) || (Reg == Mips::A2));
1814 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
1815}
1816
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001817// Write ByVal Arg to arg registers and stack.
1818static void
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001819WriteByValArg(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001820 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1821 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1822 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001823 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001824 MVT PtrType, bool isLittle) {
1825 unsigned LocMemOffset = VA.getLocMemOffset();
1826 unsigned Offset = 0;
1827 uint32_t RemainingSize = Flags.getByValSize();
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001828 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001829
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001830 // Copy the first 4 words of byval arg to registers A0 - A3.
1831 // FIXME: Use a stricter alignment if it enables better optimization in passes
1832 // run later.
1833 for (; RemainingSize >= 4 && LocMemOffset < 4 * 4;
1834 Offset += 4, RemainingSize -= 4, LocMemOffset += 4) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001835 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001836 DAG.getConstant(Offset, MVT::i32));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001837 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1838 MachinePointerInfo(),
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001839 false, false, std::min(ByValAlign,
1840 (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001841 MemOpChains.push_back(LoadVal.getValue(1));
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001842 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001843 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1844 }
1845
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001846 if (RemainingSize == 0)
1847 return;
1848
1849 // If there still is a register available for argument passing, write the
1850 // remaining part of the structure to it using subword loads and shifts.
1851 if (LocMemOffset < 4 * 4) {
1852 assert(RemainingSize <= 3 && RemainingSize >= 1 &&
1853 "There must be one to three bytes remaining.");
1854 unsigned LoadSize = (RemainingSize == 3 ? 2 : RemainingSize);
1855 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1856 DAG.getConstant(Offset, MVT::i32));
1857 unsigned Alignment = std::min(ByValAlign, (unsigned )4);
1858 SDValue LoadVal = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
1859 LoadPtr, MachinePointerInfo(),
1860 MVT::getIntegerVT(LoadSize * 8), false,
1861 false, Alignment);
1862 MemOpChains.push_back(LoadVal.getValue(1));
1863
1864 // If target is big endian, shift it to the most significant half-word or
1865 // byte.
1866 if (!isLittle)
1867 LoadVal = DAG.getNode(ISD::SHL, dl, MVT::i32, LoadVal,
1868 DAG.getConstant(32 - LoadSize * 8, MVT::i32));
1869
1870 Offset += LoadSize;
1871 RemainingSize -= LoadSize;
1872
1873 // Read second subword if necessary.
1874 if (RemainingSize != 0) {
1875 assert(RemainingSize == 1 && "There must be one byte remaining.");
1876 LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1877 DAG.getConstant(Offset, MVT::i32));
1878 unsigned Alignment = std::min(ByValAlign, (unsigned )2);
1879 SDValue Subword = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
1880 LoadPtr, MachinePointerInfo(),
1881 MVT::i8, false, false, Alignment);
1882 MemOpChains.push_back(Subword.getValue(1));
1883 // Insert the loaded byte to LoadVal.
1884 // FIXME: Use INS if supported by target.
1885 unsigned ShiftAmt = isLittle ? 16 : 8;
1886 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i32, Subword,
1887 DAG.getConstant(ShiftAmt, MVT::i32));
1888 LoadVal = DAG.getNode(ISD::OR, dl, MVT::i32, LoadVal, Shift);
1889 }
1890
1891 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
1892 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1893 return;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001894 }
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001895
1896 // Create a fixed object on stack at offset LocMemOffset and copy
1897 // remaining part of byval arg to it using memcpy.
1898 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1899 DAG.getConstant(Offset, MVT::i32));
1900 LastFI = MFI->CreateFixedObject(RemainingSize, LocMemOffset, true);
1901 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001902 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
1903 DAG.getConstant(RemainingSize, MVT::i32),
1904 std::min(ByValAlign, (unsigned)4),
1905 /*isVolatile=*/false, /*AlwaysInline=*/false,
1906 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001907}
1908
Dan Gohman98ca4f22009-08-05 01:29:28 +00001909/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001910/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001911/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001912SDValue
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001913MipsTargetLowering::LowerCall(SDValue InChain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001914 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001915 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001916 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001917 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001918 const SmallVectorImpl<ISD::InputArg> &Ins,
1919 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001920 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001921 // MIPs target does not yet support tail call optimization.
1922 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001923
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001924 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001925 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00001926 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001927 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001928 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001929
1930 // Analyze operands of the call, assigning locations to each operand.
1931 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001932 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1933 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001934
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001935 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001936 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001937 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001938 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001939
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001940 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001941 unsigned NextStackOffset = CCInfo.getNextStackOffset();
1942
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001943 // Chain is the output chain of the last Load/Store or CopyToReg node.
1944 // ByValChain is the output chain of the last Memcpy node created for copying
1945 // byval arguments to the stack.
1946 SDValue Chain, CallSeqStart, ByValChain;
1947 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
1948 Chain = CallSeqStart = DAG.getCALLSEQ_START(InChain, NextStackOffsetVal);
1949 ByValChain = InChain;
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001950
1951 // If this is the first call, create a stack frame object that points to
1952 // a location to which .cprestore saves $gp.
1953 if (IsPIC && !MipsFI->getGPFI())
1954 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
1955
Akira Hatanaka21afc632011-06-21 00:40:49 +00001956 // Get the frame index of the stack frame object that points to the location
1957 // of dynamically allocated area on the stack.
1958 int DynAllocFI = MipsFI->getDynAllocFI();
1959
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001960 // Update size of the maximum argument space.
1961 // For O32, a minimum of four words (16 bytes) of argument space is
1962 // allocated.
1963 if (Subtarget->isABI_O32())
1964 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
1965
1966 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1967
1968 if (MaxCallFrameSize < NextStackOffset) {
1969 MipsFI->setMaxCallFrameSize(NextStackOffset);
1970
Akira Hatanaka21afc632011-06-21 00:40:49 +00001971 // Set the offsets relative to $sp of the $gp restore slot and dynamically
1972 // allocated stack space. These offsets must be aligned to a boundary
1973 // determined by the stack alignment of the ABI.
1974 unsigned StackAlignment = TFL->getStackAlignment();
1975 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
1976 StackAlignment * StackAlignment;
1977
1978 if (IsPIC)
1979 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
1980
1981 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001982 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001983
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001984 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001985 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1986 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001987
Eric Christopher471e4222011-06-08 23:55:35 +00001988 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00001989
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001990 // Walk the register/memloc assignments, inserting copies/loads.
1991 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001992 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001993 CCValAssign &VA = ArgLocs[i];
1994
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001995 // Promote the value if needed.
1996 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001997 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001998 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001999 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002000 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002001 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002002 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002003 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2004 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002005 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2006 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002007 if (!Subtarget->isLittle())
2008 std::swap(Lo, Hi);
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002009 unsigned LocRegLo = VA.getLocReg();
2010 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2011 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2012 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002013 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002014 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002015 }
2016 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002017 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00002018 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002019 break;
2020 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00002021 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002022 break;
2023 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00002024 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002025 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002026 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002027
2028 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002029 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002030 if (VA.isRegLoc()) {
2031 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002032 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002033 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002034
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002035 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002036 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002037
Eric Christopher471e4222011-06-08 23:55:35 +00002038 // ByVal Arg.
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002039 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2040 if (Flags.isByVal()) {
2041 assert(Subtarget->isABI_O32() &&
2042 "No support for ByVal args by ABIs other than O32 yet.");
2043 assert(Flags.getByValSize() &&
2044 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002045 WriteByValArg(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI, MFI,
2046 DAG, Arg, VA, Flags, getPointerTy(), Subtarget->isLittle());
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002047 continue;
2048 }
2049
Chris Lattnere0b12152008-03-17 06:57:02 +00002050 // Create the frame index object for this incoming parameter
Eric Christopher471e4222011-06-08 23:55:35 +00002051 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002052 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00002053 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00002054
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002055 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002056 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00002057 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
2058 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00002059 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002060 }
2061
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002062 // Extend range of indices of frame objects for outgoing arguments that were
2063 // created during this function call. Skip this step if no such objects were
2064 // created.
2065 if (LastFI)
2066 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2067
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002068 // If a memcpy has been created to copy a byval arg to a stack, replace the
2069 // chain input of CallSeqStart with ByValChain.
2070 if (InChain != ByValChain)
2071 DAG.UpdateNodeOperands(CallSeqStart.getNode(), ByValChain,
2072 NextStackOffsetVal);
2073
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002074 // Transform all store nodes into one single node because all store
2075 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002076 if (!MemOpChains.empty())
2077 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002078 &MemOpChains[0], MemOpChains.size());
2079
Bill Wendling056292f2008-09-16 21:48:12 +00002080 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002081 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2082 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002083 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002084 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002085 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002086
2087 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002088 if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
2089 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2090 getPointerTy(), 0,MipsII:: MO_GOT);
2091 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
2092 0, MipsII::MO_ABS_LO);
2093 } else {
2094 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2095 getPointerTy(), 0, OpFlag);
2096 }
2097
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002098 LoadSymAddr = true;
2099 }
2100 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002101 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002102 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002103 LoadSymAddr = true;
2104 }
2105
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002106 SDValue InFlag;
2107
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002108 // Create nodes that load address of callee and copy it to T9
2109 if (IsPIC) {
2110 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002111 // Load callee address
Akira Hatanaka342837d2011-05-28 01:07:07 +00002112 Callee = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, Callee);
Akira Hatanaka25eba392011-06-24 19:01:25 +00002113 SDValue LoadValue = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), Callee,
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002114 MachinePointerInfo::getGOT(),
2115 false, false, 0);
2116
2117 // Use GOT+LO if callee has internal linkage.
2118 if (CalleeLo.getNode()) {
2119 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
2120 Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
2121 } else
2122 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002123 }
2124
2125 // copy to T9
2126 Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
2127 InFlag = Chain.getValue(1);
2128 Callee = DAG.getRegister(Mips::T9, MVT::i32);
2129 }
Bill Wendling056292f2008-09-16 21:48:12 +00002130
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002131 // Build a sequence of copy-to-reg nodes chained together with token
2132 // chain and flag operands which copy the outgoing args into registers.
2133 // The InFlag in necessary since all emitted instructions must be
2134 // stuck together.
2135 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2136 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2137 RegsToPass[i].second, InFlag);
2138 InFlag = Chain.getValue(1);
2139 }
2140
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002141 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002142 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002143 //
2144 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002145 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002146 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002147 Ops.push_back(Chain);
2148 Ops.push_back(Callee);
2149
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002150 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002151 // known live into the call.
2152 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2153 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2154 RegsToPass[i].second.getValueType()));
2155
Gabor Greifba36cb52008-08-28 21:40:38 +00002156 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002157 Ops.push_back(InFlag);
2158
Dale Johannesen33c960f2009-02-04 20:06:27 +00002159 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002160 InFlag = Chain.getValue(1);
2161
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002162 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002163 Chain = DAG.getCALLSEQ_END(Chain,
2164 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002165 DAG.getIntPtrConstant(0, true), InFlag);
2166 InFlag = Chain.getValue(1);
2167
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002168 // Handle result values, copying them out of physregs into vregs that we
2169 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002170 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2171 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002172}
2173
Dan Gohman98ca4f22009-08-05 01:29:28 +00002174/// LowerCallResult - Lower the result values of a call into the
2175/// appropriate copies out of appropriate physical registers.
2176SDValue
2177MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002178 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002179 const SmallVectorImpl<ISD::InputArg> &Ins,
2180 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002181 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002182 // Assign locations to each value returned by this call.
2183 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002184 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2185 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002186
Dan Gohman98ca4f22009-08-05 01:29:28 +00002187 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002188
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002189 // Copy all of the result registers out of their specified physreg.
2190 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002191 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002192 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002193 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002194 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002195 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002196
Dan Gohman98ca4f22009-08-05 01:29:28 +00002197 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002198}
2199
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002200//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002201// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002202//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002203static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2204 std::vector<SDValue>& OutChains,
2205 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2206 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2207 unsigned LocMem = VA.getLocMemOffset();
2208 unsigned FirstWord = LocMem / 4;
2209
2210 // copy register A0 - A3 to frame object
2211 for (unsigned i = 0; i < NumWords; ++i) {
2212 unsigned CurWord = FirstWord + i;
2213 if (CurWord >= O32IntRegsSize)
2214 break;
2215
2216 unsigned SrcReg = O32IntRegs[CurWord];
2217 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2218 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2219 DAG.getConstant(i * 4, MVT::i32));
2220 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2221 StorePtr, MachinePointerInfo(), false,
2222 false, 0);
2223 OutChains.push_back(Store);
2224 }
2225}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002226
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002227/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002228/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002229SDValue
2230MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002231 CallingConv::ID CallConv,
2232 bool isVarArg,
2233 const SmallVectorImpl<ISD::InputArg>
2234 &Ins,
2235 DebugLoc dl, SelectionDAG &DAG,
2236 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002237 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002238 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002239 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002240 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002241
Dan Gohman1e93df62010-04-17 14:41:14 +00002242 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002243
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002244 // Used with vargs to acumulate store chains.
2245 std::vector<SDValue> OutChains;
2246
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002247 // Assign locations to all of the incoming arguments.
2248 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002249 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2250 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002251
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002252 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002253 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002254 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002255 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002256
Akira Hatanaka43299772011-05-20 23:22:14 +00002257 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002258
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002259 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002260 CCValAssign &VA = ArgLocs[i];
2261
2262 // Arguments stored on registers
2263 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002264 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002265 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002266 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002267
Owen Anderson825b72b2009-08-11 20:47:22 +00002268 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002269 RC = Mips::CPURegsRegisterClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002270 else if (RegVT == MVT::i64)
2271 RC = Mips::CPU64RegsRegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002272 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002273 RC = Mips::FGR32RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002274 else if (RegVT == MVT::f64)
Akira Hatanakaf40de9d2011-09-26 21:55:17 +00002275 RC = HasMips64 ? Mips::FGR64RegisterClass : Mips::AFGR64RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002276 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002277 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002278
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002279 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002280 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002281 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002282 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002283
2284 // If this is an 8 or 16-bit value, it has been passed promoted
2285 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002286 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002287 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002288 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002289 if (VA.getLocInfo() == CCValAssign::SExt)
2290 Opcode = ISD::AssertSext;
2291 else if (VA.getLocInfo() == CCValAssign::ZExt)
2292 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002293 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002294 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00002295 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00002296 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002297 }
2298
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002299 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002300 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002301 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
2302 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00002303 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002304 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002305 getNextIntArgReg(ArgReg), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002306 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002307 if (!Subtarget->isLittle())
2308 std::swap(ArgValue, ArgValue2);
2309 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2310 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002311 }
2312 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002313
Dan Gohman98ca4f22009-08-05 01:29:28 +00002314 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002315 } else { // VA.isRegLoc()
2316
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002317 // sanity check
2318 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002319
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002320 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2321
2322 if (Flags.isByVal()) {
2323 assert(Subtarget->isABI_O32() &&
2324 "No support for ByVal args by ABIs other than O32 yet.");
2325 assert(Flags.getByValSize() &&
2326 "ByVal args of size 0 should have been ignored by front-end.");
2327 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2328 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2329 true);
2330 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2331 InVals.push_back(FIN);
2332 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2333
2334 continue;
2335 }
2336
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002337 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002338 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
2339 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002340
2341 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002342 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002343 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002344 MachinePointerInfo::getFixedStack(LastFI),
David Greenef6fa1862010-02-15 16:56:10 +00002345 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002346 }
2347 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002348
2349 // The mips ABIs for returning structs by value requires that we copy
2350 // the sret argument into $v0 for the return. Save the argument into
2351 // a virtual register so that we can access it from the return points.
2352 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2353 unsigned Reg = MipsFI->getSRetReturnReg();
2354 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002355 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002356 MipsFI->setSRetReturnReg(Reg);
2357 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002358 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002359 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002360 }
2361
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002362 if (isVarArg && Subtarget->isABI_O32()) {
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002363 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002364 // which is a value necessary to VASTART.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002365 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002366 assert(NextStackOffset % 4 == 0 &&
2367 "NextStackOffset must be aligned to 4-byte boundaries.");
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002368 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
2369 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002370
2371 // If NextStackOffset is smaller than o32's 16-byte reserved argument area,
2372 // copy the integer registers that have not been used for argument passing
2373 // to the caller's stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002374 for (; NextStackOffset < 16; NextStackOffset += 4) {
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002375 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002376 unsigned Idx = NextStackOffset / 4;
2377 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), O32IntRegs[Idx], RC);
2378 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
Akira Hatanaka69c19f72011-05-23 20:16:59 +00002379 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002380 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2381 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2382 MachinePointerInfo(),
2383 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002384 }
2385 }
2386
Akira Hatanaka43299772011-05-20 23:22:14 +00002387 MipsFI->setLastInArgFI(LastFI);
2388
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002389 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002390 // the size of Ins and InVals. This only happens when on varg functions
2391 if (!OutChains.empty()) {
2392 OutChains.push_back(Chain);
2393 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2394 &OutChains[0], OutChains.size());
2395 }
2396
Dan Gohman98ca4f22009-08-05 01:29:28 +00002397 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002398}
2399
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002400//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002401// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002402//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002403
Dan Gohman98ca4f22009-08-05 01:29:28 +00002404SDValue
2405MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002406 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002407 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002408 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002409 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002410
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002411 // CCValAssign - represent the assignment of
2412 // the return value to a location
2413 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002414
2415 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002416 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2417 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002418
Dan Gohman98ca4f22009-08-05 01:29:28 +00002419 // Analize return values.
2420 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002421
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002422 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002423 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002424 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002425 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002426 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002427 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002428 }
2429
Dan Gohman475871a2008-07-27 21:46:04 +00002430 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002431
2432 // Copy the result values into the output registers.
2433 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2434 CCValAssign &VA = RVLocs[i];
2435 assert(VA.isRegLoc() && "Can only return in registers!");
2436
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002437 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002438 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002439
2440 // guarantee that all emitted copies are
2441 // stuck together, avoiding something bad
2442 Flag = Chain.getValue(1);
2443 }
2444
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002445 // The mips ABIs for returning structs by value requires that we copy
2446 // the sret argument into $v0 for the return. We saved the argument into
2447 // a virtual register in the entry block, so now we copy the value out
2448 // and into $v0.
2449 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2450 MachineFunction &MF = DAG.getMachineFunction();
2451 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2452 unsigned Reg = MipsFI->getSRetReturnReg();
2453
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002454 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002455 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002456 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002457
Dale Johannesena05dca42009-02-04 23:02:30 +00002458 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002459 Flag = Chain.getValue(1);
2460 }
2461
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002462 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002463 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002464 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002465 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002466 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002467 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002468 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002469}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002470
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002471//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002472// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002473//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002474
2475/// getConstraintType - Given a constraint letter, return the type of
2476/// constraint it is for this target.
2477MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002478getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002479{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002480 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002481 // GCC config/mips/constraints.md
2482 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002483 // 'd' : An address register. Equivalent to r
2484 // unless generating MIPS16 code.
2485 // 'y' : Equivalent to r; retained for
2486 // backwards compatibility.
2487 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002488 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002489 switch (Constraint[0]) {
2490 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002491 case 'd':
2492 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002493 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002494 return C_RegisterClass;
2495 break;
2496 }
2497 }
2498 return TargetLowering::getConstraintType(Constraint);
2499}
2500
John Thompson44ab89e2010-10-29 17:29:13 +00002501/// Examine constraint type and operand type and determine a weight value.
2502/// This object must already have been set up with the operand type
2503/// and the current alternative constraint selected.
2504TargetLowering::ConstraintWeight
2505MipsTargetLowering::getSingleConstraintMatchWeight(
2506 AsmOperandInfo &info, const char *constraint) const {
2507 ConstraintWeight weight = CW_Invalid;
2508 Value *CallOperandVal = info.CallOperandVal;
2509 // If we don't have a value, we can't do a match,
2510 // but allow it at the lowest weight.
2511 if (CallOperandVal == NULL)
2512 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002513 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002514 // Look at the constraint type.
2515 switch (*constraint) {
2516 default:
2517 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2518 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002519 case 'd':
2520 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002521 if (type->isIntegerTy())
2522 weight = CW_Register;
2523 break;
2524 case 'f':
2525 if (type->isFloatTy())
2526 weight = CW_Register;
2527 break;
2528 }
2529 return weight;
2530}
2531
Eric Christopher38d64262011-06-29 19:33:04 +00002532/// Given a register class constraint, like 'r', if this corresponds directly
2533/// to an LLVM register class, return a register of 0 and the register class
2534/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002535std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002536getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002537{
2538 if (Constraint.size() == 1) {
2539 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002540 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2541 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002542 case 'r':
2543 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002544 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002545 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002546 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002547 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002548 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2549 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002550 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002551 }
2552 }
2553 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2554}
2555
Dan Gohman6520e202008-10-18 02:06:02 +00002556bool
2557MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2558 // The Mips target isn't yet aware of offsets.
2559 return false;
2560}
Evan Chengeb2f9692009-10-27 19:56:55 +00002561
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002562bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2563 if (VT != MVT::f32 && VT != MVT::f64)
2564 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002565 if (Imm.isNegZero())
2566 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002567 return Imm.isZero();
2568}