blob: 5271332d84217d4d6f38996b40b41b525f7d37ae [file] [log] [blame]
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000014
15#define DEBUG_TYPE "mips-lower"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000016#include "MipsISelLowering.h"
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +000017#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000018#include "MipsTargetMachine.h"
Chris Lattnerb71b9092009-08-13 06:28:06 +000019#include "MipsTargetObjectFile.h"
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000020#include "MipsSubtarget.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000021#include "llvm/DerivedTypes.h"
22#include "llvm/Function.h"
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +000023#include "llvm/GlobalVariable.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000024#include "llvm/Intrinsics.h"
25#include "llvm/CallingConv.h"
Akira Hatanaka794bf172011-07-07 23:56:50 +000026#include "InstPrinter/MipsInstPrinter.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000027#include "llvm/CodeGen/CallingConvLower.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000032#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033#include "llvm/CodeGen/ValueTypes.h"
34#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000036using namespace llvm;
37
Akira Hatanakadbe9a312011-08-18 20:07:42 +000038// If I is a shifted mask, set the size (Size) and the first bit of the
39// mask (Pos), and return true.
Akira Hatanaka854a7db2011-08-19 22:59:00 +000040// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
41static bool IsShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
42 if (!isUInt<32>(I) || !isShiftedMask_32(I))
43 return false;
Akira Hatanakabb15e112011-08-17 02:05:42 +000044
Akira Hatanaka854a7db2011-08-19 22:59:00 +000045 Size = CountPopulation_32(I);
46 Pos = CountTrailingZeros_32(I);
Akira Hatanakadbe9a312011-08-18 20:07:42 +000047 return true;
Akira Hatanakabb15e112011-08-17 02:05:42 +000048}
49
Chris Lattnerf0144122009-07-28 03:13:23 +000050const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
51 switch (Opcode) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000052 case MipsISD::JmpLink: return "MipsISD::JmpLink";
53 case MipsISD::Hi: return "MipsISD::Hi";
54 case MipsISD::Lo: return "MipsISD::Lo";
55 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +000056 case MipsISD::TlsGd: return "MipsISD::TlsGd";
57 case MipsISD::TprelHi: return "MipsISD::TprelHi";
58 case MipsISD::TprelLo: return "MipsISD::TprelLo";
59 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000060 case MipsISD::Ret: return "MipsISD::Ret";
61 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
62 case MipsISD::FPCmp: return "MipsISD::FPCmp";
63 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
64 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
65 case MipsISD::FPRound: return "MipsISD::FPRound";
66 case MipsISD::MAdd: return "MipsISD::MAdd";
67 case MipsISD::MAddu: return "MipsISD::MAddu";
68 case MipsISD::MSub: return "MipsISD::MSub";
69 case MipsISD::MSubu: return "MipsISD::MSubu";
70 case MipsISD::DivRem: return "MipsISD::DivRem";
71 case MipsISD::DivRemU: return "MipsISD::DivRemU";
72 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
73 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanaka342837d2011-05-28 01:07:07 +000074 case MipsISD::WrapperPIC: return "MipsISD::WrapperPIC";
Akira Hatanaka21afc632011-06-21 00:40:49 +000075 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
Akira Hatanakadb548262011-07-19 23:30:50 +000076 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanakabb15e112011-08-17 02:05:42 +000077 case MipsISD::Ext: return "MipsISD::Ext";
78 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanaka0f843822011-06-07 18:58:42 +000079 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000080 }
81}
82
83MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000084MipsTargetLowering(MipsTargetMachine &TM)
Akira Hatanaka8b4198d2011-09-26 21:47:02 +000085 : TargetLowering(TM, new MipsTargetObjectFile()),
86 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
Akira Hatanakaa5903ac2011-10-11 00:55:05 +000087 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000088
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000089 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000090 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000091 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +000092 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000093
94 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000095 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
96 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000097
Akira Hatanaka95934842011-09-24 01:34:44 +000098 if (HasMips64)
99 addRegisterClass(MVT::i64, Mips::CPU64RegsRegisterClass);
100
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000101 // When dealing with single precision only, use libcalls
Akira Hatanaka792016b2011-09-23 18:28:39 +0000102 if (!Subtarget->isSingleFloat()) {
103 if (HasMips64)
104 addRegisterClass(MVT::f64, Mips::FGR64RegisterClass);
105 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000106 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Akira Hatanaka792016b2011-09-23 18:28:39 +0000107 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000108
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000109 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000110 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
111 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
112 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000113
Eli Friedman6055a6a2009-07-17 04:07:24 +0000114 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000115 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
116 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000117
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000118 // Used by legalize types to correctly generate the setcc result.
119 // Without this, every float setcc comes with a AND/OR with the result,
120 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000121 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000122 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000123
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000124 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Akira Hatanakaa5903ac2011-10-11 00:55:05 +0000126 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000127 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000128 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
129 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
130 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
131 setOperationAction(ISD::SELECT, MVT::f32, Custom);
132 setOperationAction(ISD::SELECT, MVT::f64, Custom);
133 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
135 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000136 setOperationAction(ISD::VASTART, MVT::Other, Custom);
137
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000138 setOperationAction(ISD::SDIV, MVT::i32, Expand);
139 setOperationAction(ISD::SREM, MVT::i32, Expand);
140 setOperationAction(ISD::UDIV, MVT::i32, Expand);
141 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000142 setOperationAction(ISD::SDIV, MVT::i64, Expand);
143 setOperationAction(ISD::SREM, MVT::i64, Expand);
144 setOperationAction(ISD::UDIV, MVT::i64, Expand);
145 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000146
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000147 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000148 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
149 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
150 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
151 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
152 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
153 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
154 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
155 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
156 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000157 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000158
Akira Hatanaka56633442011-09-20 23:53:09 +0000159 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000160 setOperationAction(ISD::ROTR, MVT::i32, Expand);
161
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000162 if (!Subtarget->hasMips64r2())
163 setOperationAction(ISD::ROTR, MVT::i64, Expand);
164
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
166 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
167 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000168 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
169 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000170 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000171 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000172 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000173 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000174 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
175 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000176 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 setOperationAction(ISD::FLOG, MVT::f32, Expand);
178 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
179 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
180 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000181 setOperationAction(ISD::FMA, MVT::f32, Expand);
182 setOperationAction(ISD::FMA, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000183
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000184 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
185 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Eric Christopher471e4222011-06-08 23:55:35 +0000186
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000187 setOperationAction(ISD::VAARG, MVT::Other, Expand);
188 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
189 setOperationAction(ISD::VAEND, MVT::Other, Expand);
190
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000191 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
193 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000194
Akira Hatanakadb548262011-07-19 23:30:50 +0000195 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000196 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000197
Eli Friedman4db5aca2011-08-29 18:23:02 +0000198 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
199 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
200
Eli Friedman26689ac2011-08-03 21:06:02 +0000201 setInsertFencesForAtomic(true);
202
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000203 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000204 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000205
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000206 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000207 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
208 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000209 }
210
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000211 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000213
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000214 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000215 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000216
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000217 setTargetDAGCombine(ISD::ADDE);
218 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000219 setTargetDAGCombine(ISD::SDIVREM);
220 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000221 setTargetDAGCombine(ISD::SETCC);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000222 setTargetDAGCombine(ISD::AND);
223 setTargetDAGCombine(ISD::OR);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000224
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000225 setMinFunctionAlignment(2);
226
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000227 setStackPointerRegisterToSaveRestore(Mips::SP);
228 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000229
230 setExceptionPointerRegister(Mips::A0);
231 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000232}
233
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000234bool MipsTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Akira Hatanaka511961a2011-08-17 18:49:18 +0000235 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
Akira Hatanaka7bd19bd2011-10-11 00:27:28 +0000236 return SVT == MVT::i64 || SVT == MVT::i32 || SVT == MVT::i16;
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000237}
238
Duncan Sands28b77e92011-09-06 19:07:46 +0000239EVT MipsTargetLowering::getSetCCResultType(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000241}
242
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000243// SelectMadd -
244// Transforms a subgraph in CurDAG if the following pattern is found:
245// (addc multLo, Lo0), (adde multHi, Hi0),
246// where,
247// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000248// Lo0: initial value of Lo register
249// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000250// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000251static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000252 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000253 // for the matching to be successful.
254 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
255
256 if (ADDCNode->getOpcode() != ISD::ADDC)
257 return false;
258
259 SDValue MultHi = ADDENode->getOperand(0);
260 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000261 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000262 unsigned MultOpc = MultHi.getOpcode();
263
264 // MultHi and MultLo must be generated by the same node,
265 if (MultLo.getNode() != MultNode)
266 return false;
267
268 // and it must be a multiplication.
269 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
270 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000271
272 // MultLo amd MultHi must be the first and second output of MultNode
273 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000274 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
275 return false;
276
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000277 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000278 // of the values of MultNode, in which case MultNode will be removed in later
279 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000280 // If there exist users other than ADDENode or ADDCNode, this function returns
281 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000282 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000283 // produced.
284 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
285 return false;
286
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000287 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000288 DebugLoc dl = ADDENode->getDebugLoc();
289
290 // create MipsMAdd(u) node
291 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000292
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000293 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
294 MVT::Glue,
295 MultNode->getOperand(0),// Factor 0
296 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000297 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000298 ADDENode->getOperand(1));// Hi0
299
300 // create CopyFromReg nodes
301 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
302 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000303 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000304 Mips::HI, MVT::i32,
305 CopyFromLo.getValue(2));
306
307 // replace uses of adde and addc here
308 if (!SDValue(ADDCNode, 0).use_empty())
309 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
310
311 if (!SDValue(ADDENode, 0).use_empty())
312 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
313
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000314 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000315}
316
317// SelectMsub -
318// Transforms a subgraph in CurDAG if the following pattern is found:
319// (addc Lo0, multLo), (sube Hi0, multHi),
320// where,
321// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000322// Lo0: initial value of Lo register
323// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000324// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000325static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000326 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000327 // for the matching to be successful.
328 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
329
330 if (SUBCNode->getOpcode() != ISD::SUBC)
331 return false;
332
333 SDValue MultHi = SUBENode->getOperand(1);
334 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000335 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000336 unsigned MultOpc = MultHi.getOpcode();
337
338 // MultHi and MultLo must be generated by the same node,
339 if (MultLo.getNode() != MultNode)
340 return false;
341
342 // and it must be a multiplication.
343 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
344 return false;
345
346 // MultLo amd MultHi must be the first and second output of MultNode
347 // respectively.
348 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
349 return false;
350
351 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
352 // of the values of MultNode, in which case MultNode will be removed in later
353 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000354 // If there exist users other than SUBENode or SUBCNode, this function returns
355 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000356 // instruction node rather than a pair of MULT and MSUB instructions being
357 // produced.
358 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
359 return false;
360
361 SDValue Chain = CurDAG->getEntryNode();
362 DebugLoc dl = SUBENode->getDebugLoc();
363
364 // create MipsSub(u) node
365 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
366
367 SDValue MSub = CurDAG->getNode(MultOpc, dl,
368 MVT::Glue,
369 MultNode->getOperand(0),// Factor 0
370 MultNode->getOperand(1),// Factor 1
371 SUBCNode->getOperand(0),// Lo0
372 SUBENode->getOperand(0));// Hi0
373
374 // create CopyFromReg nodes
375 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
376 MSub);
377 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
378 Mips::HI, MVT::i32,
379 CopyFromLo.getValue(2));
380
381 // replace uses of sube and subc here
382 if (!SDValue(SUBCNode, 0).use_empty())
383 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
384
385 if (!SDValue(SUBENode, 0).use_empty())
386 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
387
388 return true;
389}
390
391static SDValue PerformADDECombine(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() && SelectMadd(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000398 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000399
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000400 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000401}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000402
403static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
404 TargetLowering::DAGCombinerInfo &DCI,
405 const MipsSubtarget* Subtarget) {
406 if (DCI.isBeforeLegalize())
407 return SDValue();
408
Akira Hatanaka56633442011-09-20 23:53:09 +0000409 if (Subtarget->hasMips32() && SelectMsub(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000410 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000411
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000412 return SDValue();
413}
414
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000415static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
416 TargetLowering::DAGCombinerInfo &DCI,
417 const MipsSubtarget* Subtarget) {
418 if (DCI.isBeforeLegalizeOps())
419 return SDValue();
420
Akira Hatanakadda4a072011-10-03 21:06:13 +0000421 EVT Ty = N->getValueType(0);
422 unsigned LO = (Ty == MVT::i32) ? Mips::LO : Mips::LO64;
423 unsigned HI = (Ty == MVT::i32) ? Mips::HI : Mips::HI64;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000424 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
425 MipsISD::DivRemU;
426 DebugLoc dl = N->getDebugLoc();
427
428 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
429 N->getOperand(0), N->getOperand(1));
430 SDValue InChain = DAG.getEntryNode();
431 SDValue InGlue = DivRem;
432
433 // insert MFLO
434 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakadda4a072011-10-03 21:06:13 +0000435 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000436 InGlue);
437 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
438 InChain = CopyFromLo.getValue(1);
439 InGlue = CopyFromLo.getValue(2);
440 }
441
442 // insert MFHI
443 if (N->hasAnyUseOfValue(1)) {
444 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000445 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000446 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
447 }
448
449 return SDValue();
450}
451
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000452static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
453 switch (CC) {
454 default: llvm_unreachable("Unknown fp condition code!");
455 case ISD::SETEQ:
456 case ISD::SETOEQ: return Mips::FCOND_OEQ;
457 case ISD::SETUNE: return Mips::FCOND_UNE;
458 case ISD::SETLT:
459 case ISD::SETOLT: return Mips::FCOND_OLT;
460 case ISD::SETGT:
461 case ISD::SETOGT: return Mips::FCOND_OGT;
462 case ISD::SETLE:
463 case ISD::SETOLE: return Mips::FCOND_OLE;
464 case ISD::SETGE:
465 case ISD::SETOGE: return Mips::FCOND_OGE;
466 case ISD::SETULT: return Mips::FCOND_ULT;
467 case ISD::SETULE: return Mips::FCOND_ULE;
468 case ISD::SETUGT: return Mips::FCOND_UGT;
469 case ISD::SETUGE: return Mips::FCOND_UGE;
470 case ISD::SETUO: return Mips::FCOND_UN;
471 case ISD::SETO: return Mips::FCOND_OR;
472 case ISD::SETNE:
473 case ISD::SETONE: return Mips::FCOND_ONE;
474 case ISD::SETUEQ: return Mips::FCOND_UEQ;
475 }
476}
477
478
479// Returns true if condition code has to be inverted.
480static bool InvertFPCondCode(Mips::CondCode CC) {
481 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
482 return false;
483
484 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
485 return true;
486
487 assert(false && "Illegal Condition Code");
488 return false;
489}
490
491// Creates and returns an FPCmp node from a setcc node.
492// Returns Op if setcc is not a floating point comparison.
493static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
494 // must be a SETCC node
495 if (Op.getOpcode() != ISD::SETCC)
496 return Op;
497
498 SDValue LHS = Op.getOperand(0);
499
500 if (!LHS.getValueType().isFloatingPoint())
501 return Op;
502
503 SDValue RHS = Op.getOperand(1);
504 DebugLoc dl = Op.getDebugLoc();
505
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000506 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
507 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000508 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
509
510 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
511 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
512}
513
514// Creates and returns a CMovFPT/F node.
515static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
516 SDValue False, DebugLoc DL) {
517 bool invert = InvertFPCondCode((Mips::CondCode)
518 cast<ConstantSDNode>(Cond.getOperand(2))
519 ->getSExtValue());
520
521 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
522 True.getValueType(), True, False, Cond);
523}
524
525static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
526 TargetLowering::DAGCombinerInfo &DCI,
527 const MipsSubtarget* Subtarget) {
528 if (DCI.isBeforeLegalizeOps())
529 return SDValue();
530
531 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
532
533 if (Cond.getOpcode() != MipsISD::FPCmp)
534 return SDValue();
535
536 SDValue True = DAG.getConstant(1, MVT::i32);
537 SDValue False = DAG.getConstant(0, MVT::i32);
538
539 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
540}
541
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000542static SDValue PerformANDCombine(SDNode *N, SelectionDAG& DAG,
543 TargetLowering::DAGCombinerInfo &DCI,
544 const MipsSubtarget* Subtarget) {
545 // Pattern match EXT.
546 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
547 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000548 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000549 return SDValue();
550
551 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
552
553 // Op's first operand must be a shift right.
554 if (ShiftRight.getOpcode() != ISD::SRA && ShiftRight.getOpcode() != ISD::SRL)
555 return SDValue();
556
557 // The second operand of the shift must be an immediate.
558 uint64_t Pos;
559 ConstantSDNode *CN;
560 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
561 return SDValue();
562
563 Pos = CN->getZExtValue();
564
565 uint64_t SMPos, SMSize;
566 // Op's second operand must be a shifted mask.
567 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000568 !IsShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000569 return SDValue();
570
571 // Return if the shifted mask does not start at bit 0 or the sum of its size
572 // and Pos exceeds the word's size.
573 if (SMPos != 0 || Pos + SMSize > 32)
574 return SDValue();
575
576 return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), MVT::i32,
577 ShiftRight.getOperand(0),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000578 DAG.getConstant(Pos, MVT::i32),
579 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000580}
581
582static SDValue PerformORCombine(SDNode *N, SelectionDAG& DAG,
583 TargetLowering::DAGCombinerInfo &DCI,
584 const MipsSubtarget* Subtarget) {
585 // Pattern match INS.
586 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
587 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
588 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000589 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000590 return SDValue();
591
592 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
593 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
594 ConstantSDNode *CN;
595
596 // See if Op's first operand matches (and $src1 , mask0).
597 if (And0.getOpcode() != ISD::AND)
598 return SDValue();
599
600 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000601 !IsShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000602 return SDValue();
603
604 // See if Op's second operand matches (and (shl $src, pos), mask1).
605 if (And1.getOpcode() != ISD::AND)
606 return SDValue();
607
608 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000609 !IsShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000610 return SDValue();
611
612 // The shift masks must have the same position and size.
613 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
614 return SDValue();
615
616 SDValue Shl = And1.getOperand(0);
617 if (Shl.getOpcode() != ISD::SHL)
618 return SDValue();
619
620 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
621 return SDValue();
622
623 unsigned Shamt = CN->getZExtValue();
624
625 // Return if the shift amount and the first bit position of mask are not the
626 // same.
627 if (Shamt != SMPos0)
628 return SDValue();
629
630 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), MVT::i32,
631 Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000632 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000633 DAG.getConstant(SMSize0, MVT::i32),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000634 And0.getOperand(0));
635}
636
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000637SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000638 const {
639 SelectionDAG &DAG = DCI.DAG;
640 unsigned opc = N->getOpcode();
641
642 switch (opc) {
643 default: break;
644 case ISD::ADDE:
645 return PerformADDECombine(N, DAG, DCI, Subtarget);
646 case ISD::SUBE:
647 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000648 case ISD::SDIVREM:
649 case ISD::UDIVREM:
650 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000651 case ISD::SETCC:
652 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000653 case ISD::AND:
654 return PerformANDCombine(N, DAG, DCI, Subtarget);
655 case ISD::OR:
656 return PerformORCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000657 }
658
659 return SDValue();
660}
661
Dan Gohman475871a2008-07-27 21:46:04 +0000662SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000663LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000664{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000665 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000666 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000667 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000668 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
669 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000670 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000671 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000672 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
673 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000674 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000675 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000676 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000677 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Akira Hatanakadb548262011-07-19 23:30:50 +0000678 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +0000679 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000680 }
Dan Gohman475871a2008-07-27 21:46:04 +0000681 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000682}
683
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000684//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000685// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000686//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000687
688// AddLiveIn - This helper function adds the specified physical register to the
689// MachineFunction as a live in value. It also creates a corresponding
690// virtual register for it.
691static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000692AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000693{
694 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000695 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
696 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000697 return VReg;
698}
699
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000700// Get fp branch code (not opcode) from condition code.
701static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
702 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
703 return Mips::BRANCH_T;
704
705 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
706 return Mips::BRANCH_F;
707
708 return Mips::BRANCH_INVALID;
709}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000710
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000711/*
Akira Hatanaka14487d42011-06-07 19:28:39 +0000712static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
713 DebugLoc dl,
714 const MipsSubtarget* Subtarget,
715 const TargetInstrInfo *TII,
716 bool isFPCmp, unsigned Opc) {
717 // There is no need to expand CMov instructions if target has
718 // conditional moves.
719 if (Subtarget->hasCondMov())
720 return BB;
721
722 // To "insert" a SELECT_CC instruction, we actually have to insert the
723 // diamond control-flow pattern. The incoming instruction knows the
724 // destination vreg to set, the condition code register to branch on, the
725 // true/false values to select between, and a branch opcode to use.
726 const BasicBlock *LLVM_BB = BB->getBasicBlock();
727 MachineFunction::iterator It = BB;
728 ++It;
729
730 // thisMBB:
731 // ...
732 // TrueVal = ...
733 // setcc r1, r2, r3
734 // bNE r1, r0, copy1MBB
735 // fallthrough --> copy0MBB
736 MachineBasicBlock *thisMBB = BB;
737 MachineFunction *F = BB->getParent();
738 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
739 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
740 F->insert(It, copy0MBB);
741 F->insert(It, sinkMBB);
742
743 // Transfer the remainder of BB and its successor edges to sinkMBB.
744 sinkMBB->splice(sinkMBB->begin(), BB,
745 llvm::next(MachineBasicBlock::iterator(MI)),
746 BB->end());
747 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
748
749 // Next, add the true and fallthrough blocks as its successors.
750 BB->addSuccessor(copy0MBB);
751 BB->addSuccessor(sinkMBB);
752
753 // Emit the right instruction according to the type of the operands compared
754 if (isFPCmp)
755 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
756 else
757 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
758 .addReg(Mips::ZERO).addMBB(sinkMBB);
759
760 // copy0MBB:
761 // %FalseValue = ...
762 // # fallthrough to sinkMBB
763 BB = copy0MBB;
764
765 // Update machine-CFG edges
766 BB->addSuccessor(sinkMBB);
767
768 // sinkMBB:
769 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
770 // ...
771 BB = sinkMBB;
772
773 if (isFPCmp)
774 BuildMI(*BB, BB->begin(), dl,
775 TII->get(Mips::PHI), MI->getOperand(0).getReg())
776 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
777 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
778 else
779 BuildMI(*BB, BB->begin(), dl,
780 TII->get(Mips::PHI), MI->getOperand(0).getReg())
781 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
782 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
783
784 MI->eraseFromParent(); // The pseudo instruction is gone now.
785 return BB;
786}
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000787*/
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000788MachineBasicBlock *
789MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000790 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000791 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000792 default:
793 assert(false && "Unexpected instr type to insert");
794 return NULL;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000795 case Mips::ATOMIC_LOAD_ADD_I8:
796 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
797 case Mips::ATOMIC_LOAD_ADD_I16:
798 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
799 case Mips::ATOMIC_LOAD_ADD_I32:
800 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
801
802 case Mips::ATOMIC_LOAD_AND_I8:
803 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
804 case Mips::ATOMIC_LOAD_AND_I16:
805 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
806 case Mips::ATOMIC_LOAD_AND_I32:
807 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
808
809 case Mips::ATOMIC_LOAD_OR_I8:
810 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
811 case Mips::ATOMIC_LOAD_OR_I16:
812 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
813 case Mips::ATOMIC_LOAD_OR_I32:
814 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
815
816 case Mips::ATOMIC_LOAD_XOR_I8:
817 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
818 case Mips::ATOMIC_LOAD_XOR_I16:
819 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
820 case Mips::ATOMIC_LOAD_XOR_I32:
821 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
822
823 case Mips::ATOMIC_LOAD_NAND_I8:
824 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
825 case Mips::ATOMIC_LOAD_NAND_I16:
826 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
827 case Mips::ATOMIC_LOAD_NAND_I32:
828 return EmitAtomicBinary(MI, BB, 4, 0, true);
829
830 case Mips::ATOMIC_LOAD_SUB_I8:
831 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
832 case Mips::ATOMIC_LOAD_SUB_I16:
833 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
834 case Mips::ATOMIC_LOAD_SUB_I32:
835 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
836
837 case Mips::ATOMIC_SWAP_I8:
838 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
839 case Mips::ATOMIC_SWAP_I16:
840 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
841 case Mips::ATOMIC_SWAP_I32:
842 return EmitAtomicBinary(MI, BB, 4, 0);
843
844 case Mips::ATOMIC_CMP_SWAP_I8:
845 return EmitAtomicCmpSwapPartword(MI, BB, 1);
846 case Mips::ATOMIC_CMP_SWAP_I16:
847 return EmitAtomicCmpSwapPartword(MI, BB, 2);
848 case Mips::ATOMIC_CMP_SWAP_I32:
849 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000850 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000851}
852
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000853// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
854// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
855MachineBasicBlock *
856MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000857 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000858 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000859 assert(Size == 4 && "Unsupported size for EmitAtomicBinary.");
860
861 MachineFunction *MF = BB->getParent();
862 MachineRegisterInfo &RegInfo = MF->getRegInfo();
863 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
864 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
865 DebugLoc dl = MI->getDebugLoc();
866
Akira Hatanaka4061da12011-07-19 20:11:17 +0000867 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000868 unsigned Ptr = MI->getOperand(1).getReg();
869 unsigned Incr = MI->getOperand(2).getReg();
870
Akira Hatanaka4061da12011-07-19 20:11:17 +0000871 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
872 unsigned AndRes = RegInfo.createVirtualRegister(RC);
873 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000874
875 // insert new blocks after the current block
876 const BasicBlock *LLVM_BB = BB->getBasicBlock();
877 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
878 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
879 MachineFunction::iterator It = BB;
880 ++It;
881 MF->insert(It, loopMBB);
882 MF->insert(It, exitMBB);
883
884 // Transfer the remainder of BB and its successor edges to exitMBB.
885 exitMBB->splice(exitMBB->begin(), BB,
886 llvm::next(MachineBasicBlock::iterator(MI)),
887 BB->end());
888 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
889
890 // thisMBB:
891 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000892 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000893 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000894 loopMBB->addSuccessor(loopMBB);
895 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000896
897 // loopMBB:
898 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000899 // <binop> storeval, oldval, incr
900 // sc success, storeval, 0(ptr)
901 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000902 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000903 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000904 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000905 // and andres, oldval, incr
906 // nor storeval, $0, andres
907 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr);
908 BuildMI(BB, dl, TII->get(Mips::NOR), StoreVal)
909 .addReg(Mips::ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000910 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000911 // <binop> storeval, oldval, incr
912 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000913 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000914 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000915 }
Akira Hatanaka4061da12011-07-19 20:11:17 +0000916 BuildMI(BB, dl, TII->get(Mips::SC), Success)
917 .addReg(StoreVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000918 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +0000919 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000920
921 MI->eraseFromParent(); // The instruction is gone now.
922
Akira Hatanaka939ece12011-07-19 03:42:13 +0000923 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000924}
925
926MachineBasicBlock *
927MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000928 MachineBasicBlock *BB,
929 unsigned Size, unsigned BinOpcode,
930 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000931 assert((Size == 1 || Size == 2) &&
932 "Unsupported size for EmitAtomicBinaryPartial.");
933
934 MachineFunction *MF = BB->getParent();
935 MachineRegisterInfo &RegInfo = MF->getRegInfo();
936 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
937 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
938 DebugLoc dl = MI->getDebugLoc();
939
940 unsigned Dest = MI->getOperand(0).getReg();
941 unsigned Ptr = MI->getOperand(1).getReg();
942 unsigned Incr = MI->getOperand(2).getReg();
943
Akira Hatanaka4061da12011-07-19 20:11:17 +0000944 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
945 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000946 unsigned Mask = RegInfo.createVirtualRegister(RC);
947 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000948 unsigned NewVal = RegInfo.createVirtualRegister(RC);
949 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000950 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000951 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
952 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
953 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
954 unsigned AndRes = RegInfo.createVirtualRegister(RC);
955 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +0000956 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000957 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
958 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
959 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
960 unsigned SllRes = RegInfo.createVirtualRegister(RC);
961 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000962
963 // insert new blocks after the current block
964 const BasicBlock *LLVM_BB = BB->getBasicBlock();
965 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000966 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000967 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
968 MachineFunction::iterator It = BB;
969 ++It;
970 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000971 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000972 MF->insert(It, exitMBB);
973
974 // Transfer the remainder of BB and its successor edges to exitMBB.
975 exitMBB->splice(exitMBB->begin(), BB,
976 llvm::next(MachineBasicBlock::iterator(MI)),
977 BB->end());
978 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
979
Akira Hatanaka81b44112011-07-19 17:09:53 +0000980 BB->addSuccessor(loopMBB);
981 loopMBB->addSuccessor(loopMBB);
982 loopMBB->addSuccessor(sinkMBB);
983 sinkMBB->addSuccessor(exitMBB);
984
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000985 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +0000986 // addiu masklsb2,$0,-4 # 0xfffffffc
987 // and alignedaddr,ptr,masklsb2
988 // andi ptrlsb2,ptr,3
989 // sll shiftamt,ptrlsb2,3
990 // ori maskupper,$0,255 # 0xff
991 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000992 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +0000993 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000994
995 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000996 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
997 .addReg(Mips::ZERO).addImm(-4);
998 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
999 .addReg(Ptr).addReg(MaskLSB2);
1000 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1001 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1002 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1003 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001004 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1005 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001006 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001007 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001008
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001009
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001010 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001011 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001012 // ll oldval,0(alignedaddr)
1013 // binop binopres,oldval,incr2
1014 // and newval,binopres,mask
1015 // and maskedoldval0,oldval,mask2
1016 // or storeval,maskedoldval0,newval
1017 // sc success,storeval,0(alignedaddr)
1018 // beq success,$0,loopMBB
1019
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001020 // atomic.swap
1021 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001022 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001023 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001024 // and maskedoldval0,oldval,mask2
1025 // or storeval,maskedoldval0,newval
1026 // sc success,storeval,0(alignedaddr)
1027 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001028
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001029 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001030 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001031 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001032 // and andres, oldval, incr2
1033 // nor binopres, $0, andres
1034 // and newval, binopres, mask
1035 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1036 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1037 .addReg(Mips::ZERO).addReg(AndRes);
1038 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001039 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001040 // <binop> binopres, oldval, incr2
1041 // and newval, binopres, mask
1042 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1043 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001044 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001045 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001046 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001047 }
1048
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001049 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001050 .addReg(OldVal).addReg(Mask2);
1051 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001052 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001053 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1054 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001055 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001056 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001057
Akira Hatanaka939ece12011-07-19 03:42:13 +00001058 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001059 // and maskedoldval1,oldval,mask
1060 // srl srlres,maskedoldval1,shiftamt
1061 // sll sllres,srlres,24
1062 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001063 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001064 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001065
Akira Hatanaka4061da12011-07-19 20:11:17 +00001066 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1067 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001068 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1069 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001070 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1071 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001072 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001073 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001074
1075 MI->eraseFromParent(); // The instruction is gone now.
1076
Akira Hatanaka939ece12011-07-19 03:42:13 +00001077 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001078}
1079
1080MachineBasicBlock *
1081MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001082 MachineBasicBlock *BB,
1083 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001084 assert(Size == 4 && "Unsupported size for EmitAtomicCmpSwap.");
1085
1086 MachineFunction *MF = BB->getParent();
1087 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1088 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1089 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1090 DebugLoc dl = MI->getDebugLoc();
1091
1092 unsigned Dest = MI->getOperand(0).getReg();
1093 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001094 unsigned OldVal = MI->getOperand(2).getReg();
1095 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001096
Akira Hatanaka4061da12011-07-19 20:11:17 +00001097 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001098
1099 // insert new blocks after the current block
1100 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1101 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1102 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1103 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1104 MachineFunction::iterator It = BB;
1105 ++It;
1106 MF->insert(It, loop1MBB);
1107 MF->insert(It, loop2MBB);
1108 MF->insert(It, exitMBB);
1109
1110 // Transfer the remainder of BB and its successor edges to exitMBB.
1111 exitMBB->splice(exitMBB->begin(), BB,
1112 llvm::next(MachineBasicBlock::iterator(MI)),
1113 BB->end());
1114 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1115
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001116 // thisMBB:
1117 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001118 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001119 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001120 loop1MBB->addSuccessor(exitMBB);
1121 loop1MBB->addSuccessor(loop2MBB);
1122 loop2MBB->addSuccessor(loop1MBB);
1123 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001124
1125 // loop1MBB:
1126 // ll dest, 0(ptr)
1127 // bne dest, oldval, exitMBB
1128 BB = loop1MBB;
Akira Hatanakad3ac47f2011-07-07 18:57:00 +00001129 BuildMI(BB, dl, TII->get(Mips::LL), Dest).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001130 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001131 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001132
1133 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001134 // sc success, newval, 0(ptr)
1135 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001136 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001137 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1138 .addReg(NewVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001139 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001140 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001141
1142 MI->eraseFromParent(); // The instruction is gone now.
1143
Akira Hatanaka939ece12011-07-19 03:42:13 +00001144 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001145}
1146
1147MachineBasicBlock *
1148MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001149 MachineBasicBlock *BB,
1150 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001151 assert((Size == 1 || Size == 2) &&
1152 "Unsupported size for EmitAtomicCmpSwapPartial.");
1153
1154 MachineFunction *MF = BB->getParent();
1155 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1156 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1157 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1158 DebugLoc dl = MI->getDebugLoc();
1159
1160 unsigned Dest = MI->getOperand(0).getReg();
1161 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001162 unsigned CmpVal = MI->getOperand(2).getReg();
1163 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001164
Akira Hatanaka4061da12011-07-19 20:11:17 +00001165 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1166 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001167 unsigned Mask = RegInfo.createVirtualRegister(RC);
1168 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001169 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1170 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1171 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1172 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1173 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1174 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1175 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1176 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1177 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1178 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1179 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1180 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1181 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1182 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001183
1184 // insert new blocks after the current block
1185 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1186 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1187 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001188 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001189 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1190 MachineFunction::iterator It = BB;
1191 ++It;
1192 MF->insert(It, loop1MBB);
1193 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001194 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001195 MF->insert(It, exitMBB);
1196
1197 // Transfer the remainder of BB and its successor edges to exitMBB.
1198 exitMBB->splice(exitMBB->begin(), BB,
1199 llvm::next(MachineBasicBlock::iterator(MI)),
1200 BB->end());
1201 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1202
Akira Hatanaka81b44112011-07-19 17:09:53 +00001203 BB->addSuccessor(loop1MBB);
1204 loop1MBB->addSuccessor(sinkMBB);
1205 loop1MBB->addSuccessor(loop2MBB);
1206 loop2MBB->addSuccessor(loop1MBB);
1207 loop2MBB->addSuccessor(sinkMBB);
1208 sinkMBB->addSuccessor(exitMBB);
1209
Akira Hatanaka70564a92011-07-19 18:14:26 +00001210 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001211 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001212 // addiu masklsb2,$0,-4 # 0xfffffffc
1213 // and alignedaddr,ptr,masklsb2
1214 // andi ptrlsb2,ptr,3
1215 // sll shiftamt,ptrlsb2,3
1216 // ori maskupper,$0,255 # 0xff
1217 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001218 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001219 // andi maskedcmpval,cmpval,255
1220 // sll shiftedcmpval,maskedcmpval,shiftamt
1221 // andi maskednewval,newval,255
1222 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001223 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001224 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1225 .addReg(Mips::ZERO).addImm(-4);
1226 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1227 .addReg(Ptr).addReg(MaskLSB2);
1228 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1229 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1230 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1231 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001232 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1233 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001234 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001235 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1236 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001237 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1238 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001239 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1240 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001241 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1242 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001243
1244 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001245 // ll oldval,0(alginedaddr)
1246 // and maskedoldval0,oldval,mask
1247 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001248 BB = loop1MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001249 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
1250 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1251 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001252 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001253 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001254
1255 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001256 // and maskedoldval1,oldval,mask2
1257 // or storeval,maskedoldval1,shiftednewval
1258 // sc success,storeval,0(alignedaddr)
1259 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001260 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001261 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1262 .addReg(OldVal).addReg(Mask2);
1263 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1264 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1265 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1266 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001267 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001268 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001269
Akira Hatanaka939ece12011-07-19 03:42:13 +00001270 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001271 // srl srlres,maskedoldval0,shiftamt
1272 // sll sllres,srlres,24
1273 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001274 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001275 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001276
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001277 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1278 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001279 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1280 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001281 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001282 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001283
1284 MI->eraseFromParent(); // The instruction is gone now.
1285
Akira Hatanaka939ece12011-07-19 03:42:13 +00001286 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001287}
1288
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001289//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001290// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001291//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001292SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001293LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001294{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001295 MachineFunction &MF = DAG.getMachineFunction();
1296 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1297
1298 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001299 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1300 "Cannot lower if the alignment of the allocated space is larger than \
1301 that of the stack.");
1302
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001303 SDValue Chain = Op.getOperand(0);
1304 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001305 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001306
1307 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +00001308 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001309
1310 // Subtract the dynamic size from the actual stack size to
1311 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +00001312 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001313
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001314 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001315 // must be placed in the stack pointer register.
Akira Hatanaka053546c2011-05-25 02:20:00 +00001316 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub,
1317 SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001318
1319 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001320 // value and a chain
Akira Hatanaka21afc632011-06-21 00:40:49 +00001321 SDVTList VTLs = DAG.getVTList(MVT::i32, MVT::Other);
1322 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1323 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1324
1325 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001326}
1327
1328SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001329LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001330{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001331 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001332 // the block to branch to if the condition is true.
1333 SDValue Chain = Op.getOperand(0);
1334 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001335 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001336
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001337 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1338
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001339 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001340 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001341 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001342
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001343 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001344 Mips::CondCode CC =
1345 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001346 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001347
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001348 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001349 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001350}
1351
1352SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001353LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001354{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001355 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001356
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001357 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001358 if (Cond.getOpcode() != MipsISD::FPCmp)
1359 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001360
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001361 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1362 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001363}
1364
Dan Gohmand858e902010-04-17 15:26:15 +00001365SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1366 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001367 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001368 DebugLoc dl = Op.getDebugLoc();
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001369 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001370
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001371 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001372 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001373
Chris Lattnerb71b9092009-08-13 06:28:06 +00001374 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001375
Chris Lattnere3736f82009-08-13 05:41:27 +00001376 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001377 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1378 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001379 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001380 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1381 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001382 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001383 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001384 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001385 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1386 MipsII::MO_ABS_HI);
1387 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1388 MipsII::MO_ABS_LO);
1389 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1390 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001391 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001392 }
1393
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001394 EVT ValTy = Op.getValueType();
1395 bool HasGotOfst = (GV->hasInternalLinkage() ||
1396 (GV->hasLocalLinkage() && !isa<Function>(GV)));
1397 unsigned GotFlag = IsN64 ?
1398 (HasGotOfst ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT_DISP) :
1399 MipsII::MO_GOT;
1400 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0, GotFlag);
1401 GA = DAG.getNode(MipsISD::WrapperPIC, dl, ValTy, GA);
1402 SDValue ResNode = DAG.getLoad(ValTy, dl,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001403 DAG.getEntryNode(), GA, MachinePointerInfo(),
1404 false, false, 0);
1405 // On functions and global targets not internal linked only
1406 // a load from got/GP is necessary for PIC to work.
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001407 if (!HasGotOfst)
Akira Hatanaka0f843822011-06-07 18:58:42 +00001408 return ResNode;
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001409 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0,
1410 IsN64 ? MipsII::MO_GOT_OFST :
1411 MipsII::MO_ABS_LO);
1412 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, GALo);
1413 return DAG.getNode(ISD::ADD, dl, ValTy, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001414}
1415
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001416SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1417 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001418 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1419 // FIXME there isn't actually debug info here
1420 DebugLoc dl = Op.getDebugLoc();
1421
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001422 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001423 // %hi/%lo relocation
1424 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
1425 MipsII::MO_ABS_HI);
1426 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
1427 MipsII::MO_ABS_LO);
1428 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1429 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1430 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001431 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001432
1433 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1434 MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001435 BAGOTOffset = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, BAGOTOffset);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001436 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1437 MipsII::MO_ABS_LO);
1438 SDValue Load = DAG.getLoad(MVT::i32, dl,
1439 DAG.getEntryNode(), BAGOTOffset,
1440 MachinePointerInfo(), false, false, 0);
1441 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
1442 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001443}
1444
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001445SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001446LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001447{
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001448 // If the relocation model is PIC, use the General Dynamic TLS Model,
1449 // otherwise use the Initial Exec or Local Exec TLS Model.
1450 // TODO: implement Local Dynamic TLS model
1451
1452 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1453 DebugLoc dl = GA->getDebugLoc();
1454 const GlobalValue *GV = GA->getGlobal();
1455 EVT PtrVT = getPointerTy();
1456
1457 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1458 // General Dynamic TLS Model
1459 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001460 0, MipsII::MO_TLSGD);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001461 SDValue Tlsgd = DAG.getNode(MipsISD::TlsGd, dl, MVT::i32, TGA);
1462 SDValue GP = DAG.getRegister(Mips::GP, MVT::i32);
1463 SDValue Argument = DAG.getNode(ISD::ADD, dl, MVT::i32, GP, Tlsgd);
1464
1465 ArgListTy Args;
1466 ArgListEntry Entry;
1467 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001468 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001469 Args.push_back(Entry);
1470 std::pair<SDValue, SDValue> CallResult =
1471 LowerCallTo(DAG.getEntryNode(),
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001472 (Type *) Type::getInt32Ty(*DAG.getContext()),
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001473 false, false, false, false, 0, CallingConv::C, false, true,
1474 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG,
1475 dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001476
1477 return CallResult.first;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001478 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001479
1480 SDValue Offset;
1481 if (GV->isDeclaration()) {
1482 // Initial Exec TLS Model
1483 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1484 MipsII::MO_GOTTPREL);
1485 Offset = DAG.getLoad(MVT::i32, dl,
1486 DAG.getEntryNode(), TGA, MachinePointerInfo(),
1487 false, false, 0);
1488 } else {
1489 // Local Exec TLS Model
1490 SDVTList VTs = DAG.getVTList(MVT::i32);
1491 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1492 MipsII::MO_TPREL_HI);
1493 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1494 MipsII::MO_TPREL_LO);
1495 SDValue Hi = DAG.getNode(MipsISD::TprelHi, dl, VTs, &TGAHi, 1);
1496 SDValue Lo = DAG.getNode(MipsISD::TprelLo, dl, MVT::i32, TGALo);
1497 Offset = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1498 }
1499
1500 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1501 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001502}
1503
1504SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001505LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001506{
Dan Gohman475871a2008-07-27 21:46:04 +00001507 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001508 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +00001509 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001510 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001511 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001512 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001513
Owen Andersone50ed302009-08-10 22:56:29 +00001514 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001515 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001516
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001517 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
1518
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +00001519 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001520 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001521 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001522 } else {// Emit Load from Global Pointer
1523 JTI = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, JTI);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001524 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
1525 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001526 false, false, 0);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001527 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001528
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001529 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1530 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001531 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001532 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001533
1534 return ResNode;
1535}
1536
Dan Gohman475871a2008-07-27 21:46:04 +00001537SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001538LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001539{
Dan Gohman475871a2008-07-27 21:46:04 +00001540 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001541 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001542 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001543 // FIXME there isn't actually debug info here
1544 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001545
1546 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001547 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001548 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001549 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001550 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001551 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001552 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1553 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001554 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001555
1556 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001557 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001558 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001559 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001560 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001561 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1562 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001563 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001564 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001565 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001566 N->getOffset(), MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001567 CP = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001568 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001569 CP, MachinePointerInfo::getConstantPool(),
1570 false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001571 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001572 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001573 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001574 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
1575 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001576
1577 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001578}
1579
Dan Gohmand858e902010-04-17 15:26:15 +00001580SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001581 MachineFunction &MF = DAG.getMachineFunction();
1582 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1583
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001584 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001585 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1586 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001587
1588 // vastart just stores the address of the VarArgsFrameIndex slot into the
1589 // memory location argument.
1590 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001591 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1592 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +00001593 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001594}
1595
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001596static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
1597 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
1598 DebugLoc dl = Op.getDebugLoc();
1599 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
1600 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
1601 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
1602 DAG.getConstant(0x7fffffff, MVT::i32));
1603 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
1604 DAG.getConstant(0x80000000, MVT::i32));
1605 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1606 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
1607}
1608
1609static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001610 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001611 // Use ext/ins instructions if target architecture is Mips32r2.
1612 // Eliminate redundant mfc1 and mtc1 instructions.
1613 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001614
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001615 if (!isLittle)
1616 std::swap(LoIdx, HiIdx);
1617
1618 DebugLoc dl = Op.getDebugLoc();
1619 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1620 Op.getOperand(0),
1621 DAG.getConstant(LoIdx, MVT::i32));
1622 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1623 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1624 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1625 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1626 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1627 DAG.getConstant(0x7fffffff, MVT::i32));
1628 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1629 DAG.getConstant(0x80000000, MVT::i32));
1630 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1631
1632 if (!isLittle)
1633 std::swap(Word0, Word1);
1634
1635 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1636}
1637
1638SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1639 const {
1640 EVT Ty = Op.getValueType();
1641
1642 assert(Ty == MVT::f32 || Ty == MVT::f64);
1643
1644 if (Ty == MVT::f32)
1645 return LowerFCOPYSIGN32(Op, DAG);
1646 else
1647 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
1648}
1649
Akira Hatanaka2e591472011-06-02 00:24:44 +00001650SDValue MipsTargetLowering::
1651LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001652 // check the depth
1653 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001654 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001655
1656 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1657 MFI->setFrameAddressIsTaken(true);
1658 EVT VT = Op.getValueType();
1659 DebugLoc dl = Op.getDebugLoc();
1660 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Mips::FP, VT);
1661 return FrameAddr;
1662}
1663
Akira Hatanakadb548262011-07-19 23:30:50 +00001664// TODO: set SType according to the desired memory barrier behavior.
1665SDValue MipsTargetLowering::LowerMEMBARRIER(SDValue Op,
1666 SelectionDAG& DAG) const {
1667 unsigned SType = 0;
1668 DebugLoc dl = Op.getDebugLoc();
1669 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1670 DAG.getConstant(SType, MVT::i32));
1671}
1672
Eli Friedman14648462011-07-27 22:21:52 +00001673SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1674 SelectionDAG& DAG) const {
1675 // FIXME: Need pseudo-fence for 'singlethread' fences
1676 // FIXME: Set SType for weaker fences where supported/appropriate.
1677 unsigned SType = 0;
1678 DebugLoc dl = Op.getDebugLoc();
1679 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1680 DAG.getConstant(SType, MVT::i32));
1681}
1682
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001683//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001684// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001685//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001686
1687#include "MipsGenCallingConv.inc"
1688
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001689//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001690// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001691// Mips O32 ABI rules:
1692// ---
1693// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001694// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001695// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001696// f64 - Only passed in two aliased f32 registers if no int reg has been used
1697// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001698// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1699// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001700//
1701// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001702//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001703
Duncan Sands1e96bab2010-11-04 10:49:57 +00001704static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001705 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001706 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1707
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001708 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001709
1710 static const unsigned IntRegs[] = {
1711 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1712 };
1713 static const unsigned F32Regs[] = {
1714 Mips::F12, Mips::F14
1715 };
1716 static const unsigned F64Regs[] = {
1717 Mips::D6, Mips::D7
1718 };
1719
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001720 // ByVal Args
1721 if (ArgFlags.isByVal()) {
1722 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1723 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1724 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1725 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1726 r < std::min(IntRegsSize, NextReg); ++r)
1727 State.AllocateReg(IntRegs[r]);
1728 return false;
1729 }
1730
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001731 // Promote i8 and i16
1732 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1733 LocVT = MVT::i32;
1734 if (ArgFlags.isSExt())
1735 LocInfo = CCValAssign::SExt;
1736 else if (ArgFlags.isZExt())
1737 LocInfo = CCValAssign::ZExt;
1738 else
1739 LocInfo = CCValAssign::AExt;
1740 }
1741
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001742 unsigned Reg;
1743
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001744 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1745 // is true: function is vararg, argument is 3rd or higher, there is previous
1746 // argument which is not f32 or f64.
1747 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1748 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001749 unsigned OrigAlign = ArgFlags.getOrigAlign();
1750 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001751
1752 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001753 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001754 // If this is the first part of an i64 arg,
1755 // the allocated register must be either A0 or A2.
1756 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1757 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001758 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001759 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1760 // Allocate int register and shadow next int register. If first
1761 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001762 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1763 if (Reg == Mips::A1 || Reg == Mips::A3)
1764 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1765 State.AllocateReg(IntRegs, IntRegsSize);
1766 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001767 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1768 // we are guaranteed to find an available float register
1769 if (ValVT == MVT::f32) {
1770 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1771 // Shadow int register
1772 State.AllocateReg(IntRegs, IntRegsSize);
1773 } else {
1774 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1775 // Shadow int registers
1776 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1777 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1778 State.AllocateReg(IntRegs, IntRegsSize);
1779 State.AllocateReg(IntRegs, IntRegsSize);
1780 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001781 } else
1782 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001783
Akira Hatanakad37776d2011-05-20 21:39:54 +00001784 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1785 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1786
1787 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001788 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001789 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001790 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001791
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001792 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001793}
1794
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001795//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001796// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001797//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001798
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001799static const unsigned O32IntRegsSize = 4;
1800
1801static const unsigned O32IntRegs[] = {
1802 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1803};
1804
Akira Hatanaka373e3a42011-09-23 00:58:33 +00001805// Return next O32 integer argument register.
1806static unsigned getNextIntArgReg(unsigned Reg) {
1807 assert((Reg == Mips::A0) || (Reg == Mips::A2));
1808 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
1809}
1810
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001811// Write ByVal Arg to arg registers and stack.
1812static void
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001813WriteByValArg(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001814 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1815 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1816 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001817 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001818 MVT PtrType, bool isLittle) {
1819 unsigned LocMemOffset = VA.getLocMemOffset();
1820 unsigned Offset = 0;
1821 uint32_t RemainingSize = Flags.getByValSize();
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001822 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001823
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001824 // Copy the first 4 words of byval arg to registers A0 - A3.
1825 // FIXME: Use a stricter alignment if it enables better optimization in passes
1826 // run later.
1827 for (; RemainingSize >= 4 && LocMemOffset < 4 * 4;
1828 Offset += 4, RemainingSize -= 4, LocMemOffset += 4) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001829 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001830 DAG.getConstant(Offset, MVT::i32));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001831 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1832 MachinePointerInfo(),
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001833 false, false, std::min(ByValAlign,
1834 (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001835 MemOpChains.push_back(LoadVal.getValue(1));
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001836 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001837 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1838 }
1839
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001840 if (RemainingSize == 0)
1841 return;
1842
1843 // If there still is a register available for argument passing, write the
1844 // remaining part of the structure to it using subword loads and shifts.
1845 if (LocMemOffset < 4 * 4) {
1846 assert(RemainingSize <= 3 && RemainingSize >= 1 &&
1847 "There must be one to three bytes remaining.");
1848 unsigned LoadSize = (RemainingSize == 3 ? 2 : RemainingSize);
1849 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1850 DAG.getConstant(Offset, MVT::i32));
1851 unsigned Alignment = std::min(ByValAlign, (unsigned )4);
1852 SDValue LoadVal = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
1853 LoadPtr, MachinePointerInfo(),
1854 MVT::getIntegerVT(LoadSize * 8), false,
1855 false, Alignment);
1856 MemOpChains.push_back(LoadVal.getValue(1));
1857
1858 // If target is big endian, shift it to the most significant half-word or
1859 // byte.
1860 if (!isLittle)
1861 LoadVal = DAG.getNode(ISD::SHL, dl, MVT::i32, LoadVal,
1862 DAG.getConstant(32 - LoadSize * 8, MVT::i32));
1863
1864 Offset += LoadSize;
1865 RemainingSize -= LoadSize;
1866
1867 // Read second subword if necessary.
1868 if (RemainingSize != 0) {
1869 assert(RemainingSize == 1 && "There must be one byte remaining.");
1870 LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1871 DAG.getConstant(Offset, MVT::i32));
1872 unsigned Alignment = std::min(ByValAlign, (unsigned )2);
1873 SDValue Subword = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
1874 LoadPtr, MachinePointerInfo(),
1875 MVT::i8, false, false, Alignment);
1876 MemOpChains.push_back(Subword.getValue(1));
1877 // Insert the loaded byte to LoadVal.
1878 // FIXME: Use INS if supported by target.
1879 unsigned ShiftAmt = isLittle ? 16 : 8;
1880 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i32, Subword,
1881 DAG.getConstant(ShiftAmt, MVT::i32));
1882 LoadVal = DAG.getNode(ISD::OR, dl, MVT::i32, LoadVal, Shift);
1883 }
1884
1885 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
1886 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1887 return;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001888 }
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001889
1890 // Create a fixed object on stack at offset LocMemOffset and copy
1891 // remaining part of byval arg to it using memcpy.
1892 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1893 DAG.getConstant(Offset, MVT::i32));
1894 LastFI = MFI->CreateFixedObject(RemainingSize, LocMemOffset, true);
1895 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001896 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
1897 DAG.getConstant(RemainingSize, MVT::i32),
1898 std::min(ByValAlign, (unsigned)4),
1899 /*isVolatile=*/false, /*AlwaysInline=*/false,
1900 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001901}
1902
Dan Gohman98ca4f22009-08-05 01:29:28 +00001903/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001904/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001905/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001906SDValue
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001907MipsTargetLowering::LowerCall(SDValue InChain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001908 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001909 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001910 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001911 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001912 const SmallVectorImpl<ISD::InputArg> &Ins,
1913 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001914 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001915 // MIPs target does not yet support tail call optimization.
1916 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001917
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001918 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001919 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00001920 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001921 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001922 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001923
1924 // Analyze operands of the call, assigning locations to each operand.
1925 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001926 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1927 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001928
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001929 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001930 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001931 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001932 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001933
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001934 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001935 unsigned NextStackOffset = CCInfo.getNextStackOffset();
1936
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001937 // Chain is the output chain of the last Load/Store or CopyToReg node.
1938 // ByValChain is the output chain of the last Memcpy node created for copying
1939 // byval arguments to the stack.
1940 SDValue Chain, CallSeqStart, ByValChain;
1941 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
1942 Chain = CallSeqStart = DAG.getCALLSEQ_START(InChain, NextStackOffsetVal);
1943 ByValChain = InChain;
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001944
1945 // If this is the first call, create a stack frame object that points to
1946 // a location to which .cprestore saves $gp.
1947 if (IsPIC && !MipsFI->getGPFI())
1948 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
1949
Akira Hatanaka21afc632011-06-21 00:40:49 +00001950 // Get the frame index of the stack frame object that points to the location
1951 // of dynamically allocated area on the stack.
1952 int DynAllocFI = MipsFI->getDynAllocFI();
1953
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001954 // Update size of the maximum argument space.
1955 // For O32, a minimum of four words (16 bytes) of argument space is
1956 // allocated.
1957 if (Subtarget->isABI_O32())
1958 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
1959
1960 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1961
1962 if (MaxCallFrameSize < NextStackOffset) {
1963 MipsFI->setMaxCallFrameSize(NextStackOffset);
1964
Akira Hatanaka21afc632011-06-21 00:40:49 +00001965 // Set the offsets relative to $sp of the $gp restore slot and dynamically
1966 // allocated stack space. These offsets must be aligned to a boundary
1967 // determined by the stack alignment of the ABI.
1968 unsigned StackAlignment = TFL->getStackAlignment();
1969 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
1970 StackAlignment * StackAlignment;
1971
1972 if (IsPIC)
1973 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
1974
1975 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001976 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001977
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001978 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001979 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1980 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001981
Eric Christopher471e4222011-06-08 23:55:35 +00001982 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00001983
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001984 // Walk the register/memloc assignments, inserting copies/loads.
1985 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001986 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001987 CCValAssign &VA = ArgLocs[i];
1988
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001989 // Promote the value if needed.
1990 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001991 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001992 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001993 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001994 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001995 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001996 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001997 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1998 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001999 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2000 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002001 if (!Subtarget->isLittle())
2002 std::swap(Lo, Hi);
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002003 unsigned LocRegLo = VA.getLocReg();
2004 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2005 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2006 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002007 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002008 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002009 }
2010 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002011 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00002012 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002013 break;
2014 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00002015 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002016 break;
2017 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00002018 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002019 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002020 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002021
2022 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002023 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002024 if (VA.isRegLoc()) {
2025 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002026 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002027 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002028
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002029 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002030 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002031
Eric Christopher471e4222011-06-08 23:55:35 +00002032 // ByVal Arg.
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002033 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2034 if (Flags.isByVal()) {
2035 assert(Subtarget->isABI_O32() &&
2036 "No support for ByVal args by ABIs other than O32 yet.");
2037 assert(Flags.getByValSize() &&
2038 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002039 WriteByValArg(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI, MFI,
2040 DAG, Arg, VA, Flags, getPointerTy(), Subtarget->isLittle());
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002041 continue;
2042 }
2043
Chris Lattnere0b12152008-03-17 06:57:02 +00002044 // Create the frame index object for this incoming parameter
Eric Christopher471e4222011-06-08 23:55:35 +00002045 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002046 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00002047 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00002048
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002049 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002050 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00002051 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
2052 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00002053 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002054 }
2055
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002056 // Extend range of indices of frame objects for outgoing arguments that were
2057 // created during this function call. Skip this step if no such objects were
2058 // created.
2059 if (LastFI)
2060 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2061
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002062 // If a memcpy has been created to copy a byval arg to a stack, replace the
2063 // chain input of CallSeqStart with ByValChain.
2064 if (InChain != ByValChain)
2065 DAG.UpdateNodeOperands(CallSeqStart.getNode(), ByValChain,
2066 NextStackOffsetVal);
2067
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002068 // Transform all store nodes into one single node because all store
2069 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002070 if (!MemOpChains.empty())
2071 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002072 &MemOpChains[0], MemOpChains.size());
2073
Bill Wendling056292f2008-09-16 21:48:12 +00002074 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002075 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2076 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002077 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002078 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002079 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002080
2081 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002082 if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
2083 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2084 getPointerTy(), 0,MipsII:: MO_GOT);
2085 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
2086 0, MipsII::MO_ABS_LO);
2087 } else {
2088 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2089 getPointerTy(), 0, OpFlag);
2090 }
2091
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002092 LoadSymAddr = true;
2093 }
2094 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002095 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002096 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002097 LoadSymAddr = true;
2098 }
2099
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002100 SDValue InFlag;
2101
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002102 // Create nodes that load address of callee and copy it to T9
2103 if (IsPIC) {
2104 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002105 // Load callee address
Akira Hatanaka342837d2011-05-28 01:07:07 +00002106 Callee = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, Callee);
Akira Hatanaka25eba392011-06-24 19:01:25 +00002107 SDValue LoadValue = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), Callee,
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002108 MachinePointerInfo::getGOT(),
2109 false, false, 0);
2110
2111 // Use GOT+LO if callee has internal linkage.
2112 if (CalleeLo.getNode()) {
2113 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
2114 Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
2115 } else
2116 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002117 }
2118
2119 // copy to T9
2120 Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
2121 InFlag = Chain.getValue(1);
2122 Callee = DAG.getRegister(Mips::T9, MVT::i32);
2123 }
Bill Wendling056292f2008-09-16 21:48:12 +00002124
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002125 // Build a sequence of copy-to-reg nodes chained together with token
2126 // chain and flag operands which copy the outgoing args into registers.
2127 // The InFlag in necessary since all emitted instructions must be
2128 // stuck together.
2129 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2130 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2131 RegsToPass[i].second, InFlag);
2132 InFlag = Chain.getValue(1);
2133 }
2134
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002135 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002136 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002137 //
2138 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002139 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002140 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002141 Ops.push_back(Chain);
2142 Ops.push_back(Callee);
2143
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002144 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002145 // known live into the call.
2146 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2147 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2148 RegsToPass[i].second.getValueType()));
2149
Gabor Greifba36cb52008-08-28 21:40:38 +00002150 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002151 Ops.push_back(InFlag);
2152
Dale Johannesen33c960f2009-02-04 20:06:27 +00002153 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002154 InFlag = Chain.getValue(1);
2155
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002156 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002157 Chain = DAG.getCALLSEQ_END(Chain,
2158 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002159 DAG.getIntPtrConstant(0, true), InFlag);
2160 InFlag = Chain.getValue(1);
2161
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002162 // Handle result values, copying them out of physregs into vregs that we
2163 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002164 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2165 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002166}
2167
Dan Gohman98ca4f22009-08-05 01:29:28 +00002168/// LowerCallResult - Lower the result values of a call into the
2169/// appropriate copies out of appropriate physical registers.
2170SDValue
2171MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002172 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002173 const SmallVectorImpl<ISD::InputArg> &Ins,
2174 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002175 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002176 // Assign locations to each value returned by this call.
2177 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002178 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2179 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002180
Dan Gohman98ca4f22009-08-05 01:29:28 +00002181 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002182
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002183 // Copy all of the result registers out of their specified physreg.
2184 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002185 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002186 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002187 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002188 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002189 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002190
Dan Gohman98ca4f22009-08-05 01:29:28 +00002191 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002192}
2193
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002194//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002195// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002196//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002197static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2198 std::vector<SDValue>& OutChains,
2199 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2200 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2201 unsigned LocMem = VA.getLocMemOffset();
2202 unsigned FirstWord = LocMem / 4;
2203
2204 // copy register A0 - A3 to frame object
2205 for (unsigned i = 0; i < NumWords; ++i) {
2206 unsigned CurWord = FirstWord + i;
2207 if (CurWord >= O32IntRegsSize)
2208 break;
2209
2210 unsigned SrcReg = O32IntRegs[CurWord];
2211 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2212 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2213 DAG.getConstant(i * 4, MVT::i32));
2214 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2215 StorePtr, MachinePointerInfo(), false,
2216 false, 0);
2217 OutChains.push_back(Store);
2218 }
2219}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002220
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002221/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002222/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002223SDValue
2224MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002225 CallingConv::ID CallConv,
2226 bool isVarArg,
2227 const SmallVectorImpl<ISD::InputArg>
2228 &Ins,
2229 DebugLoc dl, SelectionDAG &DAG,
2230 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002231 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002232 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002233 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002234 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002235
Dan Gohman1e93df62010-04-17 14:41:14 +00002236 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002237
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002238 // Used with vargs to acumulate store chains.
2239 std::vector<SDValue> OutChains;
2240
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002241 // Assign locations to all of the incoming arguments.
2242 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002243 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2244 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002245
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002246 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002247 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002248 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002249 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002250
Akira Hatanaka43299772011-05-20 23:22:14 +00002251 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002252
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002253 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002254 CCValAssign &VA = ArgLocs[i];
2255
2256 // Arguments stored on registers
2257 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002258 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002259 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002260 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002261
Owen Anderson825b72b2009-08-11 20:47:22 +00002262 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002263 RC = Mips::CPURegsRegisterClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002264 else if (RegVT == MVT::i64)
2265 RC = Mips::CPU64RegsRegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002266 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002267 RC = Mips::FGR32RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002268 else if (RegVT == MVT::f64)
Akira Hatanakaf40de9d2011-09-26 21:55:17 +00002269 RC = HasMips64 ? Mips::FGR64RegisterClass : Mips::AFGR64RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002270 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002271 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002272
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002273 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002274 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002275 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002276 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002277
2278 // If this is an 8 or 16-bit value, it has been passed promoted
2279 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002280 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002281 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002282 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002283 if (VA.getLocInfo() == CCValAssign::SExt)
2284 Opcode = ISD::AssertSext;
2285 else if (VA.getLocInfo() == CCValAssign::ZExt)
2286 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002287 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002288 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00002289 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00002290 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002291 }
2292
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002293 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002294 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002295 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
2296 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00002297 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002298 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002299 getNextIntArgReg(ArgReg), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002300 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002301 if (!Subtarget->isLittle())
2302 std::swap(ArgValue, ArgValue2);
2303 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2304 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002305 }
2306 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002307
Dan Gohman98ca4f22009-08-05 01:29:28 +00002308 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002309 } else { // VA.isRegLoc()
2310
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002311 // sanity check
2312 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002313
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002314 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2315
2316 if (Flags.isByVal()) {
2317 assert(Subtarget->isABI_O32() &&
2318 "No support for ByVal args by ABIs other than O32 yet.");
2319 assert(Flags.getByValSize() &&
2320 "ByVal args of size 0 should have been ignored by front-end.");
2321 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2322 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2323 true);
2324 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2325 InVals.push_back(FIN);
2326 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2327
2328 continue;
2329 }
2330
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002331 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002332 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
2333 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002334
2335 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002336 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002337 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002338 MachinePointerInfo::getFixedStack(LastFI),
David Greenef6fa1862010-02-15 16:56:10 +00002339 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002340 }
2341 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002342
2343 // The mips ABIs for returning structs by value requires that we copy
2344 // the sret argument into $v0 for the return. Save the argument into
2345 // a virtual register so that we can access it from the return points.
2346 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2347 unsigned Reg = MipsFI->getSRetReturnReg();
2348 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002349 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002350 MipsFI->setSRetReturnReg(Reg);
2351 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002352 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002353 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002354 }
2355
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002356 if (isVarArg && Subtarget->isABI_O32()) {
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002357 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002358 // which is a value necessary to VASTART.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002359 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002360 assert(NextStackOffset % 4 == 0 &&
2361 "NextStackOffset must be aligned to 4-byte boundaries.");
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002362 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
2363 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002364
2365 // If NextStackOffset is smaller than o32's 16-byte reserved argument area,
2366 // copy the integer registers that have not been used for argument passing
2367 // to the caller's stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002368 for (; NextStackOffset < 16; NextStackOffset += 4) {
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002369 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002370 unsigned Idx = NextStackOffset / 4;
2371 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), O32IntRegs[Idx], RC);
2372 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
Akira Hatanaka69c19f72011-05-23 20:16:59 +00002373 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002374 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2375 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2376 MachinePointerInfo(),
2377 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002378 }
2379 }
2380
Akira Hatanaka43299772011-05-20 23:22:14 +00002381 MipsFI->setLastInArgFI(LastFI);
2382
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002383 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002384 // the size of Ins and InVals. This only happens when on varg functions
2385 if (!OutChains.empty()) {
2386 OutChains.push_back(Chain);
2387 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2388 &OutChains[0], OutChains.size());
2389 }
2390
Dan Gohman98ca4f22009-08-05 01:29:28 +00002391 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002392}
2393
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002394//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002395// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002396//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002397
Dan Gohman98ca4f22009-08-05 01:29:28 +00002398SDValue
2399MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002400 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002401 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002402 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002403 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002404
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002405 // CCValAssign - represent the assignment of
2406 // the return value to a location
2407 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002408
2409 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002410 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2411 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002412
Dan Gohman98ca4f22009-08-05 01:29:28 +00002413 // Analize return values.
2414 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002415
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002416 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002417 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002418 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002419 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002420 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002421 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002422 }
2423
Dan Gohman475871a2008-07-27 21:46:04 +00002424 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002425
2426 // Copy the result values into the output registers.
2427 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2428 CCValAssign &VA = RVLocs[i];
2429 assert(VA.isRegLoc() && "Can only return in registers!");
2430
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002431 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002432 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002433
2434 // guarantee that all emitted copies are
2435 // stuck together, avoiding something bad
2436 Flag = Chain.getValue(1);
2437 }
2438
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002439 // The mips ABIs for returning structs by value requires that we copy
2440 // the sret argument into $v0 for the return. We saved the argument into
2441 // a virtual register in the entry block, so now we copy the value out
2442 // and into $v0.
2443 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2444 MachineFunction &MF = DAG.getMachineFunction();
2445 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2446 unsigned Reg = MipsFI->getSRetReturnReg();
2447
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002448 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002449 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002450 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002451
Dale Johannesena05dca42009-02-04 23:02:30 +00002452 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002453 Flag = Chain.getValue(1);
2454 }
2455
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002456 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002457 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002458 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002459 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002460 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002461 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002462 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002463}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002464
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002465//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002466// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002467//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002468
2469/// getConstraintType - Given a constraint letter, return the type of
2470/// constraint it is for this target.
2471MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002472getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002473{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002474 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002475 // GCC config/mips/constraints.md
2476 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002477 // 'd' : An address register. Equivalent to r
2478 // unless generating MIPS16 code.
2479 // 'y' : Equivalent to r; retained for
2480 // backwards compatibility.
2481 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002482 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002483 switch (Constraint[0]) {
2484 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002485 case 'd':
2486 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002487 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002488 return C_RegisterClass;
2489 break;
2490 }
2491 }
2492 return TargetLowering::getConstraintType(Constraint);
2493}
2494
John Thompson44ab89e2010-10-29 17:29:13 +00002495/// Examine constraint type and operand type and determine a weight value.
2496/// This object must already have been set up with the operand type
2497/// and the current alternative constraint selected.
2498TargetLowering::ConstraintWeight
2499MipsTargetLowering::getSingleConstraintMatchWeight(
2500 AsmOperandInfo &info, const char *constraint) const {
2501 ConstraintWeight weight = CW_Invalid;
2502 Value *CallOperandVal = info.CallOperandVal;
2503 // If we don't have a value, we can't do a match,
2504 // but allow it at the lowest weight.
2505 if (CallOperandVal == NULL)
2506 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002507 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002508 // Look at the constraint type.
2509 switch (*constraint) {
2510 default:
2511 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2512 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002513 case 'd':
2514 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002515 if (type->isIntegerTy())
2516 weight = CW_Register;
2517 break;
2518 case 'f':
2519 if (type->isFloatTy())
2520 weight = CW_Register;
2521 break;
2522 }
2523 return weight;
2524}
2525
Eric Christopher38d64262011-06-29 19:33:04 +00002526/// Given a register class constraint, like 'r', if this corresponds directly
2527/// to an LLVM register class, return a register of 0 and the register class
2528/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002529std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002530getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002531{
2532 if (Constraint.size() == 1) {
2533 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002534 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2535 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002536 case 'r':
2537 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002538 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002539 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002540 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002541 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002542 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2543 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002544 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002545 }
2546 }
2547 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2548}
2549
Dan Gohman6520e202008-10-18 02:06:02 +00002550bool
2551MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2552 // The Mips target isn't yet aware of offsets.
2553 return false;
2554}
Evan Chengeb2f9692009-10-27 19:56:55 +00002555
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002556bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2557 if (VT != MVT::f32 && VT != MVT::f64)
2558 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002559 if (Imm.isNegZero())
2560 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002561 return Imm.isZero();
2562}