blob: 23592fd57ef4461091e61fcce7ea43e98537f310 [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) {
43 if (!isUInt<32>(I) || !isShiftedMask_32(I))
44 return false;
Akira Hatanakabb15e112011-08-17 02:05:42 +000045
Akira Hatanaka854a7db2011-08-19 22:59:00 +000046 Size = CountPopulation_32(I);
47 Pos = CountTrailingZeros_32(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::TlsGd: return "MipsISD::TlsGd";
58 case MipsISD::TprelHi: return "MipsISD::TprelHi";
59 case MipsISD::TprelLo: return "MipsISD::TprelLo";
60 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000061 case MipsISD::Ret: return "MipsISD::Ret";
62 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
63 case MipsISD::FPCmp: return "MipsISD::FPCmp";
64 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
65 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
66 case MipsISD::FPRound: return "MipsISD::FPRound";
67 case MipsISD::MAdd: return "MipsISD::MAdd";
68 case MipsISD::MAddu: return "MipsISD::MAddu";
69 case MipsISD::MSub: return "MipsISD::MSub";
70 case MipsISD::MSubu: return "MipsISD::MSubu";
71 case MipsISD::DivRem: return "MipsISD::DivRem";
72 case MipsISD::DivRemU: return "MipsISD::DivRemU";
73 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
74 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanaka342837d2011-05-28 01:07:07 +000075 case MipsISD::WrapperPIC: return "MipsISD::WrapperPIC";
Akira Hatanaka21afc632011-06-21 00:40:49 +000076 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
Akira Hatanakadb548262011-07-19 23:30:50 +000077 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanakabb15e112011-08-17 02:05:42 +000078 case MipsISD::Ext: return "MipsISD::Ext";
79 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanaka0f843822011-06-07 18:58:42 +000080 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000081 }
82}
83
84MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000085MipsTargetLowering(MipsTargetMachine &TM)
Akira Hatanaka8b4198d2011-09-26 21:47:02 +000086 : TargetLowering(TM, new MipsTargetObjectFile()),
87 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +000088 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
89 IsO32(Subtarget->isABI_O32()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000090
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000091 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000092 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000093 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +000094 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000095
96 // Set up the register classes
Owen Anderson825b72b2009-08-11 20:47:22 +000097 addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
98 addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000099
Akira Hatanaka95934842011-09-24 01:34:44 +0000100 if (HasMips64)
101 addRegisterClass(MVT::i64, Mips::CPU64RegsRegisterClass);
102
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000103 // When dealing with single precision only, use libcalls
Akira Hatanaka792016b2011-09-23 18:28:39 +0000104 if (!Subtarget->isSingleFloat()) {
105 if (HasMips64)
106 addRegisterClass(MVT::f64, Mips::FGR64RegisterClass);
107 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000108 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Akira Hatanaka792016b2011-09-23 18:28:39 +0000109 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000110
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000111 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000112 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
113 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
114 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000115
Eli Friedman6055a6a2009-07-17 04:07:24 +0000116 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000117 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
118 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000119
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000120 // Used by legalize types to correctly generate the setcc result.
121 // Without this, every float setcc comes with a AND/OR with the result,
122 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000123 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000124 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000125
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000126 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Akira Hatanakaa5903ac2011-10-11 00:55:05 +0000128 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000129 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
131 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
132 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
133 setOperationAction(ISD::SELECT, MVT::f32, Custom);
134 setOperationAction(ISD::SELECT, MVT::f64, Custom);
135 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000136 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
137 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000138 setOperationAction(ISD::VASTART, MVT::Other, Custom);
139
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000140 setOperationAction(ISD::SDIV, MVT::i32, Expand);
141 setOperationAction(ISD::SREM, MVT::i32, Expand);
142 setOperationAction(ISD::UDIV, MVT::i32, Expand);
143 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000144 setOperationAction(ISD::SDIV, MVT::i64, Expand);
145 setOperationAction(ISD::SREM, MVT::i64, Expand);
146 setOperationAction(ISD::UDIV, MVT::i64, Expand);
147 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000148
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000149 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000150 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
151 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
152 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
153 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
154 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
155 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
156 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
157 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
158 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000159 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000160
Akira Hatanaka56633442011-09-20 23:53:09 +0000161 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000162 setOperationAction(ISD::ROTR, MVT::i32, Expand);
163
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000164 if (!Subtarget->hasMips64r2())
165 setOperationAction(ISD::ROTR, MVT::i64, Expand);
166
Owen Anderson825b72b2009-08-11 20:47:22 +0000167 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
168 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
169 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000170 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
171 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000172 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000173 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000174 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000175 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000176 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
177 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000178 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000179 setOperationAction(ISD::FLOG, MVT::f32, Expand);
180 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
181 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
182 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000183 setOperationAction(ISD::FMA, MVT::f32, Expand);
184 setOperationAction(ISD::FMA, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000185
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000186 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
187 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Eric Christopher471e4222011-06-08 23:55:35 +0000188
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000189 setOperationAction(ISD::VAARG, MVT::Other, Expand);
190 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
191 setOperationAction(ISD::VAEND, MVT::Other, Expand);
192
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000193 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000194 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
195 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000196
Akira Hatanakadb548262011-07-19 23:30:50 +0000197 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000198 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000199
Eli Friedman4db5aca2011-08-29 18:23:02 +0000200 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
201 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
202
Eli Friedman26689ac2011-08-03 21:06:02 +0000203 setInsertFencesForAtomic(true);
204
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000205 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000206 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000207
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000208 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000209 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
210 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000211 }
212
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000213 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000214 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000215
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000216 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000217 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000218
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000219 setTargetDAGCombine(ISD::ADDE);
220 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000221 setTargetDAGCombine(ISD::SDIVREM);
222 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000223 setTargetDAGCombine(ISD::SETCC);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000224 setTargetDAGCombine(ISD::AND);
225 setTargetDAGCombine(ISD::OR);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000226
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000227 setMinFunctionAlignment(2);
228
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000229 setStackPointerRegisterToSaveRestore(Mips::SP);
230 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000231
232 setExceptionPointerRegister(Mips::A0);
233 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000234}
235
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000236bool MipsTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Akira Hatanaka511961a2011-08-17 18:49:18 +0000237 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
Akira Hatanaka7bd19bd2011-10-11 00:27:28 +0000238 return SVT == MVT::i64 || SVT == MVT::i32 || SVT == MVT::i16;
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000239}
240
Duncan Sands28b77e92011-09-06 19:07:46 +0000241EVT MipsTargetLowering::getSetCCResultType(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000243}
244
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000245// SelectMadd -
246// Transforms a subgraph in CurDAG if the following pattern is found:
247// (addc multLo, Lo0), (adde multHi, Hi0),
248// where,
249// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000250// Lo0: initial value of Lo register
251// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000252// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000253static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000254 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000255 // for the matching to be successful.
256 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
257
258 if (ADDCNode->getOpcode() != ISD::ADDC)
259 return false;
260
261 SDValue MultHi = ADDENode->getOperand(0);
262 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000263 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000264 unsigned MultOpc = MultHi.getOpcode();
265
266 // MultHi and MultLo must be generated by the same node,
267 if (MultLo.getNode() != MultNode)
268 return false;
269
270 // and it must be a multiplication.
271 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
272 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000273
274 // MultLo amd MultHi must be the first and second output of MultNode
275 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000276 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
277 return false;
278
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000279 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000280 // of the values of MultNode, in which case MultNode will be removed in later
281 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000282 // If there exist users other than ADDENode or ADDCNode, this function returns
283 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000284 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000285 // produced.
286 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
287 return false;
288
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000289 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000290 DebugLoc dl = ADDENode->getDebugLoc();
291
292 // create MipsMAdd(u) node
293 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000294
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000295 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
296 MVT::Glue,
297 MultNode->getOperand(0),// Factor 0
298 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000299 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000300 ADDENode->getOperand(1));// Hi0
301
302 // create CopyFromReg nodes
303 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
304 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000305 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000306 Mips::HI, MVT::i32,
307 CopyFromLo.getValue(2));
308
309 // replace uses of adde and addc here
310 if (!SDValue(ADDCNode, 0).use_empty())
311 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
312
313 if (!SDValue(ADDENode, 0).use_empty())
314 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
315
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000316 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000317}
318
319// SelectMsub -
320// Transforms a subgraph in CurDAG if the following pattern is found:
321// (addc Lo0, multLo), (sube Hi0, multHi),
322// where,
323// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000324// Lo0: initial value of Lo register
325// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000326// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000327static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000328 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000329 // for the matching to be successful.
330 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
331
332 if (SUBCNode->getOpcode() != ISD::SUBC)
333 return false;
334
335 SDValue MultHi = SUBENode->getOperand(1);
336 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000337 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000338 unsigned MultOpc = MultHi.getOpcode();
339
340 // MultHi and MultLo must be generated by the same node,
341 if (MultLo.getNode() != MultNode)
342 return false;
343
344 // and it must be a multiplication.
345 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
346 return false;
347
348 // MultLo amd MultHi must be the first and second output of MultNode
349 // respectively.
350 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
351 return false;
352
353 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
354 // of the values of MultNode, in which case MultNode will be removed in later
355 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000356 // If there exist users other than SUBENode or SUBCNode, this function returns
357 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000358 // instruction node rather than a pair of MULT and MSUB instructions being
359 // produced.
360 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
361 return false;
362
363 SDValue Chain = CurDAG->getEntryNode();
364 DebugLoc dl = SUBENode->getDebugLoc();
365
366 // create MipsSub(u) node
367 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
368
369 SDValue MSub = CurDAG->getNode(MultOpc, dl,
370 MVT::Glue,
371 MultNode->getOperand(0),// Factor 0
372 MultNode->getOperand(1),// Factor 1
373 SUBCNode->getOperand(0),// Lo0
374 SUBENode->getOperand(0));// Hi0
375
376 // create CopyFromReg nodes
377 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
378 MSub);
379 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
380 Mips::HI, MVT::i32,
381 CopyFromLo.getValue(2));
382
383 // replace uses of sube and subc here
384 if (!SDValue(SUBCNode, 0).use_empty())
385 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
386
387 if (!SDValue(SUBENode, 0).use_empty())
388 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
389
390 return true;
391}
392
393static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
394 TargetLowering::DAGCombinerInfo &DCI,
395 const MipsSubtarget* Subtarget) {
396 if (DCI.isBeforeLegalize())
397 return SDValue();
398
Akira Hatanakae184fec2011-11-11 04:18:21 +0000399 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
400 SelectMadd(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000401 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000402
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000403 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000404}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000405
406static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
407 TargetLowering::DAGCombinerInfo &DCI,
408 const MipsSubtarget* Subtarget) {
409 if (DCI.isBeforeLegalize())
410 return SDValue();
411
Akira Hatanakae184fec2011-11-11 04:18:21 +0000412 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
413 SelectMsub(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000414 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000415
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000416 return SDValue();
417}
418
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000419static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
420 TargetLowering::DAGCombinerInfo &DCI,
421 const MipsSubtarget* Subtarget) {
422 if (DCI.isBeforeLegalizeOps())
423 return SDValue();
424
Akira Hatanakadda4a072011-10-03 21:06:13 +0000425 EVT Ty = N->getValueType(0);
426 unsigned LO = (Ty == MVT::i32) ? Mips::LO : Mips::LO64;
427 unsigned HI = (Ty == MVT::i32) ? Mips::HI : Mips::HI64;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000428 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
429 MipsISD::DivRemU;
430 DebugLoc dl = N->getDebugLoc();
431
432 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
433 N->getOperand(0), N->getOperand(1));
434 SDValue InChain = DAG.getEntryNode();
435 SDValue InGlue = DivRem;
436
437 // insert MFLO
438 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakadda4a072011-10-03 21:06:13 +0000439 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000440 InGlue);
441 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
442 InChain = CopyFromLo.getValue(1);
443 InGlue = CopyFromLo.getValue(2);
444 }
445
446 // insert MFHI
447 if (N->hasAnyUseOfValue(1)) {
448 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000449 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000450 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
451 }
452
453 return SDValue();
454}
455
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000456static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
457 switch (CC) {
458 default: llvm_unreachable("Unknown fp condition code!");
459 case ISD::SETEQ:
460 case ISD::SETOEQ: return Mips::FCOND_OEQ;
461 case ISD::SETUNE: return Mips::FCOND_UNE;
462 case ISD::SETLT:
463 case ISD::SETOLT: return Mips::FCOND_OLT;
464 case ISD::SETGT:
465 case ISD::SETOGT: return Mips::FCOND_OGT;
466 case ISD::SETLE:
467 case ISD::SETOLE: return Mips::FCOND_OLE;
468 case ISD::SETGE:
469 case ISD::SETOGE: return Mips::FCOND_OGE;
470 case ISD::SETULT: return Mips::FCOND_ULT;
471 case ISD::SETULE: return Mips::FCOND_ULE;
472 case ISD::SETUGT: return Mips::FCOND_UGT;
473 case ISD::SETUGE: return Mips::FCOND_UGE;
474 case ISD::SETUO: return Mips::FCOND_UN;
475 case ISD::SETO: return Mips::FCOND_OR;
476 case ISD::SETNE:
477 case ISD::SETONE: return Mips::FCOND_ONE;
478 case ISD::SETUEQ: return Mips::FCOND_UEQ;
479 }
480}
481
482
483// Returns true if condition code has to be inverted.
484static bool InvertFPCondCode(Mips::CondCode CC) {
485 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
486 return false;
487
488 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
489 return true;
490
491 assert(false && "Illegal Condition Code");
492 return false;
493}
494
495// Creates and returns an FPCmp node from a setcc node.
496// Returns Op if setcc is not a floating point comparison.
497static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
498 // must be a SETCC node
499 if (Op.getOpcode() != ISD::SETCC)
500 return Op;
501
502 SDValue LHS = Op.getOperand(0);
503
504 if (!LHS.getValueType().isFloatingPoint())
505 return Op;
506
507 SDValue RHS = Op.getOperand(1);
508 DebugLoc dl = Op.getDebugLoc();
509
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000510 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
511 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000512 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
513
514 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
515 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
516}
517
518// Creates and returns a CMovFPT/F node.
519static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
520 SDValue False, DebugLoc DL) {
521 bool invert = InvertFPCondCode((Mips::CondCode)
522 cast<ConstantSDNode>(Cond.getOperand(2))
523 ->getSExtValue());
524
525 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
526 True.getValueType(), True, False, Cond);
527}
528
529static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
530 TargetLowering::DAGCombinerInfo &DCI,
531 const MipsSubtarget* Subtarget) {
532 if (DCI.isBeforeLegalizeOps())
533 return SDValue();
534
535 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
536
537 if (Cond.getOpcode() != MipsISD::FPCmp)
538 return SDValue();
539
540 SDValue True = DAG.getConstant(1, MVT::i32);
541 SDValue False = DAG.getConstant(0, MVT::i32);
542
543 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
544}
545
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000546static SDValue PerformANDCombine(SDNode *N, SelectionDAG& DAG,
547 TargetLowering::DAGCombinerInfo &DCI,
548 const MipsSubtarget* Subtarget) {
549 // Pattern match EXT.
550 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
551 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000552 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000553 return SDValue();
554
555 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
556
557 // Op's first operand must be a shift right.
558 if (ShiftRight.getOpcode() != ISD::SRA && ShiftRight.getOpcode() != ISD::SRL)
559 return SDValue();
560
561 // The second operand of the shift must be an immediate.
562 uint64_t Pos;
563 ConstantSDNode *CN;
564 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
565 return SDValue();
566
567 Pos = CN->getZExtValue();
568
569 uint64_t SMPos, SMSize;
570 // Op's second operand must be a shifted mask.
571 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000572 !IsShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000573 return SDValue();
574
575 // Return if the shifted mask does not start at bit 0 or the sum of its size
576 // and Pos exceeds the word's size.
577 if (SMPos != 0 || Pos + SMSize > 32)
578 return SDValue();
579
580 return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), MVT::i32,
581 ShiftRight.getOperand(0),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000582 DAG.getConstant(Pos, MVT::i32),
583 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000584}
585
586static SDValue PerformORCombine(SDNode *N, SelectionDAG& DAG,
587 TargetLowering::DAGCombinerInfo &DCI,
588 const MipsSubtarget* Subtarget) {
589 // Pattern match INS.
590 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
591 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
592 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000593 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000594 return SDValue();
595
596 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
597 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
598 ConstantSDNode *CN;
599
600 // See if Op's first operand matches (and $src1 , mask0).
601 if (And0.getOpcode() != ISD::AND)
602 return SDValue();
603
604 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000605 !IsShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000606 return SDValue();
607
608 // See if Op's second operand matches (and (shl $src, pos), mask1).
609 if (And1.getOpcode() != ISD::AND)
610 return SDValue();
611
612 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000613 !IsShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000614 return SDValue();
615
616 // The shift masks must have the same position and size.
617 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
618 return SDValue();
619
620 SDValue Shl = And1.getOperand(0);
621 if (Shl.getOpcode() != ISD::SHL)
622 return SDValue();
623
624 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
625 return SDValue();
626
627 unsigned Shamt = CN->getZExtValue();
628
629 // Return if the shift amount and the first bit position of mask are not the
630 // same.
631 if (Shamt != SMPos0)
632 return SDValue();
633
634 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), MVT::i32,
635 Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000636 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000637 DAG.getConstant(SMSize0, MVT::i32),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000638 And0.getOperand(0));
639}
640
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000641SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000642 const {
643 SelectionDAG &DAG = DCI.DAG;
644 unsigned opc = N->getOpcode();
645
646 switch (opc) {
647 default: break;
648 case ISD::ADDE:
649 return PerformADDECombine(N, DAG, DCI, Subtarget);
650 case ISD::SUBE:
651 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000652 case ISD::SDIVREM:
653 case ISD::UDIVREM:
654 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000655 case ISD::SETCC:
656 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000657 case ISD::AND:
658 return PerformANDCombine(N, DAG, DCI, Subtarget);
659 case ISD::OR:
660 return PerformORCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000661 }
662
663 return SDValue();
664}
665
Dan Gohman475871a2008-07-27 21:46:04 +0000666SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000667LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000668{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000669 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000670 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000671 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000672 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
673 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000674 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000675 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000676 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
677 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000678 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000679 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000680 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000681 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Akira Hatanakadb548262011-07-19 23:30:50 +0000682 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +0000683 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000684 }
Dan Gohman475871a2008-07-27 21:46:04 +0000685 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000686}
687
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000688//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000689// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000690//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000691
692// AddLiveIn - This helper function adds the specified physical register to the
693// MachineFunction as a live in value. It also creates a corresponding
694// virtual register for it.
695static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000696AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000697{
698 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000699 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
700 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000701 return VReg;
702}
703
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000704// Get fp branch code (not opcode) from condition code.
705static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
706 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
707 return Mips::BRANCH_T;
708
709 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
710 return Mips::BRANCH_F;
711
712 return Mips::BRANCH_INVALID;
713}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000714
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000715/*
Akira Hatanaka14487d42011-06-07 19:28:39 +0000716static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
717 DebugLoc dl,
718 const MipsSubtarget* Subtarget,
719 const TargetInstrInfo *TII,
720 bool isFPCmp, unsigned Opc) {
721 // There is no need to expand CMov instructions if target has
722 // conditional moves.
723 if (Subtarget->hasCondMov())
724 return BB;
725
726 // To "insert" a SELECT_CC instruction, we actually have to insert the
727 // diamond control-flow pattern. The incoming instruction knows the
728 // destination vreg to set, the condition code register to branch on, the
729 // true/false values to select between, and a branch opcode to use.
730 const BasicBlock *LLVM_BB = BB->getBasicBlock();
731 MachineFunction::iterator It = BB;
732 ++It;
733
734 // thisMBB:
735 // ...
736 // TrueVal = ...
737 // setcc r1, r2, r3
738 // bNE r1, r0, copy1MBB
739 // fallthrough --> copy0MBB
740 MachineBasicBlock *thisMBB = BB;
741 MachineFunction *F = BB->getParent();
742 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
743 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
744 F->insert(It, copy0MBB);
745 F->insert(It, sinkMBB);
746
747 // Transfer the remainder of BB and its successor edges to sinkMBB.
748 sinkMBB->splice(sinkMBB->begin(), BB,
749 llvm::next(MachineBasicBlock::iterator(MI)),
750 BB->end());
751 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
752
753 // Next, add the true and fallthrough blocks as its successors.
754 BB->addSuccessor(copy0MBB);
755 BB->addSuccessor(sinkMBB);
756
757 // Emit the right instruction according to the type of the operands compared
758 if (isFPCmp)
759 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
760 else
761 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
762 .addReg(Mips::ZERO).addMBB(sinkMBB);
763
764 // copy0MBB:
765 // %FalseValue = ...
766 // # fallthrough to sinkMBB
767 BB = copy0MBB;
768
769 // Update machine-CFG edges
770 BB->addSuccessor(sinkMBB);
771
772 // sinkMBB:
773 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
774 // ...
775 BB = sinkMBB;
776
777 if (isFPCmp)
778 BuildMI(*BB, BB->begin(), dl,
779 TII->get(Mips::PHI), MI->getOperand(0).getReg())
780 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
781 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
782 else
783 BuildMI(*BB, BB->begin(), dl,
784 TII->get(Mips::PHI), MI->getOperand(0).getReg())
785 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
786 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
787
788 MI->eraseFromParent(); // The pseudo instruction is gone now.
789 return BB;
790}
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000791*/
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000792MachineBasicBlock *
793MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000794 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000795 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000796 default:
797 assert(false && "Unexpected instr type to insert");
798 return NULL;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000799 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000800 case Mips::ATOMIC_LOAD_ADD_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000801 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
802 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000803 case Mips::ATOMIC_LOAD_ADD_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000804 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
805 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000806 case Mips::ATOMIC_LOAD_ADD_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000807 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000808 case Mips::ATOMIC_LOAD_ADD_I64:
809 case Mips::ATOMIC_LOAD_ADD_I64_P8:
810 return EmitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000811
812 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000813 case Mips::ATOMIC_LOAD_AND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000814 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
815 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000816 case Mips::ATOMIC_LOAD_AND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000817 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
818 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000819 case Mips::ATOMIC_LOAD_AND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000820 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000821 case Mips::ATOMIC_LOAD_AND_I64:
822 case Mips::ATOMIC_LOAD_AND_I64_P8:
Akira Hatanaka73866122011-11-12 02:38:12 +0000823 return EmitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000824
825 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000826 case Mips::ATOMIC_LOAD_OR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000827 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
828 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000829 case Mips::ATOMIC_LOAD_OR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000830 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
831 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000832 case Mips::ATOMIC_LOAD_OR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000833 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000834 case Mips::ATOMIC_LOAD_OR_I64:
835 case Mips::ATOMIC_LOAD_OR_I64_P8:
836 return EmitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000837
838 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000839 case Mips::ATOMIC_LOAD_XOR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000840 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
841 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000842 case Mips::ATOMIC_LOAD_XOR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000843 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
844 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000845 case Mips::ATOMIC_LOAD_XOR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000846 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000847 case Mips::ATOMIC_LOAD_XOR_I64:
848 case Mips::ATOMIC_LOAD_XOR_I64_P8:
849 return EmitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000850
851 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000852 case Mips::ATOMIC_LOAD_NAND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000853 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
854 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000855 case Mips::ATOMIC_LOAD_NAND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000856 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
857 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000858 case Mips::ATOMIC_LOAD_NAND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000859 return EmitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000860 case Mips::ATOMIC_LOAD_NAND_I64:
861 case Mips::ATOMIC_LOAD_NAND_I64_P8:
862 return EmitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000863
864 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000865 case Mips::ATOMIC_LOAD_SUB_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000866 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
867 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000868 case Mips::ATOMIC_LOAD_SUB_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000869 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
870 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000871 case Mips::ATOMIC_LOAD_SUB_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000872 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000873 case Mips::ATOMIC_LOAD_SUB_I64:
874 case Mips::ATOMIC_LOAD_SUB_I64_P8:
875 return EmitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000876
877 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000878 case Mips::ATOMIC_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000879 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
880 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000881 case Mips::ATOMIC_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000882 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
883 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000884 case Mips::ATOMIC_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000885 return EmitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000886 case Mips::ATOMIC_SWAP_I64:
887 case Mips::ATOMIC_SWAP_I64_P8:
888 return EmitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000889
890 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000891 case Mips::ATOMIC_CMP_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000892 return EmitAtomicCmpSwapPartword(MI, BB, 1);
893 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000894 case Mips::ATOMIC_CMP_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000895 return EmitAtomicCmpSwapPartword(MI, BB, 2);
896 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000897 case Mips::ATOMIC_CMP_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000898 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000899 case Mips::ATOMIC_CMP_SWAP_I64:
900 case Mips::ATOMIC_CMP_SWAP_I64_P8:
901 return EmitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000902 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000903}
904
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000905// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
906// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
907MachineBasicBlock *
908MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000909 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000910 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000911 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000912
913 MachineFunction *MF = BB->getParent();
914 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000915 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000916 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
917 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000918 unsigned LL, SC, AND, NOR, ZERO, BEQ;
919
920 if (Size == 4) {
921 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
922 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
923 AND = Mips::AND;
924 NOR = Mips::NOR;
925 ZERO = Mips::ZERO;
926 BEQ = Mips::BEQ;
927 }
928 else {
929 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
930 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
931 AND = Mips::AND64;
932 NOR = Mips::NOR64;
933 ZERO = Mips::ZERO_64;
934 BEQ = Mips::BEQ64;
935 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000936
Akira Hatanaka4061da12011-07-19 20:11:17 +0000937 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000938 unsigned Ptr = MI->getOperand(1).getReg();
939 unsigned Incr = MI->getOperand(2).getReg();
940
Akira Hatanaka4061da12011-07-19 20:11:17 +0000941 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
942 unsigned AndRes = RegInfo.createVirtualRegister(RC);
943 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000944
945 // insert new blocks after the current block
946 const BasicBlock *LLVM_BB = BB->getBasicBlock();
947 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
948 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
949 MachineFunction::iterator It = BB;
950 ++It;
951 MF->insert(It, loopMBB);
952 MF->insert(It, exitMBB);
953
954 // Transfer the remainder of BB and its successor edges to exitMBB.
955 exitMBB->splice(exitMBB->begin(), BB,
956 llvm::next(MachineBasicBlock::iterator(MI)),
957 BB->end());
958 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
959
960 // thisMBB:
961 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000962 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000963 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000964 loopMBB->addSuccessor(loopMBB);
965 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000966
967 // loopMBB:
968 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000969 // <binop> storeval, oldval, incr
970 // sc success, storeval, 0(ptr)
971 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000972 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +0000973 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000974 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000975 // and andres, oldval, incr
976 // nor storeval, $0, andres
Akira Hatanaka59068062011-11-11 04:14:30 +0000977 BuildMI(BB, dl, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
978 BuildMI(BB, dl, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000979 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000980 // <binop> storeval, oldval, incr
981 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000982 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000983 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000984 }
Akira Hatanaka59068062011-11-11 04:14:30 +0000985 BuildMI(BB, dl, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
986 BuildMI(BB, dl, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000987
988 MI->eraseFromParent(); // The instruction is gone now.
989
Akira Hatanaka939ece12011-07-19 03:42:13 +0000990 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000991}
992
993MachineBasicBlock *
994MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000995 MachineBasicBlock *BB,
996 unsigned Size, unsigned BinOpcode,
997 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000998 assert((Size == 1 || Size == 2) &&
999 "Unsupported size for EmitAtomicBinaryPartial.");
1000
1001 MachineFunction *MF = BB->getParent();
1002 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1003 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1004 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1005 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001006 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1007 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001008
1009 unsigned Dest = MI->getOperand(0).getReg();
1010 unsigned Ptr = MI->getOperand(1).getReg();
1011 unsigned Incr = MI->getOperand(2).getReg();
1012
Akira Hatanaka4061da12011-07-19 20:11:17 +00001013 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1014 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001015 unsigned Mask = RegInfo.createVirtualRegister(RC);
1016 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001017 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1018 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001019 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001020 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1021 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1022 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1023 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1024 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001025 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001026 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1027 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1028 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1029 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1030 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001031
1032 // insert new blocks after the current block
1033 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1034 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001035 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001036 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1037 MachineFunction::iterator It = BB;
1038 ++It;
1039 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001040 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001041 MF->insert(It, exitMBB);
1042
1043 // Transfer the remainder of BB and its successor edges to exitMBB.
1044 exitMBB->splice(exitMBB->begin(), BB,
1045 llvm::next(MachineBasicBlock::iterator(MI)),
1046 BB->end());
1047 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1048
Akira Hatanaka81b44112011-07-19 17:09:53 +00001049 BB->addSuccessor(loopMBB);
1050 loopMBB->addSuccessor(loopMBB);
1051 loopMBB->addSuccessor(sinkMBB);
1052 sinkMBB->addSuccessor(exitMBB);
1053
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001054 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001055 // addiu masklsb2,$0,-4 # 0xfffffffc
1056 // and alignedaddr,ptr,masklsb2
1057 // andi ptrlsb2,ptr,3
1058 // sll shiftamt,ptrlsb2,3
1059 // ori maskupper,$0,255 # 0xff
1060 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001061 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001062 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001063
1064 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001065 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1066 .addReg(Mips::ZERO).addImm(-4);
1067 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1068 .addReg(Ptr).addReg(MaskLSB2);
1069 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1070 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1071 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1072 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001073 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1074 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001075 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001076 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001077
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001078
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001079 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001080 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001081 // ll oldval,0(alignedaddr)
1082 // binop binopres,oldval,incr2
1083 // and newval,binopres,mask
1084 // and maskedoldval0,oldval,mask2
1085 // or storeval,maskedoldval0,newval
1086 // sc success,storeval,0(alignedaddr)
1087 // beq success,$0,loopMBB
1088
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001089 // atomic.swap
1090 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001091 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001092 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001093 // and maskedoldval0,oldval,mask2
1094 // or storeval,maskedoldval0,newval
1095 // sc success,storeval,0(alignedaddr)
1096 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001097
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001098 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001099 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001100 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001101 // and andres, oldval, incr2
1102 // nor binopres, $0, andres
1103 // and newval, binopres, mask
1104 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1105 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1106 .addReg(Mips::ZERO).addReg(AndRes);
1107 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001108 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001109 // <binop> binopres, oldval, incr2
1110 // and newval, binopres, mask
1111 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1112 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001113 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001114 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001115 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001116 }
1117
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001118 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001119 .addReg(OldVal).addReg(Mask2);
1120 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001121 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001122 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001123 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001124 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001125 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001126
Akira Hatanaka939ece12011-07-19 03:42:13 +00001127 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001128 // and maskedoldval1,oldval,mask
1129 // srl srlres,maskedoldval1,shiftamt
1130 // sll sllres,srlres,24
1131 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001132 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001133 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001134
Akira Hatanaka4061da12011-07-19 20:11:17 +00001135 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1136 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001137 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1138 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001139 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1140 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001141 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001142 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001143
1144 MI->eraseFromParent(); // The instruction is gone now.
1145
Akira Hatanaka939ece12011-07-19 03:42:13 +00001146 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001147}
1148
1149MachineBasicBlock *
1150MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001151 MachineBasicBlock *BB,
1152 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001153 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001154
1155 MachineFunction *MF = BB->getParent();
1156 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001157 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001158 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1159 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001160 unsigned LL, SC, ZERO, BNE, BEQ;
1161
1162 if (Size == 4) {
1163 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1164 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1165 ZERO = Mips::ZERO;
1166 BNE = Mips::BNE;
1167 BEQ = Mips::BEQ;
1168 }
1169 else {
1170 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1171 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1172 ZERO = Mips::ZERO_64;
1173 BNE = Mips::BNE64;
1174 BEQ = Mips::BEQ64;
1175 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001176
1177 unsigned Dest = MI->getOperand(0).getReg();
1178 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001179 unsigned OldVal = MI->getOperand(2).getReg();
1180 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001181
Akira Hatanaka4061da12011-07-19 20:11:17 +00001182 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001183
1184 // insert new blocks after the current block
1185 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1186 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1187 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1188 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1189 MachineFunction::iterator It = BB;
1190 ++It;
1191 MF->insert(It, loop1MBB);
1192 MF->insert(It, loop2MBB);
1193 MF->insert(It, exitMBB);
1194
1195 // Transfer the remainder of BB and its successor edges to exitMBB.
1196 exitMBB->splice(exitMBB->begin(), BB,
1197 llvm::next(MachineBasicBlock::iterator(MI)),
1198 BB->end());
1199 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1200
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001201 // thisMBB:
1202 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001203 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001204 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001205 loop1MBB->addSuccessor(exitMBB);
1206 loop1MBB->addSuccessor(loop2MBB);
1207 loop2MBB->addSuccessor(loop1MBB);
1208 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001209
1210 // loop1MBB:
1211 // ll dest, 0(ptr)
1212 // bne dest, oldval, exitMBB
1213 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001214 BuildMI(BB, dl, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1215 BuildMI(BB, dl, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001216 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001217
1218 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001219 // sc success, newval, 0(ptr)
1220 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001221 BB = loop2MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001222 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001223 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka59068062011-11-11 04:14:30 +00001224 BuildMI(BB, dl, TII->get(BEQ))
1225 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001226
1227 MI->eraseFromParent(); // The instruction is gone now.
1228
Akira Hatanaka939ece12011-07-19 03:42:13 +00001229 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001230}
1231
1232MachineBasicBlock *
1233MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001234 MachineBasicBlock *BB,
1235 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001236 assert((Size == 1 || Size == 2) &&
1237 "Unsupported size for EmitAtomicCmpSwapPartial.");
1238
1239 MachineFunction *MF = BB->getParent();
1240 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1241 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1242 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1243 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001244 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1245 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001246
1247 unsigned Dest = MI->getOperand(0).getReg();
1248 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001249 unsigned CmpVal = MI->getOperand(2).getReg();
1250 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001251
Akira Hatanaka4061da12011-07-19 20:11:17 +00001252 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1253 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001254 unsigned Mask = RegInfo.createVirtualRegister(RC);
1255 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001256 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1257 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1258 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1259 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1260 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1261 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1262 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1263 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1264 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1265 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1266 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1267 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1268 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1269 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001270
1271 // insert new blocks after the current block
1272 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1273 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1274 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001275 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001276 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1277 MachineFunction::iterator It = BB;
1278 ++It;
1279 MF->insert(It, loop1MBB);
1280 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001281 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001282 MF->insert(It, exitMBB);
1283
1284 // Transfer the remainder of BB and its successor edges to exitMBB.
1285 exitMBB->splice(exitMBB->begin(), BB,
1286 llvm::next(MachineBasicBlock::iterator(MI)),
1287 BB->end());
1288 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1289
Akira Hatanaka81b44112011-07-19 17:09:53 +00001290 BB->addSuccessor(loop1MBB);
1291 loop1MBB->addSuccessor(sinkMBB);
1292 loop1MBB->addSuccessor(loop2MBB);
1293 loop2MBB->addSuccessor(loop1MBB);
1294 loop2MBB->addSuccessor(sinkMBB);
1295 sinkMBB->addSuccessor(exitMBB);
1296
Akira Hatanaka70564a92011-07-19 18:14:26 +00001297 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001298 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001299 // addiu masklsb2,$0,-4 # 0xfffffffc
1300 // and alignedaddr,ptr,masklsb2
1301 // andi ptrlsb2,ptr,3
1302 // sll shiftamt,ptrlsb2,3
1303 // ori maskupper,$0,255 # 0xff
1304 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001305 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001306 // andi maskedcmpval,cmpval,255
1307 // sll shiftedcmpval,maskedcmpval,shiftamt
1308 // andi maskednewval,newval,255
1309 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001310 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001311 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1312 .addReg(Mips::ZERO).addImm(-4);
1313 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1314 .addReg(Ptr).addReg(MaskLSB2);
1315 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1316 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1317 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1318 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001319 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1320 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001321 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001322 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1323 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001324 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1325 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001326 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1327 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001328 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1329 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001330
1331 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001332 // ll oldval,0(alginedaddr)
1333 // and maskedoldval0,oldval,mask
1334 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001335 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001336 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001337 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1338 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001339 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001340 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001341
1342 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001343 // and maskedoldval1,oldval,mask2
1344 // or storeval,maskedoldval1,shiftednewval
1345 // sc success,storeval,0(alignedaddr)
1346 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001347 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001348 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1349 .addReg(OldVal).addReg(Mask2);
1350 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1351 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001352 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001353 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001354 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001355 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001356
Akira Hatanaka939ece12011-07-19 03:42:13 +00001357 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001358 // srl srlres,maskedoldval0,shiftamt
1359 // sll sllres,srlres,24
1360 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001361 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001362 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001363
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001364 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1365 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001366 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1367 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001368 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001369 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001370
1371 MI->eraseFromParent(); // The instruction is gone now.
1372
Akira Hatanaka939ece12011-07-19 03:42:13 +00001373 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001374}
1375
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001376//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001377// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001378//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001379SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001380LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001381{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001382 MachineFunction &MF = DAG.getMachineFunction();
1383 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001384 unsigned SP = IsN64 ? Mips::SP_64 : Mips::SP;
Akira Hatanaka21afc632011-06-21 00:40:49 +00001385
1386 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001387 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1388 "Cannot lower if the alignment of the allocated space is larger than \
1389 that of the stack.");
1390
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001391 SDValue Chain = Op.getOperand(0);
1392 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001393 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001394
1395 // Get a reference from Mips stack pointer
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001396 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, SP, getPointerTy());
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001397
1398 // Subtract the dynamic size from the actual stack size to
1399 // obtain the new stack size.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001400 SDValue Sub = DAG.getNode(ISD::SUB, dl, getPointerTy(), StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001401
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001402 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001403 // must be placed in the stack pointer register.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001404 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, SP, Sub, SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001405
1406 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001407 // value and a chain
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001408 SDVTList VTLs = DAG.getVTList(getPointerTy(), MVT::Other);
Akira Hatanaka21afc632011-06-21 00:40:49 +00001409 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1410 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1411
1412 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001413}
1414
1415SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001416LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001417{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001418 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001419 // the block to branch to if the condition is true.
1420 SDValue Chain = Op.getOperand(0);
1421 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001422 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001423
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001424 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1425
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001426 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001427 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001428 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001429
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001430 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001431 Mips::CondCode CC =
1432 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001433 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001434
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001435 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001436 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001437}
1438
1439SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001440LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001441{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001442 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001443
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001444 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001445 if (Cond.getOpcode() != MipsISD::FPCmp)
1446 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001447
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001448 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1449 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001450}
1451
Dan Gohmand858e902010-04-17 15:26:15 +00001452SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1453 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001454 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001455 DebugLoc dl = Op.getDebugLoc();
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001456 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001457
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001458 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001459 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001460
Chris Lattnerb71b9092009-08-13 06:28:06 +00001461 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001462
Chris Lattnere3736f82009-08-13 05:41:27 +00001463 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001464 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1465 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001466 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001467 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1468 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001469 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001470 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001471 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001472 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1473 MipsII::MO_ABS_HI);
1474 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1475 MipsII::MO_ABS_LO);
1476 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1477 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001478 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001479 }
1480
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001481 EVT ValTy = Op.getValueType();
1482 bool HasGotOfst = (GV->hasInternalLinkage() ||
1483 (GV->hasLocalLinkage() && !isa<Function>(GV)));
1484 unsigned GotFlag = IsN64 ?
1485 (HasGotOfst ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT_DISP) :
1486 MipsII::MO_GOT;
1487 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0, GotFlag);
1488 GA = DAG.getNode(MipsISD::WrapperPIC, dl, ValTy, GA);
1489 SDValue ResNode = DAG.getLoad(ValTy, dl,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001490 DAG.getEntryNode(), GA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001491 false, false, false, 0);
Akira Hatanaka0f843822011-06-07 18:58:42 +00001492 // On functions and global targets not internal linked only
1493 // a load from got/GP is necessary for PIC to work.
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001494 if (!HasGotOfst)
Akira Hatanaka0f843822011-06-07 18:58:42 +00001495 return ResNode;
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001496 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0,
1497 IsN64 ? MipsII::MO_GOT_OFST :
1498 MipsII::MO_ABS_LO);
1499 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, GALo);
1500 return DAG.getNode(ISD::ADD, dl, ValTy, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001501}
1502
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001503SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1504 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001505 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1506 // FIXME there isn't actually debug info here
1507 DebugLoc dl = Op.getDebugLoc();
1508
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001509 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001510 // %hi/%lo relocation
1511 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
1512 MipsII::MO_ABS_HI);
1513 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
1514 MipsII::MO_ABS_LO);
1515 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1516 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1517 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001518 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001519
1520 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1521 MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001522 BAGOTOffset = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, BAGOTOffset);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001523 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1524 MipsII::MO_ABS_LO);
1525 SDValue Load = DAG.getLoad(MVT::i32, dl,
1526 DAG.getEntryNode(), BAGOTOffset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001527 MachinePointerInfo(), false, false, false, 0);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001528 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
1529 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001530}
1531
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001532SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001533LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001534{
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001535 // If the relocation model is PIC, use the General Dynamic TLS Model,
1536 // otherwise use the Initial Exec or Local Exec TLS Model.
1537 // TODO: implement Local Dynamic TLS model
1538
1539 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1540 DebugLoc dl = GA->getDebugLoc();
1541 const GlobalValue *GV = GA->getGlobal();
1542 EVT PtrVT = getPointerTy();
1543
1544 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1545 // General Dynamic TLS Model
1546 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001547 0, MipsII::MO_TLSGD);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001548 SDValue Tlsgd = DAG.getNode(MipsISD::TlsGd, dl, MVT::i32, TGA);
1549 SDValue GP = DAG.getRegister(Mips::GP, MVT::i32);
1550 SDValue Argument = DAG.getNode(ISD::ADD, dl, MVT::i32, GP, Tlsgd);
1551
1552 ArgListTy Args;
1553 ArgListEntry Entry;
1554 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001555 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001556 Args.push_back(Entry);
1557 std::pair<SDValue, SDValue> CallResult =
1558 LowerCallTo(DAG.getEntryNode(),
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001559 (Type *) Type::getInt32Ty(*DAG.getContext()),
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001560 false, false, false, false, 0, CallingConv::C, false, true,
1561 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG,
1562 dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001563
1564 return CallResult.first;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001565 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001566
1567 SDValue Offset;
1568 if (GV->isDeclaration()) {
1569 // Initial Exec TLS Model
1570 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1571 MipsII::MO_GOTTPREL);
1572 Offset = DAG.getLoad(MVT::i32, dl,
1573 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001574 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001575 } else {
1576 // Local Exec TLS Model
1577 SDVTList VTs = DAG.getVTList(MVT::i32);
1578 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1579 MipsII::MO_TPREL_HI);
1580 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1581 MipsII::MO_TPREL_LO);
1582 SDValue Hi = DAG.getNode(MipsISD::TprelHi, dl, VTs, &TGAHi, 1);
1583 SDValue Lo = DAG.getNode(MipsISD::TprelLo, dl, MVT::i32, TGALo);
1584 Offset = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1585 }
1586
1587 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1588 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001589}
1590
1591SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001592LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001593{
Dan Gohman475871a2008-07-27 21:46:04 +00001594 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001595 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +00001596 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001597 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001598 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001599 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001600
Owen Andersone50ed302009-08-10 22:56:29 +00001601 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001602 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001603
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001604 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
1605
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +00001606 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001607 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001608 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001609 } else {// Emit Load from Global Pointer
1610 JTI = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, JTI);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001611 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
1612 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001613 false, false, false, 0);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001614 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001615
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001616 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1617 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001618 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001619 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001620
1621 return ResNode;
1622}
1623
Dan Gohman475871a2008-07-27 21:46:04 +00001624SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001625LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001626{
Dan Gohman475871a2008-07-27 21:46:04 +00001627 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001628 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001629 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001630 // FIXME there isn't actually debug info here
1631 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001632
1633 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001634 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001635 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001636 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001637 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001638 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001639 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1640 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001641 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001642
1643 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001644 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001645 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001646 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001647 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001648 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1649 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001650 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001651 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001652 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001653 N->getOffset(), MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001654 CP = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001655 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001656 CP, MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001657 false, false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001658 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001659 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001660 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001661 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
1662 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001663
1664 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001665}
1666
Dan Gohmand858e902010-04-17 15:26:15 +00001667SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001668 MachineFunction &MF = DAG.getMachineFunction();
1669 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1670
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001671 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001672 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1673 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001674
1675 // vastart just stores the address of the VarArgsFrameIndex slot into the
1676 // memory location argument.
1677 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001678 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1679 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +00001680 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001681}
1682
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001683static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
1684 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
1685 DebugLoc dl = Op.getDebugLoc();
1686 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
1687 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
1688 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
1689 DAG.getConstant(0x7fffffff, MVT::i32));
1690 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
1691 DAG.getConstant(0x80000000, MVT::i32));
1692 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1693 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
1694}
1695
1696static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001697 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001698 // Use ext/ins instructions if target architecture is Mips32r2.
1699 // Eliminate redundant mfc1 and mtc1 instructions.
1700 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001701
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001702 if (!isLittle)
1703 std::swap(LoIdx, HiIdx);
1704
1705 DebugLoc dl = Op.getDebugLoc();
1706 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1707 Op.getOperand(0),
1708 DAG.getConstant(LoIdx, MVT::i32));
1709 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1710 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1711 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1712 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1713 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1714 DAG.getConstant(0x7fffffff, MVT::i32));
1715 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1716 DAG.getConstant(0x80000000, MVT::i32));
1717 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1718
1719 if (!isLittle)
1720 std::swap(Word0, Word1);
1721
1722 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1723}
1724
1725SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1726 const {
1727 EVT Ty = Op.getValueType();
1728
1729 assert(Ty == MVT::f32 || Ty == MVT::f64);
1730
1731 if (Ty == MVT::f32)
1732 return LowerFCOPYSIGN32(Op, DAG);
1733 else
1734 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
1735}
1736
Akira Hatanaka2e591472011-06-02 00:24:44 +00001737SDValue MipsTargetLowering::
1738LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001739 // check the depth
1740 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001741 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001742
1743 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1744 MFI->setFrameAddressIsTaken(true);
1745 EVT VT = Op.getValueType();
1746 DebugLoc dl = Op.getDebugLoc();
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001747 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
1748 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001749 return FrameAddr;
1750}
1751
Akira Hatanakadb548262011-07-19 23:30:50 +00001752// TODO: set SType according to the desired memory barrier behavior.
1753SDValue MipsTargetLowering::LowerMEMBARRIER(SDValue Op,
1754 SelectionDAG& DAG) const {
1755 unsigned SType = 0;
1756 DebugLoc dl = Op.getDebugLoc();
1757 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1758 DAG.getConstant(SType, MVT::i32));
1759}
1760
Eli Friedman14648462011-07-27 22:21:52 +00001761SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1762 SelectionDAG& DAG) const {
1763 // FIXME: Need pseudo-fence for 'singlethread' fences
1764 // FIXME: Set SType for weaker fences where supported/appropriate.
1765 unsigned SType = 0;
1766 DebugLoc dl = Op.getDebugLoc();
1767 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1768 DAG.getConstant(SType, MVT::i32));
1769}
1770
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001771//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001772// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001773//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001774
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001775//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001776// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001777// Mips O32 ABI rules:
1778// ---
1779// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001780// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001781// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001782// f64 - Only passed in two aliased f32 registers if no int reg has been used
1783// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001784// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1785// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001786//
1787// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001788//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001789
Duncan Sands1e96bab2010-11-04 10:49:57 +00001790static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001791 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001792 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1793
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001794 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001795
1796 static const unsigned IntRegs[] = {
1797 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1798 };
1799 static const unsigned F32Regs[] = {
1800 Mips::F12, Mips::F14
1801 };
1802 static const unsigned F64Regs[] = {
1803 Mips::D6, Mips::D7
1804 };
1805
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001806 // ByVal Args
1807 if (ArgFlags.isByVal()) {
1808 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1809 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1810 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1811 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1812 r < std::min(IntRegsSize, NextReg); ++r)
1813 State.AllocateReg(IntRegs[r]);
1814 return false;
1815 }
1816
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001817 // Promote i8 and i16
1818 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1819 LocVT = MVT::i32;
1820 if (ArgFlags.isSExt())
1821 LocInfo = CCValAssign::SExt;
1822 else if (ArgFlags.isZExt())
1823 LocInfo = CCValAssign::ZExt;
1824 else
1825 LocInfo = CCValAssign::AExt;
1826 }
1827
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001828 unsigned Reg;
1829
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001830 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1831 // is true: function is vararg, argument is 3rd or higher, there is previous
1832 // argument which is not f32 or f64.
1833 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1834 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001835 unsigned OrigAlign = ArgFlags.getOrigAlign();
1836 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001837
1838 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001839 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001840 // If this is the first part of an i64 arg,
1841 // the allocated register must be either A0 or A2.
1842 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1843 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001844 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001845 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1846 // Allocate int register and shadow next int register. If first
1847 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001848 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1849 if (Reg == Mips::A1 || Reg == Mips::A3)
1850 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1851 State.AllocateReg(IntRegs, IntRegsSize);
1852 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001853 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1854 // we are guaranteed to find an available float register
1855 if (ValVT == MVT::f32) {
1856 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1857 // Shadow int register
1858 State.AllocateReg(IntRegs, IntRegsSize);
1859 } else {
1860 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1861 // Shadow int registers
1862 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1863 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1864 State.AllocateReg(IntRegs, IntRegsSize);
1865 State.AllocateReg(IntRegs, IntRegsSize);
1866 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001867 } else
1868 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001869
Akira Hatanakad37776d2011-05-20 21:39:54 +00001870 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1871 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1872
1873 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001874 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001875 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001876 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001877
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001878 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001879}
1880
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00001881static const unsigned Mips64IntRegs[8] =
1882 {Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
1883 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64};
1884static const unsigned Mips64DPRegs[8] =
1885 {Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
1886 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64};
1887
1888static bool CC_Mips64Byval(unsigned ValNo, MVT ValVT, MVT LocVT,
1889 CCValAssign::LocInfo LocInfo,
1890 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1891 unsigned Align = std::max(ArgFlags.getByValAlign(), (unsigned)8);
1892 unsigned Size = (ArgFlags.getByValSize() + 7) / 8 * 8;
1893 unsigned FirstIdx = State.getFirstUnallocated(Mips64IntRegs, 8);
1894
1895 assert(Align <= 16 && "Cannot handle alignments larger than 16.");
1896
1897 // If byval is 16-byte aligned, the first arg register must be even.
1898 if ((Align == 16) && (FirstIdx % 2)) {
1899 State.AllocateReg(Mips64IntRegs[FirstIdx], Mips64DPRegs[FirstIdx]);
1900 ++FirstIdx;
1901 }
1902
1903 // Mark the registers allocated.
1904 for (unsigned I = FirstIdx; Size && (I < 8); Size -= 8, ++I)
1905 State.AllocateReg(Mips64IntRegs[I], Mips64DPRegs[I]);
1906
1907 // Allocate space on caller's stack.
1908 unsigned Offset = State.AllocateStack(Size, Align);
1909
1910 if (FirstIdx < 8)
1911 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Mips64IntRegs[FirstIdx],
1912 LocVT, LocInfo));
1913 else
1914 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
1915
1916 return true;
1917}
1918
1919#include "MipsGenCallingConv.inc"
1920
Akira Hatanaka49617092011-11-14 19:02:54 +00001921static void
1922AnalyzeMips64CallOperands(CCState CCInfo,
1923 const SmallVectorImpl<ISD::OutputArg> &Outs) {
1924 unsigned NumOps = Outs.size();
1925 for (unsigned i = 0; i != NumOps; ++i) {
1926 MVT ArgVT = Outs[i].VT;
1927 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
1928 bool R;
1929
1930 if (Outs[i].IsFixed)
1931 R = CC_MipsN(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
1932 else
1933 R = CC_MipsN_VarArg(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
1934
Akira Hatanaka49617092011-11-14 19:02:54 +00001935 if (R) {
Benjamin Kramer6296ee32011-11-14 19:51:48 +00001936#ifndef NDEBUG
Akira Hatanaka49617092011-11-14 19:02:54 +00001937 dbgs() << "Call operand #" << i << " has unhandled type "
1938 << EVT(ArgVT).getEVTString();
1939#endif
1940 llvm_unreachable(0);
1941 }
1942 }
1943}
1944
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001945//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001946// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001947//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001948
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001949static const unsigned O32IntRegsSize = 4;
1950
1951static const unsigned O32IntRegs[] = {
1952 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1953};
1954
Akira Hatanaka373e3a42011-09-23 00:58:33 +00001955// Return next O32 integer argument register.
1956static unsigned getNextIntArgReg(unsigned Reg) {
1957 assert((Reg == Mips::A0) || (Reg == Mips::A2));
1958 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
1959}
1960
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001961// Write ByVal Arg to arg registers and stack.
1962static void
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001963WriteByValArg(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001964 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1965 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1966 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001967 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001968 MVT PtrType, bool isLittle) {
1969 unsigned LocMemOffset = VA.getLocMemOffset();
1970 unsigned Offset = 0;
1971 uint32_t RemainingSize = Flags.getByValSize();
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001972 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001973
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001974 // Copy the first 4 words of byval arg to registers A0 - A3.
1975 // FIXME: Use a stricter alignment if it enables better optimization in passes
1976 // run later.
1977 for (; RemainingSize >= 4 && LocMemOffset < 4 * 4;
1978 Offset += 4, RemainingSize -= 4, LocMemOffset += 4) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001979 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001980 DAG.getConstant(Offset, MVT::i32));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001981 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1982 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001983 false, false, false, std::min(ByValAlign,
1984 (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001985 MemOpChains.push_back(LoadVal.getValue(1));
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001986 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001987 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1988 }
1989
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001990 if (RemainingSize == 0)
1991 return;
1992
1993 // If there still is a register available for argument passing, write the
1994 // remaining part of the structure to it using subword loads and shifts.
1995 if (LocMemOffset < 4 * 4) {
1996 assert(RemainingSize <= 3 && RemainingSize >= 1 &&
1997 "There must be one to three bytes remaining.");
1998 unsigned LoadSize = (RemainingSize == 3 ? 2 : RemainingSize);
1999 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2000 DAG.getConstant(Offset, MVT::i32));
2001 unsigned Alignment = std::min(ByValAlign, (unsigned )4);
2002 SDValue LoadVal = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2003 LoadPtr, MachinePointerInfo(),
2004 MVT::getIntegerVT(LoadSize * 8), false,
2005 false, Alignment);
2006 MemOpChains.push_back(LoadVal.getValue(1));
2007
2008 // If target is big endian, shift it to the most significant half-word or
2009 // byte.
2010 if (!isLittle)
2011 LoadVal = DAG.getNode(ISD::SHL, dl, MVT::i32, LoadVal,
2012 DAG.getConstant(32 - LoadSize * 8, MVT::i32));
2013
2014 Offset += LoadSize;
2015 RemainingSize -= LoadSize;
2016
2017 // Read second subword if necessary.
2018 if (RemainingSize != 0) {
2019 assert(RemainingSize == 1 && "There must be one byte remaining.");
2020 LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2021 DAG.getConstant(Offset, MVT::i32));
2022 unsigned Alignment = std::min(ByValAlign, (unsigned )2);
2023 SDValue Subword = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2024 LoadPtr, MachinePointerInfo(),
2025 MVT::i8, false, false, Alignment);
2026 MemOpChains.push_back(Subword.getValue(1));
2027 // Insert the loaded byte to LoadVal.
2028 // FIXME: Use INS if supported by target.
2029 unsigned ShiftAmt = isLittle ? 16 : 8;
2030 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i32, Subword,
2031 DAG.getConstant(ShiftAmt, MVT::i32));
2032 LoadVal = DAG.getNode(ISD::OR, dl, MVT::i32, LoadVal, Shift);
2033 }
2034
2035 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
2036 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
2037 return;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002038 }
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002039
2040 // Create a fixed object on stack at offset LocMemOffset and copy
2041 // remaining part of byval arg to it using memcpy.
2042 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2043 DAG.getConstant(Offset, MVT::i32));
2044 LastFI = MFI->CreateFixedObject(RemainingSize, LocMemOffset, true);
2045 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002046 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2047 DAG.getConstant(RemainingSize, MVT::i32),
2048 std::min(ByValAlign, (unsigned)4),
2049 /*isVolatile=*/false, /*AlwaysInline=*/false,
2050 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002051}
2052
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002053// Copy Mips64 byVal arg to registers and stack.
2054void static
2055PassByValArg64(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
2056 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
2057 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
2058 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
2059 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2060 EVT PtrTy, bool isLittle) {
2061 unsigned ByValSize = Flags.getByValSize();
2062 unsigned Alignment = std::min(Flags.getByValAlign(), (unsigned)8);
2063 bool IsRegLoc = VA.isRegLoc();
2064 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
2065 unsigned LocMemOffset = 0;
Akira Hatanaka16040852011-11-15 18:42:25 +00002066 unsigned MemCpySize = ByValSize;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002067
2068 if (!IsRegLoc)
2069 LocMemOffset = VA.getLocMemOffset();
2070 else {
2071 const unsigned *Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8,
2072 VA.getLocReg());
2073 const unsigned *RegEnd = Mips64IntRegs + 8;
2074
2075 // Copy double words to registers.
2076 for (; (Reg != RegEnd) && (ByValSize >= Offset + 8); ++Reg, Offset += 8) {
2077 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2078 DAG.getConstant(Offset, PtrTy));
2079 SDValue LoadVal = DAG.getLoad(MVT::i64, dl, Chain, LoadPtr,
2080 MachinePointerInfo(), false, false, false,
2081 Alignment);
2082 MemOpChains.push_back(LoadVal.getValue(1));
2083 RegsToPass.push_back(std::make_pair(*Reg, LoadVal));
2084 }
2085
Akira Hatanaka16040852011-11-15 18:42:25 +00002086 // Return if the struct has been fully copied.
2087 if (!(MemCpySize = ByValSize - Offset))
2088 return;
2089
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002090 // If there is an argument register available, copy the remainder of the
2091 // byval argument with sub-doubleword loads and shifts.
Akira Hatanaka16040852011-11-15 18:42:25 +00002092 if (Reg != RegEnd) {
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002093 assert((ByValSize < Offset + 8) &&
2094 "Size of the remainder should be smaller than 8-byte.");
2095 SDValue Val;
2096 for (unsigned LoadSize = 4; Offset < ByValSize; LoadSize /= 2) {
2097 unsigned RemSize = ByValSize - Offset;
2098
2099 if (RemSize < LoadSize)
2100 continue;
2101
2102 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2103 DAG.getConstant(Offset, PtrTy));
2104 SDValue LoadVal =
2105 DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i64, Chain, LoadPtr,
2106 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
2107 false, false, Alignment);
2108 MemOpChains.push_back(LoadVal.getValue(1));
2109
2110 // Offset in number of bits from double word boundary.
2111 unsigned OffsetDW = (Offset % 8) * 8;
2112 unsigned Shamt = isLittle ? OffsetDW : 64 - (OffsetDW + LoadSize * 8);
2113 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i64, LoadVal,
2114 DAG.getConstant(Shamt, MVT::i32));
2115
2116 Val = Val.getNode() ? DAG.getNode(ISD::OR, dl, MVT::i64, Val, Shift) :
2117 Shift;
2118 Offset += LoadSize;
2119 Alignment = std::min(Alignment, LoadSize);
2120 }
2121
2122 RegsToPass.push_back(std::make_pair(*Reg, Val));
2123 return;
2124 }
2125 }
2126
Akira Hatanaka16040852011-11-15 18:42:25 +00002127 assert(MemCpySize && "MemCpySize must not be zero.");
2128
2129 // Create a fixed object on stack at offset LocMemOffset and copy
2130 // remainder of byval arg to it with memcpy.
2131 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2132 DAG.getConstant(Offset, PtrTy));
2133 LastFI = MFI->CreateFixedObject(MemCpySize, LocMemOffset, true);
2134 SDValue Dst = DAG.getFrameIndex(LastFI, PtrTy);
2135 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2136 DAG.getConstant(MemCpySize, PtrTy), Alignment,
2137 /*isVolatile=*/false, /*AlwaysInline=*/false,
2138 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002139}
2140
Dan Gohman98ca4f22009-08-05 01:29:28 +00002141/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002142/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002143/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002144SDValue
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002145MipsTargetLowering::LowerCall(SDValue InChain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002146 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002147 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002148 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002149 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002150 const SmallVectorImpl<ISD::InputArg> &Ins,
2151 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002152 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002153 // MIPs target does not yet support tail call optimization.
2154 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002155
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002156 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002157 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002158 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002159 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00002160 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002161
2162 // Analyze operands of the call, assigning locations to each operand.
2163 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002164 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2165 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002166
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002167 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002168 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanaka49617092011-11-14 19:02:54 +00002169 else if (HasMips64)
2170 AnalyzeMips64CallOperands(CCInfo, Outs);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00002171 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002172 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002173
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002174 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002175 unsigned NextStackOffset = CCInfo.getNextStackOffset();
2176
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002177 // Chain is the output chain of the last Load/Store or CopyToReg node.
2178 // ByValChain is the output chain of the last Memcpy node created for copying
2179 // byval arguments to the stack.
2180 SDValue Chain, CallSeqStart, ByValChain;
2181 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
2182 Chain = CallSeqStart = DAG.getCALLSEQ_START(InChain, NextStackOffsetVal);
2183 ByValChain = InChain;
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002184
2185 // If this is the first call, create a stack frame object that points to
2186 // a location to which .cprestore saves $gp.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002187 if (IsO32 && IsPIC && !MipsFI->getGPFI())
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002188 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
2189
Akira Hatanaka21afc632011-06-21 00:40:49 +00002190 // Get the frame index of the stack frame object that points to the location
2191 // of dynamically allocated area on the stack.
2192 int DynAllocFI = MipsFI->getDynAllocFI();
2193
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002194 // Update size of the maximum argument space.
2195 // For O32, a minimum of four words (16 bytes) of argument space is
2196 // allocated.
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002197 if (IsO32)
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002198 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
2199
2200 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
2201
2202 if (MaxCallFrameSize < NextStackOffset) {
2203 MipsFI->setMaxCallFrameSize(NextStackOffset);
2204
Akira Hatanaka21afc632011-06-21 00:40:49 +00002205 // Set the offsets relative to $sp of the $gp restore slot and dynamically
2206 // allocated stack space. These offsets must be aligned to a boundary
2207 // determined by the stack alignment of the ABI.
2208 unsigned StackAlignment = TFL->getStackAlignment();
2209 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
2210 StackAlignment * StackAlignment;
2211
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002212 if (MipsFI->needGPSaveRestore())
Akira Hatanaka21afc632011-06-21 00:40:49 +00002213 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
2214
2215 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002216 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002217
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002218 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002219 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
2220 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002221
Eric Christopher471e4222011-06-08 23:55:35 +00002222 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00002223
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002224 // Walk the register/memloc assignments, inserting copies/loads.
2225 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002226 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002227 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002228 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002229 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2230
2231 // ByVal Arg.
2232 if (Flags.isByVal()) {
2233 assert(Flags.getByValSize() &&
2234 "ByVal args of size 0 should have been ignored by front-end.");
2235 if (IsO32)
2236 WriteByValArg(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2237 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2238 Subtarget->isLittle());
2239 else
2240 PassByValArg64(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2241 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2242 Subtarget->isLittle());
2243 continue;
2244 }
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002245
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002246 // Promote the value if needed.
2247 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002248 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002249 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002250 if (VA.isRegLoc()) {
2251 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
2252 (ValVT == MVT::f64 && LocVT == MVT::i64))
2253 Arg = DAG.getNode(ISD::BITCAST, dl, LocVT, Arg);
2254 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002255 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2256 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002257 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2258 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002259 if (!Subtarget->isLittle())
2260 std::swap(Lo, Hi);
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002261 unsigned LocRegLo = VA.getLocReg();
2262 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2263 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2264 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002265 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002266 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002267 }
2268 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002269 case CCValAssign::SExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002270 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002271 break;
2272 case CCValAssign::ZExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002273 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002274 break;
2275 case CCValAssign::AExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002276 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002277 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002278 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002279
2280 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002281 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002282 if (VA.isRegLoc()) {
2283 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002284 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002285 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002286
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002287 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002288 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002289
Chris Lattnere0b12152008-03-17 06:57:02 +00002290 // Create the frame index object for this incoming parameter
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002291 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002292 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00002293 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00002294
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002295 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002296 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00002297 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
2298 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00002299 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002300 }
2301
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002302 // Extend range of indices of frame objects for outgoing arguments that were
2303 // created during this function call. Skip this step if no such objects were
2304 // created.
2305 if (LastFI)
2306 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2307
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002308 // If a memcpy has been created to copy a byval arg to a stack, replace the
2309 // chain input of CallSeqStart with ByValChain.
2310 if (InChain != ByValChain)
2311 DAG.UpdateNodeOperands(CallSeqStart.getNode(), ByValChain,
2312 NextStackOffsetVal);
2313
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002314 // Transform all store nodes into one single node because all store
2315 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002316 if (!MemOpChains.empty())
2317 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002318 &MemOpChains[0], MemOpChains.size());
2319
Bill Wendling056292f2008-09-16 21:48:12 +00002320 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002321 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2322 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002323 unsigned char OpFlag;
2324 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002325 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002326 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002327
2328 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002329 if (IsPICCall && G->getGlobal()->hasInternalLinkage()) {
2330 OpFlag = IsO32 ? MipsII::MO_GOT : MipsII::MO_GOT_PAGE;
2331 unsigned char LoFlag = IsO32 ? MipsII::MO_ABS_LO : MipsII::MO_GOT_OFST;
2332 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(), 0,
2333 OpFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002334 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002335 0, LoFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002336 } else {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002337 OpFlag = IsPICCall ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002338 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2339 getPointerTy(), 0, OpFlag);
2340 }
2341
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002342 LoadSymAddr = true;
2343 }
2344 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002345 if (IsN64 || (!IsO32 && IsPIC))
2346 OpFlag = MipsII::MO_GOT_DISP;
2347 else if (!IsPIC) // !N64 && static
2348 OpFlag = MipsII::MO_NO_FLAG;
2349 else // O32 & PIC
2350 OpFlag = MipsII::MO_GOT_CALL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002351 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002352 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002353 LoadSymAddr = true;
2354 }
2355
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002356 SDValue InFlag;
2357
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002358 // Create nodes that load address of callee and copy it to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002359 if (IsPICCall) {
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002360 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002361 // Load callee address
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002362 Callee = DAG.getNode(MipsISD::WrapperPIC, dl, getPointerTy(), Callee);
2363 SDValue LoadValue = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
2364 Callee, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002365 false, false, false, 0);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002366
2367 // Use GOT+LO if callee has internal linkage.
2368 if (CalleeLo.getNode()) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002369 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, getPointerTy(), CalleeLo);
2370 Callee = DAG.getNode(ISD::ADD, dl, getPointerTy(), LoadValue, Lo);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002371 } else
2372 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002373 }
2374
2375 // copy to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002376 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
2377 Chain = DAG.getCopyToReg(Chain, dl, T9Reg, Callee, SDValue(0, 0));
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002378 InFlag = Chain.getValue(1);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002379 Callee = DAG.getRegister(T9Reg, getPointerTy());
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002380 }
Bill Wendling056292f2008-09-16 21:48:12 +00002381
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002382 // Build a sequence of copy-to-reg nodes chained together with token
2383 // chain and flag operands which copy the outgoing args into registers.
2384 // The InFlag in necessary since all emitted instructions must be
2385 // stuck together.
2386 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2387 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2388 RegsToPass[i].second, InFlag);
2389 InFlag = Chain.getValue(1);
2390 }
2391
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002392 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002393 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002394 //
2395 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002396 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002397 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002398 Ops.push_back(Chain);
2399 Ops.push_back(Callee);
2400
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002401 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002402 // known live into the call.
2403 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2404 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2405 RegsToPass[i].second.getValueType()));
2406
Gabor Greifba36cb52008-08-28 21:40:38 +00002407 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002408 Ops.push_back(InFlag);
2409
Dale Johannesen33c960f2009-02-04 20:06:27 +00002410 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002411 InFlag = Chain.getValue(1);
2412
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002413 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002414 Chain = DAG.getCALLSEQ_END(Chain,
2415 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002416 DAG.getIntPtrConstant(0, true), InFlag);
2417 InFlag = Chain.getValue(1);
2418
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002419 // Handle result values, copying them out of physregs into vregs that we
2420 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002421 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2422 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002423}
2424
Dan Gohman98ca4f22009-08-05 01:29:28 +00002425/// LowerCallResult - Lower the result values of a call into the
2426/// appropriate copies out of appropriate physical registers.
2427SDValue
2428MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002429 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002430 const SmallVectorImpl<ISD::InputArg> &Ins,
2431 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002432 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002433 // Assign locations to each value returned by this call.
2434 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002435 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2436 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002437
Dan Gohman98ca4f22009-08-05 01:29:28 +00002438 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002439
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002440 // Copy all of the result registers out of their specified physreg.
2441 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002442 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002443 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002444 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002445 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002446 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002447
Dan Gohman98ca4f22009-08-05 01:29:28 +00002448 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002449}
2450
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002451//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002452// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002453//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002454static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2455 std::vector<SDValue>& OutChains,
2456 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2457 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2458 unsigned LocMem = VA.getLocMemOffset();
2459 unsigned FirstWord = LocMem / 4;
2460
2461 // copy register A0 - A3 to frame object
2462 for (unsigned i = 0; i < NumWords; ++i) {
2463 unsigned CurWord = FirstWord + i;
2464 if (CurWord >= O32IntRegsSize)
2465 break;
2466
2467 unsigned SrcReg = O32IntRegs[CurWord];
2468 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2469 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2470 DAG.getConstant(i * 4, MVT::i32));
2471 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2472 StorePtr, MachinePointerInfo(), false,
2473 false, 0);
2474 OutChains.push_back(Store);
2475 }
2476}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002477
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002478// Create frame object on stack and copy registers used for byval passing to it.
2479static unsigned
2480CopyMips64ByValRegs(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2481 std::vector<SDValue>& OutChains, SelectionDAG &DAG,
2482 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2483 MachineFrameInfo *MFI, bool IsRegLoc,
2484 SmallVectorImpl<SDValue> &InVals, MipsFunctionInfo *MipsFI,
2485 EVT PtrTy) {
2486 const unsigned *Reg = Mips64IntRegs + 8;
2487 int FOOffset; // Frame object offset from virtual frame pointer.
2488
2489 if (IsRegLoc) {
2490 Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8, VA.getLocReg());
2491 FOOffset = (Reg - Mips64IntRegs) * 8 - 8 * 8;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002492 }
2493 else
2494 FOOffset = VA.getLocMemOffset();
2495
2496 // Create frame object.
2497 unsigned NumRegs = (Flags.getByValSize() + 7) / 8;
2498 unsigned LastFI = MFI->CreateFixedObject(NumRegs * 8, FOOffset, true);
2499 SDValue FIN = DAG.getFrameIndex(LastFI, PtrTy);
2500 InVals.push_back(FIN);
2501
2502 // Copy arg registers.
2503 for (unsigned I = 0; (Reg != Mips64IntRegs + 8) && (I < NumRegs);
2504 ++Reg, ++I) {
2505 unsigned VReg = AddLiveIn(MF, *Reg, Mips::CPU64RegsRegisterClass);
2506 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, PtrTy, FIN,
2507 DAG.getConstant(I * 8, PtrTy));
2508 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(VReg, MVT::i64),
2509 StorePtr, MachinePointerInfo(), false,
2510 false, 0);
2511 OutChains.push_back(Store);
2512 }
2513
2514 return LastFI;
2515}
2516
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002517/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002518/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002519SDValue
2520MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002521 CallingConv::ID CallConv,
2522 bool isVarArg,
2523 const SmallVectorImpl<ISD::InputArg>
2524 &Ins,
2525 DebugLoc dl, SelectionDAG &DAG,
2526 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002527 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002528 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002529 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002530 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002531
Dan Gohman1e93df62010-04-17 14:41:14 +00002532 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002533
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002534 // Used with vargs to acumulate store chains.
2535 std::vector<SDValue> OutChains;
2536
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002537 // Assign locations to all of the incoming arguments.
2538 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002539 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2540 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002541
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002542 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002543 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002544 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002545 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002546
Akira Hatanaka43299772011-05-20 23:22:14 +00002547 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002548
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002549 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002550 CCValAssign &VA = ArgLocs[i];
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002551 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002552 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2553 bool IsRegLoc = VA.isRegLoc();
2554
2555 if (Flags.isByVal()) {
2556 assert(Flags.getByValSize() &&
2557 "ByVal args of size 0 should have been ignored by front-end.");
2558 if (IsO32) {
2559 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2560 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2561 true);
2562 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2563 InVals.push_back(FIN);
2564 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2565 } else // N32/64
2566 LastFI = CopyMips64ByValRegs(MF, Chain, dl, OutChains, DAG, VA, Flags,
2567 MFI, IsRegLoc, InVals, MipsFI,
2568 getPointerTy());
2569 continue;
2570 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002571
2572 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002573 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002574 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002575 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002576 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002577
Owen Anderson825b72b2009-08-11 20:47:22 +00002578 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002579 RC = Mips::CPURegsRegisterClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002580 else if (RegVT == MVT::i64)
2581 RC = Mips::CPU64RegsRegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002582 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002583 RC = Mips::FGR32RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002584 else if (RegVT == MVT::f64)
Akira Hatanakaf40de9d2011-09-26 21:55:17 +00002585 RC = HasMips64 ? Mips::FGR64RegisterClass : Mips::AFGR64RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002586 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002587 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002588
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002589 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002590 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002591 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002592 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002593
2594 // If this is an 8 or 16-bit value, it has been passed promoted
2595 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002596 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002597 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002598 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002599 if (VA.getLocInfo() == CCValAssign::SExt)
2600 Opcode = ISD::AssertSext;
2601 else if (VA.getLocInfo() == CCValAssign::ZExt)
2602 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002603 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002604 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002605 DAG.getValueType(ValVT));
2606 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002607 }
2608
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002609 // Handle floating point arguments passed in integer registers.
2610 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
2611 (RegVT == MVT::i64 && ValVT == MVT::f64))
2612 ArgValue = DAG.getNode(ISD::BITCAST, dl, ValVT, ArgValue);
2613 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
2614 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
2615 getNextIntArgReg(ArgReg), RC);
2616 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
2617 if (!Subtarget->isLittle())
2618 std::swap(ArgValue, ArgValue2);
2619 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2620 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002621 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002622
Dan Gohman98ca4f22009-08-05 01:29:28 +00002623 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002624 } else { // VA.isRegLoc()
2625
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002626 // sanity check
2627 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002628
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002629 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002630 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002631 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002632
2633 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002634 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002635 InVals.push_back(DAG.getLoad(ValVT, dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002636 MachinePointerInfo::getFixedStack(LastFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002637 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002638 }
2639 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002640
2641 // The mips ABIs for returning structs by value requires that we copy
2642 // the sret argument into $v0 for the return. Save the argument into
2643 // a virtual register so that we can access it from the return points.
2644 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2645 unsigned Reg = MipsFI->getSRetReturnReg();
2646 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002647 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002648 MipsFI->setSRetReturnReg(Reg);
2649 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002650 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002651 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002652 }
2653
Akira Hatanakabad53f42011-11-14 19:01:09 +00002654 if (isVarArg) {
2655 unsigned NumOfRegs = IsO32 ? 4 : 8;
2656 const unsigned *ArgRegs = IsO32 ? O32IntRegs : Mips64IntRegs;
2657 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumOfRegs);
2658 int FirstRegSlotOffset = IsO32 ? 0 : -64 ; // offset of $a0's slot.
2659 TargetRegisterClass *RC
2660 = IsO32 ? Mips::CPURegsRegisterClass : Mips::CPU64RegsRegisterClass;
2661 unsigned RegSize = RC->getSize();
2662 int RegSlotOffset = FirstRegSlotOffset + Idx * RegSize;
2663
2664 // Offset of the first variable argument from stack pointer.
2665 int FirstVaArgOffset;
2666
2667 if (IsO32 || (Idx == NumOfRegs)) {
2668 FirstVaArgOffset =
2669 (CCInfo.getNextStackOffset() + RegSize - 1) / RegSize * RegSize;
2670 } else
2671 FirstVaArgOffset = RegSlotOffset;
2672
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002673 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002674 // which is a value necessary to VASTART.
Akira Hatanakabad53f42011-11-14 19:01:09 +00002675 LastFI = MFI->CreateFixedObject(RegSize, FirstVaArgOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002676 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002677
Akira Hatanakabad53f42011-11-14 19:01:09 +00002678 // Copy the integer registers that have not been used for argument passing
2679 // to the argument register save area. For O32, the save area is allocated
2680 // in the caller's stack frame, while for N32/64, it is allocated in the
2681 // callee's stack frame.
2682 for (int StackOffset = RegSlotOffset;
2683 Idx < NumOfRegs; ++Idx, StackOffset += RegSize) {
2684 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegs[Idx], RC);
2685 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2686 MVT::getIntegerVT(RegSize * 8));
2687 LastFI = MFI->CreateFixedObject(RegSize, StackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002688 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2689 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2690 MachinePointerInfo(),
2691 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002692 }
2693 }
2694
Akira Hatanaka43299772011-05-20 23:22:14 +00002695 MipsFI->setLastInArgFI(LastFI);
2696
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002697 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002698 // the size of Ins and InVals. This only happens when on varg functions
2699 if (!OutChains.empty()) {
2700 OutChains.push_back(Chain);
2701 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2702 &OutChains[0], OutChains.size());
2703 }
2704
Dan Gohman98ca4f22009-08-05 01:29:28 +00002705 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002706}
2707
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002708//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002709// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002710//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002711
Dan Gohman98ca4f22009-08-05 01:29:28 +00002712SDValue
2713MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002714 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002715 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002716 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002717 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002718
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002719 // CCValAssign - represent the assignment of
2720 // the return value to a location
2721 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002722
2723 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002724 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2725 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002726
Dan Gohman98ca4f22009-08-05 01:29:28 +00002727 // Analize return values.
2728 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002729
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002730 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002731 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002732 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002733 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002734 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002735 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002736 }
2737
Dan Gohman475871a2008-07-27 21:46:04 +00002738 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002739
2740 // Copy the result values into the output registers.
2741 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2742 CCValAssign &VA = RVLocs[i];
2743 assert(VA.isRegLoc() && "Can only return in registers!");
2744
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002745 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002746 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002747
2748 // guarantee that all emitted copies are
2749 // stuck together, avoiding something bad
2750 Flag = Chain.getValue(1);
2751 }
2752
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002753 // The mips ABIs for returning structs by value requires that we copy
2754 // the sret argument into $v0 for the return. We saved the argument into
2755 // a virtual register in the entry block, so now we copy the value out
2756 // and into $v0.
2757 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2758 MachineFunction &MF = DAG.getMachineFunction();
2759 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2760 unsigned Reg = MipsFI->getSRetReturnReg();
2761
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002762 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002763 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002764 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002765
Dale Johannesena05dca42009-02-04 23:02:30 +00002766 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002767 Flag = Chain.getValue(1);
2768 }
2769
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002770 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002771 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002772 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002773 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002774 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002775 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002776 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002777}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002778
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002779//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002780// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002781//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002782
2783/// getConstraintType - Given a constraint letter, return the type of
2784/// constraint it is for this target.
2785MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002786getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002787{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002788 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002789 // GCC config/mips/constraints.md
2790 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002791 // 'd' : An address register. Equivalent to r
2792 // unless generating MIPS16 code.
2793 // 'y' : Equivalent to r; retained for
2794 // backwards compatibility.
2795 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002796 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002797 switch (Constraint[0]) {
2798 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002799 case 'd':
2800 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002801 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002802 return C_RegisterClass;
2803 break;
2804 }
2805 }
2806 return TargetLowering::getConstraintType(Constraint);
2807}
2808
John Thompson44ab89e2010-10-29 17:29:13 +00002809/// Examine constraint type and operand type and determine a weight value.
2810/// This object must already have been set up with the operand type
2811/// and the current alternative constraint selected.
2812TargetLowering::ConstraintWeight
2813MipsTargetLowering::getSingleConstraintMatchWeight(
2814 AsmOperandInfo &info, const char *constraint) const {
2815 ConstraintWeight weight = CW_Invalid;
2816 Value *CallOperandVal = info.CallOperandVal;
2817 // If we don't have a value, we can't do a match,
2818 // but allow it at the lowest weight.
2819 if (CallOperandVal == NULL)
2820 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002821 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002822 // Look at the constraint type.
2823 switch (*constraint) {
2824 default:
2825 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2826 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002827 case 'd':
2828 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002829 if (type->isIntegerTy())
2830 weight = CW_Register;
2831 break;
2832 case 'f':
2833 if (type->isFloatTy())
2834 weight = CW_Register;
2835 break;
2836 }
2837 return weight;
2838}
2839
Eric Christopher38d64262011-06-29 19:33:04 +00002840/// Given a register class constraint, like 'r', if this corresponds directly
2841/// to an LLVM register class, return a register of 0 and the register class
2842/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002843std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002844getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002845{
2846 if (Constraint.size() == 1) {
2847 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002848 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2849 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002850 case 'r':
2851 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002852 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002853 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002854 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002855 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002856 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2857 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002858 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002859 }
2860 }
2861 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2862}
2863
Dan Gohman6520e202008-10-18 02:06:02 +00002864bool
2865MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2866 // The Mips target isn't yet aware of offsets.
2867 return false;
2868}
Evan Chengeb2f9692009-10-27 19:56:55 +00002869
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002870bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2871 if (VT != MVT::f32 && VT != MVT::f64)
2872 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002873 if (Imm.isNegZero())
2874 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002875 return Imm.isZero();
2876}