blob: 405428062611335beaa5c219a4cd3951ba5031e2 [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 {
Dale Johannesen94817572009-02-13 02:34:39 +0000791 DebugLoc dl = MI->getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000792
793 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000794 default:
795 assert(false && "Unexpected instr type to insert");
796 return NULL;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000797 case Mips::ATOMIC_LOAD_ADD_I8:
798 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
799 case Mips::ATOMIC_LOAD_ADD_I16:
800 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
801 case Mips::ATOMIC_LOAD_ADD_I32:
802 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
803
804 case Mips::ATOMIC_LOAD_AND_I8:
805 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
806 case Mips::ATOMIC_LOAD_AND_I16:
807 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
808 case Mips::ATOMIC_LOAD_AND_I32:
809 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
810
811 case Mips::ATOMIC_LOAD_OR_I8:
812 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
813 case Mips::ATOMIC_LOAD_OR_I16:
814 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
815 case Mips::ATOMIC_LOAD_OR_I32:
816 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
817
818 case Mips::ATOMIC_LOAD_XOR_I8:
819 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
820 case Mips::ATOMIC_LOAD_XOR_I16:
821 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
822 case Mips::ATOMIC_LOAD_XOR_I32:
823 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
824
825 case Mips::ATOMIC_LOAD_NAND_I8:
826 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
827 case Mips::ATOMIC_LOAD_NAND_I16:
828 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
829 case Mips::ATOMIC_LOAD_NAND_I32:
830 return EmitAtomicBinary(MI, BB, 4, 0, true);
831
832 case Mips::ATOMIC_LOAD_SUB_I8:
833 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
834 case Mips::ATOMIC_LOAD_SUB_I16:
835 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
836 case Mips::ATOMIC_LOAD_SUB_I32:
837 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
838
839 case Mips::ATOMIC_SWAP_I8:
840 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
841 case Mips::ATOMIC_SWAP_I16:
842 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
843 case Mips::ATOMIC_SWAP_I32:
844 return EmitAtomicBinary(MI, BB, 4, 0);
845
846 case Mips::ATOMIC_CMP_SWAP_I8:
847 return EmitAtomicCmpSwapPartword(MI, BB, 1);
848 case Mips::ATOMIC_CMP_SWAP_I16:
849 return EmitAtomicCmpSwapPartword(MI, BB, 2);
850 case Mips::ATOMIC_CMP_SWAP_I32:
851 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000852 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000853}
854
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000855// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
856// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
857MachineBasicBlock *
858MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000859 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000860 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000861 assert(Size == 4 && "Unsupported size for EmitAtomicBinary.");
862
863 MachineFunction *MF = BB->getParent();
864 MachineRegisterInfo &RegInfo = MF->getRegInfo();
865 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
866 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
867 DebugLoc dl = MI->getDebugLoc();
868
Akira Hatanaka4061da12011-07-19 20:11:17 +0000869 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000870 unsigned Ptr = MI->getOperand(1).getReg();
871 unsigned Incr = MI->getOperand(2).getReg();
872
Akira Hatanaka4061da12011-07-19 20:11:17 +0000873 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
874 unsigned AndRes = RegInfo.createVirtualRegister(RC);
875 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000876
877 // insert new blocks after the current block
878 const BasicBlock *LLVM_BB = BB->getBasicBlock();
879 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
880 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
881 MachineFunction::iterator It = BB;
882 ++It;
883 MF->insert(It, loopMBB);
884 MF->insert(It, exitMBB);
885
886 // Transfer the remainder of BB and its successor edges to exitMBB.
887 exitMBB->splice(exitMBB->begin(), BB,
888 llvm::next(MachineBasicBlock::iterator(MI)),
889 BB->end());
890 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
891
892 // thisMBB:
893 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000894 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000895 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000896 loopMBB->addSuccessor(loopMBB);
897 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000898
899 // loopMBB:
900 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000901 // <binop> storeval, oldval, incr
902 // sc success, storeval, 0(ptr)
903 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000904 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000905 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000906 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000907 // and andres, oldval, incr
908 // nor storeval, $0, andres
909 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr);
910 BuildMI(BB, dl, TII->get(Mips::NOR), StoreVal)
911 .addReg(Mips::ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000912 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000913 // <binop> storeval, oldval, incr
914 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000915 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000916 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000917 }
Akira Hatanaka4061da12011-07-19 20:11:17 +0000918 BuildMI(BB, dl, TII->get(Mips::SC), Success)
919 .addReg(StoreVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000920 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +0000921 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000922
923 MI->eraseFromParent(); // The instruction is gone now.
924
Akira Hatanaka939ece12011-07-19 03:42:13 +0000925 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000926}
927
928MachineBasicBlock *
929MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000930 MachineBasicBlock *BB,
931 unsigned Size, unsigned BinOpcode,
932 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000933 assert((Size == 1 || Size == 2) &&
934 "Unsupported size for EmitAtomicBinaryPartial.");
935
936 MachineFunction *MF = BB->getParent();
937 MachineRegisterInfo &RegInfo = MF->getRegInfo();
938 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
939 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
940 DebugLoc dl = MI->getDebugLoc();
941
942 unsigned Dest = MI->getOperand(0).getReg();
943 unsigned Ptr = MI->getOperand(1).getReg();
944 unsigned Incr = MI->getOperand(2).getReg();
945
Akira Hatanaka4061da12011-07-19 20:11:17 +0000946 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
947 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000948 unsigned Mask = RegInfo.createVirtualRegister(RC);
949 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000950 unsigned NewVal = RegInfo.createVirtualRegister(RC);
951 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000952 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000953 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
954 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
955 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
956 unsigned AndRes = RegInfo.createVirtualRegister(RC);
957 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +0000958 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000959 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
960 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
961 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
962 unsigned SllRes = RegInfo.createVirtualRegister(RC);
963 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000964
965 // insert new blocks after the current block
966 const BasicBlock *LLVM_BB = BB->getBasicBlock();
967 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000968 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000969 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
970 MachineFunction::iterator It = BB;
971 ++It;
972 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000973 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000974 MF->insert(It, exitMBB);
975
976 // Transfer the remainder of BB and its successor edges to exitMBB.
977 exitMBB->splice(exitMBB->begin(), BB,
978 llvm::next(MachineBasicBlock::iterator(MI)),
979 BB->end());
980 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
981
Akira Hatanaka81b44112011-07-19 17:09:53 +0000982 BB->addSuccessor(loopMBB);
983 loopMBB->addSuccessor(loopMBB);
984 loopMBB->addSuccessor(sinkMBB);
985 sinkMBB->addSuccessor(exitMBB);
986
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000987 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +0000988 // addiu masklsb2,$0,-4 # 0xfffffffc
989 // and alignedaddr,ptr,masklsb2
990 // andi ptrlsb2,ptr,3
991 // sll shiftamt,ptrlsb2,3
992 // ori maskupper,$0,255 # 0xff
993 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000994 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +0000995 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000996
997 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000998 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
999 .addReg(Mips::ZERO).addImm(-4);
1000 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1001 .addReg(Ptr).addReg(MaskLSB2);
1002 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1003 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1004 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1005 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001006 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1007 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001008 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001009 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001010
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001011
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001012 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001013 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001014 // ll oldval,0(alignedaddr)
1015 // binop binopres,oldval,incr2
1016 // and newval,binopres,mask
1017 // and maskedoldval0,oldval,mask2
1018 // or storeval,maskedoldval0,newval
1019 // sc success,storeval,0(alignedaddr)
1020 // beq success,$0,loopMBB
1021
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001022 // atomic.swap
1023 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001024 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001025 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001026 // and maskedoldval0,oldval,mask2
1027 // or storeval,maskedoldval0,newval
1028 // sc success,storeval,0(alignedaddr)
1029 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001030
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001031 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001032 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001033 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001034 // and andres, oldval, incr2
1035 // nor binopres, $0, andres
1036 // and newval, binopres, mask
1037 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1038 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1039 .addReg(Mips::ZERO).addReg(AndRes);
1040 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001041 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001042 // <binop> binopres, oldval, incr2
1043 // and newval, binopres, mask
1044 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1045 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001046 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001047 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001048 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001049 }
1050
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001051 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001052 .addReg(OldVal).addReg(Mask2);
1053 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001054 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001055 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1056 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001057 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001058 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001059
Akira Hatanaka939ece12011-07-19 03:42:13 +00001060 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001061 // and maskedoldval1,oldval,mask
1062 // srl srlres,maskedoldval1,shiftamt
1063 // sll sllres,srlres,24
1064 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001065 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001066 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001067
Akira Hatanaka4061da12011-07-19 20:11:17 +00001068 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1069 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001070 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1071 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001072 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1073 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001074 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001075 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001076
1077 MI->eraseFromParent(); // The instruction is gone now.
1078
Akira Hatanaka939ece12011-07-19 03:42:13 +00001079 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001080}
1081
1082MachineBasicBlock *
1083MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001084 MachineBasicBlock *BB,
1085 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001086 assert(Size == 4 && "Unsupported size for EmitAtomicCmpSwap.");
1087
1088 MachineFunction *MF = BB->getParent();
1089 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1090 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1091 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1092 DebugLoc dl = MI->getDebugLoc();
1093
1094 unsigned Dest = MI->getOperand(0).getReg();
1095 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001096 unsigned OldVal = MI->getOperand(2).getReg();
1097 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001098
Akira Hatanaka4061da12011-07-19 20:11:17 +00001099 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001100
1101 // insert new blocks after the current block
1102 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1103 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1104 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1105 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1106 MachineFunction::iterator It = BB;
1107 ++It;
1108 MF->insert(It, loop1MBB);
1109 MF->insert(It, loop2MBB);
1110 MF->insert(It, exitMBB);
1111
1112 // Transfer the remainder of BB and its successor edges to exitMBB.
1113 exitMBB->splice(exitMBB->begin(), BB,
1114 llvm::next(MachineBasicBlock::iterator(MI)),
1115 BB->end());
1116 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1117
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001118 // thisMBB:
1119 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001120 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001121 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001122 loop1MBB->addSuccessor(exitMBB);
1123 loop1MBB->addSuccessor(loop2MBB);
1124 loop2MBB->addSuccessor(loop1MBB);
1125 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001126
1127 // loop1MBB:
1128 // ll dest, 0(ptr)
1129 // bne dest, oldval, exitMBB
1130 BB = loop1MBB;
Akira Hatanakad3ac47f2011-07-07 18:57:00 +00001131 BuildMI(BB, dl, TII->get(Mips::LL), Dest).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001132 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001133 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001134
1135 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001136 // sc success, newval, 0(ptr)
1137 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001138 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001139 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1140 .addReg(NewVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001141 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001142 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001143
1144 MI->eraseFromParent(); // The instruction is gone now.
1145
Akira Hatanaka939ece12011-07-19 03:42:13 +00001146 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001147}
1148
1149MachineBasicBlock *
1150MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001151 MachineBasicBlock *BB,
1152 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001153 assert((Size == 1 || Size == 2) &&
1154 "Unsupported size for EmitAtomicCmpSwapPartial.");
1155
1156 MachineFunction *MF = BB->getParent();
1157 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1158 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1159 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1160 DebugLoc dl = MI->getDebugLoc();
1161
1162 unsigned Dest = MI->getOperand(0).getReg();
1163 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001164 unsigned CmpVal = MI->getOperand(2).getReg();
1165 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001166
Akira Hatanaka4061da12011-07-19 20:11:17 +00001167 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1168 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001169 unsigned Mask = RegInfo.createVirtualRegister(RC);
1170 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001171 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1172 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1173 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1174 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1175 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1176 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1177 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1178 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1179 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1180 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1181 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1182 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1183 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1184 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001185
1186 // insert new blocks after the current block
1187 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1188 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1189 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001190 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001191 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1192 MachineFunction::iterator It = BB;
1193 ++It;
1194 MF->insert(It, loop1MBB);
1195 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001196 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001197 MF->insert(It, exitMBB);
1198
1199 // Transfer the remainder of BB and its successor edges to exitMBB.
1200 exitMBB->splice(exitMBB->begin(), BB,
1201 llvm::next(MachineBasicBlock::iterator(MI)),
1202 BB->end());
1203 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1204
Akira Hatanaka81b44112011-07-19 17:09:53 +00001205 BB->addSuccessor(loop1MBB);
1206 loop1MBB->addSuccessor(sinkMBB);
1207 loop1MBB->addSuccessor(loop2MBB);
1208 loop2MBB->addSuccessor(loop1MBB);
1209 loop2MBB->addSuccessor(sinkMBB);
1210 sinkMBB->addSuccessor(exitMBB);
1211
Akira Hatanaka70564a92011-07-19 18:14:26 +00001212 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001213 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001214 // addiu masklsb2,$0,-4 # 0xfffffffc
1215 // and alignedaddr,ptr,masklsb2
1216 // andi ptrlsb2,ptr,3
1217 // sll shiftamt,ptrlsb2,3
1218 // ori maskupper,$0,255 # 0xff
1219 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001220 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001221 // andi maskedcmpval,cmpval,255
1222 // sll shiftedcmpval,maskedcmpval,shiftamt
1223 // andi maskednewval,newval,255
1224 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001225 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001226 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1227 .addReg(Mips::ZERO).addImm(-4);
1228 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1229 .addReg(Ptr).addReg(MaskLSB2);
1230 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1231 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1232 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1233 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001234 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1235 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001236 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001237 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1238 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001239 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1240 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001241 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1242 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001243 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1244 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001245
1246 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001247 // ll oldval,0(alginedaddr)
1248 // and maskedoldval0,oldval,mask
1249 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001250 BB = loop1MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001251 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
1252 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1253 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001254 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001255 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001256
1257 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001258 // and maskedoldval1,oldval,mask2
1259 // or storeval,maskedoldval1,shiftednewval
1260 // sc success,storeval,0(alignedaddr)
1261 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001262 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001263 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1264 .addReg(OldVal).addReg(Mask2);
1265 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1266 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1267 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1268 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001269 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001270 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001271
Akira Hatanaka939ece12011-07-19 03:42:13 +00001272 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001273 // srl srlres,maskedoldval0,shiftamt
1274 // sll sllres,srlres,24
1275 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001276 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001277 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001278
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001279 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1280 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001281 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1282 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001283 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001284 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001285
1286 MI->eraseFromParent(); // The instruction is gone now.
1287
Akira Hatanaka939ece12011-07-19 03:42:13 +00001288 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001289}
1290
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001291//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001292// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001293//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001294SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001295LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001296{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001297 MachineFunction &MF = DAG.getMachineFunction();
1298 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1299
1300 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001301 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1302 "Cannot lower if the alignment of the allocated space is larger than \
1303 that of the stack.");
1304
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001305 SDValue Chain = Op.getOperand(0);
1306 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001307 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001308
1309 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +00001310 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001311
1312 // Subtract the dynamic size from the actual stack size to
1313 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +00001314 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001315
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001316 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001317 // must be placed in the stack pointer register.
Akira Hatanaka053546c2011-05-25 02:20:00 +00001318 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub,
1319 SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001320
1321 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001322 // value and a chain
Akira Hatanaka21afc632011-06-21 00:40:49 +00001323 SDVTList VTLs = DAG.getVTList(MVT::i32, MVT::Other);
1324 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1325 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1326
1327 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001328}
1329
1330SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001331LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001332{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001333 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001334 // the block to branch to if the condition is true.
1335 SDValue Chain = Op.getOperand(0);
1336 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001337 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001338
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001339 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1340
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001341 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001342 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001343 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001344
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001345 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001346 Mips::CondCode CC =
1347 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001348 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001349
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001350 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001351 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001352}
1353
1354SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001355LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001356{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001357 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001358
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001359 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001360 if (Cond.getOpcode() != MipsISD::FPCmp)
1361 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001362
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001363 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1364 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001365}
1366
Dan Gohmand858e902010-04-17 15:26:15 +00001367SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1368 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001369 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001370 DebugLoc dl = Op.getDebugLoc();
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001371 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001372
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001373 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001374 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001375
Chris Lattnerb71b9092009-08-13 06:28:06 +00001376 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001377
Chris Lattnere3736f82009-08-13 05:41:27 +00001378 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001379 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1380 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001381 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001382 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1383 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001384 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001385 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001386 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001387 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1388 MipsII::MO_ABS_HI);
1389 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1390 MipsII::MO_ABS_LO);
1391 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1392 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001393 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001394 }
1395
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001396 EVT ValTy = Op.getValueType();
1397 bool HasGotOfst = (GV->hasInternalLinkage() ||
1398 (GV->hasLocalLinkage() && !isa<Function>(GV)));
1399 unsigned GotFlag = IsN64 ?
1400 (HasGotOfst ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT_DISP) :
1401 MipsII::MO_GOT;
1402 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0, GotFlag);
1403 GA = DAG.getNode(MipsISD::WrapperPIC, dl, ValTy, GA);
1404 SDValue ResNode = DAG.getLoad(ValTy, dl,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001405 DAG.getEntryNode(), GA, MachinePointerInfo(),
1406 false, false, 0);
1407 // On functions and global targets not internal linked only
1408 // a load from got/GP is necessary for PIC to work.
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001409 if (!HasGotOfst)
Akira Hatanaka0f843822011-06-07 18:58:42 +00001410 return ResNode;
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001411 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0,
1412 IsN64 ? MipsII::MO_GOT_OFST :
1413 MipsII::MO_ABS_LO);
1414 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, GALo);
1415 return DAG.getNode(ISD::ADD, dl, ValTy, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001416}
1417
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001418SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1419 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001420 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1421 // FIXME there isn't actually debug info here
1422 DebugLoc dl = Op.getDebugLoc();
1423
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001424 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001425 // %hi/%lo relocation
1426 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
1427 MipsII::MO_ABS_HI);
1428 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
1429 MipsII::MO_ABS_LO);
1430 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1431 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1432 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001433 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001434
1435 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1436 MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001437 BAGOTOffset = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, BAGOTOffset);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001438 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1439 MipsII::MO_ABS_LO);
1440 SDValue Load = DAG.getLoad(MVT::i32, dl,
1441 DAG.getEntryNode(), BAGOTOffset,
1442 MachinePointerInfo(), false, false, 0);
1443 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
1444 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001445}
1446
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001447SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001448LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001449{
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001450 // If the relocation model is PIC, use the General Dynamic TLS Model,
1451 // otherwise use the Initial Exec or Local Exec TLS Model.
1452 // TODO: implement Local Dynamic TLS model
1453
1454 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1455 DebugLoc dl = GA->getDebugLoc();
1456 const GlobalValue *GV = GA->getGlobal();
1457 EVT PtrVT = getPointerTy();
1458
1459 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1460 // General Dynamic TLS Model
1461 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001462 0, MipsII::MO_TLSGD);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001463 SDValue Tlsgd = DAG.getNode(MipsISD::TlsGd, dl, MVT::i32, TGA);
1464 SDValue GP = DAG.getRegister(Mips::GP, MVT::i32);
1465 SDValue Argument = DAG.getNode(ISD::ADD, dl, MVT::i32, GP, Tlsgd);
1466
1467 ArgListTy Args;
1468 ArgListEntry Entry;
1469 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001470 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001471 Args.push_back(Entry);
1472 std::pair<SDValue, SDValue> CallResult =
1473 LowerCallTo(DAG.getEntryNode(),
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001474 (Type *) Type::getInt32Ty(*DAG.getContext()),
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001475 false, false, false, false, 0, CallingConv::C, false, true,
1476 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG,
1477 dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001478
1479 return CallResult.first;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001480 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001481
1482 SDValue Offset;
1483 if (GV->isDeclaration()) {
1484 // Initial Exec TLS Model
1485 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1486 MipsII::MO_GOTTPREL);
1487 Offset = DAG.getLoad(MVT::i32, dl,
1488 DAG.getEntryNode(), TGA, MachinePointerInfo(),
1489 false, false, 0);
1490 } else {
1491 // Local Exec TLS Model
1492 SDVTList VTs = DAG.getVTList(MVT::i32);
1493 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1494 MipsII::MO_TPREL_HI);
1495 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1496 MipsII::MO_TPREL_LO);
1497 SDValue Hi = DAG.getNode(MipsISD::TprelHi, dl, VTs, &TGAHi, 1);
1498 SDValue Lo = DAG.getNode(MipsISD::TprelLo, dl, MVT::i32, TGALo);
1499 Offset = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1500 }
1501
1502 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1503 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001504}
1505
1506SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001507LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001508{
Dan Gohman475871a2008-07-27 21:46:04 +00001509 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001510 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +00001511 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001512 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001513 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001514 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001515
Owen Andersone50ed302009-08-10 22:56:29 +00001516 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001517 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001518
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001519 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
1520
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +00001521 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001522 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001523 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001524 } else {// Emit Load from Global Pointer
1525 JTI = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, JTI);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001526 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
1527 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001528 false, false, 0);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001529 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001530
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001531 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1532 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001533 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001534 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001535
1536 return ResNode;
1537}
1538
Dan Gohman475871a2008-07-27 21:46:04 +00001539SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001540LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001541{
Dan Gohman475871a2008-07-27 21:46:04 +00001542 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001543 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001544 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001545 // FIXME there isn't actually debug info here
1546 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001547
1548 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001549 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001550 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001551 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001552 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001553 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001554 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1555 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001556 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001557
1558 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001559 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001560 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001561 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001562 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001563 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1564 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001565 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001566 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001567 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001568 N->getOffset(), MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001569 CP = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001570 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001571 CP, MachinePointerInfo::getConstantPool(),
1572 false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001573 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001574 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001575 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001576 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
1577 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001578
1579 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001580}
1581
Dan Gohmand858e902010-04-17 15:26:15 +00001582SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001583 MachineFunction &MF = DAG.getMachineFunction();
1584 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1585
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001586 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001587 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1588 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001589
1590 // vastart just stores the address of the VarArgsFrameIndex slot into the
1591 // memory location argument.
1592 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001593 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1594 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +00001595 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001596}
1597
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001598static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
1599 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
1600 DebugLoc dl = Op.getDebugLoc();
1601 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
1602 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
1603 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
1604 DAG.getConstant(0x7fffffff, MVT::i32));
1605 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
1606 DAG.getConstant(0x80000000, MVT::i32));
1607 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1608 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
1609}
1610
1611static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001612 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001613 // Use ext/ins instructions if target architecture is Mips32r2.
1614 // Eliminate redundant mfc1 and mtc1 instructions.
1615 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001616
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001617 if (!isLittle)
1618 std::swap(LoIdx, HiIdx);
1619
1620 DebugLoc dl = Op.getDebugLoc();
1621 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1622 Op.getOperand(0),
1623 DAG.getConstant(LoIdx, MVT::i32));
1624 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1625 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1626 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1627 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1628 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1629 DAG.getConstant(0x7fffffff, MVT::i32));
1630 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1631 DAG.getConstant(0x80000000, MVT::i32));
1632 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1633
1634 if (!isLittle)
1635 std::swap(Word0, Word1);
1636
1637 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1638}
1639
1640SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1641 const {
1642 EVT Ty = Op.getValueType();
1643
1644 assert(Ty == MVT::f32 || Ty == MVT::f64);
1645
1646 if (Ty == MVT::f32)
1647 return LowerFCOPYSIGN32(Op, DAG);
1648 else
1649 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
1650}
1651
Akira Hatanaka2e591472011-06-02 00:24:44 +00001652SDValue MipsTargetLowering::
1653LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001654 // check the depth
1655 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001656 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001657
1658 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1659 MFI->setFrameAddressIsTaken(true);
1660 EVT VT = Op.getValueType();
1661 DebugLoc dl = Op.getDebugLoc();
1662 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Mips::FP, VT);
1663 return FrameAddr;
1664}
1665
Akira Hatanakadb548262011-07-19 23:30:50 +00001666// TODO: set SType according to the desired memory barrier behavior.
1667SDValue MipsTargetLowering::LowerMEMBARRIER(SDValue Op,
1668 SelectionDAG& DAG) const {
1669 unsigned SType = 0;
1670 DebugLoc dl = Op.getDebugLoc();
1671 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1672 DAG.getConstant(SType, MVT::i32));
1673}
1674
Eli Friedman14648462011-07-27 22:21:52 +00001675SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1676 SelectionDAG& DAG) const {
1677 // FIXME: Need pseudo-fence for 'singlethread' fences
1678 // FIXME: Set SType for weaker fences where supported/appropriate.
1679 unsigned SType = 0;
1680 DebugLoc dl = Op.getDebugLoc();
1681 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1682 DAG.getConstant(SType, MVT::i32));
1683}
1684
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001685//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001686// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001687//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001688
1689#include "MipsGenCallingConv.inc"
1690
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001691//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001692// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001693// Mips O32 ABI rules:
1694// ---
1695// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001696// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001697// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001698// f64 - Only passed in two aliased f32 registers if no int reg has been used
1699// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001700// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1701// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001702//
1703// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001704//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001705
Duncan Sands1e96bab2010-11-04 10:49:57 +00001706static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001707 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001708 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1709
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001710 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001711
1712 static const unsigned IntRegs[] = {
1713 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1714 };
1715 static const unsigned F32Regs[] = {
1716 Mips::F12, Mips::F14
1717 };
1718 static const unsigned F64Regs[] = {
1719 Mips::D6, Mips::D7
1720 };
1721
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001722 // ByVal Args
1723 if (ArgFlags.isByVal()) {
1724 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1725 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1726 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1727 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1728 r < std::min(IntRegsSize, NextReg); ++r)
1729 State.AllocateReg(IntRegs[r]);
1730 return false;
1731 }
1732
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001733 // Promote i8 and i16
1734 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1735 LocVT = MVT::i32;
1736 if (ArgFlags.isSExt())
1737 LocInfo = CCValAssign::SExt;
1738 else if (ArgFlags.isZExt())
1739 LocInfo = CCValAssign::ZExt;
1740 else
1741 LocInfo = CCValAssign::AExt;
1742 }
1743
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001744 unsigned Reg;
1745
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001746 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1747 // is true: function is vararg, argument is 3rd or higher, there is previous
1748 // argument which is not f32 or f64.
1749 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1750 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001751 unsigned OrigAlign = ArgFlags.getOrigAlign();
1752 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001753
1754 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001755 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001756 // If this is the first part of an i64 arg,
1757 // the allocated register must be either A0 or A2.
1758 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1759 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001760 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001761 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1762 // Allocate int register and shadow next int register. If first
1763 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001764 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1765 if (Reg == Mips::A1 || Reg == Mips::A3)
1766 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1767 State.AllocateReg(IntRegs, IntRegsSize);
1768 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001769 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1770 // we are guaranteed to find an available float register
1771 if (ValVT == MVT::f32) {
1772 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1773 // Shadow int register
1774 State.AllocateReg(IntRegs, IntRegsSize);
1775 } else {
1776 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1777 // Shadow int registers
1778 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1779 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1780 State.AllocateReg(IntRegs, IntRegsSize);
1781 State.AllocateReg(IntRegs, IntRegsSize);
1782 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001783 } else
1784 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001785
Akira Hatanakad37776d2011-05-20 21:39:54 +00001786 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1787 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1788
1789 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001790 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001791 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001792 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001793
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001794 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001795}
1796
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001797//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001798// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001799//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001800
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001801static const unsigned O32IntRegsSize = 4;
1802
1803static const unsigned O32IntRegs[] = {
1804 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1805};
1806
Akira Hatanaka373e3a42011-09-23 00:58:33 +00001807// Return next O32 integer argument register.
1808static unsigned getNextIntArgReg(unsigned Reg) {
1809 assert((Reg == Mips::A0) || (Reg == Mips::A2));
1810 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
1811}
1812
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001813// Write ByVal Arg to arg registers and stack.
1814static void
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001815WriteByValArg(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001816 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1817 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1818 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001819 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001820 MVT PtrType, bool isLittle) {
1821 unsigned LocMemOffset = VA.getLocMemOffset();
1822 unsigned Offset = 0;
1823 uint32_t RemainingSize = Flags.getByValSize();
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001824 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001825
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001826 // Copy the first 4 words of byval arg to registers A0 - A3.
1827 // FIXME: Use a stricter alignment if it enables better optimization in passes
1828 // run later.
1829 for (; RemainingSize >= 4 && LocMemOffset < 4 * 4;
1830 Offset += 4, RemainingSize -= 4, LocMemOffset += 4) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001831 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001832 DAG.getConstant(Offset, MVT::i32));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001833 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1834 MachinePointerInfo(),
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001835 false, false, std::min(ByValAlign,
1836 (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001837 MemOpChains.push_back(LoadVal.getValue(1));
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001838 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001839 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1840 }
1841
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001842 if (RemainingSize == 0)
1843 return;
1844
1845 // If there still is a register available for argument passing, write the
1846 // remaining part of the structure to it using subword loads and shifts.
1847 if (LocMemOffset < 4 * 4) {
1848 assert(RemainingSize <= 3 && RemainingSize >= 1 &&
1849 "There must be one to three bytes remaining.");
1850 unsigned LoadSize = (RemainingSize == 3 ? 2 : RemainingSize);
1851 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1852 DAG.getConstant(Offset, MVT::i32));
1853 unsigned Alignment = std::min(ByValAlign, (unsigned )4);
1854 SDValue LoadVal = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
1855 LoadPtr, MachinePointerInfo(),
1856 MVT::getIntegerVT(LoadSize * 8), false,
1857 false, Alignment);
1858 MemOpChains.push_back(LoadVal.getValue(1));
1859
1860 // If target is big endian, shift it to the most significant half-word or
1861 // byte.
1862 if (!isLittle)
1863 LoadVal = DAG.getNode(ISD::SHL, dl, MVT::i32, LoadVal,
1864 DAG.getConstant(32 - LoadSize * 8, MVT::i32));
1865
1866 Offset += LoadSize;
1867 RemainingSize -= LoadSize;
1868
1869 // Read second subword if necessary.
1870 if (RemainingSize != 0) {
1871 assert(RemainingSize == 1 && "There must be one byte remaining.");
1872 LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1873 DAG.getConstant(Offset, MVT::i32));
1874 unsigned Alignment = std::min(ByValAlign, (unsigned )2);
1875 SDValue Subword = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
1876 LoadPtr, MachinePointerInfo(),
1877 MVT::i8, false, false, Alignment);
1878 MemOpChains.push_back(Subword.getValue(1));
1879 // Insert the loaded byte to LoadVal.
1880 // FIXME: Use INS if supported by target.
1881 unsigned ShiftAmt = isLittle ? 16 : 8;
1882 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i32, Subword,
1883 DAG.getConstant(ShiftAmt, MVT::i32));
1884 LoadVal = DAG.getNode(ISD::OR, dl, MVT::i32, LoadVal, Shift);
1885 }
1886
1887 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
1888 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1889 return;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001890 }
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001891
1892 // Create a fixed object on stack at offset LocMemOffset and copy
1893 // remaining part of byval arg to it using memcpy.
1894 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1895 DAG.getConstant(Offset, MVT::i32));
1896 LastFI = MFI->CreateFixedObject(RemainingSize, LocMemOffset, true);
1897 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001898 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
1899 DAG.getConstant(RemainingSize, MVT::i32),
1900 std::min(ByValAlign, (unsigned)4),
1901 /*isVolatile=*/false, /*AlwaysInline=*/false,
1902 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001903}
1904
Dan Gohman98ca4f22009-08-05 01:29:28 +00001905/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001906/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001907/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001908SDValue
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001909MipsTargetLowering::LowerCall(SDValue InChain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001910 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001911 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001912 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001913 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001914 const SmallVectorImpl<ISD::InputArg> &Ins,
1915 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001916 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001917 // MIPs target does not yet support tail call optimization.
1918 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001919
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001920 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001921 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00001922 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001923 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001924 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001925
1926 // Analyze operands of the call, assigning locations to each operand.
1927 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001928 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1929 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001930
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001931 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001932 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001933 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001934 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001935
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001936 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001937 unsigned NextStackOffset = CCInfo.getNextStackOffset();
1938
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001939 // Chain is the output chain of the last Load/Store or CopyToReg node.
1940 // ByValChain is the output chain of the last Memcpy node created for copying
1941 // byval arguments to the stack.
1942 SDValue Chain, CallSeqStart, ByValChain;
1943 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
1944 Chain = CallSeqStart = DAG.getCALLSEQ_START(InChain, NextStackOffsetVal);
1945 ByValChain = InChain;
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001946
1947 // If this is the first call, create a stack frame object that points to
1948 // a location to which .cprestore saves $gp.
1949 if (IsPIC && !MipsFI->getGPFI())
1950 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
1951
Akira Hatanaka21afc632011-06-21 00:40:49 +00001952 // Get the frame index of the stack frame object that points to the location
1953 // of dynamically allocated area on the stack.
1954 int DynAllocFI = MipsFI->getDynAllocFI();
1955
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001956 // Update size of the maximum argument space.
1957 // For O32, a minimum of four words (16 bytes) of argument space is
1958 // allocated.
1959 if (Subtarget->isABI_O32())
1960 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
1961
1962 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1963
1964 if (MaxCallFrameSize < NextStackOffset) {
1965 MipsFI->setMaxCallFrameSize(NextStackOffset);
1966
Akira Hatanaka21afc632011-06-21 00:40:49 +00001967 // Set the offsets relative to $sp of the $gp restore slot and dynamically
1968 // allocated stack space. These offsets must be aligned to a boundary
1969 // determined by the stack alignment of the ABI.
1970 unsigned StackAlignment = TFL->getStackAlignment();
1971 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
1972 StackAlignment * StackAlignment;
1973
1974 if (IsPIC)
1975 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
1976
1977 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001978 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001979
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001980 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001981 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1982 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001983
Eric Christopher471e4222011-06-08 23:55:35 +00001984 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00001985
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001986 // Walk the register/memloc assignments, inserting copies/loads.
1987 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001988 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001989 CCValAssign &VA = ArgLocs[i];
1990
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001991 // Promote the value if needed.
1992 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001993 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001994 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001995 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001996 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001997 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001998 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001999 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2000 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002001 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2002 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002003 if (!Subtarget->isLittle())
2004 std::swap(Lo, Hi);
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002005 unsigned LocRegLo = VA.getLocReg();
2006 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2007 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2008 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002009 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002010 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002011 }
2012 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002013 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00002014 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002015 break;
2016 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00002017 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002018 break;
2019 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00002020 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002021 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002022 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002023
2024 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002025 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002026 if (VA.isRegLoc()) {
2027 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002028 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002029 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002030
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002031 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002032 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002033
Eric Christopher471e4222011-06-08 23:55:35 +00002034 // ByVal Arg.
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002035 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2036 if (Flags.isByVal()) {
2037 assert(Subtarget->isABI_O32() &&
2038 "No support for ByVal args by ABIs other than O32 yet.");
2039 assert(Flags.getByValSize() &&
2040 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002041 WriteByValArg(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI, MFI,
2042 DAG, Arg, VA, Flags, getPointerTy(), Subtarget->isLittle());
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002043 continue;
2044 }
2045
Chris Lattnere0b12152008-03-17 06:57:02 +00002046 // Create the frame index object for this incoming parameter
Eric Christopher471e4222011-06-08 23:55:35 +00002047 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002048 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00002049 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00002050
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002051 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002052 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00002053 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
2054 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00002055 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002056 }
2057
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002058 // Extend range of indices of frame objects for outgoing arguments that were
2059 // created during this function call. Skip this step if no such objects were
2060 // created.
2061 if (LastFI)
2062 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2063
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002064 // If a memcpy has been created to copy a byval arg to a stack, replace the
2065 // chain input of CallSeqStart with ByValChain.
2066 if (InChain != ByValChain)
2067 DAG.UpdateNodeOperands(CallSeqStart.getNode(), ByValChain,
2068 NextStackOffsetVal);
2069
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002070 // Transform all store nodes into one single node because all store
2071 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002072 if (!MemOpChains.empty())
2073 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002074 &MemOpChains[0], MemOpChains.size());
2075
Bill Wendling056292f2008-09-16 21:48:12 +00002076 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002077 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2078 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002079 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002080 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002081 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002082
2083 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002084 if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
2085 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2086 getPointerTy(), 0,MipsII:: MO_GOT);
2087 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
2088 0, MipsII::MO_ABS_LO);
2089 } else {
2090 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2091 getPointerTy(), 0, OpFlag);
2092 }
2093
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002094 LoadSymAddr = true;
2095 }
2096 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002097 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002098 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002099 LoadSymAddr = true;
2100 }
2101
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002102 SDValue InFlag;
2103
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002104 // Create nodes that load address of callee and copy it to T9
2105 if (IsPIC) {
2106 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002107 // Load callee address
Akira Hatanaka342837d2011-05-28 01:07:07 +00002108 Callee = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, Callee);
Akira Hatanaka25eba392011-06-24 19:01:25 +00002109 SDValue LoadValue = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), Callee,
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002110 MachinePointerInfo::getGOT(),
2111 false, false, 0);
2112
2113 // Use GOT+LO if callee has internal linkage.
2114 if (CalleeLo.getNode()) {
2115 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
2116 Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
2117 } else
2118 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002119 }
2120
2121 // copy to T9
2122 Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
2123 InFlag = Chain.getValue(1);
2124 Callee = DAG.getRegister(Mips::T9, MVT::i32);
2125 }
Bill Wendling056292f2008-09-16 21:48:12 +00002126
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002127 // Build a sequence of copy-to-reg nodes chained together with token
2128 // chain and flag operands which copy the outgoing args into registers.
2129 // The InFlag in necessary since all emitted instructions must be
2130 // stuck together.
2131 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2132 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2133 RegsToPass[i].second, InFlag);
2134 InFlag = Chain.getValue(1);
2135 }
2136
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002137 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002138 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002139 //
2140 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002141 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002142 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002143 Ops.push_back(Chain);
2144 Ops.push_back(Callee);
2145
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002146 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002147 // known live into the call.
2148 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2149 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2150 RegsToPass[i].second.getValueType()));
2151
Gabor Greifba36cb52008-08-28 21:40:38 +00002152 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002153 Ops.push_back(InFlag);
2154
Dale Johannesen33c960f2009-02-04 20:06:27 +00002155 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002156 InFlag = Chain.getValue(1);
2157
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002158 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002159 Chain = DAG.getCALLSEQ_END(Chain,
2160 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002161 DAG.getIntPtrConstant(0, true), InFlag);
2162 InFlag = Chain.getValue(1);
2163
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002164 // Handle result values, copying them out of physregs into vregs that we
2165 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002166 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2167 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002168}
2169
Dan Gohman98ca4f22009-08-05 01:29:28 +00002170/// LowerCallResult - Lower the result values of a call into the
2171/// appropriate copies out of appropriate physical registers.
2172SDValue
2173MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002174 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002175 const SmallVectorImpl<ISD::InputArg> &Ins,
2176 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002177 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002178 // Assign locations to each value returned by this call.
2179 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002180 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2181 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002182
Dan Gohman98ca4f22009-08-05 01:29:28 +00002183 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002184
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002185 // Copy all of the result registers out of their specified physreg.
2186 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002187 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002188 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002189 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002190 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002191 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002192
Dan Gohman98ca4f22009-08-05 01:29:28 +00002193 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002194}
2195
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002196//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002197// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002198//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002199static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2200 std::vector<SDValue>& OutChains,
2201 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2202 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2203 unsigned LocMem = VA.getLocMemOffset();
2204 unsigned FirstWord = LocMem / 4;
2205
2206 // copy register A0 - A3 to frame object
2207 for (unsigned i = 0; i < NumWords; ++i) {
2208 unsigned CurWord = FirstWord + i;
2209 if (CurWord >= O32IntRegsSize)
2210 break;
2211
2212 unsigned SrcReg = O32IntRegs[CurWord];
2213 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2214 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2215 DAG.getConstant(i * 4, MVT::i32));
2216 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2217 StorePtr, MachinePointerInfo(), false,
2218 false, 0);
2219 OutChains.push_back(Store);
2220 }
2221}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002222
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002223/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002224/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002225SDValue
2226MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002227 CallingConv::ID CallConv,
2228 bool isVarArg,
2229 const SmallVectorImpl<ISD::InputArg>
2230 &Ins,
2231 DebugLoc dl, SelectionDAG &DAG,
2232 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002233 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002234 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002235 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002236 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002237
Dan Gohman1e93df62010-04-17 14:41:14 +00002238 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002239
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002240 // Used with vargs to acumulate store chains.
2241 std::vector<SDValue> OutChains;
2242
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002243 // Assign locations to all of the incoming arguments.
2244 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002245 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2246 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002247
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002248 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002249 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002250 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002251 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002252
Akira Hatanaka43299772011-05-20 23:22:14 +00002253 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002254
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002255 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002256 CCValAssign &VA = ArgLocs[i];
2257
2258 // Arguments stored on registers
2259 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002260 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002261 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002262 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002263
Owen Anderson825b72b2009-08-11 20:47:22 +00002264 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002265 RC = Mips::CPURegsRegisterClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002266 else if (RegVT == MVT::i64)
2267 RC = Mips::CPU64RegsRegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002268 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002269 RC = Mips::FGR32RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002270 else if (RegVT == MVT::f64)
Akira Hatanakaf40de9d2011-09-26 21:55:17 +00002271 RC = HasMips64 ? Mips::FGR64RegisterClass : Mips::AFGR64RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002272 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002273 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002274
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002275 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002276 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002277 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002278 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002279
2280 // If this is an 8 or 16-bit value, it has been passed promoted
2281 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002282 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002283 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002284 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002285 if (VA.getLocInfo() == CCValAssign::SExt)
2286 Opcode = ISD::AssertSext;
2287 else if (VA.getLocInfo() == CCValAssign::ZExt)
2288 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002289 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002290 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00002291 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00002292 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002293 }
2294
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002295 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002296 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002297 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
2298 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00002299 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002300 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002301 getNextIntArgReg(ArgReg), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002302 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002303 if (!Subtarget->isLittle())
2304 std::swap(ArgValue, ArgValue2);
2305 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2306 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002307 }
2308 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002309
Dan Gohman98ca4f22009-08-05 01:29:28 +00002310 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002311 } else { // VA.isRegLoc()
2312
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002313 // sanity check
2314 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002315
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002316 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2317
2318 if (Flags.isByVal()) {
2319 assert(Subtarget->isABI_O32() &&
2320 "No support for ByVal args by ABIs other than O32 yet.");
2321 assert(Flags.getByValSize() &&
2322 "ByVal args of size 0 should have been ignored by front-end.");
2323 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2324 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2325 true);
2326 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2327 InVals.push_back(FIN);
2328 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2329
2330 continue;
2331 }
2332
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002333 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002334 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
2335 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002336
2337 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002338 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002339 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002340 MachinePointerInfo::getFixedStack(LastFI),
David Greenef6fa1862010-02-15 16:56:10 +00002341 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002342 }
2343 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002344
2345 // The mips ABIs for returning structs by value requires that we copy
2346 // the sret argument into $v0 for the return. Save the argument into
2347 // a virtual register so that we can access it from the return points.
2348 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2349 unsigned Reg = MipsFI->getSRetReturnReg();
2350 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002351 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002352 MipsFI->setSRetReturnReg(Reg);
2353 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002354 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002355 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002356 }
2357
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002358 if (isVarArg && Subtarget->isABI_O32()) {
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002359 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002360 // which is a value necessary to VASTART.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002361 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002362 assert(NextStackOffset % 4 == 0 &&
2363 "NextStackOffset must be aligned to 4-byte boundaries.");
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002364 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
2365 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002366
2367 // If NextStackOffset is smaller than o32's 16-byte reserved argument area,
2368 // copy the integer registers that have not been used for argument passing
2369 // to the caller's stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002370 for (; NextStackOffset < 16; NextStackOffset += 4) {
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002371 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002372 unsigned Idx = NextStackOffset / 4;
2373 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), O32IntRegs[Idx], RC);
2374 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
Akira Hatanaka69c19f72011-05-23 20:16:59 +00002375 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002376 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2377 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2378 MachinePointerInfo(),
2379 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002380 }
2381 }
2382
Akira Hatanaka43299772011-05-20 23:22:14 +00002383 MipsFI->setLastInArgFI(LastFI);
2384
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002385 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002386 // the size of Ins and InVals. This only happens when on varg functions
2387 if (!OutChains.empty()) {
2388 OutChains.push_back(Chain);
2389 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2390 &OutChains[0], OutChains.size());
2391 }
2392
Dan Gohman98ca4f22009-08-05 01:29:28 +00002393 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002394}
2395
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002396//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002397// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002398//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002399
Dan Gohman98ca4f22009-08-05 01:29:28 +00002400SDValue
2401MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002402 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002403 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002404 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002405 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002406
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002407 // CCValAssign - represent the assignment of
2408 // the return value to a location
2409 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002410
2411 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002412 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2413 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002414
Dan Gohman98ca4f22009-08-05 01:29:28 +00002415 // Analize return values.
2416 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002417
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002418 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002419 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002420 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002421 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002422 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002423 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002424 }
2425
Dan Gohman475871a2008-07-27 21:46:04 +00002426 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002427
2428 // Copy the result values into the output registers.
2429 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2430 CCValAssign &VA = RVLocs[i];
2431 assert(VA.isRegLoc() && "Can only return in registers!");
2432
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002433 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002434 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002435
2436 // guarantee that all emitted copies are
2437 // stuck together, avoiding something bad
2438 Flag = Chain.getValue(1);
2439 }
2440
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002441 // The mips ABIs for returning structs by value requires that we copy
2442 // the sret argument into $v0 for the return. We saved the argument into
2443 // a virtual register in the entry block, so now we copy the value out
2444 // and into $v0.
2445 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2446 MachineFunction &MF = DAG.getMachineFunction();
2447 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2448 unsigned Reg = MipsFI->getSRetReturnReg();
2449
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002450 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002451 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002452 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002453
Dale Johannesena05dca42009-02-04 23:02:30 +00002454 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002455 Flag = Chain.getValue(1);
2456 }
2457
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002458 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002459 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002460 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002461 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002462 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002463 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002464 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002465}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002466
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002467//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002468// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002469//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002470
2471/// getConstraintType - Given a constraint letter, return the type of
2472/// constraint it is for this target.
2473MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002474getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002475{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002476 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002477 // GCC config/mips/constraints.md
2478 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002479 // 'd' : An address register. Equivalent to r
2480 // unless generating MIPS16 code.
2481 // 'y' : Equivalent to r; retained for
2482 // backwards compatibility.
2483 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002484 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002485 switch (Constraint[0]) {
2486 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002487 case 'd':
2488 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002489 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002490 return C_RegisterClass;
2491 break;
2492 }
2493 }
2494 return TargetLowering::getConstraintType(Constraint);
2495}
2496
John Thompson44ab89e2010-10-29 17:29:13 +00002497/// Examine constraint type and operand type and determine a weight value.
2498/// This object must already have been set up with the operand type
2499/// and the current alternative constraint selected.
2500TargetLowering::ConstraintWeight
2501MipsTargetLowering::getSingleConstraintMatchWeight(
2502 AsmOperandInfo &info, const char *constraint) const {
2503 ConstraintWeight weight = CW_Invalid;
2504 Value *CallOperandVal = info.CallOperandVal;
2505 // If we don't have a value, we can't do a match,
2506 // but allow it at the lowest weight.
2507 if (CallOperandVal == NULL)
2508 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002509 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002510 // Look at the constraint type.
2511 switch (*constraint) {
2512 default:
2513 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2514 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002515 case 'd':
2516 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002517 if (type->isIntegerTy())
2518 weight = CW_Register;
2519 break;
2520 case 'f':
2521 if (type->isFloatTy())
2522 weight = CW_Register;
2523 break;
2524 }
2525 return weight;
2526}
2527
Eric Christopher38d64262011-06-29 19:33:04 +00002528/// Given a register class constraint, like 'r', if this corresponds directly
2529/// to an LLVM register class, return a register of 0 and the register class
2530/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002531std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002532getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002533{
2534 if (Constraint.size() == 1) {
2535 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002536 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2537 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002538 case 'r':
2539 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002540 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002541 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002542 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002543 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002544 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2545 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002546 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002547 }
2548 }
2549 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2550}
2551
Dan Gohman6520e202008-10-18 02:06:02 +00002552bool
2553MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2554 // The Mips target isn't yet aware of offsets.
2555 return false;
2556}
Evan Chengeb2f9692009-10-27 19:56:55 +00002557
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002558bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2559 if (VT != MVT::f32 && VT != MVT::f64)
2560 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002561 if (Imm.isNegZero())
2562 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002563 return Imm.isZero();
2564}