blob: 272b8c57f2fda8a21ed86e67eea710e12a79a7fd [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 Lopes47b92f32011-11-11 22:58:42 +000027#include "MCTargetDesc/MipsBaseInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000028#include "llvm/CodeGen/CallingConvLower.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000034#include "llvm/CodeGen/ValueTypes.h"
35#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000036#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000037using namespace llvm;
38
Akira Hatanakadbe9a312011-08-18 20:07:42 +000039// If I is a shifted mask, set the size (Size) and the first bit of the
40// mask (Pos), and return true.
Akira Hatanaka854a7db2011-08-19 22:59:00 +000041// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
42static bool IsShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanakad6bc5232011-12-05 21:26:34 +000043 if (!isShiftedMask_64(I))
Akira Hatanaka854a7db2011-08-19 22:59:00 +000044 return false;
Akira Hatanakabb15e112011-08-17 02:05:42 +000045
Akira Hatanakad6bc5232011-12-05 21:26:34 +000046 Size = CountPopulation_64(I);
47 Pos = CountTrailingZeros_64(I);
Akira Hatanakadbe9a312011-08-18 20:07:42 +000048 return true;
Akira Hatanakabb15e112011-08-17 02:05:42 +000049}
50
Chris Lattnerf0144122009-07-28 03:13:23 +000051const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
52 switch (Opcode) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000053 case MipsISD::JmpLink: return "MipsISD::JmpLink";
54 case MipsISD::Hi: return "MipsISD::Hi";
55 case MipsISD::Lo: return "MipsISD::Lo";
56 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +000057 case MipsISD::TlsGd: return "MipsISD::TlsGd";
58 case MipsISD::TprelHi: return "MipsISD::TprelHi";
59 case MipsISD::TprelLo: return "MipsISD::TprelLo";
60 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000061 case MipsISD::Ret: return "MipsISD::Ret";
62 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
63 case MipsISD::FPCmp: return "MipsISD::FPCmp";
64 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
65 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
66 case MipsISD::FPRound: return "MipsISD::FPRound";
67 case MipsISD::MAdd: return "MipsISD::MAdd";
68 case MipsISD::MAddu: return "MipsISD::MAddu";
69 case MipsISD::MSub: return "MipsISD::MSub";
70 case MipsISD::MSubu: return "MipsISD::MSubu";
71 case MipsISD::DivRem: return "MipsISD::DivRem";
72 case MipsISD::DivRemU: return "MipsISD::DivRemU";
73 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
74 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanaka342837d2011-05-28 01:07:07 +000075 case MipsISD::WrapperPIC: return "MipsISD::WrapperPIC";
Akira Hatanaka21afc632011-06-21 00:40:49 +000076 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
Akira Hatanakadb548262011-07-19 23:30:50 +000077 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanakabb15e112011-08-17 02:05:42 +000078 case MipsISD::Ext: return "MipsISD::Ext";
79 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanaka0f843822011-06-07 18:58:42 +000080 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000081 }
82}
83
84MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000085MipsTargetLowering(MipsTargetMachine &TM)
Akira Hatanaka8b4198d2011-09-26 21:47:02 +000086 : TargetLowering(TM, new MipsTargetObjectFile()),
87 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +000088 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
89 IsO32(Subtarget->isABI_O32()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000090
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000091 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000092 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000093 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +000094 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000095
96 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000097 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
98 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000099
Akira Hatanaka95934842011-09-24 01:34:44 +0000100 if (HasMips64)
101 addRegisterClass(MVT::i64, Mips::CPU64RegsRegisterClass);
102
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000103 // When dealing with single precision only, use libcalls
Akira Hatanaka792016b2011-09-23 18:28:39 +0000104 if (!Subtarget->isSingleFloat()) {
105 if (HasMips64)
106 addRegisterClass(MVT::f64, Mips::FGR64RegisterClass);
107 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000108 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Akira Hatanaka792016b2011-09-23 18:28:39 +0000109 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000110
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000111 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000112 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
113 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
114 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000115
Eli Friedman6055a6a2009-07-17 04:07:24 +0000116 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000117 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
118 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000119
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000120 // Used by legalize types to correctly generate the setcc result.
121 // Without this, every float setcc comes with a AND/OR with the result,
122 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000123 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000124 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000125
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000126 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Akira Hatanakaa5903ac2011-10-11 00:55:05 +0000128 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000129 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Akira Hatanaka9b944a82011-11-16 22:42:10 +0000130 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000131 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
132 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +0000133 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Akira Hatanaka620db892011-11-16 22:44:38 +0000135 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000136 setOperationAction(ISD::SELECT, MVT::f32, Custom);
137 setOperationAction(ISD::SELECT, MVT::f64, Custom);
138 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
140 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000141 setOperationAction(ISD::VASTART, MVT::Other, Custom);
142
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000143 setOperationAction(ISD::SDIV, MVT::i32, Expand);
144 setOperationAction(ISD::SREM, MVT::i32, Expand);
145 setOperationAction(ISD::UDIV, MVT::i32, Expand);
146 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000147 setOperationAction(ISD::SDIV, MVT::i64, Expand);
148 setOperationAction(ISD::SREM, MVT::i64, Expand);
149 setOperationAction(ISD::UDIV, MVT::i64, Expand);
150 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000151
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000152 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000153 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
154 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
155 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
156 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
157 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
158 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
159 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
160 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
161 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000162 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000163
Akira Hatanaka56633442011-09-20 23:53:09 +0000164 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000165 setOperationAction(ISD::ROTR, MVT::i32, Expand);
166
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000167 if (!Subtarget->hasMips64r2())
168 setOperationAction(ISD::ROTR, MVT::i64, Expand);
169
Owen Anderson825b72b2009-08-11 20:47:22 +0000170 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
171 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
172 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000173 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
174 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000175 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000176 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000178 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000179 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
180 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000181 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000182 setOperationAction(ISD::FLOG, MVT::f32, Expand);
183 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
184 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
185 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000186 setOperationAction(ISD::FMA, MVT::f32, Expand);
187 setOperationAction(ISD::FMA, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000188
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000189 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
190 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Eric Christopher471e4222011-06-08 23:55:35 +0000191
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000192 setOperationAction(ISD::VAARG, MVT::Other, Expand);
193 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
194 setOperationAction(ISD::VAEND, MVT::Other, Expand);
195
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000196 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000197 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
198 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000199
Akira Hatanakadb548262011-07-19 23:30:50 +0000200 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000201 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000202
Eli Friedman4db5aca2011-08-29 18:23:02 +0000203 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
204 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
205
Eli Friedman26689ac2011-08-03 21:06:02 +0000206 setInsertFencesForAtomic(true);
207
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000208 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000209 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000210
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000211 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000214 }
215
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000216 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000217 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000218
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000219 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000220 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000221
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000222 setTargetDAGCombine(ISD::ADDE);
223 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000224 setTargetDAGCombine(ISD::SDIVREM);
225 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000226 setTargetDAGCombine(ISD::SETCC);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000227 setTargetDAGCombine(ISD::AND);
228 setTargetDAGCombine(ISD::OR);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000229
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000230 setMinFunctionAlignment(2);
231
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000232 setStackPointerRegisterToSaveRestore(Mips::SP);
233 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000234
235 setExceptionPointerRegister(Mips::A0);
236 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000237}
238
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000239bool MipsTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Akira Hatanaka511961a2011-08-17 18:49:18 +0000240 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
Akira Hatanaka7bd19bd2011-10-11 00:27:28 +0000241 return SVT == MVT::i64 || SVT == MVT::i32 || SVT == MVT::i16;
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000242}
243
Duncan Sands28b77e92011-09-06 19:07:46 +0000244EVT MipsTargetLowering::getSetCCResultType(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000246}
247
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000248// SelectMadd -
249// Transforms a subgraph in CurDAG if the following pattern is found:
250// (addc multLo, Lo0), (adde multHi, Hi0),
251// where,
252// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000253// Lo0: initial value of Lo register
254// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000255// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000256static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000257 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000258 // for the matching to be successful.
259 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
260
261 if (ADDCNode->getOpcode() != ISD::ADDC)
262 return false;
263
264 SDValue MultHi = ADDENode->getOperand(0);
265 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000266 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000267 unsigned MultOpc = MultHi.getOpcode();
268
269 // MultHi and MultLo must be generated by the same node,
270 if (MultLo.getNode() != MultNode)
271 return false;
272
273 // and it must be a multiplication.
274 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
275 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000276
277 // MultLo amd MultHi must be the first and second output of MultNode
278 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000279 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
280 return false;
281
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000282 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000283 // of the values of MultNode, in which case MultNode will be removed in later
284 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000285 // If there exist users other than ADDENode or ADDCNode, this function returns
286 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000287 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000288 // produced.
289 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
290 return false;
291
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000292 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000293 DebugLoc dl = ADDENode->getDebugLoc();
294
295 // create MipsMAdd(u) node
296 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000297
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000298 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
299 MVT::Glue,
300 MultNode->getOperand(0),// Factor 0
301 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000302 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000303 ADDENode->getOperand(1));// Hi0
304
305 // create CopyFromReg nodes
306 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
307 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000308 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000309 Mips::HI, MVT::i32,
310 CopyFromLo.getValue(2));
311
312 // replace uses of adde and addc here
313 if (!SDValue(ADDCNode, 0).use_empty())
314 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
315
316 if (!SDValue(ADDENode, 0).use_empty())
317 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
318
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000319 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000320}
321
322// SelectMsub -
323// Transforms a subgraph in CurDAG if the following pattern is found:
324// (addc Lo0, multLo), (sube Hi0, multHi),
325// where,
326// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000327// Lo0: initial value of Lo register
328// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000329// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000330static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000331 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000332 // for the matching to be successful.
333 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
334
335 if (SUBCNode->getOpcode() != ISD::SUBC)
336 return false;
337
338 SDValue MultHi = SUBENode->getOperand(1);
339 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000340 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000341 unsigned MultOpc = MultHi.getOpcode();
342
343 // MultHi and MultLo must be generated by the same node,
344 if (MultLo.getNode() != MultNode)
345 return false;
346
347 // and it must be a multiplication.
348 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
349 return false;
350
351 // MultLo amd MultHi must be the first and second output of MultNode
352 // respectively.
353 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
354 return false;
355
356 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
357 // of the values of MultNode, in which case MultNode will be removed in later
358 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000359 // If there exist users other than SUBENode or SUBCNode, this function returns
360 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000361 // instruction node rather than a pair of MULT and MSUB instructions being
362 // produced.
363 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
364 return false;
365
366 SDValue Chain = CurDAG->getEntryNode();
367 DebugLoc dl = SUBENode->getDebugLoc();
368
369 // create MipsSub(u) node
370 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
371
372 SDValue MSub = CurDAG->getNode(MultOpc, dl,
373 MVT::Glue,
374 MultNode->getOperand(0),// Factor 0
375 MultNode->getOperand(1),// Factor 1
376 SUBCNode->getOperand(0),// Lo0
377 SUBENode->getOperand(0));// Hi0
378
379 // create CopyFromReg nodes
380 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
381 MSub);
382 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
383 Mips::HI, MVT::i32,
384 CopyFromLo.getValue(2));
385
386 // replace uses of sube and subc here
387 if (!SDValue(SUBCNode, 0).use_empty())
388 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
389
390 if (!SDValue(SUBENode, 0).use_empty())
391 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
392
393 return true;
394}
395
396static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
397 TargetLowering::DAGCombinerInfo &DCI,
398 const MipsSubtarget* Subtarget) {
399 if (DCI.isBeforeLegalize())
400 return SDValue();
401
Akira Hatanakae184fec2011-11-11 04:18:21 +0000402 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
403 SelectMadd(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000404 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000405
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000406 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000407}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000408
409static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
410 TargetLowering::DAGCombinerInfo &DCI,
411 const MipsSubtarget* Subtarget) {
412 if (DCI.isBeforeLegalize())
413 return SDValue();
414
Akira Hatanakae184fec2011-11-11 04:18:21 +0000415 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
416 SelectMsub(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000417 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000418
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000419 return SDValue();
420}
421
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000422static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
423 TargetLowering::DAGCombinerInfo &DCI,
424 const MipsSubtarget* Subtarget) {
425 if (DCI.isBeforeLegalizeOps())
426 return SDValue();
427
Akira Hatanakadda4a072011-10-03 21:06:13 +0000428 EVT Ty = N->getValueType(0);
429 unsigned LO = (Ty == MVT::i32) ? Mips::LO : Mips::LO64;
430 unsigned HI = (Ty == MVT::i32) ? Mips::HI : Mips::HI64;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000431 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
432 MipsISD::DivRemU;
433 DebugLoc dl = N->getDebugLoc();
434
435 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
436 N->getOperand(0), N->getOperand(1));
437 SDValue InChain = DAG.getEntryNode();
438 SDValue InGlue = DivRem;
439
440 // insert MFLO
441 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakadda4a072011-10-03 21:06:13 +0000442 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000443 InGlue);
444 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
445 InChain = CopyFromLo.getValue(1);
446 InGlue = CopyFromLo.getValue(2);
447 }
448
449 // insert MFHI
450 if (N->hasAnyUseOfValue(1)) {
451 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000452 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000453 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
454 }
455
456 return SDValue();
457}
458
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000459static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
460 switch (CC) {
461 default: llvm_unreachable("Unknown fp condition code!");
462 case ISD::SETEQ:
463 case ISD::SETOEQ: return Mips::FCOND_OEQ;
464 case ISD::SETUNE: return Mips::FCOND_UNE;
465 case ISD::SETLT:
466 case ISD::SETOLT: return Mips::FCOND_OLT;
467 case ISD::SETGT:
468 case ISD::SETOGT: return Mips::FCOND_OGT;
469 case ISD::SETLE:
470 case ISD::SETOLE: return Mips::FCOND_OLE;
471 case ISD::SETGE:
472 case ISD::SETOGE: return Mips::FCOND_OGE;
473 case ISD::SETULT: return Mips::FCOND_ULT;
474 case ISD::SETULE: return Mips::FCOND_ULE;
475 case ISD::SETUGT: return Mips::FCOND_UGT;
476 case ISD::SETUGE: return Mips::FCOND_UGE;
477 case ISD::SETUO: return Mips::FCOND_UN;
478 case ISD::SETO: return Mips::FCOND_OR;
479 case ISD::SETNE:
480 case ISD::SETONE: return Mips::FCOND_ONE;
481 case ISD::SETUEQ: return Mips::FCOND_UEQ;
482 }
483}
484
485
486// Returns true if condition code has to be inverted.
487static bool InvertFPCondCode(Mips::CondCode CC) {
488 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
489 return false;
490
491 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
492 return true;
493
494 assert(false && "Illegal Condition Code");
495 return false;
496}
497
498// Creates and returns an FPCmp node from a setcc node.
499// Returns Op if setcc is not a floating point comparison.
500static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
501 // must be a SETCC node
502 if (Op.getOpcode() != ISD::SETCC)
503 return Op;
504
505 SDValue LHS = Op.getOperand(0);
506
507 if (!LHS.getValueType().isFloatingPoint())
508 return Op;
509
510 SDValue RHS = Op.getOperand(1);
511 DebugLoc dl = Op.getDebugLoc();
512
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000513 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
514 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000515 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
516
517 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
518 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
519}
520
521// Creates and returns a CMovFPT/F node.
522static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
523 SDValue False, DebugLoc DL) {
524 bool invert = InvertFPCondCode((Mips::CondCode)
525 cast<ConstantSDNode>(Cond.getOperand(2))
526 ->getSExtValue());
527
528 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
529 True.getValueType(), True, False, Cond);
530}
531
532static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
533 TargetLowering::DAGCombinerInfo &DCI,
534 const MipsSubtarget* Subtarget) {
535 if (DCI.isBeforeLegalizeOps())
536 return SDValue();
537
538 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
539
540 if (Cond.getOpcode() != MipsISD::FPCmp)
541 return SDValue();
542
543 SDValue True = DAG.getConstant(1, MVT::i32);
544 SDValue False = DAG.getConstant(0, MVT::i32);
545
546 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
547}
548
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000549static SDValue PerformANDCombine(SDNode *N, SelectionDAG& DAG,
550 TargetLowering::DAGCombinerInfo &DCI,
551 const MipsSubtarget* Subtarget) {
552 // Pattern match EXT.
553 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
554 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000555 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000556 return SDValue();
557
558 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000559 unsigned ShiftRightOpc = ShiftRight.getOpcode();
560
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000561 // Op's first operand must be a shift right.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000562 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000563 return SDValue();
564
565 // The second operand of the shift must be an immediate.
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000566 ConstantSDNode *CN;
567 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
568 return SDValue();
569
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000570 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000571 uint64_t SMPos, SMSize;
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000572
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000573 // Op's second operand must be a shifted mask.
574 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000575 !IsShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000576 return SDValue();
577
578 // Return if the shifted mask does not start at bit 0 or the sum of its size
579 // and Pos exceeds the word's size.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000580 EVT ValTy = N->getValueType(0);
581 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000582 return SDValue();
583
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000584 return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), ValTy,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000585 ShiftRight.getOperand(0),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000586 DAG.getConstant(Pos, MVT::i32),
587 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000588}
589
590static SDValue PerformORCombine(SDNode *N, SelectionDAG& DAG,
591 TargetLowering::DAGCombinerInfo &DCI,
592 const MipsSubtarget* Subtarget) {
593 // Pattern match INS.
594 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
595 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
596 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000597 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000598 return SDValue();
599
600 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
601 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
602 ConstantSDNode *CN;
603
604 // See if Op's first operand matches (and $src1 , mask0).
605 if (And0.getOpcode() != ISD::AND)
606 return SDValue();
607
608 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000609 !IsShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000610 return SDValue();
611
612 // See if Op's second operand matches (and (shl $src, pos), mask1).
613 if (And1.getOpcode() != ISD::AND)
614 return SDValue();
615
616 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000617 !IsShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000618 return SDValue();
619
620 // The shift masks must have the same position and size.
621 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
622 return SDValue();
623
624 SDValue Shl = And1.getOperand(0);
625 if (Shl.getOpcode() != ISD::SHL)
626 return SDValue();
627
628 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
629 return SDValue();
630
631 unsigned Shamt = CN->getZExtValue();
632
633 // Return if the shift amount and the first bit position of mask are not the
634 // same.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000635 EVT ValTy = N->getValueType(0);
636 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000637 return SDValue();
638
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000639 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), ValTy,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000640 Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000641 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000642 DAG.getConstant(SMSize0, MVT::i32),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000643 And0.getOperand(0));
644}
645
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000646SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000647 const {
648 SelectionDAG &DAG = DCI.DAG;
649 unsigned opc = N->getOpcode();
650
651 switch (opc) {
652 default: break;
653 case ISD::ADDE:
654 return PerformADDECombine(N, DAG, DCI, Subtarget);
655 case ISD::SUBE:
656 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000657 case ISD::SDIVREM:
658 case ISD::UDIVREM:
659 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000660 case ISD::SETCC:
661 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000662 case ISD::AND:
663 return PerformANDCombine(N, DAG, DCI, Subtarget);
664 case ISD::OR:
665 return PerformORCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000666 }
667
668 return SDValue();
669}
670
Dan Gohman475871a2008-07-27 21:46:04 +0000671SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000672LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000673{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000674 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000675 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000676 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000677 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
678 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000679 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000680 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000681 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
682 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000683 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000684 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000685 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000686 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Akira Hatanakadb548262011-07-19 23:30:50 +0000687 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +0000688 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000689 }
Dan Gohman475871a2008-07-27 21:46:04 +0000690 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000691}
692
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000693//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000694// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000695//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000696
697// AddLiveIn - This helper function adds the specified physical register to the
698// MachineFunction as a live in value. It also creates a corresponding
699// virtual register for it.
700static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000701AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000702{
703 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000704 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
705 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000706 return VReg;
707}
708
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000709// Get fp branch code (not opcode) from condition code.
710static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
711 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
712 return Mips::BRANCH_T;
713
714 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
715 return Mips::BRANCH_F;
716
717 return Mips::BRANCH_INVALID;
718}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000719
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000720/*
Akira Hatanaka14487d42011-06-07 19:28:39 +0000721static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
722 DebugLoc dl,
723 const MipsSubtarget* Subtarget,
724 const TargetInstrInfo *TII,
725 bool isFPCmp, unsigned Opc) {
726 // There is no need to expand CMov instructions if target has
727 // conditional moves.
728 if (Subtarget->hasCondMov())
729 return BB;
730
731 // To "insert" a SELECT_CC instruction, we actually have to insert the
732 // diamond control-flow pattern. The incoming instruction knows the
733 // destination vreg to set, the condition code register to branch on, the
734 // true/false values to select between, and a branch opcode to use.
735 const BasicBlock *LLVM_BB = BB->getBasicBlock();
736 MachineFunction::iterator It = BB;
737 ++It;
738
739 // thisMBB:
740 // ...
741 // TrueVal = ...
742 // setcc r1, r2, r3
743 // bNE r1, r0, copy1MBB
744 // fallthrough --> copy0MBB
745 MachineBasicBlock *thisMBB = BB;
746 MachineFunction *F = BB->getParent();
747 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
748 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
749 F->insert(It, copy0MBB);
750 F->insert(It, sinkMBB);
751
752 // Transfer the remainder of BB and its successor edges to sinkMBB.
753 sinkMBB->splice(sinkMBB->begin(), BB,
754 llvm::next(MachineBasicBlock::iterator(MI)),
755 BB->end());
756 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
757
758 // Next, add the true and fallthrough blocks as its successors.
759 BB->addSuccessor(copy0MBB);
760 BB->addSuccessor(sinkMBB);
761
762 // Emit the right instruction according to the type of the operands compared
763 if (isFPCmp)
764 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
765 else
766 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
767 .addReg(Mips::ZERO).addMBB(sinkMBB);
768
769 // copy0MBB:
770 // %FalseValue = ...
771 // # fallthrough to sinkMBB
772 BB = copy0MBB;
773
774 // Update machine-CFG edges
775 BB->addSuccessor(sinkMBB);
776
777 // sinkMBB:
778 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
779 // ...
780 BB = sinkMBB;
781
782 if (isFPCmp)
783 BuildMI(*BB, BB->begin(), dl,
784 TII->get(Mips::PHI), MI->getOperand(0).getReg())
785 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
786 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
787 else
788 BuildMI(*BB, BB->begin(), dl,
789 TII->get(Mips::PHI), MI->getOperand(0).getReg())
790 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
791 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
792
793 MI->eraseFromParent(); // The pseudo instruction is gone now.
794 return BB;
795}
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000796*/
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000797MachineBasicBlock *
798MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000799 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000800 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000801 default:
802 assert(false && "Unexpected instr type to insert");
803 return NULL;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000804 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000805 case Mips::ATOMIC_LOAD_ADD_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000806 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
807 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000808 case Mips::ATOMIC_LOAD_ADD_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000809 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
810 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000811 case Mips::ATOMIC_LOAD_ADD_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000812 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000813 case Mips::ATOMIC_LOAD_ADD_I64:
814 case Mips::ATOMIC_LOAD_ADD_I64_P8:
815 return EmitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000816
817 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000818 case Mips::ATOMIC_LOAD_AND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000819 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
820 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000821 case Mips::ATOMIC_LOAD_AND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000822 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
823 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000824 case Mips::ATOMIC_LOAD_AND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000825 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000826 case Mips::ATOMIC_LOAD_AND_I64:
827 case Mips::ATOMIC_LOAD_AND_I64_P8:
Akira Hatanaka73866122011-11-12 02:38:12 +0000828 return EmitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000829
830 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000831 case Mips::ATOMIC_LOAD_OR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000832 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
833 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000834 case Mips::ATOMIC_LOAD_OR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000835 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
836 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000837 case Mips::ATOMIC_LOAD_OR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000838 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000839 case Mips::ATOMIC_LOAD_OR_I64:
840 case Mips::ATOMIC_LOAD_OR_I64_P8:
841 return EmitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000842
843 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000844 case Mips::ATOMIC_LOAD_XOR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000845 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
846 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000847 case Mips::ATOMIC_LOAD_XOR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000848 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
849 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000850 case Mips::ATOMIC_LOAD_XOR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000851 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000852 case Mips::ATOMIC_LOAD_XOR_I64:
853 case Mips::ATOMIC_LOAD_XOR_I64_P8:
854 return EmitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000855
856 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000857 case Mips::ATOMIC_LOAD_NAND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000858 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
859 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000860 case Mips::ATOMIC_LOAD_NAND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000861 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
862 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000863 case Mips::ATOMIC_LOAD_NAND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000864 return EmitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000865 case Mips::ATOMIC_LOAD_NAND_I64:
866 case Mips::ATOMIC_LOAD_NAND_I64_P8:
867 return EmitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000868
869 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000870 case Mips::ATOMIC_LOAD_SUB_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000871 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
872 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000873 case Mips::ATOMIC_LOAD_SUB_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000874 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
875 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000876 case Mips::ATOMIC_LOAD_SUB_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000877 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000878 case Mips::ATOMIC_LOAD_SUB_I64:
879 case Mips::ATOMIC_LOAD_SUB_I64_P8:
880 return EmitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000881
882 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000883 case Mips::ATOMIC_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000884 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
885 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000886 case Mips::ATOMIC_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000887 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
888 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000889 case Mips::ATOMIC_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000890 return EmitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000891 case Mips::ATOMIC_SWAP_I64:
892 case Mips::ATOMIC_SWAP_I64_P8:
893 return EmitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000894
895 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000896 case Mips::ATOMIC_CMP_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000897 return EmitAtomicCmpSwapPartword(MI, BB, 1);
898 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000899 case Mips::ATOMIC_CMP_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000900 return EmitAtomicCmpSwapPartword(MI, BB, 2);
901 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000902 case Mips::ATOMIC_CMP_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000903 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000904 case Mips::ATOMIC_CMP_SWAP_I64:
905 case Mips::ATOMIC_CMP_SWAP_I64_P8:
906 return EmitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000907 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000908}
909
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000910// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
911// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
912MachineBasicBlock *
913MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000914 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000915 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000916 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000917
918 MachineFunction *MF = BB->getParent();
919 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000920 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000921 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
922 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000923 unsigned LL, SC, AND, NOR, ZERO, BEQ;
924
925 if (Size == 4) {
926 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
927 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
928 AND = Mips::AND;
929 NOR = Mips::NOR;
930 ZERO = Mips::ZERO;
931 BEQ = Mips::BEQ;
932 }
933 else {
934 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
935 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
936 AND = Mips::AND64;
937 NOR = Mips::NOR64;
938 ZERO = Mips::ZERO_64;
939 BEQ = Mips::BEQ64;
940 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000941
Akira Hatanaka4061da12011-07-19 20:11:17 +0000942 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000943 unsigned Ptr = MI->getOperand(1).getReg();
944 unsigned Incr = MI->getOperand(2).getReg();
945
Akira Hatanaka4061da12011-07-19 20:11:17 +0000946 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
947 unsigned AndRes = RegInfo.createVirtualRegister(RC);
948 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000949
950 // insert new blocks after the current block
951 const BasicBlock *LLVM_BB = BB->getBasicBlock();
952 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
953 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
954 MachineFunction::iterator It = BB;
955 ++It;
956 MF->insert(It, loopMBB);
957 MF->insert(It, exitMBB);
958
959 // Transfer the remainder of BB and its successor edges to exitMBB.
960 exitMBB->splice(exitMBB->begin(), BB,
961 llvm::next(MachineBasicBlock::iterator(MI)),
962 BB->end());
963 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
964
965 // thisMBB:
966 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000967 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000968 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000969 loopMBB->addSuccessor(loopMBB);
970 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000971
972 // loopMBB:
973 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000974 // <binop> storeval, oldval, incr
975 // sc success, storeval, 0(ptr)
976 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000977 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +0000978 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000979 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000980 // and andres, oldval, incr
981 // nor storeval, $0, andres
Akira Hatanaka59068062011-11-11 04:14:30 +0000982 BuildMI(BB, dl, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
983 BuildMI(BB, dl, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000984 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000985 // <binop> storeval, oldval, incr
986 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000987 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000988 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000989 }
Akira Hatanaka59068062011-11-11 04:14:30 +0000990 BuildMI(BB, dl, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
991 BuildMI(BB, dl, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000992
993 MI->eraseFromParent(); // The instruction is gone now.
994
Akira Hatanaka939ece12011-07-19 03:42:13 +0000995 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000996}
997
998MachineBasicBlock *
999MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001000 MachineBasicBlock *BB,
1001 unsigned Size, unsigned BinOpcode,
1002 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001003 assert((Size == 1 || Size == 2) &&
1004 "Unsupported size for EmitAtomicBinaryPartial.");
1005
1006 MachineFunction *MF = BB->getParent();
1007 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1008 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1009 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1010 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001011 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1012 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001013
1014 unsigned Dest = MI->getOperand(0).getReg();
1015 unsigned Ptr = MI->getOperand(1).getReg();
1016 unsigned Incr = MI->getOperand(2).getReg();
1017
Akira Hatanaka4061da12011-07-19 20:11:17 +00001018 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1019 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001020 unsigned Mask = RegInfo.createVirtualRegister(RC);
1021 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001022 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1023 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001024 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001025 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1026 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1027 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1028 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1029 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001030 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001031 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1032 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1033 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1034 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1035 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001036
1037 // insert new blocks after the current block
1038 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1039 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001040 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001041 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1042 MachineFunction::iterator It = BB;
1043 ++It;
1044 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001045 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001046 MF->insert(It, exitMBB);
1047
1048 // Transfer the remainder of BB and its successor edges to exitMBB.
1049 exitMBB->splice(exitMBB->begin(), BB,
1050 llvm::next(MachineBasicBlock::iterator(MI)),
1051 BB->end());
1052 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1053
Akira Hatanaka81b44112011-07-19 17:09:53 +00001054 BB->addSuccessor(loopMBB);
1055 loopMBB->addSuccessor(loopMBB);
1056 loopMBB->addSuccessor(sinkMBB);
1057 sinkMBB->addSuccessor(exitMBB);
1058
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001059 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001060 // addiu masklsb2,$0,-4 # 0xfffffffc
1061 // and alignedaddr,ptr,masklsb2
1062 // andi ptrlsb2,ptr,3
1063 // sll shiftamt,ptrlsb2,3
1064 // ori maskupper,$0,255 # 0xff
1065 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001066 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001067 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001068
1069 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001070 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1071 .addReg(Mips::ZERO).addImm(-4);
1072 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1073 .addReg(Ptr).addReg(MaskLSB2);
1074 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1075 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1076 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1077 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001078 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1079 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001080 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001081 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001082
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001083
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001084 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001085 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001086 // ll oldval,0(alignedaddr)
1087 // binop binopres,oldval,incr2
1088 // and newval,binopres,mask
1089 // and maskedoldval0,oldval,mask2
1090 // or storeval,maskedoldval0,newval
1091 // sc success,storeval,0(alignedaddr)
1092 // beq success,$0,loopMBB
1093
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001094 // atomic.swap
1095 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001096 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001097 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001098 // and maskedoldval0,oldval,mask2
1099 // or storeval,maskedoldval0,newval
1100 // sc success,storeval,0(alignedaddr)
1101 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001102
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001103 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001104 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001105 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001106 // and andres, oldval, incr2
1107 // nor binopres, $0, andres
1108 // and newval, binopres, mask
1109 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1110 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1111 .addReg(Mips::ZERO).addReg(AndRes);
1112 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001113 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001114 // <binop> binopres, oldval, incr2
1115 // and newval, binopres, mask
1116 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1117 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001118 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001119 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001120 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001121 }
1122
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001123 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001124 .addReg(OldVal).addReg(Mask2);
1125 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001126 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001127 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001128 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001129 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001130 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001131
Akira Hatanaka939ece12011-07-19 03:42:13 +00001132 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001133 // and maskedoldval1,oldval,mask
1134 // srl srlres,maskedoldval1,shiftamt
1135 // sll sllres,srlres,24
1136 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001137 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001138 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001139
Akira Hatanaka4061da12011-07-19 20:11:17 +00001140 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1141 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001142 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1143 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001144 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1145 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001146 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001147 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001148
1149 MI->eraseFromParent(); // The instruction is gone now.
1150
Akira Hatanaka939ece12011-07-19 03:42:13 +00001151 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001152}
1153
1154MachineBasicBlock *
1155MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001156 MachineBasicBlock *BB,
1157 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001158 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001159
1160 MachineFunction *MF = BB->getParent();
1161 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001162 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001163 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1164 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001165 unsigned LL, SC, ZERO, BNE, BEQ;
1166
1167 if (Size == 4) {
1168 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1169 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1170 ZERO = Mips::ZERO;
1171 BNE = Mips::BNE;
1172 BEQ = Mips::BEQ;
1173 }
1174 else {
1175 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1176 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1177 ZERO = Mips::ZERO_64;
1178 BNE = Mips::BNE64;
1179 BEQ = Mips::BEQ64;
1180 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001181
1182 unsigned Dest = MI->getOperand(0).getReg();
1183 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001184 unsigned OldVal = MI->getOperand(2).getReg();
1185 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001186
Akira Hatanaka4061da12011-07-19 20:11:17 +00001187 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001188
1189 // insert new blocks after the current block
1190 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1191 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1192 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1193 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1194 MachineFunction::iterator It = BB;
1195 ++It;
1196 MF->insert(It, loop1MBB);
1197 MF->insert(It, loop2MBB);
1198 MF->insert(It, exitMBB);
1199
1200 // Transfer the remainder of BB and its successor edges to exitMBB.
1201 exitMBB->splice(exitMBB->begin(), BB,
1202 llvm::next(MachineBasicBlock::iterator(MI)),
1203 BB->end());
1204 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1205
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001206 // thisMBB:
1207 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001208 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001209 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001210 loop1MBB->addSuccessor(exitMBB);
1211 loop1MBB->addSuccessor(loop2MBB);
1212 loop2MBB->addSuccessor(loop1MBB);
1213 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001214
1215 // loop1MBB:
1216 // ll dest, 0(ptr)
1217 // bne dest, oldval, exitMBB
1218 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001219 BuildMI(BB, dl, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1220 BuildMI(BB, dl, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001221 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001222
1223 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001224 // sc success, newval, 0(ptr)
1225 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001226 BB = loop2MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001227 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001228 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka59068062011-11-11 04:14:30 +00001229 BuildMI(BB, dl, TII->get(BEQ))
1230 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001231
1232 MI->eraseFromParent(); // The instruction is gone now.
1233
Akira Hatanaka939ece12011-07-19 03:42:13 +00001234 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001235}
1236
1237MachineBasicBlock *
1238MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001239 MachineBasicBlock *BB,
1240 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001241 assert((Size == 1 || Size == 2) &&
1242 "Unsupported size for EmitAtomicCmpSwapPartial.");
1243
1244 MachineFunction *MF = BB->getParent();
1245 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1246 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1247 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1248 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001249 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1250 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001251
1252 unsigned Dest = MI->getOperand(0).getReg();
1253 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001254 unsigned CmpVal = MI->getOperand(2).getReg();
1255 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001256
Akira Hatanaka4061da12011-07-19 20:11:17 +00001257 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1258 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001259 unsigned Mask = RegInfo.createVirtualRegister(RC);
1260 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001261 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1262 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1263 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1264 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1265 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1266 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1267 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1268 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1269 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1270 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1271 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1272 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1273 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1274 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001275
1276 // insert new blocks after the current block
1277 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1278 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1279 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001280 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001281 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1282 MachineFunction::iterator It = BB;
1283 ++It;
1284 MF->insert(It, loop1MBB);
1285 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001286 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001287 MF->insert(It, exitMBB);
1288
1289 // Transfer the remainder of BB and its successor edges to exitMBB.
1290 exitMBB->splice(exitMBB->begin(), BB,
1291 llvm::next(MachineBasicBlock::iterator(MI)),
1292 BB->end());
1293 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1294
Akira Hatanaka81b44112011-07-19 17:09:53 +00001295 BB->addSuccessor(loop1MBB);
1296 loop1MBB->addSuccessor(sinkMBB);
1297 loop1MBB->addSuccessor(loop2MBB);
1298 loop2MBB->addSuccessor(loop1MBB);
1299 loop2MBB->addSuccessor(sinkMBB);
1300 sinkMBB->addSuccessor(exitMBB);
1301
Akira Hatanaka70564a92011-07-19 18:14:26 +00001302 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001303 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001304 // addiu masklsb2,$0,-4 # 0xfffffffc
1305 // and alignedaddr,ptr,masklsb2
1306 // andi ptrlsb2,ptr,3
1307 // sll shiftamt,ptrlsb2,3
1308 // ori maskupper,$0,255 # 0xff
1309 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001310 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001311 // andi maskedcmpval,cmpval,255
1312 // sll shiftedcmpval,maskedcmpval,shiftamt
1313 // andi maskednewval,newval,255
1314 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001315 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001316 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1317 .addReg(Mips::ZERO).addImm(-4);
1318 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1319 .addReg(Ptr).addReg(MaskLSB2);
1320 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1321 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1322 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1323 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001324 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1325 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001326 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001327 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1328 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001329 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1330 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001331 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1332 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001333 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1334 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001335
1336 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001337 // ll oldval,0(alginedaddr)
1338 // and maskedoldval0,oldval,mask
1339 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001340 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001341 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001342 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1343 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001344 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001345 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001346
1347 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001348 // and maskedoldval1,oldval,mask2
1349 // or storeval,maskedoldval1,shiftednewval
1350 // sc success,storeval,0(alignedaddr)
1351 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001352 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001353 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1354 .addReg(OldVal).addReg(Mask2);
1355 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1356 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001357 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001358 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001359 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001360 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001361
Akira Hatanaka939ece12011-07-19 03:42:13 +00001362 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001363 // srl srlres,maskedoldval0,shiftamt
1364 // sll sllres,srlres,24
1365 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001366 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001367 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001368
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001369 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1370 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001371 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1372 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001373 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001374 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001375
1376 MI->eraseFromParent(); // The instruction is gone now.
1377
Akira Hatanaka939ece12011-07-19 03:42:13 +00001378 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001379}
1380
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001381//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001382// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001383//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001384SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001385LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001386{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001387 MachineFunction &MF = DAG.getMachineFunction();
1388 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001389 unsigned SP = IsN64 ? Mips::SP_64 : Mips::SP;
Akira Hatanaka21afc632011-06-21 00:40:49 +00001390
1391 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001392 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1393 "Cannot lower if the alignment of the allocated space is larger than \
1394 that of the stack.");
1395
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001396 SDValue Chain = Op.getOperand(0);
1397 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001398 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001399
1400 // Get a reference from Mips stack pointer
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001401 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, SP, getPointerTy());
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001402
1403 // Subtract the dynamic size from the actual stack size to
1404 // obtain the new stack size.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001405 SDValue Sub = DAG.getNode(ISD::SUB, dl, getPointerTy(), StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001406
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001407 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001408 // must be placed in the stack pointer register.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001409 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, SP, Sub, SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001410
1411 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001412 // value and a chain
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001413 SDVTList VTLs = DAG.getVTList(getPointerTy(), MVT::Other);
Akira Hatanaka21afc632011-06-21 00:40:49 +00001414 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1415 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1416
1417 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001418}
1419
1420SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001421LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001422{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001423 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001424 // the block to branch to if the condition is true.
1425 SDValue Chain = Op.getOperand(0);
1426 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001427 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001428
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001429 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1430
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001431 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001432 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001433 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001434
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001435 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001436 Mips::CondCode CC =
1437 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001438 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001439
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001440 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001441 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001442}
1443
1444SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001445LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001446{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001447 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001448
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001449 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001450 if (Cond.getOpcode() != MipsISD::FPCmp)
1451 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001452
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001453 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1454 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001455}
1456
Dan Gohmand858e902010-04-17 15:26:15 +00001457SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1458 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001459 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001460 DebugLoc dl = Op.getDebugLoc();
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001461 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001462
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001463 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001464 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001465
Chris Lattnerb71b9092009-08-13 06:28:06 +00001466 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001467
Chris Lattnere3736f82009-08-13 05:41:27 +00001468 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001469 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1470 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001471 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001472 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1473 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001474 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001475 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001476 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001477 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1478 MipsII::MO_ABS_HI);
1479 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1480 MipsII::MO_ABS_LO);
1481 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1482 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001483 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001484 }
1485
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001486 EVT ValTy = Op.getValueType();
1487 bool HasGotOfst = (GV->hasInternalLinkage() ||
1488 (GV->hasLocalLinkage() && !isa<Function>(GV)));
1489 unsigned GotFlag = IsN64 ?
1490 (HasGotOfst ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT_DISP) :
1491 MipsII::MO_GOT;
1492 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0, GotFlag);
1493 GA = DAG.getNode(MipsISD::WrapperPIC, dl, ValTy, GA);
1494 SDValue ResNode = DAG.getLoad(ValTy, dl,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001495 DAG.getEntryNode(), GA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001496 false, false, false, 0);
Akira Hatanaka0f843822011-06-07 18:58:42 +00001497 // On functions and global targets not internal linked only
1498 // a load from got/GP is necessary for PIC to work.
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001499 if (!HasGotOfst)
Akira Hatanaka0f843822011-06-07 18:58:42 +00001500 return ResNode;
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001501 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0,
1502 IsN64 ? MipsII::MO_GOT_OFST :
1503 MipsII::MO_ABS_LO);
1504 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, GALo);
1505 return DAG.getNode(ISD::ADD, dl, ValTy, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001506}
1507
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001508SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1509 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001510 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1511 // FIXME there isn't actually debug info here
1512 DebugLoc dl = Op.getDebugLoc();
1513
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001514 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001515 // %hi/%lo relocation
1516 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
1517 MipsII::MO_ABS_HI);
1518 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
1519 MipsII::MO_ABS_LO);
1520 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1521 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1522 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001523 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001524
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001525 EVT ValTy = Op.getValueType();
1526 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1527 unsigned OFSTFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1528 SDValue BAGOTOffset = DAG.getBlockAddress(BA, ValTy, true, GOTFlag);
1529 BAGOTOffset = DAG.getNode(MipsISD::WrapperPIC, dl, ValTy, BAGOTOffset);
1530 SDValue BALOOffset = DAG.getBlockAddress(BA, ValTy, true, OFSTFlag);
1531 SDValue Load = DAG.getLoad(ValTy, dl,
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001532 DAG.getEntryNode(), BAGOTOffset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001533 MachinePointerInfo(), false, false, false, 0);
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001534 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, BALOOffset);
1535 return DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001536}
1537
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001538SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001539LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001540{
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001541 // If the relocation model is PIC, use the General Dynamic TLS Model,
1542 // otherwise use the Initial Exec or Local Exec TLS Model.
1543 // TODO: implement Local Dynamic TLS model
1544
1545 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1546 DebugLoc dl = GA->getDebugLoc();
1547 const GlobalValue *GV = GA->getGlobal();
1548 EVT PtrVT = getPointerTy();
1549
1550 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1551 // General Dynamic TLS Model
1552 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001553 0, MipsII::MO_TLSGD);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001554 SDValue Tlsgd = DAG.getNode(MipsISD::TlsGd, dl, MVT::i32, TGA);
1555 SDValue GP = DAG.getRegister(Mips::GP, MVT::i32);
1556 SDValue Argument = DAG.getNode(ISD::ADD, dl, MVT::i32, GP, Tlsgd);
1557
1558 ArgListTy Args;
1559 ArgListEntry Entry;
1560 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001561 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001562 Args.push_back(Entry);
1563 std::pair<SDValue, SDValue> CallResult =
1564 LowerCallTo(DAG.getEntryNode(),
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001565 (Type *) Type::getInt32Ty(*DAG.getContext()),
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001566 false, false, false, false, 0, CallingConv::C, false, true,
1567 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG,
1568 dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001569
1570 return CallResult.first;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001571 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001572
1573 SDValue Offset;
1574 if (GV->isDeclaration()) {
1575 // Initial Exec TLS Model
1576 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1577 MipsII::MO_GOTTPREL);
1578 Offset = DAG.getLoad(MVT::i32, dl,
1579 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001580 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001581 } else {
1582 // Local Exec TLS Model
1583 SDVTList VTs = DAG.getVTList(MVT::i32);
1584 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1585 MipsII::MO_TPREL_HI);
1586 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1587 MipsII::MO_TPREL_LO);
1588 SDValue Hi = DAG.getNode(MipsISD::TprelHi, dl, VTs, &TGAHi, 1);
1589 SDValue Lo = DAG.getNode(MipsISD::TprelLo, dl, MVT::i32, TGALo);
1590 Offset = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1591 }
1592
1593 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1594 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001595}
1596
1597SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001598LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001599{
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001600 SDValue HiPart, JTI, JTILo;
Dale Johannesende064702009-02-06 21:50:26 +00001601 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001602 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001603 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Owen Andersone50ed302009-08-10 22:56:29 +00001604 EVT PtrVT = Op.getValueType();
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001605 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001606
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001607 if (!IsPIC && !IsN64) {
1608 JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MipsII::MO_ABS_HI);
1609 HiPart = DAG.getNode(MipsISD::Hi, dl, PtrVT, JTI);
1610 JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MipsII::MO_ABS_LO);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001611 } else {// Emit Load from Global Pointer
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001612 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1613 unsigned OfstFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1614 JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, GOTFlag);
1615 JTI = DAG.getNode(MipsISD::WrapperPIC, dl, PtrVT, JTI);
1616 HiPart = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), JTI,
1617 MachinePointerInfo(), false, false, false, 0);
1618 JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OfstFlag);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001619 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001620
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001621 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, JTILo);
1622 return DAG.getNode(ISD::ADD, dl, PtrVT, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001623}
1624
Dan Gohman475871a2008-07-27 21:46:04 +00001625SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001626LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001627{
Dan Gohman475871a2008-07-27 21:46:04 +00001628 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001629 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001630 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001631 // FIXME there isn't actually debug info here
1632 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001633
1634 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001635 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001636 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001637 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001638 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001639 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001640 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1641 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001642 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001643
1644 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001645 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001646 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001647 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001648 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001649 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1650 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001651 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001652 } else {
Akira Hatanaka620db892011-11-16 22:44:38 +00001653 EVT ValTy = Op.getValueType();
1654 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1655 unsigned OFSTFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1656 SDValue CP = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1657 N->getOffset(), GOTFlag);
1658 CP = DAG.getNode(MipsISD::WrapperPIC, dl, ValTy, CP);
1659 SDValue Load = DAG.getLoad(ValTy, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001660 CP, MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001661 false, false, false, 0);
Akira Hatanaka620db892011-11-16 22:44:38 +00001662 SDValue CPLo = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1663 N->getOffset(), OFSTFlag);
1664 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, CPLo);
1665 ResNode = DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001666 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001667
1668 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001669}
1670
Dan Gohmand858e902010-04-17 15:26:15 +00001671SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001672 MachineFunction &MF = DAG.getMachineFunction();
1673 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1674
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001675 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001676 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1677 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001678
1679 // vastart just stores the address of the VarArgsFrameIndex slot into the
1680 // memory location argument.
1681 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001682 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1683 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +00001684 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001685}
1686
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001687static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
1688 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
1689 DebugLoc dl = Op.getDebugLoc();
1690 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
1691 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
1692 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
1693 DAG.getConstant(0x7fffffff, MVT::i32));
1694 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
1695 DAG.getConstant(0x80000000, MVT::i32));
1696 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1697 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
1698}
1699
1700static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001701 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001702 // Use ext/ins instructions if target architecture is Mips32r2.
1703 // Eliminate redundant mfc1 and mtc1 instructions.
1704 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001705
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001706 if (!isLittle)
1707 std::swap(LoIdx, HiIdx);
1708
1709 DebugLoc dl = Op.getDebugLoc();
1710 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1711 Op.getOperand(0),
1712 DAG.getConstant(LoIdx, MVT::i32));
1713 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1714 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1715 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1716 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1717 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1718 DAG.getConstant(0x7fffffff, MVT::i32));
1719 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1720 DAG.getConstant(0x80000000, MVT::i32));
1721 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1722
1723 if (!isLittle)
1724 std::swap(Word0, Word1);
1725
1726 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1727}
1728
1729SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1730 const {
1731 EVT Ty = Op.getValueType();
1732
1733 assert(Ty == MVT::f32 || Ty == MVT::f64);
1734
1735 if (Ty == MVT::f32)
1736 return LowerFCOPYSIGN32(Op, DAG);
1737 else
1738 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
1739}
1740
Akira Hatanaka2e591472011-06-02 00:24:44 +00001741SDValue MipsTargetLowering::
1742LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001743 // check the depth
1744 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001745 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001746
1747 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1748 MFI->setFrameAddressIsTaken(true);
1749 EVT VT = Op.getValueType();
1750 DebugLoc dl = Op.getDebugLoc();
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001751 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
1752 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001753 return FrameAddr;
1754}
1755
Akira Hatanakadb548262011-07-19 23:30:50 +00001756// TODO: set SType according to the desired memory barrier behavior.
1757SDValue MipsTargetLowering::LowerMEMBARRIER(SDValue Op,
1758 SelectionDAG& DAG) const {
1759 unsigned SType = 0;
1760 DebugLoc dl = Op.getDebugLoc();
1761 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1762 DAG.getConstant(SType, MVT::i32));
1763}
1764
Eli Friedman14648462011-07-27 22:21:52 +00001765SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1766 SelectionDAG& DAG) const {
1767 // FIXME: Need pseudo-fence for 'singlethread' fences
1768 // FIXME: Set SType for weaker fences where supported/appropriate.
1769 unsigned SType = 0;
1770 DebugLoc dl = Op.getDebugLoc();
1771 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1772 DAG.getConstant(SType, MVT::i32));
1773}
1774
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001775//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001776// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001777//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001778
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001779//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001780// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001781// Mips O32 ABI rules:
1782// ---
1783// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001784// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001785// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001786// f64 - Only passed in two aliased f32 registers if no int reg has been used
1787// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001788// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1789// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001790//
1791// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001792//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001793
Duncan Sands1e96bab2010-11-04 10:49:57 +00001794static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001795 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001796 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1797
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001798 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001799
1800 static const unsigned IntRegs[] = {
1801 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1802 };
1803 static const unsigned F32Regs[] = {
1804 Mips::F12, Mips::F14
1805 };
1806 static const unsigned F64Regs[] = {
1807 Mips::D6, Mips::D7
1808 };
1809
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001810 // ByVal Args
1811 if (ArgFlags.isByVal()) {
1812 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1813 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1814 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1815 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1816 r < std::min(IntRegsSize, NextReg); ++r)
1817 State.AllocateReg(IntRegs[r]);
1818 return false;
1819 }
1820
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001821 // Promote i8 and i16
1822 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1823 LocVT = MVT::i32;
1824 if (ArgFlags.isSExt())
1825 LocInfo = CCValAssign::SExt;
1826 else if (ArgFlags.isZExt())
1827 LocInfo = CCValAssign::ZExt;
1828 else
1829 LocInfo = CCValAssign::AExt;
1830 }
1831
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001832 unsigned Reg;
1833
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001834 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1835 // is true: function is vararg, argument is 3rd or higher, there is previous
1836 // argument which is not f32 or f64.
1837 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1838 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001839 unsigned OrigAlign = ArgFlags.getOrigAlign();
1840 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001841
1842 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001843 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001844 // If this is the first part of an i64 arg,
1845 // the allocated register must be either A0 or A2.
1846 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1847 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001848 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001849 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1850 // Allocate int register and shadow next int register. If first
1851 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001852 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1853 if (Reg == Mips::A1 || Reg == Mips::A3)
1854 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1855 State.AllocateReg(IntRegs, IntRegsSize);
1856 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001857 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1858 // we are guaranteed to find an available float register
1859 if (ValVT == MVT::f32) {
1860 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1861 // Shadow int register
1862 State.AllocateReg(IntRegs, IntRegsSize);
1863 } else {
1864 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1865 // Shadow int registers
1866 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1867 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1868 State.AllocateReg(IntRegs, IntRegsSize);
1869 State.AllocateReg(IntRegs, IntRegsSize);
1870 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001871 } else
1872 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001873
Akira Hatanakad37776d2011-05-20 21:39:54 +00001874 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1875 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1876
1877 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001878 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001879 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001880 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001881
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001882 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001883}
1884
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00001885static const unsigned Mips64IntRegs[8] =
1886 {Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
1887 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64};
1888static const unsigned Mips64DPRegs[8] =
1889 {Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
1890 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64};
1891
1892static bool CC_Mips64Byval(unsigned ValNo, MVT ValVT, MVT LocVT,
1893 CCValAssign::LocInfo LocInfo,
1894 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1895 unsigned Align = std::max(ArgFlags.getByValAlign(), (unsigned)8);
1896 unsigned Size = (ArgFlags.getByValSize() + 7) / 8 * 8;
1897 unsigned FirstIdx = State.getFirstUnallocated(Mips64IntRegs, 8);
1898
1899 assert(Align <= 16 && "Cannot handle alignments larger than 16.");
1900
1901 // If byval is 16-byte aligned, the first arg register must be even.
1902 if ((Align == 16) && (FirstIdx % 2)) {
1903 State.AllocateReg(Mips64IntRegs[FirstIdx], Mips64DPRegs[FirstIdx]);
1904 ++FirstIdx;
1905 }
1906
1907 // Mark the registers allocated.
1908 for (unsigned I = FirstIdx; Size && (I < 8); Size -= 8, ++I)
1909 State.AllocateReg(Mips64IntRegs[I], Mips64DPRegs[I]);
1910
1911 // Allocate space on caller's stack.
1912 unsigned Offset = State.AllocateStack(Size, Align);
1913
1914 if (FirstIdx < 8)
1915 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Mips64IntRegs[FirstIdx],
1916 LocVT, LocInfo));
1917 else
1918 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
1919
1920 return true;
1921}
1922
1923#include "MipsGenCallingConv.inc"
1924
Akira Hatanaka49617092011-11-14 19:02:54 +00001925static void
1926AnalyzeMips64CallOperands(CCState CCInfo,
1927 const SmallVectorImpl<ISD::OutputArg> &Outs) {
1928 unsigned NumOps = Outs.size();
1929 for (unsigned i = 0; i != NumOps; ++i) {
1930 MVT ArgVT = Outs[i].VT;
1931 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
1932 bool R;
1933
1934 if (Outs[i].IsFixed)
1935 R = CC_MipsN(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
1936 else
1937 R = CC_MipsN_VarArg(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
1938
Akira Hatanaka49617092011-11-14 19:02:54 +00001939 if (R) {
Benjamin Kramer6296ee32011-11-14 19:51:48 +00001940#ifndef NDEBUG
Akira Hatanaka49617092011-11-14 19:02:54 +00001941 dbgs() << "Call operand #" << i << " has unhandled type "
1942 << EVT(ArgVT).getEVTString();
1943#endif
1944 llvm_unreachable(0);
1945 }
1946 }
1947}
1948
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001949//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001950// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001951//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001952
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001953static const unsigned O32IntRegsSize = 4;
1954
1955static const unsigned O32IntRegs[] = {
1956 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1957};
1958
Akira Hatanaka373e3a42011-09-23 00:58:33 +00001959// Return next O32 integer argument register.
1960static unsigned getNextIntArgReg(unsigned Reg) {
1961 assert((Reg == Mips::A0) || (Reg == Mips::A2));
1962 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
1963}
1964
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001965// Write ByVal Arg to arg registers and stack.
1966static void
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001967WriteByValArg(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001968 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1969 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1970 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001971 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001972 MVT PtrType, bool isLittle) {
1973 unsigned LocMemOffset = VA.getLocMemOffset();
1974 unsigned Offset = 0;
1975 uint32_t RemainingSize = Flags.getByValSize();
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001976 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001977
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001978 // Copy the first 4 words of byval arg to registers A0 - A3.
1979 // FIXME: Use a stricter alignment if it enables better optimization in passes
1980 // run later.
1981 for (; RemainingSize >= 4 && LocMemOffset < 4 * 4;
1982 Offset += 4, RemainingSize -= 4, LocMemOffset += 4) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001983 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001984 DAG.getConstant(Offset, MVT::i32));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001985 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1986 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001987 false, false, false, std::min(ByValAlign,
1988 (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001989 MemOpChains.push_back(LoadVal.getValue(1));
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001990 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001991 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1992 }
1993
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001994 if (RemainingSize == 0)
1995 return;
1996
1997 // If there still is a register available for argument passing, write the
1998 // remaining part of the structure to it using subword loads and shifts.
1999 if (LocMemOffset < 4 * 4) {
2000 assert(RemainingSize <= 3 && RemainingSize >= 1 &&
2001 "There must be one to three bytes remaining.");
2002 unsigned LoadSize = (RemainingSize == 3 ? 2 : RemainingSize);
2003 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2004 DAG.getConstant(Offset, MVT::i32));
2005 unsigned Alignment = std::min(ByValAlign, (unsigned )4);
2006 SDValue LoadVal = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2007 LoadPtr, MachinePointerInfo(),
2008 MVT::getIntegerVT(LoadSize * 8), false,
2009 false, Alignment);
2010 MemOpChains.push_back(LoadVal.getValue(1));
2011
2012 // If target is big endian, shift it to the most significant half-word or
2013 // byte.
2014 if (!isLittle)
2015 LoadVal = DAG.getNode(ISD::SHL, dl, MVT::i32, LoadVal,
2016 DAG.getConstant(32 - LoadSize * 8, MVT::i32));
2017
2018 Offset += LoadSize;
2019 RemainingSize -= LoadSize;
2020
2021 // Read second subword if necessary.
2022 if (RemainingSize != 0) {
2023 assert(RemainingSize == 1 && "There must be one byte remaining.");
2024 LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2025 DAG.getConstant(Offset, MVT::i32));
2026 unsigned Alignment = std::min(ByValAlign, (unsigned )2);
2027 SDValue Subword = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2028 LoadPtr, MachinePointerInfo(),
2029 MVT::i8, false, false, Alignment);
2030 MemOpChains.push_back(Subword.getValue(1));
2031 // Insert the loaded byte to LoadVal.
2032 // FIXME: Use INS if supported by target.
2033 unsigned ShiftAmt = isLittle ? 16 : 8;
2034 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i32, Subword,
2035 DAG.getConstant(ShiftAmt, MVT::i32));
2036 LoadVal = DAG.getNode(ISD::OR, dl, MVT::i32, LoadVal, Shift);
2037 }
2038
2039 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
2040 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
2041 return;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002042 }
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002043
2044 // Create a fixed object on stack at offset LocMemOffset and copy
2045 // remaining part of byval arg to it using memcpy.
2046 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2047 DAG.getConstant(Offset, MVT::i32));
2048 LastFI = MFI->CreateFixedObject(RemainingSize, LocMemOffset, true);
2049 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002050 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2051 DAG.getConstant(RemainingSize, MVT::i32),
2052 std::min(ByValAlign, (unsigned)4),
2053 /*isVolatile=*/false, /*AlwaysInline=*/false,
2054 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002055}
2056
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002057// Copy Mips64 byVal arg to registers and stack.
2058void static
2059PassByValArg64(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
2060 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
2061 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
2062 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
2063 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2064 EVT PtrTy, bool isLittle) {
2065 unsigned ByValSize = Flags.getByValSize();
2066 unsigned Alignment = std::min(Flags.getByValAlign(), (unsigned)8);
2067 bool IsRegLoc = VA.isRegLoc();
2068 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
2069 unsigned LocMemOffset = 0;
Akira Hatanaka16040852011-11-15 18:42:25 +00002070 unsigned MemCpySize = ByValSize;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002071
2072 if (!IsRegLoc)
2073 LocMemOffset = VA.getLocMemOffset();
2074 else {
2075 const unsigned *Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8,
2076 VA.getLocReg());
2077 const unsigned *RegEnd = Mips64IntRegs + 8;
2078
2079 // Copy double words to registers.
2080 for (; (Reg != RegEnd) && (ByValSize >= Offset + 8); ++Reg, Offset += 8) {
2081 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2082 DAG.getConstant(Offset, PtrTy));
2083 SDValue LoadVal = DAG.getLoad(MVT::i64, dl, Chain, LoadPtr,
2084 MachinePointerInfo(), false, false, false,
2085 Alignment);
2086 MemOpChains.push_back(LoadVal.getValue(1));
2087 RegsToPass.push_back(std::make_pair(*Reg, LoadVal));
2088 }
2089
Akira Hatanaka16040852011-11-15 18:42:25 +00002090 // Return if the struct has been fully copied.
2091 if (!(MemCpySize = ByValSize - Offset))
2092 return;
2093
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002094 // If there is an argument register available, copy the remainder of the
2095 // byval argument with sub-doubleword loads and shifts.
Akira Hatanaka16040852011-11-15 18:42:25 +00002096 if (Reg != RegEnd) {
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002097 assert((ByValSize < Offset + 8) &&
2098 "Size of the remainder should be smaller than 8-byte.");
2099 SDValue Val;
2100 for (unsigned LoadSize = 4; Offset < ByValSize; LoadSize /= 2) {
2101 unsigned RemSize = ByValSize - Offset;
2102
2103 if (RemSize < LoadSize)
2104 continue;
2105
2106 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2107 DAG.getConstant(Offset, PtrTy));
2108 SDValue LoadVal =
2109 DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i64, Chain, LoadPtr,
2110 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
2111 false, false, Alignment);
2112 MemOpChains.push_back(LoadVal.getValue(1));
2113
2114 // Offset in number of bits from double word boundary.
2115 unsigned OffsetDW = (Offset % 8) * 8;
2116 unsigned Shamt = isLittle ? OffsetDW : 64 - (OffsetDW + LoadSize * 8);
2117 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i64, LoadVal,
2118 DAG.getConstant(Shamt, MVT::i32));
2119
2120 Val = Val.getNode() ? DAG.getNode(ISD::OR, dl, MVT::i64, Val, Shift) :
2121 Shift;
2122 Offset += LoadSize;
2123 Alignment = std::min(Alignment, LoadSize);
2124 }
2125
2126 RegsToPass.push_back(std::make_pair(*Reg, Val));
2127 return;
2128 }
2129 }
2130
Akira Hatanaka16040852011-11-15 18:42:25 +00002131 assert(MemCpySize && "MemCpySize must not be zero.");
2132
2133 // Create a fixed object on stack at offset LocMemOffset and copy
2134 // remainder of byval arg to it with memcpy.
2135 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2136 DAG.getConstant(Offset, PtrTy));
2137 LastFI = MFI->CreateFixedObject(MemCpySize, LocMemOffset, true);
2138 SDValue Dst = DAG.getFrameIndex(LastFI, PtrTy);
2139 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2140 DAG.getConstant(MemCpySize, PtrTy), Alignment,
2141 /*isVolatile=*/false, /*AlwaysInline=*/false,
2142 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002143}
2144
Dan Gohman98ca4f22009-08-05 01:29:28 +00002145/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002146/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002147/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002148SDValue
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002149MipsTargetLowering::LowerCall(SDValue InChain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002150 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002151 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002152 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002153 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002154 const SmallVectorImpl<ISD::InputArg> &Ins,
2155 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002156 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002157 // MIPs target does not yet support tail call optimization.
2158 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002159
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002160 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002161 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002162 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002163 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00002164 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002165
2166 // Analyze operands of the call, assigning locations to each operand.
2167 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002168 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2169 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002170
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002171 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002172 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanaka49617092011-11-14 19:02:54 +00002173 else if (HasMips64)
2174 AnalyzeMips64CallOperands(CCInfo, Outs);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00002175 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002176 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002177
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002178 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002179 unsigned NextStackOffset = CCInfo.getNextStackOffset();
2180
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002181 // Chain is the output chain of the last Load/Store or CopyToReg node.
2182 // ByValChain is the output chain of the last Memcpy node created for copying
2183 // byval arguments to the stack.
2184 SDValue Chain, CallSeqStart, ByValChain;
2185 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
2186 Chain = CallSeqStart = DAG.getCALLSEQ_START(InChain, NextStackOffsetVal);
2187 ByValChain = InChain;
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002188
2189 // If this is the first call, create a stack frame object that points to
2190 // a location to which .cprestore saves $gp.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002191 if (IsO32 && IsPIC && !MipsFI->getGPFI())
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002192 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
2193
Akira Hatanaka21afc632011-06-21 00:40:49 +00002194 // Get the frame index of the stack frame object that points to the location
2195 // of dynamically allocated area on the stack.
2196 int DynAllocFI = MipsFI->getDynAllocFI();
2197
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002198 // Update size of the maximum argument space.
2199 // For O32, a minimum of four words (16 bytes) of argument space is
2200 // allocated.
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002201 if (IsO32)
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002202 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
2203
2204 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
2205
2206 if (MaxCallFrameSize < NextStackOffset) {
2207 MipsFI->setMaxCallFrameSize(NextStackOffset);
2208
Akira Hatanaka21afc632011-06-21 00:40:49 +00002209 // Set the offsets relative to $sp of the $gp restore slot and dynamically
2210 // allocated stack space. These offsets must be aligned to a boundary
2211 // determined by the stack alignment of the ABI.
2212 unsigned StackAlignment = TFL->getStackAlignment();
2213 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
2214 StackAlignment * StackAlignment;
2215
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002216 if (MipsFI->needGPSaveRestore())
Akira Hatanaka21afc632011-06-21 00:40:49 +00002217 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
2218
2219 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002220 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002221
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002222 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002223 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
2224 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002225
Eric Christopher471e4222011-06-08 23:55:35 +00002226 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00002227
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002228 // Walk the register/memloc assignments, inserting copies/loads.
2229 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002230 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002231 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002232 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002233 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2234
2235 // ByVal Arg.
2236 if (Flags.isByVal()) {
2237 assert(Flags.getByValSize() &&
2238 "ByVal args of size 0 should have been ignored by front-end.");
2239 if (IsO32)
2240 WriteByValArg(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2241 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2242 Subtarget->isLittle());
2243 else
2244 PassByValArg64(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2245 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2246 Subtarget->isLittle());
2247 continue;
2248 }
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002249
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002250 // Promote the value if needed.
2251 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002252 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002253 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002254 if (VA.isRegLoc()) {
2255 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
2256 (ValVT == MVT::f64 && LocVT == MVT::i64))
2257 Arg = DAG.getNode(ISD::BITCAST, dl, LocVT, Arg);
2258 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002259 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2260 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002261 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2262 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002263 if (!Subtarget->isLittle())
2264 std::swap(Lo, Hi);
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002265 unsigned LocRegLo = VA.getLocReg();
2266 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2267 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2268 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002269 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002270 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002271 }
2272 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002273 case CCValAssign::SExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002274 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002275 break;
2276 case CCValAssign::ZExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002277 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002278 break;
2279 case CCValAssign::AExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002280 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002281 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002282 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002283
2284 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002285 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002286 if (VA.isRegLoc()) {
2287 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002288 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002289 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002290
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002291 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002292 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002293
Chris Lattnere0b12152008-03-17 06:57:02 +00002294 // Create the frame index object for this incoming parameter
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002295 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002296 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00002297 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00002298
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002299 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002300 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00002301 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
2302 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00002303 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002304 }
2305
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002306 // Extend range of indices of frame objects for outgoing arguments that were
2307 // created during this function call. Skip this step if no such objects were
2308 // created.
2309 if (LastFI)
2310 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2311
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002312 // If a memcpy has been created to copy a byval arg to a stack, replace the
2313 // chain input of CallSeqStart with ByValChain.
2314 if (InChain != ByValChain)
2315 DAG.UpdateNodeOperands(CallSeqStart.getNode(), ByValChain,
2316 NextStackOffsetVal);
2317
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002318 // Transform all store nodes into one single node because all store
2319 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002320 if (!MemOpChains.empty())
2321 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002322 &MemOpChains[0], MemOpChains.size());
2323
Bill Wendling056292f2008-09-16 21:48:12 +00002324 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002325 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2326 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002327 unsigned char OpFlag;
2328 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002329 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002330 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002331
2332 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002333 if (IsPICCall && G->getGlobal()->hasInternalLinkage()) {
2334 OpFlag = IsO32 ? MipsII::MO_GOT : MipsII::MO_GOT_PAGE;
2335 unsigned char LoFlag = IsO32 ? MipsII::MO_ABS_LO : MipsII::MO_GOT_OFST;
2336 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(), 0,
2337 OpFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002338 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002339 0, LoFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002340 } else {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002341 OpFlag = IsPICCall ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002342 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2343 getPointerTy(), 0, OpFlag);
2344 }
2345
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002346 LoadSymAddr = true;
2347 }
2348 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002349 if (IsN64 || (!IsO32 && IsPIC))
2350 OpFlag = MipsII::MO_GOT_DISP;
2351 else if (!IsPIC) // !N64 && static
2352 OpFlag = MipsII::MO_NO_FLAG;
2353 else // O32 & PIC
2354 OpFlag = MipsII::MO_GOT_CALL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002355 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002356 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002357 LoadSymAddr = true;
2358 }
2359
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002360 SDValue InFlag;
2361
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002362 // Create nodes that load address of callee and copy it to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002363 if (IsPICCall) {
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002364 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002365 // Load callee address
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002366 Callee = DAG.getNode(MipsISD::WrapperPIC, dl, getPointerTy(), Callee);
2367 SDValue LoadValue = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
2368 Callee, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002369 false, false, false, 0);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002370
2371 // Use GOT+LO if callee has internal linkage.
2372 if (CalleeLo.getNode()) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002373 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, getPointerTy(), CalleeLo);
2374 Callee = DAG.getNode(ISD::ADD, dl, getPointerTy(), LoadValue, Lo);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002375 } else
2376 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002377 }
2378
2379 // copy to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002380 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
2381 Chain = DAG.getCopyToReg(Chain, dl, T9Reg, Callee, SDValue(0, 0));
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002382 InFlag = Chain.getValue(1);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002383 Callee = DAG.getRegister(T9Reg, getPointerTy());
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002384 }
Bill Wendling056292f2008-09-16 21:48:12 +00002385
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002386 // Build a sequence of copy-to-reg nodes chained together with token
2387 // chain and flag operands which copy the outgoing args into registers.
2388 // The InFlag in necessary since all emitted instructions must be
2389 // stuck together.
2390 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2391 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2392 RegsToPass[i].second, InFlag);
2393 InFlag = Chain.getValue(1);
2394 }
2395
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002396 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002397 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002398 //
2399 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002400 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002401 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002402 Ops.push_back(Chain);
2403 Ops.push_back(Callee);
2404
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002405 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002406 // known live into the call.
2407 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2408 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2409 RegsToPass[i].second.getValueType()));
2410
Gabor Greifba36cb52008-08-28 21:40:38 +00002411 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002412 Ops.push_back(InFlag);
2413
Dale Johannesen33c960f2009-02-04 20:06:27 +00002414 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002415 InFlag = Chain.getValue(1);
2416
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002417 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002418 Chain = DAG.getCALLSEQ_END(Chain,
2419 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002420 DAG.getIntPtrConstant(0, true), InFlag);
2421 InFlag = Chain.getValue(1);
2422
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002423 // Handle result values, copying them out of physregs into vregs that we
2424 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002425 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2426 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002427}
2428
Dan Gohman98ca4f22009-08-05 01:29:28 +00002429/// LowerCallResult - Lower the result values of a call into the
2430/// appropriate copies out of appropriate physical registers.
2431SDValue
2432MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002433 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002434 const SmallVectorImpl<ISD::InputArg> &Ins,
2435 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002436 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002437 // Assign locations to each value returned by this call.
2438 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002439 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2440 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002441
Dan Gohman98ca4f22009-08-05 01:29:28 +00002442 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002443
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002444 // Copy all of the result registers out of their specified physreg.
2445 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002446 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002447 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002448 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002449 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002450 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002451
Dan Gohman98ca4f22009-08-05 01:29:28 +00002452 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002453}
2454
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002455//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002456// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002457//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002458static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2459 std::vector<SDValue>& OutChains,
2460 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2461 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2462 unsigned LocMem = VA.getLocMemOffset();
2463 unsigned FirstWord = LocMem / 4;
2464
2465 // copy register A0 - A3 to frame object
2466 for (unsigned i = 0; i < NumWords; ++i) {
2467 unsigned CurWord = FirstWord + i;
2468 if (CurWord >= O32IntRegsSize)
2469 break;
2470
2471 unsigned SrcReg = O32IntRegs[CurWord];
2472 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2473 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2474 DAG.getConstant(i * 4, MVT::i32));
2475 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2476 StorePtr, MachinePointerInfo(), false,
2477 false, 0);
2478 OutChains.push_back(Store);
2479 }
2480}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002481
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002482// Create frame object on stack and copy registers used for byval passing to it.
2483static unsigned
2484CopyMips64ByValRegs(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2485 std::vector<SDValue>& OutChains, SelectionDAG &DAG,
2486 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2487 MachineFrameInfo *MFI, bool IsRegLoc,
2488 SmallVectorImpl<SDValue> &InVals, MipsFunctionInfo *MipsFI,
2489 EVT PtrTy) {
2490 const unsigned *Reg = Mips64IntRegs + 8;
2491 int FOOffset; // Frame object offset from virtual frame pointer.
2492
2493 if (IsRegLoc) {
2494 Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8, VA.getLocReg());
2495 FOOffset = (Reg - Mips64IntRegs) * 8 - 8 * 8;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002496 }
2497 else
2498 FOOffset = VA.getLocMemOffset();
2499
2500 // Create frame object.
2501 unsigned NumRegs = (Flags.getByValSize() + 7) / 8;
2502 unsigned LastFI = MFI->CreateFixedObject(NumRegs * 8, FOOffset, true);
2503 SDValue FIN = DAG.getFrameIndex(LastFI, PtrTy);
2504 InVals.push_back(FIN);
2505
2506 // Copy arg registers.
2507 for (unsigned I = 0; (Reg != Mips64IntRegs + 8) && (I < NumRegs);
2508 ++Reg, ++I) {
2509 unsigned VReg = AddLiveIn(MF, *Reg, Mips::CPU64RegsRegisterClass);
2510 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, PtrTy, FIN,
2511 DAG.getConstant(I * 8, PtrTy));
2512 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(VReg, MVT::i64),
2513 StorePtr, MachinePointerInfo(), false,
2514 false, 0);
2515 OutChains.push_back(Store);
2516 }
2517
2518 return LastFI;
2519}
2520
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002521/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002522/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002523SDValue
2524MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002525 CallingConv::ID CallConv,
2526 bool isVarArg,
2527 const SmallVectorImpl<ISD::InputArg>
2528 &Ins,
2529 DebugLoc dl, SelectionDAG &DAG,
2530 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002531 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002532 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002533 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002534 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002535
Dan Gohman1e93df62010-04-17 14:41:14 +00002536 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002537
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002538 // Used with vargs to acumulate store chains.
2539 std::vector<SDValue> OutChains;
2540
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002541 // Assign locations to all of the incoming arguments.
2542 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002543 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2544 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002545
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002546 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002547 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002548 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002549 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002550
Akira Hatanaka43299772011-05-20 23:22:14 +00002551 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002552
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002553 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002554 CCValAssign &VA = ArgLocs[i];
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002555 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002556 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2557 bool IsRegLoc = VA.isRegLoc();
2558
2559 if (Flags.isByVal()) {
2560 assert(Flags.getByValSize() &&
2561 "ByVal args of size 0 should have been ignored by front-end.");
2562 if (IsO32) {
2563 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2564 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2565 true);
2566 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2567 InVals.push_back(FIN);
2568 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2569 } else // N32/64
2570 LastFI = CopyMips64ByValRegs(MF, Chain, dl, OutChains, DAG, VA, Flags,
2571 MFI, IsRegLoc, InVals, MipsFI,
2572 getPointerTy());
2573 continue;
2574 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002575
2576 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002577 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002578 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002579 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002580 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002581
Owen Anderson825b72b2009-08-11 20:47:22 +00002582 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002583 RC = Mips::CPURegsRegisterClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002584 else if (RegVT == MVT::i64)
2585 RC = Mips::CPU64RegsRegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002586 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002587 RC = Mips::FGR32RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002588 else if (RegVT == MVT::f64)
Akira Hatanakaf40de9d2011-09-26 21:55:17 +00002589 RC = HasMips64 ? Mips::FGR64RegisterClass : Mips::AFGR64RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002590 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002591 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002592
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002593 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002594 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002595 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002596 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002597
2598 // If this is an 8 or 16-bit value, it has been passed promoted
2599 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002600 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002601 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002602 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002603 if (VA.getLocInfo() == CCValAssign::SExt)
2604 Opcode = ISD::AssertSext;
2605 else if (VA.getLocInfo() == CCValAssign::ZExt)
2606 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002607 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002608 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002609 DAG.getValueType(ValVT));
2610 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002611 }
2612
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002613 // Handle floating point arguments passed in integer registers.
2614 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
2615 (RegVT == MVT::i64 && ValVT == MVT::f64))
2616 ArgValue = DAG.getNode(ISD::BITCAST, dl, ValVT, ArgValue);
2617 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
2618 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
2619 getNextIntArgReg(ArgReg), RC);
2620 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
2621 if (!Subtarget->isLittle())
2622 std::swap(ArgValue, ArgValue2);
2623 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2624 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002625 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002626
Dan Gohman98ca4f22009-08-05 01:29:28 +00002627 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002628 } else { // VA.isRegLoc()
2629
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002630 // sanity check
2631 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002632
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002633 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002634 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002635 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002636
2637 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002638 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002639 InVals.push_back(DAG.getLoad(ValVT, dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002640 MachinePointerInfo::getFixedStack(LastFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002641 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002642 }
2643 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002644
2645 // The mips ABIs for returning structs by value requires that we copy
2646 // the sret argument into $v0 for the return. Save the argument into
2647 // a virtual register so that we can access it from the return points.
2648 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2649 unsigned Reg = MipsFI->getSRetReturnReg();
2650 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002651 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002652 MipsFI->setSRetReturnReg(Reg);
2653 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002654 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002655 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002656 }
2657
Akira Hatanakabad53f42011-11-14 19:01:09 +00002658 if (isVarArg) {
2659 unsigned NumOfRegs = IsO32 ? 4 : 8;
2660 const unsigned *ArgRegs = IsO32 ? O32IntRegs : Mips64IntRegs;
2661 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumOfRegs);
2662 int FirstRegSlotOffset = IsO32 ? 0 : -64 ; // offset of $a0's slot.
2663 TargetRegisterClass *RC
2664 = IsO32 ? Mips::CPURegsRegisterClass : Mips::CPU64RegsRegisterClass;
2665 unsigned RegSize = RC->getSize();
2666 int RegSlotOffset = FirstRegSlotOffset + Idx * RegSize;
2667
2668 // Offset of the first variable argument from stack pointer.
2669 int FirstVaArgOffset;
2670
2671 if (IsO32 || (Idx == NumOfRegs)) {
2672 FirstVaArgOffset =
2673 (CCInfo.getNextStackOffset() + RegSize - 1) / RegSize * RegSize;
2674 } else
2675 FirstVaArgOffset = RegSlotOffset;
2676
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002677 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002678 // which is a value necessary to VASTART.
Akira Hatanakabad53f42011-11-14 19:01:09 +00002679 LastFI = MFI->CreateFixedObject(RegSize, FirstVaArgOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002680 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002681
Akira Hatanakabad53f42011-11-14 19:01:09 +00002682 // Copy the integer registers that have not been used for argument passing
2683 // to the argument register save area. For O32, the save area is allocated
2684 // in the caller's stack frame, while for N32/64, it is allocated in the
2685 // callee's stack frame.
2686 for (int StackOffset = RegSlotOffset;
2687 Idx < NumOfRegs; ++Idx, StackOffset += RegSize) {
2688 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegs[Idx], RC);
2689 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2690 MVT::getIntegerVT(RegSize * 8));
2691 LastFI = MFI->CreateFixedObject(RegSize, StackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002692 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2693 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2694 MachinePointerInfo(),
2695 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002696 }
2697 }
2698
Akira Hatanaka43299772011-05-20 23:22:14 +00002699 MipsFI->setLastInArgFI(LastFI);
2700
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002701 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002702 // the size of Ins and InVals. This only happens when on varg functions
2703 if (!OutChains.empty()) {
2704 OutChains.push_back(Chain);
2705 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2706 &OutChains[0], OutChains.size());
2707 }
2708
Dan Gohman98ca4f22009-08-05 01:29:28 +00002709 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002710}
2711
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002712//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002713// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002714//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002715
Dan Gohman98ca4f22009-08-05 01:29:28 +00002716SDValue
2717MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002718 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002719 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002720 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002721 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002722
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002723 // CCValAssign - represent the assignment of
2724 // the return value to a location
2725 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002726
2727 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002728 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2729 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002730
Dan Gohman98ca4f22009-08-05 01:29:28 +00002731 // Analize return values.
2732 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002733
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002734 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002735 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002736 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002737 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002738 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002739 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002740 }
2741
Dan Gohman475871a2008-07-27 21:46:04 +00002742 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002743
2744 // Copy the result values into the output registers.
2745 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2746 CCValAssign &VA = RVLocs[i];
2747 assert(VA.isRegLoc() && "Can only return in registers!");
2748
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002749 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002750 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002751
2752 // guarantee that all emitted copies are
2753 // stuck together, avoiding something bad
2754 Flag = Chain.getValue(1);
2755 }
2756
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002757 // The mips ABIs for returning structs by value requires that we copy
2758 // the sret argument into $v0 for the return. We saved the argument into
2759 // a virtual register in the entry block, so now we copy the value out
2760 // and into $v0.
2761 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2762 MachineFunction &MF = DAG.getMachineFunction();
2763 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2764 unsigned Reg = MipsFI->getSRetReturnReg();
2765
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002766 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002767 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002768 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002769
Dale Johannesena05dca42009-02-04 23:02:30 +00002770 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002771 Flag = Chain.getValue(1);
2772 }
2773
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002774 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002775 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002776 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002777 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002778 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002779 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002780 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002781}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002782
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002783//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002784// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002785//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002786
2787/// getConstraintType - Given a constraint letter, return the type of
2788/// constraint it is for this target.
2789MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002790getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002791{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002792 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002793 // GCC config/mips/constraints.md
2794 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002795 // 'd' : An address register. Equivalent to r
2796 // unless generating MIPS16 code.
2797 // 'y' : Equivalent to r; retained for
2798 // backwards compatibility.
2799 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002800 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002801 switch (Constraint[0]) {
2802 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002803 case 'd':
2804 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002805 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002806 return C_RegisterClass;
2807 break;
2808 }
2809 }
2810 return TargetLowering::getConstraintType(Constraint);
2811}
2812
John Thompson44ab89e2010-10-29 17:29:13 +00002813/// Examine constraint type and operand type and determine a weight value.
2814/// This object must already have been set up with the operand type
2815/// and the current alternative constraint selected.
2816TargetLowering::ConstraintWeight
2817MipsTargetLowering::getSingleConstraintMatchWeight(
2818 AsmOperandInfo &info, const char *constraint) const {
2819 ConstraintWeight weight = CW_Invalid;
2820 Value *CallOperandVal = info.CallOperandVal;
2821 // If we don't have a value, we can't do a match,
2822 // but allow it at the lowest weight.
2823 if (CallOperandVal == NULL)
2824 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002825 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002826 // Look at the constraint type.
2827 switch (*constraint) {
2828 default:
2829 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2830 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002831 case 'd':
2832 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002833 if (type->isIntegerTy())
2834 weight = CW_Register;
2835 break;
2836 case 'f':
2837 if (type->isFloatTy())
2838 weight = CW_Register;
2839 break;
2840 }
2841 return weight;
2842}
2843
Eric Christopher38d64262011-06-29 19:33:04 +00002844/// Given a register class constraint, like 'r', if this corresponds directly
2845/// to an LLVM register class, return a register of 0 and the register class
2846/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002847std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002848getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002849{
2850 if (Constraint.size() == 1) {
2851 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002852 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2853 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002854 case 'r':
2855 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002856 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002857 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002858 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002859 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002860 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2861 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002862 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002863 }
2864 }
2865 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2866}
2867
Dan Gohman6520e202008-10-18 02:06:02 +00002868bool
2869MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2870 // The Mips target isn't yet aware of offsets.
2871 return false;
2872}
Evan Chengeb2f9692009-10-27 19:56:55 +00002873
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002874bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2875 if (VT != MVT::f32 && VT != MVT::f64)
2876 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002877 if (Imm.isNegZero())
2878 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002879 return Imm.isZero();
2880}