blob: d19e0d5c088791a14bde7bd21e87fc8af7f0eb47 [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);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000139 setOperationAction(ISD::VASTART, MVT::Other, Custom);
140
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000141 setOperationAction(ISD::SDIV, MVT::i32, Expand);
142 setOperationAction(ISD::SREM, MVT::i32, Expand);
143 setOperationAction(ISD::UDIV, MVT::i32, Expand);
144 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000145 setOperationAction(ISD::SDIV, MVT::i64, Expand);
146 setOperationAction(ISD::SREM, MVT::i64, Expand);
147 setOperationAction(ISD::UDIV, MVT::i64, Expand);
148 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000149
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000150 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
152 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
153 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
154 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
155 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
156 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
157 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
158 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000159 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
160 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
161 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
162 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000164 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000165
Akira Hatanaka56633442011-09-20 23:53:09 +0000166 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000167 setOperationAction(ISD::ROTR, MVT::i32, Expand);
168
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000169 if (!Subtarget->hasMips64r2())
170 setOperationAction(ISD::ROTR, MVT::i64, Expand);
171
Owen Anderson825b72b2009-08-11 20:47:22 +0000172 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
173 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
174 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000175 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
176 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000178 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000179 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000180 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000181 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
182 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000183 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000184 setOperationAction(ISD::FLOG, MVT::f32, Expand);
185 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
186 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
187 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000188 setOperationAction(ISD::FMA, MVT::f32, Expand);
189 setOperationAction(ISD::FMA, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000190
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000191 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
192 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Eric Christopher471e4222011-06-08 23:55:35 +0000193
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000194 setOperationAction(ISD::VAARG, MVT::Other, Expand);
195 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
196 setOperationAction(ISD::VAEND, MVT::Other, Expand);
197
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000198 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000199 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
200 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000201
Akira Hatanakadb548262011-07-19 23:30:50 +0000202 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000203 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000204
Eli Friedman4db5aca2011-08-29 18:23:02 +0000205 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
206 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
207
Eli Friedman26689ac2011-08-03 21:06:02 +0000208 setInsertFencesForAtomic(true);
209
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000210 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000211 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000212
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000213 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000216 }
217
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000218 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000219 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000220
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000221 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000222 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000223
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000224 setTargetDAGCombine(ISD::ADDE);
225 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000226 setTargetDAGCombine(ISD::SDIVREM);
227 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000228 setTargetDAGCombine(ISD::SETCC);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000229 setTargetDAGCombine(ISD::AND);
230 setTargetDAGCombine(ISD::OR);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000231
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000232 setMinFunctionAlignment(2);
233
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000234 setStackPointerRegisterToSaveRestore(Mips::SP);
235 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000236
237 setExceptionPointerRegister(Mips::A0);
238 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000239}
240
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000241bool MipsTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Akira Hatanaka511961a2011-08-17 18:49:18 +0000242 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
Akira Hatanaka7bd19bd2011-10-11 00:27:28 +0000243 return SVT == MVT::i64 || SVT == MVT::i32 || SVT == MVT::i16;
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000244}
245
Duncan Sands28b77e92011-09-06 19:07:46 +0000246EVT MipsTargetLowering::getSetCCResultType(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000247 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000248}
249
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000250// SelectMadd -
251// Transforms a subgraph in CurDAG if the following pattern is found:
252// (addc multLo, Lo0), (adde multHi, Hi0),
253// where,
254// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000255// Lo0: initial value of Lo register
256// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000257// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000258static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000259 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000260 // for the matching to be successful.
261 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
262
263 if (ADDCNode->getOpcode() != ISD::ADDC)
264 return false;
265
266 SDValue MultHi = ADDENode->getOperand(0);
267 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000268 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000269 unsigned MultOpc = MultHi.getOpcode();
270
271 // MultHi and MultLo must be generated by the same node,
272 if (MultLo.getNode() != MultNode)
273 return false;
274
275 // and it must be a multiplication.
276 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
277 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000278
279 // MultLo amd MultHi must be the first and second output of MultNode
280 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000281 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
282 return false;
283
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000284 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000285 // of the values of MultNode, in which case MultNode will be removed in later
286 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000287 // If there exist users other than ADDENode or ADDCNode, this function returns
288 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000289 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000290 // produced.
291 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
292 return false;
293
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000294 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000295 DebugLoc dl = ADDENode->getDebugLoc();
296
297 // create MipsMAdd(u) node
298 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000299
Akira Hatanaka82099682011-12-19 19:52:25 +0000300 SDValue MAdd = CurDAG->getNode(MultOpc, dl, MVT::Glue,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000301 MultNode->getOperand(0),// Factor 0
302 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000303 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000304 ADDENode->getOperand(1));// Hi0
305
306 // create CopyFromReg nodes
307 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
308 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000309 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000310 Mips::HI, MVT::i32,
311 CopyFromLo.getValue(2));
312
313 // replace uses of adde and addc here
314 if (!SDValue(ADDCNode, 0).use_empty())
315 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
316
317 if (!SDValue(ADDENode, 0).use_empty())
318 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
319
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000320 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000321}
322
323// SelectMsub -
324// Transforms a subgraph in CurDAG if the following pattern is found:
325// (addc Lo0, multLo), (sube Hi0, multHi),
326// where,
327// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000328// Lo0: initial value of Lo register
329// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000330// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000331static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000332 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000333 // for the matching to be successful.
334 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
335
336 if (SUBCNode->getOpcode() != ISD::SUBC)
337 return false;
338
339 SDValue MultHi = SUBENode->getOperand(1);
340 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000341 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000342 unsigned MultOpc = MultHi.getOpcode();
343
344 // MultHi and MultLo must be generated by the same node,
345 if (MultLo.getNode() != MultNode)
346 return false;
347
348 // and it must be a multiplication.
349 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
350 return false;
351
352 // MultLo amd MultHi must be the first and second output of MultNode
353 // respectively.
354 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
355 return false;
356
357 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
358 // of the values of MultNode, in which case MultNode will be removed in later
359 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000360 // If there exist users other than SUBENode or SUBCNode, this function returns
361 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000362 // instruction node rather than a pair of MULT and MSUB instructions being
363 // produced.
364 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
365 return false;
366
367 SDValue Chain = CurDAG->getEntryNode();
368 DebugLoc dl = SUBENode->getDebugLoc();
369
370 // create MipsSub(u) node
371 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
372
Akira Hatanaka82099682011-12-19 19:52:25 +0000373 SDValue MSub = CurDAG->getNode(MultOpc, dl, MVT::Glue,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000374 MultNode->getOperand(0),// Factor 0
375 MultNode->getOperand(1),// Factor 1
376 SUBCNode->getOperand(0),// Lo0
377 SUBENode->getOperand(0));// Hi0
378
379 // create CopyFromReg nodes
380 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
381 MSub);
382 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
383 Mips::HI, MVT::i32,
384 CopyFromLo.getValue(2));
385
386 // replace uses of sube and subc here
387 if (!SDValue(SUBCNode, 0).use_empty())
388 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
389
390 if (!SDValue(SUBENode, 0).use_empty())
391 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
392
393 return true;
394}
395
396static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
397 TargetLowering::DAGCombinerInfo &DCI,
398 const MipsSubtarget* Subtarget) {
399 if (DCI.isBeforeLegalize())
400 return SDValue();
401
Akira Hatanakae184fec2011-11-11 04:18:21 +0000402 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
403 SelectMadd(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000404 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000405
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000406 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000407}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000408
409static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
410 TargetLowering::DAGCombinerInfo &DCI,
411 const MipsSubtarget* Subtarget) {
412 if (DCI.isBeforeLegalize())
413 return SDValue();
414
Akira Hatanakae184fec2011-11-11 04:18:21 +0000415 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
416 SelectMsub(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000417 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000418
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000419 return SDValue();
420}
421
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000422static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
423 TargetLowering::DAGCombinerInfo &DCI,
424 const MipsSubtarget* Subtarget) {
425 if (DCI.isBeforeLegalizeOps())
426 return SDValue();
427
Akira Hatanakadda4a072011-10-03 21:06:13 +0000428 EVT Ty = N->getValueType(0);
429 unsigned LO = (Ty == MVT::i32) ? Mips::LO : Mips::LO64;
430 unsigned HI = (Ty == MVT::i32) ? Mips::HI : Mips::HI64;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000431 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
432 MipsISD::DivRemU;
433 DebugLoc dl = N->getDebugLoc();
434
435 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
436 N->getOperand(0), N->getOperand(1));
437 SDValue InChain = DAG.getEntryNode();
438 SDValue InGlue = DivRem;
439
440 // insert MFLO
441 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakadda4a072011-10-03 21:06:13 +0000442 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000443 InGlue);
444 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
445 InChain = CopyFromLo.getValue(1);
446 InGlue = CopyFromLo.getValue(2);
447 }
448
449 // insert MFHI
450 if (N->hasAnyUseOfValue(1)) {
451 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000452 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000453 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
454 }
455
456 return SDValue();
457}
458
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000459static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
460 switch (CC) {
461 default: llvm_unreachable("Unknown fp condition code!");
462 case ISD::SETEQ:
463 case ISD::SETOEQ: return Mips::FCOND_OEQ;
464 case ISD::SETUNE: return Mips::FCOND_UNE;
465 case ISD::SETLT:
466 case ISD::SETOLT: return Mips::FCOND_OLT;
467 case ISD::SETGT:
468 case ISD::SETOGT: return Mips::FCOND_OGT;
469 case ISD::SETLE:
470 case ISD::SETOLE: return Mips::FCOND_OLE;
471 case ISD::SETGE:
472 case ISD::SETOGE: return Mips::FCOND_OGE;
473 case ISD::SETULT: return Mips::FCOND_ULT;
474 case ISD::SETULE: return Mips::FCOND_ULE;
475 case ISD::SETUGT: return Mips::FCOND_UGT;
476 case ISD::SETUGE: return Mips::FCOND_UGE;
477 case ISD::SETUO: return Mips::FCOND_UN;
478 case ISD::SETO: return Mips::FCOND_OR;
479 case ISD::SETNE:
480 case ISD::SETONE: return Mips::FCOND_ONE;
481 case ISD::SETUEQ: return Mips::FCOND_UEQ;
482 }
483}
484
485
486// Returns true if condition code has to be inverted.
487static bool InvertFPCondCode(Mips::CondCode CC) {
488 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
489 return false;
490
Akira Hatanaka82099682011-12-19 19:52:25 +0000491 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
492 "Illegal Condition Code");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000493
Akira Hatanaka82099682011-12-19 19:52:25 +0000494 return true;
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000495}
496
497// Creates and returns an FPCmp node from a setcc node.
498// Returns Op if setcc is not a floating point comparison.
499static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
500 // must be a SETCC node
501 if (Op.getOpcode() != ISD::SETCC)
502 return Op;
503
504 SDValue LHS = Op.getOperand(0);
505
506 if (!LHS.getValueType().isFloatingPoint())
507 return Op;
508
509 SDValue RHS = Op.getOperand(1);
510 DebugLoc dl = Op.getDebugLoc();
511
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000512 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
513 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000514 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
515
516 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
517 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
518}
519
520// Creates and returns a CMovFPT/F node.
521static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
522 SDValue False, DebugLoc DL) {
523 bool invert = InvertFPCondCode((Mips::CondCode)
524 cast<ConstantSDNode>(Cond.getOperand(2))
525 ->getSExtValue());
526
527 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
528 True.getValueType(), True, False, Cond);
529}
530
531static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
532 TargetLowering::DAGCombinerInfo &DCI,
533 const MipsSubtarget* Subtarget) {
534 if (DCI.isBeforeLegalizeOps())
535 return SDValue();
536
537 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
538
539 if (Cond.getOpcode() != MipsISD::FPCmp)
540 return SDValue();
541
542 SDValue True = DAG.getConstant(1, MVT::i32);
543 SDValue False = DAG.getConstant(0, MVT::i32);
544
545 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
546}
547
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000548static SDValue PerformANDCombine(SDNode *N, SelectionDAG& DAG,
549 TargetLowering::DAGCombinerInfo &DCI,
550 const MipsSubtarget* Subtarget) {
551 // Pattern match EXT.
552 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
553 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000554 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000555 return SDValue();
556
557 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000558 unsigned ShiftRightOpc = ShiftRight.getOpcode();
559
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000560 // Op's first operand must be a shift right.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000561 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000562 return SDValue();
563
564 // The second operand of the shift must be an immediate.
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000565 ConstantSDNode *CN;
566 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
567 return SDValue();
568
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000569 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000570 uint64_t SMPos, SMSize;
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000571
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000572 // Op's second operand must be a shifted mask.
573 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000574 !IsShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000575 return SDValue();
576
577 // Return if the shifted mask does not start at bit 0 or the sum of its size
578 // and Pos exceeds the word's size.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000579 EVT ValTy = N->getValueType(0);
580 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000581 return SDValue();
582
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000583 return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), ValTy,
Akira Hatanaka82099682011-12-19 19:52:25 +0000584 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000585 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000586}
587
588static SDValue PerformORCombine(SDNode *N, SelectionDAG& DAG,
589 TargetLowering::DAGCombinerInfo &DCI,
590 const MipsSubtarget* Subtarget) {
591 // Pattern match INS.
592 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
593 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
594 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000595 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000596 return SDValue();
597
598 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
599 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
600 ConstantSDNode *CN;
601
602 // See if Op's first operand matches (and $src1 , mask0).
603 if (And0.getOpcode() != ISD::AND)
604 return SDValue();
605
606 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000607 !IsShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000608 return SDValue();
609
610 // See if Op's second operand matches (and (shl $src, pos), mask1).
611 if (And1.getOpcode() != ISD::AND)
612 return SDValue();
613
614 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000615 !IsShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000616 return SDValue();
617
618 // The shift masks must have the same position and size.
619 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
620 return SDValue();
621
622 SDValue Shl = And1.getOperand(0);
623 if (Shl.getOpcode() != ISD::SHL)
624 return SDValue();
625
626 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
627 return SDValue();
628
629 unsigned Shamt = CN->getZExtValue();
630
631 // Return if the shift amount and the first bit position of mask are not the
632 // same.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000633 EVT ValTy = N->getValueType(0);
634 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000635 return SDValue();
636
Akira Hatanaka82099682011-12-19 19:52:25 +0000637 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), ValTy, Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000638 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka82099682011-12-19 19:52:25 +0000639 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000640}
641
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000642SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000643 const {
644 SelectionDAG &DAG = DCI.DAG;
645 unsigned opc = N->getOpcode();
646
647 switch (opc) {
648 default: break;
649 case ISD::ADDE:
650 return PerformADDECombine(N, DAG, DCI, Subtarget);
651 case ISD::SUBE:
652 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000653 case ISD::SDIVREM:
654 case ISD::UDIVREM:
655 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000656 case ISD::SETCC:
657 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000658 case ISD::AND:
659 return PerformANDCombine(N, DAG, DCI, Subtarget);
660 case ISD::OR:
661 return PerformORCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000662 }
663
664 return SDValue();
665}
666
Dan Gohman475871a2008-07-27 21:46:04 +0000667SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000668LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000669{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000670 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000671 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000672 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000673 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
674 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000675 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000676 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000677 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
678 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000679 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000680 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000681 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000682 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Akira Hatanakadb548262011-07-19 23:30:50 +0000683 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +0000684 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000685 }
Dan Gohman475871a2008-07-27 21:46:04 +0000686 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000687}
688
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000689//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000690// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000691//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000692
693// AddLiveIn - This helper function adds the specified physical register to the
694// MachineFunction as a live in value. It also creates a corresponding
695// virtual register for it.
696static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000697AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000698{
699 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000700 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
701 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000702 return VReg;
703}
704
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000705// Get fp branch code (not opcode) from condition code.
706static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
707 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
708 return Mips::BRANCH_T;
709
Akira Hatanaka82099682011-12-19 19:52:25 +0000710 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
711 "Invalid CondCode.");
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000712
Akira Hatanaka82099682011-12-19 19:52:25 +0000713 return Mips::BRANCH_F;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000714}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000715
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000716/*
Akira Hatanaka14487d42011-06-07 19:28:39 +0000717static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
718 DebugLoc dl,
719 const MipsSubtarget* Subtarget,
720 const TargetInstrInfo *TII,
721 bool isFPCmp, unsigned Opc) {
722 // There is no need to expand CMov instructions if target has
723 // conditional moves.
724 if (Subtarget->hasCondMov())
725 return BB;
726
727 // To "insert" a SELECT_CC instruction, we actually have to insert the
728 // diamond control-flow pattern. The incoming instruction knows the
729 // destination vreg to set, the condition code register to branch on, the
730 // true/false values to select between, and a branch opcode to use.
731 const BasicBlock *LLVM_BB = BB->getBasicBlock();
732 MachineFunction::iterator It = BB;
733 ++It;
734
735 // thisMBB:
736 // ...
737 // TrueVal = ...
738 // setcc r1, r2, r3
739 // bNE r1, r0, copy1MBB
740 // fallthrough --> copy0MBB
741 MachineBasicBlock *thisMBB = BB;
742 MachineFunction *F = BB->getParent();
743 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
744 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
745 F->insert(It, copy0MBB);
746 F->insert(It, sinkMBB);
747
748 // Transfer the remainder of BB and its successor edges to sinkMBB.
749 sinkMBB->splice(sinkMBB->begin(), BB,
750 llvm::next(MachineBasicBlock::iterator(MI)),
751 BB->end());
752 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
753
754 // Next, add the true and fallthrough blocks as its successors.
755 BB->addSuccessor(copy0MBB);
756 BB->addSuccessor(sinkMBB);
757
758 // Emit the right instruction according to the type of the operands compared
759 if (isFPCmp)
760 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
761 else
762 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
763 .addReg(Mips::ZERO).addMBB(sinkMBB);
764
765 // copy0MBB:
766 // %FalseValue = ...
767 // # fallthrough to sinkMBB
768 BB = copy0MBB;
769
770 // Update machine-CFG edges
771 BB->addSuccessor(sinkMBB);
772
773 // sinkMBB:
774 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
775 // ...
776 BB = sinkMBB;
777
778 if (isFPCmp)
779 BuildMI(*BB, BB->begin(), dl,
780 TII->get(Mips::PHI), MI->getOperand(0).getReg())
781 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
782 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
783 else
784 BuildMI(*BB, BB->begin(), dl,
785 TII->get(Mips::PHI), MI->getOperand(0).getReg())
786 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
787 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
788
789 MI->eraseFromParent(); // The pseudo instruction is gone now.
790 return BB;
791}
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000792*/
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000793MachineBasicBlock *
794MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000795 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000796 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000797 default:
798 assert(false && "Unexpected instr type to insert");
799 return NULL;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000800 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000801 case Mips::ATOMIC_LOAD_ADD_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000802 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
803 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000804 case Mips::ATOMIC_LOAD_ADD_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000805 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
806 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000807 case Mips::ATOMIC_LOAD_ADD_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000808 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000809 case Mips::ATOMIC_LOAD_ADD_I64:
810 case Mips::ATOMIC_LOAD_ADD_I64_P8:
811 return EmitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000812
813 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000814 case Mips::ATOMIC_LOAD_AND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000815 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
816 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000817 case Mips::ATOMIC_LOAD_AND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000818 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
819 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000820 case Mips::ATOMIC_LOAD_AND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000821 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000822 case Mips::ATOMIC_LOAD_AND_I64:
823 case Mips::ATOMIC_LOAD_AND_I64_P8:
Akira Hatanaka73866122011-11-12 02:38:12 +0000824 return EmitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000825
826 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000827 case Mips::ATOMIC_LOAD_OR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000828 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
829 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000830 case Mips::ATOMIC_LOAD_OR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000831 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
832 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000833 case Mips::ATOMIC_LOAD_OR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000834 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000835 case Mips::ATOMIC_LOAD_OR_I64:
836 case Mips::ATOMIC_LOAD_OR_I64_P8:
837 return EmitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000838
839 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000840 case Mips::ATOMIC_LOAD_XOR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000841 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
842 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000843 case Mips::ATOMIC_LOAD_XOR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000844 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
845 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000846 case Mips::ATOMIC_LOAD_XOR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000847 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000848 case Mips::ATOMIC_LOAD_XOR_I64:
849 case Mips::ATOMIC_LOAD_XOR_I64_P8:
850 return EmitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000851
852 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000853 case Mips::ATOMIC_LOAD_NAND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000854 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
855 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000856 case Mips::ATOMIC_LOAD_NAND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000857 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
858 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000859 case Mips::ATOMIC_LOAD_NAND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000860 return EmitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000861 case Mips::ATOMIC_LOAD_NAND_I64:
862 case Mips::ATOMIC_LOAD_NAND_I64_P8:
863 return EmitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000864
865 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000866 case Mips::ATOMIC_LOAD_SUB_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000867 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
868 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000869 case Mips::ATOMIC_LOAD_SUB_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000870 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
871 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000872 case Mips::ATOMIC_LOAD_SUB_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000873 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000874 case Mips::ATOMIC_LOAD_SUB_I64:
875 case Mips::ATOMIC_LOAD_SUB_I64_P8:
876 return EmitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000877
878 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000879 case Mips::ATOMIC_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000880 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
881 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000882 case Mips::ATOMIC_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000883 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
884 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000885 case Mips::ATOMIC_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000886 return EmitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000887 case Mips::ATOMIC_SWAP_I64:
888 case Mips::ATOMIC_SWAP_I64_P8:
889 return EmitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000890
891 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000892 case Mips::ATOMIC_CMP_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000893 return EmitAtomicCmpSwapPartword(MI, BB, 1);
894 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000895 case Mips::ATOMIC_CMP_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000896 return EmitAtomicCmpSwapPartword(MI, BB, 2);
897 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000898 case Mips::ATOMIC_CMP_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000899 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000900 case Mips::ATOMIC_CMP_SWAP_I64:
901 case Mips::ATOMIC_CMP_SWAP_I64_P8:
902 return EmitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000903 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000904}
905
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000906// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
907// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
908MachineBasicBlock *
909MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000910 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000911 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000912 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000913
914 MachineFunction *MF = BB->getParent();
915 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000916 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000917 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
918 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000919 unsigned LL, SC, AND, NOR, ZERO, BEQ;
920
921 if (Size == 4) {
922 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
923 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
924 AND = Mips::AND;
925 NOR = Mips::NOR;
926 ZERO = Mips::ZERO;
927 BEQ = Mips::BEQ;
928 }
929 else {
930 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
931 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
932 AND = Mips::AND64;
933 NOR = Mips::NOR64;
934 ZERO = Mips::ZERO_64;
935 BEQ = Mips::BEQ64;
936 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000937
Akira Hatanaka4061da12011-07-19 20:11:17 +0000938 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000939 unsigned Ptr = MI->getOperand(1).getReg();
940 unsigned Incr = MI->getOperand(2).getReg();
941
Akira Hatanaka4061da12011-07-19 20:11:17 +0000942 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
943 unsigned AndRes = RegInfo.createVirtualRegister(RC);
944 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000945
946 // insert new blocks after the current block
947 const BasicBlock *LLVM_BB = BB->getBasicBlock();
948 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
949 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
950 MachineFunction::iterator It = BB;
951 ++It;
952 MF->insert(It, loopMBB);
953 MF->insert(It, exitMBB);
954
955 // Transfer the remainder of BB and its successor edges to exitMBB.
956 exitMBB->splice(exitMBB->begin(), BB,
957 llvm::next(MachineBasicBlock::iterator(MI)),
958 BB->end());
959 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
960
961 // thisMBB:
962 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000963 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000964 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000965 loopMBB->addSuccessor(loopMBB);
966 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000967
968 // loopMBB:
969 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000970 // <binop> storeval, oldval, incr
971 // sc success, storeval, 0(ptr)
972 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000973 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +0000974 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000975 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000976 // and andres, oldval, incr
977 // nor storeval, $0, andres
Akira Hatanaka59068062011-11-11 04:14:30 +0000978 BuildMI(BB, dl, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
979 BuildMI(BB, dl, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000980 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000981 // <binop> storeval, oldval, incr
982 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000983 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000984 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000985 }
Akira Hatanaka59068062011-11-11 04:14:30 +0000986 BuildMI(BB, dl, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
987 BuildMI(BB, dl, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000988
989 MI->eraseFromParent(); // The instruction is gone now.
990
Akira Hatanaka939ece12011-07-19 03:42:13 +0000991 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000992}
993
994MachineBasicBlock *
995MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000996 MachineBasicBlock *BB,
997 unsigned Size, unsigned BinOpcode,
998 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000999 assert((Size == 1 || Size == 2) &&
1000 "Unsupported size for EmitAtomicBinaryPartial.");
1001
1002 MachineFunction *MF = BB->getParent();
1003 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1004 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1005 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1006 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001007 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1008 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001009
1010 unsigned Dest = MI->getOperand(0).getReg();
1011 unsigned Ptr = MI->getOperand(1).getReg();
1012 unsigned Incr = MI->getOperand(2).getReg();
1013
Akira Hatanaka4061da12011-07-19 20:11:17 +00001014 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1015 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001016 unsigned Mask = RegInfo.createVirtualRegister(RC);
1017 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001018 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1019 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001020 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001021 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1022 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1023 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1024 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1025 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001026 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001027 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1028 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1029 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1030 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1031 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001032
1033 // insert new blocks after the current block
1034 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1035 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001036 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001037 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1038 MachineFunction::iterator It = BB;
1039 ++It;
1040 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001041 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001042 MF->insert(It, exitMBB);
1043
1044 // Transfer the remainder of BB and its successor edges to exitMBB.
1045 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001046 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001047 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1048
Akira Hatanaka81b44112011-07-19 17:09:53 +00001049 BB->addSuccessor(loopMBB);
1050 loopMBB->addSuccessor(loopMBB);
1051 loopMBB->addSuccessor(sinkMBB);
1052 sinkMBB->addSuccessor(exitMBB);
1053
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001054 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001055 // addiu masklsb2,$0,-4 # 0xfffffffc
1056 // and alignedaddr,ptr,masklsb2
1057 // andi ptrlsb2,ptr,3
1058 // sll shiftamt,ptrlsb2,3
1059 // ori maskupper,$0,255 # 0xff
1060 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001061 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001062 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001063
1064 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001065 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1066 .addReg(Mips::ZERO).addImm(-4);
1067 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1068 .addReg(Ptr).addReg(MaskLSB2);
1069 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1070 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1071 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1072 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001073 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1074 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001075 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001076 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001077
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001078 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001079 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001080 // ll oldval,0(alignedaddr)
1081 // binop binopres,oldval,incr2
1082 // and newval,binopres,mask
1083 // and maskedoldval0,oldval,mask2
1084 // or storeval,maskedoldval0,newval
1085 // sc success,storeval,0(alignedaddr)
1086 // beq success,$0,loopMBB
1087
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001088 // atomic.swap
1089 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001090 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001091 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001092 // and maskedoldval0,oldval,mask2
1093 // or storeval,maskedoldval0,newval
1094 // sc success,storeval,0(alignedaddr)
1095 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001096
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001097 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001098 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001099 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001100 // and andres, oldval, incr2
1101 // nor binopres, $0, andres
1102 // and newval, binopres, mask
1103 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1104 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1105 .addReg(Mips::ZERO).addReg(AndRes);
1106 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001107 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001108 // <binop> binopres, oldval, incr2
1109 // and newval, binopres, mask
1110 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1111 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001112 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001113 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001114 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001115 }
1116
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001117 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001118 .addReg(OldVal).addReg(Mask2);
1119 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001120 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001121 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001122 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001123 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001124 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001125
Akira Hatanaka939ece12011-07-19 03:42:13 +00001126 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001127 // and maskedoldval1,oldval,mask
1128 // srl srlres,maskedoldval1,shiftamt
1129 // sll sllres,srlres,24
1130 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001131 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001132 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001133
Akira Hatanaka4061da12011-07-19 20:11:17 +00001134 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1135 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001136 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1137 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001138 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1139 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001140 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001141 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001142
1143 MI->eraseFromParent(); // The instruction is gone now.
1144
Akira Hatanaka939ece12011-07-19 03:42:13 +00001145 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001146}
1147
1148MachineBasicBlock *
1149MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001150 MachineBasicBlock *BB,
1151 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001152 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001153
1154 MachineFunction *MF = BB->getParent();
1155 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001156 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001157 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1158 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001159 unsigned LL, SC, ZERO, BNE, BEQ;
1160
1161 if (Size == 4) {
1162 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1163 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1164 ZERO = Mips::ZERO;
1165 BNE = Mips::BNE;
1166 BEQ = Mips::BEQ;
1167 }
1168 else {
1169 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1170 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1171 ZERO = Mips::ZERO_64;
1172 BNE = Mips::BNE64;
1173 BEQ = Mips::BEQ64;
1174 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001175
1176 unsigned Dest = MI->getOperand(0).getReg();
1177 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001178 unsigned OldVal = MI->getOperand(2).getReg();
1179 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001180
Akira Hatanaka4061da12011-07-19 20:11:17 +00001181 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001182
1183 // insert new blocks after the current block
1184 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1185 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1186 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1187 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1188 MachineFunction::iterator It = BB;
1189 ++It;
1190 MF->insert(It, loop1MBB);
1191 MF->insert(It, loop2MBB);
1192 MF->insert(It, exitMBB);
1193
1194 // Transfer the remainder of BB and its successor edges to exitMBB.
1195 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001196 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001197 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1198
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001199 // thisMBB:
1200 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001201 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001202 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001203 loop1MBB->addSuccessor(exitMBB);
1204 loop1MBB->addSuccessor(loop2MBB);
1205 loop2MBB->addSuccessor(loop1MBB);
1206 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001207
1208 // loop1MBB:
1209 // ll dest, 0(ptr)
1210 // bne dest, oldval, exitMBB
1211 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001212 BuildMI(BB, dl, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1213 BuildMI(BB, dl, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001214 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001215
1216 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001217 // sc success, newval, 0(ptr)
1218 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001219 BB = loop2MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001220 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001221 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka59068062011-11-11 04:14:30 +00001222 BuildMI(BB, dl, TII->get(BEQ))
1223 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001224
1225 MI->eraseFromParent(); // The instruction is gone now.
1226
Akira Hatanaka939ece12011-07-19 03:42:13 +00001227 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001228}
1229
1230MachineBasicBlock *
1231MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001232 MachineBasicBlock *BB,
1233 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001234 assert((Size == 1 || Size == 2) &&
1235 "Unsupported size for EmitAtomicCmpSwapPartial.");
1236
1237 MachineFunction *MF = BB->getParent();
1238 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1239 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1240 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1241 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001242 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1243 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001244
1245 unsigned Dest = MI->getOperand(0).getReg();
1246 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001247 unsigned CmpVal = MI->getOperand(2).getReg();
1248 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001249
Akira Hatanaka4061da12011-07-19 20:11:17 +00001250 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1251 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001252 unsigned Mask = RegInfo.createVirtualRegister(RC);
1253 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001254 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1255 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1256 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1257 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1258 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1259 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1260 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1261 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1262 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1263 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1264 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1265 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1266 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1267 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001268
1269 // insert new blocks after the current block
1270 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1271 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1272 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001273 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001274 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1275 MachineFunction::iterator It = BB;
1276 ++It;
1277 MF->insert(It, loop1MBB);
1278 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001279 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001280 MF->insert(It, exitMBB);
1281
1282 // Transfer the remainder of BB and its successor edges to exitMBB.
1283 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001284 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001285 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1286
Akira Hatanaka81b44112011-07-19 17:09:53 +00001287 BB->addSuccessor(loop1MBB);
1288 loop1MBB->addSuccessor(sinkMBB);
1289 loop1MBB->addSuccessor(loop2MBB);
1290 loop2MBB->addSuccessor(loop1MBB);
1291 loop2MBB->addSuccessor(sinkMBB);
1292 sinkMBB->addSuccessor(exitMBB);
1293
Akira Hatanaka70564a92011-07-19 18:14:26 +00001294 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001295 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001296 // addiu masklsb2,$0,-4 # 0xfffffffc
1297 // and alignedaddr,ptr,masklsb2
1298 // andi ptrlsb2,ptr,3
1299 // sll shiftamt,ptrlsb2,3
1300 // ori maskupper,$0,255 # 0xff
1301 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001302 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001303 // andi maskedcmpval,cmpval,255
1304 // sll shiftedcmpval,maskedcmpval,shiftamt
1305 // andi maskednewval,newval,255
1306 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001307 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001308 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1309 .addReg(Mips::ZERO).addImm(-4);
1310 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1311 .addReg(Ptr).addReg(MaskLSB2);
1312 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1313 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1314 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1315 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001316 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1317 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001318 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001319 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1320 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001321 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1322 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001323 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1324 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001325 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1326 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001327
1328 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001329 // ll oldval,0(alginedaddr)
1330 // and maskedoldval0,oldval,mask
1331 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001332 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001333 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001334 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1335 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001336 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001337 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001338
1339 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001340 // and maskedoldval1,oldval,mask2
1341 // or storeval,maskedoldval1,shiftednewval
1342 // sc success,storeval,0(alignedaddr)
1343 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001344 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001345 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1346 .addReg(OldVal).addReg(Mask2);
1347 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1348 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001349 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001350 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001351 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001352 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001353
Akira Hatanaka939ece12011-07-19 03:42:13 +00001354 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001355 // srl srlres,maskedoldval0,shiftamt
1356 // sll sllres,srlres,24
1357 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001358 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001359 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001360
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001361 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1362 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001363 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1364 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001365 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001366 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001367
1368 MI->eraseFromParent(); // The instruction is gone now.
1369
Akira Hatanaka939ece12011-07-19 03:42:13 +00001370 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001371}
1372
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001373//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001374// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001375//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001376SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001377LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001378{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001379 MachineFunction &MF = DAG.getMachineFunction();
1380 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001381 unsigned SP = IsN64 ? Mips::SP_64 : Mips::SP;
Akira Hatanaka21afc632011-06-21 00:40:49 +00001382
1383 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001384 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1385 "Cannot lower if the alignment of the allocated space is larger than \
1386 that of the stack.");
1387
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001388 SDValue Chain = Op.getOperand(0);
1389 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001390 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001391
1392 // Get a reference from Mips stack pointer
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001393 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, SP, getPointerTy());
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001394
1395 // Subtract the dynamic size from the actual stack size to
1396 // obtain the new stack size.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001397 SDValue Sub = DAG.getNode(ISD::SUB, dl, getPointerTy(), StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001398
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001399 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001400 // must be placed in the stack pointer register.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001401 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, SP, Sub, SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001402
1403 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001404 // value and a chain
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001405 SDVTList VTLs = DAG.getVTList(getPointerTy(), MVT::Other);
Akira Hatanaka21afc632011-06-21 00:40:49 +00001406 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1407 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1408
1409 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001410}
1411
1412SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001413LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001414{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001415 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001416 // the block to branch to if the condition is true.
1417 SDValue Chain = Op.getOperand(0);
1418 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001419 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001420
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001421 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1422
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001423 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001424 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001425 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001426
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001427 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001428 Mips::CondCode CC =
1429 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001430 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001431
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001432 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001433 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001434}
1435
1436SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001437LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001438{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001439 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001440
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001441 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001442 if (Cond.getOpcode() != MipsISD::FPCmp)
1443 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001444
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001445 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1446 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001447}
1448
Dan Gohmand858e902010-04-17 15:26:15 +00001449SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1450 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001451 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001452 DebugLoc dl = Op.getDebugLoc();
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001453 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001454
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001455 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001456 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001457
Chris Lattnerb71b9092009-08-13 06:28:06 +00001458 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001459
Chris Lattnere3736f82009-08-13 05:41:27 +00001460 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001461 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1462 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001463 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001464 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1465 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001466 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001467 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001468 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001469 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1470 MipsII::MO_ABS_HI);
1471 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1472 MipsII::MO_ABS_LO);
1473 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1474 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001475 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001476 }
1477
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001478 EVT ValTy = Op.getValueType();
1479 bool HasGotOfst = (GV->hasInternalLinkage() ||
1480 (GV->hasLocalLinkage() && !isa<Function>(GV)));
1481 unsigned GotFlag = IsN64 ?
1482 (HasGotOfst ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT_DISP) :
Bruno Cardoso Lopese3d35722011-12-07 00:28:57 +00001483 (HasGotOfst ? MipsII::MO_GOT : MipsII::MO_GOT16);
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001484 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0, GotFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001485 GA = DAG.getNode(MipsISD::Wrapper, dl, ValTy, GA);
Akira Hatanaka82099682011-12-19 19:52:25 +00001486 SDValue ResNode = DAG.getLoad(ValTy, dl, DAG.getEntryNode(), GA,
1487 MachinePointerInfo(), false, false, false, 0);
Akira Hatanaka0f843822011-06-07 18:58:42 +00001488 // On functions and global targets not internal linked only
1489 // a load from got/GP is necessary for PIC to work.
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001490 if (!HasGotOfst)
Akira Hatanaka0f843822011-06-07 18:58:42 +00001491 return ResNode;
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001492 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0,
1493 IsN64 ? MipsII::MO_GOT_OFST :
1494 MipsII::MO_ABS_LO);
1495 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, GALo);
1496 return DAG.getNode(ISD::ADD, dl, ValTy, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001497}
1498
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001499SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1500 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001501 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1502 // FIXME there isn't actually debug info here
1503 DebugLoc dl = Op.getDebugLoc();
1504
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001505 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001506 // %hi/%lo relocation
Akira Hatanaka82099682011-12-19 19:52:25 +00001507 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true, MipsII::MO_ABS_HI);
1508 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true, MipsII::MO_ABS_LO);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001509 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1510 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1511 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001512 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001513
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001514 EVT ValTy = Op.getValueType();
1515 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1516 unsigned OFSTFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1517 SDValue BAGOTOffset = DAG.getBlockAddress(BA, ValTy, true, GOTFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001518 BAGOTOffset = DAG.getNode(MipsISD::Wrapper, dl, ValTy, BAGOTOffset);
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001519 SDValue BALOOffset = DAG.getBlockAddress(BA, ValTy, true, OFSTFlag);
Akira Hatanaka82099682011-12-19 19:52:25 +00001520 SDValue Load = DAG.getLoad(ValTy, dl, DAG.getEntryNode(), BAGOTOffset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001521 MachinePointerInfo(), false, false, false, 0);
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001522 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, BALOOffset);
1523 return DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001524}
1525
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001526SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001527LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001528{
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001529 // If the relocation model is PIC, use the General Dynamic TLS Model or
1530 // Local Dynamic TLS model, otherwise use the Initial Exec or
1531 // Local Exec TLS Model.
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001532
1533 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1534 DebugLoc dl = GA->getDebugLoc();
1535 const GlobalValue *GV = GA->getGlobal();
1536 EVT PtrVT = getPointerTy();
1537
1538 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1539 // General Dynamic TLS Model
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001540 bool LocalDynamic = GV->hasInternalLinkage();
1541 unsigned Flag = LocalDynamic ? MipsII::MO_TLSLDM :MipsII::MO_TLSGD;
1542 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, Flag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001543 SDValue Argument = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, TGA);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001544 unsigned PtrSize = PtrVT.getSizeInBits();
1545 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1546
Benjamin Kramer5eccf672011-12-11 12:21:34 +00001547 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001548
1549 ArgListTy Args;
1550 ArgListEntry Entry;
1551 Entry.Node = Argument;
Akira Hatanakaca074792011-12-08 20:34:32 +00001552 Entry.Ty = PtrTy;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001553 Args.push_back(Entry);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001554
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001555 std::pair<SDValue, SDValue> CallResult =
Akira Hatanakaca074792011-12-08 20:34:32 +00001556 LowerCallTo(DAG.getEntryNode(), PtrTy,
1557 false, false, false, false, 0, CallingConv::C, false, true,
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001558 TlsGetAddr, Args, DAG, dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001559
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001560 SDValue Ret = CallResult.first;
1561
1562 if (!LocalDynamic)
1563 return Ret;
1564
1565 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1566 MipsII::MO_DTPREL_HI);
1567 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, PtrVT, TGAHi);
1568 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1569 MipsII::MO_DTPREL_LO);
1570 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, TGALo);
1571 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Ret);
1572 return DAG.getNode(ISD::ADD, dl, PtrVT, Add, Lo);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001573 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001574
1575 SDValue Offset;
1576 if (GV->isDeclaration()) {
1577 // Initial Exec TLS Model
Akira Hatanakaca074792011-12-08 20:34:32 +00001578 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001579 MipsII::MO_GOTTPREL);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001580 TGA = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, TGA);
Akira Hatanakaca074792011-12-08 20:34:32 +00001581 Offset = DAG.getLoad(PtrVT, dl,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001582 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001583 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001584 } else {
1585 // Local Exec TLS Model
Akira Hatanakaca074792011-12-08 20:34:32 +00001586 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001587 MipsII::MO_TPREL_HI);
Akira Hatanakaca074792011-12-08 20:34:32 +00001588 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001589 MipsII::MO_TPREL_LO);
Akira Hatanakaca074792011-12-08 20:34:32 +00001590 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, PtrVT, TGAHi);
1591 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, TGALo);
1592 Offset = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001593 }
1594
1595 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1596 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001597}
1598
1599SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001600LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001601{
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001602 SDValue HiPart, JTI, JTILo;
Dale Johannesende064702009-02-06 21:50:26 +00001603 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001604 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001605 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Owen Andersone50ed302009-08-10 22:56:29 +00001606 EVT PtrVT = Op.getValueType();
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001607 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001608
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001609 if (!IsPIC && !IsN64) {
1610 JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MipsII::MO_ABS_HI);
1611 HiPart = DAG.getNode(MipsISD::Hi, dl, PtrVT, JTI);
1612 JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MipsII::MO_ABS_LO);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001613 } else {// Emit Load from Global Pointer
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001614 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1615 unsigned OfstFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1616 JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, GOTFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001617 JTI = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, JTI);
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001618 HiPart = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), JTI,
1619 MachinePointerInfo(), false, false, false, 0);
1620 JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OfstFlag);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001621 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001622
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001623 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, JTILo);
1624 return DAG.getNode(ISD::ADD, dl, PtrVT, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001625}
1626
Dan Gohman475871a2008-07-27 21:46:04 +00001627SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001628LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001629{
Dan Gohman475871a2008-07-27 21:46:04 +00001630 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001631 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001632 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001633 // FIXME there isn't actually debug info here
1634 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001635
1636 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001637 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001638 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001639 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001640 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001641 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001642 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1643 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001644 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001645
1646 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001647 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001648 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001649 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001650 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001651 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1652 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001653 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001654 } else {
Akira Hatanaka620db892011-11-16 22:44:38 +00001655 EVT ValTy = Op.getValueType();
1656 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1657 unsigned OFSTFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1658 SDValue CP = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1659 N->getOffset(), GOTFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001660 CP = DAG.getNode(MipsISD::Wrapper, dl, ValTy, CP);
Akira Hatanaka82099682011-12-19 19:52:25 +00001661 SDValue Load = DAG.getLoad(ValTy, dl, DAG.getEntryNode(), CP,
1662 MachinePointerInfo::getConstantPool(), false,
1663 false, false, 0);
Akira Hatanaka620db892011-11-16 22:44:38 +00001664 SDValue CPLo = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1665 N->getOffset(), OFSTFlag);
1666 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, CPLo);
1667 ResNode = DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001668 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001669
1670 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001671}
1672
Dan Gohmand858e902010-04-17 15:26:15 +00001673SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001674 MachineFunction &MF = DAG.getMachineFunction();
1675 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1676
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001677 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001678 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1679 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001680
1681 // vastart just stores the address of the VarArgsFrameIndex slot into the
1682 // memory location argument.
1683 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001684 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
Akira Hatanaka82099682011-12-19 19:52:25 +00001685 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001686}
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001687
1688// Called if the size of integer registers is large enough to hold the whole
1689// floating point number.
1690static SDValue LowerFCOPYSIGNLargeIntReg(SDValue Op, SelectionDAG &DAG) {
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001691 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001692 EVT ValTy = Op.getValueType();
1693 EVT IntValTy = MVT::getIntegerVT(ValTy.getSizeInBits());
1694 uint64_t Mask = (uint64_t)1 << (ValTy.getSizeInBits() - 1);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001695 DebugLoc dl = Op.getDebugLoc();
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001696 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, IntValTy, Op.getOperand(0));
1697 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, IntValTy, Op.getOperand(1));
1698 SDValue And0 = DAG.getNode(ISD::AND, dl, IntValTy, Op0,
1699 DAG.getConstant(Mask - 1, IntValTy));
1700 SDValue And1 = DAG.getNode(ISD::AND, dl, IntValTy, Op1,
1701 DAG.getConstant(Mask, IntValTy));
1702 SDValue Result = DAG.getNode(ISD::OR, dl, IntValTy, And0, And1);
1703 return DAG.getNode(ISD::BITCAST, dl, ValTy, Result);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001704}
1705
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001706// Called if the size of integer registers is not large enough to hold the whole
1707// floating point number (e.g. f64 & 32-bit integer register).
1708static SDValue
1709LowerFCOPYSIGNSmallIntReg(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001710 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001711 // Use ext/ins instructions if target architecture is Mips32r2.
1712 // Eliminate redundant mfc1 and mtc1 instructions.
1713 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001714
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001715 if (!isLittle)
1716 std::swap(LoIdx, HiIdx);
1717
1718 DebugLoc dl = Op.getDebugLoc();
1719 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1720 Op.getOperand(0),
1721 DAG.getConstant(LoIdx, MVT::i32));
1722 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1723 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1724 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1725 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1726 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1727 DAG.getConstant(0x7fffffff, MVT::i32));
1728 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1729 DAG.getConstant(0x80000000, MVT::i32));
1730 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1731
1732 if (!isLittle)
1733 std::swap(Word0, Word1);
1734
1735 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1736}
1737
Akira Hatanaka82099682011-12-19 19:52:25 +00001738SDValue
1739MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001740 EVT Ty = Op.getValueType();
1741
1742 assert(Ty == MVT::f32 || Ty == MVT::f64);
1743
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001744 if (Ty == MVT::f32 || HasMips64)
1745 return LowerFCOPYSIGNLargeIntReg(Op, DAG);
Akira Hatanaka82099682011-12-19 19:52:25 +00001746
1747 return LowerFCOPYSIGNSmallIntReg(Op, DAG, Subtarget->isLittle());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001748}
1749
Akira Hatanaka2e591472011-06-02 00:24:44 +00001750SDValue MipsTargetLowering::
1751LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001752 // check the depth
1753 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001754 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001755
1756 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1757 MFI->setFrameAddressIsTaken(true);
1758 EVT VT = Op.getValueType();
1759 DebugLoc dl = Op.getDebugLoc();
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001760 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
1761 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001762 return FrameAddr;
1763}
1764
Akira Hatanakadb548262011-07-19 23:30:50 +00001765// TODO: set SType according to the desired memory barrier behavior.
Akira Hatanaka82099682011-12-19 19:52:25 +00001766SDValue
1767MipsTargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG& DAG) const {
Akira Hatanakadb548262011-07-19 23:30:50 +00001768 unsigned SType = 0;
1769 DebugLoc dl = Op.getDebugLoc();
1770 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1771 DAG.getConstant(SType, MVT::i32));
1772}
1773
Eli Friedman14648462011-07-27 22:21:52 +00001774SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1775 SelectionDAG& DAG) const {
1776 // FIXME: Need pseudo-fence for 'singlethread' fences
1777 // FIXME: Set SType for weaker fences where supported/appropriate.
1778 unsigned SType = 0;
1779 DebugLoc dl = Op.getDebugLoc();
1780 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1781 DAG.getConstant(SType, MVT::i32));
1782}
1783
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001784//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001785// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001786//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001787
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001788//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001789// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001790// Mips O32 ABI rules:
1791// ---
1792// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001793// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001794// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001795// f64 - Only passed in two aliased f32 registers if no int reg has been used
1796// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001797// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1798// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001799//
1800// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001801//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001802
Duncan Sands1e96bab2010-11-04 10:49:57 +00001803static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001804 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001805 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1806
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001807 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001808
1809 static const unsigned IntRegs[] = {
1810 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1811 };
1812 static const unsigned F32Regs[] = {
1813 Mips::F12, Mips::F14
1814 };
1815 static const unsigned F64Regs[] = {
1816 Mips::D6, Mips::D7
1817 };
1818
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001819 // ByVal Args
1820 if (ArgFlags.isByVal()) {
1821 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1822 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1823 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1824 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1825 r < std::min(IntRegsSize, NextReg); ++r)
1826 State.AllocateReg(IntRegs[r]);
1827 return false;
1828 }
1829
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001830 // Promote i8 and i16
1831 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1832 LocVT = MVT::i32;
1833 if (ArgFlags.isSExt())
1834 LocInfo = CCValAssign::SExt;
1835 else if (ArgFlags.isZExt())
1836 LocInfo = CCValAssign::ZExt;
1837 else
1838 LocInfo = CCValAssign::AExt;
1839 }
1840
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001841 unsigned Reg;
1842
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001843 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1844 // is true: function is vararg, argument is 3rd or higher, there is previous
1845 // argument which is not f32 or f64.
1846 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1847 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001848 unsigned OrigAlign = ArgFlags.getOrigAlign();
1849 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001850
1851 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001852 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001853 // If this is the first part of an i64 arg,
1854 // the allocated register must be either A0 or A2.
1855 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1856 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001857 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001858 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1859 // Allocate int register and shadow next int register. If first
1860 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001861 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1862 if (Reg == Mips::A1 || Reg == Mips::A3)
1863 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1864 State.AllocateReg(IntRegs, IntRegsSize);
1865 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001866 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1867 // we are guaranteed to find an available float register
1868 if (ValVT == MVT::f32) {
1869 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1870 // Shadow int register
1871 State.AllocateReg(IntRegs, IntRegsSize);
1872 } else {
1873 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1874 // Shadow int registers
1875 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1876 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1877 State.AllocateReg(IntRegs, IntRegsSize);
1878 State.AllocateReg(IntRegs, IntRegsSize);
1879 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001880 } else
1881 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001882
Akira Hatanakad37776d2011-05-20 21:39:54 +00001883 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1884 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1885
1886 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001887 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001888 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001889 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001890
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001891 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001892}
1893
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00001894static const unsigned Mips64IntRegs[8] =
1895 {Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
1896 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64};
1897static const unsigned Mips64DPRegs[8] =
1898 {Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
1899 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64};
1900
1901static bool CC_Mips64Byval(unsigned ValNo, MVT ValVT, MVT LocVT,
1902 CCValAssign::LocInfo LocInfo,
1903 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1904 unsigned Align = std::max(ArgFlags.getByValAlign(), (unsigned)8);
1905 unsigned Size = (ArgFlags.getByValSize() + 7) / 8 * 8;
1906 unsigned FirstIdx = State.getFirstUnallocated(Mips64IntRegs, 8);
1907
1908 assert(Align <= 16 && "Cannot handle alignments larger than 16.");
1909
1910 // If byval is 16-byte aligned, the first arg register must be even.
1911 if ((Align == 16) && (FirstIdx % 2)) {
1912 State.AllocateReg(Mips64IntRegs[FirstIdx], Mips64DPRegs[FirstIdx]);
1913 ++FirstIdx;
1914 }
1915
1916 // Mark the registers allocated.
1917 for (unsigned I = FirstIdx; Size && (I < 8); Size -= 8, ++I)
1918 State.AllocateReg(Mips64IntRegs[I], Mips64DPRegs[I]);
1919
1920 // Allocate space on caller's stack.
1921 unsigned Offset = State.AllocateStack(Size, Align);
1922
1923 if (FirstIdx < 8)
1924 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Mips64IntRegs[FirstIdx],
1925 LocVT, LocInfo));
1926 else
1927 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
1928
1929 return true;
1930}
1931
1932#include "MipsGenCallingConv.inc"
1933
Akira Hatanaka49617092011-11-14 19:02:54 +00001934static void
1935AnalyzeMips64CallOperands(CCState CCInfo,
1936 const SmallVectorImpl<ISD::OutputArg> &Outs) {
1937 unsigned NumOps = Outs.size();
1938 for (unsigned i = 0; i != NumOps; ++i) {
1939 MVT ArgVT = Outs[i].VT;
1940 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
1941 bool R;
1942
1943 if (Outs[i].IsFixed)
1944 R = CC_MipsN(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
1945 else
1946 R = CC_MipsN_VarArg(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
1947
Akira Hatanaka49617092011-11-14 19:02:54 +00001948 if (R) {
Benjamin Kramer6296ee32011-11-14 19:51:48 +00001949#ifndef NDEBUG
Akira Hatanaka49617092011-11-14 19:02:54 +00001950 dbgs() << "Call operand #" << i << " has unhandled type "
1951 << EVT(ArgVT).getEVTString();
1952#endif
1953 llvm_unreachable(0);
1954 }
1955 }
1956}
1957
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001958//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001959// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001960//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001961
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001962static const unsigned O32IntRegsSize = 4;
1963
1964static const unsigned O32IntRegs[] = {
1965 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1966};
1967
Akira Hatanaka373e3a42011-09-23 00:58:33 +00001968// Return next O32 integer argument register.
1969static unsigned getNextIntArgReg(unsigned Reg) {
1970 assert((Reg == Mips::A0) || (Reg == Mips::A2));
1971 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
1972}
1973
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001974// Write ByVal Arg to arg registers and stack.
1975static void
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001976WriteByValArg(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001977 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1978 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1979 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001980 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001981 MVT PtrType, bool isLittle) {
1982 unsigned LocMemOffset = VA.getLocMemOffset();
1983 unsigned Offset = 0;
1984 uint32_t RemainingSize = Flags.getByValSize();
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001985 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001986
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001987 // Copy the first 4 words of byval arg to registers A0 - A3.
1988 // FIXME: Use a stricter alignment if it enables better optimization in passes
1989 // run later.
1990 for (; RemainingSize >= 4 && LocMemOffset < 4 * 4;
1991 Offset += 4, RemainingSize -= 4, LocMemOffset += 4) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001992 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001993 DAG.getConstant(Offset, MVT::i32));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001994 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
Akira Hatanaka82099682011-12-19 19:52:25 +00001995 MachinePointerInfo(), false, false, false,
1996 std::min(ByValAlign, (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001997 MemOpChains.push_back(LoadVal.getValue(1));
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001998 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001999 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
2000 }
2001
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002002 if (RemainingSize == 0)
2003 return;
2004
2005 // If there still is a register available for argument passing, write the
2006 // remaining part of the structure to it using subword loads and shifts.
2007 if (LocMemOffset < 4 * 4) {
2008 assert(RemainingSize <= 3 && RemainingSize >= 1 &&
2009 "There must be one to three bytes remaining.");
2010 unsigned LoadSize = (RemainingSize == 3 ? 2 : RemainingSize);
2011 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2012 DAG.getConstant(Offset, MVT::i32));
2013 unsigned Alignment = std::min(ByValAlign, (unsigned )4);
2014 SDValue LoadVal = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2015 LoadPtr, MachinePointerInfo(),
2016 MVT::getIntegerVT(LoadSize * 8), false,
2017 false, Alignment);
2018 MemOpChains.push_back(LoadVal.getValue(1));
2019
2020 // If target is big endian, shift it to the most significant half-word or
2021 // byte.
2022 if (!isLittle)
2023 LoadVal = DAG.getNode(ISD::SHL, dl, MVT::i32, LoadVal,
2024 DAG.getConstant(32 - LoadSize * 8, MVT::i32));
2025
2026 Offset += LoadSize;
2027 RemainingSize -= LoadSize;
2028
2029 // Read second subword if necessary.
2030 if (RemainingSize != 0) {
2031 assert(RemainingSize == 1 && "There must be one byte remaining.");
2032 LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2033 DAG.getConstant(Offset, MVT::i32));
2034 unsigned Alignment = std::min(ByValAlign, (unsigned )2);
2035 SDValue Subword = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2036 LoadPtr, MachinePointerInfo(),
2037 MVT::i8, false, false, Alignment);
2038 MemOpChains.push_back(Subword.getValue(1));
2039 // Insert the loaded byte to LoadVal.
2040 // FIXME: Use INS if supported by target.
2041 unsigned ShiftAmt = isLittle ? 16 : 8;
2042 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i32, Subword,
2043 DAG.getConstant(ShiftAmt, MVT::i32));
2044 LoadVal = DAG.getNode(ISD::OR, dl, MVT::i32, LoadVal, Shift);
2045 }
2046
2047 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
2048 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
2049 return;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002050 }
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002051
2052 // Create a fixed object on stack at offset LocMemOffset and copy
2053 // remaining part of byval arg to it using memcpy.
2054 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2055 DAG.getConstant(Offset, MVT::i32));
2056 LastFI = MFI->CreateFixedObject(RemainingSize, LocMemOffset, true);
2057 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002058 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2059 DAG.getConstant(RemainingSize, MVT::i32),
2060 std::min(ByValAlign, (unsigned)4),
2061 /*isVolatile=*/false, /*AlwaysInline=*/false,
2062 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002063}
2064
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002065// Copy Mips64 byVal arg to registers and stack.
2066void static
2067PassByValArg64(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
2068 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
2069 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
2070 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
2071 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2072 EVT PtrTy, bool isLittle) {
2073 unsigned ByValSize = Flags.getByValSize();
2074 unsigned Alignment = std::min(Flags.getByValAlign(), (unsigned)8);
2075 bool IsRegLoc = VA.isRegLoc();
2076 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
2077 unsigned LocMemOffset = 0;
Akira Hatanaka16040852011-11-15 18:42:25 +00002078 unsigned MemCpySize = ByValSize;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002079
2080 if (!IsRegLoc)
2081 LocMemOffset = VA.getLocMemOffset();
2082 else {
2083 const unsigned *Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8,
2084 VA.getLocReg());
2085 const unsigned *RegEnd = Mips64IntRegs + 8;
2086
2087 // Copy double words to registers.
2088 for (; (Reg != RegEnd) && (ByValSize >= Offset + 8); ++Reg, Offset += 8) {
2089 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2090 DAG.getConstant(Offset, PtrTy));
2091 SDValue LoadVal = DAG.getLoad(MVT::i64, dl, Chain, LoadPtr,
2092 MachinePointerInfo(), false, false, false,
2093 Alignment);
2094 MemOpChains.push_back(LoadVal.getValue(1));
2095 RegsToPass.push_back(std::make_pair(*Reg, LoadVal));
2096 }
2097
Akira Hatanaka16040852011-11-15 18:42:25 +00002098 // Return if the struct has been fully copied.
2099 if (!(MemCpySize = ByValSize - Offset))
2100 return;
2101
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002102 // If there is an argument register available, copy the remainder of the
2103 // byval argument with sub-doubleword loads and shifts.
Akira Hatanaka16040852011-11-15 18:42:25 +00002104 if (Reg != RegEnd) {
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002105 assert((ByValSize < Offset + 8) &&
2106 "Size of the remainder should be smaller than 8-byte.");
2107 SDValue Val;
2108 for (unsigned LoadSize = 4; Offset < ByValSize; LoadSize /= 2) {
2109 unsigned RemSize = ByValSize - Offset;
2110
2111 if (RemSize < LoadSize)
2112 continue;
2113
2114 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2115 DAG.getConstant(Offset, PtrTy));
2116 SDValue LoadVal =
2117 DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i64, Chain, LoadPtr,
2118 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
2119 false, false, Alignment);
2120 MemOpChains.push_back(LoadVal.getValue(1));
2121
2122 // Offset in number of bits from double word boundary.
2123 unsigned OffsetDW = (Offset % 8) * 8;
2124 unsigned Shamt = isLittle ? OffsetDW : 64 - (OffsetDW + LoadSize * 8);
2125 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i64, LoadVal,
2126 DAG.getConstant(Shamt, MVT::i32));
2127
2128 Val = Val.getNode() ? DAG.getNode(ISD::OR, dl, MVT::i64, Val, Shift) :
2129 Shift;
2130 Offset += LoadSize;
2131 Alignment = std::min(Alignment, LoadSize);
2132 }
2133
2134 RegsToPass.push_back(std::make_pair(*Reg, Val));
2135 return;
2136 }
2137 }
2138
Akira Hatanaka16040852011-11-15 18:42:25 +00002139 assert(MemCpySize && "MemCpySize must not be zero.");
2140
2141 // Create a fixed object on stack at offset LocMemOffset and copy
2142 // remainder of byval arg to it with memcpy.
2143 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2144 DAG.getConstant(Offset, PtrTy));
2145 LastFI = MFI->CreateFixedObject(MemCpySize, LocMemOffset, true);
2146 SDValue Dst = DAG.getFrameIndex(LastFI, PtrTy);
2147 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2148 DAG.getConstant(MemCpySize, PtrTy), Alignment,
2149 /*isVolatile=*/false, /*AlwaysInline=*/false,
2150 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002151}
2152
Dan Gohman98ca4f22009-08-05 01:29:28 +00002153/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002154/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002155/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002156SDValue
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002157MipsTargetLowering::LowerCall(SDValue InChain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002158 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002159 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002160 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002161 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002162 const SmallVectorImpl<ISD::InputArg> &Ins,
2163 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002164 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002165 // MIPs target does not yet support tail call optimization.
2166 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002167
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002168 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002169 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002170 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002171 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00002172 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002173
2174 // Analyze operands of the call, assigning locations to each operand.
2175 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002176 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002177 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002178
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002179 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002180 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanaka49617092011-11-14 19:02:54 +00002181 else if (HasMips64)
2182 AnalyzeMips64CallOperands(CCInfo, Outs);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00002183 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002184 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002185
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002186 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002187 unsigned NextStackOffset = CCInfo.getNextStackOffset();
2188
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002189 // Chain is the output chain of the last Load/Store or CopyToReg node.
2190 // ByValChain is the output chain of the last Memcpy node created for copying
2191 // byval arguments to the stack.
2192 SDValue Chain, CallSeqStart, ByValChain;
2193 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
2194 Chain = CallSeqStart = DAG.getCALLSEQ_START(InChain, NextStackOffsetVal);
2195 ByValChain = InChain;
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002196
2197 // If this is the first call, create a stack frame object that points to
2198 // a location to which .cprestore saves $gp.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002199 if (IsO32 && IsPIC && !MipsFI->getGPFI())
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002200 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
2201
Akira Hatanaka21afc632011-06-21 00:40:49 +00002202 // Get the frame index of the stack frame object that points to the location
2203 // of dynamically allocated area on the stack.
2204 int DynAllocFI = MipsFI->getDynAllocFI();
2205
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002206 // Update size of the maximum argument space.
2207 // For O32, a minimum of four words (16 bytes) of argument space is
2208 // allocated.
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002209 if (IsO32)
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002210 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
2211
2212 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
2213
2214 if (MaxCallFrameSize < NextStackOffset) {
2215 MipsFI->setMaxCallFrameSize(NextStackOffset);
2216
Akira Hatanaka21afc632011-06-21 00:40:49 +00002217 // Set the offsets relative to $sp of the $gp restore slot and dynamically
2218 // allocated stack space. These offsets must be aligned to a boundary
2219 // determined by the stack alignment of the ABI.
2220 unsigned StackAlignment = TFL->getStackAlignment();
2221 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
2222 StackAlignment * StackAlignment;
2223
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002224 if (MipsFI->needGPSaveRestore())
Akira Hatanaka21afc632011-06-21 00:40:49 +00002225 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
2226
2227 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002228 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002229
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002230 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002231 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
2232 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002233
Eric Christopher471e4222011-06-08 23:55:35 +00002234 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00002235
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002236 // Walk the register/memloc assignments, inserting copies/loads.
2237 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002238 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002239 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002240 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002241 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2242
2243 // ByVal Arg.
2244 if (Flags.isByVal()) {
2245 assert(Flags.getByValSize() &&
2246 "ByVal args of size 0 should have been ignored by front-end.");
2247 if (IsO32)
2248 WriteByValArg(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2249 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2250 Subtarget->isLittle());
2251 else
2252 PassByValArg64(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2253 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2254 Subtarget->isLittle());
2255 continue;
2256 }
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002257
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002258 // Promote the value if needed.
2259 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002260 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002261 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002262 if (VA.isRegLoc()) {
2263 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
2264 (ValVT == MVT::f64 && LocVT == MVT::i64))
2265 Arg = DAG.getNode(ISD::BITCAST, dl, LocVT, Arg);
2266 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002267 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2268 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002269 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2270 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002271 if (!Subtarget->isLittle())
2272 std::swap(Lo, Hi);
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002273 unsigned LocRegLo = VA.getLocReg();
2274 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2275 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2276 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002277 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002278 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002279 }
2280 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002281 case CCValAssign::SExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002282 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002283 break;
2284 case CCValAssign::ZExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002285 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002286 break;
2287 case CCValAssign::AExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002288 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002289 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002290 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002291
2292 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002293 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002294 if (VA.isRegLoc()) {
2295 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002296 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002297 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002298
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002299 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002300 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002301
Chris Lattnere0b12152008-03-17 06:57:02 +00002302 // Create the frame index object for this incoming parameter
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002303 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002304 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00002305 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00002306
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002307 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002308 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00002309 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Akira Hatanaka82099682011-12-19 19:52:25 +00002310 MachinePointerInfo(), false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002311 }
2312
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002313 // Extend range of indices of frame objects for outgoing arguments that were
2314 // created during this function call. Skip this step if no such objects were
2315 // created.
2316 if (LastFI)
2317 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2318
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002319 // If a memcpy has been created to copy a byval arg to a stack, replace the
2320 // chain input of CallSeqStart with ByValChain.
2321 if (InChain != ByValChain)
2322 DAG.UpdateNodeOperands(CallSeqStart.getNode(), ByValChain,
2323 NextStackOffsetVal);
2324
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002325 // Transform all store nodes into one single node because all store
2326 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002327 if (!MemOpChains.empty())
2328 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002329 &MemOpChains[0], MemOpChains.size());
2330
Bill Wendling056292f2008-09-16 21:48:12 +00002331 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002332 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2333 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002334 unsigned char OpFlag;
2335 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002336 bool GlobalOrExternal = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002337 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002338
2339 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002340 if (IsPICCall && G->getGlobal()->hasInternalLinkage()) {
2341 OpFlag = IsO32 ? MipsII::MO_GOT : MipsII::MO_GOT_PAGE;
2342 unsigned char LoFlag = IsO32 ? MipsII::MO_ABS_LO : MipsII::MO_GOT_OFST;
2343 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(), 0,
2344 OpFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002345 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002346 0, LoFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002347 } else {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002348 OpFlag = IsPICCall ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002349 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2350 getPointerTy(), 0, OpFlag);
2351 }
2352
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002353 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002354 }
2355 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002356 if (IsN64 || (!IsO32 && IsPIC))
2357 OpFlag = MipsII::MO_GOT_DISP;
2358 else if (!IsPIC) // !N64 && static
2359 OpFlag = MipsII::MO_NO_FLAG;
2360 else // O32 & PIC
2361 OpFlag = MipsII::MO_GOT_CALL;
Akira Hatanaka82099682011-12-19 19:52:25 +00002362 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2363 OpFlag);
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002364 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002365 }
2366
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002367 SDValue InFlag;
2368
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002369 // Create nodes that load address of callee and copy it to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002370 if (IsPICCall) {
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002371 if (GlobalOrExternal) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002372 // Load callee address
Akira Hatanaka6df7e232011-12-09 01:53:17 +00002373 Callee = DAG.getNode(MipsISD::Wrapper, dl, getPointerTy(), Callee);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002374 SDValue LoadValue = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
2375 Callee, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002376 false, false, false, 0);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002377
2378 // Use GOT+LO if callee has internal linkage.
2379 if (CalleeLo.getNode()) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002380 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, getPointerTy(), CalleeLo);
2381 Callee = DAG.getNode(ISD::ADD, dl, getPointerTy(), LoadValue, Lo);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002382 } else
2383 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002384 }
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002385 }
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002386
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002387 // T9 should contain the address of the callee function if
2388 // -reloction-model=pic or it is an indirect call.
2389 if (IsPICCall || !GlobalOrExternal) {
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002390 // copy to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002391 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
2392 Chain = DAG.getCopyToReg(Chain, dl, T9Reg, Callee, SDValue(0, 0));
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002393 InFlag = Chain.getValue(1);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002394 Callee = DAG.getRegister(T9Reg, getPointerTy());
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002395 }
Bill Wendling056292f2008-09-16 21:48:12 +00002396
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002397 // Build a sequence of copy-to-reg nodes chained together with token
2398 // chain and flag operands which copy the outgoing args into registers.
2399 // The InFlag in necessary since all emitted instructions must be
2400 // stuck together.
2401 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2402 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2403 RegsToPass[i].second, InFlag);
2404 InFlag = Chain.getValue(1);
2405 }
2406
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002407 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002408 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002409 //
2410 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002411 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002412 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002413 Ops.push_back(Chain);
2414 Ops.push_back(Callee);
2415
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002416 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002417 // known live into the call.
2418 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2419 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2420 RegsToPass[i].second.getValueType()));
2421
Gabor Greifba36cb52008-08-28 21:40:38 +00002422 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002423 Ops.push_back(InFlag);
2424
Dale Johannesen33c960f2009-02-04 20:06:27 +00002425 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002426 InFlag = Chain.getValue(1);
2427
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002428 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002429 Chain = DAG.getCALLSEQ_END(Chain,
2430 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002431 DAG.getIntPtrConstant(0, true), InFlag);
2432 InFlag = Chain.getValue(1);
2433
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002434 // Handle result values, copying them out of physregs into vregs that we
2435 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002436 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2437 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002438}
2439
Dan Gohman98ca4f22009-08-05 01:29:28 +00002440/// LowerCallResult - Lower the result values of a call into the
2441/// appropriate copies out of appropriate physical registers.
2442SDValue
2443MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002444 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002445 const SmallVectorImpl<ISD::InputArg> &Ins,
2446 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002447 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002448 // Assign locations to each value returned by this call.
2449 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002450 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2451 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002452
Dan Gohman98ca4f22009-08-05 01:29:28 +00002453 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002454
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002455 // Copy all of the result registers out of their specified physreg.
2456 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002457 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002458 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002459 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002460 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002461 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002462
Dan Gohman98ca4f22009-08-05 01:29:28 +00002463 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002464}
2465
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002466//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002467// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002468//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002469static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2470 std::vector<SDValue>& OutChains,
2471 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2472 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2473 unsigned LocMem = VA.getLocMemOffset();
2474 unsigned FirstWord = LocMem / 4;
2475
2476 // copy register A0 - A3 to frame object
2477 for (unsigned i = 0; i < NumWords; ++i) {
2478 unsigned CurWord = FirstWord + i;
2479 if (CurWord >= O32IntRegsSize)
2480 break;
2481
2482 unsigned SrcReg = O32IntRegs[CurWord];
2483 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2484 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2485 DAG.getConstant(i * 4, MVT::i32));
2486 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2487 StorePtr, MachinePointerInfo(), false,
2488 false, 0);
2489 OutChains.push_back(Store);
2490 }
2491}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002492
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002493// Create frame object on stack and copy registers used for byval passing to it.
2494static unsigned
2495CopyMips64ByValRegs(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2496 std::vector<SDValue>& OutChains, SelectionDAG &DAG,
2497 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2498 MachineFrameInfo *MFI, bool IsRegLoc,
2499 SmallVectorImpl<SDValue> &InVals, MipsFunctionInfo *MipsFI,
2500 EVT PtrTy) {
2501 const unsigned *Reg = Mips64IntRegs + 8;
2502 int FOOffset; // Frame object offset from virtual frame pointer.
2503
2504 if (IsRegLoc) {
2505 Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8, VA.getLocReg());
2506 FOOffset = (Reg - Mips64IntRegs) * 8 - 8 * 8;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002507 }
2508 else
2509 FOOffset = VA.getLocMemOffset();
2510
2511 // Create frame object.
2512 unsigned NumRegs = (Flags.getByValSize() + 7) / 8;
2513 unsigned LastFI = MFI->CreateFixedObject(NumRegs * 8, FOOffset, true);
2514 SDValue FIN = DAG.getFrameIndex(LastFI, PtrTy);
2515 InVals.push_back(FIN);
2516
2517 // Copy arg registers.
2518 for (unsigned I = 0; (Reg != Mips64IntRegs + 8) && (I < NumRegs);
2519 ++Reg, ++I) {
2520 unsigned VReg = AddLiveIn(MF, *Reg, Mips::CPU64RegsRegisterClass);
2521 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, PtrTy, FIN,
2522 DAG.getConstant(I * 8, PtrTy));
2523 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(VReg, MVT::i64),
2524 StorePtr, MachinePointerInfo(), false,
2525 false, 0);
2526 OutChains.push_back(Store);
2527 }
2528
2529 return LastFI;
2530}
2531
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002532/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002533/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002534SDValue
2535MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002536 CallingConv::ID CallConv,
2537 bool isVarArg,
Akira Hatanaka82099682011-12-19 19:52:25 +00002538 const SmallVectorImpl<ISD::InputArg> &Ins,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002539 DebugLoc dl, SelectionDAG &DAG,
2540 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002541 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002542 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002543 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002544 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002545
Dan Gohman1e93df62010-04-17 14:41:14 +00002546 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002547
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002548 // Used with vargs to acumulate store chains.
2549 std::vector<SDValue> OutChains;
2550
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002551 // Assign locations to all of the incoming arguments.
2552 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002553 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002554 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002555
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002556 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002557 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002558 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002559 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002560
Akira Hatanaka43299772011-05-20 23:22:14 +00002561 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002562
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002563 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002564 CCValAssign &VA = ArgLocs[i];
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002565 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002566 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2567 bool IsRegLoc = VA.isRegLoc();
2568
2569 if (Flags.isByVal()) {
2570 assert(Flags.getByValSize() &&
2571 "ByVal args of size 0 should have been ignored by front-end.");
2572 if (IsO32) {
2573 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2574 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2575 true);
2576 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2577 InVals.push_back(FIN);
2578 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2579 } else // N32/64
2580 LastFI = CopyMips64ByValRegs(MF, Chain, dl, OutChains, DAG, VA, Flags,
2581 MFI, IsRegLoc, InVals, MipsFI,
2582 getPointerTy());
2583 continue;
2584 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002585
2586 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002587 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002588 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002589 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002590 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002591
Owen Anderson825b72b2009-08-11 20:47:22 +00002592 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002593 RC = Mips::CPURegsRegisterClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002594 else if (RegVT == MVT::i64)
2595 RC = Mips::CPU64RegsRegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002596 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002597 RC = Mips::FGR32RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002598 else if (RegVT == MVT::f64)
Akira Hatanakaf40de9d2011-09-26 21:55:17 +00002599 RC = HasMips64 ? Mips::FGR64RegisterClass : Mips::AFGR64RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002600 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002601 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002602
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002603 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002604 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002605 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002606 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002607
2608 // If this is an 8 or 16-bit value, it has been passed promoted
2609 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002610 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002611 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002612 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002613 if (VA.getLocInfo() == CCValAssign::SExt)
2614 Opcode = ISD::AssertSext;
2615 else if (VA.getLocInfo() == CCValAssign::ZExt)
2616 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002617 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002618 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002619 DAG.getValueType(ValVT));
2620 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002621 }
2622
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002623 // Handle floating point arguments passed in integer registers.
2624 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
2625 (RegVT == MVT::i64 && ValVT == MVT::f64))
2626 ArgValue = DAG.getNode(ISD::BITCAST, dl, ValVT, ArgValue);
2627 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
2628 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
2629 getNextIntArgReg(ArgReg), RC);
2630 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
2631 if (!Subtarget->isLittle())
2632 std::swap(ArgValue, ArgValue2);
2633 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2634 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002635 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002636
Dan Gohman98ca4f22009-08-05 01:29:28 +00002637 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002638 } else { // VA.isRegLoc()
2639
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002640 // sanity check
2641 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002642
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002643 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002644 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002645 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002646
2647 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002648 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002649 InVals.push_back(DAG.getLoad(ValVT, dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002650 MachinePointerInfo::getFixedStack(LastFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002651 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002652 }
2653 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002654
2655 // The mips ABIs for returning structs by value requires that we copy
2656 // the sret argument into $v0 for the return. Save the argument into
2657 // a virtual register so that we can access it from the return points.
2658 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2659 unsigned Reg = MipsFI->getSRetReturnReg();
2660 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002661 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002662 MipsFI->setSRetReturnReg(Reg);
2663 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002664 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002665 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002666 }
2667
Akira Hatanakabad53f42011-11-14 19:01:09 +00002668 if (isVarArg) {
2669 unsigned NumOfRegs = IsO32 ? 4 : 8;
2670 const unsigned *ArgRegs = IsO32 ? O32IntRegs : Mips64IntRegs;
2671 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumOfRegs);
2672 int FirstRegSlotOffset = IsO32 ? 0 : -64 ; // offset of $a0's slot.
2673 TargetRegisterClass *RC
2674 = IsO32 ? Mips::CPURegsRegisterClass : Mips::CPU64RegsRegisterClass;
2675 unsigned RegSize = RC->getSize();
2676 int RegSlotOffset = FirstRegSlotOffset + Idx * RegSize;
2677
2678 // Offset of the first variable argument from stack pointer.
2679 int FirstVaArgOffset;
2680
2681 if (IsO32 || (Idx == NumOfRegs)) {
2682 FirstVaArgOffset =
2683 (CCInfo.getNextStackOffset() + RegSize - 1) / RegSize * RegSize;
2684 } else
2685 FirstVaArgOffset = RegSlotOffset;
2686
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002687 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002688 // which is a value necessary to VASTART.
Akira Hatanakabad53f42011-11-14 19:01:09 +00002689 LastFI = MFI->CreateFixedObject(RegSize, FirstVaArgOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002690 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002691
Akira Hatanakabad53f42011-11-14 19:01:09 +00002692 // Copy the integer registers that have not been used for argument passing
2693 // to the argument register save area. For O32, the save area is allocated
2694 // in the caller's stack frame, while for N32/64, it is allocated in the
2695 // callee's stack frame.
2696 for (int StackOffset = RegSlotOffset;
2697 Idx < NumOfRegs; ++Idx, StackOffset += RegSize) {
2698 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegs[Idx], RC);
2699 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2700 MVT::getIntegerVT(RegSize * 8));
2701 LastFI = MFI->CreateFixedObject(RegSize, StackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002702 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2703 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
Akira Hatanaka82099682011-12-19 19:52:25 +00002704 MachinePointerInfo(), false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002705 }
2706 }
2707
Akira Hatanaka43299772011-05-20 23:22:14 +00002708 MipsFI->setLastInArgFI(LastFI);
2709
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002710 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002711 // the size of Ins and InVals. This only happens when on varg functions
2712 if (!OutChains.empty()) {
2713 OutChains.push_back(Chain);
2714 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2715 &OutChains[0], OutChains.size());
2716 }
2717
Dan Gohman98ca4f22009-08-05 01:29:28 +00002718 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002719}
2720
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002721//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002722// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002723//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002724
Dan Gohman98ca4f22009-08-05 01:29:28 +00002725SDValue
2726MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002727 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002728 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002729 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002730 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002731
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002732 // CCValAssign - represent the assignment of
2733 // the return value to a location
2734 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002735
2736 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002737 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2738 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002739
Dan Gohman98ca4f22009-08-05 01:29:28 +00002740 // Analize return values.
2741 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002742
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002743 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002744 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002745 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002746 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002747 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002748 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002749 }
2750
Dan Gohman475871a2008-07-27 21:46:04 +00002751 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002752
2753 // Copy the result values into the output registers.
2754 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2755 CCValAssign &VA = RVLocs[i];
2756 assert(VA.isRegLoc() && "Can only return in registers!");
2757
Akira Hatanaka82099682011-12-19 19:52:25 +00002758 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002759
2760 // guarantee that all emitted copies are
2761 // stuck together, avoiding something bad
2762 Flag = Chain.getValue(1);
2763 }
2764
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002765 // The mips ABIs for returning structs by value requires that we copy
2766 // the sret argument into $v0 for the return. We saved the argument into
2767 // a virtual register in the entry block, so now we copy the value out
2768 // and into $v0.
2769 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2770 MachineFunction &MF = DAG.getMachineFunction();
2771 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2772 unsigned Reg = MipsFI->getSRetReturnReg();
2773
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002774 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002775 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002776 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002777
Dale Johannesena05dca42009-02-04 23:02:30 +00002778 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002779 Flag = Chain.getValue(1);
2780 }
2781
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002782 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002783 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002784 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002785 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002786 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002787 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002788 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002789}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002790
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002791//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002792// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002793//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002794
2795/// getConstraintType - Given a constraint letter, return the type of
2796/// constraint it is for this target.
2797MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002798getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002799{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002800 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002801 // GCC config/mips/constraints.md
2802 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002803 // 'd' : An address register. Equivalent to r
2804 // unless generating MIPS16 code.
2805 // 'y' : Equivalent to r; retained for
2806 // backwards compatibility.
2807 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002808 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002809 switch (Constraint[0]) {
2810 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002811 case 'd':
2812 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002813 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002814 return C_RegisterClass;
2815 break;
2816 }
2817 }
2818 return TargetLowering::getConstraintType(Constraint);
2819}
2820
John Thompson44ab89e2010-10-29 17:29:13 +00002821/// Examine constraint type and operand type and determine a weight value.
2822/// This object must already have been set up with the operand type
2823/// and the current alternative constraint selected.
2824TargetLowering::ConstraintWeight
2825MipsTargetLowering::getSingleConstraintMatchWeight(
2826 AsmOperandInfo &info, const char *constraint) const {
2827 ConstraintWeight weight = CW_Invalid;
2828 Value *CallOperandVal = info.CallOperandVal;
2829 // If we don't have a value, we can't do a match,
2830 // but allow it at the lowest weight.
2831 if (CallOperandVal == NULL)
2832 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002833 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002834 // Look at the constraint type.
2835 switch (*constraint) {
2836 default:
2837 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2838 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002839 case 'd':
2840 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002841 if (type->isIntegerTy())
2842 weight = CW_Register;
2843 break;
2844 case 'f':
2845 if (type->isFloatTy())
2846 weight = CW_Register;
2847 break;
2848 }
2849 return weight;
2850}
2851
Eric Christopher38d64262011-06-29 19:33:04 +00002852/// Given a register class constraint, like 'r', if this corresponds directly
2853/// to an LLVM register class, return a register of 0 and the register class
2854/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002855std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002856getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002857{
2858 if (Constraint.size() == 1) {
2859 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002860 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2861 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002862 case 'r':
2863 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002864 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002865 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002866 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002867 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002868 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2869 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002870 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002871 }
2872 }
2873 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2874}
2875
Dan Gohman6520e202008-10-18 02:06:02 +00002876bool
2877MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2878 // The Mips target isn't yet aware of offsets.
2879 return false;
2880}
Evan Chengeb2f9692009-10-27 19:56:55 +00002881
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002882bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2883 if (VT != MVT::f32 && VT != MVT::f64)
2884 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002885 if (Imm.isNegZero())
2886 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002887 return Imm.isZero();
2888}