blob: f2682cfd249b718034d6bab6c9b35dc83f157fbe [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 Lopes972f5892007-06-06 07:42:06 +000027#include "llvm/CodeGen/CallingConvLower.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000032#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033#include "llvm/CodeGen/ValueTypes.h"
34#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000036using namespace llvm;
37
Akira Hatanakadbe9a312011-08-18 20:07:42 +000038// If I is a shifted mask, set the size (Size) and the first bit of the
39// mask (Pos), and return true.
40static bool IsShiftedMask(uint64_t I, unsigned SizeInBits, uint64_t &Pos,
41 uint64_t &Size) {
42 assert(SizeInBits == 32 || SizeInBits == 64);
43 bool Is32Bits = (SizeInBits == 32);
Akira Hatanakabb15e112011-08-17 02:05:42 +000044
Akira Hatanakadbe9a312011-08-18 20:07:42 +000045 if ((Is32Bits == 32 && !isShiftedMask_32(I)) ||
46 (!Is32Bits && !isShiftedMask_64(I)))
47 return false;
Akira Hatanakabb15e112011-08-17 02:05:42 +000048
Akira Hatanakadbe9a312011-08-18 20:07:42 +000049 Size = Is32Bits ? CountPopulation_32(I) : CountPopulation_64(I);
50 Pos = Is32Bits ? CountTrailingZeros_32(I) : CountTrailingZeros_64(I);
51 return true;
Akira Hatanakabb15e112011-08-17 02:05:42 +000052}
53
Chris Lattnerf0144122009-07-28 03:13:23 +000054const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
55 switch (Opcode) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000056 case MipsISD::JmpLink: return "MipsISD::JmpLink";
57 case MipsISD::Hi: return "MipsISD::Hi";
58 case MipsISD::Lo: return "MipsISD::Lo";
59 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +000060 case MipsISD::TlsGd: return "MipsISD::TlsGd";
61 case MipsISD::TprelHi: return "MipsISD::TprelHi";
62 case MipsISD::TprelLo: return "MipsISD::TprelLo";
63 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +000064 case MipsISD::Ret: return "MipsISD::Ret";
65 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
66 case MipsISD::FPCmp: return "MipsISD::FPCmp";
67 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
68 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
69 case MipsISD::FPRound: return "MipsISD::FPRound";
70 case MipsISD::MAdd: return "MipsISD::MAdd";
71 case MipsISD::MAddu: return "MipsISD::MAddu";
72 case MipsISD::MSub: return "MipsISD::MSub";
73 case MipsISD::MSubu: return "MipsISD::MSubu";
74 case MipsISD::DivRem: return "MipsISD::DivRem";
75 case MipsISD::DivRemU: return "MipsISD::DivRemU";
76 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
77 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanaka342837d2011-05-28 01:07:07 +000078 case MipsISD::WrapperPIC: return "MipsISD::WrapperPIC";
Akira Hatanaka21afc632011-06-21 00:40:49 +000079 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
Akira Hatanakadb548262011-07-19 23:30:50 +000080 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanakabb15e112011-08-17 02:05:42 +000081 case MipsISD::Ext: return "MipsISD::Ext";
82 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanaka0f843822011-06-07 18:58:42 +000083 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000084 }
85}
86
87MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +000088MipsTargetLowering(MipsTargetMachine &TM)
Chris Lattnerb71b9092009-08-13 06:28:06 +000089 : TargetLowering(TM, new MipsTargetObjectFile()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000090 Subtarget = &TM.getSubtarget<MipsSubtarget>();
91
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000092 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +000093 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +000094 setBooleanContents(ZeroOrOneBooleanContent);
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
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000100 // When dealing with single precision only, use libcalls
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +0000101 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000102 if (!Subtarget->isFP64bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000103 addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000104
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000105 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000106 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
107 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
108 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000109
Eli Friedman6055a6a2009-07-17 04:07:24 +0000110 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000111 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
112 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000113
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000114 // Used by legalize types to correctly generate the setcc result.
115 // Without this, every float setcc comes with a AND/OR with the result,
116 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000117 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000118 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000119
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000120 // Mips Custom Operations
Owen Anderson825b72b2009-08-11 20:47:22 +0000121 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000122 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000123 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
124 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
125 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
126 setOperationAction(ISD::SELECT, MVT::f32, Custom);
127 setOperationAction(ISD::SELECT, MVT::f64, Custom);
128 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000129 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
130 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000131 setOperationAction(ISD::VASTART, MVT::Other, Custom);
132
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000133 setOperationAction(ISD::SDIV, MVT::i32, Expand);
134 setOperationAction(ISD::SREM, MVT::i32, Expand);
135 setOperationAction(ISD::UDIV, MVT::i32, Expand);
136 setOperationAction(ISD::UREM, MVT::i32, Expand);
137
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000138 // Operations not directly supported by Mips.
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
140 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
141 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
142 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
143 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
144 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
145 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
146 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
147 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000148
149 if (!Subtarget->isMips32r2())
150 setOperationAction(ISD::ROTR, MVT::i32, Expand);
151
Owen Anderson825b72b2009-08-11 20:47:22 +0000152 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
153 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
154 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000155 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
156 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000157 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000158 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000160 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000161 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
162 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000163 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000164 setOperationAction(ISD::FLOG, MVT::f32, Expand);
165 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
166 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
167 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000168 setOperationAction(ISD::FMA, MVT::f32, Expand);
169 setOperationAction(ISD::FMA, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000170
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000171 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
172 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Eric Christopher471e4222011-06-08 23:55:35 +0000173
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000174 setOperationAction(ISD::VAARG, MVT::Other, Expand);
175 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
176 setOperationAction(ISD::VAEND, MVT::Other, Expand);
177
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000178 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000179 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
180 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000181
Akira Hatanakadb548262011-07-19 23:30:50 +0000182 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
Eli Friedman14648462011-07-27 22:21:52 +0000183 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Bruno Cardoso Lopes85e92122008-07-07 19:11:24 +0000184
Eli Friedman26689ac2011-08-03 21:06:02 +0000185 setInsertFencesForAtomic(true);
186
Bruno Cardoso Lopesea9d4d62008-08-04 06:44:31 +0000187 if (Subtarget->isSingleFloat())
Owen Anderson825b72b2009-08-11 20:47:22 +0000188 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000189
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000190 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000191 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
192 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000193 }
194
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000195 if (!Subtarget->hasBitCount())
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000197
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000198 if (!Subtarget->hasSwap())
Owen Anderson825b72b2009-08-11 20:47:22 +0000199 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000200
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000201 setTargetDAGCombine(ISD::ADDE);
202 setTargetDAGCombine(ISD::SUBE);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000203 setTargetDAGCombine(ISD::SDIVREM);
204 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000205 setTargetDAGCombine(ISD::SETCC);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000206 setTargetDAGCombine(ISD::AND);
207 setTargetDAGCombine(ISD::OR);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000208
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000209 setMinFunctionAlignment(2);
210
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000211 setStackPointerRegisterToSaveRestore(Mips::SP);
212 computeRegisterProperties();
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000213
214 setExceptionPointerRegister(Mips::A0);
215 setExceptionSelectorRegister(Mips::A1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000216}
217
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000218bool MipsTargetLowering::allowsUnalignedMemoryAccesses(EVT VT) const {
Akira Hatanaka511961a2011-08-17 18:49:18 +0000219 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
220 return SVT == MVT::i32 || SVT == MVT::i16;
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000221}
222
Owen Anderson825b72b2009-08-11 20:47:22 +0000223MVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
224 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000225}
226
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000227// SelectMadd -
228// Transforms a subgraph in CurDAG if the following pattern is found:
229// (addc multLo, Lo0), (adde multHi, Hi0),
230// where,
231// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000232// Lo0: initial value of Lo register
233// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000234// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000235static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000236 // ADDENode's second operand must be a flag output of an ADDC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000237 // for the matching to be successful.
238 SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
239
240 if (ADDCNode->getOpcode() != ISD::ADDC)
241 return false;
242
243 SDValue MultHi = ADDENode->getOperand(0);
244 SDValue MultLo = ADDCNode->getOperand(0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000245 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000246 unsigned MultOpc = MultHi.getOpcode();
247
248 // MultHi and MultLo must be generated by the same node,
249 if (MultLo.getNode() != MultNode)
250 return false;
251
252 // and it must be a multiplication.
253 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
254 return false;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000255
256 // MultLo amd MultHi must be the first and second output of MultNode
257 // respectively.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000258 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
259 return false;
260
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000261 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000262 // of the values of MultNode, in which case MultNode will be removed in later
263 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000264 // If there exist users other than ADDENode or ADDCNode, this function returns
265 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000266 // instruction node rather than a pair of MULT and MADD instructions being
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000267 // produced.
268 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
269 return false;
270
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000271 SDValue Chain = CurDAG->getEntryNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000272 DebugLoc dl = ADDENode->getDebugLoc();
273
274 // create MipsMAdd(u) node
275 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000276
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000277 SDValue MAdd = CurDAG->getNode(MultOpc, dl,
278 MVT::Glue,
279 MultNode->getOperand(0),// Factor 0
280 MultNode->getOperand(1),// Factor 1
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000281 ADDCNode->getOperand(1),// Lo0
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000282 ADDENode->getOperand(1));// Hi0
283
284 // create CopyFromReg nodes
285 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
286 MAdd);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000287 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000288 Mips::HI, MVT::i32,
289 CopyFromLo.getValue(2));
290
291 // replace uses of adde and addc here
292 if (!SDValue(ADDCNode, 0).use_empty())
293 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
294
295 if (!SDValue(ADDENode, 0).use_empty())
296 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
297
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000298 return true;
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000299}
300
301// SelectMsub -
302// Transforms a subgraph in CurDAG if the following pattern is found:
303// (addc Lo0, multLo), (sube Hi0, multHi),
304// where,
305// multHi/Lo: product of multiplication
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000306// Lo0: initial value of Lo register
307// Hi0: initial value of Hi register
Akira Hatanaka81bd78b2011-03-30 21:15:35 +0000308// Return true if pattern matching was successful.
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000309static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000310 // SUBENode's second operand must be a flag output of an SUBC node in order
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000311 // for the matching to be successful.
312 SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
313
314 if (SUBCNode->getOpcode() != ISD::SUBC)
315 return false;
316
317 SDValue MultHi = SUBENode->getOperand(1);
318 SDValue MultLo = SUBCNode->getOperand(1);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000319 SDNode* MultNode = MultHi.getNode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000320 unsigned MultOpc = MultHi.getOpcode();
321
322 // MultHi and MultLo must be generated by the same node,
323 if (MultLo.getNode() != MultNode)
324 return false;
325
326 // and it must be a multiplication.
327 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
328 return false;
329
330 // MultLo amd MultHi must be the first and second output of MultNode
331 // respectively.
332 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
333 return false;
334
335 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
336 // of the values of MultNode, in which case MultNode will be removed in later
337 // phases.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000338 // If there exist users other than SUBENode or SUBCNode, this function returns
339 // here, which will result in MultNode being mapped to a single MULT
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000340 // instruction node rather than a pair of MULT and MSUB instructions being
341 // produced.
342 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
343 return false;
344
345 SDValue Chain = CurDAG->getEntryNode();
346 DebugLoc dl = SUBENode->getDebugLoc();
347
348 // create MipsSub(u) node
349 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
350
351 SDValue MSub = CurDAG->getNode(MultOpc, dl,
352 MVT::Glue,
353 MultNode->getOperand(0),// Factor 0
354 MultNode->getOperand(1),// Factor 1
355 SUBCNode->getOperand(0),// Lo0
356 SUBENode->getOperand(0));// Hi0
357
358 // create CopyFromReg nodes
359 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
360 MSub);
361 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
362 Mips::HI, MVT::i32,
363 CopyFromLo.getValue(2));
364
365 // replace uses of sube and subc here
366 if (!SDValue(SUBCNode, 0).use_empty())
367 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
368
369 if (!SDValue(SUBENode, 0).use_empty())
370 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
371
372 return true;
373}
374
375static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
376 TargetLowering::DAGCombinerInfo &DCI,
377 const MipsSubtarget* Subtarget) {
378 if (DCI.isBeforeLegalize())
379 return SDValue();
380
381 if (Subtarget->isMips32() && SelectMadd(N, &DAG))
382 return SDValue(N, 0);
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000383
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000384 return SDValue();
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000385}
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000386
387static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
388 TargetLowering::DAGCombinerInfo &DCI,
389 const MipsSubtarget* Subtarget) {
390 if (DCI.isBeforeLegalize())
391 return SDValue();
392
393 if (Subtarget->isMips32() && SelectMsub(N, &DAG))
394 return SDValue(N, 0);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000395
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000396 return SDValue();
397}
398
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000399static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
400 TargetLowering::DAGCombinerInfo &DCI,
401 const MipsSubtarget* Subtarget) {
402 if (DCI.isBeforeLegalizeOps())
403 return SDValue();
404
405 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
406 MipsISD::DivRemU;
407 DebugLoc dl = N->getDebugLoc();
408
409 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
410 N->getOperand(0), N->getOperand(1));
411 SDValue InChain = DAG.getEntryNode();
412 SDValue InGlue = DivRem;
413
414 // insert MFLO
415 if (N->hasAnyUseOfValue(0)) {
416 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, Mips::LO, MVT::i32,
417 InGlue);
418 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
419 InChain = CopyFromLo.getValue(1);
420 InGlue = CopyFromLo.getValue(2);
421 }
422
423 // insert MFHI
424 if (N->hasAnyUseOfValue(1)) {
425 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000426 Mips::HI, MVT::i32, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000427 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
428 }
429
430 return SDValue();
431}
432
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000433static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
434 switch (CC) {
435 default: llvm_unreachable("Unknown fp condition code!");
436 case ISD::SETEQ:
437 case ISD::SETOEQ: return Mips::FCOND_OEQ;
438 case ISD::SETUNE: return Mips::FCOND_UNE;
439 case ISD::SETLT:
440 case ISD::SETOLT: return Mips::FCOND_OLT;
441 case ISD::SETGT:
442 case ISD::SETOGT: return Mips::FCOND_OGT;
443 case ISD::SETLE:
444 case ISD::SETOLE: return Mips::FCOND_OLE;
445 case ISD::SETGE:
446 case ISD::SETOGE: return Mips::FCOND_OGE;
447 case ISD::SETULT: return Mips::FCOND_ULT;
448 case ISD::SETULE: return Mips::FCOND_ULE;
449 case ISD::SETUGT: return Mips::FCOND_UGT;
450 case ISD::SETUGE: return Mips::FCOND_UGE;
451 case ISD::SETUO: return Mips::FCOND_UN;
452 case ISD::SETO: return Mips::FCOND_OR;
453 case ISD::SETNE:
454 case ISD::SETONE: return Mips::FCOND_ONE;
455 case ISD::SETUEQ: return Mips::FCOND_UEQ;
456 }
457}
458
459
460// Returns true if condition code has to be inverted.
461static bool InvertFPCondCode(Mips::CondCode CC) {
462 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
463 return false;
464
465 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
466 return true;
467
468 assert(false && "Illegal Condition Code");
469 return false;
470}
471
472// Creates and returns an FPCmp node from a setcc node.
473// Returns Op if setcc is not a floating point comparison.
474static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
475 // must be a SETCC node
476 if (Op.getOpcode() != ISD::SETCC)
477 return Op;
478
479 SDValue LHS = Op.getOperand(0);
480
481 if (!LHS.getValueType().isFloatingPoint())
482 return Op;
483
484 SDValue RHS = Op.getOperand(1);
485 DebugLoc dl = Op.getDebugLoc();
486
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000487 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
488 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000489 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
490
491 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
492 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
493}
494
495// Creates and returns a CMovFPT/F node.
496static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
497 SDValue False, DebugLoc DL) {
498 bool invert = InvertFPCondCode((Mips::CondCode)
499 cast<ConstantSDNode>(Cond.getOperand(2))
500 ->getSExtValue());
501
502 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
503 True.getValueType(), True, False, Cond);
504}
505
506static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
507 TargetLowering::DAGCombinerInfo &DCI,
508 const MipsSubtarget* Subtarget) {
509 if (DCI.isBeforeLegalizeOps())
510 return SDValue();
511
512 SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
513
514 if (Cond.getOpcode() != MipsISD::FPCmp)
515 return SDValue();
516
517 SDValue True = DAG.getConstant(1, MVT::i32);
518 SDValue False = DAG.getConstant(0, MVT::i32);
519
520 return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
521}
522
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000523static SDValue PerformANDCombine(SDNode *N, SelectionDAG& DAG,
524 TargetLowering::DAGCombinerInfo &DCI,
525 const MipsSubtarget* Subtarget) {
526 // Pattern match EXT.
527 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
528 // => ext $dst, $src, size, pos
529 if (DCI.isBeforeLegalizeOps() || !Subtarget->isMips32r2())
530 return SDValue();
531
532 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
533
534 // Op's first operand must be a shift right.
535 if (ShiftRight.getOpcode() != ISD::SRA && ShiftRight.getOpcode() != ISD::SRL)
536 return SDValue();
537
538 // The second operand of the shift must be an immediate.
539 uint64_t Pos;
540 ConstantSDNode *CN;
541 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
542 return SDValue();
543
544 Pos = CN->getZExtValue();
545
546 uint64_t SMPos, SMSize;
547 // Op's second operand must be a shifted mask.
548 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
549 !IsShiftedMask(CN->getZExtValue(), 32, SMPos, SMSize))
550 return SDValue();
551
552 // Return if the shifted mask does not start at bit 0 or the sum of its size
553 // and Pos exceeds the word's size.
554 if (SMPos != 0 || Pos + SMSize > 32)
555 return SDValue();
556
557 return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), MVT::i32,
558 ShiftRight.getOperand(0),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000559 DAG.getConstant(Pos, MVT::i32),
560 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000561}
562
563static SDValue PerformORCombine(SDNode *N, SelectionDAG& DAG,
564 TargetLowering::DAGCombinerInfo &DCI,
565 const MipsSubtarget* Subtarget) {
566 // Pattern match INS.
567 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
568 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
569 // => ins $dst, $src, size, pos, $src1
570 if (DCI.isBeforeLegalizeOps() || !Subtarget->isMips32r2())
571 return SDValue();
572
573 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
574 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
575 ConstantSDNode *CN;
576
577 // See if Op's first operand matches (and $src1 , mask0).
578 if (And0.getOpcode() != ISD::AND)
579 return SDValue();
580
581 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
582 !IsShiftedMask(~CN->getZExtValue(), 32, SMPos0, SMSize0))
583 return SDValue();
584
585 // See if Op's second operand matches (and (shl $src, pos), mask1).
586 if (And1.getOpcode() != ISD::AND)
587 return SDValue();
588
589 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
590 !IsShiftedMask(CN->getZExtValue(), CN->getValueSizeInBits(0), SMPos1,
591 SMSize1))
592 return SDValue();
593
594 // The shift masks must have the same position and size.
595 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
596 return SDValue();
597
598 SDValue Shl = And1.getOperand(0);
599 if (Shl.getOpcode() != ISD::SHL)
600 return SDValue();
601
602 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
603 return SDValue();
604
605 unsigned Shamt = CN->getZExtValue();
606
607 // Return if the shift amount and the first bit position of mask are not the
608 // same.
609 if (Shamt != SMPos0)
610 return SDValue();
611
612 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), MVT::i32,
613 Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000614 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000615 DAG.getConstant(SMSize0, MVT::i32),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000616 And0.getOperand(0));
617}
618
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000619SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000620 const {
621 SelectionDAG &DAG = DCI.DAG;
622 unsigned opc = N->getOpcode();
623
624 switch (opc) {
625 default: break;
626 case ISD::ADDE:
627 return PerformADDECombine(N, DAG, DCI, Subtarget);
628 case ISD::SUBE:
629 return PerformSUBECombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000630 case ISD::SDIVREM:
631 case ISD::UDIVREM:
632 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000633 case ISD::SETCC:
634 return PerformSETCCCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000635 case ISD::AND:
636 return PerformANDCombine(N, DAG, DCI, Subtarget);
637 case ISD::OR:
638 return PerformORCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000639 }
640
641 return SDValue();
642}
643
Dan Gohman475871a2008-07-27 21:46:04 +0000644SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000645LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000646{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000647 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000648 {
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000649 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000650 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
651 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000652 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000653 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000654 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
655 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +0000656 case ISD::SELECT: return LowerSELECT(Op, DAG);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000657 case ISD::VASTART: return LowerVASTART(Op, DAG);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +0000658 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Akira Hatanaka2e591472011-06-02 00:24:44 +0000659 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Akira Hatanakadb548262011-07-19 23:30:50 +0000660 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
Eli Friedman14648462011-07-27 22:21:52 +0000661 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000662 }
Dan Gohman475871a2008-07-27 21:46:04 +0000663 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000664}
665
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000666//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000667// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000668//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000669
670// AddLiveIn - This helper function adds the specified physical register to the
671// MachineFunction as a live in value. It also creates a corresponding
672// virtual register for it.
673static unsigned
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000674AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000675{
676 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000677 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
678 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000679 return VReg;
680}
681
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +0000682// Get fp branch code (not opcode) from condition code.
683static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
684 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
685 return Mips::BRANCH_T;
686
687 if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
688 return Mips::BRANCH_F;
689
690 return Mips::BRANCH_INVALID;
691}
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000692
Akira Hatanaka14487d42011-06-07 19:28:39 +0000693static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
694 DebugLoc dl,
695 const MipsSubtarget* Subtarget,
696 const TargetInstrInfo *TII,
697 bool isFPCmp, unsigned Opc) {
698 // There is no need to expand CMov instructions if target has
699 // conditional moves.
700 if (Subtarget->hasCondMov())
701 return BB;
702
703 // To "insert" a SELECT_CC instruction, we actually have to insert the
704 // diamond control-flow pattern. The incoming instruction knows the
705 // destination vreg to set, the condition code register to branch on, the
706 // true/false values to select between, and a branch opcode to use.
707 const BasicBlock *LLVM_BB = BB->getBasicBlock();
708 MachineFunction::iterator It = BB;
709 ++It;
710
711 // thisMBB:
712 // ...
713 // TrueVal = ...
714 // setcc r1, r2, r3
715 // bNE r1, r0, copy1MBB
716 // fallthrough --> copy0MBB
717 MachineBasicBlock *thisMBB = BB;
718 MachineFunction *F = BB->getParent();
719 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
720 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
721 F->insert(It, copy0MBB);
722 F->insert(It, sinkMBB);
723
724 // Transfer the remainder of BB and its successor edges to sinkMBB.
725 sinkMBB->splice(sinkMBB->begin(), BB,
726 llvm::next(MachineBasicBlock::iterator(MI)),
727 BB->end());
728 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
729
730 // Next, add the true and fallthrough blocks as its successors.
731 BB->addSuccessor(copy0MBB);
732 BB->addSuccessor(sinkMBB);
733
734 // Emit the right instruction according to the type of the operands compared
735 if (isFPCmp)
736 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
737 else
738 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
739 .addReg(Mips::ZERO).addMBB(sinkMBB);
740
741 // copy0MBB:
742 // %FalseValue = ...
743 // # fallthrough to sinkMBB
744 BB = copy0MBB;
745
746 // Update machine-CFG edges
747 BB->addSuccessor(sinkMBB);
748
749 // sinkMBB:
750 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
751 // ...
752 BB = sinkMBB;
753
754 if (isFPCmp)
755 BuildMI(*BB, BB->begin(), dl,
756 TII->get(Mips::PHI), MI->getOperand(0).getReg())
757 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
758 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
759 else
760 BuildMI(*BB, BB->begin(), dl,
761 TII->get(Mips::PHI), MI->getOperand(0).getReg())
762 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
763 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
764
765 MI->eraseFromParent(); // The pseudo instruction is gone now.
766 return BB;
767}
768
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000769MachineBasicBlock *
770MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000771 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000772 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen94817572009-02-13 02:34:39 +0000773 DebugLoc dl = MI->getDebugLoc();
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000774
775 switch (MI->getOpcode()) {
Akira Hatanaka14487d42011-06-07 19:28:39 +0000776 default:
777 assert(false && "Unexpected instr type to insert");
778 return NULL;
779 case Mips::MOVT:
780 case Mips::MOVT_S:
781 case Mips::MOVT_D:
782 return ExpandCondMov(MI, BB, dl, Subtarget, TII, true, Mips::BC1F);
783 case Mips::MOVF:
784 case Mips::MOVF_S:
785 case Mips::MOVF_D:
786 return ExpandCondMov(MI, BB, dl, Subtarget, TII, true, Mips::BC1T);
787 case Mips::MOVZ_I:
788 case Mips::MOVZ_S:
789 case Mips::MOVZ_D:
790 return ExpandCondMov(MI, BB, dl, Subtarget, TII, false, Mips::BNE);
791 case Mips::MOVN_I:
792 case Mips::MOVN_S:
793 case Mips::MOVN_D:
794 return ExpandCondMov(MI, BB, dl, Subtarget, TII, false, Mips::BEQ);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000795
796 case Mips::ATOMIC_LOAD_ADD_I8:
797 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
798 case Mips::ATOMIC_LOAD_ADD_I16:
799 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
800 case Mips::ATOMIC_LOAD_ADD_I32:
801 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
802
803 case Mips::ATOMIC_LOAD_AND_I8:
804 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
805 case Mips::ATOMIC_LOAD_AND_I16:
806 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
807 case Mips::ATOMIC_LOAD_AND_I32:
808 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
809
810 case Mips::ATOMIC_LOAD_OR_I8:
811 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
812 case Mips::ATOMIC_LOAD_OR_I16:
813 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
814 case Mips::ATOMIC_LOAD_OR_I32:
815 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
816
817 case Mips::ATOMIC_LOAD_XOR_I8:
818 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
819 case Mips::ATOMIC_LOAD_XOR_I16:
820 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
821 case Mips::ATOMIC_LOAD_XOR_I32:
822 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
823
824 case Mips::ATOMIC_LOAD_NAND_I8:
825 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
826 case Mips::ATOMIC_LOAD_NAND_I16:
827 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
828 case Mips::ATOMIC_LOAD_NAND_I32:
829 return EmitAtomicBinary(MI, BB, 4, 0, true);
830
831 case Mips::ATOMIC_LOAD_SUB_I8:
832 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
833 case Mips::ATOMIC_LOAD_SUB_I16:
834 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
835 case Mips::ATOMIC_LOAD_SUB_I32:
836 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
837
838 case Mips::ATOMIC_SWAP_I8:
839 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
840 case Mips::ATOMIC_SWAP_I16:
841 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
842 case Mips::ATOMIC_SWAP_I32:
843 return EmitAtomicBinary(MI, BB, 4, 0);
844
845 case Mips::ATOMIC_CMP_SWAP_I8:
846 return EmitAtomicCmpSwapPartword(MI, BB, 1);
847 case Mips::ATOMIC_CMP_SWAP_I16:
848 return EmitAtomicCmpSwapPartword(MI, BB, 2);
849 case Mips::ATOMIC_CMP_SWAP_I32:
850 return EmitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000851 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000852}
853
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000854// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
855// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
856MachineBasicBlock *
857MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000858 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000859 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000860 assert(Size == 4 && "Unsupported size for EmitAtomicBinary.");
861
862 MachineFunction *MF = BB->getParent();
863 MachineRegisterInfo &RegInfo = MF->getRegInfo();
864 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
865 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
866 DebugLoc dl = MI->getDebugLoc();
867
Akira Hatanaka4061da12011-07-19 20:11:17 +0000868 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000869 unsigned Ptr = MI->getOperand(1).getReg();
870 unsigned Incr = MI->getOperand(2).getReg();
871
Akira Hatanaka4061da12011-07-19 20:11:17 +0000872 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
873 unsigned AndRes = RegInfo.createVirtualRegister(RC);
874 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000875
876 // insert new blocks after the current block
877 const BasicBlock *LLVM_BB = BB->getBasicBlock();
878 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
879 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
880 MachineFunction::iterator It = BB;
881 ++It;
882 MF->insert(It, loopMBB);
883 MF->insert(It, exitMBB);
884
885 // Transfer the remainder of BB and its successor edges to exitMBB.
886 exitMBB->splice(exitMBB->begin(), BB,
887 llvm::next(MachineBasicBlock::iterator(MI)),
888 BB->end());
889 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
890
891 // thisMBB:
892 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000893 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000894 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000895 loopMBB->addSuccessor(loopMBB);
896 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000897
898 // loopMBB:
899 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000900 // <binop> storeval, oldval, incr
901 // sc success, storeval, 0(ptr)
902 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000903 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000904 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000905 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000906 // and andres, oldval, incr
907 // nor storeval, $0, andres
908 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr);
909 BuildMI(BB, dl, TII->get(Mips::NOR), StoreVal)
910 .addReg(Mips::ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000911 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000912 // <binop> storeval, oldval, incr
913 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000914 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000915 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000916 }
Akira Hatanaka4061da12011-07-19 20:11:17 +0000917 BuildMI(BB, dl, TII->get(Mips::SC), Success)
918 .addReg(StoreVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000919 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +0000920 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000921
922 MI->eraseFromParent(); // The instruction is gone now.
923
Akira Hatanaka939ece12011-07-19 03:42:13 +0000924 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000925}
926
927MachineBasicBlock *
928MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000929 MachineBasicBlock *BB,
930 unsigned Size, unsigned BinOpcode,
931 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000932 assert((Size == 1 || Size == 2) &&
933 "Unsupported size for EmitAtomicBinaryPartial.");
934
935 MachineFunction *MF = BB->getParent();
936 MachineRegisterInfo &RegInfo = MF->getRegInfo();
937 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
938 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
939 DebugLoc dl = MI->getDebugLoc();
940
941 unsigned Dest = MI->getOperand(0).getReg();
942 unsigned Ptr = MI->getOperand(1).getReg();
943 unsigned Incr = MI->getOperand(2).getReg();
944
Akira Hatanaka4061da12011-07-19 20:11:17 +0000945 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
946 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000947 unsigned Mask = RegInfo.createVirtualRegister(RC);
948 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000949 unsigned NewVal = RegInfo.createVirtualRegister(RC);
950 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000951 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000952 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
953 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
954 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
955 unsigned AndRes = RegInfo.createVirtualRegister(RC);
956 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +0000957 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000958 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
959 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
960 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
961 unsigned SllRes = RegInfo.createVirtualRegister(RC);
962 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000963
964 // insert new blocks after the current block
965 const BasicBlock *LLVM_BB = BB->getBasicBlock();
966 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000967 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000968 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
969 MachineFunction::iterator It = BB;
970 ++It;
971 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +0000972 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000973 MF->insert(It, exitMBB);
974
975 // Transfer the remainder of BB and its successor edges to exitMBB.
976 exitMBB->splice(exitMBB->begin(), BB,
977 llvm::next(MachineBasicBlock::iterator(MI)),
978 BB->end());
979 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
980
Akira Hatanaka81b44112011-07-19 17:09:53 +0000981 BB->addSuccessor(loopMBB);
982 loopMBB->addSuccessor(loopMBB);
983 loopMBB->addSuccessor(sinkMBB);
984 sinkMBB->addSuccessor(exitMBB);
985
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000986 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +0000987 // addiu masklsb2,$0,-4 # 0xfffffffc
988 // and alignedaddr,ptr,masklsb2
989 // andi ptrlsb2,ptr,3
990 // sll shiftamt,ptrlsb2,3
991 // ori maskupper,$0,255 # 0xff
992 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000993 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +0000994 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000995
996 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +0000997 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
998 .addReg(Mips::ZERO).addImm(-4);
999 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1000 .addReg(Ptr).addReg(MaskLSB2);
1001 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1002 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1003 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1004 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001005 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1006 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001007 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001008 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001009
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001010
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001011 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001012 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001013 // ll oldval,0(alignedaddr)
1014 // binop binopres,oldval,incr2
1015 // and newval,binopres,mask
1016 // and maskedoldval0,oldval,mask2
1017 // or storeval,maskedoldval0,newval
1018 // sc success,storeval,0(alignedaddr)
1019 // beq success,$0,loopMBB
1020
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001021 // atomic.swap
1022 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001023 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001024 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001025 // and maskedoldval0,oldval,mask2
1026 // or storeval,maskedoldval0,newval
1027 // sc success,storeval,0(alignedaddr)
1028 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001029
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001030 BB = loopMBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001031 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001032 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001033 // and andres, oldval, incr2
1034 // nor binopres, $0, andres
1035 // and newval, binopres, mask
1036 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1037 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1038 .addReg(Mips::ZERO).addReg(AndRes);
1039 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001040 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001041 // <binop> binopres, oldval, incr2
1042 // and newval, binopres, mask
1043 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1044 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001045 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001046 // and newval, incr2, mask
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001047 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001048 }
1049
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001050 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001051 .addReg(OldVal).addReg(Mask2);
1052 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001053 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001054 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1055 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001056 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001057 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001058
Akira Hatanaka939ece12011-07-19 03:42:13 +00001059 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001060 // and maskedoldval1,oldval,mask
1061 // srl srlres,maskedoldval1,shiftamt
1062 // sll sllres,srlres,24
1063 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001064 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001065 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001066
Akira Hatanaka4061da12011-07-19 20:11:17 +00001067 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1068 .addReg(OldVal).addReg(Mask);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001069 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1070 .addReg(ShiftAmt).addReg(MaskedOldVal1);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001071 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1072 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001073 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001074 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001075
1076 MI->eraseFromParent(); // The instruction is gone now.
1077
Akira Hatanaka939ece12011-07-19 03:42:13 +00001078 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001079}
1080
1081MachineBasicBlock *
1082MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001083 MachineBasicBlock *BB,
1084 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001085 assert(Size == 4 && "Unsupported size for EmitAtomicCmpSwap.");
1086
1087 MachineFunction *MF = BB->getParent();
1088 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1089 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1090 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1091 DebugLoc dl = MI->getDebugLoc();
1092
1093 unsigned Dest = MI->getOperand(0).getReg();
1094 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001095 unsigned OldVal = MI->getOperand(2).getReg();
1096 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001097
Akira Hatanaka4061da12011-07-19 20:11:17 +00001098 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001099
1100 // insert new blocks after the current block
1101 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1102 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1103 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1104 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1105 MachineFunction::iterator It = BB;
1106 ++It;
1107 MF->insert(It, loop1MBB);
1108 MF->insert(It, loop2MBB);
1109 MF->insert(It, exitMBB);
1110
1111 // Transfer the remainder of BB and its successor edges to exitMBB.
1112 exitMBB->splice(exitMBB->begin(), BB,
1113 llvm::next(MachineBasicBlock::iterator(MI)),
1114 BB->end());
1115 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1116
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001117 // thisMBB:
1118 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001119 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001120 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001121 loop1MBB->addSuccessor(exitMBB);
1122 loop1MBB->addSuccessor(loop2MBB);
1123 loop2MBB->addSuccessor(loop1MBB);
1124 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001125
1126 // loop1MBB:
1127 // ll dest, 0(ptr)
1128 // bne dest, oldval, exitMBB
1129 BB = loop1MBB;
Akira Hatanakad3ac47f2011-07-07 18:57:00 +00001130 BuildMI(BB, dl, TII->get(Mips::LL), Dest).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001131 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001132 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001133
1134 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001135 // sc success, newval, 0(ptr)
1136 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001137 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001138 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1139 .addReg(NewVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001140 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001141 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001142
1143 MI->eraseFromParent(); // The instruction is gone now.
1144
Akira Hatanaka939ece12011-07-19 03:42:13 +00001145 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001146}
1147
1148MachineBasicBlock *
1149MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001150 MachineBasicBlock *BB,
1151 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001152 assert((Size == 1 || Size == 2) &&
1153 "Unsupported size for EmitAtomicCmpSwapPartial.");
1154
1155 MachineFunction *MF = BB->getParent();
1156 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1157 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1158 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1159 DebugLoc dl = MI->getDebugLoc();
1160
1161 unsigned Dest = MI->getOperand(0).getReg();
1162 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001163 unsigned CmpVal = MI->getOperand(2).getReg();
1164 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001165
Akira Hatanaka4061da12011-07-19 20:11:17 +00001166 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1167 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001168 unsigned Mask = RegInfo.createVirtualRegister(RC);
1169 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001170 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1171 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1172 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1173 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1174 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1175 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1176 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1177 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1178 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1179 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1180 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1181 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1182 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1183 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001184
1185 // insert new blocks after the current block
1186 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1187 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1188 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001189 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001190 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1191 MachineFunction::iterator It = BB;
1192 ++It;
1193 MF->insert(It, loop1MBB);
1194 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001195 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001196 MF->insert(It, exitMBB);
1197
1198 // Transfer the remainder of BB and its successor edges to exitMBB.
1199 exitMBB->splice(exitMBB->begin(), BB,
1200 llvm::next(MachineBasicBlock::iterator(MI)),
1201 BB->end());
1202 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1203
Akira Hatanaka81b44112011-07-19 17:09:53 +00001204 BB->addSuccessor(loop1MBB);
1205 loop1MBB->addSuccessor(sinkMBB);
1206 loop1MBB->addSuccessor(loop2MBB);
1207 loop2MBB->addSuccessor(loop1MBB);
1208 loop2MBB->addSuccessor(sinkMBB);
1209 sinkMBB->addSuccessor(exitMBB);
1210
Akira Hatanaka70564a92011-07-19 18:14:26 +00001211 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001212 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001213 // addiu masklsb2,$0,-4 # 0xfffffffc
1214 // and alignedaddr,ptr,masklsb2
1215 // andi ptrlsb2,ptr,3
1216 // sll shiftamt,ptrlsb2,3
1217 // ori maskupper,$0,255 # 0xff
1218 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001219 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001220 // andi maskedcmpval,cmpval,255
1221 // sll shiftedcmpval,maskedcmpval,shiftamt
1222 // andi maskednewval,newval,255
1223 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001224 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001225 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1226 .addReg(Mips::ZERO).addImm(-4);
1227 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1228 .addReg(Ptr).addReg(MaskLSB2);
1229 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1230 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1231 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1232 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001233 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1234 .addReg(ShiftAmt).addReg(MaskUpper);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001235 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001236 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1237 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001238 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1239 .addReg(ShiftAmt).addReg(MaskedCmpVal);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001240 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1241 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001242 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1243 .addReg(ShiftAmt).addReg(MaskedNewVal);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001244
1245 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001246 // ll oldval,0(alginedaddr)
1247 // and maskedoldval0,oldval,mask
1248 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001249 BB = loop1MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001250 BuildMI(BB, dl, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
1251 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1252 .addReg(OldVal).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001253 BuildMI(BB, dl, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001254 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001255
1256 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001257 // and maskedoldval1,oldval,mask2
1258 // or storeval,maskedoldval1,shiftednewval
1259 // sc success,storeval,0(alignedaddr)
1260 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001261 BB = loop2MBB;
Akira Hatanaka4061da12011-07-19 20:11:17 +00001262 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1263 .addReg(OldVal).addReg(Mask2);
1264 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1265 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1266 BuildMI(BB, dl, TII->get(Mips::SC), Success)
1267 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001268 BuildMI(BB, dl, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001269 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001270
Akira Hatanaka939ece12011-07-19 03:42:13 +00001271 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001272 // srl srlres,maskedoldval0,shiftamt
1273 // sll sllres,srlres,24
1274 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001275 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001276 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001277
Akira Hatanakacc7ecc72011-07-19 20:34:00 +00001278 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1279 .addReg(ShiftAmt).addReg(MaskedOldVal0);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001280 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1281 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001282 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001283 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001284
1285 MI->eraseFromParent(); // The instruction is gone now.
1286
Akira Hatanaka939ece12011-07-19 03:42:13 +00001287 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001288}
1289
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001290//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001291// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001292//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001293SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001294LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001295{
Akira Hatanaka21afc632011-06-21 00:40:49 +00001296 MachineFunction &MF = DAG.getMachineFunction();
1297 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1298
1299 assert(getTargetMachine().getFrameLowering()->getStackAlignment() >=
Akira Hatanaka053546c2011-05-25 02:20:00 +00001300 cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1301 "Cannot lower if the alignment of the allocated space is larger than \
1302 that of the stack.");
1303
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001304 SDValue Chain = Op.getOperand(0);
1305 SDValue Size = Op.getOperand(1);
Dale Johannesena05dca42009-02-04 23:02:30 +00001306 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001307
1308 // Get a reference from Mips stack pointer
Owen Anderson825b72b2009-08-11 20:47:22 +00001309 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001310
1311 // Subtract the dynamic size from the actual stack size to
1312 // obtain the new stack size.
Owen Anderson825b72b2009-08-11 20:47:22 +00001313 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001314
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001315 // The Sub result contains the new stack start address, so it
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001316 // must be placed in the stack pointer register.
Akira Hatanaka053546c2011-05-25 02:20:00 +00001317 Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub,
1318 SDValue());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001319
1320 // This node always has two return values: a new stack pointer
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001321 // value and a chain
Akira Hatanaka21afc632011-06-21 00:40:49 +00001322 SDVTList VTLs = DAG.getVTList(MVT::i32, MVT::Other);
1323 SDValue Ptr = DAG.getFrameIndex(MipsFI->getDynAllocFI(), getPointerTy());
1324 SDValue Ops[] = { Chain, Ptr, Chain.getValue(1) };
1325
1326 return DAG.getNode(MipsISD::DynAlloc, dl, VTLs, Ops, 3);
Bruno Cardoso Lopes7da151c2008-08-07 19:08:11 +00001327}
1328
1329SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001330LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001331{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001332 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001333 // the block to branch to if the condition is true.
1334 SDValue Chain = Op.getOperand(0);
1335 SDValue Dest = Op.getOperand(2);
Dale Johannesende064702009-02-06 21:50:26 +00001336 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001337
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001338 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1339
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001340 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001341 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001342 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001343
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001344 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001345 Mips::CondCode CC =
1346 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001347 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001348
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001349 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001350 Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001351}
1352
1353SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001354LowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001355{
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001356 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001357
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001358 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001359 if (Cond.getOpcode() != MipsISD::FPCmp)
1360 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001361
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001362 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1363 Op.getDebugLoc());
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001364}
1365
Dan Gohmand858e902010-04-17 15:26:15 +00001366SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1367 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001368 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001369 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +00001370 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001371
Eli Friedmane2c74082009-08-03 02:22:28 +00001372 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Chris Lattnere3736f82009-08-13 05:41:27 +00001373 SDVTList VTs = DAG.getVTList(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001374
Chris Lattnerb71b9092009-08-13 06:28:06 +00001375 MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001376
Chris Lattnere3736f82009-08-13 05:41:27 +00001377 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001378 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1379 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001380 MipsII::MO_GPREL);
Chris Lattnere3736f82009-08-13 05:41:27 +00001381 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1382 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001383 return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001384 }
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001385 // %hi/%lo relocation
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001386 SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1387 MipsII::MO_ABS_HI);
1388 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1389 MipsII::MO_ABS_LO);
1390 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1391 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001392 return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001393 }
1394
Akira Hatanaka0f843822011-06-07 18:58:42 +00001395 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1396 MipsII::MO_GOT);
1397 GA = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, GA);
1398 SDValue ResNode = DAG.getLoad(MVT::i32, dl,
1399 DAG.getEntryNode(), GA, MachinePointerInfo(),
1400 false, false, 0);
1401 // On functions and global targets not internal linked only
1402 // a load from got/GP is necessary for PIC to work.
1403 if (!GV->hasInternalLinkage() &&
1404 (!GV->hasLocalLinkage() || isa<Function>(GV)))
1405 return ResNode;
1406 SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1407 MipsII::MO_ABS_LO);
1408 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
1409 return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001410}
1411
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001412SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1413 SelectionDAG &DAG) const {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001414 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1415 // FIXME there isn't actually debug info here
1416 DebugLoc dl = Op.getDebugLoc();
1417
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001418 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001419 // %hi/%lo relocation
1420 SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
1421 MipsII::MO_ABS_HI);
1422 SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
1423 MipsII::MO_ABS_LO);
1424 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1425 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1426 return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001427 }
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001428
1429 SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1430 MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001431 BAGOTOffset = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, BAGOTOffset);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001432 SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1433 MipsII::MO_ABS_LO);
1434 SDValue Load = DAG.getLoad(MVT::i32, dl,
1435 DAG.getEntryNode(), BAGOTOffset,
1436 MachinePointerInfo(), false, false, 0);
1437 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
1438 return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001439}
1440
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001441SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001442LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001443{
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001444 // If the relocation model is PIC, use the General Dynamic TLS Model,
1445 // otherwise use the Initial Exec or Local Exec TLS Model.
1446 // TODO: implement Local Dynamic TLS model
1447
1448 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1449 DebugLoc dl = GA->getDebugLoc();
1450 const GlobalValue *GV = GA->getGlobal();
1451 EVT PtrVT = getPointerTy();
1452
1453 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1454 // General Dynamic TLS Model
1455 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001456 0, MipsII::MO_TLSGD);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001457 SDValue Tlsgd = DAG.getNode(MipsISD::TlsGd, dl, MVT::i32, TGA);
1458 SDValue GP = DAG.getRegister(Mips::GP, MVT::i32);
1459 SDValue Argument = DAG.getNode(ISD::ADD, dl, MVT::i32, GP, Tlsgd);
1460
1461 ArgListTy Args;
1462 ArgListEntry Entry;
1463 Entry.Node = Argument;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001464 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001465 Args.push_back(Entry);
1466 std::pair<SDValue, SDValue> CallResult =
1467 LowerCallTo(DAG.getEntryNode(),
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001468 (Type *) Type::getInt32Ty(*DAG.getContext()),
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001469 false, false, false, false, 0, CallingConv::C, false, true,
1470 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG,
1471 dl);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001472
1473 return CallResult.first;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001474 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001475
1476 SDValue Offset;
1477 if (GV->isDeclaration()) {
1478 // Initial Exec TLS Model
1479 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1480 MipsII::MO_GOTTPREL);
1481 Offset = DAG.getLoad(MVT::i32, dl,
1482 DAG.getEntryNode(), TGA, MachinePointerInfo(),
1483 false, false, 0);
1484 } else {
1485 // Local Exec TLS Model
1486 SDVTList VTs = DAG.getVTList(MVT::i32);
1487 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1488 MipsII::MO_TPREL_HI);
1489 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1490 MipsII::MO_TPREL_LO);
1491 SDValue Hi = DAG.getNode(MipsISD::TprelHi, dl, VTs, &TGAHi, 1);
1492 SDValue Lo = DAG.getNode(MipsISD::TprelLo, dl, MVT::i32, TGALo);
1493 Offset = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1494 }
1495
1496 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1497 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001498}
1499
1500SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001501LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001502{
Dan Gohman475871a2008-07-27 21:46:04 +00001503 SDValue ResNode;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001504 SDValue HiPart;
Dale Johannesende064702009-02-06 21:50:26 +00001505 // FIXME there isn't actually debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001506 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001507 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001508 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001509
Owen Andersone50ed302009-08-10 22:56:29 +00001510 EVT PtrVT = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001511 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001512
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001513 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
1514
Bruno Cardoso Lopes46773792010-07-20 08:37:04 +00001515 if (!IsPIC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001516 SDValue Ops[] = { JTI };
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001517 HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001518 } else {// Emit Load from Global Pointer
1519 JTI = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, JTI);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001520 HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
1521 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00001522 false, false, 0);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001523 }
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001524
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001525 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1526 MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001527 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001528 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001529
1530 return ResNode;
1531}
1532
Dan Gohman475871a2008-07-27 21:46:04 +00001533SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001534LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001535{
Dan Gohman475871a2008-07-27 21:46:04 +00001536 SDValue ResNode;
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001537 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001538 const Constant *C = N->getConstVal();
Dale Johannesende064702009-02-06 21:50:26 +00001539 // FIXME there isn't actually debug info here
1540 DebugLoc dl = Op.getDebugLoc();
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001541
1542 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001543 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001544 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001545 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001546 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001547 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001548 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1549 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001550 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001551
1552 if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001553 SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001554 N->getOffset(), MipsII::MO_ABS_HI);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001555 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001556 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001557 SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1558 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Owen Anderson825b72b2009-08-11 20:47:22 +00001559 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001560 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001561 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001562 N->getOffset(), MipsII::MO_GOT);
Akira Hatanaka342837d2011-05-28 01:07:07 +00001563 CP = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001564 SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001565 CP, MachinePointerInfo::getConstantPool(),
1566 false, false, 0);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001567 SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001568 N->getOffset(), MipsII::MO_ABS_LO);
Akira Hatanakae2e436a2011-04-01 21:41:06 +00001569 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001570 ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
1571 }
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001572
1573 return ResNode;
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001574}
1575
Dan Gohmand858e902010-04-17 15:26:15 +00001576SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001577 MachineFunction &MF = DAG.getMachineFunction();
1578 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1579
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001580 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001581 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1582 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001583
1584 // vastart just stores the address of the VarArgsFrameIndex slot into the
1585 // memory location argument.
1586 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00001587 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1588 MachinePointerInfo(SV),
David Greenef6fa1862010-02-15 16:56:10 +00001589 false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001590}
1591
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001592static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
1593 // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
1594 DebugLoc dl = Op.getDebugLoc();
1595 SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
1596 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
1597 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
1598 DAG.getConstant(0x7fffffff, MVT::i32));
1599 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
1600 DAG.getConstant(0x80000000, MVT::i32));
1601 SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1602 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
1603}
1604
1605static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
Eric Christopher471e4222011-06-08 23:55:35 +00001606 // FIXME:
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001607 // Use ext/ins instructions if target architecture is Mips32r2.
1608 // Eliminate redundant mfc1 and mtc1 instructions.
1609 unsigned LoIdx = 0, HiIdx = 1;
Eric Christopher471e4222011-06-08 23:55:35 +00001610
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001611 if (!isLittle)
1612 std::swap(LoIdx, HiIdx);
1613
1614 DebugLoc dl = Op.getDebugLoc();
1615 SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1616 Op.getOperand(0),
1617 DAG.getConstant(LoIdx, MVT::i32));
1618 SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1619 Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1620 SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1621 Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1622 SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1623 DAG.getConstant(0x7fffffff, MVT::i32));
1624 SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1625 DAG.getConstant(0x80000000, MVT::i32));
1626 SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1627
1628 if (!isLittle)
1629 std::swap(Word0, Word1);
1630
1631 return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1632}
1633
1634SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1635 const {
1636 EVT Ty = Op.getValueType();
1637
1638 assert(Ty == MVT::f32 || Ty == MVT::f64);
1639
1640 if (Ty == MVT::f32)
1641 return LowerFCOPYSIGN32(Op, DAG);
1642 else
1643 return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
1644}
1645
Akira Hatanaka2e591472011-06-02 00:24:44 +00001646SDValue MipsTargetLowering::
1647LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001648 // check the depth
1649 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001650 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001651
1652 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1653 MFI->setFrameAddressIsTaken(true);
1654 EVT VT = Op.getValueType();
1655 DebugLoc dl = Op.getDebugLoc();
1656 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Mips::FP, VT);
1657 return FrameAddr;
1658}
1659
Akira Hatanakadb548262011-07-19 23:30:50 +00001660// TODO: set SType according to the desired memory barrier behavior.
1661SDValue MipsTargetLowering::LowerMEMBARRIER(SDValue Op,
1662 SelectionDAG& DAG) const {
1663 unsigned SType = 0;
1664 DebugLoc dl = Op.getDebugLoc();
1665 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1666 DAG.getConstant(SType, MVT::i32));
1667}
1668
Eli Friedman14648462011-07-27 22:21:52 +00001669SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1670 SelectionDAG& DAG) const {
1671 // FIXME: Need pseudo-fence for 'singlethread' fences
1672 // FIXME: Set SType for weaker fences where supported/appropriate.
1673 unsigned SType = 0;
1674 DebugLoc dl = Op.getDebugLoc();
1675 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
1676 DAG.getConstant(SType, MVT::i32));
1677}
1678
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001679//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001680// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001681//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001682
1683#include "MipsGenCallingConv.inc"
1684
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001685//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001686// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001687// Mips O32 ABI rules:
1688// ---
1689// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001690// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001691// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001692// f64 - Only passed in two aliased f32 registers if no int reg has been used
1693// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001694// not used, it must be shadowed. If only A3 is avaiable, shadow it and
1695// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001696//
1697// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001698//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001699
Duncan Sands1e96bab2010-11-04 10:49:57 +00001700static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001701 MVT LocVT, CCValAssign::LocInfo LocInfo,
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001702 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1703
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001704 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001705
1706 static const unsigned IntRegs[] = {
1707 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1708 };
1709 static const unsigned F32Regs[] = {
1710 Mips::F12, Mips::F14
1711 };
1712 static const unsigned F64Regs[] = {
1713 Mips::D6, Mips::D7
1714 };
1715
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001716 // ByVal Args
1717 if (ArgFlags.isByVal()) {
1718 State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1719 1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1720 unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1721 for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1722 r < std::min(IntRegsSize, NextReg); ++r)
1723 State.AllocateReg(IntRegs[r]);
1724 return false;
1725 }
1726
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001727 // Promote i8 and i16
1728 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1729 LocVT = MVT::i32;
1730 if (ArgFlags.isSExt())
1731 LocInfo = CCValAssign::SExt;
1732 else if (ArgFlags.isZExt())
1733 LocInfo = CCValAssign::ZExt;
1734 else
1735 LocInfo = CCValAssign::AExt;
1736 }
1737
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001738 unsigned Reg;
1739
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001740 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1741 // is true: function is vararg, argument is 3rd or higher, there is previous
1742 // argument which is not f32 or f64.
1743 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1744 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001745 unsigned OrigAlign = ArgFlags.getOrigAlign();
1746 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001747
1748 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001749 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00001750 // If this is the first part of an i64 arg,
1751 // the allocated register must be either A0 or A2.
1752 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1753 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001754 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001755 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1756 // Allocate int register and shadow next int register. If first
1757 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001758 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1759 if (Reg == Mips::A1 || Reg == Mips::A3)
1760 Reg = State.AllocateReg(IntRegs, IntRegsSize);
1761 State.AllocateReg(IntRegs, IntRegsSize);
1762 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001763 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1764 // we are guaranteed to find an available float register
1765 if (ValVT == MVT::f32) {
1766 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1767 // Shadow int register
1768 State.AllocateReg(IntRegs, IntRegsSize);
1769 } else {
1770 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1771 // Shadow int registers
1772 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1773 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1774 State.AllocateReg(IntRegs, IntRegsSize);
1775 State.AllocateReg(IntRegs, IntRegsSize);
1776 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001777 } else
1778 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001779
Akira Hatanakad37776d2011-05-20 21:39:54 +00001780 unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1781 unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1782
1783 if (!Reg)
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001784 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakad37776d2011-05-20 21:39:54 +00001785 else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001786 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001787
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00001788 return false; // CC must always match
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001789}
1790
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001791//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00001792// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001793//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001794
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001795static const unsigned O32IntRegsSize = 4;
1796
1797static const unsigned O32IntRegs[] = {
1798 Mips::A0, Mips::A1, Mips::A2, Mips::A3
1799};
1800
1801// Write ByVal Arg to arg registers and stack.
1802static void
1803WriteByValArg(SDValue& Chain, DebugLoc dl,
1804 SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1805 SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1806 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
Akira Hatanakaedacba82011-05-25 17:32:06 +00001807 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001808 MVT PtrType, bool isLittle) {
1809 unsigned LocMemOffset = VA.getLocMemOffset();
1810 unsigned Offset = 0;
1811 uint32_t RemainingSize = Flags.getByValSize();
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001812 unsigned ByValAlign = Flags.getByValAlign();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001813
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001814 // Copy the first 4 words of byval arg to registers A0 - A3.
1815 // FIXME: Use a stricter alignment if it enables better optimization in passes
1816 // run later.
1817 for (; RemainingSize >= 4 && LocMemOffset < 4 * 4;
1818 Offset += 4, RemainingSize -= 4, LocMemOffset += 4) {
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001819 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001820 DAG.getConstant(Offset, MVT::i32));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001821 SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1822 MachinePointerInfo(),
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +00001823 false, false, std::min(ByValAlign,
1824 (unsigned )4));
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001825 MemOpChains.push_back(LoadVal.getValue(1));
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001826 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001827 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1828 }
1829
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001830 if (RemainingSize == 0)
1831 return;
1832
1833 // If there still is a register available for argument passing, write the
1834 // remaining part of the structure to it using subword loads and shifts.
1835 if (LocMemOffset < 4 * 4) {
1836 assert(RemainingSize <= 3 && RemainingSize >= 1 &&
1837 "There must be one to three bytes remaining.");
1838 unsigned LoadSize = (RemainingSize == 3 ? 2 : RemainingSize);
1839 SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1840 DAG.getConstant(Offset, MVT::i32));
1841 unsigned Alignment = std::min(ByValAlign, (unsigned )4);
1842 SDValue LoadVal = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
1843 LoadPtr, MachinePointerInfo(),
1844 MVT::getIntegerVT(LoadSize * 8), false,
1845 false, Alignment);
1846 MemOpChains.push_back(LoadVal.getValue(1));
1847
1848 // If target is big endian, shift it to the most significant half-word or
1849 // byte.
1850 if (!isLittle)
1851 LoadVal = DAG.getNode(ISD::SHL, dl, MVT::i32, LoadVal,
1852 DAG.getConstant(32 - LoadSize * 8, MVT::i32));
1853
1854 Offset += LoadSize;
1855 RemainingSize -= LoadSize;
1856
1857 // Read second subword if necessary.
1858 if (RemainingSize != 0) {
1859 assert(RemainingSize == 1 && "There must be one byte remaining.");
1860 LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1861 DAG.getConstant(Offset, MVT::i32));
1862 unsigned Alignment = std::min(ByValAlign, (unsigned )2);
1863 SDValue Subword = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, Chain,
1864 LoadPtr, MachinePointerInfo(),
1865 MVT::i8, false, false, Alignment);
1866 MemOpChains.push_back(Subword.getValue(1));
1867 // Insert the loaded byte to LoadVal.
1868 // FIXME: Use INS if supported by target.
1869 unsigned ShiftAmt = isLittle ? 16 : 8;
1870 SDValue Shift = DAG.getNode(ISD::SHL, dl, MVT::i32, Subword,
1871 DAG.getConstant(ShiftAmt, MVT::i32));
1872 LoadVal = DAG.getNode(ISD::OR, dl, MVT::i32, LoadVal, Shift);
1873 }
1874
1875 unsigned DstReg = O32IntRegs[LocMemOffset / 4];
1876 RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1877 return;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001878 }
Akira Hatanaka5ac85472011-08-18 23:39:37 +00001879
1880 // Create a fixed object on stack at offset LocMemOffset and copy
1881 // remaining part of byval arg to it using memcpy.
1882 SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1883 DAG.getConstant(Offset, MVT::i32));
1884 LastFI = MFI->CreateFixedObject(RemainingSize, LocMemOffset, true);
1885 SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
1886 Chain = DAG.getMemcpy(Chain, dl, Dst, Src,
1887 DAG.getConstant(RemainingSize, MVT::i32),
1888 std::min(ByValAlign, (unsigned)4),
1889 /*isVolatile=*/false, /*AlwaysInline=*/false,
1890 MachinePointerInfo(0), MachinePointerInfo(0));
1891 MemOpChains.push_back(Chain);
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00001892}
1893
Dan Gohman98ca4f22009-08-05 01:29:28 +00001894/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00001895/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00001896/// TODO: isTailCall.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001897SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001898MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001899 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001900 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001901 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001902 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001903 const SmallVectorImpl<ISD::InputArg> &Ins,
1904 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001905 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001906 // MIPs target does not yet support tail call optimization.
1907 isTailCall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001908
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001909 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001910 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00001911 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001912 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Akira Hatanaka17a1e872011-05-20 18:39:33 +00001913 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001914
1915 // Analyze operands of the call, assigning locations to each operand.
1916 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001917 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1918 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00001919
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001920 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00001921 CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
Akira Hatanakabdd2ce92011-05-23 21:13:59 +00001922 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00001923 CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001924
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001925 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001926 unsigned NextStackOffset = CCInfo.getNextStackOffset();
1927
1928 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NextStackOffset,
1929 true));
1930
1931 // If this is the first call, create a stack frame object that points to
1932 // a location to which .cprestore saves $gp.
1933 if (IsPIC && !MipsFI->getGPFI())
1934 MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
1935
Akira Hatanaka21afc632011-06-21 00:40:49 +00001936 // Get the frame index of the stack frame object that points to the location
1937 // of dynamically allocated area on the stack.
1938 int DynAllocFI = MipsFI->getDynAllocFI();
1939
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001940 // Update size of the maximum argument space.
1941 // For O32, a minimum of four words (16 bytes) of argument space is
1942 // allocated.
1943 if (Subtarget->isABI_O32())
1944 NextStackOffset = std::max(NextStackOffset, (unsigned)16);
1945
1946 unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1947
1948 if (MaxCallFrameSize < NextStackOffset) {
1949 MipsFI->setMaxCallFrameSize(NextStackOffset);
1950
Akira Hatanaka21afc632011-06-21 00:40:49 +00001951 // Set the offsets relative to $sp of the $gp restore slot and dynamically
1952 // allocated stack space. These offsets must be aligned to a boundary
1953 // determined by the stack alignment of the ABI.
1954 unsigned StackAlignment = TFL->getStackAlignment();
1955 NextStackOffset = (NextStackOffset + StackAlignment - 1) /
1956 StackAlignment * StackAlignment;
1957
1958 if (IsPIC)
1959 MFI->setObjectOffset(MipsFI->getGPFI(), NextStackOffset);
1960
1961 MFI->setObjectOffset(DynAllocFI, NextStackOffset);
Akira Hatanaka3d21c242011-06-08 17:39:33 +00001962 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001963
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00001964 // With EABI is it possible to have 16 args on registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001965 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1966 SmallVector<SDValue, 8> MemOpChains;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001967
Eric Christopher471e4222011-06-08 23:55:35 +00001968 int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0;
Akira Hatanaka43299772011-05-20 23:22:14 +00001969
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001970 // Walk the register/memloc assignments, inserting copies/loads.
1971 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00001972 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001973 CCValAssign &VA = ArgLocs[i];
1974
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001975 // Promote the value if needed.
1976 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001977 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001978 case CCValAssign::Full:
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001979 if (Subtarget->isABI_O32() && VA.isRegLoc()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001980 if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001981 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001982 if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001983 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1984 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00001985 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1986 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00001987 if (!Subtarget->isLittle())
1988 std::swap(Lo, Hi);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001989 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
1990 RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
1991 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001992 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00001993 }
1994 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00001995 case CCValAssign::SExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001996 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00001997 break;
1998 case CCValAssign::ZExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00001999 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002000 break;
2001 case CCValAssign::AExt:
Dale Johannesen33c960f2009-02-04 20:06:27 +00002002 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002003 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002004 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002005
2006 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002007 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002008 if (VA.isRegLoc()) {
2009 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002010 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002011 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002012
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002013 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002014 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002015
Eric Christopher471e4222011-06-08 23:55:35 +00002016 // ByVal Arg.
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002017 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2018 if (Flags.isByVal()) {
2019 assert(Subtarget->isABI_O32() &&
2020 "No support for ByVal args by ABIs other than O32 yet.");
2021 assert(Flags.getByValSize() &&
2022 "ByVal args of size 0 should have been ignored by front-end.");
2023 WriteByValArg(Chain, dl, RegsToPass, MemOpChains, LastFI, MFI, DAG, Arg,
Akira Hatanaka5ac85472011-08-18 23:39:37 +00002024 VA, Flags, getPointerTy(), Subtarget->isLittle());
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002025 continue;
2026 }
2027
Chris Lattnere0b12152008-03-17 06:57:02 +00002028 // Create the frame index object for this incoming parameter
Eric Christopher471e4222011-06-08 23:55:35 +00002029 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002030 VA.getLocMemOffset(), true);
Akira Hatanaka43299772011-05-20 23:22:14 +00002031 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnere0b12152008-03-17 06:57:02 +00002032
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002033 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002034 // parameter value to a stack Location
Chris Lattner8026a9d2010-09-21 17:50:43 +00002035 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
2036 MachinePointerInfo(),
David Greenef6fa1862010-02-15 16:56:10 +00002037 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002038 }
2039
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002040 // Extend range of indices of frame objects for outgoing arguments that were
2041 // created during this function call. Skip this step if no such objects were
2042 // created.
2043 if (LastFI)
2044 MipsFI->extendOutArgFIRange(FirstFI, LastFI);
2045
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002046 // Transform all store nodes into one single node because all store
2047 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002048 if (!MemOpChains.empty())
2049 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002050 &MemOpChains[0], MemOpChains.size());
2051
Bill Wendling056292f2008-09-16 21:48:12 +00002052 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002053 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2054 // node so that legalize doesn't hack it.
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002055 unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002056 bool LoadSymAddr = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002057 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002058
2059 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002060 if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
2061 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2062 getPointerTy(), 0,MipsII:: MO_GOT);
2063 CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
2064 0, MipsII::MO_ABS_LO);
2065 } else {
2066 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
2067 getPointerTy(), 0, OpFlag);
2068 }
2069
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002070 LoadSymAddr = true;
2071 }
2072 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002073 Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002074 getPointerTy(), OpFlag);
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002075 LoadSymAddr = true;
2076 }
2077
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002078 SDValue InFlag;
2079
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002080 // Create nodes that load address of callee and copy it to T9
2081 if (IsPIC) {
2082 if (LoadSymAddr) {
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002083 // Load callee address
Akira Hatanaka342837d2011-05-28 01:07:07 +00002084 Callee = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, Callee);
Akira Hatanaka25eba392011-06-24 19:01:25 +00002085 SDValue LoadValue = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), Callee,
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002086 MachinePointerInfo::getGOT(),
2087 false, false, 0);
2088
2089 // Use GOT+LO if callee has internal linkage.
2090 if (CalleeLo.getNode()) {
2091 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
2092 Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
2093 } else
2094 Callee = LoadValue;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002095 }
2096
2097 // copy to T9
2098 Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
2099 InFlag = Chain.getValue(1);
2100 Callee = DAG.getRegister(Mips::T9, MVT::i32);
2101 }
Bill Wendling056292f2008-09-16 21:48:12 +00002102
Akira Hatanakacd0f90f2011-05-20 02:30:51 +00002103 // Build a sequence of copy-to-reg nodes chained together with token
2104 // chain and flag operands which copy the outgoing args into registers.
2105 // The InFlag in necessary since all emitted instructions must be
2106 // stuck together.
2107 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2108 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2109 RegsToPass[i].second, InFlag);
2110 InFlag = Chain.getValue(1);
2111 }
2112
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002113 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002114 // = Chain, Callee, Reg#1, Reg#2, ...
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002115 //
2116 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002117 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002118 SmallVector<SDValue, 8> Ops;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002119 Ops.push_back(Chain);
2120 Ops.push_back(Callee);
2121
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002122 // Add argument registers to the end of the list so that they are
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002123 // known live into the call.
2124 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2125 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2126 RegsToPass[i].second.getValueType()));
2127
Gabor Greifba36cb52008-08-28 21:40:38 +00002128 if (InFlag.getNode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002129 Ops.push_back(InFlag);
2130
Dale Johannesen33c960f2009-02-04 20:06:27 +00002131 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002132 InFlag = Chain.getValue(1);
2133
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002134 // Create the CALLSEQ_END node.
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00002135 Chain = DAG.getCALLSEQ_END(Chain,
2136 DAG.getIntPtrConstant(NextStackOffset, true),
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002137 DAG.getIntPtrConstant(0, true), InFlag);
2138 InFlag = Chain.getValue(1);
2139
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002140 // Handle result values, copying them out of physregs into vregs that we
2141 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002142 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2143 Ins, dl, DAG, InVals);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002144}
2145
Dan Gohman98ca4f22009-08-05 01:29:28 +00002146/// LowerCallResult - Lower the result values of a call into the
2147/// appropriate copies out of appropriate physical registers.
2148SDValue
2149MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002150 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002151 const SmallVectorImpl<ISD::InputArg> &Ins,
2152 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002153 SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002154 // Assign locations to each value returned by this call.
2155 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002156 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2157 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002158
Dan Gohman98ca4f22009-08-05 01:29:28 +00002159 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002160
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002161 // Copy all of the result registers out of their specified physreg.
2162 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00002163 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00002164 RVLocs[i].getValVT(), InFlag).getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002165 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002166 InVals.push_back(Chain.getValue(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002167 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002168
Dan Gohman98ca4f22009-08-05 01:29:28 +00002169 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002170}
2171
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002172//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002173// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002174//===----------------------------------------------------------------------===//
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002175static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2176 std::vector<SDValue>& OutChains,
2177 SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2178 const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2179 unsigned LocMem = VA.getLocMemOffset();
2180 unsigned FirstWord = LocMem / 4;
2181
2182 // copy register A0 - A3 to frame object
2183 for (unsigned i = 0; i < NumWords; ++i) {
2184 unsigned CurWord = FirstWord + i;
2185 if (CurWord >= O32IntRegsSize)
2186 break;
2187
2188 unsigned SrcReg = O32IntRegs[CurWord];
2189 unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2190 SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2191 DAG.getConstant(i * 4, MVT::i32));
2192 SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2193 StorePtr, MachinePointerInfo(), false,
2194 false, 0);
2195 OutChains.push_back(Store);
2196 }
2197}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002198
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002199/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002200/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002201SDValue
2202MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002203 CallingConv::ID CallConv,
2204 bool isVarArg,
2205 const SmallVectorImpl<ISD::InputArg>
2206 &Ins,
2207 DebugLoc dl, SelectionDAG &DAG,
2208 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002209 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002210 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002211 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002212 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002213
Dan Gohman1e93df62010-04-17 14:41:14 +00002214 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002215
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002216 // Used with vargs to acumulate store chains.
2217 std::vector<SDValue> OutChains;
2218
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002219 // Assign locations to all of the incoming arguments.
2220 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002221 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2222 getTargetMachine(), ArgLocs, *DAG.getContext());
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002223
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002224 if (Subtarget->isABI_O32())
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002225 CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002226 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00002227 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002228
Akira Hatanaka43299772011-05-20 23:22:14 +00002229 int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002230
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002231 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002232 CCValAssign &VA = ArgLocs[i];
2233
2234 // Arguments stored on registers
2235 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002236 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002237 unsigned ArgReg = VA.getLocReg();
Bill Wendling06b8c192008-07-09 05:55:53 +00002238 TargetRegisterClass *RC = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002239
Owen Anderson825b72b2009-08-11 20:47:22 +00002240 if (RegVT == MVT::i32)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002241 RC = Mips::CPURegsRegisterClass;
2242 else if (RegVT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002243 RC = Mips::FGR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00002244 else if (RegVT == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002245 if (!Subtarget->isSingleFloat())
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002246 RC = Mips::AFGR64RegisterClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002247 } else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002248 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002249
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002250 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002251 // physical registers into virtual ones
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002252 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002253 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002254
2255 // If this is an 8 or 16-bit value, it has been passed promoted
2256 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002257 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002258 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002259 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002260 if (VA.getLocInfo() == CCValAssign::SExt)
2261 Opcode = ISD::AssertSext;
2262 else if (VA.getLocInfo() == CCValAssign::ZExt)
2263 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002264 if (Opcode)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002265 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
Chris Lattnerd4015072009-03-26 05:28:14 +00002266 DAG.getValueType(VA.getValVT()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00002267 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002268 }
2269
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002270 // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002271 if (Subtarget->isABI_O32()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002272 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
2273 ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
Owen Anderson825b72b2009-08-11 20:47:22 +00002274 if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002275 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002276 VA.getLocReg()+1, RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002277 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002278 if (!Subtarget->isLittle())
2279 std::swap(ArgValue, ArgValue2);
2280 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2281 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002282 }
2283 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002284
Dan Gohman98ca4f22009-08-05 01:29:28 +00002285 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002286 } else { // VA.isRegLoc()
2287
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002288 // sanity check
2289 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002290
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002291 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2292
2293 if (Flags.isByVal()) {
2294 assert(Subtarget->isABI_O32() &&
2295 "No support for ByVal args by ABIs other than O32 yet.");
2296 assert(Flags.getByValSize() &&
2297 "ByVal args of size 0 should have been ignored by front-end.");
2298 unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2299 LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2300 true);
2301 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2302 InVals.push_back(FIN);
2303 ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2304
2305 continue;
2306 }
2307
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002308 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002309 LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
2310 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002311
2312 // Create load nodes to retrieve arguments from the stack
Akira Hatanaka43299772011-05-20 23:22:14 +00002313 SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002314 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Akira Hatanaka43299772011-05-20 23:22:14 +00002315 MachinePointerInfo::getFixedStack(LastFI),
David Greenef6fa1862010-02-15 16:56:10 +00002316 false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002317 }
2318 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002319
2320 // The mips ABIs for returning structs by value requires that we copy
2321 // the sret argument into $v0 for the return. Save the argument into
2322 // a virtual register so that we can access it from the return points.
2323 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2324 unsigned Reg = MipsFI->getSRetReturnReg();
2325 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002326 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002327 MipsFI->setSRetReturnReg(Reg);
2328 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002329 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00002330 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002331 }
2332
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002333 if (isVarArg && Subtarget->isABI_O32()) {
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002334 // Record the frame index of the first variable argument
Eric Christopher471e4222011-06-08 23:55:35 +00002335 // which is a value necessary to VASTART.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002336 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002337 assert(NextStackOffset % 4 == 0 &&
2338 "NextStackOffset must be aligned to 4-byte boundaries.");
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002339 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
2340 MipsFI->setVarArgsFrameIndex(LastFI);
Akira Hatanakaedacba82011-05-25 17:32:06 +00002341
2342 // If NextStackOffset is smaller than o32's 16-byte reserved argument area,
2343 // copy the integer registers that have not been used for argument passing
2344 // to the caller's stack frame.
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002345 for (; NextStackOffset < 16; NextStackOffset += 4) {
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +00002346 TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002347 unsigned Idx = NextStackOffset / 4;
2348 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), O32IntRegs[Idx], RC);
2349 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
Akira Hatanaka69c19f72011-05-23 20:16:59 +00002350 LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002351 SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2352 OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2353 MachinePointerInfo(),
2354 false, false, 0));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002355 }
2356 }
2357
Akira Hatanaka43299772011-05-20 23:22:14 +00002358 MipsFI->setLastInArgFI(LastFI);
2359
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002360 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002361 // the size of Ins and InVals. This only happens when on varg functions
2362 if (!OutChains.empty()) {
2363 OutChains.push_back(Chain);
2364 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2365 &OutChains[0], OutChains.size());
2366 }
2367
Dan Gohman98ca4f22009-08-05 01:29:28 +00002368 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002369}
2370
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002371//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002372// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002373//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002374
Dan Gohman98ca4f22009-08-05 01:29:28 +00002375SDValue
2376MipsTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002377 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002378 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002379 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00002380 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002381
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002382 // CCValAssign - represent the assignment of
2383 // the return value to a location
2384 SmallVector<CCValAssign, 16> RVLocs;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002385
2386 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +00002387 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2388 getTargetMachine(), RVLocs, *DAG.getContext());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002389
Dan Gohman98ca4f22009-08-05 01:29:28 +00002390 // Analize return values.
2391 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002392
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002393 // If this is the first return lowered for this function, add
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002394 // the regs to the liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00002395 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002396 for (unsigned i = 0; i != RVLocs.size(); ++i)
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002397 if (RVLocs[i].isRegLoc())
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002398 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002399 }
2400
Dan Gohman475871a2008-07-27 21:46:04 +00002401 SDValue Flag;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002402
2403 // Copy the result values into the output registers.
2404 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2405 CCValAssign &VA = RVLocs[i];
2406 assert(VA.isRegLoc() && "Can only return in registers!");
2407
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002408 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00002409 OutVals[i], Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002410
2411 // guarantee that all emitted copies are
2412 // stuck together, avoiding something bad
2413 Flag = Chain.getValue(1);
2414 }
2415
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002416 // The mips ABIs for returning structs by value requires that we copy
2417 // the sret argument into $v0 for the return. We saved the argument into
2418 // a virtual register in the entry block, so now we copy the value out
2419 // and into $v0.
2420 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2421 MachineFunction &MF = DAG.getMachineFunction();
2422 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2423 unsigned Reg = MipsFI->getSRetReturnReg();
2424
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002425 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002426 llvm_unreachable("sret virtual register not created in the entry block");
Dale Johannesena05dca42009-02-04 23:02:30 +00002427 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002428
Dale Johannesena05dca42009-02-04 23:02:30 +00002429 Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002430 Flag = Chain.getValue(1);
2431 }
2432
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002433 // Return on Mips is always a "jr $ra"
Gabor Greifba36cb52008-08-28 21:40:38 +00002434 if (Flag.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002435 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002436 Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002437 else // Return Void
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002438 return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
Owen Anderson825b72b2009-08-11 20:47:22 +00002439 Chain, DAG.getRegister(Mips::RA, MVT::i32));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002440}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002441
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002442//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002443// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002444//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002445
2446/// getConstraintType - Given a constraint letter, return the type of
2447/// constraint it is for this target.
2448MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002449getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002450{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002451 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002452 // GCC config/mips/constraints.md
2453 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002454 // 'd' : An address register. Equivalent to r
2455 // unless generating MIPS16 code.
2456 // 'y' : Equivalent to r; retained for
2457 // backwards compatibility.
2458 // 'f' : Floating Point registers.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002459 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002460 switch (Constraint[0]) {
2461 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002462 case 'd':
2463 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002464 case 'f':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002465 return C_RegisterClass;
2466 break;
2467 }
2468 }
2469 return TargetLowering::getConstraintType(Constraint);
2470}
2471
John Thompson44ab89e2010-10-29 17:29:13 +00002472/// Examine constraint type and operand type and determine a weight value.
2473/// This object must already have been set up with the operand type
2474/// and the current alternative constraint selected.
2475TargetLowering::ConstraintWeight
2476MipsTargetLowering::getSingleConstraintMatchWeight(
2477 AsmOperandInfo &info, const char *constraint) const {
2478 ConstraintWeight weight = CW_Invalid;
2479 Value *CallOperandVal = info.CallOperandVal;
2480 // If we don't have a value, we can't do a match,
2481 // but allow it at the lowest weight.
2482 if (CallOperandVal == NULL)
2483 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002484 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002485 // Look at the constraint type.
2486 switch (*constraint) {
2487 default:
2488 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2489 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002490 case 'd':
2491 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002492 if (type->isIntegerTy())
2493 weight = CW_Register;
2494 break;
2495 case 'f':
2496 if (type->isFloatTy())
2497 weight = CW_Register;
2498 break;
2499 }
2500 return weight;
2501}
2502
Eric Christopher38d64262011-06-29 19:33:04 +00002503/// Given a register class constraint, like 'r', if this corresponds directly
2504/// to an LLVM register class, return a register of 0 and the register class
2505/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002506std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00002507getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002508{
2509 if (Constraint.size() == 1) {
2510 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002511 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2512 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002513 case 'r':
2514 return std::make_pair(0U, Mips::CPURegsRegisterClass);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002515 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002516 if (VT == MVT::f32)
Bruno Cardoso Lopesbdfbb742009-03-21 00:05:07 +00002517 return std::make_pair(0U, Mips::FGR32RegisterClass);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002518 if (VT == MVT::f64)
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002519 if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2520 return std::make_pair(0U, Mips::AFGR64RegisterClass);
Eric Christopher314aff12011-06-29 19:04:31 +00002521 break;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002522 }
2523 }
2524 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2525}
2526
Dan Gohman6520e202008-10-18 02:06:02 +00002527bool
2528MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2529 // The Mips target isn't yet aware of offsets.
2530 return false;
2531}
Evan Chengeb2f9692009-10-27 19:56:55 +00002532
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002533bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2534 if (VT != MVT::f32 && VT != MVT::f64)
2535 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00002536 if (Imm.isNegZero())
2537 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00002538 return Imm.isZero();
2539}