blob: 18d7d9a9d0bfc45b237d94634eedcbffe060fe03 [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);
Akira Hatanaka9b944a82011-11-16 22:42:10 +0000130 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000131 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
132 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
133 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
134 setOperationAction(ISD::SELECT, MVT::f32, Custom);
135 setOperationAction(ISD::SELECT, MVT::f64, Custom);
136 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000137 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
138 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000139 setOperationAction(ISD::VASTART, MVT::Other, Custom);
140
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000141 setOperationAction(ISD::SDIV, MVT::i32, Expand);
142 setOperationAction(ISD::SREM, MVT::i32, Expand);
143 setOperationAction(ISD::UDIV, MVT::i32, Expand);
144 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000145 setOperationAction(ISD::SDIV, MVT::i64, Expand);
146 setOperationAction(ISD::SREM, MVT::i64, Expand);
147 setOperationAction(ISD::UDIV, MVT::i64, Expand);
148 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000149
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000150 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
152 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
153 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
154 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
155 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
156 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
157 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
158 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
159 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000160 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000161
Akira Hatanaka56633442011-09-20 23:53:09 +0000162 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000163 setOperationAction(ISD::ROTR, MVT::i32, Expand);
164
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000165 if (!Subtarget->hasMips64r2())
166 setOperationAction(ISD::ROTR, MVT::i64, Expand);
167
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
169 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
170 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000171 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
172 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000173 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000174 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000175 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000176 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
178 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000179 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 setOperationAction(ISD::FLOG, MVT::f32, Expand);
181 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
182 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
183 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000184 setOperationAction(ISD::FMA, MVT::f32, Expand);
185 setOperationAction(ISD::FMA, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000186
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000187 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
188 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Eric Christopher471e4222011-06-08 23:55:35 +0000189
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000190 setOperationAction(ISD::VAARG, MVT::Other, Expand);
191 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
192 setOperationAction(ISD::VAEND, MVT::Other, Expand);
193
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000194 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000195 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
196 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000197
Akira Hatanakadb548262011-07-19 23:30:50 +0000198 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000199 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000200
Eli Friedman4db5aca2011-08-29 18:23:02 +0000201 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
202 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
203
Eli Friedman26689ac2011-08-03 21:06:02 +0000204 setInsertFencesForAtomic(true);
205
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000206 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000207 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000208
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000209 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
211 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000212 }
213
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000214 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000215 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000216
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000217 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000218 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000219
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000220 setTargetDAGCombine(ISD::ADDE);
221 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000222 setTargetDAGCombine(ISD::SDIVREM);
223 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000224 setTargetDAGCombine(ISD::SETCC);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000225 setTargetDAGCombine(ISD::AND);
226 setTargetDAGCombine(ISD::OR);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000227
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000228 setMinFunctionAlignment(2);
229
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000230 setStackPointerRegisterToSaveRestore(Mips::SP);
231 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000232
233 setExceptionPointerRegister(Mips::A0);
234 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000235}
236
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000237bool MipsTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Akira Hatanaka511961a2011-08-17 18:49:18 +0000238 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
Akira Hatanaka7bd19bd2011-10-11 00:27:28 +0000239 return SVT == MVT::i64 || SVT == MVT::i32 || SVT == MVT::i16;
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000240}
241
Duncan Sands28b77e92011-09-06 19:07:46 +0000242EVT MipsTargetLowering::getSetCCResultType(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000243 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000244}
245
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000246// SelectMadd -
247// Transforms a subgraph in CurDAG if the following pattern is found:
248// (addc multLo, Lo0), (adde multHi, Hi0),
249// where,
250// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000251// Lo0: initial value of Lo register
252// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000253// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000254static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000255 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000256 // for the matching to be successful.
257 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
258
259 if (ADDCNode->getOpcode() != ISD::ADDC)
260 return false;
261
262 SDValue MultHi = ADDENode->getOperand(0);
263 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000264 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000265 unsigned MultOpc = MultHi.getOpcode();
266
267 // MultHi and MultLo must be generated by the same node,
268 if (MultLo.getNode() != MultNode)
269 return false;
270
271 // and it must be a multiplication.
272 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
273 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000274
275 // MultLo amd MultHi must be the first and second output of MultNode
276 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000277 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
278 return false;
279
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000280 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000281 // of the values of MultNode, in which case MultNode will be removed in later
282 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000283 // If there exist users other than ADDENode or ADDCNode, this function returns
284 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000285 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000286 // produced.
287 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
288 return false;
289
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000290 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000291 DebugLoc dl = ADDENode->getDebugLoc();
292
293 // create MipsMAdd(u) node
294 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000295
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000296 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
297 MVT::Glue,
298 MultNode->getOperand(0),// Factor 0
299 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000300 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000301 ADDENode->getOperand(1));// Hi0
302
303 // create CopyFromReg nodes
304 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
305 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000306 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000307 Mips::HI, MVT::i32,
308 CopyFromLo.getValue(2));
309
310 // replace uses of adde and addc here
311 if (!SDValue(ADDCNode, 0).use_empty())
312 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
313
314 if (!SDValue(ADDENode, 0).use_empty())
315 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
316
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000317 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000318}
319
320// SelectMsub -
321// Transforms a subgraph in CurDAG if the following pattern is found:
322// (addc Lo0, multLo), (sube Hi0, multHi),
323// where,
324// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000325// Lo0: initial value of Lo register
326// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000327// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000328static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000329 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000330 // for the matching to be successful.
331 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
332
333 if (SUBCNode->getOpcode() != ISD::SUBC)
334 return false;
335
336 SDValue MultHi = SUBENode->getOperand(1);
337 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000338 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000339 unsigned MultOpc = MultHi.getOpcode();
340
341 // MultHi and MultLo must be generated by the same node,
342 if (MultLo.getNode() != MultNode)
343 return false;
344
345 // and it must be a multiplication.
346 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
347 return false;
348
349 // MultLo amd MultHi must be the first and second output of MultNode
350 // respectively.
351 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
352 return false;
353
354 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
355 // of the values of MultNode, in which case MultNode will be removed in later
356 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000357 // If there exist users other than SUBENode or SUBCNode, this function returns
358 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000359 // instruction node rather than a pair of MULT and MSUB instructions being
360 // produced.
361 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
362 return false;
363
364 SDValue Chain = CurDAG->getEntryNode();
365 DebugLoc dl = SUBENode->getDebugLoc();
366
367 // create MipsSub(u) node
368 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
369
370 SDValue MSub = CurDAG->getNode(MultOpc, dl,
371 MVT::Glue,
372 MultNode->getOperand(0),// Factor 0
373 MultNode->getOperand(1),// Factor 1
374 SUBCNode->getOperand(0),// Lo0
375 SUBENode->getOperand(0));// Hi0
376
377 // create CopyFromReg nodes
378 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
379 MSub);
380 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
381 Mips::HI, MVT::i32,
382 CopyFromLo.getValue(2));
383
384 // replace uses of sube and subc here
385 if (!SDValue(SUBCNode, 0).use_empty())
386 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
387
388 if (!SDValue(SUBENode, 0).use_empty())
389 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
390
391 return true;
392}
393
394static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
395 TargetLowering::DAGCombinerInfo &DCI,
396 const MipsSubtarget* Subtarget) {
397 if (DCI.isBeforeLegalize())
398 return SDValue();
399
Akira Hatanakae184fec2011-11-11 04:18:21 +0000400 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
401 SelectMadd(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000402 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000403
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000404 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000405}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000406
407static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
408 TargetLowering::DAGCombinerInfo &DCI,
409 const MipsSubtarget* Subtarget) {
410 if (DCI.isBeforeLegalize())
411 return SDValue();
412
Akira Hatanakae184fec2011-11-11 04:18:21 +0000413 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
414 SelectMsub(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000415 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000416
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000417 return SDValue();
418}
419
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000420static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
421 TargetLowering::DAGCombinerInfo &DCI,
422 const MipsSubtarget* Subtarget) {
423 if (DCI.isBeforeLegalizeOps())
424 return SDValue();
425
Akira Hatanakadda4a072011-10-03 21:06:13 +0000426 EVT Ty = N->getValueType(0);
427 unsigned LO = (Ty == MVT::i32) ? Mips::LO : Mips::LO64;
428 unsigned HI = (Ty == MVT::i32) ? Mips::HI : Mips::HI64;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000429 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
430 MipsISD::DivRemU;
431 DebugLoc dl = N->getDebugLoc();
432
433 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
434 N->getOperand(0), N->getOperand(1));
435 SDValue InChain = DAG.getEntryNode();
436 SDValue InGlue = DivRem;
437
438 // insert MFLO
439 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakadda4a072011-10-03 21:06:13 +0000440 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000441 InGlue);
442 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
443 InChain = CopyFromLo.getValue(1);
444 InGlue = CopyFromLo.getValue(2);
445 }
446
447 // insert MFHI
448 if (N->hasAnyUseOfValue(1)) {
449 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000450 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000451 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
452 }
453
454 return SDValue();
455}
456
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000457static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
458 switch (CC) {
459 default: llvm_unreachable("Unknown fp condition code!");
460 case ISD::SETEQ:
461 case ISD::SETOEQ: return Mips::FCOND_OEQ;
462 case ISD::SETUNE: return Mips::FCOND_UNE;
463 case ISD::SETLT:
464 case ISD::SETOLT: return Mips::FCOND_OLT;
465 case ISD::SETGT:
466 case ISD::SETOGT: return Mips::FCOND_OGT;
467 case ISD::SETLE:
468 case ISD::SETOLE: return Mips::FCOND_OLE;
469 case ISD::SETGE:
470 case ISD::SETOGE: return Mips::FCOND_OGE;
471 case ISD::SETULT: return Mips::FCOND_ULT;
472 case ISD::SETULE: return Mips::FCOND_ULE;
473 case ISD::SETUGT: return Mips::FCOND_UGT;
474 case ISD::SETUGE: return Mips::FCOND_UGE;
475 case ISD::SETUO: return Mips::FCOND_UN;
476 case ISD::SETO: return Mips::FCOND_OR;
477 case ISD::SETNE:
478 case ISD::SETONE: return Mips::FCOND_ONE;
479 case ISD::SETUEQ: return Mips::FCOND_UEQ;
480 }
481}
482
483
484// Returns true if condition code has to be inverted.
485static bool InvertFPCondCode(Mips::CondCode CC) {
486 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
487 return false;
488
489 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
490 return true;
491
492 assert(false && "Illegal Condition Code");
493 return false;
494}
495
496// Creates and returns an FPCmp node from a setcc node.
497// Returns Op if setcc is not a floating point comparison.
498static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
499 // must be a SETCC node
500 if (Op.getOpcode() != ISD::SETCC)
501 return Op;
502
503 SDValue LHS = Op.getOperand(0);
504
505 if (!LHS.getValueType().isFloatingPoint())
506 return Op;
507
508 SDValue RHS = Op.getOperand(1);
509 DebugLoc dl = Op.getDebugLoc();
510
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000511 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
512 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000513 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
514
515 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
516 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
517}
518
519// Creates and returns a CMovFPT/F node.
520static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
521 SDValue False, DebugLoc DL) {
522 bool invert = InvertFPCondCode((Mips::CondCode)
523 cast<ConstantSDNode>(Cond.getOperand(2))
524 ->getSExtValue());
525
526 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
527 True.getValueType(), True, False, Cond);
528}
529
530static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
531 TargetLowering::DAGCombinerInfo &DCI,
532 const MipsSubtarget* Subtarget) {
533 if (DCI.isBeforeLegalizeOps())
534 return SDValue();
535
536 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
537
538 if (Cond.getOpcode() != MipsISD::FPCmp)
539 return SDValue();
540
541 SDValue True = DAG.getConstant(1, MVT::i32);
542 SDValue False = DAG.getConstant(0, MVT::i32);
543
544 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
545}
546
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000547static SDValue PerformANDCombine(SDNode *N, SelectionDAG& DAG,
548 TargetLowering::DAGCombinerInfo &DCI,
549 const MipsSubtarget* Subtarget) {
550 // Pattern match EXT.
551 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
552 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000553 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000554 return SDValue();
555
556 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
557
558 // Op's first operand must be a shift right.
559 if (ShiftRight.getOpcode() != ISD::SRA && ShiftRight.getOpcode() != ISD::SRL)
560 return SDValue();
561
562 // The second operand of the shift must be an immediate.
563 uint64_t Pos;
564 ConstantSDNode *CN;
565 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
566 return SDValue();
567
568 Pos = CN->getZExtValue();
569
570 uint64_t SMPos, SMSize;
571 // Op's second operand must be a shifted mask.
572 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000573 !IsShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000574 return SDValue();
575
576 // Return if the shifted mask does not start at bit 0 or the sum of its size
577 // and Pos exceeds the word's size.
578 if (SMPos != 0 || Pos + SMSize > 32)
579 return SDValue();
580
581 return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), MVT::i32,
582 ShiftRight.getOperand(0),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000583 DAG.getConstant(Pos, MVT::i32),
584 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000585}
586
587static SDValue PerformORCombine(SDNode *N, SelectionDAG& DAG,
588 TargetLowering::DAGCombinerInfo &DCI,
589 const MipsSubtarget* Subtarget) {
590 // Pattern match INS.
591 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
592 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
593 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000594 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000595 return SDValue();
596
597 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
598 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
599 ConstantSDNode *CN;
600
601 // See if Op's first operand matches (and $src1 , mask0).
602 if (And0.getOpcode() != ISD::AND)
603 return SDValue();
604
605 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000606 !IsShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000607 return SDValue();
608
609 // See if Op's second operand matches (and (shl $src, pos), mask1).
610 if (And1.getOpcode() != ISD::AND)
611 return SDValue();
612
613 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000614 !IsShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000615 return SDValue();
616
617 // The shift masks must have the same position and size.
618 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
619 return SDValue();
620
621 SDValue Shl = And1.getOperand(0);
622 if (Shl.getOpcode() != ISD::SHL)
623 return SDValue();
624
625 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
626 return SDValue();
627
628 unsigned Shamt = CN->getZExtValue();
629
630 // Return if the shift amount and the first bit position of mask are not the
631 // same.
632 if (Shamt != SMPos0)
633 return SDValue();
634
635 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), MVT::i32,
636 Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000637 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000638 DAG.getConstant(SMSize0, MVT::i32),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000639 And0.getOperand(0));
640}
641
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000642SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000643 const {
644 SelectionDAG &DAG = DCI.DAG;
645 unsigned opc = N->getOpcode();
646
647 switch (opc) {
648 default: break;
649 case ISD::ADDE:
650 return PerformADDECombine(N, DAG, DCI, Subtarget);
651 case ISD::SUBE:
652 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000653 case ISD::SDIVREM:
654 case ISD::UDIVREM:
655 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000656 case ISD::SETCC:
657 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000658 case ISD::AND:
659 return PerformANDCombine(N, DAG, DCI, Subtarget);
660 case ISD::OR:
661 return PerformORCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000662 }
663
664 return SDValue();
665}
666
Dan Gohman475871a2008-07-27 21:46:04 +0000667SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000668LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000669{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000670 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000671 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000672 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000673 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
674 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000675 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000676 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000677 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
678 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000679 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000680 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000681 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000682 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Akira Hatanakadb548262011-07-19 23:30:50 +0000683 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +0000684 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000685 }
Dan Gohman475871a2008-07-27 21:46:04 +0000686 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000687}
688
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000689//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000690// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000691//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000692
693// AddLiveIn - This helper function adds the specified physical register to the
694// MachineFunction as a live in value. It also creates a corresponding
695// virtual register for it.
696static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000697AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000698{
699 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000700 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
701 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000702 return VReg;
703}
704
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000705// Get fp branch code (not opcode) from condition code.
706static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
707 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
708 return Mips::BRANCH_T;
709
710 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
711 return Mips::BRANCH_F;
712
713 return Mips::BRANCH_INVALID;
714}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000715
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000716/*
Akira Hatanaka14487d42011-06-07 19:28:39 +0000717static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
718 DebugLoc dl,
719 const MipsSubtarget* Subtarget,
720 const TargetInstrInfo *TII,
721 bool isFPCmp, unsigned Opc) {
722 // There is no need to expand CMov instructions if target has
723 // conditional moves.
724 if (Subtarget->hasCondMov())
725 return BB;
726
727 // To "insert" a SELECT_CC instruction, we actually have to insert the
728 // diamond control-flow pattern. The incoming instruction knows the
729 // destination vreg to set, the condition code register to branch on, the
730 // true/false values to select between, and a branch opcode to use.
731 const BasicBlock *LLVM_BB = BB->getBasicBlock();
732 MachineFunction::iterator It = BB;
733 ++It;
734
735 // thisMBB:
736 // ...
737 // TrueVal = ...
738 // setcc r1, r2, r3
739 // bNE r1, r0, copy1MBB
740 // fallthrough --> copy0MBB
741 MachineBasicBlock *thisMBB = BB;
742 MachineFunction *F = BB->getParent();
743 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
744 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
745 F->insert(It, copy0MBB);
746 F->insert(It, sinkMBB);
747
748 // Transfer the remainder of BB and its successor edges to sinkMBB.
749 sinkMBB->splice(sinkMBB->begin(), BB,
750 llvm::next(MachineBasicBlock::iterator(MI)),
751 BB->end());
752 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
753
754 // Next, add the true and fallthrough blocks as its successors.
755 BB->addSuccessor(copy0MBB);
756 BB->addSuccessor(sinkMBB);
757
758 // Emit the right instruction according to the type of the operands compared
759 if (isFPCmp)
760 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
761 else
762 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
763 .addReg(Mips::ZERO).addMBB(sinkMBB);
764
765 // copy0MBB:
766 // %FalseValue = ...
767 // # fallthrough to sinkMBB
768 BB = copy0MBB;
769
770 // Update machine-CFG edges
771 BB->addSuccessor(sinkMBB);
772
773 // sinkMBB:
774 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
775 // ...
776 BB = sinkMBB;
777
778 if (isFPCmp)
779 BuildMI(*BB, BB->begin(), dl,
780 TII->get(Mips::PHI), MI->getOperand(0).getReg())
781 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
782 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
783 else
784 BuildMI(*BB, BB->begin(), dl,
785 TII->get(Mips::PHI), MI->getOperand(0).getReg())
786 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
787 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
788
789 MI->eraseFromParent(); // The pseudo instruction is gone now.
790 return BB;
791}
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000792*/
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000793MachineBasicBlock *
794MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000795 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000796 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000797 default:
798 assert(false && "Unexpected instr type to insert");
799 return NULL;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000800 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000801 case Mips::ATOMIC_LOAD_ADD_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000802 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
803 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000804 case Mips::ATOMIC_LOAD_ADD_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000805 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
806 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000807 case Mips::ATOMIC_LOAD_ADD_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000808 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000809 case Mips::ATOMIC_LOAD_ADD_I64:
810 case Mips::ATOMIC_LOAD_ADD_I64_P8:
811 return EmitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000812
813 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000814 case Mips::ATOMIC_LOAD_AND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000815 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
816 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000817 case Mips::ATOMIC_LOAD_AND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000818 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
819 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000820 case Mips::ATOMIC_LOAD_AND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000821 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000822 case Mips::ATOMIC_LOAD_AND_I64:
823 case Mips::ATOMIC_LOAD_AND_I64_P8:
Akira Hatanaka73866122011-11-12 02:38:12 +0000824 return EmitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000825
826 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000827 case Mips::ATOMIC_LOAD_OR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000828 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
829 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000830 case Mips::ATOMIC_LOAD_OR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000831 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
832 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000833 case Mips::ATOMIC_LOAD_OR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000834 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000835 case Mips::ATOMIC_LOAD_OR_I64:
836 case Mips::ATOMIC_LOAD_OR_I64_P8:
837 return EmitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000838
839 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000840 case Mips::ATOMIC_LOAD_XOR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000841 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
842 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000843 case Mips::ATOMIC_LOAD_XOR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000844 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
845 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000846 case Mips::ATOMIC_LOAD_XOR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000847 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000848 case Mips::ATOMIC_LOAD_XOR_I64:
849 case Mips::ATOMIC_LOAD_XOR_I64_P8:
850 return EmitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000851
852 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000853 case Mips::ATOMIC_LOAD_NAND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000854 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
855 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000856 case Mips::ATOMIC_LOAD_NAND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000857 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
858 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000859 case Mips::ATOMIC_LOAD_NAND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000860 return EmitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000861 case Mips::ATOMIC_LOAD_NAND_I64:
862 case Mips::ATOMIC_LOAD_NAND_I64_P8:
863 return EmitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000864
865 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000866 case Mips::ATOMIC_LOAD_SUB_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000867 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
868 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000869 case Mips::ATOMIC_LOAD_SUB_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000870 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
871 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000872 case Mips::ATOMIC_LOAD_SUB_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000873 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000874 case Mips::ATOMIC_LOAD_SUB_I64:
875 case Mips::ATOMIC_LOAD_SUB_I64_P8:
876 return EmitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000877
878 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000879 case Mips::ATOMIC_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000880 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
881 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000882 case Mips::ATOMIC_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000883 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
884 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000885 case Mips::ATOMIC_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000886 return EmitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000887 case Mips::ATOMIC_SWAP_I64:
888 case Mips::ATOMIC_SWAP_I64_P8:
889 return EmitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000890
891 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000892 case Mips::ATOMIC_CMP_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000893 return EmitAtomicCmpSwapPartword(MI, BB, 1);
894 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000895 case Mips::ATOMIC_CMP_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000896 return EmitAtomicCmpSwapPartword(MI, BB, 2);
897 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000898 case Mips::ATOMIC_CMP_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000899 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000900 case Mips::ATOMIC_CMP_SWAP_I64:
901 case Mips::ATOMIC_CMP_SWAP_I64_P8:
902 return EmitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000903 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000904}
905
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000906// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
907// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
908MachineBasicBlock *
909MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000910 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000911 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000912 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000913
914 MachineFunction *MF = BB->getParent();
915 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000916 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000917 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
918 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000919 unsigned LL, SC, AND, NOR, ZERO, BEQ;
920
921 if (Size == 4) {
922 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
923 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
924 AND = Mips::AND;
925 NOR = Mips::NOR;
926 ZERO = Mips::ZERO;
927 BEQ = Mips::BEQ;
928 }
929 else {
930 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
931 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
932 AND = Mips::AND64;
933 NOR = Mips::NOR64;
934 ZERO = Mips::ZERO_64;
935 BEQ = Mips::BEQ64;
936 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000937
Akira Hatanaka4061da12011-07-19 20:11:17 +0000938 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000939 unsigned Ptr = MI->getOperand(1).getReg();
940 unsigned Incr = MI->getOperand(2).getReg();
941
Akira Hatanaka4061da12011-07-19 20:11:17 +0000942 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
943 unsigned AndRes = RegInfo.createVirtualRegister(RC);
944 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000945
946 // insert new blocks after the current block
947 const BasicBlock *LLVM_BB = BB->getBasicBlock();
948 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
949 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
950 MachineFunction::iterator It = BB;
951 ++It;
952 MF->insert(It, loopMBB);
953 MF->insert(It, exitMBB);
954
955 // Transfer the remainder of BB and its successor edges to exitMBB.
956 exitMBB->splice(exitMBB->begin(), BB,
957 llvm::next(MachineBasicBlock::iterator(MI)),
958 BB->end());
959 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
960
961 // thisMBB:
962 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000963 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000964 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000965 loopMBB->addSuccessor(loopMBB);
966 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000967
968 // loopMBB:
969 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000970 // <binop> storeval, oldval, incr
971 // sc success, storeval, 0(ptr)
972 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000973 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +0000974 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000975 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000976 // and andres, oldval, incr
977 // nor storeval, $0, andres
Akira Hatanaka59068062011-11-11 04:14:30 +0000978 BuildMI(BB, dl, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
979 BuildMI(BB, dl, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000980 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000981 // <binop> storeval, oldval, incr
982 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000983 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000984 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000985 }
Akira Hatanaka59068062011-11-11 04:14:30 +0000986 BuildMI(BB, dl, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
987 BuildMI(BB, dl, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000988
989 MI->eraseFromParent(); // The instruction is gone now.
990
Akira Hatanaka939ece12011-07-19 03:42:13 +0000991 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000992}
993
994MachineBasicBlock *
995MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000996 MachineBasicBlock *BB,
997 unsigned Size, unsigned BinOpcode,
998 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000999 assert((Size == 1 || Size == 2) &&
1000 "Unsupported size for EmitAtomicBinaryPartial.");
1001
1002 MachineFunction *MF = BB->getParent();
1003 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1004 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1005 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1006 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001007 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1008 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001009
1010 unsigned Dest = MI->getOperand(0).getReg();
1011 unsigned Ptr = MI->getOperand(1).getReg();
1012 unsigned Incr = MI->getOperand(2).getReg();
1013
Akira Hatanaka4061da12011-07-19 20:11:17 +00001014 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1015 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001016 unsigned Mask = RegInfo.createVirtualRegister(RC);
1017 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001018 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1019 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001020 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001021 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1022 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1023 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1024 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1025 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001026 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001027 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1028 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1029 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1030 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1031 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001032
1033 // insert new blocks after the current block
1034 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1035 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001036 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001037 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1038 MachineFunction::iterator It = BB;
1039 ++It;
1040 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001041 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001042 MF->insert(It, exitMBB);
1043
1044 // Transfer the remainder of BB and its successor edges to exitMBB.
1045 exitMBB->splice(exitMBB->begin(), BB,
1046 llvm::next(MachineBasicBlock::iterator(MI)),
1047 BB->end());
1048 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1049
Akira Hatanaka81b44112011-07-19 17:09:53 +00001050 BB->addSuccessor(loopMBB);
1051 loopMBB->addSuccessor(loopMBB);
1052 loopMBB->addSuccessor(sinkMBB);
1053 sinkMBB->addSuccessor(exitMBB);
1054
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001055 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001056 // addiu masklsb2,$0,-4 # 0xfffffffc
1057 // and alignedaddr,ptr,masklsb2
1058 // andi ptrlsb2,ptr,3
1059 // sll shiftamt,ptrlsb2,3
1060 // ori maskupper,$0,255 # 0xff
1061 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001062 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001063 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001064
1065 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001066 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1067 .addReg(Mips::ZERO).addImm(-4);
1068 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1069 .addReg(Ptr).addReg(MaskLSB2);
1070 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1071 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1072 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1073 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001074 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1075 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001076 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001077 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001078
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001079
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001080 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001081 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001082 // ll oldval,0(alignedaddr)
1083 // binop binopres,oldval,incr2
1084 // and newval,binopres,mask
1085 // and maskedoldval0,oldval,mask2
1086 // or storeval,maskedoldval0,newval
1087 // sc success,storeval,0(alignedaddr)
1088 // beq success,$0,loopMBB
1089
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001090 // atomic.swap
1091 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001092 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001093 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001094 // and maskedoldval0,oldval,mask2
1095 // or storeval,maskedoldval0,newval
1096 // sc success,storeval,0(alignedaddr)
1097 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001098
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001099 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001100 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001101 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001102 // and andres, oldval, incr2
1103 // nor binopres, $0, andres
1104 // and newval, binopres, mask
1105 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1106 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1107 .addReg(Mips::ZERO).addReg(AndRes);
1108 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001109 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001110 // <binop> binopres, oldval, incr2
1111 // and newval, binopres, mask
1112 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1113 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001114 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001115 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001116 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001117 }
1118
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001119 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001120 .addReg(OldVal).addReg(Mask2);
1121 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001122 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001123 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001124 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001125 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001126 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001127
Akira Hatanaka939ece12011-07-19 03:42:13 +00001128 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001129 // and maskedoldval1,oldval,mask
1130 // srl srlres,maskedoldval1,shiftamt
1131 // sll sllres,srlres,24
1132 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001133 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001134 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001135
Akira Hatanaka4061da12011-07-19 20:11:17 +00001136 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1137 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001138 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1139 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001140 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1141 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001142 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001143 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001144
1145 MI->eraseFromParent(); // The instruction is gone now.
1146
Akira Hatanaka939ece12011-07-19 03:42:13 +00001147 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001148}
1149
1150MachineBasicBlock *
1151MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001152 MachineBasicBlock *BB,
1153 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001154 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001155
1156 MachineFunction *MF = BB->getParent();
1157 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001158 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001159 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1160 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001161 unsigned LL, SC, ZERO, BNE, BEQ;
1162
1163 if (Size == 4) {
1164 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1165 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1166 ZERO = Mips::ZERO;
1167 BNE = Mips::BNE;
1168 BEQ = Mips::BEQ;
1169 }
1170 else {
1171 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1172 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1173 ZERO = Mips::ZERO_64;
1174 BNE = Mips::BNE64;
1175 BEQ = Mips::BEQ64;
1176 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001177
1178 unsigned Dest = MI->getOperand(0).getReg();
1179 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001180 unsigned OldVal = MI->getOperand(2).getReg();
1181 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001182
Akira Hatanaka4061da12011-07-19 20:11:17 +00001183 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001184
1185 // insert new blocks after the current block
1186 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1187 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1188 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1189 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1190 MachineFunction::iterator It = BB;
1191 ++It;
1192 MF->insert(It, loop1MBB);
1193 MF->insert(It, loop2MBB);
1194 MF->insert(It, exitMBB);
1195
1196 // Transfer the remainder of BB and its successor edges to exitMBB.
1197 exitMBB->splice(exitMBB->begin(), BB,
1198 llvm::next(MachineBasicBlock::iterator(MI)),
1199 BB->end());
1200 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1201
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001202 // thisMBB:
1203 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001204 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001205 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001206 loop1MBB->addSuccessor(exitMBB);
1207 loop1MBB->addSuccessor(loop2MBB);
1208 loop2MBB->addSuccessor(loop1MBB);
1209 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001210
1211 // loop1MBB:
1212 // ll dest, 0(ptr)
1213 // bne dest, oldval, exitMBB
1214 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001215 BuildMI(BB, dl, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1216 BuildMI(BB, dl, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001217 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001218
1219 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001220 // sc success, newval, 0(ptr)
1221 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001222 BB = loop2MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001223 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001224 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka59068062011-11-11 04:14:30 +00001225 BuildMI(BB, dl, TII->get(BEQ))
1226 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001227
1228 MI->eraseFromParent(); // The instruction is gone now.
1229
Akira Hatanaka939ece12011-07-19 03:42:13 +00001230 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001231}
1232
1233MachineBasicBlock *
1234MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001235 MachineBasicBlock *BB,
1236 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001237 assert((Size == 1 || Size == 2) &&
1238 "Unsupported size for EmitAtomicCmpSwapPartial.");
1239
1240 MachineFunction *MF = BB->getParent();
1241 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1242 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1243 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1244 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001245 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1246 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001247
1248 unsigned Dest = MI->getOperand(0).getReg();
1249 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001250 unsigned CmpVal = MI->getOperand(2).getReg();
1251 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001252
Akira Hatanaka4061da12011-07-19 20:11:17 +00001253 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1254 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001255 unsigned Mask = RegInfo.createVirtualRegister(RC);
1256 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001257 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1258 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1259 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1260 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1261 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1262 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1263 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1264 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1265 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1266 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1267 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1268 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1269 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1270 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001271
1272 // insert new blocks after the current block
1273 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1274 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1275 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001276 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001277 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1278 MachineFunction::iterator It = BB;
1279 ++It;
1280 MF->insert(It, loop1MBB);
1281 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001282 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001283 MF->insert(It, exitMBB);
1284
1285 // Transfer the remainder of BB and its successor edges to exitMBB.
1286 exitMBB->splice(exitMBB->begin(), BB,
1287 llvm::next(MachineBasicBlock::iterator(MI)),
1288 BB->end());
1289 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1290
Akira Hatanaka81b44112011-07-19 17:09:53 +00001291 BB->addSuccessor(loop1MBB);
1292 loop1MBB->addSuccessor(sinkMBB);
1293 loop1MBB->addSuccessor(loop2MBB);
1294 loop2MBB->addSuccessor(loop1MBB);
1295 loop2MBB->addSuccessor(sinkMBB);
1296 sinkMBB->addSuccessor(exitMBB);
1297
Akira Hatanaka70564a92011-07-19 18:14:26 +00001298 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001299 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001300 // addiu masklsb2,$0,-4 # 0xfffffffc
1301 // and alignedaddr,ptr,masklsb2
1302 // andi ptrlsb2,ptr,3
1303 // sll shiftamt,ptrlsb2,3
1304 // ori maskupper,$0,255 # 0xff
1305 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001306 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001307 // andi maskedcmpval,cmpval,255
1308 // sll shiftedcmpval,maskedcmpval,shiftamt
1309 // andi maskednewval,newval,255
1310 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001311 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001312 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1313 .addReg(Mips::ZERO).addImm(-4);
1314 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1315 .addReg(Ptr).addReg(MaskLSB2);
1316 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1317 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1318 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1319 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001320 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1321 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001322 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001323 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1324 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001325 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1326 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001327 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1328 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001329 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1330 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001331
1332 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001333 // ll oldval,0(alginedaddr)
1334 // and maskedoldval0,oldval,mask
1335 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001336 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001337 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001338 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1339 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001340 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001341 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001342
1343 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001344 // and maskedoldval1,oldval,mask2
1345 // or storeval,maskedoldval1,shiftednewval
1346 // sc success,storeval,0(alignedaddr)
1347 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001348 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001349 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1350 .addReg(OldVal).addReg(Mask2);
1351 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1352 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001353 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001354 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001355 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001356 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001357
Akira Hatanaka939ece12011-07-19 03:42:13 +00001358 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001359 // srl srlres,maskedoldval0,shiftamt
1360 // sll sllres,srlres,24
1361 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001362 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001363 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001364
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001365 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1366 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001367 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1368 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001369 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001370 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001371
1372 MI->eraseFromParent(); // The instruction is gone now.
1373
Akira Hatanaka939ece12011-07-19 03:42:13 +00001374 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001375}
1376
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001377//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001378// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001379//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001380SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001381LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001382{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001383 MachineFunction &MF = DAG.getMachineFunction();
1384 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001385 unsigned SP = IsN64 ? Mips::SP_64 : Mips::SP;
Akira Hatanaka21afc632011-06-21 00:40:49 +00001386
1387 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001388 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1389 "Cannot lower if the alignment of the allocated space is larger than \
1390 that of the stack.");
1391
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001392 SDValue Chain = Op.getOperand(0);
1393 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001394 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001395
1396 // Get a reference from Mips stack pointer
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001397 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, SP, getPointerTy());
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001398
1399 // Subtract the dynamic size from the actual stack size to
1400 // obtain the new stack size.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001401 SDValue Sub = DAG.getNode(ISD::SUB, dl, getPointerTy(), StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001402
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001403 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001404 // must be placed in the stack pointer register.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001405 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, SP, Sub, SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001406
1407 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001408 // value and a chain
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001409 SDVTList VTLs = DAG.getVTList(getPointerTy(), MVT::Other);
Akira Hatanaka21afc632011-06-21 00:40:49 +00001410 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1411 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1412
1413 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001414}
1415
1416SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001417LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001418{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001419 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001420 // the block to branch to if the condition is true.
1421 SDValue Chain = Op.getOperand(0);
1422 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001423 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001424
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001425 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1426
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001427 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001428 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001429 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001430
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001431 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001432 Mips::CondCode CC =
1433 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001434 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001435
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001436 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001437 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001438}
1439
1440SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001441LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001442{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001443 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001444
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001445 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001446 if (Cond.getOpcode() != MipsISD::FPCmp)
1447 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001448
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001449 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1450 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001451}
1452
Dan Gohmand858e902010-04-17 15:26:15 +00001453SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1454 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001455 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001456 DebugLoc dl = Op.getDebugLoc();
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001457 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001458
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001459 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001460 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001461
Chris Lattnerb71b9092009-08-13 06:28:06 +00001462 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001463
Chris Lattnere3736f82009-08-13 05:41:27 +00001464 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001465 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1466 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001467 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001468 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1469 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001470 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001471 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001472 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001473 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1474 MipsII::MO_ABS_HI);
1475 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1476 MipsII::MO_ABS_LO);
1477 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1478 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001479 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001480 }
1481
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001482 EVT ValTy = Op.getValueType();
1483 bool HasGotOfst = (GV->hasInternalLinkage() ||
1484 (GV->hasLocalLinkage() && !isa<Function>(GV)));
1485 unsigned GotFlag = IsN64 ?
1486 (HasGotOfst ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT_DISP) :
1487 MipsII::MO_GOT;
1488 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0, GotFlag);
1489 GA = DAG.getNode(MipsISD::WrapperPIC, dl, ValTy, GA);
1490 SDValue ResNode = DAG.getLoad(ValTy, dl,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001491 DAG.getEntryNode(), GA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001492 false, false, false, 0);
Akira Hatanaka0f843822011-06-07 18:58:42 +00001493 // On functions and global targets not internal linked only
1494 // a load from got/GP is necessary for PIC to work.
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001495 if (!HasGotOfst)
Akira Hatanaka0f843822011-06-07 18:58:42 +00001496 return ResNode;
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001497 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0,
1498 IsN64 ? MipsII::MO_GOT_OFST :
1499 MipsII::MO_ABS_LO);
1500 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, GALo);
1501 return DAG.getNode(ISD::ADD, dl, ValTy, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001502}
1503
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001504SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1505 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001506 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1507 // FIXME there isn't actually debug info here
1508 DebugLoc dl = Op.getDebugLoc();
1509
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001510 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001511 // %hi/%lo relocation
1512 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
1513 MipsII::MO_ABS_HI);
1514 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
1515 MipsII::MO_ABS_LO);
1516 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1517 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1518 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001519 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001520
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001521 EVT ValTy = Op.getValueType();
1522 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1523 unsigned OFSTFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1524 SDValue BAGOTOffset = DAG.getBlockAddress(BA, ValTy, true, GOTFlag);
1525 BAGOTOffset = DAG.getNode(MipsISD::WrapperPIC, dl, ValTy, BAGOTOffset);
1526 SDValue BALOOffset = DAG.getBlockAddress(BA, ValTy, true, OFSTFlag);
1527 SDValue Load = DAG.getLoad(ValTy, dl,
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001528 DAG.getEntryNode(), BAGOTOffset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001529 MachinePointerInfo(), false, false, false, 0);
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001530 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, BALOOffset);
1531 return DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001532}
1533
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001534SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001535LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001536{
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001537 // If the relocation model is PIC, use the General Dynamic TLS Model,
1538 // otherwise use the Initial Exec or Local Exec TLS Model.
1539 // TODO: implement Local Dynamic TLS model
1540
1541 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1542 DebugLoc dl = GA->getDebugLoc();
1543 const GlobalValue *GV = GA->getGlobal();
1544 EVT PtrVT = getPointerTy();
1545
1546 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1547 // General Dynamic TLS Model
1548 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001549 0, MipsII::MO_TLSGD);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001550 SDValue Tlsgd = DAG.getNode(MipsISD::TlsGd, dl, MVT::i32, TGA);
1551 SDValue GP = DAG.getRegister(Mips::GP, MVT::i32);
1552 SDValue Argument = DAG.getNode(ISD::ADD, dl, MVT::i32, GP, Tlsgd);
1553
1554 ArgListTy Args;
1555 ArgListEntry Entry;
1556 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001557 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001558 Args.push_back(Entry);
1559 std::pair<SDValue, SDValue> CallResult =
1560 LowerCallTo(DAG.getEntryNode(),
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001561 (Type *) Type::getInt32Ty(*DAG.getContext()),
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001562 false, false, false, false, 0, CallingConv::C, false, true,
1563 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG,
1564 dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001565
1566 return CallResult.first;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001567 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001568
1569 SDValue Offset;
1570 if (GV->isDeclaration()) {
1571 // Initial Exec TLS Model
1572 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1573 MipsII::MO_GOTTPREL);
1574 Offset = DAG.getLoad(MVT::i32, dl,
1575 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001576 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001577 } else {
1578 // Local Exec TLS Model
1579 SDVTList VTs = DAG.getVTList(MVT::i32);
1580 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1581 MipsII::MO_TPREL_HI);
1582 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1583 MipsII::MO_TPREL_LO);
1584 SDValue Hi = DAG.getNode(MipsISD::TprelHi, dl, VTs, &TGAHi, 1);
1585 SDValue Lo = DAG.getNode(MipsISD::TprelLo, dl, MVT::i32, TGALo);
1586 Offset = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1587 }
1588
1589 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1590 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001591}
1592
1593SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001594LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001595{
Dan Gohman475871a2008-07-27 21:46:04 +00001596 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001597 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +00001598 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001599 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001600 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001601 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001602
Owen Andersone50ed302009-08-10 22:56:29 +00001603 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001604 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001605
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001606 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
1607
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +00001608 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001609 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001610 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001611 } else {// Emit Load from Global Pointer
1612 JTI = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, JTI);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001613 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
1614 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001615 false, false, false, 0);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001616 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001617
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001618 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1619 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001620 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001621 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001622
1623 return ResNode;
1624}
1625
Dan Gohman475871a2008-07-27 21:46:04 +00001626SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001627LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001628{
Dan Gohman475871a2008-07-27 21:46:04 +00001629 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001630 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001631 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001632 // FIXME there isn't actually debug info here
1633 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001634
1635 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001636 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001637 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001638 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001639 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001640 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001641 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1642 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001643 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001644
1645 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001646 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001647 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001648 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001649 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001650 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1651 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001652 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001653 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001654 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001655 N->getOffset(), MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001656 CP = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001657 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001658 CP, MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001659 false, false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001660 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001661 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001662 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001663 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
1664 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001665
1666 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001667}
1668
Dan Gohmand858e902010-04-17 15:26:15 +00001669SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001670 MachineFunction &MF = DAG.getMachineFunction();
1671 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1672
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001673 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001674 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1675 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001676
1677 // vastart just stores the address of the VarArgsFrameIndex slot into the
1678 // memory location argument.
1679 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001680 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1681 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +00001682 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001683}
1684
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001685static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
1686 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
1687 DebugLoc dl = Op.getDebugLoc();
1688 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
1689 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
1690 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
1691 DAG.getConstant(0x7fffffff, MVT::i32));
1692 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
1693 DAG.getConstant(0x80000000, MVT::i32));
1694 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1695 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
1696}
1697
1698static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001699 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001700 // Use ext/ins instructions if target architecture is Mips32r2.
1701 // Eliminate redundant mfc1 and mtc1 instructions.
1702 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001703
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001704 if (!isLittle)
1705 std::swap(LoIdx, HiIdx);
1706
1707 DebugLoc dl = Op.getDebugLoc();
1708 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1709 Op.getOperand(0),
1710 DAG.getConstant(LoIdx, MVT::i32));
1711 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1712 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1713 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1714 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1715 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1716 DAG.getConstant(0x7fffffff, MVT::i32));
1717 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1718 DAG.getConstant(0x80000000, MVT::i32));
1719 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1720
1721 if (!isLittle)
1722 std::swap(Word0, Word1);
1723
1724 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1725}
1726
1727SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1728 const {
1729 EVT Ty = Op.getValueType();
1730
1731 assert(Ty == MVT::f32 || Ty == MVT::f64);
1732
1733 if (Ty == MVT::f32)
1734 return LowerFCOPYSIGN32(Op, DAG);
1735 else
1736 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
1737}
1738
Akira Hatanaka2e591472011-06-02 00:24:44 +00001739SDValue MipsTargetLowering::
1740LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001741 // check the depth
1742 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001743 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001744
1745 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1746 MFI->setFrameAddressIsTaken(true);
1747 EVT VT = Op.getValueType();
1748 DebugLoc dl = Op.getDebugLoc();
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001749 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
1750 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001751 return FrameAddr;
1752}
1753
Akira Hatanakadb548262011-07-19 23:30:50 +00001754// TODO: set SType according to the desired memory barrier behavior.
1755SDValue MipsTargetLowering::LowerMEMBARRIER(SDValue Op,
1756 SelectionDAG& DAG) const {
1757 unsigned SType = 0;
1758 DebugLoc dl = Op.getDebugLoc();
1759 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1760 DAG.getConstant(SType, MVT::i32));
1761}
1762
Eli Friedman14648462011-07-27 22:21:52 +00001763SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1764 SelectionDAG& DAG) const {
1765 // FIXME: Need pseudo-fence for 'singlethread' fences
1766 // FIXME: Set SType for weaker fences where supported/appropriate.
1767 unsigned SType = 0;
1768 DebugLoc dl = Op.getDebugLoc();
1769 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1770 DAG.getConstant(SType, MVT::i32));
1771}
1772
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001773//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001774// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001775//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001776
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001777//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001778// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001779// Mips O32 ABI rules:
1780// ---
1781// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001782// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001783// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001784// f64 - Only passed in two aliased f32 registers if no int reg has been used
1785// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001786// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1787// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001788//
1789// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001790//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001791
Duncan Sands1e96bab2010-11-04 10:49:57 +00001792static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001793 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001794 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1795
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001796 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001797
1798 static const unsigned IntRegs[] = {
1799 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1800 };
1801 static const unsigned F32Regs[] = {
1802 Mips::F12, Mips::F14
1803 };
1804 static const unsigned F64Regs[] = {
1805 Mips::D6, Mips::D7
1806 };
1807
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001808 // ByVal Args
1809 if (ArgFlags.isByVal()) {
1810 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1811 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1812 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1813 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1814 r < std::min(IntRegsSize, NextReg); ++r)
1815 State.AllocateReg(IntRegs[r]);
1816 return false;
1817 }
1818
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001819 // Promote i8 and i16
1820 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1821 LocVT = MVT::i32;
1822 if (ArgFlags.isSExt())
1823 LocInfo = CCValAssign::SExt;
1824 else if (ArgFlags.isZExt())
1825 LocInfo = CCValAssign::ZExt;
1826 else
1827 LocInfo = CCValAssign::AExt;
1828 }
1829
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001830 unsigned Reg;
1831
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001832 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1833 // is true: function is vararg, argument is 3rd or higher, there is previous
1834 // argument which is not f32 or f64.
1835 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1836 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001837 unsigned OrigAlign = ArgFlags.getOrigAlign();
1838 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001839
1840 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001841 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001842 // If this is the first part of an i64 arg,
1843 // the allocated register must be either A0 or A2.
1844 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1845 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001846 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001847 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1848 // Allocate int register and shadow next int register. If first
1849 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001850 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1851 if (Reg == Mips::A1 || Reg == Mips::A3)
1852 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1853 State.AllocateReg(IntRegs, IntRegsSize);
1854 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001855 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1856 // we are guaranteed to find an available float register
1857 if (ValVT == MVT::f32) {
1858 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1859 // Shadow int register
1860 State.AllocateReg(IntRegs, IntRegsSize);
1861 } else {
1862 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1863 // Shadow int registers
1864 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1865 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1866 State.AllocateReg(IntRegs, IntRegsSize);
1867 State.AllocateReg(IntRegs, IntRegsSize);
1868 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001869 } else
1870 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001871
Akira Hatanakad37776d2011-05-20 21:39:54 +00001872 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1873 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1874
1875 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001876 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001877 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001878 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001879
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001880 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001881}
1882
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00001883static const unsigned Mips64IntRegs[8] =
1884 {Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
1885 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64};
1886static const unsigned Mips64DPRegs[8] =
1887 {Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
1888 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64};
1889
1890static bool CC_Mips64Byval(unsigned ValNo, MVT ValVT, MVT LocVT,
1891 CCValAssign::LocInfo LocInfo,
1892 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1893 unsigned Align = std::max(ArgFlags.getByValAlign(), (unsigned)8);
1894 unsigned Size = (ArgFlags.getByValSize() + 7) / 8 * 8;
1895 unsigned FirstIdx = State.getFirstUnallocated(Mips64IntRegs, 8);
1896
1897 assert(Align <= 16 && "Cannot handle alignments larger than 16.");
1898
1899 // If byval is 16-byte aligned, the first arg register must be even.
1900 if ((Align == 16) && (FirstIdx % 2)) {
1901 State.AllocateReg(Mips64IntRegs[FirstIdx], Mips64DPRegs[FirstIdx]);
1902 ++FirstIdx;
1903 }
1904
1905 // Mark the registers allocated.
1906 for (unsigned I = FirstIdx; Size && (I < 8); Size -= 8, ++I)
1907 State.AllocateReg(Mips64IntRegs[I], Mips64DPRegs[I]);
1908
1909 // Allocate space on caller's stack.
1910 unsigned Offset = State.AllocateStack(Size, Align);
1911
1912 if (FirstIdx < 8)
1913 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Mips64IntRegs[FirstIdx],
1914 LocVT, LocInfo));
1915 else
1916 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
1917
1918 return true;
1919}
1920
1921#include "MipsGenCallingConv.inc"
1922
Akira Hatanaka49617092011-11-14 19:02:54 +00001923static void
1924AnalyzeMips64CallOperands(CCState CCInfo,
1925 const SmallVectorImpl<ISD::OutputArg> &Outs) {
1926 unsigned NumOps = Outs.size();
1927 for (unsigned i = 0; i != NumOps; ++i) {
1928 MVT ArgVT = Outs[i].VT;
1929 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
1930 bool R;
1931
1932 if (Outs[i].IsFixed)
1933 R = CC_MipsN(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
1934 else
1935 R = CC_MipsN_VarArg(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
1936
Akira Hatanaka49617092011-11-14 19:02:54 +00001937 if (R) {
Benjamin Kramer6296ee32011-11-14 19:51:48 +00001938#ifndef NDEBUG
Akira Hatanaka49617092011-11-14 19:02:54 +00001939 dbgs() << "Call operand #" << i << " has unhandled type "
1940 << EVT(ArgVT).getEVTString();
1941#endif
1942 llvm_unreachable(0);
1943 }
1944 }
1945}
1946
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001947//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001948// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001949//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001950
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001951static const unsigned O32IntRegsSize = 4;
1952
1953static const unsigned O32IntRegs[] = {
1954 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1955};
1956
Akira Hatanaka373e3a42011-09-23 00:58:33 +00001957// Return next O32 integer argument register.
1958static unsigned getNextIntArgReg(unsigned Reg) {
1959 assert((Reg == Mips::A0) || (Reg == Mips::A2));
1960 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
1961}
1962
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001963// Write ByVal Arg to arg registers and stack.
1964static void
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001965WriteByValArg(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001966 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1967 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1968 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001969 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001970 MVT PtrType, bool isLittle) {
1971 unsigned LocMemOffset = VA.getLocMemOffset();
1972 unsigned Offset = 0;
1973 uint32_t RemainingSize = Flags.getByValSize();
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001974 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001975
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001976 // Copy the first 4 words of byval arg to registers A0 - A3.
1977 // FIXME: Use a stricter alignment if it enables better optimization in passes
1978 // run later.
1979 for (; RemainingSize >= 4 && LocMemOffset < 4 * 4;
1980 Offset += 4, RemainingSize -= 4, LocMemOffset += 4) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001981 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001982 DAG.getConstant(Offset, MVT::i32));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001983 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1984 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001985 false, false, false, std::min(ByValAlign,
1986 (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001987 MemOpChains.push_back(LoadVal.getValue(1));
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001988 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001989 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1990 }
1991
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001992 if (RemainingSize == 0)
1993 return;
1994
1995 // If there still is a register available for argument passing, write the
1996 // remaining part of the structure to it using subword loads and shifts.
1997 if (LocMemOffset < 4 * 4) {
1998 assert(RemainingSize <= 3 && RemainingSize >= 1 &&
1999 "There must be one to three bytes remaining.");
2000 unsigned LoadSize = (RemainingSize == 3 ? 2 : RemainingSize);
2001 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2002 DAG.getConstant(Offset, MVT::i32));
2003 unsigned Alignment = std::min(ByValAlign, (unsigned )4);
2004 SDValue LoadVal = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2005 LoadPtr, MachinePointerInfo(),
2006 MVT::getIntegerVT(LoadSize * 8), false,
2007 false, Alignment);
2008 MemOpChains.push_back(LoadVal.getValue(1));
2009
2010 // If target is big endian, shift it to the most significant half-word or
2011 // byte.
2012 if (!isLittle)
2013 LoadVal = DAG.getNode(ISD::SHL, dl, MVT::i32, LoadVal,
2014 DAG.getConstant(32 - LoadSize * 8, MVT::i32));
2015
2016 Offset += LoadSize;
2017 RemainingSize -= LoadSize;
2018
2019 // Read second subword if necessary.
2020 if (RemainingSize != 0) {
2021 assert(RemainingSize == 1 && "There must be one byte remaining.");
2022 LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2023 DAG.getConstant(Offset, MVT::i32));
2024 unsigned Alignment = std::min(ByValAlign, (unsigned )2);
2025 SDValue Subword = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2026 LoadPtr, MachinePointerInfo(),
2027 MVT::i8, false, false, Alignment);
2028 MemOpChains.push_back(Subword.getValue(1));
2029 // Insert the loaded byte to LoadVal.
2030 // FIXME: Use INS if supported by target.
2031 unsigned ShiftAmt = isLittle ? 16 : 8;
2032 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i32, Subword,
2033 DAG.getConstant(ShiftAmt, MVT::i32));
2034 LoadVal = DAG.getNode(ISD::OR, dl, MVT::i32, LoadVal, Shift);
2035 }
2036
2037 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
2038 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
2039 return;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002040 }
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002041
2042 // Create a fixed object on stack at offset LocMemOffset and copy
2043 // remaining part of byval arg to it using memcpy.
2044 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2045 DAG.getConstant(Offset, MVT::i32));
2046 LastFI = MFI->CreateFixedObject(RemainingSize, LocMemOffset, true);
2047 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002048 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2049 DAG.getConstant(RemainingSize, MVT::i32),
2050 std::min(ByValAlign, (unsigned)4),
2051 /*isVolatile=*/false, /*AlwaysInline=*/false,
2052 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002053}
2054
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002055// Copy Mips64 byVal arg to registers and stack.
2056void static
2057PassByValArg64(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
2058 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
2059 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
2060 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
2061 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2062 EVT PtrTy, bool isLittle) {
2063 unsigned ByValSize = Flags.getByValSize();
2064 unsigned Alignment = std::min(Flags.getByValAlign(), (unsigned)8);
2065 bool IsRegLoc = VA.isRegLoc();
2066 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
2067 unsigned LocMemOffset = 0;
Akira Hatanaka16040852011-11-15 18:42:25 +00002068 unsigned MemCpySize = ByValSize;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002069
2070 if (!IsRegLoc)
2071 LocMemOffset = VA.getLocMemOffset();
2072 else {
2073 const unsigned *Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8,
2074 VA.getLocReg());
2075 const unsigned *RegEnd = Mips64IntRegs + 8;
2076
2077 // Copy double words to registers.
2078 for (; (Reg != RegEnd) && (ByValSize >= Offset + 8); ++Reg, Offset += 8) {
2079 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2080 DAG.getConstant(Offset, PtrTy));
2081 SDValue LoadVal = DAG.getLoad(MVT::i64, dl, Chain, LoadPtr,
2082 MachinePointerInfo(), false, false, false,
2083 Alignment);
2084 MemOpChains.push_back(LoadVal.getValue(1));
2085 RegsToPass.push_back(std::make_pair(*Reg, LoadVal));
2086 }
2087
Akira Hatanaka16040852011-11-15 18:42:25 +00002088 // Return if the struct has been fully copied.
2089 if (!(MemCpySize = ByValSize - Offset))
2090 return;
2091
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002092 // If there is an argument register available, copy the remainder of the
2093 // byval argument with sub-doubleword loads and shifts.
Akira Hatanaka16040852011-11-15 18:42:25 +00002094 if (Reg != RegEnd) {
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002095 assert((ByValSize < Offset + 8) &&
2096 "Size of the remainder should be smaller than 8-byte.");
2097 SDValue Val;
2098 for (unsigned LoadSize = 4; Offset < ByValSize; LoadSize /= 2) {
2099 unsigned RemSize = ByValSize - Offset;
2100
2101 if (RemSize < LoadSize)
2102 continue;
2103
2104 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2105 DAG.getConstant(Offset, PtrTy));
2106 SDValue LoadVal =
2107 DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i64, Chain, LoadPtr,
2108 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
2109 false, false, Alignment);
2110 MemOpChains.push_back(LoadVal.getValue(1));
2111
2112 // Offset in number of bits from double word boundary.
2113 unsigned OffsetDW = (Offset % 8) * 8;
2114 unsigned Shamt = isLittle ? OffsetDW : 64 - (OffsetDW + LoadSize * 8);
2115 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i64, LoadVal,
2116 DAG.getConstant(Shamt, MVT::i32));
2117
2118 Val = Val.getNode() ? DAG.getNode(ISD::OR, dl, MVT::i64, Val, Shift) :
2119 Shift;
2120 Offset += LoadSize;
2121 Alignment = std::min(Alignment, LoadSize);
2122 }
2123
2124 RegsToPass.push_back(std::make_pair(*Reg, Val));
2125 return;
2126 }
2127 }
2128
Akira Hatanaka16040852011-11-15 18:42:25 +00002129 assert(MemCpySize && "MemCpySize must not be zero.");
2130
2131 // Create a fixed object on stack at offset LocMemOffset and copy
2132 // remainder of byval arg to it with memcpy.
2133 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2134 DAG.getConstant(Offset, PtrTy));
2135 LastFI = MFI->CreateFixedObject(MemCpySize, LocMemOffset, true);
2136 SDValue Dst = DAG.getFrameIndex(LastFI, PtrTy);
2137 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2138 DAG.getConstant(MemCpySize, PtrTy), Alignment,
2139 /*isVolatile=*/false, /*AlwaysInline=*/false,
2140 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002141}
2142
Dan Gohman98ca4f22009-08-05 01:29:28 +00002143/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002144/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002145/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002146SDValue
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002147MipsTargetLowering::LowerCall(SDValue InChain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002148 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002149 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002150 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002151 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002152 const SmallVectorImpl<ISD::InputArg> &Ins,
2153 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002154 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002155 // MIPs target does not yet support tail call optimization.
2156 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002157
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002158 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002159 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002160 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002161 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00002162 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002163
2164 // Analyze operands of the call, assigning locations to each operand.
2165 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002166 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2167 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002168
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002169 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002170 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanaka49617092011-11-14 19:02:54 +00002171 else if (HasMips64)
2172 AnalyzeMips64CallOperands(CCInfo, Outs);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00002173 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002174 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002175
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002176 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002177 unsigned NextStackOffset = CCInfo.getNextStackOffset();
2178
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002179 // Chain is the output chain of the last Load/Store or CopyToReg node.
2180 // ByValChain is the output chain of the last Memcpy node created for copying
2181 // byval arguments to the stack.
2182 SDValue Chain, CallSeqStart, ByValChain;
2183 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
2184 Chain = CallSeqStart = DAG.getCALLSEQ_START(InChain, NextStackOffsetVal);
2185 ByValChain = InChain;
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002186
2187 // If this is the first call, create a stack frame object that points to
2188 // a location to which .cprestore saves $gp.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002189 if (IsO32 && IsPIC && !MipsFI->getGPFI())
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002190 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
2191
Akira Hatanaka21afc632011-06-21 00:40:49 +00002192 // Get the frame index of the stack frame object that points to the location
2193 // of dynamically allocated area on the stack.
2194 int DynAllocFI = MipsFI->getDynAllocFI();
2195
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002196 // Update size of the maximum argument space.
2197 // For O32, a minimum of four words (16 bytes) of argument space is
2198 // allocated.
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002199 if (IsO32)
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002200 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
2201
2202 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
2203
2204 if (MaxCallFrameSize < NextStackOffset) {
2205 MipsFI->setMaxCallFrameSize(NextStackOffset);
2206
Akira Hatanaka21afc632011-06-21 00:40:49 +00002207 // Set the offsets relative to $sp of the $gp restore slot and dynamically
2208 // allocated stack space. These offsets must be aligned to a boundary
2209 // determined by the stack alignment of the ABI.
2210 unsigned StackAlignment = TFL->getStackAlignment();
2211 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
2212 StackAlignment * StackAlignment;
2213
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002214 if (MipsFI->needGPSaveRestore())
Akira Hatanaka21afc632011-06-21 00:40:49 +00002215 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
2216
2217 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002218 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002219
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002220 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002221 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
2222 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002223
Eric Christopher471e4222011-06-08 23:55:35 +00002224 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00002225
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002226 // Walk the register/memloc assignments, inserting copies/loads.
2227 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002228 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002229 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002230 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002231 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2232
2233 // ByVal Arg.
2234 if (Flags.isByVal()) {
2235 assert(Flags.getByValSize() &&
2236 "ByVal args of size 0 should have been ignored by front-end.");
2237 if (IsO32)
2238 WriteByValArg(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2239 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2240 Subtarget->isLittle());
2241 else
2242 PassByValArg64(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2243 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2244 Subtarget->isLittle());
2245 continue;
2246 }
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002247
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002248 // Promote the value if needed.
2249 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002250 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002251 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002252 if (VA.isRegLoc()) {
2253 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
2254 (ValVT == MVT::f64 && LocVT == MVT::i64))
2255 Arg = DAG.getNode(ISD::BITCAST, dl, LocVT, Arg);
2256 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002257 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2258 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002259 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2260 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002261 if (!Subtarget->isLittle())
2262 std::swap(Lo, Hi);
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002263 unsigned LocRegLo = VA.getLocReg();
2264 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2265 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2266 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002267 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002268 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002269 }
2270 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002271 case CCValAssign::SExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002272 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002273 break;
2274 case CCValAssign::ZExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002275 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002276 break;
2277 case CCValAssign::AExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002278 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002279 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002280 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002281
2282 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002283 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002284 if (VA.isRegLoc()) {
2285 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002286 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002287 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002288
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002289 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002290 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002291
Chris Lattnere0b12152008-03-17 06:57:02 +00002292 // Create the frame index object for this incoming parameter
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002293 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002294 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00002295 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00002296
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002297 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002298 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00002299 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
2300 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00002301 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002302 }
2303
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002304 // Extend range of indices of frame objects for outgoing arguments that were
2305 // created during this function call. Skip this step if no such objects were
2306 // created.
2307 if (LastFI)
2308 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2309
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002310 // If a memcpy has been created to copy a byval arg to a stack, replace the
2311 // chain input of CallSeqStart with ByValChain.
2312 if (InChain != ByValChain)
2313 DAG.UpdateNodeOperands(CallSeqStart.getNode(), ByValChain,
2314 NextStackOffsetVal);
2315
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002316 // Transform all store nodes into one single node because all store
2317 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002318 if (!MemOpChains.empty())
2319 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002320 &MemOpChains[0], MemOpChains.size());
2321
Bill Wendling056292f2008-09-16 21:48:12 +00002322 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002323 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2324 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002325 unsigned char OpFlag;
2326 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002327 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002328 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002329
2330 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002331 if (IsPICCall && G->getGlobal()->hasInternalLinkage()) {
2332 OpFlag = IsO32 ? MipsII::MO_GOT : MipsII::MO_GOT_PAGE;
2333 unsigned char LoFlag = IsO32 ? MipsII::MO_ABS_LO : MipsII::MO_GOT_OFST;
2334 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(), 0,
2335 OpFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002336 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002337 0, LoFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002338 } else {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002339 OpFlag = IsPICCall ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002340 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2341 getPointerTy(), 0, OpFlag);
2342 }
2343
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002344 LoadSymAddr = true;
2345 }
2346 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002347 if (IsN64 || (!IsO32 && IsPIC))
2348 OpFlag = MipsII::MO_GOT_DISP;
2349 else if (!IsPIC) // !N64 && static
2350 OpFlag = MipsII::MO_NO_FLAG;
2351 else // O32 & PIC
2352 OpFlag = MipsII::MO_GOT_CALL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002353 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002354 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002355 LoadSymAddr = true;
2356 }
2357
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002358 SDValue InFlag;
2359
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002360 // Create nodes that load address of callee and copy it to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002361 if (IsPICCall) {
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002362 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002363 // Load callee address
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002364 Callee = DAG.getNode(MipsISD::WrapperPIC, dl, getPointerTy(), Callee);
2365 SDValue LoadValue = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
2366 Callee, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002367 false, false, false, 0);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002368
2369 // Use GOT+LO if callee has internal linkage.
2370 if (CalleeLo.getNode()) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002371 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, getPointerTy(), CalleeLo);
2372 Callee = DAG.getNode(ISD::ADD, dl, getPointerTy(), LoadValue, Lo);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002373 } else
2374 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002375 }
2376
2377 // copy to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002378 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
2379 Chain = DAG.getCopyToReg(Chain, dl, T9Reg, Callee, SDValue(0, 0));
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002380 InFlag = Chain.getValue(1);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002381 Callee = DAG.getRegister(T9Reg, getPointerTy());
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002382 }
Bill Wendling056292f2008-09-16 21:48:12 +00002383
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002384 // Build a sequence of copy-to-reg nodes chained together with token
2385 // chain and flag operands which copy the outgoing args into registers.
2386 // The InFlag in necessary since all emitted instructions must be
2387 // stuck together.
2388 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2389 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2390 RegsToPass[i].second, InFlag);
2391 InFlag = Chain.getValue(1);
2392 }
2393
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002394 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002395 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002396 //
2397 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002398 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002399 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002400 Ops.push_back(Chain);
2401 Ops.push_back(Callee);
2402
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002403 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002404 // known live into the call.
2405 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2406 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2407 RegsToPass[i].second.getValueType()));
2408
Gabor Greifba36cb52008-08-28 21:40:38 +00002409 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002410 Ops.push_back(InFlag);
2411
Dale Johannesen33c960f2009-02-04 20:06:27 +00002412 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002413 InFlag = Chain.getValue(1);
2414
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002415 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002416 Chain = DAG.getCALLSEQ_END(Chain,
2417 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002418 DAG.getIntPtrConstant(0, true), InFlag);
2419 InFlag = Chain.getValue(1);
2420
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002421 // Handle result values, copying them out of physregs into vregs that we
2422 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002423 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2424 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002425}
2426
Dan Gohman98ca4f22009-08-05 01:29:28 +00002427/// LowerCallResult - Lower the result values of a call into the
2428/// appropriate copies out of appropriate physical registers.
2429SDValue
2430MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002431 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002432 const SmallVectorImpl<ISD::InputArg> &Ins,
2433 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002434 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002435 // Assign locations to each value returned by this call.
2436 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002437 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2438 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002439
Dan Gohman98ca4f22009-08-05 01:29:28 +00002440 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002441
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002442 // Copy all of the result registers out of their specified physreg.
2443 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002444 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002445 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002446 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002447 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002448 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002449
Dan Gohman98ca4f22009-08-05 01:29:28 +00002450 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002451}
2452
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002453//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002454// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002455//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002456static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2457 std::vector<SDValue>& OutChains,
2458 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2459 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2460 unsigned LocMem = VA.getLocMemOffset();
2461 unsigned FirstWord = LocMem / 4;
2462
2463 // copy register A0 - A3 to frame object
2464 for (unsigned i = 0; i < NumWords; ++i) {
2465 unsigned CurWord = FirstWord + i;
2466 if (CurWord >= O32IntRegsSize)
2467 break;
2468
2469 unsigned SrcReg = O32IntRegs[CurWord];
2470 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2471 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2472 DAG.getConstant(i * 4, MVT::i32));
2473 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2474 StorePtr, MachinePointerInfo(), false,
2475 false, 0);
2476 OutChains.push_back(Store);
2477 }
2478}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002479
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002480// Create frame object on stack and copy registers used for byval passing to it.
2481static unsigned
2482CopyMips64ByValRegs(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2483 std::vector<SDValue>& OutChains, SelectionDAG &DAG,
2484 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2485 MachineFrameInfo *MFI, bool IsRegLoc,
2486 SmallVectorImpl<SDValue> &InVals, MipsFunctionInfo *MipsFI,
2487 EVT PtrTy) {
2488 const unsigned *Reg = Mips64IntRegs + 8;
2489 int FOOffset; // Frame object offset from virtual frame pointer.
2490
2491 if (IsRegLoc) {
2492 Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8, VA.getLocReg());
2493 FOOffset = (Reg - Mips64IntRegs) * 8 - 8 * 8;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002494 }
2495 else
2496 FOOffset = VA.getLocMemOffset();
2497
2498 // Create frame object.
2499 unsigned NumRegs = (Flags.getByValSize() + 7) / 8;
2500 unsigned LastFI = MFI->CreateFixedObject(NumRegs * 8, FOOffset, true);
2501 SDValue FIN = DAG.getFrameIndex(LastFI, PtrTy);
2502 InVals.push_back(FIN);
2503
2504 // Copy arg registers.
2505 for (unsigned I = 0; (Reg != Mips64IntRegs + 8) && (I < NumRegs);
2506 ++Reg, ++I) {
2507 unsigned VReg = AddLiveIn(MF, *Reg, Mips::CPU64RegsRegisterClass);
2508 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, PtrTy, FIN,
2509 DAG.getConstant(I * 8, PtrTy));
2510 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(VReg, MVT::i64),
2511 StorePtr, MachinePointerInfo(), false,
2512 false, 0);
2513 OutChains.push_back(Store);
2514 }
2515
2516 return LastFI;
2517}
2518
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002519/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002520/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002521SDValue
2522MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002523 CallingConv::ID CallConv,
2524 bool isVarArg,
2525 const SmallVectorImpl<ISD::InputArg>
2526 &Ins,
2527 DebugLoc dl, SelectionDAG &DAG,
2528 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002529 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002530 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002531 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002532 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002533
Dan Gohman1e93df62010-04-17 14:41:14 +00002534 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002535
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002536 // Used with vargs to acumulate store chains.
2537 std::vector<SDValue> OutChains;
2538
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002539 // Assign locations to all of the incoming arguments.
2540 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002541 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2542 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002543
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002544 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002545 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002546 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002547 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002548
Akira Hatanaka43299772011-05-20 23:22:14 +00002549 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002550
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002551 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002552 CCValAssign &VA = ArgLocs[i];
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002553 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002554 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2555 bool IsRegLoc = VA.isRegLoc();
2556
2557 if (Flags.isByVal()) {
2558 assert(Flags.getByValSize() &&
2559 "ByVal args of size 0 should have been ignored by front-end.");
2560 if (IsO32) {
2561 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2562 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2563 true);
2564 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2565 InVals.push_back(FIN);
2566 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2567 } else // N32/64
2568 LastFI = CopyMips64ByValRegs(MF, Chain, dl, OutChains, DAG, VA, Flags,
2569 MFI, IsRegLoc, InVals, MipsFI,
2570 getPointerTy());
2571 continue;
2572 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002573
2574 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002575 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002576 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002577 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002578 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002579
Owen Anderson825b72b2009-08-11 20:47:22 +00002580 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002581 RC = Mips::CPURegsRegisterClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002582 else if (RegVT == MVT::i64)
2583 RC = Mips::CPU64RegsRegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002584 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002585 RC = Mips::FGR32RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002586 else if (RegVT == MVT::f64)
Akira Hatanakaf40de9d2011-09-26 21:55:17 +00002587 RC = HasMips64 ? Mips::FGR64RegisterClass : Mips::AFGR64RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002588 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002589 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002590
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002591 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002592 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002593 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002594 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002595
2596 // If this is an 8 or 16-bit value, it has been passed promoted
2597 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002598 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002599 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002600 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002601 if (VA.getLocInfo() == CCValAssign::SExt)
2602 Opcode = ISD::AssertSext;
2603 else if (VA.getLocInfo() == CCValAssign::ZExt)
2604 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002605 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002606 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002607 DAG.getValueType(ValVT));
2608 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002609 }
2610
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002611 // Handle floating point arguments passed in integer registers.
2612 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
2613 (RegVT == MVT::i64 && ValVT == MVT::f64))
2614 ArgValue = DAG.getNode(ISD::BITCAST, dl, ValVT, ArgValue);
2615 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
2616 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
2617 getNextIntArgReg(ArgReg), RC);
2618 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
2619 if (!Subtarget->isLittle())
2620 std::swap(ArgValue, ArgValue2);
2621 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2622 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002623 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002624
Dan Gohman98ca4f22009-08-05 01:29:28 +00002625 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002626 } else { // VA.isRegLoc()
2627
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002628 // sanity check
2629 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002630
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002631 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002632 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002633 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002634
2635 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002636 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002637 InVals.push_back(DAG.getLoad(ValVT, dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002638 MachinePointerInfo::getFixedStack(LastFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002639 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002640 }
2641 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002642
2643 // The mips ABIs for returning structs by value requires that we copy
2644 // the sret argument into $v0 for the return. Save the argument into
2645 // a virtual register so that we can access it from the return points.
2646 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2647 unsigned Reg = MipsFI->getSRetReturnReg();
2648 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002649 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002650 MipsFI->setSRetReturnReg(Reg);
2651 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002652 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002653 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002654 }
2655
Akira Hatanakabad53f42011-11-14 19:01:09 +00002656 if (isVarArg) {
2657 unsigned NumOfRegs = IsO32 ? 4 : 8;
2658 const unsigned *ArgRegs = IsO32 ? O32IntRegs : Mips64IntRegs;
2659 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumOfRegs);
2660 int FirstRegSlotOffset = IsO32 ? 0 : -64 ; // offset of $a0's slot.
2661 TargetRegisterClass *RC
2662 = IsO32 ? Mips::CPURegsRegisterClass : Mips::CPU64RegsRegisterClass;
2663 unsigned RegSize = RC->getSize();
2664 int RegSlotOffset = FirstRegSlotOffset + Idx * RegSize;
2665
2666 // Offset of the first variable argument from stack pointer.
2667 int FirstVaArgOffset;
2668
2669 if (IsO32 || (Idx == NumOfRegs)) {
2670 FirstVaArgOffset =
2671 (CCInfo.getNextStackOffset() + RegSize - 1) / RegSize * RegSize;
2672 } else
2673 FirstVaArgOffset = RegSlotOffset;
2674
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002675 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002676 // which is a value necessary to VASTART.
Akira Hatanakabad53f42011-11-14 19:01:09 +00002677 LastFI = MFI->CreateFixedObject(RegSize, FirstVaArgOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002678 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002679
Akira Hatanakabad53f42011-11-14 19:01:09 +00002680 // Copy the integer registers that have not been used for argument passing
2681 // to the argument register save area. For O32, the save area is allocated
2682 // in the caller's stack frame, while for N32/64, it is allocated in the
2683 // callee's stack frame.
2684 for (int StackOffset = RegSlotOffset;
2685 Idx < NumOfRegs; ++Idx, StackOffset += RegSize) {
2686 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegs[Idx], RC);
2687 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2688 MVT::getIntegerVT(RegSize * 8));
2689 LastFI = MFI->CreateFixedObject(RegSize, StackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002690 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2691 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2692 MachinePointerInfo(),
2693 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002694 }
2695 }
2696
Akira Hatanaka43299772011-05-20 23:22:14 +00002697 MipsFI->setLastInArgFI(LastFI);
2698
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002699 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002700 // the size of Ins and InVals. This only happens when on varg functions
2701 if (!OutChains.empty()) {
2702 OutChains.push_back(Chain);
2703 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2704 &OutChains[0], OutChains.size());
2705 }
2706
Dan Gohman98ca4f22009-08-05 01:29:28 +00002707 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002708}
2709
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002710//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002711// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002712//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002713
Dan Gohman98ca4f22009-08-05 01:29:28 +00002714SDValue
2715MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002716 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002717 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002718 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002719 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002720
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002721 // CCValAssign - represent the assignment of
2722 // the return value to a location
2723 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002724
2725 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002726 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2727 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002728
Dan Gohman98ca4f22009-08-05 01:29:28 +00002729 // Analize return values.
2730 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002731
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002732 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002733 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002734 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002735 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002736 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002737 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002738 }
2739
Dan Gohman475871a2008-07-27 21:46:04 +00002740 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002741
2742 // Copy the result values into the output registers.
2743 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2744 CCValAssign &VA = RVLocs[i];
2745 assert(VA.isRegLoc() && "Can only return in registers!");
2746
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002747 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002748 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002749
2750 // guarantee that all emitted copies are
2751 // stuck together, avoiding something bad
2752 Flag = Chain.getValue(1);
2753 }
2754
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002755 // The mips ABIs for returning structs by value requires that we copy
2756 // the sret argument into $v0 for the return. We saved the argument into
2757 // a virtual register in the entry block, so now we copy the value out
2758 // and into $v0.
2759 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2760 MachineFunction &MF = DAG.getMachineFunction();
2761 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2762 unsigned Reg = MipsFI->getSRetReturnReg();
2763
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002764 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002765 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002766 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002767
Dale Johannesena05dca42009-02-04 23:02:30 +00002768 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002769 Flag = Chain.getValue(1);
2770 }
2771
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002772 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002773 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002774 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002775 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002776 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002777 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002778 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002779}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002780
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002781//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002782// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002783//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002784
2785/// getConstraintType - Given a constraint letter, return the type of
2786/// constraint it is for this target.
2787MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002788getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002789{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002790 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002791 // GCC config/mips/constraints.md
2792 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002793 // 'd' : An address register. Equivalent to r
2794 // unless generating MIPS16 code.
2795 // 'y' : Equivalent to r; retained for
2796 // backwards compatibility.
2797 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002798 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002799 switch (Constraint[0]) {
2800 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002801 case 'd':
2802 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002803 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002804 return C_RegisterClass;
2805 break;
2806 }
2807 }
2808 return TargetLowering::getConstraintType(Constraint);
2809}
2810
John Thompson44ab89e2010-10-29 17:29:13 +00002811/// Examine constraint type and operand type and determine a weight value.
2812/// This object must already have been set up with the operand type
2813/// and the current alternative constraint selected.
2814TargetLowering::ConstraintWeight
2815MipsTargetLowering::getSingleConstraintMatchWeight(
2816 AsmOperandInfo &info, const char *constraint) const {
2817 ConstraintWeight weight = CW_Invalid;
2818 Value *CallOperandVal = info.CallOperandVal;
2819 // If we don't have a value, we can't do a match,
2820 // but allow it at the lowest weight.
2821 if (CallOperandVal == NULL)
2822 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002823 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002824 // Look at the constraint type.
2825 switch (*constraint) {
2826 default:
2827 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2828 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002829 case 'd':
2830 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002831 if (type->isIntegerTy())
2832 weight = CW_Register;
2833 break;
2834 case 'f':
2835 if (type->isFloatTy())
2836 weight = CW_Register;
2837 break;
2838 }
2839 return weight;
2840}
2841
Eric Christopher38d64262011-06-29 19:33:04 +00002842/// Given a register class constraint, like 'r', if this corresponds directly
2843/// to an LLVM register class, return a register of 0 and the register class
2844/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002845std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002846getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002847{
2848 if (Constraint.size() == 1) {
2849 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002850 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2851 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002852 case 'r':
2853 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002854 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002855 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002856 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002857 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002858 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2859 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002860 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002861 }
2862 }
2863 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2864}
2865
Dan Gohman6520e202008-10-18 02:06:02 +00002866bool
2867MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2868 // The Mips target isn't yet aware of offsets.
2869 return false;
2870}
Evan Chengeb2f9692009-10-27 19:56:55 +00002871
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002872bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2873 if (VT != MVT::f32 && VT != MVT::f64)
2874 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002875 if (Imm.isNegZero())
2876 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002877 return Imm.isZero();
2878}