blob: 7f3eb4107ffe842e0e5980da0a008b60492e4dda [file] [log] [blame]
Eugene Zelenkofb7f7922017-09-21 23:20:16 +00001//===- TargetLoweringBase.cpp - Implement the TargetLoweringBase class ----===//
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This implements the TargetLoweringBase class.
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramer56b31bd2013-01-11 20:05:37 +000013#include "llvm/ADT/BitVector.h"
14#include "llvm/ADT/STLExtras.h"
Eugene Zelenkofb7f7922017-09-21 23:20:16 +000015#include "llvm/ADT/SmallVector.h"
Sanjay Patel0051efc2016-10-20 16:55:45 +000016#include "llvm/ADT/StringExtras.h"
Eugene Zelenkofb7f7922017-09-21 23:20:16 +000017#include "llvm/ADT/StringRef.h"
Paul Redmondf29ddfe2013-02-15 18:45:18 +000018#include "llvm/ADT/Triple.h"
Eugene Zelenkofb7f7922017-09-21 23:20:16 +000019#include "llvm/ADT/Twine.h"
Benjamin Kramer56b31bd2013-01-11 20:05:37 +000020#include "llvm/CodeGen/Analysis.h"
Eugene Zelenkofb7f7922017-09-21 23:20:16 +000021#include "llvm/CodeGen/ISDOpcodes.h"
22#include "llvm/CodeGen/MachineBasicBlock.h"
Benjamin Kramer56b31bd2013-01-11 20:05:37 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenkofb7f7922017-09-21 23:20:16 +000025#include "llvm/CodeGen/MachineInstr.h"
Lang Hames39609992013-11-29 03:07:54 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenkofb7f7922017-09-21 23:20:16 +000027#include "llvm/CodeGen/MachineMemOperand.h"
28#include "llvm/CodeGen/MachineOperand.h"
Matthias Braun744c2152017-04-28 20:25:05 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenkofb7f7922017-09-21 23:20:16 +000030#include "llvm/CodeGen/RuntimeLibcalls.h"
Lang Hames39609992013-11-29 03:07:54 +000031#include "llvm/CodeGen/StackMaps.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000032#include "llvm/CodeGen/TargetLowering.h"
33#include "llvm/CodeGen/TargetOpcodes.h"
34#include "llvm/CodeGen/TargetRegisterInfo.h"
Craig Topper2fa14362018-03-29 17:21:10 +000035#include "llvm/CodeGen/ValueTypes.h"
Eugene Zelenkofb7f7922017-09-21 23:20:16 +000036#include "llvm/IR/Attributes.h"
37#include "llvm/IR/CallingConv.h"
Benjamin Kramer56b31bd2013-01-11 20:05:37 +000038#include "llvm/IR/DataLayout.h"
39#include "llvm/IR/DerivedTypes.h"
Eugene Zelenkofb7f7922017-09-21 23:20:16 +000040#include "llvm/IR/Function.h"
41#include "llvm/IR/GlobalValue.h"
Benjamin Kramer56b31bd2013-01-11 20:05:37 +000042#include "llvm/IR/GlobalVariable.h"
Eugene Zelenkofb7f7922017-09-21 23:20:16 +000043#include "llvm/IR/IRBuilder.h"
44#include "llvm/IR/Module.h"
45#include "llvm/IR/Type.h"
Sanjay Pateld66607b2016-04-26 17:11:17 +000046#include "llvm/Support/BranchProbability.h"
Eugene Zelenkofb7f7922017-09-21 23:20:16 +000047#include "llvm/Support/Casting.h"
Benjamin Kramer56b31bd2013-01-11 20:05:37 +000048#include "llvm/Support/CommandLine.h"
Eugene Zelenkofb7f7922017-09-21 23:20:16 +000049#include "llvm/Support/Compiler.h"
Benjamin Kramer56b31bd2013-01-11 20:05:37 +000050#include "llvm/Support/ErrorHandling.h"
David Blaikie13e77db2018-03-23 23:58:25 +000051#include "llvm/Support/MachineValueType.h"
Benjamin Kramer56b31bd2013-01-11 20:05:37 +000052#include "llvm/Support/MathExtras.h"
Benjamin Kramer56b31bd2013-01-11 20:05:37 +000053#include "llvm/Target/TargetMachine.h"
Eugene Zelenkofb7f7922017-09-21 23:20:16 +000054#include <algorithm>
55#include <cassert>
Eugene Zelenkofb7f7922017-09-21 23:20:16 +000056#include <cstddef>
57#include <cstdint>
David Blaikieb3bde2e2017-11-17 01:07:10 +000058#include <cstring>
Eugene Zelenkofb7f7922017-09-21 23:20:16 +000059#include <iterator>
60#include <string>
61#include <tuple>
62#include <utility>
63
Benjamin Kramer56b31bd2013-01-11 20:05:37 +000064using namespace llvm;
65
Sanjay Patel943829a2015-07-01 18:10:20 +000066static cl::opt<bool> JumpIsExpensiveOverride(
67 "jump-is-expensive", cl::init(false),
68 cl::desc("Do not create extra branches to split comparison logic."),
69 cl::Hidden);
70
Evandro Menezeseb97e352016-10-25 19:53:51 +000071static cl::opt<unsigned> MinimumJumpTableEntries
72 ("min-jump-table-entries", cl::init(4), cl::Hidden,
73 cl::desc("Set minimum number of entries to use a jump table."));
74
Hans Wennborg3740ae32019-09-27 09:54:26 +000075static cl::opt<unsigned> MaximumJumpTableSize
76 ("max-jump-table-size", cl::init(UINT_MAX), cl::Hidden,
77 cl::desc("Set maximum size of jump tables."));
Evandro Menezese45de8a2016-09-26 15:32:33 +000078
Jun Bum Lim919f9e82017-04-28 16:04:03 +000079/// Minimum jump table density for normal functions.
80static cl::opt<unsigned>
81 JumpTableDensity("jump-table-density", cl::init(10), cl::Hidden,
82 cl::desc("Minimum density for building a jump table in "
83 "a normal function"));
84
85/// Minimum jump table density for -Os or -Oz functions.
86static cl::opt<unsigned> OptsizeJumpTableDensity(
87 "optsize-jump-table-density", cl::init(40), cl::Hidden,
88 cl::desc("Minimum density for building a jump table in "
89 "an optsize function"));
90
Matthias Brauna4852d2c2017-12-18 23:19:42 +000091static bool darwinHasSinCos(const Triple &TT) {
92 assert(TT.isOSDarwin() && "should be called with darwin triple");
Matthias Braund2d7fb62017-12-19 20:24:12 +000093 // Don't bother with 32 bit x86.
94 if (TT.getArch() == Triple::x86)
95 return false;
96 // Macos < 10.9 has no sincos_stret.
Matthias Brauna4852d2c2017-12-18 23:19:42 +000097 if (TT.isMacOSX())
98 return !TT.isMacOSXVersionLT(10, 9) && TT.isArch64Bit();
Matthias Braun02820912017-12-18 23:33:28 +000099 // iOS < 7.0 has no sincos_stret.
Matthias Brauna4852d2c2017-12-18 23:19:42 +0000100 if (TT.isiOS())
Matthias Braun02820912017-12-18 23:33:28 +0000101 return !TT.isOSVersionLT(7, 0);
102 // Any other darwin such as WatchOS/TvOS is new enough.
Matthias Brauna4852d2c2017-12-18 23:19:42 +0000103 return true;
104}
105
Sanjay Pateld66607b2016-04-26 17:11:17 +0000106// Although this default value is arbitrary, it is not random. It is assumed
107// that a condition that evaluates the same way by a higher percentage than this
108// is best represented as control flow. Therefore, the default value N should be
109// set such that the win from N% correct executions is greater than the loss
110// from (100 - N)% mispredicted executions for the majority of intended targets.
111static cl::opt<int> MinPercentageForPredictableBranch(
112 "min-predictable-branch", cl::init(99),
113 cl::desc("Minimum percentage (0-100) that a condition must be either true "
114 "or false to assume that the condition is predictable"),
115 cl::Hidden);
116
Matthias Brauna4852d2c2017-12-18 23:19:42 +0000117void TargetLoweringBase::InitLibcalls(const Triple &TT) {
Derek Schuff36454af2017-07-19 21:53:30 +0000118#define HANDLE_LIBCALL(code, name) \
Matthias Brauna4852d2c2017-12-18 23:19:42 +0000119 setLibcallName(RTLIB::code, name);
Peter Collingbournee06bac42018-07-24 19:34:37 +0000120#include "llvm/IR/RuntimeLibcalls.def"
Derek Schuff36454af2017-07-19 21:53:30 +0000121#undef HANDLE_LIBCALL
Matthias Braun92de8b22017-12-19 00:20:33 +0000122 // Initialize calling conventions to their default.
123 for (int LC = 0; LC < RTLIB::UNKNOWN_LIBCALL; ++LC)
124 setLibcallCallingConv((RTLIB::Libcall)LC, CallingConv::C);
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000125
Fangrui Song335f9552019-07-15 05:02:32 +0000126 // For IEEE quad-precision libcall names, PPC uses "kf" instead of "tf".
127 if (TT.getArch() == Triple::ppc || TT.isPPC64()) {
128 setLibcallName(RTLIB::ADD_F128, "__addkf3");
129 setLibcallName(RTLIB::SUB_F128, "__subkf3");
130 setLibcallName(RTLIB::MUL_F128, "__mulkf3");
131 setLibcallName(RTLIB::DIV_F128, "__divkf3");
132 setLibcallName(RTLIB::FPEXT_F32_F128, "__extendsfkf2");
133 setLibcallName(RTLIB::FPEXT_F64_F128, "__extenddfkf2");
134 setLibcallName(RTLIB::FPROUND_F128_F32, "__trunckfsf2");
135 setLibcallName(RTLIB::FPROUND_F128_F64, "__trunckfdf2");
136 setLibcallName(RTLIB::FPTOSINT_F128_I32, "__fixkfsi");
137 setLibcallName(RTLIB::FPTOSINT_F128_I64, "__fixkfdi");
138 setLibcallName(RTLIB::FPTOUINT_F128_I32, "__fixunskfsi");
139 setLibcallName(RTLIB::FPTOUINT_F128_I64, "__fixunskfdi");
140 setLibcallName(RTLIB::SINTTOFP_I32_F128, "__floatsikf");
141 setLibcallName(RTLIB::SINTTOFP_I64_F128, "__floatdikf");
142 setLibcallName(RTLIB::UINTTOFP_I32_F128, "__floatunsikf");
143 setLibcallName(RTLIB::UINTTOFP_I64_F128, "__floatundikf");
144 setLibcallName(RTLIB::OEQ_F128, "__eqkf2");
145 setLibcallName(RTLIB::UNE_F128, "__nekf2");
146 setLibcallName(RTLIB::OGE_F128, "__gekf2");
147 setLibcallName(RTLIB::OLT_F128, "__ltkf2");
148 setLibcallName(RTLIB::OLE_F128, "__lekf2");
149 setLibcallName(RTLIB::OGT_F128, "__gtkf2");
150 setLibcallName(RTLIB::UO_F128, "__unordkf2");
151 setLibcallName(RTLIB::O_F128, "__unordkf2");
152 }
153
Derek Schuff36454af2017-07-19 21:53:30 +0000154 // A few names are different on particular architectures or environments.
James Y Knight7873fb92016-04-12 22:32:47 +0000155 if (TT.isOSDarwin()) {
156 // For f16/f32 conversions, Darwin uses the standard naming scheme, instead
157 // of the gnueabi-style __gnu_*_ieee.
158 // FIXME: What about other targets?
Matthias Brauna4852d2c2017-12-18 23:19:42 +0000159 setLibcallName(RTLIB::FPEXT_F16_F32, "__extendhfsf2");
160 setLibcallName(RTLIB::FPROUND_F32_F16, "__truncsfhf2");
Matthias Brauna92cecf2017-12-18 23:14:28 +0000161
Matthias Braun725ad0e2018-01-10 20:49:57 +0000162 // Some darwins have an optimized __bzero/bzero function.
163 switch (TT.getArch()) {
164 case Triple::x86:
165 case Triple::x86_64:
166 if (TT.isMacOSX() && !TT.isMacOSXVersionLT(10, 6))
167 setLibcallName(RTLIB::BZERO, "__bzero");
168 break;
169 case Triple::aarch64:
Tim Northoverf1c28922019-09-12 10:22:23 +0000170 case Triple::aarch64_32:
Matthias Braun725ad0e2018-01-10 20:49:57 +0000171 setLibcallName(RTLIB::BZERO, "bzero");
172 break;
173 default:
174 break;
Matthias Braune29c0b82017-12-19 00:43:00 +0000175 }
Matthias Brauna4852d2c2017-12-18 23:19:42 +0000176
177 if (darwinHasSinCos(TT)) {
178 setLibcallName(RTLIB::SINCOS_STRET_F32, "__sincosf_stret");
179 setLibcallName(RTLIB::SINCOS_STRET_F64, "__sincos_stret");
180 if (TT.isWatchABI()) {
181 setLibcallCallingConv(RTLIB::SINCOS_STRET_F32,
182 CallingConv::ARM_AAPCS_VFP);
183 setLibcallCallingConv(RTLIB::SINCOS_STRET_F64,
184 CallingConv::ARM_AAPCS_VFP);
185 }
186 }
James Y Knight7873fb92016-04-12 22:32:47 +0000187 } else {
Matthias Brauna4852d2c2017-12-18 23:19:42 +0000188 setLibcallName(RTLIB::FPEXT_F16_F32, "__gnu_h2f_ieee");
189 setLibcallName(RTLIB::FPROUND_F32_F16, "__gnu_f2h_ieee");
James Y Knight7873fb92016-04-12 22:32:47 +0000190 }
James Y Knight19f6cce2016-04-12 20:18:48 +0000191
John Brawn83d74142018-09-18 13:18:21 +0000192 if (TT.isGNUEnvironment() || TT.isOSFuchsia() ||
193 (TT.isAndroid() && !TT.isAndroidVersionLT(9))) {
Matthias Brauna4852d2c2017-12-18 23:19:42 +0000194 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
195 setLibcallName(RTLIB::SINCOS_F64, "sincos");
196 setLibcallName(RTLIB::SINCOS_F80, "sincosl");
197 setLibcallName(RTLIB::SINCOS_F128, "sincosl");
198 setLibcallName(RTLIB::SINCOS_PPCF128, "sincosl");
Paul Redmondf29ddfe2013-02-15 18:45:18 +0000199 }
Michael Gottesman7dce16f2013-08-12 18:45:38 +0000200
Robert Lougher13190c42019-09-02 16:53:32 +0000201 if (TT.isPS4CPU()) {
202 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
203 setLibcallName(RTLIB::SINCOS_F64, "sincos");
204 }
205
Derek Schuff36454af2017-07-19 21:53:30 +0000206 if (TT.isOSOpenBSD()) {
Matthias Brauna4852d2c2017-12-18 23:19:42 +0000207 setLibcallName(RTLIB::STACKPROTECTOR_CHECK_FAIL, nullptr);
Ahmed Bougacha6402ad22015-05-14 01:00:51 +0000208 }
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000209}
210
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000211/// getFPEXT - Return the FPEXT_*_* value for the given types, or
212/// UNKNOWN_LIBCALL if there is none.
213RTLIB::Libcall RTLIB::getFPEXT(EVT OpVT, EVT RetVT) {
Tim Northoverf7a02c12014-07-21 09:13:56 +0000214 if (OpVT == MVT::f16) {
215 if (RetVT == MVT::f32)
216 return FPEXT_F16_F32;
217 } else if (OpVT == MVT::f32) {
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000218 if (RetVT == MVT::f64)
219 return FPEXT_F32_F64;
220 if (RetVT == MVT::f128)
221 return FPEXT_F32_F128;
Petar Jovanovic23e44f52016-02-04 14:43:50 +0000222 if (RetVT == MVT::ppcf128)
223 return FPEXT_F32_PPCF128;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000224 } else if (OpVT == MVT::f64) {
225 if (RetVT == MVT::f128)
226 return FPEXT_F64_F128;
Petar Jovanovic23e44f52016-02-04 14:43:50 +0000227 else if (RetVT == MVT::ppcf128)
228 return FPEXT_F64_PPCF128;
Benjamin Kramer8b1986b2018-01-17 22:29:16 +0000229 } else if (OpVT == MVT::f80) {
230 if (RetVT == MVT::f128)
231 return FPEXT_F80_F128;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000232 }
233
234 return UNKNOWN_LIBCALL;
235}
236
237/// getFPROUND - Return the FPROUND_*_* value for the given types, or
238/// UNKNOWN_LIBCALL if there is none.
239RTLIB::Libcall RTLIB::getFPROUND(EVT OpVT, EVT RetVT) {
Tim Northover84ce0a62014-07-17 11:12:12 +0000240 if (RetVT == MVT::f16) {
241 if (OpVT == MVT::f32)
242 return FPROUND_F32_F16;
243 if (OpVT == MVT::f64)
244 return FPROUND_F64_F16;
245 if (OpVT == MVT::f80)
246 return FPROUND_F80_F16;
247 if (OpVT == MVT::f128)
248 return FPROUND_F128_F16;
249 if (OpVT == MVT::ppcf128)
250 return FPROUND_PPCF128_F16;
251 } else if (RetVT == MVT::f32) {
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000252 if (OpVT == MVT::f64)
253 return FPROUND_F64_F32;
254 if (OpVT == MVT::f80)
255 return FPROUND_F80_F32;
256 if (OpVT == MVT::f128)
257 return FPROUND_F128_F32;
258 if (OpVT == MVT::ppcf128)
259 return FPROUND_PPCF128_F32;
260 } else if (RetVT == MVT::f64) {
261 if (OpVT == MVT::f80)
262 return FPROUND_F80_F64;
263 if (OpVT == MVT::f128)
264 return FPROUND_F128_F64;
265 if (OpVT == MVT::ppcf128)
266 return FPROUND_PPCF128_F64;
Benjamin Kramer8b1986b2018-01-17 22:29:16 +0000267 } else if (RetVT == MVT::f80) {
268 if (OpVT == MVT::f128)
269 return FPROUND_F128_F80;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000270 }
271
272 return UNKNOWN_LIBCALL;
273}
274
275/// getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or
276/// UNKNOWN_LIBCALL if there is none.
277RTLIB::Libcall RTLIB::getFPTOSINT(EVT OpVT, EVT RetVT) {
278 if (OpVT == MVT::f32) {
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000279 if (RetVT == MVT::i32)
280 return FPTOSINT_F32_I32;
281 if (RetVT == MVT::i64)
282 return FPTOSINT_F32_I64;
283 if (RetVT == MVT::i128)
284 return FPTOSINT_F32_I128;
285 } else if (OpVT == MVT::f64) {
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000286 if (RetVT == MVT::i32)
287 return FPTOSINT_F64_I32;
288 if (RetVT == MVT::i64)
289 return FPTOSINT_F64_I64;
290 if (RetVT == MVT::i128)
291 return FPTOSINT_F64_I128;
292 } else if (OpVT == MVT::f80) {
293 if (RetVT == MVT::i32)
294 return FPTOSINT_F80_I32;
295 if (RetVT == MVT::i64)
296 return FPTOSINT_F80_I64;
297 if (RetVT == MVT::i128)
298 return FPTOSINT_F80_I128;
299 } else if (OpVT == MVT::f128) {
300 if (RetVT == MVT::i32)
301 return FPTOSINT_F128_I32;
302 if (RetVT == MVT::i64)
303 return FPTOSINT_F128_I64;
304 if (RetVT == MVT::i128)
305 return FPTOSINT_F128_I128;
306 } else if (OpVT == MVT::ppcf128) {
307 if (RetVT == MVT::i32)
308 return FPTOSINT_PPCF128_I32;
309 if (RetVT == MVT::i64)
310 return FPTOSINT_PPCF128_I64;
311 if (RetVT == MVT::i128)
312 return FPTOSINT_PPCF128_I128;
313 }
314 return UNKNOWN_LIBCALL;
315}
316
317/// getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or
318/// UNKNOWN_LIBCALL if there is none.
319RTLIB::Libcall RTLIB::getFPTOUINT(EVT OpVT, EVT RetVT) {
320 if (OpVT == MVT::f32) {
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000321 if (RetVT == MVT::i32)
322 return FPTOUINT_F32_I32;
323 if (RetVT == MVT::i64)
324 return FPTOUINT_F32_I64;
325 if (RetVT == MVT::i128)
326 return FPTOUINT_F32_I128;
327 } else if (OpVT == MVT::f64) {
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000328 if (RetVT == MVT::i32)
329 return FPTOUINT_F64_I32;
330 if (RetVT == MVT::i64)
331 return FPTOUINT_F64_I64;
332 if (RetVT == MVT::i128)
333 return FPTOUINT_F64_I128;
334 } else if (OpVT == MVT::f80) {
335 if (RetVT == MVT::i32)
336 return FPTOUINT_F80_I32;
337 if (RetVT == MVT::i64)
338 return FPTOUINT_F80_I64;
339 if (RetVT == MVT::i128)
340 return FPTOUINT_F80_I128;
341 } else if (OpVT == MVT::f128) {
342 if (RetVT == MVT::i32)
343 return FPTOUINT_F128_I32;
344 if (RetVT == MVT::i64)
345 return FPTOUINT_F128_I64;
346 if (RetVT == MVT::i128)
347 return FPTOUINT_F128_I128;
348 } else if (OpVT == MVT::ppcf128) {
349 if (RetVT == MVT::i32)
350 return FPTOUINT_PPCF128_I32;
351 if (RetVT == MVT::i64)
352 return FPTOUINT_PPCF128_I64;
353 if (RetVT == MVT::i128)
354 return FPTOUINT_PPCF128_I128;
355 }
356 return UNKNOWN_LIBCALL;
357}
358
359/// getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or
360/// UNKNOWN_LIBCALL if there is none.
361RTLIB::Libcall RTLIB::getSINTTOFP(EVT OpVT, EVT RetVT) {
362 if (OpVT == MVT::i32) {
363 if (RetVT == MVT::f32)
364 return SINTTOFP_I32_F32;
365 if (RetVT == MVT::f64)
366 return SINTTOFP_I32_F64;
367 if (RetVT == MVT::f80)
368 return SINTTOFP_I32_F80;
369 if (RetVT == MVT::f128)
370 return SINTTOFP_I32_F128;
371 if (RetVT == MVT::ppcf128)
372 return SINTTOFP_I32_PPCF128;
373 } else if (OpVT == MVT::i64) {
374 if (RetVT == MVT::f32)
375 return SINTTOFP_I64_F32;
376 if (RetVT == MVT::f64)
377 return SINTTOFP_I64_F64;
378 if (RetVT == MVT::f80)
379 return SINTTOFP_I64_F80;
380 if (RetVT == MVT::f128)
381 return SINTTOFP_I64_F128;
382 if (RetVT == MVT::ppcf128)
383 return SINTTOFP_I64_PPCF128;
384 } else if (OpVT == MVT::i128) {
385 if (RetVT == MVT::f32)
386 return SINTTOFP_I128_F32;
387 if (RetVT == MVT::f64)
388 return SINTTOFP_I128_F64;
389 if (RetVT == MVT::f80)
390 return SINTTOFP_I128_F80;
391 if (RetVT == MVT::f128)
392 return SINTTOFP_I128_F128;
393 if (RetVT == MVT::ppcf128)
394 return SINTTOFP_I128_PPCF128;
395 }
396 return UNKNOWN_LIBCALL;
397}
398
399/// getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or
400/// UNKNOWN_LIBCALL if there is none.
401RTLIB::Libcall RTLIB::getUINTTOFP(EVT OpVT, EVT RetVT) {
402 if (OpVT == MVT::i32) {
403 if (RetVT == MVT::f32)
404 return UINTTOFP_I32_F32;
405 if (RetVT == MVT::f64)
406 return UINTTOFP_I32_F64;
407 if (RetVT == MVT::f80)
408 return UINTTOFP_I32_F80;
409 if (RetVT == MVT::f128)
410 return UINTTOFP_I32_F128;
411 if (RetVT == MVT::ppcf128)
412 return UINTTOFP_I32_PPCF128;
413 } else if (OpVT == MVT::i64) {
414 if (RetVT == MVT::f32)
415 return UINTTOFP_I64_F32;
416 if (RetVT == MVT::f64)
417 return UINTTOFP_I64_F64;
418 if (RetVT == MVT::f80)
419 return UINTTOFP_I64_F80;
420 if (RetVT == MVT::f128)
421 return UINTTOFP_I64_F128;
422 if (RetVT == MVT::ppcf128)
423 return UINTTOFP_I64_PPCF128;
424 } else if (OpVT == MVT::i128) {
425 if (RetVT == MVT::f32)
426 return UINTTOFP_I128_F32;
427 if (RetVT == MVT::f64)
428 return UINTTOFP_I128_F64;
429 if (RetVT == MVT::f80)
430 return UINTTOFP_I128_F80;
431 if (RetVT == MVT::f128)
432 return UINTTOFP_I128_F128;
433 if (RetVT == MVT::ppcf128)
434 return UINTTOFP_I128_PPCF128;
435 }
436 return UNKNOWN_LIBCALL;
437}
438
James Y Knightf44fc522016-03-16 22:12:04 +0000439RTLIB::Libcall RTLIB::getSYNC(unsigned Opc, MVT VT) {
Benjamin Kramerc54c38e2015-03-05 20:04:29 +0000440#define OP_TO_LIBCALL(Name, Enum) \
441 case Name: \
442 switch (VT.SimpleTy) { \
443 default: \
444 return UNKNOWN_LIBCALL; \
445 case MVT::i8: \
446 return Enum##_1; \
447 case MVT::i16: \
448 return Enum##_2; \
449 case MVT::i32: \
450 return Enum##_4; \
451 case MVT::i64: \
452 return Enum##_8; \
453 case MVT::i128: \
454 return Enum##_16; \
455 }
456
457 switch (Opc) {
458 OP_TO_LIBCALL(ISD::ATOMIC_SWAP, SYNC_LOCK_TEST_AND_SET)
459 OP_TO_LIBCALL(ISD::ATOMIC_CMP_SWAP, SYNC_VAL_COMPARE_AND_SWAP)
460 OP_TO_LIBCALL(ISD::ATOMIC_LOAD_ADD, SYNC_FETCH_AND_ADD)
461 OP_TO_LIBCALL(ISD::ATOMIC_LOAD_SUB, SYNC_FETCH_AND_SUB)
462 OP_TO_LIBCALL(ISD::ATOMIC_LOAD_AND, SYNC_FETCH_AND_AND)
463 OP_TO_LIBCALL(ISD::ATOMIC_LOAD_OR, SYNC_FETCH_AND_OR)
464 OP_TO_LIBCALL(ISD::ATOMIC_LOAD_XOR, SYNC_FETCH_AND_XOR)
465 OP_TO_LIBCALL(ISD::ATOMIC_LOAD_NAND, SYNC_FETCH_AND_NAND)
466 OP_TO_LIBCALL(ISD::ATOMIC_LOAD_MAX, SYNC_FETCH_AND_MAX)
467 OP_TO_LIBCALL(ISD::ATOMIC_LOAD_UMAX, SYNC_FETCH_AND_UMAX)
468 OP_TO_LIBCALL(ISD::ATOMIC_LOAD_MIN, SYNC_FETCH_AND_MIN)
469 OP_TO_LIBCALL(ISD::ATOMIC_LOAD_UMIN, SYNC_FETCH_AND_UMIN)
470 }
471
472#undef OP_TO_LIBCALL
473
474 return UNKNOWN_LIBCALL;
475}
476
Daniel Neilson3faabbb2017-06-16 14:43:59 +0000477RTLIB::Libcall RTLIB::getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize) {
Igor Laevsky4f31e522016-12-29 14:31:07 +0000478 switch (ElementSize) {
479 case 1:
Daniel Neilson3faabbb2017-06-16 14:43:59 +0000480 return MEMCPY_ELEMENT_UNORDERED_ATOMIC_1;
Igor Laevsky4f31e522016-12-29 14:31:07 +0000481 case 2:
Daniel Neilson3faabbb2017-06-16 14:43:59 +0000482 return MEMCPY_ELEMENT_UNORDERED_ATOMIC_2;
Igor Laevsky4f31e522016-12-29 14:31:07 +0000483 case 4:
Daniel Neilson3faabbb2017-06-16 14:43:59 +0000484 return MEMCPY_ELEMENT_UNORDERED_ATOMIC_4;
Igor Laevsky4f31e522016-12-29 14:31:07 +0000485 case 8:
Daniel Neilson3faabbb2017-06-16 14:43:59 +0000486 return MEMCPY_ELEMENT_UNORDERED_ATOMIC_8;
Igor Laevsky4f31e522016-12-29 14:31:07 +0000487 case 16:
Daniel Neilson3faabbb2017-06-16 14:43:59 +0000488 return MEMCPY_ELEMENT_UNORDERED_ATOMIC_16;
Igor Laevsky4f31e522016-12-29 14:31:07 +0000489 default:
490 return UNKNOWN_LIBCALL;
491 }
Igor Laevsky4f31e522016-12-29 14:31:07 +0000492}
493
Daniel Neilson57226ef2017-07-12 15:25:26 +0000494RTLIB::Libcall RTLIB::getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize) {
495 switch (ElementSize) {
496 case 1:
497 return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_1;
498 case 2:
499 return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_2;
500 case 4:
501 return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_4;
502 case 8:
503 return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_8;
504 case 16:
505 return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_16;
506 default:
507 return UNKNOWN_LIBCALL;
508 }
509}
510
Daniel Neilson965613e2017-07-12 21:57:23 +0000511RTLIB::Libcall RTLIB::getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize) {
512 switch (ElementSize) {
513 case 1:
514 return MEMSET_ELEMENT_UNORDERED_ATOMIC_1;
515 case 2:
516 return MEMSET_ELEMENT_UNORDERED_ATOMIC_2;
517 case 4:
518 return MEMSET_ELEMENT_UNORDERED_ATOMIC_4;
519 case 8:
520 return MEMSET_ELEMENT_UNORDERED_ATOMIC_8;
521 case 16:
522 return MEMSET_ELEMENT_UNORDERED_ATOMIC_16;
523 default:
524 return UNKNOWN_LIBCALL;
525 }
526}
527
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000528/// InitCmpLibcallCCs - Set default comparison libcall CC.
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000529static void InitCmpLibcallCCs(ISD::CondCode *CCs) {
530 memset(CCs, ISD::SETCC_INVALID, sizeof(ISD::CondCode)*RTLIB::UNKNOWN_LIBCALL);
531 CCs[RTLIB::OEQ_F32] = ISD::SETEQ;
532 CCs[RTLIB::OEQ_F64] = ISD::SETEQ;
533 CCs[RTLIB::OEQ_F128] = ISD::SETEQ;
Petar Jovanovic23e44f52016-02-04 14:43:50 +0000534 CCs[RTLIB::OEQ_PPCF128] = ISD::SETEQ;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000535 CCs[RTLIB::UNE_F32] = ISD::SETNE;
536 CCs[RTLIB::UNE_F64] = ISD::SETNE;
537 CCs[RTLIB::UNE_F128] = ISD::SETNE;
Petar Jovanovic23e44f52016-02-04 14:43:50 +0000538 CCs[RTLIB::UNE_PPCF128] = ISD::SETNE;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000539 CCs[RTLIB::OGE_F32] = ISD::SETGE;
540 CCs[RTLIB::OGE_F64] = ISD::SETGE;
541 CCs[RTLIB::OGE_F128] = ISD::SETGE;
Petar Jovanovic23e44f52016-02-04 14:43:50 +0000542 CCs[RTLIB::OGE_PPCF128] = ISD::SETGE;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000543 CCs[RTLIB::OLT_F32] = ISD::SETLT;
544 CCs[RTLIB::OLT_F64] = ISD::SETLT;
545 CCs[RTLIB::OLT_F128] = ISD::SETLT;
Petar Jovanovic23e44f52016-02-04 14:43:50 +0000546 CCs[RTLIB::OLT_PPCF128] = ISD::SETLT;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000547 CCs[RTLIB::OLE_F32] = ISD::SETLE;
548 CCs[RTLIB::OLE_F64] = ISD::SETLE;
549 CCs[RTLIB::OLE_F128] = ISD::SETLE;
Petar Jovanovic23e44f52016-02-04 14:43:50 +0000550 CCs[RTLIB::OLE_PPCF128] = ISD::SETLE;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000551 CCs[RTLIB::OGT_F32] = ISD::SETGT;
552 CCs[RTLIB::OGT_F64] = ISD::SETGT;
553 CCs[RTLIB::OGT_F128] = ISD::SETGT;
Petar Jovanovic23e44f52016-02-04 14:43:50 +0000554 CCs[RTLIB::OGT_PPCF128] = ISD::SETGT;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000555 CCs[RTLIB::UO_F32] = ISD::SETNE;
556 CCs[RTLIB::UO_F64] = ISD::SETNE;
557 CCs[RTLIB::UO_F128] = ISD::SETNE;
Petar Jovanovic23e44f52016-02-04 14:43:50 +0000558 CCs[RTLIB::UO_PPCF128] = ISD::SETNE;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000559 CCs[RTLIB::O_F32] = ISD::SETEQ;
560 CCs[RTLIB::O_F64] = ISD::SETEQ;
561 CCs[RTLIB::O_F128] = ISD::SETEQ;
Petar Jovanovic23e44f52016-02-04 14:43:50 +0000562 CCs[RTLIB::O_PPCF128] = ISD::SETEQ;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000563}
564
Aditya Nandakumar30531552014-11-13 21:29:21 +0000565/// NOTE: The TargetMachine owns TLOF.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000566TargetLoweringBase::TargetLoweringBase(const TargetMachine &tm) : TM(tm) {
Bill Wendlingeb108ba2013-04-05 21:52:40 +0000567 initActions();
568
569 // Perform these initializations only once.
Zaara Syeda3a7578c2017-05-31 17:12:38 +0000570 MaxStoresPerMemset = MaxStoresPerMemcpy = MaxStoresPerMemmove =
571 MaxLoadsPerMemcmp = 8;
Sirish Pandecabe50a32018-05-16 15:36:52 +0000572 MaxGluedStoresPerMemcpy = 0;
Zaara Syeda3a7578c2017-05-31 17:12:38 +0000573 MaxStoresPerMemsetOptSize = MaxStoresPerMemcpyOptSize =
574 MaxStoresPerMemmoveOptSize = MaxLoadsPerMemcmpOptSize = 4;
Bill Wendlingeb108ba2013-04-05 21:52:40 +0000575 UseUnderscoreSetJmp = false;
576 UseUnderscoreLongJmp = false;
Hal Finkeldecb0242014-01-02 21:13:43 +0000577 HasMultipleConditionRegisters = false;
Yi Jiangb23edeb2014-04-21 22:22:44 +0000578 HasExtractBitsInsn = false;
Sanjay Patel943829a2015-07-01 18:10:20 +0000579 JumpIsExpensive = JumpIsExpensiveOverride;
Bill Wendlingeb108ba2013-04-05 21:52:40 +0000580 PredictableSelectIsExpensive = false;
Quentin Colombetfc2201e2014-12-17 01:36:17 +0000581 EnableExtLdPromotion = false;
Bill Wendlingeb108ba2013-04-05 21:52:40 +0000582 StackPointerRegisterToSaveRestore = 0;
Bill Wendlingeb108ba2013-04-05 21:52:40 +0000583 BooleanContents = UndefinedBooleanContent;
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000584 BooleanFloatContents = UndefinedBooleanContent;
Bill Wendlingeb108ba2013-04-05 21:52:40 +0000585 BooleanVectorContents = UndefinedBooleanContent;
586 SchedPreferenceInfo = Sched::ILP;
Nirav Dave54e22f32017-03-14 00:34:14 +0000587 GatherAllAliasesMaxDepth = 18;
James Y Knight19f6cce2016-04-12 20:18:48 +0000588 // TODO: the default will be switched to 0 in the next commit, along
589 // with the Target-specific changes necessary.
590 MaxAtomicSizeInBitsSupported = 1024;
Bill Wendlingeb108ba2013-04-05 21:52:40 +0000591
James Y Knight148a6462016-06-17 18:11:48 +0000592 MinCmpXchgSizeInBits = 0;
Dylan McKay80463fe2017-12-09 06:45:36 +0000593 SupportsUnalignedAtomics = false;
James Y Knight148a6462016-06-17 18:11:48 +0000594
James Y Knight7873fb92016-04-12 22:32:47 +0000595 std::fill(std::begin(LibcallRoutineNames), std::end(LibcallRoutineNames), nullptr);
596
Matthias Brauna4852d2c2017-12-18 23:19:42 +0000597 InitLibcalls(TM.getTargetTriple());
Bill Wendlingeb108ba2013-04-05 21:52:40 +0000598 InitCmpLibcallCCs(CmpLibcallCCs);
Bill Wendlingeb108ba2013-04-05 21:52:40 +0000599}
600
Bill Wendlingeb108ba2013-04-05 21:52:40 +0000601void TargetLoweringBase::initActions() {
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000602 // All operations default to being supported.
603 memset(OpActions, 0, sizeof(OpActions));
604 memset(LoadExtActions, 0, sizeof(LoadExtActions));
605 memset(TruncStoreActions, 0, sizeof(TruncStoreActions));
606 memset(IndexedModeActions, 0, sizeof(IndexedModeActions));
607 memset(CondCodeActions, 0, sizeof(CondCodeActions));
Craig Topper00230802016-04-08 07:10:46 +0000608 std::fill(std::begin(RegClassForVT), std::end(RegClassForVT), nullptr);
609 std::fill(std::begin(TargetDAGCombineArray),
610 std::end(TargetDAGCombineArray), 0);
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000611
Matt Arsenault0cb08e42019-01-17 10:49:01 +0000612 for (MVT VT : MVT::fp_valuetypes()) {
613 MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits());
614 if (IntVT.isValid()) {
615 setOperationAction(ISD::ATOMIC_SWAP, VT, Promote);
616 AddPromotedToType(ISD::ATOMIC_SWAP, VT, IntVT);
617 }
618 }
619
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000620 // Set default actions for various operations.
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000621 for (MVT VT : MVT::all_valuetypes()) {
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000622 // Default all indexed load / store to expand.
623 for (unsigned IM = (unsigned)ISD::PRE_INC;
624 IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000625 setIndexedLoadAction(IM, VT, Expand);
626 setIndexedStoreAction(IM, VT, Expand);
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000627 }
628
Tim Northover420a2162014-06-13 14:24:07 +0000629 // Most backends expect to see the node which just returns the value loaded.
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000630 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, VT, Expand);
Tim Northover420a2162014-06-13 14:24:07 +0000631
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000632 // These operations default to expand.
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000633 setOperationAction(ISD::FGETSIGN, VT, Expand);
634 setOperationAction(ISD::CONCAT_VECTORS, VT, Expand);
635 setOperationAction(ISD::FMINNUM, VT, Expand);
636 setOperationAction(ISD::FMAXNUM, VT, Expand);
Matt Arsenault687ec752018-10-22 16:27:27 +0000637 setOperationAction(ISD::FMINNUM_IEEE, VT, Expand);
638 setOperationAction(ISD::FMAXNUM_IEEE, VT, Expand);
Thomas Lively30f1d692018-10-24 22:49:55 +0000639 setOperationAction(ISD::FMINIMUM, VT, Expand);
640 setOperationAction(ISD::FMAXIMUM, VT, Expand);
Matt Arsenault0dc54c42015-02-20 22:10:33 +0000641 setOperationAction(ISD::FMAD, VT, Expand);
James Molloy7e9776b2015-05-15 09:03:15 +0000642 setOperationAction(ISD::SMIN, VT, Expand);
643 setOperationAction(ISD::SMAX, VT, Expand);
644 setOperationAction(ISD::UMIN, VT, Expand);
645 setOperationAction(ISD::UMAX, VT, Expand);
Simon Pilgrimcf2da962017-03-14 21:26:58 +0000646 setOperationAction(ISD::ABS, VT, Expand);
Simon Pilgrim180639a2018-12-05 11:12:12 +0000647 setOperationAction(ISD::FSHL, VT, Expand);
648 setOperationAction(ISD::FSHR, VT, Expand);
Leonard Chan699b3b52018-10-16 17:35:41 +0000649 setOperationAction(ISD::SADDSAT, VT, Expand);
Leonard Chan0acfc6b2018-10-22 23:08:40 +0000650 setOperationAction(ISD::UADDSAT, VT, Expand);
Leonard Chan905abe52018-10-29 16:54:37 +0000651 setOperationAction(ISD::SSUBSAT, VT, Expand);
652 setOperationAction(ISD::USUBSAT, VT, Expand);
Leonard Chan118e53f2018-12-12 06:29:14 +0000653 setOperationAction(ISD::SMULFIX, VT, Expand);
Leonard Chan0bada7c2019-05-21 19:17:19 +0000654 setOperationAction(ISD::SMULFIXSAT, VT, Expand);
Leonard Chan68d428e2019-02-04 17:18:11 +0000655 setOperationAction(ISD::UMULFIX, VT, Expand);
Bjorn Pettersson5e331e42019-09-07 12:16:14 +0000656 setOperationAction(ISD::UMULFIXSAT, VT, Expand);
Hal Finkel8ec43c62013-08-09 04:13:44 +0000657
Jan Vesely75395482015-04-29 16:30:46 +0000658 // Overflow operations default to expand
659 setOperationAction(ISD::SADDO, VT, Expand);
660 setOperationAction(ISD::SSUBO, VT, Expand);
661 setOperationAction(ISD::UADDO, VT, Expand);
662 setOperationAction(ISD::USUBO, VT, Expand);
663 setOperationAction(ISD::SMULO, VT, Expand);
664 setOperationAction(ISD::UMULO, VT, Expand);
Hal Finkelcd8664c2015-12-11 23:11:52 +0000665
Amaury Sechet8ac81f32017-04-30 19:24:09 +0000666 // ADDCARRY operations default to expand
667 setOperationAction(ISD::ADDCARRY, VT, Expand);
668 setOperationAction(ISD::SUBCARRY, VT, Expand);
Amaury Sechet251ea8a2017-06-01 11:14:17 +0000669 setOperationAction(ISD::SETCCCARRY, VT, Expand);
Amaury Sechet8ac81f32017-04-30 19:24:09 +0000670
Amaury Sechet84674112018-06-01 13:21:33 +0000671 // ADDC/ADDE/SUBC/SUBE default to expand.
672 setOperationAction(ISD::ADDC, VT, Expand);
673 setOperationAction(ISD::ADDE, VT, Expand);
674 setOperationAction(ISD::SUBC, VT, Expand);
675 setOperationAction(ISD::SUBE, VT, Expand);
676
Craig Topper33772c52016-04-28 03:34:31 +0000677 // These default to Expand so they will be expanded to CTLZ/CTTZ by default.
678 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
679 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
680
James Molloy90111f72015-11-12 12:29:09 +0000681 setOperationAction(ISD::BITREVERSE, VT, Expand);
Fangrui Songf78650a2018-07-30 19:41:25 +0000682
Hal Finkel8ec43c62013-08-09 04:13:44 +0000683 // These library functions default to expand.
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000684 setOperationAction(ISD::FROUND, VT, Expand);
Craig Topperf6d4dc52017-05-30 15:27:55 +0000685 setOperationAction(ISD::FPOWI, VT, Expand);
Hal Finkel0c5c01aa2013-08-19 23:35:46 +0000686
687 // These operations default to expand for vector types.
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000688 if (VT.isVector()) {
689 setOperationAction(ISD::FCOPYSIGN, VT, Expand);
690 setOperationAction(ISD::ANY_EXTEND_VECTOR_INREG, VT, Expand);
691 setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Expand);
692 setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Expand);
Graham Hunter84da2592019-10-18 11:48:35 +0000693 setOperationAction(ISD::SPLAT_VECTOR, VT, Expand);
Chandler Carruthd3561f62014-07-09 22:53:04 +0000694 }
Yury Gribovd7dbb662015-12-01 11:40:55 +0000695
Ulrich Weigand6c5d5ce2019-06-05 22:33:10 +0000696 // Constrained floating-point operations default to expand.
697 setOperationAction(ISD::STRICT_FADD, VT, Expand);
698 setOperationAction(ISD::STRICT_FSUB, VT, Expand);
699 setOperationAction(ISD::STRICT_FMUL, VT, Expand);
700 setOperationAction(ISD::STRICT_FDIV, VT, Expand);
701 setOperationAction(ISD::STRICT_FREM, VT, Expand);
702 setOperationAction(ISD::STRICT_FMA, VT, Expand);
703 setOperationAction(ISD::STRICT_FSQRT, VT, Expand);
704 setOperationAction(ISD::STRICT_FPOW, VT, Expand);
705 setOperationAction(ISD::STRICT_FPOWI, VT, Expand);
706 setOperationAction(ISD::STRICT_FSIN, VT, Expand);
707 setOperationAction(ISD::STRICT_FCOS, VT, Expand);
708 setOperationAction(ISD::STRICT_FEXP, VT, Expand);
709 setOperationAction(ISD::STRICT_FEXP2, VT, Expand);
710 setOperationAction(ISD::STRICT_FLOG, VT, Expand);
711 setOperationAction(ISD::STRICT_FLOG10, VT, Expand);
712 setOperationAction(ISD::STRICT_FLOG2, VT, Expand);
Kevin P. Neal1c3d19c2019-10-07 13:20:00 +0000713 setOperationAction(ISD::STRICT_LRINT, VT, Expand);
714 setOperationAction(ISD::STRICT_LLRINT, VT, Expand);
Ulrich Weigand6c5d5ce2019-06-05 22:33:10 +0000715 setOperationAction(ISD::STRICT_FRINT, VT, Expand);
716 setOperationAction(ISD::STRICT_FNEARBYINT, VT, Expand);
717 setOperationAction(ISD::STRICT_FCEIL, VT, Expand);
718 setOperationAction(ISD::STRICT_FFLOOR, VT, Expand);
Kevin P. Neal1c3d19c2019-10-07 13:20:00 +0000719 setOperationAction(ISD::STRICT_LROUND, VT, Expand);
720 setOperationAction(ISD::STRICT_LLROUND, VT, Expand);
Ulrich Weigand6c5d5ce2019-06-05 22:33:10 +0000721 setOperationAction(ISD::STRICT_FROUND, VT, Expand);
722 setOperationAction(ISD::STRICT_FTRUNC, VT, Expand);
723 setOperationAction(ISD::STRICT_FMAXNUM, VT, Expand);
724 setOperationAction(ISD::STRICT_FMINNUM, VT, Expand);
725 setOperationAction(ISD::STRICT_FP_ROUND, VT, Expand);
726 setOperationAction(ISD::STRICT_FP_EXTEND, VT, Expand);
Kevin P. Nealddf13c02019-08-28 16:33:36 +0000727 setOperationAction(ISD::STRICT_FP_TO_SINT, VT, Expand);
728 setOperationAction(ISD::STRICT_FP_TO_UINT, VT, Expand);
Ulrich Weigand6c5d5ce2019-06-05 22:33:10 +0000729
Etienne Bergeron22bfa832016-06-07 20:15:35 +0000730 // For most targets @llvm.get.dynamic.area.offset just returns 0.
Yury Gribovd7dbb662015-12-01 11:40:55 +0000731 setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, VT, Expand);
Nikita Popovaa7cfa72019-03-11 20:22:13 +0000732
733 // Vector reduction default to expand.
734 setOperationAction(ISD::VECREDUCE_FADD, VT, Expand);
735 setOperationAction(ISD::VECREDUCE_FMUL, VT, Expand);
736 setOperationAction(ISD::VECREDUCE_ADD, VT, Expand);
737 setOperationAction(ISD::VECREDUCE_MUL, VT, Expand);
738 setOperationAction(ISD::VECREDUCE_AND, VT, Expand);
739 setOperationAction(ISD::VECREDUCE_OR, VT, Expand);
740 setOperationAction(ISD::VECREDUCE_XOR, VT, Expand);
741 setOperationAction(ISD::VECREDUCE_SMAX, VT, Expand);
742 setOperationAction(ISD::VECREDUCE_SMIN, VT, Expand);
743 setOperationAction(ISD::VECREDUCE_UMAX, VT, Expand);
744 setOperationAction(ISD::VECREDUCE_UMIN, VT, Expand);
745 setOperationAction(ISD::VECREDUCE_FMAX, VT, Expand);
746 setOperationAction(ISD::VECREDUCE_FMIN, VT, Expand);
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000747 }
748
749 // Most targets ignore the @llvm.prefetch intrinsic.
750 setOperationAction(ISD::PREFETCH, MVT::Other, Expand);
751
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +0000752 // Most targets also ignore the @llvm.readcyclecounter intrinsic.
753 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Expand);
754
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000755 // ConstantFP nodes default to expand. Targets can either change this to
756 // Legal, in which case all fp constants are legal, or use isFPImmLegal()
757 // to optimize expansions for certain constants.
758 setOperationAction(ISD::ConstantFP, MVT::f16, Expand);
759 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
760 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
761 setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
762 setOperationAction(ISD::ConstantFP, MVT::f128, Expand);
763
764 // These library functions default to expand.
Ahmed Bougacha2a20e272015-03-26 23:21:03 +0000765 for (MVT VT : {MVT::f32, MVT::f64, MVT::f128}) {
Sanjay Patel3eaf5002018-09-16 16:50:26 +0000766 setOperationAction(ISD::FCBRT, VT, Expand);
Ahmed Bougacha2a20e272015-03-26 23:21:03 +0000767 setOperationAction(ISD::FLOG , VT, Expand);
768 setOperationAction(ISD::FLOG2, VT, Expand);
769 setOperationAction(ISD::FLOG10, VT, Expand);
770 setOperationAction(ISD::FEXP , VT, Expand);
771 setOperationAction(ISD::FEXP2, VT, Expand);
772 setOperationAction(ISD::FFLOOR, VT, Expand);
Ahmed Bougacha2a20e272015-03-26 23:21:03 +0000773 setOperationAction(ISD::FNEARBYINT, VT, Expand);
774 setOperationAction(ISD::FCEIL, VT, Expand);
775 setOperationAction(ISD::FRINT, VT, Expand);
776 setOperationAction(ISD::FTRUNC, VT, Expand);
777 setOperationAction(ISD::FROUND, VT, Expand);
Adhemerval Zanella73643b52019-05-16 13:15:27 +0000778 setOperationAction(ISD::LROUND, VT, Expand);
779 setOperationAction(ISD::LLROUND, VT, Expand);
Adhemerval Zanella6d7bf5e2019-05-28 20:47:44 +0000780 setOperationAction(ISD::LRINT, VT, Expand);
781 setOperationAction(ISD::LLRINT, VT, Expand);
Ahmed Bougacha2a20e272015-03-26 23:21:03 +0000782 }
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000783
784 // Default ISD::TRAP to expand (which turns it into abort).
785 setOperationAction(ISD::TRAP, MVT::Other, Expand);
786
787 // On most systems, DEBUGTRAP and TRAP have no difference. The "Expand"
788 // here is to inform DAG Legalizer to replace DEBUGTRAP with TRAP.
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000789 setOperationAction(ISD::DEBUGTRAP, MVT::Other, Expand);
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000790}
791
Mehdi Aminieaabc512015-07-09 15:12:23 +0000792MVT TargetLoweringBase::getScalarShiftAmountTy(const DataLayout &DL,
793 EVT) const {
Bjorn Pettersson8d19e942019-05-09 08:07:36 +0000794 return MVT::getIntegerVT(DL.getPointerSizeInBits(0));
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000795}
796
Craig Topper35801fa2018-02-20 17:41:05 +0000797EVT TargetLoweringBase::getShiftAmountTy(EVT LHSTy, const DataLayout &DL,
798 bool LegalTypes) const {
Michael Liao6af16fc2013-03-01 18:40:30 +0000799 assert(LHSTy.isInteger() && "Shift amount is not an integer type!");
800 if (LHSTy.isVector())
801 return LHSTy;
Craig Topper35801fa2018-02-20 17:41:05 +0000802 return LegalTypes ? getScalarShiftAmountTy(DL, LHSTy)
803 : getPointerTy(DL);
Michael Liao6af16fc2013-03-01 18:40:30 +0000804}
805
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000806bool TargetLoweringBase::canOpTrap(unsigned Op, EVT VT) const {
807 assert(isTypeLegal(VT));
808 switch (Op) {
809 default:
810 return false;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000811 case ISD::SDIV:
812 case ISD::UDIV:
813 case ISD::SREM:
814 case ISD::UREM:
815 return true;
816 }
817}
818
Sanjay Patel943829a2015-07-01 18:10:20 +0000819void TargetLoweringBase::setJumpIsExpensive(bool isExpensive) {
820 // If the command-line option was specified, ignore this request.
821 if (!JumpIsExpensiveOverride.getNumOccurrences())
822 JumpIsExpensive = isExpensive;
823}
824
Eric Christopher75dbd7c2015-02-25 22:41:30 +0000825TargetLoweringBase::LegalizeKind
826TargetLoweringBase::getTypeConversion(LLVMContext &Context, EVT VT) const {
827 // If this is a simple type, use the ComputeRegisterProp mechanism.
828 if (VT.isSimple()) {
829 MVT SVT = VT.getSimpleVT();
830 assert((unsigned)SVT.SimpleTy < array_lengthof(TransformToType));
831 MVT NVT = TransformToType[SVT.SimpleTy];
832 LegalizeTypeAction LA = ValueTypeActions.getTypeAction(SVT);
833
834 assert((LA == TypeLegal || LA == TypeSoftenFloat ||
Matt Arsenault1f2b7272019-08-15 18:58:25 +0000835 (NVT.isVector() ||
836 ValueTypeActions.getTypeAction(NVT) != TypePromoteInteger)) &&
Eric Christopher75dbd7c2015-02-25 22:41:30 +0000837 "Promote may not follow Expand or Promote");
838
839 if (LA == TypeSplitVector)
840 return LegalizeKind(LA,
841 EVT::getVectorVT(Context, SVT.getVectorElementType(),
842 SVT.getVectorNumElements() / 2));
843 if (LA == TypeScalarizeVector)
844 return LegalizeKind(LA, SVT.getVectorElementType());
845 return LegalizeKind(LA, NVT);
846 }
847
848 // Handle Extended Scalar Types.
849 if (!VT.isVector()) {
850 assert(VT.isInteger() && "Float types must be simple");
851 unsigned BitSize = VT.getSizeInBits();
852 // First promote to a power-of-two size, then expand if necessary.
853 if (BitSize < 8 || !isPowerOf2_32(BitSize)) {
854 EVT NVT = VT.getRoundIntegerType(Context);
855 assert(NVT != VT && "Unable to round integer VT");
856 LegalizeKind NextStep = getTypeConversion(Context, NVT);
857 // Avoid multi-step promotion.
858 if (NextStep.first == TypePromoteInteger)
859 return NextStep;
860 // Return rounded integer type.
861 return LegalizeKind(TypePromoteInteger, NVT);
862 }
863
864 return LegalizeKind(TypeExpandInteger,
865 EVT::getIntegerVT(Context, VT.getSizeInBits() / 2));
866 }
867
868 // Handle vector types.
869 unsigned NumElts = VT.getVectorNumElements();
870 EVT EltVT = VT.getVectorElementType();
871
872 // Vectors with only one element are always scalarized.
873 if (NumElts == 1)
874 return LegalizeKind(TypeScalarizeVector, EltVT);
875
876 // Try to widen vector elements until the element type is a power of two and
877 // promote it to a legal type later on, for example:
878 // <3 x i8> -> <4 x i8> -> <4 x i32>
879 if (EltVT.isInteger()) {
880 // Vectors with a number of elements that is not a power of two are always
881 // widened, for example <3 x i8> -> <4 x i8>.
882 if (!VT.isPow2VectorType()) {
883 NumElts = (unsigned)NextPowerOf2(NumElts);
884 EVT NVT = EVT::getVectorVT(Context, EltVT, NumElts);
885 return LegalizeKind(TypeWidenVector, NVT);
886 }
887
888 // Examine the element type.
889 LegalizeKind LK = getTypeConversion(Context, EltVT);
890
891 // If type is to be expanded, split the vector.
892 // <4 x i140> -> <2 x i140>
893 if (LK.first == TypeExpandInteger)
894 return LegalizeKind(TypeSplitVector,
895 EVT::getVectorVT(Context, EltVT, NumElts / 2));
896
897 // Promote the integer element types until a legal vector type is found
898 // or until the element integer type is too big. If a legal type was not
899 // found, fallback to the usual mechanism of widening/splitting the
900 // vector.
901 EVT OldEltVT = EltVT;
Eugene Zelenkofb7f7922017-09-21 23:20:16 +0000902 while (true) {
Eric Christopher75dbd7c2015-02-25 22:41:30 +0000903 // Increase the bitwidth of the element to the next pow-of-two
904 // (which is greater than 8 bits).
905 EltVT = EVT::getIntegerVT(Context, 1 + EltVT.getSizeInBits())
906 .getRoundIntegerType(Context);
907
908 // Stop trying when getting a non-simple element type.
909 // Note that vector elements may be greater than legal vector element
910 // types. Example: X86 XMM registers hold 64bit element on 32bit
911 // systems.
912 if (!EltVT.isSimple())
913 break;
914
915 // Build a new vector type and check if it is legal.
916 MVT NVT = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
917 // Found a legal promoted vector type.
918 if (NVT != MVT() && ValueTypeActions.getTypeAction(NVT) == TypeLegal)
919 return LegalizeKind(TypePromoteInteger,
920 EVT::getVectorVT(Context, EltVT, NumElts));
921 }
922
923 // Reset the type to the unexpanded type if we did not find a legal vector
924 // type with a promoted vector element type.
925 EltVT = OldEltVT;
926 }
927
928 // Try to widen the vector until a legal type is found.
929 // If there is no wider legal type, split the vector.
Eugene Zelenkofb7f7922017-09-21 23:20:16 +0000930 while (true) {
Eric Christopher75dbd7c2015-02-25 22:41:30 +0000931 // Round up to the next power of 2.
932 NumElts = (unsigned)NextPowerOf2(NumElts);
933
934 // If there is no simple vector type with this many elements then there
935 // cannot be a larger legal vector type. Note that this assumes that
936 // there are no skipped intermediate vector types in the simple types.
937 if (!EltVT.isSimple())
938 break;
939 MVT LargerVector = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
940 if (LargerVector == MVT())
941 break;
942
943 // If this type is legal then widen the vector.
944 if (ValueTypeActions.getTypeAction(LargerVector) == TypeLegal)
945 return LegalizeKind(TypeWidenVector, LargerVector);
946 }
947
948 // Widen odd vectors to next power of two.
949 if (!VT.isPow2VectorType()) {
950 EVT NVT = VT.getPow2VectorType(Context);
951 return LegalizeKind(TypeWidenVector, NVT);
952 }
953
954 // Vectors with illegal element types are expanded.
955 EVT NVT = EVT::getVectorVT(Context, EltVT, VT.getVectorNumElements() / 2);
956 return LegalizeKind(TypeSplitVector, NVT);
957}
Benjamin Kramer56b31bd2013-01-11 20:05:37 +0000958
959static unsigned getVectorTypeBreakdownMVT(MVT VT, MVT &IntermediateVT,
960 unsigned &NumIntermediates,
961 MVT &RegisterVT,
962 TargetLoweringBase *TLI) {
963 // Figure out the right, legal destination reg to copy into.
964 unsigned NumElts = VT.getVectorNumElements();
965 MVT EltTy = VT.getVectorElementType();
966
967 unsigned NumVectorRegs = 1;
968
969 // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally we
970 // could break down into LHS/RHS like LegalizeDAG does.
971 if (!isPowerOf2_32(NumElts)) {
972 NumVectorRegs = NumElts;
973 NumElts = 1;
974 }
975
976 // Divide the input until we get to a supported size. This will always
977 // end with a scalar if the target doesn't support vectors.
978 while (NumElts > 1 && !TLI->isTypeLegal(MVT::getVectorVT(EltTy, NumElts))) {
979 NumElts >>= 1;
980 NumVectorRegs <<= 1;
981 }
982
983 NumIntermediates = NumVectorRegs;
984
985 MVT NewVT = MVT::getVectorVT(EltTy, NumElts);
986 if (!TLI->isTypeLegal(NewVT))
987 NewVT = EltTy;
988 IntermediateVT = NewVT;
989
990 unsigned NewVTSize = NewVT.getSizeInBits();
991
992 // Convert sizes such as i33 to i64.
993 if (!isPowerOf2_32(NewVTSize))
994 NewVTSize = NextPowerOf2(NewVTSize);
995
996 MVT DestVT = TLI->getRegisterType(NewVT);
997 RegisterVT = DestVT;
998 if (EVT(DestVT).bitsLT(NewVT)) // Value is expanded, e.g. i64 -> i16.
999 return NumVectorRegs*(NewVTSize/DestVT.getSizeInBits());
1000
1001 // Otherwise, promotion or legal types use the same number of registers as
1002 // the vector decimated to the appropriate level.
1003 return NumVectorRegs;
1004}
1005
1006/// isLegalRC - Return true if the value types that can be represented by the
1007/// specified register class are all legal.
Krzysztof Parzyszekc8e8e2a2017-04-24 19:51:12 +00001008bool TargetLoweringBase::isLegalRC(const TargetRegisterInfo &TRI,
1009 const TargetRegisterClass &RC) const {
1010 for (auto I = TRI.legalclasstypes_begin(RC); *I != MVT::Other; ++I)
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001011 if (isTypeLegal(*I))
1012 return true;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001013 return false;
1014}
1015
Lang Hames39609992013-11-29 03:07:54 +00001016/// Replace/modify any TargetFrameIndex operands with a targte-dependent
1017/// sequence of memory operands that is recognized by PrologEpilogInserter.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001018MachineBasicBlock *
1019TargetLoweringBase::emitPatchPoint(MachineInstr &InitialMI,
Lang Hames39609992013-11-29 03:07:54 +00001020 MachineBasicBlock *MBB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001021 MachineInstr *MI = &InitialMI;
Justin Bognerfdf9bf42017-10-10 23:50:49 +00001022 MachineFunction &MF = *MI->getMF();
Matthias Braun941a7052016-07-28 18:40:00 +00001023 MachineFrameInfo &MFI = MF.getFrameInfo();
Philip Reamescb0f9472015-12-23 23:44:28 +00001024
1025 // We're handling multiple types of operands here:
1026 // PATCHPOINT MetaArgs - live-in, read only, direct
1027 // STATEPOINT Deopt Spill - live-through, read only, indirect
1028 // STATEPOINT Deopt Alloca - live-through, read only, direct
1029 // (We're currently conservative and mark the deopt slots read/write in
Fangrui Songf78650a2018-07-30 19:41:25 +00001030 // practice.)
Philip Reamescb0f9472015-12-23 23:44:28 +00001031 // STATEPOINT GC Spill - live-through, read/write, indirect
1032 // STATEPOINT GC Alloca - live-through, read/write, direct
1033 // The live-in vs live-through is handled already (the live through ones are
1034 // all stack slots), but we need to handle the different type of stackmap
1035 // operands and memory effects here.
Lang Hames39609992013-11-29 03:07:54 +00001036
1037 // MI changes inside this loop as we grow operands.
1038 for(unsigned OperIdx = 0; OperIdx != MI->getNumOperands(); ++OperIdx) {
1039 MachineOperand &MO = MI->getOperand(OperIdx);
1040 if (!MO.isFI())
1041 continue;
1042
1043 // foldMemoryOperand builds a new MI after replacing a single FI operand
1044 // with the canonical set of five x86 addressing-mode operands.
1045 int FI = MO.getIndex();
1046 MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), MI->getDesc());
1047
1048 // Copy operands before the frame-index.
1049 for (unsigned i = 0; i < OperIdx; ++i)
Diana Picus116bbab2017-01-13 09:58:52 +00001050 MIB.add(MI->getOperand(i));
Philip Reamescb0f9472015-12-23 23:44:28 +00001051 // Add frame index operands recognized by stackmaps.cpp
1052 if (MFI.isStatepointSpillSlotObjectIndex(FI)) {
1053 // indirect-mem-ref tag, size, #FI, offset.
1054 // Used for spills inserted by StatepointLowering. This codepath is not
1055 // used for patchpoints/stackmaps at all, for these spilling is done via
1056 // foldMemoryOperand callback only.
1057 assert(MI->getOpcode() == TargetOpcode::STATEPOINT && "sanity");
1058 MIB.addImm(StackMaps::IndirectMemRefOp);
1059 MIB.addImm(MFI.getObjectSize(FI));
Diana Picus116bbab2017-01-13 09:58:52 +00001060 MIB.add(MI->getOperand(OperIdx));
Philip Reamescb0f9472015-12-23 23:44:28 +00001061 MIB.addImm(0);
1062 } else {
1063 // direct-mem-ref tag, #FI, offset.
1064 // Used by patchpoint, and direct alloca arguments to statepoints
1065 MIB.addImm(StackMaps::DirectMemRefOp);
Diana Picus116bbab2017-01-13 09:58:52 +00001066 MIB.add(MI->getOperand(OperIdx));
Philip Reamescb0f9472015-12-23 23:44:28 +00001067 MIB.addImm(0);
1068 }
Lang Hames39609992013-11-29 03:07:54 +00001069 // Copy the operands after the frame index.
1070 for (unsigned i = OperIdx + 1; i != MI->getNumOperands(); ++i)
Diana Picus116bbab2017-01-13 09:58:52 +00001071 MIB.add(MI->getOperand(i));
Lang Hames39609992013-11-29 03:07:54 +00001072
1073 // Inherit previous memory operands.
Chandler Carruthc73c0302018-08-16 21:30:05 +00001074 MIB.cloneMemRefs(*MI);
Lang Hames39609992013-11-29 03:07:54 +00001075 assert(MIB->mayLoad() && "Folded a stackmap use to a non-load!");
1076
1077 // Add a new memory operand for this FI.
Lang Hames39609992013-11-29 03:07:54 +00001078 assert(MFI.getObjectOffset(FI) != -1);
Philip Reames0365f1a2014-12-01 22:52:56 +00001079
Philip Reames18408d52019-03-12 19:12:33 +00001080 // Note: STATEPOINT MMOs are added during SelectionDAG. STACKMAP, and
1081 // PATCHPOINT should be updated to do the same. (TODO)
1082 if (MI->getOpcode() != TargetOpcode::STATEPOINT) {
1083 auto Flags = MachineMemOperand::MOLoad;
1084 MachineMemOperand *MMO = MF.getMachineMemOperand(
1085 MachinePointerInfo::getFixedStack(MF, FI), Flags,
1086 MF.getDataLayout().getPointerSize(), MFI.getObjectAlignment(FI));
1087 MIB->addMemOperand(MF, MMO);
Philip Reames0365f1a2014-12-01 22:52:56 +00001088 }
Philip Reames18408d52019-03-12 19:12:33 +00001089
Lang Hames39609992013-11-29 03:07:54 +00001090 // Replace the instruction and update the operand index.
1091 MBB->insert(MachineBasicBlock::iterator(MI), MIB);
1092 OperIdx += (MIB->getNumOperands() - MI->getNumOperands()) - 1;
1093 MI->eraseFromParent();
1094 MI = MIB;
1095 }
1096 return MBB;
1097}
1098
Dean Michael Berriscdca0732018-02-01 02:21:54 +00001099MachineBasicBlock *
1100TargetLoweringBase::emitXRayCustomEvent(MachineInstr &MI,
1101 MachineBasicBlock *MBB) const {
1102 assert(MI.getOpcode() == TargetOpcode::PATCHABLE_EVENT_CALL &&
1103 "Called emitXRayCustomEvent on the wrong MI!");
1104 auto &MF = *MI.getMF();
1105 auto MIB = BuildMI(MF, MI.getDebugLoc(), MI.getDesc());
1106 for (unsigned OpIdx = 0; OpIdx != MI.getNumOperands(); ++OpIdx)
1107 MIB.add(MI.getOperand(OpIdx));
1108
1109 MBB->insert(MachineBasicBlock::iterator(MI), MIB);
1110 MI.eraseFromParent();
1111 return MBB;
1112}
1113
Keith Wyss3d868232018-04-17 21:30:29 +00001114MachineBasicBlock *
1115TargetLoweringBase::emitXRayTypedEvent(MachineInstr &MI,
1116 MachineBasicBlock *MBB) const {
1117 assert(MI.getOpcode() == TargetOpcode::PATCHABLE_TYPED_EVENT_CALL &&
1118 "Called emitXRayTypedEvent on the wrong MI!");
1119 auto &MF = *MI.getMF();
1120 auto MIB = BuildMI(MF, MI.getDebugLoc(), MI.getDesc());
1121 for (unsigned OpIdx = 0; OpIdx != MI.getNumOperands(); ++OpIdx)
1122 MIB.add(MI.getOperand(OpIdx));
1123
1124 MBB->insert(MachineBasicBlock::iterator(MI), MIB);
1125 MI.eraseFromParent();
1126 return MBB;
1127}
1128
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001129/// findRepresentativeClass - Return the largest legal super-reg register class
1130/// of the register class for the specified type and its associated "cost".
Eric Christopher720ab842015-03-03 19:47:14 +00001131// This function is in TargetLowering because it uses RegClassForVT which would
1132// need to be moved to TargetRegisterInfo and would necessitate moving
1133// isTypeLegal over as well - a massive change that would just require
1134// TargetLowering having a TargetRegisterInfo class member that it would use.
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001135std::pair<const TargetRegisterClass *, uint8_t>
1136TargetLoweringBase::findRepresentativeClass(const TargetRegisterInfo *TRI,
1137 MVT VT) const {
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001138 const TargetRegisterClass *RC = RegClassForVT[VT.SimpleTy];
1139 if (!RC)
1140 return std::make_pair(RC, 0);
1141
1142 // Compute the set of all super-register classes.
1143 BitVector SuperRegRC(TRI->getNumRegClasses());
1144 for (SuperRegClassIterator RCI(RC, TRI); RCI.isValid(); ++RCI)
1145 SuperRegRC.setBitsInMask(RCI.getMask());
1146
1147 // Find the first legal register class with the largest spill size.
1148 const TargetRegisterClass *BestRC = RC;
Francis Visoiu Mistrihb52e0362017-05-17 01:07:53 +00001149 for (unsigned i : SuperRegRC.set_bits()) {
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001150 const TargetRegisterClass *SuperRC = TRI->getRegClass(i);
1151 // We want the largest possible spill size.
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00001152 if (TRI->getSpillSize(*SuperRC) <= TRI->getSpillSize(*BestRC))
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001153 continue;
Krzysztof Parzyszekc8e8e2a2017-04-24 19:51:12 +00001154 if (!isLegalRC(*TRI, *SuperRC))
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001155 continue;
1156 BestRC = SuperRC;
1157 }
1158 return std::make_pair(BestRC, 1);
1159}
1160
1161/// computeRegisterProperties - Once all of the register classes are added,
1162/// this allows us to compute derived properties we expose.
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001163void TargetLoweringBase::computeRegisterProperties(
1164 const TargetRegisterInfo *TRI) {
Craig Topper6438fc32014-11-17 00:26:50 +00001165 static_assert(MVT::LAST_VALUETYPE <= MVT::MAX_ALLOWED_VALUETYPE,
1166 "Too many value types for ValueTypeActions to hold!");
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001167
1168 // Everything defaults to needing one register.
1169 for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
1170 NumRegistersForVT[i] = 1;
1171 RegisterTypeForVT[i] = TransformToType[i] = (MVT::SimpleValueType)i;
1172 }
1173 // ...except isVoid, which doesn't need any registers.
1174 NumRegistersForVT[MVT::isVoid] = 0;
1175
1176 // Find the largest integer register class.
1177 unsigned LargestIntReg = MVT::LAST_INTEGER_VALUETYPE;
Craig Topperc0196b12014-04-14 00:51:57 +00001178 for (; RegClassForVT[LargestIntReg] == nullptr; --LargestIntReg)
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001179 assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
1180
1181 // Every integer value type larger than this largest register takes twice as
1182 // many registers to represent as the previous ValueType.
1183 for (unsigned ExpandedReg = LargestIntReg + 1;
1184 ExpandedReg <= MVT::LAST_INTEGER_VALUETYPE; ++ExpandedReg) {
1185 NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1];
1186 RegisterTypeForVT[ExpandedReg] = (MVT::SimpleValueType)LargestIntReg;
1187 TransformToType[ExpandedReg] = (MVT::SimpleValueType)(ExpandedReg - 1);
1188 ValueTypeActions.setTypeAction((MVT::SimpleValueType)ExpandedReg,
1189 TypeExpandInteger);
1190 }
1191
1192 // Inspect all of the ValueType's smaller than the largest integer
1193 // register to see which ones need promotion.
1194 unsigned LegalIntReg = LargestIntReg;
1195 for (unsigned IntReg = LargestIntReg - 1;
1196 IntReg >= (unsigned)MVT::i1; --IntReg) {
1197 MVT IVT = (MVT::SimpleValueType)IntReg;
1198 if (isTypeLegal(IVT)) {
1199 LegalIntReg = IntReg;
1200 } else {
1201 RegisterTypeForVT[IntReg] = TransformToType[IntReg] =
Serge Gueltona4d9e222018-11-07 16:17:30 +00001202 (MVT::SimpleValueType)LegalIntReg;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001203 ValueTypeActions.setTypeAction(IVT, TypePromoteInteger);
1204 }
1205 }
1206
1207 // ppcf128 type is really two f64's.
1208 if (!isTypeLegal(MVT::ppcf128)) {
Petar Jovanovic23e44f52016-02-04 14:43:50 +00001209 if (isTypeLegal(MVT::f64)) {
1210 NumRegistersForVT[MVT::ppcf128] = 2*NumRegistersForVT[MVT::f64];
1211 RegisterTypeForVT[MVT::ppcf128] = MVT::f64;
1212 TransformToType[MVT::ppcf128] = MVT::f64;
1213 ValueTypeActions.setTypeAction(MVT::ppcf128, TypeExpandFloat);
1214 } else {
1215 NumRegistersForVT[MVT::ppcf128] = NumRegistersForVT[MVT::i128];
1216 RegisterTypeForVT[MVT::ppcf128] = RegisterTypeForVT[MVT::i128];
1217 TransformToType[MVT::ppcf128] = MVT::i128;
1218 ValueTypeActions.setTypeAction(MVT::ppcf128, TypeSoftenFloat);
1219 }
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001220 }
1221
Akira Hatanaka3d055582013-03-01 21:11:44 +00001222 // Decide how to handle f128. If the target does not have native f128 support,
1223 // expand it to i128 and we will be generating soft float library calls.
1224 if (!isTypeLegal(MVT::f128)) {
1225 NumRegistersForVT[MVT::f128] = NumRegistersForVT[MVT::i128];
1226 RegisterTypeForVT[MVT::f128] = RegisterTypeForVT[MVT::i128];
1227 TransformToType[MVT::f128] = MVT::i128;
1228 ValueTypeActions.setTypeAction(MVT::f128, TypeSoftenFloat);
1229 }
1230
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001231 // Decide how to handle f64. If the target does not have native f64 support,
1232 // expand it to i64 and we will be generating soft float library calls.
1233 if (!isTypeLegal(MVT::f64)) {
1234 NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64];
1235 RegisterTypeForVT[MVT::f64] = RegisterTypeForVT[MVT::i64];
1236 TransformToType[MVT::f64] = MVT::i64;
1237 ValueTypeActions.setTypeAction(MVT::f64, TypeSoftenFloat);
1238 }
1239
Ahmed Bougachaa0f35592015-03-28 01:22:37 +00001240 // Decide how to handle f32. If the target does not have native f32 support,
1241 // expand it to i32 and we will be generating soft float library calls.
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001242 if (!isTypeLegal(MVT::f32)) {
Ahmed Bougachaa0f35592015-03-28 01:22:37 +00001243 NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32];
1244 RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::i32];
1245 TransformToType[MVT::f32] = MVT::i32;
1246 ValueTypeActions.setTypeAction(MVT::f32, TypeSoftenFloat);
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001247 }
1248
Oliver Stannard56358572015-11-09 11:03:18 +00001249 // Decide how to handle f16. If the target does not have native f16 support,
1250 // promote it to f32, because there are no f16 library calls (except for
1251 // conversions).
Tim Northover20bd0ce2014-07-18 12:41:46 +00001252 if (!isTypeLegal(MVT::f16)) {
Oliver Stannard56358572015-11-09 11:03:18 +00001253 NumRegistersForVT[MVT::f16] = NumRegistersForVT[MVT::f32];
1254 RegisterTypeForVT[MVT::f16] = RegisterTypeForVT[MVT::f32];
1255 TransformToType[MVT::f16] = MVT::f32;
1256 ValueTypeActions.setTypeAction(MVT::f16, TypePromoteFloat);
Tim Northover20bd0ce2014-07-18 12:41:46 +00001257 }
1258
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001259 // Loop over all of the vector value types to see which need transformations.
1260 for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
1261 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
Chandler Carruth9d010ff2014-07-03 00:23:43 +00001262 MVT VT = (MVT::SimpleValueType) i;
1263 if (isTypeLegal(VT))
1264 continue;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001265
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001266 MVT EltVT = VT.getVectorElementType();
1267 unsigned NElts = VT.getVectorNumElements();
Chandler Carruth9d010ff2014-07-03 00:23:43 +00001268 bool IsLegalWiderType = false;
Graham Hunter84da2592019-10-18 11:48:35 +00001269 bool IsScalable = VT.isScalableVector();
Chandler Carruth9d010ff2014-07-03 00:23:43 +00001270 LegalizeTypeAction PreferredAction = getPreferredVectorAction(VT);
1271 switch (PreferredAction) {
Graham Hunter84da2592019-10-18 11:48:35 +00001272 case TypePromoteInteger: {
1273 MVT::SimpleValueType EndVT = IsScalable ?
1274 MVT::LAST_INTEGER_SCALABLE_VECTOR_VALUETYPE :
1275 MVT::LAST_INTEGER_FIXEDLEN_VECTOR_VALUETYPE;
Chandler Carruth9d010ff2014-07-03 00:23:43 +00001276 // Try to promote the elements of integer vectors. If no legal
1277 // promotion was found, fall through to the widen-vector method.
Graham Hunter1a9195d2019-09-17 10:19:23 +00001278 for (unsigned nVT = i + 1;
Graham Hunter84da2592019-10-18 11:48:35 +00001279 (MVT::SimpleValueType)nVT <= EndVT; ++nVT) {
Chandler Carruth9d010ff2014-07-03 00:23:43 +00001280 MVT SVT = (MVT::SimpleValueType) nVT;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001281 // Promote vectors of integers to vectors with the same number
1282 // of elements, with a wider element type.
Sanjay Patel1ed771f2016-09-14 16:37:15 +00001283 if (SVT.getScalarSizeInBits() > EltVT.getSizeInBits() &&
Graham Hunter84da2592019-10-18 11:48:35 +00001284 SVT.getVectorNumElements() == NElts &&
1285 SVT.isScalableVector() == IsScalable && isTypeLegal(SVT)) {
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001286 TransformToType[i] = SVT;
1287 RegisterTypeForVT[i] = SVT;
1288 NumRegistersForVT[i] = 1;
1289 ValueTypeActions.setTypeAction(VT, TypePromoteInteger);
1290 IsLegalWiderType = true;
1291 break;
1292 }
1293 }
Chandler Carruth9d010ff2014-07-03 00:23:43 +00001294 if (IsLegalWiderType)
1295 break;
Galina Kistanovabd79f732017-06-03 05:11:14 +00001296 LLVM_FALLTHROUGH;
Graham Hunter84da2592019-10-18 11:48:35 +00001297 }
Eugene Zelenkofb7f7922017-09-21 23:20:16 +00001298
1299 case TypeWidenVector:
Craig Topper74168de2019-08-18 06:28:06 +00001300 if (isPowerOf2_32(NElts)) {
1301 // Try to widen the vector.
1302 for (unsigned nVT = i + 1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
1303 MVT SVT = (MVT::SimpleValueType) nVT;
1304 if (SVT.getVectorElementType() == EltVT
Graham Hunter84da2592019-10-18 11:48:35 +00001305 && SVT.getVectorNumElements() > NElts
1306 && SVT.isScalableVector() == IsScalable && isTypeLegal(SVT)) {
Craig Topper74168de2019-08-18 06:28:06 +00001307 TransformToType[i] = SVT;
1308 RegisterTypeForVT[i] = SVT;
1309 NumRegistersForVT[i] = 1;
1310 ValueTypeActions.setTypeAction(VT, TypeWidenVector);
1311 IsLegalWiderType = true;
1312 break;
1313 }
1314 }
1315 if (IsLegalWiderType)
1316 break;
1317 } else {
1318 // Only widen to the next power of 2 to keep consistency with EVT.
1319 MVT NVT = VT.getPow2VectorType();
1320 if (isTypeLegal(NVT)) {
1321 TransformToType[i] = NVT;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001322 ValueTypeActions.setTypeAction(VT, TypeWidenVector);
Craig Topper74168de2019-08-18 06:28:06 +00001323 RegisterTypeForVT[i] = NVT;
1324 NumRegistersForVT[i] = 1;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001325 break;
1326 }
1327 }
Galina Kistanovabd79f732017-06-03 05:11:14 +00001328 LLVM_FALLTHROUGH;
Eugene Zelenkofb7f7922017-09-21 23:20:16 +00001329
Chandler Carruth9d010ff2014-07-03 00:23:43 +00001330 case TypeSplitVector:
1331 case TypeScalarizeVector: {
1332 MVT IntermediateVT;
1333 MVT RegisterVT;
1334 unsigned NumIntermediates;
Craig Topper84e83b52019-11-13 12:09:34 -08001335 unsigned NumRegisters = getVectorTypeBreakdownMVT(VT, IntermediateVT,
Chandler Carruth9d010ff2014-07-03 00:23:43 +00001336 NumIntermediates, RegisterVT, this);
Craig Topper84e83b52019-11-13 12:09:34 -08001337 NumRegistersForVT[i] = NumRegisters;
1338 assert(NumRegistersForVT[i] == NumRegisters &&
1339 "NumRegistersForVT size cannot represent NumRegisters!");
Chandler Carruth9d010ff2014-07-03 00:23:43 +00001340 RegisterTypeForVT[i] = RegisterVT;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001341
Chandler Carruth9d010ff2014-07-03 00:23:43 +00001342 MVT NVT = VT.getPow2VectorType();
1343 if (NVT == VT) {
1344 // Type is already a power of 2. The default action is to split.
1345 TransformToType[i] = MVT::Other;
1346 if (PreferredAction == TypeScalarizeVector)
1347 ValueTypeActions.setTypeAction(VT, TypeScalarizeVector);
Hao Liue02b1a02014-10-31 02:35:34 +00001348 else if (PreferredAction == TypeSplitVector)
Chandler Carruth9d010ff2014-07-03 00:23:43 +00001349 ValueTypeActions.setTypeAction(VT, TypeSplitVector);
Hao Liue02b1a02014-10-31 02:35:34 +00001350 else
1351 // Set type action according to the number of elements.
1352 ValueTypeActions.setTypeAction(VT, NElts == 1 ? TypeScalarizeVector
1353 : TypeSplitVector);
Chandler Carruth9d010ff2014-07-03 00:23:43 +00001354 } else {
1355 TransformToType[i] = NVT;
1356 ValueTypeActions.setTypeAction(VT, TypeWidenVector);
1357 }
1358 break;
1359 }
1360 default:
1361 llvm_unreachable("Unknown vector legalization action!");
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001362 }
1363 }
1364
1365 // Determine the 'representative' register class for each value type.
1366 // An representative register class is the largest (meaning one which is
1367 // not a sub-register class / subreg register class) legal register class for
1368 // a group of value types. For example, on i386, i8, i16, and i32
1369 // representative would be GR32; while on x86_64 it's GR64.
1370 for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
1371 const TargetRegisterClass* RRC;
1372 uint8_t Cost;
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001373 std::tie(RRC, Cost) = findRepresentativeClass(TRI, (MVT::SimpleValueType)i);
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001374 RepRegClassForVT[i] = RRC;
1375 RepRegClassCostForVT[i] = Cost;
1376 }
1377}
1378
Mehdi Amini44ede332015-07-09 02:09:04 +00001379EVT TargetLoweringBase::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1380 EVT VT) const {
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001381 assert(!VT.isVector() && "No default SetCC type for vectors!");
Mehdi Amini44ede332015-07-09 02:09:04 +00001382 return getPointerTy(DL).SimpleTy;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001383}
1384
1385MVT::SimpleValueType TargetLoweringBase::getCmpLibcallReturnType() const {
1386 return MVT::i32; // return the default value
1387}
1388
1389/// getVectorTypeBreakdown - Vector types are broken down into some number of
1390/// legal first class types. For example, MVT::v8f32 maps to 2 MVT::v4f32
1391/// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
1392/// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86.
1393///
1394/// This method returns the number of registers needed, and the VT for each
1395/// register. It also returns the VT and quantity of the intermediate values
1396/// before they are promoted/expanded.
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001397unsigned TargetLoweringBase::getVectorTypeBreakdown(LLVMContext &Context, EVT VT,
1398 EVT &IntermediateVT,
1399 unsigned &NumIntermediates,
1400 MVT &RegisterVT) const {
1401 unsigned NumElts = VT.getVectorNumElements();
1402
1403 // If there is a wider vector type with the same element type as this one,
1404 // or a promoted vector type that has the same number of elements which
1405 // are wider, then we should convert to that legal vector type.
1406 // This handles things like <2 x float> -> <4 x float> and
1407 // <4 x i1> -> <4 x i32>.
1408 LegalizeTypeAction TA = getTypeAction(Context, VT);
1409 if (NumElts != 1 && (TA == TypeWidenVector || TA == TypePromoteInteger)) {
1410 EVT RegisterEVT = getTypeToTransformTo(Context, VT);
1411 if (isTypeLegal(RegisterEVT)) {
1412 IntermediateVT = RegisterEVT;
1413 RegisterVT = RegisterEVT.getSimpleVT();
1414 NumIntermediates = 1;
1415 return 1;
1416 }
1417 }
1418
1419 // Figure out the right, legal destination reg to copy into.
1420 EVT EltTy = VT.getVectorElementType();
1421
1422 unsigned NumVectorRegs = 1;
1423
1424 // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally we
1425 // could break down into LHS/RHS like LegalizeDAG does.
1426 if (!isPowerOf2_32(NumElts)) {
1427 NumVectorRegs = NumElts;
1428 NumElts = 1;
1429 }
1430
1431 // Divide the input until we get to a supported size. This will always
1432 // end with a scalar if the target doesn't support vectors.
1433 while (NumElts > 1 && !isTypeLegal(
1434 EVT::getVectorVT(Context, EltTy, NumElts))) {
1435 NumElts >>= 1;
1436 NumVectorRegs <<= 1;
1437 }
1438
1439 NumIntermediates = NumVectorRegs;
1440
1441 EVT NewVT = EVT::getVectorVT(Context, EltTy, NumElts);
1442 if (!isTypeLegal(NewVT))
1443 NewVT = EltTy;
1444 IntermediateVT = NewVT;
1445
1446 MVT DestVT = getRegisterType(Context, NewVT);
1447 RegisterVT = DestVT;
1448 unsigned NewVTSize = NewVT.getSizeInBits();
1449
1450 // Convert sizes such as i33 to i64.
1451 if (!isPowerOf2_32(NewVTSize))
1452 NewVTSize = NextPowerOf2(NewVTSize);
1453
1454 if (EVT(DestVT).bitsLT(NewVT)) // Value is expanded, e.g. i64 -> i16.
1455 return NumVectorRegs*(NewVTSize/DestVT.getSizeInBits());
1456
1457 // Otherwise, promotion or legal types use the same number of registers as
1458 // the vector decimated to the appropriate level.
1459 return NumVectorRegs;
1460}
1461
Fangrui Song44d0c3d2019-10-31 13:58:52 -07001462bool TargetLoweringBase::isSuitableForJumpTable(const SwitchInst *SI,
1463 uint64_t NumCases,
1464 uint64_t Range,
1465 ProfileSummaryInfo *PSI,
1466 BlockFrequencyInfo *BFI) const {
1467 // FIXME: This function check the maximum table size and density, but the
1468 // minimum size is not checked. It would be nice if the minimum size is
1469 // also combined within this function. Currently, the minimum size check is
1470 // performed in findJumpTable() in SelectionDAGBuiler and
1471 // getEstimatedNumberOfCaseClusters() in BasicTTIImpl.
1472 const bool OptForSize =
1473 SI->getParent()->getParent()->hasOptSize() ||
1474 llvm::shouldOptimizeForSize(SI->getParent(), PSI, BFI);
1475 const unsigned MinDensity = getMinimumJumpTableDensity(OptForSize);
1476 const unsigned MaxJumpTableSize = getMaximumJumpTableSize();
1477
1478 // Check whether the number of cases is small enough and
1479 // the range is dense enough for a jump table.
1480 return (OptForSize || Range <= MaxJumpTableSize) &&
1481 (NumCases * 100 >= Range * MinDensity);
1482}
1483
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001484/// Get the EVTs and ArgFlags collections that represent the legalized return
1485/// type of the given function. This does not require a DAG or a return value,
1486/// and is suitable for use before any DAGs for the function are constructed.
1487/// TODO: Move this out of TargetLowering.cpp.
Matt Arsenault81920b02018-07-28 13:25:19 +00001488void llvm::GetReturnInfo(CallingConv::ID CC, Type *ReturnType,
1489 AttributeList attr,
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001490 SmallVectorImpl<ISD::OutputArg> &Outs,
Mehdi Amini56228da2015-07-09 01:57:34 +00001491 const TargetLowering &TLI, const DataLayout &DL) {
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001492 SmallVector<EVT, 4> ValueVTs;
Mehdi Amini56228da2015-07-09 01:57:34 +00001493 ComputeValueVTs(TLI, DL, ReturnType, ValueVTs);
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001494 unsigned NumValues = ValueVTs.size();
1495 if (NumValues == 0) return;
1496
1497 for (unsigned j = 0, f = NumValues; j != f; ++j) {
1498 EVT VT = ValueVTs[j];
1499 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
1500
Reid Klecknerb5180542017-03-21 16:57:19 +00001501 if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::SExt))
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001502 ExtendKind = ISD::SIGN_EXTEND;
Reid Klecknerb5180542017-03-21 16:57:19 +00001503 else if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::ZExt))
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001504 ExtendKind = ISD::ZERO_EXTEND;
1505
1506 // FIXME: C calling convention requires the return type to be promoted to
1507 // at least 32-bit. But this is not necessary for non-C calling
1508 // conventions. The frontend should mark functions whose return values
1509 // require promoting with signext or zeroext attributes.
1510 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
1511 MVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
1512 if (VT.bitsLT(MinVT))
1513 VT = MinVT;
1514 }
1515
Simon Dardis212cccb2017-06-09 14:37:08 +00001516 unsigned NumParts =
Matt Arsenault81920b02018-07-28 13:25:19 +00001517 TLI.getNumRegistersForCallingConv(ReturnType->getContext(), CC, VT);
Simon Dardis212cccb2017-06-09 14:37:08 +00001518 MVT PartVT =
Matt Arsenault81920b02018-07-28 13:25:19 +00001519 TLI.getRegisterTypeForCallingConv(ReturnType->getContext(), CC, VT);
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001520
1521 // 'inreg' on function refers to return value
1522 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Reid Klecknerb5180542017-03-21 16:57:19 +00001523 if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::InReg))
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001524 Flags.setInReg();
1525
1526 // Propagate extension type if any
Reid Klecknerb5180542017-03-21 16:57:19 +00001527 if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::SExt))
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001528 Flags.setSExt();
Reid Klecknerb5180542017-03-21 16:57:19 +00001529 else if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::ZExt))
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001530 Flags.setZExt();
1531
1532 for (unsigned i = 0; i < NumParts; ++i)
Rui Ueyama49a3ad22019-07-16 04:46:31 +00001533 Outs.push_back(ISD::OutputArg(Flags, PartVT, VT, /*isfixed=*/true, 0, 0));
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001534 }
1535}
1536
1537/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1538/// function arguments in the caller parameter area. This is the actual
1539/// alignment, not its logarithm.
Mehdi Amini5c183d52015-07-09 02:09:28 +00001540unsigned TargetLoweringBase::getByValTypeAlignment(Type *Ty,
1541 const DataLayout &DL) const {
1542 return DL.getABITypeAlignment(Ty);
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001543}
1544
Thomas Raoux3c8c6672019-09-26 00:16:01 +00001545bool TargetLoweringBase::allowsMemoryAccessForAlignment(
1546 LLVMContext &Context, const DataLayout &DL, EVT VT, unsigned AddrSpace,
1547 unsigned Alignment, MachineMemOperand::Flags Flags, bool *Fast) const {
Sanjay Patel0f9dcf82015-07-29 18:24:18 +00001548 // Check if the specified alignment is sufficient based on the data layout.
1549 // TODO: While using the data layout works in practice, a better solution
1550 // would be to implement this check directly (make this a virtual function).
1551 // For example, the ABI alignment may change based on software platform while
1552 // this function should only be affected by hardware implementation.
1553 Type *Ty = VT.getTypeForEVT(Context);
1554 if (Alignment >= DL.getABITypeAlignment(Ty)) {
1555 // Assume that an access that meets the ABI-specified alignment is fast.
1556 if (Fast != nullptr)
1557 *Fast = true;
1558 return true;
1559 }
Fangrui Songf78650a2018-07-30 19:41:25 +00001560
Sanjay Patel0f9dcf82015-07-29 18:24:18 +00001561 // This is a misaligned access.
Simon Pilgrim4e0648a2019-06-12 17:14:03 +00001562 return allowsMisalignedMemoryAccesses(VT, AddrSpace, Alignment, Flags, Fast);
Sanjay Patel0f9dcf82015-07-29 18:24:18 +00001563}
1564
Thomas Raoux3c8c6672019-09-26 00:16:01 +00001565bool TargetLoweringBase::allowsMemoryAccessForAlignment(
1566 LLVMContext &Context, const DataLayout &DL, EVT VT,
1567 const MachineMemOperand &MMO, bool *Fast) const {
1568 return allowsMemoryAccessForAlignment(Context, DL, VT, MMO.getAddrSpace(),
1569 MMO.getAlignment(), MMO.getFlags(),
1570 Fast);
1571}
1572
1573bool TargetLoweringBase::allowsMemoryAccess(
1574 LLVMContext &Context, const DataLayout &DL, EVT VT, unsigned AddrSpace,
1575 unsigned Alignment, MachineMemOperand::Flags Flags, bool *Fast) const {
1576 return allowsMemoryAccessForAlignment(Context, DL, VT, AddrSpace, Alignment,
1577 Flags, Fast);
1578}
1579
Simon Pilgrim266f4392019-06-11 11:00:23 +00001580bool TargetLoweringBase::allowsMemoryAccess(LLVMContext &Context,
1581 const DataLayout &DL, EVT VT,
1582 const MachineMemOperand &MMO,
1583 bool *Fast) const {
1584 return allowsMemoryAccess(Context, DL, VT, MMO.getAddrSpace(),
Simon Pilgrim4e0648a2019-06-12 17:14:03 +00001585 MMO.getAlignment(), MMO.getFlags(), Fast);
Simon Pilgrim266f4392019-06-11 11:00:23 +00001586}
1587
Sanjay Pateld66607b2016-04-26 17:11:17 +00001588BranchProbability TargetLoweringBase::getPredictableBranchThreshold() const {
1589 return BranchProbability(MinPercentageForPredictableBranch, 100);
1590}
Sanjay Patel0f9dcf82015-07-29 18:24:18 +00001591
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001592//===----------------------------------------------------------------------===//
1593// TargetTransformInfo Helpers
1594//===----------------------------------------------------------------------===//
1595
1596int TargetLoweringBase::InstructionOpcodeToISD(unsigned Opcode) const {
1597 enum InstructionOpcodes {
1598#define HANDLE_INST(NUM, OPCODE, CLASS) OPCODE = NUM,
1599#define LAST_OTHER_INST(NUM) InstructionOpcodesCount = NUM
1600#include "llvm/IR/Instruction.def"
1601 };
1602 switch (static_cast<InstructionOpcodes>(Opcode)) {
1603 case Ret: return 0;
1604 case Br: return 0;
1605 case Switch: return 0;
1606 case IndirectBr: return 0;
1607 case Invoke: return 0;
Craig Topper784929d2019-02-08 20:48:56 +00001608 case CallBr: return 0;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001609 case Resume: return 0;
1610 case Unreachable: return 0;
David Majnemer654e1302015-07-31 17:58:14 +00001611 case CleanupRet: return 0;
David Majnemer654e1302015-07-31 17:58:14 +00001612 case CatchRet: return 0;
David Majnemer8a1c45d2015-12-12 05:38:55 +00001613 case CatchPad: return 0;
1614 case CatchSwitch: return 0;
David Majnemer8a1c45d2015-12-12 05:38:55 +00001615 case CleanupPad: return 0;
Cameron McInallycbde0d92018-11-13 18:15:47 +00001616 case FNeg: return ISD::FNEG;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001617 case Add: return ISD::ADD;
1618 case FAdd: return ISD::FADD;
1619 case Sub: return ISD::SUB;
1620 case FSub: return ISD::FSUB;
1621 case Mul: return ISD::MUL;
1622 case FMul: return ISD::FMUL;
1623 case UDiv: return ISD::UDIV;
Benjamin Kramerce4b3fe2014-04-27 18:47:54 +00001624 case SDiv: return ISD::SDIV;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001625 case FDiv: return ISD::FDIV;
1626 case URem: return ISD::UREM;
1627 case SRem: return ISD::SREM;
1628 case FRem: return ISD::FREM;
1629 case Shl: return ISD::SHL;
1630 case LShr: return ISD::SRL;
1631 case AShr: return ISD::SRA;
1632 case And: return ISD::AND;
1633 case Or: return ISD::OR;
1634 case Xor: return ISD::XOR;
1635 case Alloca: return 0;
1636 case Load: return ISD::LOAD;
1637 case Store: return ISD::STORE;
1638 case GetElementPtr: return 0;
1639 case Fence: return 0;
1640 case AtomicCmpXchg: return 0;
1641 case AtomicRMW: return 0;
1642 case Trunc: return ISD::TRUNCATE;
1643 case ZExt: return ISD::ZERO_EXTEND;
1644 case SExt: return ISD::SIGN_EXTEND;
1645 case FPToUI: return ISD::FP_TO_UINT;
1646 case FPToSI: return ISD::FP_TO_SINT;
1647 case UIToFP: return ISD::UINT_TO_FP;
1648 case SIToFP: return ISD::SINT_TO_FP;
1649 case FPTrunc: return ISD::FP_ROUND;
1650 case FPExt: return ISD::FP_EXTEND;
1651 case PtrToInt: return ISD::BITCAST;
1652 case IntToPtr: return ISD::BITCAST;
1653 case BitCast: return ISD::BITCAST;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00001654 case AddrSpaceCast: return ISD::ADDRSPACECAST;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001655 case ICmp: return ISD::SETCC;
1656 case FCmp: return ISD::SETCC;
1657 case PHI: return 0;
1658 case Call: return 0;
1659 case Select: return ISD::SELECT;
1660 case UserOp1: return 0;
1661 case UserOp2: return 0;
1662 case VAArg: return 0;
1663 case ExtractElement: return ISD::EXTRACT_VECTOR_ELT;
1664 case InsertElement: return ISD::INSERT_VECTOR_ELT;
1665 case ShuffleVector: return ISD::VECTOR_SHUFFLE;
1666 case ExtractValue: return ISD::MERGE_VALUES;
1667 case InsertValue: return ISD::MERGE_VALUES;
1668 case LandingPad: return 0;
aqjune58acbce2019-11-05 15:53:22 +09001669 case Freeze: return 0;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001670 }
1671
1672 llvm_unreachable("Unknown instruction type encountered!");
1673}
1674
Chandler Carruth93205eb2015-08-05 18:08:10 +00001675std::pair<int, MVT>
Mehdi Amini44ede332015-07-09 02:09:04 +00001676TargetLoweringBase::getTypeLegalizationCost(const DataLayout &DL,
1677 Type *Ty) const {
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001678 LLVMContext &C = Ty->getContext();
Mehdi Amini44ede332015-07-09 02:09:04 +00001679 EVT MTy = getValueType(DL, Ty);
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001680
Chandler Carruth93205eb2015-08-05 18:08:10 +00001681 int Cost = 1;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001682 // We keep legalizing the type until we find a legal kind. We assume that
1683 // the only operation that costs anything is the split. After splitting
1684 // we need to handle two types.
1685 while (true) {
1686 LegalizeKind LK = getTypeConversion(C, MTy);
1687
1688 if (LK.first == TypeLegal)
1689 return std::make_pair(Cost, MTy.getSimpleVT());
1690
1691 if (LK.first == TypeSplitVector || LK.first == TypeExpandInteger)
1692 Cost *= 2;
1693
Chih-Hung Hsiehed7d81e2015-12-03 22:02:40 +00001694 // Do not loop with f128 type.
1695 if (MTy == LK.second)
1696 return std::make_pair(Cost, MTy.getSimpleVT());
1697
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001698 // Keep legalizing the type.
1699 MTy = LK.second;
1700 }
1701}
1702
David L Kreitzerd5c67552016-10-14 17:56:00 +00001703Value *TargetLoweringBase::getDefaultSafeStackPointerLocation(IRBuilder<> &IRB,
1704 bool UseTLS) const {
1705 // compiler-rt provides a variable with a magic name. Targets that do not
1706 // link with compiler-rt may also provide such a variable.
1707 Module *M = IRB.GetInsertBlock()->getParent()->getParent();
1708 const char *UnsafeStackPtrVar = "__safestack_unsafe_stack_ptr";
1709 auto UnsafeStackPtr =
1710 dyn_cast_or_null<GlobalVariable>(M->getNamedValue(UnsafeStackPtrVar));
1711
1712 Type *StackPtrTy = Type::getInt8PtrTy(M->getContext());
1713
1714 if (!UnsafeStackPtr) {
1715 auto TLSModel = UseTLS ?
1716 GlobalValue::InitialExecTLSModel :
1717 GlobalValue::NotThreadLocal;
1718 // The global variable is not defined yet, define it ourselves.
1719 // We use the initial-exec TLS model because we do not support the
1720 // variable living anywhere other than in the main executable.
1721 UnsafeStackPtr = new GlobalVariable(
1722 *M, StackPtrTy, false, GlobalValue::ExternalLinkage, nullptr,
1723 UnsafeStackPtrVar, nullptr, TLSModel);
1724 } else {
1725 // The variable exists, check its type and attributes.
1726 if (UnsafeStackPtr->getValueType() != StackPtrTy)
1727 report_fatal_error(Twine(UnsafeStackPtrVar) + " must have void* type");
1728 if (UseTLS != UnsafeStackPtr->isThreadLocal())
1729 report_fatal_error(Twine(UnsafeStackPtrVar) + " must " +
1730 (UseTLS ? "" : "not ") + "be thread-local");
1731 }
1732 return UnsafeStackPtr;
1733}
1734
Evgeniy Stepanovd1aad262015-10-26 18:28:25 +00001735Value *TargetLoweringBase::getSafeStackPointerLocation(IRBuilder<> &IRB) const {
1736 if (!TM.getTargetTriple().isAndroid())
David L Kreitzerd5c67552016-10-14 17:56:00 +00001737 return getDefaultSafeStackPointerLocation(IRB, true);
Evgeniy Stepanovd1aad262015-10-26 18:28:25 +00001738
1739 // Android provides a libc function to retrieve the address of the current
1740 // thread's unsafe stack pointer.
1741 Module *M = IRB.GetInsertBlock()->getParent()->getParent();
1742 Type *StackPtrTy = Type::getInt8PtrTy(M->getContext());
James Y Knight13680222019-02-01 02:28:03 +00001743 FunctionCallee Fn = M->getOrInsertFunction("__safestack_pointer_address",
1744 StackPtrTy->getPointerTo(0));
Evgeniy Stepanovd1aad262015-10-26 18:28:25 +00001745 return IRB.CreateCall(Fn);
1746}
1747
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001748//===----------------------------------------------------------------------===//
1749// Loop Strength Reduction hooks
1750//===----------------------------------------------------------------------===//
1751
1752/// isLegalAddressingMode - Return true if the addressing mode represented
1753/// by AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00001754bool TargetLoweringBase::isLegalAddressingMode(const DataLayout &DL,
1755 const AddrMode &AM, Type *Ty,
Jonas Paulsson024e3192017-07-21 11:59:37 +00001756 unsigned AS, Instruction *I) const {
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001757 // The default implementation of this implements a conservative RISCy, r+r and
1758 // r+i addr mode.
1759
1760 // Allows a sign-extended 16-bit immediate field.
1761 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
1762 return false;
1763
1764 // No global is ever allowed as a base.
1765 if (AM.BaseGV)
1766 return false;
1767
1768 // Only support r+r,
1769 switch (AM.Scale) {
1770 case 0: // "r+i" or just "i", depending on HasBaseReg.
1771 break;
1772 case 1:
1773 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
1774 return false;
1775 // Otherwise we have r+r or r+i.
1776 break;
1777 case 2:
1778 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
1779 return false;
1780 // Allow 2*r as r+r.
1781 break;
Tom Stellard728d4172014-02-14 21:10:34 +00001782 default: // Don't allow n * r
1783 return false;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +00001784 }
1785
1786 return true;
1787}
Tim Shen00127562016-04-08 21:26:31 +00001788
1789//===----------------------------------------------------------------------===//
1790// Stack Protector
1791//===----------------------------------------------------------------------===//
1792
1793// For OpenBSD return its special guard variable. Otherwise return nullptr,
1794// so that SelectionDAG handle SSP.
1795Value *TargetLoweringBase::getIRStackGuard(IRBuilder<> &IRB) const {
1796 if (getTargetMachine().getTargetTriple().isOSOpenBSD()) {
1797 Module &M = *IRB.GetInsertBlock()->getParent()->getParent();
1798 PointerType *PtrTy = Type::getInt8PtrTy(M.getContext());
Tim Shena5cc25e2016-08-22 18:26:27 +00001799 return M.getOrInsertGlobal("__guard_local", PtrTy);
Tim Shen00127562016-04-08 21:26:31 +00001800 }
1801 return nullptr;
1802}
1803
1804// Currently only support "standard" __stack_chk_guard.
1805// TODO: add LOAD_STACK_GUARD support.
1806void TargetLoweringBase::insertSSPDeclarations(Module &M) const {
Eli Friedman06441302018-04-21 00:07:46 +00001807 if (!M.getNamedValue("__stack_chk_guard"))
1808 new GlobalVariable(M, Type::getInt8PtrTy(M.getContext()), false,
1809 GlobalVariable::ExternalLinkage,
1810 nullptr, "__stack_chk_guard");
Tim Shen00127562016-04-08 21:26:31 +00001811}
1812
1813// Currently only support "standard" __stack_chk_guard.
1814// TODO: add LOAD_STACK_GUARD support.
Tim Shena1d8bc52016-04-19 20:14:52 +00001815Value *TargetLoweringBase::getSDagStackGuard(const Module &M) const {
Eli Friedman06441302018-04-21 00:07:46 +00001816 return M.getNamedValue("__stack_chk_guard");
Tim Shen00127562016-04-08 21:26:31 +00001817}
Etienne Bergeron22bfa832016-06-07 20:15:35 +00001818
James Y Knight7976eb52019-02-01 20:43:25 +00001819Function *TargetLoweringBase::getSSPStackGuardCheck(const Module &M) const {
Etienne Bergeron22bfa832016-06-07 20:15:35 +00001820 return nullptr;
1821}
Evandro Menezese45de8a2016-09-26 15:32:33 +00001822
Evandro Menezeseb97e352016-10-25 19:53:51 +00001823unsigned TargetLoweringBase::getMinimumJumpTableEntries() const {
1824 return MinimumJumpTableEntries;
1825}
1826
1827void TargetLoweringBase::setMinimumJumpTableEntries(unsigned Val) {
1828 MinimumJumpTableEntries = Val;
1829}
1830
Jun Bum Lim919f9e82017-04-28 16:04:03 +00001831unsigned TargetLoweringBase::getMinimumJumpTableDensity(bool OptForSize) const {
1832 return OptForSize ? OptsizeJumpTableDensity : JumpTableDensity;
1833}
1834
Hans Wennborg3740ae32019-09-27 09:54:26 +00001835unsigned TargetLoweringBase::getMaximumJumpTableSize() const {
1836 return MaximumJumpTableSize;
1837}
1838
1839void TargetLoweringBase::setMaximumJumpTableSize(unsigned Val) {
1840 MaximumJumpTableSize = Val;
1841}
1842
Sanjay Patel0051efc2016-10-20 16:55:45 +00001843//===----------------------------------------------------------------------===//
1844// Reciprocal Estimates
1845//===----------------------------------------------------------------------===//
1846
1847/// Get the reciprocal estimate attribute string for a function that will
1848/// override the target defaults.
1849static StringRef getRecipEstimateForFunc(MachineFunction &MF) {
Matthias Braunf1caa282017-12-15 22:22:58 +00001850 const Function &F = MF.getFunction();
1851 return F.getFnAttribute("reciprocal-estimates").getValueAsString();
Sanjay Patel0051efc2016-10-20 16:55:45 +00001852}
1853
1854/// Construct a string for the given reciprocal operation of the given type.
1855/// This string should match the corresponding option to the front-end's
1856/// "-mrecip" flag assuming those strings have been passed through in an
1857/// attribute string. For example, "vec-divf" for a division of a vXf32.
1858static std::string getReciprocalOpName(bool IsSqrt, EVT VT) {
1859 std::string Name = VT.isVector() ? "vec-" : "";
1860
1861 Name += IsSqrt ? "sqrt" : "div";
1862
1863 // TODO: Handle "half" or other float types?
1864 if (VT.getScalarType() == MVT::f64) {
1865 Name += "d";
1866 } else {
1867 assert(VT.getScalarType() == MVT::f32 &&
1868 "Unexpected FP type for reciprocal estimate");
1869 Name += "f";
1870 }
1871
1872 return Name;
1873}
1874
1875/// Return the character position and value (a single numeric character) of a
1876/// customized refinement operation in the input string if it exists. Return
1877/// false if there is no customized refinement step count.
1878static bool parseRefinementStep(StringRef In, size_t &Position,
1879 uint8_t &Value) {
1880 const char RefStepToken = ':';
1881 Position = In.find(RefStepToken);
1882 if (Position == StringRef::npos)
1883 return false;
1884
1885 StringRef RefStepString = In.substr(Position + 1);
1886 // Allow exactly one numeric character for the additional refinement
1887 // step parameter.
1888 if (RefStepString.size() == 1) {
1889 char RefStepChar = RefStepString[0];
1890 if (RefStepChar >= '0' && RefStepChar <= '9') {
1891 Value = RefStepChar - '0';
1892 return true;
1893 }
1894 }
1895 report_fatal_error("Invalid refinement step for -recip.");
1896}
1897
1898/// For the input attribute string, return one of the ReciprocalEstimate enum
1899/// status values (enabled, disabled, or not specified) for this operation on
1900/// the specified data type.
1901static int getOpEnabled(bool IsSqrt, EVT VT, StringRef Override) {
1902 if (Override.empty())
1903 return TargetLoweringBase::ReciprocalEstimate::Unspecified;
1904
1905 SmallVector<StringRef, 4> OverrideVector;
Craig Topper7413b322018-05-07 01:32:18 +00001906 Override.split(OverrideVector, ',');
Sanjay Patel0051efc2016-10-20 16:55:45 +00001907 unsigned NumArgs = OverrideVector.size();
1908
1909 // Check if "all", "none", or "default" was specified.
1910 if (NumArgs == 1) {
1911 // Look for an optional setting of the number of refinement steps needed
1912 // for this type of reciprocal operation.
1913 size_t RefPos;
1914 uint8_t RefSteps;
1915 if (parseRefinementStep(Override, RefPos, RefSteps)) {
1916 // Split the string for further processing.
1917 Override = Override.substr(0, RefPos);
1918 }
1919
1920 // All reciprocal types are enabled.
1921 if (Override == "all")
1922 return TargetLoweringBase::ReciprocalEstimate::Enabled;
1923
1924 // All reciprocal types are disabled.
1925 if (Override == "none")
1926 return TargetLoweringBase::ReciprocalEstimate::Disabled;
1927
1928 // Target defaults for enablement are used.
1929 if (Override == "default")
1930 return TargetLoweringBase::ReciprocalEstimate::Unspecified;
1931 }
1932
1933 // The attribute string may omit the size suffix ('f'/'d').
1934 std::string VTName = getReciprocalOpName(IsSqrt, VT);
1935 std::string VTNameNoSize = VTName;
Sanjay Patel501be9b2016-10-21 14:58:30 +00001936 VTNameNoSize.pop_back();
Sanjay Patel0051efc2016-10-20 16:55:45 +00001937 static const char DisabledPrefix = '!';
1938
1939 for (StringRef RecipType : OverrideVector) {
1940 size_t RefPos;
1941 uint8_t RefSteps;
1942 if (parseRefinementStep(RecipType, RefPos, RefSteps))
1943 RecipType = RecipType.substr(0, RefPos);
1944
1945 // Ignore the disablement token for string matching.
1946 bool IsDisabled = RecipType[0] == DisabledPrefix;
1947 if (IsDisabled)
1948 RecipType = RecipType.substr(1);
1949
1950 if (RecipType.equals(VTName) || RecipType.equals(VTNameNoSize))
1951 return IsDisabled ? TargetLoweringBase::ReciprocalEstimate::Disabled
1952 : TargetLoweringBase::ReciprocalEstimate::Enabled;
1953 }
1954
1955 return TargetLoweringBase::ReciprocalEstimate::Unspecified;
1956}
1957
1958/// For the input attribute string, return the customized refinement step count
1959/// for this operation on the specified data type. If the step count does not
1960/// exist, return the ReciprocalEstimate enum value for unspecified.
1961static int getOpRefinementSteps(bool IsSqrt, EVT VT, StringRef Override) {
1962 if (Override.empty())
1963 return TargetLoweringBase::ReciprocalEstimate::Unspecified;
1964
1965 SmallVector<StringRef, 4> OverrideVector;
Craig Topper7413b322018-05-07 01:32:18 +00001966 Override.split(OverrideVector, ',');
Sanjay Patel0051efc2016-10-20 16:55:45 +00001967 unsigned NumArgs = OverrideVector.size();
1968
1969 // Check if "all", "default", or "none" was specified.
1970 if (NumArgs == 1) {
1971 // Look for an optional setting of the number of refinement steps needed
1972 // for this type of reciprocal operation.
1973 size_t RefPos;
1974 uint8_t RefSteps;
1975 if (!parseRefinementStep(Override, RefPos, RefSteps))
1976 return TargetLoweringBase::ReciprocalEstimate::Unspecified;
1977
1978 // Split the string for further processing.
1979 Override = Override.substr(0, RefPos);
1980 assert(Override != "none" &&
1981 "Disabled reciprocals, but specifed refinement steps?");
1982
1983 // If this is a general override, return the specified number of steps.
1984 if (Override == "all" || Override == "default")
1985 return RefSteps;
1986 }
1987
1988 // The attribute string may omit the size suffix ('f'/'d').
1989 std::string VTName = getReciprocalOpName(IsSqrt, VT);
1990 std::string VTNameNoSize = VTName;
Sanjay Patel501be9b2016-10-21 14:58:30 +00001991 VTNameNoSize.pop_back();
Sanjay Patel0051efc2016-10-20 16:55:45 +00001992
1993 for (StringRef RecipType : OverrideVector) {
1994 size_t RefPos;
1995 uint8_t RefSteps;
1996 if (!parseRefinementStep(RecipType, RefPos, RefSteps))
1997 continue;
1998
1999 RecipType = RecipType.substr(0, RefPos);
2000 if (RecipType.equals(VTName) || RecipType.equals(VTNameNoSize))
2001 return RefSteps;
2002 }
2003
2004 return TargetLoweringBase::ReciprocalEstimate::Unspecified;
2005}
2006
2007int TargetLoweringBase::getRecipEstimateSqrtEnabled(EVT VT,
2008 MachineFunction &MF) const {
2009 return getOpEnabled(true, VT, getRecipEstimateForFunc(MF));
2010}
2011
2012int TargetLoweringBase::getRecipEstimateDivEnabled(EVT VT,
2013 MachineFunction &MF) const {
2014 return getOpEnabled(false, VT, getRecipEstimateForFunc(MF));
2015}
2016
2017int TargetLoweringBase::getSqrtRefinementSteps(EVT VT,
2018 MachineFunction &MF) const {
2019 return getOpRefinementSteps(true, VT, getRecipEstimateForFunc(MF));
2020}
2021
2022int TargetLoweringBase::getDivRefinementSteps(EVT VT,
2023 MachineFunction &MF) const {
2024 return getOpRefinementSteps(false, VT, getRecipEstimateForFunc(MF));
2025}
Matthias Braun744c2152017-04-28 20:25:05 +00002026
2027void TargetLoweringBase::finalizeLowering(MachineFunction &MF) const {
2028 MF.getRegInfo().freezeReservedRegs(MF);
2029}