blob: 7e2273b5efe6b434c867470858e87b7917f9a1fd [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::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000058 case MipsISD::Ret: return "MipsISD::Ret";
59 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
60 case MipsISD::FPCmp: return "MipsISD::FPCmp";
61 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
62 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
63 case MipsISD::FPRound: return "MipsISD::FPRound";
64 case MipsISD::MAdd: return "MipsISD::MAdd";
65 case MipsISD::MAddu: return "MipsISD::MAddu";
66 case MipsISD::MSub: return "MipsISD::MSub";
67 case MipsISD::MSubu: return "MipsISD::MSubu";
68 case MipsISD::DivRem: return "MipsISD::DivRem";
69 case MipsISD::DivRemU: return "MipsISD::DivRemU";
70 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
71 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakabfcb83f2011-12-12 22:38:19 +000072 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Akira Hatanaka21afc632011-06-21 00:40:49 +000073 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
Akira Hatanakadb548262011-07-19 23:30:50 +000074 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanakabb15e112011-08-17 02:05:42 +000075 case MipsISD::Ext: return "MipsISD::Ext";
76 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanaka0f843822011-06-07 18:58:42 +000077 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000078 }
79}
80
81MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000082MipsTargetLowering(MipsTargetMachine &TM)
Akira Hatanaka8b4198d2011-09-26 21:47:02 +000083 : TargetLowering(TM, new MipsTargetObjectFile()),
84 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +000085 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
86 IsO32(Subtarget->isABI_O32()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000087
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000088 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000089 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000090 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +000091 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000092
93 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000094 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
95 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000096
Akira Hatanaka95934842011-09-24 01:34:44 +000097 if (HasMips64)
98 addRegisterClass(MVT::i64, Mips::CPU64RegsRegisterClass);
99
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000100 // When dealing with single precision only, use libcalls
Akira Hatanaka792016b2011-09-23 18:28:39 +0000101 if (!Subtarget->isSingleFloat()) {
102 if (HasMips64)
103 addRegisterClass(MVT::f64, Mips::FGR64RegisterClass);
104 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000105 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Akira Hatanaka792016b2011-09-23 18:28:39 +0000106 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000107
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000108 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000109 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
110 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
111 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000112
Eli Friedman6055a6a2009-07-17 04:07:24 +0000113 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000114 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
115 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000116
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000117 // Used by legalize types to correctly generate the setcc result.
118 // Without this, every float setcc comes with a AND/OR with the result,
119 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000120 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000121 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000122
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000123 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +0000124 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Akira Hatanakaa5903ac2011-10-11 00:55:05 +0000125 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000126 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Akira Hatanaka9b944a82011-11-16 22:42:10 +0000127 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000128 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Akira Hatanakaca074792011-12-08 20:34:32 +0000129 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +0000131 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Akira Hatanaka620db892011-11-16 22:44:38 +0000133 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 setOperationAction(ISD::SELECT, MVT::f32, Custom);
135 setOperationAction(ISD::SELECT, MVT::f64, Custom);
136 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000137 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
138 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Akira Hatanaka93883832011-12-20 23:35:46 +0000139 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000140 setOperationAction(ISD::VASTART, MVT::Other, Custom);
141
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000142 setOperationAction(ISD::SDIV, MVT::i32, Expand);
143 setOperationAction(ISD::SREM, MVT::i32, Expand);
144 setOperationAction(ISD::UDIV, MVT::i32, Expand);
145 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000146 setOperationAction(ISD::SDIV, MVT::i64, Expand);
147 setOperationAction(ISD::SREM, MVT::i64, Expand);
148 setOperationAction(ISD::UDIV, MVT::i64, Expand);
149 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000150
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000151 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000152 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
153 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
154 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
155 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
156 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
157 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
158 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
159 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000160 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
161 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
162 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
163 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000164 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000165 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000166
Akira Hatanaka56633442011-09-20 23:53:09 +0000167 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000168 setOperationAction(ISD::ROTR, MVT::i32, Expand);
169
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000170 if (!Subtarget->hasMips64r2())
171 setOperationAction(ISD::ROTR, MVT::i64, Expand);
172
Owen Anderson825b72b2009-08-11 20:47:22 +0000173 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
174 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
175 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000176 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
177 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000178 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000179 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000181 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000182 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
183 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000184 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000185 setOperationAction(ISD::FLOG, MVT::f32, Expand);
186 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
187 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
188 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000189 setOperationAction(ISD::FMA, MVT::f32, Expand);
190 setOperationAction(ISD::FMA, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000191
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000192 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
193 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Eric Christopher471e4222011-06-08 23:55:35 +0000194
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000195 setOperationAction(ISD::VAARG, MVT::Other, Expand);
196 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
197 setOperationAction(ISD::VAEND, MVT::Other, Expand);
198
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000199 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000200 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
201 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000202
Akira Hatanakadb548262011-07-19 23:30:50 +0000203 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000204 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000205
Eli Friedman4db5aca2011-08-29 18:23:02 +0000206 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
207 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
208
Eli Friedman26689ac2011-08-03 21:06:02 +0000209 setInsertFencesForAtomic(true);
210
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000211 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000213
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000214 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
216 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000217 }
218
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000219 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000220 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000221
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000222 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000223 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000224
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000225 setTargetDAGCombine(ISD::ADDE);
226 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000227 setTargetDAGCombine(ISD::SDIVREM);
228 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000229 setTargetDAGCombine(ISD::SETCC);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000230 setTargetDAGCombine(ISD::AND);
231 setTargetDAGCombine(ISD::OR);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000232
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000233 setMinFunctionAlignment(2);
234
Akira Hatanaka056a1bc2011-12-20 23:28:36 +0000235 setStackPointerRegisterToSaveRestore(HasMips64 ? Mips::SP_64 : Mips::SP);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000236 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000237
238 setExceptionPointerRegister(Mips::A0);
239 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000240}
241
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000242bool MipsTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Akira Hatanaka511961a2011-08-17 18:49:18 +0000243 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
Akira Hatanaka7bd19bd2011-10-11 00:27:28 +0000244 return SVT == MVT::i64 || SVT == MVT::i32 || SVT == MVT::i16;
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000245}
246
Duncan Sands28b77e92011-09-06 19:07:46 +0000247EVT MipsTargetLowering::getSetCCResultType(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000248 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000249}
250
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000251// SelectMadd -
252// Transforms a subgraph in CurDAG if the following pattern is found:
253// (addc multLo, Lo0), (adde multHi, Hi0),
254// where,
255// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000256// Lo0: initial value of Lo register
257// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000258// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000259static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000260 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000261 // for the matching to be successful.
262 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
263
264 if (ADDCNode->getOpcode() != ISD::ADDC)
265 return false;
266
267 SDValue MultHi = ADDENode->getOperand(0);
268 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000269 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000270 unsigned MultOpc = MultHi.getOpcode();
271
272 // MultHi and MultLo must be generated by the same node,
273 if (MultLo.getNode() != MultNode)
274 return false;
275
276 // and it must be a multiplication.
277 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
278 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000279
280 // MultLo amd MultHi must be the first and second output of MultNode
281 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000282 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
283 return false;
284
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000285 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000286 // of the values of MultNode, in which case MultNode will be removed in later
287 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000288 // If there exist users other than ADDENode or ADDCNode, this function returns
289 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000290 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000291 // produced.
292 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
293 return false;
294
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000295 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000296 DebugLoc dl = ADDENode->getDebugLoc();
297
298 // create MipsMAdd(u) node
299 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000300
Akira Hatanaka82099682011-12-19 19:52:25 +0000301 SDValue MAdd = CurDAG->getNode(MultOpc, dl, MVT::Glue,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000302 MultNode->getOperand(0),// Factor 0
303 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000304 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000305 ADDENode->getOperand(1));// Hi0
306
307 // create CopyFromReg nodes
308 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
309 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000310 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000311 Mips::HI, MVT::i32,
312 CopyFromLo.getValue(2));
313
314 // replace uses of adde and addc here
315 if (!SDValue(ADDCNode, 0).use_empty())
316 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
317
318 if (!SDValue(ADDENode, 0).use_empty())
319 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
320
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000321 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000322}
323
324// SelectMsub -
325// Transforms a subgraph in CurDAG if the following pattern is found:
326// (addc Lo0, multLo), (sube Hi0, multHi),
327// where,
328// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000329// Lo0: initial value of Lo register
330// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000331// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000332static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000333 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000334 // for the matching to be successful.
335 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
336
337 if (SUBCNode->getOpcode() != ISD::SUBC)
338 return false;
339
340 SDValue MultHi = SUBENode->getOperand(1);
341 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000342 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000343 unsigned MultOpc = MultHi.getOpcode();
344
345 // MultHi and MultLo must be generated by the same node,
346 if (MultLo.getNode() != MultNode)
347 return false;
348
349 // and it must be a multiplication.
350 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
351 return false;
352
353 // MultLo amd MultHi must be the first and second output of MultNode
354 // respectively.
355 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
356 return false;
357
358 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
359 // of the values of MultNode, in which case MultNode will be removed in later
360 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000361 // If there exist users other than SUBENode or SUBCNode, this function returns
362 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000363 // instruction node rather than a pair of MULT and MSUB instructions being
364 // produced.
365 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
366 return false;
367
368 SDValue Chain = CurDAG->getEntryNode();
369 DebugLoc dl = SUBENode->getDebugLoc();
370
371 // create MipsSub(u) node
372 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
373
Akira Hatanaka82099682011-12-19 19:52:25 +0000374 SDValue MSub = CurDAG->getNode(MultOpc, dl, MVT::Glue,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000375 MultNode->getOperand(0),// Factor 0
376 MultNode->getOperand(1),// Factor 1
377 SUBCNode->getOperand(0),// Lo0
378 SUBENode->getOperand(0));// Hi0
379
380 // create CopyFromReg nodes
381 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
382 MSub);
383 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
384 Mips::HI, MVT::i32,
385 CopyFromLo.getValue(2));
386
387 // replace uses of sube and subc here
388 if (!SDValue(SUBCNode, 0).use_empty())
389 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
390
391 if (!SDValue(SUBENode, 0).use_empty())
392 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
393
394 return true;
395}
396
397static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
398 TargetLowering::DAGCombinerInfo &DCI,
399 const MipsSubtarget* Subtarget) {
400 if (DCI.isBeforeLegalize())
401 return SDValue();
402
Akira Hatanakae184fec2011-11-11 04:18:21 +0000403 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
404 SelectMadd(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000405 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000406
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000407 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000408}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000409
410static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
411 TargetLowering::DAGCombinerInfo &DCI,
412 const MipsSubtarget* Subtarget) {
413 if (DCI.isBeforeLegalize())
414 return SDValue();
415
Akira Hatanakae184fec2011-11-11 04:18:21 +0000416 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
417 SelectMsub(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000418 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000419
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000420 return SDValue();
421}
422
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000423static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
424 TargetLowering::DAGCombinerInfo &DCI,
425 const MipsSubtarget* Subtarget) {
426 if (DCI.isBeforeLegalizeOps())
427 return SDValue();
428
Akira Hatanakadda4a072011-10-03 21:06:13 +0000429 EVT Ty = N->getValueType(0);
430 unsigned LO = (Ty == MVT::i32) ? Mips::LO : Mips::LO64;
431 unsigned HI = (Ty == MVT::i32) ? Mips::HI : Mips::HI64;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000432 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
433 MipsISD::DivRemU;
434 DebugLoc dl = N->getDebugLoc();
435
436 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
437 N->getOperand(0), N->getOperand(1));
438 SDValue InChain = DAG.getEntryNode();
439 SDValue InGlue = DivRem;
440
441 // insert MFLO
442 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakadda4a072011-10-03 21:06:13 +0000443 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000444 InGlue);
445 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
446 InChain = CopyFromLo.getValue(1);
447 InGlue = CopyFromLo.getValue(2);
448 }
449
450 // insert MFHI
451 if (N->hasAnyUseOfValue(1)) {
452 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000453 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000454 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
455 }
456
457 return SDValue();
458}
459
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000460static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
461 switch (CC) {
462 default: llvm_unreachable("Unknown fp condition code!");
463 case ISD::SETEQ:
464 case ISD::SETOEQ: return Mips::FCOND_OEQ;
465 case ISD::SETUNE: return Mips::FCOND_UNE;
466 case ISD::SETLT:
467 case ISD::SETOLT: return Mips::FCOND_OLT;
468 case ISD::SETGT:
469 case ISD::SETOGT: return Mips::FCOND_OGT;
470 case ISD::SETLE:
471 case ISD::SETOLE: return Mips::FCOND_OLE;
472 case ISD::SETGE:
473 case ISD::SETOGE: return Mips::FCOND_OGE;
474 case ISD::SETULT: return Mips::FCOND_ULT;
475 case ISD::SETULE: return Mips::FCOND_ULE;
476 case ISD::SETUGT: return Mips::FCOND_UGT;
477 case ISD::SETUGE: return Mips::FCOND_UGE;
478 case ISD::SETUO: return Mips::FCOND_UN;
479 case ISD::SETO: return Mips::FCOND_OR;
480 case ISD::SETNE:
481 case ISD::SETONE: return Mips::FCOND_ONE;
482 case ISD::SETUEQ: return Mips::FCOND_UEQ;
483 }
484}
485
486
487// Returns true if condition code has to be inverted.
488static bool InvertFPCondCode(Mips::CondCode CC) {
489 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
490 return false;
491
Akira Hatanaka82099682011-12-19 19:52:25 +0000492 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
493 "Illegal Condition Code");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000494
Akira Hatanaka82099682011-12-19 19:52:25 +0000495 return true;
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000496}
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 Hatanaka82099682011-12-19 19:52:25 +0000585 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000586 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000587}
588
589static SDValue PerformORCombine(SDNode *N, SelectionDAG& DAG,
590 TargetLowering::DAGCombinerInfo &DCI,
591 const MipsSubtarget* Subtarget) {
592 // Pattern match INS.
593 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
594 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
595 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000596 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000597 return SDValue();
598
599 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
600 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
601 ConstantSDNode *CN;
602
603 // See if Op's first operand matches (and $src1 , mask0).
604 if (And0.getOpcode() != ISD::AND)
605 return SDValue();
606
607 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000608 !IsShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000609 return SDValue();
610
611 // See if Op's second operand matches (and (shl $src, pos), mask1).
612 if (And1.getOpcode() != ISD::AND)
613 return SDValue();
614
615 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000616 !IsShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000617 return SDValue();
618
619 // The shift masks must have the same position and size.
620 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
621 return SDValue();
622
623 SDValue Shl = And1.getOperand(0);
624 if (Shl.getOpcode() != ISD::SHL)
625 return SDValue();
626
627 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
628 return SDValue();
629
630 unsigned Shamt = CN->getZExtValue();
631
632 // Return if the shift amount and the first bit position of mask are not the
633 // same.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000634 EVT ValTy = N->getValueType(0);
635 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000636 return SDValue();
637
Akira Hatanaka82099682011-12-19 19:52:25 +0000638 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), ValTy, Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000639 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka82099682011-12-19 19:52:25 +0000640 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000641}
642
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000643SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000644 const {
645 SelectionDAG &DAG = DCI.DAG;
646 unsigned opc = N->getOpcode();
647
648 switch (opc) {
649 default: break;
650 case ISD::ADDE:
651 return PerformADDECombine(N, DAG, DCI, Subtarget);
652 case ISD::SUBE:
653 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000654 case ISD::SDIVREM:
655 case ISD::UDIVREM:
656 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000657 case ISD::SETCC:
658 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000659 case ISD::AND:
660 return PerformANDCombine(N, DAG, DCI, Subtarget);
661 case ISD::OR:
662 return PerformORCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000663 }
664
665 return SDValue();
666}
667
Dan Gohman475871a2008-07-27 21:46:04 +0000668SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000669LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000670{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000671 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000672 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000673 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000674 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
675 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000676 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000677 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000678 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
679 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000680 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000681 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000682 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000683 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Akira Hatanakadb548262011-07-19 23:30:50 +0000684 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +0000685 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000686 }
Dan Gohman475871a2008-07-27 21:46:04 +0000687 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000688}
689
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000690//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000691// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000692//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000693
694// AddLiveIn - This helper function adds the specified physical register to the
695// MachineFunction as a live in value. It also creates a corresponding
696// virtual register for it.
697static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000698AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000699{
700 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000701 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
702 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000703 return VReg;
704}
705
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000706// Get fp branch code (not opcode) from condition code.
707static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
708 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
709 return Mips::BRANCH_T;
710
Akira Hatanaka82099682011-12-19 19:52:25 +0000711 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
712 "Invalid CondCode.");
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000713
Akira Hatanaka82099682011-12-19 19:52:25 +0000714 return Mips::BRANCH_F;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000715}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000716
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000717/*
Akira Hatanaka14487d42011-06-07 19:28:39 +0000718static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
719 DebugLoc dl,
720 const MipsSubtarget* Subtarget,
721 const TargetInstrInfo *TII,
722 bool isFPCmp, unsigned Opc) {
723 // There is no need to expand CMov instructions if target has
724 // conditional moves.
725 if (Subtarget->hasCondMov())
726 return BB;
727
728 // To "insert" a SELECT_CC instruction, we actually have to insert the
729 // diamond control-flow pattern. The incoming instruction knows the
730 // destination vreg to set, the condition code register to branch on, the
731 // true/false values to select between, and a branch opcode to use.
732 const BasicBlock *LLVM_BB = BB->getBasicBlock();
733 MachineFunction::iterator It = BB;
734 ++It;
735
736 // thisMBB:
737 // ...
738 // TrueVal = ...
739 // setcc r1, r2, r3
740 // bNE r1, r0, copy1MBB
741 // fallthrough --> copy0MBB
742 MachineBasicBlock *thisMBB = BB;
743 MachineFunction *F = BB->getParent();
744 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
745 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
746 F->insert(It, copy0MBB);
747 F->insert(It, sinkMBB);
748
749 // Transfer the remainder of BB and its successor edges to sinkMBB.
750 sinkMBB->splice(sinkMBB->begin(), BB,
751 llvm::next(MachineBasicBlock::iterator(MI)),
752 BB->end());
753 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
754
755 // Next, add the true and fallthrough blocks as its successors.
756 BB->addSuccessor(copy0MBB);
757 BB->addSuccessor(sinkMBB);
758
759 // Emit the right instruction according to the type of the operands compared
760 if (isFPCmp)
761 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
762 else
763 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
764 .addReg(Mips::ZERO).addMBB(sinkMBB);
765
766 // copy0MBB:
767 // %FalseValue = ...
768 // # fallthrough to sinkMBB
769 BB = copy0MBB;
770
771 // Update machine-CFG edges
772 BB->addSuccessor(sinkMBB);
773
774 // sinkMBB:
775 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
776 // ...
777 BB = sinkMBB;
778
779 if (isFPCmp)
780 BuildMI(*BB, BB->begin(), dl,
781 TII->get(Mips::PHI), MI->getOperand(0).getReg())
782 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
783 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
784 else
785 BuildMI(*BB, BB->begin(), dl,
786 TII->get(Mips::PHI), MI->getOperand(0).getReg())
787 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
788 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
789
790 MI->eraseFromParent(); // The pseudo instruction is gone now.
791 return BB;
792}
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000793*/
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000794MachineBasicBlock *
795MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000796 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000797 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000798 default:
799 assert(false && "Unexpected instr type to insert");
800 return NULL;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000801 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000802 case Mips::ATOMIC_LOAD_ADD_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000803 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
804 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000805 case Mips::ATOMIC_LOAD_ADD_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000806 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
807 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000808 case Mips::ATOMIC_LOAD_ADD_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000809 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000810 case Mips::ATOMIC_LOAD_ADD_I64:
811 case Mips::ATOMIC_LOAD_ADD_I64_P8:
812 return EmitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000813
814 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000815 case Mips::ATOMIC_LOAD_AND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000816 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
817 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000818 case Mips::ATOMIC_LOAD_AND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000819 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
820 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000821 case Mips::ATOMIC_LOAD_AND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000822 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000823 case Mips::ATOMIC_LOAD_AND_I64:
824 case Mips::ATOMIC_LOAD_AND_I64_P8:
Akira Hatanaka73866122011-11-12 02:38:12 +0000825 return EmitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000826
827 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000828 case Mips::ATOMIC_LOAD_OR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000829 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
830 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000831 case Mips::ATOMIC_LOAD_OR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000832 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
833 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000834 case Mips::ATOMIC_LOAD_OR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000835 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000836 case Mips::ATOMIC_LOAD_OR_I64:
837 case Mips::ATOMIC_LOAD_OR_I64_P8:
838 return EmitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000839
840 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000841 case Mips::ATOMIC_LOAD_XOR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000842 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
843 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000844 case Mips::ATOMIC_LOAD_XOR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000845 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
846 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000847 case Mips::ATOMIC_LOAD_XOR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000848 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000849 case Mips::ATOMIC_LOAD_XOR_I64:
850 case Mips::ATOMIC_LOAD_XOR_I64_P8:
851 return EmitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000852
853 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000854 case Mips::ATOMIC_LOAD_NAND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000855 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
856 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000857 case Mips::ATOMIC_LOAD_NAND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000858 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
859 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000860 case Mips::ATOMIC_LOAD_NAND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000861 return EmitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000862 case Mips::ATOMIC_LOAD_NAND_I64:
863 case Mips::ATOMIC_LOAD_NAND_I64_P8:
864 return EmitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000865
866 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000867 case Mips::ATOMIC_LOAD_SUB_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000868 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
869 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000870 case Mips::ATOMIC_LOAD_SUB_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000871 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
872 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000873 case Mips::ATOMIC_LOAD_SUB_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000874 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000875 case Mips::ATOMIC_LOAD_SUB_I64:
876 case Mips::ATOMIC_LOAD_SUB_I64_P8:
877 return EmitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000878
879 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000880 case Mips::ATOMIC_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000881 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
882 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000883 case Mips::ATOMIC_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000884 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
885 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000886 case Mips::ATOMIC_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000887 return EmitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000888 case Mips::ATOMIC_SWAP_I64:
889 case Mips::ATOMIC_SWAP_I64_P8:
890 return EmitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000891
892 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000893 case Mips::ATOMIC_CMP_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000894 return EmitAtomicCmpSwapPartword(MI, BB, 1);
895 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000896 case Mips::ATOMIC_CMP_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000897 return EmitAtomicCmpSwapPartword(MI, BB, 2);
898 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000899 case Mips::ATOMIC_CMP_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000900 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000901 case Mips::ATOMIC_CMP_SWAP_I64:
902 case Mips::ATOMIC_CMP_SWAP_I64_P8:
903 return EmitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000904 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000905}
906
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000907// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
908// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
909MachineBasicBlock *
910MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000911 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000912 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000913 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000914
915 MachineFunction *MF = BB->getParent();
916 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000917 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000918 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
919 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000920 unsigned LL, SC, AND, NOR, ZERO, BEQ;
921
922 if (Size == 4) {
923 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
924 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
925 AND = Mips::AND;
926 NOR = Mips::NOR;
927 ZERO = Mips::ZERO;
928 BEQ = Mips::BEQ;
929 }
930 else {
931 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
932 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
933 AND = Mips::AND64;
934 NOR = Mips::NOR64;
935 ZERO = Mips::ZERO_64;
936 BEQ = Mips::BEQ64;
937 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000938
Akira Hatanaka4061da12011-07-19 20:11:17 +0000939 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000940 unsigned Ptr = MI->getOperand(1).getReg();
941 unsigned Incr = MI->getOperand(2).getReg();
942
Akira Hatanaka4061da12011-07-19 20:11:17 +0000943 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
944 unsigned AndRes = RegInfo.createVirtualRegister(RC);
945 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000946
947 // insert new blocks after the current block
948 const BasicBlock *LLVM_BB = BB->getBasicBlock();
949 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
950 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
951 MachineFunction::iterator It = BB;
952 ++It;
953 MF->insert(It, loopMBB);
954 MF->insert(It, exitMBB);
955
956 // Transfer the remainder of BB and its successor edges to exitMBB.
957 exitMBB->splice(exitMBB->begin(), BB,
958 llvm::next(MachineBasicBlock::iterator(MI)),
959 BB->end());
960 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
961
962 // thisMBB:
963 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000964 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000965 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000966 loopMBB->addSuccessor(loopMBB);
967 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000968
969 // loopMBB:
970 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000971 // <binop> storeval, oldval, incr
972 // sc success, storeval, 0(ptr)
973 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000974 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +0000975 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000976 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000977 // and andres, oldval, incr
978 // nor storeval, $0, andres
Akira Hatanaka59068062011-11-11 04:14:30 +0000979 BuildMI(BB, dl, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
980 BuildMI(BB, dl, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000981 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000982 // <binop> storeval, oldval, incr
983 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000984 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000985 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000986 }
Akira Hatanaka59068062011-11-11 04:14:30 +0000987 BuildMI(BB, dl, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
988 BuildMI(BB, dl, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000989
990 MI->eraseFromParent(); // The instruction is gone now.
991
Akira Hatanaka939ece12011-07-19 03:42:13 +0000992 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000993}
994
995MachineBasicBlock *
996MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000997 MachineBasicBlock *BB,
998 unsigned Size, unsigned BinOpcode,
999 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001000 assert((Size == 1 || Size == 2) &&
1001 "Unsupported size for EmitAtomicBinaryPartial.");
1002
1003 MachineFunction *MF = BB->getParent();
1004 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1005 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1006 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1007 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001008 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1009 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001010
1011 unsigned Dest = MI->getOperand(0).getReg();
1012 unsigned Ptr = MI->getOperand(1).getReg();
1013 unsigned Incr = MI->getOperand(2).getReg();
1014
Akira Hatanaka4061da12011-07-19 20:11:17 +00001015 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1016 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001017 unsigned Mask = RegInfo.createVirtualRegister(RC);
1018 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001019 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1020 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001021 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001022 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1023 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1024 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1025 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1026 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001027 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001028 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1029 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1030 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1031 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1032 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001033
1034 // insert new blocks after the current block
1035 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1036 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001037 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001038 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1039 MachineFunction::iterator It = BB;
1040 ++It;
1041 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001042 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001043 MF->insert(It, exitMBB);
1044
1045 // Transfer the remainder of BB and its successor edges to exitMBB.
1046 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001047 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001048 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1049
Akira Hatanaka81b44112011-07-19 17:09:53 +00001050 BB->addSuccessor(loopMBB);
1051 loopMBB->addSuccessor(loopMBB);
1052 loopMBB->addSuccessor(sinkMBB);
1053 sinkMBB->addSuccessor(exitMBB);
1054
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001055 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001056 // addiu masklsb2,$0,-4 # 0xfffffffc
1057 // and alignedaddr,ptr,masklsb2
1058 // andi ptrlsb2,ptr,3
1059 // sll shiftamt,ptrlsb2,3
1060 // ori maskupper,$0,255 # 0xff
1061 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001062 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001063 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001064
1065 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001066 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1067 .addReg(Mips::ZERO).addImm(-4);
1068 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1069 .addReg(Ptr).addReg(MaskLSB2);
1070 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1071 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1072 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1073 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001074 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1075 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001076 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001077 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001078
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001079 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001080 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001081 // ll oldval,0(alignedaddr)
1082 // binop binopres,oldval,incr2
1083 // and newval,binopres,mask
1084 // and maskedoldval0,oldval,mask2
1085 // or storeval,maskedoldval0,newval
1086 // sc success,storeval,0(alignedaddr)
1087 // beq success,$0,loopMBB
1088
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001089 // atomic.swap
1090 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001091 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001092 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001093 // and maskedoldval0,oldval,mask2
1094 // or storeval,maskedoldval0,newval
1095 // sc success,storeval,0(alignedaddr)
1096 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001097
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001098 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001099 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001100 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001101 // and andres, oldval, incr2
1102 // nor binopres, $0, andres
1103 // and newval, binopres, mask
1104 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1105 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1106 .addReg(Mips::ZERO).addReg(AndRes);
1107 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001108 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001109 // <binop> binopres, oldval, incr2
1110 // and newval, binopres, mask
1111 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1112 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001113 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001114 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001115 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001116 }
1117
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001118 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001119 .addReg(OldVal).addReg(Mask2);
1120 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001121 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001122 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001123 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001124 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001125 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001126
Akira Hatanaka939ece12011-07-19 03:42:13 +00001127 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001128 // and maskedoldval1,oldval,mask
1129 // srl srlres,maskedoldval1,shiftamt
1130 // sll sllres,srlres,24
1131 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001132 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001133 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001134
Akira Hatanaka4061da12011-07-19 20:11:17 +00001135 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1136 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001137 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1138 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001139 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1140 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001141 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001142 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001143
1144 MI->eraseFromParent(); // The instruction is gone now.
1145
Akira Hatanaka939ece12011-07-19 03:42:13 +00001146 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001147}
1148
1149MachineBasicBlock *
1150MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001151 MachineBasicBlock *BB,
1152 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001153 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001154
1155 MachineFunction *MF = BB->getParent();
1156 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001157 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001158 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1159 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001160 unsigned LL, SC, ZERO, BNE, BEQ;
1161
1162 if (Size == 4) {
1163 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1164 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1165 ZERO = Mips::ZERO;
1166 BNE = Mips::BNE;
1167 BEQ = Mips::BEQ;
1168 }
1169 else {
1170 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1171 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1172 ZERO = Mips::ZERO_64;
1173 BNE = Mips::BNE64;
1174 BEQ = Mips::BEQ64;
1175 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001176
1177 unsigned Dest = MI->getOperand(0).getReg();
1178 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001179 unsigned OldVal = MI->getOperand(2).getReg();
1180 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001181
Akira Hatanaka4061da12011-07-19 20:11:17 +00001182 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001183
1184 // insert new blocks after the current block
1185 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1186 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1187 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1188 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1189 MachineFunction::iterator It = BB;
1190 ++It;
1191 MF->insert(It, loop1MBB);
1192 MF->insert(It, loop2MBB);
1193 MF->insert(It, exitMBB);
1194
1195 // Transfer the remainder of BB and its successor edges to exitMBB.
1196 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001197 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001198 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1199
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001200 // thisMBB:
1201 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001202 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001203 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001204 loop1MBB->addSuccessor(exitMBB);
1205 loop1MBB->addSuccessor(loop2MBB);
1206 loop2MBB->addSuccessor(loop1MBB);
1207 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001208
1209 // loop1MBB:
1210 // ll dest, 0(ptr)
1211 // bne dest, oldval, exitMBB
1212 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001213 BuildMI(BB, dl, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1214 BuildMI(BB, dl, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001215 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001216
1217 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001218 // sc success, newval, 0(ptr)
1219 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001220 BB = loop2MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001221 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001222 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka59068062011-11-11 04:14:30 +00001223 BuildMI(BB, dl, TII->get(BEQ))
1224 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001225
1226 MI->eraseFromParent(); // The instruction is gone now.
1227
Akira Hatanaka939ece12011-07-19 03:42:13 +00001228 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001229}
1230
1231MachineBasicBlock *
1232MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001233 MachineBasicBlock *BB,
1234 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001235 assert((Size == 1 || Size == 2) &&
1236 "Unsupported size for EmitAtomicCmpSwapPartial.");
1237
1238 MachineFunction *MF = BB->getParent();
1239 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1240 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1241 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1242 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001243 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1244 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001245
1246 unsigned Dest = MI->getOperand(0).getReg();
1247 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001248 unsigned CmpVal = MI->getOperand(2).getReg();
1249 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001250
Akira Hatanaka4061da12011-07-19 20:11:17 +00001251 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1252 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001253 unsigned Mask = RegInfo.createVirtualRegister(RC);
1254 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001255 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1256 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1257 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1258 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1259 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1260 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1261 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1262 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1263 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1264 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1265 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1266 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1267 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1268 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001269
1270 // insert new blocks after the current block
1271 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1272 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1273 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001274 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001275 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1276 MachineFunction::iterator It = BB;
1277 ++It;
1278 MF->insert(It, loop1MBB);
1279 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001280 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001281 MF->insert(It, exitMBB);
1282
1283 // Transfer the remainder of BB and its successor edges to exitMBB.
1284 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001285 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001286 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1287
Akira Hatanaka81b44112011-07-19 17:09:53 +00001288 BB->addSuccessor(loop1MBB);
1289 loop1MBB->addSuccessor(sinkMBB);
1290 loop1MBB->addSuccessor(loop2MBB);
1291 loop2MBB->addSuccessor(loop1MBB);
1292 loop2MBB->addSuccessor(sinkMBB);
1293 sinkMBB->addSuccessor(exitMBB);
1294
Akira Hatanaka70564a92011-07-19 18:14:26 +00001295 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001296 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001297 // addiu masklsb2,$0,-4 # 0xfffffffc
1298 // and alignedaddr,ptr,masklsb2
1299 // andi ptrlsb2,ptr,3
1300 // sll shiftamt,ptrlsb2,3
1301 // ori maskupper,$0,255 # 0xff
1302 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001303 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001304 // andi maskedcmpval,cmpval,255
1305 // sll shiftedcmpval,maskedcmpval,shiftamt
1306 // andi maskednewval,newval,255
1307 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001308 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001309 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1310 .addReg(Mips::ZERO).addImm(-4);
1311 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1312 .addReg(Ptr).addReg(MaskLSB2);
1313 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1314 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1315 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1316 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001317 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1318 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001319 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001320 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1321 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001322 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1323 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001324 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1325 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001326 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1327 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001328
1329 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001330 // ll oldval,0(alginedaddr)
1331 // and maskedoldval0,oldval,mask
1332 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001333 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001334 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001335 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1336 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001337 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001338 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001339
1340 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001341 // and maskedoldval1,oldval,mask2
1342 // or storeval,maskedoldval1,shiftednewval
1343 // sc success,storeval,0(alignedaddr)
1344 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001345 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001346 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1347 .addReg(OldVal).addReg(Mask2);
1348 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1349 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001350 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001351 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001352 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001353 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001354
Akira Hatanaka939ece12011-07-19 03:42:13 +00001355 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001356 // srl srlres,maskedoldval0,shiftamt
1357 // sll sllres,srlres,24
1358 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001359 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001360 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001361
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001362 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1363 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001364 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1365 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001366 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001367 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001368
1369 MI->eraseFromParent(); // The instruction is gone now.
1370
Akira Hatanaka939ece12011-07-19 03:42:13 +00001371 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001372}
1373
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001374//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001375// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001376//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001377SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001378LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001379{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001380 MachineFunction &MF = DAG.getMachineFunction();
1381 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001382 unsigned SP = IsN64 ? Mips::SP_64 : Mips::SP;
Akira Hatanaka21afc632011-06-21 00:40:49 +00001383
1384 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001385 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1386 "Cannot lower if the alignment of the allocated space is larger than \
1387 that of the stack.");
1388
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001389 SDValue Chain = Op.getOperand(0);
1390 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001391 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001392
1393 // Get a reference from Mips stack pointer
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001394 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, SP, getPointerTy());
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001395
1396 // Subtract the dynamic size from the actual stack size to
1397 // obtain the new stack size.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001398 SDValue Sub = DAG.getNode(ISD::SUB, dl, getPointerTy(), StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001399
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001400 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001401 // must be placed in the stack pointer register.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001402 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, SP, Sub, SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001403
1404 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001405 // value and a chain
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001406 SDVTList VTLs = DAG.getVTList(getPointerTy(), MVT::Other);
Akira Hatanaka21afc632011-06-21 00:40:49 +00001407 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1408 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1409
1410 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001411}
1412
1413SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001414LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001415{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001416 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001417 // the block to branch to if the condition is true.
1418 SDValue Chain = Op.getOperand(0);
1419 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001420 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001421
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001422 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1423
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001424 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001425 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001426 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001427
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001428 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001429 Mips::CondCode CC =
1430 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001431 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001432
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001433 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001434 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001435}
1436
1437SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001438LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001439{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001440 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001441
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001442 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001443 if (Cond.getOpcode() != MipsISD::FPCmp)
1444 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001445
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001446 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1447 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001448}
1449
Dan Gohmand858e902010-04-17 15:26:15 +00001450SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1451 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001452 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001453 DebugLoc dl = Op.getDebugLoc();
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001454 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001455
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001456 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001457 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001458
Chris Lattnerb71b9092009-08-13 06:28:06 +00001459 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001460
Chris Lattnere3736f82009-08-13 05:41:27 +00001461 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001462 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1463 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001464 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001465 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1466 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001467 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001468 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001469 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001470 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1471 MipsII::MO_ABS_HI);
1472 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1473 MipsII::MO_ABS_LO);
1474 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1475 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001476 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001477 }
1478
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001479 EVT ValTy = Op.getValueType();
1480 bool HasGotOfst = (GV->hasInternalLinkage() ||
1481 (GV->hasLocalLinkage() && !isa<Function>(GV)));
1482 unsigned GotFlag = IsN64 ?
1483 (HasGotOfst ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT_DISP) :
Bruno Cardoso Lopese3d35722011-12-07 00:28:57 +00001484 (HasGotOfst ? MipsII::MO_GOT : MipsII::MO_GOT16);
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001485 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0, GotFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001486 GA = DAG.getNode(MipsISD::Wrapper, dl, ValTy, GA);
Akira Hatanaka82099682011-12-19 19:52:25 +00001487 SDValue ResNode = DAG.getLoad(ValTy, dl, DAG.getEntryNode(), GA,
1488 MachinePointerInfo(), false, false, false, 0);
Akira Hatanaka0f843822011-06-07 18:58:42 +00001489 // On functions and global targets not internal linked only
1490 // a load from got/GP is necessary for PIC to work.
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001491 if (!HasGotOfst)
Akira Hatanaka0f843822011-06-07 18:58:42 +00001492 return ResNode;
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001493 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0,
1494 IsN64 ? MipsII::MO_GOT_OFST :
1495 MipsII::MO_ABS_LO);
1496 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, GALo);
1497 return DAG.getNode(ISD::ADD, dl, ValTy, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001498}
1499
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001500SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1501 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001502 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1503 // FIXME there isn't actually debug info here
1504 DebugLoc dl = Op.getDebugLoc();
1505
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001506 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001507 // %hi/%lo relocation
Akira Hatanaka82099682011-12-19 19:52:25 +00001508 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true, MipsII::MO_ABS_HI);
1509 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true, MipsII::MO_ABS_LO);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001510 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1511 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1512 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001513 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001514
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001515 EVT ValTy = Op.getValueType();
1516 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1517 unsigned OFSTFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1518 SDValue BAGOTOffset = DAG.getBlockAddress(BA, ValTy, true, GOTFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001519 BAGOTOffset = DAG.getNode(MipsISD::Wrapper, dl, ValTy, BAGOTOffset);
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001520 SDValue BALOOffset = DAG.getBlockAddress(BA, ValTy, true, OFSTFlag);
Akira Hatanaka82099682011-12-19 19:52:25 +00001521 SDValue Load = DAG.getLoad(ValTy, dl, DAG.getEntryNode(), BAGOTOffset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001522 MachinePointerInfo(), false, false, false, 0);
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001523 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, BALOOffset);
1524 return DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001525}
1526
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001527SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001528LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001529{
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001530 // If the relocation model is PIC, use the General Dynamic TLS Model or
1531 // Local Dynamic TLS model, otherwise use the Initial Exec or
1532 // Local Exec TLS Model.
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001533
1534 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1535 DebugLoc dl = GA->getDebugLoc();
1536 const GlobalValue *GV = GA->getGlobal();
1537 EVT PtrVT = getPointerTy();
1538
1539 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1540 // General Dynamic TLS Model
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001541 bool LocalDynamic = GV->hasInternalLinkage();
1542 unsigned Flag = LocalDynamic ? MipsII::MO_TLSLDM :MipsII::MO_TLSGD;
1543 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, Flag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001544 SDValue Argument = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, TGA);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001545 unsigned PtrSize = PtrVT.getSizeInBits();
1546 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1547
Benjamin Kramer5eccf672011-12-11 12:21:34 +00001548 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001549
1550 ArgListTy Args;
1551 ArgListEntry Entry;
1552 Entry.Node = Argument;
Akira Hatanakaca074792011-12-08 20:34:32 +00001553 Entry.Ty = PtrTy;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001554 Args.push_back(Entry);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001555
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001556 std::pair<SDValue, SDValue> CallResult =
Akira Hatanakaca074792011-12-08 20:34:32 +00001557 LowerCallTo(DAG.getEntryNode(), PtrTy,
1558 false, false, false, false, 0, CallingConv::C, false, true,
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001559 TlsGetAddr, Args, DAG, dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001560
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001561 SDValue Ret = CallResult.first;
1562
1563 if (!LocalDynamic)
1564 return Ret;
1565
1566 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1567 MipsII::MO_DTPREL_HI);
1568 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, PtrVT, TGAHi);
1569 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1570 MipsII::MO_DTPREL_LO);
1571 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, TGALo);
1572 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Ret);
1573 return DAG.getNode(ISD::ADD, dl, PtrVT, Add, Lo);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001574 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001575
1576 SDValue Offset;
1577 if (GV->isDeclaration()) {
1578 // Initial Exec TLS Model
Akira Hatanakaca074792011-12-08 20:34:32 +00001579 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001580 MipsII::MO_GOTTPREL);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001581 TGA = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, TGA);
Akira Hatanakaca074792011-12-08 20:34:32 +00001582 Offset = DAG.getLoad(PtrVT, dl,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001583 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001584 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001585 } else {
1586 // Local Exec TLS Model
Akira Hatanakaca074792011-12-08 20:34:32 +00001587 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001588 MipsII::MO_TPREL_HI);
Akira Hatanakaca074792011-12-08 20:34:32 +00001589 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001590 MipsII::MO_TPREL_LO);
Akira Hatanakaca074792011-12-08 20:34:32 +00001591 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, PtrVT, TGAHi);
1592 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, TGALo);
1593 Offset = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001594 }
1595
1596 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1597 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001598}
1599
1600SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001601LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001602{
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001603 SDValue HiPart, JTI, JTILo;
Dale Johannesende064702009-02-06 21:50:26 +00001604 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001605 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001606 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Owen Andersone50ed302009-08-10 22:56:29 +00001607 EVT PtrVT = Op.getValueType();
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001608 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001609
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001610 if (!IsPIC && !IsN64) {
1611 JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MipsII::MO_ABS_HI);
1612 HiPart = DAG.getNode(MipsISD::Hi, dl, PtrVT, JTI);
1613 JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MipsII::MO_ABS_LO);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001614 } else {// Emit Load from Global Pointer
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001615 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1616 unsigned OfstFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1617 JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, GOTFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001618 JTI = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, JTI);
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001619 HiPart = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), JTI,
1620 MachinePointerInfo(), false, false, false, 0);
1621 JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OfstFlag);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001622 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001623
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001624 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, JTILo);
1625 return DAG.getNode(ISD::ADD, dl, PtrVT, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001626}
1627
Dan Gohman475871a2008-07-27 21:46:04 +00001628SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001629LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001630{
Dan Gohman475871a2008-07-27 21:46:04 +00001631 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001632 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001633 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001634 // FIXME there isn't actually debug info here
1635 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001636
1637 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001638 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001639 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001640 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001641 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001642 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001643 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1644 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001645 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001646
1647 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001648 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001649 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001650 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001651 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001652 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1653 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001654 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001655 } else {
Akira Hatanaka620db892011-11-16 22:44:38 +00001656 EVT ValTy = Op.getValueType();
1657 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1658 unsigned OFSTFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1659 SDValue CP = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1660 N->getOffset(), GOTFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001661 CP = DAG.getNode(MipsISD::Wrapper, dl, ValTy, CP);
Akira Hatanaka82099682011-12-19 19:52:25 +00001662 SDValue Load = DAG.getLoad(ValTy, dl, DAG.getEntryNode(), CP,
1663 MachinePointerInfo::getConstantPool(), false,
1664 false, false, 0);
Akira Hatanaka620db892011-11-16 22:44:38 +00001665 SDValue CPLo = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1666 N->getOffset(), OFSTFlag);
1667 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, CPLo);
1668 ResNode = DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001669 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001670
1671 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001672}
1673
Dan Gohmand858e902010-04-17 15:26:15 +00001674SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001675 MachineFunction &MF = DAG.getMachineFunction();
1676 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1677
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001678 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001679 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1680 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001681
1682 // vastart just stores the address of the VarArgsFrameIndex slot into the
1683 // memory location argument.
1684 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001685 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
Akira Hatanaka82099682011-12-19 19:52:25 +00001686 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001687}
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001688
1689// Called if the size of integer registers is large enough to hold the whole
1690// floating point number.
1691static SDValue LowerFCOPYSIGNLargeIntReg(SDValue Op, SelectionDAG &DAG) {
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001692 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001693 EVT ValTy = Op.getValueType();
1694 EVT IntValTy = MVT::getIntegerVT(ValTy.getSizeInBits());
1695 uint64_t Mask = (uint64_t)1 << (ValTy.getSizeInBits() - 1);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001696 DebugLoc dl = Op.getDebugLoc();
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001697 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, IntValTy, Op.getOperand(0));
1698 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, IntValTy, Op.getOperand(1));
1699 SDValue And0 = DAG.getNode(ISD::AND, dl, IntValTy, Op0,
1700 DAG.getConstant(Mask - 1, IntValTy));
1701 SDValue And1 = DAG.getNode(ISD::AND, dl, IntValTy, Op1,
1702 DAG.getConstant(Mask, IntValTy));
1703 SDValue Result = DAG.getNode(ISD::OR, dl, IntValTy, And0, And1);
1704 return DAG.getNode(ISD::BITCAST, dl, ValTy, Result);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001705}
1706
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001707// Called if the size of integer registers is not large enough to hold the whole
1708// floating point number (e.g. f64 & 32-bit integer register).
1709static SDValue
1710LowerFCOPYSIGNSmallIntReg(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001711 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001712 // Use ext/ins instructions if target architecture is Mips32r2.
1713 // Eliminate redundant mfc1 and mtc1 instructions.
1714 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001715
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001716 if (!isLittle)
1717 std::swap(LoIdx, HiIdx);
1718
1719 DebugLoc dl = Op.getDebugLoc();
1720 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1721 Op.getOperand(0),
1722 DAG.getConstant(LoIdx, MVT::i32));
1723 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1724 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1725 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1726 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1727 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1728 DAG.getConstant(0x7fffffff, MVT::i32));
1729 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1730 DAG.getConstant(0x80000000, MVT::i32));
1731 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1732
1733 if (!isLittle)
1734 std::swap(Word0, Word1);
1735
1736 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1737}
1738
Akira Hatanaka82099682011-12-19 19:52:25 +00001739SDValue
1740MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001741 EVT Ty = Op.getValueType();
1742
1743 assert(Ty == MVT::f32 || Ty == MVT::f64);
1744
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001745 if (Ty == MVT::f32 || HasMips64)
1746 return LowerFCOPYSIGNLargeIntReg(Op, DAG);
Akira Hatanaka82099682011-12-19 19:52:25 +00001747
1748 return LowerFCOPYSIGNSmallIntReg(Op, DAG, Subtarget->isLittle());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001749}
1750
Akira Hatanaka2e591472011-06-02 00:24:44 +00001751SDValue MipsTargetLowering::
1752LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001753 // check the depth
1754 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001755 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001756
1757 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1758 MFI->setFrameAddressIsTaken(true);
1759 EVT VT = Op.getValueType();
1760 DebugLoc dl = Op.getDebugLoc();
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001761 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
1762 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001763 return FrameAddr;
1764}
1765
Akira Hatanakadb548262011-07-19 23:30:50 +00001766// TODO: set SType according to the desired memory barrier behavior.
Akira Hatanaka82099682011-12-19 19:52:25 +00001767SDValue
1768MipsTargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG& DAG) const {
Akira Hatanakadb548262011-07-19 23:30:50 +00001769 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
Eli Friedman14648462011-07-27 22:21:52 +00001775SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1776 SelectionDAG& DAG) const {
1777 // FIXME: Need pseudo-fence for 'singlethread' fences
1778 // FIXME: Set SType for weaker fences where supported/appropriate.
1779 unsigned SType = 0;
1780 DebugLoc dl = Op.getDebugLoc();
1781 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1782 DAG.getConstant(SType, MVT::i32));
1783}
1784
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001785//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001786// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001787//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001788
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001789//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001790// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001791// Mips O32 ABI rules:
1792// ---
1793// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001794// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001795// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001796// f64 - Only passed in two aliased f32 registers if no int reg has been used
1797// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001798// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1799// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001800//
1801// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001802//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001803
Duncan Sands1e96bab2010-11-04 10:49:57 +00001804static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001805 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001806 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1807
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001808 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001809
1810 static const unsigned IntRegs[] = {
1811 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1812 };
1813 static const unsigned F32Regs[] = {
1814 Mips::F12, Mips::F14
1815 };
1816 static const unsigned F64Regs[] = {
1817 Mips::D6, Mips::D7
1818 };
1819
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001820 // ByVal Args
1821 if (ArgFlags.isByVal()) {
1822 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1823 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1824 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1825 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1826 r < std::min(IntRegsSize, NextReg); ++r)
1827 State.AllocateReg(IntRegs[r]);
1828 return false;
1829 }
1830
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001831 // Promote i8 and i16
1832 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1833 LocVT = MVT::i32;
1834 if (ArgFlags.isSExt())
1835 LocInfo = CCValAssign::SExt;
1836 else if (ArgFlags.isZExt())
1837 LocInfo = CCValAssign::ZExt;
1838 else
1839 LocInfo = CCValAssign::AExt;
1840 }
1841
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001842 unsigned Reg;
1843
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001844 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1845 // is true: function is vararg, argument is 3rd or higher, there is previous
1846 // argument which is not f32 or f64.
1847 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1848 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001849 unsigned OrigAlign = ArgFlags.getOrigAlign();
1850 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001851
1852 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001853 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001854 // If this is the first part of an i64 arg,
1855 // the allocated register must be either A0 or A2.
1856 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1857 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001858 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001859 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1860 // Allocate int register and shadow next int register. If first
1861 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001862 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1863 if (Reg == Mips::A1 || Reg == Mips::A3)
1864 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1865 State.AllocateReg(IntRegs, IntRegsSize);
1866 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001867 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1868 // we are guaranteed to find an available float register
1869 if (ValVT == MVT::f32) {
1870 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1871 // Shadow int register
1872 State.AllocateReg(IntRegs, IntRegsSize);
1873 } else {
1874 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1875 // Shadow int registers
1876 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1877 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1878 State.AllocateReg(IntRegs, IntRegsSize);
1879 State.AllocateReg(IntRegs, IntRegsSize);
1880 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001881 } else
1882 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001883
Akira Hatanakad37776d2011-05-20 21:39:54 +00001884 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1885 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1886
1887 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001888 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001889 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001890 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001891
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001892 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001893}
1894
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00001895static const unsigned Mips64IntRegs[8] =
1896 {Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
1897 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64};
1898static const unsigned Mips64DPRegs[8] =
1899 {Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
1900 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64};
1901
1902static bool CC_Mips64Byval(unsigned ValNo, MVT ValVT, MVT LocVT,
1903 CCValAssign::LocInfo LocInfo,
1904 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1905 unsigned Align = std::max(ArgFlags.getByValAlign(), (unsigned)8);
1906 unsigned Size = (ArgFlags.getByValSize() + 7) / 8 * 8;
1907 unsigned FirstIdx = State.getFirstUnallocated(Mips64IntRegs, 8);
1908
1909 assert(Align <= 16 && "Cannot handle alignments larger than 16.");
1910
1911 // If byval is 16-byte aligned, the first arg register must be even.
1912 if ((Align == 16) && (FirstIdx % 2)) {
1913 State.AllocateReg(Mips64IntRegs[FirstIdx], Mips64DPRegs[FirstIdx]);
1914 ++FirstIdx;
1915 }
1916
1917 // Mark the registers allocated.
1918 for (unsigned I = FirstIdx; Size && (I < 8); Size -= 8, ++I)
1919 State.AllocateReg(Mips64IntRegs[I], Mips64DPRegs[I]);
1920
1921 // Allocate space on caller's stack.
1922 unsigned Offset = State.AllocateStack(Size, Align);
1923
1924 if (FirstIdx < 8)
1925 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Mips64IntRegs[FirstIdx],
1926 LocVT, LocInfo));
1927 else
1928 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
1929
1930 return true;
1931}
1932
1933#include "MipsGenCallingConv.inc"
1934
Akira Hatanaka49617092011-11-14 19:02:54 +00001935static void
1936AnalyzeMips64CallOperands(CCState CCInfo,
1937 const SmallVectorImpl<ISD::OutputArg> &Outs) {
1938 unsigned NumOps = Outs.size();
1939 for (unsigned i = 0; i != NumOps; ++i) {
1940 MVT ArgVT = Outs[i].VT;
1941 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
1942 bool R;
1943
1944 if (Outs[i].IsFixed)
1945 R = CC_MipsN(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
1946 else
1947 R = CC_MipsN_VarArg(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
1948
Akira Hatanaka49617092011-11-14 19:02:54 +00001949 if (R) {
Benjamin Kramer6296ee32011-11-14 19:51:48 +00001950#ifndef NDEBUG
Akira Hatanaka49617092011-11-14 19:02:54 +00001951 dbgs() << "Call operand #" << i << " has unhandled type "
1952 << EVT(ArgVT).getEVTString();
1953#endif
1954 llvm_unreachable(0);
1955 }
1956 }
1957}
1958
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001959//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001960// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001961//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001962
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001963static const unsigned O32IntRegsSize = 4;
1964
1965static const unsigned O32IntRegs[] = {
1966 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1967};
1968
Akira Hatanaka373e3a42011-09-23 00:58:33 +00001969// Return next O32 integer argument register.
1970static unsigned getNextIntArgReg(unsigned Reg) {
1971 assert((Reg == Mips::A0) || (Reg == Mips::A2));
1972 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
1973}
1974
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001975// Write ByVal Arg to arg registers and stack.
1976static void
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001977WriteByValArg(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001978 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1979 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1980 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001981 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001982 MVT PtrType, bool isLittle) {
1983 unsigned LocMemOffset = VA.getLocMemOffset();
1984 unsigned Offset = 0;
1985 uint32_t RemainingSize = Flags.getByValSize();
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001986 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001987
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001988 // Copy the first 4 words of byval arg to registers A0 - A3.
1989 // FIXME: Use a stricter alignment if it enables better optimization in passes
1990 // run later.
1991 for (; RemainingSize >= 4 && LocMemOffset < 4 * 4;
1992 Offset += 4, RemainingSize -= 4, LocMemOffset += 4) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001993 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001994 DAG.getConstant(Offset, MVT::i32));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001995 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
Akira Hatanaka82099682011-12-19 19:52:25 +00001996 MachinePointerInfo(), false, false, false,
1997 std::min(ByValAlign, (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001998 MemOpChains.push_back(LoadVal.getValue(1));
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001999 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002000 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
2001 }
2002
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002003 if (RemainingSize == 0)
2004 return;
2005
2006 // If there still is a register available for argument passing, write the
2007 // remaining part of the structure to it using subword loads and shifts.
2008 if (LocMemOffset < 4 * 4) {
2009 assert(RemainingSize <= 3 && RemainingSize >= 1 &&
2010 "There must be one to three bytes remaining.");
2011 unsigned LoadSize = (RemainingSize == 3 ? 2 : RemainingSize);
2012 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2013 DAG.getConstant(Offset, MVT::i32));
2014 unsigned Alignment = std::min(ByValAlign, (unsigned )4);
2015 SDValue LoadVal = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2016 LoadPtr, MachinePointerInfo(),
2017 MVT::getIntegerVT(LoadSize * 8), false,
2018 false, Alignment);
2019 MemOpChains.push_back(LoadVal.getValue(1));
2020
2021 // If target is big endian, shift it to the most significant half-word or
2022 // byte.
2023 if (!isLittle)
2024 LoadVal = DAG.getNode(ISD::SHL, dl, MVT::i32, LoadVal,
2025 DAG.getConstant(32 - LoadSize * 8, MVT::i32));
2026
2027 Offset += LoadSize;
2028 RemainingSize -= LoadSize;
2029
2030 // Read second subword if necessary.
2031 if (RemainingSize != 0) {
2032 assert(RemainingSize == 1 && "There must be one byte remaining.");
2033 LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2034 DAG.getConstant(Offset, MVT::i32));
2035 unsigned Alignment = std::min(ByValAlign, (unsigned )2);
2036 SDValue Subword = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2037 LoadPtr, MachinePointerInfo(),
2038 MVT::i8, false, false, Alignment);
2039 MemOpChains.push_back(Subword.getValue(1));
2040 // Insert the loaded byte to LoadVal.
2041 // FIXME: Use INS if supported by target.
2042 unsigned ShiftAmt = isLittle ? 16 : 8;
2043 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i32, Subword,
2044 DAG.getConstant(ShiftAmt, MVT::i32));
2045 LoadVal = DAG.getNode(ISD::OR, dl, MVT::i32, LoadVal, Shift);
2046 }
2047
2048 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
2049 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
2050 return;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002051 }
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002052
2053 // Create a fixed object on stack at offset LocMemOffset and copy
2054 // remaining part of byval arg to it using memcpy.
2055 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2056 DAG.getConstant(Offset, MVT::i32));
2057 LastFI = MFI->CreateFixedObject(RemainingSize, LocMemOffset, true);
2058 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002059 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2060 DAG.getConstant(RemainingSize, MVT::i32),
2061 std::min(ByValAlign, (unsigned)4),
2062 /*isVolatile=*/false, /*AlwaysInline=*/false,
2063 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002064}
2065
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002066// Copy Mips64 byVal arg to registers and stack.
2067void static
2068PassByValArg64(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
2069 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
2070 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
2071 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
2072 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2073 EVT PtrTy, bool isLittle) {
2074 unsigned ByValSize = Flags.getByValSize();
2075 unsigned Alignment = std::min(Flags.getByValAlign(), (unsigned)8);
2076 bool IsRegLoc = VA.isRegLoc();
2077 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
2078 unsigned LocMemOffset = 0;
Akira Hatanaka16040852011-11-15 18:42:25 +00002079 unsigned MemCpySize = ByValSize;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002080
2081 if (!IsRegLoc)
2082 LocMemOffset = VA.getLocMemOffset();
2083 else {
2084 const unsigned *Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8,
2085 VA.getLocReg());
2086 const unsigned *RegEnd = Mips64IntRegs + 8;
2087
2088 // Copy double words to registers.
2089 for (; (Reg != RegEnd) && (ByValSize >= Offset + 8); ++Reg, Offset += 8) {
2090 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2091 DAG.getConstant(Offset, PtrTy));
2092 SDValue LoadVal = DAG.getLoad(MVT::i64, dl, Chain, LoadPtr,
2093 MachinePointerInfo(), false, false, false,
2094 Alignment);
2095 MemOpChains.push_back(LoadVal.getValue(1));
2096 RegsToPass.push_back(std::make_pair(*Reg, LoadVal));
2097 }
2098
Akira Hatanaka16040852011-11-15 18:42:25 +00002099 // Return if the struct has been fully copied.
2100 if (!(MemCpySize = ByValSize - Offset))
2101 return;
2102
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002103 // If there is an argument register available, copy the remainder of the
2104 // byval argument with sub-doubleword loads and shifts.
Akira Hatanaka16040852011-11-15 18:42:25 +00002105 if (Reg != RegEnd) {
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002106 assert((ByValSize < Offset + 8) &&
2107 "Size of the remainder should be smaller than 8-byte.");
2108 SDValue Val;
2109 for (unsigned LoadSize = 4; Offset < ByValSize; LoadSize /= 2) {
2110 unsigned RemSize = ByValSize - Offset;
2111
2112 if (RemSize < LoadSize)
2113 continue;
2114
2115 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2116 DAG.getConstant(Offset, PtrTy));
2117 SDValue LoadVal =
2118 DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i64, Chain, LoadPtr,
2119 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
2120 false, false, Alignment);
2121 MemOpChains.push_back(LoadVal.getValue(1));
2122
2123 // Offset in number of bits from double word boundary.
2124 unsigned OffsetDW = (Offset % 8) * 8;
2125 unsigned Shamt = isLittle ? OffsetDW : 64 - (OffsetDW + LoadSize * 8);
2126 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i64, LoadVal,
2127 DAG.getConstant(Shamt, MVT::i32));
2128
2129 Val = Val.getNode() ? DAG.getNode(ISD::OR, dl, MVT::i64, Val, Shift) :
2130 Shift;
2131 Offset += LoadSize;
2132 Alignment = std::min(Alignment, LoadSize);
2133 }
2134
2135 RegsToPass.push_back(std::make_pair(*Reg, Val));
2136 return;
2137 }
2138 }
2139
Akira Hatanaka16040852011-11-15 18:42:25 +00002140 assert(MemCpySize && "MemCpySize must not be zero.");
2141
2142 // Create a fixed object on stack at offset LocMemOffset and copy
2143 // remainder of byval arg to it with memcpy.
2144 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2145 DAG.getConstant(Offset, PtrTy));
2146 LastFI = MFI->CreateFixedObject(MemCpySize, LocMemOffset, true);
2147 SDValue Dst = DAG.getFrameIndex(LastFI, PtrTy);
2148 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2149 DAG.getConstant(MemCpySize, PtrTy), Alignment,
2150 /*isVolatile=*/false, /*AlwaysInline=*/false,
2151 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002152}
2153
Dan Gohman98ca4f22009-08-05 01:29:28 +00002154/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002155/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002156/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002157SDValue
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002158MipsTargetLowering::LowerCall(SDValue InChain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002159 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002160 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002161 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002162 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002163 const SmallVectorImpl<ISD::InputArg> &Ins,
2164 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002165 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002166 // MIPs target does not yet support tail call optimization.
2167 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002168
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002169 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002170 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002171 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002172 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00002173 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002174
2175 // Analyze operands of the call, assigning locations to each operand.
2176 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002177 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002178 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002179
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002180 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002181 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanaka49617092011-11-14 19:02:54 +00002182 else if (HasMips64)
2183 AnalyzeMips64CallOperands(CCInfo, Outs);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00002184 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002185 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002186
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002187 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002188 unsigned NextStackOffset = CCInfo.getNextStackOffset();
2189
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002190 // Chain is the output chain of the last Load/Store or CopyToReg node.
2191 // ByValChain is the output chain of the last Memcpy node created for copying
2192 // byval arguments to the stack.
2193 SDValue Chain, CallSeqStart, ByValChain;
2194 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
2195 Chain = CallSeqStart = DAG.getCALLSEQ_START(InChain, NextStackOffsetVal);
2196 ByValChain = InChain;
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002197
2198 // If this is the first call, create a stack frame object that points to
2199 // a location to which .cprestore saves $gp.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002200 if (IsO32 && IsPIC && !MipsFI->getGPFI())
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002201 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
2202
Akira Hatanaka21afc632011-06-21 00:40:49 +00002203 // Get the frame index of the stack frame object that points to the location
2204 // of dynamically allocated area on the stack.
2205 int DynAllocFI = MipsFI->getDynAllocFI();
2206
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002207 // Update size of the maximum argument space.
2208 // For O32, a minimum of four words (16 bytes) of argument space is
2209 // allocated.
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002210 if (IsO32)
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002211 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
2212
2213 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
2214
2215 if (MaxCallFrameSize < NextStackOffset) {
2216 MipsFI->setMaxCallFrameSize(NextStackOffset);
2217
Akira Hatanaka21afc632011-06-21 00:40:49 +00002218 // Set the offsets relative to $sp of the $gp restore slot and dynamically
2219 // allocated stack space. These offsets must be aligned to a boundary
2220 // determined by the stack alignment of the ABI.
2221 unsigned StackAlignment = TFL->getStackAlignment();
2222 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
2223 StackAlignment * StackAlignment;
2224
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002225 if (MipsFI->needGPSaveRestore())
Akira Hatanaka21afc632011-06-21 00:40:49 +00002226 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
2227
2228 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002229 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002230
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002231 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002232 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
2233 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002234
Eric Christopher471e4222011-06-08 23:55:35 +00002235 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00002236
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002237 // Walk the register/memloc assignments, inserting copies/loads.
2238 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002239 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002240 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002241 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002242 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2243
2244 // ByVal Arg.
2245 if (Flags.isByVal()) {
2246 assert(Flags.getByValSize() &&
2247 "ByVal args of size 0 should have been ignored by front-end.");
2248 if (IsO32)
2249 WriteByValArg(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2250 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2251 Subtarget->isLittle());
2252 else
2253 PassByValArg64(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2254 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2255 Subtarget->isLittle());
2256 continue;
2257 }
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002258
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002259 // Promote the value if needed.
2260 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002261 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002262 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002263 if (VA.isRegLoc()) {
2264 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
2265 (ValVT == MVT::f64 && LocVT == MVT::i64))
2266 Arg = DAG.getNode(ISD::BITCAST, dl, LocVT, Arg);
2267 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002268 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2269 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002270 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2271 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002272 if (!Subtarget->isLittle())
2273 std::swap(Lo, Hi);
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002274 unsigned LocRegLo = VA.getLocReg();
2275 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2276 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2277 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002278 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002279 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002280 }
2281 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002282 case CCValAssign::SExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002283 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002284 break;
2285 case CCValAssign::ZExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002286 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002287 break;
2288 case CCValAssign::AExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002289 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002290 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002291 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002292
2293 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002294 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002295 if (VA.isRegLoc()) {
2296 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002297 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002298 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002299
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002300 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002301 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002302
Chris Lattnere0b12152008-03-17 06:57:02 +00002303 // Create the frame index object for this incoming parameter
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002304 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002305 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00002306 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00002307
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002308 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002309 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00002310 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Akira Hatanaka82099682011-12-19 19:52:25 +00002311 MachinePointerInfo(), false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002312 }
2313
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002314 // Extend range of indices of frame objects for outgoing arguments that were
2315 // created during this function call. Skip this step if no such objects were
2316 // created.
2317 if (LastFI)
2318 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2319
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002320 // If a memcpy has been created to copy a byval arg to a stack, replace the
2321 // chain input of CallSeqStart with ByValChain.
2322 if (InChain != ByValChain)
2323 DAG.UpdateNodeOperands(CallSeqStart.getNode(), ByValChain,
2324 NextStackOffsetVal);
2325
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002326 // Transform all store nodes into one single node because all store
2327 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002328 if (!MemOpChains.empty())
2329 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002330 &MemOpChains[0], MemOpChains.size());
2331
Bill Wendling056292f2008-09-16 21:48:12 +00002332 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002333 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2334 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002335 unsigned char OpFlag;
2336 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002337 bool GlobalOrExternal = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002338 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002339
2340 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002341 if (IsPICCall && G->getGlobal()->hasInternalLinkage()) {
2342 OpFlag = IsO32 ? MipsII::MO_GOT : MipsII::MO_GOT_PAGE;
2343 unsigned char LoFlag = IsO32 ? MipsII::MO_ABS_LO : MipsII::MO_GOT_OFST;
2344 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(), 0,
2345 OpFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002346 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002347 0, LoFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002348 } else {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002349 OpFlag = IsPICCall ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002350 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2351 getPointerTy(), 0, OpFlag);
2352 }
2353
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002354 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002355 }
2356 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002357 if (IsN64 || (!IsO32 && IsPIC))
2358 OpFlag = MipsII::MO_GOT_DISP;
2359 else if (!IsPIC) // !N64 && static
2360 OpFlag = MipsII::MO_NO_FLAG;
2361 else // O32 & PIC
2362 OpFlag = MipsII::MO_GOT_CALL;
Akira Hatanaka82099682011-12-19 19:52:25 +00002363 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2364 OpFlag);
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002365 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002366 }
2367
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002368 SDValue InFlag;
2369
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002370 // Create nodes that load address of callee and copy it to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002371 if (IsPICCall) {
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002372 if (GlobalOrExternal) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002373 // Load callee address
Akira Hatanaka6df7e232011-12-09 01:53:17 +00002374 Callee = DAG.getNode(MipsISD::Wrapper, dl, getPointerTy(), Callee);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002375 SDValue LoadValue = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
2376 Callee, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002377 false, false, false, 0);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002378
2379 // Use GOT+LO if callee has internal linkage.
2380 if (CalleeLo.getNode()) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002381 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, getPointerTy(), CalleeLo);
2382 Callee = DAG.getNode(ISD::ADD, dl, getPointerTy(), LoadValue, Lo);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002383 } else
2384 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002385 }
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002386 }
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002387
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002388 // T9 should contain the address of the callee function if
2389 // -reloction-model=pic or it is an indirect call.
2390 if (IsPICCall || !GlobalOrExternal) {
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002391 // copy to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002392 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
2393 Chain = DAG.getCopyToReg(Chain, dl, T9Reg, Callee, SDValue(0, 0));
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002394 InFlag = Chain.getValue(1);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002395 Callee = DAG.getRegister(T9Reg, getPointerTy());
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002396 }
Bill Wendling056292f2008-09-16 21:48:12 +00002397
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002398 // Build a sequence of copy-to-reg nodes chained together with token
2399 // chain and flag operands which copy the outgoing args into registers.
2400 // The InFlag in necessary since all emitted instructions must be
2401 // stuck together.
2402 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2403 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2404 RegsToPass[i].second, InFlag);
2405 InFlag = Chain.getValue(1);
2406 }
2407
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002408 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002409 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002410 //
2411 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002412 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002413 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002414 Ops.push_back(Chain);
2415 Ops.push_back(Callee);
2416
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002417 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002418 // known live into the call.
2419 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2420 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2421 RegsToPass[i].second.getValueType()));
2422
Gabor Greifba36cb52008-08-28 21:40:38 +00002423 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002424 Ops.push_back(InFlag);
2425
Dale Johannesen33c960f2009-02-04 20:06:27 +00002426 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002427 InFlag = Chain.getValue(1);
2428
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002429 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002430 Chain = DAG.getCALLSEQ_END(Chain,
2431 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002432 DAG.getIntPtrConstant(0, true), InFlag);
2433 InFlag = Chain.getValue(1);
2434
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002435 // Handle result values, copying them out of physregs into vregs that we
2436 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002437 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2438 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002439}
2440
Dan Gohman98ca4f22009-08-05 01:29:28 +00002441/// LowerCallResult - Lower the result values of a call into the
2442/// appropriate copies out of appropriate physical registers.
2443SDValue
2444MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002445 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002446 const SmallVectorImpl<ISD::InputArg> &Ins,
2447 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002448 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002449 // Assign locations to each value returned by this call.
2450 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002451 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2452 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002453
Dan Gohman98ca4f22009-08-05 01:29:28 +00002454 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002455
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002456 // Copy all of the result registers out of their specified physreg.
2457 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002458 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002459 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002460 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002461 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002462 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002463
Dan Gohman98ca4f22009-08-05 01:29:28 +00002464 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002465}
2466
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002467//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002468// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002469//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002470static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2471 std::vector<SDValue>& OutChains,
2472 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2473 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2474 unsigned LocMem = VA.getLocMemOffset();
2475 unsigned FirstWord = LocMem / 4;
2476
2477 // copy register A0 - A3 to frame object
2478 for (unsigned i = 0; i < NumWords; ++i) {
2479 unsigned CurWord = FirstWord + i;
2480 if (CurWord >= O32IntRegsSize)
2481 break;
2482
2483 unsigned SrcReg = O32IntRegs[CurWord];
2484 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2485 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2486 DAG.getConstant(i * 4, MVT::i32));
2487 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2488 StorePtr, MachinePointerInfo(), false,
2489 false, 0);
2490 OutChains.push_back(Store);
2491 }
2492}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002493
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002494// Create frame object on stack and copy registers used for byval passing to it.
2495static unsigned
2496CopyMips64ByValRegs(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2497 std::vector<SDValue>& OutChains, SelectionDAG &DAG,
2498 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2499 MachineFrameInfo *MFI, bool IsRegLoc,
2500 SmallVectorImpl<SDValue> &InVals, MipsFunctionInfo *MipsFI,
2501 EVT PtrTy) {
2502 const unsigned *Reg = Mips64IntRegs + 8;
2503 int FOOffset; // Frame object offset from virtual frame pointer.
2504
2505 if (IsRegLoc) {
2506 Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8, VA.getLocReg());
2507 FOOffset = (Reg - Mips64IntRegs) * 8 - 8 * 8;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002508 }
2509 else
2510 FOOffset = VA.getLocMemOffset();
2511
2512 // Create frame object.
2513 unsigned NumRegs = (Flags.getByValSize() + 7) / 8;
2514 unsigned LastFI = MFI->CreateFixedObject(NumRegs * 8, FOOffset, true);
2515 SDValue FIN = DAG.getFrameIndex(LastFI, PtrTy);
2516 InVals.push_back(FIN);
2517
2518 // Copy arg registers.
2519 for (unsigned I = 0; (Reg != Mips64IntRegs + 8) && (I < NumRegs);
2520 ++Reg, ++I) {
2521 unsigned VReg = AddLiveIn(MF, *Reg, Mips::CPU64RegsRegisterClass);
2522 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, PtrTy, FIN,
2523 DAG.getConstant(I * 8, PtrTy));
2524 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(VReg, MVT::i64),
2525 StorePtr, MachinePointerInfo(), false,
2526 false, 0);
2527 OutChains.push_back(Store);
2528 }
2529
2530 return LastFI;
2531}
2532
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002533/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002534/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002535SDValue
2536MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002537 CallingConv::ID CallConv,
2538 bool isVarArg,
Akira Hatanaka82099682011-12-19 19:52:25 +00002539 const SmallVectorImpl<ISD::InputArg> &Ins,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002540 DebugLoc dl, SelectionDAG &DAG,
2541 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002542 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002543 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002544 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002545 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002546
Dan Gohman1e93df62010-04-17 14:41:14 +00002547 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002548
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002549 // Used with vargs to acumulate store chains.
2550 std::vector<SDValue> OutChains;
2551
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002552 // Assign locations to all of the incoming arguments.
2553 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002554 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002555 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002556
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002557 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002558 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002559 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002560 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002561
Akira Hatanaka43299772011-05-20 23:22:14 +00002562 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002563
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002564 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002565 CCValAssign &VA = ArgLocs[i];
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002566 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002567 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2568 bool IsRegLoc = VA.isRegLoc();
2569
2570 if (Flags.isByVal()) {
2571 assert(Flags.getByValSize() &&
2572 "ByVal args of size 0 should have been ignored by front-end.");
2573 if (IsO32) {
2574 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2575 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2576 true);
2577 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2578 InVals.push_back(FIN);
2579 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2580 } else // N32/64
2581 LastFI = CopyMips64ByValRegs(MF, Chain, dl, OutChains, DAG, VA, Flags,
2582 MFI, IsRegLoc, InVals, MipsFI,
2583 getPointerTy());
2584 continue;
2585 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002586
2587 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002588 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002589 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002590 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002591 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002592
Owen Anderson825b72b2009-08-11 20:47:22 +00002593 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002594 RC = Mips::CPURegsRegisterClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002595 else if (RegVT == MVT::i64)
2596 RC = Mips::CPU64RegsRegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002597 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002598 RC = Mips::FGR32RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002599 else if (RegVT == MVT::f64)
Akira Hatanakaf40de9d2011-09-26 21:55:17 +00002600 RC = HasMips64 ? Mips::FGR64RegisterClass : Mips::AFGR64RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002601 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002602 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002603
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002604 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002605 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002606 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002607 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002608
2609 // If this is an 8 or 16-bit value, it has been passed promoted
2610 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002611 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002612 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002613 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002614 if (VA.getLocInfo() == CCValAssign::SExt)
2615 Opcode = ISD::AssertSext;
2616 else if (VA.getLocInfo() == CCValAssign::ZExt)
2617 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002618 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002619 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002620 DAG.getValueType(ValVT));
2621 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002622 }
2623
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002624 // Handle floating point arguments passed in integer registers.
2625 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
2626 (RegVT == MVT::i64 && ValVT == MVT::f64))
2627 ArgValue = DAG.getNode(ISD::BITCAST, dl, ValVT, ArgValue);
2628 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
2629 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
2630 getNextIntArgReg(ArgReg), RC);
2631 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
2632 if (!Subtarget->isLittle())
2633 std::swap(ArgValue, ArgValue2);
2634 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2635 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002636 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002637
Dan Gohman98ca4f22009-08-05 01:29:28 +00002638 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002639 } else { // VA.isRegLoc()
2640
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002641 // sanity check
2642 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002643
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002644 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002645 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002646 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002647
2648 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002649 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002650 InVals.push_back(DAG.getLoad(ValVT, dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002651 MachinePointerInfo::getFixedStack(LastFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002652 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002653 }
2654 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002655
2656 // The mips ABIs for returning structs by value requires that we copy
2657 // the sret argument into $v0 for the return. Save the argument into
2658 // a virtual register so that we can access it from the return points.
2659 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2660 unsigned Reg = MipsFI->getSRetReturnReg();
2661 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002662 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002663 MipsFI->setSRetReturnReg(Reg);
2664 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002665 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002666 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002667 }
2668
Akira Hatanakabad53f42011-11-14 19:01:09 +00002669 if (isVarArg) {
2670 unsigned NumOfRegs = IsO32 ? 4 : 8;
2671 const unsigned *ArgRegs = IsO32 ? O32IntRegs : Mips64IntRegs;
2672 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumOfRegs);
2673 int FirstRegSlotOffset = IsO32 ? 0 : -64 ; // offset of $a0's slot.
2674 TargetRegisterClass *RC
2675 = IsO32 ? Mips::CPURegsRegisterClass : Mips::CPU64RegsRegisterClass;
2676 unsigned RegSize = RC->getSize();
2677 int RegSlotOffset = FirstRegSlotOffset + Idx * RegSize;
2678
2679 // Offset of the first variable argument from stack pointer.
2680 int FirstVaArgOffset;
2681
2682 if (IsO32 || (Idx == NumOfRegs)) {
2683 FirstVaArgOffset =
2684 (CCInfo.getNextStackOffset() + RegSize - 1) / RegSize * RegSize;
2685 } else
2686 FirstVaArgOffset = RegSlotOffset;
2687
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002688 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002689 // which is a value necessary to VASTART.
Akira Hatanakabad53f42011-11-14 19:01:09 +00002690 LastFI = MFI->CreateFixedObject(RegSize, FirstVaArgOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002691 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002692
Akira Hatanakabad53f42011-11-14 19:01:09 +00002693 // Copy the integer registers that have not been used for argument passing
2694 // to the argument register save area. For O32, the save area is allocated
2695 // in the caller's stack frame, while for N32/64, it is allocated in the
2696 // callee's stack frame.
2697 for (int StackOffset = RegSlotOffset;
2698 Idx < NumOfRegs; ++Idx, StackOffset += RegSize) {
2699 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegs[Idx], RC);
2700 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2701 MVT::getIntegerVT(RegSize * 8));
2702 LastFI = MFI->CreateFixedObject(RegSize, StackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002703 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2704 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
Akira Hatanaka82099682011-12-19 19:52:25 +00002705 MachinePointerInfo(), false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002706 }
2707 }
2708
Akira Hatanaka43299772011-05-20 23:22:14 +00002709 MipsFI->setLastInArgFI(LastFI);
2710
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002711 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002712 // the size of Ins and InVals. This only happens when on varg functions
2713 if (!OutChains.empty()) {
2714 OutChains.push_back(Chain);
2715 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2716 &OutChains[0], OutChains.size());
2717 }
2718
Dan Gohman98ca4f22009-08-05 01:29:28 +00002719 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002720}
2721
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002722//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002723// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002724//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002725
Dan Gohman98ca4f22009-08-05 01:29:28 +00002726SDValue
2727MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002728 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002729 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002730 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002731 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002732
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002733 // CCValAssign - represent the assignment of
2734 // the return value to a location
2735 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002736
2737 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002738 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2739 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002740
Dan Gohman98ca4f22009-08-05 01:29:28 +00002741 // Analize return values.
2742 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002743
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002744 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002745 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002746 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002747 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002748 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002749 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002750 }
2751
Dan Gohman475871a2008-07-27 21:46:04 +00002752 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002753
2754 // Copy the result values into the output registers.
2755 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2756 CCValAssign &VA = RVLocs[i];
2757 assert(VA.isRegLoc() && "Can only return in registers!");
2758
Akira Hatanaka82099682011-12-19 19:52:25 +00002759 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002760
2761 // guarantee that all emitted copies are
2762 // stuck together, avoiding something bad
2763 Flag = Chain.getValue(1);
2764 }
2765
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002766 // The mips ABIs for returning structs by value requires that we copy
2767 // the sret argument into $v0 for the return. We saved the argument into
2768 // a virtual register in the entry block, so now we copy the value out
2769 // and into $v0.
2770 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2771 MachineFunction &MF = DAG.getMachineFunction();
2772 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2773 unsigned Reg = MipsFI->getSRetReturnReg();
2774
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002775 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002776 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002777 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002778
Dale Johannesena05dca42009-02-04 23:02:30 +00002779 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002780 Flag = Chain.getValue(1);
2781 }
2782
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002783 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002784 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002785 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002786 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002787 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002788 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002789 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002790}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002791
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002792//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002793// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002794//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002795
2796/// getConstraintType - Given a constraint letter, return the type of
2797/// constraint it is for this target.
2798MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002799getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002800{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002801 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002802 // GCC config/mips/constraints.md
2803 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002804 // 'd' : An address register. Equivalent to r
2805 // unless generating MIPS16 code.
2806 // 'y' : Equivalent to r; retained for
2807 // backwards compatibility.
2808 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002809 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002810 switch (Constraint[0]) {
2811 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002812 case 'd':
2813 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002814 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002815 return C_RegisterClass;
2816 break;
2817 }
2818 }
2819 return TargetLowering::getConstraintType(Constraint);
2820}
2821
John Thompson44ab89e2010-10-29 17:29:13 +00002822/// Examine constraint type and operand type and determine a weight value.
2823/// This object must already have been set up with the operand type
2824/// and the current alternative constraint selected.
2825TargetLowering::ConstraintWeight
2826MipsTargetLowering::getSingleConstraintMatchWeight(
2827 AsmOperandInfo &info, const char *constraint) const {
2828 ConstraintWeight weight = CW_Invalid;
2829 Value *CallOperandVal = info.CallOperandVal;
2830 // If we don't have a value, we can't do a match,
2831 // but allow it at the lowest weight.
2832 if (CallOperandVal == NULL)
2833 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002834 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002835 // Look at the constraint type.
2836 switch (*constraint) {
2837 default:
2838 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2839 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002840 case 'd':
2841 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002842 if (type->isIntegerTy())
2843 weight = CW_Register;
2844 break;
2845 case 'f':
2846 if (type->isFloatTy())
2847 weight = CW_Register;
2848 break;
2849 }
2850 return weight;
2851}
2852
Eric Christopher38d64262011-06-29 19:33:04 +00002853/// Given a register class constraint, like 'r', if this corresponds directly
2854/// to an LLVM register class, return a register of 0 and the register class
2855/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002856std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002857getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002858{
2859 if (Constraint.size() == 1) {
2860 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002861 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2862 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002863 case 'r':
2864 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002865 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002866 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002867 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002868 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002869 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2870 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002871 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002872 }
2873 }
2874 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2875}
2876
Dan Gohman6520e202008-10-18 02:06:02 +00002877bool
2878MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2879 // The Mips target isn't yet aware of offsets.
2880 return false;
2881}
Evan Chengeb2f9692009-10-27 19:56:55 +00002882
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002883bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2884 if (VT != MVT::f32 && VT != MVT::f64)
2885 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002886 if (Imm.isNegZero())
2887 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002888 return Imm.isZero();
2889}