Bug Summary

File:include/llvm/CodeGen/SelectionDAGNodes.h
Warning:line 1163, column 10
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name MipsISelLowering.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-10~svn374877/build-llvm/lib/Target/Mips -I /build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips -I /build/llvm-toolchain-snapshot-10~svn374877/build-llvm/include -I /build/llvm-toolchain-snapshot-10~svn374877/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-10~svn374877/build-llvm/lib/Target/Mips -fdebug-prefix-map=/build/llvm-toolchain-snapshot-10~svn374877=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2019-10-15-233810-7101-1 -x c++ /build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp

/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp

1//===- MipsISelLowering.cpp - Mips DAG Lowering Implementation ------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the interfaces that Mips uses to lower LLVM code into a
10// selection DAG.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MipsISelLowering.h"
15#include "MCTargetDesc/MipsBaseInfo.h"
16#include "MCTargetDesc/MipsInstPrinter.h"
17#include "MCTargetDesc/MipsMCTargetDesc.h"
18#include "MipsCCState.h"
19#include "MipsInstrInfo.h"
20#include "MipsMachineFunction.h"
21#include "MipsRegisterInfo.h"
22#include "MipsSubtarget.h"
23#include "MipsTargetMachine.h"
24#include "MipsTargetObjectFile.h"
25#include "llvm/ADT/APFloat.h"
26#include "llvm/ADT/ArrayRef.h"
27#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/Statistic.h"
29#include "llvm/ADT/StringRef.h"
30#include "llvm/ADT/StringSwitch.h"
31#include "llvm/CodeGen/CallingConvLower.h"
32#include "llvm/CodeGen/FunctionLoweringInfo.h"
33#include "llvm/CodeGen/ISDOpcodes.h"
34#include "llvm/CodeGen/MachineBasicBlock.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
36#include "llvm/CodeGen/MachineFunction.h"
37#include "llvm/CodeGen/MachineInstr.h"
38#include "llvm/CodeGen/MachineInstrBuilder.h"
39#include "llvm/CodeGen/MachineJumpTableInfo.h"
40#include "llvm/CodeGen/MachineMemOperand.h"
41#include "llvm/CodeGen/MachineOperand.h"
42#include "llvm/CodeGen/MachineRegisterInfo.h"
43#include "llvm/CodeGen/RuntimeLibcalls.h"
44#include "llvm/CodeGen/SelectionDAG.h"
45#include "llvm/CodeGen/SelectionDAGNodes.h"
46#include "llvm/CodeGen/TargetFrameLowering.h"
47#include "llvm/CodeGen/TargetInstrInfo.h"
48#include "llvm/CodeGen/TargetRegisterInfo.h"
49#include "llvm/CodeGen/ValueTypes.h"
50#include "llvm/IR/CallingConv.h"
51#include "llvm/IR/Constants.h"
52#include "llvm/IR/DataLayout.h"
53#include "llvm/IR/DebugLoc.h"
54#include "llvm/IR/DerivedTypes.h"
55#include "llvm/IR/Function.h"
56#include "llvm/IR/GlobalValue.h"
57#include "llvm/IR/Type.h"
58#include "llvm/IR/Value.h"
59#include "llvm/MC/MCContext.h"
60#include "llvm/MC/MCRegisterInfo.h"
61#include "llvm/Support/Casting.h"
62#include "llvm/Support/CodeGen.h"
63#include "llvm/Support/CommandLine.h"
64#include "llvm/Support/Compiler.h"
65#include "llvm/Support/ErrorHandling.h"
66#include "llvm/Support/MachineValueType.h"
67#include "llvm/Support/MathExtras.h"
68#include "llvm/Target/TargetMachine.h"
69#include "llvm/Target/TargetOptions.h"
70#include <algorithm>
71#include <cassert>
72#include <cctype>
73#include <cstdint>
74#include <deque>
75#include <iterator>
76#include <utility>
77#include <vector>
78
79using namespace llvm;
80
81#define DEBUG_TYPE"mips-lower" "mips-lower"
82
83STATISTIC(NumTailCalls, "Number of tail calls")static llvm::Statistic NumTailCalls = {"mips-lower", "NumTailCalls"
, "Number of tail calls"}
;
84
85static cl::opt<bool>
86NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
87 cl::desc("MIPS: Don't trap on integer division by zero."),
88 cl::init(false));
89
90extern cl::opt<bool> EmitJalrReloc;
91
92static const MCPhysReg Mips64DPRegs[8] = {
93 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
94 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
95};
96
97// If I is a shifted mask, set the size (Size) and the first bit of the
98// mask (Pos), and return true.
99// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
100static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
101 if (!isShiftedMask_64(I))
102 return false;
103
104 Size = countPopulation(I);
105 Pos = countTrailingZeros(I);
106 return true;
107}
108
109// The MIPS MSA ABI passes vector arguments in the integer register set.
110// The number of integer registers used is dependant on the ABI used.
111MVT MipsTargetLowering::getRegisterTypeForCallingConv(LLVMContext &Context,
112 CallingConv::ID CC,
113 EVT VT) const {
114 if (VT.isVector()) {
115 if (Subtarget.isABI_O32()) {
116 return MVT::i32;
117 } else {
118 return (VT.getSizeInBits() == 32) ? MVT::i32 : MVT::i64;
119 }
120 }
121 return MipsTargetLowering::getRegisterType(Context, VT);
122}
123
124unsigned MipsTargetLowering::getNumRegistersForCallingConv(LLVMContext &Context,
125 CallingConv::ID CC,
126 EVT VT) const {
127 if (VT.isVector())
128 return std::max((VT.getSizeInBits() / (Subtarget.isABI_O32() ? 32 : 64)),
129 1U);
130 return MipsTargetLowering::getNumRegisters(Context, VT);
131}
132
133unsigned MipsTargetLowering::getVectorTypeBreakdownForCallingConv(
134 LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT,
135 unsigned &NumIntermediates, MVT &RegisterVT) const {
136 // Break down vector types to either 2 i64s or 4 i32s.
137 RegisterVT = getRegisterTypeForCallingConv(Context, CC, VT);
138 IntermediateVT = RegisterVT;
139 NumIntermediates = VT.getSizeInBits() < RegisterVT.getSizeInBits()
140 ? VT.getVectorNumElements()
141 : VT.getSizeInBits() / RegisterVT.getSizeInBits();
142
143 return NumIntermediates;
144}
145
146SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
147 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
148 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
149}
150
151SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty,
152 SelectionDAG &DAG,
153 unsigned Flag) const {
154 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag);
155}
156
157SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty,
158 SelectionDAG &DAG,
159 unsigned Flag) const {
160 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
161}
162
163SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty,
164 SelectionDAG &DAG,
165 unsigned Flag) const {
166 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
167}
168
169SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty,
170 SelectionDAG &DAG,
171 unsigned Flag) const {
172 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
173}
174
175SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty,
176 SelectionDAG &DAG,
177 unsigned Flag) const {
178 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
179 N->getOffset(), Flag);
180}
181
182const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
183 switch ((MipsISD::NodeType)Opcode) {
184 case MipsISD::FIRST_NUMBER: break;
185 case MipsISD::JmpLink: return "MipsISD::JmpLink";
186 case MipsISD::TailCall: return "MipsISD::TailCall";
187 case MipsISD::Highest: return "MipsISD::Highest";
188 case MipsISD::Higher: return "MipsISD::Higher";
189 case MipsISD::Hi: return "MipsISD::Hi";
190 case MipsISD::Lo: return "MipsISD::Lo";
191 case MipsISD::GotHi: return "MipsISD::GotHi";
192 case MipsISD::TlsHi: return "MipsISD::TlsHi";
193 case MipsISD::GPRel: return "MipsISD::GPRel";
194 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
195 case MipsISD::Ret: return "MipsISD::Ret";
196 case MipsISD::ERet: return "MipsISD::ERet";
197 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
198 case MipsISD::FMS: return "MipsISD::FMS";
199 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
200 case MipsISD::FPCmp: return "MipsISD::FPCmp";
201 case MipsISD::FSELECT: return "MipsISD::FSELECT";
202 case MipsISD::MTC1_D64: return "MipsISD::MTC1_D64";
203 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
204 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
205 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
206 case MipsISD::MFHI: return "MipsISD::MFHI";
207 case MipsISD::MFLO: return "MipsISD::MFLO";
208 case MipsISD::MTLOHI: return "MipsISD::MTLOHI";
209 case MipsISD::Mult: return "MipsISD::Mult";
210 case MipsISD::Multu: return "MipsISD::Multu";
211 case MipsISD::MAdd: return "MipsISD::MAdd";
212 case MipsISD::MAddu: return "MipsISD::MAddu";
213 case MipsISD::MSub: return "MipsISD::MSub";
214 case MipsISD::MSubu: return "MipsISD::MSubu";
215 case MipsISD::DivRem: return "MipsISD::DivRem";
216 case MipsISD::DivRemU: return "MipsISD::DivRemU";
217 case MipsISD::DivRem16: return "MipsISD::DivRem16";
218 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
219 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
220 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
221 case MipsISD::Wrapper: return "MipsISD::Wrapper";
222 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
223 case MipsISD::Sync: return "MipsISD::Sync";
224 case MipsISD::Ext: return "MipsISD::Ext";
225 case MipsISD::Ins: return "MipsISD::Ins";
226 case MipsISD::CIns: return "MipsISD::CIns";
227 case MipsISD::LWL: return "MipsISD::LWL";
228 case MipsISD::LWR: return "MipsISD::LWR";
229 case MipsISD::SWL: return "MipsISD::SWL";
230 case MipsISD::SWR: return "MipsISD::SWR";
231 case MipsISD::LDL: return "MipsISD::LDL";
232 case MipsISD::LDR: return "MipsISD::LDR";
233 case MipsISD::SDL: return "MipsISD::SDL";
234 case MipsISD::SDR: return "MipsISD::SDR";
235 case MipsISD::EXTP: return "MipsISD::EXTP";
236 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
237 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
238 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
239 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
240 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
241 case MipsISD::SHILO: return "MipsISD::SHILO";
242 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
243 case MipsISD::MULSAQ_S_W_PH: return "MipsISD::MULSAQ_S_W_PH";
244 case MipsISD::MAQ_S_W_PHL: return "MipsISD::MAQ_S_W_PHL";
245 case MipsISD::MAQ_S_W_PHR: return "MipsISD::MAQ_S_W_PHR";
246 case MipsISD::MAQ_SA_W_PHL: return "MipsISD::MAQ_SA_W_PHL";
247 case MipsISD::MAQ_SA_W_PHR: return "MipsISD::MAQ_SA_W_PHR";
248 case MipsISD::DPAU_H_QBL: return "MipsISD::DPAU_H_QBL";
249 case MipsISD::DPAU_H_QBR: return "MipsISD::DPAU_H_QBR";
250 case MipsISD::DPSU_H_QBL: return "MipsISD::DPSU_H_QBL";
251 case MipsISD::DPSU_H_QBR: return "MipsISD::DPSU_H_QBR";
252 case MipsISD::DPAQ_S_W_PH: return "MipsISD::DPAQ_S_W_PH";
253 case MipsISD::DPSQ_S_W_PH: return "MipsISD::DPSQ_S_W_PH";
254 case MipsISD::DPAQ_SA_L_W: return "MipsISD::DPAQ_SA_L_W";
255 case MipsISD::DPSQ_SA_L_W: return "MipsISD::DPSQ_SA_L_W";
256 case MipsISD::DPA_W_PH: return "MipsISD::DPA_W_PH";
257 case MipsISD::DPS_W_PH: return "MipsISD::DPS_W_PH";
258 case MipsISD::DPAQX_S_W_PH: return "MipsISD::DPAQX_S_W_PH";
259 case MipsISD::DPAQX_SA_W_PH: return "MipsISD::DPAQX_SA_W_PH";
260 case MipsISD::DPAX_W_PH: return "MipsISD::DPAX_W_PH";
261 case MipsISD::DPSX_W_PH: return "MipsISD::DPSX_W_PH";
262 case MipsISD::DPSQX_S_W_PH: return "MipsISD::DPSQX_S_W_PH";
263 case MipsISD::DPSQX_SA_W_PH: return "MipsISD::DPSQX_SA_W_PH";
264 case MipsISD::MULSA_W_PH: return "MipsISD::MULSA_W_PH";
265 case MipsISD::MULT: return "MipsISD::MULT";
266 case MipsISD::MULTU: return "MipsISD::MULTU";
267 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
268 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
269 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
270 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
271 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
272 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
273 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
274 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
275 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
276 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
277 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
278 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
279 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
280 case MipsISD::VCEQ: return "MipsISD::VCEQ";
281 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
282 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
283 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
284 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
285 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
286 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
287 case MipsISD::VNOR: return "MipsISD::VNOR";
288 case MipsISD::VSHF: return "MipsISD::VSHF";
289 case MipsISD::SHF: return "MipsISD::SHF";
290 case MipsISD::ILVEV: return "MipsISD::ILVEV";
291 case MipsISD::ILVOD: return "MipsISD::ILVOD";
292 case MipsISD::ILVL: return "MipsISD::ILVL";
293 case MipsISD::ILVR: return "MipsISD::ILVR";
294 case MipsISD::PCKEV: return "MipsISD::PCKEV";
295 case MipsISD::PCKOD: return "MipsISD::PCKOD";
296 case MipsISD::INSVE: return "MipsISD::INSVE";
297 }
298 return nullptr;
299}
300
301MipsTargetLowering::MipsTargetLowering(const MipsTargetMachine &TM,
302 const MipsSubtarget &STI)
303 : TargetLowering(TM), Subtarget(STI), ABI(TM.getABI()) {
304 // Mips does not have i1 type, so use i32 for
305 // setcc operations results (slt, sgt, ...).
306 setBooleanContents(ZeroOrOneBooleanContent);
307 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
308 // The cmp.cond.fmt instruction in MIPS32r6/MIPS64r6 uses 0 and -1 like MSA
309 // does. Integer booleans still use 0 and 1.
310 if (Subtarget.hasMips32r6())
311 setBooleanContents(ZeroOrOneBooleanContent,
312 ZeroOrNegativeOneBooleanContent);
313
314 // Load extented operations for i1 types must be promoted
315 for (MVT VT : MVT::integer_valuetypes()) {
316 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
317 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
318 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
319 }
320
321 // MIPS doesn't have extending float->double load/store. Set LoadExtAction
322 // for f32, f16
323 for (MVT VT : MVT::fp_valuetypes()) {
324 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
325 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
326 }
327
328 // Set LoadExtAction for f16 vectors to Expand
329 for (MVT VT : MVT::fp_fixedlen_vector_valuetypes()) {
330 MVT F16VT = MVT::getVectorVT(MVT::f16, VT.getVectorNumElements());
331 if (F16VT.isValid())
332 setLoadExtAction(ISD::EXTLOAD, VT, F16VT, Expand);
333 }
334
335 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
336 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
337
338 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
339
340 // Used by legalize types to correctly generate the setcc result.
341 // Without this, every float setcc comes with a AND/OR with the result,
342 // we don't want this, since the fpcmp result goes to a flag register,
343 // which is used implicitly by brcond and select operations.
344 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
345
346 // Mips Custom Operations
347 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
348 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
349 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
350 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
351 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
352 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
353 setOperationAction(ISD::SELECT, MVT::f32, Custom);
354 setOperationAction(ISD::SELECT, MVT::f64, Custom);
355 setOperationAction(ISD::SELECT, MVT::i32, Custom);
356 setOperationAction(ISD::SETCC, MVT::f32, Custom);
357 setOperationAction(ISD::SETCC, MVT::f64, Custom);
358 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
359 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
360 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
361 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
362
363 if (!(TM.Options.NoNaNsFPMath || Subtarget.inAbs2008Mode())) {
364 setOperationAction(ISD::FABS, MVT::f32, Custom);
365 setOperationAction(ISD::FABS, MVT::f64, Custom);
366 }
367
368 if (Subtarget.isGP64bit()) {
369 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
370 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
371 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
372 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
373 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
374 setOperationAction(ISD::SELECT, MVT::i64, Custom);
375 setOperationAction(ISD::LOAD, MVT::i64, Custom);
376 setOperationAction(ISD::STORE, MVT::i64, Custom);
377 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
378 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
379 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
380 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
381 }
382
383 if (!Subtarget.isGP64bit()) {
384 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
385 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
386 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
387 }
388
389 setOperationAction(ISD::EH_DWARF_CFA, MVT::i32, Custom);
390 if (Subtarget.isGP64bit())
391 setOperationAction(ISD::EH_DWARF_CFA, MVT::i64, Custom);
392
393 setOperationAction(ISD::SDIV, MVT::i32, Expand);
394 setOperationAction(ISD::SREM, MVT::i32, Expand);
395 setOperationAction(ISD::UDIV, MVT::i32, Expand);
396 setOperationAction(ISD::UREM, MVT::i32, Expand);
397 setOperationAction(ISD::SDIV, MVT::i64, Expand);
398 setOperationAction(ISD::SREM, MVT::i64, Expand);
399 setOperationAction(ISD::UDIV, MVT::i64, Expand);
400 setOperationAction(ISD::UREM, MVT::i64, Expand);
401
402 // Operations not directly supported by Mips.
403 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
404 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
405 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
406 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
407 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
408 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
409 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
410 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
411 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
412 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
413 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
414 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
415 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
416 if (Subtarget.hasCnMips()) {
417 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
418 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
419 } else {
420 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
421 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
422 }
423 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
424 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
425 setOperationAction(ISD::ROTL, MVT::i32, Expand);
426 setOperationAction(ISD::ROTL, MVT::i64, Expand);
427 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
428 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
429
430 if (!Subtarget.hasMips32r2())
431 setOperationAction(ISD::ROTR, MVT::i32, Expand);
432
433 if (!Subtarget.hasMips64r2())
434 setOperationAction(ISD::ROTR, MVT::i64, Expand);
435
436 setOperationAction(ISD::FSIN, MVT::f32, Expand);
437 setOperationAction(ISD::FSIN, MVT::f64, Expand);
438 setOperationAction(ISD::FCOS, MVT::f32, Expand);
439 setOperationAction(ISD::FCOS, MVT::f64, Expand);
440 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
441 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
442 setOperationAction(ISD::FPOW, MVT::f32, Expand);
443 setOperationAction(ISD::FPOW, MVT::f64, Expand);
444 setOperationAction(ISD::FLOG, MVT::f32, Expand);
445 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
446 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
447 setOperationAction(ISD::FEXP, MVT::f32, Expand);
448 setOperationAction(ISD::FMA, MVT::f32, Expand);
449 setOperationAction(ISD::FMA, MVT::f64, Expand);
450 setOperationAction(ISD::FREM, MVT::f32, Expand);
451 setOperationAction(ISD::FREM, MVT::f64, Expand);
452
453 // Lower f16 conversion operations into library calls
454 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
455 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
456 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
457 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
458
459 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
460
461 setOperationAction(ISD::VASTART, MVT::Other, Custom);
462 setOperationAction(ISD::VAARG, MVT::Other, Custom);
463 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
464 setOperationAction(ISD::VAEND, MVT::Other, Expand);
465
466 // Use the default for now
467 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
468 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
469
470 if (!Subtarget.isGP64bit()) {
471 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
472 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
473 }
474
475 if (!Subtarget.hasMips32r2()) {
476 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
477 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
478 }
479
480 // MIPS16 lacks MIPS32's clz and clo instructions.
481 if (!Subtarget.hasMips32() || Subtarget.inMips16Mode())
482 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
483 if (!Subtarget.hasMips64())
484 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
485
486 if (!Subtarget.hasMips32r2())
487 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
488 if (!Subtarget.hasMips64r2())
489 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
490
491 if (Subtarget.isGP64bit()) {
492 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i32, Custom);
493 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i32, Custom);
494 setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i32, Custom);
495 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
496 }
497
498 setOperationAction(ISD::TRAP, MVT::Other, Legal);
499
500 setTargetDAGCombine(ISD::SDIVREM);
501 setTargetDAGCombine(ISD::UDIVREM);
502 setTargetDAGCombine(ISD::SELECT);
503 setTargetDAGCombine(ISD::AND);
504 setTargetDAGCombine(ISD::OR);
505 setTargetDAGCombine(ISD::ADD);
506 setTargetDAGCombine(ISD::SUB);
507 setTargetDAGCombine(ISD::AssertZext);
508 setTargetDAGCombine(ISD::SHL);
509
510 if (ABI.IsO32()) {
511 // These libcalls are not available in 32-bit.
512 setLibcallName(RTLIB::SHL_I128, nullptr);
513 setLibcallName(RTLIB::SRL_I128, nullptr);
514 setLibcallName(RTLIB::SRA_I128, nullptr);
515 }
516
517 setMinFunctionAlignment(Subtarget.isGP64bit() ? Align(8) : Align(4));
518
519 // The arguments on the stack are defined in terms of 4-byte slots on O32
520 // and 8-byte slots on N32/N64.
521 setMinStackArgumentAlignment((ABI.IsN32() || ABI.IsN64()) ? Align(8)
522 : Align(4));
523
524 setStackPointerRegisterToSaveRestore(ABI.IsN64() ? Mips::SP_64 : Mips::SP);
525
526 MaxStoresPerMemcpy = 16;
527
528 isMicroMips = Subtarget.inMicroMipsMode();
529}
530
531const MipsTargetLowering *MipsTargetLowering::create(const MipsTargetMachine &TM,
532 const MipsSubtarget &STI) {
533 if (STI.inMips16Mode())
534 return createMips16TargetLowering(TM, STI);
535
536 return createMipsSETargetLowering(TM, STI);
537}
538
539// Create a fast isel object.
540FastISel *
541MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
542 const TargetLibraryInfo *libInfo) const {
543 const MipsTargetMachine &TM =
544 static_cast<const MipsTargetMachine &>(funcInfo.MF->getTarget());
545
546 // We support only the standard encoding [MIPS32,MIPS32R5] ISAs.
547 bool UseFastISel = TM.Options.EnableFastISel && Subtarget.hasMips32() &&
548 !Subtarget.hasMips32r6() && !Subtarget.inMips16Mode() &&
549 !Subtarget.inMicroMipsMode();
550
551 // Disable if either of the following is true:
552 // We do not generate PIC, the ABI is not O32, XGOT is being used.
553 if (!TM.isPositionIndependent() || !TM.getABI().IsO32() ||
554 Subtarget.useXGOT())
555 UseFastISel = false;
556
557 return UseFastISel ? Mips::createFastISel(funcInfo, libInfo) : nullptr;
558}
559
560EVT MipsTargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
561 EVT VT) const {
562 if (!VT.isVector())
563 return MVT::i32;
564 return VT.changeVectorElementTypeToInteger();
565}
566
567static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
568 TargetLowering::DAGCombinerInfo &DCI,
569 const MipsSubtarget &Subtarget) {
570 if (DCI.isBeforeLegalizeOps())
571 return SDValue();
572
573 EVT Ty = N->getValueType(0);
574 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
575 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
576 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
577 MipsISD::DivRemU16;
578 SDLoc DL(N);
579
580 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
581 N->getOperand(0), N->getOperand(1));
582 SDValue InChain = DAG.getEntryNode();
583 SDValue InGlue = DivRem;
584
585 // insert MFLO
586 if (N->hasAnyUseOfValue(0)) {
587 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
588 InGlue);
589 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
590 InChain = CopyFromLo.getValue(1);
591 InGlue = CopyFromLo.getValue(2);
592 }
593
594 // insert MFHI
595 if (N->hasAnyUseOfValue(1)) {
596 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
597 HI, Ty, InGlue);
598 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
599 }
600
601 return SDValue();
602}
603
604static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
605 switch (CC) {
606 default: llvm_unreachable("Unknown fp condition code!")::llvm::llvm_unreachable_internal("Unknown fp condition code!"
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 606)
;
607 case ISD::SETEQ:
608 case ISD::SETOEQ: return Mips::FCOND_OEQ;
609 case ISD::SETUNE: return Mips::FCOND_UNE;
610 case ISD::SETLT:
611 case ISD::SETOLT: return Mips::FCOND_OLT;
612 case ISD::SETGT:
613 case ISD::SETOGT: return Mips::FCOND_OGT;
614 case ISD::SETLE:
615 case ISD::SETOLE: return Mips::FCOND_OLE;
616 case ISD::SETGE:
617 case ISD::SETOGE: return Mips::FCOND_OGE;
618 case ISD::SETULT: return Mips::FCOND_ULT;
619 case ISD::SETULE: return Mips::FCOND_ULE;
620 case ISD::SETUGT: return Mips::FCOND_UGT;
621 case ISD::SETUGE: return Mips::FCOND_UGE;
622 case ISD::SETUO: return Mips::FCOND_UN;
623 case ISD::SETO: return Mips::FCOND_OR;
624 case ISD::SETNE:
625 case ISD::SETONE: return Mips::FCOND_ONE;
626 case ISD::SETUEQ: return Mips::FCOND_UEQ;
627 }
628}
629
630/// This function returns true if the floating point conditional branches and
631/// conditional moves which use condition code CC should be inverted.
632static bool invertFPCondCodeUser(Mips::CondCode CC) {
633 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
634 return false;
635
636 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&(((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
&& "Illegal Condition Code") ? static_cast<void>
(0) : __assert_fail ("(CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) && \"Illegal Condition Code\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 637, __PRETTY_FUNCTION__))
637 "Illegal Condition Code")(((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
&& "Illegal Condition Code") ? static_cast<void>
(0) : __assert_fail ("(CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) && \"Illegal Condition Code\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 637, __PRETTY_FUNCTION__))
;
638
639 return true;
640}
641
642// Creates and returns an FPCmp node from a setcc node.
643// Returns Op if setcc is not a floating point comparison.
644static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
645 // must be a SETCC node
646 if (Op.getOpcode() != ISD::SETCC)
647 return Op;
648
649 SDValue LHS = Op.getOperand(0);
650
651 if (!LHS.getValueType().isFloatingPoint())
652 return Op;
653
654 SDValue RHS = Op.getOperand(1);
655 SDLoc DL(Op);
656
657 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
658 // node if necessary.
659 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
660
661 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
662 DAG.getConstant(condCodeToFCC(CC), DL, MVT::i32));
663}
664
665// Creates and returns a CMovFPT/F node.
666static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
667 SDValue False, const SDLoc &DL) {
668 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
669 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
670 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
671
672 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
673 True.getValueType(), True, FCC0, False, Cond);
674}
675
676static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
677 TargetLowering::DAGCombinerInfo &DCI,
678 const MipsSubtarget &Subtarget) {
679 if (DCI.isBeforeLegalizeOps())
680 return SDValue();
681
682 SDValue SetCC = N->getOperand(0);
683
684 if ((SetCC.getOpcode() != ISD::SETCC) ||
685 !SetCC.getOperand(0).getValueType().isInteger())
686 return SDValue();
687
688 SDValue False = N->getOperand(2);
689 EVT FalseTy = False.getValueType();
690
691 if (!FalseTy.isInteger())
692 return SDValue();
693
694 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
695
696 // If the RHS (False) is 0, we swap the order of the operands
697 // of ISD::SELECT (obviously also inverting the condition) so that we can
698 // take advantage of conditional moves using the $0 register.
699 // Example:
700 // return (a != 0) ? x : 0;
701 // load $reg, x
702 // movz $reg, $0, a
703 if (!FalseC)
704 return SDValue();
705
706 const SDLoc DL(N);
707
708 if (!FalseC->getZExtValue()) {
709 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
710 SDValue True = N->getOperand(1);
711
712 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
713 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
714
715 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
716 }
717
718 // If both operands are integer constants there's a possibility that we
719 // can do some interesting optimizations.
720 SDValue True = N->getOperand(1);
721 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
722
723 if (!TrueC || !True.getValueType().isInteger())
724 return SDValue();
725
726 // We'll also ignore MVT::i64 operands as this optimizations proves
727 // to be ineffective because of the required sign extensions as the result
728 // of a SETCC operator is always MVT::i32 for non-vector types.
729 if (True.getValueType() == MVT::i64)
730 return SDValue();
731
732 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
733
734 // 1) (a < x) ? y : y-1
735 // slti $reg1, a, x
736 // addiu $reg2, $reg1, y-1
737 if (Diff == 1)
738 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
739
740 // 2) (a < x) ? y-1 : y
741 // slti $reg1, a, x
742 // xor $reg1, $reg1, 1
743 // addiu $reg2, $reg1, y-1
744 if (Diff == -1) {
745 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
746 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
747 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
748 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
749 }
750
751 // Could not optimize.
752 return SDValue();
753}
754
755static SDValue performCMovFPCombine(SDNode *N, SelectionDAG &DAG,
756 TargetLowering::DAGCombinerInfo &DCI,
757 const MipsSubtarget &Subtarget) {
758 if (DCI.isBeforeLegalizeOps())
759 return SDValue();
760
761 SDValue ValueIfTrue = N->getOperand(0), ValueIfFalse = N->getOperand(2);
762
763 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(ValueIfFalse);
764 if (!FalseC || FalseC->getZExtValue())
765 return SDValue();
766
767 // Since RHS (False) is 0, we swap the order of the True/False operands
768 // (obviously also inverting the condition) so that we can
769 // take advantage of conditional moves using the $0 register.
770 // Example:
771 // return (a != 0) ? x : 0;
772 // load $reg, x
773 // movz $reg, $0, a
774 unsigned Opc = (N->getOpcode() == MipsISD::CMovFP_T) ? MipsISD::CMovFP_F :
775 MipsISD::CMovFP_T;
776
777 SDValue FCC = N->getOperand(1), Glue = N->getOperand(3);
778 return DAG.getNode(Opc, SDLoc(N), ValueIfFalse.getValueType(),
779 ValueIfFalse, FCC, ValueIfTrue, Glue);
780}
781
782static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
783 TargetLowering::DAGCombinerInfo &DCI,
784 const MipsSubtarget &Subtarget) {
785 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
786 return SDValue();
787
788 SDValue FirstOperand = N->getOperand(0);
789 unsigned FirstOperandOpc = FirstOperand.getOpcode();
790 SDValue Mask = N->getOperand(1);
791 EVT ValTy = N->getValueType(0);
792 SDLoc DL(N);
793
794 uint64_t Pos = 0, SMPos, SMSize;
795 ConstantSDNode *CN;
796 SDValue NewOperand;
797 unsigned Opc;
798
799 // Op's second operand must be a shifted mask.
800 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
801 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
802 return SDValue();
803
804 if (FirstOperandOpc == ISD::SRA || FirstOperandOpc == ISD::SRL) {
805 // Pattern match EXT.
806 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
807 // => ext $dst, $src, pos, size
808
809 // The second operand of the shift must be an immediate.
810 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))))
811 return SDValue();
812
813 Pos = CN->getZExtValue();
814
815 // Return if the shifted mask does not start at bit 0 or the sum of its size
816 // and Pos exceeds the word's size.
817 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
818 return SDValue();
819
820 Opc = MipsISD::Ext;
821 NewOperand = FirstOperand.getOperand(0);
822 } else if (FirstOperandOpc == ISD::SHL && Subtarget.hasCnMips()) {
823 // Pattern match CINS.
824 // $dst = and (shl $src , pos), mask
825 // => cins $dst, $src, pos, size
826 // mask is a shifted mask with consecutive 1's, pos = shift amount,
827 // size = population count.
828
829 // The second operand of the shift must be an immediate.
830 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))))
831 return SDValue();
832
833 Pos = CN->getZExtValue();
834
835 if (SMPos != Pos || Pos >= ValTy.getSizeInBits() || SMSize >= 32 ||
836 Pos + SMSize > ValTy.getSizeInBits())
837 return SDValue();
838
839 NewOperand = FirstOperand.getOperand(0);
840 // SMSize is 'location' (position) in this case, not size.
841 SMSize--;
842 Opc = MipsISD::CIns;
843 } else {
844 // Pattern match EXT.
845 // $dst = and $src, (2**size - 1) , if size > 16
846 // => ext $dst, $src, pos, size , pos = 0
847
848 // If the mask is <= 0xffff, andi can be used instead.
849 if (CN->getZExtValue() <= 0xffff)
850 return SDValue();
851
852 // Return if the mask doesn't start at position 0.
853 if (SMPos)
854 return SDValue();
855
856 Opc = MipsISD::Ext;
857 NewOperand = FirstOperand;
858 }
859 return DAG.getNode(Opc, DL, ValTy, NewOperand,
860 DAG.getConstant(Pos, DL, MVT::i32),
861 DAG.getConstant(SMSize, DL, MVT::i32));
862}
863
864static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
865 TargetLowering::DAGCombinerInfo &DCI,
866 const MipsSubtarget &Subtarget) {
867 // Pattern match INS.
868 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
869 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
870 // => ins $dst, $src, size, pos, $src1
871 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
872 return SDValue();
873
874 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
875 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
876 ConstantSDNode *CN, *CN1;
877
878 // See if Op's first operand matches (and $src1 , mask0).
879 if (And0.getOpcode() != ISD::AND)
880 return SDValue();
881
882 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
883 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
884 return SDValue();
885
886 // See if Op's second operand matches (and (shl $src, pos), mask1).
887 if (And1.getOpcode() == ISD::AND &&
888 And1.getOperand(0).getOpcode() == ISD::SHL) {
889
890 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
891 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
892 return SDValue();
893
894 // The shift masks must have the same position and size.
895 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
896 return SDValue();
897
898 SDValue Shl = And1.getOperand(0);
899
900 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
901 return SDValue();
902
903 unsigned Shamt = CN->getZExtValue();
904
905 // Return if the shift amount and the first bit position of mask are not the
906 // same.
907 EVT ValTy = N->getValueType(0);
908 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
909 return SDValue();
910
911 SDLoc DL(N);
912 return DAG.getNode(MipsISD::Ins, DL, ValTy, Shl.getOperand(0),
913 DAG.getConstant(SMPos0, DL, MVT::i32),
914 DAG.getConstant(SMSize0, DL, MVT::i32),
915 And0.getOperand(0));
916 } else {
917 // Pattern match DINS.
918 // $dst = or (and $src, mask0), mask1
919 // where mask0 = ((1 << SMSize0) -1) << SMPos0
920 // => dins $dst, $src, pos, size
921 if (~CN->getSExtValue() == ((((int64_t)1 << SMSize0) - 1) << SMPos0) &&
922 ((SMSize0 + SMPos0 <= 64 && Subtarget.hasMips64r2()) ||
923 (SMSize0 + SMPos0 <= 32))) {
924 // Check if AND instruction has constant as argument
925 bool isConstCase = And1.getOpcode() != ISD::AND;
926 if (And1.getOpcode() == ISD::AND) {
927 if (!(CN1 = dyn_cast<ConstantSDNode>(And1->getOperand(1))))
928 return SDValue();
929 } else {
930 if (!(CN1 = dyn_cast<ConstantSDNode>(N->getOperand(1))))
931 return SDValue();
932 }
933 // Don't generate INS if constant OR operand doesn't fit into bits
934 // cleared by constant AND operand.
935 if (CN->getSExtValue() & CN1->getSExtValue())
936 return SDValue();
937
938 SDLoc DL(N);
939 EVT ValTy = N->getOperand(0)->getValueType(0);
940 SDValue Const1;
941 SDValue SrlX;
942 if (!isConstCase) {
943 Const1 = DAG.getConstant(SMPos0, DL, MVT::i32);
944 SrlX = DAG.getNode(ISD::SRL, DL, And1->getValueType(0), And1, Const1);
945 }
946 return DAG.getNode(
947 MipsISD::Ins, DL, N->getValueType(0),
948 isConstCase
949 ? DAG.getConstant(CN1->getSExtValue() >> SMPos0, DL, ValTy)
950 : SrlX,
951 DAG.getConstant(SMPos0, DL, MVT::i32),
952 DAG.getConstant(ValTy.getSizeInBits() / 8 < 8 ? SMSize0 & 31
953 : SMSize0,
954 DL, MVT::i32),
955 And0->getOperand(0));
956
957 }
958 return SDValue();
959 }
960}
961
962static SDValue performMADD_MSUBCombine(SDNode *ROOTNode, SelectionDAG &CurDAG,
963 const MipsSubtarget &Subtarget) {
964 // ROOTNode must have a multiplication as an operand for the match to be
965 // successful.
966 if (ROOTNode->getOperand(0).getOpcode() != ISD::MUL &&
967 ROOTNode->getOperand(1).getOpcode() != ISD::MUL)
968 return SDValue();
969
970 // We don't handle vector types here.
971 if (ROOTNode->getValueType(0).isVector())
972 return SDValue();
973
974 // For MIPS64, madd / msub instructions are inefficent to use with 64 bit
975 // arithmetic. E.g.
976 // (add (mul a b) c) =>
977 // let res = (madd (mthi (drotr c 32))x(mtlo c) a b) in
978 // MIPS64: (or (dsll (mfhi res) 32) (dsrl (dsll (mflo res) 32) 32)
979 // or
980 // MIPS64R2: (dins (mflo res) (mfhi res) 32 32)
981 //
982 // The overhead of setting up the Hi/Lo registers and reassembling the
983 // result makes this a dubious optimzation for MIPS64. The core of the
984 // problem is that Hi/Lo contain the upper and lower 32 bits of the
985 // operand and result.
986 //
987 // It requires a chain of 4 add/mul for MIPS64R2 to get better code
988 // density than doing it naively, 5 for MIPS64. Additionally, using
989 // madd/msub on MIPS64 requires the operands actually be 32 bit sign
990 // extended operands, not true 64 bit values.
991 //
992 // FIXME: For the moment, disable this completely for MIPS64.
993 if (Subtarget.hasMips64())
994 return SDValue();
995
996 SDValue Mult = ROOTNode->getOperand(0).getOpcode() == ISD::MUL
997 ? ROOTNode->getOperand(0)
998 : ROOTNode->getOperand(1);
999
1000 SDValue AddOperand = ROOTNode->getOperand(0).getOpcode() == ISD::MUL
1001 ? ROOTNode->getOperand(1)
1002 : ROOTNode->getOperand(0);
1003
1004 // Transform this to a MADD only if the user of this node is the add.
1005 // If there are other users of the mul, this function returns here.
1006 if (!Mult.hasOneUse())
1007 return SDValue();
1008
1009 // maddu and madd are unusual instructions in that on MIPS64 bits 63..31
1010 // must be in canonical form, i.e. sign extended. For MIPS32, the operands
1011 // of the multiply must have 32 or more sign bits, otherwise we cannot
1012 // perform this optimization. We have to check this here as we're performing
1013 // this optimization pre-legalization.
1014 SDValue MultLHS = Mult->getOperand(0);
1015 SDValue MultRHS = Mult->getOperand(1);
1016
1017 bool IsSigned = MultLHS->getOpcode() == ISD::SIGN_EXTEND &&
1018 MultRHS->getOpcode() == ISD::SIGN_EXTEND;
1019 bool IsUnsigned = MultLHS->getOpcode() == ISD::ZERO_EXTEND &&
1020 MultRHS->getOpcode() == ISD::ZERO_EXTEND;
1021
1022 if (!IsSigned && !IsUnsigned)
1023 return SDValue();
1024
1025 // Initialize accumulator.
1026 SDLoc DL(ROOTNode);
1027 SDValue TopHalf;
1028 SDValue BottomHalf;
1029 BottomHalf = CurDAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, AddOperand,
1030 CurDAG.getIntPtrConstant(0, DL));
1031
1032 TopHalf = CurDAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, AddOperand,
1033 CurDAG.getIntPtrConstant(1, DL));
1034 SDValue ACCIn = CurDAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
1035 BottomHalf,
1036 TopHalf);
1037
1038 // Create MipsMAdd(u) / MipsMSub(u) node.
1039 bool IsAdd = ROOTNode->getOpcode() == ISD::ADD;
1040 unsigned Opcode = IsAdd ? (IsUnsigned ? MipsISD::MAddu : MipsISD::MAdd)
1041 : (IsUnsigned ? MipsISD::MSubu : MipsISD::MSub);
1042 SDValue MAddOps[3] = {
1043 CurDAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mult->getOperand(0)),
1044 CurDAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mult->getOperand(1)), ACCIn};
1045 EVT VTs[2] = {MVT::i32, MVT::i32};
1046 SDValue MAdd = CurDAG.getNode(Opcode, DL, VTs, MAddOps);
1047
1048 SDValue ResLo = CurDAG.getNode(MipsISD::MFLO, DL, MVT::i32, MAdd);
1049 SDValue ResHi = CurDAG.getNode(MipsISD::MFHI, DL, MVT::i32, MAdd);
1050 SDValue Combined =
1051 CurDAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, ResLo, ResHi);
1052 return Combined;
1053}
1054
1055static SDValue performSUBCombine(SDNode *N, SelectionDAG &DAG,
1056 TargetLowering::DAGCombinerInfo &DCI,
1057 const MipsSubtarget &Subtarget) {
1058 // (sub v0 (mul v1, v2)) => (msub v1, v2, v0)
1059 if (DCI.isBeforeLegalizeOps()) {
1060 if (Subtarget.hasMips32() && !Subtarget.hasMips32r6() &&
1061 !Subtarget.inMips16Mode() && N->getValueType(0) == MVT::i64)
1062 return performMADD_MSUBCombine(N, DAG, Subtarget);
1063
1064 return SDValue();
1065 }
1066
1067 return SDValue();
1068}
1069
1070static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
1071 TargetLowering::DAGCombinerInfo &DCI,
1072 const MipsSubtarget &Subtarget) {
1073 // (add v0 (mul v1, v2)) => (madd v1, v2, v0)
1074 if (DCI.isBeforeLegalizeOps()) {
1075 if (Subtarget.hasMips32() && !Subtarget.hasMips32r6() &&
1076 !Subtarget.inMips16Mode() && N->getValueType(0) == MVT::i64)
1077 return performMADD_MSUBCombine(N, DAG, Subtarget);
1078
1079 return SDValue();
1080 }
1081
1082 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
1083 SDValue Add = N->getOperand(1);
1084
1085 if (Add.getOpcode() != ISD::ADD)
1086 return SDValue();
1087
1088 SDValue Lo = Add.getOperand(1);
1089
1090 if ((Lo.getOpcode() != MipsISD::Lo) ||
1091 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
1092 return SDValue();
1093
1094 EVT ValTy = N->getValueType(0);
1095 SDLoc DL(N);
1096
1097 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
1098 Add.getOperand(0));
1099 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
1100}
1101
1102static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
1103 TargetLowering::DAGCombinerInfo &DCI,
1104 const MipsSubtarget &Subtarget) {
1105 // Pattern match CINS.
1106 // $dst = shl (and $src , imm), pos
1107 // => cins $dst, $src, pos, size
1108
1109 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasCnMips())
1110 return SDValue();
1111
1112 SDValue FirstOperand = N->getOperand(0);
1113 unsigned FirstOperandOpc = FirstOperand.getOpcode();
1114 SDValue SecondOperand = N->getOperand(1);
1115 EVT ValTy = N->getValueType(0);
1116 SDLoc DL(N);
1117
1118 uint64_t Pos = 0, SMPos, SMSize;
1119 ConstantSDNode *CN;
1120 SDValue NewOperand;
1121
1122 // The second operand of the shift must be an immediate.
1123 if (!(CN = dyn_cast<ConstantSDNode>(SecondOperand)))
1124 return SDValue();
1125
1126 Pos = CN->getZExtValue();
1127
1128 if (Pos >= ValTy.getSizeInBits())
1129 return SDValue();
1130
1131 if (FirstOperandOpc != ISD::AND)
1132 return SDValue();
1133
1134 // AND's second operand must be a shifted mask.
1135 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))) ||
1136 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
1137 return SDValue();
1138
1139 // Return if the shifted mask does not start at bit 0 or the sum of its size
1140 // and Pos exceeds the word's size.
1141 if (SMPos != 0 || SMSize > 32 || Pos + SMSize > ValTy.getSizeInBits())
1142 return SDValue();
1143
1144 NewOperand = FirstOperand.getOperand(0);
1145 // SMSize is 'location' (position) in this case, not size.
1146 SMSize--;
1147
1148 return DAG.getNode(MipsISD::CIns, DL, ValTy, NewOperand,
1149 DAG.getConstant(Pos, DL, MVT::i32),
1150 DAG.getConstant(SMSize, DL, MVT::i32));
1151}
1152
1153SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
1154 const {
1155 SelectionDAG &DAG = DCI.DAG;
1156 unsigned Opc = N->getOpcode();
1157
1158 switch (Opc) {
1159 default: break;
1160 case ISD::SDIVREM:
1161 case ISD::UDIVREM:
1162 return performDivRemCombine(N, DAG, DCI, Subtarget);
1163 case ISD::SELECT:
1164 return performSELECTCombine(N, DAG, DCI, Subtarget);
1165 case MipsISD::CMovFP_F:
1166 case MipsISD::CMovFP_T:
1167 return performCMovFPCombine(N, DAG, DCI, Subtarget);
1168 case ISD::AND:
1169 return performANDCombine(N, DAG, DCI, Subtarget);
1170 case ISD::OR:
1171 return performORCombine(N, DAG, DCI, Subtarget);
1172 case ISD::ADD:
1173 return performADDCombine(N, DAG, DCI, Subtarget);
1174 case ISD::SHL:
1175 return performSHLCombine(N, DAG, DCI, Subtarget);
1176 case ISD::SUB:
1177 return performSUBCombine(N, DAG, DCI, Subtarget);
1178 }
1179
1180 return SDValue();
1181}
1182
1183bool MipsTargetLowering::isCheapToSpeculateCttz() const {
1184 return Subtarget.hasMips32();
1185}
1186
1187bool MipsTargetLowering::isCheapToSpeculateCtlz() const {
1188 return Subtarget.hasMips32();
1189}
1190
1191bool MipsTargetLowering::shouldFoldConstantShiftPairToMask(
1192 const SDNode *N, CombineLevel Level) const {
1193 if (N->getOperand(0).getValueType().isVector())
1194 return false;
1195 return true;
1196}
1197
1198void
1199MipsTargetLowering::LowerOperationWrapper(SDNode *N,
1200 SmallVectorImpl<SDValue> &Results,
1201 SelectionDAG &DAG) const {
1202 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
1203
1204 if (Res)
1205 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
1206 Results.push_back(Res.getValue(I));
1207}
1208
1209void
1210MipsTargetLowering::ReplaceNodeResults(SDNode *N,
1211 SmallVectorImpl<SDValue> &Results,
1212 SelectionDAG &DAG) const {
1213 return LowerOperationWrapper(N, Results, DAG);
1214}
1215
1216SDValue MipsTargetLowering::
1217LowerOperation(SDValue Op, SelectionDAG &DAG) const
1218{
1219 switch (Op.getOpcode())
1220 {
1221 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
1222 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
1223 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
1224 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
1225 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
1226 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
1227 case ISD::SELECT: return lowerSELECT(Op, DAG);
1228 case ISD::SETCC: return lowerSETCC(Op, DAG);
1229 case ISD::VASTART: return lowerVASTART(Op, DAG);
1230 case ISD::VAARG: return lowerVAARG(Op, DAG);
1231 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
1232 case ISD::FABS: return lowerFABS(Op, DAG);
1233 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
1234 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
1235 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
1236 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
1237 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
1238 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
1239 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
1240 case ISD::LOAD: return lowerLOAD(Op, DAG);
1241 case ISD::STORE: return lowerSTORE(Op, DAG);
1242 case ISD::EH_DWARF_CFA: return lowerEH_DWARF_CFA(Op, DAG);
1243 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
1244 }
1245 return SDValue();
1246}
1247
1248//===----------------------------------------------------------------------===//
1249// Lower helper functions
1250//===----------------------------------------------------------------------===//
1251
1252// addLiveIn - This helper function adds the specified physical register to the
1253// MachineFunction as a live in value. It also creates a corresponding
1254// virtual register for it.
1255static unsigned
1256addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
1257{
1258 Register VReg = MF.getRegInfo().createVirtualRegister(RC);
1259 MF.getRegInfo().addLiveIn(PReg, VReg);
1260 return VReg;
1261}
1262
1263static MachineBasicBlock *insertDivByZeroTrap(MachineInstr &MI,
1264 MachineBasicBlock &MBB,
1265 const TargetInstrInfo &TII,
1266 bool Is64Bit, bool IsMicroMips) {
1267 if (NoZeroDivCheck)
1268 return &MBB;
1269
1270 // Insert instruction "teq $divisor_reg, $zero, 7".
1271 MachineBasicBlock::iterator I(MI);
1272 MachineInstrBuilder MIB;
1273 MachineOperand &Divisor = MI.getOperand(2);
1274 MIB = BuildMI(MBB, std::next(I), MI.getDebugLoc(),
1275 TII.get(IsMicroMips ? Mips::TEQ_MM : Mips::TEQ))
1276 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
1277 .addReg(Mips::ZERO)
1278 .addImm(7);
1279
1280 // Use the 32-bit sub-register if this is a 64-bit division.
1281 if (Is64Bit)
1282 MIB->getOperand(0).setSubReg(Mips::sub_32);
1283
1284 // Clear Divisor's kill flag.
1285 Divisor.setIsKill(false);
1286
1287 // We would normally delete the original instruction here but in this case
1288 // we only needed to inject an additional instruction rather than replace it.
1289
1290 return &MBB;
1291}
1292
1293MachineBasicBlock *
1294MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
1295 MachineBasicBlock *BB) const {
1296 switch (MI.getOpcode()) {
1297 default:
1298 llvm_unreachable("Unexpected instr type to insert")::llvm::llvm_unreachable_internal("Unexpected instr type to insert"
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 1298)
;
1299 case Mips::ATOMIC_LOAD_ADD_I8:
1300 return emitAtomicBinaryPartword(MI, BB, 1);
1301 case Mips::ATOMIC_LOAD_ADD_I16:
1302 return emitAtomicBinaryPartword(MI, BB, 2);
1303 case Mips::ATOMIC_LOAD_ADD_I32:
1304 return emitAtomicBinary(MI, BB);
1305 case Mips::ATOMIC_LOAD_ADD_I64:
1306 return emitAtomicBinary(MI, BB);
1307
1308 case Mips::ATOMIC_LOAD_AND_I8:
1309 return emitAtomicBinaryPartword(MI, BB, 1);
1310 case Mips::ATOMIC_LOAD_AND_I16:
1311 return emitAtomicBinaryPartword(MI, BB, 2);
1312 case Mips::ATOMIC_LOAD_AND_I32:
1313 return emitAtomicBinary(MI, BB);
1314 case Mips::ATOMIC_LOAD_AND_I64:
1315 return emitAtomicBinary(MI, BB);
1316
1317 case Mips::ATOMIC_LOAD_OR_I8:
1318 return emitAtomicBinaryPartword(MI, BB, 1);
1319 case Mips::ATOMIC_LOAD_OR_I16:
1320 return emitAtomicBinaryPartword(MI, BB, 2);
1321 case Mips::ATOMIC_LOAD_OR_I32:
1322 return emitAtomicBinary(MI, BB);
1323 case Mips::ATOMIC_LOAD_OR_I64:
1324 return emitAtomicBinary(MI, BB);
1325
1326 case Mips::ATOMIC_LOAD_XOR_I8:
1327 return emitAtomicBinaryPartword(MI, BB, 1);
1328 case Mips::ATOMIC_LOAD_XOR_I16:
1329 return emitAtomicBinaryPartword(MI, BB, 2);
1330 case Mips::ATOMIC_LOAD_XOR_I32:
1331 return emitAtomicBinary(MI, BB);
1332 case Mips::ATOMIC_LOAD_XOR_I64:
1333 return emitAtomicBinary(MI, BB);
1334
1335 case Mips::ATOMIC_LOAD_NAND_I8:
1336 return emitAtomicBinaryPartword(MI, BB, 1);
1337 case Mips::ATOMIC_LOAD_NAND_I16:
1338 return emitAtomicBinaryPartword(MI, BB, 2);
1339 case Mips::ATOMIC_LOAD_NAND_I32:
1340 return emitAtomicBinary(MI, BB);
1341 case Mips::ATOMIC_LOAD_NAND_I64:
1342 return emitAtomicBinary(MI, BB);
1343
1344 case Mips::ATOMIC_LOAD_SUB_I8:
1345 return emitAtomicBinaryPartword(MI, BB, 1);
1346 case Mips::ATOMIC_LOAD_SUB_I16:
1347 return emitAtomicBinaryPartword(MI, BB, 2);
1348 case Mips::ATOMIC_LOAD_SUB_I32:
1349 return emitAtomicBinary(MI, BB);
1350 case Mips::ATOMIC_LOAD_SUB_I64:
1351 return emitAtomicBinary(MI, BB);
1352
1353 case Mips::ATOMIC_SWAP_I8:
1354 return emitAtomicBinaryPartword(MI, BB, 1);
1355 case Mips::ATOMIC_SWAP_I16:
1356 return emitAtomicBinaryPartword(MI, BB, 2);
1357 case Mips::ATOMIC_SWAP_I32:
1358 return emitAtomicBinary(MI, BB);
1359 case Mips::ATOMIC_SWAP_I64:
1360 return emitAtomicBinary(MI, BB);
1361
1362 case Mips::ATOMIC_CMP_SWAP_I8:
1363 return emitAtomicCmpSwapPartword(MI, BB, 1);
1364 case Mips::ATOMIC_CMP_SWAP_I16:
1365 return emitAtomicCmpSwapPartword(MI, BB, 2);
1366 case Mips::ATOMIC_CMP_SWAP_I32:
1367 return emitAtomicCmpSwap(MI, BB);
1368 case Mips::ATOMIC_CMP_SWAP_I64:
1369 return emitAtomicCmpSwap(MI, BB);
1370 case Mips::PseudoSDIV:
1371 case Mips::PseudoUDIV:
1372 case Mips::DIV:
1373 case Mips::DIVU:
1374 case Mips::MOD:
1375 case Mips::MODU:
1376 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false,
1377 false);
1378 case Mips::SDIV_MM_Pseudo:
1379 case Mips::UDIV_MM_Pseudo:
1380 case Mips::SDIV_MM:
1381 case Mips::UDIV_MM:
1382 case Mips::DIV_MMR6:
1383 case Mips::DIVU_MMR6:
1384 case Mips::MOD_MMR6:
1385 case Mips::MODU_MMR6:
1386 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false, true);
1387 case Mips::PseudoDSDIV:
1388 case Mips::PseudoDUDIV:
1389 case Mips::DDIV:
1390 case Mips::DDIVU:
1391 case Mips::DMOD:
1392 case Mips::DMODU:
1393 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), true, false);
1394
1395 case Mips::PseudoSELECT_I:
1396 case Mips::PseudoSELECT_I64:
1397 case Mips::PseudoSELECT_S:
1398 case Mips::PseudoSELECT_D32:
1399 case Mips::PseudoSELECT_D64:
1400 return emitPseudoSELECT(MI, BB, false, Mips::BNE);
1401 case Mips::PseudoSELECTFP_F_I:
1402 case Mips::PseudoSELECTFP_F_I64:
1403 case Mips::PseudoSELECTFP_F_S:
1404 case Mips::PseudoSELECTFP_F_D32:
1405 case Mips::PseudoSELECTFP_F_D64:
1406 return emitPseudoSELECT(MI, BB, true, Mips::BC1F);
1407 case Mips::PseudoSELECTFP_T_I:
1408 case Mips::PseudoSELECTFP_T_I64:
1409 case Mips::PseudoSELECTFP_T_S:
1410 case Mips::PseudoSELECTFP_T_D32:
1411 case Mips::PseudoSELECTFP_T_D64:
1412 return emitPseudoSELECT(MI, BB, true, Mips::BC1T);
1413 case Mips::PseudoD_SELECT_I:
1414 case Mips::PseudoD_SELECT_I64:
1415 return emitPseudoD_SELECT(MI, BB);
1416 }
1417}
1418
1419// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
1420// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
1421MachineBasicBlock *
1422MipsTargetLowering::emitAtomicBinary(MachineInstr &MI,
1423 MachineBasicBlock *BB) const {
1424
1425 MachineFunction *MF = BB->getParent();
1426 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1427 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1428 DebugLoc DL = MI.getDebugLoc();
1429
1430 unsigned AtomicOp;
1431 switch (MI.getOpcode()) {
1432 case Mips::ATOMIC_LOAD_ADD_I32:
1433 AtomicOp = Mips::ATOMIC_LOAD_ADD_I32_POSTRA;
1434 break;
1435 case Mips::ATOMIC_LOAD_SUB_I32:
1436 AtomicOp = Mips::ATOMIC_LOAD_SUB_I32_POSTRA;
1437 break;
1438 case Mips::ATOMIC_LOAD_AND_I32:
1439 AtomicOp = Mips::ATOMIC_LOAD_AND_I32_POSTRA;
1440 break;
1441 case Mips::ATOMIC_LOAD_OR_I32:
1442 AtomicOp = Mips::ATOMIC_LOAD_OR_I32_POSTRA;
1443 break;
1444 case Mips::ATOMIC_LOAD_XOR_I32:
1445 AtomicOp = Mips::ATOMIC_LOAD_XOR_I32_POSTRA;
1446 break;
1447 case Mips::ATOMIC_LOAD_NAND_I32:
1448 AtomicOp = Mips::ATOMIC_LOAD_NAND_I32_POSTRA;
1449 break;
1450 case Mips::ATOMIC_SWAP_I32:
1451 AtomicOp = Mips::ATOMIC_SWAP_I32_POSTRA;
1452 break;
1453 case Mips::ATOMIC_LOAD_ADD_I64:
1454 AtomicOp = Mips::ATOMIC_LOAD_ADD_I64_POSTRA;
1455 break;
1456 case Mips::ATOMIC_LOAD_SUB_I64:
1457 AtomicOp = Mips::ATOMIC_LOAD_SUB_I64_POSTRA;
1458 break;
1459 case Mips::ATOMIC_LOAD_AND_I64:
1460 AtomicOp = Mips::ATOMIC_LOAD_AND_I64_POSTRA;
1461 break;
1462 case Mips::ATOMIC_LOAD_OR_I64:
1463 AtomicOp = Mips::ATOMIC_LOAD_OR_I64_POSTRA;
1464 break;
1465 case Mips::ATOMIC_LOAD_XOR_I64:
1466 AtomicOp = Mips::ATOMIC_LOAD_XOR_I64_POSTRA;
1467 break;
1468 case Mips::ATOMIC_LOAD_NAND_I64:
1469 AtomicOp = Mips::ATOMIC_LOAD_NAND_I64_POSTRA;
1470 break;
1471 case Mips::ATOMIC_SWAP_I64:
1472 AtomicOp = Mips::ATOMIC_SWAP_I64_POSTRA;
1473 break;
1474 default:
1475 llvm_unreachable("Unknown pseudo atomic for replacement!")::llvm::llvm_unreachable_internal("Unknown pseudo atomic for replacement!"
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 1475)
;
1476 }
1477
1478 Register OldVal = MI.getOperand(0).getReg();
1479 Register Ptr = MI.getOperand(1).getReg();
1480 Register Incr = MI.getOperand(2).getReg();
1481 Register Scratch = RegInfo.createVirtualRegister(RegInfo.getRegClass(OldVal));
1482
1483 MachineBasicBlock::iterator II(MI);
1484
1485 // The scratch registers here with the EarlyClobber | Define | Implicit
1486 // flags is used to persuade the register allocator and the machine
1487 // verifier to accept the usage of this register. This has to be a real
1488 // register which has an UNDEF value but is dead after the instruction which
1489 // is unique among the registers chosen for the instruction.
1490
1491 // The EarlyClobber flag has the semantic properties that the operand it is
1492 // attached to is clobbered before the rest of the inputs are read. Hence it
1493 // must be unique among the operands to the instruction.
1494 // The Define flag is needed to coerce the machine verifier that an Undef
1495 // value isn't a problem.
1496 // The Dead flag is needed as the value in scratch isn't used by any other
1497 // instruction. Kill isn't used as Dead is more precise.
1498 // The implicit flag is here due to the interaction between the other flags
1499 // and the machine verifier.
1500
1501 // For correctness purpose, a new pseudo is introduced here. We need this
1502 // new pseudo, so that FastRegisterAllocator does not see an ll/sc sequence
1503 // that is spread over >1 basic blocks. A register allocator which
1504 // introduces (or any codegen infact) a store, can violate the expectations
1505 // of the hardware.
1506 //
1507 // An atomic read-modify-write sequence starts with a linked load
1508 // instruction and ends with a store conditional instruction. The atomic
1509 // read-modify-write sequence fails if any of the following conditions
1510 // occur between the execution of ll and sc:
1511 // * A coherent store is completed by another process or coherent I/O
1512 // module into the block of synchronizable physical memory containing
1513 // the word. The size and alignment of the block is
1514 // implementation-dependent.
1515 // * A coherent store is executed between an LL and SC sequence on the
1516 // same processor to the block of synchornizable physical memory
1517 // containing the word.
1518 //
1519
1520 Register PtrCopy = RegInfo.createVirtualRegister(RegInfo.getRegClass(Ptr));
1521 Register IncrCopy = RegInfo.createVirtualRegister(RegInfo.getRegClass(Incr));
1522
1523 BuildMI(*BB, II, DL, TII->get(Mips::COPY), IncrCopy).addReg(Incr);
1524 BuildMI(*BB, II, DL, TII->get(Mips::COPY), PtrCopy).addReg(Ptr);
1525
1526 BuildMI(*BB, II, DL, TII->get(AtomicOp))
1527 .addReg(OldVal, RegState::Define | RegState::EarlyClobber)
1528 .addReg(PtrCopy)
1529 .addReg(IncrCopy)
1530 .addReg(Scratch, RegState::Define | RegState::EarlyClobber |
1531 RegState::Implicit | RegState::Dead);
1532
1533 MI.eraseFromParent();
1534
1535 return BB;
1536}
1537
1538MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
1539 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
1540 unsigned SrcReg) const {
1541 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1542 const DebugLoc &DL = MI.getDebugLoc();
1543
1544 if (Subtarget.hasMips32r2() && Size == 1) {
1545 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1546 return BB;
1547 }
1548
1549 if (Subtarget.hasMips32r2() && Size == 2) {
1550 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1551 return BB;
1552 }
1553
1554 MachineFunction *MF = BB->getParent();
1555 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1556 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1557 Register ScrReg = RegInfo.createVirtualRegister(RC);
1558
1559 assert(Size < 32)((Size < 32) ? static_cast<void> (0) : __assert_fail
("Size < 32", "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 1559, __PRETTY_FUNCTION__))
;
1560 int64_t ShiftImm = 32 - (Size * 8);
1561
1562 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1563 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1564
1565 return BB;
1566}
1567
1568MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
1569 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size) const {
1570 assert((Size == 1 || Size == 2) &&(((Size == 1 || Size == 2) && "Unsupported size for EmitAtomicBinaryPartial."
) ? static_cast<void> (0) : __assert_fail ("(Size == 1 || Size == 2) && \"Unsupported size for EmitAtomicBinaryPartial.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 1571, __PRETTY_FUNCTION__))
1571 "Unsupported size for EmitAtomicBinaryPartial.")(((Size == 1 || Size == 2) && "Unsupported size for EmitAtomicBinaryPartial."
) ? static_cast<void> (0) : __assert_fail ("(Size == 1 || Size == 2) && \"Unsupported size for EmitAtomicBinaryPartial.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 1571, __PRETTY_FUNCTION__))
;
1572
1573 MachineFunction *MF = BB->getParent();
1574 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1575 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1576 const bool ArePtrs64bit = ABI.ArePtrs64bit();
1577 const TargetRegisterClass *RCp =
1578 getRegClassFor(ArePtrs64bit ? MVT::i64 : MVT::i32);
1579 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1580 DebugLoc DL = MI.getDebugLoc();
1581
1582 Register Dest = MI.getOperand(0).getReg();
1583 Register Ptr = MI.getOperand(1).getReg();
1584 Register Incr = MI.getOperand(2).getReg();
1585
1586 Register AlignedAddr = RegInfo.createVirtualRegister(RCp);
1587 Register ShiftAmt = RegInfo.createVirtualRegister(RC);
1588 Register Mask = RegInfo.createVirtualRegister(RC);
1589 Register Mask2 = RegInfo.createVirtualRegister(RC);
1590 Register Incr2 = RegInfo.createVirtualRegister(RC);
1591 Register MaskLSB2 = RegInfo.createVirtualRegister(RCp);
1592 Register PtrLSB2 = RegInfo.createVirtualRegister(RC);
1593 Register MaskUpper = RegInfo.createVirtualRegister(RC);
1594 Register Scratch = RegInfo.createVirtualRegister(RC);
1595 Register Scratch2 = RegInfo.createVirtualRegister(RC);
1596 Register Scratch3 = RegInfo.createVirtualRegister(RC);
1597
1598 unsigned AtomicOp = 0;
1599 switch (MI.getOpcode()) {
1600 case Mips::ATOMIC_LOAD_NAND_I8:
1601 AtomicOp = Mips::ATOMIC_LOAD_NAND_I8_POSTRA;
1602 break;
1603 case Mips::ATOMIC_LOAD_NAND_I16:
1604 AtomicOp = Mips::ATOMIC_LOAD_NAND_I16_POSTRA;
1605 break;
1606 case Mips::ATOMIC_SWAP_I8:
1607 AtomicOp = Mips::ATOMIC_SWAP_I8_POSTRA;
1608 break;
1609 case Mips::ATOMIC_SWAP_I16:
1610 AtomicOp = Mips::ATOMIC_SWAP_I16_POSTRA;
1611 break;
1612 case Mips::ATOMIC_LOAD_ADD_I8:
1613 AtomicOp = Mips::ATOMIC_LOAD_ADD_I8_POSTRA;
1614 break;
1615 case Mips::ATOMIC_LOAD_ADD_I16:
1616 AtomicOp = Mips::ATOMIC_LOAD_ADD_I16_POSTRA;
1617 break;
1618 case Mips::ATOMIC_LOAD_SUB_I8:
1619 AtomicOp = Mips::ATOMIC_LOAD_SUB_I8_POSTRA;
1620 break;
1621 case Mips::ATOMIC_LOAD_SUB_I16:
1622 AtomicOp = Mips::ATOMIC_LOAD_SUB_I16_POSTRA;
1623 break;
1624 case Mips::ATOMIC_LOAD_AND_I8:
1625 AtomicOp = Mips::ATOMIC_LOAD_AND_I8_POSTRA;
1626 break;
1627 case Mips::ATOMIC_LOAD_AND_I16:
1628 AtomicOp = Mips::ATOMIC_LOAD_AND_I16_POSTRA;
1629 break;
1630 case Mips::ATOMIC_LOAD_OR_I8:
1631 AtomicOp = Mips::ATOMIC_LOAD_OR_I8_POSTRA;
1632 break;
1633 case Mips::ATOMIC_LOAD_OR_I16:
1634 AtomicOp = Mips::ATOMIC_LOAD_OR_I16_POSTRA;
1635 break;
1636 case Mips::ATOMIC_LOAD_XOR_I8:
1637 AtomicOp = Mips::ATOMIC_LOAD_XOR_I8_POSTRA;
1638 break;
1639 case Mips::ATOMIC_LOAD_XOR_I16:
1640 AtomicOp = Mips::ATOMIC_LOAD_XOR_I16_POSTRA;
1641 break;
1642 default:
1643 llvm_unreachable("Unknown subword atomic pseudo for expansion!")::llvm::llvm_unreachable_internal("Unknown subword atomic pseudo for expansion!"
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 1643)
;
1644 }
1645
1646 // insert new blocks after the current block
1647 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1648 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1649 MachineFunction::iterator It = ++BB->getIterator();
1650 MF->insert(It, exitMBB);
1651
1652 // Transfer the remainder of BB and its successor edges to exitMBB.
1653 exitMBB->splice(exitMBB->begin(), BB,
1654 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1655 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1656
1657 BB->addSuccessor(exitMBB, BranchProbability::getOne());
1658
1659 // thisMBB:
1660 // addiu masklsb2,$0,-4 # 0xfffffffc
1661 // and alignedaddr,ptr,masklsb2
1662 // andi ptrlsb2,ptr,3
1663 // sll shiftamt,ptrlsb2,3
1664 // ori maskupper,$0,255 # 0xff
1665 // sll mask,maskupper,shiftamt
1666 // nor mask2,$0,mask
1667 // sll incr2,incr,shiftamt
1668
1669 int64_t MaskImm = (Size == 1) ? 255 : 65535;
1670 BuildMI(BB, DL, TII->get(ABI.GetPtrAddiuOp()), MaskLSB2)
1671 .addReg(ABI.GetNullPtr()).addImm(-4);
1672 BuildMI(BB, DL, TII->get(ABI.GetPtrAndOp()), AlignedAddr)
1673 .addReg(Ptr).addReg(MaskLSB2);
1674 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2)
1675 .addReg(Ptr, 0, ArePtrs64bit ? Mips::sub_32 : 0).addImm(3);
1676 if (Subtarget.isLittle()) {
1677 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1678 } else {
1679 Register Off = RegInfo.createVirtualRegister(RC);
1680 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1681 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1682 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1683 }
1684 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
1685 .addReg(Mips::ZERO).addImm(MaskImm);
1686 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
1687 .addReg(MaskUpper).addReg(ShiftAmt);
1688 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1689 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
1690
1691
1692 // The purposes of the flags on the scratch registers is explained in
1693 // emitAtomicBinary. In summary, we need a scratch register which is going to
1694 // be undef, that is unique among registers chosen for the instruction.
1695
1696 BuildMI(BB, DL, TII->get(AtomicOp))
1697 .addReg(Dest, RegState::Define | RegState::EarlyClobber)
1698 .addReg(AlignedAddr)
1699 .addReg(Incr2)
1700 .addReg(Mask)
1701 .addReg(Mask2)
1702 .addReg(ShiftAmt)
1703 .addReg(Scratch, RegState::EarlyClobber | RegState::Define |
1704 RegState::Dead | RegState::Implicit)
1705 .addReg(Scratch2, RegState::EarlyClobber | RegState::Define |
1706 RegState::Dead | RegState::Implicit)
1707 .addReg(Scratch3, RegState::EarlyClobber | RegState::Define |
1708 RegState::Dead | RegState::Implicit);
1709
1710 MI.eraseFromParent(); // The instruction is gone now.
1711
1712 return exitMBB;
1713}
1714
1715// Lower atomic compare and swap to a pseudo instruction, taking care to
1716// define a scratch register for the pseudo instruction's expansion. The
1717// instruction is expanded after the register allocator as to prevent
1718// the insertion of stores between the linked load and the store conditional.
1719
1720MachineBasicBlock *
1721MipsTargetLowering::emitAtomicCmpSwap(MachineInstr &MI,
1722 MachineBasicBlock *BB) const {
1723
1724 assert((MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I32 ||(((MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I32 || MI.getOpcode
() == Mips::ATOMIC_CMP_SWAP_I64) && "Unsupported atomic pseudo for EmitAtomicCmpSwap."
) ? static_cast<void> (0) : __assert_fail ("(MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I32 || MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I64) && \"Unsupported atomic pseudo for EmitAtomicCmpSwap.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 1726, __PRETTY_FUNCTION__))
1725 MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I64) &&(((MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I32 || MI.getOpcode
() == Mips::ATOMIC_CMP_SWAP_I64) && "Unsupported atomic pseudo for EmitAtomicCmpSwap."
) ? static_cast<void> (0) : __assert_fail ("(MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I32 || MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I64) && \"Unsupported atomic pseudo for EmitAtomicCmpSwap.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 1726, __PRETTY_FUNCTION__))
1726 "Unsupported atomic pseudo for EmitAtomicCmpSwap.")(((MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I32 || MI.getOpcode
() == Mips::ATOMIC_CMP_SWAP_I64) && "Unsupported atomic pseudo for EmitAtomicCmpSwap."
) ? static_cast<void> (0) : __assert_fail ("(MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I32 || MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I64) && \"Unsupported atomic pseudo for EmitAtomicCmpSwap.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 1726, __PRETTY_FUNCTION__))
;
1727
1728 const unsigned Size = MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I32 ? 4 : 8;
1729
1730 MachineFunction *MF = BB->getParent();
1731 MachineRegisterInfo &MRI = MF->getRegInfo();
1732 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
1733 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1734 DebugLoc DL = MI.getDebugLoc();
1735
1736 unsigned AtomicOp = MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I32
1737 ? Mips::ATOMIC_CMP_SWAP_I32_POSTRA
1738 : Mips::ATOMIC_CMP_SWAP_I64_POSTRA;
1739 Register Dest = MI.getOperand(0).getReg();
1740 Register Ptr = MI.getOperand(1).getReg();
1741 Register OldVal = MI.getOperand(2).getReg();
1742 Register NewVal = MI.getOperand(3).getReg();
1743
1744 Register Scratch = MRI.createVirtualRegister(RC);
1745 MachineBasicBlock::iterator II(MI);
1746
1747 // We need to create copies of the various registers and kill them at the
1748 // atomic pseudo. If the copies are not made, when the atomic is expanded
1749 // after fast register allocation, the spills will end up outside of the
1750 // blocks that their values are defined in, causing livein errors.
1751
1752 Register PtrCopy = MRI.createVirtualRegister(MRI.getRegClass(Ptr));
1753 Register OldValCopy = MRI.createVirtualRegister(MRI.getRegClass(OldVal));
1754 Register NewValCopy = MRI.createVirtualRegister(MRI.getRegClass(NewVal));
1755
1756 BuildMI(*BB, II, DL, TII->get(Mips::COPY), PtrCopy).addReg(Ptr);
1757 BuildMI(*BB, II, DL, TII->get(Mips::COPY), OldValCopy).addReg(OldVal);
1758 BuildMI(*BB, II, DL, TII->get(Mips::COPY), NewValCopy).addReg(NewVal);
1759
1760 // The purposes of the flags on the scratch registers is explained in
1761 // emitAtomicBinary. In summary, we need a scratch register which is going to
1762 // be undef, that is unique among registers chosen for the instruction.
1763
1764 BuildMI(*BB, II, DL, TII->get(AtomicOp))
1765 .addReg(Dest, RegState::Define | RegState::EarlyClobber)
1766 .addReg(PtrCopy, RegState::Kill)
1767 .addReg(OldValCopy, RegState::Kill)
1768 .addReg(NewValCopy, RegState::Kill)
1769 .addReg(Scratch, RegState::EarlyClobber | RegState::Define |
1770 RegState::Dead | RegState::Implicit);
1771
1772 MI.eraseFromParent(); // The instruction is gone now.
1773
1774 return BB;
1775}
1776
1777MachineBasicBlock *MipsTargetLowering::emitAtomicCmpSwapPartword(
1778 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size) const {
1779 assert((Size == 1 || Size == 2) &&(((Size == 1 || Size == 2) && "Unsupported size for EmitAtomicCmpSwapPartial."
) ? static_cast<void> (0) : __assert_fail ("(Size == 1 || Size == 2) && \"Unsupported size for EmitAtomicCmpSwapPartial.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 1780, __PRETTY_FUNCTION__))
1780 "Unsupported size for EmitAtomicCmpSwapPartial.")(((Size == 1 || Size == 2) && "Unsupported size for EmitAtomicCmpSwapPartial."
) ? static_cast<void> (0) : __assert_fail ("(Size == 1 || Size == 2) && \"Unsupported size for EmitAtomicCmpSwapPartial.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 1780, __PRETTY_FUNCTION__))
;
1781
1782 MachineFunction *MF = BB->getParent();
1783 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1784 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1785 const bool ArePtrs64bit = ABI.ArePtrs64bit();
1786 const TargetRegisterClass *RCp =
1787 getRegClassFor(ArePtrs64bit ? MVT::i64 : MVT::i32);
1788 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1789 DebugLoc DL = MI.getDebugLoc();
1790
1791 Register Dest = MI.getOperand(0).getReg();
1792 Register Ptr = MI.getOperand(1).getReg();
1793 Register CmpVal = MI.getOperand(2).getReg();
1794 Register NewVal = MI.getOperand(3).getReg();
1795
1796 Register AlignedAddr = RegInfo.createVirtualRegister(RCp);
1797 Register ShiftAmt = RegInfo.createVirtualRegister(RC);
1798 Register Mask = RegInfo.createVirtualRegister(RC);
1799 Register Mask2 = RegInfo.createVirtualRegister(RC);
1800 Register ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1801 Register ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1802 Register MaskLSB2 = RegInfo.createVirtualRegister(RCp);
1803 Register PtrLSB2 = RegInfo.createVirtualRegister(RC);
1804 Register MaskUpper = RegInfo.createVirtualRegister(RC);
1805 Register MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1806 Register MaskedNewVal = RegInfo.createVirtualRegister(RC);
1807 unsigned AtomicOp = MI.getOpcode() == Mips::ATOMIC_CMP_SWAP_I8
1808 ? Mips::ATOMIC_CMP_SWAP_I8_POSTRA
1809 : Mips::ATOMIC_CMP_SWAP_I16_POSTRA;
1810
1811 // The scratch registers here with the EarlyClobber | Define | Dead | Implicit
1812 // flags are used to coerce the register allocator and the machine verifier to
1813 // accept the usage of these registers.
1814 // The EarlyClobber flag has the semantic properties that the operand it is
1815 // attached to is clobbered before the rest of the inputs are read. Hence it
1816 // must be unique among the operands to the instruction.
1817 // The Define flag is needed to coerce the machine verifier that an Undef
1818 // value isn't a problem.
1819 // The Dead flag is needed as the value in scratch isn't used by any other
1820 // instruction. Kill isn't used as Dead is more precise.
1821 Register Scratch = RegInfo.createVirtualRegister(RC);
1822 Register Scratch2 = RegInfo.createVirtualRegister(RC);
1823
1824 // insert new blocks after the current block
1825 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1826 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1827 MachineFunction::iterator It = ++BB->getIterator();
1828 MF->insert(It, exitMBB);
1829
1830 // Transfer the remainder of BB and its successor edges to exitMBB.
1831 exitMBB->splice(exitMBB->begin(), BB,
1832 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1833 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1834
1835 BB->addSuccessor(exitMBB, BranchProbability::getOne());
1836
1837 // thisMBB:
1838 // addiu masklsb2,$0,-4 # 0xfffffffc
1839 // and alignedaddr,ptr,masklsb2
1840 // andi ptrlsb2,ptr,3
1841 // xori ptrlsb2,ptrlsb2,3 # Only for BE
1842 // sll shiftamt,ptrlsb2,3
1843 // ori maskupper,$0,255 # 0xff
1844 // sll mask,maskupper,shiftamt
1845 // nor mask2,$0,mask
1846 // andi maskedcmpval,cmpval,255
1847 // sll shiftedcmpval,maskedcmpval,shiftamt
1848 // andi maskednewval,newval,255
1849 // sll shiftednewval,maskednewval,shiftamt
1850 int64_t MaskImm = (Size == 1) ? 255 : 65535;
1851 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::DADDiu : Mips::ADDiu), MaskLSB2)
1852 .addReg(ABI.GetNullPtr()).addImm(-4);
1853 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::AND64 : Mips::AND), AlignedAddr)
1854 .addReg(Ptr).addReg(MaskLSB2);
1855 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2)
1856 .addReg(Ptr, 0, ArePtrs64bit ? Mips::sub_32 : 0).addImm(3);
1857 if (Subtarget.isLittle()) {
1858 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1859 } else {
1860 Register Off = RegInfo.createVirtualRegister(RC);
1861 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1862 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1863 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1864 }
1865 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
1866 .addReg(Mips::ZERO).addImm(MaskImm);
1867 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
1868 .addReg(MaskUpper).addReg(ShiftAmt);
1869 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1870 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
1871 .addReg(CmpVal).addImm(MaskImm);
1872 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
1873 .addReg(MaskedCmpVal).addReg(ShiftAmt);
1874 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
1875 .addReg(NewVal).addImm(MaskImm);
1876 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
1877 .addReg(MaskedNewVal).addReg(ShiftAmt);
1878
1879 // The purposes of the flags on the scratch registers are explained in
1880 // emitAtomicBinary. In summary, we need a scratch register which is going to
1881 // be undef, that is unique among the register chosen for the instruction.
1882
1883 BuildMI(BB, DL, TII->get(AtomicOp))
1884 .addReg(Dest, RegState::Define | RegState::EarlyClobber)
1885 .addReg(AlignedAddr)
1886 .addReg(Mask)
1887 .addReg(ShiftedCmpVal)
1888 .addReg(Mask2)
1889 .addReg(ShiftedNewVal)
1890 .addReg(ShiftAmt)
1891 .addReg(Scratch, RegState::EarlyClobber | RegState::Define |
1892 RegState::Dead | RegState::Implicit)
1893 .addReg(Scratch2, RegState::EarlyClobber | RegState::Define |
1894 RegState::Dead | RegState::Implicit);
1895
1896 MI.eraseFromParent(); // The instruction is gone now.
1897
1898 return exitMBB;
1899}
1900
1901SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
1902 // The first operand is the chain, the second is the condition, the third is
1903 // the block to branch to if the condition is true.
1904 SDValue Chain = Op.getOperand(0);
1905 SDValue Dest = Op.getOperand(2);
1906 SDLoc DL(Op);
1907
1908 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6())((!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6(
)) ? static_cast<void> (0) : __assert_fail ("!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6()"
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 1908, __PRETTY_FUNCTION__))
;
1909 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
1910
1911 // Return if flag is not set by a floating point comparison.
1912 if (CondRes.getOpcode() != MipsISD::FPCmp)
1913 return Op;
1914
1915 SDValue CCNode = CondRes.getOperand(2);
1916 Mips::CondCode CC =
1917 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
1918 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1919 SDValue BrCode = DAG.getConstant(Opc, DL, MVT::i32);
1920 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
1921 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
1922 FCC0, Dest, CondRes);
1923}
1924
1925SDValue MipsTargetLowering::
1926lowerSELECT(SDValue Op, SelectionDAG &DAG) const
1927{
1928 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6())((!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6(
)) ? static_cast<void> (0) : __assert_fail ("!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6()"
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 1928, __PRETTY_FUNCTION__))
;
1929 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
1930
1931 // Return if flag is not set by a floating point comparison.
1932 if (Cond.getOpcode() != MipsISD::FPCmp)
1933 return Op;
1934
1935 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1936 SDLoc(Op));
1937}
1938
1939SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1940 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6())((!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6(
)) ? static_cast<void> (0) : __assert_fail ("!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6()"
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 1940, __PRETTY_FUNCTION__))
;
1941 SDValue Cond = createFPCmp(DAG, Op);
1942
1943 assert(Cond.getOpcode() == MipsISD::FPCmp &&((Cond.getOpcode() == MipsISD::FPCmp && "Floating point operand expected."
) ? static_cast<void> (0) : __assert_fail ("Cond.getOpcode() == MipsISD::FPCmp && \"Floating point operand expected.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 1944, __PRETTY_FUNCTION__))
1944 "Floating point operand expected.")((Cond.getOpcode() == MipsISD::FPCmp && "Floating point operand expected."
) ? static_cast<void> (0) : __assert_fail ("Cond.getOpcode() == MipsISD::FPCmp && \"Floating point operand expected.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 1944, __PRETTY_FUNCTION__))
;
1945
1946 SDLoc DL(Op);
1947 SDValue True = DAG.getConstant(1, DL, MVT::i32);
1948 SDValue False = DAG.getConstant(0, DL, MVT::i32);
1949
1950 return createCMovFP(DAG, Cond, True, False, DL);
1951}
1952
1953SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
1954 SelectionDAG &DAG) const {
1955 EVT Ty = Op.getValueType();
1956 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1957 const GlobalValue *GV = N->getGlobal();
1958
1959 if (!isPositionIndependent()) {
1960 const MipsTargetObjectFile *TLOF =
1961 static_cast<const MipsTargetObjectFile *>(
1962 getTargetMachine().getObjFileLowering());
1963 const GlobalObject *GO = GV->getBaseObject();
1964 if (GO && TLOF->IsGlobalInSmallSection(GO, getTargetMachine()))
1965 // %gp_rel relocation
1966 return getAddrGPRel(N, SDLoc(N), Ty, DAG, ABI.IsN64());
1967
1968 // %hi/%lo relocation
1969 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
1970 // %highest/%higher/%hi/%lo relocation
1971 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
1972 }
1973
1974 // Every other architecture would use shouldAssumeDSOLocal in here, but
1975 // mips is special.
1976 // * In PIC code mips requires got loads even for local statics!
1977 // * To save on got entries, for local statics the got entry contains the
1978 // page and an additional add instruction takes care of the low bits.
1979 // * It is legal to access a hidden symbol with a non hidden undefined,
1980 // so one cannot guarantee that all access to a hidden symbol will know
1981 // it is hidden.
1982 // * Mips linkers don't support creating a page and a full got entry for
1983 // the same symbol.
1984 // * Given all that, we have to use a full got entry for hidden symbols :-(
1985 if (GV->hasLocalLinkage())
1986 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
1987
1988 if (Subtarget.useXGOT())
1989 return getAddrGlobalLargeGOT(
1990 N, SDLoc(N), Ty, DAG, MipsII::MO_GOT_HI16, MipsII::MO_GOT_LO16,
1991 DAG.getEntryNode(),
1992 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
1993
1994 return getAddrGlobal(
1995 N, SDLoc(N), Ty, DAG,
1996 (ABI.IsN32() || ABI.IsN64()) ? MipsII::MO_GOT_DISP : MipsII::MO_GOT,
1997 DAG.getEntryNode(), MachinePointerInfo::getGOT(DAG.getMachineFunction()));
1998}
1999
2000SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
2001 SelectionDAG &DAG) const {
2002 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
2003 EVT Ty = Op.getValueType();
2004
2005 if (!isPositionIndependent())
2006 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2007 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
2008
2009 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
2010}
2011
2012SDValue MipsTargetLowering::
2013lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
2014{
2015 // If the relocation model is PIC, use the General Dynamic TLS Model or
2016 // Local Dynamic TLS model, otherwise use the Initial Exec or
2017 // Local Exec TLS Model.
2018
2019 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2020 if (DAG.getTarget().useEmulatedTLS())
2021 return LowerToTLSEmulatedModel(GA, DAG);
2022
2023 SDLoc DL(GA);
2024 const GlobalValue *GV = GA->getGlobal();
2025 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2026
2027 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
2028
2029 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
2030 // General Dynamic and Local Dynamic TLS Model.
2031 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
2032 : MipsII::MO_TLSGD;
2033
2034 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
2035 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
2036 getGlobalReg(DAG, PtrVT), TGA);
2037 unsigned PtrSize = PtrVT.getSizeInBits();
2038 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
2039
2040 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
2041
2042 ArgListTy Args;
2043 ArgListEntry Entry;
2044 Entry.Node = Argument;
2045 Entry.Ty = PtrTy;
2046 Args.push_back(Entry);
2047
2048 TargetLowering::CallLoweringInfo CLI(DAG);
2049 CLI.setDebugLoc(DL)
2050 .setChain(DAG.getEntryNode())
2051 .setLibCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args));
2052 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
2053
2054 SDValue Ret = CallResult.first;
2055
2056 if (model != TLSModel::LocalDynamic)
2057 return Ret;
2058
2059 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2060 MipsII::MO_DTPREL_HI);
2061 SDValue Hi = DAG.getNode(MipsISD::TlsHi, DL, PtrVT, TGAHi);
2062 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2063 MipsII::MO_DTPREL_LO);
2064 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
2065 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
2066 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
2067 }
2068
2069 SDValue Offset;
2070 if (model == TLSModel::InitialExec) {
2071 // Initial Exec TLS Model
2072 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2073 MipsII::MO_GOTTPREL);
2074 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
2075 TGA);
2076 Offset =
2077 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), TGA, MachinePointerInfo());
2078 } else {
2079 // Local Exec TLS Model
2080 assert(model == TLSModel::LocalExec)((model == TLSModel::LocalExec) ? static_cast<void> (0)
: __assert_fail ("model == TLSModel::LocalExec", "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 2080, __PRETTY_FUNCTION__))
;
2081 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2082 MipsII::MO_TPREL_HI);
2083 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2084 MipsII::MO_TPREL_LO);
2085 SDValue Hi = DAG.getNode(MipsISD::TlsHi, DL, PtrVT, TGAHi);
2086 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
2087 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
2088 }
2089
2090 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
2091 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
2092}
2093
2094SDValue MipsTargetLowering::
2095lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
2096{
2097 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
2098 EVT Ty = Op.getValueType();
2099
2100 if (!isPositionIndependent())
2101 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2102 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
2103
2104 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
2105}
2106
2107SDValue MipsTargetLowering::
2108lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
2109{
2110 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
2111 EVT Ty = Op.getValueType();
2112
2113 if (!isPositionIndependent()) {
2114 const MipsTargetObjectFile *TLOF =
2115 static_cast<const MipsTargetObjectFile *>(
2116 getTargetMachine().getObjFileLowering());
2117
2118 if (TLOF->IsConstantInSmallSection(DAG.getDataLayout(), N->getConstVal(),
2119 getTargetMachine()))
2120 // %gp_rel relocation
2121 return getAddrGPRel(N, SDLoc(N), Ty, DAG, ABI.IsN64());
2122
2123 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2124 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
2125 }
2126
2127 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
2128}
2129
2130SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
2131 MachineFunction &MF = DAG.getMachineFunction();
2132 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
2133
2134 SDLoc DL(Op);
2135 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
2136 getPointerTy(MF.getDataLayout()));
2137
2138 // vastart just stores the address of the VarArgsFrameIndex slot into the
2139 // memory location argument.
2140 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
2141 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
2142 MachinePointerInfo(SV));
2143}
2144
2145SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
2146 SDNode *Node = Op.getNode();
2147 EVT VT = Node->getValueType(0);
2148 SDValue Chain = Node->getOperand(0);
2149 SDValue VAListPtr = Node->getOperand(1);
2150 const Align Align =
2151 llvm::MaybeAlign(Node->getConstantOperandVal(3)).valueOrOne();
2152 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2153 SDLoc DL(Node);
2154 unsigned ArgSlotSizeInBytes = (ABI.IsN32() || ABI.IsN64()) ? 8 : 4;
2155
2156 SDValue VAListLoad = DAG.getLoad(getPointerTy(DAG.getDataLayout()), DL, Chain,
2157 VAListPtr, MachinePointerInfo(SV));
2158 SDValue VAList = VAListLoad;
2159
2160 // Re-align the pointer if necessary.
2161 // It should only ever be necessary for 64-bit types on O32 since the minimum
2162 // argument alignment is the same as the maximum type alignment for N32/N64.
2163 //
2164 // FIXME: We currently align too often. The code generator doesn't notice
2165 // when the pointer is still aligned from the last va_arg (or pair of
2166 // va_args for the i64 on O32 case).
2167 if (Align > getMinStackArgumentAlignment()) {
2168 VAList = DAG.getNode(
2169 ISD::ADD, DL, VAList.getValueType(), VAList,
2170 DAG.getConstant(Align.value() - 1, DL, VAList.getValueType()));
2171
2172 VAList = DAG.getNode(
2173 ISD::AND, DL, VAList.getValueType(), VAList,
2174 DAG.getConstant(-(int64_t)Align.value(), DL, VAList.getValueType()));
2175 }
2176
2177 // Increment the pointer, VAList, to the next vaarg.
2178 auto &TD = DAG.getDataLayout();
2179 unsigned ArgSizeInBytes =
2180 TD.getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext()));
2181 SDValue Tmp3 =
2182 DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
2183 DAG.getConstant(alignTo(ArgSizeInBytes, ArgSlotSizeInBytes),
2184 DL, VAList.getValueType()));
2185 // Store the incremented VAList to the legalized pointer
2186 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
2187 MachinePointerInfo(SV));
2188
2189 // In big-endian mode we must adjust the pointer when the load size is smaller
2190 // than the argument slot size. We must also reduce the known alignment to
2191 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
2192 // the correct half of the slot, and reduce the alignment from 8 (slot
2193 // alignment) down to 4 (type alignment).
2194 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
2195 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
2196 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
2197 DAG.getIntPtrConstant(Adjustment, DL));
2198 }
2199 // Load the actual argument out of the pointer VAList
2200 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo());
2201}
2202
2203static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
2204 bool HasExtractInsert) {
2205 EVT TyX = Op.getOperand(0).getValueType();
2206 EVT TyY = Op.getOperand(1).getValueType();
2207 SDLoc DL(Op);
2208 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
2209 SDValue Const31 = DAG.getConstant(31, DL, MVT::i32);
2210 SDValue Res;
2211
2212 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
2213 // to i32.
2214 SDValue X = (TyX == MVT::f32) ?
2215 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
2216 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
2217 Const1);
2218 SDValue Y = (TyY == MVT::f32) ?
2219 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
2220 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
2221 Const1);
2222
2223 if (HasExtractInsert) {
2224 // ext E, Y, 31, 1 ; extract bit31 of Y
2225 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
2226 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
2227 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
2228 } else {
2229 // sll SllX, X, 1
2230 // srl SrlX, SllX, 1
2231 // srl SrlY, Y, 31
2232 // sll SllY, SrlX, 31
2233 // or Or, SrlX, SllY
2234 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
2235 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
2236 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
2237 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
2238 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
2239 }
2240
2241 if (TyX == MVT::f32)
2242 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
2243
2244 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2245 Op.getOperand(0),
2246 DAG.getConstant(0, DL, MVT::i32));
2247 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
2248}
2249
2250static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
2251 bool HasExtractInsert) {
2252 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
2253 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
2254 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
2255 SDLoc DL(Op);
2256 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
2257
2258 // Bitcast to integer nodes.
2259 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
2260 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
2261
2262 if (HasExtractInsert) {
2263 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
2264 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
2265 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
2266 DAG.getConstant(WidthY - 1, DL, MVT::i32), Const1);
2267
2268 if (WidthX > WidthY)
2269 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
2270 else if (WidthY > WidthX)
2271 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
2272
2273 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
2274 DAG.getConstant(WidthX - 1, DL, MVT::i32), Const1,
2275 X);
2276 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
2277 }
2278
2279 // (d)sll SllX, X, 1
2280 // (d)srl SrlX, SllX, 1
2281 // (d)srl SrlY, Y, width(Y)-1
2282 // (d)sll SllY, SrlX, width(Y)-1
2283 // or Or, SrlX, SllY
2284 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
2285 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
2286 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
2287 DAG.getConstant(WidthY - 1, DL, MVT::i32));
2288
2289 if (WidthX > WidthY)
2290 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
2291 else if (WidthY > WidthX)
2292 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
2293
2294 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
2295 DAG.getConstant(WidthX - 1, DL, MVT::i32));
2296 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
2297 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
2298}
2299
2300SDValue
2301MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
2302 if (Subtarget.isGP64bit())
2303 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
2304
2305 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
2306}
2307
2308static SDValue lowerFABS32(SDValue Op, SelectionDAG &DAG,
2309 bool HasExtractInsert) {
2310 SDLoc DL(Op);
2311 SDValue Res, Const1 = DAG.getConstant(1, DL, MVT::i32);
2312
2313 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
2314 // to i32.
2315 SDValue X = (Op.getValueType() == MVT::f32)
2316 ? DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0))
2317 : DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2318 Op.getOperand(0), Const1);
2319
2320 // Clear MSB.
2321 if (HasExtractInsert)
2322 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
2323 DAG.getRegister(Mips::ZERO, MVT::i32),
2324 DAG.getConstant(31, DL, MVT::i32), Const1, X);
2325 else {
2326 // TODO: Provide DAG patterns which transform (and x, cst)
2327 // back to a (shl (srl x (clz cst)) (clz cst)) sequence.
2328 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
2329 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
2330 }
2331
2332 if (Op.getValueType() == MVT::f32)
2333 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
2334
2335 // FIXME: For mips32r2, the sequence of (BuildPairF64 (ins (ExtractElementF64
2336 // Op 1), $zero, 31 1) (ExtractElementF64 Op 0)) and the Op has one use, we
2337 // should be able to drop the usage of mfc1/mtc1 and rewrite the register in
2338 // place.
2339 SDValue LowX =
2340 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
2341 DAG.getConstant(0, DL, MVT::i32));
2342 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
2343}
2344
2345static SDValue lowerFABS64(SDValue Op, SelectionDAG &DAG,
2346 bool HasExtractInsert) {
2347 SDLoc DL(Op);
2348 SDValue Res, Const1 = DAG.getConstant(1, DL, MVT::i32);
2349
2350 // Bitcast to integer node.
2351 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
2352
2353 // Clear MSB.
2354 if (HasExtractInsert)
2355 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
2356 DAG.getRegister(Mips::ZERO_64, MVT::i64),
2357 DAG.getConstant(63, DL, MVT::i32), Const1, X);
2358 else {
2359 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
2360 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
2361 }
2362
2363 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
2364}
2365
2366SDValue MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
2367 if ((ABI.IsN32() || ABI.IsN64()) && (Op.getValueType() == MVT::f64))
2368 return lowerFABS64(Op, DAG, Subtarget.hasExtractInsert());
2369
2370 return lowerFABS32(Op, DAG, Subtarget.hasExtractInsert());
2371}
2372
2373SDValue MipsTargetLowering::
2374lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
2375 // check the depth
2376 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() != 0) {
2377 DAG.getContext()->emitError(
2378 "return address can be determined only for current frame");
2379 return SDValue();
2380 }
2381
2382 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2383 MFI.setFrameAddressIsTaken(true);
2384 EVT VT = Op.getValueType();
2385 SDLoc DL(Op);
2386 SDValue FrameAddr = DAG.getCopyFromReg(
2387 DAG.getEntryNode(), DL, ABI.IsN64() ? Mips::FP_64 : Mips::FP, VT);
2388 return FrameAddr;
2389}
2390
2391SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
2392 SelectionDAG &DAG) const {
2393 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
2394 return SDValue();
2395
2396 // check the depth
2397 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() != 0) {
2398 DAG.getContext()->emitError(
2399 "return address can be determined only for current frame");
2400 return SDValue();
2401 }
2402
2403 MachineFunction &MF = DAG.getMachineFunction();
2404 MachineFrameInfo &MFI = MF.getFrameInfo();
2405 MVT VT = Op.getSimpleValueType();
2406 unsigned RA = ABI.IsN64() ? Mips::RA_64 : Mips::RA;
2407 MFI.setReturnAddressIsTaken(true);
2408
2409 // Return RA, which contains the return address. Mark it an implicit live-in.
2410 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
2411 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
2412}
2413
2414// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
2415// generated from __builtin_eh_return (offset, handler)
2416// The effect of this is to adjust the stack pointer by "offset"
2417// and then branch to "handler".
2418SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
2419 const {
2420 MachineFunction &MF = DAG.getMachineFunction();
2421 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2422
2423 MipsFI->setCallsEhReturn();
2424 SDValue Chain = Op.getOperand(0);
2425 SDValue Offset = Op.getOperand(1);
2426 SDValue Handler = Op.getOperand(2);
2427 SDLoc DL(Op);
2428 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
2429
2430 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
2431 // EH_RETURN nodes, so that instructions are emitted back-to-back.
2432 unsigned OffsetReg = ABI.IsN64() ? Mips::V1_64 : Mips::V1;
2433 unsigned AddrReg = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
2434 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2435 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2436 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2437 DAG.getRegister(OffsetReg, Ty),
2438 DAG.getRegister(AddrReg, getPointerTy(MF.getDataLayout())),
2439 Chain.getValue(1));
2440}
2441
2442SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
2443 SelectionDAG &DAG) const {
2444 // FIXME: Need pseudo-fence for 'singlethread' fences
2445 // FIXME: Set SType for weaker fences where supported/appropriate.
2446 unsigned SType = 0;
2447 SDLoc DL(Op);
2448 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
2449 DAG.getConstant(SType, DL, MVT::i32));
2450}
2451
2452SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
2453 SelectionDAG &DAG) const {
2454 SDLoc DL(Op);
2455 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
2456
2457 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2458 SDValue Shamt = Op.getOperand(2);
2459 // if shamt < (VT.bits):
2460 // lo = (shl lo, shamt)
2461 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2462 // else:
2463 // lo = 0
2464 // hi = (shl lo, shamt[4:0])
2465 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2466 DAG.getConstant(-1, DL, MVT::i32));
2467 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo,
2468 DAG.getConstant(1, DL, VT));
2469 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, Not);
2470 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
2471 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2472 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
2473 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2474 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
2475 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond,
2476 DAG.getConstant(0, DL, VT), ShiftLeftLo);
2477 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftLeftLo, Or);
2478
2479 SDValue Ops[2] = {Lo, Hi};
2480 return DAG.getMergeValues(Ops, DL);
2481}
2482
2483SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
2484 bool IsSRA) const {
2485 SDLoc DL(Op);
2486 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2487 SDValue Shamt = Op.getOperand(2);
2488 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
2489
2490 // if shamt < (VT.bits):
2491 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2492 // if isSRA:
2493 // hi = (sra hi, shamt)
2494 // else:
2495 // hi = (srl hi, shamt)
2496 // else:
2497 // if isSRA:
2498 // lo = (sra hi, shamt[4:0])
2499 // hi = (sra hi, 31)
2500 // else:
2501 // lo = (srl hi, shamt[4:0])
2502 // hi = 0
2503 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2504 DAG.getConstant(-1, DL, MVT::i32));
2505 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, VT, Hi,
2506 DAG.getConstant(1, DL, VT));
2507 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, ShiftLeft1Hi, Not);
2508 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
2509 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2510 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL,
2511 DL, VT, Hi, Shamt);
2512 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2513 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
2514 SDValue Ext = DAG.getNode(ISD::SRA, DL, VT, Hi,
2515 DAG.getConstant(VT.getSizeInBits() - 1, DL, VT));
2516
2517 if (!(Subtarget.hasMips4() || Subtarget.hasMips32())) {
2518 SDVTList VTList = DAG.getVTList(VT, VT);
2519 return DAG.getNode(Subtarget.isGP64bit() ? Mips::PseudoD_SELECT_I64
2520 : Mips::PseudoD_SELECT_I,
2521 DL, VTList, Cond, ShiftRightHi,
2522 IsSRA ? Ext : DAG.getConstant(0, DL, VT), Or,
2523 ShiftRightHi);
2524 }
2525
2526 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftRightHi, Or);
2527 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond,
2528 IsSRA ? Ext : DAG.getConstant(0, DL, VT), ShiftRightHi);
2529
2530 SDValue Ops[2] = {Lo, Hi};
2531 return DAG.getMergeValues(Ops, DL);
2532}
2533
2534static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
2535 SDValue Chain, SDValue Src, unsigned Offset) {
2536 SDValue Ptr = LD->getBasePtr();
2537 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
2538 EVT BasePtrVT = Ptr.getValueType();
2539 SDLoc DL(LD);
2540 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2541
2542 if (Offset)
2543 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
2544 DAG.getConstant(Offset, DL, BasePtrVT));
2545
2546 SDValue Ops[] = { Chain, Ptr, Src };
2547 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
2548 LD->getMemOperand());
2549}
2550
2551// Expand an unaligned 32 or 64-bit integer load node.
2552SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
2553 LoadSDNode *LD = cast<LoadSDNode>(Op);
2554 EVT MemVT = LD->getMemoryVT();
2555
2556 if (Subtarget.systemSupportsUnalignedAccess())
2557 return Op;
2558
2559 // Return if load is aligned or if MemVT is neither i32 nor i64.
2560 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2561 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2562 return SDValue();
2563
2564 bool IsLittle = Subtarget.isLittle();
2565 EVT VT = Op.getValueType();
2566 ISD::LoadExtType ExtType = LD->getExtensionType();
2567 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2568
2569 assert((VT == MVT::i32) || (VT == MVT::i64))(((VT == MVT::i32) || (VT == MVT::i64)) ? static_cast<void
> (0) : __assert_fail ("(VT == MVT::i32) || (VT == MVT::i64)"
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 2569, __PRETTY_FUNCTION__))
;
2570
2571 // Expand
2572 // (set dst, (i64 (load baseptr)))
2573 // to
2574 // (set tmp, (ldl (add baseptr, 7), undef))
2575 // (set dst, (ldr baseptr, tmp))
2576 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
2577 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
2578 IsLittle ? 7 : 0);
2579 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
2580 IsLittle ? 0 : 7);
2581 }
2582
2583 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
2584 IsLittle ? 3 : 0);
2585 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
2586 IsLittle ? 0 : 3);
2587
2588 // Expand
2589 // (set dst, (i32 (load baseptr))) or
2590 // (set dst, (i64 (sextload baseptr))) or
2591 // (set dst, (i64 (extload baseptr)))
2592 // to
2593 // (set tmp, (lwl (add baseptr, 3), undef))
2594 // (set dst, (lwr baseptr, tmp))
2595 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2596 (ExtType == ISD::EXTLOAD))
2597 return LWR;
2598
2599 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD))(((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD)) ? static_cast
<void> (0) : __assert_fail ("(VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD)"
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 2599, __PRETTY_FUNCTION__))
;
2600
2601 // Expand
2602 // (set dst, (i64 (zextload baseptr)))
2603 // to
2604 // (set tmp0, (lwl (add baseptr, 3), undef))
2605 // (set tmp1, (lwr baseptr, tmp0))
2606 // (set tmp2, (shl tmp1, 32))
2607 // (set dst, (srl tmp2, 32))
2608 SDLoc DL(LD);
2609 SDValue Const32 = DAG.getConstant(32, DL, MVT::i32);
2610 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
2611 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2612 SDValue Ops[] = { SRL, LWR.getValue(1) };
2613 return DAG.getMergeValues(Ops, DL);
2614}
2615
2616static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
2617 SDValue Chain, unsigned Offset) {
2618 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2619 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
2620 SDLoc DL(SD);
2621 SDVTList VTList = DAG.getVTList(MVT::Other);
2622
2623 if (Offset)
2624 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
2625 DAG.getConstant(Offset, DL, BasePtrVT));
2626
2627 SDValue Ops[] = { Chain, Value, Ptr };
2628 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
2629 SD->getMemOperand());
2630}
2631
2632// Expand an unaligned 32 or 64-bit integer store node.
2633static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2634 bool IsLittle) {
2635 SDValue Value = SD->getValue(), Chain = SD->getChain();
2636 EVT VT = Value.getValueType();
2637
2638 // Expand
2639 // (store val, baseptr) or
2640 // (truncstore val, baseptr)
2641 // to
2642 // (swl val, (add baseptr, 3))
2643 // (swr val, baseptr)
2644 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
2645 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
2646 IsLittle ? 3 : 0);
2647 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
2648 }
2649
2650 assert(VT == MVT::i64)((VT == MVT::i64) ? static_cast<void> (0) : __assert_fail
("VT == MVT::i64", "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 2650, __PRETTY_FUNCTION__))
;
2651
2652 // Expand
2653 // (store val, baseptr)
2654 // to
2655 // (sdl val, (add baseptr, 7))
2656 // (sdr val, baseptr)
2657 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2658 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
2659}
2660
2661// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2662static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG,
2663 bool SingleFloat) {
2664 SDValue Val = SD->getValue();
2665
2666 if (Val.getOpcode() != ISD::FP_TO_SINT ||
2667 (Val.getValueSizeInBits() > 32 && SingleFloat))
2668 return SDValue();
2669
2670 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
2671 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
2672 Val.getOperand(0));
2673 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
2674 SD->getPointerInfo(), SD->getAlignment(),
2675 SD->getMemOperand()->getFlags());
2676}
2677
2678SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2679 StoreSDNode *SD = cast<StoreSDNode>(Op);
2680 EVT MemVT = SD->getMemoryVT();
2681
2682 // Lower unaligned integer stores.
2683 if (!Subtarget.systemSupportsUnalignedAccess() &&
2684 (SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2685 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2686 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
2687
2688 return lowerFP_TO_SINT_STORE(SD, DAG, Subtarget.isSingleFloat());
2689}
2690
2691SDValue MipsTargetLowering::lowerEH_DWARF_CFA(SDValue Op,
2692 SelectionDAG &DAG) const {
2693
2694 // Return a fixed StackObject with offset 0 which points to the old stack
2695 // pointer.
2696 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2697 EVT ValTy = Op->getValueType(0);
2698 int FI = MFI.CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2699 return DAG.getFrameIndex(FI, ValTy);
2700}
2701
2702SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2703 SelectionDAG &DAG) const {
2704 if (Op.getValueSizeInBits() > 32 && Subtarget.isSingleFloat())
2705 return SDValue();
2706
2707 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
2708 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
2709 Op.getOperand(0));
2710 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
2711}
2712
2713//===----------------------------------------------------------------------===//
2714// Calling Convention Implementation
2715//===----------------------------------------------------------------------===//
2716
2717//===----------------------------------------------------------------------===//
2718// TODO: Implement a generic logic using tblgen that can support this.
2719// Mips O32 ABI rules:
2720// ---
2721// i32 - Passed in A0, A1, A2, A3 and stack
2722// f32 - Only passed in f32 registers if no int reg has been used yet to hold
2723// an argument. Otherwise, passed in A1, A2, A3 and stack.
2724// f64 - Only passed in two aliased f32 registers if no int reg has been used
2725// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
2726// not used, it must be shadowed. If only A3 is available, shadow it and
2727// go to stack.
2728// vXiX - Received as scalarized i32s, passed in A0 - A3 and the stack.
2729// vXf32 - Passed in either a pair of registers {A0, A1}, {A2, A3} or {A0 - A3}
2730// with the remainder spilled to the stack.
2731// vXf64 - Passed in either {A0, A1, A2, A3} or {A2, A3} and in both cases
2732// spilling the remainder to the stack.
2733//
2734// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
2735//===----------------------------------------------------------------------===//
2736
2737static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2738 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2739 CCState &State, ArrayRef<MCPhysReg> F64Regs) {
2740 const MipsSubtarget &Subtarget = static_cast<const MipsSubtarget &>(
2741 State.getMachineFunction().getSubtarget());
2742
2743 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2744
2745 const MipsCCState * MipsState = static_cast<MipsCCState *>(&State);
2746
2747 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
2748
2749 static const MCPhysReg FloatVectorIntRegs[] = { Mips::A0, Mips::A2 };
2750
2751 // Do not process byval args here.
2752 if (ArgFlags.isByVal())
2753 return true;
2754
2755 // Promote i8 and i16
2756 if (ArgFlags.isInReg() && !Subtarget.isLittle()) {
2757 if (LocVT == MVT::i8 || LocVT == MVT::i16 || LocVT == MVT::i32) {
2758 LocVT = MVT::i32;
2759 if (ArgFlags.isSExt())
2760 LocInfo = CCValAssign::SExtUpper;
2761 else if (ArgFlags.isZExt())
2762 LocInfo = CCValAssign::ZExtUpper;
2763 else
2764 LocInfo = CCValAssign::AExtUpper;
2765 }
2766 }
2767
2768 // Promote i8 and i16
2769 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2770 LocVT = MVT::i32;
2771 if (ArgFlags.isSExt())
2772 LocInfo = CCValAssign::SExt;
2773 else if (ArgFlags.isZExt())
2774 LocInfo = CCValAssign::ZExt;
2775 else
2776 LocInfo = CCValAssign::AExt;
2777 }
2778
2779 unsigned Reg;
2780
2781 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2782 // is true: function is vararg, argument is 3rd or higher, there is previous
2783 // argument which is not f32 or f64.
2784 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1 ||
2785 State.getFirstUnallocated(F32Regs) != ValNo;
2786 unsigned OrigAlign = ArgFlags.getOrigAlign();
2787 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
2788 bool isVectorFloat = MipsState->WasOriginalArgVectorFloat(ValNo);
2789
2790 // The MIPS vector ABI for floats passes them in a pair of registers
2791 if (ValVT == MVT::i32 && isVectorFloat) {
2792 // This is the start of an vector that was scalarized into an unknown number
2793 // of components. It doesn't matter how many there are. Allocate one of the
2794 // notional 8 byte aligned registers which map onto the argument stack, and
2795 // shadow the register lost to alignment requirements.
2796 if (ArgFlags.isSplit()) {
2797 Reg = State.AllocateReg(FloatVectorIntRegs);
2798 if (Reg == Mips::A2)
2799 State.AllocateReg(Mips::A1);
2800 else if (Reg == 0)
2801 State.AllocateReg(Mips::A3);
2802 } else {
2803 // If we're an intermediate component of the split, we can just attempt to
2804 // allocate a register directly.
2805 Reg = State.AllocateReg(IntRegs);
2806 }
2807 } else if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
2808 Reg = State.AllocateReg(IntRegs);
2809 // If this is the first part of an i64 arg,
2810 // the allocated register must be either A0 or A2.
2811 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2812 Reg = State.AllocateReg(IntRegs);
2813 LocVT = MVT::i32;
2814 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2815 // Allocate int register and shadow next int register. If first
2816 // available register is Mips::A1 or Mips::A3, shadow it too.
2817 Reg = State.AllocateReg(IntRegs);
2818 if (Reg == Mips::A1 || Reg == Mips::A3)
2819 Reg = State.AllocateReg(IntRegs);
2820 State.AllocateReg(IntRegs);
2821 LocVT = MVT::i32;
2822 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2823 // we are guaranteed to find an available float register
2824 if (ValVT == MVT::f32) {
2825 Reg = State.AllocateReg(F32Regs);
2826 // Shadow int register
2827 State.AllocateReg(IntRegs);
2828 } else {
2829 Reg = State.AllocateReg(F64Regs);
2830 // Shadow int registers
2831 unsigned Reg2 = State.AllocateReg(IntRegs);
2832 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2833 State.AllocateReg(IntRegs);
2834 State.AllocateReg(IntRegs);
2835 }
2836 } else
2837 llvm_unreachable("Cannot handle this ValVT.")::llvm::llvm_unreachable_internal("Cannot handle this ValVT."
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 2837)
;
2838
2839 if (!Reg) {
2840 unsigned Offset = State.AllocateStack(ValVT.getStoreSize(), OrigAlign);
2841 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
2842 } else
2843 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
2844
2845 return false;
2846}
2847
2848static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2849 MVT LocVT, CCValAssign::LocInfo LocInfo,
2850 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2851 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
2852
2853 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2854}
2855
2856static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2857 MVT LocVT, CCValAssign::LocInfo LocInfo,
2858 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2859 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
2860
2861 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2862}
2863
2864static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2865 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2866 CCState &State) LLVM_ATTRIBUTE_UNUSED__attribute__((__unused__));
2867
2868#include "MipsGenCallingConv.inc"
2869
2870 CCAssignFn *MipsTargetLowering::CCAssignFnForCall() const{
2871 return CC_Mips_FixedArg;
2872 }
2873
2874 CCAssignFn *MipsTargetLowering::CCAssignFnForReturn() const{
2875 return RetCC_Mips;
2876 }
2877//===----------------------------------------------------------------------===//
2878// Call Calling Convention Implementation
2879//===----------------------------------------------------------------------===//
2880
2881// Return next O32 integer argument register.
2882static unsigned getNextIntArgReg(unsigned Reg) {
2883 assert((Reg == Mips::A0) || (Reg == Mips::A2))(((Reg == Mips::A0) || (Reg == Mips::A2)) ? static_cast<void
> (0) : __assert_fail ("(Reg == Mips::A0) || (Reg == Mips::A2)"
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 2883, __PRETTY_FUNCTION__))
;
2884 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2885}
2886
2887SDValue MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
2888 SDValue Chain, SDValue Arg,
2889 const SDLoc &DL, bool IsTailCall,
2890 SelectionDAG &DAG) const {
2891 if (!IsTailCall) {
2892 SDValue PtrOff =
2893 DAG.getNode(ISD::ADD, DL, getPointerTy(DAG.getDataLayout()), StackPtr,
2894 DAG.getIntPtrConstant(Offset, DL));
2895 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo());
2896 }
2897
2898 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2899 int FI = MFI.CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2900 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
2901 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2902 /* Alignment = */ 0, MachineMemOperand::MOVolatile);
2903}
2904
2905void MipsTargetLowering::
2906getOpndList(SmallVectorImpl<SDValue> &Ops,
2907 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
2908 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2909 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
2910 SDValue Chain) const {
2911 // Insert node "GP copy globalreg" before call to function.
2912 //
2913 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2914 // in PIC mode) allow symbols to be resolved via lazy binding.
2915 // The lazy binding stub requires GP to point to the GOT.
2916 // Note that we don't need GP to point to the GOT for indirect calls
2917 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
2918 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
2919 // used for the function (that is, Mips linker doesn't generate lazy binding
2920 // stub for a function whose address is taken in the program).
2921 if (IsPICCall && !InternalLinkage && IsCallReloc) {
2922 unsigned GPReg = ABI.IsN64() ? Mips::GP_64 : Mips::GP;
2923 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
2924 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2925 }
2926
2927 // Build a sequence of copy-to-reg nodes chained together with token
2928 // chain and flag operands which copy the outgoing args into registers.
2929 // The InFlag in necessary since all emitted instructions must be
2930 // stuck together.
2931 SDValue InFlag;
2932
2933 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2934 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2935 RegsToPass[i].second, InFlag);
2936 InFlag = Chain.getValue(1);
2937 }
2938
2939 // Add argument registers to the end of the list so that they are
2940 // known live into the call.
2941 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2942 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2943 RegsToPass[i].second.getValueType()));
2944
2945 // Add a register mask operand representing the call-preserved registers.
2946 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
2947 const uint32_t *Mask =
2948 TRI->getCallPreservedMask(CLI.DAG.getMachineFunction(), CLI.CallConv);
2949 assert(Mask && "Missing call preserved mask for calling convention")((Mask && "Missing call preserved mask for calling convention"
) ? static_cast<void> (0) : __assert_fail ("Mask && \"Missing call preserved mask for calling convention\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 2949, __PRETTY_FUNCTION__))
;
2950 if (Subtarget.inMips16HardFloat()) {
2951 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2952 StringRef Sym = G->getGlobal()->getName();
2953 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
2954 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
2955 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2956 }
2957 }
2958 }
2959 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2960
2961 if (InFlag.getNode())
2962 Ops.push_back(InFlag);
2963}
2964
2965void MipsTargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
2966 SDNode *Node) const {
2967 switch (MI.getOpcode()) {
2968 default:
2969 return;
2970 case Mips::JALR:
2971 case Mips::JALRPseudo:
2972 case Mips::JALR64:
2973 case Mips::JALR64Pseudo:
2974 case Mips::JALR16_MM:
2975 case Mips::JALRC16_MMR6:
2976 case Mips::TAILCALLREG:
2977 case Mips::TAILCALLREG64:
2978 case Mips::TAILCALLR6REG:
2979 case Mips::TAILCALL64R6REG:
2980 case Mips::TAILCALLREG_MM:
2981 case Mips::TAILCALLREG_MMR6: {
2982 if (!EmitJalrReloc ||
2983 Subtarget.inMips16Mode() ||
2984 !isPositionIndependent() ||
2985 Node->getNumOperands() < 1 ||
2986 Node->getOperand(0).getNumOperands() < 2) {
2987 return;
2988 }
2989 // We are after the callee address, set by LowerCall().
2990 // If added to MI, asm printer will emit .reloc R_MIPS_JALR for the
2991 // symbol.
2992 const SDValue TargetAddr = Node->getOperand(0).getOperand(1);
2993 StringRef Sym;
2994 if (const GlobalAddressSDNode *G =
2995 dyn_cast_or_null<const GlobalAddressSDNode>(TargetAddr)) {
2996 Sym = G->getGlobal()->getName();
2997 }
2998 else if (const ExternalSymbolSDNode *ES =
2999 dyn_cast_or_null<const ExternalSymbolSDNode>(TargetAddr)) {
3000 Sym = ES->getSymbol();
3001 }
3002
3003 if (Sym.empty())
3004 return;
3005
3006 MachineFunction *MF = MI.getParent()->getParent();
3007 MCSymbol *S = MF->getContext().getOrCreateSymbol(Sym);
3008 MI.addOperand(MachineOperand::CreateMCSymbol(S, MipsII::MO_JALR));
3009 }
3010 }
3011}
3012
3013/// LowerCall - functions arguments are copied from virtual regs to
3014/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
3015SDValue
3016MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
3017 SmallVectorImpl<SDValue> &InVals) const {
3018 SelectionDAG &DAG = CLI.DAG;
3019 SDLoc DL = CLI.DL;
3020 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
3021 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
3022 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
3023 SDValue Chain = CLI.Chain;
3024 SDValue Callee = CLI.Callee;
1
Value assigned to 'Callee.Node'
3025 bool &IsTailCall = CLI.IsTailCall;
3026 CallingConv::ID CallConv = CLI.CallConv;
3027 bool IsVarArg = CLI.IsVarArg;
3028
3029 MachineFunction &MF = DAG.getMachineFunction();
3030 MachineFrameInfo &MFI = MF.getFrameInfo();
3031 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
3032 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
3033 bool IsPIC = isPositionIndependent();
3034
3035 // Analyze operands of the call, assigning locations to each operand.
3036 SmallVector<CCValAssign, 16> ArgLocs;
3037 MipsCCState CCInfo(
3038 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(),
3039 MipsCCState::getSpecialCallingConvForCallee(Callee.getNode(), Subtarget));
3040
3041 const ExternalSymbolSDNode *ES =
3042 dyn_cast_or_null<const ExternalSymbolSDNode>(Callee.getNode());
2
Assuming null pointer is passed into cast
3
Assuming pointer value is null
3043
3044 // There is one case where CALLSEQ_START..CALLSEQ_END can be nested, which
3045 // is during the lowering of a call with a byval argument which produces
3046 // a call to memcpy. For the O32 case, this causes the caller to allocate
3047 // stack space for the reserved argument area for the callee, then recursively
3048 // again for the memcpy call. In the NEWABI case, this doesn't occur as those
3049 // ABIs mandate that the callee allocates the reserved argument area. We do
3050 // still produce nested CALLSEQ_START..CALLSEQ_END with zero space though.
3051 //
3052 // If the callee has a byval argument and memcpy is used, we are mandated
3053 // to already have produced a reserved argument area for the callee for O32.
3054 // Therefore, the reserved argument area can be reused for both calls.
3055 //
3056 // Other cases of calling memcpy cannot have a chain with a CALLSEQ_START
3057 // present, as we have yet to hook that node onto the chain.
3058 //
3059 // Hence, the CALLSEQ_START and CALLSEQ_END nodes can be eliminated in this
3060 // case. GCC does a similar trick, in that wherever possible, it calculates
3061 // the maximum out going argument area (including the reserved area), and
3062 // preallocates the stack space on entrance to the caller.
3063 //
3064 // FIXME: We should do the same for efficiency and space.
3065
3066 // Note: The check on the calling convention below must match
3067 // MipsABIInfo::GetCalleeAllocdArgSizeInBytes().
3068 bool MemcpyInByVal = ES
3.1
'ES' is null
3.1
'ES' is null
&&
3069 StringRef(ES->getSymbol()) == StringRef("memcpy") &&
3070 CallConv != CallingConv::Fast &&
3071 Chain.getOpcode() == ISD::CALLSEQ_START;
3072
3073 // Allocate the reserved argument area. It seems strange to do this from the
3074 // caller side but removing it breaks the frame size calculation.
3075 unsigned ReservedArgArea =
3076 MemcpyInByVal
3.2
'MemcpyInByVal' is false
3.2
'MemcpyInByVal' is false
? 0 : ABI.GetCalleeAllocdArgSizeInBytes(CallConv);
4
'?' condition is false
3077 CCInfo.AllocateStack(ReservedArgArea, 1);
3078
3079 CCInfo.AnalyzeCallOperands(Outs, CC_Mips, CLI.getArgs(),
3080 ES
4.1
'ES' is null
4.1
'ES' is null
? ES->getSymbol() : nullptr);
5
'?' condition is false
3081
3082 // Get a count of how many bytes are to be pushed on the stack.
3083 unsigned NextStackOffset = CCInfo.getNextStackOffset();
3084
3085 // Check if it's really possible to do a tail call. Restrict it to functions
3086 // that are part of this compilation unit.
3087 bool InternalLinkage = false;
3088 if (IsTailCall) {
6
Assuming 'IsTailCall' is false
7
Taking false branch
3089 IsTailCall = isEligibleForTailCallOptimization(
3090 CCInfo, NextStackOffset, *MF.getInfo<MipsFunctionInfo>());
3091 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3092 InternalLinkage = G->getGlobal()->hasInternalLinkage();
3093 IsTailCall &= (InternalLinkage || G->getGlobal()->hasLocalLinkage() ||
3094 G->getGlobal()->hasPrivateLinkage() ||
3095 G->getGlobal()->hasHiddenVisibility() ||
3096 G->getGlobal()->hasProtectedVisibility());
3097 }
3098 }
3099 if (!IsTailCall
7.1
'IsTailCall' is false
7.1
'IsTailCall' is false
&& CLI.CS && CLI.CS.isMustTailCall())
8
Assuming the condition is false
3100 report_fatal_error("failed to perform tail call elimination on a call "
3101 "site marked musttail");
3102
3103 if (IsTailCall
8.1
'IsTailCall' is false
8.1
'IsTailCall' is false
)
9
Taking false branch
3104 ++NumTailCalls;
3105
3106 // Chain is the output chain of the last Load/Store or CopyToReg node.
3107 // ByValChain is the output chain of the last Memcpy node created for copying
3108 // byval arguments to the stack.
3109 unsigned StackAlignment = TFL->getStackAlignment();
3110 NextStackOffset = alignTo(NextStackOffset, StackAlignment);
3111 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, DL, true);
3112
3113 if (!(IsTailCall
9.1
'IsTailCall' is false
9.1
'IsTailCall' is false
|| MemcpyInByVal))
10
Taking true branch
3114 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffset, 0, DL);
3115
3116 SDValue StackPtr =
3117 DAG.getCopyFromReg(Chain, DL, ABI.IsN64() ? Mips::SP_64 : Mips::SP,
11
'?' condition is false
3118 getPointerTy(DAG.getDataLayout()));
3119
3120 std::deque<std::pair<unsigned, SDValue>> RegsToPass;
3121 SmallVector<SDValue, 8> MemOpChains;
3122
3123 CCInfo.rewindByValRegsInfo();
3124
3125 // Walk the register/memloc assignments, inserting copies/loads.
3126 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
12
Assuming 'i' is equal to 'e'
13
Loop condition is false. Execution continues on line 3225
3127 SDValue Arg = OutVals[i];
3128 CCValAssign &VA = ArgLocs[i];
3129 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
3130 ISD::ArgFlagsTy Flags = Outs[i].Flags;
3131 bool UseUpperBits = false;
3132
3133 // ByVal Arg.
3134 if (Flags.isByVal()) {
3135 unsigned FirstByValReg, LastByValReg;
3136 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
3137 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
3138
3139 assert(Flags.getByValSize() &&((Flags.getByValSize() && "ByVal args of size 0 should have been ignored by front-end."
) ? static_cast<void> (0) : __assert_fail ("Flags.getByValSize() && \"ByVal args of size 0 should have been ignored by front-end.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 3140, __PRETTY_FUNCTION__))
3140 "ByVal args of size 0 should have been ignored by front-end.")((Flags.getByValSize() && "ByVal args of size 0 should have been ignored by front-end."
) ? static_cast<void> (0) : __assert_fail ("Flags.getByValSize() && \"ByVal args of size 0 should have been ignored by front-end.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 3140, __PRETTY_FUNCTION__))
;
3141 assert(ByValIdx < CCInfo.getInRegsParamsCount())((ByValIdx < CCInfo.getInRegsParamsCount()) ? static_cast<
void> (0) : __assert_fail ("ByValIdx < CCInfo.getInRegsParamsCount()"
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 3141, __PRETTY_FUNCTION__))
;
3142 assert(!IsTailCall &&((!IsTailCall && "Do not tail-call optimize if there is a byval argument."
) ? static_cast<void> (0) : __assert_fail ("!IsTailCall && \"Do not tail-call optimize if there is a byval argument.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 3143, __PRETTY_FUNCTION__))
3143 "Do not tail-call optimize if there is a byval argument.")((!IsTailCall && "Do not tail-call optimize if there is a byval argument."
) ? static_cast<void> (0) : __assert_fail ("!IsTailCall && \"Do not tail-call optimize if there is a byval argument.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 3143, __PRETTY_FUNCTION__))
;
3144 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
3145 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(),
3146 VA);
3147 CCInfo.nextInRegsParam();
3148 continue;
3149 }
3150
3151 // Promote the value if needed.
3152 switch (VA.getLocInfo()) {
3153 default:
3154 llvm_unreachable("Unknown loc info!")::llvm::llvm_unreachable_internal("Unknown loc info!", "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 3154)
;
3155 case CCValAssign::Full:
3156 if (VA.isRegLoc()) {
3157 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
3158 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
3159 (ValVT == MVT::i64 && LocVT == MVT::f64))
3160 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
3161 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
3162 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
3163 Arg, DAG.getConstant(0, DL, MVT::i32));
3164 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
3165 Arg, DAG.getConstant(1, DL, MVT::i32));
3166 if (!Subtarget.isLittle())
3167 std::swap(Lo, Hi);
3168 Register LocRegLo = VA.getLocReg();
3169 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
3170 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
3171 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
3172 continue;
3173 }
3174 }
3175 break;
3176 case CCValAssign::BCvt:
3177 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
3178 break;
3179 case CCValAssign::SExtUpper:
3180 UseUpperBits = true;
3181 LLVM_FALLTHROUGH[[gnu::fallthrough]];
3182 case CCValAssign::SExt:
3183 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
3184 break;
3185 case CCValAssign::ZExtUpper:
3186 UseUpperBits = true;
3187 LLVM_FALLTHROUGH[[gnu::fallthrough]];
3188 case CCValAssign::ZExt:
3189 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
3190 break;
3191 case CCValAssign::AExtUpper:
3192 UseUpperBits = true;
3193 LLVM_FALLTHROUGH[[gnu::fallthrough]];
3194 case CCValAssign::AExt:
3195 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
3196 break;
3197 }
3198
3199 if (UseUpperBits) {
3200 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3201 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3202 Arg = DAG.getNode(
3203 ISD::SHL, DL, VA.getLocVT(), Arg,
3204 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
3205 }
3206
3207 // Arguments that can be passed on register must be kept at
3208 // RegsToPass vector
3209 if (VA.isRegLoc()) {
3210 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3211 continue;
3212 }
3213
3214 // Register can't get to this point...
3215 assert(VA.isMemLoc())((VA.isMemLoc()) ? static_cast<void> (0) : __assert_fail
("VA.isMemLoc()", "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 3215, __PRETTY_FUNCTION__))
;
3216
3217 // emit ISD::STORE whichs stores the
3218 // parameter value to a stack Location
3219 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
3220 Chain, Arg, DL, IsTailCall, DAG));
3221 }
3222
3223 // Transform all store nodes into one single node because all store
3224 // nodes are independent of each other.
3225 if (!MemOpChains.empty())
14
Taking false branch
3226 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
3227
3228 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
3229 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
3230 // node so that legalize doesn't hack it.
3231
3232 EVT Ty = Callee.getValueType();
15
Calling 'SDValue::getValueType'
3233 bool GlobalOrExternal = false, IsCallReloc = false;
3234
3235 // The long-calls feature is ignored in case of PIC.
3236 // While we do not support -mshared / -mno-shared properly,
3237 // ignore long-calls in case of -mabicalls too.
3238 if (!Subtarget.isABICalls() && !IsPIC) {
3239 // If the function should be called using "long call",
3240 // get its address into a register to prevent using
3241 // of the `jal` instruction for the direct call.
3242 if (auto *N = dyn_cast<ExternalSymbolSDNode>(Callee)) {
3243 if (Subtarget.useLongCalls())
3244 Callee = Subtarget.hasSym32()
3245 ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
3246 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
3247 } else if (auto *N = dyn_cast<GlobalAddressSDNode>(Callee)) {
3248 bool UseLongCalls = Subtarget.useLongCalls();
3249 // If the function has long-call/far/near attribute
3250 // it overrides command line switch pased to the backend.
3251 if (auto *F = dyn_cast<Function>(N->getGlobal())) {
3252 if (F->hasFnAttribute("long-call"))
3253 UseLongCalls = true;
3254 else if (F->hasFnAttribute("short-call"))
3255 UseLongCalls = false;
3256 }
3257 if (UseLongCalls)
3258 Callee = Subtarget.hasSym32()
3259 ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
3260 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
3261 }
3262 }
3263
3264 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3265 if (IsPIC) {
3266 const GlobalValue *Val = G->getGlobal();
3267 InternalLinkage = Val->hasInternalLinkage();
3268
3269 if (InternalLinkage)
3270 Callee = getAddrLocal(G, DL, Ty, DAG, ABI.IsN32() || ABI.IsN64());
3271 else if (Subtarget.useXGOT()) {
3272 Callee = getAddrGlobalLargeGOT(G, DL, Ty, DAG, MipsII::MO_CALL_HI16,
3273 MipsII::MO_CALL_LO16, Chain,
3274 FuncInfo->callPtrInfo(Val));
3275 IsCallReloc = true;
3276 } else {
3277 Callee = getAddrGlobal(G, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
3278 FuncInfo->callPtrInfo(Val));
3279 IsCallReloc = true;
3280 }
3281 } else
3282 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL,
3283 getPointerTy(DAG.getDataLayout()), 0,
3284 MipsII::MO_NO_FLAG);
3285 GlobalOrExternal = true;
3286 }
3287 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
3288 const char *Sym = S->getSymbol();
3289
3290 if (!IsPIC) // static
3291 Callee = DAG.getTargetExternalSymbol(
3292 Sym, getPointerTy(DAG.getDataLayout()), MipsII::MO_NO_FLAG);
3293 else if (Subtarget.useXGOT()) {
3294 Callee = getAddrGlobalLargeGOT(S, DL, Ty, DAG, MipsII::MO_CALL_HI16,
3295 MipsII::MO_CALL_LO16, Chain,
3296 FuncInfo->callPtrInfo(Sym));
3297 IsCallReloc = true;
3298 } else { // PIC
3299 Callee = getAddrGlobal(S, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
3300 FuncInfo->callPtrInfo(Sym));
3301 IsCallReloc = true;
3302 }
3303
3304 GlobalOrExternal = true;
3305 }
3306
3307 SmallVector<SDValue, 8> Ops(1, Chain);
3308 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
3309
3310 getOpndList(Ops, RegsToPass, IsPIC, GlobalOrExternal, InternalLinkage,
3311 IsCallReloc, CLI, Callee, Chain);
3312
3313 if (IsTailCall) {
3314 MF.getFrameInfo().setHasTailCall();
3315 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
3316 }
3317
3318 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
3319 SDValue InFlag = Chain.getValue(1);
3320
3321 // Create the CALLSEQ_END node in the case of where it is not a call to
3322 // memcpy.
3323 if (!(MemcpyInByVal)) {
3324 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
3325 DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
3326 InFlag = Chain.getValue(1);
3327 }
3328
3329 // Handle result values, copying them out of physregs into vregs that we
3330 // return.
3331 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
3332 InVals, CLI);
3333}
3334
3335/// LowerCallResult - Lower the result values of a call into the
3336/// appropriate copies out of appropriate physical registers.
3337SDValue MipsTargetLowering::LowerCallResult(
3338 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
3339 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
3340 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
3341 TargetLowering::CallLoweringInfo &CLI) const {
3342 // Assign locations to each value returned by this call.
3343 SmallVector<CCValAssign, 16> RVLocs;
3344 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
3345 *DAG.getContext());
3346
3347 const ExternalSymbolSDNode *ES =
3348 dyn_cast_or_null<const ExternalSymbolSDNode>(CLI.Callee.getNode());
3349 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips, CLI.RetTy,
3350 ES ? ES->getSymbol() : nullptr);
3351
3352 // Copy all of the result registers out of their specified physreg.
3353 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3354 CCValAssign &VA = RVLocs[i];
3355 assert(VA.isRegLoc() && "Can only return in registers!")((VA.isRegLoc() && "Can only return in registers!") ?
static_cast<void> (0) : __assert_fail ("VA.isRegLoc() && \"Can only return in registers!\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 3355, __PRETTY_FUNCTION__))
;
3356
3357 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
3358 RVLocs[i].getLocVT(), InFlag);
3359 Chain = Val.getValue(1);
3360 InFlag = Val.getValue(2);
3361
3362 if (VA.isUpperBitsInLoc()) {
3363 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
3364 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3365 unsigned Shift =
3366 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
3367 Val = DAG.getNode(
3368 Shift, DL, VA.getLocVT(), Val,
3369 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
3370 }
3371
3372 switch (VA.getLocInfo()) {
3373 default:
3374 llvm_unreachable("Unknown loc info!")::llvm::llvm_unreachable_internal("Unknown loc info!", "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 3374)
;
3375 case CCValAssign::Full:
3376 break;
3377 case CCValAssign::BCvt:
3378 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
3379 break;
3380 case CCValAssign::AExt:
3381 case CCValAssign::AExtUpper:
3382 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3383 break;
3384 case CCValAssign::ZExt:
3385 case CCValAssign::ZExtUpper:
3386 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
3387 DAG.getValueType(VA.getValVT()));
3388 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3389 break;
3390 case CCValAssign::SExt:
3391 case CCValAssign::SExtUpper:
3392 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
3393 DAG.getValueType(VA.getValVT()));
3394 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3395 break;
3396 }
3397
3398 InVals.push_back(Val);
3399 }
3400
3401 return Chain;
3402}
3403
3404static SDValue UnpackFromArgumentSlot(SDValue Val, const CCValAssign &VA,
3405 EVT ArgVT, const SDLoc &DL,
3406 SelectionDAG &DAG) {
3407 MVT LocVT = VA.getLocVT();
3408 EVT ValVT = VA.getValVT();
3409
3410 // Shift into the upper bits if necessary.
3411 switch (VA.getLocInfo()) {
3412 default:
3413 break;
3414 case CCValAssign::AExtUpper:
3415 case CCValAssign::SExtUpper:
3416 case CCValAssign::ZExtUpper: {
3417 unsigned ValSizeInBits = ArgVT.getSizeInBits();
3418 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3419 unsigned Opcode =
3420 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
3421 Val = DAG.getNode(
3422 Opcode, DL, VA.getLocVT(), Val,
3423 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
3424 break;
3425 }
3426 }
3427
3428 // If this is an value smaller than the argument slot size (32-bit for O32,
3429 // 64-bit for N32/N64), it has been promoted in some way to the argument slot
3430 // size. Extract the value and insert any appropriate assertions regarding
3431 // sign/zero extension.
3432 switch (VA.getLocInfo()) {
3433 default:
3434 llvm_unreachable("Unknown loc info!")::llvm::llvm_unreachable_internal("Unknown loc info!", "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 3434)
;
3435 case CCValAssign::Full:
3436 break;
3437 case CCValAssign::AExtUpper:
3438 case CCValAssign::AExt:
3439 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
3440 break;
3441 case CCValAssign::SExtUpper:
3442 case CCValAssign::SExt:
3443 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT));
3444 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
3445 break;
3446 case CCValAssign::ZExtUpper:
3447 case CCValAssign::ZExt:
3448 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT));
3449 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
3450 break;
3451 case CCValAssign::BCvt:
3452 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
3453 break;
3454 }
3455
3456 return Val;
3457}
3458
3459//===----------------------------------------------------------------------===//
3460// Formal Arguments Calling Convention Implementation
3461//===----------------------------------------------------------------------===//
3462/// LowerFormalArguments - transform physical registers into virtual registers
3463/// and generate load operations for arguments places on the stack.
3464SDValue MipsTargetLowering::LowerFormalArguments(
3465 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
3466 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
3467 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
3468 MachineFunction &MF = DAG.getMachineFunction();
3469 MachineFrameInfo &MFI = MF.getFrameInfo();
3470 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3471
3472 MipsFI->setVarArgsFrameIndex(0);
3473
3474 // Used with vargs to acumulate store chains.
3475 std::vector<SDValue> OutChains;
3476
3477 // Assign locations to all of the incoming arguments.
3478 SmallVector<CCValAssign, 16> ArgLocs;
3479 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
3480 *DAG.getContext());
3481 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
3482 const Function &Func = DAG.getMachineFunction().getFunction();
3483 Function::const_arg_iterator FuncArg = Func.arg_begin();
3484
3485 if (Func.hasFnAttribute("interrupt") && !Func.arg_empty())
3486 report_fatal_error(
3487 "Functions with the interrupt attribute cannot have arguments!");
3488
3489 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg);
3490 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
3491 CCInfo.getInRegsParamsCount() > 0);
3492
3493 unsigned CurArgIdx = 0;
3494 CCInfo.rewindByValRegsInfo();
3495
3496 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3497 CCValAssign &VA = ArgLocs[i];
3498 if (Ins[i].isOrigArg()) {
3499 std::advance(FuncArg, Ins[i].getOrigArgIndex() - CurArgIdx);
3500 CurArgIdx = Ins[i].getOrigArgIndex();
3501 }
3502 EVT ValVT = VA.getValVT();
3503 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3504 bool IsRegLoc = VA.isRegLoc();
3505
3506 if (Flags.isByVal()) {
3507 assert(Ins[i].isOrigArg() && "Byval arguments cannot be implicit")((Ins[i].isOrigArg() && "Byval arguments cannot be implicit"
) ? static_cast<void> (0) : __assert_fail ("Ins[i].isOrigArg() && \"Byval arguments cannot be implicit\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 3507, __PRETTY_FUNCTION__))
;
3508 unsigned FirstByValReg, LastByValReg;
3509 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
3510 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
3511
3512 assert(Flags.getByValSize() &&((Flags.getByValSize() && "ByVal args of size 0 should have been ignored by front-end."
) ? static_cast<void> (0) : __assert_fail ("Flags.getByValSize() && \"ByVal args of size 0 should have been ignored by front-end.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 3513, __PRETTY_FUNCTION__))
3513 "ByVal args of size 0 should have been ignored by front-end.")((Flags.getByValSize() && "ByVal args of size 0 should have been ignored by front-end."
) ? static_cast<void> (0) : __assert_fail ("Flags.getByValSize() && \"ByVal args of size 0 should have been ignored by front-end.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 3513, __PRETTY_FUNCTION__))
;
3514 assert(ByValIdx < CCInfo.getInRegsParamsCount())((ByValIdx < CCInfo.getInRegsParamsCount()) ? static_cast<
void> (0) : __assert_fail ("ByValIdx < CCInfo.getInRegsParamsCount()"
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 3514, __PRETTY_FUNCTION__))
;
3515 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
3516 FirstByValReg, LastByValReg, VA, CCInfo);
3517 CCInfo.nextInRegsParam();
3518 continue;
3519 }
3520
3521 // Arguments stored on registers
3522 if (IsRegLoc) {
3523 MVT RegVT = VA.getLocVT();
3524 Register ArgReg = VA.getLocReg();
3525 const TargetRegisterClass *RC = getRegClassFor(RegVT);
3526
3527 // Transform the arguments stored on
3528 // physical registers into virtual ones
3529 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
3530 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
3531
3532 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
3533
3534 // Handle floating point arguments passed in integer registers and
3535 // long double arguments passed in floating point registers.
3536 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
3537 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
3538 (RegVT == MVT::f64 && ValVT == MVT::i64))
3539 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
3540 else if (ABI.IsO32() && RegVT == MVT::i32 &&
3541 ValVT == MVT::f64) {
3542 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
3543 getNextIntArgReg(ArgReg), RC);
3544 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
3545 if (!Subtarget.isLittle())
3546 std::swap(ArgValue, ArgValue2);
3547 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
3548 ArgValue, ArgValue2);
3549 }
3550
3551 InVals.push_back(ArgValue);
3552 } else { // VA.isRegLoc()
3553 MVT LocVT = VA.getLocVT();
3554
3555 if (ABI.IsO32()) {
3556 // We ought to be able to use LocVT directly but O32 sets it to i32
3557 // when allocating floating point values to integer registers.
3558 // This shouldn't influence how we load the value into registers unless
3559 // we are targeting softfloat.
3560 if (VA.getValVT().isFloatingPoint() && !Subtarget.useSoftFloat())
3561 LocVT = VA.getValVT();
3562 }
3563
3564 // sanity check
3565 assert(VA.isMemLoc())((VA.isMemLoc()) ? static_cast<void> (0) : __assert_fail
("VA.isMemLoc()", "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 3565, __PRETTY_FUNCTION__))
;
3566
3567 // The stack pointer offset is relative to the caller stack frame.
3568 int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8,
3569 VA.getLocMemOffset(), true);
3570
3571 // Create load nodes to retrieve arguments from the stack
3572 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
3573 SDValue ArgValue = DAG.getLoad(
3574 LocVT, DL, Chain, FIN,
3575 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI));
3576 OutChains.push_back(ArgValue.getValue(1));
3577
3578 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
3579
3580 InVals.push_back(ArgValue);
3581 }
3582 }
3583
3584 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3585 // The mips ABIs for returning structs by value requires that we copy
3586 // the sret argument into $v0 for the return. Save the argument into
3587 // a virtual register so that we can access it from the return points.
3588 if (Ins[i].Flags.isSRet()) {
3589 unsigned Reg = MipsFI->getSRetReturnReg();
3590 if (!Reg) {
3591 Reg = MF.getRegInfo().createVirtualRegister(
3592 getRegClassFor(ABI.IsN64() ? MVT::i64 : MVT::i32));
3593 MipsFI->setSRetReturnReg(Reg);
3594 }
3595 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
3596 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
3597 break;
3598 }
3599 }
3600
3601 if (IsVarArg)
3602 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo);
3603
3604 // All stores are grouped in one node to allow the matching between
3605 // the size of Ins and InVals. This only happens when on varg functions
3606 if (!OutChains.empty()) {
3607 OutChains.push_back(Chain);
3608 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
3609 }
3610
3611 return Chain;
3612}
3613
3614//===----------------------------------------------------------------------===//
3615// Return Value Calling Convention Implementation
3616//===----------------------------------------------------------------------===//
3617
3618bool
3619MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
3620 MachineFunction &MF, bool IsVarArg,
3621 const SmallVectorImpl<ISD::OutputArg> &Outs,
3622 LLVMContext &Context) const {
3623 SmallVector<CCValAssign, 16> RVLocs;
3624 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
3625 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3626}
3627
3628bool
3629MipsTargetLowering::shouldSignExtendTypeInLibCall(EVT Type, bool IsSigned) const {
3630 if ((ABI.IsN32() || ABI.IsN64()) && Type == MVT::i32)
3631 return true;
3632
3633 return IsSigned;
3634}
3635
3636SDValue
3637MipsTargetLowering::LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
3638 const SDLoc &DL,
3639 SelectionDAG &DAG) const {
3640 MachineFunction &MF = DAG.getMachineFunction();
3641 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3642
3643 MipsFI->setISR();
3644
3645 return DAG.getNode(MipsISD::ERet, DL, MVT::Other, RetOps);
3646}
3647
3648SDValue
3649MipsTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
3650 bool IsVarArg,
3651 const SmallVectorImpl<ISD::OutputArg> &Outs,
3652 const SmallVectorImpl<SDValue> &OutVals,
3653 const SDLoc &DL, SelectionDAG &DAG) const {
3654 // CCValAssign - represent the assignment of
3655 // the return value to a location
3656 SmallVector<CCValAssign, 16> RVLocs;
3657 MachineFunction &MF = DAG.getMachineFunction();
3658
3659 // CCState - Info about the registers and stack slot.
3660 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
3661
3662 // Analyze return values.
3663 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
3664
3665 SDValue Flag;
3666 SmallVector<SDValue, 4> RetOps(1, Chain);
3667
3668 // Copy the result values into the output registers.
3669 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3670 SDValue Val = OutVals[i];
3671 CCValAssign &VA = RVLocs[i];
3672 assert(VA.isRegLoc() && "Can only return in registers!")((VA.isRegLoc() && "Can only return in registers!") ?
static_cast<void> (0) : __assert_fail ("VA.isRegLoc() && \"Can only return in registers!\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 3672, __PRETTY_FUNCTION__))
;
3673 bool UseUpperBits = false;
3674
3675 switch (VA.getLocInfo()) {
3676 default:
3677 llvm_unreachable("Unknown loc info!")::llvm::llvm_unreachable_internal("Unknown loc info!", "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 3677)
;
3678 case CCValAssign::Full:
3679 break;
3680 case CCValAssign::BCvt:
3681 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
3682 break;
3683 case CCValAssign::AExtUpper:
3684 UseUpperBits = true;
3685 LLVM_FALLTHROUGH[[gnu::fallthrough]];
3686 case CCValAssign::AExt:
3687 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
3688 break;
3689 case CCValAssign::ZExtUpper:
3690 UseUpperBits = true;
3691 LLVM_FALLTHROUGH[[gnu::fallthrough]];
3692 case CCValAssign::ZExt:
3693 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
3694 break;
3695 case CCValAssign::SExtUpper:
3696 UseUpperBits = true;
3697 LLVM_FALLTHROUGH[[gnu::fallthrough]];
3698 case CCValAssign::SExt:
3699 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
3700 break;
3701 }
3702
3703 if (UseUpperBits) {
3704 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3705 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3706 Val = DAG.getNode(
3707 ISD::SHL, DL, VA.getLocVT(), Val,
3708 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
3709 }
3710
3711 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
3712
3713 // Guarantee that all emitted copies are stuck together with flags.
3714 Flag = Chain.getValue(1);
3715 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
3716 }
3717
3718 // The mips ABIs for returning structs by value requires that we copy
3719 // the sret argument into $v0 for the return. We saved the argument into
3720 // a virtual register in the entry block, so now we copy the value out
3721 // and into $v0.
3722 if (MF.getFunction().hasStructRetAttr()) {
3723 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3724 unsigned Reg = MipsFI->getSRetReturnReg();
3725
3726 if (!Reg)
3727 llvm_unreachable("sret virtual register not created in the entry block")::llvm::llvm_unreachable_internal("sret virtual register not created in the entry block"
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 3727)
;
3728 SDValue Val =
3729 DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(DAG.getDataLayout()));
3730 unsigned V0 = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
3731
3732 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
3733 Flag = Chain.getValue(1);
3734 RetOps.push_back(DAG.getRegister(V0, getPointerTy(DAG.getDataLayout())));
3735 }
3736
3737 RetOps[0] = Chain; // Update chain.
3738
3739 // Add the flag if we have it.
3740 if (Flag.getNode())
3741 RetOps.push_back(Flag);
3742
3743 // ISRs must use "eret".
3744 if (DAG.getMachineFunction().getFunction().hasFnAttribute("interrupt"))
3745 return LowerInterruptReturn(RetOps, DL, DAG);
3746
3747 // Standard return on Mips is a "jr $ra"
3748 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
3749}
3750
3751//===----------------------------------------------------------------------===//
3752// Mips Inline Assembly Support
3753//===----------------------------------------------------------------------===//
3754
3755/// getConstraintType - Given a constraint letter, return the type of
3756/// constraint it is for this target.
3757MipsTargetLowering::ConstraintType
3758MipsTargetLowering::getConstraintType(StringRef Constraint) const {
3759 // Mips specific constraints
3760 // GCC config/mips/constraints.md
3761 //
3762 // 'd' : An address register. Equivalent to r
3763 // unless generating MIPS16 code.
3764 // 'y' : Equivalent to r; retained for
3765 // backwards compatibility.
3766 // 'c' : A register suitable for use in an indirect
3767 // jump. This will always be $25 for -mabicalls.
3768 // 'l' : The lo register. 1 word storage.
3769 // 'x' : The hilo register pair. Double word storage.
3770 if (Constraint.size() == 1) {
3771 switch (Constraint[0]) {
3772 default : break;
3773 case 'd':
3774 case 'y':
3775 case 'f':
3776 case 'c':
3777 case 'l':
3778 case 'x':
3779 return C_RegisterClass;
3780 case 'R':
3781 return C_Memory;
3782 }
3783 }
3784
3785 if (Constraint == "ZC")
3786 return C_Memory;
3787
3788 return TargetLowering::getConstraintType(Constraint);
3789}
3790
3791/// Examine constraint type and operand type and determine a weight value.
3792/// This object must already have been set up with the operand type
3793/// and the current alternative constraint selected.
3794TargetLowering::ConstraintWeight
3795MipsTargetLowering::getSingleConstraintMatchWeight(
3796 AsmOperandInfo &info, const char *constraint) const {
3797 ConstraintWeight weight = CW_Invalid;
3798 Value *CallOperandVal = info.CallOperandVal;
3799 // If we don't have a value, we can't do a match,
3800 // but allow it at the lowest weight.
3801 if (!CallOperandVal)
3802 return CW_Default;
3803 Type *type = CallOperandVal->getType();
3804 // Look at the constraint type.
3805 switch (*constraint) {
3806 default:
3807 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3808 break;
3809 case 'd':
3810 case 'y':
3811 if (type->isIntegerTy())
3812 weight = CW_Register;
3813 break;
3814 case 'f': // FPU or MSA register
3815 if (Subtarget.hasMSA() && type->isVectorTy() &&
3816 cast<VectorType>(type)->getBitWidth() == 128)
3817 weight = CW_Register;
3818 else if (type->isFloatTy())
3819 weight = CW_Register;
3820 break;
3821 case 'c': // $25 for indirect jumps
3822 case 'l': // lo register
3823 case 'x': // hilo register pair
3824 if (type->isIntegerTy())
3825 weight = CW_SpecificReg;
3826 break;
3827 case 'I': // signed 16 bit immediate
3828 case 'J': // integer zero
3829 case 'K': // unsigned 16 bit immediate
3830 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3831 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3832 case 'O': // signed 15 bit immediate (+- 16383)
3833 case 'P': // immediate in the range of 65535 to 1 (inclusive)
3834 if (isa<ConstantInt>(CallOperandVal))
3835 weight = CW_Constant;
3836 break;
3837 case 'R':
3838 weight = CW_Memory;
3839 break;
3840 }
3841 return weight;
3842}
3843
3844/// This is a helper function to parse a physical register string and split it
3845/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
3846/// that is returned indicates whether parsing was successful. The second flag
3847/// is true if the numeric part exists.
3848static std::pair<bool, bool> parsePhysicalReg(StringRef C, StringRef &Prefix,
3849 unsigned long long &Reg) {
3850 if (C.front() != '{' || C.back() != '}')
3851 return std::make_pair(false, false);
3852
3853 // Search for the first numeric character.
3854 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
3855 I = std::find_if(B, E, isdigit);
3856
3857 Prefix = StringRef(B, I - B);
3858
3859 // The second flag is set to false if no numeric characters were found.
3860 if (I == E)
3861 return std::make_pair(true, false);
3862
3863 // Parse the numeric characters.
3864 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
3865 true);
3866}
3867
3868EVT MipsTargetLowering::getTypeForExtReturn(LLVMContext &Context, EVT VT,
3869 ISD::NodeType) const {
3870 bool Cond = !Subtarget.isABI_O32() && VT.getSizeInBits() == 32;
3871 EVT MinVT = getRegisterType(Context, Cond ? MVT::i64 : MVT::i32);
3872 return VT.bitsLT(MinVT) ? MinVT : VT;
3873}
3874
3875std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
3876parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
3877 const TargetRegisterInfo *TRI =
3878 Subtarget.getRegisterInfo();
3879 const TargetRegisterClass *RC;
3880 StringRef Prefix;
3881 unsigned long long Reg;
3882
3883 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
3884
3885 if (!R.first)
3886 return std::make_pair(0U, nullptr);
3887
3888 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
3889 // No numeric characters follow "hi" or "lo".
3890 if (R.second)
3891 return std::make_pair(0U, nullptr);
3892
3893 RC = TRI->getRegClass(Prefix == "hi" ?
3894 Mips::HI32RegClassID : Mips::LO32RegClassID);
3895 return std::make_pair(*(RC->begin()), RC);
3896 } else if (Prefix.startswith("$msa")) {
3897 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
3898
3899 // No numeric characters follow the name.
3900 if (R.second)
3901 return std::make_pair(0U, nullptr);
3902
3903 Reg = StringSwitch<unsigned long long>(Prefix)
3904 .Case("$msair", Mips::MSAIR)
3905 .Case("$msacsr", Mips::MSACSR)
3906 .Case("$msaaccess", Mips::MSAAccess)
3907 .Case("$msasave", Mips::MSASave)
3908 .Case("$msamodify", Mips::MSAModify)
3909 .Case("$msarequest", Mips::MSARequest)
3910 .Case("$msamap", Mips::MSAMap)
3911 .Case("$msaunmap", Mips::MSAUnmap)
3912 .Default(0);
3913
3914 if (!Reg)
3915 return std::make_pair(0U, nullptr);
3916
3917 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
3918 return std::make_pair(Reg, RC);
3919 }
3920
3921 if (!R.second)
3922 return std::make_pair(0U, nullptr);
3923
3924 if (Prefix == "$f") { // Parse $f0-$f31.
3925 // If the size of FP registers is 64-bit or Reg is an even number, select
3926 // the 64-bit register class. Otherwise, select the 32-bit register class.
3927 if (VT == MVT::Other)
3928 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
3929
3930 RC = getRegClassFor(VT);
3931
3932 if (RC == &Mips::AFGR64RegClass) {
3933 assert(Reg % 2 == 0)((Reg % 2 == 0) ? static_cast<void> (0) : __assert_fail
("Reg % 2 == 0", "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 3933, __PRETTY_FUNCTION__))
;
3934 Reg >>= 1;
3935 }
3936 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
3937 RC = TRI->getRegClass(Mips::FCCRegClassID);
3938 else if (Prefix == "$w") { // Parse $w0-$w31.
3939 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
3940 } else { // Parse $0-$31.
3941 assert(Prefix == "$")((Prefix == "$") ? static_cast<void> (0) : __assert_fail
("Prefix == \"$\"", "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 3941, __PRETTY_FUNCTION__))
;
3942 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3943 }
3944
3945 assert(Reg < RC->getNumRegs())((Reg < RC->getNumRegs()) ? static_cast<void> (0)
: __assert_fail ("Reg < RC->getNumRegs()", "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 3945, __PRETTY_FUNCTION__))
;
3946 return std::make_pair(*(RC->begin() + Reg), RC);
3947}
3948
3949/// Given a register class constraint, like 'r', if this corresponds directly
3950/// to an LLVM register class, return a register of 0 and the register class
3951/// pointer.
3952std::pair<unsigned, const TargetRegisterClass *>
3953MipsTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
3954 StringRef Constraint,
3955 MVT VT) const {
3956 if (Constraint.size() == 1) {
3957 switch (Constraint[0]) {
3958 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3959 case 'y': // Same as 'r'. Exists for compatibility.
3960 case 'r':
3961 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
3962 if (Subtarget.inMips16Mode())
3963 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
3964 return std::make_pair(0U, &Mips::GPR32RegClass);
3965 }
3966 if (VT == MVT::i64 && !Subtarget.isGP64bit())
3967 return std::make_pair(0U, &Mips::GPR32RegClass);
3968 if (VT == MVT::i64 && Subtarget.isGP64bit())
3969 return std::make_pair(0U, &Mips::GPR64RegClass);
3970 // This will generate an error message
3971 return std::make_pair(0U, nullptr);
3972 case 'f': // FPU or MSA register
3973 if (VT == MVT::v16i8)
3974 return std::make_pair(0U, &Mips::MSA128BRegClass);
3975 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3976 return std::make_pair(0U, &Mips::MSA128HRegClass);
3977 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3978 return std::make_pair(0U, &Mips::MSA128WRegClass);
3979 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3980 return std::make_pair(0U, &Mips::MSA128DRegClass);
3981 else if (VT == MVT::f32)
3982 return std::make_pair(0U, &Mips::FGR32RegClass);
3983 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
3984 if (Subtarget.isFP64bit())
3985 return std::make_pair(0U, &Mips::FGR64RegClass);
3986 return std::make_pair(0U, &Mips::AFGR64RegClass);
3987 }
3988 break;
3989 case 'c': // register suitable for indirect jump
3990 if (VT == MVT::i32)
3991 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
3992 if (VT == MVT::i64)
3993 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
3994 // This will generate an error message
3995 return std::make_pair(0U, nullptr);
3996 case 'l': // use the `lo` register to store values
3997 // that are no bigger than a word
3998 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8)
3999 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
4000 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
4001 case 'x': // use the concatenated `hi` and `lo` registers
4002 // to store doubleword values
4003 // Fixme: Not triggering the use of both hi and low
4004 // This will generate an error message
4005 return std::make_pair(0U, nullptr);
4006 }
4007 }
4008
4009 std::pair<unsigned, const TargetRegisterClass *> R;
4010 R = parseRegForInlineAsmConstraint(Constraint, VT);
4011
4012 if (R.second)
4013 return R;
4014
4015 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
4016}
4017
4018/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
4019/// vector. If it is invalid, don't add anything to Ops.
4020void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
4021 std::string &Constraint,
4022 std::vector<SDValue>&Ops,
4023 SelectionDAG &DAG) const {
4024 SDLoc DL(Op);
4025 SDValue Result;
4026
4027 // Only support length 1 constraints for now.
4028 if (Constraint.length() > 1) return;
4029
4030 char ConstraintLetter = Constraint[0];
4031 switch (ConstraintLetter) {
4032 default: break; // This will fall through to the generic implementation
4033 case 'I': // Signed 16 bit constant
4034 // If this fails, the parent routine will give an error
4035 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4036 EVT Type = Op.getValueType();
4037 int64_t Val = C->getSExtValue();
4038 if (isInt<16>(Val)) {
4039 Result = DAG.getTargetConstant(Val, DL, Type);
4040 break;
4041 }
4042 }
4043 return;
4044 case 'J': // integer zero
4045 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4046 EVT Type = Op.getValueType();
4047 int64_t Val = C->getZExtValue();
4048 if (Val == 0) {
4049 Result = DAG.getTargetConstant(0, DL, Type);
4050 break;
4051 }
4052 }
4053 return;
4054 case 'K': // unsigned 16 bit immediate
4055 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4056 EVT Type = Op.getValueType();
4057 uint64_t Val = (uint64_t)C->getZExtValue();
4058 if (isUInt<16>(Val)) {
4059 Result = DAG.getTargetConstant(Val, DL, Type);
4060 break;
4061 }
4062 }
4063 return;
4064 case 'L': // signed 32 bit immediate where lower 16 bits are 0
4065 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4066 EVT Type = Op.getValueType();
4067 int64_t Val = C->getSExtValue();
4068 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
4069 Result = DAG.getTargetConstant(Val, DL, Type);
4070 break;
4071 }
4072 }
4073 return;
4074 case 'N': // immediate in the range of -65535 to -1 (inclusive)
4075 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4076 EVT Type = Op.getValueType();
4077 int64_t Val = C->getSExtValue();
4078 if ((Val >= -65535) && (Val <= -1)) {
4079 Result = DAG.getTargetConstant(Val, DL, Type);
4080 break;
4081 }
4082 }
4083 return;
4084 case 'O': // signed 15 bit immediate
4085 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4086 EVT Type = Op.getValueType();
4087 int64_t Val = C->getSExtValue();
4088 if ((isInt<15>(Val))) {
4089 Result = DAG.getTargetConstant(Val, DL, Type);
4090 break;
4091 }
4092 }
4093 return;
4094 case 'P': // immediate in the range of 1 to 65535 (inclusive)
4095 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4096 EVT Type = Op.getValueType();
4097 int64_t Val = C->getSExtValue();
4098 if ((Val <= 65535) && (Val >= 1)) {
4099 Result = DAG.getTargetConstant(Val, DL, Type);
4100 break;
4101 }
4102 }
4103 return;
4104 }
4105
4106 if (Result.getNode()) {
4107 Ops.push_back(Result);
4108 return;
4109 }
4110
4111 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
4112}
4113
4114bool MipsTargetLowering::isLegalAddressingMode(const DataLayout &DL,
4115 const AddrMode &AM, Type *Ty,
4116 unsigned AS, Instruction *I) const {
4117 // No global is ever allowed as a base.
4118 if (AM.BaseGV)
4119 return false;
4120
4121 switch (AM.Scale) {
4122 case 0: // "r+i" or just "i", depending on HasBaseReg.
4123 break;
4124 case 1:
4125 if (!AM.HasBaseReg) // allow "r+i".
4126 break;
4127 return false; // disallow "r+r" or "r+r+i".
4128 default:
4129 return false;
4130 }
4131
4132 return true;
4133}
4134
4135bool
4136MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4137 // The Mips target isn't yet aware of offsets.
4138 return false;
4139}
4140
4141EVT MipsTargetLowering::getOptimalMemOpType(
4142 uint64_t Size, unsigned DstAlign, unsigned SrcAlign, bool IsMemset,
4143 bool ZeroMemset, bool MemcpyStrSrc,
4144 const AttributeList &FuncAttributes) const {
4145 if (Subtarget.hasMips64())
4146 return MVT::i64;
4147
4148 return MVT::i32;
4149}
4150
4151bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT,
4152 bool ForCodeSize) const {
4153 if (VT != MVT::f32 && VT != MVT::f64)
4154 return false;
4155 if (Imm.isNegZero())
4156 return false;
4157 return Imm.isZero();
4158}
4159
4160unsigned MipsTargetLowering::getJumpTableEncoding() const {
4161
4162 // FIXME: For space reasons this should be: EK_GPRel32BlockAddress.
4163 if (ABI.IsN64() && isPositionIndependent())
4164 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
4165
4166 return TargetLowering::getJumpTableEncoding();
4167}
4168
4169bool MipsTargetLowering::useSoftFloat() const {
4170 return Subtarget.useSoftFloat();
4171}
4172
4173void MipsTargetLowering::copyByValRegs(
4174 SDValue Chain, const SDLoc &DL, std::vector<SDValue> &OutChains,
4175 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
4176 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
4177 unsigned FirstReg, unsigned LastReg, const CCValAssign &VA,
4178 MipsCCState &State) const {
4179 MachineFunction &MF = DAG.getMachineFunction();
4180 MachineFrameInfo &MFI = MF.getFrameInfo();
4181 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
4182 unsigned NumRegs = LastReg - FirstReg;
4183 unsigned RegAreaSize = NumRegs * GPRSizeInBytes;
4184 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
4185 int FrameObjOffset;
4186 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs();
4187
4188 if (RegAreaSize)
4189 FrameObjOffset =
4190 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
4191 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes);
4192 else
4193 FrameObjOffset = VA.getLocMemOffset();
4194
4195 // Create frame object.
4196 EVT PtrTy = getPointerTy(DAG.getDataLayout());
4197 // Make the fixed object stored to mutable so that the load instructions
4198 // referencing it have their memory dependencies added.
4199 // Set the frame object as isAliased which clears the underlying objects
4200 // vector in ScheduleDAGInstrs::buildSchedGraph() resulting in addition of all
4201 // stores as dependencies for loads referencing this fixed object.
4202 int FI = MFI.CreateFixedObject(FrameObjSize, FrameObjOffset, false, true);
4203 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4204 InVals.push_back(FIN);
4205
4206 if (!NumRegs)
4207 return;
4208
4209 // Copy arg registers.
4210 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
4211 const TargetRegisterClass *RC = getRegClassFor(RegTy);
4212
4213 for (unsigned I = 0; I < NumRegs; ++I) {
4214 unsigned ArgReg = ByValArgRegs[FirstReg + I];
4215 unsigned VReg = addLiveIn(MF, ArgReg, RC);
4216 unsigned Offset = I * GPRSizeInBytes;
4217 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
4218 DAG.getConstant(Offset, DL, PtrTy));
4219 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
4220 StorePtr, MachinePointerInfo(FuncArg, Offset));
4221 OutChains.push_back(Store);
4222 }
4223}
4224
4225// Copy byVal arg to registers and stack.
4226void MipsTargetLowering::passByValArg(
4227 SDValue Chain, const SDLoc &DL,
4228 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
4229 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
4230 MachineFrameInfo &MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg,
4231 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle,
4232 const CCValAssign &VA) const {
4233 unsigned ByValSizeInBytes = Flags.getByValSize();
4234 unsigned OffsetInBytes = 0; // From beginning of struct
4235 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
4236 unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes);
4237 EVT PtrTy = getPointerTy(DAG.getDataLayout()),
4238 RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
4239 unsigned NumRegs = LastReg - FirstReg;
4240
4241 if (NumRegs) {
4242 ArrayRef<MCPhysReg> ArgRegs = ABI.GetByValArgRegs();
4243 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes);
4244 unsigned I = 0;
4245
4246 // Copy words to registers.
4247 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) {
4248 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
4249 DAG.getConstant(OffsetInBytes, DL, PtrTy));
4250 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
4251 MachinePointerInfo(), Alignment);
4252 MemOpChains.push_back(LoadVal.getValue(1));
4253 unsigned ArgReg = ArgRegs[FirstReg + I];
4254 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
4255 }
4256
4257 // Return if the struct has been fully copied.
4258 if (ByValSizeInBytes == OffsetInBytes)
4259 return;
4260
4261 // Copy the remainder of the byval argument with sub-word loads and shifts.
4262 if (LeftoverBytes) {
4263 SDValue Val;
4264
4265 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
4266 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
4267 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
4268
4269 if (RemainingSizeInBytes < LoadSizeInBytes)
4270 continue;
4271
4272 // Load subword.
4273 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
4274 DAG.getConstant(OffsetInBytes, DL,
4275 PtrTy));
4276 SDValue LoadVal = DAG.getExtLoad(
4277 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
4278 MVT::getIntegerVT(LoadSizeInBytes * 8), Alignment);
4279 MemOpChains.push_back(LoadVal.getValue(1));
4280
4281 // Shift the loaded value.
4282 unsigned Shamt;
4283
4284 if (isLittle)
4285 Shamt = TotalBytesLoaded * 8;
4286 else
4287 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
4288
4289 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
4290 DAG.getConstant(Shamt, DL, MVT::i32));
4291
4292 if (Val.getNode())
4293 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
4294 else
4295 Val = Shift;
4296
4297 OffsetInBytes += LoadSizeInBytes;
4298 TotalBytesLoaded += LoadSizeInBytes;
4299 Alignment = std::min(Alignment, LoadSizeInBytes);
4300 }
4301
4302 unsigned ArgReg = ArgRegs[FirstReg + I];
4303 RegsToPass.push_back(std::make_pair(ArgReg, Val));
4304 return;
4305 }
4306 }
4307
4308 // Copy remainder of byval arg to it with memcpy.
4309 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
4310 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
4311 DAG.getConstant(OffsetInBytes, DL, PtrTy));
4312 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
4313 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
4314 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
4315 DAG.getConstant(MemCpySize, DL, PtrTy),
4316 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
4317 /*isTailCall=*/false,
4318 MachinePointerInfo(), MachinePointerInfo());
4319 MemOpChains.push_back(Chain);
4320}
4321
4322void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
4323 SDValue Chain, const SDLoc &DL,
4324 SelectionDAG &DAG,
4325 CCState &State) const {
4326 ArrayRef<MCPhysReg> ArgRegs = ABI.GetVarArgRegs();
4327 unsigned Idx = State.getFirstUnallocated(ArgRegs);
4328 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
4329 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
4330 const TargetRegisterClass *RC = getRegClassFor(RegTy);
4331 MachineFunction &MF = DAG.getMachineFunction();
4332 MachineFrameInfo &MFI = MF.getFrameInfo();
4333 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
4334
4335 // Offset of the first variable argument from stack pointer.
4336 int VaArgOffset;
4337
4338 if (ArgRegs.size() == Idx)
4339 VaArgOffset = alignTo(State.getNextStackOffset(), RegSizeInBytes);
4340 else {
4341 VaArgOffset =
4342 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
4343 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
4344 }
4345
4346 // Record the frame index of the first variable argument
4347 // which is a value necessary to VASTART.
4348 int FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
4349 MipsFI->setVarArgsFrameIndex(FI);
4350
4351 // Copy the integer registers that have not been used for argument passing
4352 // to the argument register save area. For O32, the save area is allocated
4353 // in the caller's stack frame, while for N32/64, it is allocated in the
4354 // callee's stack frame.
4355 for (unsigned I = Idx; I < ArgRegs.size();
4356 ++I, VaArgOffset += RegSizeInBytes) {
4357 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
4358 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
4359 FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
4360 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
4361 SDValue Store =
4362 DAG.getStore(Chain, DL, ArgValue, PtrOff, MachinePointerInfo());
4363 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
4364 (Value *)nullptr);
4365 OutChains.push_back(Store);
4366 }
4367}
4368
4369void MipsTargetLowering::HandleByVal(CCState *State, unsigned &Size,
4370 unsigned Align) const {
4371 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
4372
4373 assert(Size && "Byval argument's size shouldn't be 0.")((Size && "Byval argument's size shouldn't be 0.") ? static_cast
<void> (0) : __assert_fail ("Size && \"Byval argument's size shouldn't be 0.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 4373, __PRETTY_FUNCTION__))
;
4374
4375 Align = std::min(Align, TFL->getStackAlignment());
4376
4377 unsigned FirstReg = 0;
4378 unsigned NumRegs = 0;
4379
4380 if (State->getCallingConv() != CallingConv::Fast) {
4381 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
4382 ArrayRef<MCPhysReg> IntArgRegs = ABI.GetByValArgRegs();
4383 // FIXME: The O32 case actually describes no shadow registers.
4384 const MCPhysReg *ShadowRegs =
4385 ABI.IsO32() ? IntArgRegs.data() : Mips64DPRegs;
4386
4387 // We used to check the size as well but we can't do that anymore since
4388 // CCState::HandleByVal() rounds up the size after calling this function.
4389 assert(!(Align % RegSizeInBytes) &&((!(Align % RegSizeInBytes) && "Byval argument's alignment should be a multiple of"
"RegSizeInBytes.") ? static_cast<void> (0) : __assert_fail
("!(Align % RegSizeInBytes) && \"Byval argument's alignment should be a multiple of\" \"RegSizeInBytes.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 4391, __PRETTY_FUNCTION__))
4390 "Byval argument's alignment should be a multiple of"((!(Align % RegSizeInBytes) && "Byval argument's alignment should be a multiple of"
"RegSizeInBytes.") ? static_cast<void> (0) : __assert_fail
("!(Align % RegSizeInBytes) && \"Byval argument's alignment should be a multiple of\" \"RegSizeInBytes.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 4391, __PRETTY_FUNCTION__))
4391 "RegSizeInBytes.")((!(Align % RegSizeInBytes) && "Byval argument's alignment should be a multiple of"
"RegSizeInBytes.") ? static_cast<void> (0) : __assert_fail
("!(Align % RegSizeInBytes) && \"Byval argument's alignment should be a multiple of\" \"RegSizeInBytes.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 4391, __PRETTY_FUNCTION__))
;
4392
4393 FirstReg = State->getFirstUnallocated(IntArgRegs);
4394
4395 // If Align > RegSizeInBytes, the first arg register must be even.
4396 // FIXME: This condition happens to do the right thing but it's not the
4397 // right way to test it. We want to check that the stack frame offset
4398 // of the register is aligned.
4399 if ((Align > RegSizeInBytes) && (FirstReg % 2)) {
4400 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]);
4401 ++FirstReg;
4402 }
4403
4404 // Mark the registers allocated.
4405 Size = alignTo(Size, RegSizeInBytes);
4406 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size());
4407 Size -= RegSizeInBytes, ++I, ++NumRegs)
4408 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]);
4409 }
4410
4411 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs);
4412}
4413
4414MachineBasicBlock *MipsTargetLowering::emitPseudoSELECT(MachineInstr &MI,
4415 MachineBasicBlock *BB,
4416 bool isFPCmp,
4417 unsigned Opc) const {
4418 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&((!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
"Subtarget already supports SELECT nodes with the use of" "conditional-move instructions."
) ? static_cast<void> (0) : __assert_fail ("!(Subtarget.hasMips4() || Subtarget.hasMips32()) && \"Subtarget already supports SELECT nodes with the use of\" \"conditional-move instructions.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 4420, __PRETTY_FUNCTION__))
4419 "Subtarget already supports SELECT nodes with the use of"((!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
"Subtarget already supports SELECT nodes with the use of" "conditional-move instructions."
) ? static_cast<void> (0) : __assert_fail ("!(Subtarget.hasMips4() || Subtarget.hasMips32()) && \"Subtarget already supports SELECT nodes with the use of\" \"conditional-move instructions.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 4420, __PRETTY_FUNCTION__))
4420 "conditional-move instructions.")((!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
"Subtarget already supports SELECT nodes with the use of" "conditional-move instructions."
) ? static_cast<void> (0) : __assert_fail ("!(Subtarget.hasMips4() || Subtarget.hasMips32()) && \"Subtarget already supports SELECT nodes with the use of\" \"conditional-move instructions.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 4420, __PRETTY_FUNCTION__))
;
4421
4422 const TargetInstrInfo *TII =
4423 Subtarget.getInstrInfo();
4424 DebugLoc DL = MI.getDebugLoc();
4425
4426 // To "insert" a SELECT instruction, we actually have to insert the
4427 // diamond control-flow pattern. The incoming instruction knows the
4428 // destination vreg to set, the condition code register to branch on, the
4429 // true/false values to select between, and a branch opcode to use.
4430 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4431 MachineFunction::iterator It = ++BB->getIterator();
4432
4433 // thisMBB:
4434 // ...
4435 // TrueVal = ...
4436 // setcc r1, r2, r3
4437 // bNE r1, r0, copy1MBB
4438 // fallthrough --> copy0MBB
4439 MachineBasicBlock *thisMBB = BB;
4440 MachineFunction *F = BB->getParent();
4441 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4442 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
4443 F->insert(It, copy0MBB);
4444 F->insert(It, sinkMBB);
4445
4446 // Transfer the remainder of BB and its successor edges to sinkMBB.
4447 sinkMBB->splice(sinkMBB->begin(), BB,
4448 std::next(MachineBasicBlock::iterator(MI)), BB->end());
4449 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
4450
4451 // Next, add the true and fallthrough blocks as its successors.
4452 BB->addSuccessor(copy0MBB);
4453 BB->addSuccessor(sinkMBB);
4454
4455 if (isFPCmp) {
4456 // bc1[tf] cc, sinkMBB
4457 BuildMI(BB, DL, TII->get(Opc))
4458 .addReg(MI.getOperand(1).getReg())
4459 .addMBB(sinkMBB);
4460 } else {
4461 // bne rs, $0, sinkMBB
4462 BuildMI(BB, DL, TII->get(Opc))
4463 .addReg(MI.getOperand(1).getReg())
4464 .addReg(Mips::ZERO)
4465 .addMBB(sinkMBB);
4466 }
4467
4468 // copy0MBB:
4469 // %FalseValue = ...
4470 // # fallthrough to sinkMBB
4471 BB = copy0MBB;
4472
4473 // Update machine-CFG edges
4474 BB->addSuccessor(sinkMBB);
4475
4476 // sinkMBB:
4477 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
4478 // ...
4479 BB = sinkMBB;
4480
4481 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
4482 .addReg(MI.getOperand(2).getReg())
4483 .addMBB(thisMBB)
4484 .addReg(MI.getOperand(3).getReg())
4485 .addMBB(copy0MBB);
4486
4487 MI.eraseFromParent(); // The pseudo instruction is gone now.
4488
4489 return BB;
4490}
4491
4492MachineBasicBlock *MipsTargetLowering::emitPseudoD_SELECT(MachineInstr &MI,
4493 MachineBasicBlock *BB) const {
4494 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&((!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
"Subtarget already supports SELECT nodes with the use of" "conditional-move instructions."
) ? static_cast<void> (0) : __assert_fail ("!(Subtarget.hasMips4() || Subtarget.hasMips32()) && \"Subtarget already supports SELECT nodes with the use of\" \"conditional-move instructions.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 4496, __PRETTY_FUNCTION__))
4495 "Subtarget already supports SELECT nodes with the use of"((!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
"Subtarget already supports SELECT nodes with the use of" "conditional-move instructions."
) ? static_cast<void> (0) : __assert_fail ("!(Subtarget.hasMips4() || Subtarget.hasMips32()) && \"Subtarget already supports SELECT nodes with the use of\" \"conditional-move instructions.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 4496, __PRETTY_FUNCTION__))
4496 "conditional-move instructions.")((!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
"Subtarget already supports SELECT nodes with the use of" "conditional-move instructions."
) ? static_cast<void> (0) : __assert_fail ("!(Subtarget.hasMips4() || Subtarget.hasMips32()) && \"Subtarget already supports SELECT nodes with the use of\" \"conditional-move instructions.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/lib/Target/Mips/MipsISelLowering.cpp"
, 4496, __PRETTY_FUNCTION__))
;
4497
4498 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
4499 DebugLoc DL = MI.getDebugLoc();
4500
4501 // D_SELECT substitutes two SELECT nodes that goes one after another and
4502 // have the same condition operand. On machines which don't have
4503 // conditional-move instruction, it reduces unnecessary branch instructions
4504 // which are result of using two diamond patterns that are result of two
4505 // SELECT pseudo instructions.
4506 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4507 MachineFunction::iterator It = ++BB->getIterator();
4508
4509 // thisMBB:
4510 // ...
4511 // TrueVal = ...
4512 // setcc r1, r2, r3
4513 // bNE r1, r0, copy1MBB
4514 // fallthrough --> copy0MBB
4515 MachineBasicBlock *thisMBB = BB;
4516 MachineFunction *F = BB->getParent();
4517 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4518 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
4519 F->insert(It, copy0MBB);
4520 F->insert(It, sinkMBB);
4521
4522 // Transfer the remainder of BB and its successor edges to sinkMBB.
4523 sinkMBB->splice(sinkMBB->begin(), BB,
4524 std::next(MachineBasicBlock::iterator(MI)), BB->end());
4525 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
4526
4527 // Next, add the true and fallthrough blocks as its successors.
4528 BB->addSuccessor(copy0MBB);
4529 BB->addSuccessor(sinkMBB);
4530
4531 // bne rs, $0, sinkMBB
4532 BuildMI(BB, DL, TII->get(Mips::BNE))
4533 .addReg(MI.getOperand(2).getReg())
4534 .addReg(Mips::ZERO)
4535 .addMBB(sinkMBB);
4536
4537 // copy0MBB:
4538 // %FalseValue = ...
4539 // # fallthrough to sinkMBB
4540 BB = copy0MBB;
4541
4542 // Update machine-CFG edges
4543 BB->addSuccessor(sinkMBB);
4544
4545 // sinkMBB:
4546 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
4547 // ...
4548 BB = sinkMBB;
4549
4550 // Use two PHI nodes to select two reults
4551 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
4552 .addReg(MI.getOperand(3).getReg())
4553 .addMBB(thisMBB)
4554 .addReg(MI.getOperand(5).getReg())
4555 .addMBB(copy0MBB);
4556 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(1).getReg())
4557 .addReg(MI.getOperand(4).getReg())
4558 .addMBB(thisMBB)
4559 .addReg(MI.getOperand(6).getReg())
4560 .addMBB(copy0MBB);
4561
4562 MI.eraseFromParent(); // The pseudo instruction is gone now.
4563
4564 return BB;
4565}
4566
4567// FIXME? Maybe this could be a TableGen attribute on some registers and
4568// this table could be generated automatically from RegInfo.
4569Register MipsTargetLowering::getRegisterByName(const char* RegName, EVT VT,
4570 const MachineFunction &MF) const {
4571 // Named registers is expected to be fairly rare. For now, just support $28
4572 // since the linux kernel uses it.
4573 if (Subtarget.isGP64bit()) {
4574 Register Reg = StringSwitch<Register>(RegName)
4575 .Case("$28", Mips::GP_64)
4576 .Default(Register());
4577 if (Reg)
4578 return Reg;
4579 } else {
4580 Register Reg = StringSwitch<Register>(RegName)
4581 .Case("$28", Mips::GP)
4582 .Default(Register());
4583 if (Reg)
4584 return Reg;
4585 }
4586 report_fatal_error("Invalid register name global variable");
4587}

/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/CodeGen/SelectionDAGNodes.h

1//===- llvm/CodeGen/SelectionDAGNodes.h - SelectionDAG Nodes ----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares the SDNode class and derived classes, which are used to
10// represent the nodes and operations present in a SelectionDAG. These nodes
11// and operations are machine code level operations, with some similarities to
12// the GCC RTL representation.
13//
14// Clients should include the SelectionDAG.h file instead of this file directly.
15//
16//===----------------------------------------------------------------------===//
17
18#ifndef LLVM_CODEGEN_SELECTIONDAGNODES_H
19#define LLVM_CODEGEN_SELECTIONDAGNODES_H
20
21#include "llvm/ADT/APFloat.h"
22#include "llvm/ADT/ArrayRef.h"
23#include "llvm/ADT/BitVector.h"
24#include "llvm/ADT/FoldingSet.h"
25#include "llvm/ADT/GraphTraits.h"
26#include "llvm/ADT/SmallPtrSet.h"
27#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/ilist_node.h"
29#include "llvm/ADT/iterator.h"
30#include "llvm/ADT/iterator_range.h"
31#include "llvm/CodeGen/ISDOpcodes.h"
32#include "llvm/CodeGen/MachineMemOperand.h"
33#include "llvm/CodeGen/ValueTypes.h"
34#include "llvm/IR/Constants.h"
35#include "llvm/IR/DebugLoc.h"
36#include "llvm/IR/Instruction.h"
37#include "llvm/IR/Instructions.h"
38#include "llvm/IR/Metadata.h"
39#include "llvm/IR/Operator.h"
40#include "llvm/Support/AlignOf.h"
41#include "llvm/Support/AtomicOrdering.h"
42#include "llvm/Support/Casting.h"
43#include "llvm/Support/ErrorHandling.h"
44#include "llvm/Support/MachineValueType.h"
45#include <algorithm>
46#include <cassert>
47#include <climits>
48#include <cstddef>
49#include <cstdint>
50#include <cstring>
51#include <iterator>
52#include <string>
53#include <tuple>
54
55namespace llvm {
56
57class APInt;
58class Constant;
59template <typename T> struct DenseMapInfo;
60class GlobalValue;
61class MachineBasicBlock;
62class MachineConstantPoolValue;
63class MCSymbol;
64class raw_ostream;
65class SDNode;
66class SelectionDAG;
67class Type;
68class Value;
69
70void checkForCycles(const SDNode *N, const SelectionDAG *DAG = nullptr,
71 bool force = false);
72
73/// This represents a list of ValueType's that has been intern'd by
74/// a SelectionDAG. Instances of this simple value class are returned by
75/// SelectionDAG::getVTList(...).
76///
77struct SDVTList {
78 const EVT *VTs;
79 unsigned int NumVTs;
80};
81
82namespace ISD {
83
84 /// Node predicates
85
86 /// If N is a BUILD_VECTOR node whose elements are all the same constant or
87 /// undefined, return true and return the constant value in \p SplatValue.
88 bool isConstantSplatVector(const SDNode *N, APInt &SplatValue);
89
90 /// Return true if the specified node is a BUILD_VECTOR where all of the
91 /// elements are ~0 or undef.
92 bool isBuildVectorAllOnes(const SDNode *N);
93
94 /// Return true if the specified node is a BUILD_VECTOR where all of the
95 /// elements are 0 or undef.
96 bool isBuildVectorAllZeros(const SDNode *N);
97
98 /// Return true if the specified node is a BUILD_VECTOR node of all
99 /// ConstantSDNode or undef.
100 bool isBuildVectorOfConstantSDNodes(const SDNode *N);
101
102 /// Return true if the specified node is a BUILD_VECTOR node of all
103 /// ConstantFPSDNode or undef.
104 bool isBuildVectorOfConstantFPSDNodes(const SDNode *N);
105
106 /// Return true if the node has at least one operand and all operands of the
107 /// specified node are ISD::UNDEF.
108 bool allOperandsUndef(const SDNode *N);
109
110} // end namespace ISD
111
112//===----------------------------------------------------------------------===//
113/// Unlike LLVM values, Selection DAG nodes may return multiple
114/// values as the result of a computation. Many nodes return multiple values,
115/// from loads (which define a token and a return value) to ADDC (which returns
116/// a result and a carry value), to calls (which may return an arbitrary number
117/// of values).
118///
119/// As such, each use of a SelectionDAG computation must indicate the node that
120/// computes it as well as which return value to use from that node. This pair
121/// of information is represented with the SDValue value type.
122///
123class SDValue {
124 friend struct DenseMapInfo<SDValue>;
125
126 SDNode *Node = nullptr; // The node defining the value we are using.
127 unsigned ResNo = 0; // Which return value of the node we are using.
128
129public:
130 SDValue() = default;
131 SDValue(SDNode *node, unsigned resno);
132
133 /// get the index which selects a specific result in the SDNode
134 unsigned getResNo() const { return ResNo; }
135
136 /// get the SDNode which holds the desired result
137 SDNode *getNode() const { return Node; }
138
139 /// set the SDNode
140 void setNode(SDNode *N) { Node = N; }
141
142 inline SDNode *operator->() const { return Node; }
143
144 bool operator==(const SDValue &O) const {
145 return Node == O.Node && ResNo == O.ResNo;
146 }
147 bool operator!=(const SDValue &O) const {
148 return !operator==(O);
149 }
150 bool operator<(const SDValue &O) const {
151 return std::tie(Node, ResNo) < std::tie(O.Node, O.ResNo);
152 }
153 explicit operator bool() const {
154 return Node != nullptr;
155 }
156
157 SDValue getValue(unsigned R) const {
158 return SDValue(Node, R);
159 }
160
161 /// Return true if this node is an operand of N.
162 bool isOperandOf(const SDNode *N) const;
163
164 /// Return the ValueType of the referenced return value.
165 inline EVT getValueType() const;
166
167 /// Return the simple ValueType of the referenced return value.
168 MVT getSimpleValueType() const {
169 return getValueType().getSimpleVT();
170 }
171
172 /// Returns the size of the value in bits.
173 unsigned getValueSizeInBits() const {
174 return getValueType().getSizeInBits();
175 }
176
177 unsigned getScalarValueSizeInBits() const {
178 return getValueType().getScalarType().getSizeInBits();
179 }
180
181 // Forwarding methods - These forward to the corresponding methods in SDNode.
182 inline unsigned getOpcode() const;
183 inline unsigned getNumOperands() const;
184 inline const SDValue &getOperand(unsigned i) const;
185 inline uint64_t getConstantOperandVal(unsigned i) const;
186 inline const APInt &getConstantOperandAPInt(unsigned i) const;
187 inline bool isTargetMemoryOpcode() const;
188 inline bool isTargetOpcode() const;
189 inline bool isMachineOpcode() const;
190 inline bool isUndef() const;
191 inline unsigned getMachineOpcode() const;
192 inline const DebugLoc &getDebugLoc() const;
193 inline void dump() const;
194 inline void dump(const SelectionDAG *G) const;
195 inline void dumpr() const;
196 inline void dumpr(const SelectionDAG *G) const;
197
198 /// Return true if this operand (which must be a chain) reaches the
199 /// specified operand without crossing any side-effecting instructions.
200 /// In practice, this looks through token factors and non-volatile loads.
201 /// In order to remain efficient, this only
202 /// looks a couple of nodes in, it does not do an exhaustive search.
203 bool reachesChainWithoutSideEffects(SDValue Dest,
204 unsigned Depth = 2) const;
205
206 /// Return true if there are no nodes using value ResNo of Node.
207 inline bool use_empty() const;
208
209 /// Return true if there is exactly one node using value ResNo of Node.
210 inline bool hasOneUse() const;
211};
212
213template<> struct DenseMapInfo<SDValue> {
214 static inline SDValue getEmptyKey() {
215 SDValue V;
216 V.ResNo = -1U;
217 return V;
218 }
219
220 static inline SDValue getTombstoneKey() {
221 SDValue V;
222 V.ResNo = -2U;
223 return V;
224 }
225
226 static unsigned getHashValue(const SDValue &Val) {
227 return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^
228 (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo();
229 }
230
231 static bool isEqual(const SDValue &LHS, const SDValue &RHS) {
232 return LHS == RHS;
233 }
234};
235
236/// Allow casting operators to work directly on
237/// SDValues as if they were SDNode*'s.
238template<> struct simplify_type<SDValue> {
239 using SimpleType = SDNode *;
240
241 static SimpleType getSimplifiedValue(SDValue &Val) {
242 return Val.getNode();
243 }
244};
245template<> struct simplify_type<const SDValue> {
246 using SimpleType = /*const*/ SDNode *;
247
248 static SimpleType getSimplifiedValue(const SDValue &Val) {
249 return Val.getNode();
250 }
251};
252
253/// Represents a use of a SDNode. This class holds an SDValue,
254/// which records the SDNode being used and the result number, a
255/// pointer to the SDNode using the value, and Next and Prev pointers,
256/// which link together all the uses of an SDNode.
257///
258class SDUse {
259 /// Val - The value being used.
260 SDValue Val;
261 /// User - The user of this value.
262 SDNode *User = nullptr;
263 /// Prev, Next - Pointers to the uses list of the SDNode referred by
264 /// this operand.
265 SDUse **Prev = nullptr;
266 SDUse *Next = nullptr;
267
268public:
269 SDUse() = default;
270 SDUse(const SDUse &U) = delete;
271 SDUse &operator=(const SDUse &) = delete;
272
273 /// Normally SDUse will just implicitly convert to an SDValue that it holds.
274 operator const SDValue&() const { return Val; }
275
276 /// If implicit conversion to SDValue doesn't work, the get() method returns
277 /// the SDValue.
278 const SDValue &get() const { return Val; }
279
280 /// This returns the SDNode that contains this Use.
281 SDNode *getUser() { return User; }
282
283 /// Get the next SDUse in the use list.
284 SDUse *getNext() const { return Next; }
285
286 /// Convenience function for get().getNode().
287 SDNode *getNode() const { return Val.getNode(); }
288 /// Convenience function for get().getResNo().
289 unsigned getResNo() const { return Val.getResNo(); }
290 /// Convenience function for get().getValueType().
291 EVT getValueType() const { return Val.getValueType(); }
292
293 /// Convenience function for get().operator==
294 bool operator==(const SDValue &V) const {
295 return Val == V;
296 }
297
298 /// Convenience function for get().operator!=
299 bool operator!=(const SDValue &V) const {
300 return Val != V;
301 }
302
303 /// Convenience function for get().operator<
304 bool operator<(const SDValue &V) const {
305 return Val < V;
306 }
307
308private:
309 friend class SelectionDAG;
310 friend class SDNode;
311 // TODO: unfriend HandleSDNode once we fix its operand handling.
312 friend class HandleSDNode;
313
314 void setUser(SDNode *p) { User = p; }
315
316 /// Remove this use from its existing use list, assign it the
317 /// given value, and add it to the new value's node's use list.
318 inline void set(const SDValue &V);
319 /// Like set, but only supports initializing a newly-allocated
320 /// SDUse with a non-null value.
321 inline void setInitial(const SDValue &V);
322 /// Like set, but only sets the Node portion of the value,
323 /// leaving the ResNo portion unmodified.
324 inline void setNode(SDNode *N);
325
326 void addToList(SDUse **List) {
327 Next = *List;
328 if (Next) Next->Prev = &Next;
329 Prev = List;
330 *List = this;
331 }
332
333 void removeFromList() {
334 *Prev = Next;
335 if (Next) Next->Prev = Prev;
336 }
337};
338
339/// simplify_type specializations - Allow casting operators to work directly on
340/// SDValues as if they were SDNode*'s.
341template<> struct simplify_type<SDUse> {
342 using SimpleType = SDNode *;
343
344 static SimpleType getSimplifiedValue(SDUse &Val) {
345 return Val.getNode();
346 }
347};
348
349/// These are IR-level optimization flags that may be propagated to SDNodes.
350/// TODO: This data structure should be shared by the IR optimizer and the
351/// the backend.
352struct SDNodeFlags {
353private:
354 // This bit is used to determine if the flags are in a defined state.
355 // Flag bits can only be masked out during intersection if the masking flags
356 // are defined.
357 bool AnyDefined : 1;
358
359 bool NoUnsignedWrap : 1;
360 bool NoSignedWrap : 1;
361 bool Exact : 1;
362 bool NoNaNs : 1;
363 bool NoInfs : 1;
364 bool NoSignedZeros : 1;
365 bool AllowReciprocal : 1;
366 bool VectorReduction : 1;
367 bool AllowContract : 1;
368 bool ApproximateFuncs : 1;
369 bool AllowReassociation : 1;
370
371 // We assume instructions do not raise floating-point exceptions by default,
372 // and only those marked explicitly may do so. We could choose to represent
373 // this via a positive "FPExcept" flags like on the MI level, but having a
374 // negative "NoFPExcept" flag here (that defaults to true) makes the flag
375 // intersection logic more straightforward.
376 bool NoFPExcept : 1;
377
378public:
379 /// Default constructor turns off all optimization flags.
380 SDNodeFlags()
381 : AnyDefined(false), NoUnsignedWrap(false), NoSignedWrap(false),
382 Exact(false), NoNaNs(false), NoInfs(false),
383 NoSignedZeros(false), AllowReciprocal(false), VectorReduction(false),
384 AllowContract(false), ApproximateFuncs(false),
385 AllowReassociation(false), NoFPExcept(true) {}
386
387 /// Propagate the fast-math-flags from an IR FPMathOperator.
388 void copyFMF(const FPMathOperator &FPMO) {
389 setNoNaNs(FPMO.hasNoNaNs());
390 setNoInfs(FPMO.hasNoInfs());
391 setNoSignedZeros(FPMO.hasNoSignedZeros());
392 setAllowReciprocal(FPMO.hasAllowReciprocal());
393 setAllowContract(FPMO.hasAllowContract());
394 setApproximateFuncs(FPMO.hasApproxFunc());
395 setAllowReassociation(FPMO.hasAllowReassoc());
396 }
397
398 /// Sets the state of the flags to the defined state.
399 void setDefined() { AnyDefined = true; }
400 /// Returns true if the flags are in a defined state.
401 bool isDefined() const { return AnyDefined; }
402
403 // These are mutators for each flag.
404 void setNoUnsignedWrap(bool b) {
405 setDefined();
406 NoUnsignedWrap = b;
407 }
408 void setNoSignedWrap(bool b) {
409 setDefined();
410 NoSignedWrap = b;
411 }
412 void setExact(bool b) {
413 setDefined();
414 Exact = b;
415 }
416 void setNoNaNs(bool b) {
417 setDefined();
418 NoNaNs = b;
419 }
420 void setNoInfs(bool b) {
421 setDefined();
422 NoInfs = b;
423 }
424 void setNoSignedZeros(bool b) {
425 setDefined();
426 NoSignedZeros = b;
427 }
428 void setAllowReciprocal(bool b) {
429 setDefined();
430 AllowReciprocal = b;
431 }
432 void setVectorReduction(bool b) {
433 setDefined();
434 VectorReduction = b;
435 }
436 void setAllowContract(bool b) {
437 setDefined();
438 AllowContract = b;
439 }
440 void setApproximateFuncs(bool b) {
441 setDefined();
442 ApproximateFuncs = b;
443 }
444 void setAllowReassociation(bool b) {
445 setDefined();
446 AllowReassociation = b;
447 }
448 void setFPExcept(bool b) {
449 setDefined();
450 NoFPExcept = !b;
451 }
452
453 // These are accessors for each flag.
454 bool hasNoUnsignedWrap() const { return NoUnsignedWrap; }
455 bool hasNoSignedWrap() const { return NoSignedWrap; }
456 bool hasExact() const { return Exact; }
457 bool hasNoNaNs() const { return NoNaNs; }
458 bool hasNoInfs() const { return NoInfs; }
459 bool hasNoSignedZeros() const { return NoSignedZeros; }
460 bool hasAllowReciprocal() const { return AllowReciprocal; }
461 bool hasVectorReduction() const { return VectorReduction; }
462 bool hasAllowContract() const { return AllowContract; }
463 bool hasApproximateFuncs() const { return ApproximateFuncs; }
464 bool hasAllowReassociation() const { return AllowReassociation; }
465 bool hasFPExcept() const { return !NoFPExcept; }
466
467 bool isFast() const {
468 return NoSignedZeros && AllowReciprocal && NoNaNs && NoInfs && NoFPExcept &&
469 AllowContract && ApproximateFuncs && AllowReassociation;
470 }
471
472 /// Clear any flags in this flag set that aren't also set in Flags.
473 /// If the given Flags are undefined then don't do anything.
474 void intersectWith(const SDNodeFlags Flags) {
475 if (!Flags.isDefined())
476 return;
477 NoUnsignedWrap &= Flags.NoUnsignedWrap;
478 NoSignedWrap &= Flags.NoSignedWrap;
479 Exact &= Flags.Exact;
480 NoNaNs &= Flags.NoNaNs;
481 NoInfs &= Flags.NoInfs;
482 NoSignedZeros &= Flags.NoSignedZeros;
483 AllowReciprocal &= Flags.AllowReciprocal;
484 VectorReduction &= Flags.VectorReduction;
485 AllowContract &= Flags.AllowContract;
486 ApproximateFuncs &= Flags.ApproximateFuncs;
487 AllowReassociation &= Flags.AllowReassociation;
488 NoFPExcept &= Flags.NoFPExcept;
489 }
490};
491
492/// Represents one node in the SelectionDAG.
493///
494class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
495private:
496 /// The operation that this node performs.
497 int16_t NodeType;
498
499protected:
500 // We define a set of mini-helper classes to help us interpret the bits in our
501 // SubclassData. These are designed to fit within a uint16_t so they pack
502 // with NodeType.
503
504#if defined(_AIX) && (!defined(__GNUC__4) || defined(__ibmxl__))
505// Except for GCC; by default, AIX compilers store bit-fields in 4-byte words
506// and give the `pack` pragma push semantics.
507#define BEGIN_TWO_BYTE_PACK() _Pragma("pack(2)")pack(2)
508#define END_TWO_BYTE_PACK() _Pragma("pack(pop)")pack(pop)
509#else
510#define BEGIN_TWO_BYTE_PACK()
511#define END_TWO_BYTE_PACK()
512#endif
513
514BEGIN_TWO_BYTE_PACK()
515 class SDNodeBitfields {
516 friend class SDNode;
517 friend class MemIntrinsicSDNode;
518 friend class MemSDNode;
519 friend class SelectionDAG;
520
521 uint16_t HasDebugValue : 1;
522 uint16_t IsMemIntrinsic : 1;
523 uint16_t IsDivergent : 1;
524 };
525 enum { NumSDNodeBits = 3 };
526
527 class ConstantSDNodeBitfields {
528 friend class ConstantSDNode;
529
530 uint16_t : NumSDNodeBits;
531
532 uint16_t IsOpaque : 1;
533 };
534
535 class MemSDNodeBitfields {
536 friend class MemSDNode;
537 friend class MemIntrinsicSDNode;
538 friend class AtomicSDNode;
539
540 uint16_t : NumSDNodeBits;
541
542 uint16_t IsVolatile : 1;
543 uint16_t IsNonTemporal : 1;
544 uint16_t IsDereferenceable : 1;
545 uint16_t IsInvariant : 1;
546 };
547 enum { NumMemSDNodeBits = NumSDNodeBits + 4 };
548
549 class LSBaseSDNodeBitfields {
550 friend class LSBaseSDNode;
551 friend class MaskedGatherScatterSDNode;
552
553 uint16_t : NumMemSDNodeBits;
554
555 // This storage is shared between disparate class hierarchies to hold an
556 // enumeration specific to the class hierarchy in use.
557 // LSBaseSDNode => enum ISD::MemIndexedMode
558 // MaskedGatherScatterSDNode => enum ISD::MemIndexType
559 uint16_t AddressingMode : 3;
560 };
561 enum { NumLSBaseSDNodeBits = NumMemSDNodeBits + 3 };
562
563 class LoadSDNodeBitfields {
564 friend class LoadSDNode;
565 friend class MaskedLoadSDNode;
566
567 uint16_t : NumLSBaseSDNodeBits;
568
569 uint16_t ExtTy : 2; // enum ISD::LoadExtType
570 uint16_t IsExpanding : 1;
571 };
572
573 class StoreSDNodeBitfields {
574 friend class StoreSDNode;
575 friend class MaskedStoreSDNode;
576
577 uint16_t : NumLSBaseSDNodeBits;
578
579 uint16_t IsTruncating : 1;
580 uint16_t IsCompressing : 1;
581 };
582
583 union {
584 char RawSDNodeBits[sizeof(uint16_t)];
585 SDNodeBitfields SDNodeBits;
586 ConstantSDNodeBitfields ConstantSDNodeBits;
587 MemSDNodeBitfields MemSDNodeBits;
588 LSBaseSDNodeBitfields LSBaseSDNodeBits;
589 LoadSDNodeBitfields LoadSDNodeBits;
590 StoreSDNodeBitfields StoreSDNodeBits;
591 };
592END_TWO_BYTE_PACK()
593#undef BEGIN_TWO_BYTE_PACK
594#undef END_TWO_BYTE_PACK
595
596 // RawSDNodeBits must cover the entirety of the union. This means that all of
597 // the union's members must have size <= RawSDNodeBits. We write the RHS as
598 // "2" instead of sizeof(RawSDNodeBits) because MSVC can't handle the latter.
599 static_assert(sizeof(SDNodeBitfields) <= 2, "field too wide");
600 static_assert(sizeof(ConstantSDNodeBitfields) <= 2, "field too wide");
601 static_assert(sizeof(MemSDNodeBitfields) <= 2, "field too wide");
602 static_assert(sizeof(LSBaseSDNodeBitfields) <= 2, "field too wide");
603 static_assert(sizeof(LoadSDNodeBitfields) <= 2, "field too wide");
604 static_assert(sizeof(StoreSDNodeBitfields) <= 2, "field too wide");
605
606private:
607 friend class SelectionDAG;
608 // TODO: unfriend HandleSDNode once we fix its operand handling.
609 friend class HandleSDNode;
610
611 /// Unique id per SDNode in the DAG.
612 int NodeId = -1;
613
614 /// The values that are used by this operation.
615 SDUse *OperandList = nullptr;
616
617 /// The types of the values this node defines. SDNode's may
618 /// define multiple values simultaneously.
619 const EVT *ValueList;
620
621 /// List of uses for this SDNode.
622 SDUse *UseList = nullptr;
623
624 /// The number of entries in the Operand/Value list.
625 unsigned short NumOperands = 0;
626 unsigned short NumValues;
627
628 // The ordering of the SDNodes. It roughly corresponds to the ordering of the
629 // original LLVM instructions.
630 // This is used for turning off scheduling, because we'll forgo
631 // the normal scheduling algorithms and output the instructions according to
632 // this ordering.
633 unsigned IROrder;
634
635 /// Source line information.
636 DebugLoc debugLoc;
637
638 /// Return a pointer to the specified value type.
639 static const EVT *getValueTypeList(EVT VT);
640
641 SDNodeFlags Flags;
642
643public:
644 /// Unique and persistent id per SDNode in the DAG.
645 /// Used for debug printing.
646 uint16_t PersistentId;
647
648 //===--------------------------------------------------------------------===//
649 // Accessors
650 //
651
652 /// Return the SelectionDAG opcode value for this node. For
653 /// pre-isel nodes (those for which isMachineOpcode returns false), these
654 /// are the opcode values in the ISD and <target>ISD namespaces. For
655 /// post-isel opcodes, see getMachineOpcode.
656 unsigned getOpcode() const { return (unsigned short)NodeType; }
657
658 /// Test if this node has a target-specific opcode (in the
659 /// \<target\>ISD namespace).
660 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
661
662 /// Test if this node has a target-specific
663 /// memory-referencing opcode (in the \<target\>ISD namespace and
664 /// greater than FIRST_TARGET_MEMORY_OPCODE).
665 bool isTargetMemoryOpcode() const {
666 return NodeType >= ISD::FIRST_TARGET_MEMORY_OPCODE;
667 }
668
669 /// Return true if the type of the node type undefined.
670 bool isUndef() const { return NodeType == ISD::UNDEF; }
671
672 /// Test if this node is a memory intrinsic (with valid pointer information).
673 /// INTRINSIC_W_CHAIN and INTRINSIC_VOID nodes are sometimes created for
674 /// non-memory intrinsics (with chains) that are not really instances of
675 /// MemSDNode. For such nodes, we need some extra state to determine the
676 /// proper classof relationship.
677 bool isMemIntrinsic() const {
678 return (NodeType == ISD::INTRINSIC_W_CHAIN ||
679 NodeType == ISD::INTRINSIC_VOID) &&
680 SDNodeBits.IsMemIntrinsic;
681 }
682
683 /// Test if this node is a strict floating point pseudo-op.
684 bool isStrictFPOpcode() {
685 switch (NodeType) {
686 default:
687 return false;
688 case ISD::STRICT_FADD:
689 case ISD::STRICT_FSUB:
690 case ISD::STRICT_FMUL:
691 case ISD::STRICT_FDIV:
692 case ISD::STRICT_FREM:
693 case ISD::STRICT_FMA:
694 case ISD::STRICT_FSQRT:
695 case ISD::STRICT_FPOW:
696 case ISD::STRICT_FPOWI:
697 case ISD::STRICT_FSIN:
698 case ISD::STRICT_FCOS:
699 case ISD::STRICT_FEXP:
700 case ISD::STRICT_FEXP2:
701 case ISD::STRICT_FLOG:
702 case ISD::STRICT_FLOG10:
703 case ISD::STRICT_FLOG2:
704 case ISD::STRICT_LRINT:
705 case ISD::STRICT_LLRINT:
706 case ISD::STRICT_FRINT:
707 case ISD::STRICT_FNEARBYINT:
708 case ISD::STRICT_FMAXNUM:
709 case ISD::STRICT_FMINNUM:
710 case ISD::STRICT_FCEIL:
711 case ISD::STRICT_FFLOOR:
712 case ISD::STRICT_LROUND:
713 case ISD::STRICT_LLROUND:
714 case ISD::STRICT_FROUND:
715 case ISD::STRICT_FTRUNC:
716 case ISD::STRICT_FP_TO_SINT:
717 case ISD::STRICT_FP_TO_UINT:
718 case ISD::STRICT_FP_ROUND:
719 case ISD::STRICT_FP_EXTEND:
720 return true;
721 }
722 }
723
724 /// Test if this node has a post-isel opcode, directly
725 /// corresponding to a MachineInstr opcode.
726 bool isMachineOpcode() const { return NodeType < 0; }
727
728 /// This may only be called if isMachineOpcode returns
729 /// true. It returns the MachineInstr opcode value that the node's opcode
730 /// corresponds to.
731 unsigned getMachineOpcode() const {
732 assert(isMachineOpcode() && "Not a MachineInstr opcode!")((isMachineOpcode() && "Not a MachineInstr opcode!") ?
static_cast<void> (0) : __assert_fail ("isMachineOpcode() && \"Not a MachineInstr opcode!\""
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/CodeGen/SelectionDAGNodes.h"
, 732, __PRETTY_FUNCTION__))
;
733 return ~NodeType;
734 }
735
736 bool getHasDebugValue() const { return SDNodeBits.HasDebugValue; }
737 void setHasDebugValue(bool b) { SDNodeBits.HasDebugValue = b; }
738
739 bool isDivergent() const { return SDNodeBits.IsDivergent; }
740
741 /// Return true if there are no uses of this node.
742 bool use_empty() const { return UseList == nullptr; }
743
744 /// Return true if there is exactly one use of this node.
745 bool hasOneUse() const {
746 return !use_empty() && std::next(use_begin()) == use_end();
747 }
748
749 /// Return the number of uses of this node. This method takes
750 /// time proportional to the number of uses.
751 size_t use_size() const { return std::distance(use_begin(), use_end()); }
752
753 /// Return the unique node id.
754 int getNodeId() const { return NodeId; }
755
756 /// Set unique node id.
757 void setNodeId(int Id) { NodeId = Id; }
758
759 /// Return the node ordering.
760 unsigned getIROrder() const { return IROrder; }
761
762 /// Set the node ordering.
763 void setIROrder(unsigned Order) { IROrder = Order; }
764
765 /// Return the source location info.
766 const DebugLoc &getDebugLoc() const { return debugLoc; }
767
768 /// Set source location info. Try to avoid this, putting
769 /// it in the constructor is preferable.
770 void setDebugLoc(DebugLoc dl) { debugLoc = std::move(dl); }
771
772 /// This class provides iterator support for SDUse
773 /// operands that use a specific SDNode.
774 class use_iterator
775 : public std::iterator<std::forward_iterator_tag, SDUse, ptrdiff_t> {
776 friend class SDNode;
777
778 SDUse *Op = nullptr;
779
780 explicit use_iterator(SDUse *op) : Op(op) {}
781
782 public:
783 using reference = std::iterator<std::forward_iterator_tag,
784 SDUse, ptrdiff_t>::reference;
785 using pointer = std::iterator<std::forward_iterator_tag,
786 SDUse, ptrdiff_t>::pointer;
787
788 use_iterator() = default;
789 use_iterator(const use_iterator &I) : Op(I.Op) {}
790
791 bool operator==(const use_iterator &x) const {
792 return Op == x.Op;
793 }
794 bool operator!=(const use_iterator &x) const {
795 return !operator==(x);
796 }
797
798 /// Return true if this iterator is at the end of uses list.
799 bool atEnd() const { return Op == nullptr; }
800
801 // Iterator traversal: forward iteration only.
802 use_iterator &operator++() { // Preincrement
803 assert(Op && "Cannot increment end iterator!")((Op && "Cannot increment end iterator!") ? static_cast
<void> (0) : __assert_fail ("Op && \"Cannot increment end iterator!\""
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/CodeGen/SelectionDAGNodes.h"
, 803, __PRETTY_FUNCTION__))
;
804 Op = Op->getNext();
805 return *this;
806 }
807
808 use_iterator operator++(int) { // Postincrement
809 use_iterator tmp = *this; ++*this; return tmp;
810 }
811
812 /// Retrieve a pointer to the current user node.
813 SDNode *operator*() const {
814 assert(Op && "Cannot dereference end iterator!")((Op && "Cannot dereference end iterator!") ? static_cast
<void> (0) : __assert_fail ("Op && \"Cannot dereference end iterator!\""
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/CodeGen/SelectionDAGNodes.h"
, 814, __PRETTY_FUNCTION__))
;
815 return Op->getUser();
816 }
817
818 SDNode *operator->() const { return operator*(); }
819
820 SDUse &getUse() const { return *Op; }
821
822 /// Retrieve the operand # of this use in its user.
823 unsigned getOperandNo() const {
824 assert(Op && "Cannot dereference end iterator!")((Op && "Cannot dereference end iterator!") ? static_cast
<void> (0) : __assert_fail ("Op && \"Cannot dereference end iterator!\""
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/CodeGen/SelectionDAGNodes.h"
, 824, __PRETTY_FUNCTION__))
;
825 return (unsigned)(Op - Op->getUser()->OperandList);
826 }
827 };
828
829 /// Provide iteration support to walk over all uses of an SDNode.
830 use_iterator use_begin() const {
831 return use_iterator(UseList);
832 }
833
834 static use_iterator use_end() { return use_iterator(nullptr); }
835
836 inline iterator_range<use_iterator> uses() {
837 return make_range(use_begin(), use_end());
838 }
839 inline iterator_range<use_iterator> uses() const {
840 return make_range(use_begin(), use_end());
841 }
842
843 /// Return true if there are exactly NUSES uses of the indicated value.
844 /// This method ignores uses of other values defined by this operation.
845 bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
846
847 /// Return true if there are any use of the indicated value.
848 /// This method ignores uses of other values defined by this operation.
849 bool hasAnyUseOfValue(unsigned Value) const;
850
851 /// Return true if this node is the only use of N.
852 bool isOnlyUserOf(const SDNode *N) const;
853
854 /// Return true if this node is an operand of N.
855 bool isOperandOf(const SDNode *N) const;
856
857 /// Return true if this node is a predecessor of N.
858 /// NOTE: Implemented on top of hasPredecessor and every bit as
859 /// expensive. Use carefully.
860 bool isPredecessorOf(const SDNode *N) const {
861 return N->hasPredecessor(this);
862 }
863
864 /// Return true if N is a predecessor of this node.
865 /// N is either an operand of this node, or can be reached by recursively
866 /// traversing up the operands.
867 /// NOTE: This is an expensive method. Use it carefully.
868 bool hasPredecessor(const SDNode *N) const;
869
870 /// Returns true if N is a predecessor of any node in Worklist. This
871 /// helper keeps Visited and Worklist sets externally to allow unions
872 /// searches to be performed in parallel, caching of results across
873 /// queries and incremental addition to Worklist. Stops early if N is
874 /// found but will resume. Remember to clear Visited and Worklists
875 /// if DAG changes. MaxSteps gives a maximum number of nodes to visit before
876 /// giving up. The TopologicalPrune flag signals that positive NodeIds are
877 /// topologically ordered (Operands have strictly smaller node id) and search
878 /// can be pruned leveraging this.
879 static bool hasPredecessorHelper(const SDNode *N,
880 SmallPtrSetImpl<const SDNode *> &Visited,
881 SmallVectorImpl<const SDNode *> &Worklist,
882 unsigned int MaxSteps = 0,
883 bool TopologicalPrune = false) {
884 SmallVector<const SDNode *, 8> DeferredNodes;
885 if (Visited.count(N))
886 return true;
887
888 // Node Id's are assigned in three places: As a topological
889 // ordering (> 0), during legalization (results in values set to
890 // 0), new nodes (set to -1). If N has a topolgical id then we
891 // know that all nodes with ids smaller than it cannot be
892 // successors and we need not check them. Filter out all node
893 // that can't be matches. We add them to the worklist before exit
894 // in case of multiple calls. Note that during selection the topological id
895 // may be violated if a node's predecessor is selected before it. We mark
896 // this at selection negating the id of unselected successors and
897 // restricting topological pruning to positive ids.
898
899 int NId = N->getNodeId();
900 // If we Invalidated the Id, reconstruct original NId.
901 if (NId < -1)
902 NId = -(NId + 1);
903
904 bool Found = false;
905 while (!Worklist.empty()) {
906 const SDNode *M = Worklist.pop_back_val();
907 int MId = M->getNodeId();
908 if (TopologicalPrune && M->getOpcode() != ISD::TokenFactor && (NId > 0) &&
909 (MId > 0) && (MId < NId)) {
910 DeferredNodes.push_back(M);
911 continue;
912 }
913 for (const SDValue &OpV : M->op_values()) {
914 SDNode *Op = OpV.getNode();
915 if (Visited.insert(Op).second)
916 Worklist.push_back(Op);
917 if (Op == N)
918 Found = true;
919 }
920 if (Found)
921 break;
922 if (MaxSteps != 0 && Visited.size() >= MaxSteps)
923 break;
924 }
925 // Push deferred nodes back on worklist.
926 Worklist.append(DeferredNodes.begin(), DeferredNodes.end());
927 // If we bailed early, conservatively return found.
928 if (MaxSteps != 0 && Visited.size() >= MaxSteps)
929 return true;
930 return Found;
931 }
932
933 /// Return true if all the users of N are contained in Nodes.
934 /// NOTE: Requires at least one match, but doesn't require them all.
935 static bool areOnlyUsersOf(ArrayRef<const SDNode *> Nodes, const SDNode *N);
936
937 /// Return the number of values used by this operation.
938 unsigned getNumOperands() const { return NumOperands; }
939
940 /// Return the maximum number of operands that a SDNode can hold.
941 static constexpr size_t getMaxNumOperands() {
942 return std::numeric_limits<decltype(SDNode::NumOperands)>::max();
943 }
944
945 /// Helper method returns the integer value of a ConstantSDNode operand.
946 inline uint64_t getConstantOperandVal(unsigned Num) const;
947
948 /// Helper method returns the APInt of a ConstantSDNode operand.
949 inline const APInt &getConstantOperandAPInt(unsigned Num) const;
950
951 const SDValue &getOperand(unsigned Num) const {
952 assert(Num < NumOperands && "Invalid child # of SDNode!")((Num < NumOperands && "Invalid child # of SDNode!"
) ? static_cast<void> (0) : __assert_fail ("Num < NumOperands && \"Invalid child # of SDNode!\""
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/CodeGen/SelectionDAGNodes.h"
, 952, __PRETTY_FUNCTION__))
;
953 return OperandList[Num];
954 }
955
956 using op_iterator = SDUse *;
957
958 op_iterator op_begin() const { return OperandList; }
959 op_iterator op_end() const { return OperandList+NumOperands; }
960 ArrayRef<SDUse> ops() const { return makeArrayRef(op_begin(), op_end()); }
961
962 /// Iterator for directly iterating over the operand SDValue's.
963 struct value_op_iterator
964 : iterator_adaptor_base<value_op_iterator, op_iterator,
965 std::random_access_iterator_tag, SDValue,
966 ptrdiff_t, value_op_iterator *,
967 value_op_iterator *> {
968 explicit value_op_iterator(SDUse *U = nullptr)
969 : iterator_adaptor_base(U) {}
970
971 const SDValue &operator*() const { return I->get(); }
972 };
973
974 iterator_range<value_op_iterator> op_values() const {
975 return make_range(value_op_iterator(op_begin()),
976 value_op_iterator(op_end()));
977 }
978
979 SDVTList getVTList() const {
980 SDVTList X = { ValueList, NumValues };
981 return X;
982 }
983
984 /// If this node has a glue operand, return the node
985 /// to which the glue operand points. Otherwise return NULL.
986 SDNode *getGluedNode() const {
987 if (getNumOperands() != 0 &&
988 getOperand(getNumOperands()-1).getValueType() == MVT::Glue)
989 return getOperand(getNumOperands()-1).getNode();
990 return nullptr;
991 }
992
993 /// If this node has a glue value with a user, return
994 /// the user (there is at most one). Otherwise return NULL.
995 SDNode *getGluedUser() const {
996 for (use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI)
997 if (UI.getUse().get().getValueType() == MVT::Glue)
998 return *UI;
999 return nullptr;
1000 }
1001
1002 const SDNodeFlags getFlags() const { return Flags; }
1003 void setFlags(SDNodeFlags NewFlags) { Flags = NewFlags; }
1004 bool isFast() { return Flags.isFast(); }
1005
1006 /// Clear any flags in this node that aren't also set in Flags.
1007 /// If Flags is not in a defined state then this has no effect.
1008 void intersectFlagsWith(const SDNodeFlags Flags);
1009
1010 /// Return the number of values defined/returned by this operator.
1011 unsigned getNumValues() const { return NumValues; }
1012
1013 /// Return the type of a specified result.
1014 EVT getValueType(unsigned ResNo) const {
1015 assert(ResNo < NumValues && "Illegal result number!")((ResNo < NumValues && "Illegal result number!") ?
static_cast<void> (0) : __assert_fail ("ResNo < NumValues && \"Illegal result number!\""
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/CodeGen/SelectionDAGNodes.h"
, 1015, __PRETTY_FUNCTION__))
;
1016 return ValueList[ResNo];
1017 }
1018
1019 /// Return the type of a specified result as a simple type.
1020 MVT getSimpleValueType(unsigned ResNo) const {
1021 return getValueType(ResNo).getSimpleVT();
1022 }
1023
1024 /// Returns MVT::getSizeInBits(getValueType(ResNo)).
1025 unsigned getValueSizeInBits(unsigned ResNo) const {
1026 return getValueType(ResNo).getSizeInBits();
1027 }
1028
1029 using value_iterator = const EVT *;
1030
1031 value_iterator value_begin() const { return ValueList; }
1032 value_iterator value_end() const { return ValueList+NumValues; }
1033
1034 /// Return the opcode of this operation for printing.
1035 std::string getOperationName(const SelectionDAG *G = nullptr) const;
1036 static const char* getIndexedModeName(ISD::MemIndexedMode AM);
1037 void print_types(raw_ostream &OS, const SelectionDAG *G) const;
1038 void print_details(raw_ostream &OS, const SelectionDAG *G) const;
1039 void print(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
1040 void printr(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
1041
1042 /// Print a SelectionDAG node and all children down to
1043 /// the leaves. The given SelectionDAG allows target-specific nodes
1044 /// to be printed in human-readable form. Unlike printr, this will
1045 /// print the whole DAG, including children that appear multiple
1046 /// times.
1047 ///
1048 void printrFull(raw_ostream &O, const SelectionDAG *G = nullptr) const;
1049
1050 /// Print a SelectionDAG node and children up to
1051 /// depth "depth." The given SelectionDAG allows target-specific
1052 /// nodes to be printed in human-readable form. Unlike printr, this
1053 /// will print children that appear multiple times wherever they are
1054 /// used.
1055 ///
1056 void printrWithDepth(raw_ostream &O, const SelectionDAG *G = nullptr,
1057 unsigned depth = 100) const;
1058
1059 /// Dump this node, for debugging.
1060 void dump() const;
1061
1062 /// Dump (recursively) this node and its use-def subgraph.
1063 void dumpr() const;
1064
1065 /// Dump this node, for debugging.
1066 /// The given SelectionDAG allows target-specific nodes to be printed
1067 /// in human-readable form.
1068 void dump(const SelectionDAG *G) const;
1069
1070 /// Dump (recursively) this node and its use-def subgraph.
1071 /// The given SelectionDAG allows target-specific nodes to be printed
1072 /// in human-readable form.
1073 void dumpr(const SelectionDAG *G) const;
1074
1075 /// printrFull to dbgs(). The given SelectionDAG allows
1076 /// target-specific nodes to be printed in human-readable form.
1077 /// Unlike dumpr, this will print the whole DAG, including children
1078 /// that appear multiple times.
1079 void dumprFull(const SelectionDAG *G = nullptr) const;
1080
1081 /// printrWithDepth to dbgs(). The given
1082 /// SelectionDAG allows target-specific nodes to be printed in
1083 /// human-readable form. Unlike dumpr, this will print children
1084 /// that appear multiple times wherever they are used.
1085 ///
1086 void dumprWithDepth(const SelectionDAG *G = nullptr,
1087 unsigned depth = 100) const;
1088
1089 /// Gather unique data for the node.
1090 void Profile(FoldingSetNodeID &ID) const;
1091
1092 /// This method should only be used by the SDUse class.
1093 void addUse(SDUse &U) { U.addToList(&UseList); }
1094
1095protected:
1096 static SDVTList getSDVTList(EVT VT) {
1097 SDVTList Ret = { getValueTypeList(VT), 1 };
1098 return Ret;
1099 }
1100
1101 /// Create an SDNode.
1102 ///
1103 /// SDNodes are created without any operands, and never own the operand
1104 /// storage. To add operands, see SelectionDAG::createOperands.
1105 SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs)
1106 : NodeType(Opc), ValueList(VTs.VTs), NumValues(VTs.NumVTs),
1107 IROrder(Order), debugLoc(std::move(dl)) {
1108 memset(&RawSDNodeBits, 0, sizeof(RawSDNodeBits));
1109 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor")((debugLoc.hasTrivialDestructor() && "Expected trivial destructor"
) ? static_cast<void> (0) : __assert_fail ("debugLoc.hasTrivialDestructor() && \"Expected trivial destructor\""
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/CodeGen/SelectionDAGNodes.h"
, 1109, __PRETTY_FUNCTION__))
;
1110 assert(NumValues == VTs.NumVTs &&((NumValues == VTs.NumVTs && "NumValues wasn't wide enough for its operands!"
) ? static_cast<void> (0) : __assert_fail ("NumValues == VTs.NumVTs && \"NumValues wasn't wide enough for its operands!\""
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/CodeGen/SelectionDAGNodes.h"
, 1111, __PRETTY_FUNCTION__))
1111 "NumValues wasn't wide enough for its operands!")((NumValues == VTs.NumVTs && "NumValues wasn't wide enough for its operands!"
) ? static_cast<void> (0) : __assert_fail ("NumValues == VTs.NumVTs && \"NumValues wasn't wide enough for its operands!\""
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/CodeGen/SelectionDAGNodes.h"
, 1111, __PRETTY_FUNCTION__))
;
1112 }
1113
1114 /// Release the operands and set this node to have zero operands.
1115 void DropOperands();
1116};
1117
1118/// Wrapper class for IR location info (IR ordering and DebugLoc) to be passed
1119/// into SDNode creation functions.
1120/// When an SDNode is created from the DAGBuilder, the DebugLoc is extracted
1121/// from the original Instruction, and IROrder is the ordinal position of
1122/// the instruction.
1123/// When an SDNode is created after the DAG is being built, both DebugLoc and
1124/// the IROrder are propagated from the original SDNode.
1125/// So SDLoc class provides two constructors besides the default one, one to
1126/// be used by the DAGBuilder, the other to be used by others.
1127class SDLoc {
1128private:
1129 DebugLoc DL;
1130 int IROrder = 0;
1131
1132public:
1133 SDLoc() = default;
1134 SDLoc(const SDNode *N) : DL(N->getDebugLoc()), IROrder(N->getIROrder()) {}
1135 SDLoc(const SDValue V) : SDLoc(V.getNode()) {}
1136 SDLoc(const Instruction *I, int Order) : IROrder(Order) {
1137 assert(Order >= 0 && "bad IROrder")((Order >= 0 && "bad IROrder") ? static_cast<void
> (0) : __assert_fail ("Order >= 0 && \"bad IROrder\""
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/CodeGen/SelectionDAGNodes.h"
, 1137, __PRETTY_FUNCTION__))
;
1138 if (I)
1139 DL = I->getDebugLoc();
1140 }
1141
1142 unsigned getIROrder() const { return IROrder; }
1143 const DebugLoc &getDebugLoc() const { return DL; }
1144};
1145
1146// Define inline functions from the SDValue class.
1147
1148inline SDValue::SDValue(SDNode *node, unsigned resno)
1149 : Node(node), ResNo(resno) {
1150 // Explicitly check for !ResNo to avoid use-after-free, because there are
1151 // callers that use SDValue(N, 0) with a deleted N to indicate successful
1152 // combines.
1153 assert((!Node || !ResNo || ResNo < Node->getNumValues()) &&(((!Node || !ResNo || ResNo < Node->getNumValues()) &&
"Invalid result number for the given node!") ? static_cast<
void> (0) : __assert_fail ("(!Node || !ResNo || ResNo < Node->getNumValues()) && \"Invalid result number for the given node!\""
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/CodeGen/SelectionDAGNodes.h"
, 1154, __PRETTY_FUNCTION__))
1154 "Invalid result number for the given node!")(((!Node || !ResNo || ResNo < Node->getNumValues()) &&
"Invalid result number for the given node!") ? static_cast<
void> (0) : __assert_fail ("(!Node || !ResNo || ResNo < Node->getNumValues()) && \"Invalid result number for the given node!\""
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/CodeGen/SelectionDAGNodes.h"
, 1154, __PRETTY_FUNCTION__))
;
1155 assert(ResNo < -2U && "Cannot use result numbers reserved for DenseMaps.")((ResNo < -2U && "Cannot use result numbers reserved for DenseMaps."
) ? static_cast<void> (0) : __assert_fail ("ResNo < -2U && \"Cannot use result numbers reserved for DenseMaps.\""
, "/build/llvm-toolchain-snapshot-10~svn374877/include/llvm/CodeGen/SelectionDAGNodes.h"
, 1155, __PRETTY_FUNCTION__))
;
1156}
1157
1158inline unsigned SDValue::getOpcode() const {
1159 return Node->getOpcode();
1160}
1161
1162inline EVT SDValue::getValueType() const {
1163 return Node->getValueType(ResNo);
16
Called C++ object pointer is null
1164}
1165
1166inline unsigned SDValue::getNumOperands() const {
1167 return Node->getNumOperands();
1168}
1169
1170inline const SDValue &SDValue::getOperand(unsigned i) const {
1171 return Node->getOperand(i);
1172}
1173
1174inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
1175 return Node->getConstantOperandVal(i);
1176}
1177
1178inline const APInt &SDValue::getConstantOperandAPInt(unsigned i) const {
1179 return Node->getConstantOperandAPInt(i);
1180}
1181
1182inline bool SDValue::isTargetOpcode() const {
1183 return Node->isTargetOpcode();
1184}
1185
1186inline bool SDValue::isTargetMemoryOpcode() const {
1187 return Node->isTargetMemoryOpcode();
1188}
1189
1190inline bool SDValue::isMachineOpcode() const {
1191 return Node->isMachineOpcode();
1192}
1193
1194inline unsigned SDValue::getMachineOpcode() const {
1195 return Node->getMachineOpcode();
1196}
1197
1198inline bool SDValue::isUndef() const {
1199 return Node->isUndef();
1200}
1201
1202inline bool SDValue::use_empty() const {
1203 return !Node->hasAnyUseOfValue(ResNo);
1204}
1205
1206inline bool SDValue::hasOneUse() const {
1207 return Node->hasNUsesOfValue(1, ResNo);
1208}
1209
1210inline const DebugLoc &SDValue::getDebugLoc() const {
1211 return Node->getDebugLoc();
1212}
1213
1214inline void SDValue::dump() const {
1215 return Node->dump();
1216}
1217
1218inline void SDValue::dump(const SelectionDAG *G) const {
1219 return Node->dump(G);
1220}
1221
1222inline void SDValue::dumpr() const {
1223 return Node->dumpr();
1224}
1225
1226inline void SDValue::dumpr(const SelectionDAG *G) const {
1227 return Node->dumpr(G);
1228}
1229
1230// Define inline functions from the SDUse class.
1231
1232inline void SDUse::set(const SDValue &V) {
1233 if (Val.getNode()) removeFromList();
1234 Val = V;
1235 if (V.getNode()) V.getNode()->addUse(*this);
1236}
1237
1238inline void SDUse::setInitial(const SDValue &V) {
1239 Val = V;
1240 V.getNode()->addUse(*this);
1241}
1242
1243inline void SDUse::setNode(SDNode *N) {
1244 if (Val.getNode()) removeFromList();
1245 Val.setNode(N);
1246 if (N) N->addUse(*this);
1247}
1248
1249/// This class is used to form a handle around another node that
1250/// is persistent and is updated across invocations of replaceAllUsesWith on its
1251/// operand. This node should be directly created by end-users and not added to
1252/// the AllNodes list.
1253class HandleSDNode : public SDNode {
1254 SDUse Op;
1255
1256public:
1257 explicit HandleSDNode(SDValue X)
1258 : SDNode(ISD::HANDLENODE, 0, DebugLoc(), getSDVTList(MVT::Other)) {
1259 // HandleSDNodes are never inserted into the DAG, so they won't be
1260 // auto-numbered. Use ID 65535 as a sentinel.
1261 PersistentId = 0xffff;
1262
1263 // Manually set up the operand list. This node type is special in that it's
1264 // always stack allocated and SelectionDAG does not manage its operands.
1265 // TODO: This should either (a) not be in the SDNode hierarchy, or (b) not
1266 // be so special.
1267 Op.setUser(this);
1268 Op.setInitial(X);
1269 NumOperands = 1;
1270 OperandList = &Op;
1271 }
1272 ~HandleSDNode();
1273
1274 const SDValue &getValue() const { return Op; }
1275};
1276
1277class AddrSpaceCastSDNode : public SDNode {
1278private:
1279 unsigned SrcAddrSpace;
1280 unsigned DestAddrSpace;
1281
1282public:
1283 AddrSpaceCastSDNode(unsigned Order, const DebugLoc &dl, EVT VT,
1284 unsigned SrcAS, unsigned DestAS);
1285
1286 unsigned getSrcAddressSpace() const { return SrcAddrSpace; }
1287 unsigned getDestAddressSpace() const { return DestAddrSpace; }
1288
1289 static bool classof(const SDNode *N) {
1290 return N->getOpcode() == ISD::ADDRSPACECAST;
1291 }
1292};
1293
1294/// This is an abstract virtual class for memory operations.
1295class MemSDNode : public SDNode {
1296private:
1297 // VT of in-memory value.
1298 EVT MemoryVT;
1299
1300protected:
1301 /// Memory reference information.
1302 MachineMemOperand *MMO;
1303
1304public:
1305 MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs,
1306 EVT memvt, MachineMemOperand *MMO);
1307
1308 bool readMem() const { return MMO->isLoad(); }
1309 bool writeMem() const { return MMO->isStore(); }
1310
1311 /// Returns alignment and volatility of the memory access
1312 unsigned getOriginalAlignment() const {
1313 return MMO->getBaseAlignment();
1314 }
1315 unsigned getAlignment() const {
1316 return MMO->getAlignment();
1317 }
1318
1319 /// Return the SubclassData value, without HasDebugValue. This contains an
1320 /// encoding of the volatile flag, as well as bits used by subclasses. This
1321 /// function should only be used to compute a FoldingSetNodeID value.
1322 /// The HasDebugValue bit is masked out because CSE map needs to match
1323 /// nodes with debug info with nodes without debug info. Same is about
1324 /// isDivergent bit.
1325 unsigned getRawSubclassData() const {
1326 uint16_t Data;
1327 union {
1328 char RawSDNodeBits[sizeof(uint16_t)];
1329 SDNodeBitfields SDNodeBits;
1330 };
1331 memcpy(&RawSDNodeBits, &this->RawSDNodeBits, sizeof(this->RawSDNodeBits));
1332 SDNodeBits.HasDebugValue = 0;
1333 SDNodeBits.IsDivergent = false;
1334 memcpy(&Data, &RawSDNodeBits, sizeof(RawSDNodeBits));
1335 return Data;
1336 }
1337
1338 bool isVolatile() const { return MemSDNodeBits.IsVolatile; }
1339 bool isNonTemporal() const { return MemSDNodeBits.IsNonTemporal; }
1340 bool isDereferenceable() const { return MemSDNodeBits.IsDereferenceable; }
1341 bool isInvariant() const { return MemSDNodeBits.IsInvariant; }
1342
1343 // Returns the offset from the location of the access.
1344 int64_t getSrcValueOffset() const { return MMO->getOffset(); }
1345
1346 /// Returns the AA info that describes the dereference.
1347 AAMDNodes getAAInfo() const { return MMO->getAAInfo(); }
1348
1349 /// Returns the Ranges that describes the dereference.
1350 const MDNode *getRanges() const { return MMO->getRanges(); }
1351
1352 /// Returns the synchronization scope ID for this memory operation.
1353 SyncScope::ID getSyncScopeID() const { return MMO->getSyncScopeID(); }
1354
1355 /// Return the atomic ordering requirements for this memory operation. For
1356 /// cmpxchg atomic operations, return the atomic ordering requirements when
1357 /// store occurs.
1358 AtomicOrdering getOrdering() const { return MMO->getOrdering(); }
1359
1360 /// Return true if the memory operation ordering is Unordered or higher.
1361 bool isAtomic() const { return MMO->isAtomic(); }
1362
1363 /// Returns true if the memory operation doesn't imply any ordering
1364 /// constraints on surrounding memory operations beyond the normal memory
1365 /// aliasing rules.
1366 bool isUnordered() const { return MMO->isUnordered(); }
1367
1368 /// Returns true if the memory operation is neither atomic or volatile.
1369 bool isSimple() const { return !isAtomic() && !isVolatile(); }
1370
1371 /// Return the type of the in-memory value.
1372 EVT getMemoryVT() const { return MemoryVT; }
1373
1374 /// Return a MachineMemOperand object describing the memory
1375 /// reference performed by operation.
1376 MachineMemOperand *getMemOperand() const { return MMO; }
1377
1378 const MachinePointerInfo &getPointerInfo() const {
1379 return MMO->getPointerInfo();
1380 }
1381
1382 /// Return the address space for the associated pointer
1383 unsigned getAddressSpace() const {
1384 return getPointerInfo().getAddrSpace();
1385 }
1386
1387 /// Update this MemSDNode's MachineMemOperand information
1388 /// to reflect the alignment of NewMMO, if it has a greater alignment.
1389 /// This must only be used when the new alignment applies to all users of
1390 /// this MachineMemOperand.
1391 void refineAlignment(const MachineMemOperand *NewMMO) {
1392 MMO->refineAlignment(NewMMO);
1393 }
1394
1395 const SDValue &getChain() const { return getOperand(0); }
1396 const SDValue &getBasePtr() const {
1397 return getOperand(getOpcode() == ISD::STORE ? 2 : 1);
1398 }
1399
1400 // Methods to support isa and dyn_cast
1401 static bool classof(const SDNode *N) {
1402 // For some targets, we lower some target intrinsics to a MemIntrinsicNode
1403 // with either an intrinsic or a target opcode.
1404 return N->getOpcode() == ISD::LOAD ||