blob: 5da9a10fd4fb45723015f0a8b573c728e46a81db [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
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000300 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
301 MVT::Glue,
302 MultNode->getOperand(0),// Factor 0
303 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000304 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000305 ADDENode->getOperand(1));// Hi0
306
307 // create CopyFromReg nodes
308 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
309 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000310 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000311 Mips::HI, MVT::i32,
312 CopyFromLo.getValue(2));
313
314 // replace uses of adde and addc here
315 if (!SDValue(ADDCNode, 0).use_empty())
316 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
317
318 if (!SDValue(ADDENode, 0).use_empty())
319 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
320
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000321 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000322}
323
324// SelectMsub -
325// Transforms a subgraph in CurDAG if the following pattern is found:
326// (addc Lo0, multLo), (sube Hi0, multHi),
327// where,
328// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000329// Lo0: initial value of Lo register
330// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000331// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000332static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000333 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000334 // for the matching to be successful.
335 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
336
337 if (SUBCNode->getOpcode() != ISD::SUBC)
338 return false;
339
340 SDValue MultHi = SUBENode->getOperand(1);
341 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000342 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000343 unsigned MultOpc = MultHi.getOpcode();
344
345 // MultHi and MultLo must be generated by the same node,
346 if (MultLo.getNode() != MultNode)
347 return false;
348
349 // and it must be a multiplication.
350 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
351 return false;
352
353 // MultLo amd MultHi must be the first and second output of MultNode
354 // respectively.
355 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
356 return false;
357
358 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
359 // of the values of MultNode, in which case MultNode will be removed in later
360 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000361 // If there exist users other than SUBENode or SUBCNode, this function returns
362 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000363 // instruction node rather than a pair of MULT and MSUB instructions being
364 // produced.
365 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
366 return false;
367
368 SDValue Chain = CurDAG->getEntryNode();
369 DebugLoc dl = SUBENode->getDebugLoc();
370
371 // create MipsSub(u) node
372 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
373
374 SDValue MSub = CurDAG->getNode(MultOpc, dl,
375 MVT::Glue,
376 MultNode->getOperand(0),// Factor 0
377 MultNode->getOperand(1),// Factor 1
378 SUBCNode->getOperand(0),// Lo0
379 SUBENode->getOperand(0));// Hi0
380
381 // create CopyFromReg nodes
382 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
383 MSub);
384 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
385 Mips::HI, MVT::i32,
386 CopyFromLo.getValue(2));
387
388 // replace uses of sube and subc here
389 if (!SDValue(SUBCNode, 0).use_empty())
390 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
391
392 if (!SDValue(SUBENode, 0).use_empty())
393 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
394
395 return true;
396}
397
398static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
399 TargetLowering::DAGCombinerInfo &DCI,
400 const MipsSubtarget* Subtarget) {
401 if (DCI.isBeforeLegalize())
402 return SDValue();
403
Akira Hatanakae184fec2011-11-11 04:18:21 +0000404 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
405 SelectMadd(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000406 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000407
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000408 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000409}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000410
411static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
412 TargetLowering::DAGCombinerInfo &DCI,
413 const MipsSubtarget* Subtarget) {
414 if (DCI.isBeforeLegalize())
415 return SDValue();
416
Akira Hatanakae184fec2011-11-11 04:18:21 +0000417 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
418 SelectMsub(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000419 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000420
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000421 return SDValue();
422}
423
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000424static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
425 TargetLowering::DAGCombinerInfo &DCI,
426 const MipsSubtarget* Subtarget) {
427 if (DCI.isBeforeLegalizeOps())
428 return SDValue();
429
Akira Hatanakadda4a072011-10-03 21:06:13 +0000430 EVT Ty = N->getValueType(0);
431 unsigned LO = (Ty == MVT::i32) ? Mips::LO : Mips::LO64;
432 unsigned HI = (Ty == MVT::i32) ? Mips::HI : Mips::HI64;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000433 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
434 MipsISD::DivRemU;
435 DebugLoc dl = N->getDebugLoc();
436
437 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
438 N->getOperand(0), N->getOperand(1));
439 SDValue InChain = DAG.getEntryNode();
440 SDValue InGlue = DivRem;
441
442 // insert MFLO
443 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakadda4a072011-10-03 21:06:13 +0000444 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000445 InGlue);
446 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
447 InChain = CopyFromLo.getValue(1);
448 InGlue = CopyFromLo.getValue(2);
449 }
450
451 // insert MFHI
452 if (N->hasAnyUseOfValue(1)) {
453 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000454 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000455 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
456 }
457
458 return SDValue();
459}
460
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000461static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
462 switch (CC) {
463 default: llvm_unreachable("Unknown fp condition code!");
464 case ISD::SETEQ:
465 case ISD::SETOEQ: return Mips::FCOND_OEQ;
466 case ISD::SETUNE: return Mips::FCOND_UNE;
467 case ISD::SETLT:
468 case ISD::SETOLT: return Mips::FCOND_OLT;
469 case ISD::SETGT:
470 case ISD::SETOGT: return Mips::FCOND_OGT;
471 case ISD::SETLE:
472 case ISD::SETOLE: return Mips::FCOND_OLE;
473 case ISD::SETGE:
474 case ISD::SETOGE: return Mips::FCOND_OGE;
475 case ISD::SETULT: return Mips::FCOND_ULT;
476 case ISD::SETULE: return Mips::FCOND_ULE;
477 case ISD::SETUGT: return Mips::FCOND_UGT;
478 case ISD::SETUGE: return Mips::FCOND_UGE;
479 case ISD::SETUO: return Mips::FCOND_UN;
480 case ISD::SETO: return Mips::FCOND_OR;
481 case ISD::SETNE:
482 case ISD::SETONE: return Mips::FCOND_ONE;
483 case ISD::SETUEQ: return Mips::FCOND_UEQ;
484 }
485}
486
487
488// Returns true if condition code has to be inverted.
489static bool InvertFPCondCode(Mips::CondCode CC) {
490 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
491 return false;
492
493 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
494 return true;
495
496 assert(false && "Illegal Condition Code");
497 return false;
498}
499
500// Creates and returns an FPCmp node from a setcc node.
501// Returns Op if setcc is not a floating point comparison.
502static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
503 // must be a SETCC node
504 if (Op.getOpcode() != ISD::SETCC)
505 return Op;
506
507 SDValue LHS = Op.getOperand(0);
508
509 if (!LHS.getValueType().isFloatingPoint())
510 return Op;
511
512 SDValue RHS = Op.getOperand(1);
513 DebugLoc dl = Op.getDebugLoc();
514
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000515 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
516 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000517 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
518
519 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
520 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
521}
522
523// Creates and returns a CMovFPT/F node.
524static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
525 SDValue False, DebugLoc DL) {
526 bool invert = InvertFPCondCode((Mips::CondCode)
527 cast<ConstantSDNode>(Cond.getOperand(2))
528 ->getSExtValue());
529
530 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
531 True.getValueType(), True, False, Cond);
532}
533
534static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
535 TargetLowering::DAGCombinerInfo &DCI,
536 const MipsSubtarget* Subtarget) {
537 if (DCI.isBeforeLegalizeOps())
538 return SDValue();
539
540 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
541
542 if (Cond.getOpcode() != MipsISD::FPCmp)
543 return SDValue();
544
545 SDValue True = DAG.getConstant(1, MVT::i32);
546 SDValue False = DAG.getConstant(0, MVT::i32);
547
548 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
549}
550
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000551static SDValue PerformANDCombine(SDNode *N, SelectionDAG& DAG,
552 TargetLowering::DAGCombinerInfo &DCI,
553 const MipsSubtarget* Subtarget) {
554 // Pattern match EXT.
555 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
556 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000557 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000558 return SDValue();
559
560 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000561 unsigned ShiftRightOpc = ShiftRight.getOpcode();
562
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000563 // Op's first operand must be a shift right.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000564 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000565 return SDValue();
566
567 // The second operand of the shift must be an immediate.
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000568 ConstantSDNode *CN;
569 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
570 return SDValue();
571
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000572 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000573 uint64_t SMPos, SMSize;
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000574
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000575 // Op's second operand must be a shifted mask.
576 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000577 !IsShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000578 return SDValue();
579
580 // Return if the shifted mask does not start at bit 0 or the sum of its size
581 // and Pos exceeds the word's size.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000582 EVT ValTy = N->getValueType(0);
583 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000584 return SDValue();
585
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000586 return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), ValTy,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000587 ShiftRight.getOperand(0),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000588 DAG.getConstant(Pos, MVT::i32),
589 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000590}
591
592static SDValue PerformORCombine(SDNode *N, SelectionDAG& DAG,
593 TargetLowering::DAGCombinerInfo &DCI,
594 const MipsSubtarget* Subtarget) {
595 // Pattern match INS.
596 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
597 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
598 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000599 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000600 return SDValue();
601
602 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
603 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
604 ConstantSDNode *CN;
605
606 // See if Op's first operand matches (and $src1 , mask0).
607 if (And0.getOpcode() != ISD::AND)
608 return SDValue();
609
610 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000611 !IsShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000612 return SDValue();
613
614 // See if Op's second operand matches (and (shl $src, pos), mask1).
615 if (And1.getOpcode() != ISD::AND)
616 return SDValue();
617
618 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000619 !IsShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000620 return SDValue();
621
622 // The shift masks must have the same position and size.
623 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
624 return SDValue();
625
626 SDValue Shl = And1.getOperand(0);
627 if (Shl.getOpcode() != ISD::SHL)
628 return SDValue();
629
630 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
631 return SDValue();
632
633 unsigned Shamt = CN->getZExtValue();
634
635 // Return if the shift amount and the first bit position of mask are not the
636 // same.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000637 EVT ValTy = N->getValueType(0);
638 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000639 return SDValue();
640
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000641 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), ValTy,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000642 Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000643 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000644 DAG.getConstant(SMSize0, MVT::i32),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000645 And0.getOperand(0));
646}
647
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000648SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000649 const {
650 SelectionDAG &DAG = DCI.DAG;
651 unsigned opc = N->getOpcode();
652
653 switch (opc) {
654 default: break;
655 case ISD::ADDE:
656 return PerformADDECombine(N, DAG, DCI, Subtarget);
657 case ISD::SUBE:
658 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000659 case ISD::SDIVREM:
660 case ISD::UDIVREM:
661 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000662 case ISD::SETCC:
663 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000664 case ISD::AND:
665 return PerformANDCombine(N, DAG, DCI, Subtarget);
666 case ISD::OR:
667 return PerformORCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000668 }
669
670 return SDValue();
671}
672
Dan Gohman475871a2008-07-27 21:46:04 +0000673SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000674LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000675{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000676 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000677 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000678 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000679 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
680 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000681 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000682 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000683 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
684 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000685 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000686 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000687 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000688 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Akira Hatanakadb548262011-07-19 23:30:50 +0000689 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +0000690 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000691 }
Dan Gohman475871a2008-07-27 21:46:04 +0000692 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000693}
694
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000695//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000696// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000697//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000698
699// AddLiveIn - This helper function adds the specified physical register to the
700// MachineFunction as a live in value. It also creates a corresponding
701// virtual register for it.
702static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000703AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000704{
705 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000706 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
707 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000708 return VReg;
709}
710
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000711// Get fp branch code (not opcode) from condition code.
712static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
713 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
714 return Mips::BRANCH_T;
715
716 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
717 return Mips::BRANCH_F;
718
719 return Mips::BRANCH_INVALID;
720}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000721
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000722/*
Akira Hatanaka14487d42011-06-07 19:28:39 +0000723static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
724 DebugLoc dl,
725 const MipsSubtarget* Subtarget,
726 const TargetInstrInfo *TII,
727 bool isFPCmp, unsigned Opc) {
728 // There is no need to expand CMov instructions if target has
729 // conditional moves.
730 if (Subtarget->hasCondMov())
731 return BB;
732
733 // To "insert" a SELECT_CC instruction, we actually have to insert the
734 // diamond control-flow pattern. The incoming instruction knows the
735 // destination vreg to set, the condition code register to branch on, the
736 // true/false values to select between, and a branch opcode to use.
737 const BasicBlock *LLVM_BB = BB->getBasicBlock();
738 MachineFunction::iterator It = BB;
739 ++It;
740
741 // thisMBB:
742 // ...
743 // TrueVal = ...
744 // setcc r1, r2, r3
745 // bNE r1, r0, copy1MBB
746 // fallthrough --> copy0MBB
747 MachineBasicBlock *thisMBB = BB;
748 MachineFunction *F = BB->getParent();
749 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
750 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
751 F->insert(It, copy0MBB);
752 F->insert(It, sinkMBB);
753
754 // Transfer the remainder of BB and its successor edges to sinkMBB.
755 sinkMBB->splice(sinkMBB->begin(), BB,
756 llvm::next(MachineBasicBlock::iterator(MI)),
757 BB->end());
758 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
759
760 // Next, add the true and fallthrough blocks as its successors.
761 BB->addSuccessor(copy0MBB);
762 BB->addSuccessor(sinkMBB);
763
764 // Emit the right instruction according to the type of the operands compared
765 if (isFPCmp)
766 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
767 else
768 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
769 .addReg(Mips::ZERO).addMBB(sinkMBB);
770
771 // copy0MBB:
772 // %FalseValue = ...
773 // # fallthrough to sinkMBB
774 BB = copy0MBB;
775
776 // Update machine-CFG edges
777 BB->addSuccessor(sinkMBB);
778
779 // sinkMBB:
780 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
781 // ...
782 BB = sinkMBB;
783
784 if (isFPCmp)
785 BuildMI(*BB, BB->begin(), dl,
786 TII->get(Mips::PHI), MI->getOperand(0).getReg())
787 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
788 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
789 else
790 BuildMI(*BB, BB->begin(), dl,
791 TII->get(Mips::PHI), MI->getOperand(0).getReg())
792 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
793 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
794
795 MI->eraseFromParent(); // The pseudo instruction is gone now.
796 return BB;
797}
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000798*/
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000799MachineBasicBlock *
800MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000801 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000802 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000803 default:
804 assert(false && "Unexpected instr type to insert");
805 return NULL;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000806 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000807 case Mips::ATOMIC_LOAD_ADD_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000808 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
809 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000810 case Mips::ATOMIC_LOAD_ADD_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000811 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
812 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000813 case Mips::ATOMIC_LOAD_ADD_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000814 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000815 case Mips::ATOMIC_LOAD_ADD_I64:
816 case Mips::ATOMIC_LOAD_ADD_I64_P8:
817 return EmitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000818
819 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000820 case Mips::ATOMIC_LOAD_AND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000821 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
822 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000823 case Mips::ATOMIC_LOAD_AND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000824 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
825 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000826 case Mips::ATOMIC_LOAD_AND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000827 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000828 case Mips::ATOMIC_LOAD_AND_I64:
829 case Mips::ATOMIC_LOAD_AND_I64_P8:
Akira Hatanaka73866122011-11-12 02:38:12 +0000830 return EmitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000831
832 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000833 case Mips::ATOMIC_LOAD_OR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000834 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
835 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000836 case Mips::ATOMIC_LOAD_OR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000837 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
838 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000839 case Mips::ATOMIC_LOAD_OR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000840 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000841 case Mips::ATOMIC_LOAD_OR_I64:
842 case Mips::ATOMIC_LOAD_OR_I64_P8:
843 return EmitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000844
845 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000846 case Mips::ATOMIC_LOAD_XOR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000847 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
848 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000849 case Mips::ATOMIC_LOAD_XOR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000850 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
851 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000852 case Mips::ATOMIC_LOAD_XOR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000853 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000854 case Mips::ATOMIC_LOAD_XOR_I64:
855 case Mips::ATOMIC_LOAD_XOR_I64_P8:
856 return EmitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000857
858 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000859 case Mips::ATOMIC_LOAD_NAND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000860 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
861 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000862 case Mips::ATOMIC_LOAD_NAND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000863 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
864 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000865 case Mips::ATOMIC_LOAD_NAND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000866 return EmitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000867 case Mips::ATOMIC_LOAD_NAND_I64:
868 case Mips::ATOMIC_LOAD_NAND_I64_P8:
869 return EmitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000870
871 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000872 case Mips::ATOMIC_LOAD_SUB_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000873 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
874 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000875 case Mips::ATOMIC_LOAD_SUB_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000876 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
877 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000878 case Mips::ATOMIC_LOAD_SUB_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000879 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000880 case Mips::ATOMIC_LOAD_SUB_I64:
881 case Mips::ATOMIC_LOAD_SUB_I64_P8:
882 return EmitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000883
884 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000885 case Mips::ATOMIC_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000886 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
887 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000888 case Mips::ATOMIC_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000889 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
890 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000891 case Mips::ATOMIC_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000892 return EmitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000893 case Mips::ATOMIC_SWAP_I64:
894 case Mips::ATOMIC_SWAP_I64_P8:
895 return EmitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000896
897 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000898 case Mips::ATOMIC_CMP_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000899 return EmitAtomicCmpSwapPartword(MI, BB, 1);
900 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000901 case Mips::ATOMIC_CMP_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000902 return EmitAtomicCmpSwapPartword(MI, BB, 2);
903 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000904 case Mips::ATOMIC_CMP_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000905 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000906 case Mips::ATOMIC_CMP_SWAP_I64:
907 case Mips::ATOMIC_CMP_SWAP_I64_P8:
908 return EmitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000909 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000910}
911
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000912// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
913// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
914MachineBasicBlock *
915MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000916 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000917 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000918 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000919
920 MachineFunction *MF = BB->getParent();
921 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000922 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000923 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
924 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000925 unsigned LL, SC, AND, NOR, ZERO, BEQ;
926
927 if (Size == 4) {
928 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
929 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
930 AND = Mips::AND;
931 NOR = Mips::NOR;
932 ZERO = Mips::ZERO;
933 BEQ = Mips::BEQ;
934 }
935 else {
936 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
937 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
938 AND = Mips::AND64;
939 NOR = Mips::NOR64;
940 ZERO = Mips::ZERO_64;
941 BEQ = Mips::BEQ64;
942 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000943
Akira Hatanaka4061da12011-07-19 20:11:17 +0000944 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000945 unsigned Ptr = MI->getOperand(1).getReg();
946 unsigned Incr = MI->getOperand(2).getReg();
947
Akira Hatanaka4061da12011-07-19 20:11:17 +0000948 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
949 unsigned AndRes = RegInfo.createVirtualRegister(RC);
950 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000951
952 // insert new blocks after the current block
953 const BasicBlock *LLVM_BB = BB->getBasicBlock();
954 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
955 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
956 MachineFunction::iterator It = BB;
957 ++It;
958 MF->insert(It, loopMBB);
959 MF->insert(It, exitMBB);
960
961 // Transfer the remainder of BB and its successor edges to exitMBB.
962 exitMBB->splice(exitMBB->begin(), BB,
963 llvm::next(MachineBasicBlock::iterator(MI)),
964 BB->end());
965 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
966
967 // thisMBB:
968 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000969 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000970 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000971 loopMBB->addSuccessor(loopMBB);
972 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000973
974 // loopMBB:
975 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000976 // <binop> storeval, oldval, incr
977 // sc success, storeval, 0(ptr)
978 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000979 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +0000980 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000981 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000982 // and andres, oldval, incr
983 // nor storeval, $0, andres
Akira Hatanaka59068062011-11-11 04:14:30 +0000984 BuildMI(BB, dl, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
985 BuildMI(BB, dl, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000986 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000987 // <binop> storeval, oldval, incr
988 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000989 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000990 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000991 }
Akira Hatanaka59068062011-11-11 04:14:30 +0000992 BuildMI(BB, dl, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
993 BuildMI(BB, dl, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000994
995 MI->eraseFromParent(); // The instruction is gone now.
996
Akira Hatanaka939ece12011-07-19 03:42:13 +0000997 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000998}
999
1000MachineBasicBlock *
1001MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001002 MachineBasicBlock *BB,
1003 unsigned Size, unsigned BinOpcode,
1004 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001005 assert((Size == 1 || Size == 2) &&
1006 "Unsupported size for EmitAtomicBinaryPartial.");
1007
1008 MachineFunction *MF = BB->getParent();
1009 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1010 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1011 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1012 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001013 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1014 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001015
1016 unsigned Dest = MI->getOperand(0).getReg();
1017 unsigned Ptr = MI->getOperand(1).getReg();
1018 unsigned Incr = MI->getOperand(2).getReg();
1019
Akira Hatanaka4061da12011-07-19 20:11:17 +00001020 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1021 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001022 unsigned Mask = RegInfo.createVirtualRegister(RC);
1023 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001024 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1025 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001026 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001027 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1028 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1029 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1030 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1031 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001032 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001033 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1034 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1035 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1036 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1037 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001038
1039 // insert new blocks after the current block
1040 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1041 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001042 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001043 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1044 MachineFunction::iterator It = BB;
1045 ++It;
1046 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001047 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001048 MF->insert(It, exitMBB);
1049
1050 // Transfer the remainder of BB and its successor edges to exitMBB.
1051 exitMBB->splice(exitMBB->begin(), BB,
1052 llvm::next(MachineBasicBlock::iterator(MI)),
1053 BB->end());
1054 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1055
Akira Hatanaka81b44112011-07-19 17:09:53 +00001056 BB->addSuccessor(loopMBB);
1057 loopMBB->addSuccessor(loopMBB);
1058 loopMBB->addSuccessor(sinkMBB);
1059 sinkMBB->addSuccessor(exitMBB);
1060
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001061 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001062 // addiu masklsb2,$0,-4 # 0xfffffffc
1063 // and alignedaddr,ptr,masklsb2
1064 // andi ptrlsb2,ptr,3
1065 // sll shiftamt,ptrlsb2,3
1066 // ori maskupper,$0,255 # 0xff
1067 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001068 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001069 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001070
1071 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001072 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1073 .addReg(Mips::ZERO).addImm(-4);
1074 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1075 .addReg(Ptr).addReg(MaskLSB2);
1076 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1077 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1078 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1079 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001080 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1081 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001082 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001083 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001084
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001085
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001086 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001087 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001088 // ll oldval,0(alignedaddr)
1089 // binop binopres,oldval,incr2
1090 // and newval,binopres,mask
1091 // and maskedoldval0,oldval,mask2
1092 // or storeval,maskedoldval0,newval
1093 // sc success,storeval,0(alignedaddr)
1094 // beq success,$0,loopMBB
1095
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001096 // atomic.swap
1097 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001098 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001099 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001100 // and maskedoldval0,oldval,mask2
1101 // or storeval,maskedoldval0,newval
1102 // sc success,storeval,0(alignedaddr)
1103 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001104
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001105 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001106 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001107 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001108 // and andres, oldval, incr2
1109 // nor binopres, $0, andres
1110 // and newval, binopres, mask
1111 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1112 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1113 .addReg(Mips::ZERO).addReg(AndRes);
1114 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001115 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001116 // <binop> binopres, oldval, incr2
1117 // and newval, binopres, mask
1118 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1119 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001120 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001121 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001122 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001123 }
1124
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001125 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001126 .addReg(OldVal).addReg(Mask2);
1127 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001128 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001129 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001130 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001131 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001132 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001133
Akira Hatanaka939ece12011-07-19 03:42:13 +00001134 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001135 // and maskedoldval1,oldval,mask
1136 // srl srlres,maskedoldval1,shiftamt
1137 // sll sllres,srlres,24
1138 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001139 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001140 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001141
Akira Hatanaka4061da12011-07-19 20:11:17 +00001142 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1143 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001144 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1145 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001146 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1147 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001148 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001149 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001150
1151 MI->eraseFromParent(); // The instruction is gone now.
1152
Akira Hatanaka939ece12011-07-19 03:42:13 +00001153 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001154}
1155
1156MachineBasicBlock *
1157MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001158 MachineBasicBlock *BB,
1159 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001160 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001161
1162 MachineFunction *MF = BB->getParent();
1163 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001164 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001165 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1166 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001167 unsigned LL, SC, ZERO, BNE, BEQ;
1168
1169 if (Size == 4) {
1170 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1171 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1172 ZERO = Mips::ZERO;
1173 BNE = Mips::BNE;
1174 BEQ = Mips::BEQ;
1175 }
1176 else {
1177 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1178 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1179 ZERO = Mips::ZERO_64;
1180 BNE = Mips::BNE64;
1181 BEQ = Mips::BEQ64;
1182 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001183
1184 unsigned Dest = MI->getOperand(0).getReg();
1185 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001186 unsigned OldVal = MI->getOperand(2).getReg();
1187 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001188
Akira Hatanaka4061da12011-07-19 20:11:17 +00001189 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001190
1191 // insert new blocks after the current block
1192 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1193 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1194 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1195 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1196 MachineFunction::iterator It = BB;
1197 ++It;
1198 MF->insert(It, loop1MBB);
1199 MF->insert(It, loop2MBB);
1200 MF->insert(It, exitMBB);
1201
1202 // Transfer the remainder of BB and its successor edges to exitMBB.
1203 exitMBB->splice(exitMBB->begin(), BB,
1204 llvm::next(MachineBasicBlock::iterator(MI)),
1205 BB->end());
1206 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1207
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001208 // thisMBB:
1209 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001210 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001211 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001212 loop1MBB->addSuccessor(exitMBB);
1213 loop1MBB->addSuccessor(loop2MBB);
1214 loop2MBB->addSuccessor(loop1MBB);
1215 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001216
1217 // loop1MBB:
1218 // ll dest, 0(ptr)
1219 // bne dest, oldval, exitMBB
1220 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001221 BuildMI(BB, dl, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1222 BuildMI(BB, dl, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001223 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001224
1225 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001226 // sc success, newval, 0(ptr)
1227 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001228 BB = loop2MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001229 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001230 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka59068062011-11-11 04:14:30 +00001231 BuildMI(BB, dl, TII->get(BEQ))
1232 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001233
1234 MI->eraseFromParent(); // The instruction is gone now.
1235
Akira Hatanaka939ece12011-07-19 03:42:13 +00001236 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001237}
1238
1239MachineBasicBlock *
1240MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001241 MachineBasicBlock *BB,
1242 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001243 assert((Size == 1 || Size == 2) &&
1244 "Unsupported size for EmitAtomicCmpSwapPartial.");
1245
1246 MachineFunction *MF = BB->getParent();
1247 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1248 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1249 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1250 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001251 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1252 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001253
1254 unsigned Dest = MI->getOperand(0).getReg();
1255 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001256 unsigned CmpVal = MI->getOperand(2).getReg();
1257 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001258
Akira Hatanaka4061da12011-07-19 20:11:17 +00001259 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1260 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001261 unsigned Mask = RegInfo.createVirtualRegister(RC);
1262 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001263 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1264 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1265 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1266 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1267 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1268 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1269 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1270 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1271 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1272 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1273 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1274 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1275 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1276 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001277
1278 // insert new blocks after the current block
1279 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1280 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1281 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001282 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001283 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1284 MachineFunction::iterator It = BB;
1285 ++It;
1286 MF->insert(It, loop1MBB);
1287 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001288 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001289 MF->insert(It, exitMBB);
1290
1291 // Transfer the remainder of BB and its successor edges to exitMBB.
1292 exitMBB->splice(exitMBB->begin(), BB,
1293 llvm::next(MachineBasicBlock::iterator(MI)),
1294 BB->end());
1295 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1296
Akira Hatanaka81b44112011-07-19 17:09:53 +00001297 BB->addSuccessor(loop1MBB);
1298 loop1MBB->addSuccessor(sinkMBB);
1299 loop1MBB->addSuccessor(loop2MBB);
1300 loop2MBB->addSuccessor(loop1MBB);
1301 loop2MBB->addSuccessor(sinkMBB);
1302 sinkMBB->addSuccessor(exitMBB);
1303
Akira Hatanaka70564a92011-07-19 18:14:26 +00001304 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001305 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001306 // addiu masklsb2,$0,-4 # 0xfffffffc
1307 // and alignedaddr,ptr,masklsb2
1308 // andi ptrlsb2,ptr,3
1309 // sll shiftamt,ptrlsb2,3
1310 // ori maskupper,$0,255 # 0xff
1311 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001312 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001313 // andi maskedcmpval,cmpval,255
1314 // sll shiftedcmpval,maskedcmpval,shiftamt
1315 // andi maskednewval,newval,255
1316 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001317 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001318 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1319 .addReg(Mips::ZERO).addImm(-4);
1320 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1321 .addReg(Ptr).addReg(MaskLSB2);
1322 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1323 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1324 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1325 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001326 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1327 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001328 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001329 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1330 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001331 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1332 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001333 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1334 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001335 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1336 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001337
1338 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001339 // ll oldval,0(alginedaddr)
1340 // and maskedoldval0,oldval,mask
1341 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001342 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001343 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001344 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1345 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001346 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001347 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001348
1349 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001350 // and maskedoldval1,oldval,mask2
1351 // or storeval,maskedoldval1,shiftednewval
1352 // sc success,storeval,0(alignedaddr)
1353 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001354 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001355 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1356 .addReg(OldVal).addReg(Mask2);
1357 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1358 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001359 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001360 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001361 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001362 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001363
Akira Hatanaka939ece12011-07-19 03:42:13 +00001364 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001365 // srl srlres,maskedoldval0,shiftamt
1366 // sll sllres,srlres,24
1367 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001368 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001369 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001370
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001371 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1372 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001373 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1374 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001375 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001376 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001377
1378 MI->eraseFromParent(); // The instruction is gone now.
1379
Akira Hatanaka939ece12011-07-19 03:42:13 +00001380 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001381}
1382
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001383//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001384// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001385//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001386SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001387LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001388{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001389 MachineFunction &MF = DAG.getMachineFunction();
1390 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001391 unsigned SP = IsN64 ? Mips::SP_64 : Mips::SP;
Akira Hatanaka21afc632011-06-21 00:40:49 +00001392
1393 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001394 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1395 "Cannot lower if the alignment of the allocated space is larger than \
1396 that of the stack.");
1397
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001398 SDValue Chain = Op.getOperand(0);
1399 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001400 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001401
1402 // Get a reference from Mips stack pointer
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001403 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, SP, getPointerTy());
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001404
1405 // Subtract the dynamic size from the actual stack size to
1406 // obtain the new stack size.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001407 SDValue Sub = DAG.getNode(ISD::SUB, dl, getPointerTy(), StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001408
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001409 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001410 // must be placed in the stack pointer register.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001411 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, SP, Sub, SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001412
1413 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001414 // value and a chain
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001415 SDVTList VTLs = DAG.getVTList(getPointerTy(), MVT::Other);
Akira Hatanaka21afc632011-06-21 00:40:49 +00001416 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1417 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1418
1419 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001420}
1421
1422SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001423LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001424{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001425 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001426 // the block to branch to if the condition is true.
1427 SDValue Chain = Op.getOperand(0);
1428 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001429 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001430
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001431 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1432
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001433 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001434 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001435 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001436
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001437 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001438 Mips::CondCode CC =
1439 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001440 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001441
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001442 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001443 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001444}
1445
1446SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001447LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001448{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001449 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001450
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001451 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001452 if (Cond.getOpcode() != MipsISD::FPCmp)
1453 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001454
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001455 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1456 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001457}
1458
Dan Gohmand858e902010-04-17 15:26:15 +00001459SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1460 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001461 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001462 DebugLoc dl = Op.getDebugLoc();
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001463 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001464
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001465 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001466 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001467
Chris Lattnerb71b9092009-08-13 06:28:06 +00001468 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001469
Chris Lattnere3736f82009-08-13 05:41:27 +00001470 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001471 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1472 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001473 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001474 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1475 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001476 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001477 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001478 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001479 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1480 MipsII::MO_ABS_HI);
1481 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1482 MipsII::MO_ABS_LO);
1483 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1484 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001485 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001486 }
1487
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001488 EVT ValTy = Op.getValueType();
1489 bool HasGotOfst = (GV->hasInternalLinkage() ||
1490 (GV->hasLocalLinkage() && !isa<Function>(GV)));
1491 unsigned GotFlag = IsN64 ?
1492 (HasGotOfst ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT_DISP) :
Bruno Cardoso Lopese3d35722011-12-07 00:28:57 +00001493 (HasGotOfst ? MipsII::MO_GOT : MipsII::MO_GOT16);
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001494 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0, GotFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001495 GA = DAG.getNode(MipsISD::Wrapper, dl, ValTy, GA);
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001496 SDValue ResNode = DAG.getLoad(ValTy, dl,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001497 DAG.getEntryNode(), GA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001498 false, false, false, 0);
Akira Hatanaka0f843822011-06-07 18:58:42 +00001499 // On functions and global targets not internal linked only
1500 // a load from got/GP is necessary for PIC to work.
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001501 if (!HasGotOfst)
Akira Hatanaka0f843822011-06-07 18:58:42 +00001502 return ResNode;
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001503 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0,
1504 IsN64 ? MipsII::MO_GOT_OFST :
1505 MipsII::MO_ABS_LO);
1506 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, GALo);
1507 return DAG.getNode(ISD::ADD, dl, ValTy, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001508}
1509
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001510SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1511 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001512 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1513 // FIXME there isn't actually debug info here
1514 DebugLoc dl = Op.getDebugLoc();
1515
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001516 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001517 // %hi/%lo relocation
1518 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
1519 MipsII::MO_ABS_HI);
1520 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
1521 MipsII::MO_ABS_LO);
1522 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1523 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1524 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001525 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001526
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001527 EVT ValTy = Op.getValueType();
1528 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1529 unsigned OFSTFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1530 SDValue BAGOTOffset = DAG.getBlockAddress(BA, ValTy, true, GOTFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001531 BAGOTOffset = DAG.getNode(MipsISD::Wrapper, dl, ValTy, BAGOTOffset);
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001532 SDValue BALOOffset = DAG.getBlockAddress(BA, ValTy, true, OFSTFlag);
1533 SDValue Load = DAG.getLoad(ValTy, dl,
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001534 DAG.getEntryNode(), BAGOTOffset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001535 MachinePointerInfo(), false, false, false, 0);
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001536 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, BALOOffset);
1537 return DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001538}
1539
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001540SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001541LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001542{
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001543 // If the relocation model is PIC, use the General Dynamic TLS Model,
1544 // otherwise use the Initial Exec or Local Exec TLS Model.
1545 // TODO: implement Local Dynamic TLS model
1546
1547 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1548 DebugLoc dl = GA->getDebugLoc();
1549 const GlobalValue *GV = GA->getGlobal();
1550 EVT PtrVT = getPointerTy();
1551
1552 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1553 // General Dynamic TLS Model
Akira Hatanakaca074792011-12-08 20:34:32 +00001554 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001555 0, MipsII::MO_TLSGD);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001556 SDValue Argument = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, TGA);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001557 unsigned PtrSize = PtrVT.getSizeInBits();
1558 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1559
Benjamin Kramer5eccf672011-12-11 12:21:34 +00001560 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001561
1562 ArgListTy Args;
1563 ArgListEntry Entry;
1564 Entry.Node = Argument;
Akira Hatanakaca074792011-12-08 20:34:32 +00001565 Entry.Ty = PtrTy;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001566 Args.push_back(Entry);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001567
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001568 std::pair<SDValue, SDValue> CallResult =
Akira Hatanakaca074792011-12-08 20:34:32 +00001569 LowerCallTo(DAG.getEntryNode(), PtrTy,
1570 false, false, false, false, 0, CallingConv::C, false, true,
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001571 TlsGetAddr, Args, DAG, dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001572
1573 return CallResult.first;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001574 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001575
1576 SDValue Offset;
1577 if (GV->isDeclaration()) {
1578 // Initial Exec TLS Model
Akira Hatanakaca074792011-12-08 20:34:32 +00001579 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001580 MipsII::MO_GOTTPREL);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001581 TGA = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, TGA);
Akira Hatanakaca074792011-12-08 20:34:32 +00001582 Offset = DAG.getLoad(PtrVT, dl,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001583 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001584 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001585 } else {
1586 // Local Exec TLS Model
Akira Hatanakaca074792011-12-08 20:34:32 +00001587 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001588 MipsII::MO_TPREL_HI);
Akira Hatanakaca074792011-12-08 20:34:32 +00001589 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001590 MipsII::MO_TPREL_LO);
Akira Hatanakaca074792011-12-08 20:34:32 +00001591 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, PtrVT, TGAHi);
1592 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, TGALo);
1593 Offset = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001594 }
1595
1596 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1597 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001598}
1599
1600SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001601LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001602{
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001603 SDValue HiPart, JTI, JTILo;
Dale Johannesende064702009-02-06 21:50:26 +00001604 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001605 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001606 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Owen Andersone50ed302009-08-10 22:56:29 +00001607 EVT PtrVT = Op.getValueType();
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001608 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001609
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001610 if (!IsPIC && !IsN64) {
1611 JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MipsII::MO_ABS_HI);
1612 HiPart = DAG.getNode(MipsISD::Hi, dl, PtrVT, JTI);
1613 JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MipsII::MO_ABS_LO);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001614 } else {// Emit Load from Global Pointer
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001615 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1616 unsigned OfstFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1617 JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, GOTFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001618 JTI = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, JTI);
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001619 HiPart = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), JTI,
1620 MachinePointerInfo(), false, false, false, 0);
1621 JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OfstFlag);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001622 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001623
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001624 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, JTILo);
1625 return DAG.getNode(ISD::ADD, dl, PtrVT, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001626}
1627
Dan Gohman475871a2008-07-27 21:46:04 +00001628SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001629LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001630{
Dan Gohman475871a2008-07-27 21:46:04 +00001631 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001632 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001633 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001634 // FIXME there isn't actually debug info here
1635 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001636
1637 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001638 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001639 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001640 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001641 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001642 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001643 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1644 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001645 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001646
1647 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001648 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001649 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001650 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001651 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001652 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1653 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001654 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001655 } else {
Akira Hatanaka620db892011-11-16 22:44:38 +00001656 EVT ValTy = Op.getValueType();
1657 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1658 unsigned OFSTFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1659 SDValue CP = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1660 N->getOffset(), GOTFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001661 CP = DAG.getNode(MipsISD::Wrapper, dl, ValTy, CP);
Akira Hatanaka620db892011-11-16 22:44:38 +00001662 SDValue Load = DAG.getLoad(ValTy, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001663 CP, MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001664 false, false, false, 0);
Akira Hatanaka620db892011-11-16 22:44:38 +00001665 SDValue CPLo = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1666 N->getOffset(), OFSTFlag);
1667 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, CPLo);
1668 ResNode = DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001669 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001670
1671 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001672}
1673
Dan Gohmand858e902010-04-17 15:26:15 +00001674SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001675 MachineFunction &MF = DAG.getMachineFunction();
1676 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1677
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001678 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001679 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1680 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001681
1682 // vastart just stores the address of the VarArgsFrameIndex slot into the
1683 // memory location argument.
1684 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001685 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1686 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +00001687 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001688}
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001689
1690// Called if the size of integer registers is large enough to hold the whole
1691// floating point number.
1692static SDValue LowerFCOPYSIGNLargeIntReg(SDValue Op, SelectionDAG &DAG) {
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001693 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001694 EVT ValTy = Op.getValueType();
1695 EVT IntValTy = MVT::getIntegerVT(ValTy.getSizeInBits());
1696 uint64_t Mask = (uint64_t)1 << (ValTy.getSizeInBits() - 1);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001697 DebugLoc dl = Op.getDebugLoc();
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001698 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, IntValTy, Op.getOperand(0));
1699 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, IntValTy, Op.getOperand(1));
1700 SDValue And0 = DAG.getNode(ISD::AND, dl, IntValTy, Op0,
1701 DAG.getConstant(Mask - 1, IntValTy));
1702 SDValue And1 = DAG.getNode(ISD::AND, dl, IntValTy, Op1,
1703 DAG.getConstant(Mask, IntValTy));
1704 SDValue Result = DAG.getNode(ISD::OR, dl, IntValTy, And0, And1);
1705 return DAG.getNode(ISD::BITCAST, dl, ValTy, Result);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001706}
1707
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001708// Called if the size of integer registers is not large enough to hold the whole
1709// floating point number (e.g. f64 & 32-bit integer register).
1710static SDValue
1711LowerFCOPYSIGNSmallIntReg(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001712 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001713 // Use ext/ins instructions if target architecture is Mips32r2.
1714 // Eliminate redundant mfc1 and mtc1 instructions.
1715 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001716
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001717 if (!isLittle)
1718 std::swap(LoIdx, HiIdx);
1719
1720 DebugLoc dl = Op.getDebugLoc();
1721 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1722 Op.getOperand(0),
1723 DAG.getConstant(LoIdx, MVT::i32));
1724 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1725 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1726 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1727 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1728 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1729 DAG.getConstant(0x7fffffff, MVT::i32));
1730 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1731 DAG.getConstant(0x80000000, MVT::i32));
1732 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1733
1734 if (!isLittle)
1735 std::swap(Word0, Word1);
1736
1737 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1738}
1739
1740SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1741 const {
1742 EVT Ty = Op.getValueType();
1743
1744 assert(Ty == MVT::f32 || Ty == MVT::f64);
1745
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001746 if (Ty == MVT::f32 || HasMips64)
1747 return LowerFCOPYSIGNLargeIntReg(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001748 else
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001749 return LowerFCOPYSIGNSmallIntReg(Op, DAG, Subtarget->isLittle());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001750}
1751
Akira Hatanaka2e591472011-06-02 00:24:44 +00001752SDValue MipsTargetLowering::
1753LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001754 // check the depth
1755 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001756 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001757
1758 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1759 MFI->setFrameAddressIsTaken(true);
1760 EVT VT = Op.getValueType();
1761 DebugLoc dl = Op.getDebugLoc();
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001762 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
1763 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001764 return FrameAddr;
1765}
1766
Akira Hatanakadb548262011-07-19 23:30:50 +00001767// TODO: set SType according to the desired memory barrier behavior.
1768SDValue MipsTargetLowering::LowerMEMBARRIER(SDValue Op,
1769 SelectionDAG& DAG) const {
1770 unsigned SType = 0;
1771 DebugLoc dl = Op.getDebugLoc();
1772 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1773 DAG.getConstant(SType, MVT::i32));
1774}
1775
Eli Friedman14648462011-07-27 22:21:52 +00001776SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1777 SelectionDAG& DAG) const {
1778 // FIXME: Need pseudo-fence for 'singlethread' fences
1779 // FIXME: Set SType for weaker fences where supported/appropriate.
1780 unsigned SType = 0;
1781 DebugLoc dl = Op.getDebugLoc();
1782 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1783 DAG.getConstant(SType, MVT::i32));
1784}
1785
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001786//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001787// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001788//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001789
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001790//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001791// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001792// Mips O32 ABI rules:
1793// ---
1794// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001795// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001796// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001797// f64 - Only passed in two aliased f32 registers if no int reg has been used
1798// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001799// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1800// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001801//
1802// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001803//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001804
Duncan Sands1e96bab2010-11-04 10:49:57 +00001805static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001806 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001807 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1808
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001809 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001810
1811 static const unsigned IntRegs[] = {
1812 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1813 };
1814 static const unsigned F32Regs[] = {
1815 Mips::F12, Mips::F14
1816 };
1817 static const unsigned F64Regs[] = {
1818 Mips::D6, Mips::D7
1819 };
1820
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001821 // ByVal Args
1822 if (ArgFlags.isByVal()) {
1823 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1824 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1825 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1826 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1827 r < std::min(IntRegsSize, NextReg); ++r)
1828 State.AllocateReg(IntRegs[r]);
1829 return false;
1830 }
1831
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001832 // Promote i8 and i16
1833 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1834 LocVT = MVT::i32;
1835 if (ArgFlags.isSExt())
1836 LocInfo = CCValAssign::SExt;
1837 else if (ArgFlags.isZExt())
1838 LocInfo = CCValAssign::ZExt;
1839 else
1840 LocInfo = CCValAssign::AExt;
1841 }
1842
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001843 unsigned Reg;
1844
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001845 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1846 // is true: function is vararg, argument is 3rd or higher, there is previous
1847 // argument which is not f32 or f64.
1848 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1849 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001850 unsigned OrigAlign = ArgFlags.getOrigAlign();
1851 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001852
1853 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001854 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001855 // If this is the first part of an i64 arg,
1856 // the allocated register must be either A0 or A2.
1857 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1858 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001859 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001860 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1861 // Allocate int register and shadow next int register. If first
1862 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001863 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1864 if (Reg == Mips::A1 || Reg == Mips::A3)
1865 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1866 State.AllocateReg(IntRegs, IntRegsSize);
1867 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001868 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1869 // we are guaranteed to find an available float register
1870 if (ValVT == MVT::f32) {
1871 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1872 // Shadow int register
1873 State.AllocateReg(IntRegs, IntRegsSize);
1874 } else {
1875 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1876 // Shadow int registers
1877 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1878 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1879 State.AllocateReg(IntRegs, IntRegsSize);
1880 State.AllocateReg(IntRegs, IntRegsSize);
1881 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001882 } else
1883 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001884
Akira Hatanakad37776d2011-05-20 21:39:54 +00001885 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1886 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1887
1888 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001889 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001890 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001891 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001892
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001893 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001894}
1895
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00001896static const unsigned Mips64IntRegs[8] =
1897 {Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
1898 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64};
1899static const unsigned Mips64DPRegs[8] =
1900 {Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
1901 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64};
1902
1903static bool CC_Mips64Byval(unsigned ValNo, MVT ValVT, MVT LocVT,
1904 CCValAssign::LocInfo LocInfo,
1905 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1906 unsigned Align = std::max(ArgFlags.getByValAlign(), (unsigned)8);
1907 unsigned Size = (ArgFlags.getByValSize() + 7) / 8 * 8;
1908 unsigned FirstIdx = State.getFirstUnallocated(Mips64IntRegs, 8);
1909
1910 assert(Align <= 16 && "Cannot handle alignments larger than 16.");
1911
1912 // If byval is 16-byte aligned, the first arg register must be even.
1913 if ((Align == 16) && (FirstIdx % 2)) {
1914 State.AllocateReg(Mips64IntRegs[FirstIdx], Mips64DPRegs[FirstIdx]);
1915 ++FirstIdx;
1916 }
1917
1918 // Mark the registers allocated.
1919 for (unsigned I = FirstIdx; Size && (I < 8); Size -= 8, ++I)
1920 State.AllocateReg(Mips64IntRegs[I], Mips64DPRegs[I]);
1921
1922 // Allocate space on caller's stack.
1923 unsigned Offset = State.AllocateStack(Size, Align);
1924
1925 if (FirstIdx < 8)
1926 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Mips64IntRegs[FirstIdx],
1927 LocVT, LocInfo));
1928 else
1929 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
1930
1931 return true;
1932}
1933
1934#include "MipsGenCallingConv.inc"
1935
Akira Hatanaka49617092011-11-14 19:02:54 +00001936static void
1937AnalyzeMips64CallOperands(CCState CCInfo,
1938 const SmallVectorImpl<ISD::OutputArg> &Outs) {
1939 unsigned NumOps = Outs.size();
1940 for (unsigned i = 0; i != NumOps; ++i) {
1941 MVT ArgVT = Outs[i].VT;
1942 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
1943 bool R;
1944
1945 if (Outs[i].IsFixed)
1946 R = CC_MipsN(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
1947 else
1948 R = CC_MipsN_VarArg(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
1949
Akira Hatanaka49617092011-11-14 19:02:54 +00001950 if (R) {
Benjamin Kramer6296ee32011-11-14 19:51:48 +00001951#ifndef NDEBUG
Akira Hatanaka49617092011-11-14 19:02:54 +00001952 dbgs() << "Call operand #" << i << " has unhandled type "
1953 << EVT(ArgVT).getEVTString();
1954#endif
1955 llvm_unreachable(0);
1956 }
1957 }
1958}
1959
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001960//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001961// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001962//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001963
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001964static const unsigned O32IntRegsSize = 4;
1965
1966static const unsigned O32IntRegs[] = {
1967 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1968};
1969
Akira Hatanaka373e3a42011-09-23 00:58:33 +00001970// Return next O32 integer argument register.
1971static unsigned getNextIntArgReg(unsigned Reg) {
1972 assert((Reg == Mips::A0) || (Reg == Mips::A2));
1973 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
1974}
1975
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001976// Write ByVal Arg to arg registers and stack.
1977static void
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001978WriteByValArg(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001979 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1980 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1981 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001982 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001983 MVT PtrType, bool isLittle) {
1984 unsigned LocMemOffset = VA.getLocMemOffset();
1985 unsigned Offset = 0;
1986 uint32_t RemainingSize = Flags.getByValSize();
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001987 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001988
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001989 // Copy the first 4 words of byval arg to registers A0 - A3.
1990 // FIXME: Use a stricter alignment if it enables better optimization in passes
1991 // run later.
1992 for (; RemainingSize >= 4 && LocMemOffset < 4 * 4;
1993 Offset += 4, RemainingSize -= 4, LocMemOffset += 4) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001994 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001995 DAG.getConstant(Offset, MVT::i32));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001996 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1997 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001998 false, false, false, std::min(ByValAlign,
1999 (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002000 MemOpChains.push_back(LoadVal.getValue(1));
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002001 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002002 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
2003 }
2004
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002005 if (RemainingSize == 0)
2006 return;
2007
2008 // If there still is a register available for argument passing, write the
2009 // remaining part of the structure to it using subword loads and shifts.
2010 if (LocMemOffset < 4 * 4) {
2011 assert(RemainingSize <= 3 && RemainingSize >= 1 &&
2012 "There must be one to three bytes remaining.");
2013 unsigned LoadSize = (RemainingSize == 3 ? 2 : RemainingSize);
2014 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2015 DAG.getConstant(Offset, MVT::i32));
2016 unsigned Alignment = std::min(ByValAlign, (unsigned )4);
2017 SDValue LoadVal = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2018 LoadPtr, MachinePointerInfo(),
2019 MVT::getIntegerVT(LoadSize * 8), false,
2020 false, Alignment);
2021 MemOpChains.push_back(LoadVal.getValue(1));
2022
2023 // If target is big endian, shift it to the most significant half-word or
2024 // byte.
2025 if (!isLittle)
2026 LoadVal = DAG.getNode(ISD::SHL, dl, MVT::i32, LoadVal,
2027 DAG.getConstant(32 - LoadSize * 8, MVT::i32));
2028
2029 Offset += LoadSize;
2030 RemainingSize -= LoadSize;
2031
2032 // Read second subword if necessary.
2033 if (RemainingSize != 0) {
2034 assert(RemainingSize == 1 && "There must be one byte remaining.");
2035 LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2036 DAG.getConstant(Offset, MVT::i32));
2037 unsigned Alignment = std::min(ByValAlign, (unsigned )2);
2038 SDValue Subword = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2039 LoadPtr, MachinePointerInfo(),
2040 MVT::i8, false, false, Alignment);
2041 MemOpChains.push_back(Subword.getValue(1));
2042 // Insert the loaded byte to LoadVal.
2043 // FIXME: Use INS if supported by target.
2044 unsigned ShiftAmt = isLittle ? 16 : 8;
2045 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i32, Subword,
2046 DAG.getConstant(ShiftAmt, MVT::i32));
2047 LoadVal = DAG.getNode(ISD::OR, dl, MVT::i32, LoadVal, Shift);
2048 }
2049
2050 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
2051 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
2052 return;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002053 }
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002054
2055 // Create a fixed object on stack at offset LocMemOffset and copy
2056 // remaining part of byval arg to it using memcpy.
2057 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2058 DAG.getConstant(Offset, MVT::i32));
2059 LastFI = MFI->CreateFixedObject(RemainingSize, LocMemOffset, true);
2060 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002061 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2062 DAG.getConstant(RemainingSize, MVT::i32),
2063 std::min(ByValAlign, (unsigned)4),
2064 /*isVolatile=*/false, /*AlwaysInline=*/false,
2065 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002066}
2067
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002068// Copy Mips64 byVal arg to registers and stack.
2069void static
2070PassByValArg64(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
2071 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
2072 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
2073 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
2074 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2075 EVT PtrTy, bool isLittle) {
2076 unsigned ByValSize = Flags.getByValSize();
2077 unsigned Alignment = std::min(Flags.getByValAlign(), (unsigned)8);
2078 bool IsRegLoc = VA.isRegLoc();
2079 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
2080 unsigned LocMemOffset = 0;
Akira Hatanaka16040852011-11-15 18:42:25 +00002081 unsigned MemCpySize = ByValSize;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002082
2083 if (!IsRegLoc)
2084 LocMemOffset = VA.getLocMemOffset();
2085 else {
2086 const unsigned *Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8,
2087 VA.getLocReg());
2088 const unsigned *RegEnd = Mips64IntRegs + 8;
2089
2090 // Copy double words to registers.
2091 for (; (Reg != RegEnd) && (ByValSize >= Offset + 8); ++Reg, Offset += 8) {
2092 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2093 DAG.getConstant(Offset, PtrTy));
2094 SDValue LoadVal = DAG.getLoad(MVT::i64, dl, Chain, LoadPtr,
2095 MachinePointerInfo(), false, false, false,
2096 Alignment);
2097 MemOpChains.push_back(LoadVal.getValue(1));
2098 RegsToPass.push_back(std::make_pair(*Reg, LoadVal));
2099 }
2100
Akira Hatanaka16040852011-11-15 18:42:25 +00002101 // Return if the struct has been fully copied.
2102 if (!(MemCpySize = ByValSize - Offset))
2103 return;
2104
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002105 // If there is an argument register available, copy the remainder of the
2106 // byval argument with sub-doubleword loads and shifts.
Akira Hatanaka16040852011-11-15 18:42:25 +00002107 if (Reg != RegEnd) {
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002108 assert((ByValSize < Offset + 8) &&
2109 "Size of the remainder should be smaller than 8-byte.");
2110 SDValue Val;
2111 for (unsigned LoadSize = 4; Offset < ByValSize; LoadSize /= 2) {
2112 unsigned RemSize = ByValSize - Offset;
2113
2114 if (RemSize < LoadSize)
2115 continue;
2116
2117 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2118 DAG.getConstant(Offset, PtrTy));
2119 SDValue LoadVal =
2120 DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i64, Chain, LoadPtr,
2121 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
2122 false, false, Alignment);
2123 MemOpChains.push_back(LoadVal.getValue(1));
2124
2125 // Offset in number of bits from double word boundary.
2126 unsigned OffsetDW = (Offset % 8) * 8;
2127 unsigned Shamt = isLittle ? OffsetDW : 64 - (OffsetDW + LoadSize * 8);
2128 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i64, LoadVal,
2129 DAG.getConstant(Shamt, MVT::i32));
2130
2131 Val = Val.getNode() ? DAG.getNode(ISD::OR, dl, MVT::i64, Val, Shift) :
2132 Shift;
2133 Offset += LoadSize;
2134 Alignment = std::min(Alignment, LoadSize);
2135 }
2136
2137 RegsToPass.push_back(std::make_pair(*Reg, Val));
2138 return;
2139 }
2140 }
2141
Akira Hatanaka16040852011-11-15 18:42:25 +00002142 assert(MemCpySize && "MemCpySize must not be zero.");
2143
2144 // Create a fixed object on stack at offset LocMemOffset and copy
2145 // remainder of byval arg to it with memcpy.
2146 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2147 DAG.getConstant(Offset, PtrTy));
2148 LastFI = MFI->CreateFixedObject(MemCpySize, LocMemOffset, true);
2149 SDValue Dst = DAG.getFrameIndex(LastFI, PtrTy);
2150 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2151 DAG.getConstant(MemCpySize, PtrTy), Alignment,
2152 /*isVolatile=*/false, /*AlwaysInline=*/false,
2153 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002154}
2155
Dan Gohman98ca4f22009-08-05 01:29:28 +00002156/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002157/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002158/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002159SDValue
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002160MipsTargetLowering::LowerCall(SDValue InChain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002161 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002162 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002163 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002164 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002165 const SmallVectorImpl<ISD::InputArg> &Ins,
2166 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002167 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002168 // MIPs target does not yet support tail call optimization.
2169 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002170
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002171 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002172 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002173 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002174 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00002175 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002176
2177 // Analyze operands of the call, assigning locations to each operand.
2178 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002179 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2180 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002181
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002182 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002183 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanaka49617092011-11-14 19:02:54 +00002184 else if (HasMips64)
2185 AnalyzeMips64CallOperands(CCInfo, Outs);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00002186 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002187 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002188
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002189 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002190 unsigned NextStackOffset = CCInfo.getNextStackOffset();
2191
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002192 // Chain is the output chain of the last Load/Store or CopyToReg node.
2193 // ByValChain is the output chain of the last Memcpy node created for copying
2194 // byval arguments to the stack.
2195 SDValue Chain, CallSeqStart, ByValChain;
2196 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
2197 Chain = CallSeqStart = DAG.getCALLSEQ_START(InChain, NextStackOffsetVal);
2198 ByValChain = InChain;
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002199
2200 // If this is the first call, create a stack frame object that points to
2201 // a location to which .cprestore saves $gp.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002202 if (IsO32 && IsPIC && !MipsFI->getGPFI())
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002203 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
2204
Akira Hatanaka21afc632011-06-21 00:40:49 +00002205 // Get the frame index of the stack frame object that points to the location
2206 // of dynamically allocated area on the stack.
2207 int DynAllocFI = MipsFI->getDynAllocFI();
2208
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002209 // Update size of the maximum argument space.
2210 // For O32, a minimum of four words (16 bytes) of argument space is
2211 // allocated.
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002212 if (IsO32)
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002213 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
2214
2215 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
2216
2217 if (MaxCallFrameSize < NextStackOffset) {
2218 MipsFI->setMaxCallFrameSize(NextStackOffset);
2219
Akira Hatanaka21afc632011-06-21 00:40:49 +00002220 // Set the offsets relative to $sp of the $gp restore slot and dynamically
2221 // allocated stack space. These offsets must be aligned to a boundary
2222 // determined by the stack alignment of the ABI.
2223 unsigned StackAlignment = TFL->getStackAlignment();
2224 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
2225 StackAlignment * StackAlignment;
2226
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002227 if (MipsFI->needGPSaveRestore())
Akira Hatanaka21afc632011-06-21 00:40:49 +00002228 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
2229
2230 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002231 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002232
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002233 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002234 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
2235 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002236
Eric Christopher471e4222011-06-08 23:55:35 +00002237 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00002238
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002239 // Walk the register/memloc assignments, inserting copies/loads.
2240 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002241 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002242 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002243 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002244 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2245
2246 // ByVal Arg.
2247 if (Flags.isByVal()) {
2248 assert(Flags.getByValSize() &&
2249 "ByVal args of size 0 should have been ignored by front-end.");
2250 if (IsO32)
2251 WriteByValArg(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2252 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2253 Subtarget->isLittle());
2254 else
2255 PassByValArg64(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2256 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2257 Subtarget->isLittle());
2258 continue;
2259 }
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002260
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002261 // Promote the value if needed.
2262 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002263 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002264 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002265 if (VA.isRegLoc()) {
2266 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
2267 (ValVT == MVT::f64 && LocVT == MVT::i64))
2268 Arg = DAG.getNode(ISD::BITCAST, dl, LocVT, Arg);
2269 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002270 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2271 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002272 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2273 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002274 if (!Subtarget->isLittle())
2275 std::swap(Lo, Hi);
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002276 unsigned LocRegLo = VA.getLocReg();
2277 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2278 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2279 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002280 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002281 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002282 }
2283 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002284 case CCValAssign::SExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002285 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002286 break;
2287 case CCValAssign::ZExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002288 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002289 break;
2290 case CCValAssign::AExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002291 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002292 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002293 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002294
2295 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002296 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002297 if (VA.isRegLoc()) {
2298 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002299 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002300 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002301
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002302 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002303 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002304
Chris Lattnere0b12152008-03-17 06:57:02 +00002305 // Create the frame index object for this incoming parameter
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002306 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002307 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00002308 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00002309
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002310 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002311 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00002312 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
2313 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00002314 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002315 }
2316
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002317 // Extend range of indices of frame objects for outgoing arguments that were
2318 // created during this function call. Skip this step if no such objects were
2319 // created.
2320 if (LastFI)
2321 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2322
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002323 // If a memcpy has been created to copy a byval arg to a stack, replace the
2324 // chain input of CallSeqStart with ByValChain.
2325 if (InChain != ByValChain)
2326 DAG.UpdateNodeOperands(CallSeqStart.getNode(), ByValChain,
2327 NextStackOffsetVal);
2328
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002329 // Transform all store nodes into one single node because all store
2330 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002331 if (!MemOpChains.empty())
2332 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002333 &MemOpChains[0], MemOpChains.size());
2334
Bill Wendling056292f2008-09-16 21:48:12 +00002335 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002336 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2337 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002338 unsigned char OpFlag;
2339 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002340 bool GlobalOrExternal = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002341 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002342
2343 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002344 if (IsPICCall && G->getGlobal()->hasInternalLinkage()) {
2345 OpFlag = IsO32 ? MipsII::MO_GOT : MipsII::MO_GOT_PAGE;
2346 unsigned char LoFlag = IsO32 ? MipsII::MO_ABS_LO : MipsII::MO_GOT_OFST;
2347 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(), 0,
2348 OpFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002349 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002350 0, LoFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002351 } else {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002352 OpFlag = IsPICCall ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002353 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2354 getPointerTy(), 0, OpFlag);
2355 }
2356
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002357 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002358 }
2359 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002360 if (IsN64 || (!IsO32 && IsPIC))
2361 OpFlag = MipsII::MO_GOT_DISP;
2362 else if (!IsPIC) // !N64 && static
2363 OpFlag = MipsII::MO_NO_FLAG;
2364 else // O32 & PIC
2365 OpFlag = MipsII::MO_GOT_CALL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002366 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002367 getPointerTy(), OpFlag);
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002368 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002369 }
2370
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002371 SDValue InFlag;
2372
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002373 // Create nodes that load address of callee and copy it to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002374 if (IsPICCall) {
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002375 if (GlobalOrExternal) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002376 // Load callee address
Akira Hatanaka6df7e232011-12-09 01:53:17 +00002377 Callee = DAG.getNode(MipsISD::Wrapper, dl, getPointerTy(), Callee);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002378 SDValue LoadValue = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
2379 Callee, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002380 false, false, false, 0);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002381
2382 // Use GOT+LO if callee has internal linkage.
2383 if (CalleeLo.getNode()) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002384 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, getPointerTy(), CalleeLo);
2385 Callee = DAG.getNode(ISD::ADD, dl, getPointerTy(), LoadValue, Lo);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002386 } else
2387 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002388 }
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002389 }
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002390
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002391 // T9 should contain the address of the callee function if
2392 // -reloction-model=pic or it is an indirect call.
2393 if (IsPICCall || !GlobalOrExternal) {
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002394 // copy to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002395 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
2396 Chain = DAG.getCopyToReg(Chain, dl, T9Reg, Callee, SDValue(0, 0));
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002397 InFlag = Chain.getValue(1);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002398 Callee = DAG.getRegister(T9Reg, getPointerTy());
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002399 }
Bill Wendling056292f2008-09-16 21:48:12 +00002400
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002401 // Build a sequence of copy-to-reg nodes chained together with token
2402 // chain and flag operands which copy the outgoing args into registers.
2403 // The InFlag in necessary since all emitted instructions must be
2404 // stuck together.
2405 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2406 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2407 RegsToPass[i].second, InFlag);
2408 InFlag = Chain.getValue(1);
2409 }
2410
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002411 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002412 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002413 //
2414 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002415 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002416 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002417 Ops.push_back(Chain);
2418 Ops.push_back(Callee);
2419
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002420 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002421 // known live into the call.
2422 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2423 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2424 RegsToPass[i].second.getValueType()));
2425
Gabor Greifba36cb52008-08-28 21:40:38 +00002426 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002427 Ops.push_back(InFlag);
2428
Dale Johannesen33c960f2009-02-04 20:06:27 +00002429 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002430 InFlag = Chain.getValue(1);
2431
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002432 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002433 Chain = DAG.getCALLSEQ_END(Chain,
2434 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002435 DAG.getIntPtrConstant(0, true), InFlag);
2436 InFlag = Chain.getValue(1);
2437
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002438 // Handle result values, copying them out of physregs into vregs that we
2439 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002440 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2441 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002442}
2443
Dan Gohman98ca4f22009-08-05 01:29:28 +00002444/// LowerCallResult - Lower the result values of a call into the
2445/// appropriate copies out of appropriate physical registers.
2446SDValue
2447MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002448 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002449 const SmallVectorImpl<ISD::InputArg> &Ins,
2450 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002451 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002452 // Assign locations to each value returned by this call.
2453 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002454 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2455 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002456
Dan Gohman98ca4f22009-08-05 01:29:28 +00002457 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002458
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002459 // Copy all of the result registers out of their specified physreg.
2460 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002461 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002462 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002463 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002464 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002465 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002466
Dan Gohman98ca4f22009-08-05 01:29:28 +00002467 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002468}
2469
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002470//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002471// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002472//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002473static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2474 std::vector<SDValue>& OutChains,
2475 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2476 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2477 unsigned LocMem = VA.getLocMemOffset();
2478 unsigned FirstWord = LocMem / 4;
2479
2480 // copy register A0 - A3 to frame object
2481 for (unsigned i = 0; i < NumWords; ++i) {
2482 unsigned CurWord = FirstWord + i;
2483 if (CurWord >= O32IntRegsSize)
2484 break;
2485
2486 unsigned SrcReg = O32IntRegs[CurWord];
2487 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2488 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2489 DAG.getConstant(i * 4, MVT::i32));
2490 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2491 StorePtr, MachinePointerInfo(), false,
2492 false, 0);
2493 OutChains.push_back(Store);
2494 }
2495}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002496
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002497// Create frame object on stack and copy registers used for byval passing to it.
2498static unsigned
2499CopyMips64ByValRegs(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2500 std::vector<SDValue>& OutChains, SelectionDAG &DAG,
2501 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2502 MachineFrameInfo *MFI, bool IsRegLoc,
2503 SmallVectorImpl<SDValue> &InVals, MipsFunctionInfo *MipsFI,
2504 EVT PtrTy) {
2505 const unsigned *Reg = Mips64IntRegs + 8;
2506 int FOOffset; // Frame object offset from virtual frame pointer.
2507
2508 if (IsRegLoc) {
2509 Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8, VA.getLocReg());
2510 FOOffset = (Reg - Mips64IntRegs) * 8 - 8 * 8;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002511 }
2512 else
2513 FOOffset = VA.getLocMemOffset();
2514
2515 // Create frame object.
2516 unsigned NumRegs = (Flags.getByValSize() + 7) / 8;
2517 unsigned LastFI = MFI->CreateFixedObject(NumRegs * 8, FOOffset, true);
2518 SDValue FIN = DAG.getFrameIndex(LastFI, PtrTy);
2519 InVals.push_back(FIN);
2520
2521 // Copy arg registers.
2522 for (unsigned I = 0; (Reg != Mips64IntRegs + 8) && (I < NumRegs);
2523 ++Reg, ++I) {
2524 unsigned VReg = AddLiveIn(MF, *Reg, Mips::CPU64RegsRegisterClass);
2525 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, PtrTy, FIN,
2526 DAG.getConstant(I * 8, PtrTy));
2527 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(VReg, MVT::i64),
2528 StorePtr, MachinePointerInfo(), false,
2529 false, 0);
2530 OutChains.push_back(Store);
2531 }
2532
2533 return LastFI;
2534}
2535
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002536/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002537/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002538SDValue
2539MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002540 CallingConv::ID CallConv,
2541 bool isVarArg,
2542 const SmallVectorImpl<ISD::InputArg>
2543 &Ins,
2544 DebugLoc dl, SelectionDAG &DAG,
2545 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002546 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002547 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002548 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002549 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002550
Dan Gohman1e93df62010-04-17 14:41:14 +00002551 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002552
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002553 // Used with vargs to acumulate store chains.
2554 std::vector<SDValue> OutChains;
2555
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002556 // Assign locations to all of the incoming arguments.
2557 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002558 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2559 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002560
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002561 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002562 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002563 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002564 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002565
Akira Hatanaka43299772011-05-20 23:22:14 +00002566 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002567
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002568 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002569 CCValAssign &VA = ArgLocs[i];
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002570 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002571 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2572 bool IsRegLoc = VA.isRegLoc();
2573
2574 if (Flags.isByVal()) {
2575 assert(Flags.getByValSize() &&
2576 "ByVal args of size 0 should have been ignored by front-end.");
2577 if (IsO32) {
2578 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2579 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2580 true);
2581 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2582 InVals.push_back(FIN);
2583 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2584 } else // N32/64
2585 LastFI = CopyMips64ByValRegs(MF, Chain, dl, OutChains, DAG, VA, Flags,
2586 MFI, IsRegLoc, InVals, MipsFI,
2587 getPointerTy());
2588 continue;
2589 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002590
2591 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002592 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002593 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002594 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002595 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002596
Owen Anderson825b72b2009-08-11 20:47:22 +00002597 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002598 RC = Mips::CPURegsRegisterClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002599 else if (RegVT == MVT::i64)
2600 RC = Mips::CPU64RegsRegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002601 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002602 RC = Mips::FGR32RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002603 else if (RegVT == MVT::f64)
Akira Hatanakaf40de9d2011-09-26 21:55:17 +00002604 RC = HasMips64 ? Mips::FGR64RegisterClass : Mips::AFGR64RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002605 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002606 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002607
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002608 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002609 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002610 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002611 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002612
2613 // If this is an 8 or 16-bit value, it has been passed promoted
2614 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002615 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002616 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002617 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002618 if (VA.getLocInfo() == CCValAssign::SExt)
2619 Opcode = ISD::AssertSext;
2620 else if (VA.getLocInfo() == CCValAssign::ZExt)
2621 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002622 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002623 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002624 DAG.getValueType(ValVT));
2625 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002626 }
2627
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002628 // Handle floating point arguments passed in integer registers.
2629 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
2630 (RegVT == MVT::i64 && ValVT == MVT::f64))
2631 ArgValue = DAG.getNode(ISD::BITCAST, dl, ValVT, ArgValue);
2632 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
2633 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
2634 getNextIntArgReg(ArgReg), RC);
2635 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
2636 if (!Subtarget->isLittle())
2637 std::swap(ArgValue, ArgValue2);
2638 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2639 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002640 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002641
Dan Gohman98ca4f22009-08-05 01:29:28 +00002642 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002643 } else { // VA.isRegLoc()
2644
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002645 // sanity check
2646 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002647
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002648 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002649 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002650 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002651
2652 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002653 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002654 InVals.push_back(DAG.getLoad(ValVT, dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002655 MachinePointerInfo::getFixedStack(LastFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002656 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002657 }
2658 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002659
2660 // The mips ABIs for returning structs by value requires that we copy
2661 // the sret argument into $v0 for the return. Save the argument into
2662 // a virtual register so that we can access it from the return points.
2663 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2664 unsigned Reg = MipsFI->getSRetReturnReg();
2665 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002666 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002667 MipsFI->setSRetReturnReg(Reg);
2668 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002669 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002670 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002671 }
2672
Akira Hatanakabad53f42011-11-14 19:01:09 +00002673 if (isVarArg) {
2674 unsigned NumOfRegs = IsO32 ? 4 : 8;
2675 const unsigned *ArgRegs = IsO32 ? O32IntRegs : Mips64IntRegs;
2676 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumOfRegs);
2677 int FirstRegSlotOffset = IsO32 ? 0 : -64 ; // offset of $a0's slot.
2678 TargetRegisterClass *RC
2679 = IsO32 ? Mips::CPURegsRegisterClass : Mips::CPU64RegsRegisterClass;
2680 unsigned RegSize = RC->getSize();
2681 int RegSlotOffset = FirstRegSlotOffset + Idx * RegSize;
2682
2683 // Offset of the first variable argument from stack pointer.
2684 int FirstVaArgOffset;
2685
2686 if (IsO32 || (Idx == NumOfRegs)) {
2687 FirstVaArgOffset =
2688 (CCInfo.getNextStackOffset() + RegSize - 1) / RegSize * RegSize;
2689 } else
2690 FirstVaArgOffset = RegSlotOffset;
2691
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002692 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002693 // which is a value necessary to VASTART.
Akira Hatanakabad53f42011-11-14 19:01:09 +00002694 LastFI = MFI->CreateFixedObject(RegSize, FirstVaArgOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002695 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002696
Akira Hatanakabad53f42011-11-14 19:01:09 +00002697 // Copy the integer registers that have not been used for argument passing
2698 // to the argument register save area. For O32, the save area is allocated
2699 // in the caller's stack frame, while for N32/64, it is allocated in the
2700 // callee's stack frame.
2701 for (int StackOffset = RegSlotOffset;
2702 Idx < NumOfRegs; ++Idx, StackOffset += RegSize) {
2703 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegs[Idx], RC);
2704 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2705 MVT::getIntegerVT(RegSize * 8));
2706 LastFI = MFI->CreateFixedObject(RegSize, StackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002707 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2708 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2709 MachinePointerInfo(),
2710 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002711 }
2712 }
2713
Akira Hatanaka43299772011-05-20 23:22:14 +00002714 MipsFI->setLastInArgFI(LastFI);
2715
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002716 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002717 // the size of Ins and InVals. This only happens when on varg functions
2718 if (!OutChains.empty()) {
2719 OutChains.push_back(Chain);
2720 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2721 &OutChains[0], OutChains.size());
2722 }
2723
Dan Gohman98ca4f22009-08-05 01:29:28 +00002724 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002725}
2726
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002727//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002728// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002729//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002730
Dan Gohman98ca4f22009-08-05 01:29:28 +00002731SDValue
2732MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002733 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002734 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002735 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002736 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002737
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002738 // CCValAssign - represent the assignment of
2739 // the return value to a location
2740 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002741
2742 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002743 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2744 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002745
Dan Gohman98ca4f22009-08-05 01:29:28 +00002746 // Analize return values.
2747 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002748
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002749 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002750 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002751 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002752 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002753 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002754 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002755 }
2756
Dan Gohman475871a2008-07-27 21:46:04 +00002757 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002758
2759 // Copy the result values into the output registers.
2760 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2761 CCValAssign &VA = RVLocs[i];
2762 assert(VA.isRegLoc() && "Can only return in registers!");
2763
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002764 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002765 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002766
2767 // guarantee that all emitted copies are
2768 // stuck together, avoiding something bad
2769 Flag = Chain.getValue(1);
2770 }
2771
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002772 // The mips ABIs for returning structs by value requires that we copy
2773 // the sret argument into $v0 for the return. We saved the argument into
2774 // a virtual register in the entry block, so now we copy the value out
2775 // and into $v0.
2776 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2777 MachineFunction &MF = DAG.getMachineFunction();
2778 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2779 unsigned Reg = MipsFI->getSRetReturnReg();
2780
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002781 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002782 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002783 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002784
Dale Johannesena05dca42009-02-04 23:02:30 +00002785 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002786 Flag = Chain.getValue(1);
2787 }
2788
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002789 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002790 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002791 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002792 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002793 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002794 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002795 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002796}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002797
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002798//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002799// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002800//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002801
2802/// getConstraintType - Given a constraint letter, return the type of
2803/// constraint it is for this target.
2804MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002805getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002806{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002807 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002808 // GCC config/mips/constraints.md
2809 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002810 // 'd' : An address register. Equivalent to r
2811 // unless generating MIPS16 code.
2812 // 'y' : Equivalent to r; retained for
2813 // backwards compatibility.
2814 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002815 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002816 switch (Constraint[0]) {
2817 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002818 case 'd':
2819 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002820 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002821 return C_RegisterClass;
2822 break;
2823 }
2824 }
2825 return TargetLowering::getConstraintType(Constraint);
2826}
2827
John Thompson44ab89e2010-10-29 17:29:13 +00002828/// Examine constraint type and operand type and determine a weight value.
2829/// This object must already have been set up with the operand type
2830/// and the current alternative constraint selected.
2831TargetLowering::ConstraintWeight
2832MipsTargetLowering::getSingleConstraintMatchWeight(
2833 AsmOperandInfo &info, const char *constraint) const {
2834 ConstraintWeight weight = CW_Invalid;
2835 Value *CallOperandVal = info.CallOperandVal;
2836 // If we don't have a value, we can't do a match,
2837 // but allow it at the lowest weight.
2838 if (CallOperandVal == NULL)
2839 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002840 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002841 // Look at the constraint type.
2842 switch (*constraint) {
2843 default:
2844 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2845 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002846 case 'd':
2847 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002848 if (type->isIntegerTy())
2849 weight = CW_Register;
2850 break;
2851 case 'f':
2852 if (type->isFloatTy())
2853 weight = CW_Register;
2854 break;
2855 }
2856 return weight;
2857}
2858
Eric Christopher38d64262011-06-29 19:33:04 +00002859/// Given a register class constraint, like 'r', if this corresponds directly
2860/// to an LLVM register class, return a register of 0 and the register class
2861/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002862std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002863getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002864{
2865 if (Constraint.size() == 1) {
2866 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002867 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2868 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002869 case 'r':
2870 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002871 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002872 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002873 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002874 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002875 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2876 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002877 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002878 }
2879 }
2880 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2881}
2882
Dan Gohman6520e202008-10-18 02:06:02 +00002883bool
2884MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2885 // The Mips target isn't yet aware of offsets.
2886 return false;
2887}
Evan Chengeb2f9692009-10-27 19:56:55 +00002888
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002889bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2890 if (VT != MVT::f32 && VT != MVT::f64)
2891 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002892 if (Imm.isNegZero())
2893 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002894 return Imm.isZero();
2895}