blob: d0ee632db74faddd5dc4e2767e5e4e1680f75a57 [file] [log] [blame]
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000014
15#define DEBUG_TYPE "mips-lower"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000016#include "MipsISelLowering.h"
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +000017#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000018#include "MipsTargetMachine.h"
Chris Lattnerb71b9092009-08-13 06:28:06 +000019#include "MipsTargetObjectFile.h"
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000020#include "MipsSubtarget.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000021#include "llvm/DerivedTypes.h"
22#include "llvm/Function.h"
Bruno Cardoso Lopes91fd5322008-07-21 18:52:34 +000023#include "llvm/GlobalVariable.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000024#include "llvm/Intrinsics.h"
25#include "llvm/CallingConv.h"
Akira Hatanaka794bf172011-07-07 23:56:50 +000026#include "InstPrinter/MipsInstPrinter.h"
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +000027#include "MCTargetDesc/MipsBaseInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000028#include "llvm/CodeGen/CallingConvLower.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000034#include "llvm/CodeGen/ValueTypes.h"
35#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000036#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000037using namespace llvm;
38
Akira Hatanakadbe9a312011-08-18 20:07:42 +000039// If I is a shifted mask, set the size (Size) and the first bit of the
40// mask (Pos), and return true.
Akira Hatanaka854a7db2011-08-19 22:59:00 +000041// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
42static bool IsShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanakad6bc5232011-12-05 21:26:34 +000043 if (!isShiftedMask_64(I))
Akira Hatanaka854a7db2011-08-19 22:59:00 +000044 return false;
Akira Hatanakabb15e112011-08-17 02:05:42 +000045
Akira Hatanakad6bc5232011-12-05 21:26:34 +000046 Size = CountPopulation_64(I);
47 Pos = CountTrailingZeros_64(I);
Akira Hatanakadbe9a312011-08-18 20:07:42 +000048 return true;
Akira Hatanakabb15e112011-08-17 02:05:42 +000049}
50
Chris Lattnerf0144122009-07-28 03:13:23 +000051const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
52 switch (Opcode) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000053 case MipsISD::JmpLink: return "MipsISD::JmpLink";
54 case MipsISD::Hi: return "MipsISD::Hi";
55 case MipsISD::Lo: return "MipsISD::Lo";
56 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +000057 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000058 case MipsISD::Ret: return "MipsISD::Ret";
59 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
60 case MipsISD::FPCmp: return "MipsISD::FPCmp";
61 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
62 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
63 case MipsISD::FPRound: return "MipsISD::FPRound";
64 case MipsISD::MAdd: return "MipsISD::MAdd";
65 case MipsISD::MAddu: return "MipsISD::MAddu";
66 case MipsISD::MSub: return "MipsISD::MSub";
67 case MipsISD::MSubu: return "MipsISD::MSubu";
68 case MipsISD::DivRem: return "MipsISD::DivRem";
69 case MipsISD::DivRemU: return "MipsISD::DivRemU";
70 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
71 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakabfcb83f2011-12-12 22:38:19 +000072 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Akira Hatanaka21afc632011-06-21 00:40:49 +000073 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
Akira Hatanakadb548262011-07-19 23:30:50 +000074 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanakabb15e112011-08-17 02:05:42 +000075 case MipsISD::Ext: return "MipsISD::Ext";
76 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanaka0f843822011-06-07 18:58:42 +000077 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000078 }
79}
80
81MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000082MipsTargetLowering(MipsTargetMachine &TM)
Akira Hatanaka8b4198d2011-09-26 21:47:02 +000083 : TargetLowering(TM, new MipsTargetObjectFile()),
84 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +000085 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
86 IsO32(Subtarget->isABI_O32()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000087
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000088 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000089 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000090 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +000091 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000092
93 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000094 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
95 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000096
Akira Hatanaka95934842011-09-24 01:34:44 +000097 if (HasMips64)
98 addRegisterClass(MVT::i64, Mips::CPU64RegsRegisterClass);
99
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000100 // When dealing with single precision only, use libcalls
Akira Hatanaka792016b2011-09-23 18:28:39 +0000101 if (!Subtarget->isSingleFloat()) {
102 if (HasMips64)
103 addRegisterClass(MVT::f64, Mips::FGR64RegisterClass);
104 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000105 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Akira Hatanaka792016b2011-09-23 18:28:39 +0000106 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000107
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000108 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000109 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
110 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
111 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000112
Eli Friedman6055a6a2009-07-17 04:07:24 +0000113 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000114 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
115 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000116
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000117 // Used by legalize types to correctly generate the setcc result.
118 // Without this, every float setcc comes with a AND/OR with the result,
119 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000120 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000121 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000122
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000123 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +0000124 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Akira Hatanakaa5903ac2011-10-11 00:55:05 +0000125 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000126 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Akira Hatanaka9b944a82011-11-16 22:42:10 +0000127 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000128 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Akira Hatanakaca074792011-12-08 20:34:32 +0000129 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +0000131 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Akira Hatanaka620db892011-11-16 22:44:38 +0000133 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 setOperationAction(ISD::SELECT, MVT::f32, Custom);
135 setOperationAction(ISD::SELECT, MVT::f64, Custom);
136 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000137 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
138 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Akira Hatanaka93883832011-12-20 23:35:46 +0000139 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000140 setOperationAction(ISD::VASTART, MVT::Other, Custom);
141
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000142 setOperationAction(ISD::SDIV, MVT::i32, Expand);
143 setOperationAction(ISD::SREM, MVT::i32, Expand);
144 setOperationAction(ISD::UDIV, MVT::i32, Expand);
145 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000146 setOperationAction(ISD::SDIV, MVT::i64, Expand);
147 setOperationAction(ISD::SREM, MVT::i64, Expand);
148 setOperationAction(ISD::UDIV, MVT::i64, Expand);
149 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000150
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000151 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000152 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
153 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
154 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
155 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000156 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000157 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000158 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
160 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
161 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000162 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
163 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
164 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
165 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000166 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000167 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000168
Akira Hatanaka56633442011-09-20 23:53:09 +0000169 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000170 setOperationAction(ISD::ROTR, MVT::i32, Expand);
171
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000172 if (!Subtarget->hasMips64r2())
173 setOperationAction(ISD::ROTR, MVT::i64, Expand);
174
Owen Anderson825b72b2009-08-11 20:47:22 +0000175 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
176 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
177 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000178 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
179 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000181 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000182 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000183 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000184 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
185 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000186 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000187 setOperationAction(ISD::FLOG, MVT::f32, Expand);
188 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
189 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
190 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000191 setOperationAction(ISD::FMA, MVT::f32, Expand);
192 setOperationAction(ISD::FMA, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000193
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000194 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
195 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Eric Christopher471e4222011-06-08 23:55:35 +0000196
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000197 setOperationAction(ISD::VAARG, MVT::Other, Expand);
198 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
199 setOperationAction(ISD::VAEND, MVT::Other, Expand);
200
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000201 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000202 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
203 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000204
Akira Hatanakadb548262011-07-19 23:30:50 +0000205 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000206 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000207
Eli Friedman4db5aca2011-08-29 18:23:02 +0000208 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
209 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
210
Eli Friedman26689ac2011-08-03 21:06:02 +0000211 setInsertFencesForAtomic(true);
212
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000213 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000214 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000215
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000216 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000217 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
218 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000219 }
220
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000221 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000222 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000223
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000224 if (!Subtarget->hasSwap()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000225 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000226 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
227 }
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000228
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000229 setTargetDAGCombine(ISD::ADDE);
230 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000231 setTargetDAGCombine(ISD::SDIVREM);
232 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000233 setTargetDAGCombine(ISD::SETCC);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000234 setTargetDAGCombine(ISD::AND);
235 setTargetDAGCombine(ISD::OR);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000236
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000237 setMinFunctionAlignment(2);
238
Akira Hatanaka056a1bc2011-12-20 23:28:36 +0000239 setStackPointerRegisterToSaveRestore(HasMips64 ? Mips::SP_64 : Mips::SP);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000240 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000241
242 setExceptionPointerRegister(Mips::A0);
243 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000244}
245
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000246bool MipsTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Akira Hatanaka511961a2011-08-17 18:49:18 +0000247 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
Akira Hatanaka7bd19bd2011-10-11 00:27:28 +0000248 return SVT == MVT::i64 || SVT == MVT::i32 || SVT == MVT::i16;
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000249}
250
Duncan Sands28b77e92011-09-06 19:07:46 +0000251EVT MipsTargetLowering::getSetCCResultType(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000253}
254
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000255// SelectMadd -
256// Transforms a subgraph in CurDAG if the following pattern is found:
257// (addc multLo, Lo0), (adde multHi, Hi0),
258// where,
259// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000260// Lo0: initial value of Lo register
261// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000262// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000263static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000264 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000265 // for the matching to be successful.
266 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
267
268 if (ADDCNode->getOpcode() != ISD::ADDC)
269 return false;
270
271 SDValue MultHi = ADDENode->getOperand(0);
272 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000273 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000274 unsigned MultOpc = MultHi.getOpcode();
275
276 // MultHi and MultLo must be generated by the same node,
277 if (MultLo.getNode() != MultNode)
278 return false;
279
280 // and it must be a multiplication.
281 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
282 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000283
284 // MultLo amd MultHi must be the first and second output of MultNode
285 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000286 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
287 return false;
288
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000289 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000290 // of the values of MultNode, in which case MultNode will be removed in later
291 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000292 // If there exist users other than ADDENode or ADDCNode, this function returns
293 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000294 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000295 // produced.
296 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
297 return false;
298
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000299 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000300 DebugLoc dl = ADDENode->getDebugLoc();
301
302 // create MipsMAdd(u) node
303 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000304
Akira Hatanaka82099682011-12-19 19:52:25 +0000305 SDValue MAdd = CurDAG->getNode(MultOpc, dl, MVT::Glue,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000306 MultNode->getOperand(0),// Factor 0
307 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000308 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000309 ADDENode->getOperand(1));// Hi0
310
311 // create CopyFromReg nodes
312 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
313 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000314 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000315 Mips::HI, MVT::i32,
316 CopyFromLo.getValue(2));
317
318 // replace uses of adde and addc here
319 if (!SDValue(ADDCNode, 0).use_empty())
320 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
321
322 if (!SDValue(ADDENode, 0).use_empty())
323 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
324
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000325 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000326}
327
328// SelectMsub -
329// Transforms a subgraph in CurDAG if the following pattern is found:
330// (addc Lo0, multLo), (sube Hi0, multHi),
331// where,
332// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000333// Lo0: initial value of Lo register
334// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000335// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000336static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000337 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000338 // for the matching to be successful.
339 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
340
341 if (SUBCNode->getOpcode() != ISD::SUBC)
342 return false;
343
344 SDValue MultHi = SUBENode->getOperand(1);
345 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000346 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000347 unsigned MultOpc = MultHi.getOpcode();
348
349 // MultHi and MultLo must be generated by the same node,
350 if (MultLo.getNode() != MultNode)
351 return false;
352
353 // and it must be a multiplication.
354 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
355 return false;
356
357 // MultLo amd MultHi must be the first and second output of MultNode
358 // respectively.
359 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
360 return false;
361
362 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
363 // of the values of MultNode, in which case MultNode will be removed in later
364 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000365 // If there exist users other than SUBENode or SUBCNode, this function returns
366 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000367 // instruction node rather than a pair of MULT and MSUB instructions being
368 // produced.
369 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
370 return false;
371
372 SDValue Chain = CurDAG->getEntryNode();
373 DebugLoc dl = SUBENode->getDebugLoc();
374
375 // create MipsSub(u) node
376 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
377
Akira Hatanaka82099682011-12-19 19:52:25 +0000378 SDValue MSub = CurDAG->getNode(MultOpc, dl, MVT::Glue,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000379 MultNode->getOperand(0),// Factor 0
380 MultNode->getOperand(1),// Factor 1
381 SUBCNode->getOperand(0),// Lo0
382 SUBENode->getOperand(0));// Hi0
383
384 // create CopyFromReg nodes
385 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
386 MSub);
387 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
388 Mips::HI, MVT::i32,
389 CopyFromLo.getValue(2));
390
391 // replace uses of sube and subc here
392 if (!SDValue(SUBCNode, 0).use_empty())
393 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
394
395 if (!SDValue(SUBENode, 0).use_empty())
396 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
397
398 return true;
399}
400
401static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
402 TargetLowering::DAGCombinerInfo &DCI,
403 const MipsSubtarget* Subtarget) {
404 if (DCI.isBeforeLegalize())
405 return SDValue();
406
Akira Hatanakae184fec2011-11-11 04:18:21 +0000407 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
408 SelectMadd(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000409 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000410
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000411 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000412}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000413
414static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
415 TargetLowering::DAGCombinerInfo &DCI,
416 const MipsSubtarget* Subtarget) {
417 if (DCI.isBeforeLegalize())
418 return SDValue();
419
Akira Hatanakae184fec2011-11-11 04:18:21 +0000420 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
421 SelectMsub(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000422 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000423
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000424 return SDValue();
425}
426
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000427static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
428 TargetLowering::DAGCombinerInfo &DCI,
429 const MipsSubtarget* Subtarget) {
430 if (DCI.isBeforeLegalizeOps())
431 return SDValue();
432
Akira Hatanakadda4a072011-10-03 21:06:13 +0000433 EVT Ty = N->getValueType(0);
434 unsigned LO = (Ty == MVT::i32) ? Mips::LO : Mips::LO64;
435 unsigned HI = (Ty == MVT::i32) ? Mips::HI : Mips::HI64;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000436 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
437 MipsISD::DivRemU;
438 DebugLoc dl = N->getDebugLoc();
439
440 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
441 N->getOperand(0), N->getOperand(1));
442 SDValue InChain = DAG.getEntryNode();
443 SDValue InGlue = DivRem;
444
445 // insert MFLO
446 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakadda4a072011-10-03 21:06:13 +0000447 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000448 InGlue);
449 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
450 InChain = CopyFromLo.getValue(1);
451 InGlue = CopyFromLo.getValue(2);
452 }
453
454 // insert MFHI
455 if (N->hasAnyUseOfValue(1)) {
456 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000457 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000458 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
459 }
460
461 return SDValue();
462}
463
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000464static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
465 switch (CC) {
466 default: llvm_unreachable("Unknown fp condition code!");
467 case ISD::SETEQ:
468 case ISD::SETOEQ: return Mips::FCOND_OEQ;
469 case ISD::SETUNE: return Mips::FCOND_UNE;
470 case ISD::SETLT:
471 case ISD::SETOLT: return Mips::FCOND_OLT;
472 case ISD::SETGT:
473 case ISD::SETOGT: return Mips::FCOND_OGT;
474 case ISD::SETLE:
475 case ISD::SETOLE: return Mips::FCOND_OLE;
476 case ISD::SETGE:
477 case ISD::SETOGE: return Mips::FCOND_OGE;
478 case ISD::SETULT: return Mips::FCOND_ULT;
479 case ISD::SETULE: return Mips::FCOND_ULE;
480 case ISD::SETUGT: return Mips::FCOND_UGT;
481 case ISD::SETUGE: return Mips::FCOND_UGE;
482 case ISD::SETUO: return Mips::FCOND_UN;
483 case ISD::SETO: return Mips::FCOND_OR;
484 case ISD::SETNE:
485 case ISD::SETONE: return Mips::FCOND_ONE;
486 case ISD::SETUEQ: return Mips::FCOND_UEQ;
487 }
488}
489
490
491// Returns true if condition code has to be inverted.
492static bool InvertFPCondCode(Mips::CondCode CC) {
493 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
494 return false;
495
Akira Hatanaka82099682011-12-19 19:52:25 +0000496 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
497 "Illegal Condition Code");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000498
Akira Hatanaka82099682011-12-19 19:52:25 +0000499 return true;
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000500}
501
502// Creates and returns an FPCmp node from a setcc node.
503// Returns Op if setcc is not a floating point comparison.
504static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
505 // must be a SETCC node
506 if (Op.getOpcode() != ISD::SETCC)
507 return Op;
508
509 SDValue LHS = Op.getOperand(0);
510
511 if (!LHS.getValueType().isFloatingPoint())
512 return Op;
513
514 SDValue RHS = Op.getOperand(1);
515 DebugLoc dl = Op.getDebugLoc();
516
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000517 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
518 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000519 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
520
521 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
522 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
523}
524
525// Creates and returns a CMovFPT/F node.
526static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
527 SDValue False, DebugLoc DL) {
528 bool invert = InvertFPCondCode((Mips::CondCode)
529 cast<ConstantSDNode>(Cond.getOperand(2))
530 ->getSExtValue());
531
532 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
533 True.getValueType(), True, False, Cond);
534}
535
536static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
537 TargetLowering::DAGCombinerInfo &DCI,
538 const MipsSubtarget* Subtarget) {
539 if (DCI.isBeforeLegalizeOps())
540 return SDValue();
541
542 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
543
544 if (Cond.getOpcode() != MipsISD::FPCmp)
545 return SDValue();
546
547 SDValue True = DAG.getConstant(1, MVT::i32);
548 SDValue False = DAG.getConstant(0, MVT::i32);
549
550 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
551}
552
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000553static SDValue PerformANDCombine(SDNode *N, SelectionDAG& DAG,
554 TargetLowering::DAGCombinerInfo &DCI,
555 const MipsSubtarget* Subtarget) {
556 // Pattern match EXT.
557 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
558 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000559 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000560 return SDValue();
561
562 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000563 unsigned ShiftRightOpc = ShiftRight.getOpcode();
564
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000565 // Op's first operand must be a shift right.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000566 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000567 return SDValue();
568
569 // The second operand of the shift must be an immediate.
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000570 ConstantSDNode *CN;
571 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
572 return SDValue();
573
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000574 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000575 uint64_t SMPos, SMSize;
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000576
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000577 // Op's second operand must be a shifted mask.
578 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000579 !IsShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000580 return SDValue();
581
582 // Return if the shifted mask does not start at bit 0 or the sum of its size
583 // and Pos exceeds the word's size.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000584 EVT ValTy = N->getValueType(0);
585 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000586 return SDValue();
587
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000588 return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), ValTy,
Akira Hatanaka82099682011-12-19 19:52:25 +0000589 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000590 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000591}
592
593static SDValue PerformORCombine(SDNode *N, SelectionDAG& DAG,
594 TargetLowering::DAGCombinerInfo &DCI,
595 const MipsSubtarget* Subtarget) {
596 // Pattern match INS.
597 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
598 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
599 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000600 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000601 return SDValue();
602
603 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
604 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
605 ConstantSDNode *CN;
606
607 // See if Op's first operand matches (and $src1 , mask0).
608 if (And0.getOpcode() != ISD::AND)
609 return SDValue();
610
611 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000612 !IsShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000613 return SDValue();
614
615 // See if Op's second operand matches (and (shl $src, pos), mask1).
616 if (And1.getOpcode() != ISD::AND)
617 return SDValue();
618
619 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000620 !IsShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000621 return SDValue();
622
623 // The shift masks must have the same position and size.
624 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
625 return SDValue();
626
627 SDValue Shl = And1.getOperand(0);
628 if (Shl.getOpcode() != ISD::SHL)
629 return SDValue();
630
631 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
632 return SDValue();
633
634 unsigned Shamt = CN->getZExtValue();
635
636 // Return if the shift amount and the first bit position of mask are not the
637 // same.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000638 EVT ValTy = N->getValueType(0);
639 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000640 return SDValue();
641
Akira Hatanaka82099682011-12-19 19:52:25 +0000642 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), ValTy, Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000643 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka82099682011-12-19 19:52:25 +0000644 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000645}
646
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000647SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000648 const {
649 SelectionDAG &DAG = DCI.DAG;
650 unsigned opc = N->getOpcode();
651
652 switch (opc) {
653 default: break;
654 case ISD::ADDE:
655 return PerformADDECombine(N, DAG, DCI, Subtarget);
656 case ISD::SUBE:
657 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000658 case ISD::SDIVREM:
659 case ISD::UDIVREM:
660 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000661 case ISD::SETCC:
662 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000663 case ISD::AND:
664 return PerformANDCombine(N, DAG, DCI, Subtarget);
665 case ISD::OR:
666 return PerformORCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000667 }
668
669 return SDValue();
670}
671
Dan Gohman475871a2008-07-27 21:46:04 +0000672SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000673LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000674{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000675 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000676 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000677 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000678 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
679 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000680 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000681 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000682 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
683 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000684 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000685 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000686 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000687 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Akira Hatanakadb548262011-07-19 23:30:50 +0000688 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +0000689 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000690 }
Dan Gohman475871a2008-07-27 21:46:04 +0000691 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000692}
693
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000694//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000695// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000696//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000697
698// AddLiveIn - This helper function adds the specified physical register to the
699// MachineFunction as a live in value. It also creates a corresponding
700// virtual register for it.
701static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000702AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000703{
704 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000705 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
706 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000707 return VReg;
708}
709
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000710// Get fp branch code (not opcode) from condition code.
711static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
712 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
713 return Mips::BRANCH_T;
714
Akira Hatanaka82099682011-12-19 19:52:25 +0000715 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
716 "Invalid CondCode.");
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000717
Akira Hatanaka82099682011-12-19 19:52:25 +0000718 return Mips::BRANCH_F;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000719}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000720
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000721/*
Akira Hatanaka14487d42011-06-07 19:28:39 +0000722static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
723 DebugLoc dl,
724 const MipsSubtarget* Subtarget,
725 const TargetInstrInfo *TII,
726 bool isFPCmp, unsigned Opc) {
727 // There is no need to expand CMov instructions if target has
728 // conditional moves.
729 if (Subtarget->hasCondMov())
730 return BB;
731
732 // To "insert" a SELECT_CC instruction, we actually have to insert the
733 // diamond control-flow pattern. The incoming instruction knows the
734 // destination vreg to set, the condition code register to branch on, the
735 // true/false values to select between, and a branch opcode to use.
736 const BasicBlock *LLVM_BB = BB->getBasicBlock();
737 MachineFunction::iterator It = BB;
738 ++It;
739
740 // thisMBB:
741 // ...
742 // TrueVal = ...
743 // setcc r1, r2, r3
744 // bNE r1, r0, copy1MBB
745 // fallthrough --> copy0MBB
746 MachineBasicBlock *thisMBB = BB;
747 MachineFunction *F = BB->getParent();
748 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
749 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
750 F->insert(It, copy0MBB);
751 F->insert(It, sinkMBB);
752
753 // Transfer the remainder of BB and its successor edges to sinkMBB.
754 sinkMBB->splice(sinkMBB->begin(), BB,
755 llvm::next(MachineBasicBlock::iterator(MI)),
756 BB->end());
757 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
758
759 // Next, add the true and fallthrough blocks as its successors.
760 BB->addSuccessor(copy0MBB);
761 BB->addSuccessor(sinkMBB);
762
763 // Emit the right instruction according to the type of the operands compared
764 if (isFPCmp)
765 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
766 else
767 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
768 .addReg(Mips::ZERO).addMBB(sinkMBB);
769
770 // copy0MBB:
771 // %FalseValue = ...
772 // # fallthrough to sinkMBB
773 BB = copy0MBB;
774
775 // Update machine-CFG edges
776 BB->addSuccessor(sinkMBB);
777
778 // sinkMBB:
779 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
780 // ...
781 BB = sinkMBB;
782
783 if (isFPCmp)
784 BuildMI(*BB, BB->begin(), dl,
785 TII->get(Mips::PHI), MI->getOperand(0).getReg())
786 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
787 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
788 else
789 BuildMI(*BB, BB->begin(), dl,
790 TII->get(Mips::PHI), MI->getOperand(0).getReg())
791 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
792 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
793
794 MI->eraseFromParent(); // The pseudo instruction is gone now.
795 return BB;
796}
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000797*/
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000798MachineBasicBlock *
799MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000800 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000801 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000802 default:
803 assert(false && "Unexpected instr type to insert");
804 return NULL;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000805 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000806 case Mips::ATOMIC_LOAD_ADD_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000807 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
808 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000809 case Mips::ATOMIC_LOAD_ADD_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000810 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
811 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000812 case Mips::ATOMIC_LOAD_ADD_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000813 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000814 case Mips::ATOMIC_LOAD_ADD_I64:
815 case Mips::ATOMIC_LOAD_ADD_I64_P8:
816 return EmitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000817
818 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000819 case Mips::ATOMIC_LOAD_AND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000820 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
821 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000822 case Mips::ATOMIC_LOAD_AND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000823 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
824 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000825 case Mips::ATOMIC_LOAD_AND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000826 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000827 case Mips::ATOMIC_LOAD_AND_I64:
828 case Mips::ATOMIC_LOAD_AND_I64_P8:
Akira Hatanaka73866122011-11-12 02:38:12 +0000829 return EmitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000830
831 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000832 case Mips::ATOMIC_LOAD_OR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000833 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
834 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000835 case Mips::ATOMIC_LOAD_OR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000836 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
837 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000838 case Mips::ATOMIC_LOAD_OR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000839 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000840 case Mips::ATOMIC_LOAD_OR_I64:
841 case Mips::ATOMIC_LOAD_OR_I64_P8:
842 return EmitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000843
844 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000845 case Mips::ATOMIC_LOAD_XOR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000846 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
847 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000848 case Mips::ATOMIC_LOAD_XOR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000849 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
850 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000851 case Mips::ATOMIC_LOAD_XOR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000852 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000853 case Mips::ATOMIC_LOAD_XOR_I64:
854 case Mips::ATOMIC_LOAD_XOR_I64_P8:
855 return EmitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000856
857 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000858 case Mips::ATOMIC_LOAD_NAND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000859 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
860 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000861 case Mips::ATOMIC_LOAD_NAND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000862 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
863 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000864 case Mips::ATOMIC_LOAD_NAND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000865 return EmitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000866 case Mips::ATOMIC_LOAD_NAND_I64:
867 case Mips::ATOMIC_LOAD_NAND_I64_P8:
868 return EmitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000869
870 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000871 case Mips::ATOMIC_LOAD_SUB_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000872 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
873 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000874 case Mips::ATOMIC_LOAD_SUB_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000875 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
876 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000877 case Mips::ATOMIC_LOAD_SUB_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000878 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000879 case Mips::ATOMIC_LOAD_SUB_I64:
880 case Mips::ATOMIC_LOAD_SUB_I64_P8:
881 return EmitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000882
883 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000884 case Mips::ATOMIC_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000885 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
886 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000887 case Mips::ATOMIC_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000888 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
889 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000890 case Mips::ATOMIC_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000891 return EmitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000892 case Mips::ATOMIC_SWAP_I64:
893 case Mips::ATOMIC_SWAP_I64_P8:
894 return EmitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000895
896 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000897 case Mips::ATOMIC_CMP_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000898 return EmitAtomicCmpSwapPartword(MI, BB, 1);
899 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000900 case Mips::ATOMIC_CMP_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000901 return EmitAtomicCmpSwapPartword(MI, BB, 2);
902 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000903 case Mips::ATOMIC_CMP_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000904 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000905 case Mips::ATOMIC_CMP_SWAP_I64:
906 case Mips::ATOMIC_CMP_SWAP_I64_P8:
907 return EmitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000908 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000909}
910
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000911// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
912// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
913MachineBasicBlock *
914MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000915 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000916 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000917 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000918
919 MachineFunction *MF = BB->getParent();
920 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000921 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000922 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
923 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000924 unsigned LL, SC, AND, NOR, ZERO, BEQ;
925
926 if (Size == 4) {
927 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
928 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
929 AND = Mips::AND;
930 NOR = Mips::NOR;
931 ZERO = Mips::ZERO;
932 BEQ = Mips::BEQ;
933 }
934 else {
935 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
936 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
937 AND = Mips::AND64;
938 NOR = Mips::NOR64;
939 ZERO = Mips::ZERO_64;
940 BEQ = Mips::BEQ64;
941 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000942
Akira Hatanaka4061da12011-07-19 20:11:17 +0000943 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000944 unsigned Ptr = MI->getOperand(1).getReg();
945 unsigned Incr = MI->getOperand(2).getReg();
946
Akira Hatanaka4061da12011-07-19 20:11:17 +0000947 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
948 unsigned AndRes = RegInfo.createVirtualRegister(RC);
949 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000950
951 // insert new blocks after the current block
952 const BasicBlock *LLVM_BB = BB->getBasicBlock();
953 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
954 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
955 MachineFunction::iterator It = BB;
956 ++It;
957 MF->insert(It, loopMBB);
958 MF->insert(It, exitMBB);
959
960 // Transfer the remainder of BB and its successor edges to exitMBB.
961 exitMBB->splice(exitMBB->begin(), BB,
962 llvm::next(MachineBasicBlock::iterator(MI)),
963 BB->end());
964 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
965
966 // thisMBB:
967 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000968 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000969 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000970 loopMBB->addSuccessor(loopMBB);
971 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000972
973 // loopMBB:
974 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000975 // <binop> storeval, oldval, incr
976 // sc success, storeval, 0(ptr)
977 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000978 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +0000979 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000980 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000981 // and andres, oldval, incr
982 // nor storeval, $0, andres
Akira Hatanaka59068062011-11-11 04:14:30 +0000983 BuildMI(BB, dl, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
984 BuildMI(BB, dl, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000985 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000986 // <binop> storeval, oldval, incr
987 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000988 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000989 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000990 }
Akira Hatanaka59068062011-11-11 04:14:30 +0000991 BuildMI(BB, dl, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
992 BuildMI(BB, dl, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000993
994 MI->eraseFromParent(); // The instruction is gone now.
995
Akira Hatanaka939ece12011-07-19 03:42:13 +0000996 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000997}
998
999MachineBasicBlock *
1000MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001001 MachineBasicBlock *BB,
1002 unsigned Size, unsigned BinOpcode,
1003 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001004 assert((Size == 1 || Size == 2) &&
1005 "Unsupported size for EmitAtomicBinaryPartial.");
1006
1007 MachineFunction *MF = BB->getParent();
1008 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1009 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1010 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1011 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001012 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1013 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001014
1015 unsigned Dest = MI->getOperand(0).getReg();
1016 unsigned Ptr = MI->getOperand(1).getReg();
1017 unsigned Incr = MI->getOperand(2).getReg();
1018
Akira Hatanaka4061da12011-07-19 20:11:17 +00001019 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1020 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001021 unsigned Mask = RegInfo.createVirtualRegister(RC);
1022 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001023 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1024 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001025 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001026 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1027 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1028 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1029 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1030 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001031 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001032 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1033 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1034 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1035 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1036 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001037
1038 // insert new blocks after the current block
1039 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1040 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001041 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001042 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1043 MachineFunction::iterator It = BB;
1044 ++It;
1045 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001046 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001047 MF->insert(It, exitMBB);
1048
1049 // Transfer the remainder of BB and its successor edges to exitMBB.
1050 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001051 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001052 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1053
Akira Hatanaka81b44112011-07-19 17:09:53 +00001054 BB->addSuccessor(loopMBB);
1055 loopMBB->addSuccessor(loopMBB);
1056 loopMBB->addSuccessor(sinkMBB);
1057 sinkMBB->addSuccessor(exitMBB);
1058
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001059 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001060 // addiu masklsb2,$0,-4 # 0xfffffffc
1061 // and alignedaddr,ptr,masklsb2
1062 // andi ptrlsb2,ptr,3
1063 // sll shiftamt,ptrlsb2,3
1064 // ori maskupper,$0,255 # 0xff
1065 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001066 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001067 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001068
1069 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001070 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1071 .addReg(Mips::ZERO).addImm(-4);
1072 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1073 .addReg(Ptr).addReg(MaskLSB2);
1074 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1075 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1076 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1077 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001078 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1079 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001080 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001081 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001082
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001083 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001084 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001085 // ll oldval,0(alignedaddr)
1086 // binop binopres,oldval,incr2
1087 // and newval,binopres,mask
1088 // and maskedoldval0,oldval,mask2
1089 // or storeval,maskedoldval0,newval
1090 // sc success,storeval,0(alignedaddr)
1091 // beq success,$0,loopMBB
1092
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001093 // atomic.swap
1094 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001095 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001096 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001097 // and maskedoldval0,oldval,mask2
1098 // or storeval,maskedoldval0,newval
1099 // sc success,storeval,0(alignedaddr)
1100 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001101
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001102 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001103 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001104 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001105 // and andres, oldval, incr2
1106 // nor binopres, $0, andres
1107 // and newval, binopres, mask
1108 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1109 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1110 .addReg(Mips::ZERO).addReg(AndRes);
1111 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001112 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001113 // <binop> binopres, oldval, incr2
1114 // and newval, binopres, mask
1115 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1116 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001117 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001118 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001119 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001120 }
1121
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001122 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001123 .addReg(OldVal).addReg(Mask2);
1124 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001125 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001126 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001127 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001128 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001129 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001130
Akira Hatanaka939ece12011-07-19 03:42:13 +00001131 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001132 // and maskedoldval1,oldval,mask
1133 // srl srlres,maskedoldval1,shiftamt
1134 // sll sllres,srlres,24
1135 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001136 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001137 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001138
Akira Hatanaka4061da12011-07-19 20:11:17 +00001139 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1140 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001141 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1142 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001143 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1144 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001145 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001146 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001147
1148 MI->eraseFromParent(); // The instruction is gone now.
1149
Akira Hatanaka939ece12011-07-19 03:42:13 +00001150 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001151}
1152
1153MachineBasicBlock *
1154MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001155 MachineBasicBlock *BB,
1156 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001157 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001158
1159 MachineFunction *MF = BB->getParent();
1160 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001161 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001162 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1163 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001164 unsigned LL, SC, ZERO, BNE, BEQ;
1165
1166 if (Size == 4) {
1167 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1168 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1169 ZERO = Mips::ZERO;
1170 BNE = Mips::BNE;
1171 BEQ = Mips::BEQ;
1172 }
1173 else {
1174 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1175 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1176 ZERO = Mips::ZERO_64;
1177 BNE = Mips::BNE64;
1178 BEQ = Mips::BEQ64;
1179 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001180
1181 unsigned Dest = MI->getOperand(0).getReg();
1182 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001183 unsigned OldVal = MI->getOperand(2).getReg();
1184 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001185
Akira Hatanaka4061da12011-07-19 20:11:17 +00001186 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001187
1188 // insert new blocks after the current block
1189 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1190 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1191 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1192 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1193 MachineFunction::iterator It = BB;
1194 ++It;
1195 MF->insert(It, loop1MBB);
1196 MF->insert(It, loop2MBB);
1197 MF->insert(It, exitMBB);
1198
1199 // Transfer the remainder of BB and its successor edges to exitMBB.
1200 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001201 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001202 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1203
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001204 // thisMBB:
1205 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001206 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001207 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001208 loop1MBB->addSuccessor(exitMBB);
1209 loop1MBB->addSuccessor(loop2MBB);
1210 loop2MBB->addSuccessor(loop1MBB);
1211 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001212
1213 // loop1MBB:
1214 // ll dest, 0(ptr)
1215 // bne dest, oldval, exitMBB
1216 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001217 BuildMI(BB, dl, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1218 BuildMI(BB, dl, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001219 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001220
1221 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001222 // sc success, newval, 0(ptr)
1223 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001224 BB = loop2MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001225 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001226 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka59068062011-11-11 04:14:30 +00001227 BuildMI(BB, dl, TII->get(BEQ))
1228 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001229
1230 MI->eraseFromParent(); // The instruction is gone now.
1231
Akira Hatanaka939ece12011-07-19 03:42:13 +00001232 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001233}
1234
1235MachineBasicBlock *
1236MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001237 MachineBasicBlock *BB,
1238 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001239 assert((Size == 1 || Size == 2) &&
1240 "Unsupported size for EmitAtomicCmpSwapPartial.");
1241
1242 MachineFunction *MF = BB->getParent();
1243 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1244 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1245 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1246 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001247 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1248 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001249
1250 unsigned Dest = MI->getOperand(0).getReg();
1251 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001252 unsigned CmpVal = MI->getOperand(2).getReg();
1253 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001254
Akira Hatanaka4061da12011-07-19 20:11:17 +00001255 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1256 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001257 unsigned Mask = RegInfo.createVirtualRegister(RC);
1258 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001259 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1260 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1261 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1262 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1263 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1264 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1265 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1266 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1267 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1268 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1269 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1270 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1271 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1272 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001273
1274 // insert new blocks after the current block
1275 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1276 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1277 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001278 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001279 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1280 MachineFunction::iterator It = BB;
1281 ++It;
1282 MF->insert(It, loop1MBB);
1283 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001284 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001285 MF->insert(It, exitMBB);
1286
1287 // Transfer the remainder of BB and its successor edges to exitMBB.
1288 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001289 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001290 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1291
Akira Hatanaka81b44112011-07-19 17:09:53 +00001292 BB->addSuccessor(loop1MBB);
1293 loop1MBB->addSuccessor(sinkMBB);
1294 loop1MBB->addSuccessor(loop2MBB);
1295 loop2MBB->addSuccessor(loop1MBB);
1296 loop2MBB->addSuccessor(sinkMBB);
1297 sinkMBB->addSuccessor(exitMBB);
1298
Akira Hatanaka70564a92011-07-19 18:14:26 +00001299 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001300 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001301 // addiu masklsb2,$0,-4 # 0xfffffffc
1302 // and alignedaddr,ptr,masklsb2
1303 // andi ptrlsb2,ptr,3
1304 // sll shiftamt,ptrlsb2,3
1305 // ori maskupper,$0,255 # 0xff
1306 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001307 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001308 // andi maskedcmpval,cmpval,255
1309 // sll shiftedcmpval,maskedcmpval,shiftamt
1310 // andi maskednewval,newval,255
1311 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001312 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001313 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1314 .addReg(Mips::ZERO).addImm(-4);
1315 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1316 .addReg(Ptr).addReg(MaskLSB2);
1317 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1318 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1319 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1320 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001321 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1322 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001323 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001324 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1325 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001326 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1327 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001328 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1329 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001330 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1331 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001332
1333 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001334 // ll oldval,0(alginedaddr)
1335 // and maskedoldval0,oldval,mask
1336 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001337 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001338 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001339 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1340 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001341 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001342 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001343
1344 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001345 // and maskedoldval1,oldval,mask2
1346 // or storeval,maskedoldval1,shiftednewval
1347 // sc success,storeval,0(alignedaddr)
1348 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001349 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001350 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1351 .addReg(OldVal).addReg(Mask2);
1352 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1353 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001354 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001355 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001356 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001357 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001358
Akira Hatanaka939ece12011-07-19 03:42:13 +00001359 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001360 // srl srlres,maskedoldval0,shiftamt
1361 // sll sllres,srlres,24
1362 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001363 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001364 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001365
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001366 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1367 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001368 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1369 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001370 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001371 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001372
1373 MI->eraseFromParent(); // The instruction is gone now.
1374
Akira Hatanaka939ece12011-07-19 03:42:13 +00001375 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001376}
1377
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001378//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001379// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001380//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001381SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001382LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001383{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001384 MachineFunction &MF = DAG.getMachineFunction();
1385 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001386 unsigned SP = IsN64 ? Mips::SP_64 : Mips::SP;
Akira Hatanaka21afc632011-06-21 00:40:49 +00001387
1388 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001389 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1390 "Cannot lower if the alignment of the allocated space is larger than \
1391 that of the stack.");
1392
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001393 SDValue Chain = Op.getOperand(0);
1394 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001395 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001396
1397 // Get a reference from Mips stack pointer
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001398 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, SP, getPointerTy());
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001399
1400 // Subtract the dynamic size from the actual stack size to
1401 // obtain the new stack size.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001402 SDValue Sub = DAG.getNode(ISD::SUB, dl, getPointerTy(), StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001403
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001404 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001405 // must be placed in the stack pointer register.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001406 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, SP, Sub, SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001407
1408 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001409 // value and a chain
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001410 SDVTList VTLs = DAG.getVTList(getPointerTy(), MVT::Other);
Akira Hatanaka21afc632011-06-21 00:40:49 +00001411 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1412 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1413
1414 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001415}
1416
1417SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001418LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001419{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001420 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001421 // the block to branch to if the condition is true.
1422 SDValue Chain = Op.getOperand(0);
1423 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001424 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001425
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001426 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1427
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001428 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001429 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001430 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001431
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001432 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001433 Mips::CondCode CC =
1434 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001435 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001436
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001437 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001438 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001439}
1440
1441SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001442LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001443{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001444 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001445
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001446 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001447 if (Cond.getOpcode() != MipsISD::FPCmp)
1448 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001449
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001450 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1451 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001452}
1453
Dan Gohmand858e902010-04-17 15:26:15 +00001454SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1455 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001456 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001457 DebugLoc dl = Op.getDebugLoc();
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001458 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001459
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001460 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001461 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001462
Chris Lattnerb71b9092009-08-13 06:28:06 +00001463 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001464
Chris Lattnere3736f82009-08-13 05:41:27 +00001465 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001466 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1467 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001468 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001469 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1470 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001471 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001472 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001473 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001474 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1475 MipsII::MO_ABS_HI);
1476 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1477 MipsII::MO_ABS_LO);
1478 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1479 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001480 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001481 }
1482
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001483 EVT ValTy = Op.getValueType();
1484 bool HasGotOfst = (GV->hasInternalLinkage() ||
1485 (GV->hasLocalLinkage() && !isa<Function>(GV)));
1486 unsigned GotFlag = IsN64 ?
1487 (HasGotOfst ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT_DISP) :
Bruno Cardoso Lopese3d35722011-12-07 00:28:57 +00001488 (HasGotOfst ? MipsII::MO_GOT : MipsII::MO_GOT16);
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001489 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0, GotFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001490 GA = DAG.getNode(MipsISD::Wrapper, dl, ValTy, GA);
Akira Hatanaka82099682011-12-19 19:52:25 +00001491 SDValue ResNode = DAG.getLoad(ValTy, dl, DAG.getEntryNode(), GA,
1492 MachinePointerInfo(), false, false, false, 0);
Akira Hatanaka0f843822011-06-07 18:58:42 +00001493 // On functions and global targets not internal linked only
1494 // a load from got/GP is necessary for PIC to work.
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001495 if (!HasGotOfst)
Akira Hatanaka0f843822011-06-07 18:58:42 +00001496 return ResNode;
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001497 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0,
1498 IsN64 ? MipsII::MO_GOT_OFST :
1499 MipsII::MO_ABS_LO);
1500 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, GALo);
1501 return DAG.getNode(ISD::ADD, dl, ValTy, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001502}
1503
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001504SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1505 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001506 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1507 // FIXME there isn't actually debug info here
1508 DebugLoc dl = Op.getDebugLoc();
1509
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001510 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001511 // %hi/%lo relocation
Akira Hatanaka82099682011-12-19 19:52:25 +00001512 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true, MipsII::MO_ABS_HI);
1513 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true, MipsII::MO_ABS_LO);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001514 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1515 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1516 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001517 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001518
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001519 EVT ValTy = Op.getValueType();
1520 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1521 unsigned OFSTFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1522 SDValue BAGOTOffset = DAG.getBlockAddress(BA, ValTy, true, GOTFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001523 BAGOTOffset = DAG.getNode(MipsISD::Wrapper, dl, ValTy, BAGOTOffset);
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001524 SDValue BALOOffset = DAG.getBlockAddress(BA, ValTy, true, OFSTFlag);
Akira Hatanaka82099682011-12-19 19:52:25 +00001525 SDValue Load = DAG.getLoad(ValTy, dl, DAG.getEntryNode(), BAGOTOffset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001526 MachinePointerInfo(), false, false, false, 0);
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001527 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, BALOOffset);
1528 return DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001529}
1530
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001531SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001532LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001533{
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001534 // If the relocation model is PIC, use the General Dynamic TLS Model or
1535 // Local Dynamic TLS model, otherwise use the Initial Exec or
1536 // Local Exec TLS Model.
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001537
1538 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1539 DebugLoc dl = GA->getDebugLoc();
1540 const GlobalValue *GV = GA->getGlobal();
1541 EVT PtrVT = getPointerTy();
1542
1543 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1544 // General Dynamic TLS Model
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001545 bool LocalDynamic = GV->hasInternalLinkage();
1546 unsigned Flag = LocalDynamic ? MipsII::MO_TLSLDM :MipsII::MO_TLSGD;
1547 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, Flag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001548 SDValue Argument = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, TGA);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001549 unsigned PtrSize = PtrVT.getSizeInBits();
1550 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1551
Benjamin Kramer5eccf672011-12-11 12:21:34 +00001552 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001553
1554 ArgListTy Args;
1555 ArgListEntry Entry;
1556 Entry.Node = Argument;
Akira Hatanakaca074792011-12-08 20:34:32 +00001557 Entry.Ty = PtrTy;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001558 Args.push_back(Entry);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001559
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001560 std::pair<SDValue, SDValue> CallResult =
Akira Hatanakaca074792011-12-08 20:34:32 +00001561 LowerCallTo(DAG.getEntryNode(), PtrTy,
1562 false, false, false, false, 0, CallingConv::C, false, true,
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001563 TlsGetAddr, Args, DAG, dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001564
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001565 SDValue Ret = CallResult.first;
1566
1567 if (!LocalDynamic)
1568 return Ret;
1569
1570 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1571 MipsII::MO_DTPREL_HI);
1572 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, PtrVT, TGAHi);
1573 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1574 MipsII::MO_DTPREL_LO);
1575 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, TGALo);
1576 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Ret);
1577 return DAG.getNode(ISD::ADD, dl, PtrVT, Add, Lo);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001578 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001579
1580 SDValue Offset;
1581 if (GV->isDeclaration()) {
1582 // Initial Exec TLS Model
Akira Hatanakaca074792011-12-08 20:34:32 +00001583 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001584 MipsII::MO_GOTTPREL);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001585 TGA = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, TGA);
Akira Hatanakaca074792011-12-08 20:34:32 +00001586 Offset = DAG.getLoad(PtrVT, dl,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001587 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001588 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001589 } else {
1590 // Local Exec TLS Model
Akira Hatanakaca074792011-12-08 20:34:32 +00001591 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001592 MipsII::MO_TPREL_HI);
Akira Hatanakaca074792011-12-08 20:34:32 +00001593 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001594 MipsII::MO_TPREL_LO);
Akira Hatanakaca074792011-12-08 20:34:32 +00001595 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, PtrVT, TGAHi);
1596 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, TGALo);
1597 Offset = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001598 }
1599
1600 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1601 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001602}
1603
1604SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001605LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001606{
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001607 SDValue HiPart, JTI, JTILo;
Dale Johannesende064702009-02-06 21:50:26 +00001608 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001609 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001610 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Owen Andersone50ed302009-08-10 22:56:29 +00001611 EVT PtrVT = Op.getValueType();
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001612 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001613
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001614 if (!IsPIC && !IsN64) {
1615 JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MipsII::MO_ABS_HI);
1616 HiPart = DAG.getNode(MipsISD::Hi, dl, PtrVT, JTI);
1617 JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MipsII::MO_ABS_LO);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001618 } else {// Emit Load from Global Pointer
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001619 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1620 unsigned OfstFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1621 JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, GOTFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001622 JTI = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, JTI);
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001623 HiPart = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), JTI,
1624 MachinePointerInfo(), false, false, false, 0);
1625 JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OfstFlag);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001626 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001627
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001628 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, JTILo);
1629 return DAG.getNode(ISD::ADD, dl, PtrVT, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001630}
1631
Dan Gohman475871a2008-07-27 21:46:04 +00001632SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001633LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001634{
Dan Gohman475871a2008-07-27 21:46:04 +00001635 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001636 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001637 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001638 // FIXME there isn't actually debug info here
1639 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001640
1641 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001642 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001643 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001644 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001645 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001646 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001647 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1648 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001649 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001650
1651 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001652 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001653 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001654 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001655 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001656 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1657 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001658 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001659 } else {
Akira Hatanaka620db892011-11-16 22:44:38 +00001660 EVT ValTy = Op.getValueType();
1661 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1662 unsigned OFSTFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1663 SDValue CP = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1664 N->getOffset(), GOTFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001665 CP = DAG.getNode(MipsISD::Wrapper, dl, ValTy, CP);
Akira Hatanaka82099682011-12-19 19:52:25 +00001666 SDValue Load = DAG.getLoad(ValTy, dl, DAG.getEntryNode(), CP,
1667 MachinePointerInfo::getConstantPool(), false,
1668 false, false, 0);
Akira Hatanaka620db892011-11-16 22:44:38 +00001669 SDValue CPLo = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1670 N->getOffset(), OFSTFlag);
1671 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, CPLo);
1672 ResNode = DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001673 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001674
1675 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001676}
1677
Dan Gohmand858e902010-04-17 15:26:15 +00001678SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001679 MachineFunction &MF = DAG.getMachineFunction();
1680 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1681
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001682 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001683 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1684 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001685
1686 // vastart just stores the address of the VarArgsFrameIndex slot into the
1687 // memory location argument.
1688 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001689 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
Akira Hatanaka82099682011-12-19 19:52:25 +00001690 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001691}
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001692
1693// Called if the size of integer registers is large enough to hold the whole
1694// floating point number.
1695static SDValue LowerFCOPYSIGNLargeIntReg(SDValue Op, SelectionDAG &DAG) {
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001696 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001697 EVT ValTy = Op.getValueType();
1698 EVT IntValTy = MVT::getIntegerVT(ValTy.getSizeInBits());
1699 uint64_t Mask = (uint64_t)1 << (ValTy.getSizeInBits() - 1);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001700 DebugLoc dl = Op.getDebugLoc();
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001701 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, IntValTy, Op.getOperand(0));
1702 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, IntValTy, Op.getOperand(1));
1703 SDValue And0 = DAG.getNode(ISD::AND, dl, IntValTy, Op0,
1704 DAG.getConstant(Mask - 1, IntValTy));
1705 SDValue And1 = DAG.getNode(ISD::AND, dl, IntValTy, Op1,
1706 DAG.getConstant(Mask, IntValTy));
1707 SDValue Result = DAG.getNode(ISD::OR, dl, IntValTy, And0, And1);
1708 return DAG.getNode(ISD::BITCAST, dl, ValTy, Result);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001709}
1710
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001711// Called if the size of integer registers is not large enough to hold the whole
1712// floating point number (e.g. f64 & 32-bit integer register).
1713static SDValue
1714LowerFCOPYSIGNSmallIntReg(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001715 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001716 // Use ext/ins instructions if target architecture is Mips32r2.
1717 // Eliminate redundant mfc1 and mtc1 instructions.
1718 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001719
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001720 if (!isLittle)
1721 std::swap(LoIdx, HiIdx);
1722
1723 DebugLoc dl = Op.getDebugLoc();
1724 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1725 Op.getOperand(0),
1726 DAG.getConstant(LoIdx, MVT::i32));
1727 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1728 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1729 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1730 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1731 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1732 DAG.getConstant(0x7fffffff, MVT::i32));
1733 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1734 DAG.getConstant(0x80000000, MVT::i32));
1735 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1736
1737 if (!isLittle)
1738 std::swap(Word0, Word1);
1739
1740 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1741}
1742
Akira Hatanaka82099682011-12-19 19:52:25 +00001743SDValue
1744MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001745 EVT Ty = Op.getValueType();
1746
1747 assert(Ty == MVT::f32 || Ty == MVT::f64);
1748
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001749 if (Ty == MVT::f32 || HasMips64)
1750 return LowerFCOPYSIGNLargeIntReg(Op, DAG);
Akira Hatanaka82099682011-12-19 19:52:25 +00001751
1752 return LowerFCOPYSIGNSmallIntReg(Op, DAG, Subtarget->isLittle());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001753}
1754
Akira Hatanaka2e591472011-06-02 00:24:44 +00001755SDValue MipsTargetLowering::
1756LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001757 // check the depth
1758 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001759 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001760
1761 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1762 MFI->setFrameAddressIsTaken(true);
1763 EVT VT = Op.getValueType();
1764 DebugLoc dl = Op.getDebugLoc();
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001765 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
1766 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001767 return FrameAddr;
1768}
1769
Akira Hatanakadb548262011-07-19 23:30:50 +00001770// TODO: set SType according to the desired memory barrier behavior.
Akira Hatanaka82099682011-12-19 19:52:25 +00001771SDValue
1772MipsTargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG& DAG) const {
Akira Hatanakadb548262011-07-19 23:30:50 +00001773 unsigned SType = 0;
1774 DebugLoc dl = Op.getDebugLoc();
1775 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1776 DAG.getConstant(SType, MVT::i32));
1777}
1778
Eli Friedman14648462011-07-27 22:21:52 +00001779SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1780 SelectionDAG& DAG) const {
1781 // FIXME: Need pseudo-fence for 'singlethread' fences
1782 // FIXME: Set SType for weaker fences where supported/appropriate.
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
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001789//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001790// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001791//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001792
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001793//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001794// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001795// Mips O32 ABI rules:
1796// ---
1797// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001798// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001799// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001800// f64 - Only passed in two aliased f32 registers if no int reg has been used
1801// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001802// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1803// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001804//
1805// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001806//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001807
Duncan Sands1e96bab2010-11-04 10:49:57 +00001808static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001809 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001810 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1811
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001812 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001813
1814 static const unsigned IntRegs[] = {
1815 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1816 };
1817 static const unsigned F32Regs[] = {
1818 Mips::F12, Mips::F14
1819 };
1820 static const unsigned F64Regs[] = {
1821 Mips::D6, Mips::D7
1822 };
1823
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001824 // ByVal Args
1825 if (ArgFlags.isByVal()) {
1826 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1827 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1828 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1829 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1830 r < std::min(IntRegsSize, NextReg); ++r)
1831 State.AllocateReg(IntRegs[r]);
1832 return false;
1833 }
1834
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001835 // Promote i8 and i16
1836 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1837 LocVT = MVT::i32;
1838 if (ArgFlags.isSExt())
1839 LocInfo = CCValAssign::SExt;
1840 else if (ArgFlags.isZExt())
1841 LocInfo = CCValAssign::ZExt;
1842 else
1843 LocInfo = CCValAssign::AExt;
1844 }
1845
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001846 unsigned Reg;
1847
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001848 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1849 // is true: function is vararg, argument is 3rd or higher, there is previous
1850 // argument which is not f32 or f64.
1851 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1852 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001853 unsigned OrigAlign = ArgFlags.getOrigAlign();
1854 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001855
1856 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001857 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001858 // If this is the first part of an i64 arg,
1859 // the allocated register must be either A0 or A2.
1860 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1861 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001862 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001863 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1864 // Allocate int register and shadow next int register. If first
1865 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001866 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1867 if (Reg == Mips::A1 || Reg == Mips::A3)
1868 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1869 State.AllocateReg(IntRegs, IntRegsSize);
1870 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001871 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1872 // we are guaranteed to find an available float register
1873 if (ValVT == MVT::f32) {
1874 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1875 // Shadow int register
1876 State.AllocateReg(IntRegs, IntRegsSize);
1877 } else {
1878 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1879 // Shadow int registers
1880 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1881 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1882 State.AllocateReg(IntRegs, IntRegsSize);
1883 State.AllocateReg(IntRegs, IntRegsSize);
1884 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001885 } else
1886 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001887
Akira Hatanakad37776d2011-05-20 21:39:54 +00001888 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1889 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1890
1891 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001892 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001893 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001894 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001895
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001896 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001897}
1898
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00001899static const unsigned Mips64IntRegs[8] =
1900 {Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
1901 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64};
1902static const unsigned Mips64DPRegs[8] =
1903 {Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
1904 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64};
1905
1906static bool CC_Mips64Byval(unsigned ValNo, MVT ValVT, MVT LocVT,
1907 CCValAssign::LocInfo LocInfo,
1908 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1909 unsigned Align = std::max(ArgFlags.getByValAlign(), (unsigned)8);
1910 unsigned Size = (ArgFlags.getByValSize() + 7) / 8 * 8;
1911 unsigned FirstIdx = State.getFirstUnallocated(Mips64IntRegs, 8);
1912
1913 assert(Align <= 16 && "Cannot handle alignments larger than 16.");
1914
1915 // If byval is 16-byte aligned, the first arg register must be even.
1916 if ((Align == 16) && (FirstIdx % 2)) {
1917 State.AllocateReg(Mips64IntRegs[FirstIdx], Mips64DPRegs[FirstIdx]);
1918 ++FirstIdx;
1919 }
1920
1921 // Mark the registers allocated.
1922 for (unsigned I = FirstIdx; Size && (I < 8); Size -= 8, ++I)
1923 State.AllocateReg(Mips64IntRegs[I], Mips64DPRegs[I]);
1924
1925 // Allocate space on caller's stack.
1926 unsigned Offset = State.AllocateStack(Size, Align);
1927
1928 if (FirstIdx < 8)
1929 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Mips64IntRegs[FirstIdx],
1930 LocVT, LocInfo));
1931 else
1932 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
1933
1934 return true;
1935}
1936
1937#include "MipsGenCallingConv.inc"
1938
Akira Hatanaka49617092011-11-14 19:02:54 +00001939static void
1940AnalyzeMips64CallOperands(CCState CCInfo,
1941 const SmallVectorImpl<ISD::OutputArg> &Outs) {
1942 unsigned NumOps = Outs.size();
1943 for (unsigned i = 0; i != NumOps; ++i) {
1944 MVT ArgVT = Outs[i].VT;
1945 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
1946 bool R;
1947
1948 if (Outs[i].IsFixed)
1949 R = CC_MipsN(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
1950 else
1951 R = CC_MipsN_VarArg(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
1952
Akira Hatanaka49617092011-11-14 19:02:54 +00001953 if (R) {
Benjamin Kramer6296ee32011-11-14 19:51:48 +00001954#ifndef NDEBUG
Akira Hatanaka49617092011-11-14 19:02:54 +00001955 dbgs() << "Call operand #" << i << " has unhandled type "
1956 << EVT(ArgVT).getEVTString();
1957#endif
1958 llvm_unreachable(0);
1959 }
1960 }
1961}
1962
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001963//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001964// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001965//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001966
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001967static const unsigned O32IntRegsSize = 4;
1968
1969static const unsigned O32IntRegs[] = {
1970 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1971};
1972
Akira Hatanaka373e3a42011-09-23 00:58:33 +00001973// Return next O32 integer argument register.
1974static unsigned getNextIntArgReg(unsigned Reg) {
1975 assert((Reg == Mips::A0) || (Reg == Mips::A2));
1976 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
1977}
1978
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001979// Write ByVal Arg to arg registers and stack.
1980static void
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001981WriteByValArg(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001982 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1983 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1984 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001985 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001986 MVT PtrType, bool isLittle) {
1987 unsigned LocMemOffset = VA.getLocMemOffset();
1988 unsigned Offset = 0;
1989 uint32_t RemainingSize = Flags.getByValSize();
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001990 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001991
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001992 // Copy the first 4 words of byval arg to registers A0 - A3.
1993 // FIXME: Use a stricter alignment if it enables better optimization in passes
1994 // run later.
1995 for (; RemainingSize >= 4 && LocMemOffset < 4 * 4;
1996 Offset += 4, RemainingSize -= 4, LocMemOffset += 4) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001997 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001998 DAG.getConstant(Offset, MVT::i32));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001999 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
Akira Hatanaka82099682011-12-19 19:52:25 +00002000 MachinePointerInfo(), false, false, false,
2001 std::min(ByValAlign, (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002002 MemOpChains.push_back(LoadVal.getValue(1));
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002003 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002004 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
2005 }
2006
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002007 if (RemainingSize == 0)
2008 return;
2009
2010 // If there still is a register available for argument passing, write the
2011 // remaining part of the structure to it using subword loads and shifts.
2012 if (LocMemOffset < 4 * 4) {
2013 assert(RemainingSize <= 3 && RemainingSize >= 1 &&
2014 "There must be one to three bytes remaining.");
2015 unsigned LoadSize = (RemainingSize == 3 ? 2 : RemainingSize);
2016 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2017 DAG.getConstant(Offset, MVT::i32));
2018 unsigned Alignment = std::min(ByValAlign, (unsigned )4);
2019 SDValue LoadVal = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2020 LoadPtr, MachinePointerInfo(),
2021 MVT::getIntegerVT(LoadSize * 8), false,
2022 false, Alignment);
2023 MemOpChains.push_back(LoadVal.getValue(1));
2024
2025 // If target is big endian, shift it to the most significant half-word or
2026 // byte.
2027 if (!isLittle)
2028 LoadVal = DAG.getNode(ISD::SHL, dl, MVT::i32, LoadVal,
2029 DAG.getConstant(32 - LoadSize * 8, MVT::i32));
2030
2031 Offset += LoadSize;
2032 RemainingSize -= LoadSize;
2033
2034 // Read second subword if necessary.
2035 if (RemainingSize != 0) {
2036 assert(RemainingSize == 1 && "There must be one byte remaining.");
2037 LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2038 DAG.getConstant(Offset, MVT::i32));
2039 unsigned Alignment = std::min(ByValAlign, (unsigned )2);
2040 SDValue Subword = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2041 LoadPtr, MachinePointerInfo(),
2042 MVT::i8, false, false, Alignment);
2043 MemOpChains.push_back(Subword.getValue(1));
2044 // Insert the loaded byte to LoadVal.
2045 // FIXME: Use INS if supported by target.
2046 unsigned ShiftAmt = isLittle ? 16 : 8;
2047 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i32, Subword,
2048 DAG.getConstant(ShiftAmt, MVT::i32));
2049 LoadVal = DAG.getNode(ISD::OR, dl, MVT::i32, LoadVal, Shift);
2050 }
2051
2052 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
2053 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
2054 return;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002055 }
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002056
2057 // Create a fixed object on stack at offset LocMemOffset and copy
2058 // remaining part of byval arg to it using memcpy.
2059 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2060 DAG.getConstant(Offset, MVT::i32));
2061 LastFI = MFI->CreateFixedObject(RemainingSize, LocMemOffset, true);
2062 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002063 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2064 DAG.getConstant(RemainingSize, MVT::i32),
2065 std::min(ByValAlign, (unsigned)4),
2066 /*isVolatile=*/false, /*AlwaysInline=*/false,
2067 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002068}
2069
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002070// Copy Mips64 byVal arg to registers and stack.
2071void static
2072PassByValArg64(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
2073 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
2074 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
2075 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
2076 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2077 EVT PtrTy, bool isLittle) {
2078 unsigned ByValSize = Flags.getByValSize();
2079 unsigned Alignment = std::min(Flags.getByValAlign(), (unsigned)8);
2080 bool IsRegLoc = VA.isRegLoc();
2081 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
2082 unsigned LocMemOffset = 0;
Akira Hatanaka16040852011-11-15 18:42:25 +00002083 unsigned MemCpySize = ByValSize;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002084
2085 if (!IsRegLoc)
2086 LocMemOffset = VA.getLocMemOffset();
2087 else {
2088 const unsigned *Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8,
2089 VA.getLocReg());
2090 const unsigned *RegEnd = Mips64IntRegs + 8;
2091
2092 // Copy double words to registers.
2093 for (; (Reg != RegEnd) && (ByValSize >= Offset + 8); ++Reg, Offset += 8) {
2094 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2095 DAG.getConstant(Offset, PtrTy));
2096 SDValue LoadVal = DAG.getLoad(MVT::i64, dl, Chain, LoadPtr,
2097 MachinePointerInfo(), false, false, false,
2098 Alignment);
2099 MemOpChains.push_back(LoadVal.getValue(1));
2100 RegsToPass.push_back(std::make_pair(*Reg, LoadVal));
2101 }
2102
Akira Hatanaka16040852011-11-15 18:42:25 +00002103 // Return if the struct has been fully copied.
2104 if (!(MemCpySize = ByValSize - Offset))
2105 return;
2106
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002107 // If there is an argument register available, copy the remainder of the
2108 // byval argument with sub-doubleword loads and shifts.
Akira Hatanaka16040852011-11-15 18:42:25 +00002109 if (Reg != RegEnd) {
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002110 assert((ByValSize < Offset + 8) &&
2111 "Size of the remainder should be smaller than 8-byte.");
2112 SDValue Val;
2113 for (unsigned LoadSize = 4; Offset < ByValSize; LoadSize /= 2) {
2114 unsigned RemSize = ByValSize - Offset;
2115
2116 if (RemSize < LoadSize)
2117 continue;
2118
2119 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2120 DAG.getConstant(Offset, PtrTy));
2121 SDValue LoadVal =
2122 DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i64, Chain, LoadPtr,
2123 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
2124 false, false, Alignment);
2125 MemOpChains.push_back(LoadVal.getValue(1));
2126
2127 // Offset in number of bits from double word boundary.
2128 unsigned OffsetDW = (Offset % 8) * 8;
2129 unsigned Shamt = isLittle ? OffsetDW : 64 - (OffsetDW + LoadSize * 8);
2130 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i64, LoadVal,
2131 DAG.getConstant(Shamt, MVT::i32));
2132
2133 Val = Val.getNode() ? DAG.getNode(ISD::OR, dl, MVT::i64, Val, Shift) :
2134 Shift;
2135 Offset += LoadSize;
2136 Alignment = std::min(Alignment, LoadSize);
2137 }
2138
2139 RegsToPass.push_back(std::make_pair(*Reg, Val));
2140 return;
2141 }
2142 }
2143
Akira Hatanaka16040852011-11-15 18:42:25 +00002144 assert(MemCpySize && "MemCpySize must not be zero.");
2145
2146 // Create a fixed object on stack at offset LocMemOffset and copy
2147 // remainder of byval arg to it with memcpy.
2148 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2149 DAG.getConstant(Offset, PtrTy));
2150 LastFI = MFI->CreateFixedObject(MemCpySize, LocMemOffset, true);
2151 SDValue Dst = DAG.getFrameIndex(LastFI, PtrTy);
2152 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2153 DAG.getConstant(MemCpySize, PtrTy), Alignment,
2154 /*isVolatile=*/false, /*AlwaysInline=*/false,
2155 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002156}
2157
Dan Gohman98ca4f22009-08-05 01:29:28 +00002158/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002159/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002160/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002161SDValue
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002162MipsTargetLowering::LowerCall(SDValue InChain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002163 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002164 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002165 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002166 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002167 const SmallVectorImpl<ISD::InputArg> &Ins,
2168 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002169 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002170 // MIPs target does not yet support tail call optimization.
2171 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002172
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002173 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002174 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002175 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002176 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00002177 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002178
2179 // Analyze operands of the call, assigning locations to each operand.
2180 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002181 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002182 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002183
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002184 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002185 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanaka49617092011-11-14 19:02:54 +00002186 else if (HasMips64)
2187 AnalyzeMips64CallOperands(CCInfo, Outs);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00002188 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002189 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002190
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002191 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002192 unsigned NextStackOffset = CCInfo.getNextStackOffset();
2193
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002194 // Chain is the output chain of the last Load/Store or CopyToReg node.
2195 // ByValChain is the output chain of the last Memcpy node created for copying
2196 // byval arguments to the stack.
2197 SDValue Chain, CallSeqStart, ByValChain;
2198 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
2199 Chain = CallSeqStart = DAG.getCALLSEQ_START(InChain, NextStackOffsetVal);
2200 ByValChain = InChain;
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002201
2202 // If this is the first call, create a stack frame object that points to
2203 // a location to which .cprestore saves $gp.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002204 if (IsO32 && IsPIC && !MipsFI->getGPFI())
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002205 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
2206
Akira Hatanaka21afc632011-06-21 00:40:49 +00002207 // Get the frame index of the stack frame object that points to the location
2208 // of dynamically allocated area on the stack.
2209 int DynAllocFI = MipsFI->getDynAllocFI();
2210
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002211 // Update size of the maximum argument space.
2212 // For O32, a minimum of four words (16 bytes) of argument space is
2213 // allocated.
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002214 if (IsO32)
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002215 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
2216
2217 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
2218
2219 if (MaxCallFrameSize < NextStackOffset) {
2220 MipsFI->setMaxCallFrameSize(NextStackOffset);
2221
Akira Hatanaka21afc632011-06-21 00:40:49 +00002222 // Set the offsets relative to $sp of the $gp restore slot and dynamically
2223 // allocated stack space. These offsets must be aligned to a boundary
2224 // determined by the stack alignment of the ABI.
2225 unsigned StackAlignment = TFL->getStackAlignment();
2226 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
2227 StackAlignment * StackAlignment;
2228
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002229 if (MipsFI->needGPSaveRestore())
Akira Hatanaka21afc632011-06-21 00:40:49 +00002230 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
2231
2232 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002233 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002234
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002235 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002236 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
2237 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002238
Eric Christopher471e4222011-06-08 23:55:35 +00002239 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00002240
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002241 // Walk the register/memloc assignments, inserting copies/loads.
2242 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002243 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002244 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002245 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002246 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2247
2248 // ByVal Arg.
2249 if (Flags.isByVal()) {
2250 assert(Flags.getByValSize() &&
2251 "ByVal args of size 0 should have been ignored by front-end.");
2252 if (IsO32)
2253 WriteByValArg(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2254 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2255 Subtarget->isLittle());
2256 else
2257 PassByValArg64(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2258 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2259 Subtarget->isLittle());
2260 continue;
2261 }
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002262
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002263 // Promote the value if needed.
2264 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002265 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002266 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002267 if (VA.isRegLoc()) {
2268 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
2269 (ValVT == MVT::f64 && LocVT == MVT::i64))
2270 Arg = DAG.getNode(ISD::BITCAST, dl, LocVT, Arg);
2271 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002272 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2273 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002274 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2275 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002276 if (!Subtarget->isLittle())
2277 std::swap(Lo, Hi);
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002278 unsigned LocRegLo = VA.getLocReg();
2279 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2280 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2281 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002282 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002283 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002284 }
2285 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002286 case CCValAssign::SExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002287 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002288 break;
2289 case CCValAssign::ZExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002290 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002291 break;
2292 case CCValAssign::AExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002293 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002294 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002295 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002296
2297 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002298 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002299 if (VA.isRegLoc()) {
2300 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002301 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002302 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002303
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002304 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002305 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002306
Chris Lattnere0b12152008-03-17 06:57:02 +00002307 // Create the frame index object for this incoming parameter
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002308 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002309 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00002310 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00002311
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002312 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002313 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00002314 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Akira Hatanaka82099682011-12-19 19:52:25 +00002315 MachinePointerInfo(), false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002316 }
2317
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002318 // Extend range of indices of frame objects for outgoing arguments that were
2319 // created during this function call. Skip this step if no such objects were
2320 // created.
2321 if (LastFI)
2322 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2323
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002324 // If a memcpy has been created to copy a byval arg to a stack, replace the
2325 // chain input of CallSeqStart with ByValChain.
2326 if (InChain != ByValChain)
2327 DAG.UpdateNodeOperands(CallSeqStart.getNode(), ByValChain,
2328 NextStackOffsetVal);
2329
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002330 // Transform all store nodes into one single node because all store
2331 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002332 if (!MemOpChains.empty())
2333 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002334 &MemOpChains[0], MemOpChains.size());
2335
Bill Wendling056292f2008-09-16 21:48:12 +00002336 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002337 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2338 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002339 unsigned char OpFlag;
2340 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002341 bool GlobalOrExternal = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002342 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002343
2344 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002345 if (IsPICCall && G->getGlobal()->hasInternalLinkage()) {
2346 OpFlag = IsO32 ? MipsII::MO_GOT : MipsII::MO_GOT_PAGE;
2347 unsigned char LoFlag = IsO32 ? MipsII::MO_ABS_LO : MipsII::MO_GOT_OFST;
2348 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(), 0,
2349 OpFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002350 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002351 0, LoFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002352 } else {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002353 OpFlag = IsPICCall ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002354 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2355 getPointerTy(), 0, OpFlag);
2356 }
2357
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002358 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002359 }
2360 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002361 if (IsN64 || (!IsO32 && IsPIC))
2362 OpFlag = MipsII::MO_GOT_DISP;
2363 else if (!IsPIC) // !N64 && static
2364 OpFlag = MipsII::MO_NO_FLAG;
2365 else // O32 & PIC
2366 OpFlag = MipsII::MO_GOT_CALL;
Akira Hatanaka82099682011-12-19 19:52:25 +00002367 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2368 OpFlag);
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002369 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002370 }
2371
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002372 SDValue InFlag;
2373
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002374 // Create nodes that load address of callee and copy it to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002375 if (IsPICCall) {
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002376 if (GlobalOrExternal) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002377 // Load callee address
Akira Hatanaka6df7e232011-12-09 01:53:17 +00002378 Callee = DAG.getNode(MipsISD::Wrapper, dl, getPointerTy(), Callee);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002379 SDValue LoadValue = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
2380 Callee, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002381 false, false, false, 0);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002382
2383 // Use GOT+LO if callee has internal linkage.
2384 if (CalleeLo.getNode()) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002385 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, getPointerTy(), CalleeLo);
2386 Callee = DAG.getNode(ISD::ADD, dl, getPointerTy(), LoadValue, Lo);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002387 } else
2388 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002389 }
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002390 }
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002391
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002392 // T9 should contain the address of the callee function if
2393 // -reloction-model=pic or it is an indirect call.
2394 if (IsPICCall || !GlobalOrExternal) {
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002395 // copy to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002396 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
2397 Chain = DAG.getCopyToReg(Chain, dl, T9Reg, Callee, SDValue(0, 0));
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002398 InFlag = Chain.getValue(1);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002399 Callee = DAG.getRegister(T9Reg, getPointerTy());
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002400 }
Bill Wendling056292f2008-09-16 21:48:12 +00002401
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002402 // Build a sequence of copy-to-reg nodes chained together with token
2403 // chain and flag operands which copy the outgoing args into registers.
2404 // The InFlag in necessary since all emitted instructions must be
2405 // stuck together.
2406 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2407 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2408 RegsToPass[i].second, InFlag);
2409 InFlag = Chain.getValue(1);
2410 }
2411
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002412 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002413 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002414 //
2415 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002416 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002417 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002418 Ops.push_back(Chain);
2419 Ops.push_back(Callee);
2420
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002421 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002422 // known live into the call.
2423 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2424 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2425 RegsToPass[i].second.getValueType()));
2426
Gabor Greifba36cb52008-08-28 21:40:38 +00002427 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002428 Ops.push_back(InFlag);
2429
Dale Johannesen33c960f2009-02-04 20:06:27 +00002430 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002431 InFlag = Chain.getValue(1);
2432
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002433 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002434 Chain = DAG.getCALLSEQ_END(Chain,
2435 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002436 DAG.getIntPtrConstant(0, true), InFlag);
2437 InFlag = Chain.getValue(1);
2438
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002439 // Handle result values, copying them out of physregs into vregs that we
2440 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002441 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2442 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002443}
2444
Dan Gohman98ca4f22009-08-05 01:29:28 +00002445/// LowerCallResult - Lower the result values of a call into the
2446/// appropriate copies out of appropriate physical registers.
2447SDValue
2448MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002449 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002450 const SmallVectorImpl<ISD::InputArg> &Ins,
2451 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002452 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002453 // Assign locations to each value returned by this call.
2454 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002455 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2456 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002457
Dan Gohman98ca4f22009-08-05 01:29:28 +00002458 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002459
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002460 // Copy all of the result registers out of their specified physreg.
2461 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002462 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002463 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002464 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002465 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002466 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002467
Dan Gohman98ca4f22009-08-05 01:29:28 +00002468 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002469}
2470
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002471//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002472// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002473//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002474static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2475 std::vector<SDValue>& OutChains,
2476 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2477 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2478 unsigned LocMem = VA.getLocMemOffset();
2479 unsigned FirstWord = LocMem / 4;
2480
2481 // copy register A0 - A3 to frame object
2482 for (unsigned i = 0; i < NumWords; ++i) {
2483 unsigned CurWord = FirstWord + i;
2484 if (CurWord >= O32IntRegsSize)
2485 break;
2486
2487 unsigned SrcReg = O32IntRegs[CurWord];
2488 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2489 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2490 DAG.getConstant(i * 4, MVT::i32));
2491 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2492 StorePtr, MachinePointerInfo(), false,
2493 false, 0);
2494 OutChains.push_back(Store);
2495 }
2496}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002497
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002498// Create frame object on stack and copy registers used for byval passing to it.
2499static unsigned
2500CopyMips64ByValRegs(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2501 std::vector<SDValue>& OutChains, SelectionDAG &DAG,
2502 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2503 MachineFrameInfo *MFI, bool IsRegLoc,
2504 SmallVectorImpl<SDValue> &InVals, MipsFunctionInfo *MipsFI,
2505 EVT PtrTy) {
2506 const unsigned *Reg = Mips64IntRegs + 8;
2507 int FOOffset; // Frame object offset from virtual frame pointer.
2508
2509 if (IsRegLoc) {
2510 Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8, VA.getLocReg());
2511 FOOffset = (Reg - Mips64IntRegs) * 8 - 8 * 8;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002512 }
2513 else
2514 FOOffset = VA.getLocMemOffset();
2515
2516 // Create frame object.
2517 unsigned NumRegs = (Flags.getByValSize() + 7) / 8;
2518 unsigned LastFI = MFI->CreateFixedObject(NumRegs * 8, FOOffset, true);
2519 SDValue FIN = DAG.getFrameIndex(LastFI, PtrTy);
2520 InVals.push_back(FIN);
2521
2522 // Copy arg registers.
2523 for (unsigned I = 0; (Reg != Mips64IntRegs + 8) && (I < NumRegs);
2524 ++Reg, ++I) {
2525 unsigned VReg = AddLiveIn(MF, *Reg, Mips::CPU64RegsRegisterClass);
2526 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, PtrTy, FIN,
2527 DAG.getConstant(I * 8, PtrTy));
2528 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(VReg, MVT::i64),
2529 StorePtr, MachinePointerInfo(), false,
2530 false, 0);
2531 OutChains.push_back(Store);
2532 }
2533
2534 return LastFI;
2535}
2536
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002537/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002538/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002539SDValue
2540MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002541 CallingConv::ID CallConv,
2542 bool isVarArg,
Akira Hatanaka82099682011-12-19 19:52:25 +00002543 const SmallVectorImpl<ISD::InputArg> &Ins,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002544 DebugLoc dl, SelectionDAG &DAG,
2545 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002546 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002547 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002548 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002549 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002550
Dan Gohman1e93df62010-04-17 14:41:14 +00002551 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002552
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002553 // Used with vargs to acumulate store chains.
2554 std::vector<SDValue> OutChains;
2555
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002556 // Assign locations to all of the incoming arguments.
2557 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002558 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002559 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002560
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002561 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002562 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002563 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002564 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002565
Akira Hatanaka43299772011-05-20 23:22:14 +00002566 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002567
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002568 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002569 CCValAssign &VA = ArgLocs[i];
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002570 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002571 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2572 bool IsRegLoc = VA.isRegLoc();
2573
2574 if (Flags.isByVal()) {
2575 assert(Flags.getByValSize() &&
2576 "ByVal args of size 0 should have been ignored by front-end.");
2577 if (IsO32) {
2578 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2579 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2580 true);
2581 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2582 InVals.push_back(FIN);
2583 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2584 } else // N32/64
2585 LastFI = CopyMips64ByValRegs(MF, Chain, dl, OutChains, DAG, VA, Flags,
2586 MFI, IsRegLoc, InVals, MipsFI,
2587 getPointerTy());
2588 continue;
2589 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002590
2591 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002592 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002593 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002594 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002595 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002596
Owen Anderson825b72b2009-08-11 20:47:22 +00002597 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002598 RC = Mips::CPURegsRegisterClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002599 else if (RegVT == MVT::i64)
2600 RC = Mips::CPU64RegsRegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002601 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002602 RC = Mips::FGR32RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002603 else if (RegVT == MVT::f64)
Akira Hatanakaf40de9d2011-09-26 21:55:17 +00002604 RC = HasMips64 ? Mips::FGR64RegisterClass : Mips::AFGR64RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002605 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002606 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002607
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002608 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002609 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002610 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002611 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002612
2613 // If this is an 8 or 16-bit value, it has been passed promoted
2614 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002615 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002616 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002617 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002618 if (VA.getLocInfo() == CCValAssign::SExt)
2619 Opcode = ISD::AssertSext;
2620 else if (VA.getLocInfo() == CCValAssign::ZExt)
2621 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002622 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002623 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002624 DAG.getValueType(ValVT));
2625 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002626 }
2627
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002628 // Handle floating point arguments passed in integer registers.
2629 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
2630 (RegVT == MVT::i64 && ValVT == MVT::f64))
2631 ArgValue = DAG.getNode(ISD::BITCAST, dl, ValVT, ArgValue);
2632 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
2633 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
2634 getNextIntArgReg(ArgReg), RC);
2635 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
2636 if (!Subtarget->isLittle())
2637 std::swap(ArgValue, ArgValue2);
2638 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2639 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002640 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002641
Dan Gohman98ca4f22009-08-05 01:29:28 +00002642 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002643 } else { // VA.isRegLoc()
2644
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002645 // sanity check
2646 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002647
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002648 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002649 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002650 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002651
2652 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002653 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002654 InVals.push_back(DAG.getLoad(ValVT, dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002655 MachinePointerInfo::getFixedStack(LastFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002656 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002657 }
2658 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002659
2660 // The mips ABIs for returning structs by value requires that we copy
2661 // the sret argument into $v0 for the return. Save the argument into
2662 // a virtual register so that we can access it from the return points.
2663 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2664 unsigned Reg = MipsFI->getSRetReturnReg();
2665 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002666 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002667 MipsFI->setSRetReturnReg(Reg);
2668 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002669 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002670 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002671 }
2672
Akira Hatanakabad53f42011-11-14 19:01:09 +00002673 if (isVarArg) {
2674 unsigned NumOfRegs = IsO32 ? 4 : 8;
2675 const unsigned *ArgRegs = IsO32 ? O32IntRegs : Mips64IntRegs;
2676 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumOfRegs);
2677 int FirstRegSlotOffset = IsO32 ? 0 : -64 ; // offset of $a0's slot.
2678 TargetRegisterClass *RC
2679 = IsO32 ? Mips::CPURegsRegisterClass : Mips::CPU64RegsRegisterClass;
2680 unsigned RegSize = RC->getSize();
2681 int RegSlotOffset = FirstRegSlotOffset + Idx * RegSize;
2682
2683 // Offset of the first variable argument from stack pointer.
2684 int FirstVaArgOffset;
2685
2686 if (IsO32 || (Idx == NumOfRegs)) {
2687 FirstVaArgOffset =
2688 (CCInfo.getNextStackOffset() + RegSize - 1) / RegSize * RegSize;
2689 } else
2690 FirstVaArgOffset = RegSlotOffset;
2691
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002692 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002693 // which is a value necessary to VASTART.
Akira Hatanakabad53f42011-11-14 19:01:09 +00002694 LastFI = MFI->CreateFixedObject(RegSize, FirstVaArgOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002695 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002696
Akira Hatanakabad53f42011-11-14 19:01:09 +00002697 // Copy the integer registers that have not been used for argument passing
2698 // to the argument register save area. For O32, the save area is allocated
2699 // in the caller's stack frame, while for N32/64, it is allocated in the
2700 // callee's stack frame.
2701 for (int StackOffset = RegSlotOffset;
2702 Idx < NumOfRegs; ++Idx, StackOffset += RegSize) {
2703 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegs[Idx], RC);
2704 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2705 MVT::getIntegerVT(RegSize * 8));
2706 LastFI = MFI->CreateFixedObject(RegSize, StackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002707 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2708 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
Akira Hatanaka82099682011-12-19 19:52:25 +00002709 MachinePointerInfo(), false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002710 }
2711 }
2712
Akira Hatanaka43299772011-05-20 23:22:14 +00002713 MipsFI->setLastInArgFI(LastFI);
2714
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002715 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002716 // the size of Ins and InVals. This only happens when on varg functions
2717 if (!OutChains.empty()) {
2718 OutChains.push_back(Chain);
2719 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2720 &OutChains[0], OutChains.size());
2721 }
2722
Dan Gohman98ca4f22009-08-05 01:29:28 +00002723 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002724}
2725
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002726//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002727// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002728//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002729
Dan Gohman98ca4f22009-08-05 01:29:28 +00002730SDValue
2731MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002732 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002733 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002734 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002735 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002736
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002737 // CCValAssign - represent the assignment of
2738 // the return value to a location
2739 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002740
2741 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002742 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2743 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002744
Dan Gohman98ca4f22009-08-05 01:29:28 +00002745 // Analize return values.
2746 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002747
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002748 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002749 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002750 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002751 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002752 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002753 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002754 }
2755
Dan Gohman475871a2008-07-27 21:46:04 +00002756 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002757
2758 // Copy the result values into the output registers.
2759 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2760 CCValAssign &VA = RVLocs[i];
2761 assert(VA.isRegLoc() && "Can only return in registers!");
2762
Akira Hatanaka82099682011-12-19 19:52:25 +00002763 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002764
2765 // guarantee that all emitted copies are
2766 // stuck together, avoiding something bad
2767 Flag = Chain.getValue(1);
2768 }
2769
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002770 // The mips ABIs for returning structs by value requires that we copy
2771 // the sret argument into $v0 for the return. We saved the argument into
2772 // a virtual register in the entry block, so now we copy the value out
2773 // and into $v0.
2774 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2775 MachineFunction &MF = DAG.getMachineFunction();
2776 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2777 unsigned Reg = MipsFI->getSRetReturnReg();
2778
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002779 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002780 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002781 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002782
Dale Johannesena05dca42009-02-04 23:02:30 +00002783 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002784 Flag = Chain.getValue(1);
2785 }
2786
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002787 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002788 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002789 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002790 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002791 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002792 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002793 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002794}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002795
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002796//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002797// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002798//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002799
2800/// getConstraintType - Given a constraint letter, return the type of
2801/// constraint it is for this target.
2802MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002803getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002804{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002805 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002806 // GCC config/mips/constraints.md
2807 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002808 // 'd' : An address register. Equivalent to r
2809 // unless generating MIPS16 code.
2810 // 'y' : Equivalent to r; retained for
2811 // backwards compatibility.
2812 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002813 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002814 switch (Constraint[0]) {
2815 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002816 case 'd':
2817 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002818 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002819 return C_RegisterClass;
2820 break;
2821 }
2822 }
2823 return TargetLowering::getConstraintType(Constraint);
2824}
2825
John Thompson44ab89e2010-10-29 17:29:13 +00002826/// Examine constraint type and operand type and determine a weight value.
2827/// This object must already have been set up with the operand type
2828/// and the current alternative constraint selected.
2829TargetLowering::ConstraintWeight
2830MipsTargetLowering::getSingleConstraintMatchWeight(
2831 AsmOperandInfo &info, const char *constraint) const {
2832 ConstraintWeight weight = CW_Invalid;
2833 Value *CallOperandVal = info.CallOperandVal;
2834 // If we don't have a value, we can't do a match,
2835 // but allow it at the lowest weight.
2836 if (CallOperandVal == NULL)
2837 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002838 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002839 // Look at the constraint type.
2840 switch (*constraint) {
2841 default:
2842 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2843 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002844 case 'd':
2845 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002846 if (type->isIntegerTy())
2847 weight = CW_Register;
2848 break;
2849 case 'f':
2850 if (type->isFloatTy())
2851 weight = CW_Register;
2852 break;
2853 }
2854 return weight;
2855}
2856
Eric Christopher38d64262011-06-29 19:33:04 +00002857/// Given a register class constraint, like 'r', if this corresponds directly
2858/// to an LLVM register class, return a register of 0 and the register class
2859/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002860std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002861getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002862{
2863 if (Constraint.size() == 1) {
2864 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002865 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2866 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002867 case 'r':
2868 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002869 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002870 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002871 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002872 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002873 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2874 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002875 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002876 }
2877 }
2878 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2879}
2880
Dan Gohman6520e202008-10-18 02:06:02 +00002881bool
2882MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2883 // The Mips target isn't yet aware of offsets.
2884 return false;
2885}
Evan Chengeb2f9692009-10-27 19:56:55 +00002886
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002887bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2888 if (VT != MVT::f32 && VT != MVT::f64)
2889 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002890 if (Imm.isNegZero())
2891 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002892 return Imm.isZero();
2893}