blob: 1932e745c59336b4e6100252493da9767a3ce425 [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 Hatanaka14487d42011-06-07 19:28:39 +0000711static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
712 DebugLoc dl,
713 const MipsSubtarget* Subtarget,
714 const TargetInstrInfo *TII,
715 bool isFPCmp, unsigned Opc) {
716 // There is no need to expand CMov instructions if target has
717 // conditional moves.
718 if (Subtarget->hasCondMov())
719 return BB;
720
721 // To "insert" a SELECT_CC instruction, we actually have to insert the
722 // diamond control-flow pattern. The incoming instruction knows the
723 // destination vreg to set, the condition code register to branch on, the
724 // true/false values to select between, and a branch opcode to use.
725 const BasicBlock *LLVM_BB = BB->getBasicBlock();
726 MachineFunction::iterator It = BB;
727 ++It;
728
729 // thisMBB:
730 // ...
731 // TrueVal = ...
732 // setcc r1, r2, r3
733 // bNE r1, r0, copy1MBB
734 // fallthrough --> copy0MBB
735 MachineBasicBlock *thisMBB = BB;
736 MachineFunction *F = BB->getParent();
737 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
738 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
739 F->insert(It, copy0MBB);
740 F->insert(It, sinkMBB);
741
742 // Transfer the remainder of BB and its successor edges to sinkMBB.
743 sinkMBB->splice(sinkMBB->begin(), BB,
744 llvm::next(MachineBasicBlock::iterator(MI)),
745 BB->end());
746 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
747
748 // Next, add the true and fallthrough blocks as its successors.
749 BB->addSuccessor(copy0MBB);
750 BB->addSuccessor(sinkMBB);
751
752 // Emit the right instruction according to the type of the operands compared
753 if (isFPCmp)
754 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
755 else
756 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
757 .addReg(Mips::ZERO).addMBB(sinkMBB);
758
759 // copy0MBB:
760 // %FalseValue = ...
761 // # fallthrough to sinkMBB
762 BB = copy0MBB;
763
764 // Update machine-CFG edges
765 BB->addSuccessor(sinkMBB);
766
767 // sinkMBB:
768 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
769 // ...
770 BB = sinkMBB;
771
772 if (isFPCmp)
773 BuildMI(*BB, BB->begin(), dl,
774 TII->get(Mips::PHI), MI->getOperand(0).getReg())
775 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
776 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
777 else
778 BuildMI(*BB, BB->begin(), dl,
779 TII->get(Mips::PHI), MI->getOperand(0).getReg())
780 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
781 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
782
783 MI->eraseFromParent(); // The pseudo instruction is gone now.
784 return BB;
785}
786
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000787MachineBasicBlock *
788MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000789 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000790 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
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;
797 case Mips::MOVT:
798 case Mips::MOVT_S:
799 case Mips::MOVT_D:
800 return ExpandCondMov(MI, BB, dl, Subtarget, TII, true, Mips::BC1F);
801 case Mips::MOVF:
802 case Mips::MOVF_S:
803 case Mips::MOVF_D:
804 return ExpandCondMov(MI, BB, dl, Subtarget, TII, true, Mips::BC1T);
805 case Mips::MOVZ_I:
806 case Mips::MOVZ_S:
807 case Mips::MOVZ_D:
808 return ExpandCondMov(MI, BB, dl, Subtarget, TII, false, Mips::BNE);
809 case Mips::MOVN_I:
810 case Mips::MOVN_S:
811 case Mips::MOVN_D:
812 return ExpandCondMov(MI, BB, dl, Subtarget, TII, false, Mips::BEQ);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000813
814 case Mips::ATOMIC_LOAD_ADD_I8:
815 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
816 case Mips::ATOMIC_LOAD_ADD_I16:
817 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
818 case Mips::ATOMIC_LOAD_ADD_I32:
819 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
820
821 case Mips::ATOMIC_LOAD_AND_I8:
822 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
823 case Mips::ATOMIC_LOAD_AND_I16:
824 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
825 case Mips::ATOMIC_LOAD_AND_I32:
826 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
827
828 case Mips::ATOMIC_LOAD_OR_I8:
829 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
830 case Mips::ATOMIC_LOAD_OR_I16:
831 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
832 case Mips::ATOMIC_LOAD_OR_I32:
833 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
834
835 case Mips::ATOMIC_LOAD_XOR_I8:
836 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
837 case Mips::ATOMIC_LOAD_XOR_I16:
838 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
839 case Mips::ATOMIC_LOAD_XOR_I32:
840 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
841
842 case Mips::ATOMIC_LOAD_NAND_I8:
843 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
844 case Mips::ATOMIC_LOAD_NAND_I16:
845 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
846 case Mips::ATOMIC_LOAD_NAND_I32:
847 return EmitAtomicBinary(MI, BB, 4, 0, true);
848
849 case Mips::ATOMIC_LOAD_SUB_I8:
850 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
851 case Mips::ATOMIC_LOAD_SUB_I16:
852 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
853 case Mips::ATOMIC_LOAD_SUB_I32:
854 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
855
856 case Mips::ATOMIC_SWAP_I8:
857 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
858 case Mips::ATOMIC_SWAP_I16:
859 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
860 case Mips::ATOMIC_SWAP_I32:
861 return EmitAtomicBinary(MI, BB, 4, 0);
862
863 case Mips::ATOMIC_CMP_SWAP_I8:
864 return EmitAtomicCmpSwapPartword(MI, BB, 1);
865 case Mips::ATOMIC_CMP_SWAP_I16:
866 return EmitAtomicCmpSwapPartword(MI, BB, 2);
867 case Mips::ATOMIC_CMP_SWAP_I32:
868 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000869 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000870}
871
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000872// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
873// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
874MachineBasicBlock *
875MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000876 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000877 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000878 assert(Size == 4 && "Unsupported size for EmitAtomicBinary.");
879
880 MachineFunction *MF = BB->getParent();
881 MachineRegisterInfo &RegInfo = MF->getRegInfo();
882 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
883 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
884 DebugLoc dl = MI->getDebugLoc();
885
Akira Hatanaka4061da12011-07-19 20:11:17 +0000886 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000887 unsigned Ptr = MI->getOperand(1).getReg();
888 unsigned Incr = MI->getOperand(2).getReg();
889
Akira Hatanaka4061da12011-07-19 20:11:17 +0000890 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
891 unsigned AndRes = RegInfo.createVirtualRegister(RC);
892 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000893
894 // insert new blocks after the current block
895 const BasicBlock *LLVM_BB = BB->getBasicBlock();
896 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
897 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
898 MachineFunction::iterator It = BB;
899 ++It;
900 MF->insert(It, loopMBB);
901 MF->insert(It, exitMBB);
902
903 // Transfer the remainder of BB and its successor edges to exitMBB.
904 exitMBB->splice(exitMBB->begin(), BB,
905 llvm::next(MachineBasicBlock::iterator(MI)),
906 BB->end());
907 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
908
909 // thisMBB:
910 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000911 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000912 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000913 loopMBB->addSuccessor(loopMBB);
914 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000915
916 // loopMBB:
917 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000918 // <binop> storeval, oldval, incr
919 // sc success, storeval, 0(ptr)
920 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000921 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000922 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000923 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000924 // and andres, oldval, incr
925 // nor storeval, $0, andres
926 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr);
927 BuildMI(BB, dl, TII->get(Mips::NOR), StoreVal)
928 .addReg(Mips::ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000929 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000930 // <binop> storeval, oldval, incr
931 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000932 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000933 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000934 }
Akira Hatanaka4061da12011-07-19 20:11:17 +0000935 BuildMI(BB, dl, TII->get(Mips::SC), Success)
936 .addReg(StoreVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000937 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +0000938 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000939
940 MI->eraseFromParent(); // The instruction is gone now.
941
Akira Hatanaka939ece12011-07-19 03:42:13 +0000942 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000943}
944
945MachineBasicBlock *
946MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000947 MachineBasicBlock *BB,
948 unsigned Size, unsigned BinOpcode,
949 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000950 assert((Size == 1 || Size == 2) &&
951 "Unsupported size for EmitAtomicBinaryPartial.");
952
953 MachineFunction *MF = BB->getParent();
954 MachineRegisterInfo &RegInfo = MF->getRegInfo();
955 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
956 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
957 DebugLoc dl = MI->getDebugLoc();
958
959 unsigned Dest = MI->getOperand(0).getReg();
960 unsigned Ptr = MI->getOperand(1).getReg();
961 unsigned Incr = MI->getOperand(2).getReg();
962
Akira Hatanaka4061da12011-07-19 20:11:17 +0000963 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
964 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000965 unsigned Mask = RegInfo.createVirtualRegister(RC);
966 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000967 unsigned NewVal = RegInfo.createVirtualRegister(RC);
968 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000969 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000970 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
971 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
972 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
973 unsigned AndRes = RegInfo.createVirtualRegister(RC);
974 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +0000975 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000976 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
977 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
978 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
979 unsigned SllRes = RegInfo.createVirtualRegister(RC);
980 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000981
982 // insert new blocks after the current block
983 const BasicBlock *LLVM_BB = BB->getBasicBlock();
984 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000985 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000986 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
987 MachineFunction::iterator It = BB;
988 ++It;
989 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000990 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000991 MF->insert(It, exitMBB);
992
993 // Transfer the remainder of BB and its successor edges to exitMBB.
994 exitMBB->splice(exitMBB->begin(), BB,
995 llvm::next(MachineBasicBlock::iterator(MI)),
996 BB->end());
997 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
998
Akira Hatanaka81b44112011-07-19 17:09:53 +0000999 BB->addSuccessor(loopMBB);
1000 loopMBB->addSuccessor(loopMBB);
1001 loopMBB->addSuccessor(sinkMBB);
1002 sinkMBB->addSuccessor(exitMBB);
1003
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001004 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001005 // addiu masklsb2,$0,-4 # 0xfffffffc
1006 // and alignedaddr,ptr,masklsb2
1007 // andi ptrlsb2,ptr,3
1008 // sll shiftamt,ptrlsb2,3
1009 // ori maskupper,$0,255 # 0xff
1010 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001011 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001012 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001013
1014 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001015 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1016 .addReg(Mips::ZERO).addImm(-4);
1017 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1018 .addReg(Ptr).addReg(MaskLSB2);
1019 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1020 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1021 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1022 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001023 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1024 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001025 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001026 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001027
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001028
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001029 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001030 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001031 // ll oldval,0(alignedaddr)
1032 // binop binopres,oldval,incr2
1033 // and newval,binopres,mask
1034 // and maskedoldval0,oldval,mask2
1035 // or storeval,maskedoldval0,newval
1036 // sc success,storeval,0(alignedaddr)
1037 // beq success,$0,loopMBB
1038
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001039 // atomic.swap
1040 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001041 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001042 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001043 // and maskedoldval0,oldval,mask2
1044 // or storeval,maskedoldval0,newval
1045 // sc success,storeval,0(alignedaddr)
1046 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001047
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001048 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001049 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001050 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001051 // and andres, oldval, incr2
1052 // nor binopres, $0, andres
1053 // and newval, binopres, mask
1054 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1055 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1056 .addReg(Mips::ZERO).addReg(AndRes);
1057 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001058 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001059 // <binop> binopres, oldval, incr2
1060 // and newval, binopres, mask
1061 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1062 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001063 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001064 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001065 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001066 }
1067
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001068 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001069 .addReg(OldVal).addReg(Mask2);
1070 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001071 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001072 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1073 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001074 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001075 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001076
Akira Hatanaka939ece12011-07-19 03:42:13 +00001077 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001078 // and maskedoldval1,oldval,mask
1079 // srl srlres,maskedoldval1,shiftamt
1080 // sll sllres,srlres,24
1081 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001082 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001083 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001084
Akira Hatanaka4061da12011-07-19 20:11:17 +00001085 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1086 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001087 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1088 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001089 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1090 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001091 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001092 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001093
1094 MI->eraseFromParent(); // The instruction is gone now.
1095
Akira Hatanaka939ece12011-07-19 03:42:13 +00001096 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001097}
1098
1099MachineBasicBlock *
1100MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001101 MachineBasicBlock *BB,
1102 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001103 assert(Size == 4 && "Unsupported size for EmitAtomicCmpSwap.");
1104
1105 MachineFunction *MF = BB->getParent();
1106 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1107 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1108 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1109 DebugLoc dl = MI->getDebugLoc();
1110
1111 unsigned Dest = MI->getOperand(0).getReg();
1112 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001113 unsigned OldVal = MI->getOperand(2).getReg();
1114 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001115
Akira Hatanaka4061da12011-07-19 20:11:17 +00001116 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001117
1118 // insert new blocks after the current block
1119 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1120 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1121 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1122 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1123 MachineFunction::iterator It = BB;
1124 ++It;
1125 MF->insert(It, loop1MBB);
1126 MF->insert(It, loop2MBB);
1127 MF->insert(It, exitMBB);
1128
1129 // Transfer the remainder of BB and its successor edges to exitMBB.
1130 exitMBB->splice(exitMBB->begin(), BB,
1131 llvm::next(MachineBasicBlock::iterator(MI)),
1132 BB->end());
1133 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1134
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001135 // thisMBB:
1136 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001137 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001138 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001139 loop1MBB->addSuccessor(exitMBB);
1140 loop1MBB->addSuccessor(loop2MBB);
1141 loop2MBB->addSuccessor(loop1MBB);
1142 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001143
1144 // loop1MBB:
1145 // ll dest, 0(ptr)
1146 // bne dest, oldval, exitMBB
1147 BB = loop1MBB;
Akira Hatanakad3ac47f2011-07-07 18:57:00 +00001148 BuildMI(BB, dl, TII->get(Mips::LL), Dest).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001149 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001150 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001151
1152 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001153 // sc success, newval, 0(ptr)
1154 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001155 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001156 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1157 .addReg(NewVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001158 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001159 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001160
1161 MI->eraseFromParent(); // The instruction is gone now.
1162
Akira Hatanaka939ece12011-07-19 03:42:13 +00001163 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001164}
1165
1166MachineBasicBlock *
1167MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001168 MachineBasicBlock *BB,
1169 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001170 assert((Size == 1 || Size == 2) &&
1171 "Unsupported size for EmitAtomicCmpSwapPartial.");
1172
1173 MachineFunction *MF = BB->getParent();
1174 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1175 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1176 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1177 DebugLoc dl = MI->getDebugLoc();
1178
1179 unsigned Dest = MI->getOperand(0).getReg();
1180 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001181 unsigned CmpVal = MI->getOperand(2).getReg();
1182 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001183
Akira Hatanaka4061da12011-07-19 20:11:17 +00001184 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1185 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001186 unsigned Mask = RegInfo.createVirtualRegister(RC);
1187 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001188 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1189 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1190 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1191 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1192 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1193 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1194 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1195 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1196 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1197 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1198 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1199 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1200 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1201 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001202
1203 // insert new blocks after the current block
1204 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1205 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1206 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001207 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001208 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1209 MachineFunction::iterator It = BB;
1210 ++It;
1211 MF->insert(It, loop1MBB);
1212 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001213 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001214 MF->insert(It, exitMBB);
1215
1216 // Transfer the remainder of BB and its successor edges to exitMBB.
1217 exitMBB->splice(exitMBB->begin(), BB,
1218 llvm::next(MachineBasicBlock::iterator(MI)),
1219 BB->end());
1220 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1221
Akira Hatanaka81b44112011-07-19 17:09:53 +00001222 BB->addSuccessor(loop1MBB);
1223 loop1MBB->addSuccessor(sinkMBB);
1224 loop1MBB->addSuccessor(loop2MBB);
1225 loop2MBB->addSuccessor(loop1MBB);
1226 loop2MBB->addSuccessor(sinkMBB);
1227 sinkMBB->addSuccessor(exitMBB);
1228
Akira Hatanaka70564a92011-07-19 18:14:26 +00001229 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001230 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001231 // addiu masklsb2,$0,-4 # 0xfffffffc
1232 // and alignedaddr,ptr,masklsb2
1233 // andi ptrlsb2,ptr,3
1234 // sll shiftamt,ptrlsb2,3
1235 // ori maskupper,$0,255 # 0xff
1236 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001237 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001238 // andi maskedcmpval,cmpval,255
1239 // sll shiftedcmpval,maskedcmpval,shiftamt
1240 // andi maskednewval,newval,255
1241 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001242 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001243 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1244 .addReg(Mips::ZERO).addImm(-4);
1245 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1246 .addReg(Ptr).addReg(MaskLSB2);
1247 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1248 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1249 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1250 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001251 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1252 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001253 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001254 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1255 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001256 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1257 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001258 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1259 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001260 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1261 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001262
1263 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001264 // ll oldval,0(alginedaddr)
1265 // and maskedoldval0,oldval,mask
1266 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001267 BB = loop1MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001268 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
1269 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1270 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001271 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001272 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001273
1274 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001275 // and maskedoldval1,oldval,mask2
1276 // or storeval,maskedoldval1,shiftednewval
1277 // sc success,storeval,0(alignedaddr)
1278 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001279 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001280 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1281 .addReg(OldVal).addReg(Mask2);
1282 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1283 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1284 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1285 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001286 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001287 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001288
Akira Hatanaka939ece12011-07-19 03:42:13 +00001289 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001290 // srl srlres,maskedoldval0,shiftamt
1291 // sll sllres,srlres,24
1292 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001293 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001294 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001295
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001296 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1297 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001298 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1299 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001300 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001301 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001302
1303 MI->eraseFromParent(); // The instruction is gone now.
1304
Akira Hatanaka939ece12011-07-19 03:42:13 +00001305 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001306}
1307
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001308//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001309// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001310//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001311SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001312LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001313{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001314 MachineFunction &MF = DAG.getMachineFunction();
1315 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1316
1317 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001318 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1319 "Cannot lower if the alignment of the allocated space is larger than \
1320 that of the stack.");
1321
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001322 SDValue Chain = Op.getOperand(0);
1323 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001324 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001325
1326 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +00001327 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001328
1329 // Subtract the dynamic size from the actual stack size to
1330 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +00001331 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001332
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001333 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001334 // must be placed in the stack pointer register.
Akira Hatanaka053546c2011-05-25 02:20:00 +00001335 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub,
1336 SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001337
1338 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001339 // value and a chain
Akira Hatanaka21afc632011-06-21 00:40:49 +00001340 SDVTList VTLs = DAG.getVTList(MVT::i32, MVT::Other);
1341 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1342 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1343
1344 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001345}
1346
1347SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001348LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001349{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001350 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001351 // the block to branch to if the condition is true.
1352 SDValue Chain = Op.getOperand(0);
1353 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001354 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001355
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001356 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1357
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001358 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001359 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001360 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001361
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001362 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001363 Mips::CondCode CC =
1364 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001365 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001366
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001367 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001368 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001369}
1370
1371SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001372LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001373{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001374 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001375
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001376 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001377 if (Cond.getOpcode() != MipsISD::FPCmp)
1378 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001379
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001380 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1381 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001382}
1383
Dan Gohmand858e902010-04-17 15:26:15 +00001384SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1385 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001386 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001387 DebugLoc dl = Op.getDebugLoc();
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001388 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001389
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001390 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001391 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001392
Chris Lattnerb71b9092009-08-13 06:28:06 +00001393 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001394
Chris Lattnere3736f82009-08-13 05:41:27 +00001395 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001396 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1397 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001398 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001399 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1400 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001401 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001402 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001403 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001404 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1405 MipsII::MO_ABS_HI);
1406 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1407 MipsII::MO_ABS_LO);
1408 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1409 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001410 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001411 }
1412
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001413 EVT ValTy = Op.getValueType();
1414 bool HasGotOfst = (GV->hasInternalLinkage() ||
1415 (GV->hasLocalLinkage() && !isa<Function>(GV)));
1416 unsigned GotFlag = IsN64 ?
1417 (HasGotOfst ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT_DISP) :
1418 MipsII::MO_GOT;
1419 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0, GotFlag);
1420 GA = DAG.getNode(MipsISD::WrapperPIC, dl, ValTy, GA);
1421 SDValue ResNode = DAG.getLoad(ValTy, dl,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001422 DAG.getEntryNode(), GA, MachinePointerInfo(),
1423 false, false, 0);
1424 // On functions and global targets not internal linked only
1425 // a load from got/GP is necessary for PIC to work.
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001426 if (!HasGotOfst)
Akira Hatanaka0f843822011-06-07 18:58:42 +00001427 return ResNode;
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001428 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0,
1429 IsN64 ? MipsII::MO_GOT_OFST :
1430 MipsII::MO_ABS_LO);
1431 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, GALo);
1432 return DAG.getNode(ISD::ADD, dl, ValTy, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001433}
1434
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001435SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1436 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001437 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1438 // FIXME there isn't actually debug info here
1439 DebugLoc dl = Op.getDebugLoc();
1440
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001441 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001442 // %hi/%lo relocation
1443 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
1444 MipsII::MO_ABS_HI);
1445 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
1446 MipsII::MO_ABS_LO);
1447 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1448 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1449 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001450 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001451
1452 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1453 MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001454 BAGOTOffset = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, BAGOTOffset);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001455 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1456 MipsII::MO_ABS_LO);
1457 SDValue Load = DAG.getLoad(MVT::i32, dl,
1458 DAG.getEntryNode(), BAGOTOffset,
1459 MachinePointerInfo(), false, false, 0);
1460 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
1461 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001462}
1463
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001464SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001465LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001466{
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001467 // If the relocation model is PIC, use the General Dynamic TLS Model,
1468 // otherwise use the Initial Exec or Local Exec TLS Model.
1469 // TODO: implement Local Dynamic TLS model
1470
1471 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1472 DebugLoc dl = GA->getDebugLoc();
1473 const GlobalValue *GV = GA->getGlobal();
1474 EVT PtrVT = getPointerTy();
1475
1476 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1477 // General Dynamic TLS Model
1478 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001479 0, MipsII::MO_TLSGD);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001480 SDValue Tlsgd = DAG.getNode(MipsISD::TlsGd, dl, MVT::i32, TGA);
1481 SDValue GP = DAG.getRegister(Mips::GP, MVT::i32);
1482 SDValue Argument = DAG.getNode(ISD::ADD, dl, MVT::i32, GP, Tlsgd);
1483
1484 ArgListTy Args;
1485 ArgListEntry Entry;
1486 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001487 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001488 Args.push_back(Entry);
1489 std::pair<SDValue, SDValue> CallResult =
1490 LowerCallTo(DAG.getEntryNode(),
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001491 (Type *) Type::getInt32Ty(*DAG.getContext()),
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001492 false, false, false, false, 0, CallingConv::C, false, true,
1493 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG,
1494 dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001495
1496 return CallResult.first;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001497 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001498
1499 SDValue Offset;
1500 if (GV->isDeclaration()) {
1501 // Initial Exec TLS Model
1502 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1503 MipsII::MO_GOTTPREL);
1504 Offset = DAG.getLoad(MVT::i32, dl,
1505 DAG.getEntryNode(), TGA, MachinePointerInfo(),
1506 false, false, 0);
1507 } else {
1508 // Local Exec TLS Model
1509 SDVTList VTs = DAG.getVTList(MVT::i32);
1510 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1511 MipsII::MO_TPREL_HI);
1512 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1513 MipsII::MO_TPREL_LO);
1514 SDValue Hi = DAG.getNode(MipsISD::TprelHi, dl, VTs, &TGAHi, 1);
1515 SDValue Lo = DAG.getNode(MipsISD::TprelLo, dl, MVT::i32, TGALo);
1516 Offset = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1517 }
1518
1519 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1520 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001521}
1522
1523SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001524LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001525{
Dan Gohman475871a2008-07-27 21:46:04 +00001526 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001527 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +00001528 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001529 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001530 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001531 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001532
Owen Andersone50ed302009-08-10 22:56:29 +00001533 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001534 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001535
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001536 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
1537
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +00001538 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001539 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001540 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001541 } else {// Emit Load from Global Pointer
1542 JTI = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, JTI);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001543 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
1544 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001545 false, false, 0);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001546 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001547
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001548 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1549 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001550 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001551 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001552
1553 return ResNode;
1554}
1555
Dan Gohman475871a2008-07-27 21:46:04 +00001556SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001557LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001558{
Dan Gohman475871a2008-07-27 21:46:04 +00001559 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001560 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001561 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001562 // FIXME there isn't actually debug info here
1563 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001564
1565 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001566 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001567 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001568 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001569 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001570 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001571 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1572 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001573 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001574
1575 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001576 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001577 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001578 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001579 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001580 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1581 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001582 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001583 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001584 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001585 N->getOffset(), MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001586 CP = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001587 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001588 CP, MachinePointerInfo::getConstantPool(),
1589 false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001590 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001591 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001592 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001593 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
1594 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001595
1596 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001597}
1598
Dan Gohmand858e902010-04-17 15:26:15 +00001599SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001600 MachineFunction &MF = DAG.getMachineFunction();
1601 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1602
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001603 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001604 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1605 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001606
1607 // vastart just stores the address of the VarArgsFrameIndex slot into the
1608 // memory location argument.
1609 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001610 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1611 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +00001612 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001613}
1614
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001615static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
1616 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
1617 DebugLoc dl = Op.getDebugLoc();
1618 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
1619 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
1620 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
1621 DAG.getConstant(0x7fffffff, MVT::i32));
1622 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
1623 DAG.getConstant(0x80000000, MVT::i32));
1624 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1625 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
1626}
1627
1628static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001629 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001630 // Use ext/ins instructions if target architecture is Mips32r2.
1631 // Eliminate redundant mfc1 and mtc1 instructions.
1632 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001633
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001634 if (!isLittle)
1635 std::swap(LoIdx, HiIdx);
1636
1637 DebugLoc dl = Op.getDebugLoc();
1638 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1639 Op.getOperand(0),
1640 DAG.getConstant(LoIdx, MVT::i32));
1641 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1642 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1643 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1644 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1645 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1646 DAG.getConstant(0x7fffffff, MVT::i32));
1647 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1648 DAG.getConstant(0x80000000, MVT::i32));
1649 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1650
1651 if (!isLittle)
1652 std::swap(Word0, Word1);
1653
1654 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1655}
1656
1657SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1658 const {
1659 EVT Ty = Op.getValueType();
1660
1661 assert(Ty == MVT::f32 || Ty == MVT::f64);
1662
1663 if (Ty == MVT::f32)
1664 return LowerFCOPYSIGN32(Op, DAG);
1665 else
1666 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
1667}
1668
Akira Hatanaka2e591472011-06-02 00:24:44 +00001669SDValue MipsTargetLowering::
1670LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001671 // check the depth
1672 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001673 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001674
1675 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1676 MFI->setFrameAddressIsTaken(true);
1677 EVT VT = Op.getValueType();
1678 DebugLoc dl = Op.getDebugLoc();
1679 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Mips::FP, VT);
1680 return FrameAddr;
1681}
1682
Akira Hatanakadb548262011-07-19 23:30:50 +00001683// TODO: set SType according to the desired memory barrier behavior.
1684SDValue MipsTargetLowering::LowerMEMBARRIER(SDValue Op,
1685 SelectionDAG& DAG) const {
1686 unsigned SType = 0;
1687 DebugLoc dl = Op.getDebugLoc();
1688 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1689 DAG.getConstant(SType, MVT::i32));
1690}
1691
Eli Friedman14648462011-07-27 22:21:52 +00001692SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1693 SelectionDAG& DAG) const {
1694 // FIXME: Need pseudo-fence for 'singlethread' fences
1695 // FIXME: Set SType for weaker fences where supported/appropriate.
1696 unsigned SType = 0;
1697 DebugLoc dl = Op.getDebugLoc();
1698 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1699 DAG.getConstant(SType, MVT::i32));
1700}
1701
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001702//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001703// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001704//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001705
1706#include "MipsGenCallingConv.inc"
1707
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001708//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001709// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001710// Mips O32 ABI rules:
1711// ---
1712// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001713// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001714// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001715// f64 - Only passed in two aliased f32 registers if no int reg has been used
1716// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001717// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1718// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001719//
1720// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001721//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001722
Duncan Sands1e96bab2010-11-04 10:49:57 +00001723static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001724 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001725 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1726
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001727 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001728
1729 static const unsigned IntRegs[] = {
1730 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1731 };
1732 static const unsigned F32Regs[] = {
1733 Mips::F12, Mips::F14
1734 };
1735 static const unsigned F64Regs[] = {
1736 Mips::D6, Mips::D7
1737 };
1738
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001739 // ByVal Args
1740 if (ArgFlags.isByVal()) {
1741 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1742 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1743 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1744 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1745 r < std::min(IntRegsSize, NextReg); ++r)
1746 State.AllocateReg(IntRegs[r]);
1747 return false;
1748 }
1749
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001750 // Promote i8 and i16
1751 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1752 LocVT = MVT::i32;
1753 if (ArgFlags.isSExt())
1754 LocInfo = CCValAssign::SExt;
1755 else if (ArgFlags.isZExt())
1756 LocInfo = CCValAssign::ZExt;
1757 else
1758 LocInfo = CCValAssign::AExt;
1759 }
1760
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001761 unsigned Reg;
1762
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001763 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1764 // is true: function is vararg, argument is 3rd or higher, there is previous
1765 // argument which is not f32 or f64.
1766 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1767 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001768 unsigned OrigAlign = ArgFlags.getOrigAlign();
1769 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001770
1771 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001772 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001773 // If this is the first part of an i64 arg,
1774 // the allocated register must be either A0 or A2.
1775 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1776 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001777 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001778 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1779 // Allocate int register and shadow next int register. If first
1780 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001781 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1782 if (Reg == Mips::A1 || Reg == Mips::A3)
1783 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1784 State.AllocateReg(IntRegs, IntRegsSize);
1785 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001786 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1787 // we are guaranteed to find an available float register
1788 if (ValVT == MVT::f32) {
1789 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1790 // Shadow int register
1791 State.AllocateReg(IntRegs, IntRegsSize);
1792 } else {
1793 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1794 // Shadow int registers
1795 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1796 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1797 State.AllocateReg(IntRegs, IntRegsSize);
1798 State.AllocateReg(IntRegs, IntRegsSize);
1799 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001800 } else
1801 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001802
Akira Hatanakad37776d2011-05-20 21:39:54 +00001803 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1804 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1805
1806 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001807 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001808 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001809 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001810
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001811 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001812}
1813
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001814//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001815// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001816//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001817
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001818static const unsigned O32IntRegsSize = 4;
1819
1820static const unsigned O32IntRegs[] = {
1821 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1822};
1823
Akira Hatanaka373e3a42011-09-23 00:58:33 +00001824// Return next O32 integer argument register.
1825static unsigned getNextIntArgReg(unsigned Reg) {
1826 assert((Reg == Mips::A0) || (Reg == Mips::A2));
1827 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
1828}
1829
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001830// Write ByVal Arg to arg registers and stack.
1831static void
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001832WriteByValArg(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001833 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1834 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1835 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001836 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001837 MVT PtrType, bool isLittle) {
1838 unsigned LocMemOffset = VA.getLocMemOffset();
1839 unsigned Offset = 0;
1840 uint32_t RemainingSize = Flags.getByValSize();
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001841 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001842
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001843 // Copy the first 4 words of byval arg to registers A0 - A3.
1844 // FIXME: Use a stricter alignment if it enables better optimization in passes
1845 // run later.
1846 for (; RemainingSize >= 4 && LocMemOffset < 4 * 4;
1847 Offset += 4, RemainingSize -= 4, LocMemOffset += 4) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001848 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001849 DAG.getConstant(Offset, MVT::i32));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001850 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1851 MachinePointerInfo(),
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001852 false, false, std::min(ByValAlign,
1853 (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001854 MemOpChains.push_back(LoadVal.getValue(1));
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001855 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001856 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1857 }
1858
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001859 if (RemainingSize == 0)
1860 return;
1861
1862 // If there still is a register available for argument passing, write the
1863 // remaining part of the structure to it using subword loads and shifts.
1864 if (LocMemOffset < 4 * 4) {
1865 assert(RemainingSize <= 3 && RemainingSize >= 1 &&
1866 "There must be one to three bytes remaining.");
1867 unsigned LoadSize = (RemainingSize == 3 ? 2 : RemainingSize);
1868 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1869 DAG.getConstant(Offset, MVT::i32));
1870 unsigned Alignment = std::min(ByValAlign, (unsigned )4);
1871 SDValue LoadVal = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
1872 LoadPtr, MachinePointerInfo(),
1873 MVT::getIntegerVT(LoadSize * 8), false,
1874 false, Alignment);
1875 MemOpChains.push_back(LoadVal.getValue(1));
1876
1877 // If target is big endian, shift it to the most significant half-word or
1878 // byte.
1879 if (!isLittle)
1880 LoadVal = DAG.getNode(ISD::SHL, dl, MVT::i32, LoadVal,
1881 DAG.getConstant(32 - LoadSize * 8, MVT::i32));
1882
1883 Offset += LoadSize;
1884 RemainingSize -= LoadSize;
1885
1886 // Read second subword if necessary.
1887 if (RemainingSize != 0) {
1888 assert(RemainingSize == 1 && "There must be one byte remaining.");
1889 LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1890 DAG.getConstant(Offset, MVT::i32));
1891 unsigned Alignment = std::min(ByValAlign, (unsigned )2);
1892 SDValue Subword = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
1893 LoadPtr, MachinePointerInfo(),
1894 MVT::i8, false, false, Alignment);
1895 MemOpChains.push_back(Subword.getValue(1));
1896 // Insert the loaded byte to LoadVal.
1897 // FIXME: Use INS if supported by target.
1898 unsigned ShiftAmt = isLittle ? 16 : 8;
1899 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i32, Subword,
1900 DAG.getConstant(ShiftAmt, MVT::i32));
1901 LoadVal = DAG.getNode(ISD::OR, dl, MVT::i32, LoadVal, Shift);
1902 }
1903
1904 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
1905 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1906 return;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001907 }
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001908
1909 // Create a fixed object on stack at offset LocMemOffset and copy
1910 // remaining part of byval arg to it using memcpy.
1911 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1912 DAG.getConstant(Offset, MVT::i32));
1913 LastFI = MFI->CreateFixedObject(RemainingSize, LocMemOffset, true);
1914 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001915 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
1916 DAG.getConstant(RemainingSize, MVT::i32),
1917 std::min(ByValAlign, (unsigned)4),
1918 /*isVolatile=*/false, /*AlwaysInline=*/false,
1919 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001920}
1921
Dan Gohman98ca4f22009-08-05 01:29:28 +00001922/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001923/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001924/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001925SDValue
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001926MipsTargetLowering::LowerCall(SDValue InChain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001927 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001928 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001929 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001930 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001931 const SmallVectorImpl<ISD::InputArg> &Ins,
1932 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001933 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001934 // MIPs target does not yet support tail call optimization.
1935 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001936
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001937 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001938 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00001939 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001940 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001941 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001942
1943 // Analyze operands of the call, assigning locations to each operand.
1944 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001945 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1946 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001947
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001948 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001949 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001950 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001951 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001952
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001953 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001954 unsigned NextStackOffset = CCInfo.getNextStackOffset();
1955
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001956 // Chain is the output chain of the last Load/Store or CopyToReg node.
1957 // ByValChain is the output chain of the last Memcpy node created for copying
1958 // byval arguments to the stack.
1959 SDValue Chain, CallSeqStart, ByValChain;
1960 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
1961 Chain = CallSeqStart = DAG.getCALLSEQ_START(InChain, NextStackOffsetVal);
1962 ByValChain = InChain;
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001963
1964 // If this is the first call, create a stack frame object that points to
1965 // a location to which .cprestore saves $gp.
1966 if (IsPIC && !MipsFI->getGPFI())
1967 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
1968
Akira Hatanaka21afc632011-06-21 00:40:49 +00001969 // Get the frame index of the stack frame object that points to the location
1970 // of dynamically allocated area on the stack.
1971 int DynAllocFI = MipsFI->getDynAllocFI();
1972
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001973 // Update size of the maximum argument space.
1974 // For O32, a minimum of four words (16 bytes) of argument space is
1975 // allocated.
1976 if (Subtarget->isABI_O32())
1977 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
1978
1979 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1980
1981 if (MaxCallFrameSize < NextStackOffset) {
1982 MipsFI->setMaxCallFrameSize(NextStackOffset);
1983
Akira Hatanaka21afc632011-06-21 00:40:49 +00001984 // Set the offsets relative to $sp of the $gp restore slot and dynamically
1985 // allocated stack space. These offsets must be aligned to a boundary
1986 // determined by the stack alignment of the ABI.
1987 unsigned StackAlignment = TFL->getStackAlignment();
1988 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
1989 StackAlignment * StackAlignment;
1990
1991 if (IsPIC)
1992 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
1993
1994 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001995 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001996
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001997 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001998 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1999 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002000
Eric Christopher471e4222011-06-08 23:55:35 +00002001 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00002002
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002003 // Walk the register/memloc assignments, inserting copies/loads.
2004 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002005 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002006 CCValAssign &VA = ArgLocs[i];
2007
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002008 // Promote the value if needed.
2009 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002010 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002011 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002012 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002013 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002014 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002015 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002016 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2017 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002018 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2019 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002020 if (!Subtarget->isLittle())
2021 std::swap(Lo, Hi);
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002022 unsigned LocRegLo = VA.getLocReg();
2023 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2024 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2025 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002026 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002027 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002028 }
2029 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002030 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00002031 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002032 break;
2033 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00002034 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002035 break;
2036 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00002037 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002038 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002039 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002040
2041 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002042 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002043 if (VA.isRegLoc()) {
2044 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002045 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002046 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002047
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002048 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002049 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002050
Eric Christopher471e4222011-06-08 23:55:35 +00002051 // ByVal Arg.
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002052 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2053 if (Flags.isByVal()) {
2054 assert(Subtarget->isABI_O32() &&
2055 "No support for ByVal args by ABIs other than O32 yet.");
2056 assert(Flags.getByValSize() &&
2057 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002058 WriteByValArg(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI, MFI,
2059 DAG, Arg, VA, Flags, getPointerTy(), Subtarget->isLittle());
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002060 continue;
2061 }
2062
Chris Lattnere0b12152008-03-17 06:57:02 +00002063 // Create the frame index object for this incoming parameter
Eric Christopher471e4222011-06-08 23:55:35 +00002064 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002065 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00002066 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00002067
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002068 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002069 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00002070 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
2071 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00002072 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002073 }
2074
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002075 // Extend range of indices of frame objects for outgoing arguments that were
2076 // created during this function call. Skip this step if no such objects were
2077 // created.
2078 if (LastFI)
2079 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2080
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002081 // If a memcpy has been created to copy a byval arg to a stack, replace the
2082 // chain input of CallSeqStart with ByValChain.
2083 if (InChain != ByValChain)
2084 DAG.UpdateNodeOperands(CallSeqStart.getNode(), ByValChain,
2085 NextStackOffsetVal);
2086
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002087 // Transform all store nodes into one single node because all store
2088 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002089 if (!MemOpChains.empty())
2090 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002091 &MemOpChains[0], MemOpChains.size());
2092
Bill Wendling056292f2008-09-16 21:48:12 +00002093 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002094 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2095 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002096 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002097 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002098 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002099
2100 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002101 if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
2102 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2103 getPointerTy(), 0,MipsII:: MO_GOT);
2104 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
2105 0, MipsII::MO_ABS_LO);
2106 } else {
2107 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2108 getPointerTy(), 0, OpFlag);
2109 }
2110
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002111 LoadSymAddr = true;
2112 }
2113 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002114 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002115 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002116 LoadSymAddr = true;
2117 }
2118
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002119 SDValue InFlag;
2120
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002121 // Create nodes that load address of callee and copy it to T9
2122 if (IsPIC) {
2123 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002124 // Load callee address
Akira Hatanaka342837d2011-05-28 01:07:07 +00002125 Callee = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, Callee);
Akira Hatanaka25eba392011-06-24 19:01:25 +00002126 SDValue LoadValue = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), Callee,
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002127 MachinePointerInfo::getGOT(),
2128 false, false, 0);
2129
2130 // Use GOT+LO if callee has internal linkage.
2131 if (CalleeLo.getNode()) {
2132 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
2133 Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
2134 } else
2135 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002136 }
2137
2138 // copy to T9
2139 Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
2140 InFlag = Chain.getValue(1);
2141 Callee = DAG.getRegister(Mips::T9, MVT::i32);
2142 }
Bill Wendling056292f2008-09-16 21:48:12 +00002143
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002144 // Build a sequence of copy-to-reg nodes chained together with token
2145 // chain and flag operands which copy the outgoing args into registers.
2146 // The InFlag in necessary since all emitted instructions must be
2147 // stuck together.
2148 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2149 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2150 RegsToPass[i].second, InFlag);
2151 InFlag = Chain.getValue(1);
2152 }
2153
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002154 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002155 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002156 //
2157 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002158 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002159 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002160 Ops.push_back(Chain);
2161 Ops.push_back(Callee);
2162
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002163 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002164 // known live into the call.
2165 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2166 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2167 RegsToPass[i].second.getValueType()));
2168
Gabor Greifba36cb52008-08-28 21:40:38 +00002169 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002170 Ops.push_back(InFlag);
2171
Dale Johannesen33c960f2009-02-04 20:06:27 +00002172 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002173 InFlag = Chain.getValue(1);
2174
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002175 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002176 Chain = DAG.getCALLSEQ_END(Chain,
2177 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002178 DAG.getIntPtrConstant(0, true), InFlag);
2179 InFlag = Chain.getValue(1);
2180
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002181 // Handle result values, copying them out of physregs into vregs that we
2182 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002183 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2184 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002185}
2186
Dan Gohman98ca4f22009-08-05 01:29:28 +00002187/// LowerCallResult - Lower the result values of a call into the
2188/// appropriate copies out of appropriate physical registers.
2189SDValue
2190MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002191 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002192 const SmallVectorImpl<ISD::InputArg> &Ins,
2193 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002194 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002195 // Assign locations to each value returned by this call.
2196 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002197 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2198 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002199
Dan Gohman98ca4f22009-08-05 01:29:28 +00002200 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002201
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002202 // Copy all of the result registers out of their specified physreg.
2203 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002204 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002205 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002206 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002207 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002208 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002209
Dan Gohman98ca4f22009-08-05 01:29:28 +00002210 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002211}
2212
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002213//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002214// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002215//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002216static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2217 std::vector<SDValue>& OutChains,
2218 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2219 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2220 unsigned LocMem = VA.getLocMemOffset();
2221 unsigned FirstWord = LocMem / 4;
2222
2223 // copy register A0 - A3 to frame object
2224 for (unsigned i = 0; i < NumWords; ++i) {
2225 unsigned CurWord = FirstWord + i;
2226 if (CurWord >= O32IntRegsSize)
2227 break;
2228
2229 unsigned SrcReg = O32IntRegs[CurWord];
2230 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2231 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2232 DAG.getConstant(i * 4, MVT::i32));
2233 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2234 StorePtr, MachinePointerInfo(), false,
2235 false, 0);
2236 OutChains.push_back(Store);
2237 }
2238}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002239
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002240/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002241/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002242SDValue
2243MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002244 CallingConv::ID CallConv,
2245 bool isVarArg,
2246 const SmallVectorImpl<ISD::InputArg>
2247 &Ins,
2248 DebugLoc dl, SelectionDAG &DAG,
2249 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002250 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002251 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002252 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002253 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002254
Dan Gohman1e93df62010-04-17 14:41:14 +00002255 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002256
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002257 // Used with vargs to acumulate store chains.
2258 std::vector<SDValue> OutChains;
2259
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002260 // Assign locations to all of the incoming arguments.
2261 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002262 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2263 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002264
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002265 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002266 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002267 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002268 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002269
Akira Hatanaka43299772011-05-20 23:22:14 +00002270 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002271
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002272 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002273 CCValAssign &VA = ArgLocs[i];
2274
2275 // Arguments stored on registers
2276 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002277 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002278 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002279 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002280
Owen Anderson825b72b2009-08-11 20:47:22 +00002281 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002282 RC = Mips::CPURegsRegisterClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002283 else if (RegVT == MVT::i64)
2284 RC = Mips::CPU64RegsRegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002285 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002286 RC = Mips::FGR32RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002287 else if (RegVT == MVT::f64)
Akira Hatanakaf40de9d2011-09-26 21:55:17 +00002288 RC = HasMips64 ? Mips::FGR64RegisterClass : Mips::AFGR64RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002289 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002290 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002291
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002292 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002293 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002294 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002295 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002296
2297 // If this is an 8 or 16-bit value, it has been passed promoted
2298 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002299 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002300 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002301 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002302 if (VA.getLocInfo() == CCValAssign::SExt)
2303 Opcode = ISD::AssertSext;
2304 else if (VA.getLocInfo() == CCValAssign::ZExt)
2305 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002306 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002307 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00002308 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00002309 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002310 }
2311
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002312 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002313 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002314 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
2315 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00002316 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002317 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002318 getNextIntArgReg(ArgReg), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002319 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002320 if (!Subtarget->isLittle())
2321 std::swap(ArgValue, ArgValue2);
2322 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2323 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002324 }
2325 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002326
Dan Gohman98ca4f22009-08-05 01:29:28 +00002327 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002328 } else { // VA.isRegLoc()
2329
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002330 // sanity check
2331 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002332
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002333 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2334
2335 if (Flags.isByVal()) {
2336 assert(Subtarget->isABI_O32() &&
2337 "No support for ByVal args by ABIs other than O32 yet.");
2338 assert(Flags.getByValSize() &&
2339 "ByVal args of size 0 should have been ignored by front-end.");
2340 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2341 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2342 true);
2343 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2344 InVals.push_back(FIN);
2345 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2346
2347 continue;
2348 }
2349
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002350 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002351 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
2352 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002353
2354 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002355 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002356 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002357 MachinePointerInfo::getFixedStack(LastFI),
David Greenef6fa1862010-02-15 16:56:10 +00002358 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002359 }
2360 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002361
2362 // The mips ABIs for returning structs by value requires that we copy
2363 // the sret argument into $v0 for the return. Save the argument into
2364 // a virtual register so that we can access it from the return points.
2365 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2366 unsigned Reg = MipsFI->getSRetReturnReg();
2367 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002368 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002369 MipsFI->setSRetReturnReg(Reg);
2370 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002371 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002372 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002373 }
2374
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002375 if (isVarArg && Subtarget->isABI_O32()) {
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002376 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002377 // which is a value necessary to VASTART.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002378 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002379 assert(NextStackOffset % 4 == 0 &&
2380 "NextStackOffset must be aligned to 4-byte boundaries.");
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002381 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
2382 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002383
2384 // If NextStackOffset is smaller than o32's 16-byte reserved argument area,
2385 // copy the integer registers that have not been used for argument passing
2386 // to the caller's stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002387 for (; NextStackOffset < 16; NextStackOffset += 4) {
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002388 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002389 unsigned Idx = NextStackOffset / 4;
2390 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), O32IntRegs[Idx], RC);
2391 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
Akira Hatanaka69c19f72011-05-23 20:16:59 +00002392 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002393 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2394 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2395 MachinePointerInfo(),
2396 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002397 }
2398 }
2399
Akira Hatanaka43299772011-05-20 23:22:14 +00002400 MipsFI->setLastInArgFI(LastFI);
2401
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002402 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002403 // the size of Ins and InVals. This only happens when on varg functions
2404 if (!OutChains.empty()) {
2405 OutChains.push_back(Chain);
2406 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2407 &OutChains[0], OutChains.size());
2408 }
2409
Dan Gohman98ca4f22009-08-05 01:29:28 +00002410 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002411}
2412
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002413//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002414// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002415//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002416
Dan Gohman98ca4f22009-08-05 01:29:28 +00002417SDValue
2418MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002419 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002420 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002421 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002422 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002423
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002424 // CCValAssign - represent the assignment of
2425 // the return value to a location
2426 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002427
2428 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002429 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2430 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002431
Dan Gohman98ca4f22009-08-05 01:29:28 +00002432 // Analize return values.
2433 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002434
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002435 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002436 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002437 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002438 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002439 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002440 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002441 }
2442
Dan Gohman475871a2008-07-27 21:46:04 +00002443 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002444
2445 // Copy the result values into the output registers.
2446 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2447 CCValAssign &VA = RVLocs[i];
2448 assert(VA.isRegLoc() && "Can only return in registers!");
2449
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002450 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002451 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002452
2453 // guarantee that all emitted copies are
2454 // stuck together, avoiding something bad
2455 Flag = Chain.getValue(1);
2456 }
2457
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002458 // The mips ABIs for returning structs by value requires that we copy
2459 // the sret argument into $v0 for the return. We saved the argument into
2460 // a virtual register in the entry block, so now we copy the value out
2461 // and into $v0.
2462 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2463 MachineFunction &MF = DAG.getMachineFunction();
2464 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2465 unsigned Reg = MipsFI->getSRetReturnReg();
2466
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002467 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002468 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002469 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002470
Dale Johannesena05dca42009-02-04 23:02:30 +00002471 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002472 Flag = Chain.getValue(1);
2473 }
2474
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002475 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002476 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002477 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002478 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002479 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002480 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002481 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002482}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002483
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002484//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002485// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002486//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002487
2488/// getConstraintType - Given a constraint letter, return the type of
2489/// constraint it is for this target.
2490MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002491getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002492{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002493 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002494 // GCC config/mips/constraints.md
2495 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002496 // 'd' : An address register. Equivalent to r
2497 // unless generating MIPS16 code.
2498 // 'y' : Equivalent to r; retained for
2499 // backwards compatibility.
2500 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002501 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002502 switch (Constraint[0]) {
2503 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002504 case 'd':
2505 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002506 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002507 return C_RegisterClass;
2508 break;
2509 }
2510 }
2511 return TargetLowering::getConstraintType(Constraint);
2512}
2513
John Thompson44ab89e2010-10-29 17:29:13 +00002514/// Examine constraint type and operand type and determine a weight value.
2515/// This object must already have been set up with the operand type
2516/// and the current alternative constraint selected.
2517TargetLowering::ConstraintWeight
2518MipsTargetLowering::getSingleConstraintMatchWeight(
2519 AsmOperandInfo &info, const char *constraint) const {
2520 ConstraintWeight weight = CW_Invalid;
2521 Value *CallOperandVal = info.CallOperandVal;
2522 // If we don't have a value, we can't do a match,
2523 // but allow it at the lowest weight.
2524 if (CallOperandVal == NULL)
2525 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002526 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002527 // Look at the constraint type.
2528 switch (*constraint) {
2529 default:
2530 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2531 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002532 case 'd':
2533 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002534 if (type->isIntegerTy())
2535 weight = CW_Register;
2536 break;
2537 case 'f':
2538 if (type->isFloatTy())
2539 weight = CW_Register;
2540 break;
2541 }
2542 return weight;
2543}
2544
Eric Christopher38d64262011-06-29 19:33:04 +00002545/// Given a register class constraint, like 'r', if this corresponds directly
2546/// to an LLVM register class, return a register of 0 and the register class
2547/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002548std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002549getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002550{
2551 if (Constraint.size() == 1) {
2552 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002553 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2554 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002555 case 'r':
2556 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002557 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002558 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002559 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002560 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002561 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2562 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002563 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002564 }
2565 }
2566 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2567}
2568
Dan Gohman6520e202008-10-18 02:06:02 +00002569bool
2570MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2571 // The Mips target isn't yet aware of offsets.
2572 return false;
2573}
Evan Chengeb2f9692009-10-27 19:56:55 +00002574
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002575bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2576 if (VT != MVT::f32 && VT != MVT::f64)
2577 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002578 if (Imm.isNegZero())
2579 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002580 return Imm.isZero();
2581}