blob: 31378a7b862d84eb4aeaf415811931181923097e [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>()),
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +000087 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
88 IsO32(Subtarget->isABI_O32()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000089
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000090 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000091 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000092 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +000093 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000094
95 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000096 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
97 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000098
Akira Hatanaka95934842011-09-24 01:34:44 +000099 if (HasMips64)
100 addRegisterClass(MVT::i64, Mips::CPU64RegsRegisterClass);
101
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000102 // When dealing with single precision only, use libcalls
Akira Hatanaka792016b2011-09-23 18:28:39 +0000103 if (!Subtarget->isSingleFloat()) {
104 if (HasMips64)
105 addRegisterClass(MVT::f64, Mips::FGR64RegisterClass);
106 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000107 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Akira Hatanaka792016b2011-09-23 18:28:39 +0000108 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000109
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000110 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000111 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
112 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
113 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000114
Eli Friedman6055a6a2009-07-17 04:07:24 +0000115 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000116 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
117 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000118
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000119 // Used by legalize types to correctly generate the setcc result.
120 // Without this, every float setcc comes with a AND/OR with the result,
121 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000122 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000123 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000124
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000125 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +0000126 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Akira Hatanakaa5903ac2011-10-11 00:55:05 +0000127 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000128 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000129 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
130 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
131 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
132 setOperationAction(ISD::SELECT, MVT::f32, Custom);
133 setOperationAction(ISD::SELECT, MVT::f64, Custom);
134 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
136 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000137 setOperationAction(ISD::VASTART, MVT::Other, Custom);
138
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000139 setOperationAction(ISD::SDIV, MVT::i32, Expand);
140 setOperationAction(ISD::SREM, MVT::i32, Expand);
141 setOperationAction(ISD::UDIV, MVT::i32, Expand);
142 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000143 setOperationAction(ISD::SDIV, MVT::i64, Expand);
144 setOperationAction(ISD::SREM, MVT::i64, Expand);
145 setOperationAction(ISD::UDIV, MVT::i64, Expand);
146 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000147
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000148 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000149 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
150 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
151 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
152 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
153 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
154 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
155 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
156 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
157 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000158 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000159
Akira Hatanaka56633442011-09-20 23:53:09 +0000160 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000161 setOperationAction(ISD::ROTR, MVT::i32, Expand);
162
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000163 if (!Subtarget->hasMips64r2())
164 setOperationAction(ISD::ROTR, MVT::i64, Expand);
165
Owen Anderson825b72b2009-08-11 20:47:22 +0000166 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
167 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
168 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000169 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
170 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000171 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000172 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000173 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000174 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000175 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
176 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000177 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000178 setOperationAction(ISD::FLOG, MVT::f32, Expand);
179 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
180 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
181 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000182 setOperationAction(ISD::FMA, MVT::f32, Expand);
183 setOperationAction(ISD::FMA, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000184
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000185 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
186 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Eric Christopher471e4222011-06-08 23:55:35 +0000187
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000188 setOperationAction(ISD::VAARG, MVT::Other, Expand);
189 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
190 setOperationAction(ISD::VAEND, MVT::Other, Expand);
191
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000192 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000193 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
194 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000195
Akira Hatanakadb548262011-07-19 23:30:50 +0000196 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000197 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000198
Eli Friedman4db5aca2011-08-29 18:23:02 +0000199 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
200 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
201
Eli Friedman26689ac2011-08-03 21:06:02 +0000202 setInsertFencesForAtomic(true);
203
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000204 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000205 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000206
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000207 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000208 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
209 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000210 }
211
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000212 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000213 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000214
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000215 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000216 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000217
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000218 setTargetDAGCombine(ISD::ADDE);
219 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000220 setTargetDAGCombine(ISD::SDIVREM);
221 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000222 setTargetDAGCombine(ISD::SETCC);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000223 setTargetDAGCombine(ISD::AND);
224 setTargetDAGCombine(ISD::OR);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000225
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000226 setMinFunctionAlignment(2);
227
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000228 setStackPointerRegisterToSaveRestore(Mips::SP);
229 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000230
231 setExceptionPointerRegister(Mips::A0);
232 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000233}
234
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000235bool MipsTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Akira Hatanaka511961a2011-08-17 18:49:18 +0000236 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
Akira Hatanaka7bd19bd2011-10-11 00:27:28 +0000237 return SVT == MVT::i64 || SVT == MVT::i32 || SVT == MVT::i16;
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000238}
239
Duncan Sands28b77e92011-09-06 19:07:46 +0000240EVT MipsTargetLowering::getSetCCResultType(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000241 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000242}
243
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000244// SelectMadd -
245// Transforms a subgraph in CurDAG if the following pattern is found:
246// (addc multLo, Lo0), (adde multHi, Hi0),
247// where,
248// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000249// Lo0: initial value of Lo register
250// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000251// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000252static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000253 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000254 // for the matching to be successful.
255 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
256
257 if (ADDCNode->getOpcode() != ISD::ADDC)
258 return false;
259
260 SDValue MultHi = ADDENode->getOperand(0);
261 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000262 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000263 unsigned MultOpc = MultHi.getOpcode();
264
265 // MultHi and MultLo must be generated by the same node,
266 if (MultLo.getNode() != MultNode)
267 return false;
268
269 // and it must be a multiplication.
270 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
271 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000272
273 // MultLo amd MultHi must be the first and second output of MultNode
274 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000275 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
276 return false;
277
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000278 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000279 // of the values of MultNode, in which case MultNode will be removed in later
280 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000281 // If there exist users other than ADDENode or ADDCNode, this function returns
282 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000283 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000284 // produced.
285 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
286 return false;
287
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000288 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000289 DebugLoc dl = ADDENode->getDebugLoc();
290
291 // create MipsMAdd(u) node
292 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000293
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000294 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
295 MVT::Glue,
296 MultNode->getOperand(0),// Factor 0
297 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000298 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000299 ADDENode->getOperand(1));// Hi0
300
301 // create CopyFromReg nodes
302 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
303 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000304 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000305 Mips::HI, MVT::i32,
306 CopyFromLo.getValue(2));
307
308 // replace uses of adde and addc here
309 if (!SDValue(ADDCNode, 0).use_empty())
310 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
311
312 if (!SDValue(ADDENode, 0).use_empty())
313 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
314
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000315 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000316}
317
318// SelectMsub -
319// Transforms a subgraph in CurDAG if the following pattern is found:
320// (addc Lo0, multLo), (sube Hi0, multHi),
321// where,
322// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000323// Lo0: initial value of Lo register
324// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000325// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000326static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000327 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000328 // for the matching to be successful.
329 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
330
331 if (SUBCNode->getOpcode() != ISD::SUBC)
332 return false;
333
334 SDValue MultHi = SUBENode->getOperand(1);
335 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000336 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000337 unsigned MultOpc = MultHi.getOpcode();
338
339 // MultHi and MultLo must be generated by the same node,
340 if (MultLo.getNode() != MultNode)
341 return false;
342
343 // and it must be a multiplication.
344 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
345 return false;
346
347 // MultLo amd MultHi must be the first and second output of MultNode
348 // respectively.
349 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
350 return false;
351
352 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
353 // of the values of MultNode, in which case MultNode will be removed in later
354 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000355 // If there exist users other than SUBENode or SUBCNode, this function returns
356 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000357 // instruction node rather than a pair of MULT and MSUB instructions being
358 // produced.
359 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
360 return false;
361
362 SDValue Chain = CurDAG->getEntryNode();
363 DebugLoc dl = SUBENode->getDebugLoc();
364
365 // create MipsSub(u) node
366 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
367
368 SDValue MSub = CurDAG->getNode(MultOpc, dl,
369 MVT::Glue,
370 MultNode->getOperand(0),// Factor 0
371 MultNode->getOperand(1),// Factor 1
372 SUBCNode->getOperand(0),// Lo0
373 SUBENode->getOperand(0));// Hi0
374
375 // create CopyFromReg nodes
376 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
377 MSub);
378 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
379 Mips::HI, MVT::i32,
380 CopyFromLo.getValue(2));
381
382 // replace uses of sube and subc here
383 if (!SDValue(SUBCNode, 0).use_empty())
384 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
385
386 if (!SDValue(SUBENode, 0).use_empty())
387 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
388
389 return true;
390}
391
392static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
393 TargetLowering::DAGCombinerInfo &DCI,
394 const MipsSubtarget* Subtarget) {
395 if (DCI.isBeforeLegalize())
396 return SDValue();
397
Akira Hatanaka56633442011-09-20 23:53:09 +0000398 if (Subtarget->hasMips32() && SelectMadd(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000399 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000400
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000401 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000402}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000403
404static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
405 TargetLowering::DAGCombinerInfo &DCI,
406 const MipsSubtarget* Subtarget) {
407 if (DCI.isBeforeLegalize())
408 return SDValue();
409
Akira Hatanaka56633442011-09-20 23:53:09 +0000410 if (Subtarget->hasMips32() && SelectMsub(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000411 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000412
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000413 return SDValue();
414}
415
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000416static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
417 TargetLowering::DAGCombinerInfo &DCI,
418 const MipsSubtarget* Subtarget) {
419 if (DCI.isBeforeLegalizeOps())
420 return SDValue();
421
Akira Hatanakadda4a072011-10-03 21:06:13 +0000422 EVT Ty = N->getValueType(0);
423 unsigned LO = (Ty == MVT::i32) ? Mips::LO : Mips::LO64;
424 unsigned HI = (Ty == MVT::i32) ? Mips::HI : Mips::HI64;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000425 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
426 MipsISD::DivRemU;
427 DebugLoc dl = N->getDebugLoc();
428
429 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
430 N->getOperand(0), N->getOperand(1));
431 SDValue InChain = DAG.getEntryNode();
432 SDValue InGlue = DivRem;
433
434 // insert MFLO
435 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakadda4a072011-10-03 21:06:13 +0000436 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000437 InGlue);
438 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
439 InChain = CopyFromLo.getValue(1);
440 InGlue = CopyFromLo.getValue(2);
441 }
442
443 // insert MFHI
444 if (N->hasAnyUseOfValue(1)) {
445 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000446 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000447 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
448 }
449
450 return SDValue();
451}
452
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000453static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
454 switch (CC) {
455 default: llvm_unreachable("Unknown fp condition code!");
456 case ISD::SETEQ:
457 case ISD::SETOEQ: return Mips::FCOND_OEQ;
458 case ISD::SETUNE: return Mips::FCOND_UNE;
459 case ISD::SETLT:
460 case ISD::SETOLT: return Mips::FCOND_OLT;
461 case ISD::SETGT:
462 case ISD::SETOGT: return Mips::FCOND_OGT;
463 case ISD::SETLE:
464 case ISD::SETOLE: return Mips::FCOND_OLE;
465 case ISD::SETGE:
466 case ISD::SETOGE: return Mips::FCOND_OGE;
467 case ISD::SETULT: return Mips::FCOND_ULT;
468 case ISD::SETULE: return Mips::FCOND_ULE;
469 case ISD::SETUGT: return Mips::FCOND_UGT;
470 case ISD::SETUGE: return Mips::FCOND_UGE;
471 case ISD::SETUO: return Mips::FCOND_UN;
472 case ISD::SETO: return Mips::FCOND_OR;
473 case ISD::SETNE:
474 case ISD::SETONE: return Mips::FCOND_ONE;
475 case ISD::SETUEQ: return Mips::FCOND_UEQ;
476 }
477}
478
479
480// Returns true if condition code has to be inverted.
481static bool InvertFPCondCode(Mips::CondCode CC) {
482 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
483 return false;
484
485 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
486 return true;
487
488 assert(false && "Illegal Condition Code");
489 return false;
490}
491
492// Creates and returns an FPCmp node from a setcc node.
493// Returns Op if setcc is not a floating point comparison.
494static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
495 // must be a SETCC node
496 if (Op.getOpcode() != ISD::SETCC)
497 return Op;
498
499 SDValue LHS = Op.getOperand(0);
500
501 if (!LHS.getValueType().isFloatingPoint())
502 return Op;
503
504 SDValue RHS = Op.getOperand(1);
505 DebugLoc dl = Op.getDebugLoc();
506
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000507 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
508 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000509 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
510
511 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
512 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
513}
514
515// Creates and returns a CMovFPT/F node.
516static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
517 SDValue False, DebugLoc DL) {
518 bool invert = InvertFPCondCode((Mips::CondCode)
519 cast<ConstantSDNode>(Cond.getOperand(2))
520 ->getSExtValue());
521
522 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
523 True.getValueType(), True, False, Cond);
524}
525
526static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
527 TargetLowering::DAGCombinerInfo &DCI,
528 const MipsSubtarget* Subtarget) {
529 if (DCI.isBeforeLegalizeOps())
530 return SDValue();
531
532 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
533
534 if (Cond.getOpcode() != MipsISD::FPCmp)
535 return SDValue();
536
537 SDValue True = DAG.getConstant(1, MVT::i32);
538 SDValue False = DAG.getConstant(0, MVT::i32);
539
540 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
541}
542
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000543static SDValue PerformANDCombine(SDNode *N, SelectionDAG& DAG,
544 TargetLowering::DAGCombinerInfo &DCI,
545 const MipsSubtarget* Subtarget) {
546 // Pattern match EXT.
547 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
548 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000549 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000550 return SDValue();
551
552 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
553
554 // Op's first operand must be a shift right.
555 if (ShiftRight.getOpcode() != ISD::SRA && ShiftRight.getOpcode() != ISD::SRL)
556 return SDValue();
557
558 // The second operand of the shift must be an immediate.
559 uint64_t Pos;
560 ConstantSDNode *CN;
561 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
562 return SDValue();
563
564 Pos = CN->getZExtValue();
565
566 uint64_t SMPos, SMSize;
567 // Op's second operand must be a shifted mask.
568 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000569 !IsShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000570 return SDValue();
571
572 // Return if the shifted mask does not start at bit 0 or the sum of its size
573 // and Pos exceeds the word's size.
574 if (SMPos != 0 || Pos + SMSize > 32)
575 return SDValue();
576
577 return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), MVT::i32,
578 ShiftRight.getOperand(0),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000579 DAG.getConstant(Pos, MVT::i32),
580 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000581}
582
583static SDValue PerformORCombine(SDNode *N, SelectionDAG& DAG,
584 TargetLowering::DAGCombinerInfo &DCI,
585 const MipsSubtarget* Subtarget) {
586 // Pattern match INS.
587 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
588 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
589 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000590 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000591 return SDValue();
592
593 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
594 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
595 ConstantSDNode *CN;
596
597 // See if Op's first operand matches (and $src1 , mask0).
598 if (And0.getOpcode() != ISD::AND)
599 return SDValue();
600
601 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000602 !IsShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000603 return SDValue();
604
605 // See if Op's second operand matches (and (shl $src, pos), mask1).
606 if (And1.getOpcode() != ISD::AND)
607 return SDValue();
608
609 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000610 !IsShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000611 return SDValue();
612
613 // The shift masks must have the same position and size.
614 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
615 return SDValue();
616
617 SDValue Shl = And1.getOperand(0);
618 if (Shl.getOpcode() != ISD::SHL)
619 return SDValue();
620
621 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
622 return SDValue();
623
624 unsigned Shamt = CN->getZExtValue();
625
626 // Return if the shift amount and the first bit position of mask are not the
627 // same.
628 if (Shamt != SMPos0)
629 return SDValue();
630
631 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), MVT::i32,
632 Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000633 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000634 DAG.getConstant(SMSize0, MVT::i32),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000635 And0.getOperand(0));
636}
637
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000638SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000639 const {
640 SelectionDAG &DAG = DCI.DAG;
641 unsigned opc = N->getOpcode();
642
643 switch (opc) {
644 default: break;
645 case ISD::ADDE:
646 return PerformADDECombine(N, DAG, DCI, Subtarget);
647 case ISD::SUBE:
648 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000649 case ISD::SDIVREM:
650 case ISD::UDIVREM:
651 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000652 case ISD::SETCC:
653 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000654 case ISD::AND:
655 return PerformANDCombine(N, DAG, DCI, Subtarget);
656 case ISD::OR:
657 return PerformORCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000658 }
659
660 return SDValue();
661}
662
Dan Gohman475871a2008-07-27 21:46:04 +0000663SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000664LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000665{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000666 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000667 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000668 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000669 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
670 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000671 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000672 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000673 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
674 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000675 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000676 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000677 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000678 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Akira Hatanakadb548262011-07-19 23:30:50 +0000679 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +0000680 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000681 }
Dan Gohman475871a2008-07-27 21:46:04 +0000682 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000683}
684
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000685//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000686// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000687//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000688
689// AddLiveIn - This helper function adds the specified physical register to the
690// MachineFunction as a live in value. It also creates a corresponding
691// virtual register for it.
692static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000693AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000694{
695 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000696 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
697 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000698 return VReg;
699}
700
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000701// Get fp branch code (not opcode) from condition code.
702static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
703 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
704 return Mips::BRANCH_T;
705
706 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
707 return Mips::BRANCH_F;
708
709 return Mips::BRANCH_INVALID;
710}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000711
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000712/*
Akira Hatanaka14487d42011-06-07 19:28:39 +0000713static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
714 DebugLoc dl,
715 const MipsSubtarget* Subtarget,
716 const TargetInstrInfo *TII,
717 bool isFPCmp, unsigned Opc) {
718 // There is no need to expand CMov instructions if target has
719 // conditional moves.
720 if (Subtarget->hasCondMov())
721 return BB;
722
723 // To "insert" a SELECT_CC instruction, we actually have to insert the
724 // diamond control-flow pattern. The incoming instruction knows the
725 // destination vreg to set, the condition code register to branch on, the
726 // true/false values to select between, and a branch opcode to use.
727 const BasicBlock *LLVM_BB = BB->getBasicBlock();
728 MachineFunction::iterator It = BB;
729 ++It;
730
731 // thisMBB:
732 // ...
733 // TrueVal = ...
734 // setcc r1, r2, r3
735 // bNE r1, r0, copy1MBB
736 // fallthrough --> copy0MBB
737 MachineBasicBlock *thisMBB = BB;
738 MachineFunction *F = BB->getParent();
739 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
740 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
741 F->insert(It, copy0MBB);
742 F->insert(It, sinkMBB);
743
744 // Transfer the remainder of BB and its successor edges to sinkMBB.
745 sinkMBB->splice(sinkMBB->begin(), BB,
746 llvm::next(MachineBasicBlock::iterator(MI)),
747 BB->end());
748 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
749
750 // Next, add the true and fallthrough blocks as its successors.
751 BB->addSuccessor(copy0MBB);
752 BB->addSuccessor(sinkMBB);
753
754 // Emit the right instruction according to the type of the operands compared
755 if (isFPCmp)
756 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
757 else
758 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
759 .addReg(Mips::ZERO).addMBB(sinkMBB);
760
761 // copy0MBB:
762 // %FalseValue = ...
763 // # fallthrough to sinkMBB
764 BB = copy0MBB;
765
766 // Update machine-CFG edges
767 BB->addSuccessor(sinkMBB);
768
769 // sinkMBB:
770 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
771 // ...
772 BB = sinkMBB;
773
774 if (isFPCmp)
775 BuildMI(*BB, BB->begin(), dl,
776 TII->get(Mips::PHI), MI->getOperand(0).getReg())
777 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
778 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
779 else
780 BuildMI(*BB, BB->begin(), dl,
781 TII->get(Mips::PHI), MI->getOperand(0).getReg())
782 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
783 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
784
785 MI->eraseFromParent(); // The pseudo instruction is gone now.
786 return BB;
787}
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000788*/
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000789MachineBasicBlock *
790MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000791 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000792 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000793 default:
794 assert(false && "Unexpected instr type to insert");
795 return NULL;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000796 case Mips::ATOMIC_LOAD_ADD_I8:
797 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
798 case Mips::ATOMIC_LOAD_ADD_I16:
799 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
800 case Mips::ATOMIC_LOAD_ADD_I32:
801 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
802
803 case Mips::ATOMIC_LOAD_AND_I8:
804 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
805 case Mips::ATOMIC_LOAD_AND_I16:
806 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
807 case Mips::ATOMIC_LOAD_AND_I32:
808 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
809
810 case Mips::ATOMIC_LOAD_OR_I8:
811 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
812 case Mips::ATOMIC_LOAD_OR_I16:
813 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
814 case Mips::ATOMIC_LOAD_OR_I32:
815 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
816
817 case Mips::ATOMIC_LOAD_XOR_I8:
818 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
819 case Mips::ATOMIC_LOAD_XOR_I16:
820 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
821 case Mips::ATOMIC_LOAD_XOR_I32:
822 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
823
824 case Mips::ATOMIC_LOAD_NAND_I8:
825 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
826 case Mips::ATOMIC_LOAD_NAND_I16:
827 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
828 case Mips::ATOMIC_LOAD_NAND_I32:
829 return EmitAtomicBinary(MI, BB, 4, 0, true);
830
831 case Mips::ATOMIC_LOAD_SUB_I8:
832 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
833 case Mips::ATOMIC_LOAD_SUB_I16:
834 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
835 case Mips::ATOMIC_LOAD_SUB_I32:
836 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
837
838 case Mips::ATOMIC_SWAP_I8:
839 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
840 case Mips::ATOMIC_SWAP_I16:
841 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
842 case Mips::ATOMIC_SWAP_I32:
843 return EmitAtomicBinary(MI, BB, 4, 0);
844
845 case Mips::ATOMIC_CMP_SWAP_I8:
846 return EmitAtomicCmpSwapPartword(MI, BB, 1);
847 case Mips::ATOMIC_CMP_SWAP_I16:
848 return EmitAtomicCmpSwapPartword(MI, BB, 2);
849 case Mips::ATOMIC_CMP_SWAP_I32:
850 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000851 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000852}
853
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000854// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
855// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
856MachineBasicBlock *
857MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000858 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000859 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000860 assert(Size == 4 && "Unsupported size for EmitAtomicBinary.");
861
862 MachineFunction *MF = BB->getParent();
863 MachineRegisterInfo &RegInfo = MF->getRegInfo();
864 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
865 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
866 DebugLoc dl = MI->getDebugLoc();
867
Akira Hatanaka4061da12011-07-19 20:11:17 +0000868 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000869 unsigned Ptr = MI->getOperand(1).getReg();
870 unsigned Incr = MI->getOperand(2).getReg();
871
Akira Hatanaka4061da12011-07-19 20:11:17 +0000872 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
873 unsigned AndRes = RegInfo.createVirtualRegister(RC);
874 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000875
876 // insert new blocks after the current block
877 const BasicBlock *LLVM_BB = BB->getBasicBlock();
878 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
879 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
880 MachineFunction::iterator It = BB;
881 ++It;
882 MF->insert(It, loopMBB);
883 MF->insert(It, exitMBB);
884
885 // Transfer the remainder of BB and its successor edges to exitMBB.
886 exitMBB->splice(exitMBB->begin(), BB,
887 llvm::next(MachineBasicBlock::iterator(MI)),
888 BB->end());
889 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
890
891 // thisMBB:
892 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000893 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000894 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000895 loopMBB->addSuccessor(loopMBB);
896 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000897
898 // loopMBB:
899 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000900 // <binop> storeval, oldval, incr
901 // sc success, storeval, 0(ptr)
902 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000903 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000904 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000905 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000906 // and andres, oldval, incr
907 // nor storeval, $0, andres
908 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr);
909 BuildMI(BB, dl, TII->get(Mips::NOR), StoreVal)
910 .addReg(Mips::ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000911 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000912 // <binop> storeval, oldval, incr
913 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000914 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000915 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000916 }
Akira Hatanaka4061da12011-07-19 20:11:17 +0000917 BuildMI(BB, dl, TII->get(Mips::SC), Success)
918 .addReg(StoreVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000919 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +0000920 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000921
922 MI->eraseFromParent(); // The instruction is gone now.
923
Akira Hatanaka939ece12011-07-19 03:42:13 +0000924 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000925}
926
927MachineBasicBlock *
928MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000929 MachineBasicBlock *BB,
930 unsigned Size, unsigned BinOpcode,
931 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000932 assert((Size == 1 || Size == 2) &&
933 "Unsupported size for EmitAtomicBinaryPartial.");
934
935 MachineFunction *MF = BB->getParent();
936 MachineRegisterInfo &RegInfo = MF->getRegInfo();
937 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
938 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
939 DebugLoc dl = MI->getDebugLoc();
940
941 unsigned Dest = MI->getOperand(0).getReg();
942 unsigned Ptr = MI->getOperand(1).getReg();
943 unsigned Incr = MI->getOperand(2).getReg();
944
Akira Hatanaka4061da12011-07-19 20:11:17 +0000945 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
946 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000947 unsigned Mask = RegInfo.createVirtualRegister(RC);
948 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000949 unsigned NewVal = RegInfo.createVirtualRegister(RC);
950 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000951 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000952 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
953 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
954 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
955 unsigned AndRes = RegInfo.createVirtualRegister(RC);
956 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +0000957 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000958 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
959 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
960 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
961 unsigned SllRes = RegInfo.createVirtualRegister(RC);
962 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000963
964 // insert new blocks after the current block
965 const BasicBlock *LLVM_BB = BB->getBasicBlock();
966 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000967 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000968 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
969 MachineFunction::iterator It = BB;
970 ++It;
971 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000972 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000973 MF->insert(It, exitMBB);
974
975 // Transfer the remainder of BB and its successor edges to exitMBB.
976 exitMBB->splice(exitMBB->begin(), BB,
977 llvm::next(MachineBasicBlock::iterator(MI)),
978 BB->end());
979 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
980
Akira Hatanaka81b44112011-07-19 17:09:53 +0000981 BB->addSuccessor(loopMBB);
982 loopMBB->addSuccessor(loopMBB);
983 loopMBB->addSuccessor(sinkMBB);
984 sinkMBB->addSuccessor(exitMBB);
985
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000986 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +0000987 // addiu masklsb2,$0,-4 # 0xfffffffc
988 // and alignedaddr,ptr,masklsb2
989 // andi ptrlsb2,ptr,3
990 // sll shiftamt,ptrlsb2,3
991 // ori maskupper,$0,255 # 0xff
992 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000993 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +0000994 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000995
996 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000997 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
998 .addReg(Mips::ZERO).addImm(-4);
999 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1000 .addReg(Ptr).addReg(MaskLSB2);
1001 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1002 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1003 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1004 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001005 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1006 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001007 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001008 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001009
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001010
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001011 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001012 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001013 // ll oldval,0(alignedaddr)
1014 // binop binopres,oldval,incr2
1015 // and newval,binopres,mask
1016 // and maskedoldval0,oldval,mask2
1017 // or storeval,maskedoldval0,newval
1018 // sc success,storeval,0(alignedaddr)
1019 // beq success,$0,loopMBB
1020
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001021 // atomic.swap
1022 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001023 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001024 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001025 // and maskedoldval0,oldval,mask2
1026 // or storeval,maskedoldval0,newval
1027 // sc success,storeval,0(alignedaddr)
1028 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001029
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001030 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001031 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001032 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001033 // and andres, oldval, incr2
1034 // nor binopres, $0, andres
1035 // and newval, binopres, mask
1036 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1037 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1038 .addReg(Mips::ZERO).addReg(AndRes);
1039 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001040 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001041 // <binop> binopres, oldval, incr2
1042 // and newval, binopres, mask
1043 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1044 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001045 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001046 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001047 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001048 }
1049
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001050 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001051 .addReg(OldVal).addReg(Mask2);
1052 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001053 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001054 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1055 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001056 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001057 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001058
Akira Hatanaka939ece12011-07-19 03:42:13 +00001059 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001060 // and maskedoldval1,oldval,mask
1061 // srl srlres,maskedoldval1,shiftamt
1062 // sll sllres,srlres,24
1063 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001064 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001065 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001066
Akira Hatanaka4061da12011-07-19 20:11:17 +00001067 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1068 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001069 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1070 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001071 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1072 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001073 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001074 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001075
1076 MI->eraseFromParent(); // The instruction is gone now.
1077
Akira Hatanaka939ece12011-07-19 03:42:13 +00001078 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001079}
1080
1081MachineBasicBlock *
1082MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001083 MachineBasicBlock *BB,
1084 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001085 assert(Size == 4 && "Unsupported size for EmitAtomicCmpSwap.");
1086
1087 MachineFunction *MF = BB->getParent();
1088 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1089 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1090 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1091 DebugLoc dl = MI->getDebugLoc();
1092
1093 unsigned Dest = MI->getOperand(0).getReg();
1094 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001095 unsigned OldVal = MI->getOperand(2).getReg();
1096 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001097
Akira Hatanaka4061da12011-07-19 20:11:17 +00001098 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001099
1100 // insert new blocks after the current block
1101 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1102 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1103 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1104 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1105 MachineFunction::iterator It = BB;
1106 ++It;
1107 MF->insert(It, loop1MBB);
1108 MF->insert(It, loop2MBB);
1109 MF->insert(It, exitMBB);
1110
1111 // Transfer the remainder of BB and its successor edges to exitMBB.
1112 exitMBB->splice(exitMBB->begin(), BB,
1113 llvm::next(MachineBasicBlock::iterator(MI)),
1114 BB->end());
1115 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1116
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001117 // thisMBB:
1118 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001119 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001120 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001121 loop1MBB->addSuccessor(exitMBB);
1122 loop1MBB->addSuccessor(loop2MBB);
1123 loop2MBB->addSuccessor(loop1MBB);
1124 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001125
1126 // loop1MBB:
1127 // ll dest, 0(ptr)
1128 // bne dest, oldval, exitMBB
1129 BB = loop1MBB;
Akira Hatanakad3ac47f2011-07-07 18:57:00 +00001130 BuildMI(BB, dl, TII->get(Mips::LL), Dest).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001131 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001132 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001133
1134 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001135 // sc success, newval, 0(ptr)
1136 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001137 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001138 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1139 .addReg(NewVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001140 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001141 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001142
1143 MI->eraseFromParent(); // The instruction is gone now.
1144
Akira Hatanaka939ece12011-07-19 03:42:13 +00001145 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001146}
1147
1148MachineBasicBlock *
1149MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001150 MachineBasicBlock *BB,
1151 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001152 assert((Size == 1 || Size == 2) &&
1153 "Unsupported size for EmitAtomicCmpSwapPartial.");
1154
1155 MachineFunction *MF = BB->getParent();
1156 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1157 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1158 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1159 DebugLoc dl = MI->getDebugLoc();
1160
1161 unsigned Dest = MI->getOperand(0).getReg();
1162 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001163 unsigned CmpVal = MI->getOperand(2).getReg();
1164 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001165
Akira Hatanaka4061da12011-07-19 20:11:17 +00001166 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1167 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001168 unsigned Mask = RegInfo.createVirtualRegister(RC);
1169 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001170 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1171 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1172 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1173 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1174 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1175 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1176 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1177 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1178 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1179 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1180 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1181 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1182 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1183 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001184
1185 // insert new blocks after the current block
1186 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1187 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1188 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001189 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001190 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1191 MachineFunction::iterator It = BB;
1192 ++It;
1193 MF->insert(It, loop1MBB);
1194 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001195 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001196 MF->insert(It, exitMBB);
1197
1198 // Transfer the remainder of BB and its successor edges to exitMBB.
1199 exitMBB->splice(exitMBB->begin(), BB,
1200 llvm::next(MachineBasicBlock::iterator(MI)),
1201 BB->end());
1202 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1203
Akira Hatanaka81b44112011-07-19 17:09:53 +00001204 BB->addSuccessor(loop1MBB);
1205 loop1MBB->addSuccessor(sinkMBB);
1206 loop1MBB->addSuccessor(loop2MBB);
1207 loop2MBB->addSuccessor(loop1MBB);
1208 loop2MBB->addSuccessor(sinkMBB);
1209 sinkMBB->addSuccessor(exitMBB);
1210
Akira Hatanaka70564a92011-07-19 18:14:26 +00001211 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001212 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001213 // addiu masklsb2,$0,-4 # 0xfffffffc
1214 // and alignedaddr,ptr,masklsb2
1215 // andi ptrlsb2,ptr,3
1216 // sll shiftamt,ptrlsb2,3
1217 // ori maskupper,$0,255 # 0xff
1218 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001219 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001220 // andi maskedcmpval,cmpval,255
1221 // sll shiftedcmpval,maskedcmpval,shiftamt
1222 // andi maskednewval,newval,255
1223 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001224 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001225 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1226 .addReg(Mips::ZERO).addImm(-4);
1227 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1228 .addReg(Ptr).addReg(MaskLSB2);
1229 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1230 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1231 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1232 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001233 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1234 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001235 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001236 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1237 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001238 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1239 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001240 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1241 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001242 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1243 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001244
1245 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001246 // ll oldval,0(alginedaddr)
1247 // and maskedoldval0,oldval,mask
1248 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001249 BB = loop1MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001250 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
1251 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1252 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001253 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001254 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001255
1256 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001257 // and maskedoldval1,oldval,mask2
1258 // or storeval,maskedoldval1,shiftednewval
1259 // sc success,storeval,0(alignedaddr)
1260 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001261 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001262 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1263 .addReg(OldVal).addReg(Mask2);
1264 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1265 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1266 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1267 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001268 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001269 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001270
Akira Hatanaka939ece12011-07-19 03:42:13 +00001271 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001272 // srl srlres,maskedoldval0,shiftamt
1273 // sll sllres,srlres,24
1274 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001275 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001276 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001277
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001278 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1279 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001280 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1281 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001282 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001283 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001284
1285 MI->eraseFromParent(); // The instruction is gone now.
1286
Akira Hatanaka939ece12011-07-19 03:42:13 +00001287 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001288}
1289
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001290//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001291// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001292//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001293SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001294LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001295{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001296 MachineFunction &MF = DAG.getMachineFunction();
1297 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1298
1299 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001300 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1301 "Cannot lower if the alignment of the allocated space is larger than \
1302 that of the stack.");
1303
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001304 SDValue Chain = Op.getOperand(0);
1305 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001306 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001307
1308 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +00001309 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001310
1311 // Subtract the dynamic size from the actual stack size to
1312 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +00001313 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001314
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001315 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001316 // must be placed in the stack pointer register.
Akira Hatanaka053546c2011-05-25 02:20:00 +00001317 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub,
1318 SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001319
1320 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001321 // value and a chain
Akira Hatanaka21afc632011-06-21 00:40:49 +00001322 SDVTList VTLs = DAG.getVTList(MVT::i32, MVT::Other);
1323 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1324 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1325
1326 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001327}
1328
1329SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001330LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001331{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001332 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001333 // the block to branch to if the condition is true.
1334 SDValue Chain = Op.getOperand(0);
1335 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001336 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001337
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001338 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1339
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001340 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001341 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001342 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001343
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001344 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001345 Mips::CondCode CC =
1346 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001347 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001348
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001349 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001350 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001351}
1352
1353SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001354LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001355{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001356 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001357
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001358 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001359 if (Cond.getOpcode() != MipsISD::FPCmp)
1360 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001361
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001362 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1363 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001364}
1365
Dan Gohmand858e902010-04-17 15:26:15 +00001366SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1367 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001368 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001369 DebugLoc dl = Op.getDebugLoc();
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001370 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001371
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001372 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001373 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001374
Chris Lattnerb71b9092009-08-13 06:28:06 +00001375 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001376
Chris Lattnere3736f82009-08-13 05:41:27 +00001377 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001378 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1379 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001380 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001381 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1382 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001383 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001384 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001385 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001386 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1387 MipsII::MO_ABS_HI);
1388 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1389 MipsII::MO_ABS_LO);
1390 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1391 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001392 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001393 }
1394
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001395 EVT ValTy = Op.getValueType();
1396 bool HasGotOfst = (GV->hasInternalLinkage() ||
1397 (GV->hasLocalLinkage() && !isa<Function>(GV)));
1398 unsigned GotFlag = IsN64 ?
1399 (HasGotOfst ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT_DISP) :
1400 MipsII::MO_GOT;
1401 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0, GotFlag);
1402 GA = DAG.getNode(MipsISD::WrapperPIC, dl, ValTy, GA);
1403 SDValue ResNode = DAG.getLoad(ValTy, dl,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001404 DAG.getEntryNode(), GA, MachinePointerInfo(),
1405 false, false, 0);
1406 // On functions and global targets not internal linked only
1407 // a load from got/GP is necessary for PIC to work.
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001408 if (!HasGotOfst)
Akira Hatanaka0f843822011-06-07 18:58:42 +00001409 return ResNode;
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001410 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0,
1411 IsN64 ? MipsII::MO_GOT_OFST :
1412 MipsII::MO_ABS_LO);
1413 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, GALo);
1414 return DAG.getNode(ISD::ADD, dl, ValTy, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001415}
1416
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001417SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1418 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001419 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1420 // FIXME there isn't actually debug info here
1421 DebugLoc dl = Op.getDebugLoc();
1422
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001423 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001424 // %hi/%lo relocation
1425 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
1426 MipsII::MO_ABS_HI);
1427 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
1428 MipsII::MO_ABS_LO);
1429 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1430 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1431 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001432 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001433
1434 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1435 MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001436 BAGOTOffset = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, BAGOTOffset);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001437 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1438 MipsII::MO_ABS_LO);
1439 SDValue Load = DAG.getLoad(MVT::i32, dl,
1440 DAG.getEntryNode(), BAGOTOffset,
1441 MachinePointerInfo(), false, false, 0);
1442 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
1443 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001444}
1445
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001446SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001447LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001448{
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001449 // If the relocation model is PIC, use the General Dynamic TLS Model,
1450 // otherwise use the Initial Exec or Local Exec TLS Model.
1451 // TODO: implement Local Dynamic TLS model
1452
1453 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1454 DebugLoc dl = GA->getDebugLoc();
1455 const GlobalValue *GV = GA->getGlobal();
1456 EVT PtrVT = getPointerTy();
1457
1458 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1459 // General Dynamic TLS Model
1460 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001461 0, MipsII::MO_TLSGD);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001462 SDValue Tlsgd = DAG.getNode(MipsISD::TlsGd, dl, MVT::i32, TGA);
1463 SDValue GP = DAG.getRegister(Mips::GP, MVT::i32);
1464 SDValue Argument = DAG.getNode(ISD::ADD, dl, MVT::i32, GP, Tlsgd);
1465
1466 ArgListTy Args;
1467 ArgListEntry Entry;
1468 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001469 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001470 Args.push_back(Entry);
1471 std::pair<SDValue, SDValue> CallResult =
1472 LowerCallTo(DAG.getEntryNode(),
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001473 (Type *) Type::getInt32Ty(*DAG.getContext()),
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001474 false, false, false, false, 0, CallingConv::C, false, true,
1475 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG,
1476 dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001477
1478 return CallResult.first;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001479 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001480
1481 SDValue Offset;
1482 if (GV->isDeclaration()) {
1483 // Initial Exec TLS Model
1484 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1485 MipsII::MO_GOTTPREL);
1486 Offset = DAG.getLoad(MVT::i32, dl,
1487 DAG.getEntryNode(), TGA, MachinePointerInfo(),
1488 false, false, 0);
1489 } else {
1490 // Local Exec TLS Model
1491 SDVTList VTs = DAG.getVTList(MVT::i32);
1492 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1493 MipsII::MO_TPREL_HI);
1494 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1495 MipsII::MO_TPREL_LO);
1496 SDValue Hi = DAG.getNode(MipsISD::TprelHi, dl, VTs, &TGAHi, 1);
1497 SDValue Lo = DAG.getNode(MipsISD::TprelLo, dl, MVT::i32, TGALo);
1498 Offset = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1499 }
1500
1501 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1502 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001503}
1504
1505SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001506LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001507{
Dan Gohman475871a2008-07-27 21:46:04 +00001508 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001509 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +00001510 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001511 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001512 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001513 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001514
Owen Andersone50ed302009-08-10 22:56:29 +00001515 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001516 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001517
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001518 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
1519
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +00001520 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001521 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001522 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001523 } else {// Emit Load from Global Pointer
1524 JTI = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, JTI);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001525 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
1526 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001527 false, false, 0);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001528 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001529
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001530 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1531 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001532 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001533 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001534
1535 return ResNode;
1536}
1537
Dan Gohman475871a2008-07-27 21:46:04 +00001538SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001539LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001540{
Dan Gohman475871a2008-07-27 21:46:04 +00001541 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001542 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001543 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001544 // FIXME there isn't actually debug info here
1545 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001546
1547 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001548 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001549 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001550 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001551 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001552 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001553 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1554 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001555 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001556
1557 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001558 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001559 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001560 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001561 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001562 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1563 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001564 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001565 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001566 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001567 N->getOffset(), MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001568 CP = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001569 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001570 CP, MachinePointerInfo::getConstantPool(),
1571 false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001572 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001573 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001574 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001575 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
1576 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001577
1578 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001579}
1580
Dan Gohmand858e902010-04-17 15:26:15 +00001581SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001582 MachineFunction &MF = DAG.getMachineFunction();
1583 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1584
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001585 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001586 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1587 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001588
1589 // vastart just stores the address of the VarArgsFrameIndex slot into the
1590 // memory location argument.
1591 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001592 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1593 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +00001594 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001595}
1596
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001597static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
1598 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
1599 DebugLoc dl = Op.getDebugLoc();
1600 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
1601 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
1602 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
1603 DAG.getConstant(0x7fffffff, MVT::i32));
1604 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
1605 DAG.getConstant(0x80000000, MVT::i32));
1606 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1607 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
1608}
1609
1610static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001611 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001612 // Use ext/ins instructions if target architecture is Mips32r2.
1613 // Eliminate redundant mfc1 and mtc1 instructions.
1614 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001615
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001616 if (!isLittle)
1617 std::swap(LoIdx, HiIdx);
1618
1619 DebugLoc dl = Op.getDebugLoc();
1620 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1621 Op.getOperand(0),
1622 DAG.getConstant(LoIdx, MVT::i32));
1623 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1624 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1625 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1626 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1627 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1628 DAG.getConstant(0x7fffffff, MVT::i32));
1629 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1630 DAG.getConstant(0x80000000, MVT::i32));
1631 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1632
1633 if (!isLittle)
1634 std::swap(Word0, Word1);
1635
1636 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1637}
1638
1639SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1640 const {
1641 EVT Ty = Op.getValueType();
1642
1643 assert(Ty == MVT::f32 || Ty == MVT::f64);
1644
1645 if (Ty == MVT::f32)
1646 return LowerFCOPYSIGN32(Op, DAG);
1647 else
1648 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
1649}
1650
Akira Hatanaka2e591472011-06-02 00:24:44 +00001651SDValue MipsTargetLowering::
1652LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001653 // check the depth
1654 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001655 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001656
1657 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1658 MFI->setFrameAddressIsTaken(true);
1659 EVT VT = Op.getValueType();
1660 DebugLoc dl = Op.getDebugLoc();
1661 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Mips::FP, VT);
1662 return FrameAddr;
1663}
1664
Akira Hatanakadb548262011-07-19 23:30:50 +00001665// TODO: set SType according to the desired memory barrier behavior.
1666SDValue MipsTargetLowering::LowerMEMBARRIER(SDValue Op,
1667 SelectionDAG& DAG) const {
1668 unsigned SType = 0;
1669 DebugLoc dl = Op.getDebugLoc();
1670 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1671 DAG.getConstant(SType, MVT::i32));
1672}
1673
Eli Friedman14648462011-07-27 22:21:52 +00001674SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1675 SelectionDAG& DAG) const {
1676 // FIXME: Need pseudo-fence for 'singlethread' fences
1677 // FIXME: Set SType for weaker fences where supported/appropriate.
1678 unsigned SType = 0;
1679 DebugLoc dl = Op.getDebugLoc();
1680 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1681 DAG.getConstant(SType, MVT::i32));
1682}
1683
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001684//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001685// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001686//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001687
1688#include "MipsGenCallingConv.inc"
1689
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001690//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001691// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001692// Mips O32 ABI rules:
1693// ---
1694// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001695// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001696// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001697// f64 - Only passed in two aliased f32 registers if no int reg has been used
1698// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001699// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1700// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001701//
1702// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001703//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001704
Duncan Sands1e96bab2010-11-04 10:49:57 +00001705static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001706 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001707 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1708
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001709 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001710
1711 static const unsigned IntRegs[] = {
1712 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1713 };
1714 static const unsigned F32Regs[] = {
1715 Mips::F12, Mips::F14
1716 };
1717 static const unsigned F64Regs[] = {
1718 Mips::D6, Mips::D7
1719 };
1720
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001721 // ByVal Args
1722 if (ArgFlags.isByVal()) {
1723 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1724 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1725 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1726 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1727 r < std::min(IntRegsSize, NextReg); ++r)
1728 State.AllocateReg(IntRegs[r]);
1729 return false;
1730 }
1731
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001732 // Promote i8 and i16
1733 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1734 LocVT = MVT::i32;
1735 if (ArgFlags.isSExt())
1736 LocInfo = CCValAssign::SExt;
1737 else if (ArgFlags.isZExt())
1738 LocInfo = CCValAssign::ZExt;
1739 else
1740 LocInfo = CCValAssign::AExt;
1741 }
1742
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001743 unsigned Reg;
1744
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001745 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1746 // is true: function is vararg, argument is 3rd or higher, there is previous
1747 // argument which is not f32 or f64.
1748 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1749 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001750 unsigned OrigAlign = ArgFlags.getOrigAlign();
1751 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001752
1753 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001754 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001755 // If this is the first part of an i64 arg,
1756 // the allocated register must be either A0 or A2.
1757 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1758 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001759 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001760 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1761 // Allocate int register and shadow next int register. If first
1762 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001763 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1764 if (Reg == Mips::A1 || Reg == Mips::A3)
1765 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1766 State.AllocateReg(IntRegs, IntRegsSize);
1767 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001768 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1769 // we are guaranteed to find an available float register
1770 if (ValVT == MVT::f32) {
1771 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1772 // Shadow int register
1773 State.AllocateReg(IntRegs, IntRegsSize);
1774 } else {
1775 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1776 // Shadow int registers
1777 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1778 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1779 State.AllocateReg(IntRegs, IntRegsSize);
1780 State.AllocateReg(IntRegs, IntRegsSize);
1781 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001782 } else
1783 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001784
Akira Hatanakad37776d2011-05-20 21:39:54 +00001785 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1786 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1787
1788 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001789 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001790 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001791 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001792
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001793 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001794}
1795
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001796//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001797// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001798//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001799
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001800static const unsigned O32IntRegsSize = 4;
1801
1802static const unsigned O32IntRegs[] = {
1803 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1804};
1805
Akira Hatanaka373e3a42011-09-23 00:58:33 +00001806// Return next O32 integer argument register.
1807static unsigned getNextIntArgReg(unsigned Reg) {
1808 assert((Reg == Mips::A0) || (Reg == Mips::A2));
1809 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
1810}
1811
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001812// Write ByVal Arg to arg registers and stack.
1813static void
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001814WriteByValArg(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001815 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1816 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1817 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001818 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001819 MVT PtrType, bool isLittle) {
1820 unsigned LocMemOffset = VA.getLocMemOffset();
1821 unsigned Offset = 0;
1822 uint32_t RemainingSize = Flags.getByValSize();
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001823 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001824
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001825 // Copy the first 4 words of byval arg to registers A0 - A3.
1826 // FIXME: Use a stricter alignment if it enables better optimization in passes
1827 // run later.
1828 for (; RemainingSize >= 4 && LocMemOffset < 4 * 4;
1829 Offset += 4, RemainingSize -= 4, LocMemOffset += 4) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001830 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001831 DAG.getConstant(Offset, MVT::i32));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001832 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1833 MachinePointerInfo(),
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001834 false, false, std::min(ByValAlign,
1835 (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001836 MemOpChains.push_back(LoadVal.getValue(1));
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001837 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001838 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1839 }
1840
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001841 if (RemainingSize == 0)
1842 return;
1843
1844 // If there still is a register available for argument passing, write the
1845 // remaining part of the structure to it using subword loads and shifts.
1846 if (LocMemOffset < 4 * 4) {
1847 assert(RemainingSize <= 3 && RemainingSize >= 1 &&
1848 "There must be one to three bytes remaining.");
1849 unsigned LoadSize = (RemainingSize == 3 ? 2 : RemainingSize);
1850 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1851 DAG.getConstant(Offset, MVT::i32));
1852 unsigned Alignment = std::min(ByValAlign, (unsigned )4);
1853 SDValue LoadVal = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
1854 LoadPtr, MachinePointerInfo(),
1855 MVT::getIntegerVT(LoadSize * 8), false,
1856 false, Alignment);
1857 MemOpChains.push_back(LoadVal.getValue(1));
1858
1859 // If target is big endian, shift it to the most significant half-word or
1860 // byte.
1861 if (!isLittle)
1862 LoadVal = DAG.getNode(ISD::SHL, dl, MVT::i32, LoadVal,
1863 DAG.getConstant(32 - LoadSize * 8, MVT::i32));
1864
1865 Offset += LoadSize;
1866 RemainingSize -= LoadSize;
1867
1868 // Read second subword if necessary.
1869 if (RemainingSize != 0) {
1870 assert(RemainingSize == 1 && "There must be one byte remaining.");
1871 LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1872 DAG.getConstant(Offset, MVT::i32));
1873 unsigned Alignment = std::min(ByValAlign, (unsigned )2);
1874 SDValue Subword = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
1875 LoadPtr, MachinePointerInfo(),
1876 MVT::i8, false, false, Alignment);
1877 MemOpChains.push_back(Subword.getValue(1));
1878 // Insert the loaded byte to LoadVal.
1879 // FIXME: Use INS if supported by target.
1880 unsigned ShiftAmt = isLittle ? 16 : 8;
1881 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i32, Subword,
1882 DAG.getConstant(ShiftAmt, MVT::i32));
1883 LoadVal = DAG.getNode(ISD::OR, dl, MVT::i32, LoadVal, Shift);
1884 }
1885
1886 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
1887 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1888 return;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001889 }
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001890
1891 // Create a fixed object on stack at offset LocMemOffset and copy
1892 // remaining part of byval arg to it using memcpy.
1893 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1894 DAG.getConstant(Offset, MVT::i32));
1895 LastFI = MFI->CreateFixedObject(RemainingSize, LocMemOffset, true);
1896 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001897 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
1898 DAG.getConstant(RemainingSize, MVT::i32),
1899 std::min(ByValAlign, (unsigned)4),
1900 /*isVolatile=*/false, /*AlwaysInline=*/false,
1901 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001902}
1903
Dan Gohman98ca4f22009-08-05 01:29:28 +00001904/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001905/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001906/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001907SDValue
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001908MipsTargetLowering::LowerCall(SDValue InChain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001909 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001910 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001911 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001912 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001913 const SmallVectorImpl<ISD::InputArg> &Ins,
1914 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001915 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001916 // MIPs target does not yet support tail call optimization.
1917 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001918
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001919 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001920 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00001921 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001922 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001923 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001924
1925 // Analyze operands of the call, assigning locations to each operand.
1926 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001927 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1928 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001929
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00001930 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001931 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001932 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001933 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001934
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001935 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001936 unsigned NextStackOffset = CCInfo.getNextStackOffset();
1937
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001938 // Chain is the output chain of the last Load/Store or CopyToReg node.
1939 // ByValChain is the output chain of the last Memcpy node created for copying
1940 // byval arguments to the stack.
1941 SDValue Chain, CallSeqStart, ByValChain;
1942 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
1943 Chain = CallSeqStart = DAG.getCALLSEQ_START(InChain, NextStackOffsetVal);
1944 ByValChain = InChain;
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001945
1946 // If this is the first call, create a stack frame object that points to
1947 // a location to which .cprestore saves $gp.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00001948 if (IsO32 && IsPIC && !MipsFI->getGPFI())
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001949 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
1950
Akira Hatanaka21afc632011-06-21 00:40:49 +00001951 // Get the frame index of the stack frame object that points to the location
1952 // of dynamically allocated area on the stack.
1953 int DynAllocFI = MipsFI->getDynAllocFI();
1954
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001955 // Update size of the maximum argument space.
1956 // For O32, a minimum of four words (16 bytes) of argument space is
1957 // allocated.
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00001958 if (IsO32)
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001959 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
1960
1961 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1962
1963 if (MaxCallFrameSize < NextStackOffset) {
1964 MipsFI->setMaxCallFrameSize(NextStackOffset);
1965
Akira Hatanaka21afc632011-06-21 00:40:49 +00001966 // Set the offsets relative to $sp of the $gp restore slot and dynamically
1967 // allocated stack space. These offsets must be aligned to a boundary
1968 // determined by the stack alignment of the ABI.
1969 unsigned StackAlignment = TFL->getStackAlignment();
1970 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
1971 StackAlignment * StackAlignment;
1972
Akira Hatanakae42f33b2011-10-28 19:49:00 +00001973 if (MipsFI->needGPSaveRestore())
Akira Hatanaka21afc632011-06-21 00:40:49 +00001974 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
1975
1976 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001977 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001978
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001979 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001980 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1981 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001982
Eric Christopher471e4222011-06-08 23:55:35 +00001983 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00001984
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001985 // Walk the register/memloc assignments, inserting copies/loads.
1986 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001987 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001988 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00001989 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
1990
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001991 // Promote the value if needed.
1992 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001993 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001994 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00001995 if (VA.isRegLoc()) {
1996 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
1997 (ValVT == MVT::f64 && LocVT == MVT::i64))
1998 Arg = DAG.getNode(ISD::BITCAST, dl, LocVT, Arg);
1999 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002000 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2001 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002002 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2003 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002004 if (!Subtarget->isLittle())
2005 std::swap(Lo, Hi);
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002006 unsigned LocRegLo = VA.getLocReg();
2007 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2008 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2009 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002010 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002011 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002012 }
2013 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002014 case CCValAssign::SExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002015 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002016 break;
2017 case CCValAssign::ZExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002018 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002019 break;
2020 case CCValAssign::AExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002021 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002022 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002023 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002024
2025 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002026 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002027 if (VA.isRegLoc()) {
2028 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002029 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002030 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002031
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002032 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002033 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002034
Eric Christopher471e4222011-06-08 23:55:35 +00002035 // ByVal Arg.
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002036 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2037 if (Flags.isByVal()) {
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002038 assert(IsO32 &&
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002039 "No support for ByVal args by ABIs other than O32 yet.");
2040 assert(Flags.getByValSize() &&
2041 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002042 WriteByValArg(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI, MFI,
2043 DAG, Arg, VA, Flags, getPointerTy(), Subtarget->isLittle());
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002044 continue;
2045 }
2046
Chris Lattnere0b12152008-03-17 06:57:02 +00002047 // Create the frame index object for this incoming parameter
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002048 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002049 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00002050 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00002051
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002052 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002053 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00002054 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
2055 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00002056 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002057 }
2058
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002059 // Extend range of indices of frame objects for outgoing arguments that were
2060 // created during this function call. Skip this step if no such objects were
2061 // created.
2062 if (LastFI)
2063 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2064
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002065 // If a memcpy has been created to copy a byval arg to a stack, replace the
2066 // chain input of CallSeqStart with ByValChain.
2067 if (InChain != ByValChain)
2068 DAG.UpdateNodeOperands(CallSeqStart.getNode(), ByValChain,
2069 NextStackOffsetVal);
2070
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002071 // Transform all store nodes into one single node because all store
2072 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002073 if (!MemOpChains.empty())
2074 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002075 &MemOpChains[0], MemOpChains.size());
2076
Bill Wendling056292f2008-09-16 21:48:12 +00002077 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002078 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2079 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002080 unsigned char OpFlag;
2081 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002082 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002083 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002084
2085 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002086 if (IsPICCall && G->getGlobal()->hasInternalLinkage()) {
2087 OpFlag = IsO32 ? MipsII::MO_GOT : MipsII::MO_GOT_PAGE;
2088 unsigned char LoFlag = IsO32 ? MipsII::MO_ABS_LO : MipsII::MO_GOT_OFST;
2089 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(), 0,
2090 OpFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002091 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002092 0, LoFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002093 } else {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002094 OpFlag = IsPICCall ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002095 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2096 getPointerTy(), 0, OpFlag);
2097 }
2098
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002099 LoadSymAddr = true;
2100 }
2101 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002102 if (IsN64 || (!IsO32 && IsPIC))
2103 OpFlag = MipsII::MO_GOT_DISP;
2104 else if (!IsPIC) // !N64 && static
2105 OpFlag = MipsII::MO_NO_FLAG;
2106 else // O32 & PIC
2107 OpFlag = MipsII::MO_GOT_CALL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002108 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002109 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002110 LoadSymAddr = true;
2111 }
2112
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002113 SDValue InFlag;
2114
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002115 // Create nodes that load address of callee and copy it to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002116 if (IsPICCall) {
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002117 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002118 // Load callee address
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002119 Callee = DAG.getNode(MipsISD::WrapperPIC, dl, getPointerTy(), Callee);
2120 SDValue LoadValue = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
2121 Callee, MachinePointerInfo::getGOT(),
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002122 false, false, 0);
2123
2124 // Use GOT+LO if callee has internal linkage.
2125 if (CalleeLo.getNode()) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002126 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, getPointerTy(), CalleeLo);
2127 Callee = DAG.getNode(ISD::ADD, dl, getPointerTy(), LoadValue, Lo);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002128 } else
2129 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002130 }
2131
2132 // copy to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002133 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
2134 Chain = DAG.getCopyToReg(Chain, dl, T9Reg, Callee, SDValue(0, 0));
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002135 InFlag = Chain.getValue(1);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002136 Callee = DAG.getRegister(T9Reg, getPointerTy());
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002137 }
Bill Wendling056292f2008-09-16 21:48:12 +00002138
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002139 // Build a sequence of copy-to-reg nodes chained together with token
2140 // chain and flag operands which copy the outgoing args into registers.
2141 // The InFlag in necessary since all emitted instructions must be
2142 // stuck together.
2143 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2144 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2145 RegsToPass[i].second, InFlag);
2146 InFlag = Chain.getValue(1);
2147 }
2148
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002149 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002150 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002151 //
2152 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002153 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002154 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002155 Ops.push_back(Chain);
2156 Ops.push_back(Callee);
2157
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002158 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002159 // known live into the call.
2160 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2161 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2162 RegsToPass[i].second.getValueType()));
2163
Gabor Greifba36cb52008-08-28 21:40:38 +00002164 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002165 Ops.push_back(InFlag);
2166
Dale Johannesen33c960f2009-02-04 20:06:27 +00002167 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002168 InFlag = Chain.getValue(1);
2169
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002170 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002171 Chain = DAG.getCALLSEQ_END(Chain,
2172 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002173 DAG.getIntPtrConstant(0, true), InFlag);
2174 InFlag = Chain.getValue(1);
2175
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002176 // Handle result values, copying them out of physregs into vregs that we
2177 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002178 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2179 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002180}
2181
Dan Gohman98ca4f22009-08-05 01:29:28 +00002182/// LowerCallResult - Lower the result values of a call into the
2183/// appropriate copies out of appropriate physical registers.
2184SDValue
2185MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002186 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002187 const SmallVectorImpl<ISD::InputArg> &Ins,
2188 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002189 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002190 // Assign locations to each value returned by this call.
2191 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002192 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2193 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002194
Dan Gohman98ca4f22009-08-05 01:29:28 +00002195 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002196
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002197 // Copy all of the result registers out of their specified physreg.
2198 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002199 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002200 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002201 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002202 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002203 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002204
Dan Gohman98ca4f22009-08-05 01:29:28 +00002205 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002206}
2207
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002208//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002209// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002210//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002211static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2212 std::vector<SDValue>& OutChains,
2213 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2214 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2215 unsigned LocMem = VA.getLocMemOffset();
2216 unsigned FirstWord = LocMem / 4;
2217
2218 // copy register A0 - A3 to frame object
2219 for (unsigned i = 0; i < NumWords; ++i) {
2220 unsigned CurWord = FirstWord + i;
2221 if (CurWord >= O32IntRegsSize)
2222 break;
2223
2224 unsigned SrcReg = O32IntRegs[CurWord];
2225 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2226 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2227 DAG.getConstant(i * 4, MVT::i32));
2228 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2229 StorePtr, MachinePointerInfo(), false,
2230 false, 0);
2231 OutChains.push_back(Store);
2232 }
2233}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002234
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002235/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002236/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002237SDValue
2238MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002239 CallingConv::ID CallConv,
2240 bool isVarArg,
2241 const SmallVectorImpl<ISD::InputArg>
2242 &Ins,
2243 DebugLoc dl, SelectionDAG &DAG,
2244 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002245 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002246 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002247 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002248 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002249
Dan Gohman1e93df62010-04-17 14:41:14 +00002250 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002251
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002252 // Used with vargs to acumulate store chains.
2253 std::vector<SDValue> OutChains;
2254
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002255 // Assign locations to all of the incoming arguments.
2256 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002257 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2258 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002259
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002260 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002261 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002262 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002263 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002264
Akira Hatanaka43299772011-05-20 23:22:14 +00002265 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002266
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002267 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002268 CCValAssign &VA = ArgLocs[i];
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002269 EVT ValVT = VA.getValVT();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002270
2271 // Arguments stored on registers
2272 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002273 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002274 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002275 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002276
Owen Anderson825b72b2009-08-11 20:47:22 +00002277 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002278 RC = Mips::CPURegsRegisterClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002279 else if (RegVT == MVT::i64)
2280 RC = Mips::CPU64RegsRegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002281 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002282 RC = Mips::FGR32RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002283 else if (RegVT == MVT::f64)
Akira Hatanakaf40de9d2011-09-26 21:55:17 +00002284 RC = HasMips64 ? Mips::FGR64RegisterClass : Mips::AFGR64RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002285 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002286 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002287
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002288 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002289 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002290 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002291 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002292
2293 // If this is an 8 or 16-bit value, it has been passed promoted
2294 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002295 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002296 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002297 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002298 if (VA.getLocInfo() == CCValAssign::SExt)
2299 Opcode = ISD::AssertSext;
2300 else if (VA.getLocInfo() == CCValAssign::ZExt)
2301 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002302 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002303 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002304 DAG.getValueType(ValVT));
2305 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002306 }
2307
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002308 // Handle floating point arguments passed in integer registers.
2309 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
2310 (RegVT == MVT::i64 && ValVT == MVT::f64))
2311 ArgValue = DAG.getNode(ISD::BITCAST, dl, ValVT, ArgValue);
2312 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
2313 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
2314 getNextIntArgReg(ArgReg), RC);
2315 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
2316 if (!Subtarget->isLittle())
2317 std::swap(ArgValue, ArgValue2);
2318 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2319 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002320 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002321
Dan Gohman98ca4f22009-08-05 01:29:28 +00002322 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002323 } else { // VA.isRegLoc()
2324
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002325 // sanity check
2326 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002327
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002328 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2329
2330 if (Flags.isByVal()) {
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002331 assert(IsO32 &&
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002332 "No support for ByVal args by ABIs other than O32 yet.");
2333 assert(Flags.getByValSize() &&
2334 "ByVal args of size 0 should have been ignored by front-end.");
2335 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2336 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2337 true);
2338 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2339 InVals.push_back(FIN);
2340 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2341
2342 continue;
2343 }
2344
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002345 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002346 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002347 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002348
2349 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002350 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002351 InVals.push_back(DAG.getLoad(ValVT, dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002352 MachinePointerInfo::getFixedStack(LastFI),
David Greenef6fa1862010-02-15 16:56:10 +00002353 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002354 }
2355 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002356
2357 // The mips ABIs for returning structs by value requires that we copy
2358 // the sret argument into $v0 for the return. Save the argument into
2359 // a virtual register so that we can access it from the return points.
2360 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2361 unsigned Reg = MipsFI->getSRetReturnReg();
2362 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002363 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002364 MipsFI->setSRetReturnReg(Reg);
2365 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002366 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002367 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002368 }
2369
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002370 if (isVarArg && IsO32) {
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002371 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002372 // which is a value necessary to VASTART.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002373 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002374 assert(NextStackOffset % 4 == 0 &&
2375 "NextStackOffset must be aligned to 4-byte boundaries.");
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002376 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
2377 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002378
2379 // If NextStackOffset is smaller than o32's 16-byte reserved argument area,
2380 // copy the integer registers that have not been used for argument passing
2381 // to the caller's stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002382 for (; NextStackOffset < 16; NextStackOffset += 4) {
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002383 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002384 unsigned Idx = NextStackOffset / 4;
2385 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), O32IntRegs[Idx], RC);
2386 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
Akira Hatanaka69c19f72011-05-23 20:16:59 +00002387 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002388 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2389 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2390 MachinePointerInfo(),
2391 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002392 }
2393 }
2394
Akira Hatanaka43299772011-05-20 23:22:14 +00002395 MipsFI->setLastInArgFI(LastFI);
2396
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002397 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002398 // the size of Ins and InVals. This only happens when on varg functions
2399 if (!OutChains.empty()) {
2400 OutChains.push_back(Chain);
2401 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2402 &OutChains[0], OutChains.size());
2403 }
2404
Dan Gohman98ca4f22009-08-05 01:29:28 +00002405 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002406}
2407
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002408//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002409// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002410//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002411
Dan Gohman98ca4f22009-08-05 01:29:28 +00002412SDValue
2413MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002414 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002415 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002416 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002417 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002418
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002419 // CCValAssign - represent the assignment of
2420 // the return value to a location
2421 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002422
2423 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002424 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2425 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002426
Dan Gohman98ca4f22009-08-05 01:29:28 +00002427 // Analize return values.
2428 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002429
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002430 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002431 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002432 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002433 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002434 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002435 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002436 }
2437
Dan Gohman475871a2008-07-27 21:46:04 +00002438 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002439
2440 // Copy the result values into the output registers.
2441 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2442 CCValAssign &VA = RVLocs[i];
2443 assert(VA.isRegLoc() && "Can only return in registers!");
2444
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002445 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002446 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002447
2448 // guarantee that all emitted copies are
2449 // stuck together, avoiding something bad
2450 Flag = Chain.getValue(1);
2451 }
2452
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002453 // The mips ABIs for returning structs by value requires that we copy
2454 // the sret argument into $v0 for the return. We saved the argument into
2455 // a virtual register in the entry block, so now we copy the value out
2456 // and into $v0.
2457 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2458 MachineFunction &MF = DAG.getMachineFunction();
2459 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2460 unsigned Reg = MipsFI->getSRetReturnReg();
2461
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002462 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002463 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002464 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002465
Dale Johannesena05dca42009-02-04 23:02:30 +00002466 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002467 Flag = Chain.getValue(1);
2468 }
2469
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002470 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002471 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002472 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002473 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002474 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002475 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002476 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002477}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002478
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002479//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002480// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002481//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002482
2483/// getConstraintType - Given a constraint letter, return the type of
2484/// constraint it is for this target.
2485MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002486getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002487{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002488 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002489 // GCC config/mips/constraints.md
2490 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002491 // 'd' : An address register. Equivalent to r
2492 // unless generating MIPS16 code.
2493 // 'y' : Equivalent to r; retained for
2494 // backwards compatibility.
2495 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002496 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002497 switch (Constraint[0]) {
2498 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002499 case 'd':
2500 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002501 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002502 return C_RegisterClass;
2503 break;
2504 }
2505 }
2506 return TargetLowering::getConstraintType(Constraint);
2507}
2508
John Thompson44ab89e2010-10-29 17:29:13 +00002509/// Examine constraint type and operand type and determine a weight value.
2510/// This object must already have been set up with the operand type
2511/// and the current alternative constraint selected.
2512TargetLowering::ConstraintWeight
2513MipsTargetLowering::getSingleConstraintMatchWeight(
2514 AsmOperandInfo &info, const char *constraint) const {
2515 ConstraintWeight weight = CW_Invalid;
2516 Value *CallOperandVal = info.CallOperandVal;
2517 // If we don't have a value, we can't do a match,
2518 // but allow it at the lowest weight.
2519 if (CallOperandVal == NULL)
2520 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002521 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002522 // Look at the constraint type.
2523 switch (*constraint) {
2524 default:
2525 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2526 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002527 case 'd':
2528 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002529 if (type->isIntegerTy())
2530 weight = CW_Register;
2531 break;
2532 case 'f':
2533 if (type->isFloatTy())
2534 weight = CW_Register;
2535 break;
2536 }
2537 return weight;
2538}
2539
Eric Christopher38d64262011-06-29 19:33:04 +00002540/// Given a register class constraint, like 'r', if this corresponds directly
2541/// to an LLVM register class, return a register of 0 and the register class
2542/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002543std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002544getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002545{
2546 if (Constraint.size() == 1) {
2547 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002548 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2549 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002550 case 'r':
2551 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002552 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002553 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002554 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002555 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002556 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2557 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002558 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002559 }
2560 }
2561 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2562}
2563
Dan Gohman6520e202008-10-18 02:06:02 +00002564bool
2565MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2566 // The Mips target isn't yet aware of offsets.
2567 return false;
2568}
Evan Chengeb2f9692009-10-27 19:56:55 +00002569
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002570bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2571 if (VT != MVT::f32 && VT != MVT::f64)
2572 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002573 if (Imm.isNegZero())
2574 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002575 return Imm.isZero();
2576}