blob: 4bd3d8afb840de9be8c772997071cc29e2c72dee [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 Hatanaka6df7e232011-12-09 01:53:17 +000072 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Akira Hatanaka21afc632011-06-21 00:40:49 +000073 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
Akira Hatanakadb548262011-07-19 23:30:50 +000074 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanakabb15e112011-08-17 02:05:42 +000075 case MipsISD::Ext: return "MipsISD::Ext";
76 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanaka0f843822011-06-07 18:58:42 +000077 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000078 }
79}
80
81MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000082MipsTargetLowering(MipsTargetMachine &TM)
Akira Hatanaka8b4198d2011-09-26 21:47:02 +000083 : TargetLowering(TM, new MipsTargetObjectFile()),
84 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +000085 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
86 IsO32(Subtarget->isABI_O32()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000087
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000088 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000089 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000090 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +000091 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000092
93 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000094 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
95 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000096
Akira Hatanaka95934842011-09-24 01:34:44 +000097 if (HasMips64)
98 addRegisterClass(MVT::i64, Mips::CPU64RegsRegisterClass);
99
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000100 // When dealing with single precision only, use libcalls
Akira Hatanaka792016b2011-09-23 18:28:39 +0000101 if (!Subtarget->isSingleFloat()) {
102 if (HasMips64)
103 addRegisterClass(MVT::f64, Mips::FGR64RegisterClass);
104 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000105 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Akira Hatanaka792016b2011-09-23 18:28:39 +0000106 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000107
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000108 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000109 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
110 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
111 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000112
Eli Friedman6055a6a2009-07-17 04:07:24 +0000113 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000114 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
115 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000116
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000117 // Used by legalize types to correctly generate the setcc result.
118 // Without this, every float setcc comes with a AND/OR with the result,
119 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000120 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000121 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000122
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000123 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +0000124 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Akira Hatanakaa5903ac2011-10-11 00:55:05 +0000125 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000126 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Akira Hatanaka9b944a82011-11-16 22:42:10 +0000127 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000128 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Akira Hatanakaca074792011-12-08 20:34:32 +0000129 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +0000131 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Akira Hatanaka620db892011-11-16 22:44:38 +0000133 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 setOperationAction(ISD::SELECT, MVT::f32, Custom);
135 setOperationAction(ISD::SELECT, MVT::f64, Custom);
136 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000137 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
138 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000139 setOperationAction(ISD::VASTART, MVT::Other, Custom);
140
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000141 setOperationAction(ISD::SDIV, MVT::i32, Expand);
142 setOperationAction(ISD::SREM, MVT::i32, Expand);
143 setOperationAction(ISD::UDIV, MVT::i32, Expand);
144 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000145 setOperationAction(ISD::SDIV, MVT::i64, Expand);
146 setOperationAction(ISD::SREM, MVT::i64, Expand);
147 setOperationAction(ISD::UDIV, MVT::i64, Expand);
148 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000149
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000150 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
152 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
153 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
154 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
155 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
156 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
157 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
158 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
159 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000160 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000161
Akira Hatanaka56633442011-09-20 23:53:09 +0000162 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000163 setOperationAction(ISD::ROTR, MVT::i32, Expand);
164
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000165 if (!Subtarget->hasMips64r2())
166 setOperationAction(ISD::ROTR, MVT::i64, Expand);
167
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
169 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
170 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000171 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
172 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000173 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000174 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000175 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000176 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
178 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000179 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 setOperationAction(ISD::FLOG, MVT::f32, Expand);
181 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
182 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
183 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000184 setOperationAction(ISD::FMA, MVT::f32, Expand);
185 setOperationAction(ISD::FMA, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000186
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000187 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
188 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Eric Christopher471e4222011-06-08 23:55:35 +0000189
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000190 setOperationAction(ISD::VAARG, MVT::Other, Expand);
191 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
192 setOperationAction(ISD::VAEND, MVT::Other, Expand);
193
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000194 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000195 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
196 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000197
Akira Hatanakadb548262011-07-19 23:30:50 +0000198 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000199 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000200
Eli Friedman4db5aca2011-08-29 18:23:02 +0000201 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
202 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
203
Eli Friedman26689ac2011-08-03 21:06:02 +0000204 setInsertFencesForAtomic(true);
205
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000206 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000207 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000208
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000209 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
211 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000212 }
213
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000214 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000215 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000216
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000217 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000218 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000219
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000220 setTargetDAGCombine(ISD::ADDE);
221 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000222 setTargetDAGCombine(ISD::SDIVREM);
223 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000224 setTargetDAGCombine(ISD::SETCC);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000225 setTargetDAGCombine(ISD::AND);
226 setTargetDAGCombine(ISD::OR);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000227
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000228 setMinFunctionAlignment(2);
229
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000230 setStackPointerRegisterToSaveRestore(Mips::SP);
231 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000232
233 setExceptionPointerRegister(Mips::A0);
234 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000235}
236
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000237bool MipsTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Akira Hatanaka511961a2011-08-17 18:49:18 +0000238 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
Akira Hatanaka7bd19bd2011-10-11 00:27:28 +0000239 return SVT == MVT::i64 || SVT == MVT::i32 || SVT == MVT::i16;
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000240}
241
Duncan Sands28b77e92011-09-06 19:07:46 +0000242EVT MipsTargetLowering::getSetCCResultType(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000244}
245
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000246// SelectMadd -
247// Transforms a subgraph in CurDAG if the following pattern is found:
248// (addc multLo, Lo0), (adde multHi, Hi0),
249// where,
250// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000251// Lo0: initial value of Lo register
252// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000253// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000254static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000255 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000256 // for the matching to be successful.
257 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
258
259 if (ADDCNode->getOpcode() != ISD::ADDC)
260 return false;
261
262 SDValue MultHi = ADDENode->getOperand(0);
263 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000264 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000265 unsigned MultOpc = MultHi.getOpcode();
266
267 // MultHi and MultLo must be generated by the same node,
268 if (MultLo.getNode() != MultNode)
269 return false;
270
271 // and it must be a multiplication.
272 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
273 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000274
275 // MultLo amd MultHi must be the first and second output of MultNode
276 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000277 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
278 return false;
279
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000280 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000281 // of the values of MultNode, in which case MultNode will be removed in later
282 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000283 // If there exist users other than ADDENode or ADDCNode, this function returns
284 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000285 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000286 // produced.
287 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
288 return false;
289
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000290 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000291 DebugLoc dl = ADDENode->getDebugLoc();
292
293 // create MipsMAdd(u) node
294 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000295
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000296 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
297 MVT::Glue,
298 MultNode->getOperand(0),// Factor 0
299 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000300 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000301 ADDENode->getOperand(1));// Hi0
302
303 // create CopyFromReg nodes
304 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
305 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000306 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000307 Mips::HI, MVT::i32,
308 CopyFromLo.getValue(2));
309
310 // replace uses of adde and addc here
311 if (!SDValue(ADDCNode, 0).use_empty())
312 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
313
314 if (!SDValue(ADDENode, 0).use_empty())
315 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
316
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000317 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000318}
319
320// SelectMsub -
321// Transforms a subgraph in CurDAG if the following pattern is found:
322// (addc Lo0, multLo), (sube Hi0, multHi),
323// where,
324// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000325// Lo0: initial value of Lo register
326// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000327// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000328static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000329 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000330 // for the matching to be successful.
331 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
332
333 if (SUBCNode->getOpcode() != ISD::SUBC)
334 return false;
335
336 SDValue MultHi = SUBENode->getOperand(1);
337 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000338 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000339 unsigned MultOpc = MultHi.getOpcode();
340
341 // MultHi and MultLo must be generated by the same node,
342 if (MultLo.getNode() != MultNode)
343 return false;
344
345 // and it must be a multiplication.
346 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
347 return false;
348
349 // MultLo amd MultHi must be the first and second output of MultNode
350 // respectively.
351 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
352 return false;
353
354 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
355 // of the values of MultNode, in which case MultNode will be removed in later
356 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000357 // If there exist users other than SUBENode or SUBCNode, this function returns
358 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000359 // instruction node rather than a pair of MULT and MSUB instructions being
360 // produced.
361 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
362 return false;
363
364 SDValue Chain = CurDAG->getEntryNode();
365 DebugLoc dl = SUBENode->getDebugLoc();
366
367 // create MipsSub(u) node
368 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
369
370 SDValue MSub = CurDAG->getNode(MultOpc, dl,
371 MVT::Glue,
372 MultNode->getOperand(0),// Factor 0
373 MultNode->getOperand(1),// Factor 1
374 SUBCNode->getOperand(0),// Lo0
375 SUBENode->getOperand(0));// Hi0
376
377 // create CopyFromReg nodes
378 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
379 MSub);
380 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
381 Mips::HI, MVT::i32,
382 CopyFromLo.getValue(2));
383
384 // replace uses of sube and subc here
385 if (!SDValue(SUBCNode, 0).use_empty())
386 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
387
388 if (!SDValue(SUBENode, 0).use_empty())
389 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
390
391 return true;
392}
393
394static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
395 TargetLowering::DAGCombinerInfo &DCI,
396 const MipsSubtarget* Subtarget) {
397 if (DCI.isBeforeLegalize())
398 return SDValue();
399
Akira Hatanakae184fec2011-11-11 04:18:21 +0000400 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
401 SelectMadd(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000402 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000403
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000404 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000405}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000406
407static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
408 TargetLowering::DAGCombinerInfo &DCI,
409 const MipsSubtarget* Subtarget) {
410 if (DCI.isBeforeLegalize())
411 return SDValue();
412
Akira Hatanakae184fec2011-11-11 04:18:21 +0000413 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
414 SelectMsub(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000415 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000416
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000417 return SDValue();
418}
419
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000420static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
421 TargetLowering::DAGCombinerInfo &DCI,
422 const MipsSubtarget* Subtarget) {
423 if (DCI.isBeforeLegalizeOps())
424 return SDValue();
425
Akira Hatanakadda4a072011-10-03 21:06:13 +0000426 EVT Ty = N->getValueType(0);
427 unsigned LO = (Ty == MVT::i32) ? Mips::LO : Mips::LO64;
428 unsigned HI = (Ty == MVT::i32) ? Mips::HI : Mips::HI64;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000429 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
430 MipsISD::DivRemU;
431 DebugLoc dl = N->getDebugLoc();
432
433 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
434 N->getOperand(0), N->getOperand(1));
435 SDValue InChain = DAG.getEntryNode();
436 SDValue InGlue = DivRem;
437
438 // insert MFLO
439 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakadda4a072011-10-03 21:06:13 +0000440 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000441 InGlue);
442 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
443 InChain = CopyFromLo.getValue(1);
444 InGlue = CopyFromLo.getValue(2);
445 }
446
447 // insert MFHI
448 if (N->hasAnyUseOfValue(1)) {
449 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000450 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000451 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
452 }
453
454 return SDValue();
455}
456
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000457static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
458 switch (CC) {
459 default: llvm_unreachable("Unknown fp condition code!");
460 case ISD::SETEQ:
461 case ISD::SETOEQ: return Mips::FCOND_OEQ;
462 case ISD::SETUNE: return Mips::FCOND_UNE;
463 case ISD::SETLT:
464 case ISD::SETOLT: return Mips::FCOND_OLT;
465 case ISD::SETGT:
466 case ISD::SETOGT: return Mips::FCOND_OGT;
467 case ISD::SETLE:
468 case ISD::SETOLE: return Mips::FCOND_OLE;
469 case ISD::SETGE:
470 case ISD::SETOGE: return Mips::FCOND_OGE;
471 case ISD::SETULT: return Mips::FCOND_ULT;
472 case ISD::SETULE: return Mips::FCOND_ULE;
473 case ISD::SETUGT: return Mips::FCOND_UGT;
474 case ISD::SETUGE: return Mips::FCOND_UGE;
475 case ISD::SETUO: return Mips::FCOND_UN;
476 case ISD::SETO: return Mips::FCOND_OR;
477 case ISD::SETNE:
478 case ISD::SETONE: return Mips::FCOND_ONE;
479 case ISD::SETUEQ: return Mips::FCOND_UEQ;
480 }
481}
482
483
484// Returns true if condition code has to be inverted.
485static bool InvertFPCondCode(Mips::CondCode CC) {
486 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
487 return false;
488
489 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
490 return true;
491
492 assert(false && "Illegal Condition Code");
493 return false;
494}
495
496// Creates and returns an FPCmp node from a setcc node.
497// Returns Op if setcc is not a floating point comparison.
498static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
499 // must be a SETCC node
500 if (Op.getOpcode() != ISD::SETCC)
501 return Op;
502
503 SDValue LHS = Op.getOperand(0);
504
505 if (!LHS.getValueType().isFloatingPoint())
506 return Op;
507
508 SDValue RHS = Op.getOperand(1);
509 DebugLoc dl = Op.getDebugLoc();
510
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000511 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
512 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000513 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
514
515 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
516 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
517}
518
519// Creates and returns a CMovFPT/F node.
520static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
521 SDValue False, DebugLoc DL) {
522 bool invert = InvertFPCondCode((Mips::CondCode)
523 cast<ConstantSDNode>(Cond.getOperand(2))
524 ->getSExtValue());
525
526 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
527 True.getValueType(), True, False, Cond);
528}
529
530static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
531 TargetLowering::DAGCombinerInfo &DCI,
532 const MipsSubtarget* Subtarget) {
533 if (DCI.isBeforeLegalizeOps())
534 return SDValue();
535
536 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
537
538 if (Cond.getOpcode() != MipsISD::FPCmp)
539 return SDValue();
540
541 SDValue True = DAG.getConstant(1, MVT::i32);
542 SDValue False = DAG.getConstant(0, MVT::i32);
543
544 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
545}
546
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000547static SDValue PerformANDCombine(SDNode *N, SelectionDAG& DAG,
548 TargetLowering::DAGCombinerInfo &DCI,
549 const MipsSubtarget* Subtarget) {
550 // Pattern match EXT.
551 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
552 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000553 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000554 return SDValue();
555
556 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000557 unsigned ShiftRightOpc = ShiftRight.getOpcode();
558
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000559 // Op's first operand must be a shift right.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000560 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000561 return SDValue();
562
563 // The second operand of the shift must be an immediate.
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000564 ConstantSDNode *CN;
565 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
566 return SDValue();
567
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000568 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000569 uint64_t SMPos, SMSize;
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000570
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000571 // Op's second operand must be a shifted mask.
572 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000573 !IsShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000574 return SDValue();
575
576 // Return if the shifted mask does not start at bit 0 or the sum of its size
577 // and Pos exceeds the word's size.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000578 EVT ValTy = N->getValueType(0);
579 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000580 return SDValue();
581
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000582 return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), ValTy,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000583 ShiftRight.getOperand(0),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000584 DAG.getConstant(Pos, MVT::i32),
585 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000586}
587
588static SDValue PerformORCombine(SDNode *N, SelectionDAG& DAG,
589 TargetLowering::DAGCombinerInfo &DCI,
590 const MipsSubtarget* Subtarget) {
591 // Pattern match INS.
592 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
593 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
594 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000595 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000596 return SDValue();
597
598 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
599 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
600 ConstantSDNode *CN;
601
602 // See if Op's first operand matches (and $src1 , mask0).
603 if (And0.getOpcode() != ISD::AND)
604 return SDValue();
605
606 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000607 !IsShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000608 return SDValue();
609
610 // See if Op's second operand matches (and (shl $src, pos), mask1).
611 if (And1.getOpcode() != ISD::AND)
612 return SDValue();
613
614 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000615 !IsShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000616 return SDValue();
617
618 // The shift masks must have the same position and size.
619 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
620 return SDValue();
621
622 SDValue Shl = And1.getOperand(0);
623 if (Shl.getOpcode() != ISD::SHL)
624 return SDValue();
625
626 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
627 return SDValue();
628
629 unsigned Shamt = CN->getZExtValue();
630
631 // Return if the shift amount and the first bit position of mask are not the
632 // same.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000633 EVT ValTy = N->getValueType(0);
634 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000635 return SDValue();
636
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000637 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), ValTy,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000638 Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000639 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000640 DAG.getConstant(SMSize0, MVT::i32),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000641 And0.getOperand(0));
642}
643
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000644SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000645 const {
646 SelectionDAG &DAG = DCI.DAG;
647 unsigned opc = N->getOpcode();
648
649 switch (opc) {
650 default: break;
651 case ISD::ADDE:
652 return PerformADDECombine(N, DAG, DCI, Subtarget);
653 case ISD::SUBE:
654 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000655 case ISD::SDIVREM:
656 case ISD::UDIVREM:
657 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000658 case ISD::SETCC:
659 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000660 case ISD::AND:
661 return PerformANDCombine(N, DAG, DCI, Subtarget);
662 case ISD::OR:
663 return PerformORCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000664 }
665
666 return SDValue();
667}
668
Dan Gohman475871a2008-07-27 21:46:04 +0000669SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000670LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000671{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000672 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000673 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000674 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000675 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
676 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000677 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000678 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000679 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
680 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000681 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000682 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000683 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000684 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Akira Hatanakadb548262011-07-19 23:30:50 +0000685 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +0000686 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000687 }
Dan Gohman475871a2008-07-27 21:46:04 +0000688 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000689}
690
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000691//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000692// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000693//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000694
695// AddLiveIn - This helper function adds the specified physical register to the
696// MachineFunction as a live in value. It also creates a corresponding
697// virtual register for it.
698static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000699AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000700{
701 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000702 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
703 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000704 return VReg;
705}
706
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000707// Get fp branch code (not opcode) from condition code.
708static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
709 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
710 return Mips::BRANCH_T;
711
712 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
713 return Mips::BRANCH_F;
714
715 return Mips::BRANCH_INVALID;
716}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000717
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000718/*
Akira Hatanaka14487d42011-06-07 19:28:39 +0000719static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
720 DebugLoc dl,
721 const MipsSubtarget* Subtarget,
722 const TargetInstrInfo *TII,
723 bool isFPCmp, unsigned Opc) {
724 // There is no need to expand CMov instructions if target has
725 // conditional moves.
726 if (Subtarget->hasCondMov())
727 return BB;
728
729 // To "insert" a SELECT_CC instruction, we actually have to insert the
730 // diamond control-flow pattern. The incoming instruction knows the
731 // destination vreg to set, the condition code register to branch on, the
732 // true/false values to select between, and a branch opcode to use.
733 const BasicBlock *LLVM_BB = BB->getBasicBlock();
734 MachineFunction::iterator It = BB;
735 ++It;
736
737 // thisMBB:
738 // ...
739 // TrueVal = ...
740 // setcc r1, r2, r3
741 // bNE r1, r0, copy1MBB
742 // fallthrough --> copy0MBB
743 MachineBasicBlock *thisMBB = BB;
744 MachineFunction *F = BB->getParent();
745 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
746 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
747 F->insert(It, copy0MBB);
748 F->insert(It, sinkMBB);
749
750 // Transfer the remainder of BB and its successor edges to sinkMBB.
751 sinkMBB->splice(sinkMBB->begin(), BB,
752 llvm::next(MachineBasicBlock::iterator(MI)),
753 BB->end());
754 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
755
756 // Next, add the true and fallthrough blocks as its successors.
757 BB->addSuccessor(copy0MBB);
758 BB->addSuccessor(sinkMBB);
759
760 // Emit the right instruction according to the type of the operands compared
761 if (isFPCmp)
762 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
763 else
764 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
765 .addReg(Mips::ZERO).addMBB(sinkMBB);
766
767 // copy0MBB:
768 // %FalseValue = ...
769 // # fallthrough to sinkMBB
770 BB = copy0MBB;
771
772 // Update machine-CFG edges
773 BB->addSuccessor(sinkMBB);
774
775 // sinkMBB:
776 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
777 // ...
778 BB = sinkMBB;
779
780 if (isFPCmp)
781 BuildMI(*BB, BB->begin(), dl,
782 TII->get(Mips::PHI), MI->getOperand(0).getReg())
783 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
784 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
785 else
786 BuildMI(*BB, BB->begin(), dl,
787 TII->get(Mips::PHI), MI->getOperand(0).getReg())
788 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
789 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
790
791 MI->eraseFromParent(); // The pseudo instruction is gone now.
792 return BB;
793}
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000794*/
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000795MachineBasicBlock *
796MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000797 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000798 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000799 default:
800 assert(false && "Unexpected instr type to insert");
801 return NULL;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000802 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000803 case Mips::ATOMIC_LOAD_ADD_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000804 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
805 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000806 case Mips::ATOMIC_LOAD_ADD_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000807 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
808 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000809 case Mips::ATOMIC_LOAD_ADD_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000810 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000811 case Mips::ATOMIC_LOAD_ADD_I64:
812 case Mips::ATOMIC_LOAD_ADD_I64_P8:
813 return EmitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000814
815 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000816 case Mips::ATOMIC_LOAD_AND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000817 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
818 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000819 case Mips::ATOMIC_LOAD_AND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000820 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
821 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000822 case Mips::ATOMIC_LOAD_AND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000823 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000824 case Mips::ATOMIC_LOAD_AND_I64:
825 case Mips::ATOMIC_LOAD_AND_I64_P8:
Akira Hatanaka73866122011-11-12 02:38:12 +0000826 return EmitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000827
828 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000829 case Mips::ATOMIC_LOAD_OR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000830 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
831 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000832 case Mips::ATOMIC_LOAD_OR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000833 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
834 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000835 case Mips::ATOMIC_LOAD_OR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000836 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000837 case Mips::ATOMIC_LOAD_OR_I64:
838 case Mips::ATOMIC_LOAD_OR_I64_P8:
839 return EmitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000840
841 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000842 case Mips::ATOMIC_LOAD_XOR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000843 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
844 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000845 case Mips::ATOMIC_LOAD_XOR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000846 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
847 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000848 case Mips::ATOMIC_LOAD_XOR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000849 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000850 case Mips::ATOMIC_LOAD_XOR_I64:
851 case Mips::ATOMIC_LOAD_XOR_I64_P8:
852 return EmitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000853
854 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000855 case Mips::ATOMIC_LOAD_NAND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000856 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
857 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000858 case Mips::ATOMIC_LOAD_NAND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000859 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
860 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000861 case Mips::ATOMIC_LOAD_NAND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000862 return EmitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000863 case Mips::ATOMIC_LOAD_NAND_I64:
864 case Mips::ATOMIC_LOAD_NAND_I64_P8:
865 return EmitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000866
867 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000868 case Mips::ATOMIC_LOAD_SUB_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000869 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
870 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000871 case Mips::ATOMIC_LOAD_SUB_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000872 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
873 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000874 case Mips::ATOMIC_LOAD_SUB_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000875 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000876 case Mips::ATOMIC_LOAD_SUB_I64:
877 case Mips::ATOMIC_LOAD_SUB_I64_P8:
878 return EmitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000879
880 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000881 case Mips::ATOMIC_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000882 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
883 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000884 case Mips::ATOMIC_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000885 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
886 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000887 case Mips::ATOMIC_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000888 return EmitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000889 case Mips::ATOMIC_SWAP_I64:
890 case Mips::ATOMIC_SWAP_I64_P8:
891 return EmitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000892
893 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000894 case Mips::ATOMIC_CMP_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000895 return EmitAtomicCmpSwapPartword(MI, BB, 1);
896 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000897 case Mips::ATOMIC_CMP_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000898 return EmitAtomicCmpSwapPartword(MI, BB, 2);
899 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000900 case Mips::ATOMIC_CMP_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000901 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000902 case Mips::ATOMIC_CMP_SWAP_I64:
903 case Mips::ATOMIC_CMP_SWAP_I64_P8:
904 return EmitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000905 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000906}
907
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000908// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
909// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
910MachineBasicBlock *
911MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000912 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000913 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000914 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000915
916 MachineFunction *MF = BB->getParent();
917 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000918 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000919 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
920 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000921 unsigned LL, SC, AND, NOR, ZERO, BEQ;
922
923 if (Size == 4) {
924 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
925 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
926 AND = Mips::AND;
927 NOR = Mips::NOR;
928 ZERO = Mips::ZERO;
929 BEQ = Mips::BEQ;
930 }
931 else {
932 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
933 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
934 AND = Mips::AND64;
935 NOR = Mips::NOR64;
936 ZERO = Mips::ZERO_64;
937 BEQ = Mips::BEQ64;
938 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000939
Akira Hatanaka4061da12011-07-19 20:11:17 +0000940 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000941 unsigned Ptr = MI->getOperand(1).getReg();
942 unsigned Incr = MI->getOperand(2).getReg();
943
Akira Hatanaka4061da12011-07-19 20:11:17 +0000944 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
945 unsigned AndRes = RegInfo.createVirtualRegister(RC);
946 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000947
948 // insert new blocks after the current block
949 const BasicBlock *LLVM_BB = BB->getBasicBlock();
950 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
951 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
952 MachineFunction::iterator It = BB;
953 ++It;
954 MF->insert(It, loopMBB);
955 MF->insert(It, exitMBB);
956
957 // Transfer the remainder of BB and its successor edges to exitMBB.
958 exitMBB->splice(exitMBB->begin(), BB,
959 llvm::next(MachineBasicBlock::iterator(MI)),
960 BB->end());
961 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
962
963 // thisMBB:
964 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000965 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000966 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000967 loopMBB->addSuccessor(loopMBB);
968 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000969
970 // loopMBB:
971 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000972 // <binop> storeval, oldval, incr
973 // sc success, storeval, 0(ptr)
974 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000975 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +0000976 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000977 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000978 // and andres, oldval, incr
979 // nor storeval, $0, andres
Akira Hatanaka59068062011-11-11 04:14:30 +0000980 BuildMI(BB, dl, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
981 BuildMI(BB, dl, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000982 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000983 // <binop> storeval, oldval, incr
984 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000985 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000986 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000987 }
Akira Hatanaka59068062011-11-11 04:14:30 +0000988 BuildMI(BB, dl, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
989 BuildMI(BB, dl, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000990
991 MI->eraseFromParent(); // The instruction is gone now.
992
Akira Hatanaka939ece12011-07-19 03:42:13 +0000993 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000994}
995
996MachineBasicBlock *
997MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000998 MachineBasicBlock *BB,
999 unsigned Size, unsigned BinOpcode,
1000 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001001 assert((Size == 1 || Size == 2) &&
1002 "Unsupported size for EmitAtomicBinaryPartial.");
1003
1004 MachineFunction *MF = BB->getParent();
1005 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1006 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1007 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1008 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001009 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1010 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001011
1012 unsigned Dest = MI->getOperand(0).getReg();
1013 unsigned Ptr = MI->getOperand(1).getReg();
1014 unsigned Incr = MI->getOperand(2).getReg();
1015
Akira Hatanaka4061da12011-07-19 20:11:17 +00001016 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1017 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001018 unsigned Mask = RegInfo.createVirtualRegister(RC);
1019 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001020 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1021 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001022 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001023 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1024 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1025 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1026 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1027 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001028 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001029 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1030 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1031 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1032 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1033 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001034
1035 // insert new blocks after the current block
1036 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1037 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001038 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001039 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1040 MachineFunction::iterator It = BB;
1041 ++It;
1042 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001043 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001044 MF->insert(It, exitMBB);
1045
1046 // Transfer the remainder of BB and its successor edges to exitMBB.
1047 exitMBB->splice(exitMBB->begin(), BB,
1048 llvm::next(MachineBasicBlock::iterator(MI)),
1049 BB->end());
1050 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
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001081
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001082 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001083 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001084 // ll oldval,0(alignedaddr)
1085 // binop binopres,oldval,incr2
1086 // and newval,binopres,mask
1087 // and maskedoldval0,oldval,mask2
1088 // or storeval,maskedoldval0,newval
1089 // sc success,storeval,0(alignedaddr)
1090 // beq success,$0,loopMBB
1091
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001092 // atomic.swap
1093 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001094 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001095 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001096 // and maskedoldval0,oldval,mask2
1097 // or storeval,maskedoldval0,newval
1098 // sc success,storeval,0(alignedaddr)
1099 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001100
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001101 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001102 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001103 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001104 // and andres, oldval, incr2
1105 // nor binopres, $0, andres
1106 // and newval, binopres, mask
1107 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1108 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1109 .addReg(Mips::ZERO).addReg(AndRes);
1110 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001111 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001112 // <binop> binopres, oldval, incr2
1113 // and newval, binopres, mask
1114 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1115 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001116 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001117 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001118 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001119 }
1120
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001121 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001122 .addReg(OldVal).addReg(Mask2);
1123 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001124 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001125 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001126 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001127 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001128 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001129
Akira Hatanaka939ece12011-07-19 03:42:13 +00001130 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001131 // and maskedoldval1,oldval,mask
1132 // srl srlres,maskedoldval1,shiftamt
1133 // sll sllres,srlres,24
1134 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001135 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001136 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001137
Akira Hatanaka4061da12011-07-19 20:11:17 +00001138 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1139 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001140 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1141 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001142 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1143 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001144 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001145 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001146
1147 MI->eraseFromParent(); // The instruction is gone now.
1148
Akira Hatanaka939ece12011-07-19 03:42:13 +00001149 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001150}
1151
1152MachineBasicBlock *
1153MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001154 MachineBasicBlock *BB,
1155 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001156 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001157
1158 MachineFunction *MF = BB->getParent();
1159 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001160 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001161 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1162 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001163 unsigned LL, SC, ZERO, BNE, BEQ;
1164
1165 if (Size == 4) {
1166 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1167 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1168 ZERO = Mips::ZERO;
1169 BNE = Mips::BNE;
1170 BEQ = Mips::BEQ;
1171 }
1172 else {
1173 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1174 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1175 ZERO = Mips::ZERO_64;
1176 BNE = Mips::BNE64;
1177 BEQ = Mips::BEQ64;
1178 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001179
1180 unsigned Dest = MI->getOperand(0).getReg();
1181 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001182 unsigned OldVal = MI->getOperand(2).getReg();
1183 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001184
Akira Hatanaka4061da12011-07-19 20:11:17 +00001185 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001186
1187 // insert new blocks after the current block
1188 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1189 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1190 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1191 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1192 MachineFunction::iterator It = BB;
1193 ++It;
1194 MF->insert(It, loop1MBB);
1195 MF->insert(It, loop2MBB);
1196 MF->insert(It, exitMBB);
1197
1198 // Transfer the remainder of BB and its successor edges to exitMBB.
1199 exitMBB->splice(exitMBB->begin(), BB,
1200 llvm::next(MachineBasicBlock::iterator(MI)),
1201 BB->end());
1202 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1203
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001204 // thisMBB:
1205 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001206 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001207 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001208 loop1MBB->addSuccessor(exitMBB);
1209 loop1MBB->addSuccessor(loop2MBB);
1210 loop2MBB->addSuccessor(loop1MBB);
1211 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001212
1213 // loop1MBB:
1214 // ll dest, 0(ptr)
1215 // bne dest, oldval, exitMBB
1216 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001217 BuildMI(BB, dl, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1218 BuildMI(BB, dl, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001219 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001220
1221 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001222 // sc success, newval, 0(ptr)
1223 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001224 BB = loop2MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001225 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001226 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka59068062011-11-11 04:14:30 +00001227 BuildMI(BB, dl, TII->get(BEQ))
1228 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001229
1230 MI->eraseFromParent(); // The instruction is gone now.
1231
Akira Hatanaka939ece12011-07-19 03:42:13 +00001232 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001233}
1234
1235MachineBasicBlock *
1236MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001237 MachineBasicBlock *BB,
1238 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001239 assert((Size == 1 || Size == 2) &&
1240 "Unsupported size for EmitAtomicCmpSwapPartial.");
1241
1242 MachineFunction *MF = BB->getParent();
1243 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1244 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1245 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1246 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001247 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1248 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001249
1250 unsigned Dest = MI->getOperand(0).getReg();
1251 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001252 unsigned CmpVal = MI->getOperand(2).getReg();
1253 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001254
Akira Hatanaka4061da12011-07-19 20:11:17 +00001255 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1256 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001257 unsigned Mask = RegInfo.createVirtualRegister(RC);
1258 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001259 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1260 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1261 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1262 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1263 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1264 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1265 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1266 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1267 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1268 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1269 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1270 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1271 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1272 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001273
1274 // insert new blocks after the current block
1275 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1276 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1277 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001278 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001279 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1280 MachineFunction::iterator It = BB;
1281 ++It;
1282 MF->insert(It, loop1MBB);
1283 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001284 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001285 MF->insert(It, exitMBB);
1286
1287 // Transfer the remainder of BB and its successor edges to exitMBB.
1288 exitMBB->splice(exitMBB->begin(), BB,
1289 llvm::next(MachineBasicBlock::iterator(MI)),
1290 BB->end());
1291 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1292
Akira Hatanaka81b44112011-07-19 17:09:53 +00001293 BB->addSuccessor(loop1MBB);
1294 loop1MBB->addSuccessor(sinkMBB);
1295 loop1MBB->addSuccessor(loop2MBB);
1296 loop2MBB->addSuccessor(loop1MBB);
1297 loop2MBB->addSuccessor(sinkMBB);
1298 sinkMBB->addSuccessor(exitMBB);
1299
Akira Hatanaka70564a92011-07-19 18:14:26 +00001300 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001301 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001302 // addiu masklsb2,$0,-4 # 0xfffffffc
1303 // and alignedaddr,ptr,masklsb2
1304 // andi ptrlsb2,ptr,3
1305 // sll shiftamt,ptrlsb2,3
1306 // ori maskupper,$0,255 # 0xff
1307 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001308 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001309 // andi maskedcmpval,cmpval,255
1310 // sll shiftedcmpval,maskedcmpval,shiftamt
1311 // andi maskednewval,newval,255
1312 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001313 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001314 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1315 .addReg(Mips::ZERO).addImm(-4);
1316 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1317 .addReg(Ptr).addReg(MaskLSB2);
1318 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1319 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1320 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1321 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001322 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1323 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001324 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001325 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1326 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001327 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1328 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001329 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1330 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001331 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1332 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001333
1334 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001335 // ll oldval,0(alginedaddr)
1336 // and maskedoldval0,oldval,mask
1337 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001338 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001339 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001340 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1341 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001342 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001343 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001344
1345 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001346 // and maskedoldval1,oldval,mask2
1347 // or storeval,maskedoldval1,shiftednewval
1348 // sc success,storeval,0(alignedaddr)
1349 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001350 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001351 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1352 .addReg(OldVal).addReg(Mask2);
1353 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1354 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001355 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001356 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001357 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001358 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001359
Akira Hatanaka939ece12011-07-19 03:42:13 +00001360 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001361 // srl srlres,maskedoldval0,shiftamt
1362 // sll sllres,srlres,24
1363 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001364 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001365 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001366
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001367 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1368 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001369 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1370 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001371 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001372 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001373
1374 MI->eraseFromParent(); // The instruction is gone now.
1375
Akira Hatanaka939ece12011-07-19 03:42:13 +00001376 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001377}
1378
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001379//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001380// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001381//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001382SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001383LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001384{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001385 MachineFunction &MF = DAG.getMachineFunction();
1386 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001387 unsigned SP = IsN64 ? Mips::SP_64 : Mips::SP;
Akira Hatanaka21afc632011-06-21 00:40:49 +00001388
1389 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001390 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1391 "Cannot lower if the alignment of the allocated space is larger than \
1392 that of the stack.");
1393
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001394 SDValue Chain = Op.getOperand(0);
1395 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001396 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001397
1398 // Get a reference from Mips stack pointer
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001399 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, SP, getPointerTy());
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001400
1401 // Subtract the dynamic size from the actual stack size to
1402 // obtain the new stack size.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001403 SDValue Sub = DAG.getNode(ISD::SUB, dl, getPointerTy(), StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001404
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001405 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001406 // must be placed in the stack pointer register.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001407 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, SP, Sub, SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001408
1409 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001410 // value and a chain
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001411 SDVTList VTLs = DAG.getVTList(getPointerTy(), MVT::Other);
Akira Hatanaka21afc632011-06-21 00:40:49 +00001412 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1413 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1414
1415 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001416}
1417
1418SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001419LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001420{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001421 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001422 // the block to branch to if the condition is true.
1423 SDValue Chain = Op.getOperand(0);
1424 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001425 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001426
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001427 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1428
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001429 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001430 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001431 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001432
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001433 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001434 Mips::CondCode CC =
1435 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001436 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001437
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001438 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001439 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001440}
1441
1442SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001443LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001444{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001445 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001446
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001447 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001448 if (Cond.getOpcode() != MipsISD::FPCmp)
1449 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001450
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001451 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1452 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001453}
1454
Dan Gohmand858e902010-04-17 15:26:15 +00001455SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1456 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001457 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001458 DebugLoc dl = Op.getDebugLoc();
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001459 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001460
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001461 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001462 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001463
Chris Lattnerb71b9092009-08-13 06:28:06 +00001464 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001465
Chris Lattnere3736f82009-08-13 05:41:27 +00001466 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001467 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1468 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001469 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001470 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1471 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001472 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001473 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001474 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001475 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1476 MipsII::MO_ABS_HI);
1477 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1478 MipsII::MO_ABS_LO);
1479 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1480 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001481 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001482 }
1483
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001484 EVT ValTy = Op.getValueType();
1485 bool HasGotOfst = (GV->hasInternalLinkage() ||
1486 (GV->hasLocalLinkage() && !isa<Function>(GV)));
1487 unsigned GotFlag = IsN64 ?
1488 (HasGotOfst ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT_DISP) :
Bruno Cardoso Lopese3d35722011-12-07 00:28:57 +00001489 (HasGotOfst ? MipsII::MO_GOT : MipsII::MO_GOT16);
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001490 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0, GotFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001491 GA = DAG.getNode(MipsISD::Wrapper, dl, ValTy, GA);
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001492 SDValue ResNode = DAG.getLoad(ValTy, dl,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001493 DAG.getEntryNode(), GA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001494 false, false, false, 0);
Akira Hatanaka0f843822011-06-07 18:58:42 +00001495 // On functions and global targets not internal linked only
1496 // a load from got/GP is necessary for PIC to work.
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001497 if (!HasGotOfst)
Akira Hatanaka0f843822011-06-07 18:58:42 +00001498 return ResNode;
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001499 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0,
1500 IsN64 ? MipsII::MO_GOT_OFST :
1501 MipsII::MO_ABS_LO);
1502 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, GALo);
1503 return DAG.getNode(ISD::ADD, dl, ValTy, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001504}
1505
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001506SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1507 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001508 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1509 // FIXME there isn't actually debug info here
1510 DebugLoc dl = Op.getDebugLoc();
1511
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001512 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001513 // %hi/%lo relocation
1514 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
1515 MipsII::MO_ABS_HI);
1516 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
1517 MipsII::MO_ABS_LO);
1518 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1519 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1520 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001521 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001522
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001523 EVT ValTy = Op.getValueType();
1524 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1525 unsigned OFSTFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1526 SDValue BAGOTOffset = DAG.getBlockAddress(BA, ValTy, true, GOTFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001527 BAGOTOffset = DAG.getNode(MipsISD::Wrapper, dl, ValTy, BAGOTOffset);
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001528 SDValue BALOOffset = DAG.getBlockAddress(BA, ValTy, true, OFSTFlag);
1529 SDValue Load = DAG.getLoad(ValTy, dl,
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001530 DAG.getEntryNode(), BAGOTOffset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001531 MachinePointerInfo(), false, false, false, 0);
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001532 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, BALOOffset);
1533 return DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001534}
1535
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001536SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001537LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001538{
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001539 // If the relocation model is PIC, use the General Dynamic TLS Model,
1540 // otherwise use the Initial Exec or Local Exec TLS Model.
1541 // TODO: implement Local Dynamic TLS model
1542
1543 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1544 DebugLoc dl = GA->getDebugLoc();
1545 const GlobalValue *GV = GA->getGlobal();
1546 EVT PtrVT = getPointerTy();
1547
1548 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1549 // General Dynamic TLS Model
Akira Hatanakaca074792011-12-08 20:34:32 +00001550 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001551 0, MipsII::MO_TLSGD);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001552 SDValue Argument = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, TGA);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001553 unsigned PtrSize = PtrVT.getSizeInBits();
1554 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1555
1556 SmallVector<Type*, 1> Params;
1557 Params.push_back(PtrTy);
1558 FunctionType *FuncTy = FunctionType::get(PtrTy, Params, false);
1559 Function *Func = Function::Create(FuncTy, GlobalValue::ExternalLinkage,
1560 "__tls_get_addr");
1561 SDValue TlsGetAddr = DAG.getGlobalAddress(Func, dl, PtrVT);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001562
1563 ArgListTy Args;
1564 ArgListEntry Entry;
1565 Entry.Node = Argument;
Akira Hatanakaca074792011-12-08 20:34:32 +00001566 Entry.Ty = PtrTy;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001567 Args.push_back(Entry);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001568
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001569 std::pair<SDValue, SDValue> CallResult =
Akira Hatanakaca074792011-12-08 20:34:32 +00001570 LowerCallTo(DAG.getEntryNode(), PtrTy,
1571 false, false, false, false, 0, CallingConv::C, false, true,
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001572 TlsGetAddr, Args, DAG, dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001573
1574 return CallResult.first;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001575 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001576
1577 SDValue Offset;
1578 if (GV->isDeclaration()) {
1579 // Initial Exec TLS Model
Akira Hatanakaca074792011-12-08 20:34:32 +00001580 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001581 MipsII::MO_GOTTPREL);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001582 TGA = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, TGA);
Akira Hatanakaca074792011-12-08 20:34:32 +00001583 Offset = DAG.getLoad(PtrVT, dl,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001584 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001585 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001586 } else {
1587 // Local Exec TLS Model
Akira Hatanakaca074792011-12-08 20:34:32 +00001588 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001589 MipsII::MO_TPREL_HI);
Akira Hatanakaca074792011-12-08 20:34:32 +00001590 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001591 MipsII::MO_TPREL_LO);
Akira Hatanakaca074792011-12-08 20:34:32 +00001592 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, PtrVT, TGAHi);
1593 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, TGALo);
1594 Offset = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001595 }
1596
1597 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1598 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001599}
1600
1601SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001602LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001603{
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001604 SDValue HiPart, JTI, JTILo;
Dale Johannesende064702009-02-06 21:50:26 +00001605 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001606 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001607 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Owen Andersone50ed302009-08-10 22:56:29 +00001608 EVT PtrVT = Op.getValueType();
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001609 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001610
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001611 if (!IsPIC && !IsN64) {
1612 JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MipsII::MO_ABS_HI);
1613 HiPart = DAG.getNode(MipsISD::Hi, dl, PtrVT, JTI);
1614 JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MipsII::MO_ABS_LO);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001615 } else {// Emit Load from Global Pointer
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001616 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1617 unsigned OfstFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1618 JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, GOTFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001619 JTI = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, JTI);
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001620 HiPart = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), JTI,
1621 MachinePointerInfo(), false, false, false, 0);
1622 JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OfstFlag);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001623 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001624
Akira Hatanaka2bf08ec2011-12-05 21:03:03 +00001625 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, JTILo);
1626 return DAG.getNode(ISD::ADD, dl, PtrVT, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001627}
1628
Dan Gohman475871a2008-07-27 21:46:04 +00001629SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001630LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001631{
Dan Gohman475871a2008-07-27 21:46:04 +00001632 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001633 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001634 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001635 // FIXME there isn't actually debug info here
1636 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001637
1638 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001639 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001640 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001641 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001642 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001643 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001644 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1645 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001646 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001647
1648 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001649 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001650 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001651 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001652 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001653 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1654 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001655 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001656 } else {
Akira Hatanaka620db892011-11-16 22:44:38 +00001657 EVT ValTy = Op.getValueType();
1658 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1659 unsigned OFSTFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1660 SDValue CP = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1661 N->getOffset(), GOTFlag);
Akira Hatanaka6df7e232011-12-09 01:53:17 +00001662 CP = DAG.getNode(MipsISD::Wrapper, dl, ValTy, CP);
Akira Hatanaka620db892011-11-16 22:44:38 +00001663 SDValue Load = DAG.getLoad(ValTy, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001664 CP, MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001665 false, false, false, 0);
Akira Hatanaka620db892011-11-16 22:44:38 +00001666 SDValue CPLo = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1667 N->getOffset(), OFSTFlag);
1668 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, CPLo);
1669 ResNode = DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001670 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001671
1672 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001673}
1674
Dan Gohmand858e902010-04-17 15:26:15 +00001675SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001676 MachineFunction &MF = DAG.getMachineFunction();
1677 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1678
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001679 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001680 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1681 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001682
1683 // vastart just stores the address of the VarArgsFrameIndex slot into the
1684 // memory location argument.
1685 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001686 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1687 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +00001688 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
1741SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1742 const {
1743 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 Hatanaka9c3d57c2011-05-25 19:32:07 +00001749 else
Akira Hatanaka7398bf02011-12-07 21:48:50 +00001750 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.
1769SDValue MipsTargetLowering::LowerMEMBARRIER(SDValue Op,
1770 SelectionDAG& DAG) const {
1771 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,
1998 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001999 false, false, false, std::min(ByValAlign,
2000 (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002001 MemOpChains.push_back(LoadVal.getValue(1));
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002002 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002003 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
2004 }
2005
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002006 if (RemainingSize == 0)
2007 return;
2008
2009 // If there still is a register available for argument passing, write the
2010 // remaining part of the structure to it using subword loads and shifts.
2011 if (LocMemOffset < 4 * 4) {
2012 assert(RemainingSize <= 3 && RemainingSize >= 1 &&
2013 "There must be one to three bytes remaining.");
2014 unsigned LoadSize = (RemainingSize == 3 ? 2 : RemainingSize);
2015 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2016 DAG.getConstant(Offset, MVT::i32));
2017 unsigned Alignment = std::min(ByValAlign, (unsigned )4);
2018 SDValue LoadVal = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2019 LoadPtr, MachinePointerInfo(),
2020 MVT::getIntegerVT(LoadSize * 8), false,
2021 false, Alignment);
2022 MemOpChains.push_back(LoadVal.getValue(1));
2023
2024 // If target is big endian, shift it to the most significant half-word or
2025 // byte.
2026 if (!isLittle)
2027 LoadVal = DAG.getNode(ISD::SHL, dl, MVT::i32, LoadVal,
2028 DAG.getConstant(32 - LoadSize * 8, MVT::i32));
2029
2030 Offset += LoadSize;
2031 RemainingSize -= LoadSize;
2032
2033 // Read second subword if necessary.
2034 if (RemainingSize != 0) {
2035 assert(RemainingSize == 1 && "There must be one byte remaining.");
2036 LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2037 DAG.getConstant(Offset, MVT::i32));
2038 unsigned Alignment = std::min(ByValAlign, (unsigned )2);
2039 SDValue Subword = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2040 LoadPtr, MachinePointerInfo(),
2041 MVT::i8, false, false, Alignment);
2042 MemOpChains.push_back(Subword.getValue(1));
2043 // Insert the loaded byte to LoadVal.
2044 // FIXME: Use INS if supported by target.
2045 unsigned ShiftAmt = isLittle ? 16 : 8;
2046 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i32, Subword,
2047 DAG.getConstant(ShiftAmt, MVT::i32));
2048 LoadVal = DAG.getNode(ISD::OR, dl, MVT::i32, LoadVal, Shift);
2049 }
2050
2051 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
2052 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
2053 return;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002054 }
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002055
2056 // Create a fixed object on stack at offset LocMemOffset and copy
2057 // remaining part of byval arg to it using memcpy.
2058 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2059 DAG.getConstant(Offset, MVT::i32));
2060 LastFI = MFI->CreateFixedObject(RemainingSize, LocMemOffset, true);
2061 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002062 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2063 DAG.getConstant(RemainingSize, MVT::i32),
2064 std::min(ByValAlign, (unsigned)4),
2065 /*isVolatile=*/false, /*AlwaysInline=*/false,
2066 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002067}
2068
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002069// Copy Mips64 byVal arg to registers and stack.
2070void static
2071PassByValArg64(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
2072 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
2073 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
2074 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
2075 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2076 EVT PtrTy, bool isLittle) {
2077 unsigned ByValSize = Flags.getByValSize();
2078 unsigned Alignment = std::min(Flags.getByValAlign(), (unsigned)8);
2079 bool IsRegLoc = VA.isRegLoc();
2080 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
2081 unsigned LocMemOffset = 0;
Akira Hatanaka16040852011-11-15 18:42:25 +00002082 unsigned MemCpySize = ByValSize;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002083
2084 if (!IsRegLoc)
2085 LocMemOffset = VA.getLocMemOffset();
2086 else {
2087 const unsigned *Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8,
2088 VA.getLocReg());
2089 const unsigned *RegEnd = Mips64IntRegs + 8;
2090
2091 // Copy double words to registers.
2092 for (; (Reg != RegEnd) && (ByValSize >= Offset + 8); ++Reg, Offset += 8) {
2093 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2094 DAG.getConstant(Offset, PtrTy));
2095 SDValue LoadVal = DAG.getLoad(MVT::i64, dl, Chain, LoadPtr,
2096 MachinePointerInfo(), false, false, false,
2097 Alignment);
2098 MemOpChains.push_back(LoadVal.getValue(1));
2099 RegsToPass.push_back(std::make_pair(*Reg, LoadVal));
2100 }
2101
Akira Hatanaka16040852011-11-15 18:42:25 +00002102 // Return if the struct has been fully copied.
2103 if (!(MemCpySize = ByValSize - Offset))
2104 return;
2105
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002106 // If there is an argument register available, copy the remainder of the
2107 // byval argument with sub-doubleword loads and shifts.
Akira Hatanaka16040852011-11-15 18:42:25 +00002108 if (Reg != RegEnd) {
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002109 assert((ByValSize < Offset + 8) &&
2110 "Size of the remainder should be smaller than 8-byte.");
2111 SDValue Val;
2112 for (unsigned LoadSize = 4; Offset < ByValSize; LoadSize /= 2) {
2113 unsigned RemSize = ByValSize - Offset;
2114
2115 if (RemSize < LoadSize)
2116 continue;
2117
2118 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2119 DAG.getConstant(Offset, PtrTy));
2120 SDValue LoadVal =
2121 DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i64, Chain, LoadPtr,
2122 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
2123 false, false, Alignment);
2124 MemOpChains.push_back(LoadVal.getValue(1));
2125
2126 // Offset in number of bits from double word boundary.
2127 unsigned OffsetDW = (Offset % 8) * 8;
2128 unsigned Shamt = isLittle ? OffsetDW : 64 - (OffsetDW + LoadSize * 8);
2129 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i64, LoadVal,
2130 DAG.getConstant(Shamt, MVT::i32));
2131
2132 Val = Val.getNode() ? DAG.getNode(ISD::OR, dl, MVT::i64, Val, Shift) :
2133 Shift;
2134 Offset += LoadSize;
2135 Alignment = std::min(Alignment, LoadSize);
2136 }
2137
2138 RegsToPass.push_back(std::make_pair(*Reg, Val));
2139 return;
2140 }
2141 }
2142
Akira Hatanaka16040852011-11-15 18:42:25 +00002143 assert(MemCpySize && "MemCpySize must not be zero.");
2144
2145 // Create a fixed object on stack at offset LocMemOffset and copy
2146 // remainder of byval arg to it with memcpy.
2147 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2148 DAG.getConstant(Offset, PtrTy));
2149 LastFI = MFI->CreateFixedObject(MemCpySize, LocMemOffset, true);
2150 SDValue Dst = DAG.getFrameIndex(LastFI, PtrTy);
2151 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2152 DAG.getConstant(MemCpySize, PtrTy), Alignment,
2153 /*isVolatile=*/false, /*AlwaysInline=*/false,
2154 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002155}
2156
Dan Gohman98ca4f22009-08-05 01:29:28 +00002157/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002158/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002159/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002160SDValue
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002161MipsTargetLowering::LowerCall(SDValue InChain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002162 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002163 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002164 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002165 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002166 const SmallVectorImpl<ISD::InputArg> &Ins,
2167 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002168 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002169 // MIPs target does not yet support tail call optimization.
2170 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002171
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002172 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002173 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002174 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002175 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00002176 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002177
2178 // Analyze operands of the call, assigning locations to each operand.
2179 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002180 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2181 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002182
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002183 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002184 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanaka49617092011-11-14 19:02:54 +00002185 else if (HasMips64)
2186 AnalyzeMips64CallOperands(CCInfo, Outs);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00002187 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002188 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002189
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002190 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002191 unsigned NextStackOffset = CCInfo.getNextStackOffset();
2192
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002193 // Chain is the output chain of the last Load/Store or CopyToReg node.
2194 // ByValChain is the output chain of the last Memcpy node created for copying
2195 // byval arguments to the stack.
2196 SDValue Chain, CallSeqStart, ByValChain;
2197 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
2198 Chain = CallSeqStart = DAG.getCALLSEQ_START(InChain, NextStackOffsetVal);
2199 ByValChain = InChain;
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002200
2201 // If this is the first call, create a stack frame object that points to
2202 // a location to which .cprestore saves $gp.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002203 if (IsO32 && IsPIC && !MipsFI->getGPFI())
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002204 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
2205
Akira Hatanaka21afc632011-06-21 00:40:49 +00002206 // Get the frame index of the stack frame object that points to the location
2207 // of dynamically allocated area on the stack.
2208 int DynAllocFI = MipsFI->getDynAllocFI();
2209
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002210 // Update size of the maximum argument space.
2211 // For O32, a minimum of four words (16 bytes) of argument space is
2212 // allocated.
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002213 if (IsO32)
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002214 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
2215
2216 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
2217
2218 if (MaxCallFrameSize < NextStackOffset) {
2219 MipsFI->setMaxCallFrameSize(NextStackOffset);
2220
Akira Hatanaka21afc632011-06-21 00:40:49 +00002221 // Set the offsets relative to $sp of the $gp restore slot and dynamically
2222 // allocated stack space. These offsets must be aligned to a boundary
2223 // determined by the stack alignment of the ABI.
2224 unsigned StackAlignment = TFL->getStackAlignment();
2225 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
2226 StackAlignment * StackAlignment;
2227
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002228 if (MipsFI->needGPSaveRestore())
Akira Hatanaka21afc632011-06-21 00:40:49 +00002229 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
2230
2231 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002232 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002233
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002234 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002235 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
2236 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002237
Eric Christopher471e4222011-06-08 23:55:35 +00002238 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00002239
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002240 // Walk the register/memloc assignments, inserting copies/loads.
2241 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002242 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002243 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002244 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002245 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2246
2247 // ByVal Arg.
2248 if (Flags.isByVal()) {
2249 assert(Flags.getByValSize() &&
2250 "ByVal args of size 0 should have been ignored by front-end.");
2251 if (IsO32)
2252 WriteByValArg(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2253 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2254 Subtarget->isLittle());
2255 else
2256 PassByValArg64(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2257 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2258 Subtarget->isLittle());
2259 continue;
2260 }
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002261
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002262 // Promote the value if needed.
2263 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002264 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002265 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002266 if (VA.isRegLoc()) {
2267 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
2268 (ValVT == MVT::f64 && LocVT == MVT::i64))
2269 Arg = DAG.getNode(ISD::BITCAST, dl, LocVT, Arg);
2270 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002271 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2272 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002273 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2274 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002275 if (!Subtarget->isLittle())
2276 std::swap(Lo, Hi);
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002277 unsigned LocRegLo = VA.getLocReg();
2278 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2279 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2280 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002281 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002282 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002283 }
2284 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002285 case CCValAssign::SExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002286 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002287 break;
2288 case CCValAssign::ZExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002289 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002290 break;
2291 case CCValAssign::AExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002292 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002293 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002294 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002295
2296 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002297 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002298 if (VA.isRegLoc()) {
2299 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002300 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002301 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002302
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002303 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002304 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002305
Chris Lattnere0b12152008-03-17 06:57:02 +00002306 // Create the frame index object for this incoming parameter
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002307 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002308 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00002309 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00002310
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002311 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002312 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00002313 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
2314 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00002315 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002316 }
2317
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002318 // Extend range of indices of frame objects for outgoing arguments that were
2319 // created during this function call. Skip this step if no such objects were
2320 // created.
2321 if (LastFI)
2322 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2323
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002324 // If a memcpy has been created to copy a byval arg to a stack, replace the
2325 // chain input of CallSeqStart with ByValChain.
2326 if (InChain != ByValChain)
2327 DAG.UpdateNodeOperands(CallSeqStart.getNode(), ByValChain,
2328 NextStackOffsetVal);
2329
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002330 // Transform all store nodes into one single node because all store
2331 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002332 if (!MemOpChains.empty())
2333 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002334 &MemOpChains[0], MemOpChains.size());
2335
Bill Wendling056292f2008-09-16 21:48:12 +00002336 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002337 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2338 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002339 unsigned char OpFlag;
2340 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002341 bool GlobalOrExternal = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002342 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002343
2344 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002345 if (IsPICCall && G->getGlobal()->hasInternalLinkage()) {
2346 OpFlag = IsO32 ? MipsII::MO_GOT : MipsII::MO_GOT_PAGE;
2347 unsigned char LoFlag = IsO32 ? MipsII::MO_ABS_LO : MipsII::MO_GOT_OFST;
2348 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(), 0,
2349 OpFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002350 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002351 0, LoFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002352 } else {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002353 OpFlag = IsPICCall ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002354 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2355 getPointerTy(), 0, OpFlag);
2356 }
2357
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002358 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002359 }
2360 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002361 if (IsN64 || (!IsO32 && IsPIC))
2362 OpFlag = MipsII::MO_GOT_DISP;
2363 else if (!IsPIC) // !N64 && static
2364 OpFlag = MipsII::MO_NO_FLAG;
2365 else // O32 & PIC
2366 OpFlag = MipsII::MO_GOT_CALL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002367 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002368 getPointerTy(), OpFlag);
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002369 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002370 }
2371
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002372 SDValue InFlag;
2373
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002374 // Create nodes that load address of callee and copy it to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002375 if (IsPICCall) {
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002376 if (GlobalOrExternal) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002377 // Load callee address
Akira Hatanaka6df7e232011-12-09 01:53:17 +00002378 Callee = DAG.getNode(MipsISD::Wrapper, dl, getPointerTy(), Callee);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002379 SDValue LoadValue = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
2380 Callee, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002381 false, false, false, 0);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002382
2383 // Use GOT+LO if callee has internal linkage.
2384 if (CalleeLo.getNode()) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002385 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, getPointerTy(), CalleeLo);
2386 Callee = DAG.getNode(ISD::ADD, dl, getPointerTy(), LoadValue, Lo);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002387 } else
2388 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002389 }
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002390 }
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002391
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002392 // T9 should contain the address of the callee function if
2393 // -reloction-model=pic or it is an indirect call.
2394 if (IsPICCall || !GlobalOrExternal) {
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002395 // copy to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002396 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
2397 Chain = DAG.getCopyToReg(Chain, dl, T9Reg, Callee, SDValue(0, 0));
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002398 InFlag = Chain.getValue(1);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002399 Callee = DAG.getRegister(T9Reg, getPointerTy());
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002400 }
Bill Wendling056292f2008-09-16 21:48:12 +00002401
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002402 // Build a sequence of copy-to-reg nodes chained together with token
2403 // chain and flag operands which copy the outgoing args into registers.
2404 // The InFlag in necessary since all emitted instructions must be
2405 // stuck together.
2406 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2407 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2408 RegsToPass[i].second, InFlag);
2409 InFlag = Chain.getValue(1);
2410 }
2411
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002412 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002413 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002414 //
2415 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002416 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002417 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002418 Ops.push_back(Chain);
2419 Ops.push_back(Callee);
2420
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002421 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002422 // known live into the call.
2423 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2424 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2425 RegsToPass[i].second.getValueType()));
2426
Gabor Greifba36cb52008-08-28 21:40:38 +00002427 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002428 Ops.push_back(InFlag);
2429
Dale Johannesen33c960f2009-02-04 20:06:27 +00002430 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002431 InFlag = Chain.getValue(1);
2432
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002433 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002434 Chain = DAG.getCALLSEQ_END(Chain,
2435 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002436 DAG.getIntPtrConstant(0, true), InFlag);
2437 InFlag = Chain.getValue(1);
2438
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002439 // Handle result values, copying them out of physregs into vregs that we
2440 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002441 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2442 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002443}
2444
Dan Gohman98ca4f22009-08-05 01:29:28 +00002445/// LowerCallResult - Lower the result values of a call into the
2446/// appropriate copies out of appropriate physical registers.
2447SDValue
2448MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002449 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002450 const SmallVectorImpl<ISD::InputArg> &Ins,
2451 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002452 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002453 // Assign locations to each value returned by this call.
2454 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002455 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2456 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002457
Dan Gohman98ca4f22009-08-05 01:29:28 +00002458 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002459
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002460 // Copy all of the result registers out of their specified physreg.
2461 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002462 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002463 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002464 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002465 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002466 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002467
Dan Gohman98ca4f22009-08-05 01:29:28 +00002468 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002469}
2470
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002471//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002472// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002473//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002474static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2475 std::vector<SDValue>& OutChains,
2476 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2477 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2478 unsigned LocMem = VA.getLocMemOffset();
2479 unsigned FirstWord = LocMem / 4;
2480
2481 // copy register A0 - A3 to frame object
2482 for (unsigned i = 0; i < NumWords; ++i) {
2483 unsigned CurWord = FirstWord + i;
2484 if (CurWord >= O32IntRegsSize)
2485 break;
2486
2487 unsigned SrcReg = O32IntRegs[CurWord];
2488 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2489 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2490 DAG.getConstant(i * 4, MVT::i32));
2491 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2492 StorePtr, MachinePointerInfo(), false,
2493 false, 0);
2494 OutChains.push_back(Store);
2495 }
2496}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002497
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002498// Create frame object on stack and copy registers used for byval passing to it.
2499static unsigned
2500CopyMips64ByValRegs(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2501 std::vector<SDValue>& OutChains, SelectionDAG &DAG,
2502 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2503 MachineFrameInfo *MFI, bool IsRegLoc,
2504 SmallVectorImpl<SDValue> &InVals, MipsFunctionInfo *MipsFI,
2505 EVT PtrTy) {
2506 const unsigned *Reg = Mips64IntRegs + 8;
2507 int FOOffset; // Frame object offset from virtual frame pointer.
2508
2509 if (IsRegLoc) {
2510 Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8, VA.getLocReg());
2511 FOOffset = (Reg - Mips64IntRegs) * 8 - 8 * 8;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002512 }
2513 else
2514 FOOffset = VA.getLocMemOffset();
2515
2516 // Create frame object.
2517 unsigned NumRegs = (Flags.getByValSize() + 7) / 8;
2518 unsigned LastFI = MFI->CreateFixedObject(NumRegs * 8, FOOffset, true);
2519 SDValue FIN = DAG.getFrameIndex(LastFI, PtrTy);
2520 InVals.push_back(FIN);
2521
2522 // Copy arg registers.
2523 for (unsigned I = 0; (Reg != Mips64IntRegs + 8) && (I < NumRegs);
2524 ++Reg, ++I) {
2525 unsigned VReg = AddLiveIn(MF, *Reg, Mips::CPU64RegsRegisterClass);
2526 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, PtrTy, FIN,
2527 DAG.getConstant(I * 8, PtrTy));
2528 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(VReg, MVT::i64),
2529 StorePtr, MachinePointerInfo(), false,
2530 false, 0);
2531 OutChains.push_back(Store);
2532 }
2533
2534 return LastFI;
2535}
2536
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002537/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002538/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002539SDValue
2540MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002541 CallingConv::ID CallConv,
2542 bool isVarArg,
2543 const SmallVectorImpl<ISD::InputArg>
2544 &Ins,
2545 DebugLoc dl, SelectionDAG &DAG,
2546 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002547 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002548 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002549 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002550 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002551
Dan Gohman1e93df62010-04-17 14:41:14 +00002552 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002553
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002554 // Used with vargs to acumulate store chains.
2555 std::vector<SDValue> OutChains;
2556
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002557 // Assign locations to all of the incoming arguments.
2558 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002559 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2560 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002561
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002562 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002563 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002564 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002565 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002566
Akira Hatanaka43299772011-05-20 23:22:14 +00002567 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002568
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002569 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002570 CCValAssign &VA = ArgLocs[i];
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002571 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002572 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2573 bool IsRegLoc = VA.isRegLoc();
2574
2575 if (Flags.isByVal()) {
2576 assert(Flags.getByValSize() &&
2577 "ByVal args of size 0 should have been ignored by front-end.");
2578 if (IsO32) {
2579 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2580 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2581 true);
2582 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2583 InVals.push_back(FIN);
2584 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2585 } else // N32/64
2586 LastFI = CopyMips64ByValRegs(MF, Chain, dl, OutChains, DAG, VA, Flags,
2587 MFI, IsRegLoc, InVals, MipsFI,
2588 getPointerTy());
2589 continue;
2590 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002591
2592 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002593 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002594 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002595 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002596 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002597
Owen Anderson825b72b2009-08-11 20:47:22 +00002598 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002599 RC = Mips::CPURegsRegisterClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002600 else if (RegVT == MVT::i64)
2601 RC = Mips::CPU64RegsRegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002602 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002603 RC = Mips::FGR32RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002604 else if (RegVT == MVT::f64)
Akira Hatanakaf40de9d2011-09-26 21:55:17 +00002605 RC = HasMips64 ? Mips::FGR64RegisterClass : Mips::AFGR64RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002606 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002607 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002608
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002609 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002610 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002611 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002612 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002613
2614 // If this is an 8 or 16-bit value, it has been passed promoted
2615 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002616 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002617 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002618 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002619 if (VA.getLocInfo() == CCValAssign::SExt)
2620 Opcode = ISD::AssertSext;
2621 else if (VA.getLocInfo() == CCValAssign::ZExt)
2622 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002623 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002624 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002625 DAG.getValueType(ValVT));
2626 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002627 }
2628
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002629 // Handle floating point arguments passed in integer registers.
2630 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
2631 (RegVT == MVT::i64 && ValVT == MVT::f64))
2632 ArgValue = DAG.getNode(ISD::BITCAST, dl, ValVT, ArgValue);
2633 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
2634 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
2635 getNextIntArgReg(ArgReg), RC);
2636 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
2637 if (!Subtarget->isLittle())
2638 std::swap(ArgValue, ArgValue2);
2639 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2640 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002641 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002642
Dan Gohman98ca4f22009-08-05 01:29:28 +00002643 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002644 } else { // VA.isRegLoc()
2645
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002646 // sanity check
2647 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002648
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002649 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002650 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002651 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002652
2653 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002654 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002655 InVals.push_back(DAG.getLoad(ValVT, dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002656 MachinePointerInfo::getFixedStack(LastFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002657 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002658 }
2659 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002660
2661 // The mips ABIs for returning structs by value requires that we copy
2662 // the sret argument into $v0 for the return. Save the argument into
2663 // a virtual register so that we can access it from the return points.
2664 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2665 unsigned Reg = MipsFI->getSRetReturnReg();
2666 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002667 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002668 MipsFI->setSRetReturnReg(Reg);
2669 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002670 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002671 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002672 }
2673
Akira Hatanakabad53f42011-11-14 19:01:09 +00002674 if (isVarArg) {
2675 unsigned NumOfRegs = IsO32 ? 4 : 8;
2676 const unsigned *ArgRegs = IsO32 ? O32IntRegs : Mips64IntRegs;
2677 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumOfRegs);
2678 int FirstRegSlotOffset = IsO32 ? 0 : -64 ; // offset of $a0's slot.
2679 TargetRegisterClass *RC
2680 = IsO32 ? Mips::CPURegsRegisterClass : Mips::CPU64RegsRegisterClass;
2681 unsigned RegSize = RC->getSize();
2682 int RegSlotOffset = FirstRegSlotOffset + Idx * RegSize;
2683
2684 // Offset of the first variable argument from stack pointer.
2685 int FirstVaArgOffset;
2686
2687 if (IsO32 || (Idx == NumOfRegs)) {
2688 FirstVaArgOffset =
2689 (CCInfo.getNextStackOffset() + RegSize - 1) / RegSize * RegSize;
2690 } else
2691 FirstVaArgOffset = RegSlotOffset;
2692
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002693 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002694 // which is a value necessary to VASTART.
Akira Hatanakabad53f42011-11-14 19:01:09 +00002695 LastFI = MFI->CreateFixedObject(RegSize, FirstVaArgOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002696 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002697
Akira Hatanakabad53f42011-11-14 19:01:09 +00002698 // Copy the integer registers that have not been used for argument passing
2699 // to the argument register save area. For O32, the save area is allocated
2700 // in the caller's stack frame, while for N32/64, it is allocated in the
2701 // callee's stack frame.
2702 for (int StackOffset = RegSlotOffset;
2703 Idx < NumOfRegs; ++Idx, StackOffset += RegSize) {
2704 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegs[Idx], RC);
2705 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2706 MVT::getIntegerVT(RegSize * 8));
2707 LastFI = MFI->CreateFixedObject(RegSize, StackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002708 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2709 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2710 MachinePointerInfo(),
2711 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002712 }
2713 }
2714
Akira Hatanaka43299772011-05-20 23:22:14 +00002715 MipsFI->setLastInArgFI(LastFI);
2716
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002717 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002718 // the size of Ins and InVals. This only happens when on varg functions
2719 if (!OutChains.empty()) {
2720 OutChains.push_back(Chain);
2721 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2722 &OutChains[0], OutChains.size());
2723 }
2724
Dan Gohman98ca4f22009-08-05 01:29:28 +00002725 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002726}
2727
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002728//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002729// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002730//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002731
Dan Gohman98ca4f22009-08-05 01:29:28 +00002732SDValue
2733MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002734 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002735 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002736 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002737 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002738
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002739 // CCValAssign - represent the assignment of
2740 // the return value to a location
2741 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002742
2743 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002744 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2745 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002746
Dan Gohman98ca4f22009-08-05 01:29:28 +00002747 // Analize return values.
2748 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002749
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002750 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002751 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002752 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002753 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002754 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002755 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002756 }
2757
Dan Gohman475871a2008-07-27 21:46:04 +00002758 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002759
2760 // Copy the result values into the output registers.
2761 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2762 CCValAssign &VA = RVLocs[i];
2763 assert(VA.isRegLoc() && "Can only return in registers!");
2764
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002765 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002766 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002767
2768 // guarantee that all emitted copies are
2769 // stuck together, avoiding something bad
2770 Flag = Chain.getValue(1);
2771 }
2772
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002773 // The mips ABIs for returning structs by value requires that we copy
2774 // the sret argument into $v0 for the return. We saved the argument into
2775 // a virtual register in the entry block, so now we copy the value out
2776 // and into $v0.
2777 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2778 MachineFunction &MF = DAG.getMachineFunction();
2779 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2780 unsigned Reg = MipsFI->getSRetReturnReg();
2781
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002782 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002783 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002784 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002785
Dale Johannesena05dca42009-02-04 23:02:30 +00002786 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002787 Flag = Chain.getValue(1);
2788 }
2789
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002790 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002791 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002792 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002793 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002794 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002795 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002796 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002797}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002798
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002799//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002800// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002801//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002802
2803/// getConstraintType - Given a constraint letter, return the type of
2804/// constraint it is for this target.
2805MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002806getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002807{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002808 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002809 // GCC config/mips/constraints.md
2810 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002811 // 'd' : An address register. Equivalent to r
2812 // unless generating MIPS16 code.
2813 // 'y' : Equivalent to r; retained for
2814 // backwards compatibility.
2815 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002816 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002817 switch (Constraint[0]) {
2818 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002819 case 'd':
2820 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002821 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002822 return C_RegisterClass;
2823 break;
2824 }
2825 }
2826 return TargetLowering::getConstraintType(Constraint);
2827}
2828
John Thompson44ab89e2010-10-29 17:29:13 +00002829/// Examine constraint type and operand type and determine a weight value.
2830/// This object must already have been set up with the operand type
2831/// and the current alternative constraint selected.
2832TargetLowering::ConstraintWeight
2833MipsTargetLowering::getSingleConstraintMatchWeight(
2834 AsmOperandInfo &info, const char *constraint) const {
2835 ConstraintWeight weight = CW_Invalid;
2836 Value *CallOperandVal = info.CallOperandVal;
2837 // If we don't have a value, we can't do a match,
2838 // but allow it at the lowest weight.
2839 if (CallOperandVal == NULL)
2840 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002841 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002842 // Look at the constraint type.
2843 switch (*constraint) {
2844 default:
2845 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2846 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002847 case 'd':
2848 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002849 if (type->isIntegerTy())
2850 weight = CW_Register;
2851 break;
2852 case 'f':
2853 if (type->isFloatTy())
2854 weight = CW_Register;
2855 break;
2856 }
2857 return weight;
2858}
2859
Eric Christopher38d64262011-06-29 19:33:04 +00002860/// Given a register class constraint, like 'r', if this corresponds directly
2861/// to an LLVM register class, return a register of 0 and the register class
2862/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002863std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002864getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002865{
2866 if (Constraint.size() == 1) {
2867 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002868 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2869 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002870 case 'r':
2871 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002872 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002873 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002874 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002875 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002876 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2877 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002878 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002879 }
2880 }
2881 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2882}
2883
Dan Gohman6520e202008-10-18 02:06:02 +00002884bool
2885MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2886 // The Mips target isn't yet aware of offsets.
2887 return false;
2888}
Evan Chengeb2f9692009-10-27 19:56:55 +00002889
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002890bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2891 if (VT != MVT::f32 && VT != MVT::f64)
2892 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002893 if (Imm.isNegZero())
2894 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002895 return Imm.isZero();
2896}