blob: b5a15cf36dd7fb08e6bffc17e1ef1ce262c96974 [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);
Akira Hatanaka620db892011-11-16 22:44:38 +0000134 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 setOperationAction(ISD::SELECT, MVT::f32, Custom);
136 setOperationAction(ISD::SELECT, MVT::f64, Custom);
137 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000138 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
139 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000140 setOperationAction(ISD::VASTART, MVT::Other, Custom);
141
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000142 setOperationAction(ISD::SDIV, MVT::i32, Expand);
143 setOperationAction(ISD::SREM, MVT::i32, Expand);
144 setOperationAction(ISD::UDIV, MVT::i32, Expand);
145 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000146 setOperationAction(ISD::SDIV, MVT::i64, Expand);
147 setOperationAction(ISD::SREM, MVT::i64, Expand);
148 setOperationAction(ISD::UDIV, MVT::i64, Expand);
149 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000150
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000151 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000152 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
153 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
154 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
155 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
156 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
157 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
158 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
159 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
160 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000161 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000162
Akira Hatanaka56633442011-09-20 23:53:09 +0000163 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000164 setOperationAction(ISD::ROTR, MVT::i32, Expand);
165
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000166 if (!Subtarget->hasMips64r2())
167 setOperationAction(ISD::ROTR, MVT::i64, Expand);
168
Owen Anderson825b72b2009-08-11 20:47:22 +0000169 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
170 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
171 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000172 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
173 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000174 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000175 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000176 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000177 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000178 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
179 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000180 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000181 setOperationAction(ISD::FLOG, MVT::f32, Expand);
182 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
183 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
184 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000185 setOperationAction(ISD::FMA, MVT::f32, Expand);
186 setOperationAction(ISD::FMA, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000187
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000188 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
189 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Eric Christopher471e4222011-06-08 23:55:35 +0000190
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000191 setOperationAction(ISD::VAARG, MVT::Other, Expand);
192 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
193 setOperationAction(ISD::VAEND, MVT::Other, Expand);
194
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000195 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
197 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000198
Akira Hatanakadb548262011-07-19 23:30:50 +0000199 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000200 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000201
Eli Friedman4db5aca2011-08-29 18:23:02 +0000202 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
203 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
204
Eli Friedman26689ac2011-08-03 21:06:02 +0000205 setInsertFencesForAtomic(true);
206
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000207 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000208 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000209
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000210 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000211 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
212 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000213 }
214
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000215 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000216 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000217
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000218 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000219 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000220
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000221 setTargetDAGCombine(ISD::ADDE);
222 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000223 setTargetDAGCombine(ISD::SDIVREM);
224 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000225 setTargetDAGCombine(ISD::SETCC);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000226 setTargetDAGCombine(ISD::AND);
227 setTargetDAGCombine(ISD::OR);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000228
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000229 setMinFunctionAlignment(2);
230
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000231 setStackPointerRegisterToSaveRestore(Mips::SP);
232 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000233
234 setExceptionPointerRegister(Mips::A0);
235 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000236}
237
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000238bool MipsTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Akira Hatanaka511961a2011-08-17 18:49:18 +0000239 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
Akira Hatanaka7bd19bd2011-10-11 00:27:28 +0000240 return SVT == MVT::i64 || SVT == MVT::i32 || SVT == MVT::i16;
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000241}
242
Duncan Sands28b77e92011-09-06 19:07:46 +0000243EVT MipsTargetLowering::getSetCCResultType(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000245}
246
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000247// SelectMadd -
248// Transforms a subgraph in CurDAG if the following pattern is found:
249// (addc multLo, Lo0), (adde multHi, Hi0),
250// where,
251// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000252// Lo0: initial value of Lo register
253// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000254// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000255static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000256 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000257 // for the matching to be successful.
258 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
259
260 if (ADDCNode->getOpcode() != ISD::ADDC)
261 return false;
262
263 SDValue MultHi = ADDENode->getOperand(0);
264 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000265 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000266 unsigned MultOpc = MultHi.getOpcode();
267
268 // MultHi and MultLo must be generated by the same node,
269 if (MultLo.getNode() != MultNode)
270 return false;
271
272 // and it must be a multiplication.
273 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
274 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000275
276 // MultLo amd MultHi must be the first and second output of MultNode
277 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000278 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
279 return false;
280
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000281 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000282 // of the values of MultNode, in which case MultNode will be removed in later
283 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000284 // If there exist users other than ADDENode or ADDCNode, this function returns
285 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000286 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000287 // produced.
288 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
289 return false;
290
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000291 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000292 DebugLoc dl = ADDENode->getDebugLoc();
293
294 // create MipsMAdd(u) node
295 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000296
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000297 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
298 MVT::Glue,
299 MultNode->getOperand(0),// Factor 0
300 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000301 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000302 ADDENode->getOperand(1));// Hi0
303
304 // create CopyFromReg nodes
305 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
306 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000307 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000308 Mips::HI, MVT::i32,
309 CopyFromLo.getValue(2));
310
311 // replace uses of adde and addc here
312 if (!SDValue(ADDCNode, 0).use_empty())
313 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
314
315 if (!SDValue(ADDENode, 0).use_empty())
316 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
317
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000318 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000319}
320
321// SelectMsub -
322// Transforms a subgraph in CurDAG if the following pattern is found:
323// (addc Lo0, multLo), (sube Hi0, multHi),
324// where,
325// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000326// Lo0: initial value of Lo register
327// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000328// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000329static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000330 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000331 // for the matching to be successful.
332 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
333
334 if (SUBCNode->getOpcode() != ISD::SUBC)
335 return false;
336
337 SDValue MultHi = SUBENode->getOperand(1);
338 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000339 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000340 unsigned MultOpc = MultHi.getOpcode();
341
342 // MultHi and MultLo must be generated by the same node,
343 if (MultLo.getNode() != MultNode)
344 return false;
345
346 // and it must be a multiplication.
347 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
348 return false;
349
350 // MultLo amd MultHi must be the first and second output of MultNode
351 // respectively.
352 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
353 return false;
354
355 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
356 // of the values of MultNode, in which case MultNode will be removed in later
357 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000358 // If there exist users other than SUBENode or SUBCNode, this function returns
359 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000360 // instruction node rather than a pair of MULT and MSUB instructions being
361 // produced.
362 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
363 return false;
364
365 SDValue Chain = CurDAG->getEntryNode();
366 DebugLoc dl = SUBENode->getDebugLoc();
367
368 // create MipsSub(u) node
369 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
370
371 SDValue MSub = CurDAG->getNode(MultOpc, dl,
372 MVT::Glue,
373 MultNode->getOperand(0),// Factor 0
374 MultNode->getOperand(1),// Factor 1
375 SUBCNode->getOperand(0),// Lo0
376 SUBENode->getOperand(0));// Hi0
377
378 // create CopyFromReg nodes
379 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
380 MSub);
381 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
382 Mips::HI, MVT::i32,
383 CopyFromLo.getValue(2));
384
385 // replace uses of sube and subc here
386 if (!SDValue(SUBCNode, 0).use_empty())
387 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
388
389 if (!SDValue(SUBENode, 0).use_empty())
390 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
391
392 return true;
393}
394
395static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
396 TargetLowering::DAGCombinerInfo &DCI,
397 const MipsSubtarget* Subtarget) {
398 if (DCI.isBeforeLegalize())
399 return SDValue();
400
Akira Hatanakae184fec2011-11-11 04:18:21 +0000401 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
402 SelectMadd(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000403 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000404
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000405 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000406}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000407
408static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
409 TargetLowering::DAGCombinerInfo &DCI,
410 const MipsSubtarget* Subtarget) {
411 if (DCI.isBeforeLegalize())
412 return SDValue();
413
Akira Hatanakae184fec2011-11-11 04:18:21 +0000414 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
415 SelectMsub(N, &DAG))
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000416 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000417
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000418 return SDValue();
419}
420
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000421static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
422 TargetLowering::DAGCombinerInfo &DCI,
423 const MipsSubtarget* Subtarget) {
424 if (DCI.isBeforeLegalizeOps())
425 return SDValue();
426
Akira Hatanakadda4a072011-10-03 21:06:13 +0000427 EVT Ty = N->getValueType(0);
428 unsigned LO = (Ty == MVT::i32) ? Mips::LO : Mips::LO64;
429 unsigned HI = (Ty == MVT::i32) ? Mips::HI : Mips::HI64;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000430 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
431 MipsISD::DivRemU;
432 DebugLoc dl = N->getDebugLoc();
433
434 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
435 N->getOperand(0), N->getOperand(1));
436 SDValue InChain = DAG.getEntryNode();
437 SDValue InGlue = DivRem;
438
439 // insert MFLO
440 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakadda4a072011-10-03 21:06:13 +0000441 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000442 InGlue);
443 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
444 InChain = CopyFromLo.getValue(1);
445 InGlue = CopyFromLo.getValue(2);
446 }
447
448 // insert MFHI
449 if (N->hasAnyUseOfValue(1)) {
450 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000451 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000452 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
453 }
454
455 return SDValue();
456}
457
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000458static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
459 switch (CC) {
460 default: llvm_unreachable("Unknown fp condition code!");
461 case ISD::SETEQ:
462 case ISD::SETOEQ: return Mips::FCOND_OEQ;
463 case ISD::SETUNE: return Mips::FCOND_UNE;
464 case ISD::SETLT:
465 case ISD::SETOLT: return Mips::FCOND_OLT;
466 case ISD::SETGT:
467 case ISD::SETOGT: return Mips::FCOND_OGT;
468 case ISD::SETLE:
469 case ISD::SETOLE: return Mips::FCOND_OLE;
470 case ISD::SETGE:
471 case ISD::SETOGE: return Mips::FCOND_OGE;
472 case ISD::SETULT: return Mips::FCOND_ULT;
473 case ISD::SETULE: return Mips::FCOND_ULE;
474 case ISD::SETUGT: return Mips::FCOND_UGT;
475 case ISD::SETUGE: return Mips::FCOND_UGE;
476 case ISD::SETUO: return Mips::FCOND_UN;
477 case ISD::SETO: return Mips::FCOND_OR;
478 case ISD::SETNE:
479 case ISD::SETONE: return Mips::FCOND_ONE;
480 case ISD::SETUEQ: return Mips::FCOND_UEQ;
481 }
482}
483
484
485// Returns true if condition code has to be inverted.
486static bool InvertFPCondCode(Mips::CondCode CC) {
487 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
488 return false;
489
490 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
491 return true;
492
493 assert(false && "Illegal Condition Code");
494 return false;
495}
496
497// Creates and returns an FPCmp node from a setcc node.
498// Returns Op if setcc is not a floating point comparison.
499static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
500 // must be a SETCC node
501 if (Op.getOpcode() != ISD::SETCC)
502 return Op;
503
504 SDValue LHS = Op.getOperand(0);
505
506 if (!LHS.getValueType().isFloatingPoint())
507 return Op;
508
509 SDValue RHS = Op.getOperand(1);
510 DebugLoc dl = Op.getDebugLoc();
511
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000512 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
513 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000514 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
515
516 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
517 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
518}
519
520// Creates and returns a CMovFPT/F node.
521static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
522 SDValue False, DebugLoc DL) {
523 bool invert = InvertFPCondCode((Mips::CondCode)
524 cast<ConstantSDNode>(Cond.getOperand(2))
525 ->getSExtValue());
526
527 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
528 True.getValueType(), True, False, Cond);
529}
530
531static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
532 TargetLowering::DAGCombinerInfo &DCI,
533 const MipsSubtarget* Subtarget) {
534 if (DCI.isBeforeLegalizeOps())
535 return SDValue();
536
537 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
538
539 if (Cond.getOpcode() != MipsISD::FPCmp)
540 return SDValue();
541
542 SDValue True = DAG.getConstant(1, MVT::i32);
543 SDValue False = DAG.getConstant(0, MVT::i32);
544
545 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
546}
547
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000548static SDValue PerformANDCombine(SDNode *N, SelectionDAG& DAG,
549 TargetLowering::DAGCombinerInfo &DCI,
550 const MipsSubtarget* Subtarget) {
551 // Pattern match EXT.
552 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
553 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000554 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000555 return SDValue();
556
557 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
558
559 // Op's first operand must be a shift right.
560 if (ShiftRight.getOpcode() != ISD::SRA && ShiftRight.getOpcode() != ISD::SRL)
561 return SDValue();
562
563 // The second operand of the shift must be an immediate.
564 uint64_t Pos;
565 ConstantSDNode *CN;
566 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
567 return SDValue();
568
569 Pos = CN->getZExtValue();
570
571 uint64_t SMPos, SMSize;
572 // Op's second operand must be a shifted mask.
573 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000574 !IsShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000575 return SDValue();
576
577 // Return if the shifted mask does not start at bit 0 or the sum of its size
578 // and Pos exceeds the word's size.
579 if (SMPos != 0 || Pos + SMSize > 32)
580 return SDValue();
581
582 return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), MVT::i32,
583 ShiftRight.getOperand(0),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000584 DAG.getConstant(Pos, MVT::i32),
585 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000586}
587
588static SDValue PerformORCombine(SDNode *N, SelectionDAG& DAG,
589 TargetLowering::DAGCombinerInfo &DCI,
590 const MipsSubtarget* Subtarget) {
591 // Pattern match INS.
592 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
593 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
594 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000595 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000596 return SDValue();
597
598 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
599 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
600 ConstantSDNode *CN;
601
602 // See if Op's first operand matches (and $src1 , mask0).
603 if (And0.getOpcode() != ISD::AND)
604 return SDValue();
605
606 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000607 !IsShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000608 return SDValue();
609
610 // See if Op's second operand matches (and (shl $src, pos), mask1).
611 if (And1.getOpcode() != ISD::AND)
612 return SDValue();
613
614 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka854a7db2011-08-19 22:59:00 +0000615 !IsShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000616 return SDValue();
617
618 // The shift masks must have the same position and size.
619 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
620 return SDValue();
621
622 SDValue Shl = And1.getOperand(0);
623 if (Shl.getOpcode() != ISD::SHL)
624 return SDValue();
625
626 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
627 return SDValue();
628
629 unsigned Shamt = CN->getZExtValue();
630
631 // Return if the shift amount and the first bit position of mask are not the
632 // same.
633 if (Shamt != SMPos0)
634 return SDValue();
635
636 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), MVT::i32,
637 Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000638 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000639 DAG.getConstant(SMSize0, MVT::i32),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000640 And0.getOperand(0));
641}
642
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000643SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000644 const {
645 SelectionDAG &DAG = DCI.DAG;
646 unsigned opc = N->getOpcode();
647
648 switch (opc) {
649 default: break;
650 case ISD::ADDE:
651 return PerformADDECombine(N, DAG, DCI, Subtarget);
652 case ISD::SUBE:
653 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000654 case ISD::SDIVREM:
655 case ISD::UDIVREM:
656 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000657 case ISD::SETCC:
658 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000659 case ISD::AND:
660 return PerformANDCombine(N, DAG, DCI, Subtarget);
661 case ISD::OR:
662 return PerformORCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000663 }
664
665 return SDValue();
666}
667
Dan Gohman475871a2008-07-27 21:46:04 +0000668SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000669LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000670{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000671 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000672 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000673 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000674 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
675 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000676 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000677 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000678 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
679 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000680 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000681 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000682 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000683 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Akira Hatanakadb548262011-07-19 23:30:50 +0000684 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +0000685 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000686 }
Dan Gohman475871a2008-07-27 21:46:04 +0000687 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000688}
689
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000690//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000691// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000692//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000693
694// AddLiveIn - This helper function adds the specified physical register to the
695// MachineFunction as a live in value. It also creates a corresponding
696// virtual register for it.
697static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000698AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000699{
700 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000701 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
702 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000703 return VReg;
704}
705
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000706// Get fp branch code (not opcode) from condition code.
707static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
708 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
709 return Mips::BRANCH_T;
710
711 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
712 return Mips::BRANCH_F;
713
714 return Mips::BRANCH_INVALID;
715}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000716
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000717/*
Akira Hatanaka14487d42011-06-07 19:28:39 +0000718static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
719 DebugLoc dl,
720 const MipsSubtarget* Subtarget,
721 const TargetInstrInfo *TII,
722 bool isFPCmp, unsigned Opc) {
723 // There is no need to expand CMov instructions if target has
724 // conditional moves.
725 if (Subtarget->hasCondMov())
726 return BB;
727
728 // To "insert" a SELECT_CC instruction, we actually have to insert the
729 // diamond control-flow pattern. The incoming instruction knows the
730 // destination vreg to set, the condition code register to branch on, the
731 // true/false values to select between, and a branch opcode to use.
732 const BasicBlock *LLVM_BB = BB->getBasicBlock();
733 MachineFunction::iterator It = BB;
734 ++It;
735
736 // thisMBB:
737 // ...
738 // TrueVal = ...
739 // setcc r1, r2, r3
740 // bNE r1, r0, copy1MBB
741 // fallthrough --> copy0MBB
742 MachineBasicBlock *thisMBB = BB;
743 MachineFunction *F = BB->getParent();
744 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
745 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
746 F->insert(It, copy0MBB);
747 F->insert(It, sinkMBB);
748
749 // Transfer the remainder of BB and its successor edges to sinkMBB.
750 sinkMBB->splice(sinkMBB->begin(), BB,
751 llvm::next(MachineBasicBlock::iterator(MI)),
752 BB->end());
753 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
754
755 // Next, add the true and fallthrough blocks as its successors.
756 BB->addSuccessor(copy0MBB);
757 BB->addSuccessor(sinkMBB);
758
759 // Emit the right instruction according to the type of the operands compared
760 if (isFPCmp)
761 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
762 else
763 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
764 .addReg(Mips::ZERO).addMBB(sinkMBB);
765
766 // copy0MBB:
767 // %FalseValue = ...
768 // # fallthrough to sinkMBB
769 BB = copy0MBB;
770
771 // Update machine-CFG edges
772 BB->addSuccessor(sinkMBB);
773
774 // sinkMBB:
775 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
776 // ...
777 BB = sinkMBB;
778
779 if (isFPCmp)
780 BuildMI(*BB, BB->begin(), dl,
781 TII->get(Mips::PHI), MI->getOperand(0).getReg())
782 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
783 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
784 else
785 BuildMI(*BB, BB->begin(), dl,
786 TII->get(Mips::PHI), MI->getOperand(0).getReg())
787 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
788 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
789
790 MI->eraseFromParent(); // The pseudo instruction is gone now.
791 return BB;
792}
Akira Hatanaka8ae330a2011-10-17 18:53:29 +0000793*/
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000794MachineBasicBlock *
795MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000796 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000797 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000798 default:
799 assert(false && "Unexpected instr type to insert");
800 return NULL;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000801 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000802 case Mips::ATOMIC_LOAD_ADD_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000803 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
804 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000805 case Mips::ATOMIC_LOAD_ADD_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000806 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
807 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000808 case Mips::ATOMIC_LOAD_ADD_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000809 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000810 case Mips::ATOMIC_LOAD_ADD_I64:
811 case Mips::ATOMIC_LOAD_ADD_I64_P8:
812 return EmitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000813
814 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000815 case Mips::ATOMIC_LOAD_AND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000816 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
817 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000818 case Mips::ATOMIC_LOAD_AND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000819 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
820 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000821 case Mips::ATOMIC_LOAD_AND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000822 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000823 case Mips::ATOMIC_LOAD_AND_I64:
824 case Mips::ATOMIC_LOAD_AND_I64_P8:
Akira Hatanaka73866122011-11-12 02:38:12 +0000825 return EmitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000826
827 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000828 case Mips::ATOMIC_LOAD_OR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000829 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
830 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000831 case Mips::ATOMIC_LOAD_OR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000832 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
833 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000834 case Mips::ATOMIC_LOAD_OR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000835 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000836 case Mips::ATOMIC_LOAD_OR_I64:
837 case Mips::ATOMIC_LOAD_OR_I64_P8:
838 return EmitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000839
840 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000841 case Mips::ATOMIC_LOAD_XOR_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000842 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
843 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000844 case Mips::ATOMIC_LOAD_XOR_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000845 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
846 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000847 case Mips::ATOMIC_LOAD_XOR_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000848 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000849 case Mips::ATOMIC_LOAD_XOR_I64:
850 case Mips::ATOMIC_LOAD_XOR_I64_P8:
851 return EmitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000852
853 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000854 case Mips::ATOMIC_LOAD_NAND_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000855 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
856 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000857 case Mips::ATOMIC_LOAD_NAND_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000858 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
859 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000860 case Mips::ATOMIC_LOAD_NAND_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000861 return EmitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000862 case Mips::ATOMIC_LOAD_NAND_I64:
863 case Mips::ATOMIC_LOAD_NAND_I64_P8:
864 return EmitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000865
866 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000867 case Mips::ATOMIC_LOAD_SUB_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000868 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
869 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000870 case Mips::ATOMIC_LOAD_SUB_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000871 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
872 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000873 case Mips::ATOMIC_LOAD_SUB_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000874 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000875 case Mips::ATOMIC_LOAD_SUB_I64:
876 case Mips::ATOMIC_LOAD_SUB_I64_P8:
877 return EmitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000878
879 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000880 case Mips::ATOMIC_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000881 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
882 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000883 case Mips::ATOMIC_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000884 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
885 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000886 case Mips::ATOMIC_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000887 return EmitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000888 case Mips::ATOMIC_SWAP_I64:
889 case Mips::ATOMIC_SWAP_I64_P8:
890 return EmitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000891
892 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka59068062011-11-11 04:14:30 +0000893 case Mips::ATOMIC_CMP_SWAP_I8_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000894 return EmitAtomicCmpSwapPartword(MI, BB, 1);
895 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka59068062011-11-11 04:14:30 +0000896 case Mips::ATOMIC_CMP_SWAP_I16_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000897 return EmitAtomicCmpSwapPartword(MI, BB, 2);
898 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka59068062011-11-11 04:14:30 +0000899 case Mips::ATOMIC_CMP_SWAP_I32_P8:
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000900 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000901 case Mips::ATOMIC_CMP_SWAP_I64:
902 case Mips::ATOMIC_CMP_SWAP_I64_P8:
903 return EmitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000904 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000905}
906
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000907// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
908// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
909MachineBasicBlock *
910MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000911 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000912 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000913 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000914
915 MachineFunction *MF = BB->getParent();
916 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000917 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000918 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
919 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000920 unsigned LL, SC, AND, NOR, ZERO, BEQ;
921
922 if (Size == 4) {
923 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
924 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
925 AND = Mips::AND;
926 NOR = Mips::NOR;
927 ZERO = Mips::ZERO;
928 BEQ = Mips::BEQ;
929 }
930 else {
931 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
932 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
933 AND = Mips::AND64;
934 NOR = Mips::NOR64;
935 ZERO = Mips::ZERO_64;
936 BEQ = Mips::BEQ64;
937 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000938
Akira Hatanaka4061da12011-07-19 20:11:17 +0000939 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000940 unsigned Ptr = MI->getOperand(1).getReg();
941 unsigned Incr = MI->getOperand(2).getReg();
942
Akira Hatanaka4061da12011-07-19 20:11:17 +0000943 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
944 unsigned AndRes = RegInfo.createVirtualRegister(RC);
945 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000946
947 // insert new blocks after the current block
948 const BasicBlock *LLVM_BB = BB->getBasicBlock();
949 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
950 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
951 MachineFunction::iterator It = BB;
952 ++It;
953 MF->insert(It, loopMBB);
954 MF->insert(It, exitMBB);
955
956 // Transfer the remainder of BB and its successor edges to exitMBB.
957 exitMBB->splice(exitMBB->begin(), BB,
958 llvm::next(MachineBasicBlock::iterator(MI)),
959 BB->end());
960 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
961
962 // thisMBB:
963 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000964 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000965 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000966 loopMBB->addSuccessor(loopMBB);
967 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000968
969 // loopMBB:
970 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000971 // <binop> storeval, oldval, incr
972 // sc success, storeval, 0(ptr)
973 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000974 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +0000975 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000976 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000977 // and andres, oldval, incr
978 // nor storeval, $0, andres
Akira Hatanaka59068062011-11-11 04:14:30 +0000979 BuildMI(BB, dl, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
980 BuildMI(BB, dl, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000981 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000982 // <binop> storeval, oldval, incr
983 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000984 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000985 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000986 }
Akira Hatanaka59068062011-11-11 04:14:30 +0000987 BuildMI(BB, dl, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
988 BuildMI(BB, dl, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000989
990 MI->eraseFromParent(); // The instruction is gone now.
991
Akira Hatanaka939ece12011-07-19 03:42:13 +0000992 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000993}
994
995MachineBasicBlock *
996MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000997 MachineBasicBlock *BB,
998 unsigned Size, unsigned BinOpcode,
999 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001000 assert((Size == 1 || Size == 2) &&
1001 "Unsupported size for EmitAtomicBinaryPartial.");
1002
1003 MachineFunction *MF = BB->getParent();
1004 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1005 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1006 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1007 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001008 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1009 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001010
1011 unsigned Dest = MI->getOperand(0).getReg();
1012 unsigned Ptr = MI->getOperand(1).getReg();
1013 unsigned Incr = MI->getOperand(2).getReg();
1014
Akira Hatanaka4061da12011-07-19 20:11:17 +00001015 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1016 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001017 unsigned Mask = RegInfo.createVirtualRegister(RC);
1018 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001019 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1020 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001021 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001022 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1023 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1024 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1025 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1026 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001027 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001028 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1029 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1030 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1031 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1032 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001033
1034 // insert new blocks after the current block
1035 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1036 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001037 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001038 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1039 MachineFunction::iterator It = BB;
1040 ++It;
1041 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001042 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001043 MF->insert(It, exitMBB);
1044
1045 // Transfer the remainder of BB and its successor edges to exitMBB.
1046 exitMBB->splice(exitMBB->begin(), BB,
1047 llvm::next(MachineBasicBlock::iterator(MI)),
1048 BB->end());
1049 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1050
Akira Hatanaka81b44112011-07-19 17:09:53 +00001051 BB->addSuccessor(loopMBB);
1052 loopMBB->addSuccessor(loopMBB);
1053 loopMBB->addSuccessor(sinkMBB);
1054 sinkMBB->addSuccessor(exitMBB);
1055
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001056 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001057 // addiu masklsb2,$0,-4 # 0xfffffffc
1058 // and alignedaddr,ptr,masklsb2
1059 // andi ptrlsb2,ptr,3
1060 // sll shiftamt,ptrlsb2,3
1061 // ori maskupper,$0,255 # 0xff
1062 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001063 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001064 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001065
1066 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001067 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1068 .addReg(Mips::ZERO).addImm(-4);
1069 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1070 .addReg(Ptr).addReg(MaskLSB2);
1071 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1072 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1073 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1074 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001075 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1076 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001077 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001078 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001079
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001080
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001081 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001082 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001083 // ll oldval,0(alignedaddr)
1084 // binop binopres,oldval,incr2
1085 // and newval,binopres,mask
1086 // and maskedoldval0,oldval,mask2
1087 // or storeval,maskedoldval0,newval
1088 // sc success,storeval,0(alignedaddr)
1089 // beq success,$0,loopMBB
1090
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001091 // atomic.swap
1092 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001093 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001094 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001095 // and maskedoldval0,oldval,mask2
1096 // or storeval,maskedoldval0,newval
1097 // sc success,storeval,0(alignedaddr)
1098 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001099
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001100 BB = loopMBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001101 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001102 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001103 // and andres, oldval, incr2
1104 // nor binopres, $0, andres
1105 // and newval, binopres, mask
1106 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1107 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1108 .addReg(Mips::ZERO).addReg(AndRes);
1109 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001110 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001111 // <binop> binopres, oldval, incr2
1112 // and newval, binopres, mask
1113 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1114 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001115 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001116 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001117 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001118 }
1119
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001120 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001121 .addReg(OldVal).addReg(Mask2);
1122 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001123 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001124 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001125 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001126 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001127 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001128
Akira Hatanaka939ece12011-07-19 03:42:13 +00001129 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001130 // and maskedoldval1,oldval,mask
1131 // srl srlres,maskedoldval1,shiftamt
1132 // sll sllres,srlres,24
1133 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001134 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001135 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001136
Akira Hatanaka4061da12011-07-19 20:11:17 +00001137 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1138 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001139 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1140 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001141 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1142 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001143 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001144 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001145
1146 MI->eraseFromParent(); // The instruction is gone now.
1147
Akira Hatanaka939ece12011-07-19 03:42:13 +00001148 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001149}
1150
1151MachineBasicBlock *
1152MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001153 MachineBasicBlock *BB,
1154 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001155 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001156
1157 MachineFunction *MF = BB->getParent();
1158 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001159 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001160 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1161 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001162 unsigned LL, SC, ZERO, BNE, BEQ;
1163
1164 if (Size == 4) {
1165 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1166 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1167 ZERO = Mips::ZERO;
1168 BNE = Mips::BNE;
1169 BEQ = Mips::BEQ;
1170 }
1171 else {
1172 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1173 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1174 ZERO = Mips::ZERO_64;
1175 BNE = Mips::BNE64;
1176 BEQ = Mips::BEQ64;
1177 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001178
1179 unsigned Dest = MI->getOperand(0).getReg();
1180 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001181 unsigned OldVal = MI->getOperand(2).getReg();
1182 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001183
Akira Hatanaka4061da12011-07-19 20:11:17 +00001184 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001185
1186 // insert new blocks after the current block
1187 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1188 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1189 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1190 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1191 MachineFunction::iterator It = BB;
1192 ++It;
1193 MF->insert(It, loop1MBB);
1194 MF->insert(It, loop2MBB);
1195 MF->insert(It, exitMBB);
1196
1197 // Transfer the remainder of BB and its successor edges to exitMBB.
1198 exitMBB->splice(exitMBB->begin(), BB,
1199 llvm::next(MachineBasicBlock::iterator(MI)),
1200 BB->end());
1201 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1202
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001203 // thisMBB:
1204 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001205 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001206 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001207 loop1MBB->addSuccessor(exitMBB);
1208 loop1MBB->addSuccessor(loop2MBB);
1209 loop2MBB->addSuccessor(loop1MBB);
1210 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001211
1212 // loop1MBB:
1213 // ll dest, 0(ptr)
1214 // bne dest, oldval, exitMBB
1215 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001216 BuildMI(BB, dl, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1217 BuildMI(BB, dl, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001218 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001219
1220 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001221 // sc success, newval, 0(ptr)
1222 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001223 BB = loop2MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001224 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001225 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka59068062011-11-11 04:14:30 +00001226 BuildMI(BB, dl, TII->get(BEQ))
1227 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001228
1229 MI->eraseFromParent(); // The instruction is gone now.
1230
Akira Hatanaka939ece12011-07-19 03:42:13 +00001231 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001232}
1233
1234MachineBasicBlock *
1235MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001236 MachineBasicBlock *BB,
1237 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001238 assert((Size == 1 || Size == 2) &&
1239 "Unsupported size for EmitAtomicCmpSwapPartial.");
1240
1241 MachineFunction *MF = BB->getParent();
1242 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1243 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1244 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1245 DebugLoc dl = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001246 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1247 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001248
1249 unsigned Dest = MI->getOperand(0).getReg();
1250 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001251 unsigned CmpVal = MI->getOperand(2).getReg();
1252 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001253
Akira Hatanaka4061da12011-07-19 20:11:17 +00001254 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1255 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001256 unsigned Mask = RegInfo.createVirtualRegister(RC);
1257 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001258 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1259 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1260 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1261 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1262 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1263 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1264 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1265 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1266 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1267 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1268 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1269 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1270 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1271 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001272
1273 // insert new blocks after the current block
1274 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1275 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1276 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001277 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001278 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1279 MachineFunction::iterator It = BB;
1280 ++It;
1281 MF->insert(It, loop1MBB);
1282 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001283 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001284 MF->insert(It, exitMBB);
1285
1286 // Transfer the remainder of BB and its successor edges to exitMBB.
1287 exitMBB->splice(exitMBB->begin(), BB,
1288 llvm::next(MachineBasicBlock::iterator(MI)),
1289 BB->end());
1290 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1291
Akira Hatanaka81b44112011-07-19 17:09:53 +00001292 BB->addSuccessor(loop1MBB);
1293 loop1MBB->addSuccessor(sinkMBB);
1294 loop1MBB->addSuccessor(loop2MBB);
1295 loop2MBB->addSuccessor(loop1MBB);
1296 loop2MBB->addSuccessor(sinkMBB);
1297 sinkMBB->addSuccessor(exitMBB);
1298
Akira Hatanaka70564a92011-07-19 18:14:26 +00001299 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001300 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001301 // addiu masklsb2,$0,-4 # 0xfffffffc
1302 // and alignedaddr,ptr,masklsb2
1303 // andi ptrlsb2,ptr,3
1304 // sll shiftamt,ptrlsb2,3
1305 // ori maskupper,$0,255 # 0xff
1306 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001307 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001308 // andi maskedcmpval,cmpval,255
1309 // sll shiftedcmpval,maskedcmpval,shiftamt
1310 // andi maskednewval,newval,255
1311 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001312 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001313 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1314 .addReg(Mips::ZERO).addImm(-4);
1315 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1316 .addReg(Ptr).addReg(MaskLSB2);
1317 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1318 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1319 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1320 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001321 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1322 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001323 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001324 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1325 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001326 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1327 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001328 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1329 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001330 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1331 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001332
1333 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001334 // ll oldval,0(alginedaddr)
1335 // and maskedoldval0,oldval,mask
1336 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001337 BB = loop1MBB;
Akira Hatanaka59068062011-11-11 04:14:30 +00001338 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001339 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1340 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001341 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001342 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001343
1344 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001345 // and maskedoldval1,oldval,mask2
1346 // or storeval,maskedoldval1,shiftednewval
1347 // sc success,storeval,0(alignedaddr)
1348 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001349 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001350 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1351 .addReg(OldVal).addReg(Mask2);
1352 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1353 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanaka59068062011-11-11 04:14:30 +00001354 BuildMI(BB, dl, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001355 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001356 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001357 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001358
Akira Hatanaka939ece12011-07-19 03:42:13 +00001359 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001360 // srl srlres,maskedoldval0,shiftamt
1361 // sll sllres,srlres,24
1362 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001363 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001364 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001365
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001366 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1367 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001368 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1369 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001370 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001371 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001372
1373 MI->eraseFromParent(); // The instruction is gone now.
1374
Akira Hatanaka939ece12011-07-19 03:42:13 +00001375 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001376}
1377
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001378//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001379// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001380//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001381SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001382LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001383{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001384 MachineFunction &MF = DAG.getMachineFunction();
1385 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001386 unsigned SP = IsN64 ? Mips::SP_64 : Mips::SP;
Akira Hatanaka21afc632011-06-21 00:40:49 +00001387
1388 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001389 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1390 "Cannot lower if the alignment of the allocated space is larger than \
1391 that of the stack.");
1392
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001393 SDValue Chain = Op.getOperand(0);
1394 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001395 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001396
1397 // Get a reference from Mips stack pointer
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001398 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, SP, getPointerTy());
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001399
1400 // Subtract the dynamic size from the actual stack size to
1401 // obtain the new stack size.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001402 SDValue Sub = DAG.getNode(ISD::SUB, dl, getPointerTy(), StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001403
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001404 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001405 // must be placed in the stack pointer register.
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001406 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, SP, Sub, SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001407
1408 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001409 // value and a chain
Akira Hatanakac742e4f2011-11-11 04:06:38 +00001410 SDVTList VTLs = DAG.getVTList(getPointerTy(), MVT::Other);
Akira Hatanaka21afc632011-06-21 00:40:49 +00001411 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1412 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1413
1414 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001415}
1416
1417SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001418LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001419{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001420 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001421 // the block to branch to if the condition is true.
1422 SDValue Chain = Op.getOperand(0);
1423 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001424 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001425
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001426 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1427
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001428 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001429 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001430 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001431
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001432 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001433 Mips::CondCode CC =
1434 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001435 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001436
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001437 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001438 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001439}
1440
1441SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001442LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001443{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001444 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001445
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001446 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001447 if (Cond.getOpcode() != MipsISD::FPCmp)
1448 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001449
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001450 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1451 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001452}
1453
Dan Gohmand858e902010-04-17 15:26:15 +00001454SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1455 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001456 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001457 DebugLoc dl = Op.getDebugLoc();
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001458 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001459
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001460 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001461 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001462
Chris Lattnerb71b9092009-08-13 06:28:06 +00001463 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001464
Chris Lattnere3736f82009-08-13 05:41:27 +00001465 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001466 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1467 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001468 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001469 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1470 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001471 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001472 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001473 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001474 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1475 MipsII::MO_ABS_HI);
1476 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1477 MipsII::MO_ABS_LO);
1478 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1479 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001480 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001481 }
1482
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001483 EVT ValTy = Op.getValueType();
1484 bool HasGotOfst = (GV->hasInternalLinkage() ||
1485 (GV->hasLocalLinkage() && !isa<Function>(GV)));
1486 unsigned GotFlag = IsN64 ?
1487 (HasGotOfst ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT_DISP) :
1488 MipsII::MO_GOT;
1489 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0, GotFlag);
1490 GA = DAG.getNode(MipsISD::WrapperPIC, dl, ValTy, GA);
1491 SDValue ResNode = DAG.getLoad(ValTy, dl,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001492 DAG.getEntryNode(), GA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001493 false, false, false, 0);
Akira Hatanaka0f843822011-06-07 18:58:42 +00001494 // On functions and global targets not internal linked only
1495 // a load from got/GP is necessary for PIC to work.
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001496 if (!HasGotOfst)
Akira Hatanaka0f843822011-06-07 18:58:42 +00001497 return ResNode;
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001498 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, ValTy, 0,
1499 IsN64 ? MipsII::MO_GOT_OFST :
1500 MipsII::MO_ABS_LO);
1501 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, GALo);
1502 return DAG.getNode(ISD::ADD, dl, ValTy, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001503}
1504
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001505SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1506 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001507 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1508 // FIXME there isn't actually debug info here
1509 DebugLoc dl = Op.getDebugLoc();
1510
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001511 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001512 // %hi/%lo relocation
1513 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
1514 MipsII::MO_ABS_HI);
1515 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
1516 MipsII::MO_ABS_LO);
1517 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1518 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1519 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001520 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001521
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001522 EVT ValTy = Op.getValueType();
1523 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1524 unsigned OFSTFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1525 SDValue BAGOTOffset = DAG.getBlockAddress(BA, ValTy, true, GOTFlag);
1526 BAGOTOffset = DAG.getNode(MipsISD::WrapperPIC, dl, ValTy, BAGOTOffset);
1527 SDValue BALOOffset = DAG.getBlockAddress(BA, ValTy, true, OFSTFlag);
1528 SDValue Load = DAG.getLoad(ValTy, dl,
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001529 DAG.getEntryNode(), BAGOTOffset,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001530 MachinePointerInfo(), false, false, false, 0);
Akira Hatanaka9b944a82011-11-16 22:42:10 +00001531 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, BALOOffset);
1532 return DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001533}
1534
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001535SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001536LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001537{
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001538 // If the relocation model is PIC, use the General Dynamic TLS Model,
1539 // otherwise use the Initial Exec or Local Exec TLS Model.
1540 // TODO: implement Local Dynamic TLS model
1541
1542 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1543 DebugLoc dl = GA->getDebugLoc();
1544 const GlobalValue *GV = GA->getGlobal();
1545 EVT PtrVT = getPointerTy();
1546
1547 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1548 // General Dynamic TLS Model
1549 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001550 0, MipsII::MO_TLSGD);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001551 SDValue Tlsgd = DAG.getNode(MipsISD::TlsGd, dl, MVT::i32, TGA);
1552 SDValue GP = DAG.getRegister(Mips::GP, MVT::i32);
1553 SDValue Argument = DAG.getNode(ISD::ADD, dl, MVT::i32, GP, Tlsgd);
1554
1555 ArgListTy Args;
1556 ArgListEntry Entry;
1557 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001558 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001559 Args.push_back(Entry);
1560 std::pair<SDValue, SDValue> CallResult =
1561 LowerCallTo(DAG.getEntryNode(),
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001562 (Type *) Type::getInt32Ty(*DAG.getContext()),
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001563 false, false, false, false, 0, CallingConv::C, false, true,
1564 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG,
1565 dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001566
1567 return CallResult.first;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001568 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001569
1570 SDValue Offset;
1571 if (GV->isDeclaration()) {
1572 // Initial Exec TLS Model
1573 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1574 MipsII::MO_GOTTPREL);
1575 Offset = DAG.getLoad(MVT::i32, dl,
1576 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001577 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001578 } else {
1579 // Local Exec TLS Model
1580 SDVTList VTs = DAG.getVTList(MVT::i32);
1581 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1582 MipsII::MO_TPREL_HI);
1583 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1584 MipsII::MO_TPREL_LO);
1585 SDValue Hi = DAG.getNode(MipsISD::TprelHi, dl, VTs, &TGAHi, 1);
1586 SDValue Lo = DAG.getNode(MipsISD::TprelLo, dl, MVT::i32, TGALo);
1587 Offset = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1588 }
1589
1590 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1591 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001592}
1593
1594SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001595LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001596{
Dan Gohman475871a2008-07-27 21:46:04 +00001597 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001598 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +00001599 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001600 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001601 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001602 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001603
Owen Andersone50ed302009-08-10 22:56:29 +00001604 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001605 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001606
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001607 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
1608
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +00001609 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001610 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001611 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001612 } else {// Emit Load from Global Pointer
1613 JTI = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, JTI);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001614 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
1615 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001616 false, false, false, 0);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001617 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001618
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001619 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1620 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001621 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001622 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001623
1624 return ResNode;
1625}
1626
Dan Gohman475871a2008-07-27 21:46:04 +00001627SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001628LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001629{
Dan Gohman475871a2008-07-27 21:46:04 +00001630 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001631 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001632 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001633 // FIXME there isn't actually debug info here
1634 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001635
1636 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001637 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001638 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001639 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001640 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001641 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001642 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1643 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001644 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001645
1646 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001647 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001648 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001649 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001650 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001651 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1652 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001653 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001654 } else {
Akira Hatanaka620db892011-11-16 22:44:38 +00001655 EVT ValTy = Op.getValueType();
1656 unsigned GOTFlag = IsN64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
1657 unsigned OFSTFlag = IsN64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
1658 SDValue CP = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1659 N->getOffset(), GOTFlag);
1660 CP = DAG.getNode(MipsISD::WrapperPIC, dl, ValTy, CP);
1661 SDValue Load = DAG.getLoad(ValTy, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001662 CP, MachinePointerInfo::getConstantPool(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001663 false, false, false, 0);
Akira Hatanaka620db892011-11-16 22:44:38 +00001664 SDValue CPLo = DAG.getTargetConstantPool(C, ValTy, N->getAlignment(),
1665 N->getOffset(), OFSTFlag);
1666 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, ValTy, CPLo);
1667 ResNode = DAG.getNode(ISD::ADD, dl, ValTy, Load, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001668 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001669
1670 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001671}
1672
Dan Gohmand858e902010-04-17 15:26:15 +00001673SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001674 MachineFunction &MF = DAG.getMachineFunction();
1675 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1676
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001677 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001678 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1679 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001680
1681 // vastart just stores the address of the VarArgsFrameIndex slot into the
1682 // memory location argument.
1683 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001684 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1685 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +00001686 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001687}
1688
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001689static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
1690 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
1691 DebugLoc dl = Op.getDebugLoc();
1692 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
1693 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
1694 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
1695 DAG.getConstant(0x7fffffff, MVT::i32));
1696 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
1697 DAG.getConstant(0x80000000, MVT::i32));
1698 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1699 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
1700}
1701
1702static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001703 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001704 // Use ext/ins instructions if target architecture is Mips32r2.
1705 // Eliminate redundant mfc1 and mtc1 instructions.
1706 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001707
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001708 if (!isLittle)
1709 std::swap(LoIdx, HiIdx);
1710
1711 DebugLoc dl = Op.getDebugLoc();
1712 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1713 Op.getOperand(0),
1714 DAG.getConstant(LoIdx, MVT::i32));
1715 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1716 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1717 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1718 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1719 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1720 DAG.getConstant(0x7fffffff, MVT::i32));
1721 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1722 DAG.getConstant(0x80000000, MVT::i32));
1723 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1724
1725 if (!isLittle)
1726 std::swap(Word0, Word1);
1727
1728 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1729}
1730
1731SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1732 const {
1733 EVT Ty = Op.getValueType();
1734
1735 assert(Ty == MVT::f32 || Ty == MVT::f64);
1736
1737 if (Ty == MVT::f32)
1738 return LowerFCOPYSIGN32(Op, DAG);
1739 else
1740 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
1741}
1742
Akira Hatanaka2e591472011-06-02 00:24:44 +00001743SDValue MipsTargetLowering::
1744LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001745 // check the depth
1746 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001747 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001748
1749 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1750 MFI->setFrameAddressIsTaken(true);
1751 EVT VT = Op.getValueType();
1752 DebugLoc dl = Op.getDebugLoc();
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001753 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
1754 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001755 return FrameAddr;
1756}
1757
Akira Hatanakadb548262011-07-19 23:30:50 +00001758// TODO: set SType according to the desired memory barrier behavior.
1759SDValue MipsTargetLowering::LowerMEMBARRIER(SDValue Op,
1760 SelectionDAG& DAG) const {
1761 unsigned SType = 0;
1762 DebugLoc dl = Op.getDebugLoc();
1763 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1764 DAG.getConstant(SType, MVT::i32));
1765}
1766
Eli Friedman14648462011-07-27 22:21:52 +00001767SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1768 SelectionDAG& DAG) const {
1769 // FIXME: Need pseudo-fence for 'singlethread' fences
1770 // FIXME: Set SType for weaker fences where supported/appropriate.
1771 unsigned SType = 0;
1772 DebugLoc dl = Op.getDebugLoc();
1773 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1774 DAG.getConstant(SType, MVT::i32));
1775}
1776
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001777//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001778// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001779//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001780
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001781//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001782// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001783// Mips O32 ABI rules:
1784// ---
1785// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001786// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001787// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001788// f64 - Only passed in two aliased f32 registers if no int reg has been used
1789// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001790// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1791// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001792//
1793// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001794//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001795
Duncan Sands1e96bab2010-11-04 10:49:57 +00001796static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001797 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001798 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1799
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001800 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001801
1802 static const unsigned IntRegs[] = {
1803 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1804 };
1805 static const unsigned F32Regs[] = {
1806 Mips::F12, Mips::F14
1807 };
1808 static const unsigned F64Regs[] = {
1809 Mips::D6, Mips::D7
1810 };
1811
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001812 // ByVal Args
1813 if (ArgFlags.isByVal()) {
1814 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1815 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1816 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1817 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1818 r < std::min(IntRegsSize, NextReg); ++r)
1819 State.AllocateReg(IntRegs[r]);
1820 return false;
1821 }
1822
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001823 // Promote i8 and i16
1824 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1825 LocVT = MVT::i32;
1826 if (ArgFlags.isSExt())
1827 LocInfo = CCValAssign::SExt;
1828 else if (ArgFlags.isZExt())
1829 LocInfo = CCValAssign::ZExt;
1830 else
1831 LocInfo = CCValAssign::AExt;
1832 }
1833
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001834 unsigned Reg;
1835
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001836 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1837 // is true: function is vararg, argument is 3rd or higher, there is previous
1838 // argument which is not f32 or f64.
1839 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1840 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001841 unsigned OrigAlign = ArgFlags.getOrigAlign();
1842 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001843
1844 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001845 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001846 // If this is the first part of an i64 arg,
1847 // the allocated register must be either A0 or A2.
1848 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1849 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001850 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001851 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1852 // Allocate int register and shadow next int register. If first
1853 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001854 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1855 if (Reg == Mips::A1 || Reg == Mips::A3)
1856 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1857 State.AllocateReg(IntRegs, IntRegsSize);
1858 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001859 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1860 // we are guaranteed to find an available float register
1861 if (ValVT == MVT::f32) {
1862 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1863 // Shadow int register
1864 State.AllocateReg(IntRegs, IntRegsSize);
1865 } else {
1866 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1867 // Shadow int registers
1868 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1869 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1870 State.AllocateReg(IntRegs, IntRegsSize);
1871 State.AllocateReg(IntRegs, IntRegsSize);
1872 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001873 } else
1874 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001875
Akira Hatanakad37776d2011-05-20 21:39:54 +00001876 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1877 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1878
1879 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001880 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001881 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001882 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001883
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001884 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001885}
1886
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00001887static const unsigned Mips64IntRegs[8] =
1888 {Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
1889 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64};
1890static const unsigned Mips64DPRegs[8] =
1891 {Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
1892 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64};
1893
1894static bool CC_Mips64Byval(unsigned ValNo, MVT ValVT, MVT LocVT,
1895 CCValAssign::LocInfo LocInfo,
1896 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1897 unsigned Align = std::max(ArgFlags.getByValAlign(), (unsigned)8);
1898 unsigned Size = (ArgFlags.getByValSize() + 7) / 8 * 8;
1899 unsigned FirstIdx = State.getFirstUnallocated(Mips64IntRegs, 8);
1900
1901 assert(Align <= 16 && "Cannot handle alignments larger than 16.");
1902
1903 // If byval is 16-byte aligned, the first arg register must be even.
1904 if ((Align == 16) && (FirstIdx % 2)) {
1905 State.AllocateReg(Mips64IntRegs[FirstIdx], Mips64DPRegs[FirstIdx]);
1906 ++FirstIdx;
1907 }
1908
1909 // Mark the registers allocated.
1910 for (unsigned I = FirstIdx; Size && (I < 8); Size -= 8, ++I)
1911 State.AllocateReg(Mips64IntRegs[I], Mips64DPRegs[I]);
1912
1913 // Allocate space on caller's stack.
1914 unsigned Offset = State.AllocateStack(Size, Align);
1915
1916 if (FirstIdx < 8)
1917 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Mips64IntRegs[FirstIdx],
1918 LocVT, LocInfo));
1919 else
1920 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
1921
1922 return true;
1923}
1924
1925#include "MipsGenCallingConv.inc"
1926
Akira Hatanaka49617092011-11-14 19:02:54 +00001927static void
1928AnalyzeMips64CallOperands(CCState CCInfo,
1929 const SmallVectorImpl<ISD::OutputArg> &Outs) {
1930 unsigned NumOps = Outs.size();
1931 for (unsigned i = 0; i != NumOps; ++i) {
1932 MVT ArgVT = Outs[i].VT;
1933 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
1934 bool R;
1935
1936 if (Outs[i].IsFixed)
1937 R = CC_MipsN(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
1938 else
1939 R = CC_MipsN_VarArg(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
1940
Akira Hatanaka49617092011-11-14 19:02:54 +00001941 if (R) {
Benjamin Kramer6296ee32011-11-14 19:51:48 +00001942#ifndef NDEBUG
Akira Hatanaka49617092011-11-14 19:02:54 +00001943 dbgs() << "Call operand #" << i << " has unhandled type "
1944 << EVT(ArgVT).getEVTString();
1945#endif
1946 llvm_unreachable(0);
1947 }
1948 }
1949}
1950
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001951//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001952// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001953//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001954
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001955static const unsigned O32IntRegsSize = 4;
1956
1957static const unsigned O32IntRegs[] = {
1958 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1959};
1960
Akira Hatanaka373e3a42011-09-23 00:58:33 +00001961// Return next O32 integer argument register.
1962static unsigned getNextIntArgReg(unsigned Reg) {
1963 assert((Reg == Mips::A0) || (Reg == Mips::A2));
1964 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
1965}
1966
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001967// Write ByVal Arg to arg registers and stack.
1968static void
Akira Hatanakada7f5f12011-09-19 20:26:02 +00001969WriteByValArg(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001970 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1971 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1972 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001973 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001974 MVT PtrType, bool isLittle) {
1975 unsigned LocMemOffset = VA.getLocMemOffset();
1976 unsigned Offset = 0;
1977 uint32_t RemainingSize = Flags.getByValSize();
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001978 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001979
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001980 // Copy the first 4 words of byval arg to registers A0 - A3.
1981 // FIXME: Use a stricter alignment if it enables better optimization in passes
1982 // run later.
1983 for (; RemainingSize >= 4 && LocMemOffset < 4 * 4;
1984 Offset += 4, RemainingSize -= 4, LocMemOffset += 4) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001985 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001986 DAG.getConstant(Offset, MVT::i32));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001987 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1988 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001989 false, false, false, std::min(ByValAlign,
1990 (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001991 MemOpChains.push_back(LoadVal.getValue(1));
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001992 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001993 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1994 }
1995
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001996 if (RemainingSize == 0)
1997 return;
1998
1999 // If there still is a register available for argument passing, write the
2000 // remaining part of the structure to it using subword loads and shifts.
2001 if (LocMemOffset < 4 * 4) {
2002 assert(RemainingSize <= 3 && RemainingSize >= 1 &&
2003 "There must be one to three bytes remaining.");
2004 unsigned LoadSize = (RemainingSize == 3 ? 2 : RemainingSize);
2005 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2006 DAG.getConstant(Offset, MVT::i32));
2007 unsigned Alignment = std::min(ByValAlign, (unsigned )4);
2008 SDValue LoadVal = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2009 LoadPtr, MachinePointerInfo(),
2010 MVT::getIntegerVT(LoadSize * 8), false,
2011 false, Alignment);
2012 MemOpChains.push_back(LoadVal.getValue(1));
2013
2014 // If target is big endian, shift it to the most significant half-word or
2015 // byte.
2016 if (!isLittle)
2017 LoadVal = DAG.getNode(ISD::SHL, dl, MVT::i32, LoadVal,
2018 DAG.getConstant(32 - LoadSize * 8, MVT::i32));
2019
2020 Offset += LoadSize;
2021 RemainingSize -= LoadSize;
2022
2023 // Read second subword if necessary.
2024 if (RemainingSize != 0) {
2025 assert(RemainingSize == 1 && "There must be one byte remaining.");
2026 LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2027 DAG.getConstant(Offset, MVT::i32));
2028 unsigned Alignment = std::min(ByValAlign, (unsigned )2);
2029 SDValue Subword = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
2030 LoadPtr, MachinePointerInfo(),
2031 MVT::i8, false, false, Alignment);
2032 MemOpChains.push_back(Subword.getValue(1));
2033 // Insert the loaded byte to LoadVal.
2034 // FIXME: Use INS if supported by target.
2035 unsigned ShiftAmt = isLittle ? 16 : 8;
2036 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i32, Subword,
2037 DAG.getConstant(ShiftAmt, MVT::i32));
2038 LoadVal = DAG.getNode(ISD::OR, dl, MVT::i32, LoadVal, Shift);
2039 }
2040
2041 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
2042 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
2043 return;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002044 }
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002045
2046 // Create a fixed object on stack at offset LocMemOffset and copy
2047 // remaining part of byval arg to it using memcpy.
2048 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
2049 DAG.getConstant(Offset, MVT::i32));
2050 LastFI = MFI->CreateFixedObject(RemainingSize, LocMemOffset, true);
2051 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002052 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2053 DAG.getConstant(RemainingSize, MVT::i32),
2054 std::min(ByValAlign, (unsigned)4),
2055 /*isVolatile=*/false, /*AlwaysInline=*/false,
2056 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002057}
2058
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002059// Copy Mips64 byVal arg to registers and stack.
2060void static
2061PassByValArg64(SDValue& ByValChain, SDValue Chain, DebugLoc dl,
2062 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
2063 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
2064 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
2065 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2066 EVT PtrTy, bool isLittle) {
2067 unsigned ByValSize = Flags.getByValSize();
2068 unsigned Alignment = std::min(Flags.getByValAlign(), (unsigned)8);
2069 bool IsRegLoc = VA.isRegLoc();
2070 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
2071 unsigned LocMemOffset = 0;
Akira Hatanaka16040852011-11-15 18:42:25 +00002072 unsigned MemCpySize = ByValSize;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002073
2074 if (!IsRegLoc)
2075 LocMemOffset = VA.getLocMemOffset();
2076 else {
2077 const unsigned *Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8,
2078 VA.getLocReg());
2079 const unsigned *RegEnd = Mips64IntRegs + 8;
2080
2081 // Copy double words to registers.
2082 for (; (Reg != RegEnd) && (ByValSize >= Offset + 8); ++Reg, Offset += 8) {
2083 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2084 DAG.getConstant(Offset, PtrTy));
2085 SDValue LoadVal = DAG.getLoad(MVT::i64, dl, Chain, LoadPtr,
2086 MachinePointerInfo(), false, false, false,
2087 Alignment);
2088 MemOpChains.push_back(LoadVal.getValue(1));
2089 RegsToPass.push_back(std::make_pair(*Reg, LoadVal));
2090 }
2091
Akira Hatanaka16040852011-11-15 18:42:25 +00002092 // Return if the struct has been fully copied.
2093 if (!(MemCpySize = ByValSize - Offset))
2094 return;
2095
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002096 // If there is an argument register available, copy the remainder of the
2097 // byval argument with sub-doubleword loads and shifts.
Akira Hatanaka16040852011-11-15 18:42:25 +00002098 if (Reg != RegEnd) {
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002099 assert((ByValSize < Offset + 8) &&
2100 "Size of the remainder should be smaller than 8-byte.");
2101 SDValue Val;
2102 for (unsigned LoadSize = 4; Offset < ByValSize; LoadSize /= 2) {
2103 unsigned RemSize = ByValSize - Offset;
2104
2105 if (RemSize < LoadSize)
2106 continue;
2107
2108 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2109 DAG.getConstant(Offset, PtrTy));
2110 SDValue LoadVal =
2111 DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i64, Chain, LoadPtr,
2112 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
2113 false, false, Alignment);
2114 MemOpChains.push_back(LoadVal.getValue(1));
2115
2116 // Offset in number of bits from double word boundary.
2117 unsigned OffsetDW = (Offset % 8) * 8;
2118 unsigned Shamt = isLittle ? OffsetDW : 64 - (OffsetDW + LoadSize * 8);
2119 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i64, LoadVal,
2120 DAG.getConstant(Shamt, MVT::i32));
2121
2122 Val = Val.getNode() ? DAG.getNode(ISD::OR, dl, MVT::i64, Val, Shift) :
2123 Shift;
2124 Offset += LoadSize;
2125 Alignment = std::min(Alignment, LoadSize);
2126 }
2127
2128 RegsToPass.push_back(std::make_pair(*Reg, Val));
2129 return;
2130 }
2131 }
2132
Akira Hatanaka16040852011-11-15 18:42:25 +00002133 assert(MemCpySize && "MemCpySize must not be zero.");
2134
2135 // Create a fixed object on stack at offset LocMemOffset and copy
2136 // remainder of byval arg to it with memcpy.
2137 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrTy, Arg,
2138 DAG.getConstant(Offset, PtrTy));
2139 LastFI = MFI->CreateFixedObject(MemCpySize, LocMemOffset, true);
2140 SDValue Dst = DAG.getFrameIndex(LastFI, PtrTy);
2141 ByValChain = DAG.getMemcpy(ByValChain, dl, Dst, Src,
2142 DAG.getConstant(MemCpySize, PtrTy), Alignment,
2143 /*isVolatile=*/false, /*AlwaysInline=*/false,
2144 MachinePointerInfo(0), MachinePointerInfo(0));
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002145}
2146
Dan Gohman98ca4f22009-08-05 01:29:28 +00002147/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002148/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002149/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002150SDValue
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002151MipsTargetLowering::LowerCall(SDValue InChain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002152 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002153 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002154 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002155 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002156 const SmallVectorImpl<ISD::InputArg> &Ins,
2157 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002158 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002159 // MIPs target does not yet support tail call optimization.
2160 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002161
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002162 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002163 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002164 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002165 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00002166 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002167
2168 // Analyze operands of the call, assigning locations to each operand.
2169 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002170 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2171 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002172
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002173 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002174 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanaka49617092011-11-14 19:02:54 +00002175 else if (HasMips64)
2176 AnalyzeMips64CallOperands(CCInfo, Outs);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00002177 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002178 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002179
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002180 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002181 unsigned NextStackOffset = CCInfo.getNextStackOffset();
2182
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002183 // Chain is the output chain of the last Load/Store or CopyToReg node.
2184 // ByValChain is the output chain of the last Memcpy node created for copying
2185 // byval arguments to the stack.
2186 SDValue Chain, CallSeqStart, ByValChain;
2187 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
2188 Chain = CallSeqStart = DAG.getCALLSEQ_START(InChain, NextStackOffsetVal);
2189 ByValChain = InChain;
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002190
2191 // If this is the first call, create a stack frame object that points to
2192 // a location to which .cprestore saves $gp.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002193 if (IsO32 && IsPIC && !MipsFI->getGPFI())
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002194 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
2195
Akira Hatanaka21afc632011-06-21 00:40:49 +00002196 // Get the frame index of the stack frame object that points to the location
2197 // of dynamically allocated area on the stack.
2198 int DynAllocFI = MipsFI->getDynAllocFI();
2199
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002200 // Update size of the maximum argument space.
2201 // For O32, a minimum of four words (16 bytes) of argument space is
2202 // allocated.
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002203 if (IsO32)
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002204 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
2205
2206 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
2207
2208 if (MaxCallFrameSize < NextStackOffset) {
2209 MipsFI->setMaxCallFrameSize(NextStackOffset);
2210
Akira Hatanaka21afc632011-06-21 00:40:49 +00002211 // Set the offsets relative to $sp of the $gp restore slot and dynamically
2212 // allocated stack space. These offsets must be aligned to a boundary
2213 // determined by the stack alignment of the ABI.
2214 unsigned StackAlignment = TFL->getStackAlignment();
2215 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
2216 StackAlignment * StackAlignment;
2217
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002218 if (MipsFI->needGPSaveRestore())
Akira Hatanaka21afc632011-06-21 00:40:49 +00002219 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
2220
2221 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002222 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002223
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002224 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00002225 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
2226 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002227
Eric Christopher471e4222011-06-08 23:55:35 +00002228 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00002229
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002230 // Walk the register/memloc assignments, inserting copies/loads.
2231 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002232 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002233 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002234 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002235 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2236
2237 // ByVal Arg.
2238 if (Flags.isByVal()) {
2239 assert(Flags.getByValSize() &&
2240 "ByVal args of size 0 should have been ignored by front-end.");
2241 if (IsO32)
2242 WriteByValArg(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2243 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2244 Subtarget->isLittle());
2245 else
2246 PassByValArg64(ByValChain, Chain, dl, RegsToPass, MemOpChains, LastFI,
2247 MFI, DAG, Arg, VA, Flags, getPointerTy(),
2248 Subtarget->isLittle());
2249 continue;
2250 }
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002251
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002252 // Promote the value if needed.
2253 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002254 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002255 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002256 if (VA.isRegLoc()) {
2257 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
2258 (ValVT == MVT::f64 && LocVT == MVT::i64))
2259 Arg = DAG.getNode(ISD::BITCAST, dl, LocVT, Arg);
2260 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002261 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2262 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002263 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
2264 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002265 if (!Subtarget->isLittle())
2266 std::swap(Lo, Hi);
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002267 unsigned LocRegLo = VA.getLocReg();
2268 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2269 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2270 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002271 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002272 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002273 }
2274 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002275 case CCValAssign::SExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002276 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002277 break;
2278 case CCValAssign::ZExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002279 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002280 break;
2281 case CCValAssign::AExt:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002282 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002283 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002284 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002285
2286 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002287 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002288 if (VA.isRegLoc()) {
2289 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002290 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002291 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002292
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002293 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002294 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002295
Chris Lattnere0b12152008-03-17 06:57:02 +00002296 // Create the frame index object for this incoming parameter
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002297 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002298 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00002299 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00002300
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002301 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002302 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00002303 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
2304 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00002305 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002306 }
2307
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002308 // Extend range of indices of frame objects for outgoing arguments that were
2309 // created during this function call. Skip this step if no such objects were
2310 // created.
2311 if (LastFI)
2312 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2313
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002314 // If a memcpy has been created to copy a byval arg to a stack, replace the
2315 // chain input of CallSeqStart with ByValChain.
2316 if (InChain != ByValChain)
2317 DAG.UpdateNodeOperands(CallSeqStart.getNode(), ByValChain,
2318 NextStackOffsetVal);
2319
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002320 // Transform all store nodes into one single node because all store
2321 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002322 if (!MemOpChains.empty())
2323 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002324 &MemOpChains[0], MemOpChains.size());
2325
Bill Wendling056292f2008-09-16 21:48:12 +00002326 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002327 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2328 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002329 unsigned char OpFlag;
2330 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002331 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002332 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002333
2334 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002335 if (IsPICCall && G->getGlobal()->hasInternalLinkage()) {
2336 OpFlag = IsO32 ? MipsII::MO_GOT : MipsII::MO_GOT_PAGE;
2337 unsigned char LoFlag = IsO32 ? MipsII::MO_ABS_LO : MipsII::MO_GOT_OFST;
2338 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(), 0,
2339 OpFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002340 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002341 0, LoFlag);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002342 } else {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002343 OpFlag = IsPICCall ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002344 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2345 getPointerTy(), 0, OpFlag);
2346 }
2347
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002348 LoadSymAddr = true;
2349 }
2350 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002351 if (IsN64 || (!IsO32 && IsPIC))
2352 OpFlag = MipsII::MO_GOT_DISP;
2353 else if (!IsPIC) // !N64 && static
2354 OpFlag = MipsII::MO_NO_FLAG;
2355 else // O32 & PIC
2356 OpFlag = MipsII::MO_GOT_CALL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002357 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002358 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002359 LoadSymAddr = true;
2360 }
2361
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002362 SDValue InFlag;
2363
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002364 // Create nodes that load address of callee and copy it to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002365 if (IsPICCall) {
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002366 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002367 // Load callee address
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002368 Callee = DAG.getNode(MipsISD::WrapperPIC, dl, getPointerTy(), Callee);
2369 SDValue LoadValue = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
2370 Callee, MachinePointerInfo::getGOT(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002371 false, false, false, 0);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002372
2373 // Use GOT+LO if callee has internal linkage.
2374 if (CalleeLo.getNode()) {
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002375 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, getPointerTy(), CalleeLo);
2376 Callee = DAG.getNode(ISD::ADD, dl, getPointerTy(), LoadValue, Lo);
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002377 } else
2378 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002379 }
2380
2381 // copy to T9
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002382 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
2383 Chain = DAG.getCopyToReg(Chain, dl, T9Reg, Callee, SDValue(0, 0));
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002384 InFlag = Chain.getValue(1);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002385 Callee = DAG.getRegister(T9Reg, getPointerTy());
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002386 }
Bill Wendling056292f2008-09-16 21:48:12 +00002387
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002388 // Build a sequence of copy-to-reg nodes chained together with token
2389 // chain and flag operands which copy the outgoing args into registers.
2390 // The InFlag in necessary since all emitted instructions must be
2391 // stuck together.
2392 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2393 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2394 RegsToPass[i].second, InFlag);
2395 InFlag = Chain.getValue(1);
2396 }
2397
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002398 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002399 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002400 //
2401 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002402 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002403 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002404 Ops.push_back(Chain);
2405 Ops.push_back(Callee);
2406
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002407 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002408 // known live into the call.
2409 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2410 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2411 RegsToPass[i].second.getValueType()));
2412
Gabor Greifba36cb52008-08-28 21:40:38 +00002413 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002414 Ops.push_back(InFlag);
2415
Dale Johannesen33c960f2009-02-04 20:06:27 +00002416 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002417 InFlag = Chain.getValue(1);
2418
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002419 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002420 Chain = DAG.getCALLSEQ_END(Chain,
2421 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002422 DAG.getIntPtrConstant(0, true), InFlag);
2423 InFlag = Chain.getValue(1);
2424
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002425 // Handle result values, copying them out of physregs into vregs that we
2426 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002427 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2428 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002429}
2430
Dan Gohman98ca4f22009-08-05 01:29:28 +00002431/// LowerCallResult - Lower the result values of a call into the
2432/// appropriate copies out of appropriate physical registers.
2433SDValue
2434MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002435 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002436 const SmallVectorImpl<ISD::InputArg> &Ins,
2437 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002438 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002439 // Assign locations to each value returned by this call.
2440 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002441 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2442 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002443
Dan Gohman98ca4f22009-08-05 01:29:28 +00002444 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002445
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002446 // Copy all of the result registers out of their specified physreg.
2447 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002448 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002449 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002450 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002451 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002452 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002453
Dan Gohman98ca4f22009-08-05 01:29:28 +00002454 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002455}
2456
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002457//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002458// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002459//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002460static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2461 std::vector<SDValue>& OutChains,
2462 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2463 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2464 unsigned LocMem = VA.getLocMemOffset();
2465 unsigned FirstWord = LocMem / 4;
2466
2467 // copy register A0 - A3 to frame object
2468 for (unsigned i = 0; i < NumWords; ++i) {
2469 unsigned CurWord = FirstWord + i;
2470 if (CurWord >= O32IntRegsSize)
2471 break;
2472
2473 unsigned SrcReg = O32IntRegs[CurWord];
2474 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2475 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2476 DAG.getConstant(i * 4, MVT::i32));
2477 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2478 StorePtr, MachinePointerInfo(), false,
2479 false, 0);
2480 OutChains.push_back(Store);
2481 }
2482}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002483
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002484// Create frame object on stack and copy registers used for byval passing to it.
2485static unsigned
2486CopyMips64ByValRegs(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2487 std::vector<SDValue>& OutChains, SelectionDAG &DAG,
2488 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
2489 MachineFrameInfo *MFI, bool IsRegLoc,
2490 SmallVectorImpl<SDValue> &InVals, MipsFunctionInfo *MipsFI,
2491 EVT PtrTy) {
2492 const unsigned *Reg = Mips64IntRegs + 8;
2493 int FOOffset; // Frame object offset from virtual frame pointer.
2494
2495 if (IsRegLoc) {
2496 Reg = std::find(Mips64IntRegs, Mips64IntRegs + 8, VA.getLocReg());
2497 FOOffset = (Reg - Mips64IntRegs) * 8 - 8 * 8;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002498 }
2499 else
2500 FOOffset = VA.getLocMemOffset();
2501
2502 // Create frame object.
2503 unsigned NumRegs = (Flags.getByValSize() + 7) / 8;
2504 unsigned LastFI = MFI->CreateFixedObject(NumRegs * 8, FOOffset, true);
2505 SDValue FIN = DAG.getFrameIndex(LastFI, PtrTy);
2506 InVals.push_back(FIN);
2507
2508 // Copy arg registers.
2509 for (unsigned I = 0; (Reg != Mips64IntRegs + 8) && (I < NumRegs);
2510 ++Reg, ++I) {
2511 unsigned VReg = AddLiveIn(MF, *Reg, Mips::CPU64RegsRegisterClass);
2512 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, PtrTy, FIN,
2513 DAG.getConstant(I * 8, PtrTy));
2514 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(VReg, MVT::i64),
2515 StorePtr, MachinePointerInfo(), false,
2516 false, 0);
2517 OutChains.push_back(Store);
2518 }
2519
2520 return LastFI;
2521}
2522
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002523/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002524/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002525SDValue
2526MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002527 CallingConv::ID CallConv,
2528 bool isVarArg,
2529 const SmallVectorImpl<ISD::InputArg>
2530 &Ins,
2531 DebugLoc dl, SelectionDAG &DAG,
2532 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002533 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002534 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002535 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002536 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002537
Dan Gohman1e93df62010-04-17 14:41:14 +00002538 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002539
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002540 // Used with vargs to acumulate store chains.
2541 std::vector<SDValue> OutChains;
2542
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002543 // Assign locations to all of the incoming arguments.
2544 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002545 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2546 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002547
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +00002548 if (IsO32)
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002549 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002550 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002551 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002552
Akira Hatanaka43299772011-05-20 23:22:14 +00002553 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002554
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002555 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002556 CCValAssign &VA = ArgLocs[i];
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002557 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002558 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2559 bool IsRegLoc = VA.isRegLoc();
2560
2561 if (Flags.isByVal()) {
2562 assert(Flags.getByValSize() &&
2563 "ByVal args of size 0 should have been ignored by front-end.");
2564 if (IsO32) {
2565 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2566 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2567 true);
2568 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2569 InVals.push_back(FIN);
2570 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2571 } else // N32/64
2572 LastFI = CopyMips64ByValRegs(MF, Chain, dl, OutChains, DAG, VA, Flags,
2573 MFI, IsRegLoc, InVals, MipsFI,
2574 getPointerTy());
2575 continue;
2576 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002577
2578 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002579 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002580 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002581 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002582 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002583
Owen Anderson825b72b2009-08-11 20:47:22 +00002584 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002585 RC = Mips::CPURegsRegisterClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002586 else if (RegVT == MVT::i64)
2587 RC = Mips::CPU64RegsRegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002588 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002589 RC = Mips::FGR32RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002590 else if (RegVT == MVT::f64)
Akira Hatanakaf40de9d2011-09-26 21:55:17 +00002591 RC = HasMips64 ? Mips::FGR64RegisterClass : Mips::AFGR64RegisterClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002592 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002593 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002594
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002595 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002596 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002597 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002598 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002599
2600 // If this is an 8 or 16-bit value, it has been passed promoted
2601 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002602 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002603 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002604 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002605 if (VA.getLocInfo() == CCValAssign::SExt)
2606 Opcode = ISD::AssertSext;
2607 else if (VA.getLocInfo() == CCValAssign::ZExt)
2608 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002609 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002610 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002611 DAG.getValueType(ValVT));
2612 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002613 }
2614
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002615 // Handle floating point arguments passed in integer registers.
2616 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
2617 (RegVT == MVT::i64 && ValVT == MVT::f64))
2618 ArgValue = DAG.getNode(ISD::BITCAST, dl, ValVT, ArgValue);
2619 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
2620 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
2621 getNextIntArgReg(ArgReg), RC);
2622 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
2623 if (!Subtarget->isLittle())
2624 std::swap(ArgValue, ArgValue2);
2625 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2626 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002627 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002628
Dan Gohman98ca4f22009-08-05 01:29:28 +00002629 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002630 } else { // VA.isRegLoc()
2631
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002632 // sanity check
2633 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002634
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002635 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002636 LastFI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002637 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002638
2639 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002640 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002641 InVals.push_back(DAG.getLoad(ValVT, dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002642 MachinePointerInfo::getFixedStack(LastFI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002643 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002644 }
2645 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002646
2647 // The mips ABIs for returning structs by value requires that we copy
2648 // the sret argument into $v0 for the return. Save the argument into
2649 // a virtual register so that we can access it from the return points.
2650 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2651 unsigned Reg = MipsFI->getSRetReturnReg();
2652 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002653 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002654 MipsFI->setSRetReturnReg(Reg);
2655 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002656 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002657 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002658 }
2659
Akira Hatanakabad53f42011-11-14 19:01:09 +00002660 if (isVarArg) {
2661 unsigned NumOfRegs = IsO32 ? 4 : 8;
2662 const unsigned *ArgRegs = IsO32 ? O32IntRegs : Mips64IntRegs;
2663 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumOfRegs);
2664 int FirstRegSlotOffset = IsO32 ? 0 : -64 ; // offset of $a0's slot.
2665 TargetRegisterClass *RC
2666 = IsO32 ? Mips::CPURegsRegisterClass : Mips::CPU64RegsRegisterClass;
2667 unsigned RegSize = RC->getSize();
2668 int RegSlotOffset = FirstRegSlotOffset + Idx * RegSize;
2669
2670 // Offset of the first variable argument from stack pointer.
2671 int FirstVaArgOffset;
2672
2673 if (IsO32 || (Idx == NumOfRegs)) {
2674 FirstVaArgOffset =
2675 (CCInfo.getNextStackOffset() + RegSize - 1) / RegSize * RegSize;
2676 } else
2677 FirstVaArgOffset = RegSlotOffset;
2678
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002679 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002680 // which is a value necessary to VASTART.
Akira Hatanakabad53f42011-11-14 19:01:09 +00002681 LastFI = MFI->CreateFixedObject(RegSize, FirstVaArgOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002682 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002683
Akira Hatanakabad53f42011-11-14 19:01:09 +00002684 // Copy the integer registers that have not been used for argument passing
2685 // to the argument register save area. For O32, the save area is allocated
2686 // in the caller's stack frame, while for N32/64, it is allocated in the
2687 // callee's stack frame.
2688 for (int StackOffset = RegSlotOffset;
2689 Idx < NumOfRegs; ++Idx, StackOffset += RegSize) {
2690 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgRegs[Idx], RC);
2691 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg,
2692 MVT::getIntegerVT(RegSize * 8));
2693 LastFI = MFI->CreateFixedObject(RegSize, StackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002694 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2695 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2696 MachinePointerInfo(),
2697 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002698 }
2699 }
2700
Akira Hatanaka43299772011-05-20 23:22:14 +00002701 MipsFI->setLastInArgFI(LastFI);
2702
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002703 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002704 // the size of Ins and InVals. This only happens when on varg functions
2705 if (!OutChains.empty()) {
2706 OutChains.push_back(Chain);
2707 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2708 &OutChains[0], OutChains.size());
2709 }
2710
Dan Gohman98ca4f22009-08-05 01:29:28 +00002711 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002712}
2713
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002714//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002715// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002716//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002717
Dan Gohman98ca4f22009-08-05 01:29:28 +00002718SDValue
2719MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002720 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002721 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002722 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002723 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002724
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002725 // CCValAssign - represent the assignment of
2726 // the return value to a location
2727 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002728
2729 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002730 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2731 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002732
Dan Gohman98ca4f22009-08-05 01:29:28 +00002733 // Analize return values.
2734 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002735
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002736 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002737 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002738 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002739 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002740 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002741 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002742 }
2743
Dan Gohman475871a2008-07-27 21:46:04 +00002744 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002745
2746 // Copy the result values into the output registers.
2747 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2748 CCValAssign &VA = RVLocs[i];
2749 assert(VA.isRegLoc() && "Can only return in registers!");
2750
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002751 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002752 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002753
2754 // guarantee that all emitted copies are
2755 // stuck together, avoiding something bad
2756 Flag = Chain.getValue(1);
2757 }
2758
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002759 // The mips ABIs for returning structs by value requires that we copy
2760 // the sret argument into $v0 for the return. We saved the argument into
2761 // a virtual register in the entry block, so now we copy the value out
2762 // and into $v0.
2763 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2764 MachineFunction &MF = DAG.getMachineFunction();
2765 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2766 unsigned Reg = MipsFI->getSRetReturnReg();
2767
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002768 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002769 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002770 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002771
Dale Johannesena05dca42009-02-04 23:02:30 +00002772 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002773 Flag = Chain.getValue(1);
2774 }
2775
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002776 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002777 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002778 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002779 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002780 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002781 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002782 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002783}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002784
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002785//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002786// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002787//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002788
2789/// getConstraintType - Given a constraint letter, return the type of
2790/// constraint it is for this target.
2791MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002792getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002793{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002794 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002795 // GCC config/mips/constraints.md
2796 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002797 // 'd' : An address register. Equivalent to r
2798 // unless generating MIPS16 code.
2799 // 'y' : Equivalent to r; retained for
2800 // backwards compatibility.
2801 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002802 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002803 switch (Constraint[0]) {
2804 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002805 case 'd':
2806 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002807 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002808 return C_RegisterClass;
2809 break;
2810 }
2811 }
2812 return TargetLowering::getConstraintType(Constraint);
2813}
2814
John Thompson44ab89e2010-10-29 17:29:13 +00002815/// Examine constraint type and operand type and determine a weight value.
2816/// This object must already have been set up with the operand type
2817/// and the current alternative constraint selected.
2818TargetLowering::ConstraintWeight
2819MipsTargetLowering::getSingleConstraintMatchWeight(
2820 AsmOperandInfo &info, const char *constraint) const {
2821 ConstraintWeight weight = CW_Invalid;
2822 Value *CallOperandVal = info.CallOperandVal;
2823 // If we don't have a value, we can't do a match,
2824 // but allow it at the lowest weight.
2825 if (CallOperandVal == NULL)
2826 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002827 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002828 // Look at the constraint type.
2829 switch (*constraint) {
2830 default:
2831 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2832 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002833 case 'd':
2834 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002835 if (type->isIntegerTy())
2836 weight = CW_Register;
2837 break;
2838 case 'f':
2839 if (type->isFloatTy())
2840 weight = CW_Register;
2841 break;
2842 }
2843 return weight;
2844}
2845
Eric Christopher38d64262011-06-29 19:33:04 +00002846/// Given a register class constraint, like 'r', if this corresponds directly
2847/// to an LLVM register class, return a register of 0 and the register class
2848/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002849std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002850getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002851{
2852 if (Constraint.size() == 1) {
2853 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002854 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2855 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002856 case 'r':
2857 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002858 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002859 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002860 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002861 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002862 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2863 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002864 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002865 }
2866 }
2867 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2868}
2869
Dan Gohman6520e202008-10-18 02:06:02 +00002870bool
2871MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2872 // The Mips target isn't yet aware of offsets.
2873 return false;
2874}
Evan Chengeb2f9692009-10-27 19:56:55 +00002875
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002876bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2877 if (VT != MVT::f32 && VT != MVT::f64)
2878 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002879 if (Imm.isNegZero())
2880 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002881 return Imm.isZero();
2882}