blob: c9b657c654b6a5115e360aa2ad367a673e7d8c85 [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{
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001543 // If the relocation model is PIC, use the General Dynamic TLS Model or
1544 // Local Dynamic TLS model, otherwise use the Initial Exec or
1545 // Local Exec TLS Model.
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001546
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 Hatanaka3faac0a2011-12-14 18:26:41 +00001554 bool LocalDynamic = GV->hasInternalLinkage();
1555 unsigned Flag = LocalDynamic ? MipsII::MO_TLSLDM :MipsII::MO_TLSGD;
1556 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, Flag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001557 SDValue Argument = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, TGA);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001558 unsigned PtrSize = PtrVT.getSizeInBits();
1559 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1560
Benjamin Kramer5eccf672011-12-11 12:21:34 +00001561 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001562
1563 ArgListTy Args;
1564 ArgListEntry Entry;
1565 Entry.Node = Argument;
Akira Hatanakaca074792011-12-08 20:34:32 +00001566 Entry.Ty = PtrTy;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001567 Args.push_back(Entry);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001568
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001569 std::pair<SDValue, SDValue> CallResult =
Akira Hatanakaca074792011-12-08 20:34:32 +00001570 LowerCallTo(DAG.getEntryNode(), PtrTy,
1571 false, false, false, false, 0, CallingConv::C, false, true,
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001572 TlsGetAddr, Args, DAG, dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001573
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001574 SDValue Ret = CallResult.first;
1575
1576 if (!LocalDynamic)
1577 return Ret;
1578
1579 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1580 MipsII::MO_DTPREL_HI);
1581 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, PtrVT, TGAHi);
1582 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1583 MipsII::MO_DTPREL_LO);
1584 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, TGALo);
1585 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Ret);
1586 return DAG.getNode(ISD::ADD, dl, PtrVT, Add, Lo);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001587 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001588
1589 SDValue Offset;
1590 if (GV->isDeclaration()) {
1591 // Initial Exec TLS Model
Akira Hatanakaca074792011-12-08 20:34:32 +00001592 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001593 MipsII::MO_GOTTPREL);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001594 TGA = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, TGA);
Akira Hatanakaca074792011-12-08 20:34:32 +00001595 Offset = DAG.getLoad(PtrVT, dl,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001596 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001597 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001598 } else {
1599 // Local Exec TLS Model
Akira Hatanakaca074792011-12-08 20:34:32 +00001600 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001601 MipsII::MO_TPREL_HI);
Akira Hatanakaca074792011-12-08 20:34:32 +00001602 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001603 MipsII::MO_TPREL_LO);
Akira Hatanakaca074792011-12-08 20:34:32 +00001604 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, PtrVT, TGAHi);
1605 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, TGALo);
1606 Offset = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001607 }
1608
1609 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1610 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001611}
1612
1613SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001614LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001615{
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001616 SDValue HiPart, JTI, JTILo;
Dale Johannesende064702009-02-06 21:50:26 +00001617 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001618 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001619 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Owen Andersone50ed302009-08-10 22:56:29 +00001620 EVT PtrVT = Op.getValueType();
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001621 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001622
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001623 if (!IsPIC && !IsN64) {
1624 JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MipsII::MO_ABS_HI);
1625 HiPart = DAG.getNode(MipsISD::Hi, dl, PtrVT, JTI);
1626 JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MipsII::MO_ABS_LO);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001627 } else {// Emit Load from Global Pointer
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001628 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1629 unsigned OfstFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1630 JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, GOTFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001631 JTI = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, JTI);
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001632 HiPart = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), JTI,
1633 MachinePointerInfo(), false, false, false, 0);
1634 JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OfstFlag);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001635 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001636
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001637 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, JTILo);
1638 return DAG.getNode(ISD::ADD, dl, PtrVT, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001639}
1640
Dan Gohman475871a2008-07-27 21:46:04 +00001641SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001642LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001643{
Dan Gohman475871a2008-07-27 21:46:04 +00001644 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001645 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001646 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001647 // FIXME there isn't actually debug info here
1648 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001649
1650 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001651 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001652 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001653 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001654 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001655 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001656 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1657 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001658 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001659
1660 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001661 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001662 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001663 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001664 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001665 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1666 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001667 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001668 } else {
Akira Hatanaka620db892011-11-16 22:44:38 +00001669 EVT ValTy = Op.getValueType();
1670 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1671 unsigned OFSTFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1672 SDValue CP = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1673 N->getOffset(), GOTFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001674 CP = DAG.getNode(MipsISD::Wrapper, dl, ValTy, CP);
Akira Hatanaka620db892011-11-16 22:44:38 +00001675 SDValue Load = DAG.getLoad(ValTy, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001676 CP, MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001677 false, false, false, 0);
Akira Hatanaka620db892011-11-16 22:44:38 +00001678 SDValue CPLo = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1679 N->getOffset(), OFSTFlag);
1680 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, CPLo);
1681 ResNode = DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001682 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001683
1684 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001685}
1686
Dan Gohmand858e902010-04-17 15:26:15 +00001687SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001688 MachineFunction &MF = DAG.getMachineFunction();
1689 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1690
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001691 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001692 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1693 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001694
1695 // vastart just stores the address of the VarArgsFrameIndex slot into the
1696 // memory location argument.
1697 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001698 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1699 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +00001700 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001701}
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001702
1703// Called if the size of integer registers is large enough to hold the whole
1704// floating point number.
1705static SDValue LowerFCOPYSIGNLargeIntReg(SDValue Op, SelectionDAG &DAG) {
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001706 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001707 EVT ValTy = Op.getValueType();
1708 EVT IntValTy = MVT::getIntegerVT(ValTy.getSizeInBits());
1709 uint64_t Mask = (uint64_t)1 << (ValTy.getSizeInBits() - 1);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001710 DebugLoc dl = Op.getDebugLoc();
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001711 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, IntValTy, Op.getOperand(0));
1712 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, IntValTy, Op.getOperand(1));
1713 SDValue And0 = DAG.getNode(ISD::AND, dl, IntValTy, Op0,
1714 DAG.getConstant(Mask - 1, IntValTy));
1715 SDValue And1 = DAG.getNode(ISD::AND, dl, IntValTy, Op1,
1716 DAG.getConstant(Mask, IntValTy));
1717 SDValue Result = DAG.getNode(ISD::OR, dl, IntValTy, And0, And1);
1718 return DAG.getNode(ISD::BITCAST, dl, ValTy, Result);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001719}
1720
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001721// Called if the size of integer registers is not large enough to hold the whole
1722// floating point number (e.g. f64 & 32-bit integer register).
1723static SDValue
1724LowerFCOPYSIGNSmallIntReg(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001725 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001726 // Use ext/ins instructions if target architecture is Mips32r2.
1727 // Eliminate redundant mfc1 and mtc1 instructions.
1728 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001729
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001730 if (!isLittle)
1731 std::swap(LoIdx, HiIdx);
1732
1733 DebugLoc dl = Op.getDebugLoc();
1734 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1735 Op.getOperand(0),
1736 DAG.getConstant(LoIdx, MVT::i32));
1737 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1738 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1739 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1740 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1741 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1742 DAG.getConstant(0x7fffffff, MVT::i32));
1743 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1744 DAG.getConstant(0x80000000, MVT::i32));
1745 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1746
1747 if (!isLittle)
1748 std::swap(Word0, Word1);
1749
1750 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1751}
1752
1753SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1754 const {
1755 EVT Ty = Op.getValueType();
1756
1757 assert(Ty == MVT::f32 || Ty == MVT::f64);
1758
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001759 if (Ty == MVT::f32 || HasMips64)
1760 return LowerFCOPYSIGNLargeIntReg(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001761 else
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001762 return LowerFCOPYSIGNSmallIntReg(Op, DAG, Subtarget->isLittle());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001763}
1764
Akira Hatanaka2e591472011-06-02 00:24:44 +00001765SDValue MipsTargetLowering::
1766LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001767 // check the depth
1768 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001769 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001770
1771 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1772 MFI->setFrameAddressIsTaken(true);
1773 EVT VT = Op.getValueType();
1774 DebugLoc dl = Op.getDebugLoc();
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001775 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
1776 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001777 return FrameAddr;
1778}
1779
Akira Hatanakadb548262011-07-19 23:30:50 +00001780// TODO: set SType according to the desired memory barrier behavior.
1781SDValue MipsTargetLowering::LowerMEMBARRIER(SDValue Op,
1782 SelectionDAG& DAG) const {
1783 unsigned SType = 0;
1784 DebugLoc dl = Op.getDebugLoc();
1785 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1786 DAG.getConstant(SType, MVT::i32));
1787}
1788
Eli Friedman14648462011-07-27 22:21:52 +00001789SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1790 SelectionDAG& DAG) const {
1791 // FIXME: Need pseudo-fence for 'singlethread' fences
1792 // FIXME: Set SType for weaker fences where supported/appropriate.
1793 unsigned SType = 0;
1794 DebugLoc dl = Op.getDebugLoc();
1795 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1796 DAG.getConstant(SType, MVT::i32));
1797}
1798
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001799//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001800// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001801//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001802
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001803//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001804// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001805// Mips O32 ABI rules:
1806// ---
1807// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001808// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001809// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001810// f64 - Only passed in two aliased f32 registers if no int reg has been used
1811// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001812// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1813// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001814//
1815// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001816//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001817
Duncan Sands1e96bab2010-11-04 10:49:57 +00001818static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001819 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001820 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1821
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001822 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001823
1824 static const unsigned IntRegs[] = {
1825 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1826 };
1827 static const unsigned F32Regs[] = {
1828 Mips::F12, Mips::F14
1829 };
1830 static const unsigned F64Regs[] = {
1831 Mips::D6, Mips::D7
1832 };
1833
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001834 // ByVal Args
1835 if (ArgFlags.isByVal()) {
1836 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1837 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1838 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1839 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1840 r < std::min(IntRegsSize, NextReg); ++r)
1841 State.AllocateReg(IntRegs[r]);
1842 return false;
1843 }
1844
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001845 // Promote i8 and i16
1846 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1847 LocVT = MVT::i32;
1848 if (ArgFlags.isSExt())
1849 LocInfo = CCValAssign::SExt;
1850 else if (ArgFlags.isZExt())
1851 LocInfo = CCValAssign::ZExt;
1852 else
1853 LocInfo = CCValAssign::AExt;
1854 }
1855
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001856 unsigned Reg;
1857
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001858 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1859 // is true: function is vararg, argument is 3rd or higher, there is previous
1860 // argument which is not f32 or f64.
1861 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1862 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001863 unsigned OrigAlign = ArgFlags.getOrigAlign();
1864 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001865
1866 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001867 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001868 // If this is the first part of an i64 arg,
1869 // the allocated register must be either A0 or A2.
1870 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1871 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001872 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001873 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1874 // Allocate int register and shadow next int register. If first
1875 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001876 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1877 if (Reg == Mips::A1 || Reg == Mips::A3)
1878 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1879 State.AllocateReg(IntRegs, IntRegsSize);
1880 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001881 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1882 // we are guaranteed to find an available float register
1883 if (ValVT == MVT::f32) {
1884 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1885 // Shadow int register
1886 State.AllocateReg(IntRegs, IntRegsSize);
1887 } else {
1888 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1889 // Shadow int registers
1890 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1891 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1892 State.AllocateReg(IntRegs, IntRegsSize);
1893 State.AllocateReg(IntRegs, IntRegsSize);
1894 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001895 } else
1896 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001897
Akira Hatanakad37776d2011-05-20 21:39:54 +00001898 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1899 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1900
1901 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001902 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001903 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001904 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001905
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001906 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001907}
1908
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00001909static const unsigned Mips64IntRegs[8] =
1910 {Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
1911 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64};
1912static const unsigned Mips64DPRegs[8] =
1913 {Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
1914 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64};
1915
1916static bool CC_Mips64Byval(unsigned ValNo, MVT ValVT, MVT LocVT,
1917 CCValAssign::LocInfo LocInfo,
1918 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1919 unsigned Align = std::max(ArgFlags.getByValAlign(), (unsigned)8);
1920 unsigned Size = (ArgFlags.getByValSize() + 7) / 8 * 8;
1921 unsigned FirstIdx = State.getFirstUnallocated(Mips64IntRegs, 8);
1922
1923 assert(Align <= 16 && "Cannot handle alignments larger than 16.");
1924
1925 // If byval is 16-byte aligned, the first arg register must be even.
1926 if ((Align == 16) && (FirstIdx % 2)) {
1927 State.AllocateReg(Mips64IntRegs[FirstIdx], Mips64DPRegs[FirstIdx]);
1928 ++FirstIdx;
1929 }
1930
1931 // Mark the registers allocated.
1932 for (unsigned I = FirstIdx; Size && (I < 8); Size -= 8, ++I)
1933 State.AllocateReg(Mips64IntRegs[I], Mips64DPRegs[I]);
1934
1935 // Allocate space on caller's stack.
1936 unsigned Offset = State.AllocateStack(Size, Align);
1937
1938 if (FirstIdx < 8)
1939 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Mips64IntRegs[FirstIdx],
1940 LocVT, LocInfo));
1941 else
1942 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
1943
1944 return true;
1945}
1946
1947#include "MipsGenCallingConv.inc"
1948
Akira Hatanaka49617092011-11-14 19:02:54 +00001949static void
1950AnalyzeMips64CallOperands(CCState CCInfo,
1951 const SmallVectorImpl<ISD::OutputArg> &Outs) {
1952 unsigned NumOps = Outs.size();
1953 for (unsigned i = 0; i != NumOps; ++i) {
1954 MVT ArgVT = Outs[i].VT;
1955 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
1956 bool R;
1957
1958 if (Outs[i].IsFixed)
1959 R = CC_MipsN(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
1960 else
1961 R = CC_MipsN_VarArg(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
1962
Akira Hatanaka49617092011-11-14 19:02:54 +00001963 if (R) {
Benjamin Kramer6296ee32011-11-14 19:51:48 +00001964#ifndef NDEBUG
Akira Hatanaka49617092011-11-14 19:02:54 +00001965 dbgs() << "Call operand #" << i << " has unhandled type "
1966 << EVT(ArgVT).getEVTString();
1967#endif
1968 llvm_unreachable(0);
1969 }
1970 }
1971}
1972
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001973//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001974// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001975//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001976
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001977static const unsigned O32IntRegsSize = 4;
1978
1979static const unsigned O32IntRegs[] = {
1980 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1981};
1982
Akira Hatanaka373e3a42011-09-23 00:58:33 +00001983// Return next O32 integer argument register.
1984static unsigned getNextIntArgReg(unsigned Reg) {
1985 assert((Reg == Mips::A0) || (Reg == Mips::A2));
1986 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
1987}
1988
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001989// Write ByVal Arg to arg registers and stack.
1990static void
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001991WriteByValArg(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001992 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1993 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1994 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001995 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001996 MVT PtrType, bool isLittle) {
1997 unsigned LocMemOffset = VA.getLocMemOffset();
1998 unsigned Offset = 0;
1999 uint32_t RemainingSize = Flags.getByValSize();
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00002000 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002001
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002002 // Copy the first 4 words of byval arg to registers A0 - A3.
2003 // FIXME: Use a stricter alignment if it enables better optimization in passes
2004 // run later.
2005 for (; RemainingSize >= 4 && LocMemOffset < 4 * 4;
2006 Offset += 4, RemainingSize -= 4, LocMemOffset += 4) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002007 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002008 DAG.getConstant(Offset, MVT::i32));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002009 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
2010 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002011 false, false, false, std::min(ByValAlign,
2012 (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002013 MemOpChains.push_back(LoadVal.getValue(1));
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002014 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002015 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
2016 }
2017
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002018 if (RemainingSize == 0)
2019 return;
2020
2021 // If there still is a register available for argument passing, write the
2022 // remaining part of the structure to it using subword loads and shifts.
2023 if (LocMemOffset < 4 * 4) {
2024 assert(RemainingSize <= 3 && RemainingSize >= 1 &&
2025 "There must be one to three bytes remaining.");
2026 unsigned LoadSize = (RemainingSize == 3 ? 2 : RemainingSize);
2027 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2028 DAG.getConstant(Offset, MVT::i32));
2029 unsigned Alignment = std::min(ByValAlign, (unsigned )4);
2030 SDValue LoadVal = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2031 LoadPtr, MachinePointerInfo(),
2032 MVT::getIntegerVT(LoadSize * 8), false,
2033 false, Alignment);
2034 MemOpChains.push_back(LoadVal.getValue(1));
2035
2036 // If target is big endian, shift it to the most significant half-word or
2037 // byte.
2038 if (!isLittle)
2039 LoadVal = DAG.getNode(ISD::SHL, dl, MVT::i32, LoadVal,
2040 DAG.getConstant(32 - LoadSize * 8, MVT::i32));
2041
2042 Offset += LoadSize;
2043 RemainingSize -= LoadSize;
2044
2045 // Read second subword if necessary.
2046 if (RemainingSize != 0) {
2047 assert(RemainingSize == 1 && "There must be one byte remaining.");
2048 LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2049 DAG.getConstant(Offset, MVT::i32));
2050 unsigned Alignment = std::min(ByValAlign, (unsigned )2);
2051 SDValue Subword = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2052 LoadPtr, MachinePointerInfo(),
2053 MVT::i8, false, false, Alignment);
2054 MemOpChains.push_back(Subword.getValue(1));
2055 // Insert the loaded byte to LoadVal.
2056 // FIXME: Use INS if supported by target.
2057 unsigned ShiftAmt = isLittle ? 16 : 8;
2058 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i32, Subword,
2059 DAG.getConstant(ShiftAmt, MVT::i32));
2060 LoadVal = DAG.getNode(ISD::OR, dl, MVT::i32, LoadVal, Shift);
2061 }
2062
2063 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
2064 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
2065 return;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002066 }
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002067
2068 // Create a fixed object on stack at offset LocMemOffset and copy
2069 // remaining part of byval arg to it using memcpy.
2070 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2071 DAG.getConstant(Offset, MVT::i32));
2072 LastFI = MFI->CreateFixedObject(RemainingSize, LocMemOffset, true);
2073 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002074 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2075 DAG.getConstant(RemainingSize, MVT::i32),
2076 std::min(ByValAlign, (unsigned)4),
2077 /*isVolatile=*/false, /*AlwaysInline=*/false,
2078 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002079}
2080
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002081// Copy Mips64 byVal arg to registers and stack.
2082void static
2083PassByValArg64(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
2084 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
2085 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
2086 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
2087 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2088 EVT PtrTy, bool isLittle) {
2089 unsigned ByValSize = Flags.getByValSize();
2090 unsigned Alignment = std::min(Flags.getByValAlign(), (unsigned)8);
2091 bool IsRegLoc = VA.isRegLoc();
2092 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
2093 unsigned LocMemOffset = 0;
Akira Hatanaka16040852011-11-15 18:42:25 +00002094 unsigned MemCpySize = ByValSize;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002095
2096 if (!IsRegLoc)
2097 LocMemOffset = VA.getLocMemOffset();
2098 else {
2099 const unsigned *Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8,
2100 VA.getLocReg());
2101 const unsigned *RegEnd = Mips64IntRegs + 8;
2102
2103 // Copy double words to registers.
2104 for (; (Reg != RegEnd) && (ByValSize >= Offset + 8); ++Reg, Offset += 8) {
2105 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2106 DAG.getConstant(Offset, PtrTy));
2107 SDValue LoadVal = DAG.getLoad(MVT::i64, dl, Chain, LoadPtr,
2108 MachinePointerInfo(), false, false, false,
2109 Alignment);
2110 MemOpChains.push_back(LoadVal.getValue(1));
2111 RegsToPass.push_back(std::make_pair(*Reg, LoadVal));
2112 }
2113
Akira Hatanaka16040852011-11-15 18:42:25 +00002114 // Return if the struct has been fully copied.
2115 if (!(MemCpySize = ByValSize - Offset))
2116 return;
2117
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002118 // If there is an argument register available, copy the remainder of the
2119 // byval argument with sub-doubleword loads and shifts.
Akira Hatanaka16040852011-11-15 18:42:25 +00002120 if (Reg != RegEnd) {
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002121 assert((ByValSize < Offset + 8) &&
2122 "Size of the remainder should be smaller than 8-byte.");
2123 SDValue Val;
2124 for (unsigned LoadSize = 4; Offset < ByValSize; LoadSize /= 2) {
2125 unsigned RemSize = ByValSize - Offset;
2126
2127 if (RemSize < LoadSize)
2128 continue;
2129
2130 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2131 DAG.getConstant(Offset, PtrTy));
2132 SDValue LoadVal =
2133 DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i64, Chain, LoadPtr,
2134 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
2135 false, false, Alignment);
2136 MemOpChains.push_back(LoadVal.getValue(1));
2137
2138 // Offset in number of bits from double word boundary.
2139 unsigned OffsetDW = (Offset % 8) * 8;
2140 unsigned Shamt = isLittle ? OffsetDW : 64 - (OffsetDW + LoadSize * 8);
2141 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i64, LoadVal,
2142 DAG.getConstant(Shamt, MVT::i32));
2143
2144 Val = Val.getNode() ? DAG.getNode(ISD::OR, dl, MVT::i64, Val, Shift) :
2145 Shift;
2146 Offset += LoadSize;
2147 Alignment = std::min(Alignment, LoadSize);
2148 }
2149
2150 RegsToPass.push_back(std::make_pair(*Reg, Val));
2151 return;
2152 }
2153 }
2154
Akira Hatanaka16040852011-11-15 18:42:25 +00002155 assert(MemCpySize && "MemCpySize must not be zero.");
2156
2157 // Create a fixed object on stack at offset LocMemOffset and copy
2158 // remainder of byval arg to it with memcpy.
2159 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2160 DAG.getConstant(Offset, PtrTy));
2161 LastFI = MFI->CreateFixedObject(MemCpySize, LocMemOffset, true);
2162 SDValue Dst = DAG.getFrameIndex(LastFI, PtrTy);
2163 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2164 DAG.getConstant(MemCpySize, PtrTy), Alignment,
2165 /*isVolatile=*/false, /*AlwaysInline=*/false,
2166 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002167}
2168
Dan Gohman98ca4f22009-08-05 01:29:28 +00002169/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002170/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002171/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002172SDValue
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002173MipsTargetLowering::LowerCall(SDValue InChain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002174 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002175 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002176 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002177 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002178 const SmallVectorImpl<ISD::InputArg> &Ins,
2179 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002180 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002181 // MIPs target does not yet support tail call optimization.
2182 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002183
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002184 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002185 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002186 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002187 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00002188 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002189
2190 // Analyze operands of the call, assigning locations to each operand.
2191 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002192 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2193 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002194
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002195 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002196 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanaka49617092011-11-14 19:02:54 +00002197 else if (HasMips64)
2198 AnalyzeMips64CallOperands(CCInfo, Outs);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00002199 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002200 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002201
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002202 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002203 unsigned NextStackOffset = CCInfo.getNextStackOffset();
2204
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002205 // Chain is the output chain of the last Load/Store or CopyToReg node.
2206 // ByValChain is the output chain of the last Memcpy node created for copying
2207 // byval arguments to the stack.
2208 SDValue Chain, CallSeqStart, ByValChain;
2209 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
2210 Chain = CallSeqStart = DAG.getCALLSEQ_START(InChain, NextStackOffsetVal);
2211 ByValChain = InChain;
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002212
2213 // If this is the first call, create a stack frame object that points to
2214 // a location to which .cprestore saves $gp.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002215 if (IsO32 && IsPIC && !MipsFI->getGPFI())
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002216 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
2217
Akira Hatanaka21afc632011-06-21 00:40:49 +00002218 // Get the frame index of the stack frame object that points to the location
2219 // of dynamically allocated area on the stack.
2220 int DynAllocFI = MipsFI->getDynAllocFI();
2221
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002222 // Update size of the maximum argument space.
2223 // For O32, a minimum of four words (16 bytes) of argument space is
2224 // allocated.
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002225 if (IsO32)
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002226 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
2227
2228 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
2229
2230 if (MaxCallFrameSize < NextStackOffset) {
2231 MipsFI->setMaxCallFrameSize(NextStackOffset);
2232
Akira Hatanaka21afc632011-06-21 00:40:49 +00002233 // Set the offsets relative to $sp of the $gp restore slot and dynamically
2234 // allocated stack space. These offsets must be aligned to a boundary
2235 // determined by the stack alignment of the ABI.
2236 unsigned StackAlignment = TFL->getStackAlignment();
2237 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
2238 StackAlignment * StackAlignment;
2239
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002240 if (MipsFI->needGPSaveRestore())
Akira Hatanaka21afc632011-06-21 00:40:49 +00002241 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
2242
2243 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002244 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002245
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002246 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002247 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
2248 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002249
Eric Christopher471e4222011-06-08 23:55:35 +00002250 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00002251
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002252 // Walk the register/memloc assignments, inserting copies/loads.
2253 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002254 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002255 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002256 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002257 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2258
2259 // ByVal Arg.
2260 if (Flags.isByVal()) {
2261 assert(Flags.getByValSize() &&
2262 "ByVal args of size 0 should have been ignored by front-end.");
2263 if (IsO32)
2264 WriteByValArg(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2265 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2266 Subtarget->isLittle());
2267 else
2268 PassByValArg64(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2269 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2270 Subtarget->isLittle());
2271 continue;
2272 }
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002273
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002274 // Promote the value if needed.
2275 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002276 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002277 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002278 if (VA.isRegLoc()) {
2279 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
2280 (ValVT == MVT::f64 && LocVT == MVT::i64))
2281 Arg = DAG.getNode(ISD::BITCAST, dl, LocVT, Arg);
2282 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002283 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2284 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002285 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2286 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002287 if (!Subtarget->isLittle())
2288 std::swap(Lo, Hi);
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002289 unsigned LocRegLo = VA.getLocReg();
2290 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2291 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2292 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002293 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002294 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002295 }
2296 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002297 case CCValAssign::SExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002298 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002299 break;
2300 case CCValAssign::ZExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002301 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002302 break;
2303 case CCValAssign::AExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002304 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002305 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002306 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002307
2308 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002309 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002310 if (VA.isRegLoc()) {
2311 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002312 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002313 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002314
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002315 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002316 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002317
Chris Lattnere0b12152008-03-17 06:57:02 +00002318 // Create the frame index object for this incoming parameter
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002319 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002320 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00002321 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00002322
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002323 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002324 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00002325 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
2326 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00002327 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002328 }
2329
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002330 // Extend range of indices of frame objects for outgoing arguments that were
2331 // created during this function call. Skip this step if no such objects were
2332 // created.
2333 if (LastFI)
2334 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2335
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002336 // If a memcpy has been created to copy a byval arg to a stack, replace the
2337 // chain input of CallSeqStart with ByValChain.
2338 if (InChain != ByValChain)
2339 DAG.UpdateNodeOperands(CallSeqStart.getNode(), ByValChain,
2340 NextStackOffsetVal);
2341
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002342 // Transform all store nodes into one single node because all store
2343 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002344 if (!MemOpChains.empty())
2345 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002346 &MemOpChains[0], MemOpChains.size());
2347
Bill Wendling056292f2008-09-16 21:48:12 +00002348 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002349 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2350 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002351 unsigned char OpFlag;
2352 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002353 bool GlobalOrExternal = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002354 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002355
2356 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002357 if (IsPICCall && G->getGlobal()->hasInternalLinkage()) {
2358 OpFlag = IsO32 ? MipsII::MO_GOT : MipsII::MO_GOT_PAGE;
2359 unsigned char LoFlag = IsO32 ? MipsII::MO_ABS_LO : MipsII::MO_GOT_OFST;
2360 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(), 0,
2361 OpFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002362 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002363 0, LoFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002364 } else {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002365 OpFlag = IsPICCall ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002366 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2367 getPointerTy(), 0, OpFlag);
2368 }
2369
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002370 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002371 }
2372 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002373 if (IsN64 || (!IsO32 && IsPIC))
2374 OpFlag = MipsII::MO_GOT_DISP;
2375 else if (!IsPIC) // !N64 && static
2376 OpFlag = MipsII::MO_NO_FLAG;
2377 else // O32 & PIC
2378 OpFlag = MipsII::MO_GOT_CALL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002379 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002380 getPointerTy(), OpFlag);
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002381 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002382 }
2383
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002384 SDValue InFlag;
2385
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002386 // Create nodes that load address of callee and copy it to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002387 if (IsPICCall) {
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002388 if (GlobalOrExternal) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002389 // Load callee address
Akira Hatanaka6df7e232011-12-09 01:53:17 +00002390 Callee = DAG.getNode(MipsISD::Wrapper, dl, getPointerTy(), Callee);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002391 SDValue LoadValue = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
2392 Callee, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002393 false, false, false, 0);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002394
2395 // Use GOT+LO if callee has internal linkage.
2396 if (CalleeLo.getNode()) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002397 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, getPointerTy(), CalleeLo);
2398 Callee = DAG.getNode(ISD::ADD, dl, getPointerTy(), LoadValue, Lo);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002399 } else
2400 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002401 }
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002402 }
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002403
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002404 // T9 should contain the address of the callee function if
2405 // -reloction-model=pic or it is an indirect call.
2406 if (IsPICCall || !GlobalOrExternal) {
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002407 // copy to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002408 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
2409 Chain = DAG.getCopyToReg(Chain, dl, T9Reg, Callee, SDValue(0, 0));
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002410 InFlag = Chain.getValue(1);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002411 Callee = DAG.getRegister(T9Reg, getPointerTy());
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002412 }
Bill Wendling056292f2008-09-16 21:48:12 +00002413
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002414 // Build a sequence of copy-to-reg nodes chained together with token
2415 // chain and flag operands which copy the outgoing args into registers.
2416 // The InFlag in necessary since all emitted instructions must be
2417 // stuck together.
2418 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2419 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2420 RegsToPass[i].second, InFlag);
2421 InFlag = Chain.getValue(1);
2422 }
2423
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002424 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002425 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002426 //
2427 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002428 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002429 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002430 Ops.push_back(Chain);
2431 Ops.push_back(Callee);
2432
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002433 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002434 // known live into the call.
2435 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2436 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2437 RegsToPass[i].second.getValueType()));
2438
Gabor Greifba36cb52008-08-28 21:40:38 +00002439 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002440 Ops.push_back(InFlag);
2441
Dale Johannesen33c960f2009-02-04 20:06:27 +00002442 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002443 InFlag = Chain.getValue(1);
2444
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002445 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002446 Chain = DAG.getCALLSEQ_END(Chain,
2447 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002448 DAG.getIntPtrConstant(0, true), InFlag);
2449 InFlag = Chain.getValue(1);
2450
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002451 // Handle result values, copying them out of physregs into vregs that we
2452 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002453 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2454 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002455}
2456
Dan Gohman98ca4f22009-08-05 01:29:28 +00002457/// LowerCallResult - Lower the result values of a call into the
2458/// appropriate copies out of appropriate physical registers.
2459SDValue
2460MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002461 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002462 const SmallVectorImpl<ISD::InputArg> &Ins,
2463 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002464 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002465 // Assign locations to each value returned by this call.
2466 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002467 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2468 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002469
Dan Gohman98ca4f22009-08-05 01:29:28 +00002470 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002471
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002472 // Copy all of the result registers out of their specified physreg.
2473 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002474 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002475 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002476 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002477 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002478 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002479
Dan Gohman98ca4f22009-08-05 01:29:28 +00002480 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002481}
2482
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002483//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002484// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002485//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002486static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2487 std::vector<SDValue>& OutChains,
2488 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2489 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2490 unsigned LocMem = VA.getLocMemOffset();
2491 unsigned FirstWord = LocMem / 4;
2492
2493 // copy register A0 - A3 to frame object
2494 for (unsigned i = 0; i < NumWords; ++i) {
2495 unsigned CurWord = FirstWord + i;
2496 if (CurWord >= O32IntRegsSize)
2497 break;
2498
2499 unsigned SrcReg = O32IntRegs[CurWord];
2500 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2501 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2502 DAG.getConstant(i * 4, MVT::i32));
2503 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2504 StorePtr, MachinePointerInfo(), false,
2505 false, 0);
2506 OutChains.push_back(Store);
2507 }
2508}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002509
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002510// Create frame object on stack and copy registers used for byval passing to it.
2511static unsigned
2512CopyMips64ByValRegs(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2513 std::vector<SDValue>& OutChains, SelectionDAG &DAG,
2514 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2515 MachineFrameInfo *MFI, bool IsRegLoc,
2516 SmallVectorImpl<SDValue> &InVals, MipsFunctionInfo *MipsFI,
2517 EVT PtrTy) {
2518 const unsigned *Reg = Mips64IntRegs + 8;
2519 int FOOffset; // Frame object offset from virtual frame pointer.
2520
2521 if (IsRegLoc) {
2522 Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8, VA.getLocReg());
2523 FOOffset = (Reg - Mips64IntRegs) * 8 - 8 * 8;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002524 }
2525 else
2526 FOOffset = VA.getLocMemOffset();
2527
2528 // Create frame object.
2529 unsigned NumRegs = (Flags.getByValSize() + 7) / 8;
2530 unsigned LastFI = MFI->CreateFixedObject(NumRegs * 8, FOOffset, true);
2531 SDValue FIN = DAG.getFrameIndex(LastFI, PtrTy);
2532 InVals.push_back(FIN);
2533
2534 // Copy arg registers.
2535 for (unsigned I = 0; (Reg != Mips64IntRegs + 8) && (I < NumRegs);
2536 ++Reg, ++I) {
2537 unsigned VReg = AddLiveIn(MF, *Reg, Mips::CPU64RegsRegisterClass);
2538 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, PtrTy, FIN,
2539 DAG.getConstant(I * 8, PtrTy));
2540 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(VReg, MVT::i64),
2541 StorePtr, MachinePointerInfo(), false,
2542 false, 0);
2543 OutChains.push_back(Store);
2544 }
2545
2546 return LastFI;
2547}
2548
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002549/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002550/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002551SDValue
2552MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002553 CallingConv::ID CallConv,
2554 bool isVarArg,
2555 const SmallVectorImpl<ISD::InputArg>
2556 &Ins,
2557 DebugLoc dl, SelectionDAG &DAG,
2558 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002559 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002560 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002561 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002562 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002563
Dan Gohman1e93df62010-04-17 14:41:14 +00002564 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002565
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002566 // Used with vargs to acumulate store chains.
2567 std::vector<SDValue> OutChains;
2568
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002569 // Assign locations to all of the incoming arguments.
2570 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002571 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2572 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002573
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002574 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002575 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002576 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002577 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002578
Akira Hatanaka43299772011-05-20 23:22:14 +00002579 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002580
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002581 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002582 CCValAssign &VA = ArgLocs[i];
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002583 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002584 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2585 bool IsRegLoc = VA.isRegLoc();
2586
2587 if (Flags.isByVal()) {
2588 assert(Flags.getByValSize() &&
2589 "ByVal args of size 0 should have been ignored by front-end.");
2590 if (IsO32) {
2591 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2592 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2593 true);
2594 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2595 InVals.push_back(FIN);
2596 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2597 } else // N32/64
2598 LastFI = CopyMips64ByValRegs(MF, Chain, dl, OutChains, DAG, VA, Flags,
2599 MFI, IsRegLoc, InVals, MipsFI,
2600 getPointerTy());
2601 continue;
2602 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002603
2604 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002605 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002606 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002607 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002608 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002609
Owen Anderson825b72b2009-08-11 20:47:22 +00002610 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002611 RC = Mips::CPURegsRegisterClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002612 else if (RegVT == MVT::i64)
2613 RC = Mips::CPU64RegsRegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002614 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002615 RC = Mips::FGR32RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002616 else if (RegVT == MVT::f64)
Akira Hatanakaf40de9d2011-09-26 21:55:17 +00002617 RC = HasMips64 ? Mips::FGR64RegisterClass : Mips::AFGR64RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002618 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002619 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002620
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002621 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002622 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002623 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002624 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002625
2626 // If this is an 8 or 16-bit value, it has been passed promoted
2627 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002628 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002629 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002630 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002631 if (VA.getLocInfo() == CCValAssign::SExt)
2632 Opcode = ISD::AssertSext;
2633 else if (VA.getLocInfo() == CCValAssign::ZExt)
2634 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002635 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002636 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002637 DAG.getValueType(ValVT));
2638 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002639 }
2640
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002641 // Handle floating point arguments passed in integer registers.
2642 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
2643 (RegVT == MVT::i64 && ValVT == MVT::f64))
2644 ArgValue = DAG.getNode(ISD::BITCAST, dl, ValVT, ArgValue);
2645 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
2646 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
2647 getNextIntArgReg(ArgReg), RC);
2648 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
2649 if (!Subtarget->isLittle())
2650 std::swap(ArgValue, ArgValue2);
2651 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2652 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002653 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002654
Dan Gohman98ca4f22009-08-05 01:29:28 +00002655 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002656 } else { // VA.isRegLoc()
2657
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002658 // sanity check
2659 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002660
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002661 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002662 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002663 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002664
2665 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002666 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002667 InVals.push_back(DAG.getLoad(ValVT, dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002668 MachinePointerInfo::getFixedStack(LastFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002669 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002670 }
2671 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002672
2673 // The mips ABIs for returning structs by value requires that we copy
2674 // the sret argument into $v0 for the return. Save the argument into
2675 // a virtual register so that we can access it from the return points.
2676 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2677 unsigned Reg = MipsFI->getSRetReturnReg();
2678 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002679 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002680 MipsFI->setSRetReturnReg(Reg);
2681 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002682 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002683 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002684 }
2685
Akira Hatanakabad53f42011-11-14 19:01:09 +00002686 if (isVarArg) {
2687 unsigned NumOfRegs = IsO32 ? 4 : 8;
2688 const unsigned *ArgRegs = IsO32 ? O32IntRegs : Mips64IntRegs;
2689 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumOfRegs);
2690 int FirstRegSlotOffset = IsO32 ? 0 : -64 ; // offset of $a0's slot.
2691 TargetRegisterClass *RC
2692 = IsO32 ? Mips::CPURegsRegisterClass : Mips::CPU64RegsRegisterClass;
2693 unsigned RegSize = RC->getSize();
2694 int RegSlotOffset = FirstRegSlotOffset + Idx * RegSize;
2695
2696 // Offset of the first variable argument from stack pointer.
2697 int FirstVaArgOffset;
2698
2699 if (IsO32 || (Idx == NumOfRegs)) {
2700 FirstVaArgOffset =
2701 (CCInfo.getNextStackOffset() + RegSize - 1) / RegSize * RegSize;
2702 } else
2703 FirstVaArgOffset = RegSlotOffset;
2704
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002705 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002706 // which is a value necessary to VASTART.
Akira Hatanakabad53f42011-11-14 19:01:09 +00002707 LastFI = MFI->CreateFixedObject(RegSize, FirstVaArgOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002708 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002709
Akira Hatanakabad53f42011-11-14 19:01:09 +00002710 // Copy the integer registers that have not been used for argument passing
2711 // to the argument register save area. For O32, the save area is allocated
2712 // in the caller's stack frame, while for N32/64, it is allocated in the
2713 // callee's stack frame.
2714 for (int StackOffset = RegSlotOffset;
2715 Idx < NumOfRegs; ++Idx, StackOffset += RegSize) {
2716 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegs[Idx], RC);
2717 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2718 MVT::getIntegerVT(RegSize * 8));
2719 LastFI = MFI->CreateFixedObject(RegSize, StackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002720 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2721 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2722 MachinePointerInfo(),
2723 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002724 }
2725 }
2726
Akira Hatanaka43299772011-05-20 23:22:14 +00002727 MipsFI->setLastInArgFI(LastFI);
2728
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002729 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002730 // the size of Ins and InVals. This only happens when on varg functions
2731 if (!OutChains.empty()) {
2732 OutChains.push_back(Chain);
2733 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2734 &OutChains[0], OutChains.size());
2735 }
2736
Dan Gohman98ca4f22009-08-05 01:29:28 +00002737 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002738}
2739
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002740//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002741// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002742//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002743
Dan Gohman98ca4f22009-08-05 01:29:28 +00002744SDValue
2745MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002746 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002747 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002748 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002749 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002750
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002751 // CCValAssign - represent the assignment of
2752 // the return value to a location
2753 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002754
2755 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002756 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2757 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002758
Dan Gohman98ca4f22009-08-05 01:29:28 +00002759 // Analize return values.
2760 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002761
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002762 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002763 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002764 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002765 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002766 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002767 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002768 }
2769
Dan Gohman475871a2008-07-27 21:46:04 +00002770 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002771
2772 // Copy the result values into the output registers.
2773 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2774 CCValAssign &VA = RVLocs[i];
2775 assert(VA.isRegLoc() && "Can only return in registers!");
2776
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002777 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002778 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002779
2780 // guarantee that all emitted copies are
2781 // stuck together, avoiding something bad
2782 Flag = Chain.getValue(1);
2783 }
2784
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002785 // The mips ABIs for returning structs by value requires that we copy
2786 // the sret argument into $v0 for the return. We saved the argument into
2787 // a virtual register in the entry block, so now we copy the value out
2788 // and into $v0.
2789 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2790 MachineFunction &MF = DAG.getMachineFunction();
2791 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2792 unsigned Reg = MipsFI->getSRetReturnReg();
2793
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002794 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002795 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002796 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002797
Dale Johannesena05dca42009-02-04 23:02:30 +00002798 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002799 Flag = Chain.getValue(1);
2800 }
2801
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002802 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002803 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002804 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002805 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002806 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002807 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002808 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002809}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002810
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002811//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002812// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002813//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002814
2815/// getConstraintType - Given a constraint letter, return the type of
2816/// constraint it is for this target.
2817MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002818getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002819{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002820 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002821 // GCC config/mips/constraints.md
2822 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002823 // 'd' : An address register. Equivalent to r
2824 // unless generating MIPS16 code.
2825 // 'y' : Equivalent to r; retained for
2826 // backwards compatibility.
2827 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002828 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002829 switch (Constraint[0]) {
2830 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002831 case 'd':
2832 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002833 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002834 return C_RegisterClass;
2835 break;
2836 }
2837 }
2838 return TargetLowering::getConstraintType(Constraint);
2839}
2840
John Thompson44ab89e2010-10-29 17:29:13 +00002841/// Examine constraint type and operand type and determine a weight value.
2842/// This object must already have been set up with the operand type
2843/// and the current alternative constraint selected.
2844TargetLowering::ConstraintWeight
2845MipsTargetLowering::getSingleConstraintMatchWeight(
2846 AsmOperandInfo &info, const char *constraint) const {
2847 ConstraintWeight weight = CW_Invalid;
2848 Value *CallOperandVal = info.CallOperandVal;
2849 // If we don't have a value, we can't do a match,
2850 // but allow it at the lowest weight.
2851 if (CallOperandVal == NULL)
2852 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002853 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002854 // Look at the constraint type.
2855 switch (*constraint) {
2856 default:
2857 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2858 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002859 case 'd':
2860 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002861 if (type->isIntegerTy())
2862 weight = CW_Register;
2863 break;
2864 case 'f':
2865 if (type->isFloatTy())
2866 weight = CW_Register;
2867 break;
2868 }
2869 return weight;
2870}
2871
Eric Christopher38d64262011-06-29 19:33:04 +00002872/// Given a register class constraint, like 'r', if this corresponds directly
2873/// to an LLVM register class, return a register of 0 and the register class
2874/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002875std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002876getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002877{
2878 if (Constraint.size() == 1) {
2879 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002880 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2881 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002882 case 'r':
2883 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002884 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002885 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002886 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002887 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002888 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2889 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002890 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002891 }
2892 }
2893 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2894}
2895
Dan Gohman6520e202008-10-18 02:06:02 +00002896bool
2897MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2898 // The Mips target isn't yet aware of offsets.
2899 return false;
2900}
Evan Chengeb2f9692009-10-27 19:56:55 +00002901
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002902bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2903 if (VT != MVT::f32 && VT != MVT::f64)
2904 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002905 if (Imm.isNegZero())
2906 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002907 return Imm.isZero();
2908}