blob: cfdce3a447f5d93823a0c133c3f2d27e7673701e [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
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000224 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000225 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000226
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000227 setTargetDAGCombine(ISD::ADDE);
228 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000229 setTargetDAGCombine(ISD::SDIVREM);
230 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000231 setTargetDAGCombine(ISD::SETCC);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000232 setTargetDAGCombine(ISD::AND);
233 setTargetDAGCombine(ISD::OR);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000234
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000235 setMinFunctionAlignment(2);
236
Akira Hatanaka056a1bc2011-12-20 23:28:36 +0000237 setStackPointerRegisterToSaveRestore(HasMips64 ? Mips::SP_64 : Mips::SP);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000238 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000239
240 setExceptionPointerRegister(Mips::A0);
241 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000242}
243
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000244bool MipsTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Akira Hatanaka511961a2011-08-17 18:49:18 +0000245 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
Akira Hatanaka7bd19bd2011-10-11 00:27:28 +0000246 return SVT == MVT::i64 || SVT == MVT::i32 || SVT == MVT::i16;
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000247}
248
Duncan Sands28b77e92011-09-06 19:07:46 +0000249EVT MipsTargetLowering::getSetCCResultType(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000251}
252
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000253// SelectMadd -
254// Transforms a subgraph in CurDAG if the following pattern is found:
255// (addc multLo, Lo0), (adde multHi, Hi0),
256// where,
257// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000258// Lo0: initial value of Lo register
259// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000260// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000261static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000262 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000263 // for the matching to be successful.
264 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
265
266 if (ADDCNode->getOpcode() != ISD::ADDC)
267 return false;
268
269 SDValue MultHi = ADDENode->getOperand(0);
270 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000271 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000272 unsigned MultOpc = MultHi.getOpcode();
273
274 // MultHi and MultLo must be generated by the same node,
275 if (MultLo.getNode() != MultNode)
276 return false;
277
278 // and it must be a multiplication.
279 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
280 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000281
282 // MultLo amd MultHi must be the first and second output of MultNode
283 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000284 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
285 return false;
286
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000287 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000288 // of the values of MultNode, in which case MultNode will be removed in later
289 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000290 // If there exist users other than ADDENode or ADDCNode, this function returns
291 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000292 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000293 // produced.
294 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
295 return false;
296
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000297 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000298 DebugLoc dl = ADDENode->getDebugLoc();
299
300 // create MipsMAdd(u) node
301 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000302
Akira Hatanaka82099682011-12-19 19:52:25 +0000303 SDValue MAdd = CurDAG->getNode(MultOpc, dl, MVT::Glue,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000304 MultNode->getOperand(0),// Factor 0
305 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000306 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000307 ADDENode->getOperand(1));// Hi0
308
309 // create CopyFromReg nodes
310 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
311 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000312 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000313 Mips::HI, MVT::i32,
314 CopyFromLo.getValue(2));
315
316 // replace uses of adde and addc here
317 if (!SDValue(ADDCNode, 0).use_empty())
318 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
319
320 if (!SDValue(ADDENode, 0).use_empty())
321 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
322
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000323 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000324}
325
326// SelectMsub -
327// Transforms a subgraph in CurDAG if the following pattern is found:
328// (addc Lo0, multLo), (sube Hi0, multHi),
329// where,
330// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000331// Lo0: initial value of Lo register
332// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000333// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000334static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000335 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000336 // for the matching to be successful.
337 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
338
339 if (SUBCNode->getOpcode() != ISD::SUBC)
340 return false;
341
342 SDValue MultHi = SUBENode->getOperand(1);
343 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000344 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000345 unsigned MultOpc = MultHi.getOpcode();
346
347 // MultHi and MultLo must be generated by the same node,
348 if (MultLo.getNode() != MultNode)
349 return false;
350
351 // and it must be a multiplication.
352 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
353 return false;
354
355 // MultLo amd MultHi must be the first and second output of MultNode
356 // respectively.
357 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
358 return false;
359
360 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
361 // of the values of MultNode, in which case MultNode will be removed in later
362 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000363 // If there exist users other than SUBENode or SUBCNode, this function returns
364 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000365 // instruction node rather than a pair of MULT and MSUB instructions being
366 // produced.
367 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
368 return false;
369
370 SDValue Chain = CurDAG->getEntryNode();
371 DebugLoc dl = SUBENode->getDebugLoc();
372
373 // create MipsSub(u) node
374 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
375
Akira Hatanaka82099682011-12-19 19:52:25 +0000376 SDValue MSub = CurDAG->getNode(MultOpc, dl, MVT::Glue,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000377 MultNode->getOperand(0),// Factor 0
378 MultNode->getOperand(1),// Factor 1
379 SUBCNode->getOperand(0),// Lo0
380 SUBENode->getOperand(0));// Hi0
381
382 // create CopyFromReg nodes
383 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
384 MSub);
385 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
386 Mips::HI, MVT::i32,
387 CopyFromLo.getValue(2));
388
389 // replace uses of sube and subc here
390 if (!SDValue(SUBCNode, 0).use_empty())
391 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
392
393 if (!SDValue(SUBENode, 0).use_empty())
394 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
395
396 return true;
397}
398
399static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
400 TargetLowering::DAGCombinerInfo &DCI,
401 const MipsSubtarget* Subtarget) {
402 if (DCI.isBeforeLegalize())
403 return SDValue();
404
Akira Hatanakae184fec2011-11-11 04:18:21 +0000405 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
406 SelectMadd(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000407 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000408
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000409 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000410}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000411
412static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
413 TargetLowering::DAGCombinerInfo &DCI,
414 const MipsSubtarget* Subtarget) {
415 if (DCI.isBeforeLegalize())
416 return SDValue();
417
Akira Hatanakae184fec2011-11-11 04:18:21 +0000418 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
419 SelectMsub(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000420 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000421
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000422 return SDValue();
423}
424
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000425static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
426 TargetLowering::DAGCombinerInfo &DCI,
427 const MipsSubtarget* Subtarget) {
428 if (DCI.isBeforeLegalizeOps())
429 return SDValue();
430
Akira Hatanakadda4a072011-10-03 21:06:13 +0000431 EVT Ty = N->getValueType(0);
432 unsigned LO = (Ty == MVT::i32) ? Mips::LO : Mips::LO64;
433 unsigned HI = (Ty == MVT::i32) ? Mips::HI : Mips::HI64;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000434 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
435 MipsISD::DivRemU;
436 DebugLoc dl = N->getDebugLoc();
437
438 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
439 N->getOperand(0), N->getOperand(1));
440 SDValue InChain = DAG.getEntryNode();
441 SDValue InGlue = DivRem;
442
443 // insert MFLO
444 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakadda4a072011-10-03 21:06:13 +0000445 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000446 InGlue);
447 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
448 InChain = CopyFromLo.getValue(1);
449 InGlue = CopyFromLo.getValue(2);
450 }
451
452 // insert MFHI
453 if (N->hasAnyUseOfValue(1)) {
454 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000455 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000456 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
457 }
458
459 return SDValue();
460}
461
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000462static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
463 switch (CC) {
464 default: llvm_unreachable("Unknown fp condition code!");
465 case ISD::SETEQ:
466 case ISD::SETOEQ: return Mips::FCOND_OEQ;
467 case ISD::SETUNE: return Mips::FCOND_UNE;
468 case ISD::SETLT:
469 case ISD::SETOLT: return Mips::FCOND_OLT;
470 case ISD::SETGT:
471 case ISD::SETOGT: return Mips::FCOND_OGT;
472 case ISD::SETLE:
473 case ISD::SETOLE: return Mips::FCOND_OLE;
474 case ISD::SETGE:
475 case ISD::SETOGE: return Mips::FCOND_OGE;
476 case ISD::SETULT: return Mips::FCOND_ULT;
477 case ISD::SETULE: return Mips::FCOND_ULE;
478 case ISD::SETUGT: return Mips::FCOND_UGT;
479 case ISD::SETUGE: return Mips::FCOND_UGE;
480 case ISD::SETUO: return Mips::FCOND_UN;
481 case ISD::SETO: return Mips::FCOND_OR;
482 case ISD::SETNE:
483 case ISD::SETONE: return Mips::FCOND_ONE;
484 case ISD::SETUEQ: return Mips::FCOND_UEQ;
485 }
486}
487
488
489// Returns true if condition code has to be inverted.
490static bool InvertFPCondCode(Mips::CondCode CC) {
491 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
492 return false;
493
Akira Hatanaka82099682011-12-19 19:52:25 +0000494 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
495 "Illegal Condition Code");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000496
Akira Hatanaka82099682011-12-19 19:52:25 +0000497 return true;
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000498}
499
500// Creates and returns an FPCmp node from a setcc node.
501// Returns Op if setcc is not a floating point comparison.
502static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
503 // must be a SETCC node
504 if (Op.getOpcode() != ISD::SETCC)
505 return Op;
506
507 SDValue LHS = Op.getOperand(0);
508
509 if (!LHS.getValueType().isFloatingPoint())
510 return Op;
511
512 SDValue RHS = Op.getOperand(1);
513 DebugLoc dl = Op.getDebugLoc();
514
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000515 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
516 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000517 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
518
519 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
520 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
521}
522
523// Creates and returns a CMovFPT/F node.
524static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
525 SDValue False, DebugLoc DL) {
526 bool invert = InvertFPCondCode((Mips::CondCode)
527 cast<ConstantSDNode>(Cond.getOperand(2))
528 ->getSExtValue());
529
530 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
531 True.getValueType(), True, False, Cond);
532}
533
534static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
535 TargetLowering::DAGCombinerInfo &DCI,
536 const MipsSubtarget* Subtarget) {
537 if (DCI.isBeforeLegalizeOps())
538 return SDValue();
539
540 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
541
542 if (Cond.getOpcode() != MipsISD::FPCmp)
543 return SDValue();
544
545 SDValue True = DAG.getConstant(1, MVT::i32);
546 SDValue False = DAG.getConstant(0, MVT::i32);
547
548 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
549}
550
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000551static SDValue PerformANDCombine(SDNode *N, SelectionDAG& DAG,
552 TargetLowering::DAGCombinerInfo &DCI,
553 const MipsSubtarget* Subtarget) {
554 // Pattern match EXT.
555 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
556 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000557 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000558 return SDValue();
559
560 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000561 unsigned ShiftRightOpc = ShiftRight.getOpcode();
562
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000563 // Op's first operand must be a shift right.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000564 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000565 return SDValue();
566
567 // The second operand of the shift must be an immediate.
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000568 ConstantSDNode *CN;
569 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
570 return SDValue();
571
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000572 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000573 uint64_t SMPos, SMSize;
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000574
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000575 // Op's second operand must be a shifted mask.
576 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000577 !IsShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000578 return SDValue();
579
580 // Return if the shifted mask does not start at bit 0 or the sum of its size
581 // and Pos exceeds the word's size.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000582 EVT ValTy = N->getValueType(0);
583 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000584 return SDValue();
585
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000586 return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), ValTy,
Akira Hatanaka82099682011-12-19 19:52:25 +0000587 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000588 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000589}
590
591static SDValue PerformORCombine(SDNode *N, SelectionDAG& DAG,
592 TargetLowering::DAGCombinerInfo &DCI,
593 const MipsSubtarget* Subtarget) {
594 // Pattern match INS.
595 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
596 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
597 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000598 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000599 return SDValue();
600
601 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
602 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
603 ConstantSDNode *CN;
604
605 // See if Op's first operand matches (and $src1 , mask0).
606 if (And0.getOpcode() != ISD::AND)
607 return SDValue();
608
609 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000610 !IsShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000611 return SDValue();
612
613 // See if Op's second operand matches (and (shl $src, pos), mask1).
614 if (And1.getOpcode() != ISD::AND)
615 return SDValue();
616
617 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000618 !IsShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000619 return SDValue();
620
621 // The shift masks must have the same position and size.
622 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
623 return SDValue();
624
625 SDValue Shl = And1.getOperand(0);
626 if (Shl.getOpcode() != ISD::SHL)
627 return SDValue();
628
629 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
630 return SDValue();
631
632 unsigned Shamt = CN->getZExtValue();
633
634 // Return if the shift amount and the first bit position of mask are not the
635 // same.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000636 EVT ValTy = N->getValueType(0);
637 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000638 return SDValue();
639
Akira Hatanaka82099682011-12-19 19:52:25 +0000640 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), ValTy, Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000641 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka82099682011-12-19 19:52:25 +0000642 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000643}
644
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000645SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000646 const {
647 SelectionDAG &DAG = DCI.DAG;
648 unsigned opc = N->getOpcode();
649
650 switch (opc) {
651 default: break;
652 case ISD::ADDE:
653 return PerformADDECombine(N, DAG, DCI, Subtarget);
654 case ISD::SUBE:
655 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000656 case ISD::SDIVREM:
657 case ISD::UDIVREM:
658 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000659 case ISD::SETCC:
660 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000661 case ISD::AND:
662 return PerformANDCombine(N, DAG, DCI, Subtarget);
663 case ISD::OR:
664 return PerformORCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000665 }
666
667 return SDValue();
668}
669
Dan Gohman475871a2008-07-27 21:46:04 +0000670SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000671LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000672{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000673 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000674 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000675 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000676 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
677 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000678 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000679 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000680 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
681 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000682 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000683 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000684 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000685 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Akira Hatanakadb548262011-07-19 23:30:50 +0000686 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +0000687 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000688 }
Dan Gohman475871a2008-07-27 21:46:04 +0000689 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000690}
691
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000692//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000693// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000694//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000695
696// AddLiveIn - This helper function adds the specified physical register to the
697// MachineFunction as a live in value. It also creates a corresponding
698// virtual register for it.
699static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000700AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000701{
702 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000703 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
704 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000705 return VReg;
706}
707
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000708// Get fp branch code (not opcode) from condition code.
709static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
710 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
711 return Mips::BRANCH_T;
712
Akira Hatanaka82099682011-12-19 19:52:25 +0000713 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
714 "Invalid CondCode.");
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000715
Akira Hatanaka82099682011-12-19 19:52:25 +0000716 return Mips::BRANCH_F;
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000717}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000718
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000719/*
Akira Hatanaka14487d42011-06-07 19:28:39 +0000720static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
721 DebugLoc dl,
722 const MipsSubtarget* Subtarget,
723 const TargetInstrInfo *TII,
724 bool isFPCmp, unsigned Opc) {
725 // There is no need to expand CMov instructions if target has
726 // conditional moves.
727 if (Subtarget->hasCondMov())
728 return BB;
729
730 // To "insert" a SELECT_CC instruction, we actually have to insert the
731 // diamond control-flow pattern. The incoming instruction knows the
732 // destination vreg to set, the condition code register to branch on, the
733 // true/false values to select between, and a branch opcode to use.
734 const BasicBlock *LLVM_BB = BB->getBasicBlock();
735 MachineFunction::iterator It = BB;
736 ++It;
737
738 // thisMBB:
739 // ...
740 // TrueVal = ...
741 // setcc r1, r2, r3
742 // bNE r1, r0, copy1MBB
743 // fallthrough --> copy0MBB
744 MachineBasicBlock *thisMBB = BB;
745 MachineFunction *F = BB->getParent();
746 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
747 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
748 F->insert(It, copy0MBB);
749 F->insert(It, sinkMBB);
750
751 // Transfer the remainder of BB and its successor edges to sinkMBB.
752 sinkMBB->splice(sinkMBB->begin(), BB,
753 llvm::next(MachineBasicBlock::iterator(MI)),
754 BB->end());
755 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
756
757 // Next, add the true and fallthrough blocks as its successors.
758 BB->addSuccessor(copy0MBB);
759 BB->addSuccessor(sinkMBB);
760
761 // Emit the right instruction according to the type of the operands compared
762 if (isFPCmp)
763 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
764 else
765 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
766 .addReg(Mips::ZERO).addMBB(sinkMBB);
767
768 // copy0MBB:
769 // %FalseValue = ...
770 // # fallthrough to sinkMBB
771 BB = copy0MBB;
772
773 // Update machine-CFG edges
774 BB->addSuccessor(sinkMBB);
775
776 // sinkMBB:
777 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
778 // ...
779 BB = sinkMBB;
780
781 if (isFPCmp)
782 BuildMI(*BB, BB->begin(), dl,
783 TII->get(Mips::PHI), MI->getOperand(0).getReg())
784 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
785 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
786 else
787 BuildMI(*BB, BB->begin(), dl,
788 TII->get(Mips::PHI), MI->getOperand(0).getReg())
789 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
790 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
791
792 MI->eraseFromParent(); // The pseudo instruction is gone now.
793 return BB;
794}
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000795*/
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000796MachineBasicBlock *
797MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000798 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000799 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000800 default:
801 assert(false && "Unexpected instr type to insert");
802 return NULL;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000803 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000804 case Mips::ATOMIC_LOAD_ADD_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000805 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
806 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000807 case Mips::ATOMIC_LOAD_ADD_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000808 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
809 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000810 case Mips::ATOMIC_LOAD_ADD_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000811 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000812 case Mips::ATOMIC_LOAD_ADD_I64:
813 case Mips::ATOMIC_LOAD_ADD_I64_P8:
814 return EmitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000815
816 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000817 case Mips::ATOMIC_LOAD_AND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000818 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
819 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000820 case Mips::ATOMIC_LOAD_AND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000821 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
822 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000823 case Mips::ATOMIC_LOAD_AND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000824 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000825 case Mips::ATOMIC_LOAD_AND_I64:
826 case Mips::ATOMIC_LOAD_AND_I64_P8:
Akira Hatanaka73866122011-11-12 02:38:12 +0000827 return EmitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000828
829 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000830 case Mips::ATOMIC_LOAD_OR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000831 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
832 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000833 case Mips::ATOMIC_LOAD_OR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000834 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
835 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000836 case Mips::ATOMIC_LOAD_OR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000837 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000838 case Mips::ATOMIC_LOAD_OR_I64:
839 case Mips::ATOMIC_LOAD_OR_I64_P8:
840 return EmitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000841
842 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000843 case Mips::ATOMIC_LOAD_XOR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000844 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
845 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000846 case Mips::ATOMIC_LOAD_XOR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000847 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
848 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000849 case Mips::ATOMIC_LOAD_XOR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000850 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000851 case Mips::ATOMIC_LOAD_XOR_I64:
852 case Mips::ATOMIC_LOAD_XOR_I64_P8:
853 return EmitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000854
855 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000856 case Mips::ATOMIC_LOAD_NAND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000857 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
858 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000859 case Mips::ATOMIC_LOAD_NAND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000860 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
861 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000862 case Mips::ATOMIC_LOAD_NAND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000863 return EmitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000864 case Mips::ATOMIC_LOAD_NAND_I64:
865 case Mips::ATOMIC_LOAD_NAND_I64_P8:
866 return EmitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000867
868 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000869 case Mips::ATOMIC_LOAD_SUB_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000870 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
871 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000872 case Mips::ATOMIC_LOAD_SUB_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000873 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
874 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000875 case Mips::ATOMIC_LOAD_SUB_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000876 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000877 case Mips::ATOMIC_LOAD_SUB_I64:
878 case Mips::ATOMIC_LOAD_SUB_I64_P8:
879 return EmitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000880
881 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000882 case Mips::ATOMIC_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000883 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
884 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000885 case Mips::ATOMIC_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000886 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
887 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000888 case Mips::ATOMIC_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000889 return EmitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000890 case Mips::ATOMIC_SWAP_I64:
891 case Mips::ATOMIC_SWAP_I64_P8:
892 return EmitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000893
894 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000895 case Mips::ATOMIC_CMP_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000896 return EmitAtomicCmpSwapPartword(MI, BB, 1);
897 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000898 case Mips::ATOMIC_CMP_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000899 return EmitAtomicCmpSwapPartword(MI, BB, 2);
900 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000901 case Mips::ATOMIC_CMP_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000902 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000903 case Mips::ATOMIC_CMP_SWAP_I64:
904 case Mips::ATOMIC_CMP_SWAP_I64_P8:
905 return EmitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000906 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000907}
908
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000909// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
910// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
911MachineBasicBlock *
912MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000913 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000914 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000915 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000916
917 MachineFunction *MF = BB->getParent();
918 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000919 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000920 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
921 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000922 unsigned LL, SC, AND, NOR, ZERO, BEQ;
923
924 if (Size == 4) {
925 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
926 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
927 AND = Mips::AND;
928 NOR = Mips::NOR;
929 ZERO = Mips::ZERO;
930 BEQ = Mips::BEQ;
931 }
932 else {
933 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
934 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
935 AND = Mips::AND64;
936 NOR = Mips::NOR64;
937 ZERO = Mips::ZERO_64;
938 BEQ = Mips::BEQ64;
939 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000940
Akira Hatanaka4061da12011-07-19 20:11:17 +0000941 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000942 unsigned Ptr = MI->getOperand(1).getReg();
943 unsigned Incr = MI->getOperand(2).getReg();
944
Akira Hatanaka4061da12011-07-19 20:11:17 +0000945 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
946 unsigned AndRes = RegInfo.createVirtualRegister(RC);
947 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000948
949 // insert new blocks after the current block
950 const BasicBlock *LLVM_BB = BB->getBasicBlock();
951 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
952 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
953 MachineFunction::iterator It = BB;
954 ++It;
955 MF->insert(It, loopMBB);
956 MF->insert(It, exitMBB);
957
958 // Transfer the remainder of BB and its successor edges to exitMBB.
959 exitMBB->splice(exitMBB->begin(), BB,
960 llvm::next(MachineBasicBlock::iterator(MI)),
961 BB->end());
962 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
963
964 // thisMBB:
965 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000966 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000967 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000968 loopMBB->addSuccessor(loopMBB);
969 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000970
971 // loopMBB:
972 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000973 // <binop> storeval, oldval, incr
974 // sc success, storeval, 0(ptr)
975 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000976 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +0000977 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000978 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000979 // and andres, oldval, incr
980 // nor storeval, $0, andres
Akira Hatanaka59068062011-11-11 04:14:30 +0000981 BuildMI(BB, dl, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
982 BuildMI(BB, dl, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000983 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000984 // <binop> storeval, oldval, incr
985 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000986 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000987 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000988 }
Akira Hatanaka59068062011-11-11 04:14:30 +0000989 BuildMI(BB, dl, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
990 BuildMI(BB, dl, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000991
992 MI->eraseFromParent(); // The instruction is gone now.
993
Akira Hatanaka939ece12011-07-19 03:42:13 +0000994 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000995}
996
997MachineBasicBlock *
998MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000999 MachineBasicBlock *BB,
1000 unsigned Size, unsigned BinOpcode,
1001 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001002 assert((Size == 1 || Size == 2) &&
1003 "Unsupported size for EmitAtomicBinaryPartial.");
1004
1005 MachineFunction *MF = BB->getParent();
1006 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1007 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1008 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1009 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001010 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1011 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001012
1013 unsigned Dest = MI->getOperand(0).getReg();
1014 unsigned Ptr = MI->getOperand(1).getReg();
1015 unsigned Incr = MI->getOperand(2).getReg();
1016
Akira Hatanaka4061da12011-07-19 20:11:17 +00001017 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1018 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001019 unsigned Mask = RegInfo.createVirtualRegister(RC);
1020 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001021 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1022 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001023 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001024 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1025 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1026 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1027 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1028 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001029 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001030 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1031 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1032 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1033 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1034 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001035
1036 // insert new blocks after the current block
1037 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1038 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001039 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001040 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1041 MachineFunction::iterator It = BB;
1042 ++It;
1043 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001044 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001045 MF->insert(It, exitMBB);
1046
1047 // Transfer the remainder of BB and its successor edges to exitMBB.
1048 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001049 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001050 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1051
Akira Hatanaka81b44112011-07-19 17:09:53 +00001052 BB->addSuccessor(loopMBB);
1053 loopMBB->addSuccessor(loopMBB);
1054 loopMBB->addSuccessor(sinkMBB);
1055 sinkMBB->addSuccessor(exitMBB);
1056
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001057 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001058 // addiu masklsb2,$0,-4 # 0xfffffffc
1059 // and alignedaddr,ptr,masklsb2
1060 // andi ptrlsb2,ptr,3
1061 // sll shiftamt,ptrlsb2,3
1062 // ori maskupper,$0,255 # 0xff
1063 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001064 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001065 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001066
1067 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001068 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1069 .addReg(Mips::ZERO).addImm(-4);
1070 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1071 .addReg(Ptr).addReg(MaskLSB2);
1072 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1073 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1074 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1075 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001076 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1077 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001078 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001079 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001080
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001081 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001082 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001083 // ll oldval,0(alignedaddr)
1084 // binop binopres,oldval,incr2
1085 // and newval,binopres,mask
1086 // and maskedoldval0,oldval,mask2
1087 // or storeval,maskedoldval0,newval
1088 // sc success,storeval,0(alignedaddr)
1089 // beq success,$0,loopMBB
1090
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001091 // atomic.swap
1092 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001093 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001094 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001095 // and maskedoldval0,oldval,mask2
1096 // or storeval,maskedoldval0,newval
1097 // sc success,storeval,0(alignedaddr)
1098 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001099
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001100 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001101 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001102 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001103 // and andres, oldval, incr2
1104 // nor binopres, $0, andres
1105 // and newval, binopres, mask
1106 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1107 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1108 .addReg(Mips::ZERO).addReg(AndRes);
1109 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001110 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001111 // <binop> binopres, oldval, incr2
1112 // and newval, binopres, mask
1113 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1114 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001115 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001116 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001117 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001118 }
1119
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001120 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001121 .addReg(OldVal).addReg(Mask2);
1122 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001123 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001124 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001125 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001126 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001127 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001128
Akira Hatanaka939ece12011-07-19 03:42:13 +00001129 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001130 // and maskedoldval1,oldval,mask
1131 // srl srlres,maskedoldval1,shiftamt
1132 // sll sllres,srlres,24
1133 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001134 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001135 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001136
Akira Hatanaka4061da12011-07-19 20:11:17 +00001137 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1138 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001139 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1140 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001141 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1142 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001143 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001144 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001145
1146 MI->eraseFromParent(); // The instruction is gone now.
1147
Akira Hatanaka939ece12011-07-19 03:42:13 +00001148 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001149}
1150
1151MachineBasicBlock *
1152MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001153 MachineBasicBlock *BB,
1154 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001155 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001156
1157 MachineFunction *MF = BB->getParent();
1158 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001159 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001160 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1161 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001162 unsigned LL, SC, ZERO, BNE, BEQ;
1163
1164 if (Size == 4) {
1165 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1166 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1167 ZERO = Mips::ZERO;
1168 BNE = Mips::BNE;
1169 BEQ = Mips::BEQ;
1170 }
1171 else {
1172 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1173 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1174 ZERO = Mips::ZERO_64;
1175 BNE = Mips::BNE64;
1176 BEQ = Mips::BEQ64;
1177 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001178
1179 unsigned Dest = MI->getOperand(0).getReg();
1180 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001181 unsigned OldVal = MI->getOperand(2).getReg();
1182 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001183
Akira Hatanaka4061da12011-07-19 20:11:17 +00001184 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001185
1186 // insert new blocks after the current block
1187 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1188 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1189 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1190 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1191 MachineFunction::iterator It = BB;
1192 ++It;
1193 MF->insert(It, loop1MBB);
1194 MF->insert(It, loop2MBB);
1195 MF->insert(It, exitMBB);
1196
1197 // Transfer the remainder of BB and its successor edges to exitMBB.
1198 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001199 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001200 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1201
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001202 // thisMBB:
1203 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001204 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001205 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001206 loop1MBB->addSuccessor(exitMBB);
1207 loop1MBB->addSuccessor(loop2MBB);
1208 loop2MBB->addSuccessor(loop1MBB);
1209 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001210
1211 // loop1MBB:
1212 // ll dest, 0(ptr)
1213 // bne dest, oldval, exitMBB
1214 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001215 BuildMI(BB, dl, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1216 BuildMI(BB, dl, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001217 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001218
1219 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001220 // sc success, newval, 0(ptr)
1221 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001222 BB = loop2MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001223 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001224 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka59068062011-11-11 04:14:30 +00001225 BuildMI(BB, dl, TII->get(BEQ))
1226 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001227
1228 MI->eraseFromParent(); // The instruction is gone now.
1229
Akira Hatanaka939ece12011-07-19 03:42:13 +00001230 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001231}
1232
1233MachineBasicBlock *
1234MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001235 MachineBasicBlock *BB,
1236 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001237 assert((Size == 1 || Size == 2) &&
1238 "Unsupported size for EmitAtomicCmpSwapPartial.");
1239
1240 MachineFunction *MF = BB->getParent();
1241 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1242 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1243 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1244 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001245 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1246 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001247
1248 unsigned Dest = MI->getOperand(0).getReg();
1249 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001250 unsigned CmpVal = MI->getOperand(2).getReg();
1251 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001252
Akira Hatanaka4061da12011-07-19 20:11:17 +00001253 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1254 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001255 unsigned Mask = RegInfo.createVirtualRegister(RC);
1256 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001257 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1258 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1259 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1260 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1261 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1262 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1263 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1264 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1265 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1266 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1267 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1268 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1269 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1270 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001271
1272 // insert new blocks after the current block
1273 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1274 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1275 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001276 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001277 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1278 MachineFunction::iterator It = BB;
1279 ++It;
1280 MF->insert(It, loop1MBB);
1281 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001282 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001283 MF->insert(It, exitMBB);
1284
1285 // Transfer the remainder of BB and its successor edges to exitMBB.
1286 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001287 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001288 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1289
Akira Hatanaka81b44112011-07-19 17:09:53 +00001290 BB->addSuccessor(loop1MBB);
1291 loop1MBB->addSuccessor(sinkMBB);
1292 loop1MBB->addSuccessor(loop2MBB);
1293 loop2MBB->addSuccessor(loop1MBB);
1294 loop2MBB->addSuccessor(sinkMBB);
1295 sinkMBB->addSuccessor(exitMBB);
1296
Akira Hatanaka70564a92011-07-19 18:14:26 +00001297 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001298 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001299 // addiu masklsb2,$0,-4 # 0xfffffffc
1300 // and alignedaddr,ptr,masklsb2
1301 // andi ptrlsb2,ptr,3
1302 // sll shiftamt,ptrlsb2,3
1303 // ori maskupper,$0,255 # 0xff
1304 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001305 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001306 // andi maskedcmpval,cmpval,255
1307 // sll shiftedcmpval,maskedcmpval,shiftamt
1308 // andi maskednewval,newval,255
1309 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001310 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001311 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1312 .addReg(Mips::ZERO).addImm(-4);
1313 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1314 .addReg(Ptr).addReg(MaskLSB2);
1315 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1316 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1317 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1318 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001319 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1320 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001321 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001322 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1323 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001324 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1325 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001326 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1327 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001328 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1329 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001330
1331 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001332 // ll oldval,0(alginedaddr)
1333 // and maskedoldval0,oldval,mask
1334 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001335 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001336 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001337 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1338 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001339 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001340 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001341
1342 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001343 // and maskedoldval1,oldval,mask2
1344 // or storeval,maskedoldval1,shiftednewval
1345 // sc success,storeval,0(alignedaddr)
1346 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001347 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001348 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1349 .addReg(OldVal).addReg(Mask2);
1350 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1351 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001352 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001353 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001354 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001355 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001356
Akira Hatanaka939ece12011-07-19 03:42:13 +00001357 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001358 // srl srlres,maskedoldval0,shiftamt
1359 // sll sllres,srlres,24
1360 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001361 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001362 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001363
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001364 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1365 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001366 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1367 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001368 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001369 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001370
1371 MI->eraseFromParent(); // The instruction is gone now.
1372
Akira Hatanaka939ece12011-07-19 03:42:13 +00001373 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001374}
1375
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001376//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001377// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001378//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001379SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001380LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001381{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001382 MachineFunction &MF = DAG.getMachineFunction();
1383 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001384 unsigned SP = IsN64 ? Mips::SP_64 : Mips::SP;
Akira Hatanaka21afc632011-06-21 00:40:49 +00001385
1386 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001387 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1388 "Cannot lower if the alignment of the allocated space is larger than \
1389 that of the stack.");
1390
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001391 SDValue Chain = Op.getOperand(0);
1392 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001393 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001394
1395 // Get a reference from Mips stack pointer
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001396 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, SP, getPointerTy());
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001397
1398 // Subtract the dynamic size from the actual stack size to
1399 // obtain the new stack size.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001400 SDValue Sub = DAG.getNode(ISD::SUB, dl, getPointerTy(), StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001401
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001402 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001403 // must be placed in the stack pointer register.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001404 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, SP, Sub, SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001405
1406 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001407 // value and a chain
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001408 SDVTList VTLs = DAG.getVTList(getPointerTy(), MVT::Other);
Akira Hatanaka21afc632011-06-21 00:40:49 +00001409 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1410 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1411
1412 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001413}
1414
1415SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001416LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001417{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001418 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001419 // the block to branch to if the condition is true.
1420 SDValue Chain = Op.getOperand(0);
1421 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001422 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001423
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001424 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1425
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001426 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001427 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001428 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001429
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001430 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001431 Mips::CondCode CC =
1432 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001433 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001434
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001435 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001436 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001437}
1438
1439SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001440LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001441{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001442 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001443
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001444 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001445 if (Cond.getOpcode() != MipsISD::FPCmp)
1446 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001447
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001448 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1449 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001450}
1451
Dan Gohmand858e902010-04-17 15:26:15 +00001452SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1453 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001454 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001455 DebugLoc dl = Op.getDebugLoc();
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001456 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001457
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001458 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001459 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001460
Chris Lattnerb71b9092009-08-13 06:28:06 +00001461 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001462
Chris Lattnere3736f82009-08-13 05:41:27 +00001463 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001464 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1465 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001466 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001467 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1468 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001469 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001470 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001471 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001472 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1473 MipsII::MO_ABS_HI);
1474 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1475 MipsII::MO_ABS_LO);
1476 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1477 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001478 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001479 }
1480
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001481 EVT ValTy = Op.getValueType();
1482 bool HasGotOfst = (GV->hasInternalLinkage() ||
1483 (GV->hasLocalLinkage() && !isa<Function>(GV)));
1484 unsigned GotFlag = IsN64 ?
1485 (HasGotOfst ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT_DISP) :
Bruno Cardoso Lopese3d35722011-12-07 00:28:57 +00001486 (HasGotOfst ? MipsII::MO_GOT : MipsII::MO_GOT16);
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001487 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0, GotFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001488 GA = DAG.getNode(MipsISD::Wrapper, dl, ValTy, GA);
Akira Hatanaka82099682011-12-19 19:52:25 +00001489 SDValue ResNode = DAG.getLoad(ValTy, dl, DAG.getEntryNode(), GA,
1490 MachinePointerInfo(), false, false, false, 0);
Akira Hatanaka0f843822011-06-07 18:58:42 +00001491 // On functions and global targets not internal linked only
1492 // a load from got/GP is necessary for PIC to work.
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001493 if (!HasGotOfst)
Akira Hatanaka0f843822011-06-07 18:58:42 +00001494 return ResNode;
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001495 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0,
1496 IsN64 ? MipsII::MO_GOT_OFST :
1497 MipsII::MO_ABS_LO);
1498 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, GALo);
1499 return DAG.getNode(ISD::ADD, dl, ValTy, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001500}
1501
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001502SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1503 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001504 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1505 // FIXME there isn't actually debug info here
1506 DebugLoc dl = Op.getDebugLoc();
1507
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001508 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001509 // %hi/%lo relocation
Akira Hatanaka82099682011-12-19 19:52:25 +00001510 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true, MipsII::MO_ABS_HI);
1511 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true, MipsII::MO_ABS_LO);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001512 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1513 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1514 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001515 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001516
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001517 EVT ValTy = Op.getValueType();
1518 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1519 unsigned OFSTFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1520 SDValue BAGOTOffset = DAG.getBlockAddress(BA, ValTy, true, GOTFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001521 BAGOTOffset = DAG.getNode(MipsISD::Wrapper, dl, ValTy, BAGOTOffset);
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001522 SDValue BALOOffset = DAG.getBlockAddress(BA, ValTy, true, OFSTFlag);
Akira Hatanaka82099682011-12-19 19:52:25 +00001523 SDValue Load = DAG.getLoad(ValTy, dl, DAG.getEntryNode(), BAGOTOffset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001524 MachinePointerInfo(), false, false, false, 0);
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001525 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, BALOOffset);
1526 return DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001527}
1528
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001529SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001530LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001531{
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001532 // If the relocation model is PIC, use the General Dynamic TLS Model or
1533 // Local Dynamic TLS model, otherwise use the Initial Exec or
1534 // Local Exec TLS Model.
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001535
1536 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1537 DebugLoc dl = GA->getDebugLoc();
1538 const GlobalValue *GV = GA->getGlobal();
1539 EVT PtrVT = getPointerTy();
1540
1541 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1542 // General Dynamic TLS Model
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001543 bool LocalDynamic = GV->hasInternalLinkage();
1544 unsigned Flag = LocalDynamic ? MipsII::MO_TLSLDM :MipsII::MO_TLSGD;
1545 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, Flag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001546 SDValue Argument = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, TGA);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001547 unsigned PtrSize = PtrVT.getSizeInBits();
1548 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1549
Benjamin Kramer5eccf672011-12-11 12:21:34 +00001550 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001551
1552 ArgListTy Args;
1553 ArgListEntry Entry;
1554 Entry.Node = Argument;
Akira Hatanakaca074792011-12-08 20:34:32 +00001555 Entry.Ty = PtrTy;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001556 Args.push_back(Entry);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001557
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001558 std::pair<SDValue, SDValue> CallResult =
Akira Hatanakaca074792011-12-08 20:34:32 +00001559 LowerCallTo(DAG.getEntryNode(), PtrTy,
1560 false, false, false, false, 0, CallingConv::C, false, true,
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001561 TlsGetAddr, Args, DAG, dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001562
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001563 SDValue Ret = CallResult.first;
1564
1565 if (!LocalDynamic)
1566 return Ret;
1567
1568 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1569 MipsII::MO_DTPREL_HI);
1570 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, PtrVT, TGAHi);
1571 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1572 MipsII::MO_DTPREL_LO);
1573 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, TGALo);
1574 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Ret);
1575 return DAG.getNode(ISD::ADD, dl, PtrVT, Add, Lo);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001576 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001577
1578 SDValue Offset;
1579 if (GV->isDeclaration()) {
1580 // Initial Exec TLS Model
Akira Hatanakaca074792011-12-08 20:34:32 +00001581 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001582 MipsII::MO_GOTTPREL);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001583 TGA = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, TGA);
Akira Hatanakaca074792011-12-08 20:34:32 +00001584 Offset = DAG.getLoad(PtrVT, dl,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001585 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001586 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001587 } else {
1588 // Local Exec TLS Model
Akira Hatanakaca074792011-12-08 20:34:32 +00001589 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001590 MipsII::MO_TPREL_HI);
Akira Hatanakaca074792011-12-08 20:34:32 +00001591 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001592 MipsII::MO_TPREL_LO);
Akira Hatanakaca074792011-12-08 20:34:32 +00001593 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, PtrVT, TGAHi);
1594 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, TGALo);
1595 Offset = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001596 }
1597
1598 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1599 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001600}
1601
1602SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001603LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001604{
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001605 SDValue HiPart, JTI, JTILo;
Dale Johannesende064702009-02-06 21:50:26 +00001606 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001607 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001608 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Owen Andersone50ed302009-08-10 22:56:29 +00001609 EVT PtrVT = Op.getValueType();
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001610 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001611
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001612 if (!IsPIC && !IsN64) {
1613 JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MipsII::MO_ABS_HI);
1614 HiPart = DAG.getNode(MipsISD::Hi, dl, PtrVT, JTI);
1615 JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MipsII::MO_ABS_LO);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001616 } else {// Emit Load from Global Pointer
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001617 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1618 unsigned OfstFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1619 JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, GOTFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001620 JTI = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, JTI);
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001621 HiPart = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), JTI,
1622 MachinePointerInfo(), false, false, false, 0);
1623 JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OfstFlag);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001624 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001625
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001626 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, JTILo);
1627 return DAG.getNode(ISD::ADD, dl, PtrVT, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001628}
1629
Dan Gohman475871a2008-07-27 21:46:04 +00001630SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001631LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001632{
Dan Gohman475871a2008-07-27 21:46:04 +00001633 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001634 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001635 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001636 // FIXME there isn't actually debug info here
1637 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001638
1639 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001640 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001641 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001642 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001643 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001644 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001645 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1646 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001647 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001648
1649 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001650 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001651 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001652 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001653 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001654 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1655 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001656 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001657 } else {
Akira Hatanaka620db892011-11-16 22:44:38 +00001658 EVT ValTy = Op.getValueType();
1659 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1660 unsigned OFSTFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1661 SDValue CP = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1662 N->getOffset(), GOTFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001663 CP = DAG.getNode(MipsISD::Wrapper, dl, ValTy, CP);
Akira Hatanaka82099682011-12-19 19:52:25 +00001664 SDValue Load = DAG.getLoad(ValTy, dl, DAG.getEntryNode(), CP,
1665 MachinePointerInfo::getConstantPool(), false,
1666 false, false, 0);
Akira Hatanaka620db892011-11-16 22:44:38 +00001667 SDValue CPLo = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1668 N->getOffset(), OFSTFlag);
1669 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, CPLo);
1670 ResNode = DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001671 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001672
1673 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001674}
1675
Dan Gohmand858e902010-04-17 15:26:15 +00001676SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001677 MachineFunction &MF = DAG.getMachineFunction();
1678 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1679
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001680 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001681 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1682 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001683
1684 // vastart just stores the address of the VarArgsFrameIndex slot into the
1685 // memory location argument.
1686 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001687 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
Akira Hatanaka82099682011-12-19 19:52:25 +00001688 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001689}
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001690
1691// Called if the size of integer registers is large enough to hold the whole
1692// floating point number.
1693static SDValue LowerFCOPYSIGNLargeIntReg(SDValue Op, SelectionDAG &DAG) {
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001694 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001695 EVT ValTy = Op.getValueType();
1696 EVT IntValTy = MVT::getIntegerVT(ValTy.getSizeInBits());
1697 uint64_t Mask = (uint64_t)1 << (ValTy.getSizeInBits() - 1);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001698 DebugLoc dl = Op.getDebugLoc();
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001699 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, IntValTy, Op.getOperand(0));
1700 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, IntValTy, Op.getOperand(1));
1701 SDValue And0 = DAG.getNode(ISD::AND, dl, IntValTy, Op0,
1702 DAG.getConstant(Mask - 1, IntValTy));
1703 SDValue And1 = DAG.getNode(ISD::AND, dl, IntValTy, Op1,
1704 DAG.getConstant(Mask, IntValTy));
1705 SDValue Result = DAG.getNode(ISD::OR, dl, IntValTy, And0, And1);
1706 return DAG.getNode(ISD::BITCAST, dl, ValTy, Result);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001707}
1708
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001709// Called if the size of integer registers is not large enough to hold the whole
1710// floating point number (e.g. f64 & 32-bit integer register).
1711static SDValue
1712LowerFCOPYSIGNSmallIntReg(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001713 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001714 // Use ext/ins instructions if target architecture is Mips32r2.
1715 // Eliminate redundant mfc1 and mtc1 instructions.
1716 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001717
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001718 if (!isLittle)
1719 std::swap(LoIdx, HiIdx);
1720
1721 DebugLoc dl = Op.getDebugLoc();
1722 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1723 Op.getOperand(0),
1724 DAG.getConstant(LoIdx, MVT::i32));
1725 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1726 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1727 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1728 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1729 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1730 DAG.getConstant(0x7fffffff, MVT::i32));
1731 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1732 DAG.getConstant(0x80000000, MVT::i32));
1733 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1734
1735 if (!isLittle)
1736 std::swap(Word0, Word1);
1737
1738 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1739}
1740
Akira Hatanaka82099682011-12-19 19:52:25 +00001741SDValue
1742MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001743 EVT Ty = Op.getValueType();
1744
1745 assert(Ty == MVT::f32 || Ty == MVT::f64);
1746
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001747 if (Ty == MVT::f32 || HasMips64)
1748 return LowerFCOPYSIGNLargeIntReg(Op, DAG);
Akira Hatanaka82099682011-12-19 19:52:25 +00001749
1750 return LowerFCOPYSIGNSmallIntReg(Op, DAG, Subtarget->isLittle());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001751}
1752
Akira Hatanaka2e591472011-06-02 00:24:44 +00001753SDValue MipsTargetLowering::
1754LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001755 // check the depth
1756 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001757 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001758
1759 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1760 MFI->setFrameAddressIsTaken(true);
1761 EVT VT = Op.getValueType();
1762 DebugLoc dl = Op.getDebugLoc();
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001763 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
1764 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001765 return FrameAddr;
1766}
1767
Akira Hatanakadb548262011-07-19 23:30:50 +00001768// TODO: set SType according to the desired memory barrier behavior.
Akira Hatanaka82099682011-12-19 19:52:25 +00001769SDValue
1770MipsTargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG& DAG) const {
Akira Hatanakadb548262011-07-19 23:30:50 +00001771 unsigned SType = 0;
1772 DebugLoc dl = Op.getDebugLoc();
1773 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1774 DAG.getConstant(SType, MVT::i32));
1775}
1776
Eli Friedman14648462011-07-27 22:21:52 +00001777SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1778 SelectionDAG& DAG) const {
1779 // FIXME: Need pseudo-fence for 'singlethread' fences
1780 // FIXME: Set SType for weaker fences where supported/appropriate.
1781 unsigned SType = 0;
1782 DebugLoc dl = Op.getDebugLoc();
1783 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1784 DAG.getConstant(SType, MVT::i32));
1785}
1786
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001787//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001788// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001789//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001790
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001791//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001792// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001793// Mips O32 ABI rules:
1794// ---
1795// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001796// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001797// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001798// f64 - Only passed in two aliased f32 registers if no int reg has been used
1799// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001800// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1801// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001802//
1803// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001804//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001805
Duncan Sands1e96bab2010-11-04 10:49:57 +00001806static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001807 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001808 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1809
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001810 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001811
1812 static const unsigned IntRegs[] = {
1813 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1814 };
1815 static const unsigned F32Regs[] = {
1816 Mips::F12, Mips::F14
1817 };
1818 static const unsigned F64Regs[] = {
1819 Mips::D6, Mips::D7
1820 };
1821
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001822 // ByVal Args
1823 if (ArgFlags.isByVal()) {
1824 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1825 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1826 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1827 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1828 r < std::min(IntRegsSize, NextReg); ++r)
1829 State.AllocateReg(IntRegs[r]);
1830 return false;
1831 }
1832
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001833 // Promote i8 and i16
1834 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1835 LocVT = MVT::i32;
1836 if (ArgFlags.isSExt())
1837 LocInfo = CCValAssign::SExt;
1838 else if (ArgFlags.isZExt())
1839 LocInfo = CCValAssign::ZExt;
1840 else
1841 LocInfo = CCValAssign::AExt;
1842 }
1843
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001844 unsigned Reg;
1845
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001846 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1847 // is true: function is vararg, argument is 3rd or higher, there is previous
1848 // argument which is not f32 or f64.
1849 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1850 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001851 unsigned OrigAlign = ArgFlags.getOrigAlign();
1852 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001853
1854 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001855 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001856 // If this is the first part of an i64 arg,
1857 // the allocated register must be either A0 or A2.
1858 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1859 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001860 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001861 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1862 // Allocate int register and shadow next int register. If first
1863 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001864 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1865 if (Reg == Mips::A1 || Reg == Mips::A3)
1866 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1867 State.AllocateReg(IntRegs, IntRegsSize);
1868 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001869 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1870 // we are guaranteed to find an available float register
1871 if (ValVT == MVT::f32) {
1872 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1873 // Shadow int register
1874 State.AllocateReg(IntRegs, IntRegsSize);
1875 } else {
1876 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1877 // Shadow int registers
1878 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1879 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1880 State.AllocateReg(IntRegs, IntRegsSize);
1881 State.AllocateReg(IntRegs, IntRegsSize);
1882 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001883 } else
1884 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001885
Akira Hatanakad37776d2011-05-20 21:39:54 +00001886 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1887 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1888
1889 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001890 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001891 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001892 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001893
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001894 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001895}
1896
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00001897static const unsigned Mips64IntRegs[8] =
1898 {Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
1899 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64};
1900static const unsigned Mips64DPRegs[8] =
1901 {Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
1902 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64};
1903
1904static bool CC_Mips64Byval(unsigned ValNo, MVT ValVT, MVT LocVT,
1905 CCValAssign::LocInfo LocInfo,
1906 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1907 unsigned Align = std::max(ArgFlags.getByValAlign(), (unsigned)8);
1908 unsigned Size = (ArgFlags.getByValSize() + 7) / 8 * 8;
1909 unsigned FirstIdx = State.getFirstUnallocated(Mips64IntRegs, 8);
1910
1911 assert(Align <= 16 && "Cannot handle alignments larger than 16.");
1912
1913 // If byval is 16-byte aligned, the first arg register must be even.
1914 if ((Align == 16) && (FirstIdx % 2)) {
1915 State.AllocateReg(Mips64IntRegs[FirstIdx], Mips64DPRegs[FirstIdx]);
1916 ++FirstIdx;
1917 }
1918
1919 // Mark the registers allocated.
1920 for (unsigned I = FirstIdx; Size && (I < 8); Size -= 8, ++I)
1921 State.AllocateReg(Mips64IntRegs[I], Mips64DPRegs[I]);
1922
1923 // Allocate space on caller's stack.
1924 unsigned Offset = State.AllocateStack(Size, Align);
1925
1926 if (FirstIdx < 8)
1927 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Mips64IntRegs[FirstIdx],
1928 LocVT, LocInfo));
1929 else
1930 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
1931
1932 return true;
1933}
1934
1935#include "MipsGenCallingConv.inc"
1936
Akira Hatanaka49617092011-11-14 19:02:54 +00001937static void
1938AnalyzeMips64CallOperands(CCState CCInfo,
1939 const SmallVectorImpl<ISD::OutputArg> &Outs) {
1940 unsigned NumOps = Outs.size();
1941 for (unsigned i = 0; i != NumOps; ++i) {
1942 MVT ArgVT = Outs[i].VT;
1943 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
1944 bool R;
1945
1946 if (Outs[i].IsFixed)
1947 R = CC_MipsN(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
1948 else
1949 R = CC_MipsN_VarArg(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
1950
Akira Hatanaka49617092011-11-14 19:02:54 +00001951 if (R) {
Benjamin Kramer6296ee32011-11-14 19:51:48 +00001952#ifndef NDEBUG
Akira Hatanaka49617092011-11-14 19:02:54 +00001953 dbgs() << "Call operand #" << i << " has unhandled type "
1954 << EVT(ArgVT).getEVTString();
1955#endif
1956 llvm_unreachable(0);
1957 }
1958 }
1959}
1960
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001961//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001962// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001963//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001964
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001965static const unsigned O32IntRegsSize = 4;
1966
1967static const unsigned O32IntRegs[] = {
1968 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1969};
1970
Akira Hatanaka373e3a42011-09-23 00:58:33 +00001971// Return next O32 integer argument register.
1972static unsigned getNextIntArgReg(unsigned Reg) {
1973 assert((Reg == Mips::A0) || (Reg == Mips::A2));
1974 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
1975}
1976
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001977// Write ByVal Arg to arg registers and stack.
1978static void
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001979WriteByValArg(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001980 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1981 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1982 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001983 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001984 MVT PtrType, bool isLittle) {
1985 unsigned LocMemOffset = VA.getLocMemOffset();
1986 unsigned Offset = 0;
1987 uint32_t RemainingSize = Flags.getByValSize();
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001988 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001989
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001990 // Copy the first 4 words of byval arg to registers A0 - A3.
1991 // FIXME: Use a stricter alignment if it enables better optimization in passes
1992 // run later.
1993 for (; RemainingSize >= 4 && LocMemOffset < 4 * 4;
1994 Offset += 4, RemainingSize -= 4, LocMemOffset += 4) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001995 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001996 DAG.getConstant(Offset, MVT::i32));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001997 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
Akira Hatanaka82099682011-12-19 19:52:25 +00001998 MachinePointerInfo(), false, false, false,
1999 std::min(ByValAlign, (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002000 MemOpChains.push_back(LoadVal.getValue(1));
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002001 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002002 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
2003 }
2004
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002005 if (RemainingSize == 0)
2006 return;
2007
2008 // If there still is a register available for argument passing, write the
2009 // remaining part of the structure to it using subword loads and shifts.
2010 if (LocMemOffset < 4 * 4) {
2011 assert(RemainingSize <= 3 && RemainingSize >= 1 &&
2012 "There must be one to three bytes remaining.");
2013 unsigned LoadSize = (RemainingSize == 3 ? 2 : RemainingSize);
2014 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2015 DAG.getConstant(Offset, MVT::i32));
2016 unsigned Alignment = std::min(ByValAlign, (unsigned )4);
2017 SDValue LoadVal = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2018 LoadPtr, MachinePointerInfo(),
2019 MVT::getIntegerVT(LoadSize * 8), false,
2020 false, Alignment);
2021 MemOpChains.push_back(LoadVal.getValue(1));
2022
2023 // If target is big endian, shift it to the most significant half-word or
2024 // byte.
2025 if (!isLittle)
2026 LoadVal = DAG.getNode(ISD::SHL, dl, MVT::i32, LoadVal,
2027 DAG.getConstant(32 - LoadSize * 8, MVT::i32));
2028
2029 Offset += LoadSize;
2030 RemainingSize -= LoadSize;
2031
2032 // Read second subword if necessary.
2033 if (RemainingSize != 0) {
2034 assert(RemainingSize == 1 && "There must be one byte remaining.");
2035 LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2036 DAG.getConstant(Offset, MVT::i32));
2037 unsigned Alignment = std::min(ByValAlign, (unsigned )2);
2038 SDValue Subword = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2039 LoadPtr, MachinePointerInfo(),
2040 MVT::i8, false, false, Alignment);
2041 MemOpChains.push_back(Subword.getValue(1));
2042 // Insert the loaded byte to LoadVal.
2043 // FIXME: Use INS if supported by target.
2044 unsigned ShiftAmt = isLittle ? 16 : 8;
2045 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i32, Subword,
2046 DAG.getConstant(ShiftAmt, MVT::i32));
2047 LoadVal = DAG.getNode(ISD::OR, dl, MVT::i32, LoadVal, Shift);
2048 }
2049
2050 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
2051 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
2052 return;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002053 }
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002054
2055 // Create a fixed object on stack at offset LocMemOffset and copy
2056 // remaining part of byval arg to it using memcpy.
2057 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2058 DAG.getConstant(Offset, MVT::i32));
2059 LastFI = MFI->CreateFixedObject(RemainingSize, LocMemOffset, true);
2060 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002061 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2062 DAG.getConstant(RemainingSize, MVT::i32),
2063 std::min(ByValAlign, (unsigned)4),
2064 /*isVolatile=*/false, /*AlwaysInline=*/false,
2065 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002066}
2067
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002068// Copy Mips64 byVal arg to registers and stack.
2069void static
2070PassByValArg64(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
2071 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
2072 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
2073 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
2074 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2075 EVT PtrTy, bool isLittle) {
2076 unsigned ByValSize = Flags.getByValSize();
2077 unsigned Alignment = std::min(Flags.getByValAlign(), (unsigned)8);
2078 bool IsRegLoc = VA.isRegLoc();
2079 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
2080 unsigned LocMemOffset = 0;
Akira Hatanaka16040852011-11-15 18:42:25 +00002081 unsigned MemCpySize = ByValSize;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002082
2083 if (!IsRegLoc)
2084 LocMemOffset = VA.getLocMemOffset();
2085 else {
2086 const unsigned *Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8,
2087 VA.getLocReg());
2088 const unsigned *RegEnd = Mips64IntRegs + 8;
2089
2090 // Copy double words to registers.
2091 for (; (Reg != RegEnd) && (ByValSize >= Offset + 8); ++Reg, Offset += 8) {
2092 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2093 DAG.getConstant(Offset, PtrTy));
2094 SDValue LoadVal = DAG.getLoad(MVT::i64, dl, Chain, LoadPtr,
2095 MachinePointerInfo(), false, false, false,
2096 Alignment);
2097 MemOpChains.push_back(LoadVal.getValue(1));
2098 RegsToPass.push_back(std::make_pair(*Reg, LoadVal));
2099 }
2100
Akira Hatanaka16040852011-11-15 18:42:25 +00002101 // Return if the struct has been fully copied.
2102 if (!(MemCpySize = ByValSize - Offset))
2103 return;
2104
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002105 // If there is an argument register available, copy the remainder of the
2106 // byval argument with sub-doubleword loads and shifts.
Akira Hatanaka16040852011-11-15 18:42:25 +00002107 if (Reg != RegEnd) {
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002108 assert((ByValSize < Offset + 8) &&
2109 "Size of the remainder should be smaller than 8-byte.");
2110 SDValue Val;
2111 for (unsigned LoadSize = 4; Offset < ByValSize; LoadSize /= 2) {
2112 unsigned RemSize = ByValSize - Offset;
2113
2114 if (RemSize < LoadSize)
2115 continue;
2116
2117 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2118 DAG.getConstant(Offset, PtrTy));
2119 SDValue LoadVal =
2120 DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i64, Chain, LoadPtr,
2121 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
2122 false, false, Alignment);
2123 MemOpChains.push_back(LoadVal.getValue(1));
2124
2125 // Offset in number of bits from double word boundary.
2126 unsigned OffsetDW = (Offset % 8) * 8;
2127 unsigned Shamt = isLittle ? OffsetDW : 64 - (OffsetDW + LoadSize * 8);
2128 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i64, LoadVal,
2129 DAG.getConstant(Shamt, MVT::i32));
2130
2131 Val = Val.getNode() ? DAG.getNode(ISD::OR, dl, MVT::i64, Val, Shift) :
2132 Shift;
2133 Offset += LoadSize;
2134 Alignment = std::min(Alignment, LoadSize);
2135 }
2136
2137 RegsToPass.push_back(std::make_pair(*Reg, Val));
2138 return;
2139 }
2140 }
2141
Akira Hatanaka16040852011-11-15 18:42:25 +00002142 assert(MemCpySize && "MemCpySize must not be zero.");
2143
2144 // Create a fixed object on stack at offset LocMemOffset and copy
2145 // remainder of byval arg to it with memcpy.
2146 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2147 DAG.getConstant(Offset, PtrTy));
2148 LastFI = MFI->CreateFixedObject(MemCpySize, LocMemOffset, true);
2149 SDValue Dst = DAG.getFrameIndex(LastFI, PtrTy);
2150 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2151 DAG.getConstant(MemCpySize, PtrTy), Alignment,
2152 /*isVolatile=*/false, /*AlwaysInline=*/false,
2153 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002154}
2155
Dan Gohman98ca4f22009-08-05 01:29:28 +00002156/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002157/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002158/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002159SDValue
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002160MipsTargetLowering::LowerCall(SDValue InChain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002161 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002162 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002163 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002164 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002165 const SmallVectorImpl<ISD::InputArg> &Ins,
2166 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002167 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002168 // MIPs target does not yet support tail call optimization.
2169 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002170
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002171 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002172 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002173 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002174 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00002175 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002176
2177 // Analyze operands of the call, assigning locations to each operand.
2178 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002179 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002180 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002181
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002182 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002183 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanaka49617092011-11-14 19:02:54 +00002184 else if (HasMips64)
2185 AnalyzeMips64CallOperands(CCInfo, Outs);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00002186 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002187 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002188
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002189 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002190 unsigned NextStackOffset = CCInfo.getNextStackOffset();
2191
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002192 // Chain is the output chain of the last Load/Store or CopyToReg node.
2193 // ByValChain is the output chain of the last Memcpy node created for copying
2194 // byval arguments to the stack.
2195 SDValue Chain, CallSeqStart, ByValChain;
2196 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
2197 Chain = CallSeqStart = DAG.getCALLSEQ_START(InChain, NextStackOffsetVal);
2198 ByValChain = InChain;
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002199
2200 // If this is the first call, create a stack frame object that points to
2201 // a location to which .cprestore saves $gp.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002202 if (IsO32 && IsPIC && !MipsFI->getGPFI())
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002203 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
2204
Akira Hatanaka21afc632011-06-21 00:40:49 +00002205 // Get the frame index of the stack frame object that points to the location
2206 // of dynamically allocated area on the stack.
2207 int DynAllocFI = MipsFI->getDynAllocFI();
2208
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002209 // Update size of the maximum argument space.
2210 // For O32, a minimum of four words (16 bytes) of argument space is
2211 // allocated.
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002212 if (IsO32)
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002213 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
2214
2215 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
2216
2217 if (MaxCallFrameSize < NextStackOffset) {
2218 MipsFI->setMaxCallFrameSize(NextStackOffset);
2219
Akira Hatanaka21afc632011-06-21 00:40:49 +00002220 // Set the offsets relative to $sp of the $gp restore slot and dynamically
2221 // allocated stack space. These offsets must be aligned to a boundary
2222 // determined by the stack alignment of the ABI.
2223 unsigned StackAlignment = TFL->getStackAlignment();
2224 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
2225 StackAlignment * StackAlignment;
2226
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002227 if (MipsFI->needGPSaveRestore())
Akira Hatanaka21afc632011-06-21 00:40:49 +00002228 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
2229
2230 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002231 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002232
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002233 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002234 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
2235 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002236
Eric Christopher471e4222011-06-08 23:55:35 +00002237 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00002238
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002239 // Walk the register/memloc assignments, inserting copies/loads.
2240 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002241 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002242 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002243 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002244 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2245
2246 // ByVal Arg.
2247 if (Flags.isByVal()) {
2248 assert(Flags.getByValSize() &&
2249 "ByVal args of size 0 should have been ignored by front-end.");
2250 if (IsO32)
2251 WriteByValArg(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2252 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2253 Subtarget->isLittle());
2254 else
2255 PassByValArg64(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2256 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2257 Subtarget->isLittle());
2258 continue;
2259 }
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002260
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002261 // Promote the value if needed.
2262 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002263 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002264 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002265 if (VA.isRegLoc()) {
2266 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
2267 (ValVT == MVT::f64 && LocVT == MVT::i64))
2268 Arg = DAG.getNode(ISD::BITCAST, dl, LocVT, Arg);
2269 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002270 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2271 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002272 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2273 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002274 if (!Subtarget->isLittle())
2275 std::swap(Lo, Hi);
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002276 unsigned LocRegLo = VA.getLocReg();
2277 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2278 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2279 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002280 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002281 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002282 }
2283 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002284 case CCValAssign::SExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002285 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002286 break;
2287 case CCValAssign::ZExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002288 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002289 break;
2290 case CCValAssign::AExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002291 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002292 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002293 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002294
2295 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002296 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002297 if (VA.isRegLoc()) {
2298 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002299 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002300 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002301
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002302 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002303 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002304
Chris Lattnere0b12152008-03-17 06:57:02 +00002305 // Create the frame index object for this incoming parameter
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002306 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002307 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00002308 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00002309
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002310 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002311 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00002312 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Akira Hatanaka82099682011-12-19 19:52:25 +00002313 MachinePointerInfo(), false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002314 }
2315
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002316 // Extend range of indices of frame objects for outgoing arguments that were
2317 // created during this function call. Skip this step if no such objects were
2318 // created.
2319 if (LastFI)
2320 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2321
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002322 // If a memcpy has been created to copy a byval arg to a stack, replace the
2323 // chain input of CallSeqStart with ByValChain.
2324 if (InChain != ByValChain)
2325 DAG.UpdateNodeOperands(CallSeqStart.getNode(), ByValChain,
2326 NextStackOffsetVal);
2327
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002328 // Transform all store nodes into one single node because all store
2329 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002330 if (!MemOpChains.empty())
2331 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002332 &MemOpChains[0], MemOpChains.size());
2333
Bill Wendling056292f2008-09-16 21:48:12 +00002334 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002335 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2336 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002337 unsigned char OpFlag;
2338 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002339 bool GlobalOrExternal = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002340 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002341
2342 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002343 if (IsPICCall && G->getGlobal()->hasInternalLinkage()) {
2344 OpFlag = IsO32 ? MipsII::MO_GOT : MipsII::MO_GOT_PAGE;
2345 unsigned char LoFlag = IsO32 ? MipsII::MO_ABS_LO : MipsII::MO_GOT_OFST;
2346 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(), 0,
2347 OpFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002348 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002349 0, LoFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002350 } else {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002351 OpFlag = IsPICCall ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002352 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2353 getPointerTy(), 0, OpFlag);
2354 }
2355
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002356 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002357 }
2358 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002359 if (IsN64 || (!IsO32 && IsPIC))
2360 OpFlag = MipsII::MO_GOT_DISP;
2361 else if (!IsPIC) // !N64 && static
2362 OpFlag = MipsII::MO_NO_FLAG;
2363 else // O32 & PIC
2364 OpFlag = MipsII::MO_GOT_CALL;
Akira Hatanaka82099682011-12-19 19:52:25 +00002365 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2366 OpFlag);
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002367 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002368 }
2369
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002370 SDValue InFlag;
2371
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002372 // Create nodes that load address of callee and copy it to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002373 if (IsPICCall) {
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002374 if (GlobalOrExternal) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002375 // Load callee address
Akira Hatanaka6df7e232011-12-09 01:53:17 +00002376 Callee = DAG.getNode(MipsISD::Wrapper, dl, getPointerTy(), Callee);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002377 SDValue LoadValue = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
2378 Callee, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002379 false, false, false, 0);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002380
2381 // Use GOT+LO if callee has internal linkage.
2382 if (CalleeLo.getNode()) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002383 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, getPointerTy(), CalleeLo);
2384 Callee = DAG.getNode(ISD::ADD, dl, getPointerTy(), LoadValue, Lo);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002385 } else
2386 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002387 }
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002388 }
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002389
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002390 // T9 should contain the address of the callee function if
2391 // -reloction-model=pic or it is an indirect call.
2392 if (IsPICCall || !GlobalOrExternal) {
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002393 // copy to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002394 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
2395 Chain = DAG.getCopyToReg(Chain, dl, T9Reg, Callee, SDValue(0, 0));
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002396 InFlag = Chain.getValue(1);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002397 Callee = DAG.getRegister(T9Reg, getPointerTy());
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002398 }
Bill Wendling056292f2008-09-16 21:48:12 +00002399
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002400 // Build a sequence of copy-to-reg nodes chained together with token
2401 // chain and flag operands which copy the outgoing args into registers.
2402 // The InFlag in necessary since all emitted instructions must be
2403 // stuck together.
2404 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2405 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2406 RegsToPass[i].second, InFlag);
2407 InFlag = Chain.getValue(1);
2408 }
2409
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002410 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002411 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002412 //
2413 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002414 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002415 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002416 Ops.push_back(Chain);
2417 Ops.push_back(Callee);
2418
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002419 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002420 // known live into the call.
2421 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2422 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2423 RegsToPass[i].second.getValueType()));
2424
Gabor Greifba36cb52008-08-28 21:40:38 +00002425 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002426 Ops.push_back(InFlag);
2427
Dale Johannesen33c960f2009-02-04 20:06:27 +00002428 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002429 InFlag = Chain.getValue(1);
2430
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002431 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002432 Chain = DAG.getCALLSEQ_END(Chain,
2433 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002434 DAG.getIntPtrConstant(0, true), InFlag);
2435 InFlag = Chain.getValue(1);
2436
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002437 // Handle result values, copying them out of physregs into vregs that we
2438 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002439 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2440 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002441}
2442
Dan Gohman98ca4f22009-08-05 01:29:28 +00002443/// LowerCallResult - Lower the result values of a call into the
2444/// appropriate copies out of appropriate physical registers.
2445SDValue
2446MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002447 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002448 const SmallVectorImpl<ISD::InputArg> &Ins,
2449 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002450 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002451 // Assign locations to each value returned by this call.
2452 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002453 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2454 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002455
Dan Gohman98ca4f22009-08-05 01:29:28 +00002456 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002457
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002458 // Copy all of the result registers out of their specified physreg.
2459 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002460 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002461 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002462 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002463 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002464 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002465
Dan Gohman98ca4f22009-08-05 01:29:28 +00002466 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002467}
2468
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002469//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002470// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002471//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002472static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2473 std::vector<SDValue>& OutChains,
2474 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2475 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2476 unsigned LocMem = VA.getLocMemOffset();
2477 unsigned FirstWord = LocMem / 4;
2478
2479 // copy register A0 - A3 to frame object
2480 for (unsigned i = 0; i < NumWords; ++i) {
2481 unsigned CurWord = FirstWord + i;
2482 if (CurWord >= O32IntRegsSize)
2483 break;
2484
2485 unsigned SrcReg = O32IntRegs[CurWord];
2486 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2487 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2488 DAG.getConstant(i * 4, MVT::i32));
2489 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2490 StorePtr, MachinePointerInfo(), false,
2491 false, 0);
2492 OutChains.push_back(Store);
2493 }
2494}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002495
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002496// Create frame object on stack and copy registers used for byval passing to it.
2497static unsigned
2498CopyMips64ByValRegs(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2499 std::vector<SDValue>& OutChains, SelectionDAG &DAG,
2500 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2501 MachineFrameInfo *MFI, bool IsRegLoc,
2502 SmallVectorImpl<SDValue> &InVals, MipsFunctionInfo *MipsFI,
2503 EVT PtrTy) {
2504 const unsigned *Reg = Mips64IntRegs + 8;
2505 int FOOffset; // Frame object offset from virtual frame pointer.
2506
2507 if (IsRegLoc) {
2508 Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8, VA.getLocReg());
2509 FOOffset = (Reg - Mips64IntRegs) * 8 - 8 * 8;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002510 }
2511 else
2512 FOOffset = VA.getLocMemOffset();
2513
2514 // Create frame object.
2515 unsigned NumRegs = (Flags.getByValSize() + 7) / 8;
2516 unsigned LastFI = MFI->CreateFixedObject(NumRegs * 8, FOOffset, true);
2517 SDValue FIN = DAG.getFrameIndex(LastFI, PtrTy);
2518 InVals.push_back(FIN);
2519
2520 // Copy arg registers.
2521 for (unsigned I = 0; (Reg != Mips64IntRegs + 8) && (I < NumRegs);
2522 ++Reg, ++I) {
2523 unsigned VReg = AddLiveIn(MF, *Reg, Mips::CPU64RegsRegisterClass);
2524 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, PtrTy, FIN,
2525 DAG.getConstant(I * 8, PtrTy));
2526 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(VReg, MVT::i64),
2527 StorePtr, MachinePointerInfo(), false,
2528 false, 0);
2529 OutChains.push_back(Store);
2530 }
2531
2532 return LastFI;
2533}
2534
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002535/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002536/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002537SDValue
2538MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002539 CallingConv::ID CallConv,
2540 bool isVarArg,
Akira Hatanaka82099682011-12-19 19:52:25 +00002541 const SmallVectorImpl<ISD::InputArg> &Ins,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002542 DebugLoc dl, SelectionDAG &DAG,
2543 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002544 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002545 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002546 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002547 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002548
Dan Gohman1e93df62010-04-17 14:41:14 +00002549 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002550
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002551 // Used with vargs to acumulate store chains.
2552 std::vector<SDValue> OutChains;
2553
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002554 // Assign locations to all of the incoming arguments.
2555 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002556 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002557 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002558
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002559 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002560 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002561 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002562 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002563
Akira Hatanaka43299772011-05-20 23:22:14 +00002564 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002565
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002566 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002567 CCValAssign &VA = ArgLocs[i];
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002568 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002569 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2570 bool IsRegLoc = VA.isRegLoc();
2571
2572 if (Flags.isByVal()) {
2573 assert(Flags.getByValSize() &&
2574 "ByVal args of size 0 should have been ignored by front-end.");
2575 if (IsO32) {
2576 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2577 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2578 true);
2579 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2580 InVals.push_back(FIN);
2581 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2582 } else // N32/64
2583 LastFI = CopyMips64ByValRegs(MF, Chain, dl, OutChains, DAG, VA, Flags,
2584 MFI, IsRegLoc, InVals, MipsFI,
2585 getPointerTy());
2586 continue;
2587 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002588
2589 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002590 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002591 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002592 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002593 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002594
Owen Anderson825b72b2009-08-11 20:47:22 +00002595 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002596 RC = Mips::CPURegsRegisterClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002597 else if (RegVT == MVT::i64)
2598 RC = Mips::CPU64RegsRegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002599 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002600 RC = Mips::FGR32RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002601 else if (RegVT == MVT::f64)
Akira Hatanakaf40de9d2011-09-26 21:55:17 +00002602 RC = HasMips64 ? Mips::FGR64RegisterClass : Mips::AFGR64RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002603 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002604 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002605
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002606 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002607 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002608 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002609 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002610
2611 // If this is an 8 or 16-bit value, it has been passed promoted
2612 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002613 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002614 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002615 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002616 if (VA.getLocInfo() == CCValAssign::SExt)
2617 Opcode = ISD::AssertSext;
2618 else if (VA.getLocInfo() == CCValAssign::ZExt)
2619 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002620 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002621 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002622 DAG.getValueType(ValVT));
2623 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002624 }
2625
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002626 // Handle floating point arguments passed in integer registers.
2627 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
2628 (RegVT == MVT::i64 && ValVT == MVT::f64))
2629 ArgValue = DAG.getNode(ISD::BITCAST, dl, ValVT, ArgValue);
2630 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
2631 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
2632 getNextIntArgReg(ArgReg), RC);
2633 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
2634 if (!Subtarget->isLittle())
2635 std::swap(ArgValue, ArgValue2);
2636 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2637 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002638 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002639
Dan Gohman98ca4f22009-08-05 01:29:28 +00002640 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002641 } else { // VA.isRegLoc()
2642
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002643 // sanity check
2644 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002645
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002646 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002647 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002648 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002649
2650 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002651 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002652 InVals.push_back(DAG.getLoad(ValVT, dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002653 MachinePointerInfo::getFixedStack(LastFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002654 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002655 }
2656 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002657
2658 // The mips ABIs for returning structs by value requires that we copy
2659 // the sret argument into $v0 for the return. Save the argument into
2660 // a virtual register so that we can access it from the return points.
2661 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2662 unsigned Reg = MipsFI->getSRetReturnReg();
2663 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002664 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002665 MipsFI->setSRetReturnReg(Reg);
2666 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002667 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002668 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002669 }
2670
Akira Hatanakabad53f42011-11-14 19:01:09 +00002671 if (isVarArg) {
2672 unsigned NumOfRegs = IsO32 ? 4 : 8;
2673 const unsigned *ArgRegs = IsO32 ? O32IntRegs : Mips64IntRegs;
2674 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumOfRegs);
2675 int FirstRegSlotOffset = IsO32 ? 0 : -64 ; // offset of $a0's slot.
2676 TargetRegisterClass *RC
2677 = IsO32 ? Mips::CPURegsRegisterClass : Mips::CPU64RegsRegisterClass;
2678 unsigned RegSize = RC->getSize();
2679 int RegSlotOffset = FirstRegSlotOffset + Idx * RegSize;
2680
2681 // Offset of the first variable argument from stack pointer.
2682 int FirstVaArgOffset;
2683
2684 if (IsO32 || (Idx == NumOfRegs)) {
2685 FirstVaArgOffset =
2686 (CCInfo.getNextStackOffset() + RegSize - 1) / RegSize * RegSize;
2687 } else
2688 FirstVaArgOffset = RegSlotOffset;
2689
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002690 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002691 // which is a value necessary to VASTART.
Akira Hatanakabad53f42011-11-14 19:01:09 +00002692 LastFI = MFI->CreateFixedObject(RegSize, FirstVaArgOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002693 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002694
Akira Hatanakabad53f42011-11-14 19:01:09 +00002695 // Copy the integer registers that have not been used for argument passing
2696 // to the argument register save area. For O32, the save area is allocated
2697 // in the caller's stack frame, while for N32/64, it is allocated in the
2698 // callee's stack frame.
2699 for (int StackOffset = RegSlotOffset;
2700 Idx < NumOfRegs; ++Idx, StackOffset += RegSize) {
2701 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegs[Idx], RC);
2702 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2703 MVT::getIntegerVT(RegSize * 8));
2704 LastFI = MFI->CreateFixedObject(RegSize, StackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002705 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2706 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
Akira Hatanaka82099682011-12-19 19:52:25 +00002707 MachinePointerInfo(), false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002708 }
2709 }
2710
Akira Hatanaka43299772011-05-20 23:22:14 +00002711 MipsFI->setLastInArgFI(LastFI);
2712
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002713 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002714 // the size of Ins and InVals. This only happens when on varg functions
2715 if (!OutChains.empty()) {
2716 OutChains.push_back(Chain);
2717 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2718 &OutChains[0], OutChains.size());
2719 }
2720
Dan Gohman98ca4f22009-08-05 01:29:28 +00002721 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002722}
2723
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002724//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002725// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002726//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002727
Dan Gohman98ca4f22009-08-05 01:29:28 +00002728SDValue
2729MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002730 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002731 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002732 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002733 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002734
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002735 // CCValAssign - represent the assignment of
2736 // the return value to a location
2737 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002738
2739 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002740 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2741 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002742
Dan Gohman98ca4f22009-08-05 01:29:28 +00002743 // Analize return values.
2744 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002745
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002746 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002747 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002748 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002749 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002750 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002751 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002752 }
2753
Dan Gohman475871a2008-07-27 21:46:04 +00002754 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002755
2756 // Copy the result values into the output registers.
2757 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2758 CCValAssign &VA = RVLocs[i];
2759 assert(VA.isRegLoc() && "Can only return in registers!");
2760
Akira Hatanaka82099682011-12-19 19:52:25 +00002761 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002762
2763 // guarantee that all emitted copies are
2764 // stuck together, avoiding something bad
2765 Flag = Chain.getValue(1);
2766 }
2767
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002768 // The mips ABIs for returning structs by value requires that we copy
2769 // the sret argument into $v0 for the return. We saved the argument into
2770 // a virtual register in the entry block, so now we copy the value out
2771 // and into $v0.
2772 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2773 MachineFunction &MF = DAG.getMachineFunction();
2774 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2775 unsigned Reg = MipsFI->getSRetReturnReg();
2776
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002777 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002778 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002779 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002780
Dale Johannesena05dca42009-02-04 23:02:30 +00002781 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002782 Flag = Chain.getValue(1);
2783 }
2784
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002785 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002786 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002787 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002788 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002789 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002790 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002791 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002792}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002793
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002794//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002795// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002796//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002797
2798/// getConstraintType - Given a constraint letter, return the type of
2799/// constraint it is for this target.
2800MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002801getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002802{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002803 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002804 // GCC config/mips/constraints.md
2805 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002806 // 'd' : An address register. Equivalent to r
2807 // unless generating MIPS16 code.
2808 // 'y' : Equivalent to r; retained for
2809 // backwards compatibility.
2810 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002811 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002812 switch (Constraint[0]) {
2813 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002814 case 'd':
2815 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002816 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002817 return C_RegisterClass;
2818 break;
2819 }
2820 }
2821 return TargetLowering::getConstraintType(Constraint);
2822}
2823
John Thompson44ab89e2010-10-29 17:29:13 +00002824/// Examine constraint type and operand type and determine a weight value.
2825/// This object must already have been set up with the operand type
2826/// and the current alternative constraint selected.
2827TargetLowering::ConstraintWeight
2828MipsTargetLowering::getSingleConstraintMatchWeight(
2829 AsmOperandInfo &info, const char *constraint) const {
2830 ConstraintWeight weight = CW_Invalid;
2831 Value *CallOperandVal = info.CallOperandVal;
2832 // If we don't have a value, we can't do a match,
2833 // but allow it at the lowest weight.
2834 if (CallOperandVal == NULL)
2835 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002836 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002837 // Look at the constraint type.
2838 switch (*constraint) {
2839 default:
2840 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2841 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002842 case 'd':
2843 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002844 if (type->isIntegerTy())
2845 weight = CW_Register;
2846 break;
2847 case 'f':
2848 if (type->isFloatTy())
2849 weight = CW_Register;
2850 break;
2851 }
2852 return weight;
2853}
2854
Eric Christopher38d64262011-06-29 19:33:04 +00002855/// Given a register class constraint, like 'r', if this corresponds directly
2856/// to an LLVM register class, return a register of 0 and the register class
2857/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002858std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002859getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002860{
2861 if (Constraint.size() == 1) {
2862 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002863 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2864 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002865 case 'r':
2866 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002867 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002868 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002869 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002870 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002871 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2872 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002873 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002874 }
2875 }
2876 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2877}
2878
Dan Gohman6520e202008-10-18 02:06:02 +00002879bool
2880MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2881 // The Mips target isn't yet aware of offsets.
2882 return false;
2883}
Evan Chengeb2f9692009-10-27 19:56:55 +00002884
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002885bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2886 if (VT != MVT::f32 && VT != MVT::f64)
2887 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002888 if (Imm.isNegZero())
2889 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002890 return Imm.isZero();
2891}