blob: 0c0b9abdd3f78328f25ca5bbab959ea5ab9f39c3 [file] [log] [blame]
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000014
15#define DEBUG_TYPE "mips-lower"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000016#include "MipsISelLowering.h"
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +000017#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000018#include "MipsTargetMachine.h"
Chris Lattnerb71b9092009-08-13 06:28:06 +000019#include "MipsTargetObjectFile.h"
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000020#include "MipsSubtarget.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000021#include "llvm/DerivedTypes.h"
22#include "llvm/Function.h"
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +000023#include "llvm/GlobalVariable.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000024#include "llvm/Intrinsics.h"
25#include "llvm/CallingConv.h"
Akira Hatanaka794bf172011-07-07 23:56:50 +000026#include "InstPrinter/MipsInstPrinter.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000027#include "llvm/CodeGen/CallingConvLower.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000032#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033#include "llvm/CodeGen/ValueTypes.h"
34#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000036using namespace llvm;
37
Akira Hatanakadbe9a312011-08-18 20:07:42 +000038// If I is a shifted mask, set the size (Size) and the first bit of the
39// mask (Pos), and return true.
Akira Hatanaka854a7db2011-08-19 22:59:00 +000040// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
41static bool IsShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
42 if (!isUInt<32>(I) || !isShiftedMask_32(I))
43 return false;
Akira Hatanakabb15e112011-08-17 02:05:42 +000044
Akira Hatanaka854a7db2011-08-19 22:59:00 +000045 Size = CountPopulation_32(I);
46 Pos = CountTrailingZeros_32(I);
Akira Hatanakadbe9a312011-08-18 20:07:42 +000047 return true;
Akira Hatanakabb15e112011-08-17 02:05:42 +000048}
49
Chris Lattnerf0144122009-07-28 03:13:23 +000050const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
51 switch (Opcode) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000052 case MipsISD::JmpLink: return "MipsISD::JmpLink";
53 case MipsISD::Hi: return "MipsISD::Hi";
54 case MipsISD::Lo: return "MipsISD::Lo";
55 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +000056 case MipsISD::TlsGd: return "MipsISD::TlsGd";
57 case MipsISD::TprelHi: return "MipsISD::TprelHi";
58 case MipsISD::TprelLo: return "MipsISD::TprelLo";
59 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000060 case MipsISD::Ret: return "MipsISD::Ret";
61 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
62 case MipsISD::FPCmp: return "MipsISD::FPCmp";
63 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
64 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
65 case MipsISD::FPRound: return "MipsISD::FPRound";
66 case MipsISD::MAdd: return "MipsISD::MAdd";
67 case MipsISD::MAddu: return "MipsISD::MAddu";
68 case MipsISD::MSub: return "MipsISD::MSub";
69 case MipsISD::MSubu: return "MipsISD::MSubu";
70 case MipsISD::DivRem: return "MipsISD::DivRem";
71 case MipsISD::DivRemU: return "MipsISD::DivRemU";
72 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
73 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanaka342837d2011-05-28 01:07:07 +000074 case MipsISD::WrapperPIC: return "MipsISD::WrapperPIC";
Akira Hatanaka21afc632011-06-21 00:40:49 +000075 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
Akira Hatanakadb548262011-07-19 23:30:50 +000076 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanakabb15e112011-08-17 02:05:42 +000077 case MipsISD::Ext: return "MipsISD::Ext";
78 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanaka0f843822011-06-07 18:58:42 +000079 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000080 }
81}
82
83MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000084MipsTargetLowering(MipsTargetMachine &TM)
Chris Lattnerb71b9092009-08-13 06:28:06 +000085 : TargetLowering(TM, new MipsTargetObjectFile()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000086 Subtarget = &TM.getSubtarget<MipsSubtarget>();
Akira Hatanaka792016b2011-09-23 18:28:39 +000087 bool HasMips64 = Subtarget->hasMips64();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000088
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000089 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000090 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000091 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +000092 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000093
94 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000095 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
96 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000097
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000098 // When dealing with single precision only, use libcalls
Akira Hatanaka792016b2011-09-23 18:28:39 +000099 if (!Subtarget->isSingleFloat()) {
100 if (HasMips64)
101 addRegisterClass(MVT::f64, Mips::FGR64RegisterClass);
102 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000103 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Akira Hatanaka792016b2011-09-23 18:28:39 +0000104 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000105
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000106 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000107 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
108 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
109 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000110
Eli Friedman6055a6a2009-07-17 04:07:24 +0000111 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000112 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
113 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000114
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000115 // Used by legalize types to correctly generate the setcc result.
116 // Without this, every float setcc comes with a AND/OR with the result,
117 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000118 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000119 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000120
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000121 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +0000122 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000123 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000124 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
125 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
126 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
127 setOperationAction(ISD::SELECT, MVT::f32, Custom);
128 setOperationAction(ISD::SELECT, MVT::f64, Custom);
129 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
131 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000132 setOperationAction(ISD::VASTART, MVT::Other, Custom);
133
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000134 setOperationAction(ISD::SDIV, MVT::i32, Expand);
135 setOperationAction(ISD::SREM, MVT::i32, Expand);
136 setOperationAction(ISD::UDIV, MVT::i32, Expand);
137 setOperationAction(ISD::UREM, MVT::i32, Expand);
138
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000139 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
141 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
142 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
143 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
144 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
145 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
146 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
147 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
148 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000149
Akira Hatanaka56633442011-09-20 23:53:09 +0000150 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000151 setOperationAction(ISD::ROTR, MVT::i32, Expand);
152
Owen Anderson825b72b2009-08-11 20:47:22 +0000153 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
154 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
155 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000156 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
157 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000159 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000161 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000162 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
163 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000164 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 setOperationAction(ISD::FLOG, MVT::f32, Expand);
166 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
167 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
168 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000169 setOperationAction(ISD::FMA, MVT::f32, Expand);
170 setOperationAction(ISD::FMA, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000171
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000172 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
173 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Eric Christopher471e4222011-06-08 23:55:35 +0000174
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000175 setOperationAction(ISD::VAARG, MVT::Other, Expand);
176 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
177 setOperationAction(ISD::VAEND, MVT::Other, Expand);
178
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000179 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
181 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000182
Akira Hatanakadb548262011-07-19 23:30:50 +0000183 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000184 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000185
Eli Friedman4db5aca2011-08-29 18:23:02 +0000186 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
187 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
188
Eli Friedman26689ac2011-08-03 21:06:02 +0000189 setInsertFencesForAtomic(true);
190
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000191 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000193
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000194 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000195 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
196 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000197 }
198
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000199 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000200 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000201
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000202 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000204
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000205 setTargetDAGCombine(ISD::ADDE);
206 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000207 setTargetDAGCombine(ISD::SDIVREM);
208 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000209 setTargetDAGCombine(ISD::SETCC);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000210 setTargetDAGCombine(ISD::AND);
211 setTargetDAGCombine(ISD::OR);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000212
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000213 setMinFunctionAlignment(2);
214
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000215 setStackPointerRegisterToSaveRestore(Mips::SP);
216 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000217
218 setExceptionPointerRegister(Mips::A0);
219 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000220}
221
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000222bool MipsTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Akira Hatanaka511961a2011-08-17 18:49:18 +0000223 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
224 return SVT == MVT::i32 || SVT == MVT::i16;
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000225}
226
Duncan Sands28b77e92011-09-06 19:07:46 +0000227EVT MipsTargetLowering::getSetCCResultType(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000228 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000229}
230
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000231// SelectMadd -
232// Transforms a subgraph in CurDAG if the following pattern is found:
233// (addc multLo, Lo0), (adde multHi, Hi0),
234// where,
235// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000236// Lo0: initial value of Lo register
237// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000238// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000239static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000240 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000241 // for the matching to be successful.
242 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
243
244 if (ADDCNode->getOpcode() != ISD::ADDC)
245 return false;
246
247 SDValue MultHi = ADDENode->getOperand(0);
248 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000249 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000250 unsigned MultOpc = MultHi.getOpcode();
251
252 // MultHi and MultLo must be generated by the same node,
253 if (MultLo.getNode() != MultNode)
254 return false;
255
256 // and it must be a multiplication.
257 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
258 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000259
260 // MultLo amd MultHi must be the first and second output of MultNode
261 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000262 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
263 return false;
264
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000265 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000266 // of the values of MultNode, in which case MultNode will be removed in later
267 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000268 // If there exist users other than ADDENode or ADDCNode, this function returns
269 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000270 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000271 // produced.
272 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
273 return false;
274
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000275 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000276 DebugLoc dl = ADDENode->getDebugLoc();
277
278 // create MipsMAdd(u) node
279 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000280
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000281 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
282 MVT::Glue,
283 MultNode->getOperand(0),// Factor 0
284 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000285 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000286 ADDENode->getOperand(1));// Hi0
287
288 // create CopyFromReg nodes
289 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
290 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000291 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000292 Mips::HI, MVT::i32,
293 CopyFromLo.getValue(2));
294
295 // replace uses of adde and addc here
296 if (!SDValue(ADDCNode, 0).use_empty())
297 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
298
299 if (!SDValue(ADDENode, 0).use_empty())
300 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
301
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000302 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000303}
304
305// SelectMsub -
306// Transforms a subgraph in CurDAG if the following pattern is found:
307// (addc Lo0, multLo), (sube Hi0, multHi),
308// where,
309// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000310// Lo0: initial value of Lo register
311// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000312// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000313static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000314 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000315 // for the matching to be successful.
316 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
317
318 if (SUBCNode->getOpcode() != ISD::SUBC)
319 return false;
320
321 SDValue MultHi = SUBENode->getOperand(1);
322 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000323 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000324 unsigned MultOpc = MultHi.getOpcode();
325
326 // MultHi and MultLo must be generated by the same node,
327 if (MultLo.getNode() != MultNode)
328 return false;
329
330 // and it must be a multiplication.
331 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
332 return false;
333
334 // MultLo amd MultHi must be the first and second output of MultNode
335 // respectively.
336 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
337 return false;
338
339 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
340 // of the values of MultNode, in which case MultNode will be removed in later
341 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000342 // If there exist users other than SUBENode or SUBCNode, this function returns
343 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000344 // instruction node rather than a pair of MULT and MSUB instructions being
345 // produced.
346 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
347 return false;
348
349 SDValue Chain = CurDAG->getEntryNode();
350 DebugLoc dl = SUBENode->getDebugLoc();
351
352 // create MipsSub(u) node
353 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
354
355 SDValue MSub = CurDAG->getNode(MultOpc, dl,
356 MVT::Glue,
357 MultNode->getOperand(0),// Factor 0
358 MultNode->getOperand(1),// Factor 1
359 SUBCNode->getOperand(0),// Lo0
360 SUBENode->getOperand(0));// Hi0
361
362 // create CopyFromReg nodes
363 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
364 MSub);
365 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
366 Mips::HI, MVT::i32,
367 CopyFromLo.getValue(2));
368
369 // replace uses of sube and subc here
370 if (!SDValue(SUBCNode, 0).use_empty())
371 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
372
373 if (!SDValue(SUBENode, 0).use_empty())
374 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
375
376 return true;
377}
378
379static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
380 TargetLowering::DAGCombinerInfo &DCI,
381 const MipsSubtarget* Subtarget) {
382 if (DCI.isBeforeLegalize())
383 return SDValue();
384
Akira Hatanaka56633442011-09-20 23:53:09 +0000385 if (Subtarget->hasMips32() && SelectMadd(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000386 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000387
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000388 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000389}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000390
391static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
392 TargetLowering::DAGCombinerInfo &DCI,
393 const MipsSubtarget* Subtarget) {
394 if (DCI.isBeforeLegalize())
395 return SDValue();
396
Akira Hatanaka56633442011-09-20 23:53:09 +0000397 if (Subtarget->hasMips32() && SelectMsub(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000398 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000399
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000400 return SDValue();
401}
402
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000403static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
404 TargetLowering::DAGCombinerInfo &DCI,
405 const MipsSubtarget* Subtarget) {
406 if (DCI.isBeforeLegalizeOps())
407 return SDValue();
408
409 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
410 MipsISD::DivRemU;
411 DebugLoc dl = N->getDebugLoc();
412
413 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
414 N->getOperand(0), N->getOperand(1));
415 SDValue InChain = DAG.getEntryNode();
416 SDValue InGlue = DivRem;
417
418 // insert MFLO
419 if (N->hasAnyUseOfValue(0)) {
420 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, Mips::LO, MVT::i32,
421 InGlue);
422 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
423 InChain = CopyFromLo.getValue(1);
424 InGlue = CopyFromLo.getValue(2);
425 }
426
427 // insert MFHI
428 if (N->hasAnyUseOfValue(1)) {
429 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000430 Mips::HI, MVT::i32, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000431 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
432 }
433
434 return SDValue();
435}
436
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000437static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
438 switch (CC) {
439 default: llvm_unreachable("Unknown fp condition code!");
440 case ISD::SETEQ:
441 case ISD::SETOEQ: return Mips::FCOND_OEQ;
442 case ISD::SETUNE: return Mips::FCOND_UNE;
443 case ISD::SETLT:
444 case ISD::SETOLT: return Mips::FCOND_OLT;
445 case ISD::SETGT:
446 case ISD::SETOGT: return Mips::FCOND_OGT;
447 case ISD::SETLE:
448 case ISD::SETOLE: return Mips::FCOND_OLE;
449 case ISD::SETGE:
450 case ISD::SETOGE: return Mips::FCOND_OGE;
451 case ISD::SETULT: return Mips::FCOND_ULT;
452 case ISD::SETULE: return Mips::FCOND_ULE;
453 case ISD::SETUGT: return Mips::FCOND_UGT;
454 case ISD::SETUGE: return Mips::FCOND_UGE;
455 case ISD::SETUO: return Mips::FCOND_UN;
456 case ISD::SETO: return Mips::FCOND_OR;
457 case ISD::SETNE:
458 case ISD::SETONE: return Mips::FCOND_ONE;
459 case ISD::SETUEQ: return Mips::FCOND_UEQ;
460 }
461}
462
463
464// Returns true if condition code has to be inverted.
465static bool InvertFPCondCode(Mips::CondCode CC) {
466 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
467 return false;
468
469 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
470 return true;
471
472 assert(false && "Illegal Condition Code");
473 return false;
474}
475
476// Creates and returns an FPCmp node from a setcc node.
477// Returns Op if setcc is not a floating point comparison.
478static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
479 // must be a SETCC node
480 if (Op.getOpcode() != ISD::SETCC)
481 return Op;
482
483 SDValue LHS = Op.getOperand(0);
484
485 if (!LHS.getValueType().isFloatingPoint())
486 return Op;
487
488 SDValue RHS = Op.getOperand(1);
489 DebugLoc dl = Op.getDebugLoc();
490
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000491 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
492 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000493 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
494
495 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
496 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
497}
498
499// Creates and returns a CMovFPT/F node.
500static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
501 SDValue False, DebugLoc DL) {
502 bool invert = InvertFPCondCode((Mips::CondCode)
503 cast<ConstantSDNode>(Cond.getOperand(2))
504 ->getSExtValue());
505
506 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
507 True.getValueType(), True, False, Cond);
508}
509
510static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
511 TargetLowering::DAGCombinerInfo &DCI,
512 const MipsSubtarget* Subtarget) {
513 if (DCI.isBeforeLegalizeOps())
514 return SDValue();
515
516 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
517
518 if (Cond.getOpcode() != MipsISD::FPCmp)
519 return SDValue();
520
521 SDValue True = DAG.getConstant(1, MVT::i32);
522 SDValue False = DAG.getConstant(0, MVT::i32);
523
524 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
525}
526
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000527static SDValue PerformANDCombine(SDNode *N, SelectionDAG& DAG,
528 TargetLowering::DAGCombinerInfo &DCI,
529 const MipsSubtarget* Subtarget) {
530 // Pattern match EXT.
531 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
532 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000533 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000534 return SDValue();
535
536 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
537
538 // Op's first operand must be a shift right.
539 if (ShiftRight.getOpcode() != ISD::SRA && ShiftRight.getOpcode() != ISD::SRL)
540 return SDValue();
541
542 // The second operand of the shift must be an immediate.
543 uint64_t Pos;
544 ConstantSDNode *CN;
545 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
546 return SDValue();
547
548 Pos = CN->getZExtValue();
549
550 uint64_t SMPos, SMSize;
551 // Op's second operand must be a shifted mask.
552 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000553 !IsShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000554 return SDValue();
555
556 // Return if the shifted mask does not start at bit 0 or the sum of its size
557 // and Pos exceeds the word's size.
558 if (SMPos != 0 || Pos + SMSize > 32)
559 return SDValue();
560
561 return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), MVT::i32,
562 ShiftRight.getOperand(0),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000563 DAG.getConstant(Pos, MVT::i32),
564 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000565}
566
567static SDValue PerformORCombine(SDNode *N, SelectionDAG& DAG,
568 TargetLowering::DAGCombinerInfo &DCI,
569 const MipsSubtarget* Subtarget) {
570 // Pattern match INS.
571 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
572 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
573 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000574 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000575 return SDValue();
576
577 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
578 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
579 ConstantSDNode *CN;
580
581 // See if Op's first operand matches (and $src1 , mask0).
582 if (And0.getOpcode() != ISD::AND)
583 return SDValue();
584
585 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000586 !IsShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000587 return SDValue();
588
589 // See if Op's second operand matches (and (shl $src, pos), mask1).
590 if (And1.getOpcode() != ISD::AND)
591 return SDValue();
592
593 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000594 !IsShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000595 return SDValue();
596
597 // The shift masks must have the same position and size.
598 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
599 return SDValue();
600
601 SDValue Shl = And1.getOperand(0);
602 if (Shl.getOpcode() != ISD::SHL)
603 return SDValue();
604
605 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
606 return SDValue();
607
608 unsigned Shamt = CN->getZExtValue();
609
610 // Return if the shift amount and the first bit position of mask are not the
611 // same.
612 if (Shamt != SMPos0)
613 return SDValue();
614
615 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), MVT::i32,
616 Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000617 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000618 DAG.getConstant(SMSize0, MVT::i32),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000619 And0.getOperand(0));
620}
621
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000622SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000623 const {
624 SelectionDAG &DAG = DCI.DAG;
625 unsigned opc = N->getOpcode();
626
627 switch (opc) {
628 default: break;
629 case ISD::ADDE:
630 return PerformADDECombine(N, DAG, DCI, Subtarget);
631 case ISD::SUBE:
632 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000633 case ISD::SDIVREM:
634 case ISD::UDIVREM:
635 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000636 case ISD::SETCC:
637 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000638 case ISD::AND:
639 return PerformANDCombine(N, DAG, DCI, Subtarget);
640 case ISD::OR:
641 return PerformORCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000642 }
643
644 return SDValue();
645}
646
Dan Gohman475871a2008-07-27 21:46:04 +0000647SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000648LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000649{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000650 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000651 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000652 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000653 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
654 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000655 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000656 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000657 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
658 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000659 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000660 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000661 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000662 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Akira Hatanakadb548262011-07-19 23:30:50 +0000663 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +0000664 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000665 }
Dan Gohman475871a2008-07-27 21:46:04 +0000666 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000667}
668
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000669//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000670// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000671//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000672
673// AddLiveIn - This helper function adds the specified physical register to the
674// MachineFunction as a live in value. It also creates a corresponding
675// virtual register for it.
676static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000677AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000678{
679 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000680 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
681 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000682 return VReg;
683}
684
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000685// Get fp branch code (not opcode) from condition code.
686static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
687 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
688 return Mips::BRANCH_T;
689
690 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
691 return Mips::BRANCH_F;
692
693 return Mips::BRANCH_INVALID;
694}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000695
Akira Hatanaka14487d42011-06-07 19:28:39 +0000696static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
697 DebugLoc dl,
698 const MipsSubtarget* Subtarget,
699 const TargetInstrInfo *TII,
700 bool isFPCmp, unsigned Opc) {
701 // There is no need to expand CMov instructions if target has
702 // conditional moves.
703 if (Subtarget->hasCondMov())
704 return BB;
705
706 // To "insert" a SELECT_CC instruction, we actually have to insert the
707 // diamond control-flow pattern. The incoming instruction knows the
708 // destination vreg to set, the condition code register to branch on, the
709 // true/false values to select between, and a branch opcode to use.
710 const BasicBlock *LLVM_BB = BB->getBasicBlock();
711 MachineFunction::iterator It = BB;
712 ++It;
713
714 // thisMBB:
715 // ...
716 // TrueVal = ...
717 // setcc r1, r2, r3
718 // bNE r1, r0, copy1MBB
719 // fallthrough --> copy0MBB
720 MachineBasicBlock *thisMBB = BB;
721 MachineFunction *F = BB->getParent();
722 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
723 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
724 F->insert(It, copy0MBB);
725 F->insert(It, sinkMBB);
726
727 // Transfer the remainder of BB and its successor edges to sinkMBB.
728 sinkMBB->splice(sinkMBB->begin(), BB,
729 llvm::next(MachineBasicBlock::iterator(MI)),
730 BB->end());
731 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
732
733 // Next, add the true and fallthrough blocks as its successors.
734 BB->addSuccessor(copy0MBB);
735 BB->addSuccessor(sinkMBB);
736
737 // Emit the right instruction according to the type of the operands compared
738 if (isFPCmp)
739 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
740 else
741 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
742 .addReg(Mips::ZERO).addMBB(sinkMBB);
743
744 // copy0MBB:
745 // %FalseValue = ...
746 // # fallthrough to sinkMBB
747 BB = copy0MBB;
748
749 // Update machine-CFG edges
750 BB->addSuccessor(sinkMBB);
751
752 // sinkMBB:
753 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
754 // ...
755 BB = sinkMBB;
756
757 if (isFPCmp)
758 BuildMI(*BB, BB->begin(), dl,
759 TII->get(Mips::PHI), MI->getOperand(0).getReg())
760 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
761 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
762 else
763 BuildMI(*BB, BB->begin(), dl,
764 TII->get(Mips::PHI), MI->getOperand(0).getReg())
765 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
766 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
767
768 MI->eraseFromParent(); // The pseudo instruction is gone now.
769 return BB;
770}
771
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000772MachineBasicBlock *
773MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000774 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000775 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen94817572009-02-13 02:34:39 +0000776 DebugLoc dl = MI->getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000777
778 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000779 default:
780 assert(false && "Unexpected instr type to insert");
781 return NULL;
782 case Mips::MOVT:
783 case Mips::MOVT_S:
784 case Mips::MOVT_D:
785 return ExpandCondMov(MI, BB, dl, Subtarget, TII, true, Mips::BC1F);
786 case Mips::MOVF:
787 case Mips::MOVF_S:
788 case Mips::MOVF_D:
789 return ExpandCondMov(MI, BB, dl, Subtarget, TII, true, Mips::BC1T);
790 case Mips::MOVZ_I:
791 case Mips::MOVZ_S:
792 case Mips::MOVZ_D:
793 return ExpandCondMov(MI, BB, dl, Subtarget, TII, false, Mips::BNE);
794 case Mips::MOVN_I:
795 case Mips::MOVN_S:
796 case Mips::MOVN_D:
797 return ExpandCondMov(MI, BB, dl, Subtarget, TII, false, Mips::BEQ);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000798
799 case Mips::ATOMIC_LOAD_ADD_I8:
800 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
801 case Mips::ATOMIC_LOAD_ADD_I16:
802 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
803 case Mips::ATOMIC_LOAD_ADD_I32:
804 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
805
806 case Mips::ATOMIC_LOAD_AND_I8:
807 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
808 case Mips::ATOMIC_LOAD_AND_I16:
809 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
810 case Mips::ATOMIC_LOAD_AND_I32:
811 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
812
813 case Mips::ATOMIC_LOAD_OR_I8:
814 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
815 case Mips::ATOMIC_LOAD_OR_I16:
816 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
817 case Mips::ATOMIC_LOAD_OR_I32:
818 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
819
820 case Mips::ATOMIC_LOAD_XOR_I8:
821 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
822 case Mips::ATOMIC_LOAD_XOR_I16:
823 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
824 case Mips::ATOMIC_LOAD_XOR_I32:
825 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
826
827 case Mips::ATOMIC_LOAD_NAND_I8:
828 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
829 case Mips::ATOMIC_LOAD_NAND_I16:
830 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
831 case Mips::ATOMIC_LOAD_NAND_I32:
832 return EmitAtomicBinary(MI, BB, 4, 0, true);
833
834 case Mips::ATOMIC_LOAD_SUB_I8:
835 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
836 case Mips::ATOMIC_LOAD_SUB_I16:
837 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
838 case Mips::ATOMIC_LOAD_SUB_I32:
839 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
840
841 case Mips::ATOMIC_SWAP_I8:
842 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
843 case Mips::ATOMIC_SWAP_I16:
844 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
845 case Mips::ATOMIC_SWAP_I32:
846 return EmitAtomicBinary(MI, BB, 4, 0);
847
848 case Mips::ATOMIC_CMP_SWAP_I8:
849 return EmitAtomicCmpSwapPartword(MI, BB, 1);
850 case Mips::ATOMIC_CMP_SWAP_I16:
851 return EmitAtomicCmpSwapPartword(MI, BB, 2);
852 case Mips::ATOMIC_CMP_SWAP_I32:
853 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000854 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000855}
856
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000857// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
858// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
859MachineBasicBlock *
860MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000861 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000862 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000863 assert(Size == 4 && "Unsupported size for EmitAtomicBinary.");
864
865 MachineFunction *MF = BB->getParent();
866 MachineRegisterInfo &RegInfo = MF->getRegInfo();
867 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
868 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
869 DebugLoc dl = MI->getDebugLoc();
870
Akira Hatanaka4061da12011-07-19 20:11:17 +0000871 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000872 unsigned Ptr = MI->getOperand(1).getReg();
873 unsigned Incr = MI->getOperand(2).getReg();
874
Akira Hatanaka4061da12011-07-19 20:11:17 +0000875 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
876 unsigned AndRes = RegInfo.createVirtualRegister(RC);
877 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000878
879 // insert new blocks after the current block
880 const BasicBlock *LLVM_BB = BB->getBasicBlock();
881 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
882 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
883 MachineFunction::iterator It = BB;
884 ++It;
885 MF->insert(It, loopMBB);
886 MF->insert(It, exitMBB);
887
888 // Transfer the remainder of BB and its successor edges to exitMBB.
889 exitMBB->splice(exitMBB->begin(), BB,
890 llvm::next(MachineBasicBlock::iterator(MI)),
891 BB->end());
892 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
893
894 // thisMBB:
895 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000896 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000897 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000898 loopMBB->addSuccessor(loopMBB);
899 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000900
901 // loopMBB:
902 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000903 // <binop> storeval, oldval, incr
904 // sc success, storeval, 0(ptr)
905 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000906 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000907 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000908 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000909 // and andres, oldval, incr
910 // nor storeval, $0, andres
911 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr);
912 BuildMI(BB, dl, TII->get(Mips::NOR), StoreVal)
913 .addReg(Mips::ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000914 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000915 // <binop> storeval, oldval, incr
916 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000917 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000918 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000919 }
Akira Hatanaka4061da12011-07-19 20:11:17 +0000920 BuildMI(BB, dl, TII->get(Mips::SC), Success)
921 .addReg(StoreVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000922 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +0000923 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000924
925 MI->eraseFromParent(); // The instruction is gone now.
926
Akira Hatanaka939ece12011-07-19 03:42:13 +0000927 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000928}
929
930MachineBasicBlock *
931MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000932 MachineBasicBlock *BB,
933 unsigned Size, unsigned BinOpcode,
934 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000935 assert((Size == 1 || Size == 2) &&
936 "Unsupported size for EmitAtomicBinaryPartial.");
937
938 MachineFunction *MF = BB->getParent();
939 MachineRegisterInfo &RegInfo = MF->getRegInfo();
940 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
941 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
942 DebugLoc dl = MI->getDebugLoc();
943
944 unsigned Dest = MI->getOperand(0).getReg();
945 unsigned Ptr = MI->getOperand(1).getReg();
946 unsigned Incr = MI->getOperand(2).getReg();
947
Akira Hatanaka4061da12011-07-19 20:11:17 +0000948 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
949 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000950 unsigned Mask = RegInfo.createVirtualRegister(RC);
951 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000952 unsigned NewVal = RegInfo.createVirtualRegister(RC);
953 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000954 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000955 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
956 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
957 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
958 unsigned AndRes = RegInfo.createVirtualRegister(RC);
959 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +0000960 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000961 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
962 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
963 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
964 unsigned SllRes = RegInfo.createVirtualRegister(RC);
965 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000966
967 // insert new blocks after the current block
968 const BasicBlock *LLVM_BB = BB->getBasicBlock();
969 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000970 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000971 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
972 MachineFunction::iterator It = BB;
973 ++It;
974 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000975 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000976 MF->insert(It, exitMBB);
977
978 // Transfer the remainder of BB and its successor edges to exitMBB.
979 exitMBB->splice(exitMBB->begin(), BB,
980 llvm::next(MachineBasicBlock::iterator(MI)),
981 BB->end());
982 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
983
Akira Hatanaka81b44112011-07-19 17:09:53 +0000984 BB->addSuccessor(loopMBB);
985 loopMBB->addSuccessor(loopMBB);
986 loopMBB->addSuccessor(sinkMBB);
987 sinkMBB->addSuccessor(exitMBB);
988
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000989 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +0000990 // addiu masklsb2,$0,-4 # 0xfffffffc
991 // and alignedaddr,ptr,masklsb2
992 // andi ptrlsb2,ptr,3
993 // sll shiftamt,ptrlsb2,3
994 // ori maskupper,$0,255 # 0xff
995 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000996 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +0000997 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000998
999 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001000 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1001 .addReg(Mips::ZERO).addImm(-4);
1002 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1003 .addReg(Ptr).addReg(MaskLSB2);
1004 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1005 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1006 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1007 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001008 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1009 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001010 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001011 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001012
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001013
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001014 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001015 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001016 // ll oldval,0(alignedaddr)
1017 // binop binopres,oldval,incr2
1018 // and newval,binopres,mask
1019 // and maskedoldval0,oldval,mask2
1020 // or storeval,maskedoldval0,newval
1021 // sc success,storeval,0(alignedaddr)
1022 // beq success,$0,loopMBB
1023
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001024 // atomic.swap
1025 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001026 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001027 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001028 // and maskedoldval0,oldval,mask2
1029 // or storeval,maskedoldval0,newval
1030 // sc success,storeval,0(alignedaddr)
1031 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001032
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001033 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001034 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001035 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001036 // and andres, oldval, incr2
1037 // nor binopres, $0, andres
1038 // and newval, binopres, mask
1039 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1040 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1041 .addReg(Mips::ZERO).addReg(AndRes);
1042 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001043 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001044 // <binop> binopres, oldval, incr2
1045 // and newval, binopres, mask
1046 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1047 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001048 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001049 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001050 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001051 }
1052
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001053 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001054 .addReg(OldVal).addReg(Mask2);
1055 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001056 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001057 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1058 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001059 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001060 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001061
Akira Hatanaka939ece12011-07-19 03:42:13 +00001062 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001063 // and maskedoldval1,oldval,mask
1064 // srl srlres,maskedoldval1,shiftamt
1065 // sll sllres,srlres,24
1066 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001067 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001068 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001069
Akira Hatanaka4061da12011-07-19 20:11:17 +00001070 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1071 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001072 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1073 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001074 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1075 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001076 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001077 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001078
1079 MI->eraseFromParent(); // The instruction is gone now.
1080
Akira Hatanaka939ece12011-07-19 03:42:13 +00001081 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001082}
1083
1084MachineBasicBlock *
1085MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001086 MachineBasicBlock *BB,
1087 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001088 assert(Size == 4 && "Unsupported size for EmitAtomicCmpSwap.");
1089
1090 MachineFunction *MF = BB->getParent();
1091 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1092 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1093 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1094 DebugLoc dl = MI->getDebugLoc();
1095
1096 unsigned Dest = MI->getOperand(0).getReg();
1097 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001098 unsigned OldVal = MI->getOperand(2).getReg();
1099 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001100
Akira Hatanaka4061da12011-07-19 20:11:17 +00001101 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001102
1103 // insert new blocks after the current block
1104 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1105 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1106 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1107 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1108 MachineFunction::iterator It = BB;
1109 ++It;
1110 MF->insert(It, loop1MBB);
1111 MF->insert(It, loop2MBB);
1112 MF->insert(It, exitMBB);
1113
1114 // Transfer the remainder of BB and its successor edges to exitMBB.
1115 exitMBB->splice(exitMBB->begin(), BB,
1116 llvm::next(MachineBasicBlock::iterator(MI)),
1117 BB->end());
1118 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1119
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001120 // thisMBB:
1121 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001122 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001123 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001124 loop1MBB->addSuccessor(exitMBB);
1125 loop1MBB->addSuccessor(loop2MBB);
1126 loop2MBB->addSuccessor(loop1MBB);
1127 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001128
1129 // loop1MBB:
1130 // ll dest, 0(ptr)
1131 // bne dest, oldval, exitMBB
1132 BB = loop1MBB;
Akira Hatanakad3ac47f2011-07-07 18:57:00 +00001133 BuildMI(BB, dl, TII->get(Mips::LL), Dest).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001134 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001135 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001136
1137 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001138 // sc success, newval, 0(ptr)
1139 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001140 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001141 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1142 .addReg(NewVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001143 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001144 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001145
1146 MI->eraseFromParent(); // The instruction is gone now.
1147
Akira Hatanaka939ece12011-07-19 03:42:13 +00001148 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001149}
1150
1151MachineBasicBlock *
1152MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001153 MachineBasicBlock *BB,
1154 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001155 assert((Size == 1 || Size == 2) &&
1156 "Unsupported size for EmitAtomicCmpSwapPartial.");
1157
1158 MachineFunction *MF = BB->getParent();
1159 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1160 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1161 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1162 DebugLoc dl = MI->getDebugLoc();
1163
1164 unsigned Dest = MI->getOperand(0).getReg();
1165 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001166 unsigned CmpVal = MI->getOperand(2).getReg();
1167 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001168
Akira Hatanaka4061da12011-07-19 20:11:17 +00001169 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1170 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001171 unsigned Mask = RegInfo.createVirtualRegister(RC);
1172 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001173 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1174 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1175 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1176 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1177 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1178 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1179 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1180 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1181 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1182 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1183 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1184 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1185 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1186 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001187
1188 // insert new blocks after the current block
1189 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1190 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1191 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001192 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001193 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1194 MachineFunction::iterator It = BB;
1195 ++It;
1196 MF->insert(It, loop1MBB);
1197 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001198 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001199 MF->insert(It, exitMBB);
1200
1201 // Transfer the remainder of BB and its successor edges to exitMBB.
1202 exitMBB->splice(exitMBB->begin(), BB,
1203 llvm::next(MachineBasicBlock::iterator(MI)),
1204 BB->end());
1205 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1206
Akira Hatanaka81b44112011-07-19 17:09:53 +00001207 BB->addSuccessor(loop1MBB);
1208 loop1MBB->addSuccessor(sinkMBB);
1209 loop1MBB->addSuccessor(loop2MBB);
1210 loop2MBB->addSuccessor(loop1MBB);
1211 loop2MBB->addSuccessor(sinkMBB);
1212 sinkMBB->addSuccessor(exitMBB);
1213
Akira Hatanaka70564a92011-07-19 18:14:26 +00001214 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001215 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001216 // addiu masklsb2,$0,-4 # 0xfffffffc
1217 // and alignedaddr,ptr,masklsb2
1218 // andi ptrlsb2,ptr,3
1219 // sll shiftamt,ptrlsb2,3
1220 // ori maskupper,$0,255 # 0xff
1221 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001222 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001223 // andi maskedcmpval,cmpval,255
1224 // sll shiftedcmpval,maskedcmpval,shiftamt
1225 // andi maskednewval,newval,255
1226 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001227 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001228 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1229 .addReg(Mips::ZERO).addImm(-4);
1230 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1231 .addReg(Ptr).addReg(MaskLSB2);
1232 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1233 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1234 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1235 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001236 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1237 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001238 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001239 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1240 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001241 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1242 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001243 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1244 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001245 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1246 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001247
1248 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001249 // ll oldval,0(alginedaddr)
1250 // and maskedoldval0,oldval,mask
1251 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001252 BB = loop1MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001253 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
1254 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1255 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001256 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001257 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001258
1259 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001260 // and maskedoldval1,oldval,mask2
1261 // or storeval,maskedoldval1,shiftednewval
1262 // sc success,storeval,0(alignedaddr)
1263 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001264 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001265 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1266 .addReg(OldVal).addReg(Mask2);
1267 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1268 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1269 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1270 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001271 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001272 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001273
Akira Hatanaka939ece12011-07-19 03:42:13 +00001274 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001275 // srl srlres,maskedoldval0,shiftamt
1276 // sll sllres,srlres,24
1277 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001278 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001279 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001280
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001281 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1282 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001283 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1284 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001285 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001286 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001287
1288 MI->eraseFromParent(); // The instruction is gone now.
1289
Akira Hatanaka939ece12011-07-19 03:42:13 +00001290 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001291}
1292
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001293//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001294// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001295//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001296SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001297LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001298{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001299 MachineFunction &MF = DAG.getMachineFunction();
1300 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1301
1302 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001303 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1304 "Cannot lower if the alignment of the allocated space is larger than \
1305 that of the stack.");
1306
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001307 SDValue Chain = Op.getOperand(0);
1308 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001309 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001310
1311 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +00001312 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001313
1314 // Subtract the dynamic size from the actual stack size to
1315 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +00001316 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001317
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001318 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001319 // must be placed in the stack pointer register.
Akira Hatanaka053546c2011-05-25 02:20:00 +00001320 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub,
1321 SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001322
1323 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001324 // value and a chain
Akira Hatanaka21afc632011-06-21 00:40:49 +00001325 SDVTList VTLs = DAG.getVTList(MVT::i32, MVT::Other);
1326 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1327 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1328
1329 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001330}
1331
1332SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001333LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001334{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001335 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001336 // the block to branch to if the condition is true.
1337 SDValue Chain = Op.getOperand(0);
1338 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001339 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001340
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001341 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1342
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001343 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001344 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001345 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001346
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001347 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001348 Mips::CondCode CC =
1349 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001350 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001351
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001352 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001353 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001354}
1355
1356SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001357LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001358{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001359 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001360
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001361 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001362 if (Cond.getOpcode() != MipsISD::FPCmp)
1363 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001364
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001365 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1366 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001367}
1368
Dan Gohmand858e902010-04-17 15:26:15 +00001369SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1370 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001371 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001372 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001373 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001374
Eli Friedmane2c74082009-08-03 02:22:28 +00001375 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001376 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001377
Chris Lattnerb71b9092009-08-13 06:28:06 +00001378 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001379
Chris Lattnere3736f82009-08-13 05:41:27 +00001380 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001381 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1382 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001383 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001384 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1385 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001386 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001387 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001388 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001389 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1390 MipsII::MO_ABS_HI);
1391 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1392 MipsII::MO_ABS_LO);
1393 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1394 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001395 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001396 }
1397
Akira Hatanaka0f843822011-06-07 18:58:42 +00001398 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1399 MipsII::MO_GOT);
1400 GA = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, GA);
1401 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
1402 DAG.getEntryNode(), GA, MachinePointerInfo(),
1403 false, false, 0);
1404 // On functions and global targets not internal linked only
1405 // a load from got/GP is necessary for PIC to work.
1406 if (!GV->hasInternalLinkage() &&
1407 (!GV->hasLocalLinkage() || isa<Function>(GV)))
1408 return ResNode;
1409 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1410 MipsII::MO_ABS_LO);
1411 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
1412 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001413}
1414
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001415SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1416 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001417 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1418 // FIXME there isn't actually debug info here
1419 DebugLoc dl = Op.getDebugLoc();
1420
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001421 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001422 // %hi/%lo relocation
1423 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
1424 MipsII::MO_ABS_HI);
1425 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
1426 MipsII::MO_ABS_LO);
1427 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1428 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1429 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001430 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001431
1432 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1433 MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001434 BAGOTOffset = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, BAGOTOffset);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001435 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1436 MipsII::MO_ABS_LO);
1437 SDValue Load = DAG.getLoad(MVT::i32, dl,
1438 DAG.getEntryNode(), BAGOTOffset,
1439 MachinePointerInfo(), false, false, 0);
1440 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
1441 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001442}
1443
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001444SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001445LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001446{
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001447 // If the relocation model is PIC, use the General Dynamic TLS Model,
1448 // otherwise use the Initial Exec or Local Exec TLS Model.
1449 // TODO: implement Local Dynamic TLS model
1450
1451 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1452 DebugLoc dl = GA->getDebugLoc();
1453 const GlobalValue *GV = GA->getGlobal();
1454 EVT PtrVT = getPointerTy();
1455
1456 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1457 // General Dynamic TLS Model
1458 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001459 0, MipsII::MO_TLSGD);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001460 SDValue Tlsgd = DAG.getNode(MipsISD::TlsGd, dl, MVT::i32, TGA);
1461 SDValue GP = DAG.getRegister(Mips::GP, MVT::i32);
1462 SDValue Argument = DAG.getNode(ISD::ADD, dl, MVT::i32, GP, Tlsgd);
1463
1464 ArgListTy Args;
1465 ArgListEntry Entry;
1466 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001467 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001468 Args.push_back(Entry);
1469 std::pair<SDValue, SDValue> CallResult =
1470 LowerCallTo(DAG.getEntryNode(),
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001471 (Type *) Type::getInt32Ty(*DAG.getContext()),
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001472 false, false, false, false, 0, CallingConv::C, false, true,
1473 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG,
1474 dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001475
1476 return CallResult.first;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001477 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001478
1479 SDValue Offset;
1480 if (GV->isDeclaration()) {
1481 // Initial Exec TLS Model
1482 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1483 MipsII::MO_GOTTPREL);
1484 Offset = DAG.getLoad(MVT::i32, dl,
1485 DAG.getEntryNode(), TGA, MachinePointerInfo(),
1486 false, false, 0);
1487 } else {
1488 // Local Exec TLS Model
1489 SDVTList VTs = DAG.getVTList(MVT::i32);
1490 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1491 MipsII::MO_TPREL_HI);
1492 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1493 MipsII::MO_TPREL_LO);
1494 SDValue Hi = DAG.getNode(MipsISD::TprelHi, dl, VTs, &TGAHi, 1);
1495 SDValue Lo = DAG.getNode(MipsISD::TprelLo, dl, MVT::i32, TGALo);
1496 Offset = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1497 }
1498
1499 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1500 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001501}
1502
1503SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001504LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001505{
Dan Gohman475871a2008-07-27 21:46:04 +00001506 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001507 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +00001508 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001509 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001510 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001511 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001512
Owen Andersone50ed302009-08-10 22:56:29 +00001513 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001514 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001515
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001516 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
1517
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +00001518 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001519 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001520 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001521 } else {// Emit Load from Global Pointer
1522 JTI = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, JTI);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001523 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
1524 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001525 false, false, 0);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001526 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001527
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001528 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1529 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001530 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001531 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001532
1533 return ResNode;
1534}
1535
Dan Gohman475871a2008-07-27 21:46:04 +00001536SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001537LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001538{
Dan Gohman475871a2008-07-27 21:46:04 +00001539 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001540 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001541 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001542 // FIXME there isn't actually debug info here
1543 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001544
1545 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001546 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001547 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001548 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001549 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001550 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001551 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1552 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001553 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001554
1555 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001556 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001557 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001558 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001559 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001560 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1561 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001562 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001563 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001564 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001565 N->getOffset(), MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001566 CP = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001567 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001568 CP, MachinePointerInfo::getConstantPool(),
1569 false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001570 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001571 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001572 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001573 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
1574 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001575
1576 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001577}
1578
Dan Gohmand858e902010-04-17 15:26:15 +00001579SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001580 MachineFunction &MF = DAG.getMachineFunction();
1581 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1582
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001583 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001584 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1585 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001586
1587 // vastart just stores the address of the VarArgsFrameIndex slot into the
1588 // memory location argument.
1589 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001590 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1591 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +00001592 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001593}
1594
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001595static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
1596 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
1597 DebugLoc dl = Op.getDebugLoc();
1598 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
1599 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
1600 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
1601 DAG.getConstant(0x7fffffff, MVT::i32));
1602 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
1603 DAG.getConstant(0x80000000, MVT::i32));
1604 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1605 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
1606}
1607
1608static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001609 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001610 // Use ext/ins instructions if target architecture is Mips32r2.
1611 // Eliminate redundant mfc1 and mtc1 instructions.
1612 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001613
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001614 if (!isLittle)
1615 std::swap(LoIdx, HiIdx);
1616
1617 DebugLoc dl = Op.getDebugLoc();
1618 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1619 Op.getOperand(0),
1620 DAG.getConstant(LoIdx, MVT::i32));
1621 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1622 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1623 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1624 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1625 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1626 DAG.getConstant(0x7fffffff, MVT::i32));
1627 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1628 DAG.getConstant(0x80000000, MVT::i32));
1629 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1630
1631 if (!isLittle)
1632 std::swap(Word0, Word1);
1633
1634 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1635}
1636
1637SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1638 const {
1639 EVT Ty = Op.getValueType();
1640
1641 assert(Ty == MVT::f32 || Ty == MVT::f64);
1642
1643 if (Ty == MVT::f32)
1644 return LowerFCOPYSIGN32(Op, DAG);
1645 else
1646 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
1647}
1648
Akira Hatanaka2e591472011-06-02 00:24:44 +00001649SDValue MipsTargetLowering::
1650LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001651 // check the depth
1652 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001653 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001654
1655 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1656 MFI->setFrameAddressIsTaken(true);
1657 EVT VT = Op.getValueType();
1658 DebugLoc dl = Op.getDebugLoc();
1659 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Mips::FP, VT);
1660 return FrameAddr;
1661}
1662
Akira Hatanakadb548262011-07-19 23:30:50 +00001663// TODO: set SType according to the desired memory barrier behavior.
1664SDValue MipsTargetLowering::LowerMEMBARRIER(SDValue Op,
1665 SelectionDAG& DAG) const {
1666 unsigned SType = 0;
1667 DebugLoc dl = Op.getDebugLoc();
1668 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1669 DAG.getConstant(SType, MVT::i32));
1670}
1671
Eli Friedman14648462011-07-27 22:21:52 +00001672SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1673 SelectionDAG& DAG) const {
1674 // FIXME: Need pseudo-fence for 'singlethread' fences
1675 // FIXME: Set SType for weaker fences where supported/appropriate.
1676 unsigned SType = 0;
1677 DebugLoc dl = Op.getDebugLoc();
1678 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1679 DAG.getConstant(SType, MVT::i32));
1680}
1681
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001682//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001683// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001684//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001685
1686#include "MipsGenCallingConv.inc"
1687
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001688//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001689// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001690// Mips O32 ABI rules:
1691// ---
1692// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001693// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001694// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001695// f64 - Only passed in two aliased f32 registers if no int reg has been used
1696// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001697// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1698// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001699//
1700// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001701//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001702
Duncan Sands1e96bab2010-11-04 10:49:57 +00001703static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001704 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001705 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1706
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001707 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001708
1709 static const unsigned IntRegs[] = {
1710 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1711 };
1712 static const unsigned F32Regs[] = {
1713 Mips::F12, Mips::F14
1714 };
1715 static const unsigned F64Regs[] = {
1716 Mips::D6, Mips::D7
1717 };
1718
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001719 // ByVal Args
1720 if (ArgFlags.isByVal()) {
1721 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1722 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1723 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1724 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1725 r < std::min(IntRegsSize, NextReg); ++r)
1726 State.AllocateReg(IntRegs[r]);
1727 return false;
1728 }
1729
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001730 // Promote i8 and i16
1731 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1732 LocVT = MVT::i32;
1733 if (ArgFlags.isSExt())
1734 LocInfo = CCValAssign::SExt;
1735 else if (ArgFlags.isZExt())
1736 LocInfo = CCValAssign::ZExt;
1737 else
1738 LocInfo = CCValAssign::AExt;
1739 }
1740
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001741 unsigned Reg;
1742
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001743 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1744 // is true: function is vararg, argument is 3rd or higher, there is previous
1745 // argument which is not f32 or f64.
1746 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1747 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001748 unsigned OrigAlign = ArgFlags.getOrigAlign();
1749 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001750
1751 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001752 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001753 // If this is the first part of an i64 arg,
1754 // the allocated register must be either A0 or A2.
1755 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1756 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001757 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001758 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1759 // Allocate int register and shadow next int register. If first
1760 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001761 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1762 if (Reg == Mips::A1 || Reg == Mips::A3)
1763 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1764 State.AllocateReg(IntRegs, IntRegsSize);
1765 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001766 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1767 // we are guaranteed to find an available float register
1768 if (ValVT == MVT::f32) {
1769 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1770 // Shadow int register
1771 State.AllocateReg(IntRegs, IntRegsSize);
1772 } else {
1773 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1774 // Shadow int registers
1775 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1776 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1777 State.AllocateReg(IntRegs, IntRegsSize);
1778 State.AllocateReg(IntRegs, IntRegsSize);
1779 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001780 } else
1781 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001782
Akira Hatanakad37776d2011-05-20 21:39:54 +00001783 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1784 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1785
1786 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001787 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001788 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001789 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001790
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001791 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001792}
1793
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001794//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001795// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001796//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001797
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001798static const unsigned O32IntRegsSize = 4;
1799
1800static const unsigned O32IntRegs[] = {
1801 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1802};
1803
Akira Hatanaka373e3a42011-09-23 00:58:33 +00001804// Return next O32 integer argument register.
1805static unsigned getNextIntArgReg(unsigned Reg) {
1806 assert((Reg == Mips::A0) || (Reg == Mips::A2));
1807 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
1808}
1809
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001810// Write ByVal Arg to arg registers and stack.
1811static void
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001812WriteByValArg(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001813 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1814 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1815 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001816 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001817 MVT PtrType, bool isLittle) {
1818 unsigned LocMemOffset = VA.getLocMemOffset();
1819 unsigned Offset = 0;
1820 uint32_t RemainingSize = Flags.getByValSize();
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001821 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001822
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001823 // Copy the first 4 words of byval arg to registers A0 - A3.
1824 // FIXME: Use a stricter alignment if it enables better optimization in passes
1825 // run later.
1826 for (; RemainingSize >= 4 && LocMemOffset < 4 * 4;
1827 Offset += 4, RemainingSize -= 4, LocMemOffset += 4) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001828 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001829 DAG.getConstant(Offset, MVT::i32));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001830 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1831 MachinePointerInfo(),
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001832 false, false, std::min(ByValAlign,
1833 (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001834 MemOpChains.push_back(LoadVal.getValue(1));
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001835 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001836 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1837 }
1838
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001839 if (RemainingSize == 0)
1840 return;
1841
1842 // If there still is a register available for argument passing, write the
1843 // remaining part of the structure to it using subword loads and shifts.
1844 if (LocMemOffset < 4 * 4) {
1845 assert(RemainingSize <= 3 && RemainingSize >= 1 &&
1846 "There must be one to three bytes remaining.");
1847 unsigned LoadSize = (RemainingSize == 3 ? 2 : RemainingSize);
1848 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1849 DAG.getConstant(Offset, MVT::i32));
1850 unsigned Alignment = std::min(ByValAlign, (unsigned )4);
1851 SDValue LoadVal = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
1852 LoadPtr, MachinePointerInfo(),
1853 MVT::getIntegerVT(LoadSize * 8), false,
1854 false, Alignment);
1855 MemOpChains.push_back(LoadVal.getValue(1));
1856
1857 // If target is big endian, shift it to the most significant half-word or
1858 // byte.
1859 if (!isLittle)
1860 LoadVal = DAG.getNode(ISD::SHL, dl, MVT::i32, LoadVal,
1861 DAG.getConstant(32 - LoadSize * 8, MVT::i32));
1862
1863 Offset += LoadSize;
1864 RemainingSize -= LoadSize;
1865
1866 // Read second subword if necessary.
1867 if (RemainingSize != 0) {
1868 assert(RemainingSize == 1 && "There must be one byte remaining.");
1869 LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1870 DAG.getConstant(Offset, MVT::i32));
1871 unsigned Alignment = std::min(ByValAlign, (unsigned )2);
1872 SDValue Subword = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
1873 LoadPtr, MachinePointerInfo(),
1874 MVT::i8, false, false, Alignment);
1875 MemOpChains.push_back(Subword.getValue(1));
1876 // Insert the loaded byte to LoadVal.
1877 // FIXME: Use INS if supported by target.
1878 unsigned ShiftAmt = isLittle ? 16 : 8;
1879 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i32, Subword,
1880 DAG.getConstant(ShiftAmt, MVT::i32));
1881 LoadVal = DAG.getNode(ISD::OR, dl, MVT::i32, LoadVal, Shift);
1882 }
1883
1884 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
1885 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1886 return;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001887 }
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001888
1889 // Create a fixed object on stack at offset LocMemOffset and copy
1890 // remaining part of byval arg to it using memcpy.
1891 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1892 DAG.getConstant(Offset, MVT::i32));
1893 LastFI = MFI->CreateFixedObject(RemainingSize, LocMemOffset, true);
1894 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001895 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
1896 DAG.getConstant(RemainingSize, MVT::i32),
1897 std::min(ByValAlign, (unsigned)4),
1898 /*isVolatile=*/false, /*AlwaysInline=*/false,
1899 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001900}
1901
Dan Gohman98ca4f22009-08-05 01:29:28 +00001902/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001903/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001904/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001905SDValue
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001906MipsTargetLowering::LowerCall(SDValue InChain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001907 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001908 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001909 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001910 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001911 const SmallVectorImpl<ISD::InputArg> &Ins,
1912 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001913 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001914 // MIPs target does not yet support tail call optimization.
1915 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001916
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001917 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001918 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00001919 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001920 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001921 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001922
1923 // Analyze operands of the call, assigning locations to each operand.
1924 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001925 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1926 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001927
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001928 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001929 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001930 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001931 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001932
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001933 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001934 unsigned NextStackOffset = CCInfo.getNextStackOffset();
1935
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001936 // Chain is the output chain of the last Load/Store or CopyToReg node.
1937 // ByValChain is the output chain of the last Memcpy node created for copying
1938 // byval arguments to the stack.
1939 SDValue Chain, CallSeqStart, ByValChain;
1940 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
1941 Chain = CallSeqStart = DAG.getCALLSEQ_START(InChain, NextStackOffsetVal);
1942 ByValChain = InChain;
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001943
1944 // If this is the first call, create a stack frame object that points to
1945 // a location to which .cprestore saves $gp.
1946 if (IsPIC && !MipsFI->getGPFI())
1947 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
1948
Akira Hatanaka21afc632011-06-21 00:40:49 +00001949 // Get the frame index of the stack frame object that points to the location
1950 // of dynamically allocated area on the stack.
1951 int DynAllocFI = MipsFI->getDynAllocFI();
1952
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001953 // Update size of the maximum argument space.
1954 // For O32, a minimum of four words (16 bytes) of argument space is
1955 // allocated.
1956 if (Subtarget->isABI_O32())
1957 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
1958
1959 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1960
1961 if (MaxCallFrameSize < NextStackOffset) {
1962 MipsFI->setMaxCallFrameSize(NextStackOffset);
1963
Akira Hatanaka21afc632011-06-21 00:40:49 +00001964 // Set the offsets relative to $sp of the $gp restore slot and dynamically
1965 // allocated stack space. These offsets must be aligned to a boundary
1966 // determined by the stack alignment of the ABI.
1967 unsigned StackAlignment = TFL->getStackAlignment();
1968 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
1969 StackAlignment * StackAlignment;
1970
1971 if (IsPIC)
1972 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
1973
1974 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001975 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001976
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001977 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001978 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1979 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001980
Eric Christopher471e4222011-06-08 23:55:35 +00001981 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00001982
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001983 // Walk the register/memloc assignments, inserting copies/loads.
1984 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001985 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001986 CCValAssign &VA = ArgLocs[i];
1987
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001988 // Promote the value if needed.
1989 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001990 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001991 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001992 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001993 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001994 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001995 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001996 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1997 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001998 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1999 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002000 if (!Subtarget->isLittle())
2001 std::swap(Lo, Hi);
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002002 unsigned LocRegLo = VA.getLocReg();
2003 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2004 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2005 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002006 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002007 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002008 }
2009 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002010 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00002011 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002012 break;
2013 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00002014 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002015 break;
2016 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00002017 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002018 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002019 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002020
2021 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002022 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002023 if (VA.isRegLoc()) {
2024 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002025 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002026 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002027
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002028 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002029 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002030
Eric Christopher471e4222011-06-08 23:55:35 +00002031 // ByVal Arg.
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002032 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2033 if (Flags.isByVal()) {
2034 assert(Subtarget->isABI_O32() &&
2035 "No support for ByVal args by ABIs other than O32 yet.");
2036 assert(Flags.getByValSize() &&
2037 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002038 WriteByValArg(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI, MFI,
2039 DAG, Arg, VA, Flags, getPointerTy(), Subtarget->isLittle());
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002040 continue;
2041 }
2042
Chris Lattnere0b12152008-03-17 06:57:02 +00002043 // Create the frame index object for this incoming parameter
Eric Christopher471e4222011-06-08 23:55:35 +00002044 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002045 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00002046 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00002047
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002048 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002049 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00002050 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
2051 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00002052 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002053 }
2054
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002055 // Extend range of indices of frame objects for outgoing arguments that were
2056 // created during this function call. Skip this step if no such objects were
2057 // created.
2058 if (LastFI)
2059 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2060
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002061 // If a memcpy has been created to copy a byval arg to a stack, replace the
2062 // chain input of CallSeqStart with ByValChain.
2063 if (InChain != ByValChain)
2064 DAG.UpdateNodeOperands(CallSeqStart.getNode(), ByValChain,
2065 NextStackOffsetVal);
2066
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002067 // Transform all store nodes into one single node because all store
2068 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002069 if (!MemOpChains.empty())
2070 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002071 &MemOpChains[0], MemOpChains.size());
2072
Bill Wendling056292f2008-09-16 21:48:12 +00002073 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002074 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2075 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002076 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002077 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002078 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002079
2080 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002081 if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
2082 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2083 getPointerTy(), 0,MipsII:: MO_GOT);
2084 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
2085 0, MipsII::MO_ABS_LO);
2086 } else {
2087 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2088 getPointerTy(), 0, OpFlag);
2089 }
2090
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002091 LoadSymAddr = true;
2092 }
2093 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002094 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002095 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002096 LoadSymAddr = true;
2097 }
2098
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002099 SDValue InFlag;
2100
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002101 // Create nodes that load address of callee and copy it to T9
2102 if (IsPIC) {
2103 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002104 // Load callee address
Akira Hatanaka342837d2011-05-28 01:07:07 +00002105 Callee = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, Callee);
Akira Hatanaka25eba392011-06-24 19:01:25 +00002106 SDValue LoadValue = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), Callee,
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002107 MachinePointerInfo::getGOT(),
2108 false, false, 0);
2109
2110 // Use GOT+LO if callee has internal linkage.
2111 if (CalleeLo.getNode()) {
2112 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
2113 Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
2114 } else
2115 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002116 }
2117
2118 // copy to T9
2119 Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
2120 InFlag = Chain.getValue(1);
2121 Callee = DAG.getRegister(Mips::T9, MVT::i32);
2122 }
Bill Wendling056292f2008-09-16 21:48:12 +00002123
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002124 // Build a sequence of copy-to-reg nodes chained together with token
2125 // chain and flag operands which copy the outgoing args into registers.
2126 // The InFlag in necessary since all emitted instructions must be
2127 // stuck together.
2128 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2129 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2130 RegsToPass[i].second, InFlag);
2131 InFlag = Chain.getValue(1);
2132 }
2133
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002134 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002135 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002136 //
2137 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002138 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002139 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002140 Ops.push_back(Chain);
2141 Ops.push_back(Callee);
2142
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002143 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002144 // known live into the call.
2145 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2146 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2147 RegsToPass[i].second.getValueType()));
2148
Gabor Greifba36cb52008-08-28 21:40:38 +00002149 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002150 Ops.push_back(InFlag);
2151
Dale Johannesen33c960f2009-02-04 20:06:27 +00002152 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002153 InFlag = Chain.getValue(1);
2154
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002155 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002156 Chain = DAG.getCALLSEQ_END(Chain,
2157 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002158 DAG.getIntPtrConstant(0, true), InFlag);
2159 InFlag = Chain.getValue(1);
2160
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002161 // Handle result values, copying them out of physregs into vregs that we
2162 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002163 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2164 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002165}
2166
Dan Gohman98ca4f22009-08-05 01:29:28 +00002167/// LowerCallResult - Lower the result values of a call into the
2168/// appropriate copies out of appropriate physical registers.
2169SDValue
2170MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002171 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002172 const SmallVectorImpl<ISD::InputArg> &Ins,
2173 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002174 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002175 // Assign locations to each value returned by this call.
2176 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002177 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2178 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002179
Dan Gohman98ca4f22009-08-05 01:29:28 +00002180 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002181
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002182 // Copy all of the result registers out of their specified physreg.
2183 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002184 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002185 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002186 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002187 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002188 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002189
Dan Gohman98ca4f22009-08-05 01:29:28 +00002190 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002191}
2192
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002193//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002194// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002195//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002196static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2197 std::vector<SDValue>& OutChains,
2198 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2199 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2200 unsigned LocMem = VA.getLocMemOffset();
2201 unsigned FirstWord = LocMem / 4;
2202
2203 // copy register A0 - A3 to frame object
2204 for (unsigned i = 0; i < NumWords; ++i) {
2205 unsigned CurWord = FirstWord + i;
2206 if (CurWord >= O32IntRegsSize)
2207 break;
2208
2209 unsigned SrcReg = O32IntRegs[CurWord];
2210 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2211 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2212 DAG.getConstant(i * 4, MVT::i32));
2213 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2214 StorePtr, MachinePointerInfo(), false,
2215 false, 0);
2216 OutChains.push_back(Store);
2217 }
2218}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002219
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002220/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002221/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002222SDValue
2223MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002224 CallingConv::ID CallConv,
2225 bool isVarArg,
2226 const SmallVectorImpl<ISD::InputArg>
2227 &Ins,
2228 DebugLoc dl, SelectionDAG &DAG,
2229 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002230 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002231 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002232 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002233 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002234
Dan Gohman1e93df62010-04-17 14:41:14 +00002235 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002236
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002237 // Used with vargs to acumulate store chains.
2238 std::vector<SDValue> OutChains;
2239
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002240 // Assign locations to all of the incoming arguments.
2241 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002242 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2243 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002244
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002245 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002246 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002247 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002248 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002249
Akira Hatanaka43299772011-05-20 23:22:14 +00002250 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002251
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002252 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002253 CCValAssign &VA = ArgLocs[i];
2254
2255 // Arguments stored on registers
2256 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002257 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002258 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002259 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002260
Owen Anderson825b72b2009-08-11 20:47:22 +00002261 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002262 RC = Mips::CPURegsRegisterClass;
2263 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002264 RC = Mips::FGR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002265 else if (RegVT == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002266 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002267 RC = Mips::AFGR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002268 } else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002269 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002270
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002271 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002272 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002273 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002274 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002275
2276 // If this is an 8 or 16-bit value, it has been passed promoted
2277 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002278 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002279 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002280 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002281 if (VA.getLocInfo() == CCValAssign::SExt)
2282 Opcode = ISD::AssertSext;
2283 else if (VA.getLocInfo() == CCValAssign::ZExt)
2284 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002285 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002286 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00002287 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00002288 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002289 }
2290
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002291 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002292 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002293 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
2294 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00002295 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002296 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002297 getNextIntArgReg(ArgReg), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002298 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002299 if (!Subtarget->isLittle())
2300 std::swap(ArgValue, ArgValue2);
2301 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2302 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002303 }
2304 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002305
Dan Gohman98ca4f22009-08-05 01:29:28 +00002306 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002307 } else { // VA.isRegLoc()
2308
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002309 // sanity check
2310 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002311
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002312 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2313
2314 if (Flags.isByVal()) {
2315 assert(Subtarget->isABI_O32() &&
2316 "No support for ByVal args by ABIs other than O32 yet.");
2317 assert(Flags.getByValSize() &&
2318 "ByVal args of size 0 should have been ignored by front-end.");
2319 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2320 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2321 true);
2322 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2323 InVals.push_back(FIN);
2324 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2325
2326 continue;
2327 }
2328
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002329 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002330 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
2331 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002332
2333 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002334 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002335 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002336 MachinePointerInfo::getFixedStack(LastFI),
David Greenef6fa1862010-02-15 16:56:10 +00002337 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002338 }
2339 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002340
2341 // The mips ABIs for returning structs by value requires that we copy
2342 // the sret argument into $v0 for the return. Save the argument into
2343 // a virtual register so that we can access it from the return points.
2344 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2345 unsigned Reg = MipsFI->getSRetReturnReg();
2346 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002347 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002348 MipsFI->setSRetReturnReg(Reg);
2349 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002350 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002351 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002352 }
2353
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002354 if (isVarArg && Subtarget->isABI_O32()) {
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002355 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002356 // which is a value necessary to VASTART.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002357 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002358 assert(NextStackOffset % 4 == 0 &&
2359 "NextStackOffset must be aligned to 4-byte boundaries.");
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002360 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
2361 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002362
2363 // If NextStackOffset is smaller than o32's 16-byte reserved argument area,
2364 // copy the integer registers that have not been used for argument passing
2365 // to the caller's stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002366 for (; NextStackOffset < 16; NextStackOffset += 4) {
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002367 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002368 unsigned Idx = NextStackOffset / 4;
2369 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), O32IntRegs[Idx], RC);
2370 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
Akira Hatanaka69c19f72011-05-23 20:16:59 +00002371 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002372 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2373 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2374 MachinePointerInfo(),
2375 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002376 }
2377 }
2378
Akira Hatanaka43299772011-05-20 23:22:14 +00002379 MipsFI->setLastInArgFI(LastFI);
2380
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002381 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002382 // the size of Ins and InVals. This only happens when on varg functions
2383 if (!OutChains.empty()) {
2384 OutChains.push_back(Chain);
2385 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2386 &OutChains[0], OutChains.size());
2387 }
2388
Dan Gohman98ca4f22009-08-05 01:29:28 +00002389 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002390}
2391
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002392//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002393// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002394//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002395
Dan Gohman98ca4f22009-08-05 01:29:28 +00002396SDValue
2397MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002398 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002399 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002400 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002401 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002402
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002403 // CCValAssign - represent the assignment of
2404 // the return value to a location
2405 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002406
2407 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002408 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2409 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002410
Dan Gohman98ca4f22009-08-05 01:29:28 +00002411 // Analize return values.
2412 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002413
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002414 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002415 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002416 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002417 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002418 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002419 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002420 }
2421
Dan Gohman475871a2008-07-27 21:46:04 +00002422 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002423
2424 // Copy the result values into the output registers.
2425 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2426 CCValAssign &VA = RVLocs[i];
2427 assert(VA.isRegLoc() && "Can only return in registers!");
2428
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002429 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002430 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002431
2432 // guarantee that all emitted copies are
2433 // stuck together, avoiding something bad
2434 Flag = Chain.getValue(1);
2435 }
2436
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002437 // The mips ABIs for returning structs by value requires that we copy
2438 // the sret argument into $v0 for the return. We saved the argument into
2439 // a virtual register in the entry block, so now we copy the value out
2440 // and into $v0.
2441 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2442 MachineFunction &MF = DAG.getMachineFunction();
2443 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2444 unsigned Reg = MipsFI->getSRetReturnReg();
2445
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002446 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002447 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002448 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002449
Dale Johannesena05dca42009-02-04 23:02:30 +00002450 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002451 Flag = Chain.getValue(1);
2452 }
2453
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002454 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002455 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002456 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002457 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002458 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002459 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002460 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002461}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002462
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002463//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002464// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002465//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002466
2467/// getConstraintType - Given a constraint letter, return the type of
2468/// constraint it is for this target.
2469MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002470getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002471{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002472 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002473 // GCC config/mips/constraints.md
2474 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002475 // 'd' : An address register. Equivalent to r
2476 // unless generating MIPS16 code.
2477 // 'y' : Equivalent to r; retained for
2478 // backwards compatibility.
2479 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002480 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002481 switch (Constraint[0]) {
2482 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002483 case 'd':
2484 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002485 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002486 return C_RegisterClass;
2487 break;
2488 }
2489 }
2490 return TargetLowering::getConstraintType(Constraint);
2491}
2492
John Thompson44ab89e2010-10-29 17:29:13 +00002493/// Examine constraint type and operand type and determine a weight value.
2494/// This object must already have been set up with the operand type
2495/// and the current alternative constraint selected.
2496TargetLowering::ConstraintWeight
2497MipsTargetLowering::getSingleConstraintMatchWeight(
2498 AsmOperandInfo &info, const char *constraint) const {
2499 ConstraintWeight weight = CW_Invalid;
2500 Value *CallOperandVal = info.CallOperandVal;
2501 // If we don't have a value, we can't do a match,
2502 // but allow it at the lowest weight.
2503 if (CallOperandVal == NULL)
2504 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002505 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002506 // Look at the constraint type.
2507 switch (*constraint) {
2508 default:
2509 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2510 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002511 case 'd':
2512 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002513 if (type->isIntegerTy())
2514 weight = CW_Register;
2515 break;
2516 case 'f':
2517 if (type->isFloatTy())
2518 weight = CW_Register;
2519 break;
2520 }
2521 return weight;
2522}
2523
Eric Christopher38d64262011-06-29 19:33:04 +00002524/// Given a register class constraint, like 'r', if this corresponds directly
2525/// to an LLVM register class, return a register of 0 and the register class
2526/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002527std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002528getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002529{
2530 if (Constraint.size() == 1) {
2531 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002532 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2533 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002534 case 'r':
2535 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002536 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002537 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002538 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002539 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002540 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2541 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002542 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002543 }
2544 }
2545 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2546}
2547
Dan Gohman6520e202008-10-18 02:06:02 +00002548bool
2549MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2550 // The Mips target isn't yet aware of offsets.
2551 return false;
2552}
Evan Chengeb2f9692009-10-27 19:56:55 +00002553
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002554bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2555 if (VT != MVT::f32 && VT != MVT::f64)
2556 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002557 if (Imm.isNegZero())
2558 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002559 return Imm.isZero();
2560}