LLVM 22.0.0git
SelectionDAGBuilder.cpp
Go to the documentation of this file.
1//===- SelectionDAGBuilder.cpp - Selection-DAG building -------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This implements routines for translating from LLVM IR into SelectionDAG IR.
10//
11//===----------------------------------------------------------------------===//
12
13#include "SelectionDAGBuilder.h"
14#include "SDNodeDbgValue.h"
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/BitVector.h"
18#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/ADT/Twine.h"
26#include "llvm/Analysis/Loads.h"
57#include "llvm/IR/Argument.h"
58#include "llvm/IR/Attributes.h"
59#include "llvm/IR/BasicBlock.h"
60#include "llvm/IR/CFG.h"
61#include "llvm/IR/CallingConv.h"
62#include "llvm/IR/Constant.h"
64#include "llvm/IR/Constants.h"
65#include "llvm/IR/DataLayout.h"
66#include "llvm/IR/DebugInfo.h"
71#include "llvm/IR/Function.h"
73#include "llvm/IR/InlineAsm.h"
74#include "llvm/IR/InstrTypes.h"
77#include "llvm/IR/Intrinsics.h"
78#include "llvm/IR/IntrinsicsAArch64.h"
79#include "llvm/IR/IntrinsicsAMDGPU.h"
80#include "llvm/IR/IntrinsicsWebAssembly.h"
81#include "llvm/IR/LLVMContext.h"
83#include "llvm/IR/Metadata.h"
84#include "llvm/IR/Module.h"
85#include "llvm/IR/Operator.h"
87#include "llvm/IR/Statepoint.h"
88#include "llvm/IR/Type.h"
89#include "llvm/IR/User.h"
90#include "llvm/IR/Value.h"
91#include "llvm/MC/MCContext.h"
96#include "llvm/Support/Debug.h"
104#include <cstddef>
105#include <limits>
106#include <optional>
107#include <tuple>
108
109using namespace llvm;
110using namespace PatternMatch;
111using namespace SwitchCG;
112
113#define DEBUG_TYPE "isel"
114
115/// LimitFloatPrecision - Generate low-precision inline sequences for
116/// some float libcalls (6, 8 or 12 bits).
117static unsigned LimitFloatPrecision;
118
119static cl::opt<bool>
120 InsertAssertAlign("insert-assert-align", cl::init(true),
121 cl::desc("Insert the experimental `assertalign` node."),
123
125 LimitFPPrecision("limit-float-precision",
126 cl::desc("Generate low-precision inline sequences "
127 "for some float libcalls"),
129 cl::init(0));
130
132 "switch-peel-threshold", cl::Hidden, cl::init(66),
133 cl::desc("Set the case probability threshold for peeling the case from a "
134 "switch statement. A value greater than 100 will void this "
135 "optimization"));
136
137// Limit the width of DAG chains. This is important in general to prevent
138// DAG-based analysis from blowing up. For example, alias analysis and
139// load clustering may not complete in reasonable time. It is difficult to
140// recognize and avoid this situation within each individual analysis, and
141// future analyses are likely to have the same behavior. Limiting DAG width is
142// the safe approach and will be especially important with global DAGs.
143//
144// MaxParallelChains default is arbitrarily high to avoid affecting
145// optimization, but could be lowered to improve compile time. Any ld-ld-st-st
146// sequence over this should have been converted to llvm.memcpy by the
147// frontend. It is easy to induce this behavior with .ll code such as:
148// %buffer = alloca [4096 x i8]
149// %data = load [4096 x i8]* %argPtr
150// store [4096 x i8] %data, [4096 x i8]* %buffer
151static const unsigned MaxParallelChains = 64;
152
154 const SDValue *Parts, unsigned NumParts,
155 MVT PartVT, EVT ValueVT, const Value *V,
156 SDValue InChain,
157 std::optional<CallingConv::ID> CC);
158
159/// getCopyFromParts - Create a value that contains the specified legal parts
160/// combined into the value they represent. If the parts combine to a type
161/// larger than ValueVT then AssertOp can be used to specify whether the extra
162/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
163/// (ISD::AssertSext).
164static SDValue
165getCopyFromParts(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts,
166 unsigned NumParts, MVT PartVT, EVT ValueVT, const Value *V,
167 SDValue InChain,
168 std::optional<CallingConv::ID> CC = std::nullopt,
169 std::optional<ISD::NodeType> AssertOp = std::nullopt) {
170 // Let the target assemble the parts if it wants to
171 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
172 if (SDValue Val = TLI.joinRegisterPartsIntoValue(DAG, DL, Parts, NumParts,
173 PartVT, ValueVT, CC))
174 return Val;
175
176 if (ValueVT.isVector())
177 return getCopyFromPartsVector(DAG, DL, Parts, NumParts, PartVT, ValueVT, V,
178 InChain, CC);
179
180 assert(NumParts > 0 && "No parts to assemble!");
181 SDValue Val = Parts[0];
182
183 if (NumParts > 1) {
184 // Assemble the value from multiple parts.
185 if (ValueVT.isInteger()) {
186 unsigned PartBits = PartVT.getSizeInBits();
187 unsigned ValueBits = ValueVT.getSizeInBits();
188
189 // Assemble the power of 2 part.
190 unsigned RoundParts = llvm::bit_floor(NumParts);
191 unsigned RoundBits = PartBits * RoundParts;
192 EVT RoundVT = RoundBits == ValueBits ?
193 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
194 SDValue Lo, Hi;
195
196 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
197
198 if (RoundParts > 2) {
199 Lo = getCopyFromParts(DAG, DL, Parts, RoundParts / 2, PartVT, HalfVT, V,
200 InChain);
201 Hi = getCopyFromParts(DAG, DL, Parts + RoundParts / 2, RoundParts / 2,
202 PartVT, HalfVT, V, InChain);
203 } else {
204 Lo = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[0]);
205 Hi = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[1]);
206 }
207
208 if (DAG.getDataLayout().isBigEndian())
209 std::swap(Lo, Hi);
210
211 Val = DAG.getNode(ISD::BUILD_PAIR, DL, RoundVT, Lo, Hi);
212
213 if (RoundParts < NumParts) {
214 // Assemble the trailing non-power-of-2 part.
215 unsigned OddParts = NumParts - RoundParts;
216 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
217 Hi = getCopyFromParts(DAG, DL, Parts + RoundParts, OddParts, PartVT,
218 OddVT, V, InChain, CC);
219
220 // Combine the round and odd parts.
221 Lo = Val;
222 if (DAG.getDataLayout().isBigEndian())
223 std::swap(Lo, Hi);
224 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
225 Hi = DAG.getNode(ISD::ANY_EXTEND, DL, TotalVT, Hi);
226 Hi = DAG.getNode(
227 ISD::SHL, DL, TotalVT, Hi,
228 DAG.getShiftAmountConstant(Lo.getValueSizeInBits(), TotalVT, DL));
229 Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, TotalVT, Lo);
230 Val = DAG.getNode(ISD::OR, DL, TotalVT, Lo, Hi);
231 }
232 } else if (PartVT.isFloatingPoint()) {
233 // FP split into multiple FP parts (for ppcf128)
234 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == MVT::f64 &&
235 "Unexpected split");
236 SDValue Lo, Hi;
237 Lo = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[0]);
238 Hi = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[1]);
239 if (TLI.hasBigEndianPartOrdering(ValueVT, DAG.getDataLayout()))
240 std::swap(Lo, Hi);
241 Val = DAG.getNode(ISD::BUILD_PAIR, DL, ValueVT, Lo, Hi);
242 } else {
243 // FP split into integer parts (soft fp)
244 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
245 !PartVT.isVector() && "Unexpected split");
246 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
247 Val = getCopyFromParts(DAG, DL, Parts, NumParts, PartVT, IntVT, V,
248 InChain, CC);
249 }
250 }
251
252 // There is now one part, held in Val. Correct it to match ValueVT.
253 // PartEVT is the type of the register class that holds the value.
254 // ValueVT is the type of the inline asm operation.
255 EVT PartEVT = Val.getValueType();
256
257 if (PartEVT == ValueVT)
258 return Val;
259
260 if (PartEVT.isInteger() && ValueVT.isFloatingPoint() &&
261 ValueVT.bitsLT(PartEVT)) {
262 // For an FP value in an integer part, we need to truncate to the right
263 // width first.
264 PartEVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
265 Val = DAG.getNode(ISD::TRUNCATE, DL, PartEVT, Val);
266 }
267
268 // Handle types that have the same size.
269 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits())
270 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
271
272 // Handle types with different sizes.
273 if (PartEVT.isInteger() && ValueVT.isInteger()) {
274 if (ValueVT.bitsLT(PartEVT)) {
275 // For a truncate, see if we have any information to
276 // indicate whether the truncated bits will always be
277 // zero or sign-extension.
278 if (AssertOp)
279 Val = DAG.getNode(*AssertOp, DL, PartEVT, Val,
280 DAG.getValueType(ValueVT));
281 return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
282 }
283 return DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
284 }
285
286 if (PartEVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
287 // FP_ROUND's are always exact here.
288 if (ValueVT.bitsLT(Val.getValueType())) {
289
290 SDValue NoChange =
292
293 if (DAG.getMachineFunction().getFunction().getAttributes().hasFnAttr(
294 llvm::Attribute::StrictFP)) {
295 return DAG.getNode(ISD::STRICT_FP_ROUND, DL,
296 DAG.getVTList(ValueVT, MVT::Other), InChain, Val,
297 NoChange);
298 }
299
300 return DAG.getNode(ISD::FP_ROUND, DL, ValueVT, Val, NoChange);
301 }
302
303 return DAG.getNode(ISD::FP_EXTEND, DL, ValueVT, Val);
304 }
305
306 // Handle MMX to a narrower integer type by bitcasting MMX to integer and
307 // then truncating.
308 if (PartEVT == MVT::x86mmx && ValueVT.isInteger() &&
309 ValueVT.bitsLT(PartEVT)) {
310 Val = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Val);
311 return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
312 }
313
314 report_fatal_error("Unknown mismatch in getCopyFromParts!");
315}
316
318 const Twine &ErrMsg) {
320 if (!I)
321 return Ctx.emitError(ErrMsg);
322
323 if (const CallInst *CI = dyn_cast<CallInst>(I))
324 if (CI->isInlineAsm()) {
325 return Ctx.diagnose(DiagnosticInfoInlineAsm(
326 *CI, ErrMsg + ", possible invalid constraint for vector type"));
327 }
328
329 return Ctx.emitError(I, ErrMsg);
330}
331
332/// getCopyFromPartsVector - Create a value that contains the specified legal
333/// parts combined into the value they represent. If the parts combine to a
334/// type larger than ValueVT then AssertOp can be used to specify whether the
335/// extra bits are known to be zero (ISD::AssertZext) or sign extended from
336/// ValueVT (ISD::AssertSext).
338 const SDValue *Parts, unsigned NumParts,
339 MVT PartVT, EVT ValueVT, const Value *V,
340 SDValue InChain,
341 std::optional<CallingConv::ID> CallConv) {
342 assert(ValueVT.isVector() && "Not a vector value");
343 assert(NumParts > 0 && "No parts to assemble!");
344 const bool IsABIRegCopy = CallConv.has_value();
345
346 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
347 SDValue Val = Parts[0];
348
349 // Handle a multi-element vector.
350 if (NumParts > 1) {
351 EVT IntermediateVT;
352 MVT RegisterVT;
353 unsigned NumIntermediates;
354 unsigned NumRegs;
355
356 if (IsABIRegCopy) {
358 *DAG.getContext(), *CallConv, ValueVT, IntermediateVT,
359 NumIntermediates, RegisterVT);
360 } else {
361 NumRegs =
362 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
363 NumIntermediates, RegisterVT);
364 }
365
366 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
367 NumParts = NumRegs; // Silence a compiler warning.
368 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
369 assert(RegisterVT.getSizeInBits() ==
370 Parts[0].getSimpleValueType().getSizeInBits() &&
371 "Part type sizes don't match!");
372
373 // Assemble the parts into intermediate operands.
374 SmallVector<SDValue, 8> Ops(NumIntermediates);
375 if (NumIntermediates == NumParts) {
376 // If the register was not expanded, truncate or copy the value,
377 // as appropriate.
378 for (unsigned i = 0; i != NumParts; ++i)
379 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i], 1, PartVT, IntermediateVT,
380 V, InChain, CallConv);
381 } else if (NumParts > 0) {
382 // If the intermediate type was expanded, build the intermediate
383 // operands from the parts.
384 assert(NumParts % NumIntermediates == 0 &&
385 "Must expand into a divisible number of parts!");
386 unsigned Factor = NumParts / NumIntermediates;
387 for (unsigned i = 0; i != NumIntermediates; ++i)
388 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i * Factor], Factor, PartVT,
389 IntermediateVT, V, InChain, CallConv);
390 }
391
392 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
393 // intermediate operands.
394 EVT BuiltVectorTy =
395 IntermediateVT.isVector()
397 *DAG.getContext(), IntermediateVT.getScalarType(),
398 IntermediateVT.getVectorElementCount() * NumParts)
400 IntermediateVT.getScalarType(),
401 NumIntermediates);
402 Val = DAG.getNode(IntermediateVT.isVector() ? ISD::CONCAT_VECTORS
404 DL, BuiltVectorTy, Ops);
405 }
406
407 // There is now one part, held in Val. Correct it to match ValueVT.
408 EVT PartEVT = Val.getValueType();
409
410 if (PartEVT == ValueVT)
411 return Val;
412
413 if (PartEVT.isVector()) {
414 // Vector/Vector bitcast.
415 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits())
416 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
417
418 // If the parts vector has more elements than the value vector, then we
419 // have a vector widening case (e.g. <2 x float> -> <4 x float>).
420 // Extract the elements we want.
421 if (PartEVT.getVectorElementCount() != ValueVT.getVectorElementCount()) {
424 (PartEVT.getVectorElementCount().isScalable() ==
425 ValueVT.getVectorElementCount().isScalable()) &&
426 "Cannot narrow, it would be a lossy transformation");
427 PartEVT =
429 ValueVT.getVectorElementCount());
430 Val = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, PartEVT, Val,
431 DAG.getVectorIdxConstant(0, DL));
432 if (PartEVT == ValueVT)
433 return Val;
434 if (PartEVT.isInteger() && ValueVT.isFloatingPoint())
435 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
436
437 // Vector/Vector bitcast (e.g. <2 x bfloat> -> <2 x half>).
438 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits())
439 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
440 }
441
442 // Promoted vector extract
443 return DAG.getAnyExtOrTrunc(Val, DL, ValueVT);
444 }
445
446 // Trivial bitcast if the types are the same size and the destination
447 // vector type is legal.
448 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits() &&
449 TLI.isTypeLegal(ValueVT))
450 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
451
452 if (ValueVT.getVectorNumElements() != 1) {
453 // Certain ABIs require that vectors are passed as integers. For vectors
454 // are the same size, this is an obvious bitcast.
455 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits()) {
456 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
457 } else if (ValueVT.bitsLT(PartEVT)) {
458 const uint64_t ValueSize = ValueVT.getFixedSizeInBits();
459 EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize);
460 // Drop the extra bits.
461 Val = DAG.getNode(ISD::TRUNCATE, DL, IntermediateType, Val);
462 return DAG.getBitcast(ValueVT, Val);
463 }
464
466 *DAG.getContext(), V, "non-trivial scalar-to-vector conversion");
467 return DAG.getUNDEF(ValueVT);
468 }
469
470 // Handle cases such as i8 -> <1 x i1>
471 EVT ValueSVT = ValueVT.getVectorElementType();
472 if (ValueVT.getVectorNumElements() == 1 && ValueSVT != PartEVT) {
473 unsigned ValueSize = ValueSVT.getSizeInBits();
474 if (ValueSize == PartEVT.getSizeInBits()) {
475 Val = DAG.getNode(ISD::BITCAST, DL, ValueSVT, Val);
476 } else if (ValueSVT.isFloatingPoint() && PartEVT.isInteger()) {
477 // It's possible a scalar floating point type gets softened to integer and
478 // then promoted to a larger integer. If PartEVT is the larger integer
479 // we need to truncate it and then bitcast to the FP type.
480 assert(ValueSVT.bitsLT(PartEVT) && "Unexpected types");
481 EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize);
482 Val = DAG.getNode(ISD::TRUNCATE, DL, IntermediateType, Val);
483 Val = DAG.getBitcast(ValueSVT, Val);
484 } else {
485 Val = ValueVT.isFloatingPoint()
486 ? DAG.getFPExtendOrRound(Val, DL, ValueSVT)
487 : DAG.getAnyExtOrTrunc(Val, DL, ValueSVT);
488 }
489 }
490
491 return DAG.getBuildVector(ValueVT, DL, Val);
492}
493
494static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &dl,
495 SDValue Val, SDValue *Parts, unsigned NumParts,
496 MVT PartVT, const Value *V,
497 std::optional<CallingConv::ID> CallConv);
498
499/// getCopyToParts - Create a series of nodes that contain the specified value
500/// split into legal parts. If the parts contain more bits than Val, then, for
501/// integers, ExtendKind can be used to specify how to generate the extra bits.
502static void
504 unsigned NumParts, MVT PartVT, const Value *V,
505 std::optional<CallingConv::ID> CallConv = std::nullopt,
506 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
507 // Let the target split the parts if it wants to
508 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
509 if (TLI.splitValueIntoRegisterParts(DAG, DL, Val, Parts, NumParts, PartVT,
510 CallConv))
511 return;
512 EVT ValueVT = Val.getValueType();
513
514 // Handle the vector case separately.
515 if (ValueVT.isVector())
516 return getCopyToPartsVector(DAG, DL, Val, Parts, NumParts, PartVT, V,
517 CallConv);
518
519 unsigned OrigNumParts = NumParts;
521 "Copying to an illegal type!");
522
523 if (NumParts == 0)
524 return;
525
526 assert(!ValueVT.isVector() && "Vector case handled elsewhere");
527 EVT PartEVT = PartVT;
528 if (PartEVT == ValueVT) {
529 assert(NumParts == 1 && "No-op copy with multiple parts!");
530 Parts[0] = Val;
531 return;
532 }
533
534 unsigned PartBits = PartVT.getSizeInBits();
535 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
536 // If the parts cover more bits than the value has, promote the value.
537 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
538 assert(NumParts == 1 && "Do not know what to promote to!");
539 Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
540 } else {
541 if (ValueVT.isFloatingPoint()) {
542 // FP values need to be bitcast, then extended if they are being put
543 // into a larger container.
544 ValueVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
545 Val = DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
546 }
547 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
548 ValueVT.isInteger() &&
549 "Unknown mismatch!");
550 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
551 Val = DAG.getNode(ExtendKind, DL, ValueVT, Val);
552 if (PartVT == MVT::x86mmx)
553 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
554 }
555 } else if (PartBits == ValueVT.getSizeInBits()) {
556 // Different types of the same size.
557 assert(NumParts == 1 && PartEVT != ValueVT);
558 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
559 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
560 // If the parts cover less bits than value has, truncate the value.
561 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
562 ValueVT.isInteger() &&
563 "Unknown mismatch!");
564 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
565 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
566 if (PartVT == MVT::x86mmx)
567 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
568 }
569
570 // The value may have changed - recompute ValueVT.
571 ValueVT = Val.getValueType();
572 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
573 "Failed to tile the value with PartVT!");
574
575 if (NumParts == 1) {
576 if (PartEVT != ValueVT) {
578 "scalar-to-vector conversion failed");
579 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
580 }
581
582 Parts[0] = Val;
583 return;
584 }
585
586 // Expand the value into multiple parts.
587 if (NumParts & (NumParts - 1)) {
588 // The number of parts is not a power of 2. Split off and copy the tail.
589 assert(PartVT.isInteger() && ValueVT.isInteger() &&
590 "Do not know what to expand to!");
591 unsigned RoundParts = llvm::bit_floor(NumParts);
592 unsigned RoundBits = RoundParts * PartBits;
593 unsigned OddParts = NumParts - RoundParts;
594 SDValue OddVal = DAG.getNode(ISD::SRL, DL, ValueVT, Val,
595 DAG.getShiftAmountConstant(RoundBits, ValueVT, DL));
596
597 getCopyToParts(DAG, DL, OddVal, Parts + RoundParts, OddParts, PartVT, V,
598 CallConv);
599
600 if (DAG.getDataLayout().isBigEndian())
601 // The odd parts were reversed by getCopyToParts - unreverse them.
602 std::reverse(Parts + RoundParts, Parts + NumParts);
603
604 NumParts = RoundParts;
605 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
606 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
607 }
608
609 // The number of parts is a power of 2. Repeatedly bisect the value using
610 // EXTRACT_ELEMENT.
611 Parts[0] = DAG.getNode(ISD::BITCAST, DL,
613 ValueVT.getSizeInBits()),
614 Val);
615
616 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
617 for (unsigned i = 0; i < NumParts; i += StepSize) {
618 unsigned ThisBits = StepSize * PartBits / 2;
619 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
620 SDValue &Part0 = Parts[i];
621 SDValue &Part1 = Parts[i+StepSize/2];
622
623 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
624 ThisVT, Part0, DAG.getIntPtrConstant(1, DL));
625 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
626 ThisVT, Part0, DAG.getIntPtrConstant(0, DL));
627
628 if (ThisBits == PartBits && ThisVT != PartVT) {
629 Part0 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part0);
630 Part1 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part1);
631 }
632 }
633 }
634
635 if (DAG.getDataLayout().isBigEndian())
636 std::reverse(Parts, Parts + OrigNumParts);
637}
638
640 const SDLoc &DL, EVT PartVT) {
641 if (!PartVT.isVector())
642 return SDValue();
643
644 EVT ValueVT = Val.getValueType();
645 EVT PartEVT = PartVT.getVectorElementType();
646 EVT ValueEVT = ValueVT.getVectorElementType();
647 ElementCount PartNumElts = PartVT.getVectorElementCount();
648 ElementCount ValueNumElts = ValueVT.getVectorElementCount();
649
650 // We only support widening vectors with equivalent element types and
651 // fixed/scalable properties. If a target needs to widen a fixed-length type
652 // to a scalable one, it should be possible to use INSERT_SUBVECTOR below.
653 if (ElementCount::isKnownLE(PartNumElts, ValueNumElts) ||
654 PartNumElts.isScalable() != ValueNumElts.isScalable())
655 return SDValue();
656
657 // Have a try for bf16 because some targets share its ABI with fp16.
658 if (ValueEVT == MVT::bf16 && PartEVT == MVT::f16) {
660 "Cannot widen to illegal type");
661 Val = DAG.getNode(ISD::BITCAST, DL,
662 ValueVT.changeVectorElementType(MVT::f16), Val);
663 } else if (PartEVT != ValueEVT) {
664 return SDValue();
665 }
666
667 // Widening a scalable vector to another scalable vector is done by inserting
668 // the vector into a larger undef one.
669 if (PartNumElts.isScalable())
670 return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, PartVT, DAG.getUNDEF(PartVT),
671 Val, DAG.getVectorIdxConstant(0, DL));
672
673 // Vector widening case, e.g. <2 x float> -> <4 x float>. Shuffle in
674 // undef elements.
676 DAG.ExtractVectorElements(Val, Ops);
677 SDValue EltUndef = DAG.getUNDEF(PartEVT);
678 Ops.append((PartNumElts - ValueNumElts).getFixedValue(), EltUndef);
679
680 // FIXME: Use CONCAT for 2x -> 4x.
681 return DAG.getBuildVector(PartVT, DL, Ops);
682}
683
684/// getCopyToPartsVector - Create a series of nodes that contain the specified
685/// value split into legal parts.
686static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &DL,
687 SDValue Val, SDValue *Parts, unsigned NumParts,
688 MVT PartVT, const Value *V,
689 std::optional<CallingConv::ID> CallConv) {
690 EVT ValueVT = Val.getValueType();
691 assert(ValueVT.isVector() && "Not a vector");
692 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
693 const bool IsABIRegCopy = CallConv.has_value();
694
695 if (NumParts == 1) {
696 EVT PartEVT = PartVT;
697 if (PartEVT == ValueVT) {
698 // Nothing to do.
699 } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
700 // Bitconvert vector->vector case.
701 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
702 } else if (SDValue Widened = widenVectorToPartType(DAG, Val, DL, PartVT)) {
703 Val = Widened;
704 } else if (PartVT.isVector() &&
706 ValueVT.getVectorElementType()) &&
707 PartEVT.getVectorElementCount() ==
708 ValueVT.getVectorElementCount()) {
709
710 // Promoted vector extract
711 Val = DAG.getAnyExtOrTrunc(Val, DL, PartVT);
712 } else if (PartEVT.isVector() &&
713 PartEVT.getVectorElementType() !=
714 ValueVT.getVectorElementType() &&
715 TLI.getTypeAction(*DAG.getContext(), ValueVT) ==
717 // Combination of widening and promotion.
718 EVT WidenVT =
720 PartVT.getVectorElementCount());
721 SDValue Widened = widenVectorToPartType(DAG, Val, DL, WidenVT);
722 Val = DAG.getAnyExtOrTrunc(Widened, DL, PartVT);
723 } else {
724 // Don't extract an integer from a float vector. This can happen if the
725 // FP type gets softened to integer and then promoted. The promotion
726 // prevents it from being picked up by the earlier bitcast case.
727 if (ValueVT.getVectorElementCount().isScalar() &&
728 (!ValueVT.isFloatingPoint() || !PartVT.isInteger())) {
729 // If we reach this condition and PartVT is FP, this means that
730 // ValueVT is also FP and both have a different size, otherwise we
731 // would have bitcasted them. Producing an EXTRACT_VECTOR_ELT here
732 // would be invalid since that would mean the smaller FP type has to
733 // be extended to the larger one.
734 if (PartVT.isFloatingPoint()) {
735 Val = DAG.getBitcast(ValueVT.getScalarType(), Val);
736 Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
737 } else
738 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, PartVT, Val,
739 DAG.getVectorIdxConstant(0, DL));
740 } else {
741 uint64_t ValueSize = ValueVT.getFixedSizeInBits();
742 assert(PartVT.getFixedSizeInBits() > ValueSize &&
743 "lossy conversion of vector to scalar type");
744 EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize);
745 Val = DAG.getBitcast(IntermediateType, Val);
746 Val = DAG.getAnyExtOrTrunc(Val, DL, PartVT);
747 }
748 }
749
750 assert(Val.getValueType() == PartVT && "Unexpected vector part value type");
751 Parts[0] = Val;
752 return;
753 }
754
755 // Handle a multi-element vector.
756 EVT IntermediateVT;
757 MVT RegisterVT;
758 unsigned NumIntermediates;
759 unsigned NumRegs;
760 if (IsABIRegCopy) {
762 *DAG.getContext(), *CallConv, ValueVT, IntermediateVT, NumIntermediates,
763 RegisterVT);
764 } else {
765 NumRegs =
766 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
767 NumIntermediates, RegisterVT);
768 }
769
770 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
771 NumParts = NumRegs; // Silence a compiler warning.
772 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
773
774 assert(IntermediateVT.isScalableVector() == ValueVT.isScalableVector() &&
775 "Mixing scalable and fixed vectors when copying in parts");
776
777 std::optional<ElementCount> DestEltCnt;
778
779 if (IntermediateVT.isVector())
780 DestEltCnt = IntermediateVT.getVectorElementCount() * NumIntermediates;
781 else
782 DestEltCnt = ElementCount::getFixed(NumIntermediates);
783
784 EVT BuiltVectorTy = EVT::getVectorVT(
785 *DAG.getContext(), IntermediateVT.getScalarType(), *DestEltCnt);
786
787 if (ValueVT == BuiltVectorTy) {
788 // Nothing to do.
789 } else if (ValueVT.getSizeInBits() == BuiltVectorTy.getSizeInBits()) {
790 // Bitconvert vector->vector case.
791 Val = DAG.getNode(ISD::BITCAST, DL, BuiltVectorTy, Val);
792 } else {
793 if (BuiltVectorTy.getVectorElementType().bitsGT(
794 ValueVT.getVectorElementType())) {
795 // Integer promotion.
796 ValueVT = EVT::getVectorVT(*DAG.getContext(),
797 BuiltVectorTy.getVectorElementType(),
798 ValueVT.getVectorElementCount());
799 Val = DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
800 }
801
802 if (SDValue Widened = widenVectorToPartType(DAG, Val, DL, BuiltVectorTy)) {
803 Val = Widened;
804 }
805 }
806
807 assert(Val.getValueType() == BuiltVectorTy && "Unexpected vector value type");
808
809 // Split the vector into intermediate operands.
810 SmallVector<SDValue, 8> Ops(NumIntermediates);
811 for (unsigned i = 0; i != NumIntermediates; ++i) {
812 if (IntermediateVT.isVector()) {
813 // This does something sensible for scalable vectors - see the
814 // definition of EXTRACT_SUBVECTOR for further details.
815 unsigned IntermediateNumElts = IntermediateVT.getVectorMinNumElements();
816 Ops[i] =
817 DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, IntermediateVT, Val,
818 DAG.getVectorIdxConstant(i * IntermediateNumElts, DL));
819 } else {
820 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntermediateVT, Val,
821 DAG.getVectorIdxConstant(i, DL));
822 }
823 }
824
825 // Split the intermediate operands into legal parts.
826 if (NumParts == NumIntermediates) {
827 // If the register was not expanded, promote or copy the value,
828 // as appropriate.
829 for (unsigned i = 0; i != NumParts; ++i)
830 getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT, V, CallConv);
831 } else if (NumParts > 0) {
832 // If the intermediate type was expanded, split each the value into
833 // legal parts.
834 assert(NumIntermediates != 0 && "division by zero");
835 assert(NumParts % NumIntermediates == 0 &&
836 "Must expand into a divisible number of parts!");
837 unsigned Factor = NumParts / NumIntermediates;
838 for (unsigned i = 0; i != NumIntermediates; ++i)
839 getCopyToParts(DAG, DL, Ops[i], &Parts[i * Factor], Factor, PartVT, V,
840 CallConv);
841 }
842}
843
844static void failForInvalidBundles(const CallBase &I, StringRef Name,
845 ArrayRef<uint32_t> AllowedBundles) {
846 if (I.hasOperandBundlesOtherThan(AllowedBundles)) {
847 ListSeparator LS;
848 std::string Error;
850 for (unsigned i = 0, e = I.getNumOperandBundles(); i != e; ++i) {
851 OperandBundleUse U = I.getOperandBundleAt(i);
852 if (!is_contained(AllowedBundles, U.getTagID()))
853 OS << LS << U.getTagName();
854 }
856 Twine("cannot lower ", Name)
857 .concat(Twine(" with arbitrary operand bundles: ", Error)));
858 }
859}
860
862 EVT valuevt, std::optional<CallingConv::ID> CC)
863 : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs),
864 RegCount(1, regs.size()), CallConv(CC) {}
865
867 const DataLayout &DL, Register Reg, Type *Ty,
868 std::optional<CallingConv::ID> CC) {
869 ComputeValueVTs(TLI, DL, Ty, ValueVTs);
870
871 CallConv = CC;
872
873 for (EVT ValueVT : ValueVTs) {
874 unsigned NumRegs =
876 ? TLI.getNumRegistersForCallingConv(Context, *CC, ValueVT)
877 : TLI.getNumRegisters(Context, ValueVT);
878 MVT RegisterVT =
880 ? TLI.getRegisterTypeForCallingConv(Context, *CC, ValueVT)
881 : TLI.getRegisterType(Context, ValueVT);
882 for (unsigned i = 0; i != NumRegs; ++i)
883 Regs.push_back(Reg + i);
884 RegVTs.push_back(RegisterVT);
885 RegCount.push_back(NumRegs);
886 Reg = Reg.id() + NumRegs;
887 }
888}
889
891 FunctionLoweringInfo &FuncInfo,
892 const SDLoc &dl, SDValue &Chain,
893 SDValue *Glue, const Value *V) const {
894 // A Value with type {} or [0 x %t] needs no registers.
895 if (ValueVTs.empty())
896 return SDValue();
897
898 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
899
900 // Assemble the legal parts into the final values.
901 SmallVector<SDValue, 4> Values(ValueVTs.size());
903 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
904 // Copy the legal parts from the registers.
905 EVT ValueVT = ValueVTs[Value];
906 unsigned NumRegs = RegCount[Value];
907 MVT RegisterVT = isABIMangled()
909 *DAG.getContext(), *CallConv, RegVTs[Value])
910 : RegVTs[Value];
911
912 Parts.resize(NumRegs);
913 for (unsigned i = 0; i != NumRegs; ++i) {
914 SDValue P;
915 if (!Glue) {
916 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
917 } else {
918 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Glue);
919 *Glue = P.getValue(2);
920 }
921
922 Chain = P.getValue(1);
923 Parts[i] = P;
924
925 // If the source register was virtual and if we know something about it,
926 // add an assert node.
927 if (!Regs[Part + i].isVirtual() || !RegisterVT.isInteger())
928 continue;
929
931 FuncInfo.GetLiveOutRegInfo(Regs[Part+i]);
932 if (!LOI)
933 continue;
934
935 unsigned RegSize = RegisterVT.getScalarSizeInBits();
936 unsigned NumSignBits = LOI->NumSignBits;
937 unsigned NumZeroBits = LOI->Known.countMinLeadingZeros();
938
939 if (NumZeroBits == RegSize) {
940 // The current value is a zero.
941 // Explicitly express that as it would be easier for
942 // optimizations to kick in.
943 Parts[i] = DAG.getConstant(0, dl, RegisterVT);
944 continue;
945 }
946
947 // FIXME: We capture more information than the dag can represent. For
948 // now, just use the tightest assertzext/assertsext possible.
949 bool isSExt;
950 EVT FromVT(MVT::Other);
951 if (NumZeroBits) {
952 FromVT = EVT::getIntegerVT(*DAG.getContext(), RegSize - NumZeroBits);
953 isSExt = false;
954 } else if (NumSignBits > 1) {
955 FromVT =
956 EVT::getIntegerVT(*DAG.getContext(), RegSize - NumSignBits + 1);
957 isSExt = true;
958 } else {
959 continue;
960 }
961 // Add an assertion node.
962 assert(FromVT != MVT::Other);
963 Parts[i] = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
964 RegisterVT, P, DAG.getValueType(FromVT));
965 }
966
967 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), NumRegs,
968 RegisterVT, ValueVT, V, Chain, CallConv);
969 Part += NumRegs;
970 Parts.clear();
971 }
972
973 return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(ValueVTs), Values);
974}
975
977 const SDLoc &dl, SDValue &Chain, SDValue *Glue,
978 const Value *V,
979 ISD::NodeType PreferredExtendType) const {
980 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
981 ISD::NodeType ExtendKind = PreferredExtendType;
982
983 // Get the list of the values's legal parts.
984 unsigned NumRegs = Regs.size();
985 SmallVector<SDValue, 8> Parts(NumRegs);
986 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
987 unsigned NumParts = RegCount[Value];
988
989 MVT RegisterVT = isABIMangled()
991 *DAG.getContext(), *CallConv, RegVTs[Value])
992 : RegVTs[Value];
993
994 if (ExtendKind == ISD::ANY_EXTEND && TLI.isZExtFree(Val, RegisterVT))
995 ExtendKind = ISD::ZERO_EXTEND;
996
997 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value), &Parts[Part],
998 NumParts, RegisterVT, V, CallConv, ExtendKind);
999 Part += NumParts;
1000 }
1001
1002 // Copy the parts into the registers.
1003 SmallVector<SDValue, 8> Chains(NumRegs);
1004 for (unsigned i = 0; i != NumRegs; ++i) {
1005 SDValue Part;
1006 if (!Glue) {
1007 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
1008 } else {
1009 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Glue);
1010 *Glue = Part.getValue(1);
1011 }
1012
1013 Chains[i] = Part.getValue(0);
1014 }
1015
1016 if (NumRegs == 1 || Glue)
1017 // If NumRegs > 1 && Glue is used then the use of the last CopyToReg is
1018 // flagged to it. That is the CopyToReg nodes and the user are considered
1019 // a single scheduling unit. If we create a TokenFactor and return it as
1020 // chain, then the TokenFactor is both a predecessor (operand) of the
1021 // user as well as a successor (the TF operands are flagged to the user).
1022 // c1, f1 = CopyToReg
1023 // c2, f2 = CopyToReg
1024 // c3 = TokenFactor c1, c2
1025 // ...
1026 // = op c3, ..., f2
1027 Chain = Chains[NumRegs-1];
1028 else
1029 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
1030}
1031
1033 unsigned MatchingIdx, const SDLoc &dl,
1034 SelectionDAG &DAG,
1035 std::vector<SDValue> &Ops) const {
1036 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1037
1038 InlineAsm::Flag Flag(Code, Regs.size());
1039 if (HasMatching)
1040 Flag.setMatchingOp(MatchingIdx);
1041 else if (!Regs.empty() && Regs.front().isVirtual()) {
1042 // Put the register class of the virtual registers in the flag word. That
1043 // way, later passes can recompute register class constraints for inline
1044 // assembly as well as normal instructions.
1045 // Don't do this for tied operands that can use the regclass information
1046 // from the def.
1048 const TargetRegisterClass *RC = MRI.getRegClass(Regs.front());
1049 Flag.setRegClass(RC->getID());
1050 }
1051
1052 SDValue Res = DAG.getTargetConstant(Flag, dl, MVT::i32);
1053 Ops.push_back(Res);
1054
1055 if (Code == InlineAsm::Kind::Clobber) {
1056 // Clobbers should always have a 1:1 mapping with registers, and may
1057 // reference registers that have illegal (e.g. vector) types. Hence, we
1058 // shouldn't try to apply any sort of splitting logic to them.
1059 assert(Regs.size() == RegVTs.size() && Regs.size() == ValueVTs.size() &&
1060 "No 1:1 mapping from clobbers to regs?");
1062 (void)SP;
1063 for (unsigned I = 0, E = ValueVTs.size(); I != E; ++I) {
1064 Ops.push_back(DAG.getRegister(Regs[I], RegVTs[I]));
1065 assert(
1066 (Regs[I] != SP ||
1068 "If we clobbered the stack pointer, MFI should know about it.");
1069 }
1070 return;
1071 }
1072
1073 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
1074 MVT RegisterVT = RegVTs[Value];
1075 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value],
1076 RegisterVT);
1077 for (unsigned i = 0; i != NumRegs; ++i) {
1078 assert(Reg < Regs.size() && "Mismatch in # registers expected");
1079 Register TheReg = Regs[Reg++];
1080 Ops.push_back(DAG.getRegister(TheReg, RegisterVT));
1081 }
1082 }
1083}
1084
1088 unsigned I = 0;
1089 for (auto CountAndVT : zip_first(RegCount, RegVTs)) {
1090 unsigned RegCount = std::get<0>(CountAndVT);
1091 MVT RegisterVT = std::get<1>(CountAndVT);
1092 TypeSize RegisterSize = RegisterVT.getSizeInBits();
1093 for (unsigned E = I + RegCount; I != E; ++I)
1094 OutVec.push_back(std::make_pair(Regs[I], RegisterSize));
1095 }
1096 return OutVec;
1097}
1098
1100 AssumptionCache *ac, const TargetLibraryInfo *li,
1101 const TargetTransformInfo &TTI) {
1102 BatchAA = aa;
1103 AC = ac;
1104 GFI = gfi;
1105 LibInfo = li;
1106 Context = DAG.getContext();
1107 LPadToCallSiteMap.clear();
1108 this->TTI = &TTI;
1109 SL->init(DAG.getTargetLoweringInfo(), TM, DAG.getDataLayout());
1110 AssignmentTrackingEnabled = isAssignmentTrackingEnabled(
1111 *DAG.getMachineFunction().getFunction().getParent());
1112}
1113
1115 NodeMap.clear();
1116 UnusedArgNodeMap.clear();
1117 PendingLoads.clear();
1118 PendingExports.clear();
1119 PendingConstrainedFP.clear();
1120 PendingConstrainedFPStrict.clear();
1121 CurInst = nullptr;
1122 HasTailCall = false;
1123 SDNodeOrder = LowestSDNodeOrder;
1124 StatepointLowering.clear();
1125}
1126
1128 DanglingDebugInfoMap.clear();
1129}
1130
1131// Update DAG root to include dependencies on Pending chains.
1132SDValue SelectionDAGBuilder::updateRoot(SmallVectorImpl<SDValue> &Pending) {
1133 SDValue Root = DAG.getRoot();
1134
1135 if (Pending.empty())
1136 return Root;
1137
1138 // Add current root to PendingChains, unless we already indirectly
1139 // depend on it.
1140 if (Root.getOpcode() != ISD::EntryToken) {
1141 unsigned i = 0, e = Pending.size();
1142 for (; i != e; ++i) {
1143 assert(Pending[i].getNode()->getNumOperands() > 1);
1144 if (Pending[i].getNode()->getOperand(0) == Root)
1145 break; // Don't add the root if we already indirectly depend on it.
1146 }
1147
1148 if (i == e)
1149 Pending.push_back(Root);
1150 }
1151
1152 if (Pending.size() == 1)
1153 Root = Pending[0];
1154 else
1155 Root = DAG.getTokenFactor(getCurSDLoc(), Pending);
1156
1157 DAG.setRoot(Root);
1158 Pending.clear();
1159 return Root;
1160}
1161
1165
1167 // If the new exception behavior differs from that of the pending
1168 // ones, chain up them and update the root.
1169 switch (EB) {
1172 // Floating-point exceptions produced by such operations are not intended
1173 // to be observed, so the sequence of these operations does not need to be
1174 // preserved.
1175 //
1176 // They however must not be mixed with the instructions that have strict
1177 // exception behavior. Placing an operation with 'ebIgnore' behavior between
1178 // 'ebStrict' operations could distort the observed exception behavior.
1179 if (!PendingConstrainedFPStrict.empty()) {
1180 assert(PendingConstrainedFP.empty());
1181 updateRoot(PendingConstrainedFPStrict);
1182 }
1183 break;
1185 // Floating-point exception produced by these operations may be observed, so
1186 // they must be correctly chained. If trapping on FP exceptions is
1187 // disabled, the exceptions can be observed only by functions that read
1188 // exception flags, like 'llvm.get_fpenv' or 'fetestexcept'. It means that
1189 // the order of operations is not significant between barriers.
1190 //
1191 // If trapping is enabled, each operation becomes an implicit observation
1192 // point, so the operations must be sequenced according their original
1193 // source order.
1194 if (!PendingConstrainedFP.empty()) {
1195 assert(PendingConstrainedFPStrict.empty());
1196 updateRoot(PendingConstrainedFP);
1197 }
1198 // TODO: Add support for trapping-enabled scenarios.
1199 }
1200 return DAG.getRoot();
1201}
1202
1204 // Chain up all pending constrained intrinsics together with all
1205 // pending loads, by simply appending them to PendingLoads and
1206 // then calling getMemoryRoot().
1207 PendingLoads.reserve(PendingLoads.size() +
1208 PendingConstrainedFP.size() +
1209 PendingConstrainedFPStrict.size());
1210 PendingLoads.append(PendingConstrainedFP.begin(),
1211 PendingConstrainedFP.end());
1212 PendingLoads.append(PendingConstrainedFPStrict.begin(),
1213 PendingConstrainedFPStrict.end());
1214 PendingConstrainedFP.clear();
1215 PendingConstrainedFPStrict.clear();
1216 return getMemoryRoot();
1217}
1218
1220 // We need to emit pending fpexcept.strict constrained intrinsics,
1221 // so append them to the PendingExports list.
1222 PendingExports.append(PendingConstrainedFPStrict.begin(),
1223 PendingConstrainedFPStrict.end());
1224 PendingConstrainedFPStrict.clear();
1225 return updateRoot(PendingExports);
1226}
1227
1229 DILocalVariable *Variable,
1231 DebugLoc DL) {
1232 assert(Variable && "Missing variable");
1233
1234 // Check if address has undef value.
1235 if (!Address || isa<UndefValue>(Address) ||
1236 (Address->use_empty() && !isa<Argument>(Address))) {
1237 LLVM_DEBUG(
1238 dbgs()
1239 << "dbg_declare: Dropping debug info (bad/undef/unused-arg address)\n");
1240 return;
1241 }
1242
1243 bool IsParameter = Variable->isParameter() || isa<Argument>(Address);
1244
1245 SDValue &N = NodeMap[Address];
1246 if (!N.getNode() && isa<Argument>(Address))
1247 // Check unused arguments map.
1248 N = UnusedArgNodeMap[Address];
1249 SDDbgValue *SDV;
1250 if (N.getNode()) {
1251 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
1252 Address = BCI->getOperand(0);
1253 // Parameters are handled specially.
1254 auto *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
1255 if (IsParameter && FINode) {
1256 // Byval parameter. We have a frame index at this point.
1257 SDV = DAG.getFrameIndexDbgValue(Variable, Expression, FINode->getIndex(),
1258 /*IsIndirect*/ true, DL, SDNodeOrder);
1259 } else if (isa<Argument>(Address)) {
1260 // Address is an argument, so try to emit its dbg value using
1261 // virtual register info from the FuncInfo.ValueMap.
1262 EmitFuncArgumentDbgValue(Address, Variable, Expression, DL,
1263 FuncArgumentDbgValueKind::Declare, N);
1264 return;
1265 } else {
1266 SDV = DAG.getDbgValue(Variable, Expression, N.getNode(), N.getResNo(),
1267 true, DL, SDNodeOrder);
1268 }
1269 DAG.AddDbgValue(SDV, IsParameter);
1270 } else {
1271 // If Address is an argument then try to emit its dbg value using
1272 // virtual register info from the FuncInfo.ValueMap.
1273 if (!EmitFuncArgumentDbgValue(Address, Variable, Expression, DL,
1274 FuncArgumentDbgValueKind::Declare, N)) {
1275 LLVM_DEBUG(dbgs() << "dbg_declare: Dropping debug info"
1276 << " (could not emit func-arg dbg_value)\n");
1277 }
1278 }
1279}
1280
1282 // Add SDDbgValue nodes for any var locs here. Do so before updating
1283 // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}.
1284 if (FunctionVarLocs const *FnVarLocs = DAG.getFunctionVarLocs()) {
1285 // Add SDDbgValue nodes for any var locs here. Do so before updating
1286 // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}.
1287 for (auto It = FnVarLocs->locs_begin(&I), End = FnVarLocs->locs_end(&I);
1288 It != End; ++It) {
1289 auto *Var = FnVarLocs->getDILocalVariable(It->VariableID);
1290 dropDanglingDebugInfo(Var, It->Expr);
1291 if (It->Values.isKillLocation(It->Expr)) {
1292 handleKillDebugValue(Var, It->Expr, It->DL, SDNodeOrder);
1293 continue;
1294 }
1295 SmallVector<Value *> Values(It->Values.location_ops());
1296 if (!handleDebugValue(Values, Var, It->Expr, It->DL, SDNodeOrder,
1297 It->Values.hasArgList())) {
1298 SmallVector<Value *, 4> Vals(It->Values.location_ops());
1300 FnVarLocs->getDILocalVariable(It->VariableID),
1301 It->Expr, Vals.size() > 1, It->DL, SDNodeOrder);
1302 }
1303 }
1304 }
1305
1306 // We must skip DbgVariableRecords if they've already been processed above as
1307 // we have just emitted the debug values resulting from assignment tracking
1308 // analysis, making any existing DbgVariableRecords redundant (and probably
1309 // less correct). We still need to process DbgLabelRecords. This does sink
1310 // DbgLabelRecords to the bottom of the group of debug records. That sholdn't
1311 // be important as it does so deterministcally and ordering between
1312 // DbgLabelRecords and DbgVariableRecords is immaterial (other than for MIR/IR
1313 // printing).
1314 bool SkipDbgVariableRecords = DAG.getFunctionVarLocs();
1315 // Is there is any debug-info attached to this instruction, in the form of
1316 // DbgRecord non-instruction debug-info records.
1317 for (DbgRecord &DR : I.getDbgRecordRange()) {
1318 if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) {
1319 assert(DLR->getLabel() && "Missing label");
1320 SDDbgLabel *SDV =
1321 DAG.getDbgLabel(DLR->getLabel(), DLR->getDebugLoc(), SDNodeOrder);
1322 DAG.AddDbgLabel(SDV);
1323 continue;
1324 }
1325
1326 if (SkipDbgVariableRecords)
1327 continue;
1329 DILocalVariable *Variable = DVR.getVariable();
1332
1334 if (FuncInfo.PreprocessedDVRDeclares.contains(&DVR))
1335 continue;
1336 LLVM_DEBUG(dbgs() << "SelectionDAG visiting dbg_declare: " << DVR
1337 << "\n");
1339 DVR.getDebugLoc());
1340 continue;
1341 }
1342
1343 // A DbgVariableRecord with no locations is a kill location.
1345 if (Values.empty()) {
1347 SDNodeOrder);
1348 continue;
1349 }
1350
1351 // A DbgVariableRecord with an undef or absent location is also a kill
1352 // location.
1353 if (llvm::any_of(Values,
1354 [](Value *V) { return !V || isa<UndefValue>(V); })) {
1356 SDNodeOrder);
1357 continue;
1358 }
1359
1360 bool IsVariadic = DVR.hasArgList();
1361 if (!handleDebugValue(Values, Variable, Expression, DVR.getDebugLoc(),
1362 SDNodeOrder, IsVariadic)) {
1363 addDanglingDebugInfo(Values, Variable, Expression, IsVariadic,
1364 DVR.getDebugLoc(), SDNodeOrder);
1365 }
1366 }
1367}
1368
1370 visitDbgInfo(I);
1371
1372 // Set up outgoing PHI node register values before emitting the terminator.
1373 if (I.isTerminator()) {
1374 HandlePHINodesInSuccessorBlocks(I.getParent());
1375 }
1376
1377 ++SDNodeOrder;
1378 CurInst = &I;
1379
1380 // Set inserted listener only if required.
1381 bool NodeInserted = false;
1382 std::unique_ptr<SelectionDAG::DAGNodeInsertedListener> InsertedListener;
1383 MDNode *PCSectionsMD = I.getMetadata(LLVMContext::MD_pcsections);
1384 MDNode *MMRA = I.getMetadata(LLVMContext::MD_mmra);
1385 if (PCSectionsMD || MMRA) {
1386 InsertedListener = std::make_unique<SelectionDAG::DAGNodeInsertedListener>(
1387 DAG, [&](SDNode *) { NodeInserted = true; });
1388 }
1389
1390 visit(I.getOpcode(), I);
1391
1392 if (!I.isTerminator() && !HasTailCall &&
1393 !isa<GCStatepointInst>(I)) // statepoints handle their exports internally
1395
1396 // Handle metadata.
1397 if (PCSectionsMD || MMRA) {
1398 auto It = NodeMap.find(&I);
1399 if (It != NodeMap.end()) {
1400 if (PCSectionsMD)
1401 DAG.addPCSections(It->second.getNode(), PCSectionsMD);
1402 if (MMRA)
1403 DAG.addMMRAMetadata(It->second.getNode(), MMRA);
1404 } else if (NodeInserted) {
1405 // This should not happen; if it does, don't let it go unnoticed so we can
1406 // fix it. Relevant visit*() function is probably missing a setValue().
1407 errs() << "warning: loosing !pcsections and/or !mmra metadata ["
1408 << I.getModule()->getName() << "]\n";
1409 LLVM_DEBUG(I.dump());
1410 assert(false);
1411 }
1412 }
1413
1414 CurInst = nullptr;
1415}
1416
1417void SelectionDAGBuilder::visitPHI(const PHINode &) {
1418 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
1419}
1420
1421void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
1422 // Note: this doesn't use InstVisitor, because it has to work with
1423 // ConstantExpr's in addition to instructions.
1424 switch (Opcode) {
1425 default: llvm_unreachable("Unknown instruction type encountered!");
1426 // Build the switch statement using the Instruction.def file.
1427#define HANDLE_INST(NUM, OPCODE, CLASS) \
1428 case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break;
1429#include "llvm/IR/Instruction.def"
1430 }
1431}
1432
1434 DILocalVariable *Variable,
1435 DebugLoc DL, unsigned Order,
1438 // For variadic dbg_values we will now insert poison.
1439 // FIXME: We can potentially recover these!
1441 for (const Value *V : Values) {
1442 auto *Poison = PoisonValue::get(V->getType());
1444 }
1445 SDDbgValue *SDV = DAG.getDbgValueList(Variable, Expression, Locs, {},
1446 /*IsIndirect=*/false, DL, Order,
1447 /*IsVariadic=*/true);
1448 DAG.AddDbgValue(SDV, /*isParameter=*/false);
1449 return true;
1450}
1451
1453 DILocalVariable *Var,
1454 DIExpression *Expr,
1455 bool IsVariadic, DebugLoc DL,
1456 unsigned Order) {
1457 if (IsVariadic) {
1458 handleDanglingVariadicDebugInfo(DAG, Var, DL, Order, Values, Expr);
1459 return;
1460 }
1461 // TODO: Dangling debug info will eventually either be resolved or produce
1462 // a poison DBG_VALUE. However in the resolution case, a gap may appear
1463 // between the original dbg.value location and its resolved DBG_VALUE,
1464 // which we should ideally fill with an extra poison DBG_VALUE.
1465 assert(Values.size() == 1);
1466 DanglingDebugInfoMap[Values[0]].emplace_back(Var, Expr, DL, Order);
1467}
1468
1470 const DIExpression *Expr) {
1471 auto isMatchingDbgValue = [&](DanglingDebugInfo &DDI) {
1472 DIVariable *DanglingVariable = DDI.getVariable();
1473 DIExpression *DanglingExpr = DDI.getExpression();
1474 if (DanglingVariable == Variable && Expr->fragmentsOverlap(DanglingExpr)) {
1475 LLVM_DEBUG(dbgs() << "Dropping dangling debug info for "
1476 << printDDI(nullptr, DDI) << "\n");
1477 return true;
1478 }
1479 return false;
1480 };
1481
1482 for (auto &DDIMI : DanglingDebugInfoMap) {
1483 DanglingDebugInfoVector &DDIV = DDIMI.second;
1484
1485 // If debug info is to be dropped, run it through final checks to see
1486 // whether it can be salvaged.
1487 for (auto &DDI : DDIV)
1488 if (isMatchingDbgValue(DDI))
1489 salvageUnresolvedDbgValue(DDIMI.first, DDI);
1490
1491 erase_if(DDIV, isMatchingDbgValue);
1492 }
1493}
1494
1495// resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
1496// generate the debug data structures now that we've seen its definition.
1498 SDValue Val) {
1499 auto DanglingDbgInfoIt = DanglingDebugInfoMap.find(V);
1500 if (DanglingDbgInfoIt == DanglingDebugInfoMap.end())
1501 return;
1502
1503 DanglingDebugInfoVector &DDIV = DanglingDbgInfoIt->second;
1504 for (auto &DDI : DDIV) {
1505 DebugLoc DL = DDI.getDebugLoc();
1506 unsigned ValSDNodeOrder = Val.getNode()->getIROrder();
1507 unsigned DbgSDNodeOrder = DDI.getSDNodeOrder();
1508 DILocalVariable *Variable = DDI.getVariable();
1509 DIExpression *Expr = DDI.getExpression();
1510 assert(Variable->isValidLocationForIntrinsic(DL) &&
1511 "Expected inlined-at fields to agree");
1512 SDDbgValue *SDV;
1513 if (Val.getNode()) {
1514 // FIXME: I doubt that it is correct to resolve a dangling DbgValue as a
1515 // FuncArgumentDbgValue (it would be hoisted to the function entry, and if
1516 // we couldn't resolve it directly when examining the DbgValue intrinsic
1517 // in the first place we should not be more successful here). Unless we
1518 // have some test case that prove this to be correct we should avoid
1519 // calling EmitFuncArgumentDbgValue here.
1520 if (!EmitFuncArgumentDbgValue(V, Variable, Expr, DL,
1521 FuncArgumentDbgValueKind::Value, Val)) {
1522 LLVM_DEBUG(dbgs() << "Resolve dangling debug info for "
1523 << printDDI(V, DDI) << "\n");
1524 LLVM_DEBUG(dbgs() << " By mapping to:\n "; Val.dump());
1525 // Increase the SDNodeOrder for the DbgValue here to make sure it is
1526 // inserted after the definition of Val when emitting the instructions
1527 // after ISel. An alternative could be to teach
1528 // ScheduleDAGSDNodes::EmitSchedule to delay the insertion properly.
1529 LLVM_DEBUG(if (ValSDNodeOrder > DbgSDNodeOrder) dbgs()
1530 << "changing SDNodeOrder from " << DbgSDNodeOrder << " to "
1531 << ValSDNodeOrder << "\n");
1532 SDV = getDbgValue(Val, Variable, Expr, DL,
1533 std::max(DbgSDNodeOrder, ValSDNodeOrder));
1534 DAG.AddDbgValue(SDV, false);
1535 } else
1536 LLVM_DEBUG(dbgs() << "Resolved dangling debug info for "
1537 << printDDI(V, DDI)
1538 << " in EmitFuncArgumentDbgValue\n");
1539 } else {
1540 LLVM_DEBUG(dbgs() << "Dropping debug info for " << printDDI(V, DDI)
1541 << "\n");
1542 auto Poison = PoisonValue::get(V->getType());
1543 auto SDV =
1544 DAG.getConstantDbgValue(Variable, Expr, Poison, DL, DbgSDNodeOrder);
1545 DAG.AddDbgValue(SDV, false);
1546 }
1547 }
1548 DDIV.clear();
1549}
1550
1552 DanglingDebugInfo &DDI) {
1553 // TODO: For the variadic implementation, instead of only checking the fail
1554 // state of `handleDebugValue`, we need know specifically which values were
1555 // invalid, so that we attempt to salvage only those values when processing
1556 // a DIArgList.
1557 const Value *OrigV = V;
1558 DILocalVariable *Var = DDI.getVariable();
1559 DIExpression *Expr = DDI.getExpression();
1560 DebugLoc DL = DDI.getDebugLoc();
1561 unsigned SDOrder = DDI.getSDNodeOrder();
1562
1563 // Currently we consider only dbg.value intrinsics -- we tell the salvager
1564 // that DW_OP_stack_value is desired.
1565 bool StackValue = true;
1566
1567 // Can this Value can be encoded without any further work?
1568 if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false))
1569 return;
1570
1571 // Attempt to salvage back through as many instructions as possible. Bail if
1572 // a non-instruction is seen, such as a constant expression or global
1573 // variable. FIXME: Further work could recover those too.
1574 while (isa<Instruction>(V)) {
1575 const Instruction &VAsInst = *cast<const Instruction>(V);
1576 // Temporary "0", awaiting real implementation.
1578 SmallVector<Value *, 4> AdditionalValues;
1579 V = salvageDebugInfoImpl(const_cast<Instruction &>(VAsInst),
1580 Expr->getNumLocationOperands(), Ops,
1581 AdditionalValues);
1582 // If we cannot salvage any further, and haven't yet found a suitable debug
1583 // expression, bail out.
1584 if (!V)
1585 break;
1586
1587 // TODO: If AdditionalValues isn't empty, then the salvage can only be
1588 // represented with a DBG_VALUE_LIST, so we give up. When we have support
1589 // here for variadic dbg_values, remove that condition.
1590 if (!AdditionalValues.empty())
1591 break;
1592
1593 // New value and expr now represent this debuginfo.
1594 Expr = DIExpression::appendOpsToArg(Expr, Ops, 0, StackValue);
1595
1596 // Some kind of simplification occurred: check whether the operand of the
1597 // salvaged debug expression can be encoded in this DAG.
1598 if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false)) {
1599 LLVM_DEBUG(
1600 dbgs() << "Salvaged debug location info for:\n " << *Var << "\n"
1601 << *OrigV << "\nBy stripping back to:\n " << *V << "\n");
1602 return;
1603 }
1604 }
1605
1606 // This was the final opportunity to salvage this debug information, and it
1607 // couldn't be done. Place a poison DBG_VALUE at this location to terminate
1608 // any earlier variable location.
1609 assert(OrigV && "V shouldn't be null");
1610 auto *Poison = PoisonValue::get(OrigV->getType());
1611 auto *SDV = DAG.getConstantDbgValue(Var, Expr, Poison, DL, SDNodeOrder);
1612 DAG.AddDbgValue(SDV, false);
1613 LLVM_DEBUG(dbgs() << "Dropping debug value info for:\n "
1614 << printDDI(OrigV, DDI) << "\n");
1615}
1616
1618 DIExpression *Expr,
1619 DebugLoc DbgLoc,
1620 unsigned Order) {
1624 handleDebugValue(Poison, Var, NewExpr, DbgLoc, Order,
1625 /*IsVariadic*/ false);
1626}
1627
1629 DILocalVariable *Var,
1630 DIExpression *Expr, DebugLoc DbgLoc,
1631 unsigned Order, bool IsVariadic) {
1632 if (Values.empty())
1633 return true;
1634
1635 // Filter EntryValue locations out early.
1636 if (visitEntryValueDbgValue(Values, Var, Expr, DbgLoc))
1637 return true;
1638
1639 SmallVector<SDDbgOperand> LocationOps;
1640 SmallVector<SDNode *> Dependencies;
1641 for (const Value *V : Values) {
1642 // Constant value.
1645 LocationOps.emplace_back(SDDbgOperand::fromConst(V));
1646 continue;
1647 }
1648
1649 // Look through IntToPtr constants.
1650 if (auto *CE = dyn_cast<ConstantExpr>(V))
1651 if (CE->getOpcode() == Instruction::IntToPtr) {
1652 LocationOps.emplace_back(SDDbgOperand::fromConst(CE->getOperand(0)));
1653 continue;
1654 }
1655
1656 // If the Value is a frame index, we can create a FrameIndex debug value
1657 // without relying on the DAG at all.
1658 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1659 auto SI = FuncInfo.StaticAllocaMap.find(AI);
1660 if (SI != FuncInfo.StaticAllocaMap.end()) {
1661 LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(SI->second));
1662 continue;
1663 }
1664 }
1665
1666 // Do not use getValue() in here; we don't want to generate code at
1667 // this point if it hasn't been done yet.
1668 SDValue N = NodeMap[V];
1669 if (!N.getNode() && isa<Argument>(V)) // Check unused arguments map.
1670 N = UnusedArgNodeMap[V];
1671
1672 if (N.getNode()) {
1673 // Only emit func arg dbg value for non-variadic dbg.values for now.
1674 if (!IsVariadic &&
1675 EmitFuncArgumentDbgValue(V, Var, Expr, DbgLoc,
1676 FuncArgumentDbgValueKind::Value, N))
1677 return true;
1678 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
1679 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can
1680 // describe stack slot locations.
1681 //
1682 // Consider "int x = 0; int *px = &x;". There are two kinds of
1683 // interesting debug values here after optimization:
1684 //
1685 // dbg.value(i32* %px, !"int *px", !DIExpression()), and
1686 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
1687 //
1688 // Both describe the direct values of their associated variables.
1689 Dependencies.push_back(N.getNode());
1690 LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(FISDN->getIndex()));
1691 continue;
1692 }
1693 LocationOps.emplace_back(
1694 SDDbgOperand::fromNode(N.getNode(), N.getResNo()));
1695 continue;
1696 }
1697
1698 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1699 // Special rules apply for the first dbg.values of parameter variables in a
1700 // function. Identify them by the fact they reference Argument Values, that
1701 // they're parameters, and they are parameters of the current function. We
1702 // need to let them dangle until they get an SDNode.
1703 bool IsParamOfFunc =
1704 isa<Argument>(V) && Var->isParameter() && !DbgLoc.getInlinedAt();
1705 if (IsParamOfFunc)
1706 return false;
1707
1708 // The value is not used in this block yet (or it would have an SDNode).
1709 // We still want the value to appear for the user if possible -- if it has
1710 // an associated VReg, we can refer to that instead.
1711 auto VMI = FuncInfo.ValueMap.find(V);
1712 if (VMI != FuncInfo.ValueMap.end()) {
1713 Register Reg = VMI->second;
1714 // If this is a PHI node, it may be split up into several MI PHI nodes
1715 // (in FunctionLoweringInfo::set).
1716 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg,
1717 V->getType(), std::nullopt);
1718 if (RFV.occupiesMultipleRegs()) {
1719 // FIXME: We could potentially support variadic dbg_values here.
1720 if (IsVariadic)
1721 return false;
1722 unsigned Offset = 0;
1723 unsigned BitsToDescribe = 0;
1724 if (auto VarSize = Var->getSizeInBits())
1725 BitsToDescribe = *VarSize;
1726 if (auto Fragment = Expr->getFragmentInfo())
1727 BitsToDescribe = Fragment->SizeInBits;
1728 for (const auto &RegAndSize : RFV.getRegsAndSizes()) {
1729 // Bail out if all bits are described already.
1730 if (Offset >= BitsToDescribe)
1731 break;
1732 // TODO: handle scalable vectors.
1733 unsigned RegisterSize = RegAndSize.second;
1734 unsigned FragmentSize = (Offset + RegisterSize > BitsToDescribe)
1735 ? BitsToDescribe - Offset
1736 : RegisterSize;
1737 auto FragmentExpr = DIExpression::createFragmentExpression(
1738 Expr, Offset, FragmentSize);
1739 if (!FragmentExpr)
1740 continue;
1741 SDDbgValue *SDV = DAG.getVRegDbgValue(
1742 Var, *FragmentExpr, RegAndSize.first, false, DbgLoc, Order);
1743 DAG.AddDbgValue(SDV, false);
1744 Offset += RegisterSize;
1745 }
1746 return true;
1747 }
1748 // We can use simple vreg locations for variadic dbg_values as well.
1749 LocationOps.emplace_back(SDDbgOperand::fromVReg(Reg));
1750 continue;
1751 }
1752 // We failed to create a SDDbgOperand for V.
1753 return false;
1754 }
1755
1756 // We have created a SDDbgOperand for each Value in Values.
1757 assert(!LocationOps.empty());
1758 SDDbgValue *SDV =
1759 DAG.getDbgValueList(Var, Expr, LocationOps, Dependencies,
1760 /*IsIndirect=*/false, DbgLoc, Order, IsVariadic);
1761 DAG.AddDbgValue(SDV, /*isParameter=*/false);
1762 return true;
1763}
1764
1766 // Try to fixup any remaining dangling debug info -- and drop it if we can't.
1767 for (auto &Pair : DanglingDebugInfoMap)
1768 for (auto &DDI : Pair.second)
1769 salvageUnresolvedDbgValue(const_cast<Value *>(Pair.first), DDI);
1771}
1772
1773/// getCopyFromRegs - If there was virtual register allocated for the value V
1774/// emit CopyFromReg of the specified type Ty. Return empty SDValue() otherwise.
1777 SDValue Result;
1778
1779 if (It != FuncInfo.ValueMap.end()) {
1780 Register InReg = It->second;
1781
1782 RegsForValue RFV(*DAG.getContext(), DAG.getTargetLoweringInfo(),
1783 DAG.getDataLayout(), InReg, Ty,
1784 std::nullopt); // This is not an ABI copy.
1785 SDValue Chain = DAG.getEntryNode();
1786 Result = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr,
1787 V);
1788 resolveDanglingDebugInfo(V, Result);
1789 }
1790
1791 return Result;
1792}
1793
1794/// getValue - Return an SDValue for the given Value.
1796 // If we already have an SDValue for this value, use it. It's important
1797 // to do this first, so that we don't create a CopyFromReg if we already
1798 // have a regular SDValue.
1799 SDValue &N = NodeMap[V];
1800 if (N.getNode()) return N;
1801
1802 // If there's a virtual register allocated and initialized for this
1803 // value, use it.
1804 if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
1805 return copyFromReg;
1806
1807 // Otherwise create a new SDValue and remember it.
1808 SDValue Val = getValueImpl(V);
1809 NodeMap[V] = Val;
1811 return Val;
1812}
1813
1814/// getNonRegisterValue - Return an SDValue for the given Value, but
1815/// don't look in FuncInfo.ValueMap for a virtual register.
1817 // If we already have an SDValue for this value, use it.
1818 SDValue &N = NodeMap[V];
1819 if (N.getNode()) {
1820 if (isIntOrFPConstant(N)) {
1821 // Remove the debug location from the node as the node is about to be used
1822 // in a location which may differ from the original debug location. This
1823 // is relevant to Constant and ConstantFP nodes because they can appear
1824 // as constant expressions inside PHI nodes.
1825 N->setDebugLoc(DebugLoc());
1826 }
1827 return N;
1828 }
1829
1830 // Otherwise create a new SDValue and remember it.
1831 SDValue Val = getValueImpl(V);
1832 NodeMap[V] = Val;
1834 return Val;
1835}
1836
1837/// getValueImpl - Helper function for getValue and getNonRegisterValue.
1838/// Create an SDValue for the given value.
1840 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1841
1842 if (const Constant *C = dyn_cast<Constant>(V)) {
1843 EVT VT = TLI.getValueType(DAG.getDataLayout(), V->getType(), true);
1844
1845 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
1846 SDLoc DL = getCurSDLoc();
1847
1848 // DAG.getConstant() may attempt to legalise the vector constant which can
1849 // significantly change the combines applied to the DAG. To reduce the
1850 // divergence when enabling ConstantInt based vectors we try to construct
1851 // the DAG in the same way as shufflevector based splats. TODO: The
1852 // divergence sometimes leads to better optimisations. Ideally we should
1853 // prevent DAG.getConstant() from legalising too early but there are some
1854 // degradations preventing this.
1855 if (VT.isScalableVector())
1856 return DAG.getNode(
1857 ISD::SPLAT_VECTOR, DL, VT,
1858 DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
1859 if (VT.isFixedLengthVector())
1860 return DAG.getSplatBuildVector(
1861 VT, DL,
1862 DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
1863 return DAG.getConstant(*CI, DL, VT);
1864 }
1865
1866 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
1867 return DAG.getGlobalAddress(GV, getCurSDLoc(), VT);
1868
1869 if (const ConstantPtrAuth *CPA = dyn_cast<ConstantPtrAuth>(C)) {
1870 return DAG.getNode(ISD::PtrAuthGlobalAddress, getCurSDLoc(), VT,
1871 getValue(CPA->getPointer()), getValue(CPA->getKey()),
1872 getValue(CPA->getAddrDiscriminator()),
1873 getValue(CPA->getDiscriminator()));
1874 }
1875
1877 return DAG.getConstant(0, getCurSDLoc(), VT);
1878
1879 if (match(C, m_VScale()))
1880 return DAG.getVScale(getCurSDLoc(), VT, APInt(VT.getSizeInBits(), 1));
1881
1882 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
1883 return DAG.getConstantFP(*CFP, getCurSDLoc(), VT);
1884
1885 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
1886 return isa<PoisonValue>(C) ? DAG.getPOISON(VT) : DAG.getUNDEF(VT);
1887
1888 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1889 visit(CE->getOpcode(), *CE);
1890 SDValue N1 = NodeMap[V];
1891 assert(N1.getNode() && "visit didn't populate the NodeMap!");
1892 return N1;
1893 }
1894
1896 SmallVector<SDValue, 4> Constants;
1897 for (const Use &U : C->operands()) {
1898 SDNode *Val = getValue(U).getNode();
1899 // If the operand is an empty aggregate, there are no values.
1900 if (!Val) continue;
1901 // Add each leaf value from the operand to the Constants list
1902 // to form a flattened list of all the values.
1903 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1904 Constants.push_back(SDValue(Val, i));
1905 }
1906
1907 return DAG.getMergeValues(Constants, getCurSDLoc());
1908 }
1909
1910 if (const ConstantDataSequential *CDS =
1913 for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i) {
1914 SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode();
1915 // Add each leaf value from the operand to the Constants list
1916 // to form a flattened list of all the values.
1917 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1918 Ops.push_back(SDValue(Val, i));
1919 }
1920
1921 if (isa<ArrayType>(CDS->getType()))
1922 return DAG.getMergeValues(Ops, getCurSDLoc());
1923 return DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1924 }
1925
1926 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
1928 "Unknown struct or array constant!");
1929
1930 SmallVector<EVT, 4> ValueVTs;
1931 ComputeValueVTs(TLI, DAG.getDataLayout(), C->getType(), ValueVTs);
1932 unsigned NumElts = ValueVTs.size();
1933 if (NumElts == 0)
1934 return SDValue(); // empty struct
1935 SmallVector<SDValue, 4> Constants(NumElts);
1936 for (unsigned i = 0; i != NumElts; ++i) {
1937 EVT EltVT = ValueVTs[i];
1938 if (isa<UndefValue>(C))
1939 Constants[i] = DAG.getUNDEF(EltVT);
1940 else if (EltVT.isFloatingPoint())
1941 Constants[i] = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1942 else
1943 Constants[i] = DAG.getConstant(0, getCurSDLoc(), EltVT);
1944 }
1945
1946 return DAG.getMergeValues(Constants, getCurSDLoc());
1947 }
1948
1949 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
1950 return DAG.getBlockAddress(BA, VT);
1951
1952 if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(C))
1953 return getValue(Equiv->getGlobalValue());
1954
1955 if (const auto *NC = dyn_cast<NoCFIValue>(C))
1956 return getValue(NC->getGlobalValue());
1957
1958 if (VT == MVT::aarch64svcount) {
1959 assert(C->isNullValue() && "Can only zero this target type!");
1960 return DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT,
1961 DAG.getConstant(0, getCurSDLoc(), MVT::nxv16i1));
1962 }
1963
1964 if (VT.isRISCVVectorTuple()) {
1965 assert(C->isNullValue() && "Can only zero this target type!");
1966 return DAG.getNode(
1967 ISD::BITCAST, getCurSDLoc(), VT,
1968 DAG.getNode(
1970 EVT::getVectorVT(*DAG.getContext(), MVT::i8,
1971 VT.getSizeInBits().getKnownMinValue() / 8, true),
1972 DAG.getConstant(0, getCurSDLoc(), MVT::getIntegerVT(8))));
1973 }
1974
1975 VectorType *VecTy = cast<VectorType>(V->getType());
1976
1977 // Now that we know the number and type of the elements, get that number of
1978 // elements into the Ops array based on what kind of constant it is.
1979 if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
1981 unsigned NumElements = cast<FixedVectorType>(VecTy)->getNumElements();
1982 for (unsigned i = 0; i != NumElements; ++i)
1983 Ops.push_back(getValue(CV->getOperand(i)));
1984
1985 return DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1986 }
1987
1989 EVT EltVT =
1990 TLI.getValueType(DAG.getDataLayout(), VecTy->getElementType());
1991
1992 SDValue Op;
1993 if (EltVT.isFloatingPoint())
1994 Op = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1995 else
1996 Op = DAG.getConstant(0, getCurSDLoc(), EltVT);
1997
1998 return DAG.getSplat(VT, getCurSDLoc(), Op);
1999 }
2000
2001 llvm_unreachable("Unknown vector constant");
2002 }
2003
2004 // If this is a static alloca, generate it as the frameindex instead of
2005 // computation.
2006 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
2008 FuncInfo.StaticAllocaMap.find(AI);
2009 if (SI != FuncInfo.StaticAllocaMap.end())
2010 return DAG.getFrameIndex(
2011 SI->second, TLI.getValueType(DAG.getDataLayout(), AI->getType()));
2012 }
2013
2014 // If this is an instruction which fast-isel has deferred, select it now.
2015 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
2016 Register InReg = FuncInfo.InitializeRegForValue(Inst);
2017
2018 std::optional<CallingConv::ID> CallConv;
2019 auto *CB = dyn_cast<CallBase>(Inst);
2020 if (CB && !CB->isInlineAsm())
2021 CallConv = CB->getCallingConv();
2022
2023 RegsForValue RFV(*DAG.getContext(), TLI, DAG.getDataLayout(), InReg,
2024 Inst->getType(), CallConv);
2025 SDValue Chain = DAG.getEntryNode();
2026 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, V);
2027 }
2028
2029 if (const MetadataAsValue *MD = dyn_cast<MetadataAsValue>(V))
2030 return DAG.getMDNode(cast<MDNode>(MD->getMetadata()));
2031
2032 if (const auto *BB = dyn_cast<BasicBlock>(V))
2033 return DAG.getBasicBlock(FuncInfo.getMBB(BB));
2034
2035 llvm_unreachable("Can't get register for value!");
2036}
2037
2038void SelectionDAGBuilder::visitCatchPad(const CatchPadInst &I) {
2040 bool IsMSVCCXX = Pers == EHPersonality::MSVC_CXX;
2041 bool IsCoreCLR = Pers == EHPersonality::CoreCLR;
2042 bool IsSEH = isAsynchronousEHPersonality(Pers);
2043 MachineBasicBlock *CatchPadMBB = FuncInfo.MBB;
2044 if (IsSEH) {
2045 // For SEH, EHCont Guard needs to know that this catchpad is a target.
2046 CatchPadMBB->setIsEHContTarget(true);
2048 } else
2049 CatchPadMBB->setIsEHScopeEntry();
2050 // In MSVC C++ and CoreCLR, catchblocks are funclets and need prologues.
2051 if (IsMSVCCXX || IsCoreCLR)
2052 CatchPadMBB->setIsEHFuncletEntry();
2053}
2054
2055void SelectionDAGBuilder::visitCatchRet(const CatchReturnInst &I) {
2056 // Update machine-CFG edge.
2057 MachineBasicBlock *TargetMBB = FuncInfo.getMBB(I.getSuccessor());
2058 FuncInfo.MBB->addSuccessor(TargetMBB);
2059
2060 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
2061 bool IsSEH = isAsynchronousEHPersonality(Pers);
2062 if (IsSEH) {
2063 // If this is not a fall-through branch or optimizations are switched off,
2064 // emit the branch.
2065 if (TargetMBB != NextBlock(FuncInfo.MBB) ||
2066 TM.getOptLevel() == CodeGenOptLevel::None)
2067 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
2068 getControlRoot(), DAG.getBasicBlock(TargetMBB)));
2069 return;
2070 }
2071
2072 // For non-SEH, EHCont Guard needs to know that this catchret is a target.
2073 TargetMBB->setIsEHContTarget(true);
2074 DAG.getMachineFunction().setHasEHContTarget(true);
2075
2076 // Figure out the funclet membership for the catchret's successor.
2077 // This will be used by the FuncletLayout pass to determine how to order the
2078 // BB's.
2079 // A 'catchret' returns to the outer scope's color.
2080 Value *ParentPad = I.getCatchSwitchParentPad();
2081 const BasicBlock *SuccessorColor;
2082 if (isa<ConstantTokenNone>(ParentPad))
2083 SuccessorColor = &FuncInfo.Fn->getEntryBlock();
2084 else
2085 SuccessorColor = cast<Instruction>(ParentPad)->getParent();
2086 assert(SuccessorColor && "No parent funclet for catchret!");
2087 MachineBasicBlock *SuccessorColorMBB = FuncInfo.getMBB(SuccessorColor);
2088 assert(SuccessorColorMBB && "No MBB for SuccessorColor!");
2089
2090 // Create the terminator node.
2091 SDValue Ret = DAG.getNode(ISD::CATCHRET, getCurSDLoc(), MVT::Other,
2092 getControlRoot(), DAG.getBasicBlock(TargetMBB),
2093 DAG.getBasicBlock(SuccessorColorMBB));
2094 DAG.setRoot(Ret);
2095}
2096
2097void SelectionDAGBuilder::visitCleanupPad(const CleanupPadInst &CPI) {
2098 // Don't emit any special code for the cleanuppad instruction. It just marks
2099 // the start of an EH scope/funclet.
2100 FuncInfo.MBB->setIsEHScopeEntry();
2101 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
2102 if (Pers != EHPersonality::Wasm_CXX) {
2103 FuncInfo.MBB->setIsEHFuncletEntry();
2104 FuncInfo.MBB->setIsCleanupFuncletEntry();
2105 }
2106}
2107
2108/// When an invoke or a cleanupret unwinds to the next EH pad, there are
2109/// many places it could ultimately go. In the IR, we have a single unwind
2110/// destination, but in the machine CFG, we enumerate all the possible blocks.
2111/// This function skips over imaginary basic blocks that hold catchswitch
2112/// instructions, and finds all the "real" machine
2113/// basic block destinations. As those destinations may not be successors of
2114/// EHPadBB, here we also calculate the edge probability to those destinations.
2115/// The passed-in Prob is the edge probability to EHPadBB.
2117 FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB,
2118 BranchProbability Prob,
2119 SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>>
2120 &UnwindDests) {
2121 EHPersonality Personality =
2123 bool IsMSVCCXX = Personality == EHPersonality::MSVC_CXX;
2124 bool IsCoreCLR = Personality == EHPersonality::CoreCLR;
2125 bool IsWasmCXX = Personality == EHPersonality::Wasm_CXX;
2126 bool IsSEH = isAsynchronousEHPersonality(Personality);
2127
2128 while (EHPadBB) {
2130 BasicBlock *NewEHPadBB = nullptr;
2131 if (isa<LandingPadInst>(Pad)) {
2132 // Stop on landingpads. They are not funclets.
2133 UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
2134 break;
2135 } else if (isa<CleanupPadInst>(Pad)) {
2136 // Stop on cleanup pads. Cleanups are always funclet entries for all known
2137 // personalities except Wasm. And in Wasm this becomes a catch_all(_ref),
2138 // which always catches an exception.
2139 UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
2140 UnwindDests.back().first->setIsEHScopeEntry();
2141 // In Wasm, EH scopes are not funclets
2142 if (!IsWasmCXX)
2143 UnwindDests.back().first->setIsEHFuncletEntry();
2144 break;
2145 } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) {
2146 // Add the catchpad handlers to the possible destinations.
2147 for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
2148 UnwindDests.emplace_back(FuncInfo.getMBB(CatchPadBB), Prob);
2149 // For MSVC++ and the CLR, catchblocks are funclets and need prologues.
2150 if (IsMSVCCXX || IsCoreCLR)
2151 UnwindDests.back().first->setIsEHFuncletEntry();
2152 if (!IsSEH)
2153 UnwindDests.back().first->setIsEHScopeEntry();
2154 }
2155 NewEHPadBB = CatchSwitch->getUnwindDest();
2156 } else {
2157 continue;
2158 }
2159
2160 BranchProbabilityInfo *BPI = FuncInfo.BPI;
2161 if (BPI && NewEHPadBB)
2162 Prob *= BPI->getEdgeProbability(EHPadBB, NewEHPadBB);
2163 EHPadBB = NewEHPadBB;
2164 }
2165}
2166
2167void SelectionDAGBuilder::visitCleanupRet(const CleanupReturnInst &I) {
2168 // Update successor info.
2170 auto UnwindDest = I.getUnwindDest();
2171 BranchProbabilityInfo *BPI = FuncInfo.BPI;
2172 BranchProbability UnwindDestProb =
2173 (BPI && UnwindDest)
2174 ? BPI->getEdgeProbability(FuncInfo.MBB->getBasicBlock(), UnwindDest)
2176 findUnwindDestinations(FuncInfo, UnwindDest, UnwindDestProb, UnwindDests);
2177 for (auto &UnwindDest : UnwindDests) {
2178 UnwindDest.first->setIsEHPad();
2179 addSuccessorWithProb(FuncInfo.MBB, UnwindDest.first, UnwindDest.second);
2180 }
2181 FuncInfo.MBB->normalizeSuccProbs();
2182
2183 // Create the terminator node.
2184 MachineBasicBlock *CleanupPadMBB =
2185 FuncInfo.getMBB(I.getCleanupPad()->getParent());
2186 SDValue Ret = DAG.getNode(ISD::CLEANUPRET, getCurSDLoc(), MVT::Other,
2187 getControlRoot(), DAG.getBasicBlock(CleanupPadMBB));
2188 DAG.setRoot(Ret);
2189}
2190
2191void SelectionDAGBuilder::visitCatchSwitch(const CatchSwitchInst &CSI) {
2192 report_fatal_error("visitCatchSwitch not yet implemented!");
2193}
2194
2195void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
2196 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2197 auto &DL = DAG.getDataLayout();
2198 SDValue Chain = getControlRoot();
2201
2202 // Calls to @llvm.experimental.deoptimize don't generate a return value, so
2203 // lower
2204 //
2205 // %val = call <ty> @llvm.experimental.deoptimize()
2206 // ret <ty> %val
2207 //
2208 // differently.
2209 if (I.getParent()->getTerminatingDeoptimizeCall()) {
2211 return;
2212 }
2213
2214 if (!FuncInfo.CanLowerReturn) {
2215 Register DemoteReg = FuncInfo.DemoteRegister;
2216
2217 // Emit a store of the return value through the virtual register.
2218 // Leave Outs empty so that LowerReturn won't try to load return
2219 // registers the usual way.
2220 MVT PtrValueVT = TLI.getPointerTy(DL, DL.getAllocaAddrSpace());
2221 SDValue RetPtr =
2222 DAG.getCopyFromReg(Chain, getCurSDLoc(), DemoteReg, PtrValueVT);
2223 SDValue RetOp = getValue(I.getOperand(0));
2224
2225 SmallVector<EVT, 4> ValueVTs, MemVTs;
2226 SmallVector<uint64_t, 4> Offsets;
2227 ComputeValueVTs(TLI, DL, I.getOperand(0)->getType(), ValueVTs, &MemVTs,
2228 &Offsets, 0);
2229 unsigned NumValues = ValueVTs.size();
2230
2231 SmallVector<SDValue, 4> Chains(NumValues);
2232 Align BaseAlign = DL.getPrefTypeAlign(I.getOperand(0)->getType());
2233 for (unsigned i = 0; i != NumValues; ++i) {
2234 // An aggregate return value cannot wrap around the address space, so
2235 // offsets to its parts don't wrap either.
2236 SDValue Ptr = DAG.getObjectPtrOffset(getCurSDLoc(), RetPtr,
2237 TypeSize::getFixed(Offsets[i]));
2238
2239 SDValue Val = RetOp.getValue(RetOp.getResNo() + i);
2240 if (MemVTs[i] != ValueVTs[i])
2241 Val = DAG.getPtrExtOrTrunc(Val, getCurSDLoc(), MemVTs[i]);
2242 Chains[i] = DAG.getStore(
2243 Chain, getCurSDLoc(), Val,
2244 // FIXME: better loc info would be nice.
2245 Ptr, MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()),
2246 commonAlignment(BaseAlign, Offsets[i]));
2247 }
2248
2249 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
2250 MVT::Other, Chains);
2251 } else if (I.getNumOperands() != 0) {
2253 ComputeValueTypes(DL, I.getOperand(0)->getType(), Types);
2254 unsigned NumValues = Types.size();
2255 if (NumValues) {
2256 SDValue RetOp = getValue(I.getOperand(0));
2257
2258 const Function *F = I.getParent()->getParent();
2259
2260 bool NeedsRegBlock = TLI.functionArgumentNeedsConsecutiveRegisters(
2261 I.getOperand(0)->getType(), F->getCallingConv(),
2262 /*IsVarArg*/ false, DL);
2263
2264 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
2265 if (F->getAttributes().hasRetAttr(Attribute::SExt))
2266 ExtendKind = ISD::SIGN_EXTEND;
2267 else if (F->getAttributes().hasRetAttr(Attribute::ZExt))
2268 ExtendKind = ISD::ZERO_EXTEND;
2269
2270 LLVMContext &Context = F->getContext();
2271 bool RetInReg = F->getAttributes().hasRetAttr(Attribute::InReg);
2272
2273 for (unsigned j = 0; j != NumValues; ++j) {
2274 EVT VT = TLI.getValueType(DL, Types[j]);
2275
2276 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
2277 VT = TLI.getTypeForExtReturn(Context, VT, ExtendKind);
2278
2279 CallingConv::ID CC = F->getCallingConv();
2280
2281 unsigned NumParts = TLI.getNumRegistersForCallingConv(Context, CC, VT);
2282 MVT PartVT = TLI.getRegisterTypeForCallingConv(Context, CC, VT);
2283 SmallVector<SDValue, 4> Parts(NumParts);
2285 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
2286 &Parts[0], NumParts, PartVT, &I, CC, ExtendKind);
2287
2288 // 'inreg' on function refers to return value
2289 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2290 if (RetInReg)
2291 Flags.setInReg();
2292
2293 if (I.getOperand(0)->getType()->isPointerTy()) {
2294 Flags.setPointer();
2295 Flags.setPointerAddrSpace(
2296 cast<PointerType>(I.getOperand(0)->getType())->getAddressSpace());
2297 }
2298
2299 if (NeedsRegBlock) {
2300 Flags.setInConsecutiveRegs();
2301 if (j == NumValues - 1)
2302 Flags.setInConsecutiveRegsLast();
2303 }
2304
2305 // Propagate extension type if any
2306 if (ExtendKind == ISD::SIGN_EXTEND)
2307 Flags.setSExt();
2308 else if (ExtendKind == ISD::ZERO_EXTEND)
2309 Flags.setZExt();
2310 else if (F->getAttributes().hasRetAttr(Attribute::NoExt))
2311 Flags.setNoExt();
2312
2313 for (unsigned i = 0; i < NumParts; ++i) {
2314 Outs.push_back(ISD::OutputArg(Flags,
2315 Parts[i].getValueType().getSimpleVT(),
2316 VT, Types[j], 0, 0));
2317 OutVals.push_back(Parts[i]);
2318 }
2319 }
2320 }
2321 }
2322
2323 // Push in swifterror virtual register as the last element of Outs. This makes
2324 // sure swifterror virtual register will be returned in the swifterror
2325 // physical register.
2326 const Function *F = I.getParent()->getParent();
2327 if (TLI.supportSwiftError() &&
2328 F->getAttributes().hasAttrSomewhere(Attribute::SwiftError)) {
2329 assert(SwiftError.getFunctionArg() && "Need a swift error argument");
2330 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2331 Flags.setSwiftError();
2332 Outs.push_back(ISD::OutputArg(Flags, /*vt=*/TLI.getPointerTy(DL),
2333 /*argvt=*/EVT(TLI.getPointerTy(DL)),
2334 PointerType::getUnqual(*DAG.getContext()),
2335 /*origidx=*/1, /*partOffs=*/0));
2336 // Create SDNode for the swifterror virtual register.
2337 OutVals.push_back(
2338 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(
2339 &I, FuncInfo.MBB, SwiftError.getFunctionArg()),
2340 EVT(TLI.getPointerTy(DL))));
2341 }
2342
2343 bool isVarArg = DAG.getMachineFunction().getFunction().isVarArg();
2344 CallingConv::ID CallConv =
2345 DAG.getMachineFunction().getFunction().getCallingConv();
2346 Chain = DAG.getTargetLoweringInfo().LowerReturn(
2347 Chain, CallConv, isVarArg, Outs, OutVals, getCurSDLoc(), DAG);
2348
2349 // Verify that the target's LowerReturn behaved as expected.
2350 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
2351 "LowerReturn didn't return a valid chain!");
2352
2353 // Update the DAG with the new chain value resulting from return lowering.
2354 DAG.setRoot(Chain);
2355}
2356
2357/// CopyToExportRegsIfNeeded - If the given value has virtual registers
2358/// created for it, emit nodes to copy the value into the virtual
2359/// registers.
2361 // Skip empty types
2362 if (V->getType()->isEmptyTy())
2363 return;
2364
2366 if (VMI != FuncInfo.ValueMap.end()) {
2367 assert((!V->use_empty() || isa<CallBrInst>(V)) &&
2368 "Unused value assigned virtual registers!");
2369 CopyValueToVirtualRegister(V, VMI->second);
2370 }
2371}
2372
2373/// ExportFromCurrentBlock - If this condition isn't known to be exported from
2374/// the current basic block, add it to ValueMap now so that we'll get a
2375/// CopyTo/FromReg.
2377 // No need to export constants.
2378 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
2379
2380 // Already exported?
2381 if (FuncInfo.isExportedInst(V)) return;
2382
2383 Register Reg = FuncInfo.InitializeRegForValue(V);
2385}
2386
2388 const BasicBlock *FromBB) {
2389 // The operands of the setcc have to be in this block. We don't know
2390 // how to export them from some other block.
2391 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
2392 // Can export from current BB.
2393 if (VI->getParent() == FromBB)
2394 return true;
2395
2396 // Is already exported, noop.
2397 return FuncInfo.isExportedInst(V);
2398 }
2399
2400 // If this is an argument, we can export it if the BB is the entry block or
2401 // if it is already exported.
2402 if (isa<Argument>(V)) {
2403 if (FromBB->isEntryBlock())
2404 return true;
2405
2406 // Otherwise, can only export this if it is already exported.
2407 return FuncInfo.isExportedInst(V);
2408 }
2409
2410 // Otherwise, constants can always be exported.
2411 return true;
2412}
2413
2414/// Return branch probability calculated by BranchProbabilityInfo for IR blocks.
2416SelectionDAGBuilder::getEdgeProbability(const MachineBasicBlock *Src,
2417 const MachineBasicBlock *Dst) const {
2419 const BasicBlock *SrcBB = Src->getBasicBlock();
2420 const BasicBlock *DstBB = Dst->getBasicBlock();
2421 if (!BPI) {
2422 // If BPI is not available, set the default probability as 1 / N, where N is
2423 // the number of successors.
2424 auto SuccSize = std::max<uint32_t>(succ_size(SrcBB), 1);
2425 return BranchProbability(1, SuccSize);
2426 }
2427 return BPI->getEdgeProbability(SrcBB, DstBB);
2428}
2429
2430void SelectionDAGBuilder::addSuccessorWithProb(MachineBasicBlock *Src,
2431 MachineBasicBlock *Dst,
2432 BranchProbability Prob) {
2433 if (!FuncInfo.BPI)
2434 Src->addSuccessorWithoutProb(Dst);
2435 else {
2436 if (Prob.isUnknown())
2437 Prob = getEdgeProbability(Src, Dst);
2438 Src->addSuccessor(Dst, Prob);
2439 }
2440}
2441
2442static bool InBlock(const Value *V, const BasicBlock *BB) {
2443 if (const Instruction *I = dyn_cast<Instruction>(V))
2444 return I->getParent() == BB;
2445 return true;
2446}
2447
2448/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
2449/// This function emits a branch and is used at the leaves of an OR or an
2450/// AND operator tree.
2451void
2454 MachineBasicBlock *FBB,
2455 MachineBasicBlock *CurBB,
2456 MachineBasicBlock *SwitchBB,
2457 BranchProbability TProb,
2458 BranchProbability FProb,
2459 bool InvertCond) {
2460 const BasicBlock *BB = CurBB->getBasicBlock();
2461
2462 // If the leaf of the tree is a comparison, merge the condition into
2463 // the caseblock.
2464 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
2465 // The operands of the cmp have to be in this block. We don't know
2466 // how to export them from some other block. If this is the first block
2467 // of the sequence, no exporting is needed.
2468 if (CurBB == SwitchBB ||
2469 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
2470 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
2471 ISD::CondCode Condition;
2472 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
2473 ICmpInst::Predicate Pred =
2474 InvertCond ? IC->getInversePredicate() : IC->getPredicate();
2475 Condition = getICmpCondCode(Pred);
2476 } else {
2477 const FCmpInst *FC = cast<FCmpInst>(Cond);
2478 FCmpInst::Predicate Pred =
2479 InvertCond ? FC->getInversePredicate() : FC->getPredicate();
2480 Condition = getFCmpCondCode(Pred);
2481 if (TM.Options.NoNaNsFPMath)
2482 Condition = getFCmpCodeWithoutNaN(Condition);
2483 }
2484
2485 CaseBlock CB(Condition, BOp->getOperand(0), BOp->getOperand(1), nullptr,
2486 TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2487 SL->SwitchCases.push_back(CB);
2488 return;
2489 }
2490 }
2491
2492 // Create a CaseBlock record representing this branch.
2493 ISD::CondCode Opc = InvertCond ? ISD::SETNE : ISD::SETEQ;
2494 CaseBlock CB(Opc, Cond, ConstantInt::getTrue(*DAG.getContext()),
2495 nullptr, TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2496 SL->SwitchCases.push_back(CB);
2497}
2498
2499// Collect dependencies on V recursively. This is used for the cost analysis in
2500// `shouldKeepJumpConditionsTogether`.
2504 unsigned Depth = 0) {
2505 // Return false if we have an incomplete count.
2507 return false;
2508
2509 auto *I = dyn_cast<Instruction>(V);
2510 if (I == nullptr)
2511 return true;
2512
2513 if (Necessary != nullptr) {
2514 // This instruction is necessary for the other side of the condition so
2515 // don't count it.
2516 if (Necessary->contains(I))
2517 return true;
2518 }
2519
2520 // Already added this dep.
2521 if (!Deps->try_emplace(I, false).second)
2522 return true;
2523
2524 for (unsigned OpIdx = 0, E = I->getNumOperands(); OpIdx < E; ++OpIdx)
2525 if (!collectInstructionDeps(Deps, I->getOperand(OpIdx), Necessary,
2526 Depth + 1))
2527 return false;
2528 return true;
2529}
2530
2533 Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs,
2535 if (I.getNumSuccessors() != 2)
2536 return false;
2537
2538 if (!I.isConditional())
2539 return false;
2540
2541 if (Params.BaseCost < 0)
2542 return false;
2543
2544 // Baseline cost.
2545 InstructionCost CostThresh = Params.BaseCost;
2546
2547 BranchProbabilityInfo *BPI = nullptr;
2548 if (Params.LikelyBias || Params.UnlikelyBias)
2549 BPI = FuncInfo.BPI;
2550 if (BPI != nullptr) {
2551 // See if we are either likely to get an early out or compute both lhs/rhs
2552 // of the condition.
2553 BasicBlock *IfFalse = I.getSuccessor(0);
2554 BasicBlock *IfTrue = I.getSuccessor(1);
2555
2556 std::optional<bool> Likely;
2557 if (BPI->isEdgeHot(I.getParent(), IfTrue))
2558 Likely = true;
2559 else if (BPI->isEdgeHot(I.getParent(), IfFalse))
2560 Likely = false;
2561
2562 if (Likely) {
2563 if (Opc == (*Likely ? Instruction::And : Instruction::Or))
2564 // Its likely we will have to compute both lhs and rhs of condition
2565 CostThresh += Params.LikelyBias;
2566 else {
2567 if (Params.UnlikelyBias < 0)
2568 return false;
2569 // Its likely we will get an early out.
2570 CostThresh -= Params.UnlikelyBias;
2571 }
2572 }
2573 }
2574
2575 if (CostThresh <= 0)
2576 return false;
2577
2578 // Collect "all" instructions that lhs condition is dependent on.
2579 // Use map for stable iteration (to avoid non-determanism of iteration of
2580 // SmallPtrSet). The `bool` value is just a dummy.
2582 collectInstructionDeps(&LhsDeps, Lhs);
2583 // Collect "all" instructions that rhs condition is dependent on AND are
2584 // dependencies of lhs. This gives us an estimate on which instructions we
2585 // stand to save by splitting the condition.
2586 if (!collectInstructionDeps(&RhsDeps, Rhs, &LhsDeps))
2587 return false;
2588 // Add the compare instruction itself unless its a dependency on the LHS.
2589 if (const auto *RhsI = dyn_cast<Instruction>(Rhs))
2590 if (!LhsDeps.contains(RhsI))
2591 RhsDeps.try_emplace(RhsI, false);
2592
2593 InstructionCost CostOfIncluding = 0;
2594 // See if this instruction will need to computed independently of whether RHS
2595 // is.
2596 Value *BrCond = I.getCondition();
2597 auto ShouldCountInsn = [&RhsDeps, &BrCond](const Instruction *Ins) {
2598 for (const auto *U : Ins->users()) {
2599 // If user is independent of RHS calculation we don't need to count it.
2600 if (auto *UIns = dyn_cast<Instruction>(U))
2601 if (UIns != BrCond && !RhsDeps.contains(UIns))
2602 return false;
2603 }
2604 return true;
2605 };
2606
2607 // Prune instructions from RHS Deps that are dependencies of unrelated
2608 // instructions. The value (SelectionDAG::MaxRecursionDepth) is fairly
2609 // arbitrary and just meant to cap the how much time we spend in the pruning
2610 // loop. Its highly unlikely to come into affect.
2611 const unsigned MaxPruneIters = SelectionDAG::MaxRecursionDepth;
2612 // Stop after a certain point. No incorrectness from including too many
2613 // instructions.
2614 for (unsigned PruneIters = 0; PruneIters < MaxPruneIters; ++PruneIters) {
2615 const Instruction *ToDrop = nullptr;
2616 for (const auto &InsPair : RhsDeps) {
2617 if (!ShouldCountInsn(InsPair.first)) {
2618 ToDrop = InsPair.first;
2619 break;
2620 }
2621 }
2622 if (ToDrop == nullptr)
2623 break;
2624 RhsDeps.erase(ToDrop);
2625 }
2626
2627 for (const auto &InsPair : RhsDeps) {
2628 // Finally accumulate latency that we can only attribute to computing the
2629 // RHS condition. Use latency because we are essentially trying to calculate
2630 // the cost of the dependency chain.
2631 // Possible TODO: We could try to estimate ILP and make this more precise.
2632 CostOfIncluding += TTI->getInstructionCost(
2633 InsPair.first, TargetTransformInfo::TCK_Latency);
2634
2635 if (CostOfIncluding > CostThresh)
2636 return false;
2637 }
2638 return true;
2639}
2640
2643 MachineBasicBlock *FBB,
2644 MachineBasicBlock *CurBB,
2645 MachineBasicBlock *SwitchBB,
2647 BranchProbability TProb,
2648 BranchProbability FProb,
2649 bool InvertCond) {
2650 // Skip over not part of the tree and remember to invert op and operands at
2651 // next level.
2652 Value *NotCond;
2653 if (match(Cond, m_OneUse(m_Not(m_Value(NotCond)))) &&
2654 InBlock(NotCond, CurBB->getBasicBlock())) {
2655 FindMergedConditions(NotCond, TBB, FBB, CurBB, SwitchBB, Opc, TProb, FProb,
2656 !InvertCond);
2657 return;
2658 }
2659
2661 const Value *BOpOp0, *BOpOp1;
2662 // Compute the effective opcode for Cond, taking into account whether it needs
2663 // to be inverted, e.g.
2664 // and (not (or A, B)), C
2665 // gets lowered as
2666 // and (and (not A, not B), C)
2668 if (BOp) {
2669 BOpc = match(BOp, m_LogicalAnd(m_Value(BOpOp0), m_Value(BOpOp1)))
2670 ? Instruction::And
2671 : (match(BOp, m_LogicalOr(m_Value(BOpOp0), m_Value(BOpOp1)))
2672 ? Instruction::Or
2674 if (InvertCond) {
2675 if (BOpc == Instruction::And)
2676 BOpc = Instruction::Or;
2677 else if (BOpc == Instruction::Or)
2678 BOpc = Instruction::And;
2679 }
2680 }
2681
2682 // If this node is not part of the or/and tree, emit it as a branch.
2683 // Note that all nodes in the tree should have same opcode.
2684 bool BOpIsInOrAndTree = BOpc && BOpc == Opc && BOp->hasOneUse();
2685 if (!BOpIsInOrAndTree || BOp->getParent() != CurBB->getBasicBlock() ||
2686 !InBlock(BOpOp0, CurBB->getBasicBlock()) ||
2687 !InBlock(BOpOp1, CurBB->getBasicBlock())) {
2688 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB,
2689 TProb, FProb, InvertCond);
2690 return;
2691 }
2692
2693 // Create TmpBB after CurBB.
2694 MachineFunction::iterator BBI(CurBB);
2695 MachineFunction &MF = DAG.getMachineFunction();
2697 CurBB->getParent()->insert(++BBI, TmpBB);
2698
2699 if (Opc == Instruction::Or) {
2700 // Codegen X | Y as:
2701 // BB1:
2702 // jmp_if_X TBB
2703 // jmp TmpBB
2704 // TmpBB:
2705 // jmp_if_Y TBB
2706 // jmp FBB
2707 //
2708
2709 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2710 // The requirement is that
2711 // TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB)
2712 // = TrueProb for original BB.
2713 // Assuming the original probabilities are A and B, one choice is to set
2714 // BB1's probabilities to A/2 and A/2+B, and set TmpBB's probabilities to
2715 // A/(1+B) and 2B/(1+B). This choice assumes that
2716 // TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB.
2717 // Another choice is to assume TrueProb for BB1 equals to TrueProb for
2718 // TmpBB, but the math is more complicated.
2719
2720 auto NewTrueProb = TProb / 2;
2721 auto NewFalseProb = TProb / 2 + FProb;
2722 // Emit the LHS condition.
2723 FindMergedConditions(BOpOp0, TBB, TmpBB, CurBB, SwitchBB, Opc, NewTrueProb,
2724 NewFalseProb, InvertCond);
2725
2726 // Normalize A/2 and B to get A/(1+B) and 2B/(1+B).
2727 SmallVector<BranchProbability, 2> Probs{TProb / 2, FProb};
2729 // Emit the RHS condition into TmpBB.
2730 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2731 Probs[1], InvertCond);
2732 } else {
2733 assert(Opc == Instruction::And && "Unknown merge op!");
2734 // Codegen X & Y as:
2735 // BB1:
2736 // jmp_if_X TmpBB
2737 // jmp FBB
2738 // TmpBB:
2739 // jmp_if_Y TBB
2740 // jmp FBB
2741 //
2742 // This requires creation of TmpBB after CurBB.
2743
2744 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2745 // The requirement is that
2746 // FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB)
2747 // = FalseProb for original BB.
2748 // Assuming the original probabilities are A and B, one choice is to set
2749 // BB1's probabilities to A+B/2 and B/2, and set TmpBB's probabilities to
2750 // 2A/(1+A) and B/(1+A). This choice assumes that FalseProb for BB1 ==
2751 // TrueProb for BB1 * FalseProb for TmpBB.
2752
2753 auto NewTrueProb = TProb + FProb / 2;
2754 auto NewFalseProb = FProb / 2;
2755 // Emit the LHS condition.
2756 FindMergedConditions(BOpOp0, TmpBB, FBB, CurBB, SwitchBB, Opc, NewTrueProb,
2757 NewFalseProb, InvertCond);
2758
2759 // Normalize A and B/2 to get 2A/(1+A) and B/(1+A).
2760 SmallVector<BranchProbability, 2> Probs{TProb, FProb / 2};
2762 // Emit the RHS condition into TmpBB.
2763 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2764 Probs[1], InvertCond);
2765 }
2766}
2767
2768/// If the set of cases should be emitted as a series of branches, return true.
2769/// If we should emit this as a bunch of and/or'd together conditions, return
2770/// false.
2771bool
2772SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases) {
2773 if (Cases.size() != 2) return true;
2774
2775 // If this is two comparisons of the same values or'd or and'd together, they
2776 // will get folded into a single comparison, so don't emit two blocks.
2777 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
2778 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
2779 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
2780 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
2781 return false;
2782 }
2783
2784 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
2785 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
2786 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
2787 Cases[0].CC == Cases[1].CC &&
2788 isa<Constant>(Cases[0].CmpRHS) &&
2789 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
2790 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
2791 return false;
2792 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
2793 return false;
2794 }
2795
2796 return true;
2797}
2798
2799void SelectionDAGBuilder::visitBr(const BranchInst &I) {
2801
2802 // Update machine-CFG edges.
2803 MachineBasicBlock *Succ0MBB = FuncInfo.getMBB(I.getSuccessor(0));
2804
2805 if (I.isUnconditional()) {
2806 // Update machine-CFG edges.
2807 BrMBB->addSuccessor(Succ0MBB);
2808
2809 // If this is not a fall-through branch or optimizations are switched off,
2810 // emit the branch.
2811 if (Succ0MBB != NextBlock(BrMBB) ||
2813 auto Br = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
2814 getControlRoot(), DAG.getBasicBlock(Succ0MBB));
2815 setValue(&I, Br);
2816 DAG.setRoot(Br);
2817 }
2818
2819 return;
2820 }
2821
2822 // If this condition is one of the special cases we handle, do special stuff
2823 // now.
2824 const Value *CondVal = I.getCondition();
2825 MachineBasicBlock *Succ1MBB = FuncInfo.getMBB(I.getSuccessor(1));
2826
2827 // If this is a series of conditions that are or'd or and'd together, emit
2828 // this as a sequence of branches instead of setcc's with and/or operations.
2829 // As long as jumps are not expensive (exceptions for multi-use logic ops,
2830 // unpredictable branches, and vector extracts because those jumps are likely
2831 // expensive for any target), this should improve performance.
2832 // For example, instead of something like:
2833 // cmp A, B
2834 // C = seteq
2835 // cmp D, E
2836 // F = setle
2837 // or C, F
2838 // jnz foo
2839 // Emit:
2840 // cmp A, B
2841 // je foo
2842 // cmp D, E
2843 // jle foo
2844 bool IsUnpredictable = I.hasMetadata(LLVMContext::MD_unpredictable);
2845 const Instruction *BOp = dyn_cast<Instruction>(CondVal);
2846 if (!DAG.getTargetLoweringInfo().isJumpExpensive() && BOp &&
2847 BOp->hasOneUse() && !IsUnpredictable) {
2848 Value *Vec;
2849 const Value *BOp0, *BOp1;
2851 if (match(BOp, m_LogicalAnd(m_Value(BOp0), m_Value(BOp1))))
2852 Opcode = Instruction::And;
2853 else if (match(BOp, m_LogicalOr(m_Value(BOp0), m_Value(BOp1))))
2854 Opcode = Instruction::Or;
2855
2856 if (Opcode &&
2857 !(match(BOp0, m_ExtractElt(m_Value(Vec), m_Value())) &&
2858 match(BOp1, m_ExtractElt(m_Specific(Vec), m_Value()))) &&
2860 FuncInfo, I, Opcode, BOp0, BOp1,
2861 DAG.getTargetLoweringInfo().getJumpConditionMergingParams(
2862 Opcode, BOp0, BOp1))) {
2863 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB, Opcode,
2864 getEdgeProbability(BrMBB, Succ0MBB),
2865 getEdgeProbability(BrMBB, Succ1MBB),
2866 /*InvertCond=*/false);
2867 // If the compares in later blocks need to use values not currently
2868 // exported from this block, export them now. This block should always
2869 // be the first entry.
2870 assert(SL->SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
2871
2872 // Allow some cases to be rejected.
2873 if (ShouldEmitAsBranches(SL->SwitchCases)) {
2874 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i) {
2875 ExportFromCurrentBlock(SL->SwitchCases[i].CmpLHS);
2876 ExportFromCurrentBlock(SL->SwitchCases[i].CmpRHS);
2877 }
2878
2879 // Emit the branch for this block.
2880 visitSwitchCase(SL->SwitchCases[0], BrMBB);
2881 SL->SwitchCases.erase(SL->SwitchCases.begin());
2882 return;
2883 }
2884
2885 // Okay, we decided not to do this, remove any inserted MBB's and clear
2886 // SwitchCases.
2887 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i)
2888 FuncInfo.MF->erase(SL->SwitchCases[i].ThisBB);
2889
2890 SL->SwitchCases.clear();
2891 }
2892 }
2893
2894 // Create a CaseBlock record representing this branch.
2895 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
2896 nullptr, Succ0MBB, Succ1MBB, BrMBB, getCurSDLoc(),
2898 IsUnpredictable);
2899
2900 // Use visitSwitchCase to actually insert the fast branch sequence for this
2901 // cond branch.
2902 visitSwitchCase(CB, BrMBB);
2903}
2904
2905/// visitSwitchCase - Emits the necessary code to represent a single node in
2906/// the binary search tree resulting from lowering a switch instruction.
2908 MachineBasicBlock *SwitchBB) {
2909 SDValue Cond;
2910 SDValue CondLHS = getValue(CB.CmpLHS);
2911 SDLoc dl = CB.DL;
2912
2913 if (CB.CC == ISD::SETTRUE) {
2914 // Branch or fall through to TrueBB.
2915 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2916 SwitchBB->normalizeSuccProbs();
2917 if (CB.TrueBB != NextBlock(SwitchBB)) {
2918 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, getControlRoot(),
2919 DAG.getBasicBlock(CB.TrueBB)));
2920 }
2921 return;
2922 }
2923
2924 auto &TLI = DAG.getTargetLoweringInfo();
2925 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), CB.CmpLHS->getType());
2926
2927 // Build the setcc now.
2928 if (!CB.CmpMHS) {
2929 // Fold "(X == true)" to X and "(X == false)" to !X to
2930 // handle common cases produced by branch lowering.
2931 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
2932 CB.CC == ISD::SETEQ)
2933 Cond = CondLHS;
2934 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
2935 CB.CC == ISD::SETEQ) {
2936 SDValue True = DAG.getConstant(1, dl, CondLHS.getValueType());
2937 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
2938 } else {
2939 SDValue CondRHS = getValue(CB.CmpRHS);
2940
2941 // If a pointer's DAG type is larger than its memory type then the DAG
2942 // values are zero-extended. This breaks signed comparisons so truncate
2943 // back to the underlying type before doing the compare.
2944 if (CondLHS.getValueType() != MemVT) {
2945 CondLHS = DAG.getPtrExtOrTrunc(CondLHS, getCurSDLoc(), MemVT);
2946 CondRHS = DAG.getPtrExtOrTrunc(CondRHS, getCurSDLoc(), MemVT);
2947 }
2948 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, CondRHS, CB.CC);
2949 }
2950 } else {
2951 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
2952
2953 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
2954 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
2955
2956 SDValue CmpOp = getValue(CB.CmpMHS);
2957 EVT VT = CmpOp.getValueType();
2958
2959 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
2960 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, dl, VT),
2961 ISD::SETLE);
2962 } else {
2963 SDValue SUB = DAG.getNode(ISD::SUB, dl,
2964 VT, CmpOp, DAG.getConstant(Low, dl, VT));
2965 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
2966 DAG.getConstant(High-Low, dl, VT), ISD::SETULE);
2967 }
2968 }
2969
2970 // Update successor info
2971 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2972 // TrueBB and FalseBB are always different unless the incoming IR is
2973 // degenerate. This only happens when running llc on weird IR.
2974 if (CB.TrueBB != CB.FalseBB)
2975 addSuccessorWithProb(SwitchBB, CB.FalseBB, CB.FalseProb);
2976 SwitchBB->normalizeSuccProbs();
2977
2978 // If the lhs block is the next block, invert the condition so that we can
2979 // fall through to the lhs instead of the rhs block.
2980 if (CB.TrueBB == NextBlock(SwitchBB)) {
2981 std::swap(CB.TrueBB, CB.FalseBB);
2982 SDValue True = DAG.getConstant(1, dl, Cond.getValueType());
2983 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
2984 }
2985
2986 SDNodeFlags Flags;
2988 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, MVT::Other, getControlRoot(),
2989 Cond, DAG.getBasicBlock(CB.TrueBB), Flags);
2990
2991 setValue(CurInst, BrCond);
2992
2993 // Insert the false branch. Do this even if it's a fall through branch,
2994 // this makes it easier to do DAG optimizations which require inverting
2995 // the branch condition.
2996 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
2997 DAG.getBasicBlock(CB.FalseBB));
2998
2999 DAG.setRoot(BrCond);
3000}
3001
3002/// visitJumpTable - Emit JumpTable node in the current MBB
3004 // Emit the code for the jump table
3005 assert(JT.SL && "Should set SDLoc for SelectionDAG!");
3006 assert(JT.Reg && "Should lower JT Header first!");
3007 EVT PTy = DAG.getTargetLoweringInfo().getJumpTableRegTy(DAG.getDataLayout());
3008 SDValue Index = DAG.getCopyFromReg(getControlRoot(), *JT.SL, JT.Reg, PTy);
3009 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
3010 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, *JT.SL, MVT::Other,
3011 Index.getValue(1), Table, Index);
3012 DAG.setRoot(BrJumpTable);
3013}
3014
3015/// visitJumpTableHeader - This function emits necessary code to produce index
3016/// in the JumpTable from switch case.
3018 JumpTableHeader &JTH,
3019 MachineBasicBlock *SwitchBB) {
3020 assert(JT.SL && "Should set SDLoc for SelectionDAG!");
3021 const SDLoc &dl = *JT.SL;
3022
3023 // Subtract the lowest switch case value from the value being switched on.
3024 SDValue SwitchOp = getValue(JTH.SValue);
3025 EVT VT = SwitchOp.getValueType();
3026 SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, SwitchOp,
3027 DAG.getConstant(JTH.First, dl, VT));
3028
3029 // The SDNode we just created, which holds the value being switched on minus
3030 // the smallest case value, needs to be copied to a virtual register so it
3031 // can be used as an index into the jump table in a subsequent basic block.
3032 // This value may be smaller or larger than the target's pointer type, and
3033 // therefore require extension or truncating.
3034 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3035 SwitchOp =
3036 DAG.getZExtOrTrunc(Sub, dl, TLI.getJumpTableRegTy(DAG.getDataLayout()));
3037
3038 Register JumpTableReg =
3039 FuncInfo.CreateReg(TLI.getJumpTableRegTy(DAG.getDataLayout()));
3040 SDValue CopyTo =
3041 DAG.getCopyToReg(getControlRoot(), dl, JumpTableReg, SwitchOp);
3042 JT.Reg = JumpTableReg;
3043
3044 if (!JTH.FallthroughUnreachable) {
3045 // Emit the range check for the jump table, and branch to the default block
3046 // for the switch statement if the value being switched on exceeds the
3047 // largest case in the switch.
3048 SDValue CMP = DAG.getSetCC(
3049 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
3050 Sub.getValueType()),
3051 Sub, DAG.getConstant(JTH.Last - JTH.First, dl, VT), ISD::SETUGT);
3052
3053 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
3054 MVT::Other, CopyTo, CMP,
3055 DAG.getBasicBlock(JT.Default));
3056
3057 // Avoid emitting unnecessary branches to the next block.
3058 if (JT.MBB != NextBlock(SwitchBB))
3059 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
3060 DAG.getBasicBlock(JT.MBB));
3061
3062 DAG.setRoot(BrCond);
3063 } else {
3064 // Avoid emitting unnecessary branches to the next block.
3065 if (JT.MBB != NextBlock(SwitchBB))
3066 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, CopyTo,
3067 DAG.getBasicBlock(JT.MBB)));
3068 else
3069 DAG.setRoot(CopyTo);
3070 }
3071}
3072
3073/// Create a LOAD_STACK_GUARD node, and let it carry the target specific global
3074/// variable if there exists one.
3076 SDValue &Chain) {
3077 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3078 EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout());
3079 EVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout());
3083 DAG.getMachineNode(TargetOpcode::LOAD_STACK_GUARD, DL, PtrTy, Chain);
3084 if (Global) {
3085 MachinePointerInfo MPInfo(Global);
3089 MPInfo, Flags, PtrTy.getSizeInBits() / 8, DAG.getEVTAlign(PtrTy));
3090 DAG.setNodeMemRefs(Node, {MemRef});
3091 }
3092 if (PtrTy != PtrMemTy)
3093 return DAG.getPtrExtOrTrunc(SDValue(Node, 0), DL, PtrMemTy);
3094 return SDValue(Node, 0);
3095}
3096
3097/// Codegen a new tail for a stack protector check ParentMBB which has had its
3098/// tail spliced into a stack protector check success bb.
3099///
3100/// For a high level explanation of how this fits into the stack protector
3101/// generation see the comment on the declaration of class
3102/// StackProtectorDescriptor.
3104 MachineBasicBlock *ParentBB) {
3105
3106 // First create the loads to the guard/stack slot for the comparison.
3107 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3108 auto &DL = DAG.getDataLayout();
3109 EVT PtrTy = TLI.getFrameIndexTy(DL);
3110 EVT PtrMemTy = TLI.getPointerMemTy(DL, DL.getAllocaAddrSpace());
3111
3112 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3113 int FI = MFI.getStackProtectorIndex();
3114
3115 SDValue Guard;
3116 SDLoc dl = getCurSDLoc();
3117 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
3118 const Module &M = *ParentBB->getParent()->getFunction().getParent();
3119 Align Align = DL.getPrefTypeAlign(
3120 PointerType::get(M.getContext(), DL.getAllocaAddrSpace()));
3121
3122 // Generate code to load the content of the guard slot.
3123 SDValue GuardVal = DAG.getLoad(
3124 PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
3125 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
3127
3128 if (TLI.useStackGuardXorFP())
3129 GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
3130
3131 // If we're using function-based instrumentation, call the guard check
3132 // function
3134 // Get the guard check function from the target and verify it exists since
3135 // we're using function-based instrumentation
3136 const Function *GuardCheckFn = TLI.getSSPStackGuardCheck(M);
3137 assert(GuardCheckFn && "Guard check function is null");
3138
3139 // The target provides a guard check function to validate the guard value.
3140 // Generate a call to that function with the content of the guard slot as
3141 // argument.
3142 FunctionType *FnTy = GuardCheckFn->getFunctionType();
3143 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3144
3146 TargetLowering::ArgListEntry Entry(GuardVal, FnTy->getParamType(0));
3147 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3148 Entry.IsInReg = true;
3149 Args.push_back(Entry);
3150
3153 .setChain(DAG.getEntryNode())
3154 .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
3155 getValue(GuardCheckFn), std::move(Args));
3156
3157 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
3158 DAG.setRoot(Result.second);
3159 return;
3160 }
3161
3162 // If useLoadStackGuardNode returns true, generate LOAD_STACK_GUARD.
3163 // Otherwise, emit a volatile load to retrieve the stack guard value.
3164 SDValue Chain = DAG.getEntryNode();
3165 if (TLI.useLoadStackGuardNode(M)) {
3166 Guard = getLoadStackGuard(DAG, dl, Chain);
3167 } else {
3168 if (const Value *IRGuard = TLI.getSDagStackGuard(M)) {
3169 SDValue GuardPtr = getValue(IRGuard);
3170 Guard = DAG.getLoad(PtrMemTy, dl, Chain, GuardPtr,
3171 MachinePointerInfo(IRGuard, 0), Align,
3173 } else {
3174 LLVMContext &Ctx = *DAG.getContext();
3175 Ctx.diagnose(DiagnosticInfoGeneric("unable to lower stackguard"));
3176 Guard = DAG.getPOISON(PtrMemTy);
3177 }
3178 }
3179
3180 // Perform the comparison via a getsetcc.
3181 SDValue Cmp = DAG.getSetCC(
3182 dl, TLI.getSetCCResultType(DL, *DAG.getContext(), Guard.getValueType()),
3183 Guard, GuardVal, ISD::SETNE);
3184
3185 // If the guard/stackslot do not equal, branch to failure MBB.
3186 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
3187 MVT::Other, GuardVal.getOperand(0),
3188 Cmp, DAG.getBasicBlock(SPD.getFailureMBB()));
3189 // Otherwise branch to success MBB.
3190 SDValue Br = DAG.getNode(ISD::BR, dl,
3191 MVT::Other, BrCond,
3192 DAG.getBasicBlock(SPD.getSuccessMBB()));
3193
3194 DAG.setRoot(Br);
3195}
3196
3197/// Codegen the failure basic block for a stack protector check.
3198///
3199/// A failure stack protector machine basic block consists simply of a call to
3200/// __stack_chk_fail().
3201///
3202/// For a high level explanation of how this fits into the stack protector
3203/// generation see the comment on the declaration of class
3204/// StackProtectorDescriptor.
3207
3208 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3209 MachineBasicBlock *ParentBB = SPD.getParentMBB();
3210 const Module &M = *ParentBB->getParent()->getFunction().getParent();
3211 SDValue Chain;
3212
3213 // For -Oz builds with a guard check function, we use function-based
3214 // instrumentation. Otherwise, if we have a guard check function, we call it
3215 // in the failure block.
3216 auto *GuardCheckFn = TLI.getSSPStackGuardCheck(M);
3217 if (GuardCheckFn && !SPD.shouldEmitFunctionBasedCheckStackProtector()) {
3218 // First create the loads to the guard/stack slot for the comparison.
3219 auto &DL = DAG.getDataLayout();
3220 EVT PtrTy = TLI.getFrameIndexTy(DL);
3221 EVT PtrMemTy = TLI.getPointerMemTy(DL, DL.getAllocaAddrSpace());
3222
3223 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3224 int FI = MFI.getStackProtectorIndex();
3225
3226 SDLoc dl = getCurSDLoc();
3227 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
3228 Align Align = DL.getPrefTypeAlign(
3229 PointerType::get(M.getContext(), DL.getAllocaAddrSpace()));
3230
3231 // Generate code to load the content of the guard slot.
3232 SDValue GuardVal = DAG.getLoad(
3233 PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
3234 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
3236
3237 if (TLI.useStackGuardXorFP())
3238 GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
3239
3240 // The target provides a guard check function to validate the guard value.
3241 // Generate a call to that function with the content of the guard slot as
3242 // argument.
3243 FunctionType *FnTy = GuardCheckFn->getFunctionType();
3244 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3245
3247 TargetLowering::ArgListEntry Entry(GuardVal, FnTy->getParamType(0));
3248 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3249 Entry.IsInReg = true;
3250 Args.push_back(Entry);
3251
3254 .setChain(DAG.getEntryNode())
3255 .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
3256 getValue(GuardCheckFn), std::move(Args));
3257
3258 Chain = TLI.LowerCallTo(CLI).second;
3259 } else {
3261 CallOptions.setDiscardResult(true);
3262 Chain = TLI.makeLibCall(DAG, RTLIB::STACKPROTECTOR_CHECK_FAIL, MVT::isVoid,
3263 {}, CallOptions, getCurSDLoc())
3264 .second;
3265 }
3266
3267 // Emit a trap instruction if we are required to do so.
3268 const TargetOptions &TargetOpts = DAG.getTarget().Options;
3269 if (TargetOpts.TrapUnreachable && !TargetOpts.NoTrapAfterNoreturn)
3270 Chain = DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, Chain);
3271
3272 DAG.setRoot(Chain);
3273}
3274
3275/// visitBitTestHeader - This function emits necessary code to produce value
3276/// suitable for "bit tests"
3278 MachineBasicBlock *SwitchBB) {
3279 SDLoc dl = getCurSDLoc();
3280
3281 // Subtract the minimum value.
3282 SDValue SwitchOp = getValue(B.SValue);
3283 EVT VT = SwitchOp.getValueType();
3284 SDValue RangeSub =
3285 DAG.getNode(ISD::SUB, dl, VT, SwitchOp, DAG.getConstant(B.First, dl, VT));
3286
3287 // Determine the type of the test operands.
3288 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3289 bool UsePtrType = false;
3290 if (!TLI.isTypeLegal(VT)) {
3291 UsePtrType = true;
3292 } else {
3293 for (const BitTestCase &Case : B.Cases)
3294 if (!isUIntN(VT.getSizeInBits(), Case.Mask)) {
3295 // Switch table case range are encoded into series of masks.
3296 // Just use pointer type, it's guaranteed to fit.
3297 UsePtrType = true;
3298 break;
3299 }
3300 }
3301 SDValue Sub = RangeSub;
3302 if (UsePtrType) {
3303 VT = TLI.getPointerTy(DAG.getDataLayout());
3304 Sub = DAG.getZExtOrTrunc(Sub, dl, VT);
3305 }
3306
3307 B.RegVT = VT.getSimpleVT();
3308 B.Reg = FuncInfo.CreateReg(B.RegVT);
3309 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), dl, B.Reg, Sub);
3310
3311 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
3312
3313 if (!B.FallthroughUnreachable)
3314 addSuccessorWithProb(SwitchBB, B.Default, B.DefaultProb);
3315 addSuccessorWithProb(SwitchBB, MBB, B.Prob);
3316 SwitchBB->normalizeSuccProbs();
3317
3318 SDValue Root = CopyTo;
3319 if (!B.FallthroughUnreachable) {
3320 // Conditional branch to the default block.
3321 SDValue RangeCmp = DAG.getSetCC(dl,
3322 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
3323 RangeSub.getValueType()),
3324 RangeSub, DAG.getConstant(B.Range, dl, RangeSub.getValueType()),
3325 ISD::SETUGT);
3326
3327 Root = DAG.getNode(ISD::BRCOND, dl, MVT::Other, Root, RangeCmp,
3328 DAG.getBasicBlock(B.Default));
3329 }
3330
3331 // Avoid emitting unnecessary branches to the next block.
3332 if (MBB != NextBlock(SwitchBB))
3333 Root = DAG.getNode(ISD::BR, dl, MVT::Other, Root, DAG.getBasicBlock(MBB));
3334
3335 DAG.setRoot(Root);
3336}
3337
3338/// visitBitTestCase - this function produces one "bit test"
3340 MachineBasicBlock *NextMBB,
3341 BranchProbability BranchProbToNext,
3342 Register Reg, BitTestCase &B,
3343 MachineBasicBlock *SwitchBB) {
3344 SDLoc dl = getCurSDLoc();
3345 MVT VT = BB.RegVT;
3346 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), dl, Reg, VT);
3347 SDValue Cmp;
3348 unsigned PopCount = llvm::popcount(B.Mask);
3349 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3350 if (PopCount == 1) {
3351 // Testing for a single bit; just compare the shift count with what it
3352 // would need to be to shift a 1 bit in that position.
3353 Cmp = DAG.getSetCC(
3354 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3355 ShiftOp, DAG.getConstant(llvm::countr_zero(B.Mask), dl, VT),
3356 ISD::SETEQ);
3357 } else if (PopCount == BB.Range) {
3358 // There is only one zero bit in the range, test for it directly.
3359 Cmp = DAG.getSetCC(
3360 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3361 ShiftOp, DAG.getConstant(llvm::countr_one(B.Mask), dl, VT), ISD::SETNE);
3362 } else {
3363 // Make desired shift
3364 SDValue SwitchVal = DAG.getNode(ISD::SHL, dl, VT,
3365 DAG.getConstant(1, dl, VT), ShiftOp);
3366
3367 // Emit bit tests and jumps
3368 SDValue AndOp = DAG.getNode(ISD::AND, dl,
3369 VT, SwitchVal, DAG.getConstant(B.Mask, dl, VT));
3370 Cmp = DAG.getSetCC(
3371 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3372 AndOp, DAG.getConstant(0, dl, VT), ISD::SETNE);
3373 }
3374
3375 // The branch probability from SwitchBB to B.TargetBB is B.ExtraProb.
3376 addSuccessorWithProb(SwitchBB, B.TargetBB, B.ExtraProb);
3377 // The branch probability from SwitchBB to NextMBB is BranchProbToNext.
3378 addSuccessorWithProb(SwitchBB, NextMBB, BranchProbToNext);
3379 // It is not guaranteed that the sum of B.ExtraProb and BranchProbToNext is
3380 // one as they are relative probabilities (and thus work more like weights),
3381 // and hence we need to normalize them to let the sum of them become one.
3382 SwitchBB->normalizeSuccProbs();
3383
3384 SDValue BrAnd = DAG.getNode(ISD::BRCOND, dl,
3385 MVT::Other, getControlRoot(),
3386 Cmp, DAG.getBasicBlock(B.TargetBB));
3387
3388 // Avoid emitting unnecessary branches to the next block.
3389 if (NextMBB != NextBlock(SwitchBB))
3390 BrAnd = DAG.getNode(ISD::BR, dl, MVT::Other, BrAnd,
3391 DAG.getBasicBlock(NextMBB));
3392
3393 DAG.setRoot(BrAnd);
3394}
3395
3396void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
3397 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
3398
3399 // Retrieve successors. Look through artificial IR level blocks like
3400 // catchswitch for successors.
3401 MachineBasicBlock *Return = FuncInfo.getMBB(I.getSuccessor(0));
3402 const BasicBlock *EHPadBB = I.getSuccessor(1);
3403 MachineBasicBlock *EHPadMBB = FuncInfo.getMBB(EHPadBB);
3404
3405 // Deopt and ptrauth bundles are lowered in helper functions, and we don't
3406 // have to do anything here to lower funclet bundles.
3407 failForInvalidBundles(I, "invokes",
3413
3414 const Value *Callee(I.getCalledOperand());
3415 const Function *Fn = dyn_cast<Function>(Callee);
3416 if (isa<InlineAsm>(Callee))
3417 visitInlineAsm(I, EHPadBB);
3418 else if (Fn && Fn->isIntrinsic()) {
3419 switch (Fn->getIntrinsicID()) {
3420 default:
3421 llvm_unreachable("Cannot invoke this intrinsic");
3422 case Intrinsic::donothing:
3423 // Ignore invokes to @llvm.donothing: jump directly to the next BB.
3424 case Intrinsic::seh_try_begin:
3425 case Intrinsic::seh_scope_begin:
3426 case Intrinsic::seh_try_end:
3427 case Intrinsic::seh_scope_end:
3428 if (EHPadMBB)
3429 // a block referenced by EH table
3430 // so dtor-funclet not removed by opts
3431 EHPadMBB->setMachineBlockAddressTaken();
3432 break;
3433 case Intrinsic::experimental_patchpoint_void:
3434 case Intrinsic::experimental_patchpoint:
3435 visitPatchpoint(I, EHPadBB);
3436 break;
3437 case Intrinsic::experimental_gc_statepoint:
3439 break;
3440 // wasm_throw, wasm_rethrow: This is usually done in visitTargetIntrinsic,
3441 // but these intrinsics are special because they can be invoked, so we
3442 // manually lower it to a DAG node here.
3443 case Intrinsic::wasm_throw: {
3445 std::array<SDValue, 4> Ops = {
3446 getControlRoot(), // inchain for the terminator node
3447 DAG.getTargetConstant(Intrinsic::wasm_throw, getCurSDLoc(),
3449 getValue(I.getArgOperand(0)), // tag
3450 getValue(I.getArgOperand(1)) // thrown value
3451 };
3452 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3453 DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
3454 break;
3455 }
3456 case Intrinsic::wasm_rethrow: {
3457 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3458 std::array<SDValue, 2> Ops = {
3459 getControlRoot(), // inchain for the terminator node
3460 DAG.getTargetConstant(Intrinsic::wasm_rethrow, getCurSDLoc(),
3461 TLI.getPointerTy(DAG.getDataLayout()))};
3462 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3463 DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
3464 break;
3465 }
3466 }
3467 } else if (I.hasDeoptState()) {
3468 // Currently we do not lower any intrinsic calls with deopt operand bundles.
3469 // Eventually we will support lowering the @llvm.experimental.deoptimize
3470 // intrinsic, and right now there are no plans to support other intrinsics
3471 // with deopt state.
3472 LowerCallSiteWithDeoptBundle(&I, getValue(Callee), EHPadBB);
3473 } else if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
3475 } else {
3476 LowerCallTo(I, getValue(Callee), false, false, EHPadBB);
3477 }
3478
3479 // If the value of the invoke is used outside of its defining block, make it
3480 // available as a virtual register.
3481 // We already took care of the exported value for the statepoint instruction
3482 // during call to the LowerStatepoint.
3483 if (!isa<GCStatepointInst>(I)) {
3485 }
3486
3488 BranchProbabilityInfo *BPI = FuncInfo.BPI;
3489 BranchProbability EHPadBBProb =
3490 BPI ? BPI->getEdgeProbability(InvokeMBB->getBasicBlock(), EHPadBB)
3492 findUnwindDestinations(FuncInfo, EHPadBB, EHPadBBProb, UnwindDests);
3493
3494 // Update successor info.
3495 addSuccessorWithProb(InvokeMBB, Return);
3496 for (auto &UnwindDest : UnwindDests) {
3497 UnwindDest.first->setIsEHPad();
3498 addSuccessorWithProb(InvokeMBB, UnwindDest.first, UnwindDest.second);
3499 }
3500 InvokeMBB->normalizeSuccProbs();
3501
3502 // Drop into normal successor.
3503 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, getControlRoot(),
3504 DAG.getBasicBlock(Return)));
3505}
3506
3507void SelectionDAGBuilder::visitCallBr(const CallBrInst &I) {
3508 MachineBasicBlock *CallBrMBB = FuncInfo.MBB;
3509
3510 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
3511 // have to do anything here to lower funclet bundles.
3512 failForInvalidBundles(I, "callbrs",
3514
3515 assert(I.isInlineAsm() && "Only know how to handle inlineasm callbr");
3516 visitInlineAsm(I);
3518
3519 // Retrieve successors.
3520 SmallPtrSet<BasicBlock *, 8> Dests;
3521 Dests.insert(I.getDefaultDest());
3522 MachineBasicBlock *Return = FuncInfo.getMBB(I.getDefaultDest());
3523
3524 // Update successor info.
3525 addSuccessorWithProb(CallBrMBB, Return, BranchProbability::getOne());
3526 for (BasicBlock *Dest : I.getIndirectDests()) {
3527 MachineBasicBlock *Target = FuncInfo.getMBB(Dest);
3528 Target->setIsInlineAsmBrIndirectTarget();
3529 // If we introduce a type of asm goto statement that is permitted to use an
3530 // indirect call instruction to jump to its labels, then we should add a
3531 // call to Target->setMachineBlockAddressTaken() here, to mark the target
3532 // block as requiring a BTI.
3533
3534 Target->setLabelMustBeEmitted();
3535 // Don't add duplicate machine successors.
3536 if (Dests.insert(Dest).second)
3537 addSuccessorWithProb(CallBrMBB, Target, BranchProbability::getZero());
3538 }
3539 CallBrMBB->normalizeSuccProbs();
3540
3541 // Drop into default successor.
3542 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
3543 MVT::Other, getControlRoot(),
3544 DAG.getBasicBlock(Return)));
3545}
3546
3547void SelectionDAGBuilder::visitResume(const ResumeInst &RI) {
3548 llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!");
3549}
3550
3551void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) {
3552 assert(FuncInfo.MBB->isEHPad() &&
3553 "Call to landingpad not in landing pad!");
3554
3555 // If there aren't registers to copy the values into (e.g., during SjLj
3556 // exceptions), then don't bother to create these DAG nodes.
3557 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3558 const Constant *PersonalityFn = FuncInfo.Fn->getPersonalityFn();
3559 if (TLI.getExceptionPointerRegister(PersonalityFn) == 0 &&
3560 TLI.getExceptionSelectorRegister(PersonalityFn) == 0)
3561 return;
3562
3563 // If landingpad's return type is token type, we don't create DAG nodes
3564 // for its exception pointer and selector value. The extraction of exception
3565 // pointer or selector value from token type landingpads is not currently
3566 // supported.
3567 if (LP.getType()->isTokenTy())
3568 return;
3569
3570 SmallVector<EVT, 2> ValueVTs;
3571 SDLoc dl = getCurSDLoc();
3572 ComputeValueVTs(TLI, DAG.getDataLayout(), LP.getType(), ValueVTs);
3573 assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported");
3574
3575 // Get the two live-in registers as SDValues. The physregs have already been
3576 // copied into virtual registers.
3577 SDValue Ops[2];
3578 if (FuncInfo.ExceptionPointerVirtReg) {
3579 Ops[0] = DAG.getZExtOrTrunc(
3580 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3581 FuncInfo.ExceptionPointerVirtReg,
3582 TLI.getPointerTy(DAG.getDataLayout())),
3583 dl, ValueVTs[0]);
3584 } else {
3585 Ops[0] = DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout()));
3586 }
3587 Ops[1] = DAG.getZExtOrTrunc(
3588 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3589 FuncInfo.ExceptionSelectorVirtReg,
3590 TLI.getPointerTy(DAG.getDataLayout())),
3591 dl, ValueVTs[1]);
3592
3593 // Merge into one.
3594 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
3595 DAG.getVTList(ValueVTs), Ops);
3596 setValue(&LP, Res);
3597}
3598
3601 // Update JTCases.
3602 for (JumpTableBlock &JTB : SL->JTCases)
3603 if (JTB.first.HeaderBB == First)
3604 JTB.first.HeaderBB = Last;
3605
3606 // Update BitTestCases.
3607 for (BitTestBlock &BTB : SL->BitTestCases)
3608 if (BTB.Parent == First)
3609 BTB.Parent = Last;
3610}
3611
3612void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
3613 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
3614
3615 // Update machine-CFG edges with unique successors.
3617 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) {
3618 BasicBlock *BB = I.getSuccessor(i);
3619 bool Inserted = Done.insert(BB).second;
3620 if (!Inserted)
3621 continue;
3622
3623 MachineBasicBlock *Succ = FuncInfo.getMBB(BB);
3624 addSuccessorWithProb(IndirectBrMBB, Succ);
3625 }
3626 IndirectBrMBB->normalizeSuccProbs();
3627
3628 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurSDLoc(),
3629 MVT::Other, getControlRoot(),
3630 getValue(I.getAddress())));
3631}
3632
3633void SelectionDAGBuilder::visitUnreachable(const UnreachableInst &I) {
3634 if (!I.shouldLowerToTrap(DAG.getTarget().Options.TrapUnreachable,
3635 DAG.getTarget().Options.NoTrapAfterNoreturn))
3636 return;
3637
3638 DAG.setRoot(DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, DAG.getRoot()));
3639}
3640
3641void SelectionDAGBuilder::visitUnary(const User &I, unsigned Opcode) {
3642 SDNodeFlags Flags;
3643 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3644 Flags.copyFMF(*FPOp);
3645
3646 SDValue Op = getValue(I.getOperand(0));
3647 SDValue UnNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op.getValueType(),
3648 Op, Flags);
3649 setValue(&I, UnNodeValue);
3650}
3651
3652void SelectionDAGBuilder::visitBinary(const User &I, unsigned Opcode) {
3653 SDNodeFlags Flags;
3654 if (auto *OFBinOp = dyn_cast<OverflowingBinaryOperator>(&I)) {
3655 Flags.setNoSignedWrap(OFBinOp->hasNoSignedWrap());
3656 Flags.setNoUnsignedWrap(OFBinOp->hasNoUnsignedWrap());
3657 }
3658 if (auto *ExactOp = dyn_cast<PossiblyExactOperator>(&I))
3659 Flags.setExact(ExactOp->isExact());
3660 if (auto *DisjointOp = dyn_cast<PossiblyDisjointInst>(&I))
3661 Flags.setDisjoint(DisjointOp->isDisjoint());
3662 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3663 Flags.copyFMF(*FPOp);
3664
3665 SDValue Op1 = getValue(I.getOperand(0));
3666 SDValue Op2 = getValue(I.getOperand(1));
3667 SDValue BinNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(),
3668 Op1, Op2, Flags);
3669 setValue(&I, BinNodeValue);
3670}
3671
3672void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
3673 SDValue Op1 = getValue(I.getOperand(0));
3674 SDValue Op2 = getValue(I.getOperand(1));
3675
3676 EVT ShiftTy = DAG.getTargetLoweringInfo().getShiftAmountTy(
3677 Op1.getValueType(), DAG.getDataLayout());
3678
3679 // Coerce the shift amount to the right type if we can. This exposes the
3680 // truncate or zext to optimization early.
3681 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
3683 "Unexpected shift type");
3684 Op2 = DAG.getZExtOrTrunc(Op2, getCurSDLoc(), ShiftTy);
3685 }
3686
3687 bool nuw = false;
3688 bool nsw = false;
3689 bool exact = false;
3690
3691 if (Opcode == ISD::SRL || Opcode == ISD::SRA || Opcode == ISD::SHL) {
3692
3693 if (const OverflowingBinaryOperator *OFBinOp =
3695 nuw = OFBinOp->hasNoUnsignedWrap();
3696 nsw = OFBinOp->hasNoSignedWrap();
3697 }
3698 if (const PossiblyExactOperator *ExactOp =
3700 exact = ExactOp->isExact();
3701 }
3702 SDNodeFlags Flags;
3703 Flags.setExact(exact);
3704 Flags.setNoSignedWrap(nsw);
3705 Flags.setNoUnsignedWrap(nuw);
3706 SDValue Res = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(), Op1, Op2,
3707 Flags);
3708 setValue(&I, Res);
3709}
3710
3711void SelectionDAGBuilder::visitSDiv(const User &I) {
3712 SDValue Op1 = getValue(I.getOperand(0));
3713 SDValue Op2 = getValue(I.getOperand(1));
3714
3715 SDNodeFlags Flags;
3716 Flags.setExact(isa<PossiblyExactOperator>(&I) &&
3717 cast<PossiblyExactOperator>(&I)->isExact());
3718 setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(), Op1,
3719 Op2, Flags));
3720}
3721
3722void SelectionDAGBuilder::visitICmp(const ICmpInst &I) {
3723 ICmpInst::Predicate predicate = I.getPredicate();
3724 SDValue Op1 = getValue(I.getOperand(0));
3725 SDValue Op2 = getValue(I.getOperand(1));
3726 ISD::CondCode Opcode = getICmpCondCode(predicate);
3727
3728 auto &TLI = DAG.getTargetLoweringInfo();
3729 EVT MemVT =
3730 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
3731
3732 // If a pointer's DAG type is larger than its memory type then the DAG values
3733 // are zero-extended. This breaks signed comparisons so truncate back to the
3734 // underlying type before doing the compare.
3735 if (Op1.getValueType() != MemVT) {
3736 Op1 = DAG.getPtrExtOrTrunc(Op1, getCurSDLoc(), MemVT);
3737 Op2 = DAG.getPtrExtOrTrunc(Op2, getCurSDLoc(), MemVT);
3738 }
3739
3740 SDNodeFlags Flags;
3741 Flags.setSameSign(I.hasSameSign());
3742 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
3743
3744 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3745 I.getType());
3746 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode));
3747}
3748
3749void SelectionDAGBuilder::visitFCmp(const FCmpInst &I) {
3750 FCmpInst::Predicate predicate = I.getPredicate();
3751 SDValue Op1 = getValue(I.getOperand(0));
3752 SDValue Op2 = getValue(I.getOperand(1));
3753
3754 ISD::CondCode Condition = getFCmpCondCode(predicate);
3755 auto *FPMO = cast<FPMathOperator>(&I);
3756 if (FPMO->hasNoNaNs() || TM.Options.NoNaNsFPMath)
3757 Condition = getFCmpCodeWithoutNaN(Condition);
3758
3759 SDNodeFlags Flags;
3760 Flags.copyFMF(*FPMO);
3761 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
3762
3763 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3764 I.getType());
3765 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition));
3766}
3767
3768// Check if the condition of the select has one use or two users that are both
3769// selects with the same condition.
3770static bool hasOnlySelectUsers(const Value *Cond) {
3771 return llvm::all_of(Cond->users(), [](const Value *V) {
3772 return isa<SelectInst>(V);
3773 });
3774}
3775
3776void SelectionDAGBuilder::visitSelect(const User &I) {
3777 SmallVector<EVT, 4> ValueVTs;
3778 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
3779 ValueVTs);
3780 unsigned NumValues = ValueVTs.size();
3781 if (NumValues == 0) return;
3782
3783 SmallVector<SDValue, 4> Values(NumValues);
3784 SDValue Cond = getValue(I.getOperand(0));
3785 SDValue LHSVal = getValue(I.getOperand(1));
3786 SDValue RHSVal = getValue(I.getOperand(2));
3787 SmallVector<SDValue, 1> BaseOps(1, Cond);
3789 Cond.getValueType().isVector() ? ISD::VSELECT : ISD::SELECT;
3790
3791 bool IsUnaryAbs = false;
3792 bool Negate = false;
3793
3794 SDNodeFlags Flags;
3795 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3796 Flags.copyFMF(*FPOp);
3797
3798 Flags.setUnpredictable(
3799 cast<SelectInst>(I).getMetadata(LLVMContext::MD_unpredictable));
3800
3801 // Min/max matching is only viable if all output VTs are the same.
3802 if (all_equal(ValueVTs)) {
3803 EVT VT = ValueVTs[0];
3804 LLVMContext &Ctx = *DAG.getContext();
3805 auto &TLI = DAG.getTargetLoweringInfo();
3806
3807 // We care about the legality of the operation after it has been type
3808 // legalized.
3809 while (TLI.getTypeAction(Ctx, VT) != TargetLoweringBase::TypeLegal)
3810 VT = TLI.getTypeToTransformTo(Ctx, VT);
3811
3812 // If the vselect is legal, assume we want to leave this as a vector setcc +
3813 // vselect. Otherwise, if this is going to be scalarized, we want to see if
3814 // min/max is legal on the scalar type.
3815 bool UseScalarMinMax = VT.isVector() &&
3817
3818 // ValueTracking's select pattern matching does not account for -0.0,
3819 // so we can't lower to FMINIMUM/FMAXIMUM because those nodes specify that
3820 // -0.0 is less than +0.0.
3821 const Value *LHS, *RHS;
3822 auto SPR = matchSelectPattern(&I, LHS, RHS);
3824 switch (SPR.Flavor) {
3825 case SPF_UMAX: Opc = ISD::UMAX; break;
3826 case SPF_UMIN: Opc = ISD::UMIN; break;
3827 case SPF_SMAX: Opc = ISD::SMAX; break;
3828 case SPF_SMIN: Opc = ISD::SMIN; break;
3829 case SPF_FMINNUM:
3830 switch (SPR.NaNBehavior) {
3831 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3832 case SPNB_RETURNS_NAN: break;
3833 case SPNB_RETURNS_OTHER: Opc = ISD::FMINNUM; break;
3834 case SPNB_RETURNS_ANY:
3835 if (TLI.isOperationLegalOrCustom(ISD::FMINNUM, VT) ||
3836 (UseScalarMinMax &&
3837 TLI.isOperationLegalOrCustom(ISD::FMINNUM, VT.getScalarType())))
3838 Opc = ISD::FMINNUM;
3839 break;
3840 }
3841 break;
3842 case SPF_FMAXNUM:
3843 switch (SPR.NaNBehavior) {
3844 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3845 case SPNB_RETURNS_NAN: break;
3846 case SPNB_RETURNS_OTHER: Opc = ISD::FMAXNUM; break;
3847 case SPNB_RETURNS_ANY:
3848 if (TLI.isOperationLegalOrCustom(ISD::FMAXNUM, VT) ||
3849 (UseScalarMinMax &&
3850 TLI.isOperationLegalOrCustom(ISD::FMAXNUM, VT.getScalarType())))
3851 Opc = ISD::FMAXNUM;
3852 break;
3853 }
3854 break;
3855 case SPF_NABS:
3856 Negate = true;
3857 [[fallthrough]];
3858 case SPF_ABS:
3859 IsUnaryAbs = true;
3860 Opc = ISD::ABS;
3861 break;
3862 default: break;
3863 }
3864
3865 if (!IsUnaryAbs && Opc != ISD::DELETED_NODE &&
3866 (TLI.isOperationLegalOrCustom(Opc, VT) ||
3867 (UseScalarMinMax &&
3869 // If the underlying comparison instruction is used by any other
3870 // instruction, the consumed instructions won't be destroyed, so it is
3871 // not profitable to convert to a min/max.
3873 OpCode = Opc;
3874 LHSVal = getValue(LHS);
3875 RHSVal = getValue(RHS);
3876 BaseOps.clear();
3877 }
3878
3879 if (IsUnaryAbs) {
3880 OpCode = Opc;
3881 LHSVal = getValue(LHS);
3882 BaseOps.clear();
3883 }
3884 }
3885
3886 if (IsUnaryAbs) {
3887 for (unsigned i = 0; i != NumValues; ++i) {
3888 SDLoc dl = getCurSDLoc();
3889 EVT VT = LHSVal.getNode()->getValueType(LHSVal.getResNo() + i);
3890 Values[i] =
3891 DAG.getNode(OpCode, dl, VT, LHSVal.getValue(LHSVal.getResNo() + i));
3892 if (Negate)
3893 Values[i] = DAG.getNegative(Values[i], dl, VT);
3894 }
3895 } else {
3896 for (unsigned i = 0; i != NumValues; ++i) {
3897 SmallVector<SDValue, 3> Ops(BaseOps.begin(), BaseOps.end());
3898 Ops.push_back(SDValue(LHSVal.getNode(), LHSVal.getResNo() + i));
3899 Ops.push_back(SDValue(RHSVal.getNode(), RHSVal.getResNo() + i));
3900 Values[i] = DAG.getNode(
3901 OpCode, getCurSDLoc(),
3902 LHSVal.getNode()->getValueType(LHSVal.getResNo() + i), Ops, Flags);
3903 }
3904 }
3905
3907 DAG.getVTList(ValueVTs), Values));
3908}
3909
3910void SelectionDAGBuilder::visitTrunc(const User &I) {
3911 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
3912 SDValue N = getValue(I.getOperand(0));
3913 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3914 I.getType());
3915 SDNodeFlags Flags;
3916 if (auto *Trunc = dyn_cast<TruncInst>(&I)) {
3917 Flags.setNoSignedWrap(Trunc->hasNoSignedWrap());
3918 Flags.setNoUnsignedWrap(Trunc->hasNoUnsignedWrap());
3919 }
3920
3921 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N, Flags));
3922}
3923
3924void SelectionDAGBuilder::visitZExt(const User &I) {
3925 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
3926 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
3927 SDValue N = getValue(I.getOperand(0));
3928 auto &TLI = DAG.getTargetLoweringInfo();
3929 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3930
3931 SDNodeFlags Flags;
3932 if (auto *PNI = dyn_cast<PossiblyNonNegInst>(&I))
3933 Flags.setNonNeg(PNI->hasNonNeg());
3934
3935 // Eagerly use nonneg information to canonicalize towards sign_extend if
3936 // that is the target's preference.
3937 // TODO: Let the target do this later.
3938 if (Flags.hasNonNeg() &&
3939 TLI.isSExtCheaperThanZExt(N.getValueType(), DestVT)) {
3940 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
3941 return;
3942 }
3943
3944 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N, Flags));
3945}
3946
3947void SelectionDAGBuilder::visitSExt(const User &I) {
3948 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
3949 // SExt also can't be a cast to bool for same reason. So, nothing much to do
3950 SDValue N = getValue(I.getOperand(0));
3951 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3952 I.getType());
3953 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
3954}
3955
3956void SelectionDAGBuilder::visitFPTrunc(const User &I) {
3957 // FPTrunc is never a no-op cast, no need to check
3958 SDValue N = getValue(I.getOperand(0));
3959 SDLoc dl = getCurSDLoc();
3960 SDNodeFlags Flags;
3961 if (auto *TruncInst = dyn_cast<FPMathOperator>(&I))
3962 Flags.copyFMF(*TruncInst);
3963 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3964 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3965 setValue(&I, DAG.getNode(ISD::FP_ROUND, dl, DestVT, N,
3966 DAG.getTargetConstant(
3967 0, dl, TLI.getPointerTy(DAG.getDataLayout())),
3968 Flags));
3969}
3970
3971void SelectionDAGBuilder::visitFPExt(const User &I) {
3972 // FPExt is never a no-op cast, no need to check
3973 SDValue N = getValue(I.getOperand(0));
3974 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3975 I.getType());
3976 SDNodeFlags Flags;
3977 if (auto *TruncInst = dyn_cast<FPMathOperator>(&I))
3978 Flags.copyFMF(*TruncInst);
3979 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N, Flags));
3980}
3981
3982void SelectionDAGBuilder::visitFPToUI(const User &I) {
3983 // FPToUI is never a no-op cast, no need to check
3984 SDValue N = getValue(I.getOperand(0));
3985 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3986 I.getType());
3987 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N));
3988}
3989
3990void SelectionDAGBuilder::visitFPToSI(const User &I) {
3991 // FPToSI is never a no-op cast, no need to check
3992 SDValue N = getValue(I.getOperand(0));
3993 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3994 I.getType());
3995 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N));
3996}
3997
3998void SelectionDAGBuilder::visitUIToFP(const User &I) {
3999 // UIToFP is never a no-op cast, no need to check
4000 SDValue N = getValue(I.getOperand(0));
4001 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4002 I.getType());
4003 SDNodeFlags Flags;
4004 if (auto *PNI = dyn_cast<PossiblyNonNegInst>(&I))
4005 Flags.setNonNeg(PNI->hasNonNeg());
4006
4007 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N, Flags));
4008}
4009
4010void SelectionDAGBuilder::visitSIToFP(const User &I) {
4011 // SIToFP is never a no-op cast, no need to check
4012 SDValue N = getValue(I.getOperand(0));
4013 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4014 I.getType());
4015 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N));
4016}
4017
4018void SelectionDAGBuilder::visitPtrToAddr(const User &I) {
4019 SDValue N = getValue(I.getOperand(0));
4020 // By definition the type of the ptrtoaddr must be equal to the address type.
4021 const auto &TLI = DAG.getTargetLoweringInfo();
4022 EVT AddrVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4023 // The address width must be smaller or equal to the pointer representation
4024 // width, so we lower ptrtoaddr as a truncate (possibly folded to a no-op).
4025 N = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), AddrVT, N);
4026 setValue(&I, N);
4027}
4028
4029void SelectionDAGBuilder::visitPtrToInt(const User &I) {
4030 // What to do depends on the size of the integer and the size of the pointer.
4031 // We can either truncate, zero extend, or no-op, accordingly.
4032 SDValue N = getValue(I.getOperand(0));
4033 auto &TLI = DAG.getTargetLoweringInfo();
4034 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4035 I.getType());
4036 EVT PtrMemVT =
4037 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
4038 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
4039 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT);
4040 setValue(&I, N);
4041}
4042
4043void SelectionDAGBuilder::visitIntToPtr(const User &I) {
4044 // What to do depends on the size of the integer and the size of the pointer.
4045 // We can either truncate, zero extend, or no-op, accordingly.
4046 SDValue N = getValue(I.getOperand(0));
4047 auto &TLI = DAG.getTargetLoweringInfo();
4048 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4049 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
4050 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
4051 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), DestVT);
4052 setValue(&I, N);
4053}
4054
4055void SelectionDAGBuilder::visitBitCast(const User &I) {
4056 SDValue N = getValue(I.getOperand(0));
4057 SDLoc dl = getCurSDLoc();
4058 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4059 I.getType());
4060
4061 // BitCast assures us that source and destination are the same size so this is
4062 // either a BITCAST or a no-op.
4063 if (DestVT != N.getValueType())
4064 setValue(&I, DAG.getNode(ISD::BITCAST, dl,
4065 DestVT, N)); // convert types.
4066 // Check if the original LLVM IR Operand was a ConstantInt, because getValue()
4067 // might fold any kind of constant expression to an integer constant and that
4068 // is not what we are looking for. Only recognize a bitcast of a genuine
4069 // constant integer as an opaque constant.
4070 else if(ConstantInt *C = dyn_cast<ConstantInt>(I.getOperand(0)))
4071 setValue(&I, DAG.getConstant(C->getValue(), dl, DestVT, /*isTarget=*/false,
4072 /*isOpaque*/true));
4073 else
4074 setValue(&I, N); // noop cast.
4075}
4076
4077void SelectionDAGBuilder::visitAddrSpaceCast(const User &I) {
4078 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4079 const Value *SV = I.getOperand(0);
4080 SDValue N = getValue(SV);
4081 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4082
4083 unsigned SrcAS = SV->getType()->getPointerAddressSpace();
4084 unsigned DestAS = I.getType()->getPointerAddressSpace();
4085
4086 if (!TM.isNoopAddrSpaceCast(SrcAS, DestAS))
4087 N = DAG.getAddrSpaceCast(getCurSDLoc(), DestVT, N, SrcAS, DestAS);
4088
4089 setValue(&I, N);
4090}
4091
4092void SelectionDAGBuilder::visitInsertElement(const User &I) {
4093 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4094 SDValue InVec = getValue(I.getOperand(0));
4095 SDValue InVal = getValue(I.getOperand(1));
4096 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(2)), getCurSDLoc(),
4097 TLI.getVectorIdxTy(DAG.getDataLayout()));
4099 TLI.getValueType(DAG.getDataLayout(), I.getType()),
4100 InVec, InVal, InIdx));
4101}
4102
4103void SelectionDAGBuilder::visitExtractElement(const User &I) {
4104 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4105 SDValue InVec = getValue(I.getOperand(0));
4106 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(1)), getCurSDLoc(),
4107 TLI.getVectorIdxTy(DAG.getDataLayout()));
4109 TLI.getValueType(DAG.getDataLayout(), I.getType()),
4110 InVec, InIdx));
4111}
4112
4113void SelectionDAGBuilder::visitShuffleVector(const User &I) {
4114 SDValue Src1 = getValue(I.getOperand(0));
4115 SDValue Src2 = getValue(I.getOperand(1));
4116 ArrayRef<int> Mask;
4117 if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))
4118 Mask = SVI->getShuffleMask();
4119 else
4120 Mask = cast<ConstantExpr>(I).getShuffleMask();
4121 SDLoc DL = getCurSDLoc();
4122 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4123 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4124 EVT SrcVT = Src1.getValueType();
4125
4126 if (all_of(Mask, [](int Elem) { return Elem == 0; }) &&
4127 VT.isScalableVector()) {
4128 // Canonical splat form of first element of first input vector.
4129 SDValue FirstElt =
4130 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, SrcVT.getScalarType(), Src1,
4131 DAG.getVectorIdxConstant(0, DL));
4132 setValue(&I, DAG.getNode(ISD::SPLAT_VECTOR, DL, VT, FirstElt));
4133 return;
4134 }
4135
4136 // For now, we only handle splats for scalable vectors.
4137 // The DAGCombiner will perform a BUILD_VECTOR -> SPLAT_VECTOR transformation
4138 // for targets that support a SPLAT_VECTOR for non-scalable vector types.
4139 assert(!VT.isScalableVector() && "Unsupported scalable vector shuffle");
4140
4141 unsigned SrcNumElts = SrcVT.getVectorNumElements();
4142 unsigned MaskNumElts = Mask.size();
4143
4144 if (SrcNumElts == MaskNumElts) {
4145 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, Mask));
4146 return;
4147 }
4148
4149 // Normalize the shuffle vector since mask and vector length don't match.
4150 if (SrcNumElts < MaskNumElts) {
4151 // Mask is longer than the source vectors. We can use concatenate vector to
4152 // make the mask and vectors lengths match.
4153
4154 if (MaskNumElts % SrcNumElts == 0) {
4155 // Mask length is a multiple of the source vector length.
4156 // Check if the shuffle is some kind of concatenation of the input
4157 // vectors.
4158 unsigned NumConcat = MaskNumElts / SrcNumElts;
4159 bool IsConcat = true;
4160 SmallVector<int, 8> ConcatSrcs(NumConcat, -1);
4161 for (unsigned i = 0; i != MaskNumElts; ++i) {
4162 int Idx = Mask[i];
4163 if (Idx < 0)
4164 continue;
4165 // Ensure the indices in each SrcVT sized piece are sequential and that
4166 // the same source is used for the whole piece.
4167 if ((Idx % SrcNumElts != (i % SrcNumElts)) ||
4168 (ConcatSrcs[i / SrcNumElts] >= 0 &&
4169 ConcatSrcs[i / SrcNumElts] != (int)(Idx / SrcNumElts))) {
4170 IsConcat = false;
4171 break;
4172 }
4173 // Remember which source this index came from.
4174 ConcatSrcs[i / SrcNumElts] = Idx / SrcNumElts;
4175 }
4176
4177 // The shuffle is concatenating multiple vectors together. Just emit
4178 // a CONCAT_VECTORS operation.
4179 if (IsConcat) {
4180 SmallVector<SDValue, 8> ConcatOps;
4181 for (auto Src : ConcatSrcs) {
4182 if (Src < 0)
4183 ConcatOps.push_back(DAG.getUNDEF(SrcVT));
4184 else if (Src == 0)
4185 ConcatOps.push_back(Src1);
4186 else
4187 ConcatOps.push_back(Src2);
4188 }
4189 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, ConcatOps));
4190 return;
4191 }
4192 }
4193
4194 unsigned PaddedMaskNumElts = alignTo(MaskNumElts, SrcNumElts);
4195 unsigned NumConcat = PaddedMaskNumElts / SrcNumElts;
4196 EVT PaddedVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
4197 PaddedMaskNumElts);
4198
4199 // Pad both vectors with undefs to make them the same length as the mask.
4200 SDValue UndefVal = DAG.getUNDEF(SrcVT);
4201
4202 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
4203 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
4204 MOps1[0] = Src1;
4205 MOps2[0] = Src2;
4206
4207 Src1 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps1);
4208 Src2 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps2);
4209
4210 // Readjust mask for new input vector length.
4211 SmallVector<int, 8> MappedOps(PaddedMaskNumElts, -1);
4212 for (unsigned i = 0; i != MaskNumElts; ++i) {
4213 int Idx = Mask[i];
4214 if (Idx >= (int)SrcNumElts)
4215 Idx -= SrcNumElts - PaddedMaskNumElts;
4216 MappedOps[i] = Idx;
4217 }
4218
4219 SDValue Result = DAG.getVectorShuffle(PaddedVT, DL, Src1, Src2, MappedOps);
4220
4221 // If the concatenated vector was padded, extract a subvector with the
4222 // correct number of elements.
4223 if (MaskNumElts != PaddedMaskNumElts)
4224 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Result,
4225 DAG.getVectorIdxConstant(0, DL));
4226
4227 setValue(&I, Result);
4228 return;
4229 }
4230
4231 assert(SrcNumElts > MaskNumElts);
4232
4233 // Analyze the access pattern of the vector to see if we can extract
4234 // two subvectors and do the shuffle.
4235 int StartIdx[2] = {-1, -1}; // StartIdx to extract from
4236 bool CanExtract = true;
4237 for (int Idx : Mask) {
4238 unsigned Input = 0;
4239 if (Idx < 0)
4240 continue;
4241
4242 if (Idx >= (int)SrcNumElts) {
4243 Input = 1;
4244 Idx -= SrcNumElts;
4245 }
4246
4247 // If all the indices come from the same MaskNumElts sized portion of
4248 // the sources we can use extract. Also make sure the extract wouldn't
4249 // extract past the end of the source.
4250 int NewStartIdx = alignDown(Idx, MaskNumElts);
4251 if (NewStartIdx + MaskNumElts > SrcNumElts ||
4252 (StartIdx[Input] >= 0 && StartIdx[Input] != NewStartIdx))
4253 CanExtract = false;
4254 // Make sure we always update StartIdx as we use it to track if all
4255 // elements are undef.
4256 StartIdx[Input] = NewStartIdx;
4257 }
4258
4259 if (StartIdx[0] < 0 && StartIdx[1] < 0) {
4260 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
4261 return;
4262 }
4263 if (CanExtract) {
4264 // Extract appropriate subvector and generate a vector shuffle
4265 for (unsigned Input = 0; Input < 2; ++Input) {
4266 SDValue &Src = Input == 0 ? Src1 : Src2;
4267 if (StartIdx[Input] < 0)
4268 Src = DAG.getUNDEF(VT);
4269 else {
4270 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Src,
4271 DAG.getVectorIdxConstant(StartIdx[Input], DL));
4272 }
4273 }
4274
4275 // Calculate new mask.
4276 SmallVector<int, 8> MappedOps(Mask);
4277 for (int &Idx : MappedOps) {
4278 if (Idx >= (int)SrcNumElts)
4279 Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
4280 else if (Idx >= 0)
4281 Idx -= StartIdx[0];
4282 }
4283
4284 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, MappedOps));
4285 return;
4286 }
4287
4288 // We can't use either concat vectors or extract subvectors so fall back to
4289 // replacing the shuffle with extract and build vector.
4290 // to insert and build vector.
4291 EVT EltVT = VT.getVectorElementType();
4293 for (int Idx : Mask) {
4294 SDValue Res;
4295
4296 if (Idx < 0) {
4297 Res = DAG.getUNDEF(EltVT);
4298 } else {
4299 SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2;
4300 if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts;
4301
4302 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Src,
4303 DAG.getVectorIdxConstant(Idx, DL));
4304 }
4305
4306 Ops.push_back(Res);
4307 }
4308
4309 setValue(&I, DAG.getBuildVector(VT, DL, Ops));
4310}
4311
4312void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
4313 ArrayRef<unsigned> Indices = I.getIndices();
4314 const Value *Op0 = I.getOperand(0);
4315 const Value *Op1 = I.getOperand(1);
4316 Type *AggTy = I.getType();
4317 Type *ValTy = Op1->getType();
4318 bool IntoUndef = isa<UndefValue>(Op0);
4319 bool FromUndef = isa<UndefValue>(Op1);
4320
4321 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4322
4323 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4324 SmallVector<EVT, 4> AggValueVTs;
4325 ComputeValueVTs(TLI, DAG.getDataLayout(), AggTy, AggValueVTs);
4326 SmallVector<EVT, 4> ValValueVTs;
4327 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4328
4329 unsigned NumAggValues = AggValueVTs.size();
4330 unsigned NumValValues = ValValueVTs.size();
4331 SmallVector<SDValue, 4> Values(NumAggValues);
4332
4333 // Ignore an insertvalue that produces an empty object
4334 if (!NumAggValues) {
4335 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4336 return;
4337 }
4338
4339 SDValue Agg = getValue(Op0);
4340 unsigned i = 0;
4341 // Copy the beginning value(s) from the original aggregate.
4342 for (; i != LinearIndex; ++i)
4343 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4344 SDValue(Agg.getNode(), Agg.getResNo() + i);
4345 // Copy values from the inserted value(s).
4346 if (NumValValues) {
4347 SDValue Val = getValue(Op1);
4348 for (; i != LinearIndex + NumValValues; ++i)
4349 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4350 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
4351 }
4352 // Copy remaining value(s) from the original aggregate.
4353 for (; i != NumAggValues; ++i)
4354 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4355 SDValue(Agg.getNode(), Agg.getResNo() + i);
4356
4358 DAG.getVTList(AggValueVTs), Values));
4359}
4360
4361void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
4362 ArrayRef<unsigned> Indices = I.getIndices();
4363 const Value *Op0 = I.getOperand(0);
4364 Type *AggTy = Op0->getType();
4365 Type *ValTy = I.getType();
4366 bool OutOfUndef = isa<UndefValue>(Op0);
4367
4368 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4369
4370 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4371 SmallVector<EVT, 4> ValValueVTs;
4372 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4373
4374 unsigned NumValValues = ValValueVTs.size();
4375
4376 // Ignore a extractvalue that produces an empty object
4377 if (!NumValValues) {
4378 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4379 return;
4380 }
4381
4382 SmallVector<SDValue, 4> Values(NumValValues);
4383
4384 SDValue Agg = getValue(Op0);
4385 // Copy out the selected value(s).
4386 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
4387 Values[i - LinearIndex] =
4388 OutOfUndef ?
4389 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
4390 SDValue(Agg.getNode(), Agg.getResNo() + i);
4391
4393 DAG.getVTList(ValValueVTs), Values));
4394}
4395
4396void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
4397 Value *Op0 = I.getOperand(0);
4398 // Note that the pointer operand may be a vector of pointers. Take the scalar
4399 // element which holds a pointer.
4400 unsigned AS = Op0->getType()->getScalarType()->getPointerAddressSpace();
4401 SDValue N = getValue(Op0);
4402 SDLoc dl = getCurSDLoc();
4403 auto &TLI = DAG.getTargetLoweringInfo();
4404 GEPNoWrapFlags NW = cast<GEPOperator>(I).getNoWrapFlags();
4405
4406 // For a vector GEP, keep the prefix scalar as long as possible, then
4407 // convert any scalars encountered after the first vector operand to vectors.
4408 bool IsVectorGEP = I.getType()->isVectorTy();
4409 ElementCount VectorElementCount =
4410 IsVectorGEP ? cast<VectorType>(I.getType())->getElementCount()
4412
4414 GTI != E; ++GTI) {
4415 const Value *Idx = GTI.getOperand();
4416 if (StructType *StTy = GTI.getStructTypeOrNull()) {
4417 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
4418 if (Field) {
4419 // N = N + Offset
4420 uint64_t Offset =
4421 DAG.getDataLayout().getStructLayout(StTy)->getElementOffset(Field);
4422
4423 // In an inbounds GEP with an offset that is nonnegative even when
4424 // interpreted as signed, assume there is no unsigned overflow.
4425 SDNodeFlags Flags;
4426 if (NW.hasNoUnsignedWrap() ||
4427 (int64_t(Offset) >= 0 && NW.hasNoUnsignedSignedWrap()))
4429 Flags.setInBounds(NW.isInBounds());
4430
4431 N = DAG.getMemBasePlusOffset(
4432 N, DAG.getConstant(Offset, dl, N.getValueType()), dl, Flags);
4433 }
4434 } else {
4435 // IdxSize is the width of the arithmetic according to IR semantics.
4436 // In SelectionDAG, we may prefer to do arithmetic in a wider bitwidth
4437 // (and fix up the result later).
4438 unsigned IdxSize = DAG.getDataLayout().getIndexSizeInBits(AS);
4439 MVT IdxTy = MVT::getIntegerVT(IdxSize);
4440 TypeSize ElementSize =
4441 GTI.getSequentialElementStride(DAG.getDataLayout());
4442 // We intentionally mask away the high bits here; ElementSize may not
4443 // fit in IdxTy.
4444 APInt ElementMul(IdxSize, ElementSize.getKnownMinValue(),
4445 /*isSigned=*/false, /*implicitTrunc=*/true);
4446 bool ElementScalable = ElementSize.isScalable();
4447
4448 // If this is a scalar constant or a splat vector of constants,
4449 // handle it quickly.
4450 const auto *C = dyn_cast<Constant>(Idx);
4451 if (C && isa<VectorType>(C->getType()))
4452 C = C->getSplatValue();
4453
4454 const auto *CI = dyn_cast_or_null<ConstantInt>(C);
4455 if (CI && CI->isZero())
4456 continue;
4457 if (CI && !ElementScalable) {
4458 APInt Offs = ElementMul * CI->getValue().sextOrTrunc(IdxSize);
4459 LLVMContext &Context = *DAG.getContext();
4460 SDValue OffsVal;
4461 if (N.getValueType().isVector())
4462 OffsVal = DAG.getConstant(
4463 Offs, dl, EVT::getVectorVT(Context, IdxTy, VectorElementCount));
4464 else
4465 OffsVal = DAG.getConstant(Offs, dl, IdxTy);
4466
4467 // In an inbounds GEP with an offset that is nonnegative even when
4468 // interpreted as signed, assume there is no unsigned overflow.
4469 SDNodeFlags Flags;
4470 if (NW.hasNoUnsignedWrap() ||
4471 (Offs.isNonNegative() && NW.hasNoUnsignedSignedWrap()))
4472 Flags.setNoUnsignedWrap(true);
4473 Flags.setInBounds(NW.isInBounds());
4474
4475 OffsVal = DAG.getSExtOrTrunc(OffsVal, dl, N.getValueType());
4476
4477 N = DAG.getMemBasePlusOffset(N, OffsVal, dl, Flags);
4478 continue;
4479 }
4480
4481 // N = N + Idx * ElementMul;
4482 SDValue IdxN = getValue(Idx);
4483
4484 if (IdxN.getValueType().isVector() != N.getValueType().isVector()) {
4485 if (N.getValueType().isVector()) {
4486 EVT VT = EVT::getVectorVT(*Context, IdxN.getValueType(),
4487 VectorElementCount);
4488 IdxN = DAG.getSplat(VT, dl, IdxN);
4489 } else {
4490 EVT VT =
4491 EVT::getVectorVT(*Context, N.getValueType(), VectorElementCount);
4492 N = DAG.getSplat(VT, dl, N);
4493 }
4494 }
4495
4496 // If the index is smaller or larger than intptr_t, truncate or extend
4497 // it.
4498 IdxN = DAG.getSExtOrTrunc(IdxN, dl, N.getValueType());
4499
4500 SDNodeFlags ScaleFlags;
4501 // The multiplication of an index by the type size does not wrap the
4502 // pointer index type in a signed sense (mul nsw).
4504
4505 // The multiplication of an index by the type size does not wrap the
4506 // pointer index type in an unsigned sense (mul nuw).
4507 ScaleFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4508
4509 if (ElementScalable) {
4510 EVT VScaleTy = N.getValueType().getScalarType();
4511 SDValue VScale = DAG.getNode(
4512 ISD::VSCALE, dl, VScaleTy,
4513 DAG.getConstant(ElementMul.getZExtValue(), dl, VScaleTy));
4514 if (N.getValueType().isVector())
4515 VScale = DAG.getSplatVector(N.getValueType(), dl, VScale);
4516 IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, VScale,
4517 ScaleFlags);
4518 } else {
4519 // If this is a multiply by a power of two, turn it into a shl
4520 // immediately. This is a very common case.
4521 if (ElementMul != 1) {
4522 if (ElementMul.isPowerOf2()) {
4523 unsigned Amt = ElementMul.logBase2();
4524 IdxN = DAG.getNode(
4525 ISD::SHL, dl, N.getValueType(), IdxN,
4526 DAG.getShiftAmountConstant(Amt, N.getValueType(), dl),
4527 ScaleFlags);
4528 } else {
4529 SDValue Scale = DAG.getConstant(ElementMul.getZExtValue(), dl,
4530 IdxN.getValueType());
4531 IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, Scale,
4532 ScaleFlags);
4533 }
4534 }
4535 }
4536
4537 // The successive addition of the current address, truncated to the
4538 // pointer index type and interpreted as an unsigned number, and each
4539 // offset, also interpreted as an unsigned number, does not wrap the
4540 // pointer index type (add nuw).
4541 SDNodeFlags AddFlags;
4542 AddFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4543 AddFlags.setInBounds(NW.isInBounds());
4544
4545 N = DAG.getMemBasePlusOffset(N, IdxN, dl, AddFlags);
4546 }
4547 }
4548
4549 if (IsVectorGEP && !N.getValueType().isVector()) {
4550 EVT VT = EVT::getVectorVT(*Context, N.getValueType(), VectorElementCount);
4551 N = DAG.getSplat(VT, dl, N);
4552 }
4553
4554 MVT PtrTy = TLI.getPointerTy(DAG.getDataLayout(), AS);
4555 MVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout(), AS);
4556 if (IsVectorGEP) {
4557 PtrTy = MVT::getVectorVT(PtrTy, VectorElementCount);
4558 PtrMemTy = MVT::getVectorVT(PtrMemTy, VectorElementCount);
4559 }
4560
4561 if (PtrMemTy != PtrTy && !cast<GEPOperator>(I).isInBounds())
4562 N = DAG.getPtrExtendInReg(N, dl, PtrMemTy);
4563
4564 setValue(&I, N);
4565}
4566
4567void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
4568 // If this is a fixed sized alloca in the entry block of the function,
4569 // allocate it statically on the stack.
4570 if (FuncInfo.StaticAllocaMap.count(&I))
4571 return; // getValue will auto-populate this.
4572
4573 SDLoc dl = getCurSDLoc();
4574 Type *Ty = I.getAllocatedType();
4575 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4576 auto &DL = DAG.getDataLayout();
4577 TypeSize TySize = DL.getTypeAllocSize(Ty);
4578 MaybeAlign Alignment = std::max(DL.getPrefTypeAlign(Ty), I.getAlign());
4579
4580 SDValue AllocSize = getValue(I.getArraySize());
4581
4582 EVT IntPtr = TLI.getPointerTy(DL, I.getAddressSpace());
4583 if (AllocSize.getValueType() != IntPtr)
4584 AllocSize = DAG.getZExtOrTrunc(AllocSize, dl, IntPtr);
4585
4586 if (TySize.isScalable())
4587 AllocSize = DAG.getNode(ISD::MUL, dl, IntPtr, AllocSize,
4588 DAG.getVScale(dl, IntPtr,
4589 APInt(IntPtr.getScalarSizeInBits(),
4590 TySize.getKnownMinValue())));
4591 else {
4592 SDValue TySizeValue =
4593 DAG.getConstant(TySize.getFixedValue(), dl, MVT::getIntegerVT(64));
4594 AllocSize = DAG.getNode(ISD::MUL, dl, IntPtr, AllocSize,
4595 DAG.getZExtOrTrunc(TySizeValue, dl, IntPtr));
4596 }
4597
4598 // Handle alignment. If the requested alignment is less than or equal to
4599 // the stack alignment, ignore it. If the size is greater than or equal to
4600 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
4601 Align StackAlign = DAG.getSubtarget().getFrameLowering()->getStackAlign();
4602 if (*Alignment <= StackAlign)
4603 Alignment = std::nullopt;
4604
4605 const uint64_t StackAlignMask = StackAlign.value() - 1U;
4606 // Round the size of the allocation up to the stack alignment size
4607 // by add SA-1 to the size. This doesn't overflow because we're computing
4608 // an address inside an alloca.
4609 AllocSize = DAG.getNode(ISD::ADD, dl, AllocSize.getValueType(), AllocSize,
4610 DAG.getConstant(StackAlignMask, dl, IntPtr),
4612
4613 // Mask out the low bits for alignment purposes.
4614 AllocSize = DAG.getNode(ISD::AND, dl, AllocSize.getValueType(), AllocSize,
4615 DAG.getSignedConstant(~StackAlignMask, dl, IntPtr));
4616
4617 SDValue Ops[] = {
4618 getRoot(), AllocSize,
4619 DAG.getConstant(Alignment ? Alignment->value() : 0, dl, IntPtr)};
4620 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
4621 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, dl, VTs, Ops);
4622 setValue(&I, DSA);
4623 DAG.setRoot(DSA.getValue(1));
4624
4625 assert(FuncInfo.MF->getFrameInfo().hasVarSizedObjects());
4626}
4627
4628static const MDNode *getRangeMetadata(const Instruction &I) {
4629 return I.getMetadata(LLVMContext::MD_range);
4630}
4631
4632static std::optional<ConstantRange> getRange(const Instruction &I) {
4633 if (const auto *CB = dyn_cast<CallBase>(&I))
4634 if (std::optional<ConstantRange> CR = CB->getRange())
4635 return CR;
4636 if (const MDNode *Range = getRangeMetadata(I))
4638 return std::nullopt;
4639}
4640
4642 if (const auto *CB = dyn_cast<CallBase>(&I))
4643 return CB->getRetNoFPClass();
4644 return fcNone;
4645}
4646
4647void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
4648 if (I.isAtomic())
4649 return visitAtomicLoad(I);
4650
4651 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4652 const Value *SV = I.getOperand(0);
4653 if (TLI.supportSwiftError()) {
4654 // Swifterror values can come from either a function parameter with
4655 // swifterror attribute or an alloca with swifterror attribute.
4656 if (const Argument *Arg = dyn_cast<Argument>(SV)) {
4657 if (Arg->hasSwiftErrorAttr())
4658 return visitLoadFromSwiftError(I);
4659 }
4660
4661 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
4662 if (Alloca->isSwiftError())
4663 return visitLoadFromSwiftError(I);
4664 }
4665 }
4666
4667 SDValue Ptr = getValue(SV);
4668
4669 Type *Ty = I.getType();
4670 SmallVector<EVT, 4> ValueVTs, MemVTs;
4672 ComputeValueVTs(TLI, DAG.getDataLayout(), Ty, ValueVTs, &MemVTs, &Offsets);
4673 unsigned NumValues = ValueVTs.size();
4674 if (NumValues == 0)
4675 return;
4676
4677 Align Alignment = I.getAlign();
4678 AAMDNodes AAInfo = I.getAAMetadata();
4679 const MDNode *Ranges = getRangeMetadata(I);
4680 bool isVolatile = I.isVolatile();
4681 MachineMemOperand::Flags MMOFlags =
4682 TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
4683
4684 SDValue Root;
4685 bool ConstantMemory = false;
4686 if (isVolatile)
4687 // Serialize volatile loads with other side effects.
4688 Root = getRoot();
4689 else if (NumValues > MaxParallelChains)
4690 Root = getMemoryRoot();
4691 else if (BatchAA &&
4692 BatchAA->pointsToConstantMemory(MemoryLocation(
4693 SV,
4694 LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4695 AAInfo))) {
4696 // Do not serialize (non-volatile) loads of constant memory with anything.
4697 Root = DAG.getEntryNode();
4698 ConstantMemory = true;
4700 } else {
4701 // Do not serialize non-volatile loads against each other.
4702 Root = DAG.getRoot();
4703 }
4704
4705 SDLoc dl = getCurSDLoc();
4706
4707 if (isVolatile)
4708 Root = TLI.prepareVolatileOrAtomicLoad(Root, dl, DAG);
4709
4710 SmallVector<SDValue, 4> Values(NumValues);
4711 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4712
4713 unsigned ChainI = 0;
4714 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4715 // Serializing loads here may result in excessive register pressure, and
4716 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
4717 // could recover a bit by hoisting nodes upward in the chain by recognizing
4718 // they are side-effect free or do not alias. The optimizer should really
4719 // avoid this case by converting large object/array copies to llvm.memcpy
4720 // (MaxParallelChains should always remain as failsafe).
4721 if (ChainI == MaxParallelChains) {
4722 assert(PendingLoads.empty() && "PendingLoads must be serialized first");
4723 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4724 ArrayRef(Chains.data(), ChainI));
4725 Root = Chain;
4726 ChainI = 0;
4727 }
4728
4729 // TODO: MachinePointerInfo only supports a fixed length offset.
4730 MachinePointerInfo PtrInfo =
4731 !Offsets[i].isScalable() || Offsets[i].isZero()
4732 ? MachinePointerInfo(SV, Offsets[i].getKnownMinValue())
4733 : MachinePointerInfo();
4734
4735 SDValue A = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4736 SDValue L = DAG.getLoad(MemVTs[i], dl, Root, A, PtrInfo, Alignment,
4737 MMOFlags, AAInfo, Ranges);
4738 Chains[ChainI] = L.getValue(1);
4739
4740 if (MemVTs[i] != ValueVTs[i])
4741 L = DAG.getPtrExtOrTrunc(L, dl, ValueVTs[i]);
4742
4743 Values[i] = L;
4744 }
4745
4746 if (!ConstantMemory) {
4747 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4748 ArrayRef(Chains.data(), ChainI));
4749 if (isVolatile)
4750 DAG.setRoot(Chain);
4751 else
4752 PendingLoads.push_back(Chain);
4753 }
4754
4755 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, dl,
4756 DAG.getVTList(ValueVTs), Values));
4757}
4758
4759void SelectionDAGBuilder::visitStoreToSwiftError(const StoreInst &I) {
4760 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4761 "call visitStoreToSwiftError when backend supports swifterror");
4762
4763 SmallVector<EVT, 4> ValueVTs;
4764 SmallVector<uint64_t, 4> Offsets;
4765 const Value *SrcV = I.getOperand(0);
4766 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4767 SrcV->getType(), ValueVTs, /*MemVTs=*/nullptr, &Offsets, 0);
4768 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4769 "expect a single EVT for swifterror");
4770
4771 SDValue Src = getValue(SrcV);
4772 // Create a virtual register, then update the virtual register.
4773 Register VReg =
4774 SwiftError.getOrCreateVRegDefAt(&I, FuncInfo.MBB, I.getPointerOperand());
4775 // Chain, DL, Reg, N or Chain, DL, Reg, N, Glue
4776 // Chain can be getRoot or getControlRoot.
4777 SDValue CopyNode = DAG.getCopyToReg(getRoot(), getCurSDLoc(), VReg,
4778 SDValue(Src.getNode(), Src.getResNo()));
4779 DAG.setRoot(CopyNode);
4780}
4781
4782void SelectionDAGBuilder::visitLoadFromSwiftError(const LoadInst &I) {
4783 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4784 "call visitLoadFromSwiftError when backend supports swifterror");
4785
4786 assert(!I.isVolatile() &&
4787 !I.hasMetadata(LLVMContext::MD_nontemporal) &&
4788 !I.hasMetadata(LLVMContext::MD_invariant_load) &&
4789 "Support volatile, non temporal, invariant for load_from_swift_error");
4790
4791 const Value *SV = I.getOperand(0);
4792 Type *Ty = I.getType();
4793 assert(
4794 (!BatchAA ||
4795 !BatchAA->pointsToConstantMemory(MemoryLocation(
4796 SV, LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4797 I.getAAMetadata()))) &&
4798 "load_from_swift_error should not be constant memory");
4799
4800 SmallVector<EVT, 4> ValueVTs;
4801 SmallVector<uint64_t, 4> Offsets;
4802 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), Ty,
4803 ValueVTs, /*MemVTs=*/nullptr, &Offsets, 0);
4804 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4805 "expect a single EVT for swifterror");
4806
4807 // Chain, DL, Reg, VT, Glue or Chain, DL, Reg, VT
4808 SDValue L = DAG.getCopyFromReg(
4809 getRoot(), getCurSDLoc(),
4810 SwiftError.getOrCreateVRegUseAt(&I, FuncInfo.MBB, SV), ValueVTs[0]);
4811
4812 setValue(&I, L);
4813}
4814
4815void SelectionDAGBuilder::visitStore(const StoreInst &I) {
4816 if (I.isAtomic())
4817 return visitAtomicStore(I);
4818
4819 const Value *SrcV = I.getOperand(0);
4820 const Value *PtrV = I.getOperand(1);
4821
4822 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4823 if (TLI.supportSwiftError()) {
4824 // Swifterror values can come from either a function parameter with
4825 // swifterror attribute or an alloca with swifterror attribute.
4826 if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
4827 if (Arg->hasSwiftErrorAttr())
4828 return visitStoreToSwiftError(I);
4829 }
4830
4831 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
4832 if (Alloca->isSwiftError())
4833 return visitStoreToSwiftError(I);
4834 }
4835 }
4836
4837 SmallVector<EVT, 4> ValueVTs, MemVTs;
4839 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4840 SrcV->getType(), ValueVTs, &MemVTs, &Offsets);
4841 unsigned NumValues = ValueVTs.size();
4842 if (NumValues == 0)
4843 return;
4844
4845 // Get the lowered operands. Note that we do this after
4846 // checking if NumResults is zero, because with zero results
4847 // the operands won't have values in the map.
4848 SDValue Src = getValue(SrcV);
4849 SDValue Ptr = getValue(PtrV);
4850
4851 SDValue Root = I.isVolatile() ? getRoot() : getMemoryRoot();
4852 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4853 SDLoc dl = getCurSDLoc();
4854 Align Alignment = I.getAlign();
4855 AAMDNodes AAInfo = I.getAAMetadata();
4856
4857 auto MMOFlags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
4858
4859 unsigned ChainI = 0;
4860 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4861 // See visitLoad comments.
4862 if (ChainI == MaxParallelChains) {
4863 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4864 ArrayRef(Chains.data(), ChainI));
4865 Root = Chain;
4866 ChainI = 0;
4867 }
4868
4869 // TODO: MachinePointerInfo only supports a fixed length offset.
4870 MachinePointerInfo PtrInfo =
4871 !Offsets[i].isScalable() || Offsets[i].isZero()
4872 ? MachinePointerInfo(PtrV, Offsets[i].getKnownMinValue())
4873 : MachinePointerInfo();
4874
4875 SDValue Add = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4876 SDValue Val = SDValue(Src.getNode(), Src.getResNo() + i);
4877 if (MemVTs[i] != ValueVTs[i])
4878 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVTs[i]);
4879 SDValue St =
4880 DAG.getStore(Root, dl, Val, Add, PtrInfo, Alignment, MMOFlags, AAInfo);
4881 Chains[ChainI] = St;
4882 }
4883
4884 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4885 ArrayRef(Chains.data(), ChainI));
4886 setValue(&I, StoreNode);
4887 DAG.setRoot(StoreNode);
4888}
4889
4890void SelectionDAGBuilder::visitMaskedStore(const CallInst &I,
4891 bool IsCompressing) {
4892 SDLoc sdl = getCurSDLoc();
4893
4894 Value *Src0Operand = I.getArgOperand(0);
4895 Value *PtrOperand = I.getArgOperand(1);
4896 Value *MaskOperand = I.getArgOperand(2);
4897 Align Alignment = I.getParamAlign(1).valueOrOne();
4898
4899 SDValue Ptr = getValue(PtrOperand);
4900 SDValue Src0 = getValue(Src0Operand);
4901 SDValue Mask = getValue(MaskOperand);
4902 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
4903
4904 EVT VT = Src0.getValueType();
4905
4906 auto MMOFlags = MachineMemOperand::MOStore;
4907 if (I.hasMetadata(LLVMContext::MD_nontemporal))
4909
4910 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4911 MachinePointerInfo(PtrOperand), MMOFlags,
4912 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
4913
4914 const auto &TLI = DAG.getTargetLoweringInfo();
4915
4916 SDValue StoreNode =
4917 !IsCompressing && TTI->hasConditionalLoadStoreForType(
4918 I.getArgOperand(0)->getType(), /*IsStore=*/true)
4919 ? TLI.visitMaskedStore(DAG, sdl, getMemoryRoot(), MMO, Ptr, Src0,
4920 Mask)
4921 : DAG.getMaskedStore(getMemoryRoot(), sdl, Src0, Ptr, Offset, Mask,
4922 VT, MMO, ISD::UNINDEXED, /*Truncating=*/false,
4923 IsCompressing);
4924 DAG.setRoot(StoreNode);
4925 setValue(&I, StoreNode);
4926}
4927
4928// Get a uniform base for the Gather/Scatter intrinsic.
4929// The first argument of the Gather/Scatter intrinsic is a vector of pointers.
4930// We try to represent it as a base pointer + vector of indices.
4931// Usually, the vector of pointers comes from a 'getelementptr' instruction.
4932// The first operand of the GEP may be a single pointer or a vector of pointers
4933// Example:
4934// %gep.ptr = getelementptr i32, <8 x i32*> %vptr, <8 x i32> %ind
4935// or
4936// %gep.ptr = getelementptr i32, i32* %ptr, <8 x i32> %ind
4937// %res = call <8 x i32> @llvm.masked.gather.v8i32(<8 x i32*> %gep.ptr, ..
4938//
4939// When the first GEP operand is a single pointer - it is the uniform base we
4940// are looking for. If first operand of the GEP is a splat vector - we
4941// extract the splat value and use it as a uniform base.
4942// In all other cases the function returns 'false'.
4943static bool getUniformBase(const Value *Ptr, SDValue &Base, SDValue &Index,
4944 SDValue &Scale, SelectionDAGBuilder *SDB,
4945 const BasicBlock *CurBB, uint64_t ElemSize) {
4946 SelectionDAG& DAG = SDB->DAG;
4947 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4948 const DataLayout &DL = DAG.getDataLayout();
4949
4950 assert(Ptr->getType()->isVectorTy() && "Unexpected pointer type");
4951
4952 // Handle splat constant pointer.
4953 if (auto *C = dyn_cast<Constant>(Ptr)) {
4954 C = C->getSplatValue();
4955 if (!C)
4956 return false;
4957
4958 Base = SDB->getValue(C);
4959
4960 ElementCount NumElts = cast<VectorType>(Ptr->getType())->getElementCount();
4961 EVT VT = EVT::getVectorVT(*DAG.getContext(), TLI.getPointerTy(DL), NumElts);
4962 Index = DAG.getConstant(0, SDB->getCurSDLoc(), VT);
4963 Scale = DAG.getTargetConstant(1, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
4964 return true;
4965 }
4966
4968 if (!GEP || GEP->getParent() != CurBB)
4969 return false;
4970
4971 if (GEP->getNumOperands() != 2)
4972 return false;
4973
4974 const Value *BasePtr = GEP->getPointerOperand();
4975 const Value *IndexVal = GEP->getOperand(GEP->getNumOperands() - 1);
4976
4977 // Make sure the base is scalar and the index is a vector.
4978 if (BasePtr->getType()->isVectorTy() || !IndexVal->getType()->isVectorTy())
4979 return false;
4980
4981 TypeSize ScaleVal = DL.getTypeAllocSize(GEP->getResultElementType());
4982 if (ScaleVal.isScalable())
4983 return false;
4984
4985 // Target may not support the required addressing mode.
4986 if (ScaleVal != 1 &&
4987 !TLI.isLegalScaleForGatherScatter(ScaleVal.getFixedValue(), ElemSize))
4988 return false;
4989
4990 Base = SDB->getValue(BasePtr);
4991 Index = SDB->getValue(IndexVal);
4992
4993 Scale =
4994 DAG.getTargetConstant(ScaleVal, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
4995 return true;
4996}
4997
4998void SelectionDAGBuilder::visitMaskedScatter(const CallInst &I) {
4999 SDLoc sdl = getCurSDLoc();
5000
5001 // llvm.masked.scatter.*(Src0, Ptrs, Mask)
5002 const Value *Ptr = I.getArgOperand(1);
5003 SDValue Src0 = getValue(I.getArgOperand(0));
5004 SDValue Mask = getValue(I.getArgOperand(2));
5005 EVT VT = Src0.getValueType();
5006 Align Alignment = I.getParamAlign(1).valueOrOne();
5007 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5008
5009 SDValue Base;
5010 SDValue Index;
5011 SDValue Scale;
5012 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
5013 I.getParent(), VT.getScalarStoreSize());
5014
5015 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
5016 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5017 MachinePointerInfo(AS), MachineMemOperand::MOStore,
5018 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
5019 if (!UniformBase) {
5020 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5021 Index = getValue(Ptr);
5022 Scale =
5023 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5024 }
5025
5026 EVT IdxVT = Index.getValueType();
5027 EVT EltTy = IdxVT.getVectorElementType();
5028 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
5029 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
5030 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
5031 }
5032
5033 SDValue Ops[] = { getMemoryRoot(), Src0, Mask, Base, Index, Scale };
5034 SDValue Scatter = DAG.getMaskedScatter(DAG.getVTList(MVT::Other), VT, sdl,
5035 Ops, MMO, ISD::SIGNED_SCALED, false);
5036 DAG.setRoot(Scatter);
5037 setValue(&I, Scatter);
5038}
5039
5040void SelectionDAGBuilder::visitMaskedLoad(const CallInst &I, bool IsExpanding) {
5041 SDLoc sdl = getCurSDLoc();
5042
5043 Value *PtrOperand = I.getArgOperand(0);
5044 Value *MaskOperand = I.getArgOperand(1);
5045 Value *Src0Operand = I.getArgOperand(2);
5046 Align Alignment = I.getParamAlign(0).valueOrOne();
5047
5048 SDValue Ptr = getValue(PtrOperand);
5049 SDValue Src0 = getValue(Src0Operand);
5050 SDValue Mask = getValue(MaskOperand);
5051 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
5052
5053 EVT VT = Src0.getValueType();
5054 AAMDNodes AAInfo = I.getAAMetadata();
5055 const MDNode *Ranges = getRangeMetadata(I);
5056
5057 // Do not serialize masked loads of constant memory with anything.
5058 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
5059 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
5060
5061 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
5062
5063 auto MMOFlags = MachineMemOperand::MOLoad;
5064 if (I.hasMetadata(LLVMContext::MD_nontemporal))
5066
5067 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5068 MachinePointerInfo(PtrOperand), MMOFlags,
5069 LocationSize::beforeOrAfterPointer(), Alignment, AAInfo, Ranges);
5070
5071 const auto &TLI = DAG.getTargetLoweringInfo();
5072
5073 // The Load/Res may point to different values and both of them are output
5074 // variables.
5075 SDValue Load;
5076 SDValue Res;
5077 if (!IsExpanding &&
5078 TTI->hasConditionalLoadStoreForType(Src0Operand->getType(),
5079 /*IsStore=*/false))
5080 Res = TLI.visitMaskedLoad(DAG, sdl, InChain, MMO, Load, Ptr, Src0, Mask);
5081 else
5082 Res = Load =
5083 DAG.getMaskedLoad(VT, sdl, InChain, Ptr, Offset, Mask, Src0, VT, MMO,
5084 ISD::UNINDEXED, ISD::NON_EXTLOAD, IsExpanding);
5085 if (AddToChain)
5086 PendingLoads.push_back(Load.getValue(1));
5087 setValue(&I, Res);
5088}
5089
5090void SelectionDAGBuilder::visitMaskedGather(const CallInst &I) {
5091 SDLoc sdl = getCurSDLoc();
5092
5093 // @llvm.masked.gather.*(Ptrs, Mask, Src0)
5094 const Value *Ptr = I.getArgOperand(0);
5095 SDValue Src0 = getValue(I.getArgOperand(2));
5096 SDValue Mask = getValue(I.getArgOperand(1));
5097
5098 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5099 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5100 Align Alignment = I.getParamAlign(0).valueOrOne();
5101
5102 const MDNode *Ranges = getRangeMetadata(I);
5103
5104 SDValue Root = DAG.getRoot();
5105 SDValue Base;
5106 SDValue Index;
5107 SDValue Scale;
5108 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
5109 I.getParent(), VT.getScalarStoreSize());
5110 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
5111 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5112 MachinePointerInfo(AS), MachineMemOperand::MOLoad,
5113 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata(),
5114 Ranges);
5115
5116 if (!UniformBase) {
5117 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5118 Index = getValue(Ptr);
5119 Scale =
5120 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5121 }
5122
5123 EVT IdxVT = Index.getValueType();
5124 EVT EltTy = IdxVT.getVectorElementType();
5125 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
5126 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
5127 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
5128 }
5129
5130 SDValue Ops[] = { Root, Src0, Mask, Base, Index, Scale };
5131 SDValue Gather =
5132 DAG.getMaskedGather(DAG.getVTList(VT, MVT::Other), VT, sdl, Ops, MMO,
5134
5135 PendingLoads.push_back(Gather.getValue(1));
5136 setValue(&I, Gather);
5137}
5138
5139void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) {
5140 SDLoc dl = getCurSDLoc();
5141 AtomicOrdering SuccessOrdering = I.getSuccessOrdering();
5142 AtomicOrdering FailureOrdering = I.getFailureOrdering();
5143 SyncScope::ID SSID = I.getSyncScopeID();
5144
5145 SDValue InChain = getRoot();
5146
5147 MVT MemVT = getValue(I.getCompareOperand()).getSimpleValueType();
5148 SDVTList VTs = DAG.getVTList(MemVT, MVT::i1, MVT::Other);
5149
5150 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5151 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5152
5153 MachineFunction &MF = DAG.getMachineFunction();
5154 MachineMemOperand *MMO = MF.getMachineMemOperand(
5155 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5156 DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, SuccessOrdering,
5157 FailureOrdering);
5158
5159 SDValue L = DAG.getAtomicCmpSwap(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS,
5160 dl, MemVT, VTs, InChain,
5161 getValue(I.getPointerOperand()),
5162 getValue(I.getCompareOperand()),
5163 getValue(I.getNewValOperand()), MMO);
5164
5165 SDValue OutChain = L.getValue(2);
5166
5167 setValue(&I, L);
5168 DAG.setRoot(OutChain);
5169}
5170
5171void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) {
5172 SDLoc dl = getCurSDLoc();
5174 switch (I.getOperation()) {
5175 default: llvm_unreachable("Unknown atomicrmw operation");
5176 case AtomicRMWInst::Xchg: NT = ISD::ATOMIC_SWAP; break;
5177 case AtomicRMWInst::Add: NT = ISD::ATOMIC_LOAD_ADD; break;
5178 case AtomicRMWInst::Sub: NT = ISD::ATOMIC_LOAD_SUB; break;
5179 case AtomicRMWInst::And: NT = ISD::ATOMIC_LOAD_AND; break;
5180 case AtomicRMWInst::Nand: NT = ISD::ATOMIC_LOAD_NAND; break;
5181 case AtomicRMWInst::Or: NT = ISD::ATOMIC_LOAD_OR; break;
5182 case AtomicRMWInst::Xor: NT = ISD::ATOMIC_LOAD_XOR; break;
5183 case AtomicRMWInst::Max: NT = ISD::ATOMIC_LOAD_MAX; break;
5184 case AtomicRMWInst::Min: NT = ISD::ATOMIC_LOAD_MIN; break;
5185 case AtomicRMWInst::UMax: NT = ISD::ATOMIC_LOAD_UMAX; break;
5186 case AtomicRMWInst::UMin: NT = ISD::ATOMIC_LOAD_UMIN; break;
5187 case AtomicRMWInst::FAdd: NT = ISD::ATOMIC_LOAD_FADD; break;
5188 case AtomicRMWInst::FSub: NT = ISD::ATOMIC_LOAD_FSUB; break;
5189 case AtomicRMWInst::FMax: NT = ISD::ATOMIC_LOAD_FMAX; break;
5190 case AtomicRMWInst::FMin: NT = ISD::ATOMIC_LOAD_FMIN; break;
5192 NT = ISD::ATOMIC_LOAD_FMAXIMUM;
5193 break;
5195 NT = ISD::ATOMIC_LOAD_FMINIMUM;
5196 break;
5198 NT = ISD::ATOMIC_LOAD_UINC_WRAP;
5199 break;
5201 NT = ISD::ATOMIC_LOAD_UDEC_WRAP;
5202 break;
5204 NT = ISD::ATOMIC_LOAD_USUB_COND;
5205 break;
5207 NT = ISD::ATOMIC_LOAD_USUB_SAT;
5208 break;
5209 }
5210 AtomicOrdering Ordering = I.getOrdering();
5211 SyncScope::ID SSID = I.getSyncScopeID();
5212
5213 SDValue InChain = getRoot();
5214
5215 auto MemVT = getValue(I.getValOperand()).getSimpleValueType();
5216 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5217 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5218
5219 MachineFunction &MF = DAG.getMachineFunction();
5220 MachineMemOperand *MMO = MF.getMachineMemOperand(
5221 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5222 DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, Ordering);
5223
5224 SDValue L =
5225 DAG.getAtomic(NT, dl, MemVT, InChain,
5226 getValue(I.getPointerOperand()), getValue(I.getValOperand()),
5227 MMO);
5228
5229 SDValue OutChain = L.getValue(1);
5230
5231 setValue(&I, L);
5232 DAG.setRoot(OutChain);
5233}
5234
5235void SelectionDAGBuilder::visitFence(const FenceInst &I) {
5236 SDLoc dl = getCurSDLoc();
5237 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5238 SDValue Ops[3];
5239 Ops[0] = getRoot();
5240 Ops[1] = DAG.getTargetConstant((unsigned)I.getOrdering(), dl,
5241 TLI.getFenceOperandTy(DAG.getDataLayout()));
5242 Ops[2] = DAG.getTargetConstant(I.getSyncScopeID(), dl,
5243 TLI.getFenceOperandTy(DAG.getDataLayout()));
5244 SDValue N = DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops);
5245 setValue(&I, N);
5246 DAG.setRoot(N);
5247}
5248
5249void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
5250 SDLoc dl = getCurSDLoc();
5251 AtomicOrdering Order = I.getOrdering();
5252 SyncScope::ID SSID = I.getSyncScopeID();
5253
5254 SDValue InChain = getRoot();
5255
5256 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5257 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5258 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
5259
5260 if (!TLI.supportsUnalignedAtomics() &&
5261 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5262 report_fatal_error("Cannot generate unaligned atomic load");
5263
5264 auto Flags = TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
5265
5266 const MDNode *Ranges = getRangeMetadata(I);
5267 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5268 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5269 I.getAlign(), AAMDNodes(), Ranges, SSID, Order);
5270
5271 InChain = TLI.prepareVolatileOrAtomicLoad(InChain, dl, DAG);
5272
5273 SDValue Ptr = getValue(I.getPointerOperand());
5274 SDValue L =
5275 DAG.getAtomicLoad(ISD::NON_EXTLOAD, dl, MemVT, MemVT, InChain, Ptr, MMO);
5276
5277 SDValue OutChain = L.getValue(1);
5278 if (MemVT != VT)
5279 L = DAG.getPtrExtOrTrunc(L, dl, VT);
5280
5281 setValue(&I, L);
5282 DAG.setRoot(OutChain);
5283}
5284
5285void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) {
5286 SDLoc dl = getCurSDLoc();
5287
5288 AtomicOrdering Ordering = I.getOrdering();
5289 SyncScope::ID SSID = I.getSyncScopeID();
5290
5291 SDValue InChain = getRoot();
5292
5293 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5294 EVT MemVT =
5295 TLI.getMemValueType(DAG.getDataLayout(), I.getValueOperand()->getType());
5296
5297 if (!TLI.supportsUnalignedAtomics() &&
5298 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5299 report_fatal_error("Cannot generate unaligned atomic store");
5300
5301 auto Flags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
5302
5303 MachineFunction &MF = DAG.getMachineFunction();
5304 MachineMemOperand *MMO = MF.getMachineMemOperand(
5305 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5306 I.getAlign(), AAMDNodes(), nullptr, SSID, Ordering);
5307
5308 SDValue Val = getValue(I.getValueOperand());
5309 if (Val.getValueType() != MemVT)
5310 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVT);
5311 SDValue Ptr = getValue(I.getPointerOperand());
5312
5313 SDValue OutChain =
5314 DAG.getAtomic(ISD::ATOMIC_STORE, dl, MemVT, InChain, Val, Ptr, MMO);
5315
5316 setValue(&I, OutChain);
5317 DAG.setRoot(OutChain);
5318}
5319
5320/// Check if this intrinsic call depends on the chain (1st return value)
5321/// and if it only *loads* memory.
5322/// Ignore the callsite's attributes. A specific call site may be marked with
5323/// readnone, but the lowering code will expect the chain based on the
5324/// definition.
5325std::pair<bool, bool>
5326SelectionDAGBuilder::getTargetIntrinsicCallProperties(const CallBase &I) {
5327 const Function *F = I.getCalledFunction();
5328 bool HasChain = !F->doesNotAccessMemory();
5329 bool OnlyLoad =
5330 HasChain && F->onlyReadsMemory() && F->willReturn() && F->doesNotThrow();
5331
5332 return {HasChain, OnlyLoad};
5333}
5334
5335SmallVector<SDValue, 8> SelectionDAGBuilder::getTargetIntrinsicOperands(
5336 const CallBase &I, bool HasChain, bool OnlyLoad,
5337 TargetLowering::IntrinsicInfo *TgtMemIntrinsicInfo) {
5338 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5339
5340 // Build the operand list.
5342 if (HasChain) { // If this intrinsic has side-effects, chainify it.
5343 if (OnlyLoad) {
5344 // We don't need to serialize loads against other loads.
5345 Ops.push_back(DAG.getRoot());
5346 } else {
5347 Ops.push_back(getRoot());
5348 }
5349 }
5350
5351 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
5352 if (!TgtMemIntrinsicInfo || TgtMemIntrinsicInfo->opc == ISD::INTRINSIC_VOID ||
5353 TgtMemIntrinsicInfo->opc == ISD::INTRINSIC_W_CHAIN)
5354 Ops.push_back(DAG.getTargetConstant(I.getIntrinsicID(), getCurSDLoc(),
5355 TLI.getPointerTy(DAG.getDataLayout())));
5356
5357 // Add all operands of the call to the operand list.
5358 for (unsigned i = 0, e = I.arg_size(); i != e; ++i) {
5359 const Value *Arg = I.getArgOperand(i);
5360 if (!I.paramHasAttr(i, Attribute::ImmArg)) {
5361 Ops.push_back(getValue(Arg));
5362 continue;
5363 }
5364
5365 // Use TargetConstant instead of a regular constant for immarg.
5366 EVT VT = TLI.getValueType(DAG.getDataLayout(), Arg->getType(), true);
5367 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Arg)) {
5368 assert(CI->getBitWidth() <= 64 &&
5369 "large intrinsic immediates not handled");
5370 Ops.push_back(DAG.getTargetConstant(*CI, SDLoc(), VT));
5371 } else {
5372 Ops.push_back(
5373 DAG.getTargetConstantFP(*cast<ConstantFP>(Arg), SDLoc(), VT));
5374 }
5375 }
5376
5377 if (std::optional<OperandBundleUse> Bundle =
5378 I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
5379 Value *Token = Bundle->Inputs[0].get();
5380 SDValue ConvControlToken = getValue(Token);
5381 assert(Ops.back().getValueType() != MVT::Glue &&
5382 "Did not expect another glue node here.");
5383 ConvControlToken =
5384 DAG.getNode(ISD::CONVERGENCECTRL_GLUE, {}, MVT::Glue, ConvControlToken);
5385 Ops.push_back(ConvControlToken);
5386 }
5387
5388 return Ops;
5389}
5390
5391SDVTList SelectionDAGBuilder::getTargetIntrinsicVTList(const CallBase &I,
5392 bool HasChain) {
5393 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5394
5395 SmallVector<EVT, 4> ValueVTs;
5396 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
5397
5398 if (HasChain)
5399 ValueVTs.push_back(MVT::Other);
5400
5401 return DAG.getVTList(ValueVTs);
5402}
5403
5404/// Get an INTRINSIC node for a target intrinsic which does not touch memory.
5405SDValue SelectionDAGBuilder::getTargetNonMemIntrinsicNode(
5406 const Type &IntrinsicVT, bool HasChain, ArrayRef<SDValue> Ops,
5407 const SDVTList &VTs) {
5408 if (!HasChain)
5409 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(), VTs, Ops);
5410 if (!IntrinsicVT.isVoidTy())
5411 return DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(), VTs, Ops);
5412 return DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops);
5413}
5414
5415/// Set root, convert return type if necessary and check alignment.
5416SDValue SelectionDAGBuilder::handleTargetIntrinsicRet(const CallBase &I,
5417 bool HasChain,
5418 bool OnlyLoad,
5419 SDValue Result) {
5420 if (HasChain) {
5421 SDValue Chain = Result.getValue(Result.getNode()->getNumValues() - 1);
5422 if (OnlyLoad)
5423 PendingLoads.push_back(Chain);
5424 else
5425 DAG.setRoot(Chain);
5426 }
5427
5428 if (I.getType()->isVoidTy())
5429 return Result;
5430
5431 if (MaybeAlign Alignment = I.getRetAlign(); InsertAssertAlign && Alignment) {
5432 // Insert `assertalign` node if there's an alignment.
5433 Result = DAG.getAssertAlign(getCurSDLoc(), Result, Alignment.valueOrOne());
5434 } else if (!isa<VectorType>(I.getType())) {
5435 Result = lowerRangeToAssertZExt(DAG, I, Result);
5436 }
5437
5438 return Result;
5439}
5440
5441/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
5442/// node.
5443void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
5444 unsigned Intrinsic) {
5445 auto [HasChain, OnlyLoad] = getTargetIntrinsicCallProperties(I);
5446
5447 // Info is set by getTgtMemIntrinsic
5448 TargetLowering::IntrinsicInfo Info;
5449 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5450 bool IsTgtMemIntrinsic =
5451 TLI.getTgtMemIntrinsic(Info, I, DAG.getMachineFunction(), Intrinsic);
5452
5453 SmallVector<SDValue, 8> Ops = getTargetIntrinsicOperands(
5454 I, HasChain, OnlyLoad, IsTgtMemIntrinsic ? &Info : nullptr);
5455 SDVTList VTs = getTargetIntrinsicVTList(I, HasChain);
5456
5457 // Propagate fast-math-flags from IR to node(s).
5458 SDNodeFlags Flags;
5459 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
5460 Flags.copyFMF(*FPMO);
5461 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
5462
5463 // Create the node.
5465
5466 // In some cases, custom collection of operands from CallInst I may be needed.
5468 if (IsTgtMemIntrinsic) {
5469 // This is target intrinsic that touches memory
5470 //
5471 // TODO: We currently just fallback to address space 0 if getTgtMemIntrinsic
5472 // didn't yield anything useful.
5473 MachinePointerInfo MPI;
5474 if (Info.ptrVal)
5475 MPI = MachinePointerInfo(Info.ptrVal, Info.offset);
5476 else if (Info.fallbackAddressSpace)
5477 MPI = MachinePointerInfo(*Info.fallbackAddressSpace);
5478 EVT MemVT = Info.memVT;
5479 LocationSize Size = LocationSize::precise(Info.size);
5480 if (Size.hasValue() && !Size.getValue())
5482 Align Alignment = Info.align.value_or(DAG.getEVTAlign(MemVT));
5483 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5484 MPI, Info.flags, Size, Alignment, I.getAAMetadata(), /*Ranges=*/nullptr,
5485 Info.ssid, Info.order, Info.failureOrder);
5486 Result =
5487 DAG.getMemIntrinsicNode(Info.opc, getCurSDLoc(), VTs, Ops, MemVT, MMO);
5488 } else {
5489 Result = getTargetNonMemIntrinsicNode(*I.getType(), HasChain, Ops, VTs);
5490 }
5491
5492 Result = handleTargetIntrinsicRet(I, HasChain, OnlyLoad, Result);
5493
5494 setValue(&I, Result);
5495}
5496
5497/// GetSignificand - Get the significand and build it into a floating-point
5498/// number with exponent of 1:
5499///
5500/// Op = (Op & 0x007fffff) | 0x3f800000;
5501///
5502/// where Op is the hexadecimal representation of floating point value.
5504 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5505 DAG.getConstant(0x007fffff, dl, MVT::i32));
5506 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
5507 DAG.getConstant(0x3f800000, dl, MVT::i32));
5508 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
5509}
5510
5511/// GetExponent - Get the exponent:
5512///
5513/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
5514///
5515/// where Op is the hexadecimal representation of floating point value.
5517 const TargetLowering &TLI, const SDLoc &dl) {
5518 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5519 DAG.getConstant(0x7f800000, dl, MVT::i32));
5520 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
5521 DAG.getShiftAmountConstant(23, MVT::i32, dl));
5522 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
5523 DAG.getConstant(127, dl, MVT::i32));
5524 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
5525}
5526
5527/// getF32Constant - Get 32-bit floating point constant.
5528static SDValue getF32Constant(SelectionDAG &DAG, unsigned Flt,
5529 const SDLoc &dl) {
5530 return DAG.getConstantFP(APFloat(APFloat::IEEEsingle(), APInt(32, Flt)), dl,
5531 MVT::f32);
5532}
5533
5535 SelectionDAG &DAG) {
5536 // TODO: What fast-math-flags should be set on the floating-point nodes?
5537
5538 // IntegerPartOfX = ((int32_t)(t0);
5539 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
5540
5541 // FractionalPartOfX = t0 - (float)IntegerPartOfX;
5542 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
5543 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
5544
5545 // IntegerPartOfX <<= 23;
5546 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
5547 DAG.getShiftAmountConstant(23, MVT::i32, dl));
5548
5549 SDValue TwoToFractionalPartOfX;
5550 if (LimitFloatPrecision <= 6) {
5551 // For floating-point precision of 6:
5552 //
5553 // TwoToFractionalPartOfX =
5554 // 0.997535578f +
5555 // (0.735607626f + 0.252464424f * x) * x;
5556 //
5557 // error 0.0144103317, which is 6 bits
5558 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5559 getF32Constant(DAG, 0x3e814304, dl));
5560 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5561 getF32Constant(DAG, 0x3f3c50c8, dl));
5562 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5563 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5564 getF32Constant(DAG, 0x3f7f5e7e, dl));
5565 } else if (LimitFloatPrecision <= 12) {
5566 // For floating-point precision of 12:
5567 //
5568 // TwoToFractionalPartOfX =
5569 // 0.999892986f +
5570 // (0.696457318f +
5571 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
5572 //
5573 // error 0.000107046256, which is 13 to 14 bits
5574 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5575 getF32Constant(DAG, 0x3da235e3, dl));
5576 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5577 getF32Constant(DAG, 0x3e65b8f3, dl));
5578 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5579 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5580 getF32Constant(DAG, 0x3f324b07, dl));
5581 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5582 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5583 getF32Constant(DAG, 0x3f7ff8fd, dl));
5584 } else { // LimitFloatPrecision <= 18
5585 // For floating-point precision of 18:
5586 //
5587 // TwoToFractionalPartOfX =
5588 // 0.999999982f +
5589 // (0.693148872f +
5590 // (0.240227044f +
5591 // (0.554906021e-1f +
5592 // (0.961591928e-2f +
5593 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
5594 // error 2.47208000*10^(-7), which is better than 18 bits
5595 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5596 getF32Constant(DAG, 0x3924b03e, dl));
5597 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5598 getF32Constant(DAG, 0x3ab24b87, dl));
5599 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5600 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5601 getF32Constant(DAG, 0x3c1d8c17, dl));
5602 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5603 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5604 getF32Constant(DAG, 0x3d634a1d, dl));
5605 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5606 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5607 getF32Constant(DAG, 0x3e75fe14, dl));
5608 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5609 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
5610 getF32Constant(DAG, 0x3f317234, dl));
5611 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
5612 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
5613 getF32Constant(DAG, 0x3f800000, dl));
5614 }
5615
5616 // Add the exponent into the result in integer domain.
5617 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFractionalPartOfX);
5618 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
5619 DAG.getNode(ISD::ADD, dl, MVT::i32, t13, IntegerPartOfX));
5620}
5621
5622/// expandExp - Lower an exp intrinsic. Handles the special sequences for
5623/// limited-precision mode.
5625 const TargetLowering &TLI, SDNodeFlags Flags) {
5626 if (Op.getValueType() == MVT::f32 &&
5628
5629 // Put the exponent in the right bit position for later addition to the
5630 // final result:
5631 //
5632 // t0 = Op * log2(e)
5633
5634 // TODO: What fast-math-flags should be set here?
5635 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
5636 DAG.getConstantFP(numbers::log2ef, dl, MVT::f32));
5637 return getLimitedPrecisionExp2(t0, dl, DAG);
5638 }
5639
5640 // No special expansion.
5641 return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op, Flags);
5642}
5643
5644/// expandLog - Lower a log intrinsic. Handles the special sequences for
5645/// limited-precision mode.
5647 const TargetLowering &TLI, SDNodeFlags Flags) {
5648 // TODO: What fast-math-flags should be set on the floating-point nodes?
5649
5650 if (Op.getValueType() == MVT::f32 &&
5652 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5653
5654 // Scale the exponent by log(2).
5655 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5656 SDValue LogOfExponent =
5657 DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5658 DAG.getConstantFP(numbers::ln2f, dl, MVT::f32));
5659
5660 // Get the significand and build it into a floating-point number with
5661 // exponent of 1.
5662 SDValue X = GetSignificand(DAG, Op1, dl);
5663
5664 SDValue LogOfMantissa;
5665 if (LimitFloatPrecision <= 6) {
5666 // For floating-point precision of 6:
5667 //
5668 // LogofMantissa =
5669 // -1.1609546f +
5670 // (1.4034025f - 0.23903021f * x) * x;
5671 //
5672 // error 0.0034276066, which is better than 8 bits
5673 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5674 getF32Constant(DAG, 0xbe74c456, dl));
5675 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5676 getF32Constant(DAG, 0x3fb3a2b1, dl));
5677 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5678 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5679 getF32Constant(DAG, 0x3f949a29, dl));
5680 } else if (LimitFloatPrecision <= 12) {
5681 // For floating-point precision of 12:
5682 //
5683 // LogOfMantissa =
5684 // -1.7417939f +
5685 // (2.8212026f +
5686 // (-1.4699568f +
5687 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
5688 //
5689 // error 0.000061011436, which is 14 bits
5690 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5691 getF32Constant(DAG, 0xbd67b6d6, dl));
5692 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5693 getF32Constant(DAG, 0x3ee4f4b8, dl));
5694 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5695 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5696 getF32Constant(DAG, 0x3fbc278b, dl));
5697 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5698 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5699 getF32Constant(DAG, 0x40348e95, dl));
5700 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5701 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5702 getF32Constant(DAG, 0x3fdef31a, dl));
5703 } else { // LimitFloatPrecision <= 18
5704 // For floating-point precision of 18:
5705 //
5706 // LogOfMantissa =
5707 // -2.1072184f +
5708 // (4.2372794f +
5709 // (-3.7029485f +
5710 // (2.2781945f +
5711 // (-0.87823314f +
5712 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
5713 //
5714 // error 0.0000023660568, which is better than 18 bits
5715 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5716 getF32Constant(DAG, 0xbc91e5ac, dl));
5717 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5718 getF32Constant(DAG, 0x3e4350aa, dl));
5719 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5720 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5721 getF32Constant(DAG, 0x3f60d3e3, dl));
5722 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5723 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5724 getF32Constant(DAG, 0x4011cdf0, dl));
5725 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5726 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5727 getF32Constant(DAG, 0x406cfd1c, dl));
5728 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5729 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5730 getF32Constant(DAG, 0x408797cb, dl));
5731 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5732 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5733 getF32Constant(DAG, 0x4006dcab, dl));
5734 }
5735
5736 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa);
5737 }
5738
5739 // No special expansion.
5740 return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op, Flags);
5741}
5742
5743/// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for
5744/// limited-precision mode.
5746 const TargetLowering &TLI, SDNodeFlags Flags) {
5747 // TODO: What fast-math-flags should be set on the floating-point nodes?
5748
5749 if (Op.getValueType() == MVT::f32 &&
5751 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5752
5753 // Get the exponent.
5754 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
5755
5756 // Get the significand and build it into a floating-point number with
5757 // exponent of 1.
5758 SDValue X = GetSignificand(DAG, Op1, dl);
5759
5760 // Different possible minimax approximations of significand in
5761 // floating-point for various degrees of accuracy over [1,2].
5762 SDValue Log2ofMantissa;
5763 if (LimitFloatPrecision <= 6) {
5764 // For floating-point precision of 6:
5765 //
5766 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
5767 //
5768 // error 0.0049451742, which is more than 7 bits
5769 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5770 getF32Constant(DAG, 0xbeb08fe0, dl));
5771 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5772 getF32Constant(DAG, 0x40019463, dl));
5773 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5774 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5775 getF32Constant(DAG, 0x3fd6633d, dl));
5776 } else if (LimitFloatPrecision <= 12) {
5777 // For floating-point precision of 12:
5778 //
5779 // Log2ofMantissa =
5780 // -2.51285454f +
5781 // (4.07009056f +
5782 // (-2.12067489f +
5783 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
5784 //
5785 // error 0.0000876136000, which is better than 13 bits
5786 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5787 getF32Constant(DAG, 0xbda7262e, dl));
5788 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5789 getF32Constant(DAG, 0x3f25280b, dl));
5790 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5791 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5792 getF32Constant(DAG, 0x4007b923, dl));
5793 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5794 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5795 getF32Constant(DAG, 0x40823e2f, dl));
5796 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5797 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5798 getF32Constant(DAG, 0x4020d29c, dl));
5799 } else { // LimitFloatPrecision <= 18
5800 // For floating-point precision of 18:
5801 //
5802 // Log2ofMantissa =
5803 // -3.0400495f +
5804 // (6.1129976f +
5805 // (-5.3420409f +
5806 // (3.2865683f +
5807 // (-1.2669343f +
5808 // (0.27515199f -
5809 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
5810 //
5811 // error 0.0000018516, which is better than 18 bits
5812 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5813 getF32Constant(DAG, 0xbcd2769e, dl));
5814 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5815 getF32Constant(DAG, 0x3e8ce0b9, dl));
5816 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5817 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5818 getF32Constant(DAG, 0x3fa22ae7, dl));
5819 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5820 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5821 getF32Constant(DAG, 0x40525723, dl));
5822 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5823 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5824 getF32Constant(DAG, 0x40aaf200, dl));
5825 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5826 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5827 getF32Constant(DAG, 0x40c39dad, dl));
5828 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5829 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5830 getF32Constant(DAG, 0x4042902c, dl));
5831 }
5832
5833 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
5834 }
5835
5836 // No special expansion.
5837 return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op, Flags);
5838}
5839
5840/// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for
5841/// limited-precision mode.
5843 const TargetLowering &TLI, SDNodeFlags Flags) {
5844 // TODO: What fast-math-flags should be set on the floating-point nodes?
5845
5846 if (Op.getValueType() == MVT::f32 &&
5848 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5849
5850 // Scale the exponent by log10(2) [0.30102999f].
5851 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5852 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5853 getF32Constant(DAG, 0x3e9a209a, dl));
5854
5855 // Get the significand and build it into a floating-point number with
5856 // exponent of 1.
5857 SDValue X = GetSignificand(DAG, Op1, dl);
5858
5859 SDValue Log10ofMantissa;
5860 if (LimitFloatPrecision <= 6) {
5861 // For floating-point precision of 6:
5862 //
5863 // Log10ofMantissa =
5864 // -0.50419619f +
5865 // (0.60948995f - 0.10380950f * x) * x;
5866 //
5867 // error 0.0014886165, which is 6 bits
5868 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5869 getF32Constant(DAG, 0xbdd49a13, dl));
5870 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5871 getF32Constant(DAG, 0x3f1c0789, dl));
5872 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5873 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5874 getF32Constant(DAG, 0x3f011300, dl));
5875 } else if (LimitFloatPrecision <= 12) {
5876 // For floating-point precision of 12:
5877 //
5878 // Log10ofMantissa =
5879 // -0.64831180f +
5880 // (0.91751397f +
5881 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
5882 //
5883 // error 0.00019228036, which is better than 12 bits
5884 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5885 getF32Constant(DAG, 0x3d431f31, dl));
5886 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5887 getF32Constant(DAG, 0x3ea21fb2, dl));
5888 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5889 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5890 getF32Constant(DAG, 0x3f6ae232, dl));
5891 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5892 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5893 getF32Constant(DAG, 0x3f25f7c3, dl));
5894 } else { // LimitFloatPrecision <= 18
5895 // For floating-point precision of 18:
5896 //
5897 // Log10ofMantissa =
5898 // -0.84299375f +
5899 // (1.5327582f +
5900 // (-1.0688956f +
5901 // (0.49102474f +
5902 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
5903 //
5904 // error 0.0000037995730, which is better than 18 bits
5905 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5906 getF32Constant(DAG, 0x3c5d51ce, dl));
5907 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5908 getF32Constant(DAG, 0x3e00685a, dl));
5909 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5910 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5911 getF32Constant(DAG, 0x3efb6798, dl));
5912 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5913 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5914 getF32Constant(DAG, 0x3f88d192, dl));
5915 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5916 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5917 getF32Constant(DAG, 0x3fc4316c, dl));
5918 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5919 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
5920 getF32Constant(DAG, 0x3f57ce70, dl));
5921 }
5922
5923 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
5924 }
5925
5926 // No special expansion.
5927 return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op, Flags);
5928}
5929
5930/// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for
5931/// limited-precision mode.
5933 const TargetLowering &TLI, SDNodeFlags Flags) {
5934 if (Op.getValueType() == MVT::f32 &&
5936 return getLimitedPrecisionExp2(Op, dl, DAG);
5937
5938 // No special expansion.
5939 return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op, Flags);
5940}
5941
5942/// visitPow - Lower a pow intrinsic. Handles the special sequences for
5943/// limited-precision mode with x == 10.0f.
5945 SelectionDAG &DAG, const TargetLowering &TLI,
5946 SDNodeFlags Flags) {
5947 bool IsExp10 = false;
5948 if (LHS.getValueType() == MVT::f32 && RHS.getValueType() == MVT::f32 &&
5951 APFloat Ten(10.0f);
5952 IsExp10 = LHSC->isExactlyValue(Ten);
5953 }
5954 }
5955
5956 // TODO: What fast-math-flags should be set on the FMUL node?
5957 if (IsExp10) {
5958 // Put the exponent in the right bit position for later addition to the
5959 // final result:
5960 //
5961 // #define LOG2OF10 3.3219281f
5962 // t0 = Op * LOG2OF10;
5963 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS,
5964 getF32Constant(DAG, 0x40549a78, dl));
5965 return getLimitedPrecisionExp2(t0, dl, DAG);
5966 }
5967
5968 // No special expansion.
5969 return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS, Flags);
5970}
5971
5972/// ExpandPowI - Expand a llvm.powi intrinsic.
5974 SelectionDAG &DAG) {
5975 // If RHS is a constant, we can expand this out to a multiplication tree if
5976 // it's beneficial on the target, otherwise we end up lowering to a call to
5977 // __powidf2 (for example).
5979 unsigned Val = RHSC->getSExtValue();
5980
5981 // powi(x, 0) -> 1.0
5982 if (Val == 0)
5983 return DAG.getConstantFP(1.0, DL, LHS.getValueType());
5984
5986 Val, DAG.shouldOptForSize())) {
5987 // Get the exponent as a positive value.
5988 if ((int)Val < 0)
5989 Val = -Val;
5990 // We use the simple binary decomposition method to generate the multiply
5991 // sequence. There are more optimal ways to do this (for example,
5992 // powi(x,15) generates one more multiply than it should), but this has
5993 // the benefit of being both really simple and much better than a libcall.
5994 SDValue Res; // Logically starts equal to 1.0
5995 SDValue CurSquare = LHS;
5996 // TODO: Intrinsics should have fast-math-flags that propagate to these
5997 // nodes.
5998 while (Val) {
5999 if (Val & 1) {
6000 if (Res.getNode())
6001 Res =
6002 DAG.getNode(ISD::FMUL, DL, Res.getValueType(), Res, CurSquare);
6003 else
6004 Res = CurSquare; // 1.0*CurSquare.
6005 }
6006
6007 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
6008 CurSquare, CurSquare);
6009 Val >>= 1;
6010 }
6011
6012 // If the original was negative, invert the result, producing 1/(x*x*x).
6013 if (RHSC->getSExtValue() < 0)
6014 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
6015 DAG.getConstantFP(1.0, DL, LHS.getValueType()), Res);
6016 return Res;
6017 }
6018 }
6019
6020 // Otherwise, expand to a libcall.
6021 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
6022}
6023
6024static SDValue expandDivFix(unsigned Opcode, const SDLoc &DL,
6025 SDValue LHS, SDValue RHS, SDValue Scale,
6026 SelectionDAG &DAG, const TargetLowering &TLI) {
6027 EVT VT = LHS.getValueType();
6028 bool Signed = Opcode == ISD::SDIVFIX || Opcode == ISD::SDIVFIXSAT;
6029 bool Saturating = Opcode == ISD::SDIVFIXSAT || Opcode == ISD::UDIVFIXSAT;
6030 LLVMContext &Ctx = *DAG.getContext();
6031
6032 // If the type is legal but the operation isn't, this node might survive all
6033 // the way to operation legalization. If we end up there and we do not have
6034 // the ability to widen the type (if VT*2 is not legal), we cannot expand the
6035 // node.
6036
6037 // Coax the legalizer into expanding the node during type legalization instead
6038 // by bumping the size by one bit. This will force it to Promote, enabling the
6039 // early expansion and avoiding the need to expand later.
6040
6041 // We don't have to do this if Scale is 0; that can always be expanded, unless
6042 // it's a saturating signed operation. Those can experience true integer
6043 // division overflow, a case which we must avoid.
6044
6045 // FIXME: We wouldn't have to do this (or any of the early
6046 // expansion/promotion) if it was possible to expand a libcall of an
6047 // illegal type during operation legalization. But it's not, so things
6048 // get a bit hacky.
6049 unsigned ScaleInt = Scale->getAsZExtVal();
6050 if ((ScaleInt > 0 || (Saturating && Signed)) &&
6051 (TLI.isTypeLegal(VT) ||
6052 (VT.isVector() && TLI.isTypeLegal(VT.getVectorElementType())))) {
6054 Opcode, VT, ScaleInt);
6055 if (Action != TargetLowering::Legal && Action != TargetLowering::Custom) {
6056 EVT PromVT;
6057 if (VT.isScalarInteger())
6058 PromVT = EVT::getIntegerVT(Ctx, VT.getSizeInBits() + 1);
6059 else if (VT.isVector()) {
6060 PromVT = VT.getVectorElementType();
6061 PromVT = EVT::getIntegerVT(Ctx, PromVT.getSizeInBits() + 1);
6062 PromVT = EVT::getVectorVT(Ctx, PromVT, VT.getVectorElementCount());
6063 } else
6064 llvm_unreachable("Wrong VT for DIVFIX?");
6065 LHS = DAG.getExtOrTrunc(Signed, LHS, DL, PromVT);
6066 RHS = DAG.getExtOrTrunc(Signed, RHS, DL, PromVT);
6067 EVT ShiftTy = TLI.getShiftAmountTy(PromVT, DAG.getDataLayout());
6068 // For saturating operations, we need to shift up the LHS to get the
6069 // proper saturation width, and then shift down again afterwards.
6070 if (Saturating)
6071 LHS = DAG.getNode(ISD::SHL, DL, PromVT, LHS,
6072 DAG.getConstant(1, DL, ShiftTy));
6073 SDValue Res = DAG.getNode(Opcode, DL, PromVT, LHS, RHS, Scale);
6074 if (Saturating)
6075 Res = DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, PromVT, Res,
6076 DAG.getConstant(1, DL, ShiftTy));
6077 return DAG.getZExtOrTrunc(Res, DL, VT);
6078 }
6079 }
6080
6081 return DAG.getNode(Opcode, DL, VT, LHS, RHS, Scale);
6082}
6083
6084// getUnderlyingArgRegs - Find underlying registers used for a truncated,
6085// bitcasted, or split argument. Returns a list of <Register, size in bits>
6086static void
6087getUnderlyingArgRegs(SmallVectorImpl<std::pair<Register, TypeSize>> &Regs,
6088 const SDValue &N) {
6089 switch (N.getOpcode()) {
6090 case ISD::CopyFromReg: {
6091 SDValue Op = N.getOperand(1);
6092 Regs.emplace_back(cast<RegisterSDNode>(Op)->getReg(),
6093 Op.getValueType().getSizeInBits());
6094 return;
6095 }
6096 case ISD::BITCAST:
6097 case ISD::AssertZext:
6098 case ISD::AssertSext:
6099 case ISD::TRUNCATE:
6100 getUnderlyingArgRegs(Regs, N.getOperand(0));
6101 return;
6102 case ISD::BUILD_PAIR:
6103 case ISD::BUILD_VECTOR:
6105 for (SDValue Op : N->op_values())
6106 getUnderlyingArgRegs(Regs, Op);
6107 return;
6108 default:
6109 return;
6110 }
6111}
6112
6113/// If the DbgValueInst is a dbg_value of a function argument, create the
6114/// corresponding DBG_VALUE machine instruction for it now. At the end of
6115/// instruction selection, they will be inserted to the entry BB.
6116/// We don't currently support this for variadic dbg_values, as they shouldn't
6117/// appear for function arguments or in the prologue.
6118bool SelectionDAGBuilder::EmitFuncArgumentDbgValue(
6119 const Value *V, DILocalVariable *Variable, DIExpression *Expr,
6120 DILocation *DL, FuncArgumentDbgValueKind Kind, const SDValue &N) {
6121 const Argument *Arg = dyn_cast<Argument>(V);
6122 if (!Arg)
6123 return false;
6124
6125 MachineFunction &MF = DAG.getMachineFunction();
6126 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
6127
6128 // Helper to create DBG_INSTR_REFs or DBG_VALUEs, depending on what kind
6129 // we've been asked to pursue.
6130 auto MakeVRegDbgValue = [&](Register Reg, DIExpression *FragExpr,
6131 bool Indirect) {
6132 if (Reg.isVirtual() && MF.useDebugInstrRef()) {
6133 // For VRegs, in instruction referencing mode, create a DBG_INSTR_REF
6134 // pointing at the VReg, which will be patched up later.
6135 auto &Inst = TII->get(TargetOpcode::DBG_INSTR_REF);
6137 /* Reg */ Reg, /* isDef */ false, /* isImp */ false,
6138 /* isKill */ false, /* isDead */ false,
6139 /* isUndef */ false, /* isEarlyClobber */ false,
6140 /* SubReg */ 0, /* isDebug */ true)});
6141
6142 auto *NewDIExpr = FragExpr;
6143 // We don't have an "Indirect" field in DBG_INSTR_REF, fold that into
6144 // the DIExpression.
6145 if (Indirect)
6146 NewDIExpr = DIExpression::prepend(FragExpr, DIExpression::DerefBefore);
6148 NewDIExpr = DIExpression::prependOpcodes(NewDIExpr, Ops);
6149 return BuildMI(MF, DL, Inst, false, MOs, Variable, NewDIExpr);
6150 } else {
6151 // Create a completely standard DBG_VALUE.
6152 auto &Inst = TII->get(TargetOpcode::DBG_VALUE);
6153 return BuildMI(MF, DL, Inst, Indirect, Reg, Variable, FragExpr);
6154 }
6155 };
6156
6157 if (Kind == FuncArgumentDbgValueKind::Value) {
6158 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6159 // should only emit as ArgDbgValue if the dbg.value intrinsic is found in
6160 // the entry block.
6161 bool IsInEntryBlock = FuncInfo.MBB == &FuncInfo.MF->front();
6162 if (!IsInEntryBlock)
6163 return false;
6164
6165 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6166 // should only emit as ArgDbgValue if the dbg.value intrinsic describes a
6167 // variable that also is a param.
6168 //
6169 // Although, if we are at the top of the entry block already, we can still
6170 // emit using ArgDbgValue. This might catch some situations when the
6171 // dbg.value refers to an argument that isn't used in the entry block, so
6172 // any CopyToReg node would be optimized out and the only way to express
6173 // this DBG_VALUE is by using the physical reg (or FI) as done in this
6174 // method. ArgDbgValues are hoisted to the beginning of the entry block. So
6175 // we should only emit as ArgDbgValue if the Variable is an argument to the
6176 // current function, and the dbg.value intrinsic is found in the entry
6177 // block.
6178 bool VariableIsFunctionInputArg = Variable->isParameter() &&
6179 !DL->getInlinedAt();
6180 bool IsInPrologue = SDNodeOrder == LowestSDNodeOrder;
6181 if (!IsInPrologue && !VariableIsFunctionInputArg)
6182 return false;
6183
6184 // Here we assume that a function argument on IR level only can be used to
6185 // describe one input parameter on source level. If we for example have
6186 // source code like this
6187 //
6188 // struct A { long x, y; };
6189 // void foo(struct A a, long b) {
6190 // ...
6191 // b = a.x;
6192 // ...
6193 // }
6194 //
6195 // and IR like this
6196 //
6197 // define void @foo(i32 %a1, i32 %a2, i32 %b) {
6198 // entry:
6199 // call void @llvm.dbg.value(metadata i32 %a1, "a", DW_OP_LLVM_fragment
6200 // call void @llvm.dbg.value(metadata i32 %a2, "a", DW_OP_LLVM_fragment
6201 // call void @llvm.dbg.value(metadata i32 %b, "b",
6202 // ...
6203 // call void @llvm.dbg.value(metadata i32 %a1, "b"
6204 // ...
6205 //
6206 // then the last dbg.value is describing a parameter "b" using a value that
6207 // is an argument. But since we already has used %a1 to describe a parameter
6208 // we should not handle that last dbg.value here (that would result in an
6209 // incorrect hoisting of the DBG_VALUE to the function entry).
6210 // Notice that we allow one dbg.value per IR level argument, to accommodate
6211 // for the situation with fragments above.
6212 // If there is no node for the value being handled, we return true to skip
6213 // the normal generation of debug info, as it would kill existing debug
6214 // info for the parameter in case of duplicates.
6215 if (VariableIsFunctionInputArg) {
6216 unsigned ArgNo = Arg->getArgNo();
6217 if (ArgNo >= FuncInfo.DescribedArgs.size())
6218 FuncInfo.DescribedArgs.resize(ArgNo + 1, false);
6219 else if (!IsInPrologue && FuncInfo.DescribedArgs.test(ArgNo))
6220 return !NodeMap[V].getNode();
6221 FuncInfo.DescribedArgs.set(ArgNo);
6222 }
6223 }
6224
6225 bool IsIndirect = false;
6226 std::optional<MachineOperand> Op;
6227 // Some arguments' frame index is recorded during argument lowering.
6228 int FI = FuncInfo.getArgumentFrameIndex(Arg);
6229 if (FI != std::numeric_limits<int>::max())
6231
6233 if (!Op && N.getNode()) {
6234 getUnderlyingArgRegs(ArgRegsAndSizes, N);
6235 Register Reg;
6236 if (ArgRegsAndSizes.size() == 1)
6237 Reg = ArgRegsAndSizes.front().first;
6238
6239 if (Reg && Reg.isVirtual()) {
6240 MachineRegisterInfo &RegInfo = MF.getRegInfo();
6241 Register PR = RegInfo.getLiveInPhysReg(Reg);
6242 if (PR)
6243 Reg = PR;
6244 }
6245 if (Reg) {
6247 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6248 }
6249 }
6250
6251 if (!Op && N.getNode()) {
6252 // Check if frame index is available.
6253 SDValue LCandidate = peekThroughBitcasts(N);
6254 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(LCandidate.getNode()))
6255 if (FrameIndexSDNode *FINode =
6256 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
6257 Op = MachineOperand::CreateFI(FINode->getIndex());
6258 }
6259
6260 if (!Op) {
6261 // Create a DBG_VALUE for each decomposed value in ArgRegs to cover Reg
6262 auto splitMultiRegDbgValue = [&](ArrayRef<std::pair<Register, TypeSize>>
6263 SplitRegs) {
6264 unsigned Offset = 0;
6265 for (const auto &RegAndSize : SplitRegs) {
6266 // If the expression is already a fragment, the current register
6267 // offset+size might extend beyond the fragment. In this case, only
6268 // the register bits that are inside the fragment are relevant.
6269 int RegFragmentSizeInBits = RegAndSize.second;
6270 if (auto ExprFragmentInfo = Expr->getFragmentInfo()) {
6271 uint64_t ExprFragmentSizeInBits = ExprFragmentInfo->SizeInBits;
6272 // The register is entirely outside the expression fragment,
6273 // so is irrelevant for debug info.
6274 if (Offset >= ExprFragmentSizeInBits)
6275 break;
6276 // The register is partially outside the expression fragment, only
6277 // the low bits within the fragment are relevant for debug info.
6278 if (Offset + RegFragmentSizeInBits > ExprFragmentSizeInBits) {
6279 RegFragmentSizeInBits = ExprFragmentSizeInBits - Offset;
6280 }
6281 }
6282
6283 auto FragmentExpr = DIExpression::createFragmentExpression(
6284 Expr, Offset, RegFragmentSizeInBits);
6285 Offset += RegAndSize.second;
6286 // If a valid fragment expression cannot be created, the variable's
6287 // correct value cannot be determined and so it is set as poison.
6288 if (!FragmentExpr) {
6289 SDDbgValue *SDV = DAG.getConstantDbgValue(
6290 Variable, Expr, PoisonValue::get(V->getType()), DL, SDNodeOrder);
6291 DAG.AddDbgValue(SDV, false);
6292 continue;
6293 }
6294 MachineInstr *NewMI =
6295 MakeVRegDbgValue(RegAndSize.first, *FragmentExpr,
6296 Kind != FuncArgumentDbgValueKind::Value);
6297 FuncInfo.ArgDbgValues.push_back(NewMI);
6298 }
6299 };
6300
6301 // Check if ValueMap has reg number.
6303 VMI = FuncInfo.ValueMap.find(V);
6304 if (VMI != FuncInfo.ValueMap.end()) {
6305 const auto &TLI = DAG.getTargetLoweringInfo();
6306 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), VMI->second,
6307 V->getType(), std::nullopt);
6308 if (RFV.occupiesMultipleRegs()) {
6309 splitMultiRegDbgValue(RFV.getRegsAndSizes());
6310 return true;
6311 }
6312
6313 Op = MachineOperand::CreateReg(VMI->second, false);
6314 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6315 } else if (ArgRegsAndSizes.size() > 1) {
6316 // This was split due to the calling convention, and no virtual register
6317 // mapping exists for the value.
6318 splitMultiRegDbgValue(ArgRegsAndSizes);
6319 return true;
6320 }
6321 }
6322
6323 if (!Op)
6324 return false;
6325
6326 assert(Variable->isValidLocationForIntrinsic(DL) &&
6327 "Expected inlined-at fields to agree");
6328 MachineInstr *NewMI = nullptr;
6329
6330 if (Op->isReg())
6331 NewMI = MakeVRegDbgValue(Op->getReg(), Expr, IsIndirect);
6332 else
6333 NewMI = BuildMI(MF, DL, TII->get(TargetOpcode::DBG_VALUE), true, *Op,
6334 Variable, Expr);
6335
6336 // Otherwise, use ArgDbgValues.
6337 FuncInfo.ArgDbgValues.push_back(NewMI);
6338 return true;
6339}
6340
6341/// Return the appropriate SDDbgValue based on N.
6342SDDbgValue *SelectionDAGBuilder::getDbgValue(SDValue N,
6343 DILocalVariable *Variable,
6344 DIExpression *Expr,
6345 const DebugLoc &dl,
6346 unsigned DbgSDNodeOrder) {
6347 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
6348 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can describe
6349 // stack slot locations.
6350 //
6351 // Consider "int x = 0; int *px = &x;". There are two kinds of interesting
6352 // debug values here after optimization:
6353 //
6354 // dbg.value(i32* %px, !"int *px", !DIExpression()), and
6355 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
6356 //
6357 // Both describe the direct values of their associated variables.
6358 return DAG.getFrameIndexDbgValue(Variable, Expr, FISDN->getIndex(),
6359 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6360 }
6361 return DAG.getDbgValue(Variable, Expr, N.getNode(), N.getResNo(),
6362 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6363}
6364
6365static unsigned FixedPointIntrinsicToOpcode(unsigned Intrinsic) {
6366 switch (Intrinsic) {
6367 case Intrinsic::smul_fix:
6368 return ISD::SMULFIX;
6369 case Intrinsic::umul_fix:
6370 return ISD::UMULFIX;
6371 case Intrinsic::smul_fix_sat:
6372 return ISD::SMULFIXSAT;
6373 case Intrinsic::umul_fix_sat:
6374 return ISD::UMULFIXSAT;
6375 case Intrinsic::sdiv_fix:
6376 return ISD::SDIVFIX;
6377 case Intrinsic::udiv_fix:
6378 return ISD::UDIVFIX;
6379 case Intrinsic::sdiv_fix_sat:
6380 return ISD::SDIVFIXSAT;
6381 case Intrinsic::udiv_fix_sat:
6382 return ISD::UDIVFIXSAT;
6383 default:
6384 llvm_unreachable("Unhandled fixed point intrinsic");
6385 }
6386}
6387
6388/// Given a @llvm.call.preallocated.setup, return the corresponding
6389/// preallocated call.
6390static const CallBase *FindPreallocatedCall(const Value *PreallocatedSetup) {
6391 assert(cast<CallBase>(PreallocatedSetup)
6393 ->getIntrinsicID() == Intrinsic::call_preallocated_setup &&
6394 "expected call_preallocated_setup Value");
6395 for (const auto *U : PreallocatedSetup->users()) {
6396 auto *UseCall = cast<CallBase>(U);
6397 const Function *Fn = UseCall->getCalledFunction();
6398 if (!Fn || Fn->getIntrinsicID() != Intrinsic::call_preallocated_arg) {
6399 return UseCall;
6400 }
6401 }
6402 llvm_unreachable("expected corresponding call to preallocated setup/arg");
6403}
6404
6405/// If DI is a debug value with an EntryValue expression, lower it using the
6406/// corresponding physical register of the associated Argument value
6407/// (guaranteed to exist by the verifier).
6408bool SelectionDAGBuilder::visitEntryValueDbgValue(
6409 ArrayRef<const Value *> Values, DILocalVariable *Variable,
6410 DIExpression *Expr, DebugLoc DbgLoc) {
6411 if (!Expr->isEntryValue() || !hasSingleElement(Values))
6412 return false;
6413
6414 // These properties are guaranteed by the verifier.
6415 const Argument *Arg = cast<Argument>(Values[0]);
6416 assert(Arg->hasAttribute(Attribute::AttrKind::SwiftAsync));
6417
6418 auto ArgIt = FuncInfo.ValueMap.find(Arg);
6419 if (ArgIt == FuncInfo.ValueMap.end()) {
6420 LLVM_DEBUG(
6421 dbgs() << "Dropping dbg.value: expression is entry_value but "
6422 "couldn't find an associated register for the Argument\n");
6423 return true;
6424 }
6425 Register ArgVReg = ArgIt->getSecond();
6426
6427 for (auto [PhysReg, VirtReg] : FuncInfo.RegInfo->liveins())
6428 if (ArgVReg == VirtReg || ArgVReg == PhysReg) {
6429 SDDbgValue *SDV = DAG.getVRegDbgValue(
6430 Variable, Expr, PhysReg, false /*IsIndidrect*/, DbgLoc, SDNodeOrder);
6431 DAG.AddDbgValue(SDV, false /*treat as dbg.declare byval parameter*/);
6432 return true;
6433 }
6434 LLVM_DEBUG(dbgs() << "Dropping dbg.value: expression is entry_value but "
6435 "couldn't find a physical register\n");
6436 return true;
6437}
6438
6439/// Lower the call to the specified intrinsic function.
6440void SelectionDAGBuilder::visitConvergenceControl(const CallInst &I,
6441 unsigned Intrinsic) {
6442 SDLoc sdl = getCurSDLoc();
6443 switch (Intrinsic) {
6444 case Intrinsic::experimental_convergence_anchor:
6445 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ANCHOR, sdl, MVT::Untyped));
6446 break;
6447 case Intrinsic::experimental_convergence_entry:
6448 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ENTRY, sdl, MVT::Untyped));
6449 break;
6450 case Intrinsic::experimental_convergence_loop: {
6451 auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl);
6452 auto *Token = Bundle->Inputs[0].get();
6453 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_LOOP, sdl, MVT::Untyped,
6454 getValue(Token)));
6455 break;
6456 }
6457 }
6458}
6459
6460void SelectionDAGBuilder::visitVectorHistogram(const CallInst &I,
6461 unsigned IntrinsicID) {
6462 // For now, we're only lowering an 'add' histogram.
6463 // We can add others later, e.g. saturating adds, min/max.
6464 assert(IntrinsicID == Intrinsic::experimental_vector_histogram_add &&
6465 "Tried to lower unsupported histogram type");
6466 SDLoc sdl = getCurSDLoc();
6467 Value *Ptr = I.getOperand(0);
6468 SDValue Inc = getValue(I.getOperand(1));
6469 SDValue Mask = getValue(I.getOperand(2));
6470
6471 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6472 DataLayout TargetDL = DAG.getDataLayout();
6473 EVT VT = Inc.getValueType();
6474 Align Alignment = DAG.getEVTAlign(VT);
6475
6476 const MDNode *Ranges = getRangeMetadata(I);
6477
6478 SDValue Root = DAG.getRoot();
6479 SDValue Base;
6480 SDValue Index;
6481 SDValue Scale;
6482 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
6483 I.getParent(), VT.getScalarStoreSize());
6484
6485 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
6486
6487 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
6488 MachinePointerInfo(AS),
6490 MemoryLocation::UnknownSize, Alignment, I.getAAMetadata(), Ranges);
6491
6492 if (!UniformBase) {
6493 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6494 Index = getValue(Ptr);
6495 Scale =
6496 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6497 }
6498
6499 EVT IdxVT = Index.getValueType();
6500 EVT EltTy = IdxVT.getVectorElementType();
6501 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
6502 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
6503 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
6504 }
6505
6506 SDValue ID = DAG.getTargetConstant(IntrinsicID, sdl, MVT::i32);
6507
6508 SDValue Ops[] = {Root, Inc, Mask, Base, Index, Scale, ID};
6509 SDValue Histogram = DAG.getMaskedHistogram(DAG.getVTList(MVT::Other), VT, sdl,
6510 Ops, MMO, ISD::SIGNED_SCALED);
6511
6512 setValue(&I, Histogram);
6513 DAG.setRoot(Histogram);
6514}
6515
6516void SelectionDAGBuilder::visitVectorExtractLastActive(const CallInst &I,
6517 unsigned Intrinsic) {
6518 assert(Intrinsic == Intrinsic::experimental_vector_extract_last_active &&
6519 "Tried lowering invalid vector extract last");
6520 SDLoc sdl = getCurSDLoc();
6521 const DataLayout &Layout = DAG.getDataLayout();
6522 SDValue Data = getValue(I.getOperand(0));
6523 SDValue Mask = getValue(I.getOperand(1));
6524
6525 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6526 EVT ResVT = TLI.getValueType(Layout, I.getType());
6527
6528 EVT ExtVT = TLI.getVectorIdxTy(Layout);
6529 SDValue Idx = DAG.getNode(ISD::VECTOR_FIND_LAST_ACTIVE, sdl, ExtVT, Mask);
6530 SDValue Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl, ResVT, Data, Idx);
6531
6532 Value *Default = I.getOperand(2);
6534 SDValue PassThru = getValue(Default);
6535 EVT BoolVT = Mask.getValueType().getScalarType();
6536 SDValue AnyActive = DAG.getNode(ISD::VECREDUCE_OR, sdl, BoolVT, Mask);
6537 Result = DAG.getSelect(sdl, ResVT, AnyActive, Result, PassThru);
6538 }
6539
6540 setValue(&I, Result);
6541}
6542
6543/// Lower the call to the specified intrinsic function.
6544void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
6545 unsigned Intrinsic) {
6546 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6547 SDLoc sdl = getCurSDLoc();
6548 DebugLoc dl = getCurDebugLoc();
6549 SDValue Res;
6550
6551 SDNodeFlags Flags;
6552 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
6553 Flags.copyFMF(*FPOp);
6554
6555 switch (Intrinsic) {
6556 default:
6557 // By default, turn this into a target intrinsic node.
6558 visitTargetIntrinsic(I, Intrinsic);
6559 return;
6560 case Intrinsic::vscale: {
6561 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6562 setValue(&I, DAG.getVScale(sdl, VT, APInt(VT.getSizeInBits(), 1)));
6563 return;
6564 }
6565 case Intrinsic::vastart: visitVAStart(I); return;
6566 case Intrinsic::vaend: visitVAEnd(I); return;
6567 case Intrinsic::vacopy: visitVACopy(I); return;
6568 case Intrinsic::returnaddress:
6569 setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl,
6570 TLI.getValueType(DAG.getDataLayout(), I.getType()),
6571 getValue(I.getArgOperand(0))));
6572 return;
6573 case Intrinsic::addressofreturnaddress:
6574 setValue(&I,
6575 DAG.getNode(ISD::ADDROFRETURNADDR, sdl,
6576 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6577 return;
6578 case Intrinsic::sponentry:
6579 setValue(&I,
6580 DAG.getNode(ISD::SPONENTRY, sdl,
6581 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6582 return;
6583 case Intrinsic::frameaddress:
6584 setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl,
6585 TLI.getFrameIndexTy(DAG.getDataLayout()),
6586 getValue(I.getArgOperand(0))));
6587 return;
6588 case Intrinsic::read_volatile_register:
6589 case Intrinsic::read_register: {
6590 Value *Reg = I.getArgOperand(0);
6591 SDValue Chain = getRoot();
6593 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6594 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6595 Res = DAG.getNode(ISD::READ_REGISTER, sdl,
6596 DAG.getVTList(VT, MVT::Other), Chain, RegName);
6597 setValue(&I, Res);
6598 DAG.setRoot(Res.getValue(1));
6599 return;
6600 }
6601 case Intrinsic::write_register: {
6602 Value *Reg = I.getArgOperand(0);
6603 Value *RegValue = I.getArgOperand(1);
6604 SDValue Chain = getRoot();
6606 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6607 DAG.setRoot(DAG.getNode(ISD::WRITE_REGISTER, sdl, MVT::Other, Chain,
6608 RegName, getValue(RegValue)));
6609 return;
6610 }
6611 case Intrinsic::memcpy:
6612 case Intrinsic::memcpy_inline: {
6613 const auto &MCI = cast<MemCpyInst>(I);
6614 SDValue Dst = getValue(I.getArgOperand(0));
6615 SDValue Src = getValue(I.getArgOperand(1));
6616 SDValue Size = getValue(I.getArgOperand(2));
6617 assert((!MCI.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6618 "memcpy_inline needs constant size");
6619 // @llvm.memcpy.inline defines 0 and 1 to both mean no alignment.
6620 Align DstAlign = MCI.getDestAlign().valueOrOne();
6621 Align SrcAlign = MCI.getSourceAlign().valueOrOne();
6622 Align Alignment = std::min(DstAlign, SrcAlign);
6623 bool isVol = MCI.isVolatile();
6624 // FIXME: Support passing different dest/src alignments to the memcpy DAG
6625 // node.
6626 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6627 SDValue MC = DAG.getMemcpy(Root, sdl, Dst, Src, Size, Alignment, isVol,
6628 MCI.isForceInlined(), &I, std::nullopt,
6629 MachinePointerInfo(I.getArgOperand(0)),
6630 MachinePointerInfo(I.getArgOperand(1)),
6631 I.getAAMetadata(), BatchAA);
6632 updateDAGForMaybeTailCall(MC);
6633 return;
6634 }
6635 case Intrinsic::memset:
6636 case Intrinsic::memset_inline: {
6637 const auto &MSII = cast<MemSetInst>(I);
6638 SDValue Dst = getValue(I.getArgOperand(0));
6639 SDValue Value = getValue(I.getArgOperand(1));
6640 SDValue Size = getValue(I.getArgOperand(2));
6641 assert((!MSII.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6642 "memset_inline needs constant size");
6643 // @llvm.memset defines 0 and 1 to both mean no alignment.
6644 Align DstAlign = MSII.getDestAlign().valueOrOne();
6645 bool isVol = MSII.isVolatile();
6646 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6647 SDValue MC = DAG.getMemset(
6648 Root, sdl, Dst, Value, Size, DstAlign, isVol, MSII.isForceInlined(),
6649 &I, MachinePointerInfo(I.getArgOperand(0)), I.getAAMetadata());
6650 updateDAGForMaybeTailCall(MC);
6651 return;
6652 }
6653 case Intrinsic::memmove: {
6654 const auto &MMI = cast<MemMoveInst>(I);
6655 SDValue Op1 = getValue(I.getArgOperand(0));
6656 SDValue Op2 = getValue(I.getArgOperand(1));
6657 SDValue Op3 = getValue(I.getArgOperand(2));
6658 // @llvm.memmove defines 0 and 1 to both mean no alignment.
6659 Align DstAlign = MMI.getDestAlign().valueOrOne();
6660 Align SrcAlign = MMI.getSourceAlign().valueOrOne();
6661 Align Alignment = std::min(DstAlign, SrcAlign);
6662 bool isVol = MMI.isVolatile();
6663 // FIXME: Support passing different dest/src alignments to the memmove DAG
6664 // node.
6665 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6666 SDValue MM = DAG.getMemmove(Root, sdl, Op1, Op2, Op3, Alignment, isVol, &I,
6667 /* OverrideTailCall */ std::nullopt,
6668 MachinePointerInfo(I.getArgOperand(0)),
6669 MachinePointerInfo(I.getArgOperand(1)),
6670 I.getAAMetadata(), BatchAA);
6671 updateDAGForMaybeTailCall(MM);
6672 return;
6673 }
6674 case Intrinsic::memcpy_element_unordered_atomic: {
6675 auto &MI = cast<AnyMemCpyInst>(I);
6676 SDValue Dst = getValue(MI.getRawDest());
6677 SDValue Src = getValue(MI.getRawSource());
6678 SDValue Length = getValue(MI.getLength());
6679
6680 Type *LengthTy = MI.getLength()->getType();
6681 unsigned ElemSz = MI.getElementSizeInBytes();
6682 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6683 SDValue MC =
6684 DAG.getAtomicMemcpy(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6685 isTC, MachinePointerInfo(MI.getRawDest()),
6686 MachinePointerInfo(MI.getRawSource()));
6687 updateDAGForMaybeTailCall(MC);
6688 return;
6689 }
6690 case Intrinsic::memmove_element_unordered_atomic: {
6691 auto &MI = cast<AnyMemMoveInst>(I);
6692 SDValue Dst = getValue(MI.getRawDest());
6693 SDValue Src = getValue(MI.getRawSource());
6694 SDValue Length = getValue(MI.getLength());
6695
6696 Type *LengthTy = MI.getLength()->getType();
6697 unsigned ElemSz = MI.getElementSizeInBytes();
6698 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6699 SDValue MC =
6700 DAG.getAtomicMemmove(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6701 isTC, MachinePointerInfo(MI.getRawDest()),
6702 MachinePointerInfo(MI.getRawSource()));
6703 updateDAGForMaybeTailCall(MC);
6704 return;
6705 }
6706 case Intrinsic::memset_element_unordered_atomic: {
6707 auto &MI = cast<AnyMemSetInst>(I);
6708 SDValue Dst = getValue(MI.getRawDest());
6709 SDValue Val = getValue(MI.getValue());
6710 SDValue Length = getValue(MI.getLength());
6711
6712 Type *LengthTy = MI.getLength()->getType();
6713 unsigned ElemSz = MI.getElementSizeInBytes();
6714 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6715 SDValue MC =
6716 DAG.getAtomicMemset(getRoot(), sdl, Dst, Val, Length, LengthTy, ElemSz,
6717 isTC, MachinePointerInfo(MI.getRawDest()));
6718 updateDAGForMaybeTailCall(MC);
6719 return;
6720 }
6721 case Intrinsic::call_preallocated_setup: {
6722 const CallBase *PreallocatedCall = FindPreallocatedCall(&I);
6723 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6724 SDValue Res = DAG.getNode(ISD::PREALLOCATED_SETUP, sdl, MVT::Other,
6725 getRoot(), SrcValue);
6726 setValue(&I, Res);
6727 DAG.setRoot(Res);
6728 return;
6729 }
6730 case Intrinsic::call_preallocated_arg: {
6731 const CallBase *PreallocatedCall = FindPreallocatedCall(I.getOperand(0));
6732 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6733 SDValue Ops[3];
6734 Ops[0] = getRoot();
6735 Ops[1] = SrcValue;
6736 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
6737 MVT::i32); // arg index
6738 SDValue Res = DAG.getNode(
6739 ISD::PREALLOCATED_ARG, sdl,
6740 DAG.getVTList(TLI.getPointerTy(DAG.getDataLayout()), MVT::Other), Ops);
6741 setValue(&I, Res);
6742 DAG.setRoot(Res.getValue(1));
6743 return;
6744 }
6745
6746 case Intrinsic::eh_typeid_for: {
6747 // Find the type id for the given typeinfo.
6748 GlobalValue *GV = ExtractTypeInfo(I.getArgOperand(0));
6749 unsigned TypeID = DAG.getMachineFunction().getTypeIDFor(GV);
6750 Res = DAG.getConstant(TypeID, sdl, MVT::i32);
6751 setValue(&I, Res);
6752 return;
6753 }
6754
6755 case Intrinsic::eh_return_i32:
6756 case Intrinsic::eh_return_i64:
6757 DAG.getMachineFunction().setCallsEHReturn(true);
6758 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl,
6759 MVT::Other,
6761 getValue(I.getArgOperand(0)),
6762 getValue(I.getArgOperand(1))));
6763 return;
6764 case Intrinsic::eh_unwind_init:
6765 DAG.getMachineFunction().setCallsUnwindInit(true);
6766 return;
6767 case Intrinsic::eh_dwarf_cfa:
6768 setValue(&I, DAG.getNode(ISD::EH_DWARF_CFA, sdl,
6769 TLI.getPointerTy(DAG.getDataLayout()),
6770 getValue(I.getArgOperand(0))));
6771 return;
6772 case Intrinsic::eh_sjlj_callsite: {
6773 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(0));
6774 assert(FuncInfo.getCurrentCallSite() == 0 && "Overlapping call sites!");
6775
6776 FuncInfo.setCurrentCallSite(CI->getZExtValue());
6777 return;
6778 }
6779 case Intrinsic::eh_sjlj_functioncontext: {
6780 // Get and store the index of the function context.
6781 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
6782 AllocaInst *FnCtx =
6783 cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
6784 int FI = FuncInfo.StaticAllocaMap[FnCtx];
6786 return;
6787 }
6788 case Intrinsic::eh_sjlj_setjmp: {
6789 SDValue Ops[2];
6790 Ops[0] = getRoot();
6791 Ops[1] = getValue(I.getArgOperand(0));
6792 SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl,
6793 DAG.getVTList(MVT::i32, MVT::Other), Ops);
6794 setValue(&I, Op.getValue(0));
6795 DAG.setRoot(Op.getValue(1));
6796 return;
6797 }
6798 case Intrinsic::eh_sjlj_longjmp:
6799 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other,
6800 getRoot(), getValue(I.getArgOperand(0))));
6801 return;
6802 case Intrinsic::eh_sjlj_setup_dispatch:
6803 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_SETUP_DISPATCH, sdl, MVT::Other,
6804 getRoot()));
6805 return;
6806 case Intrinsic::masked_gather:
6807 visitMaskedGather(I);
6808 return;
6809 case Intrinsic::masked_load:
6810 visitMaskedLoad(I);
6811 return;
6812 case Intrinsic::masked_scatter:
6813 visitMaskedScatter(I);
6814 return;
6815 case Intrinsic::masked_store:
6816 visitMaskedStore(I);
6817 return;
6818 case Intrinsic::masked_expandload:
6819 visitMaskedLoad(I, true /* IsExpanding */);
6820 return;
6821 case Intrinsic::masked_compressstore:
6822 visitMaskedStore(I, true /* IsCompressing */);
6823 return;
6824 case Intrinsic::powi:
6825 setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)),
6826 getValue(I.getArgOperand(1)), DAG));
6827 return;
6828 case Intrinsic::log:
6829 setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6830 return;
6831 case Intrinsic::log2:
6832 setValue(&I,
6833 expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6834 return;
6835 case Intrinsic::log10:
6836 setValue(&I,
6837 expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6838 return;
6839 case Intrinsic::exp:
6840 setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6841 return;
6842 case Intrinsic::exp2:
6843 setValue(&I,
6844 expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6845 return;
6846 case Intrinsic::pow:
6847 setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)),
6848 getValue(I.getArgOperand(1)), DAG, TLI, Flags));
6849 return;
6850 case Intrinsic::sqrt:
6851 case Intrinsic::fabs:
6852 case Intrinsic::sin:
6853 case Intrinsic::cos:
6854 case Intrinsic::tan:
6855 case Intrinsic::asin:
6856 case Intrinsic::acos:
6857 case Intrinsic::atan:
6858 case Intrinsic::sinh:
6859 case Intrinsic::cosh:
6860 case Intrinsic::tanh:
6861 case Intrinsic::exp10:
6862 case Intrinsic::floor:
6863 case Intrinsic::ceil:
6864 case Intrinsic::trunc:
6865 case Intrinsic::rint:
6866 case Intrinsic::nearbyint:
6867 case Intrinsic::round:
6868 case Intrinsic::roundeven:
6869 case Intrinsic::canonicalize: {
6870 unsigned Opcode;
6871 // clang-format off
6872 switch (Intrinsic) {
6873 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
6874 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
6875 case Intrinsic::fabs: Opcode = ISD::FABS; break;
6876 case Intrinsic::sin: Opcode = ISD::FSIN; break;
6877 case Intrinsic::cos: Opcode = ISD::FCOS; break;
6878 case Intrinsic::tan: Opcode = ISD::FTAN; break;
6879 case Intrinsic::asin: Opcode = ISD::FASIN; break;
6880 case Intrinsic::acos: Opcode = ISD::FACOS; break;
6881 case Intrinsic::atan: Opcode = ISD::FATAN; break;
6882 case Intrinsic::sinh: Opcode = ISD::FSINH; break;
6883 case Intrinsic::cosh: Opcode = ISD::FCOSH; break;
6884 case Intrinsic::tanh: Opcode = ISD::FTANH; break;
6885 case Intrinsic::exp10: Opcode = ISD::FEXP10; break;
6886 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
6887 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
6888 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
6889 case Intrinsic::rint: Opcode = ISD::FRINT; break;
6890 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
6891 case Intrinsic::round: Opcode = ISD::FROUND; break;
6892 case Intrinsic::roundeven: Opcode = ISD::FROUNDEVEN; break;
6893 case Intrinsic::canonicalize: Opcode = ISD::FCANONICALIZE; break;
6894 }
6895 // clang-format on
6896
6897 setValue(&I, DAG.getNode(Opcode, sdl,
6898 getValue(I.getArgOperand(0)).getValueType(),
6899 getValue(I.getArgOperand(0)), Flags));
6900 return;
6901 }
6902 case Intrinsic::atan2:
6903 setValue(&I, DAG.getNode(ISD::FATAN2, sdl,
6904 getValue(I.getArgOperand(0)).getValueType(),
6905 getValue(I.getArgOperand(0)),
6906 getValue(I.getArgOperand(1)), Flags));
6907 return;
6908 case Intrinsic::lround:
6909 case Intrinsic::llround:
6910 case Intrinsic::lrint:
6911 case Intrinsic::llrint: {
6912 unsigned Opcode;
6913 // clang-format off
6914 switch (Intrinsic) {
6915 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
6916 case Intrinsic::lround: Opcode = ISD::LROUND; break;
6917 case Intrinsic::llround: Opcode = ISD::LLROUND; break;
6918 case Intrinsic::lrint: Opcode = ISD::LRINT; break;
6919 case Intrinsic::llrint: Opcode = ISD::LLRINT; break;
6920 }
6921 // clang-format on
6922
6923 EVT RetVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6924 setValue(&I, DAG.getNode(Opcode, sdl, RetVT,
6925 getValue(I.getArgOperand(0))));
6926 return;
6927 }
6928 case Intrinsic::minnum:
6929 setValue(&I, DAG.getNode(ISD::FMINNUM, sdl,
6930 getValue(I.getArgOperand(0)).getValueType(),
6931 getValue(I.getArgOperand(0)),
6932 getValue(I.getArgOperand(1)), Flags));
6933 return;
6934 case Intrinsic::maxnum:
6935 setValue(&I, DAG.getNode(ISD::FMAXNUM, sdl,
6936 getValue(I.getArgOperand(0)).getValueType(),
6937 getValue(I.getArgOperand(0)),
6938 getValue(I.getArgOperand(1)), Flags));
6939 return;
6940 case Intrinsic::minimum:
6941 setValue(&I, DAG.getNode(ISD::FMINIMUM, sdl,
6942 getValue(I.getArgOperand(0)).getValueType(),
6943 getValue(I.getArgOperand(0)),
6944 getValue(I.getArgOperand(1)), Flags));
6945 return;
6946 case Intrinsic::maximum:
6947 setValue(&I, DAG.getNode(ISD::FMAXIMUM, sdl,
6948 getValue(I.getArgOperand(0)).getValueType(),
6949 getValue(I.getArgOperand(0)),
6950 getValue(I.getArgOperand(1)), Flags));
6951 return;
6952 case Intrinsic::minimumnum:
6953 setValue(&I, DAG.getNode(ISD::FMINIMUMNUM, sdl,
6954 getValue(I.getArgOperand(0)).getValueType(),
6955 getValue(I.getArgOperand(0)),
6956 getValue(I.getArgOperand(1)), Flags));
6957 return;
6958 case Intrinsic::maximumnum:
6959 setValue(&I, DAG.getNode(ISD::FMAXIMUMNUM, sdl,
6960 getValue(I.getArgOperand(0)).getValueType(),
6961 getValue(I.getArgOperand(0)),
6962 getValue(I.getArgOperand(1)), Flags));
6963 return;
6964 case Intrinsic::copysign:
6965 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, sdl,
6966 getValue(I.getArgOperand(0)).getValueType(),
6967 getValue(I.getArgOperand(0)),
6968 getValue(I.getArgOperand(1)), Flags));
6969 return;
6970 case Intrinsic::ldexp:
6971 setValue(&I, DAG.getNode(ISD::FLDEXP, sdl,
6972 getValue(I.getArgOperand(0)).getValueType(),
6973 getValue(I.getArgOperand(0)),
6974 getValue(I.getArgOperand(1)), Flags));
6975 return;
6976 case Intrinsic::modf:
6977 case Intrinsic::sincos:
6978 case Intrinsic::sincospi:
6979 case Intrinsic::frexp: {
6980 unsigned Opcode;
6981 switch (Intrinsic) {
6982 default:
6983 llvm_unreachable("unexpected intrinsic");
6984 case Intrinsic::sincos:
6985 Opcode = ISD::FSINCOS;
6986 break;
6987 case Intrinsic::sincospi:
6988 Opcode = ISD::FSINCOSPI;
6989 break;
6990 case Intrinsic::modf:
6991 Opcode = ISD::FMODF;
6992 break;
6993 case Intrinsic::frexp:
6994 Opcode = ISD::FFREXP;
6995 break;
6996 }
6997 SmallVector<EVT, 2> ValueVTs;
6998 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
6999 SDVTList VTs = DAG.getVTList(ValueVTs);
7000 setValue(
7001 &I, DAG.getNode(Opcode, sdl, VTs, getValue(I.getArgOperand(0)), Flags));
7002 return;
7003 }
7004 case Intrinsic::arithmetic_fence: {
7005 setValue(&I, DAG.getNode(ISD::ARITH_FENCE, sdl,
7006 getValue(I.getArgOperand(0)).getValueType(),
7007 getValue(I.getArgOperand(0)), Flags));
7008 return;
7009 }
7010 case Intrinsic::fma:
7011 setValue(&I, DAG.getNode(
7012 ISD::FMA, sdl, getValue(I.getArgOperand(0)).getValueType(),
7013 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)),
7014 getValue(I.getArgOperand(2)), Flags));
7015 return;
7016#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
7017 case Intrinsic::INTRINSIC:
7018#include "llvm/IR/ConstrainedOps.def"
7019 visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(I));
7020 return;
7021#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
7022#include "llvm/IR/VPIntrinsics.def"
7023 visitVectorPredicationIntrinsic(cast<VPIntrinsic>(I));
7024 return;
7025 case Intrinsic::fptrunc_round: {
7026 // Get the last argument, the metadata and convert it to an integer in the
7027 // call
7028 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(1))->getMetadata();
7029 std::optional<RoundingMode> RoundMode =
7030 convertStrToRoundingMode(cast<MDString>(MD)->getString());
7031
7032 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7033
7034 // Propagate fast-math-flags from IR to node(s).
7035 SDNodeFlags Flags;
7036 Flags.copyFMF(*cast<FPMathOperator>(&I));
7037 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
7038
7040 Result = DAG.getNode(
7041 ISD::FPTRUNC_ROUND, sdl, VT, getValue(I.getArgOperand(0)),
7042 DAG.getTargetConstant((int)*RoundMode, sdl, MVT::i32));
7043 setValue(&I, Result);
7044
7045 return;
7046 }
7047 case Intrinsic::fmuladd: {
7048 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7049 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
7050 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
7051 setValue(&I, DAG.getNode(ISD::FMA, sdl,
7052 getValue(I.getArgOperand(0)).getValueType(),
7053 getValue(I.getArgOperand(0)),
7054 getValue(I.getArgOperand(1)),
7055 getValue(I.getArgOperand(2)), Flags));
7056 } else if (TLI.isOperationLegalOrCustom(ISD::FMULADD, VT)) {
7057 // TODO: Support splitting the vector.
7058 setValue(&I, DAG.getNode(ISD::FMULADD, sdl,
7059 getValue(I.getArgOperand(0)).getValueType(),
7060 getValue(I.getArgOperand(0)),
7061 getValue(I.getArgOperand(1)),
7062 getValue(I.getArgOperand(2)), Flags));
7063 } else {
7064 // TODO: Intrinsic calls should have fast-math-flags.
7065 SDValue Mul = DAG.getNode(
7066 ISD::FMUL, sdl, getValue(I.getArgOperand(0)).getValueType(),
7067 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)), Flags);
7068 SDValue Add = DAG.getNode(ISD::FADD, sdl,
7069 getValue(I.getArgOperand(0)).getValueType(),
7070 Mul, getValue(I.getArgOperand(2)), Flags);
7071 setValue(&I, Add);
7072 }
7073 return;
7074 }
7075 case Intrinsic::convert_to_fp16:
7076 setValue(&I, DAG.getNode(ISD::BITCAST, sdl, MVT::i16,
7077 DAG.getNode(ISD::FP_ROUND, sdl, MVT::f16,
7078 getValue(I.getArgOperand(0)),
7079 DAG.getTargetConstant(0, sdl,
7080 MVT::i32))));
7081 return;
7082 case Intrinsic::convert_from_fp16:
7083 setValue(&I, DAG.getNode(ISD::FP_EXTEND, sdl,
7084 TLI.getValueType(DAG.getDataLayout(), I.getType()),
7085 DAG.getNode(ISD::BITCAST, sdl, MVT::f16,
7086 getValue(I.getArgOperand(0)))));
7087 return;
7088 case Intrinsic::fptosi_sat: {
7089 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7090 setValue(&I, DAG.getNode(ISD::FP_TO_SINT_SAT, sdl, VT,
7091 getValue(I.getArgOperand(0)),
7092 DAG.getValueType(VT.getScalarType())));
7093 return;
7094 }
7095 case Intrinsic::fptoui_sat: {
7096 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7097 setValue(&I, DAG.getNode(ISD::FP_TO_UINT_SAT, sdl, VT,
7098 getValue(I.getArgOperand(0)),
7099 DAG.getValueType(VT.getScalarType())));
7100 return;
7101 }
7102 case Intrinsic::set_rounding:
7103 Res = DAG.getNode(ISD::SET_ROUNDING, sdl, MVT::Other,
7104 {getRoot(), getValue(I.getArgOperand(0))});
7105 setValue(&I, Res);
7106 DAG.setRoot(Res.getValue(0));
7107 return;
7108 case Intrinsic::is_fpclass: {
7109 const DataLayout DLayout = DAG.getDataLayout();
7110 EVT DestVT = TLI.getValueType(DLayout, I.getType());
7111 EVT ArgVT = TLI.getValueType(DLayout, I.getArgOperand(0)->getType());
7112 FPClassTest Test = static_cast<FPClassTest>(
7113 cast<ConstantInt>(I.getArgOperand(1))->getZExtValue());
7114 MachineFunction &MF = DAG.getMachineFunction();
7115 const Function &F = MF.getFunction();
7116 SDValue Op = getValue(I.getArgOperand(0));
7117 SDNodeFlags Flags;
7118 Flags.setNoFPExcept(
7119 !F.getAttributes().hasFnAttr(llvm::Attribute::StrictFP));
7120 // If ISD::IS_FPCLASS should be expanded, do it right now, because the
7121 // expansion can use illegal types. Making expansion early allows
7122 // legalizing these types prior to selection.
7123 if (!TLI.isOperationLegal(ISD::IS_FPCLASS, ArgVT) &&
7124 !TLI.isOperationCustom(ISD::IS_FPCLASS, ArgVT)) {
7125 SDValue Result = TLI.expandIS_FPCLASS(DestVT, Op, Test, Flags, sdl, DAG);
7126 setValue(&I, Result);
7127 return;
7128 }
7129
7130 SDValue Check = DAG.getTargetConstant(Test, sdl, MVT::i32);
7131 SDValue V = DAG.getNode(ISD::IS_FPCLASS, sdl, DestVT, {Op, Check}, Flags);
7132 setValue(&I, V);
7133 return;
7134 }
7135 case Intrinsic::get_fpenv: {
7136 const DataLayout DLayout = DAG.getDataLayout();
7137 EVT EnvVT = TLI.getValueType(DLayout, I.getType());
7138 Align TempAlign = DAG.getEVTAlign(EnvVT);
7139 SDValue Chain = getRoot();
7140 // Use GET_FPENV if it is legal or custom. Otherwise use memory-based node
7141 // and temporary storage in stack.
7142 if (TLI.isOperationLegalOrCustom(ISD::GET_FPENV, EnvVT)) {
7143 Res = DAG.getNode(
7144 ISD::GET_FPENV, sdl,
7145 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7146 MVT::Other),
7147 Chain);
7148 } else {
7149 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7150 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7151 auto MPI =
7152 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7153 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7155 TempAlign);
7156 Chain = DAG.getGetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7157 Res = DAG.getLoad(EnvVT, sdl, Chain, Temp, MPI);
7158 }
7159 setValue(&I, Res);
7160 DAG.setRoot(Res.getValue(1));
7161 return;
7162 }
7163 case Intrinsic::set_fpenv: {
7164 const DataLayout DLayout = DAG.getDataLayout();
7165 SDValue Env = getValue(I.getArgOperand(0));
7166 EVT EnvVT = Env.getValueType();
7167 Align TempAlign = DAG.getEVTAlign(EnvVT);
7168 SDValue Chain = getRoot();
7169 // If SET_FPENV is custom or legal, use it. Otherwise use loading
7170 // environment from memory.
7171 if (TLI.isOperationLegalOrCustom(ISD::SET_FPENV, EnvVT)) {
7172 Chain = DAG.getNode(ISD::SET_FPENV, sdl, MVT::Other, Chain, Env);
7173 } else {
7174 // Allocate space in stack, copy environment bits into it and use this
7175 // memory in SET_FPENV_MEM.
7176 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7177 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7178 auto MPI =
7179 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7180 Chain = DAG.getStore(Chain, sdl, Env, Temp, MPI, TempAlign,
7182 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7184 TempAlign);
7185 Chain = DAG.getSetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7186 }
7187 DAG.setRoot(Chain);
7188 return;
7189 }
7190 case Intrinsic::reset_fpenv:
7191 DAG.setRoot(DAG.getNode(ISD::RESET_FPENV, sdl, MVT::Other, getRoot()));
7192 return;
7193 case Intrinsic::get_fpmode:
7194 Res = DAG.getNode(
7195 ISD::GET_FPMODE, sdl,
7196 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7197 MVT::Other),
7198 DAG.getRoot());
7199 setValue(&I, Res);
7200 DAG.setRoot(Res.getValue(1));
7201 return;
7202 case Intrinsic::set_fpmode:
7203 Res = DAG.getNode(ISD::SET_FPMODE, sdl, MVT::Other, {DAG.getRoot()},
7204 getValue(I.getArgOperand(0)));
7205 DAG.setRoot(Res);
7206 return;
7207 case Intrinsic::reset_fpmode: {
7208 Res = DAG.getNode(ISD::RESET_FPMODE, sdl, MVT::Other, getRoot());
7209 DAG.setRoot(Res);
7210 return;
7211 }
7212 case Intrinsic::pcmarker: {
7213 SDValue Tmp = getValue(I.getArgOperand(0));
7214 DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp));
7215 return;
7216 }
7217 case Intrinsic::readcyclecounter: {
7218 SDValue Op = getRoot();
7219 Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl,
7220 DAG.getVTList(MVT::i64, MVT::Other), Op);
7221 setValue(&I, Res);
7222 DAG.setRoot(Res.getValue(1));
7223 return;
7224 }
7225 case Intrinsic::readsteadycounter: {
7226 SDValue Op = getRoot();
7227 Res = DAG.getNode(ISD::READSTEADYCOUNTER, sdl,
7228 DAG.getVTList(MVT::i64, MVT::Other), Op);
7229 setValue(&I, Res);
7230 DAG.setRoot(Res.getValue(1));
7231 return;
7232 }
7233 case Intrinsic::bitreverse:
7234 setValue(&I, DAG.getNode(ISD::BITREVERSE, sdl,
7235 getValue(I.getArgOperand(0)).getValueType(),
7236 getValue(I.getArgOperand(0))));
7237 return;
7238 case Intrinsic::bswap:
7239 setValue(&I, DAG.getNode(ISD::BSWAP, sdl,
7240 getValue(I.getArgOperand(0)).getValueType(),
7241 getValue(I.getArgOperand(0))));
7242 return;
7243 case Intrinsic::cttz: {
7244 SDValue Arg = getValue(I.getArgOperand(0));
7245 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7246 EVT Ty = Arg.getValueType();
7247 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF,
7248 sdl, Ty, Arg));
7249 return;
7250 }
7251 case Intrinsic::ctlz: {
7252 SDValue Arg = getValue(I.getArgOperand(0));
7253 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7254 EVT Ty = Arg.getValueType();
7255 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF,
7256 sdl, Ty, Arg));
7257 return;
7258 }
7259 case Intrinsic::ctpop: {
7260 SDValue Arg = getValue(I.getArgOperand(0));
7261 EVT Ty = Arg.getValueType();
7262 setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg));
7263 return;
7264 }
7265 case Intrinsic::fshl:
7266 case Intrinsic::fshr: {
7267 bool IsFSHL = Intrinsic == Intrinsic::fshl;
7268 SDValue X = getValue(I.getArgOperand(0));
7269 SDValue Y = getValue(I.getArgOperand(1));
7270 SDValue Z = getValue(I.getArgOperand(2));
7271 EVT VT = X.getValueType();
7272
7273 if (X == Y) {
7274 auto RotateOpcode = IsFSHL ? ISD::ROTL : ISD::ROTR;
7275 setValue(&I, DAG.getNode(RotateOpcode, sdl, VT, X, Z));
7276 } else {
7277 auto FunnelOpcode = IsFSHL ? ISD::FSHL : ISD::FSHR;
7278 setValue(&I, DAG.getNode(FunnelOpcode, sdl, VT, X, Y, Z));
7279 }
7280 return;
7281 }
7282 case Intrinsic::sadd_sat: {
7283 SDValue Op1 = getValue(I.getArgOperand(0));
7284 SDValue Op2 = getValue(I.getArgOperand(1));
7285 setValue(&I, DAG.getNode(ISD::SADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7286 return;
7287 }
7288 case Intrinsic::uadd_sat: {
7289 SDValue Op1 = getValue(I.getArgOperand(0));
7290 SDValue Op2 = getValue(I.getArgOperand(1));
7291 setValue(&I, DAG.getNode(ISD::UADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7292 return;
7293 }
7294 case Intrinsic::ssub_sat: {
7295 SDValue Op1 = getValue(I.getArgOperand(0));
7296 SDValue Op2 = getValue(I.getArgOperand(1));
7297 setValue(&I, DAG.getNode(ISD::SSUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7298 return;
7299 }
7300 case Intrinsic::usub_sat: {
7301 SDValue Op1 = getValue(I.getArgOperand(0));
7302 SDValue Op2 = getValue(I.getArgOperand(1));
7303 setValue(&I, DAG.getNode(ISD::USUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7304 return;
7305 }
7306 case Intrinsic::sshl_sat: {
7307 SDValue Op1 = getValue(I.getArgOperand(0));
7308 SDValue Op2 = getValue(I.getArgOperand(1));
7309 setValue(&I, DAG.getNode(ISD::SSHLSAT, sdl, Op1.getValueType(), Op1, Op2));
7310 return;
7311 }
7312 case Intrinsic::ushl_sat: {
7313 SDValue Op1 = getValue(I.getArgOperand(0));
7314 SDValue Op2 = getValue(I.getArgOperand(1));
7315 setValue(&I, DAG.getNode(ISD::USHLSAT, sdl, Op1.getValueType(), Op1, Op2));
7316 return;
7317 }
7318 case Intrinsic::smul_fix:
7319 case Intrinsic::umul_fix:
7320 case Intrinsic::smul_fix_sat:
7321 case Intrinsic::umul_fix_sat: {
7322 SDValue Op1 = getValue(I.getArgOperand(0));
7323 SDValue Op2 = getValue(I.getArgOperand(1));
7324 SDValue Op3 = getValue(I.getArgOperand(2));
7325 setValue(&I, DAG.getNode(FixedPointIntrinsicToOpcode(Intrinsic), sdl,
7326 Op1.getValueType(), Op1, Op2, Op3));
7327 return;
7328 }
7329 case Intrinsic::sdiv_fix:
7330 case Intrinsic::udiv_fix:
7331 case Intrinsic::sdiv_fix_sat:
7332 case Intrinsic::udiv_fix_sat: {
7333 SDValue Op1 = getValue(I.getArgOperand(0));
7334 SDValue Op2 = getValue(I.getArgOperand(1));
7335 SDValue Op3 = getValue(I.getArgOperand(2));
7337 Op1, Op2, Op3, DAG, TLI));
7338 return;
7339 }
7340 case Intrinsic::smax: {
7341 SDValue Op1 = getValue(I.getArgOperand(0));
7342 SDValue Op2 = getValue(I.getArgOperand(1));
7343 setValue(&I, DAG.getNode(ISD::SMAX, sdl, Op1.getValueType(), Op1, Op2));
7344 return;
7345 }
7346 case Intrinsic::smin: {
7347 SDValue Op1 = getValue(I.getArgOperand(0));
7348 SDValue Op2 = getValue(I.getArgOperand(1));
7349 setValue(&I, DAG.getNode(ISD::SMIN, sdl, Op1.getValueType(), Op1, Op2));
7350 return;
7351 }
7352 case Intrinsic::umax: {
7353 SDValue Op1 = getValue(I.getArgOperand(0));
7354 SDValue Op2 = getValue(I.getArgOperand(1));
7355 setValue(&I, DAG.getNode(ISD::UMAX, sdl, Op1.getValueType(), Op1, Op2));
7356 return;
7357 }
7358 case Intrinsic::umin: {
7359 SDValue Op1 = getValue(I.getArgOperand(0));
7360 SDValue Op2 = getValue(I.getArgOperand(1));
7361 setValue(&I, DAG.getNode(ISD::UMIN, sdl, Op1.getValueType(), Op1, Op2));
7362 return;
7363 }
7364 case Intrinsic::abs: {
7365 // TODO: Preserve "int min is poison" arg in SDAG?
7366 SDValue Op1 = getValue(I.getArgOperand(0));
7367 setValue(&I, DAG.getNode(ISD::ABS, sdl, Op1.getValueType(), Op1));
7368 return;
7369 }
7370 case Intrinsic::scmp: {
7371 SDValue Op1 = getValue(I.getArgOperand(0));
7372 SDValue Op2 = getValue(I.getArgOperand(1));
7373 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7374 setValue(&I, DAG.getNode(ISD::SCMP, sdl, DestVT, Op1, Op2));
7375 break;
7376 }
7377 case Intrinsic::ucmp: {
7378 SDValue Op1 = getValue(I.getArgOperand(0));
7379 SDValue Op2 = getValue(I.getArgOperand(1));
7380 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7381 setValue(&I, DAG.getNode(ISD::UCMP, sdl, DestVT, Op1, Op2));
7382 break;
7383 }
7384 case Intrinsic::stacksave: {
7385 SDValue Op = getRoot();
7386 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7387 Res = DAG.getNode(ISD::STACKSAVE, sdl, DAG.getVTList(VT, MVT::Other), Op);
7388 setValue(&I, Res);
7389 DAG.setRoot(Res.getValue(1));
7390 return;
7391 }
7392 case Intrinsic::stackrestore:
7393 Res = getValue(I.getArgOperand(0));
7394 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res));
7395 return;
7396 case Intrinsic::get_dynamic_area_offset: {
7397 SDValue Op = getRoot();
7398 EVT ResTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7399 Res = DAG.getNode(ISD::GET_DYNAMIC_AREA_OFFSET, sdl, DAG.getVTList(ResTy),
7400 Op);
7401 DAG.setRoot(Op);
7402 setValue(&I, Res);
7403 return;
7404 }
7405 case Intrinsic::stackguard: {
7406 MachineFunction &MF = DAG.getMachineFunction();
7407 const Module &M = *MF.getFunction().getParent();
7408 EVT PtrTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7409 SDValue Chain = getRoot();
7410 if (TLI.useLoadStackGuardNode(M)) {
7411 Res = getLoadStackGuard(DAG, sdl, Chain);
7412 Res = DAG.getPtrExtOrTrunc(Res, sdl, PtrTy);
7413 } else {
7414 const Value *Global = TLI.getSDagStackGuard(M);
7415 if (!Global) {
7416 LLVMContext &Ctx = *DAG.getContext();
7417 Ctx.diagnose(DiagnosticInfoGeneric("unable to lower stackguard"));
7418 setValue(&I, DAG.getPOISON(PtrTy));
7419 return;
7420 }
7421
7422 Align Align = DAG.getDataLayout().getPrefTypeAlign(Global->getType());
7423 Res = DAG.getLoad(PtrTy, sdl, Chain, getValue(Global),
7424 MachinePointerInfo(Global, 0), Align,
7426 }
7427 if (TLI.useStackGuardXorFP())
7428 Res = TLI.emitStackGuardXorFP(DAG, Res, sdl);
7429 DAG.setRoot(Chain);
7430 setValue(&I, Res);
7431 return;
7432 }
7433 case Intrinsic::stackprotector: {
7434 // Emit code into the DAG to store the stack guard onto the stack.
7435 MachineFunction &MF = DAG.getMachineFunction();
7436 MachineFrameInfo &MFI = MF.getFrameInfo();
7437 const Module &M = *MF.getFunction().getParent();
7438 SDValue Src, Chain = getRoot();
7439
7440 if (TLI.useLoadStackGuardNode(M))
7441 Src = getLoadStackGuard(DAG, sdl, Chain);
7442 else
7443 Src = getValue(I.getArgOperand(0)); // The guard's value.
7444
7445 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
7446
7447 int FI = FuncInfo.StaticAllocaMap[Slot];
7448 MFI.setStackProtectorIndex(FI);
7449 EVT PtrTy = TLI.getFrameIndexTy(DAG.getDataLayout());
7450
7451 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
7452
7453 // Store the stack protector onto the stack.
7454 Res = DAG.getStore(
7455 Chain, sdl, Src, FIN,
7456 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
7457 MaybeAlign(), MachineMemOperand::MOVolatile);
7458 setValue(&I, Res);
7459 DAG.setRoot(Res);
7460 return;
7461 }
7462 case Intrinsic::objectsize:
7463 llvm_unreachable("llvm.objectsize.* should have been lowered already");
7464
7465 case Intrinsic::is_constant:
7466 llvm_unreachable("llvm.is.constant.* should have been lowered already");
7467
7468 case Intrinsic::annotation:
7469 case Intrinsic::ptr_annotation:
7470 case Intrinsic::launder_invariant_group:
7471 case Intrinsic::strip_invariant_group:
7472 // Drop the intrinsic, but forward the value
7473 setValue(&I, getValue(I.getOperand(0)));
7474 return;
7475
7476 case Intrinsic::type_test:
7477 case Intrinsic::public_type_test:
7478 setValue(&I, getValue(ConstantInt::getTrue(I.getType())));
7479 return;
7480
7481 case Intrinsic::assume:
7482 case Intrinsic::experimental_noalias_scope_decl:
7483 case Intrinsic::var_annotation:
7484 case Intrinsic::sideeffect:
7485 // Discard annotate attributes, noalias scope declarations, assumptions, and
7486 // artificial side-effects.
7487 return;
7488
7489 case Intrinsic::codeview_annotation: {
7490 // Emit a label associated with this metadata.
7491 MachineFunction &MF = DAG.getMachineFunction();
7492 MCSymbol *Label = MF.getContext().createTempSymbol("annotation", true);
7493 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(0))->getMetadata();
7494 MF.addCodeViewAnnotation(Label, cast<MDNode>(MD));
7495 Res = DAG.getLabelNode(ISD::ANNOTATION_LABEL, sdl, getRoot(), Label);
7496 DAG.setRoot(Res);
7497 return;
7498 }
7499
7500 case Intrinsic::init_trampoline: {
7501 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
7502
7503 SDValue Ops[6];
7504 Ops[0] = getRoot();
7505 Ops[1] = getValue(I.getArgOperand(0));
7506 Ops[2] = getValue(I.getArgOperand(1));
7507 Ops[3] = getValue(I.getArgOperand(2));
7508 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
7509 Ops[5] = DAG.getSrcValue(F);
7510
7511 Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops);
7512
7513 DAG.setRoot(Res);
7514 return;
7515 }
7516 case Intrinsic::adjust_trampoline:
7517 setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl,
7518 TLI.getPointerTy(DAG.getDataLayout()),
7519 getValue(I.getArgOperand(0))));
7520 return;
7521 case Intrinsic::gcroot: {
7522 assert(DAG.getMachineFunction().getFunction().hasGC() &&
7523 "only valid in functions with gc specified, enforced by Verifier");
7524 assert(GFI && "implied by previous");
7525 const Value *Alloca = I.getArgOperand(0)->stripPointerCasts();
7526 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
7527
7528 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
7529 GFI->addStackRoot(FI->getIndex(), TypeMap);
7530 return;
7531 }
7532 case Intrinsic::gcread:
7533 case Intrinsic::gcwrite:
7534 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
7535 case Intrinsic::get_rounding:
7536 Res = DAG.getNode(ISD::GET_ROUNDING, sdl, {MVT::i32, MVT::Other}, getRoot());
7537 setValue(&I, Res);
7538 DAG.setRoot(Res.getValue(1));
7539 return;
7540
7541 case Intrinsic::expect:
7542 case Intrinsic::expect_with_probability:
7543 // Just replace __builtin_expect(exp, c) and
7544 // __builtin_expect_with_probability(exp, c, p) with EXP.
7545 setValue(&I, getValue(I.getArgOperand(0)));
7546 return;
7547
7548 case Intrinsic::ubsantrap:
7549 case Intrinsic::debugtrap:
7550 case Intrinsic::trap: {
7551 StringRef TrapFuncName =
7552 I.getAttributes().getFnAttr("trap-func-name").getValueAsString();
7553 if (TrapFuncName.empty()) {
7554 switch (Intrinsic) {
7555 case Intrinsic::trap:
7556 DAG.setRoot(DAG.getNode(ISD::TRAP, sdl, MVT::Other, getRoot()));
7557 break;
7558 case Intrinsic::debugtrap:
7559 DAG.setRoot(DAG.getNode(ISD::DEBUGTRAP, sdl, MVT::Other, getRoot()));
7560 break;
7561 case Intrinsic::ubsantrap:
7562 DAG.setRoot(DAG.getNode(
7563 ISD::UBSANTRAP, sdl, MVT::Other, getRoot(),
7564 DAG.getTargetConstant(
7565 cast<ConstantInt>(I.getArgOperand(0))->getZExtValue(), sdl,
7566 MVT::i32)));
7567 break;
7568 default: llvm_unreachable("unknown trap intrinsic");
7569 }
7570 DAG.addNoMergeSiteInfo(DAG.getRoot().getNode(),
7571 I.hasFnAttr(Attribute::NoMerge));
7572 return;
7573 }
7575 if (Intrinsic == Intrinsic::ubsantrap) {
7576 Value *Arg = I.getArgOperand(0);
7577 Args.emplace_back(Arg, getValue(Arg));
7578 }
7579
7580 TargetLowering::CallLoweringInfo CLI(DAG);
7581 CLI.setDebugLoc(sdl).setChain(getRoot()).setLibCallee(
7582 CallingConv::C, I.getType(),
7583 DAG.getExternalSymbol(TrapFuncName.data(),
7584 TLI.getPointerTy(DAG.getDataLayout())),
7585 std::move(Args));
7586 CLI.NoMerge = I.hasFnAttr(Attribute::NoMerge);
7587 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
7588 DAG.setRoot(Result.second);
7589 return;
7590 }
7591
7592 case Intrinsic::allow_runtime_check:
7593 case Intrinsic::allow_ubsan_check:
7594 setValue(&I, getValue(ConstantInt::getTrue(I.getType())));
7595 return;
7596
7597 case Intrinsic::uadd_with_overflow:
7598 case Intrinsic::sadd_with_overflow:
7599 case Intrinsic::usub_with_overflow:
7600 case Intrinsic::ssub_with_overflow:
7601 case Intrinsic::umul_with_overflow:
7602 case Intrinsic::smul_with_overflow: {
7604 switch (Intrinsic) {
7605 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
7606 case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break;
7607 case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break;
7608 case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break;
7609 case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break;
7610 case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break;
7611 case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break;
7612 }
7613 SDValue Op1 = getValue(I.getArgOperand(0));
7614 SDValue Op2 = getValue(I.getArgOperand(1));
7615
7616 EVT ResultVT = Op1.getValueType();
7617 EVT OverflowVT = MVT::i1;
7618 if (ResultVT.isVector())
7619 OverflowVT = EVT::getVectorVT(
7620 *Context, OverflowVT, ResultVT.getVectorElementCount());
7621
7622 SDVTList VTs = DAG.getVTList(ResultVT, OverflowVT);
7623 setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2));
7624 return;
7625 }
7626 case Intrinsic::prefetch: {
7627 SDValue Ops[5];
7628 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7630 Ops[0] = DAG.getRoot();
7631 Ops[1] = getValue(I.getArgOperand(0));
7632 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
7633 MVT::i32);
7634 Ops[3] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(2)), sdl,
7635 MVT::i32);
7636 Ops[4] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(3)), sdl,
7637 MVT::i32);
7638 SDValue Result = DAG.getMemIntrinsicNode(
7639 ISD::PREFETCH, sdl, DAG.getVTList(MVT::Other), Ops,
7640 EVT::getIntegerVT(*Context, 8), MachinePointerInfo(I.getArgOperand(0)),
7641 /* align */ std::nullopt, Flags);
7642
7643 // Chain the prefetch in parallel with any pending loads, to stay out of
7644 // the way of later optimizations.
7645 PendingLoads.push_back(Result);
7646 Result = getRoot();
7647 DAG.setRoot(Result);
7648 return;
7649 }
7650 case Intrinsic::lifetime_start:
7651 case Intrinsic::lifetime_end: {
7652 bool IsStart = (Intrinsic == Intrinsic::lifetime_start);
7653 // Stack coloring is not enabled in O0, discard region information.
7654 if (TM.getOptLevel() == CodeGenOptLevel::None)
7655 return;
7656
7657 const AllocaInst *LifetimeObject = dyn_cast<AllocaInst>(I.getArgOperand(0));
7658 if (!LifetimeObject)
7659 return;
7660
7661 // First check that the Alloca is static, otherwise it won't have a
7662 // valid frame index.
7663 auto SI = FuncInfo.StaticAllocaMap.find(LifetimeObject);
7664 if (SI == FuncInfo.StaticAllocaMap.end())
7665 return;
7666
7667 const int FrameIndex = SI->second;
7668 Res = DAG.getLifetimeNode(IsStart, sdl, getRoot(), FrameIndex);
7669 DAG.setRoot(Res);
7670 return;
7671 }
7672 case Intrinsic::pseudoprobe: {
7673 auto Guid = cast<ConstantInt>(I.getArgOperand(0))->getZExtValue();
7674 auto Index = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7675 auto Attr = cast<ConstantInt>(I.getArgOperand(2))->getZExtValue();
7676 Res = DAG.getPseudoProbeNode(sdl, getRoot(), Guid, Index, Attr);
7677 DAG.setRoot(Res);
7678 return;
7679 }
7680 case Intrinsic::invariant_start:
7681 // Discard region information.
7682 setValue(&I,
7683 DAG.getUNDEF(TLI.getValueType(DAG.getDataLayout(), I.getType())));
7684 return;
7685 case Intrinsic::invariant_end:
7686 // Discard region information.
7687 return;
7688 case Intrinsic::clear_cache: {
7689 SDValue InputChain = DAG.getRoot();
7690 SDValue StartVal = getValue(I.getArgOperand(0));
7691 SDValue EndVal = getValue(I.getArgOperand(1));
7692 Res = DAG.getNode(ISD::CLEAR_CACHE, sdl, DAG.getVTList(MVT::Other),
7693 {InputChain, StartVal, EndVal});
7694 setValue(&I, Res);
7695 DAG.setRoot(Res);
7696 return;
7697 }
7698 case Intrinsic::donothing:
7699 case Intrinsic::seh_try_begin:
7700 case Intrinsic::seh_scope_begin:
7701 case Intrinsic::seh_try_end:
7702 case Intrinsic::seh_scope_end:
7703 // ignore
7704 return;
7705 case Intrinsic::experimental_stackmap:
7706 visitStackmap(I);
7707 return;
7708 case Intrinsic::experimental_patchpoint_void:
7709 case Intrinsic::experimental_patchpoint:
7710 visitPatchpoint(I);
7711 return;
7712 case Intrinsic::experimental_gc_statepoint:
7714 return;
7715 case Intrinsic::experimental_gc_result:
7716 visitGCResult(cast<GCResultInst>(I));
7717 return;
7718 case Intrinsic::experimental_gc_relocate:
7719 visitGCRelocate(cast<GCRelocateInst>(I));
7720 return;
7721 case Intrinsic::instrprof_cover:
7722 llvm_unreachable("instrprof failed to lower a cover");
7723 case Intrinsic::instrprof_increment:
7724 llvm_unreachable("instrprof failed to lower an increment");
7725 case Intrinsic::instrprof_timestamp:
7726 llvm_unreachable("instrprof failed to lower a timestamp");
7727 case Intrinsic::instrprof_value_profile:
7728 llvm_unreachable("instrprof failed to lower a value profiling call");
7729 case Intrinsic::instrprof_mcdc_parameters:
7730 llvm_unreachable("instrprof failed to lower mcdc parameters");
7731 case Intrinsic::instrprof_mcdc_tvbitmap_update:
7732 llvm_unreachable("instrprof failed to lower an mcdc tvbitmap update");
7733 case Intrinsic::localescape: {
7734 MachineFunction &MF = DAG.getMachineFunction();
7735 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
7736
7737 // Directly emit some LOCAL_ESCAPE machine instrs. Label assignment emission
7738 // is the same on all targets.
7739 for (unsigned Idx = 0, E = I.arg_size(); Idx < E; ++Idx) {
7740 Value *Arg = I.getArgOperand(Idx)->stripPointerCasts();
7741 if (isa<ConstantPointerNull>(Arg))
7742 continue; // Skip null pointers. They represent a hole in index space.
7743 AllocaInst *Slot = cast<AllocaInst>(Arg);
7744 assert(FuncInfo.StaticAllocaMap.count(Slot) &&
7745 "can only escape static allocas");
7746 int FI = FuncInfo.StaticAllocaMap[Slot];
7747 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
7749 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, dl,
7750 TII->get(TargetOpcode::LOCAL_ESCAPE))
7751 .addSym(FrameAllocSym)
7752 .addFrameIndex(FI);
7753 }
7754
7755 return;
7756 }
7757
7758 case Intrinsic::localrecover: {
7759 // i8* @llvm.localrecover(i8* %fn, i8* %fp, i32 %idx)
7760 MachineFunction &MF = DAG.getMachineFunction();
7761
7762 // Get the symbol that defines the frame offset.
7763 auto *Fn = cast<Function>(I.getArgOperand(0)->stripPointerCasts());
7764 auto *Idx = cast<ConstantInt>(I.getArgOperand(2));
7765 unsigned IdxVal =
7766 unsigned(Idx->getLimitedValue(std::numeric_limits<int>::max()));
7767 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
7769
7770 Value *FP = I.getArgOperand(1);
7771 SDValue FPVal = getValue(FP);
7772 EVT PtrVT = FPVal.getValueType();
7773
7774 // Create a MCSymbol for the label to avoid any target lowering
7775 // that would make this PC relative.
7776 SDValue OffsetSym = DAG.getMCSymbol(FrameAllocSym, PtrVT);
7777 SDValue OffsetVal =
7778 DAG.getNode(ISD::LOCAL_RECOVER, sdl, PtrVT, OffsetSym);
7779
7780 // Add the offset to the FP.
7781 SDValue Add = DAG.getMemBasePlusOffset(FPVal, OffsetVal, sdl);
7782 setValue(&I, Add);
7783
7784 return;
7785 }
7786
7787 case Intrinsic::fake_use: {
7788 Value *V = I.getArgOperand(0);
7789 SDValue Ops[2];
7790 // For Values not declared or previously used in this basic block, the
7791 // NodeMap will not have an entry, and `getValue` will assert if V has no
7792 // valid register value.
7793 auto FakeUseValue = [&]() -> SDValue {
7794 SDValue &N = NodeMap[V];
7795 if (N.getNode())
7796 return N;
7797
7798 // If there's a virtual register allocated and initialized for this
7799 // value, use it.
7800 if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
7801 return copyFromReg;
7802 // FIXME: Do we want to preserve constants? It seems pointless.
7803 if (isa<Constant>(V))
7804 return getValue(V);
7805 return SDValue();
7806 }();
7807 if (!FakeUseValue || FakeUseValue.isUndef())
7808 return;
7809 Ops[0] = getRoot();
7810 Ops[1] = FakeUseValue;
7811 // Also, do not translate a fake use with an undef operand, or any other
7812 // empty SDValues.
7813 if (!Ops[1] || Ops[1].isUndef())
7814 return;
7815 DAG.setRoot(DAG.getNode(ISD::FAKE_USE, sdl, MVT::Other, Ops));
7816 return;
7817 }
7818
7819 case Intrinsic::reloc_none: {
7820 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(0))->getMetadata();
7821 StringRef SymbolName = cast<MDString>(MD)->getString();
7822 SDValue Ops[2] = {
7823 getRoot(),
7824 DAG.getTargetExternalSymbol(
7825 SymbolName.data(), TLI.getProgramPointerTy(DAG.getDataLayout()))};
7826 DAG.setRoot(DAG.getNode(ISD::RELOC_NONE, sdl, MVT::Other, Ops));
7827 return;
7828 }
7829
7830 case Intrinsic::eh_exceptionpointer:
7831 case Intrinsic::eh_exceptioncode: {
7832 // Get the exception pointer vreg, copy from it, and resize it to fit.
7833 const auto *CPI = cast<CatchPadInst>(I.getArgOperand(0));
7834 MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
7835 const TargetRegisterClass *PtrRC = TLI.getRegClassFor(PtrVT);
7836 Register VReg = FuncInfo.getCatchPadExceptionPointerVReg(CPI, PtrRC);
7837 SDValue N = DAG.getCopyFromReg(DAG.getEntryNode(), sdl, VReg, PtrVT);
7838 if (Intrinsic == Intrinsic::eh_exceptioncode)
7839 N = DAG.getZExtOrTrunc(N, sdl, MVT::i32);
7840 setValue(&I, N);
7841 return;
7842 }
7843 case Intrinsic::xray_customevent: {
7844 // Here we want to make sure that the intrinsic behaves as if it has a
7845 // specific calling convention.
7846 const auto &Triple = DAG.getTarget().getTargetTriple();
7847 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64)
7848 return;
7849
7851
7852 // We want to say that we always want the arguments in registers.
7853 SDValue LogEntryVal = getValue(I.getArgOperand(0));
7854 SDValue StrSizeVal = getValue(I.getArgOperand(1));
7855 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
7856 SDValue Chain = getRoot();
7857 Ops.push_back(LogEntryVal);
7858 Ops.push_back(StrSizeVal);
7859 Ops.push_back(Chain);
7860
7861 // We need to enforce the calling convention for the callsite, so that
7862 // argument ordering is enforced correctly, and that register allocation can
7863 // see that some registers may be assumed clobbered and have to preserve
7864 // them across calls to the intrinsic.
7865 MachineSDNode *MN = DAG.getMachineNode(TargetOpcode::PATCHABLE_EVENT_CALL,
7866 sdl, NodeTys, Ops);
7867 SDValue patchableNode = SDValue(MN, 0);
7868 DAG.setRoot(patchableNode);
7869 setValue(&I, patchableNode);
7870 return;
7871 }
7872 case Intrinsic::xray_typedevent: {
7873 // Here we want to make sure that the intrinsic behaves as if it has a
7874 // specific calling convention.
7875 const auto &Triple = DAG.getTarget().getTargetTriple();
7876 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64)
7877 return;
7878
7880
7881 // We want to say that we always want the arguments in registers.
7882 // It's unclear to me how manipulating the selection DAG here forces callers
7883 // to provide arguments in registers instead of on the stack.
7884 SDValue LogTypeId = getValue(I.getArgOperand(0));
7885 SDValue LogEntryVal = getValue(I.getArgOperand(1));
7886 SDValue StrSizeVal = getValue(I.getArgOperand(2));
7887 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
7888 SDValue Chain = getRoot();
7889 Ops.push_back(LogTypeId);
7890 Ops.push_back(LogEntryVal);
7891 Ops.push_back(StrSizeVal);
7892 Ops.push_back(Chain);
7893
7894 // We need to enforce the calling convention for the callsite, so that
7895 // argument ordering is enforced correctly, and that register allocation can
7896 // see that some registers may be assumed clobbered and have to preserve
7897 // them across calls to the intrinsic.
7898 MachineSDNode *MN = DAG.getMachineNode(
7899 TargetOpcode::PATCHABLE_TYPED_EVENT_CALL, sdl, NodeTys, Ops);
7900 SDValue patchableNode = SDValue(MN, 0);
7901 DAG.setRoot(patchableNode);
7902 setValue(&I, patchableNode);
7903 return;
7904 }
7905 case Intrinsic::experimental_deoptimize:
7907 return;
7908 case Intrinsic::stepvector:
7909 visitStepVector(I);
7910 return;
7911 case Intrinsic::vector_reduce_fadd:
7912 case Intrinsic::vector_reduce_fmul:
7913 case Intrinsic::vector_reduce_add:
7914 case Intrinsic::vector_reduce_mul:
7915 case Intrinsic::vector_reduce_and:
7916 case Intrinsic::vector_reduce_or:
7917 case Intrinsic::vector_reduce_xor:
7918 case Intrinsic::vector_reduce_smax:
7919 case Intrinsic::vector_reduce_smin:
7920 case Intrinsic::vector_reduce_umax:
7921 case Intrinsic::vector_reduce_umin:
7922 case Intrinsic::vector_reduce_fmax:
7923 case Intrinsic::vector_reduce_fmin:
7924 case Intrinsic::vector_reduce_fmaximum:
7925 case Intrinsic::vector_reduce_fminimum:
7926 visitVectorReduce(I, Intrinsic);
7927 return;
7928
7929 case Intrinsic::icall_branch_funnel: {
7931 Ops.push_back(getValue(I.getArgOperand(0)));
7932
7933 int64_t Offset;
7935 I.getArgOperand(1), Offset, DAG.getDataLayout()));
7936 if (!Base)
7938 "llvm.icall.branch.funnel operand must be a GlobalValue");
7939 Ops.push_back(DAG.getTargetGlobalAddress(Base, sdl, MVT::i64, 0));
7940
7941 struct BranchFunnelTarget {
7942 int64_t Offset;
7944 };
7946
7947 for (unsigned Op = 1, N = I.arg_size(); Op != N; Op += 2) {
7949 I.getArgOperand(Op), Offset, DAG.getDataLayout()));
7950 if (ElemBase != Base)
7951 report_fatal_error("all llvm.icall.branch.funnel operands must refer "
7952 "to the same GlobalValue");
7953
7954 SDValue Val = getValue(I.getArgOperand(Op + 1));
7955 auto *GA = dyn_cast<GlobalAddressSDNode>(Val);
7956 if (!GA)
7958 "llvm.icall.branch.funnel operand must be a GlobalValue");
7959 Targets.push_back({Offset, DAG.getTargetGlobalAddress(
7960 GA->getGlobal(), sdl, Val.getValueType(),
7961 GA->getOffset())});
7962 }
7963 llvm::sort(Targets,
7964 [](const BranchFunnelTarget &T1, const BranchFunnelTarget &T2) {
7965 return T1.Offset < T2.Offset;
7966 });
7967
7968 for (auto &T : Targets) {
7969 Ops.push_back(DAG.getTargetConstant(T.Offset, sdl, MVT::i32));
7970 Ops.push_back(T.Target);
7971 }
7972
7973 Ops.push_back(DAG.getRoot()); // Chain
7974 SDValue N(DAG.getMachineNode(TargetOpcode::ICALL_BRANCH_FUNNEL, sdl,
7975 MVT::Other, Ops),
7976 0);
7977 DAG.setRoot(N);
7978 setValue(&I, N);
7979 HasTailCall = true;
7980 return;
7981 }
7982
7983 case Intrinsic::wasm_landingpad_index:
7984 // Information this intrinsic contained has been transferred to
7985 // MachineFunction in SelectionDAGISel::PrepareEHLandingPad. We can safely
7986 // delete it now.
7987 return;
7988
7989 case Intrinsic::aarch64_settag:
7990 case Intrinsic::aarch64_settag_zero: {
7991 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
7992 bool ZeroMemory = Intrinsic == Intrinsic::aarch64_settag_zero;
7994 DAG, sdl, getRoot(), getValue(I.getArgOperand(0)),
7995 getValue(I.getArgOperand(1)), MachinePointerInfo(I.getArgOperand(0)),
7996 ZeroMemory);
7997 DAG.setRoot(Val);
7998 setValue(&I, Val);
7999 return;
8000 }
8001 case Intrinsic::amdgcn_cs_chain: {
8002 // At this point we don't care if it's amdgpu_cs_chain or
8003 // amdgpu_cs_chain_preserve.
8005
8006 Type *RetTy = I.getType();
8007 assert(RetTy->isVoidTy() && "Should not return");
8008
8009 SDValue Callee = getValue(I.getOperand(0));
8010
8011 // We only have 2 actual args: one for the SGPRs and one for the VGPRs.
8012 // We'll also tack the value of the EXEC mask at the end.
8014 Args.reserve(3);
8015
8016 for (unsigned Idx : {2, 3, 1}) {
8017 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
8018 I.getOperand(Idx)->getType());
8019 Arg.setAttributes(&I, Idx);
8020 Args.push_back(Arg);
8021 }
8022
8023 assert(Args[0].IsInReg && "SGPR args should be marked inreg");
8024 assert(!Args[1].IsInReg && "VGPR args should not be marked inreg");
8025 Args[2].IsInReg = true; // EXEC should be inreg
8026
8027 // Forward the flags and any additional arguments.
8028 for (unsigned Idx = 4; Idx < I.arg_size(); ++Idx) {
8029 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
8030 I.getOperand(Idx)->getType());
8031 Arg.setAttributes(&I, Idx);
8032 Args.push_back(Arg);
8033 }
8034
8035 TargetLowering::CallLoweringInfo CLI(DAG);
8036 CLI.setDebugLoc(getCurSDLoc())
8037 .setChain(getRoot())
8038 .setCallee(CC, RetTy, Callee, std::move(Args))
8039 .setNoReturn(true)
8040 .setTailCall(true)
8041 .setConvergent(I.isConvergent());
8042 CLI.CB = &I;
8043 std::pair<SDValue, SDValue> Result =
8044 lowerInvokable(CLI, /*EHPadBB*/ nullptr);
8045 (void)Result;
8046 assert(!Result.first.getNode() && !Result.second.getNode() &&
8047 "Should've lowered as tail call");
8048
8049 HasTailCall = true;
8050 return;
8051 }
8052 case Intrinsic::amdgcn_call_whole_wave: {
8054 bool isTailCall = I.isTailCall();
8055
8056 // The first argument is the callee. Skip it when assembling the call args.
8057 for (unsigned Idx = 1; Idx < I.arg_size(); ++Idx) {
8058 TargetLowering::ArgListEntry Arg(getValue(I.getArgOperand(Idx)),
8059 I.getArgOperand(Idx)->getType());
8060 Arg.setAttributes(&I, Idx);
8061
8062 // If we have an explicit sret argument that is an Instruction, (i.e., it
8063 // might point to function-local memory), we can't meaningfully tail-call.
8064 if (Arg.IsSRet && isa<Instruction>(I.getArgOperand(Idx)))
8065 isTailCall = false;
8066
8067 Args.push_back(Arg);
8068 }
8069
8070 SDValue ConvControlToken;
8071 if (auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
8072 auto *Token = Bundle->Inputs[0].get();
8073 ConvControlToken = getValue(Token);
8074 }
8075
8076 TargetLowering::CallLoweringInfo CLI(DAG);
8077 CLI.setDebugLoc(getCurSDLoc())
8078 .setChain(getRoot())
8079 .setCallee(CallingConv::AMDGPU_Gfx_WholeWave, I.getType(),
8080 getValue(I.getArgOperand(0)), std::move(Args))
8081 .setTailCall(isTailCall && canTailCall(I))
8082 .setIsPreallocated(
8083 I.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0)
8084 .setConvergent(I.isConvergent())
8085 .setConvergenceControlToken(ConvControlToken);
8086 CLI.CB = &I;
8087
8088 std::pair<SDValue, SDValue> Result =
8089 lowerInvokable(CLI, /*EHPadBB=*/nullptr);
8090
8091 if (Result.first.getNode())
8092 setValue(&I, Result.first);
8093 return;
8094 }
8095 case Intrinsic::ptrmask: {
8096 SDValue Ptr = getValue(I.getOperand(0));
8097 SDValue Mask = getValue(I.getOperand(1));
8098
8099 // On arm64_32, pointers are 32 bits when stored in memory, but
8100 // zero-extended to 64 bits when in registers. Thus the mask is 32 bits to
8101 // match the index type, but the pointer is 64 bits, so the mask must be
8102 // zero-extended up to 64 bits to match the pointer.
8103 EVT PtrVT =
8104 TLI.getValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8105 EVT MemVT =
8106 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8107 assert(PtrVT == Ptr.getValueType());
8108 if (Mask.getValueType().getFixedSizeInBits() < MemVT.getFixedSizeInBits()) {
8109 // For AMDGPU buffer descriptors the mask is 48 bits, but the pointer is
8110 // 128-bit, so we have to pad the mask with ones for unused bits.
8111 auto HighOnes = DAG.getNode(
8112 ISD::SHL, sdl, PtrVT, DAG.getAllOnesConstant(sdl, PtrVT),
8113 DAG.getShiftAmountConstant(Mask.getValueType().getFixedSizeInBits(),
8114 PtrVT, sdl));
8115 Mask = DAG.getNode(ISD::OR, sdl, PtrVT,
8116 DAG.getZExtOrTrunc(Mask, sdl, PtrVT), HighOnes);
8117 } else if (Mask.getValueType() != PtrVT)
8118 Mask = DAG.getPtrExtOrTrunc(Mask, sdl, PtrVT);
8119
8120 assert(Mask.getValueType() == PtrVT);
8121 setValue(&I, DAG.getNode(ISD::AND, sdl, PtrVT, Ptr, Mask));
8122 return;
8123 }
8124 case Intrinsic::threadlocal_address: {
8125 setValue(&I, getValue(I.getOperand(0)));
8126 return;
8127 }
8128 case Intrinsic::get_active_lane_mask: {
8129 EVT CCVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8130 SDValue Index = getValue(I.getOperand(0));
8131 SDValue TripCount = getValue(I.getOperand(1));
8132 EVT ElementVT = Index.getValueType();
8133
8134 if (!TLI.shouldExpandGetActiveLaneMask(CCVT, ElementVT)) {
8135 setValue(&I, DAG.getNode(ISD::GET_ACTIVE_LANE_MASK, sdl, CCVT, Index,
8136 TripCount));
8137 return;
8138 }
8139
8140 EVT VecTy = EVT::getVectorVT(*DAG.getContext(), ElementVT,
8141 CCVT.getVectorElementCount());
8142
8143 SDValue VectorIndex = DAG.getSplat(VecTy, sdl, Index);
8144 SDValue VectorTripCount = DAG.getSplat(VecTy, sdl, TripCount);
8145 SDValue VectorStep = DAG.getStepVector(sdl, VecTy);
8146 SDValue VectorInduction = DAG.getNode(
8147 ISD::UADDSAT, sdl, VecTy, VectorIndex, VectorStep);
8148 SDValue SetCC = DAG.getSetCC(sdl, CCVT, VectorInduction,
8149 VectorTripCount, ISD::CondCode::SETULT);
8150 setValue(&I, SetCC);
8151 return;
8152 }
8153 case Intrinsic::experimental_get_vector_length: {
8154 assert(cast<ConstantInt>(I.getOperand(1))->getSExtValue() > 0 &&
8155 "Expected positive VF");
8156 unsigned VF = cast<ConstantInt>(I.getOperand(1))->getZExtValue();
8157 bool IsScalable = cast<ConstantInt>(I.getOperand(2))->isOne();
8158
8159 SDValue Count = getValue(I.getOperand(0));
8160 EVT CountVT = Count.getValueType();
8161
8162 if (!TLI.shouldExpandGetVectorLength(CountVT, VF, IsScalable)) {
8163 visitTargetIntrinsic(I, Intrinsic);
8164 return;
8165 }
8166
8167 // Expand to a umin between the trip count and the maximum elements the type
8168 // can hold.
8169 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8170
8171 // Extend the trip count to at least the result VT.
8172 if (CountVT.bitsLT(VT)) {
8173 Count = DAG.getNode(ISD::ZERO_EXTEND, sdl, VT, Count);
8174 CountVT = VT;
8175 }
8176
8177 SDValue MaxEVL = DAG.getElementCount(sdl, CountVT,
8178 ElementCount::get(VF, IsScalable));
8179
8180 SDValue UMin = DAG.getNode(ISD::UMIN, sdl, CountVT, Count, MaxEVL);
8181 // Clip to the result type if needed.
8182 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, sdl, VT, UMin);
8183
8184 setValue(&I, Trunc);
8185 return;
8186 }
8187 case Intrinsic::vector_partial_reduce_add: {
8188 SDValue Acc = getValue(I.getOperand(0));
8189 SDValue Input = getValue(I.getOperand(1));
8190 setValue(&I,
8191 DAG.getNode(ISD::PARTIAL_REDUCE_UMLA, sdl, Acc.getValueType(), Acc,
8192 Input, DAG.getConstant(1, sdl, Input.getValueType())));
8193 return;
8194 }
8195 case Intrinsic::vector_partial_reduce_fadd: {
8196 SDValue Acc = getValue(I.getOperand(0));
8197 SDValue Input = getValue(I.getOperand(1));
8198 setValue(&I, DAG.getNode(
8199 ISD::PARTIAL_REDUCE_FMLA, sdl, Acc.getValueType(), Acc,
8200 Input, DAG.getConstantFP(1.0, sdl, Input.getValueType())));
8201 return;
8202 }
8203 case Intrinsic::experimental_cttz_elts: {
8204 auto DL = getCurSDLoc();
8205 SDValue Op = getValue(I.getOperand(0));
8206 EVT OpVT = Op.getValueType();
8207
8208 if (!TLI.shouldExpandCttzElements(OpVT)) {
8209 visitTargetIntrinsic(I, Intrinsic);
8210 return;
8211 }
8212
8213 if (OpVT.getScalarType() != MVT::i1) {
8214 // Compare the input vector elements to zero & use to count trailing zeros
8215 SDValue AllZero = DAG.getConstant(0, DL, OpVT);
8216 OpVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1,
8217 OpVT.getVectorElementCount());
8218 Op = DAG.getSetCC(DL, OpVT, Op, AllZero, ISD::SETNE);
8219 }
8220
8221 // If the zero-is-poison flag is set, we can assume the upper limit
8222 // of the result is VF-1.
8223 bool ZeroIsPoison =
8224 !cast<ConstantSDNode>(getValue(I.getOperand(1)))->isZero();
8225 ConstantRange VScaleRange(1, true); // Dummy value.
8226 if (isa<ScalableVectorType>(I.getOperand(0)->getType()))
8227 VScaleRange = getVScaleRange(I.getCaller(), 64);
8228 unsigned EltWidth = TLI.getBitWidthForCttzElements(
8229 I.getType(), OpVT.getVectorElementCount(), ZeroIsPoison, &VScaleRange);
8230
8231 MVT NewEltTy = MVT::getIntegerVT(EltWidth);
8232
8233 // Create the new vector type & get the vector length
8234 EVT NewVT = EVT::getVectorVT(*DAG.getContext(), NewEltTy,
8235 OpVT.getVectorElementCount());
8236
8237 SDValue VL =
8238 DAG.getElementCount(DL, NewEltTy, OpVT.getVectorElementCount());
8239
8240 SDValue StepVec = DAG.getStepVector(DL, NewVT);
8241 SDValue SplatVL = DAG.getSplat(NewVT, DL, VL);
8242 SDValue StepVL = DAG.getNode(ISD::SUB, DL, NewVT, SplatVL, StepVec);
8243 SDValue Ext = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, Op);
8244 SDValue And = DAG.getNode(ISD::AND, DL, NewVT, StepVL, Ext);
8245 SDValue Max = DAG.getNode(ISD::VECREDUCE_UMAX, DL, NewEltTy, And);
8246 SDValue Sub = DAG.getNode(ISD::SUB, DL, NewEltTy, VL, Max);
8247
8248 EVT RetTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
8249 SDValue Ret = DAG.getZExtOrTrunc(Sub, DL, RetTy);
8250
8251 setValue(&I, Ret);
8252 return;
8253 }
8254 case Intrinsic::vector_insert: {
8255 SDValue Vec = getValue(I.getOperand(0));
8256 SDValue SubVec = getValue(I.getOperand(1));
8257 SDValue Index = getValue(I.getOperand(2));
8258
8259 // The intrinsic's index type is i64, but the SDNode requires an index type
8260 // suitable for the target. Convert the index as required.
8261 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8262 if (Index.getValueType() != VectorIdxTy)
8263 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8264
8265 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8266 setValue(&I, DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, ResultVT, Vec, SubVec,
8267 Index));
8268 return;
8269 }
8270 case Intrinsic::vector_extract: {
8271 SDValue Vec = getValue(I.getOperand(0));
8272 SDValue Index = getValue(I.getOperand(1));
8273 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8274
8275 // The intrinsic's index type is i64, but the SDNode requires an index type
8276 // suitable for the target. Convert the index as required.
8277 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8278 if (Index.getValueType() != VectorIdxTy)
8279 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8280
8281 setValue(&I,
8282 DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, ResultVT, Vec, Index));
8283 return;
8284 }
8285 case Intrinsic::experimental_vector_match: {
8286 SDValue Op1 = getValue(I.getOperand(0));
8287 SDValue Op2 = getValue(I.getOperand(1));
8288 SDValue Mask = getValue(I.getOperand(2));
8289 EVT Op1VT = Op1.getValueType();
8290 EVT Op2VT = Op2.getValueType();
8291 EVT ResVT = Mask.getValueType();
8292 unsigned SearchSize = Op2VT.getVectorNumElements();
8293
8294 // If the target has native support for this vector match operation, lower
8295 // the intrinsic untouched; otherwise, expand it below.
8296 if (!TLI.shouldExpandVectorMatch(Op1VT, SearchSize)) {
8297 visitTargetIntrinsic(I, Intrinsic);
8298 return;
8299 }
8300
8301 SDValue Ret = DAG.getConstant(0, sdl, ResVT);
8302
8303 for (unsigned i = 0; i < SearchSize; ++i) {
8304 SDValue Op2Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl,
8305 Op2VT.getVectorElementType(), Op2,
8306 DAG.getVectorIdxConstant(i, sdl));
8307 SDValue Splat = DAG.getNode(ISD::SPLAT_VECTOR, sdl, Op1VT, Op2Elem);
8308 SDValue Cmp = DAG.getSetCC(sdl, ResVT, Op1, Splat, ISD::SETEQ);
8309 Ret = DAG.getNode(ISD::OR, sdl, ResVT, Ret, Cmp);
8310 }
8311
8312 setValue(&I, DAG.getNode(ISD::AND, sdl, ResVT, Ret, Mask));
8313 return;
8314 }
8315 case Intrinsic::vector_reverse:
8316 visitVectorReverse(I);
8317 return;
8318 case Intrinsic::vector_splice:
8319 visitVectorSplice(I);
8320 return;
8321 case Intrinsic::callbr_landingpad:
8322 visitCallBrLandingPad(I);
8323 return;
8324 case Intrinsic::vector_interleave2:
8325 visitVectorInterleave(I, 2);
8326 return;
8327 case Intrinsic::vector_interleave3:
8328 visitVectorInterleave(I, 3);
8329 return;
8330 case Intrinsic::vector_interleave4:
8331 visitVectorInterleave(I, 4);
8332 return;
8333 case Intrinsic::vector_interleave5:
8334 visitVectorInterleave(I, 5);
8335 return;
8336 case Intrinsic::vector_interleave6:
8337 visitVectorInterleave(I, 6);
8338 return;
8339 case Intrinsic::vector_interleave7:
8340 visitVectorInterleave(I, 7);
8341 return;
8342 case Intrinsic::vector_interleave8:
8343 visitVectorInterleave(I, 8);
8344 return;
8345 case Intrinsic::vector_deinterleave2:
8346 visitVectorDeinterleave(I, 2);
8347 return;
8348 case Intrinsic::vector_deinterleave3:
8349 visitVectorDeinterleave(I, 3);
8350 return;
8351 case Intrinsic::vector_deinterleave4:
8352 visitVectorDeinterleave(I, 4);
8353 return;
8354 case Intrinsic::vector_deinterleave5:
8355 visitVectorDeinterleave(I, 5);
8356 return;
8357 case Intrinsic::vector_deinterleave6:
8358 visitVectorDeinterleave(I, 6);
8359 return;
8360 case Intrinsic::vector_deinterleave7:
8361 visitVectorDeinterleave(I, 7);
8362 return;
8363 case Intrinsic::vector_deinterleave8:
8364 visitVectorDeinterleave(I, 8);
8365 return;
8366 case Intrinsic::experimental_vector_compress:
8367 setValue(&I, DAG.getNode(ISD::VECTOR_COMPRESS, sdl,
8368 getValue(I.getArgOperand(0)).getValueType(),
8369 getValue(I.getArgOperand(0)),
8370 getValue(I.getArgOperand(1)),
8371 getValue(I.getArgOperand(2)), Flags));
8372 return;
8373 case Intrinsic::experimental_convergence_anchor:
8374 case Intrinsic::experimental_convergence_entry:
8375 case Intrinsic::experimental_convergence_loop:
8376 visitConvergenceControl(I, Intrinsic);
8377 return;
8378 case Intrinsic::experimental_vector_histogram_add: {
8379 visitVectorHistogram(I, Intrinsic);
8380 return;
8381 }
8382 case Intrinsic::experimental_vector_extract_last_active: {
8383 visitVectorExtractLastActive(I, Intrinsic);
8384 return;
8385 }
8386 case Intrinsic::loop_dependence_war_mask:
8387 setValue(&I,
8389 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8390 getValue(I.getOperand(1)), getValue(I.getOperand(2))));
8391 return;
8392 case Intrinsic::loop_dependence_raw_mask:
8393 setValue(&I,
8395 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8396 getValue(I.getOperand(1)), getValue(I.getOperand(2))));
8397 return;
8398 }
8399}
8400
8401void SelectionDAGBuilder::pushFPOpOutChain(SDValue Result,
8403 assert(Result.getNode()->getNumValues() == 2);
8404 SDValue OutChain = Result.getValue(1);
8405 assert(OutChain.getValueType() == MVT::Other);
8406
8407 // Instead of updating the root immediately, push the produced chain to the
8408 // appropriate list, deferring the update until the root is requested. In this
8409 // case, the nodes from the lists are chained using TokenFactor, indicating
8410 // that the operations are independent.
8411 //
8412 // In particular, the root is updated before any call that might access the
8413 // floating-point environment, except for constrained intrinsics.
8414 switch (EB) {
8417 PendingConstrainedFP.push_back(OutChain);
8418 break;
8420 PendingConstrainedFPStrict.push_back(OutChain);
8421 break;
8422 }
8423}
8424
8425void SelectionDAGBuilder::visitConstrainedFPIntrinsic(
8426 const ConstrainedFPIntrinsic &FPI) {
8427 SDLoc sdl = getCurSDLoc();
8428
8429 // We do not need to serialize constrained FP intrinsics against
8430 // each other or against (nonvolatile) loads, so they can be
8431 // chained like loads.
8433 SDValue Chain = getFPOperationRoot(EB);
8435 Opers.push_back(Chain);
8436 for (unsigned I = 0, E = FPI.getNonMetadataArgCount(); I != E; ++I)
8437 Opers.push_back(getValue(FPI.getArgOperand(I)));
8438
8439 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8440 EVT VT = TLI.getValueType(DAG.getDataLayout(), FPI.getType());
8441 SDVTList VTs = DAG.getVTList(VT, MVT::Other);
8442
8443 SDNodeFlags Flags;
8445 Flags.setNoFPExcept(true);
8446
8447 if (auto *FPOp = dyn_cast<FPMathOperator>(&FPI))
8448 Flags.copyFMF(*FPOp);
8449
8450 unsigned Opcode;
8451 switch (FPI.getIntrinsicID()) {
8452 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
8453#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
8454 case Intrinsic::INTRINSIC: \
8455 Opcode = ISD::STRICT_##DAGN; \
8456 break;
8457#include "llvm/IR/ConstrainedOps.def"
8458 case Intrinsic::experimental_constrained_fmuladd: {
8459 Opcode = ISD::STRICT_FMA;
8460 // Break fmuladd into fmul and fadd.
8461 if (TM.Options.AllowFPOpFusion == FPOpFusion::Strict ||
8462 !TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
8463 Opers.pop_back();
8464 SDValue Mul = DAG.getNode(ISD::STRICT_FMUL, sdl, VTs, Opers, Flags);
8465 pushFPOpOutChain(Mul, EB);
8466 Opcode = ISD::STRICT_FADD;
8467 Opers.clear();
8468 Opers.push_back(Mul.getValue(1));
8469 Opers.push_back(Mul.getValue(0));
8470 Opers.push_back(getValue(FPI.getArgOperand(2)));
8471 }
8472 break;
8473 }
8474 }
8475
8476 // A few strict DAG nodes carry additional operands that are not
8477 // set up by the default code above.
8478 switch (Opcode) {
8479 default: break;
8481 Opers.push_back(
8482 DAG.getTargetConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout())));
8483 break;
8484 case ISD::STRICT_FSETCC:
8485 case ISD::STRICT_FSETCCS: {
8487 ISD::CondCode Condition = getFCmpCondCode(FPCmp->getPredicate());
8488 if (TM.Options.NoNaNsFPMath)
8489 Condition = getFCmpCodeWithoutNaN(Condition);
8490 Opers.push_back(DAG.getCondCode(Condition));
8491 break;
8492 }
8493 }
8494
8495 SDValue Result = DAG.getNode(Opcode, sdl, VTs, Opers, Flags);
8496 pushFPOpOutChain(Result, EB);
8497
8498 SDValue FPResult = Result.getValue(0);
8499 setValue(&FPI, FPResult);
8500}
8501
8502static unsigned getISDForVPIntrinsic(const VPIntrinsic &VPIntrin) {
8503 std::optional<unsigned> ResOPC;
8504 switch (VPIntrin.getIntrinsicID()) {
8505 case Intrinsic::vp_ctlz: {
8506 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8507 ResOPC = IsZeroUndef ? ISD::VP_CTLZ_ZERO_UNDEF : ISD::VP_CTLZ;
8508 break;
8509 }
8510 case Intrinsic::vp_cttz: {
8511 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8512 ResOPC = IsZeroUndef ? ISD::VP_CTTZ_ZERO_UNDEF : ISD::VP_CTTZ;
8513 break;
8514 }
8515 case Intrinsic::vp_cttz_elts: {
8516 bool IsZeroPoison = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8517 ResOPC = IsZeroPoison ? ISD::VP_CTTZ_ELTS_ZERO_UNDEF : ISD::VP_CTTZ_ELTS;
8518 break;
8519 }
8520#define HELPER_MAP_VPID_TO_VPSD(VPID, VPSD) \
8521 case Intrinsic::VPID: \
8522 ResOPC = ISD::VPSD; \
8523 break;
8524#include "llvm/IR/VPIntrinsics.def"
8525 }
8526
8527 if (!ResOPC)
8529 "Inconsistency: no SDNode available for this VPIntrinsic!");
8530
8531 if (*ResOPC == ISD::VP_REDUCE_SEQ_FADD ||
8532 *ResOPC == ISD::VP_REDUCE_SEQ_FMUL) {
8533 if (VPIntrin.getFastMathFlags().allowReassoc())
8534 return *ResOPC == ISD::VP_REDUCE_SEQ_FADD ? ISD::VP_REDUCE_FADD
8535 : ISD::VP_REDUCE_FMUL;
8536 }
8537
8538 return *ResOPC;
8539}
8540
8541void SelectionDAGBuilder::visitVPLoad(
8542 const VPIntrinsic &VPIntrin, EVT VT,
8543 const SmallVectorImpl<SDValue> &OpValues) {
8544 SDLoc DL = getCurSDLoc();
8545 Value *PtrOperand = VPIntrin.getArgOperand(0);
8546 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8547 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8548 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8549 SDValue LD;
8550 // Do not serialize variable-length loads of constant memory with
8551 // anything.
8552 if (!Alignment)
8553 Alignment = DAG.getEVTAlign(VT);
8554 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8555 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8556 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8557 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8558 MachineMemOperand::Flags MMOFlags =
8559 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8560 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8561 MachinePointerInfo(PtrOperand), MMOFlags,
8562 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8563 LD = DAG.getLoadVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8564 MMO, false /*IsExpanding */);
8565 if (AddToChain)
8566 PendingLoads.push_back(LD.getValue(1));
8567 setValue(&VPIntrin, LD);
8568}
8569
8570void SelectionDAGBuilder::visitVPLoadFF(
8571 const VPIntrinsic &VPIntrin, EVT VT, EVT EVLVT,
8572 const SmallVectorImpl<SDValue> &OpValues) {
8573 assert(OpValues.size() == 3 && "Unexpected number of operands");
8574 SDLoc DL = getCurSDLoc();
8575 Value *PtrOperand = VPIntrin.getArgOperand(0);
8576 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8577 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8578 const MDNode *Ranges = VPIntrin.getMetadata(LLVMContext::MD_range);
8579 SDValue LD;
8580 // Do not serialize variable-length loads of constant memory with
8581 // anything.
8582 if (!Alignment)
8583 Alignment = DAG.getEVTAlign(VT);
8584 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8585 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8586 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8587 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8588 MachinePointerInfo(PtrOperand), MachineMemOperand::MOLoad,
8589 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8590 LD = DAG.getLoadFFVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8591 MMO);
8592 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, EVLVT, LD.getValue(1));
8593 if (AddToChain)
8594 PendingLoads.push_back(LD.getValue(2));
8595 setValue(&VPIntrin, DAG.getMergeValues({LD.getValue(0), Trunc}, DL));
8596}
8597
8598void SelectionDAGBuilder::visitVPGather(
8599 const VPIntrinsic &VPIntrin, EVT VT,
8600 const SmallVectorImpl<SDValue> &OpValues) {
8601 SDLoc DL = getCurSDLoc();
8602 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8603 Value *PtrOperand = VPIntrin.getArgOperand(0);
8604 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8605 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8606 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8607 SDValue LD;
8608 if (!Alignment)
8609 Alignment = DAG.getEVTAlign(VT.getScalarType());
8610 unsigned AS =
8611 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8612 MachineMemOperand::Flags MMOFlags =
8613 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8614 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8615 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8616 *Alignment, AAInfo, Ranges);
8617 SDValue Base, Index, Scale;
8618 bool UniformBase =
8619 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8620 VT.getScalarStoreSize());
8621 if (!UniformBase) {
8622 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8623 Index = getValue(PtrOperand);
8624 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8625 }
8626 EVT IdxVT = Index.getValueType();
8627 EVT EltTy = IdxVT.getVectorElementType();
8628 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8629 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
8630 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8631 }
8632 LD = DAG.getGatherVP(
8633 DAG.getVTList(VT, MVT::Other), VT, DL,
8634 {DAG.getRoot(), Base, Index, Scale, OpValues[1], OpValues[2]}, MMO,
8636 PendingLoads.push_back(LD.getValue(1));
8637 setValue(&VPIntrin, LD);
8638}
8639
8640void SelectionDAGBuilder::visitVPStore(
8641 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8642 SDLoc DL = getCurSDLoc();
8643 Value *PtrOperand = VPIntrin.getArgOperand(1);
8644 EVT VT = OpValues[0].getValueType();
8645 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8646 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8647 SDValue ST;
8648 if (!Alignment)
8649 Alignment = DAG.getEVTAlign(VT);
8650 SDValue Ptr = OpValues[1];
8651 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
8652 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8653 MachineMemOperand::Flags MMOFlags =
8654 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8655 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8656 MachinePointerInfo(PtrOperand), MMOFlags,
8657 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo);
8658 ST = DAG.getStoreVP(getMemoryRoot(), DL, OpValues[0], Ptr, Offset,
8659 OpValues[2], OpValues[3], VT, MMO, ISD::UNINDEXED,
8660 /* IsTruncating */ false, /*IsCompressing*/ false);
8661 DAG.setRoot(ST);
8662 setValue(&VPIntrin, ST);
8663}
8664
8665void SelectionDAGBuilder::visitVPScatter(
8666 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8667 SDLoc DL = getCurSDLoc();
8668 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8669 Value *PtrOperand = VPIntrin.getArgOperand(1);
8670 EVT VT = OpValues[0].getValueType();
8671 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8672 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8673 SDValue ST;
8674 if (!Alignment)
8675 Alignment = DAG.getEVTAlign(VT.getScalarType());
8676 unsigned AS =
8677 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8678 MachineMemOperand::Flags MMOFlags =
8679 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8680 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8681 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8682 *Alignment, AAInfo);
8683 SDValue Base, Index, Scale;
8684 bool UniformBase =
8685 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8686 VT.getScalarStoreSize());
8687 if (!UniformBase) {
8688 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8689 Index = getValue(PtrOperand);
8690 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8691 }
8692 EVT IdxVT = Index.getValueType();
8693 EVT EltTy = IdxVT.getVectorElementType();
8694 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8695 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
8696 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8697 }
8698 ST = DAG.getScatterVP(DAG.getVTList(MVT::Other), VT, DL,
8699 {getMemoryRoot(), OpValues[0], Base, Index, Scale,
8700 OpValues[2], OpValues[3]},
8701 MMO, ISD::SIGNED_SCALED);
8702 DAG.setRoot(ST);
8703 setValue(&VPIntrin, ST);
8704}
8705
8706void SelectionDAGBuilder::visitVPStridedLoad(
8707 const VPIntrinsic &VPIntrin, EVT VT,
8708 const SmallVectorImpl<SDValue> &OpValues) {
8709 SDLoc DL = getCurSDLoc();
8710 Value *PtrOperand = VPIntrin.getArgOperand(0);
8711 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8712 if (!Alignment)
8713 Alignment = DAG.getEVTAlign(VT.getScalarType());
8714 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8715 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8716 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8717 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8718 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8719 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8720 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8721 MachineMemOperand::Flags MMOFlags =
8722 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8723 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8724 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8725 *Alignment, AAInfo, Ranges);
8726
8727 SDValue LD = DAG.getStridedLoadVP(VT, DL, InChain, OpValues[0], OpValues[1],
8728 OpValues[2], OpValues[3], MMO,
8729 false /*IsExpanding*/);
8730
8731 if (AddToChain)
8732 PendingLoads.push_back(LD.getValue(1));
8733 setValue(&VPIntrin, LD);
8734}
8735
8736void SelectionDAGBuilder::visitVPStridedStore(
8737 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8738 SDLoc DL = getCurSDLoc();
8739 Value *PtrOperand = VPIntrin.getArgOperand(1);
8740 EVT VT = OpValues[0].getValueType();
8741 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8742 if (!Alignment)
8743 Alignment = DAG.getEVTAlign(VT.getScalarType());
8744 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8745 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8746 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8747 MachineMemOperand::Flags MMOFlags =
8748 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8749 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8750 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8751 *Alignment, AAInfo);
8752
8753 SDValue ST = DAG.getStridedStoreVP(
8754 getMemoryRoot(), DL, OpValues[0], OpValues[1],
8755 DAG.getUNDEF(OpValues[1].getValueType()), OpValues[2], OpValues[3],
8756 OpValues[4], VT, MMO, ISD::UNINDEXED, /*IsTruncating*/ false,
8757 /*IsCompressing*/ false);
8758
8759 DAG.setRoot(ST);
8760 setValue(&VPIntrin, ST);
8761}
8762
8763void SelectionDAGBuilder::visitVPCmp(const VPCmpIntrinsic &VPIntrin) {
8764 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8765 SDLoc DL = getCurSDLoc();
8766
8767 ISD::CondCode Condition;
8769 bool IsFP = VPIntrin.getOperand(0)->getType()->isFPOrFPVectorTy();
8770 if (IsFP) {
8771 // FIXME: Regular fcmps are FPMathOperators which may have fast-math (nnan)
8772 // flags, but calls that don't return floating-point types can't be
8773 // FPMathOperators, like vp.fcmp. This affects constrained fcmp too.
8774 Condition = getFCmpCondCode(CondCode);
8775 if (TM.Options.NoNaNsFPMath)
8776 Condition = getFCmpCodeWithoutNaN(Condition);
8777 } else {
8778 Condition = getICmpCondCode(CondCode);
8779 }
8780
8781 SDValue Op1 = getValue(VPIntrin.getOperand(0));
8782 SDValue Op2 = getValue(VPIntrin.getOperand(1));
8783 // #2 is the condition code
8784 SDValue MaskOp = getValue(VPIntrin.getOperand(3));
8785 SDValue EVL = getValue(VPIntrin.getOperand(4));
8786 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8787 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8788 "Unexpected target EVL type");
8789 EVL = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, EVL);
8790
8791 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
8792 VPIntrin.getType());
8793 setValue(&VPIntrin,
8794 DAG.getSetCCVP(DL, DestVT, Op1, Op2, Condition, MaskOp, EVL));
8795}
8796
8797void SelectionDAGBuilder::visitVectorPredicationIntrinsic(
8798 const VPIntrinsic &VPIntrin) {
8799 SDLoc DL = getCurSDLoc();
8800 unsigned Opcode = getISDForVPIntrinsic(VPIntrin);
8801
8802 auto IID = VPIntrin.getIntrinsicID();
8803
8804 if (const auto *CmpI = dyn_cast<VPCmpIntrinsic>(&VPIntrin))
8805 return visitVPCmp(*CmpI);
8806
8807 SmallVector<EVT, 4> ValueVTs;
8808 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8809 ComputeValueVTs(TLI, DAG.getDataLayout(), VPIntrin.getType(), ValueVTs);
8810 SDVTList VTs = DAG.getVTList(ValueVTs);
8811
8812 auto EVLParamPos = VPIntrinsic::getVectorLengthParamPos(IID);
8813
8814 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8815 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8816 "Unexpected target EVL type");
8817
8818 // Request operands.
8819 SmallVector<SDValue, 7> OpValues;
8820 for (unsigned I = 0; I < VPIntrin.arg_size(); ++I) {
8821 auto Op = getValue(VPIntrin.getArgOperand(I));
8822 if (I == EVLParamPos)
8823 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, Op);
8824 OpValues.push_back(Op);
8825 }
8826
8827 switch (Opcode) {
8828 default: {
8829 SDNodeFlags SDFlags;
8830 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8831 SDFlags.copyFMF(*FPMO);
8832 SDValue Result = DAG.getNode(Opcode, DL, VTs, OpValues, SDFlags);
8833 setValue(&VPIntrin, Result);
8834 break;
8835 }
8836 case ISD::VP_LOAD:
8837 visitVPLoad(VPIntrin, ValueVTs[0], OpValues);
8838 break;
8839 case ISD::VP_LOAD_FF:
8840 visitVPLoadFF(VPIntrin, ValueVTs[0], ValueVTs[1], OpValues);
8841 break;
8842 case ISD::VP_GATHER:
8843 visitVPGather(VPIntrin, ValueVTs[0], OpValues);
8844 break;
8845 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
8846 visitVPStridedLoad(VPIntrin, ValueVTs[0], OpValues);
8847 break;
8848 case ISD::VP_STORE:
8849 visitVPStore(VPIntrin, OpValues);
8850 break;
8851 case ISD::VP_SCATTER:
8852 visitVPScatter(VPIntrin, OpValues);
8853 break;
8854 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
8855 visitVPStridedStore(VPIntrin, OpValues);
8856 break;
8857 case ISD::VP_FMULADD: {
8858 assert(OpValues.size() == 5 && "Unexpected number of operands");
8859 SDNodeFlags SDFlags;
8860 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8861 SDFlags.copyFMF(*FPMO);
8862 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
8863 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), ValueVTs[0])) {
8864 setValue(&VPIntrin, DAG.getNode(ISD::VP_FMA, DL, VTs, OpValues, SDFlags));
8865 } else {
8866 SDValue Mul = DAG.getNode(
8867 ISD::VP_FMUL, DL, VTs,
8868 {OpValues[0], OpValues[1], OpValues[3], OpValues[4]}, SDFlags);
8869 SDValue Add =
8870 DAG.getNode(ISD::VP_FADD, DL, VTs,
8871 {Mul, OpValues[2], OpValues[3], OpValues[4]}, SDFlags);
8872 setValue(&VPIntrin, Add);
8873 }
8874 break;
8875 }
8876 case ISD::VP_IS_FPCLASS: {
8877 const DataLayout DLayout = DAG.getDataLayout();
8878 EVT DestVT = TLI.getValueType(DLayout, VPIntrin.getType());
8879 auto Constant = OpValues[1]->getAsZExtVal();
8880 SDValue Check = DAG.getTargetConstant(Constant, DL, MVT::i32);
8881 SDValue V = DAG.getNode(ISD::VP_IS_FPCLASS, DL, DestVT,
8882 {OpValues[0], Check, OpValues[2], OpValues[3]});
8883 setValue(&VPIntrin, V);
8884 return;
8885 }
8886 case ISD::VP_INTTOPTR: {
8887 SDValue N = OpValues[0];
8888 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), VPIntrin.getType());
8889 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), VPIntrin.getType());
8890 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
8891 OpValues[2]);
8892 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
8893 OpValues[2]);
8894 setValue(&VPIntrin, N);
8895 break;
8896 }
8897 case ISD::VP_PTRTOINT: {
8898 SDValue N = OpValues[0];
8899 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
8900 VPIntrin.getType());
8901 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(),
8902 VPIntrin.getOperand(0)->getType());
8903 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
8904 OpValues[2]);
8905 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
8906 OpValues[2]);
8907 setValue(&VPIntrin, N);
8908 break;
8909 }
8910 case ISD::VP_ABS:
8911 case ISD::VP_CTLZ:
8912 case ISD::VP_CTLZ_ZERO_UNDEF:
8913 case ISD::VP_CTTZ:
8914 case ISD::VP_CTTZ_ZERO_UNDEF:
8915 case ISD::VP_CTTZ_ELTS_ZERO_UNDEF:
8916 case ISD::VP_CTTZ_ELTS: {
8917 SDValue Result =
8918 DAG.getNode(Opcode, DL, VTs, {OpValues[0], OpValues[2], OpValues[3]});
8919 setValue(&VPIntrin, Result);
8920 break;
8921 }
8922 }
8923}
8924
8925SDValue SelectionDAGBuilder::lowerStartEH(SDValue Chain,
8926 const BasicBlock *EHPadBB,
8927 MCSymbol *&BeginLabel) {
8928 MachineFunction &MF = DAG.getMachineFunction();
8929
8930 // Insert a label before the invoke call to mark the try range. This can be
8931 // used to detect deletion of the invoke via the MachineModuleInfo.
8932 BeginLabel = MF.getContext().createTempSymbol();
8933
8934 // For SjLj, keep track of which landing pads go with which invokes
8935 // so as to maintain the ordering of pads in the LSDA.
8936 unsigned CallSiteIndex = FuncInfo.getCurrentCallSite();
8937 if (CallSiteIndex) {
8938 MF.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
8939 LPadToCallSiteMap[FuncInfo.getMBB(EHPadBB)].push_back(CallSiteIndex);
8940
8941 // Now that the call site is handled, stop tracking it.
8942 FuncInfo.setCurrentCallSite(0);
8943 }
8944
8945 return DAG.getEHLabel(getCurSDLoc(), Chain, BeginLabel);
8946}
8947
8948SDValue SelectionDAGBuilder::lowerEndEH(SDValue Chain, const InvokeInst *II,
8949 const BasicBlock *EHPadBB,
8950 MCSymbol *BeginLabel) {
8951 assert(BeginLabel && "BeginLabel should've been set");
8952
8953 MachineFunction &MF = DAG.getMachineFunction();
8954
8955 // Insert a label at the end of the invoke call to mark the try range. This
8956 // can be used to detect deletion of the invoke via the MachineModuleInfo.
8957 MCSymbol *EndLabel = MF.getContext().createTempSymbol();
8958 Chain = DAG.getEHLabel(getCurSDLoc(), Chain, EndLabel);
8959
8960 // Inform MachineModuleInfo of range.
8961 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
8962 // There is a platform (e.g. wasm) that uses funclet style IR but does not
8963 // actually use outlined funclets and their LSDA info style.
8964 if (MF.hasEHFunclets() && isFuncletEHPersonality(Pers)) {
8965 assert(II && "II should've been set");
8966 WinEHFuncInfo *EHInfo = MF.getWinEHFuncInfo();
8967 EHInfo->addIPToStateRange(II, BeginLabel, EndLabel);
8968 } else if (!isScopedEHPersonality(Pers)) {
8969 assert(EHPadBB);
8970 MF.addInvoke(FuncInfo.getMBB(EHPadBB), BeginLabel, EndLabel);
8971 }
8972
8973 return Chain;
8974}
8975
8976std::pair<SDValue, SDValue>
8978 const BasicBlock *EHPadBB) {
8979 MCSymbol *BeginLabel = nullptr;
8980
8981 if (EHPadBB) {
8982 // Both PendingLoads and PendingExports must be flushed here;
8983 // this call might not return.
8984 (void)getRoot();
8985 DAG.setRoot(lowerStartEH(getControlRoot(), EHPadBB, BeginLabel));
8986 CLI.setChain(getRoot());
8987 }
8988
8989 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8990 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
8991
8992 assert((CLI.IsTailCall || Result.second.getNode()) &&
8993 "Non-null chain expected with non-tail call!");
8994 assert((Result.second.getNode() || !Result.first.getNode()) &&
8995 "Null value expected with tail call!");
8996
8997 if (!Result.second.getNode()) {
8998 // As a special case, a null chain means that a tail call has been emitted
8999 // and the DAG root is already updated.
9000 HasTailCall = true;
9001
9002 // Since there's no actual continuation from this block, nothing can be
9003 // relying on us setting vregs for them.
9004 PendingExports.clear();
9005 } else {
9006 DAG.setRoot(Result.second);
9007 }
9008
9009 if (EHPadBB) {
9010 DAG.setRoot(lowerEndEH(getRoot(), cast_or_null<InvokeInst>(CLI.CB), EHPadBB,
9011 BeginLabel));
9012 Result.second = getRoot();
9013 }
9014
9015 return Result;
9016}
9017
9019 bool isMustTailCall = CB.isMustTailCall();
9020
9021 // Avoid emitting tail calls in functions with the disable-tail-calls
9022 // attribute.
9023 const Function *Caller = CB.getParent()->getParent();
9024 if (!isMustTailCall &&
9025 Caller->getFnAttribute("disable-tail-calls").getValueAsBool())
9026 return false;
9027
9028 // We can't tail call inside a function with a swifterror argument. Lowering
9029 // does not support this yet. It would have to move into the swifterror
9030 // register before the call.
9031 if (DAG.getTargetLoweringInfo().supportSwiftError() &&
9032 Caller->getAttributes().hasAttrSomewhere(Attribute::SwiftError))
9033 return false;
9034
9035 // Check if target-independent constraints permit a tail call here.
9036 // Target-dependent constraints are checked within TLI->LowerCallTo.
9037 return isInTailCallPosition(CB, DAG.getTarget());
9038}
9039
9041 bool isTailCall, bool isMustTailCall,
9042 const BasicBlock *EHPadBB,
9043 const TargetLowering::PtrAuthInfo *PAI) {
9044 auto &DL = DAG.getDataLayout();
9045 FunctionType *FTy = CB.getFunctionType();
9046 Type *RetTy = CB.getType();
9047
9049 Args.reserve(CB.arg_size());
9050
9051 const Value *SwiftErrorVal = nullptr;
9052 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9053
9054 if (isTailCall)
9055 isTailCall = canTailCall(CB);
9056
9057 for (auto I = CB.arg_begin(), E = CB.arg_end(); I != E; ++I) {
9058 const Value *V = *I;
9059
9060 // Skip empty types
9061 if (V->getType()->isEmptyTy())
9062 continue;
9063
9064 SDValue ArgNode = getValue(V);
9065 TargetLowering::ArgListEntry Entry(ArgNode, V->getType());
9066 Entry.setAttributes(&CB, I - CB.arg_begin());
9067
9068 // Use swifterror virtual register as input to the call.
9069 if (Entry.IsSwiftError && TLI.supportSwiftError()) {
9070 SwiftErrorVal = V;
9071 // We find the virtual register for the actual swifterror argument.
9072 // Instead of using the Value, we use the virtual register instead.
9073 Entry.Node =
9074 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(&CB, FuncInfo.MBB, V),
9075 EVT(TLI.getPointerTy(DL)));
9076 }
9077
9078 Args.push_back(Entry);
9079
9080 // If we have an explicit sret argument that is an Instruction, (i.e., it
9081 // might point to function-local memory), we can't meaningfully tail-call.
9082 if (Entry.IsSRet && isa<Instruction>(V))
9083 isTailCall = false;
9084 }
9085
9086 // If call site has a cfguardtarget operand bundle, create and add an
9087 // additional ArgListEntry.
9088 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_cfguardtarget)) {
9089 Value *V = Bundle->Inputs[0];
9091 Entry.IsCFGuardTarget = true;
9092 Args.push_back(Entry);
9093 }
9094
9095 // Disable tail calls if there is an swifterror argument. Targets have not
9096 // been updated to support tail calls.
9097 if (TLI.supportSwiftError() && SwiftErrorVal)
9098 isTailCall = false;
9099
9100 ConstantInt *CFIType = nullptr;
9101 if (CB.isIndirectCall()) {
9102 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_kcfi)) {
9103 if (!TLI.supportKCFIBundles())
9105 "Target doesn't support calls with kcfi operand bundles.");
9106 CFIType = cast<ConstantInt>(Bundle->Inputs[0]);
9107 assert(CFIType->getType()->isIntegerTy(32) && "Invalid CFI type");
9108 }
9109 }
9110
9111 SDValue ConvControlToken;
9112 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_convergencectrl)) {
9113 auto *Token = Bundle->Inputs[0].get();
9114 ConvControlToken = getValue(Token);
9115 }
9116
9119 .setChain(getRoot())
9120 .setCallee(RetTy, FTy, Callee, std::move(Args), CB)
9121 .setTailCall(isTailCall)
9125 .setCFIType(CFIType)
9126 .setConvergenceControlToken(ConvControlToken);
9127
9128 // Set the pointer authentication info if we have it.
9129 if (PAI) {
9130 if (!TLI.supportPtrAuthBundles())
9132 "This target doesn't support calls with ptrauth operand bundles.");
9133 CLI.setPtrAuth(*PAI);
9134 }
9135
9136 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
9137
9138 if (Result.first.getNode()) {
9139 Result.first = lowerRangeToAssertZExt(DAG, CB, Result.first);
9140 Result.first = lowerNoFPClassToAssertNoFPClass(DAG, CB, Result.first);
9141 setValue(&CB, Result.first);
9142 }
9143
9144 // The last element of CLI.InVals has the SDValue for swifterror return.
9145 // Here we copy it to a virtual register and update SwiftErrorMap for
9146 // book-keeping.
9147 if (SwiftErrorVal && TLI.supportSwiftError()) {
9148 // Get the last element of InVals.
9149 SDValue Src = CLI.InVals.back();
9150 Register VReg =
9151 SwiftError.getOrCreateVRegDefAt(&CB, FuncInfo.MBB, SwiftErrorVal);
9152 SDValue CopyNode = CLI.DAG.getCopyToReg(Result.second, CLI.DL, VReg, Src);
9153 DAG.setRoot(CopyNode);
9154 }
9155}
9156
9157static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
9158 SelectionDAGBuilder &Builder) {
9159 // Check to see if this load can be trivially constant folded, e.g. if the
9160 // input is from a string literal.
9161 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
9162 // Cast pointer to the type we really want to load.
9163 Type *LoadTy =
9164 Type::getIntNTy(PtrVal->getContext(), LoadVT.getScalarSizeInBits());
9165 if (LoadVT.isVector())
9166 LoadTy = FixedVectorType::get(LoadTy, LoadVT.getVectorNumElements());
9167 if (const Constant *LoadCst =
9168 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
9169 LoadTy, Builder.DAG.getDataLayout()))
9170 return Builder.getValue(LoadCst);
9171 }
9172
9173 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
9174 // still constant memory, the input chain can be the entry node.
9175 SDValue Root;
9176 bool ConstantMemory = false;
9177
9178 // Do not serialize (non-volatile) loads of constant memory with anything.
9179 if (Builder.BatchAA && Builder.BatchAA->pointsToConstantMemory(PtrVal)) {
9180 Root = Builder.DAG.getEntryNode();
9181 ConstantMemory = true;
9182 } else {
9183 // Do not serialize non-volatile loads against each other.
9184 Root = Builder.DAG.getRoot();
9185 }
9186
9187 SDValue Ptr = Builder.getValue(PtrVal);
9188 SDValue LoadVal =
9189 Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root, Ptr,
9190 MachinePointerInfo(PtrVal), Align(1));
9191
9192 if (!ConstantMemory)
9193 Builder.PendingLoads.push_back(LoadVal.getValue(1));
9194 return LoadVal;
9195}
9196
9197/// Record the value for an instruction that produces an integer result,
9198/// converting the type where necessary.
9199void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I,
9200 SDValue Value,
9201 bool IsSigned) {
9202 EVT VT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9203 I.getType(), true);
9204 Value = DAG.getExtOrTrunc(IsSigned, Value, getCurSDLoc(), VT);
9205 setValue(&I, Value);
9206}
9207
9208/// See if we can lower a memcmp/bcmp call into an optimized form. If so, return
9209/// true and lower it. Otherwise return false, and it will be lowered like a
9210/// normal call.
9211/// The caller already checked that \p I calls the appropriate LibFunc with a
9212/// correct prototype.
9213bool SelectionDAGBuilder::visitMemCmpBCmpCall(const CallInst &I) {
9214 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
9215 const Value *Size = I.getArgOperand(2);
9216 const ConstantSDNode *CSize = dyn_cast<ConstantSDNode>(getValue(Size));
9217 if (CSize && CSize->getZExtValue() == 0) {
9218 EVT CallVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9219 I.getType(), true);
9220 setValue(&I, DAG.getConstant(0, getCurSDLoc(), CallVT));
9221 return true;
9222 }
9223
9224 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9225 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForMemcmp(
9226 DAG, getCurSDLoc(), DAG.getRoot(), getValue(LHS), getValue(RHS),
9227 getValue(Size), &I);
9228 if (Res.first.getNode()) {
9229 processIntegerCallValue(I, Res.first, true);
9230 PendingLoads.push_back(Res.second);
9231 return true;
9232 }
9233
9234 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
9235 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
9236 if (!CSize || !isOnlyUsedInZeroEqualityComparison(&I))
9237 return false;
9238
9239 // If the target has a fast compare for the given size, it will return a
9240 // preferred load type for that size. Require that the load VT is legal and
9241 // that the target supports unaligned loads of that type. Otherwise, return
9242 // INVALID.
9243 auto hasFastLoadsAndCompare = [&](unsigned NumBits) {
9244 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9245 MVT LVT = TLI.hasFastEqualityCompare(NumBits);
9246 if (LVT != MVT::INVALID_SIMPLE_VALUE_TYPE) {
9247 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
9248 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
9249 // TODO: Check alignment of src and dest ptrs.
9250 unsigned DstAS = LHS->getType()->getPointerAddressSpace();
9251 unsigned SrcAS = RHS->getType()->getPointerAddressSpace();
9252 if (!TLI.isTypeLegal(LVT) ||
9253 !TLI.allowsMisalignedMemoryAccesses(LVT, SrcAS) ||
9254 !TLI.allowsMisalignedMemoryAccesses(LVT, DstAS))
9256 }
9257
9258 return LVT;
9259 };
9260
9261 // This turns into unaligned loads. We only do this if the target natively
9262 // supports the MVT we'll be loading or if it is small enough (<= 4) that
9263 // we'll only produce a small number of byte loads.
9264 MVT LoadVT;
9265 unsigned NumBitsToCompare = CSize->getZExtValue() * 8;
9266 switch (NumBitsToCompare) {
9267 default:
9268 return false;
9269 case 16:
9270 LoadVT = MVT::i16;
9271 break;
9272 case 32:
9273 LoadVT = MVT::i32;
9274 break;
9275 case 64:
9276 case 128:
9277 case 256:
9278 LoadVT = hasFastLoadsAndCompare(NumBitsToCompare);
9279 break;
9280 }
9281
9282 if (LoadVT == MVT::INVALID_SIMPLE_VALUE_TYPE)
9283 return false;
9284
9285 SDValue LoadL = getMemCmpLoad(LHS, LoadVT, *this);
9286 SDValue LoadR = getMemCmpLoad(RHS, LoadVT, *this);
9287
9288 // Bitcast to a wide integer type if the loads are vectors.
9289 if (LoadVT.isVector()) {
9290 EVT CmpVT = EVT::getIntegerVT(LHS->getContext(), LoadVT.getSizeInBits());
9291 LoadL = DAG.getBitcast(CmpVT, LoadL);
9292 LoadR = DAG.getBitcast(CmpVT, LoadR);
9293 }
9294
9295 SDValue Cmp = DAG.getSetCC(getCurSDLoc(), MVT::i1, LoadL, LoadR, ISD::SETNE);
9296 processIntegerCallValue(I, Cmp, false);
9297 return true;
9298}
9299
9300/// See if we can lower a memchr call into an optimized form. If so, return
9301/// true and lower it. Otherwise return false, and it will be lowered like a
9302/// normal call.
9303/// The caller already checked that \p I calls the appropriate LibFunc with a
9304/// correct prototype.
9305bool SelectionDAGBuilder::visitMemChrCall(const CallInst &I) {
9306 const Value *Src = I.getArgOperand(0);
9307 const Value *Char = I.getArgOperand(1);
9308 const Value *Length = I.getArgOperand(2);
9309
9310 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9311 std::pair<SDValue, SDValue> Res =
9312 TSI.EmitTargetCodeForMemchr(DAG, getCurSDLoc(), DAG.getRoot(),
9313 getValue(Src), getValue(Char), getValue(Length),
9314 MachinePointerInfo(Src));
9315 if (Res.first.getNode()) {
9316 setValue(&I, Res.first);
9317 PendingLoads.push_back(Res.second);
9318 return true;
9319 }
9320
9321 return false;
9322}
9323
9324/// See if we can lower a mempcpy call into an optimized form. If so, return
9325/// true and lower it. Otherwise return false, and it will be lowered like a
9326/// normal call.
9327/// The caller already checked that \p I calls the appropriate LibFunc with a
9328/// correct prototype.
9329bool SelectionDAGBuilder::visitMemPCpyCall(const CallInst &I) {
9330 SDValue Dst = getValue(I.getArgOperand(0));
9331 SDValue Src = getValue(I.getArgOperand(1));
9332 SDValue Size = getValue(I.getArgOperand(2));
9333
9334 Align DstAlign = DAG.InferPtrAlign(Dst).valueOrOne();
9335 Align SrcAlign = DAG.InferPtrAlign(Src).valueOrOne();
9336 // DAG::getMemcpy needs Alignment to be defined.
9337 Align Alignment = std::min(DstAlign, SrcAlign);
9338
9339 SDLoc sdl = getCurSDLoc();
9340
9341 // In the mempcpy context we need to pass in a false value for isTailCall
9342 // because the return pointer needs to be adjusted by the size of
9343 // the copied memory.
9344 SDValue Root = getMemoryRoot();
9345 SDValue MC = DAG.getMemcpy(
9346 Root, sdl, Dst, Src, Size, Alignment, false, false, /*CI=*/nullptr,
9347 std::nullopt, MachinePointerInfo(I.getArgOperand(0)),
9348 MachinePointerInfo(I.getArgOperand(1)), I.getAAMetadata());
9349 assert(MC.getNode() != nullptr &&
9350 "** memcpy should not be lowered as TailCall in mempcpy context **");
9351 DAG.setRoot(MC);
9352
9353 // Check if Size needs to be truncated or extended.
9354 Size = DAG.getSExtOrTrunc(Size, sdl, Dst.getValueType());
9355
9356 // Adjust return pointer to point just past the last dst byte.
9357 SDValue DstPlusSize = DAG.getMemBasePlusOffset(Dst, Size, sdl);
9358 setValue(&I, DstPlusSize);
9359 return true;
9360}
9361
9362/// See if we can lower a strcpy call into an optimized form. If so, return
9363/// true and lower it, otherwise return false and it will be lowered like a
9364/// normal call.
9365/// The caller already checked that \p I calls the appropriate LibFunc with a
9366/// correct prototype.
9367bool SelectionDAGBuilder::visitStrCpyCall(const CallInst &I, bool isStpcpy) {
9368 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9369
9370 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9371 std::pair<SDValue, SDValue> Res =
9373 getValue(Arg0), getValue(Arg1),
9374 MachinePointerInfo(Arg0),
9375 MachinePointerInfo(Arg1), isStpcpy);
9376 if (Res.first.getNode()) {
9377 setValue(&I, Res.first);
9378 DAG.setRoot(Res.second);
9379 return true;
9380 }
9381
9382 return false;
9383}
9384
9385/// See if we can lower a strcmp call into an optimized form. If so, return
9386/// true and lower it, otherwise return false and it will be lowered like a
9387/// normal call.
9388/// The caller already checked that \p I calls the appropriate LibFunc with a
9389/// correct prototype.
9390bool SelectionDAGBuilder::visitStrCmpCall(const CallInst &I) {
9391 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9392
9393 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9394 std::pair<SDValue, SDValue> Res =
9395 TSI.EmitTargetCodeForStrcmp(DAG, getCurSDLoc(), DAG.getRoot(),
9396 getValue(Arg0), getValue(Arg1),
9397 MachinePointerInfo(Arg0),
9398 MachinePointerInfo(Arg1));
9399 if (Res.first.getNode()) {
9400 processIntegerCallValue(I, Res.first, true);
9401 PendingLoads.push_back(Res.second);
9402 return true;
9403 }
9404
9405 return false;
9406}
9407
9408/// See if we can lower a strlen call into an optimized form. If so, return
9409/// true and lower it, otherwise return false and it will be lowered like a
9410/// normal call.
9411/// The caller already checked that \p I calls the appropriate LibFunc with a
9412/// correct prototype.
9413bool SelectionDAGBuilder::visitStrLenCall(const CallInst &I) {
9414 const Value *Arg0 = I.getArgOperand(0);
9415
9416 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9417 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrlen(
9418 DAG, getCurSDLoc(), DAG.getRoot(), getValue(Arg0), &I);
9419 if (Res.first.getNode()) {
9420 processIntegerCallValue(I, Res.first, false);
9421 PendingLoads.push_back(Res.second);
9422 return true;
9423 }
9424
9425 return false;
9426}
9427
9428/// See if we can lower a strnlen call into an optimized form. If so, return
9429/// true and lower it, otherwise return false and it will be lowered like a
9430/// normal call.
9431/// The caller already checked that \p I calls the appropriate LibFunc with a
9432/// correct prototype.
9433bool SelectionDAGBuilder::visitStrNLenCall(const CallInst &I) {
9434 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9435
9436 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9437 std::pair<SDValue, SDValue> Res =
9438 TSI.EmitTargetCodeForStrnlen(DAG, getCurSDLoc(), DAG.getRoot(),
9439 getValue(Arg0), getValue(Arg1),
9440 MachinePointerInfo(Arg0));
9441 if (Res.first.getNode()) {
9442 processIntegerCallValue(I, Res.first, false);
9443 PendingLoads.push_back(Res.second);
9444 return true;
9445 }
9446
9447 return false;
9448}
9449
9450/// See if we can lower a unary floating-point operation into an SDNode with
9451/// the specified Opcode. If so, return true and lower it, otherwise return
9452/// false and it will be lowered like a normal call.
9453/// The caller already checked that \p I calls the appropriate LibFunc with a
9454/// correct prototype.
9455bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
9456 unsigned Opcode) {
9457 // We already checked this call's prototype; verify it doesn't modify errno.
9458 // Do not perform optimizations for call sites that require strict
9459 // floating-point semantics.
9460 if (!I.onlyReadsMemory() || I.isStrictFP())
9461 return false;
9462
9463 SDNodeFlags Flags;
9464 Flags.copyFMF(cast<FPMathOperator>(I));
9465
9466 SDValue Tmp = getValue(I.getArgOperand(0));
9467 setValue(&I,
9468 DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp, Flags));
9469 return true;
9470}
9471
9472/// See if we can lower a binary floating-point operation into an SDNode with
9473/// the specified Opcode. If so, return true and lower it. Otherwise return
9474/// false, and it will be lowered like a normal call.
9475/// The caller already checked that \p I calls the appropriate LibFunc with a
9476/// correct prototype.
9477bool SelectionDAGBuilder::visitBinaryFloatCall(const CallInst &I,
9478 unsigned Opcode) {
9479 // We already checked this call's prototype; verify it doesn't modify errno.
9480 // Do not perform optimizations for call sites that require strict
9481 // floating-point semantics.
9482 if (!I.onlyReadsMemory() || I.isStrictFP())
9483 return false;
9484
9485 SDNodeFlags Flags;
9486 Flags.copyFMF(cast<FPMathOperator>(I));
9487
9488 SDValue Tmp0 = getValue(I.getArgOperand(0));
9489 SDValue Tmp1 = getValue(I.getArgOperand(1));
9490 EVT VT = Tmp0.getValueType();
9491 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), VT, Tmp0, Tmp1, Flags));
9492 return true;
9493}
9494
9495void SelectionDAGBuilder::visitCall(const CallInst &I) {
9496 // Handle inline assembly differently.
9497 if (I.isInlineAsm()) {
9498 visitInlineAsm(I);
9499 return;
9500 }
9501
9503
9504 if (Function *F = I.getCalledFunction()) {
9505 if (F->isDeclaration()) {
9506 // Is this an LLVM intrinsic?
9507 if (unsigned IID = F->getIntrinsicID()) {
9508 visitIntrinsicCall(I, IID);
9509 return;
9510 }
9511 }
9512
9513 // Check for well-known libc/libm calls. If the function is internal, it
9514 // can't be a library call. Don't do the check if marked as nobuiltin for
9515 // some reason.
9516 LibFunc Func;
9517 if (!I.isNoBuiltin() && !F->hasLocalLinkage() && F->hasName() &&
9518 LibInfo->getLibFunc(*F, Func) && LibInfo->hasOptimizedCodeGen(Func)) {
9519 switch (Func) {
9520 default: break;
9521 case LibFunc_bcmp:
9522 if (visitMemCmpBCmpCall(I))
9523 return;
9524 break;
9525 case LibFunc_copysign:
9526 case LibFunc_copysignf:
9527 case LibFunc_copysignl:
9528 // We already checked this call's prototype; verify it doesn't modify
9529 // errno.
9530 if (I.onlyReadsMemory()) {
9531 SDValue LHS = getValue(I.getArgOperand(0));
9532 SDValue RHS = getValue(I.getArgOperand(1));
9534 LHS.getValueType(), LHS, RHS));
9535 return;
9536 }
9537 break;
9538 case LibFunc_fabs:
9539 case LibFunc_fabsf:
9540 case LibFunc_fabsl:
9541 if (visitUnaryFloatCall(I, ISD::FABS))
9542 return;
9543 break;
9544 case LibFunc_fmin:
9545 case LibFunc_fminf:
9546 case LibFunc_fminl:
9547 if (visitBinaryFloatCall(I, ISD::FMINNUM))
9548 return;
9549 break;
9550 case LibFunc_fmax:
9551 case LibFunc_fmaxf:
9552 case LibFunc_fmaxl:
9553 if (visitBinaryFloatCall(I, ISD::FMAXNUM))
9554 return;
9555 break;
9556 case LibFunc_fminimum_num:
9557 case LibFunc_fminimum_numf:
9558 case LibFunc_fminimum_numl:
9559 if (visitBinaryFloatCall(I, ISD::FMINIMUMNUM))
9560 return;
9561 break;
9562 case LibFunc_fmaximum_num:
9563 case LibFunc_fmaximum_numf:
9564 case LibFunc_fmaximum_numl:
9565 if (visitBinaryFloatCall(I, ISD::FMAXIMUMNUM))
9566 return;
9567 break;
9568 case LibFunc_sin:
9569 case LibFunc_sinf:
9570 case LibFunc_sinl:
9571 if (visitUnaryFloatCall(I, ISD::FSIN))
9572 return;
9573 break;
9574 case LibFunc_cos:
9575 case LibFunc_cosf:
9576 case LibFunc_cosl:
9577 if (visitUnaryFloatCall(I, ISD::FCOS))
9578 return;
9579 break;
9580 case LibFunc_tan:
9581 case LibFunc_tanf:
9582 case LibFunc_tanl:
9583 if (visitUnaryFloatCall(I, ISD::FTAN))
9584 return;
9585 break;
9586 case LibFunc_asin:
9587 case LibFunc_asinf:
9588 case LibFunc_asinl:
9589 if (visitUnaryFloatCall(I, ISD::FASIN))
9590 return;
9591 break;
9592 case LibFunc_acos:
9593 case LibFunc_acosf:
9594 case LibFunc_acosl:
9595 if (visitUnaryFloatCall(I, ISD::FACOS))
9596 return;
9597 break;
9598 case LibFunc_atan:
9599 case LibFunc_atanf:
9600 case LibFunc_atanl:
9601 if (visitUnaryFloatCall(I, ISD::FATAN))
9602 return;
9603 break;
9604 case LibFunc_atan2:
9605 case LibFunc_atan2f:
9606 case LibFunc_atan2l:
9607 if (visitBinaryFloatCall(I, ISD::FATAN2))
9608 return;
9609 break;
9610 case LibFunc_sinh:
9611 case LibFunc_sinhf:
9612 case LibFunc_sinhl:
9613 if (visitUnaryFloatCall(I, ISD::FSINH))
9614 return;
9615 break;
9616 case LibFunc_cosh:
9617 case LibFunc_coshf:
9618 case LibFunc_coshl:
9619 if (visitUnaryFloatCall(I, ISD::FCOSH))
9620 return;
9621 break;
9622 case LibFunc_tanh:
9623 case LibFunc_tanhf:
9624 case LibFunc_tanhl:
9625 if (visitUnaryFloatCall(I, ISD::FTANH))
9626 return;
9627 break;
9628 case LibFunc_sqrt:
9629 case LibFunc_sqrtf:
9630 case LibFunc_sqrtl:
9631 case LibFunc_sqrt_finite:
9632 case LibFunc_sqrtf_finite:
9633 case LibFunc_sqrtl_finite:
9634 if (visitUnaryFloatCall(I, ISD::FSQRT))
9635 return;
9636 break;
9637 case LibFunc_floor:
9638 case LibFunc_floorf:
9639 case LibFunc_floorl:
9640 if (visitUnaryFloatCall(I, ISD::FFLOOR))
9641 return;
9642 break;
9643 case LibFunc_nearbyint:
9644 case LibFunc_nearbyintf:
9645 case LibFunc_nearbyintl:
9646 if (visitUnaryFloatCall(I, ISD::FNEARBYINT))
9647 return;
9648 break;
9649 case LibFunc_ceil:
9650 case LibFunc_ceilf:
9651 case LibFunc_ceill:
9652 if (visitUnaryFloatCall(I, ISD::FCEIL))
9653 return;
9654 break;
9655 case LibFunc_rint:
9656 case LibFunc_rintf:
9657 case LibFunc_rintl:
9658 if (visitUnaryFloatCall(I, ISD::FRINT))
9659 return;
9660 break;
9661 case LibFunc_round:
9662 case LibFunc_roundf:
9663 case LibFunc_roundl:
9664 if (visitUnaryFloatCall(I, ISD::FROUND))
9665 return;
9666 break;
9667 case LibFunc_trunc:
9668 case LibFunc_truncf:
9669 case LibFunc_truncl:
9670 if (visitUnaryFloatCall(I, ISD::FTRUNC))
9671 return;
9672 break;
9673 case LibFunc_log2:
9674 case LibFunc_log2f:
9675 case LibFunc_log2l:
9676 if (visitUnaryFloatCall(I, ISD::FLOG2))
9677 return;
9678 break;
9679 case LibFunc_exp2:
9680 case LibFunc_exp2f:
9681 case LibFunc_exp2l:
9682 if (visitUnaryFloatCall(I, ISD::FEXP2))
9683 return;
9684 break;
9685 case LibFunc_exp10:
9686 case LibFunc_exp10f:
9687 case LibFunc_exp10l:
9688 if (visitUnaryFloatCall(I, ISD::FEXP10))
9689 return;
9690 break;
9691 case LibFunc_ldexp:
9692 case LibFunc_ldexpf:
9693 case LibFunc_ldexpl:
9694 if (visitBinaryFloatCall(I, ISD::FLDEXP))
9695 return;
9696 break;
9697 case LibFunc_memcmp:
9698 if (visitMemCmpBCmpCall(I))
9699 return;
9700 break;
9701 case LibFunc_mempcpy:
9702 if (visitMemPCpyCall(I))
9703 return;
9704 break;
9705 case LibFunc_memchr:
9706 if (visitMemChrCall(I))
9707 return;
9708 break;
9709 case LibFunc_strcpy:
9710 if (visitStrCpyCall(I, false))
9711 return;
9712 break;
9713 case LibFunc_stpcpy:
9714 if (visitStrCpyCall(I, true))
9715 return;
9716 break;
9717 case LibFunc_strcmp:
9718 if (visitStrCmpCall(I))
9719 return;
9720 break;
9721 case LibFunc_strlen:
9722 if (visitStrLenCall(I))
9723 return;
9724 break;
9725 case LibFunc_strnlen:
9726 if (visitStrNLenCall(I))
9727 return;
9728 break;
9729 }
9730 }
9731 }
9732
9733 if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
9734 LowerCallSiteWithPtrAuthBundle(cast<CallBase>(I), /*EHPadBB=*/nullptr);
9735 return;
9736 }
9737
9738 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
9739 // have to do anything here to lower funclet bundles.
9740 // CFGuardTarget bundles are lowered in LowerCallTo.
9742 I, "calls",
9747
9748 SDValue Callee = getValue(I.getCalledOperand());
9749
9750 if (I.hasDeoptState())
9751 LowerCallSiteWithDeoptBundle(&I, Callee, nullptr);
9752 else
9753 // Check if we can potentially perform a tail call. More detailed checking
9754 // is be done within LowerCallTo, after more information about the call is
9755 // known.
9756 LowerCallTo(I, Callee, I.isTailCall(), I.isMustTailCall());
9757}
9758
9760 const CallBase &CB, const BasicBlock *EHPadBB) {
9761 auto PAB = CB.getOperandBundle("ptrauth");
9762 const Value *CalleeV = CB.getCalledOperand();
9763
9764 // Gather the call ptrauth data from the operand bundle:
9765 // [ i32 <key>, i64 <discriminator> ]
9766 const auto *Key = cast<ConstantInt>(PAB->Inputs[0]);
9767 const Value *Discriminator = PAB->Inputs[1];
9768
9769 assert(Key->getType()->isIntegerTy(32) && "Invalid ptrauth key");
9770 assert(Discriminator->getType()->isIntegerTy(64) &&
9771 "Invalid ptrauth discriminator");
9772
9773 // Look through ptrauth constants to find the raw callee.
9774 // Do a direct unauthenticated call if we found it and everything matches.
9775 if (const auto *CalleeCPA = dyn_cast<ConstantPtrAuth>(CalleeV))
9776 if (CalleeCPA->isKnownCompatibleWith(Key, Discriminator,
9777 DAG.getDataLayout()))
9778 return LowerCallTo(CB, getValue(CalleeCPA->getPointer()), CB.isTailCall(),
9779 CB.isMustTailCall(), EHPadBB);
9780
9781 // Functions should never be ptrauth-called directly.
9782 assert(!isa<Function>(CalleeV) && "invalid direct ptrauth call");
9783
9784 // Otherwise, do an authenticated indirect call.
9785 TargetLowering::PtrAuthInfo PAI = {Key->getZExtValue(),
9786 getValue(Discriminator)};
9787
9788 LowerCallTo(CB, getValue(CalleeV), CB.isTailCall(), CB.isMustTailCall(),
9789 EHPadBB, &PAI);
9790}
9791
9792namespace {
9793
9794/// AsmOperandInfo - This contains information for each constraint that we are
9795/// lowering.
9796class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
9797public:
9798 /// CallOperand - If this is the result output operand or a clobber
9799 /// this is null, otherwise it is the incoming operand to the CallInst.
9800 /// This gets modified as the asm is processed.
9801 SDValue CallOperand;
9802
9803 /// AssignedRegs - If this is a register or register class operand, this
9804 /// contains the set of register corresponding to the operand.
9805 RegsForValue AssignedRegs;
9806
9807 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
9808 : TargetLowering::AsmOperandInfo(info), CallOperand(nullptr, 0) {
9809 }
9810
9811 /// Whether or not this operand accesses memory
9812 bool hasMemory(const TargetLowering &TLI) const {
9813 // Indirect operand accesses access memory.
9814 if (isIndirect)
9815 return true;
9816
9817 for (const auto &Code : Codes)
9819 return true;
9820
9821 return false;
9822 }
9823};
9824
9825
9826} // end anonymous namespace
9827
9828/// Make sure that the output operand \p OpInfo and its corresponding input
9829/// operand \p MatchingOpInfo have compatible constraint types (otherwise error
9830/// out).
9831static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo,
9832 SDISelAsmOperandInfo &MatchingOpInfo,
9833 SelectionDAG &DAG) {
9834 if (OpInfo.ConstraintVT == MatchingOpInfo.ConstraintVT)
9835 return;
9836
9838 const auto &TLI = DAG.getTargetLoweringInfo();
9839
9840 std::pair<unsigned, const TargetRegisterClass *> MatchRC =
9841 TLI.getRegForInlineAsmConstraint(TRI, OpInfo.ConstraintCode,
9842 OpInfo.ConstraintVT);
9843 std::pair<unsigned, const TargetRegisterClass *> InputRC =
9844 TLI.getRegForInlineAsmConstraint(TRI, MatchingOpInfo.ConstraintCode,
9845 MatchingOpInfo.ConstraintVT);
9846 const bool OutOpIsIntOrFP =
9847 OpInfo.ConstraintVT.isInteger() || OpInfo.ConstraintVT.isFloatingPoint();
9848 const bool InOpIsIntOrFP = MatchingOpInfo.ConstraintVT.isInteger() ||
9849 MatchingOpInfo.ConstraintVT.isFloatingPoint();
9850 if ((OutOpIsIntOrFP != InOpIsIntOrFP) || (MatchRC.second != InputRC.second)) {
9851 // FIXME: error out in a more elegant fashion
9852 report_fatal_error("Unsupported asm: input constraint"
9853 " with a matching output constraint of"
9854 " incompatible type!");
9855 }
9856 MatchingOpInfo.ConstraintVT = OpInfo.ConstraintVT;
9857}
9858
9859/// Get a direct memory input to behave well as an indirect operand.
9860/// This may introduce stores, hence the need for a \p Chain.
9861/// \return The (possibly updated) chain.
9862static SDValue getAddressForMemoryInput(SDValue Chain, const SDLoc &Location,
9863 SDISelAsmOperandInfo &OpInfo,
9864 SelectionDAG &DAG) {
9865 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9866
9867 // If we don't have an indirect input, put it in the constpool if we can,
9868 // otherwise spill it to a stack slot.
9869 // TODO: This isn't quite right. We need to handle these according to
9870 // the addressing mode that the constraint wants. Also, this may take
9871 // an additional register for the computation and we don't want that
9872 // either.
9873
9874 // If the operand is a float, integer, or vector constant, spill to a
9875 // constant pool entry to get its address.
9876 const Value *OpVal = OpInfo.CallOperandVal;
9877 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
9879 OpInfo.CallOperand = DAG.getConstantPool(
9880 cast<Constant>(OpVal), TLI.getPointerTy(DAG.getDataLayout()));
9881 return Chain;
9882 }
9883
9884 // Otherwise, create a stack slot and emit a store to it before the asm.
9885 Type *Ty = OpVal->getType();
9886 auto &DL = DAG.getDataLayout();
9887 TypeSize TySize = DL.getTypeAllocSize(Ty);
9890 int StackID = 0;
9891 if (TySize.isScalable())
9892 StackID = TFI->getStackIDForScalableVectors();
9893 int SSFI = MF.getFrameInfo().CreateStackObject(TySize.getKnownMinValue(),
9894 DL.getPrefTypeAlign(Ty), false,
9895 nullptr, StackID);
9896 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getFrameIndexTy(DL));
9897 Chain = DAG.getTruncStore(Chain, Location, OpInfo.CallOperand, StackSlot,
9899 TLI.getMemValueType(DL, Ty));
9900 OpInfo.CallOperand = StackSlot;
9901
9902 return Chain;
9903}
9904
9905/// GetRegistersForValue - Assign registers (virtual or physical) for the
9906/// specified operand. We prefer to assign virtual registers, to allow the
9907/// register allocator to handle the assignment process. However, if the asm
9908/// uses features that we can't model on machineinstrs, we have SDISel do the
9909/// allocation. This produces generally horrible, but correct, code.
9910///
9911/// OpInfo describes the operand
9912/// RefOpInfo describes the matching operand if any, the operand otherwise
9913static std::optional<unsigned>
9915 SDISelAsmOperandInfo &OpInfo,
9916 SDISelAsmOperandInfo &RefOpInfo) {
9917 LLVMContext &Context = *DAG.getContext();
9918 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9919
9923
9924 // No work to do for memory/address operands.
9925 if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
9926 OpInfo.ConstraintType == TargetLowering::C_Address)
9927 return std::nullopt;
9928
9929 // If this is a constraint for a single physreg, or a constraint for a
9930 // register class, find it.
9931 unsigned AssignedReg;
9932 const TargetRegisterClass *RC;
9933 std::tie(AssignedReg, RC) = TLI.getRegForInlineAsmConstraint(
9934 &TRI, RefOpInfo.ConstraintCode, RefOpInfo.ConstraintVT);
9935 // RC is unset only on failure. Return immediately.
9936 if (!RC)
9937 return std::nullopt;
9938
9939 // Get the actual register value type. This is important, because the user
9940 // may have asked for (e.g.) the AX register in i32 type. We need to
9941 // remember that AX is actually i16 to get the right extension.
9942 const MVT RegVT = *TRI.legalclasstypes_begin(*RC);
9943
9944 if (OpInfo.ConstraintVT != MVT::Other && RegVT != MVT::Untyped) {
9945 // If this is an FP operand in an integer register (or visa versa), or more
9946 // generally if the operand value disagrees with the register class we plan
9947 // to stick it in, fix the operand type.
9948 //
9949 // If this is an input value, the bitcast to the new type is done now.
9950 // Bitcast for output value is done at the end of visitInlineAsm().
9951 if ((OpInfo.Type == InlineAsm::isOutput ||
9952 OpInfo.Type == InlineAsm::isInput) &&
9953 !TRI.isTypeLegalForClass(*RC, OpInfo.ConstraintVT)) {
9954 // Try to convert to the first EVT that the reg class contains. If the
9955 // types are identical size, use a bitcast to convert (e.g. two differing
9956 // vector types). Note: output bitcast is done at the end of
9957 // visitInlineAsm().
9958 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
9959 // Exclude indirect inputs while they are unsupported because the code
9960 // to perform the load is missing and thus OpInfo.CallOperand still
9961 // refers to the input address rather than the pointed-to value.
9962 if (OpInfo.Type == InlineAsm::isInput && !OpInfo.isIndirect)
9963 OpInfo.CallOperand =
9964 DAG.getNode(ISD::BITCAST, DL, RegVT, OpInfo.CallOperand);
9965 OpInfo.ConstraintVT = RegVT;
9966 // If the operand is an FP value and we want it in integer registers,
9967 // use the corresponding integer type. This turns an f64 value into
9968 // i64, which can be passed with two i32 values on a 32-bit machine.
9969 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
9970 MVT VT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
9971 if (OpInfo.Type == InlineAsm::isInput)
9972 OpInfo.CallOperand =
9973 DAG.getNode(ISD::BITCAST, DL, VT, OpInfo.CallOperand);
9974 OpInfo.ConstraintVT = VT;
9975 }
9976 }
9977 }
9978
9979 // No need to allocate a matching input constraint since the constraint it's
9980 // matching to has already been allocated.
9981 if (OpInfo.isMatchingInputConstraint())
9982 return std::nullopt;
9983
9984 EVT ValueVT = OpInfo.ConstraintVT;
9985 if (OpInfo.ConstraintVT == MVT::Other)
9986 ValueVT = RegVT;
9987
9988 // Initialize NumRegs.
9989 unsigned NumRegs = 1;
9990 if (OpInfo.ConstraintVT != MVT::Other)
9991 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT, RegVT);
9992
9993 // If this is a constraint for a specific physical register, like {r17},
9994 // assign it now.
9995
9996 // If this associated to a specific register, initialize iterator to correct
9997 // place. If virtual, make sure we have enough registers
9998
9999 // Initialize iterator if necessary
10002
10003 // Do not check for single registers.
10004 if (AssignedReg) {
10005 I = std::find(I, RC->end(), AssignedReg);
10006 if (I == RC->end()) {
10007 // RC does not contain the selected register, which indicates a
10008 // mismatch between the register and the required type/bitwidth.
10009 return {AssignedReg};
10010 }
10011 }
10012
10013 for (; NumRegs; --NumRegs, ++I) {
10014 assert(I != RC->end() && "Ran out of registers to allocate!");
10015 Register R = AssignedReg ? Register(*I) : RegInfo.createVirtualRegister(RC);
10016 Regs.push_back(R);
10017 }
10018
10019 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
10020 return std::nullopt;
10021}
10022
10023static unsigned
10025 const std::vector<SDValue> &AsmNodeOperands) {
10026 // Scan until we find the definition we already emitted of this operand.
10027 unsigned CurOp = InlineAsm::Op_FirstOperand;
10028 for (; OperandNo; --OperandNo) {
10029 // Advance to the next operand.
10030 unsigned OpFlag = AsmNodeOperands[CurOp]->getAsZExtVal();
10031 const InlineAsm::Flag F(OpFlag);
10032 assert(
10033 (F.isRegDefKind() || F.isRegDefEarlyClobberKind() || F.isMemKind()) &&
10034 "Skipped past definitions?");
10035 CurOp += F.getNumOperandRegisters() + 1;
10036 }
10037 return CurOp;
10038}
10039
10040namespace {
10041
10042class ExtraFlags {
10043 unsigned Flags = 0;
10044
10045public:
10046 explicit ExtraFlags(const CallBase &Call) {
10047 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
10048 if (IA->hasSideEffects())
10050 if (IA->isAlignStack())
10052 if (Call.isConvergent())
10054 Flags |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
10055 }
10056
10057 void update(const TargetLowering::AsmOperandInfo &OpInfo) {
10058 // Ideally, we would only check against memory constraints. However, the
10059 // meaning of an Other constraint can be target-specific and we can't easily
10060 // reason about it. Therefore, be conservative and set MayLoad/MayStore
10061 // for Other constraints as well.
10064 if (OpInfo.Type == InlineAsm::isInput)
10066 else if (OpInfo.Type == InlineAsm::isOutput)
10068 else if (OpInfo.Type == InlineAsm::isClobber)
10070 }
10071 }
10072
10073 unsigned get() const { return Flags; }
10074};
10075
10076} // end anonymous namespace
10077
10078static bool isFunction(SDValue Op) {
10079 if (Op && Op.getOpcode() == ISD::GlobalAddress) {
10080 if (auto *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
10081 auto Fn = dyn_cast_or_null<Function>(GA->getGlobal());
10082
10083 // In normal "call dllimport func" instruction (non-inlineasm) it force
10084 // indirect access by specifing call opcode. And usually specially print
10085 // asm with indirect symbol (i.g: "*") according to opcode. Inline asm can
10086 // not do in this way now. (In fact, this is similar with "Data Access"
10087 // action). So here we ignore dllimport function.
10088 if (Fn && !Fn->hasDLLImportStorageClass())
10089 return true;
10090 }
10091 }
10092 return false;
10093}
10094
10095/// visitInlineAsm - Handle a call to an InlineAsm object.
10096void SelectionDAGBuilder::visitInlineAsm(const CallBase &Call,
10097 const BasicBlock *EHPadBB) {
10098 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
10099
10100 /// ConstraintOperands - Information about all of the constraints.
10101 SmallVector<SDISelAsmOperandInfo, 16> ConstraintOperands;
10102
10103 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10105 DAG.getDataLayout(), DAG.getSubtarget().getRegisterInfo(), Call);
10106
10107 // First Pass: Calculate HasSideEffects and ExtraFlags (AlignStack,
10108 // AsmDialect, MayLoad, MayStore).
10109 bool HasSideEffect = IA->hasSideEffects();
10110 ExtraFlags ExtraInfo(Call);
10111
10112 for (auto &T : TargetConstraints) {
10113 ConstraintOperands.push_back(SDISelAsmOperandInfo(T));
10114 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
10115
10116 if (OpInfo.CallOperandVal)
10117 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
10118
10119 if (!HasSideEffect)
10120 HasSideEffect = OpInfo.hasMemory(TLI);
10121
10122 // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
10123 // FIXME: Could we compute this on OpInfo rather than T?
10124
10125 // Compute the constraint code and ConstraintType to use.
10127
10128 if (T.ConstraintType == TargetLowering::C_Immediate &&
10129 OpInfo.CallOperand && !isa<ConstantSDNode>(OpInfo.CallOperand))
10130 // We've delayed emitting a diagnostic like the "n" constraint because
10131 // inlining could cause an integer showing up.
10132 return emitInlineAsmError(Call, "constraint '" + Twine(T.ConstraintCode) +
10133 "' expects an integer constant "
10134 "expression");
10135
10136 ExtraInfo.update(T);
10137 }
10138
10139 // We won't need to flush pending loads if this asm doesn't touch
10140 // memory and is nonvolatile.
10141 SDValue Glue, Chain = (HasSideEffect) ? getRoot() : DAG.getRoot();
10142
10143 bool EmitEHLabels = isa<InvokeInst>(Call);
10144 if (EmitEHLabels) {
10145 assert(EHPadBB && "InvokeInst must have an EHPadBB");
10146 }
10147 bool IsCallBr = isa<CallBrInst>(Call);
10148
10149 if (IsCallBr || EmitEHLabels) {
10150 // If this is a callbr or invoke we need to flush pending exports since
10151 // inlineasm_br and invoke are terminators.
10152 // We need to do this before nodes are glued to the inlineasm_br node.
10153 Chain = getControlRoot();
10154 }
10155
10156 MCSymbol *BeginLabel = nullptr;
10157 if (EmitEHLabels) {
10158 Chain = lowerStartEH(Chain, EHPadBB, BeginLabel);
10159 }
10160
10161 int OpNo = -1;
10162 SmallVector<StringRef> AsmStrs;
10163 IA->collectAsmStrs(AsmStrs);
10164
10165 // Second pass over the constraints: compute which constraint option to use.
10166 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10167 if (OpInfo.hasArg() || OpInfo.Type == InlineAsm::isOutput)
10168 OpNo++;
10169
10170 // If this is an output operand with a matching input operand, look up the
10171 // matching input. If their types mismatch, e.g. one is an integer, the
10172 // other is floating point, or their sizes are different, flag it as an
10173 // error.
10174 if (OpInfo.hasMatchingInput()) {
10175 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
10176 patchMatchingInput(OpInfo, Input, DAG);
10177 }
10178
10179 // Compute the constraint code and ConstraintType to use.
10180 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
10181
10182 if ((OpInfo.ConstraintType == TargetLowering::C_Memory &&
10183 OpInfo.Type == InlineAsm::isClobber) ||
10184 OpInfo.ConstraintType == TargetLowering::C_Address)
10185 continue;
10186
10187 // In Linux PIC model, there are 4 cases about value/label addressing:
10188 //
10189 // 1: Function call or Label jmp inside the module.
10190 // 2: Data access (such as global variable, static variable) inside module.
10191 // 3: Function call or Label jmp outside the module.
10192 // 4: Data access (such as global variable) outside the module.
10193 //
10194 // Due to current llvm inline asm architecture designed to not "recognize"
10195 // the asm code, there are quite troubles for us to treat mem addressing
10196 // differently for same value/adress used in different instuctions.
10197 // For example, in pic model, call a func may in plt way or direclty
10198 // pc-related, but lea/mov a function adress may use got.
10199 //
10200 // Here we try to "recognize" function call for the case 1 and case 3 in
10201 // inline asm. And try to adjust the constraint for them.
10202 //
10203 // TODO: Due to current inline asm didn't encourage to jmp to the outsider
10204 // label, so here we don't handle jmp function label now, but we need to
10205 // enhance it (especilly in PIC model) if we meet meaningful requirements.
10206 if (OpInfo.isIndirect && isFunction(OpInfo.CallOperand) &&
10207 TLI.isInlineAsmTargetBranch(AsmStrs, OpNo) &&
10208 TM.getCodeModel() != CodeModel::Large) {
10209 OpInfo.isIndirect = false;
10210 OpInfo.ConstraintType = TargetLowering::C_Address;
10211 }
10212
10213 // If this is a memory input, and if the operand is not indirect, do what we
10214 // need to provide an address for the memory input.
10215 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
10216 !OpInfo.isIndirect) {
10217 assert((OpInfo.isMultipleAlternative ||
10218 (OpInfo.Type == InlineAsm::isInput)) &&
10219 "Can only indirectify direct input operands!");
10220
10221 // Memory operands really want the address of the value.
10222 Chain = getAddressForMemoryInput(Chain, getCurSDLoc(), OpInfo, DAG);
10223
10224 // There is no longer a Value* corresponding to this operand.
10225 OpInfo.CallOperandVal = nullptr;
10226
10227 // It is now an indirect operand.
10228 OpInfo.isIndirect = true;
10229 }
10230
10231 }
10232
10233 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
10234 std::vector<SDValue> AsmNodeOperands;
10235 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
10236 AsmNodeOperands.push_back(DAG.getTargetExternalSymbol(
10237 IA->getAsmString().data(), TLI.getProgramPointerTy(DAG.getDataLayout())));
10238
10239 // If we have a !srcloc metadata node associated with it, we want to attach
10240 // this to the ultimately generated inline asm machineinstr. To do this, we
10241 // pass in the third operand as this (potentially null) inline asm MDNode.
10242 const MDNode *SrcLoc = Call.getMetadata("srcloc");
10243 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
10244
10245 // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
10246 // bits as operand 3.
10247 AsmNodeOperands.push_back(DAG.getTargetConstant(
10248 ExtraInfo.get(), getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10249
10250 // Third pass: Loop over operands to prepare DAG-level operands.. As part of
10251 // this, assign virtual and physical registers for inputs and otput.
10252 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10253 // Assign Registers.
10254 SDISelAsmOperandInfo &RefOpInfo =
10255 OpInfo.isMatchingInputConstraint()
10256 ? ConstraintOperands[OpInfo.getMatchedOperand()]
10257 : OpInfo;
10258 const auto RegError =
10259 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, RefOpInfo);
10260 if (RegError) {
10261 const MachineFunction &MF = DAG.getMachineFunction();
10262 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10263 const char *RegName = TRI.getName(*RegError);
10264 emitInlineAsmError(Call, "register '" + Twine(RegName) +
10265 "' allocated for constraint '" +
10266 Twine(OpInfo.ConstraintCode) +
10267 "' does not match required type");
10268 return;
10269 }
10270
10271 auto DetectWriteToReservedRegister = [&]() {
10272 const MachineFunction &MF = DAG.getMachineFunction();
10273 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10274 for (Register Reg : OpInfo.AssignedRegs.Regs) {
10275 if (Reg.isPhysical() && TRI.isInlineAsmReadOnlyReg(MF, Reg)) {
10276 const char *RegName = TRI.getName(Reg);
10277 emitInlineAsmError(Call, "write to reserved register '" +
10278 Twine(RegName) + "'");
10279 return true;
10280 }
10281 }
10282 return false;
10283 };
10284 assert((OpInfo.ConstraintType != TargetLowering::C_Address ||
10285 (OpInfo.Type == InlineAsm::isInput &&
10286 !OpInfo.isMatchingInputConstraint())) &&
10287 "Only address as input operand is allowed.");
10288
10289 switch (OpInfo.Type) {
10291 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10292 const InlineAsm::ConstraintCode ConstraintID =
10293 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10295 "Failed to convert memory constraint code to constraint id.");
10296
10297 // Add information to the INLINEASM node to know about this output.
10298 InlineAsm::Flag OpFlags(InlineAsm::Kind::Mem, 1);
10299 OpFlags.setMemConstraint(ConstraintID);
10300 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags, getCurSDLoc(),
10301 MVT::i32));
10302 AsmNodeOperands.push_back(OpInfo.CallOperand);
10303 } else {
10304 // Otherwise, this outputs to a register (directly for C_Register /
10305 // C_RegisterClass, and a target-defined fashion for
10306 // C_Immediate/C_Other). Find a register that we can use.
10307 if (OpInfo.AssignedRegs.Regs.empty()) {
10308 emitInlineAsmError(
10309 Call, "couldn't allocate output register for constraint '" +
10310 Twine(OpInfo.ConstraintCode) + "'");
10311 return;
10312 }
10313
10314 if (DetectWriteToReservedRegister())
10315 return;
10316
10317 // Add information to the INLINEASM node to know that this register is
10318 // set.
10319 OpInfo.AssignedRegs.AddInlineAsmOperands(
10320 OpInfo.isEarlyClobber ? InlineAsm::Kind::RegDefEarlyClobber
10322 false, 0, getCurSDLoc(), DAG, AsmNodeOperands);
10323 }
10324 break;
10325
10326 case InlineAsm::isInput:
10327 case InlineAsm::isLabel: {
10328 SDValue InOperandVal = OpInfo.CallOperand;
10329
10330 if (OpInfo.isMatchingInputConstraint()) {
10331 // If this is required to match an output register we have already set,
10332 // just use its register.
10333 auto CurOp = findMatchingInlineAsmOperand(OpInfo.getMatchedOperand(),
10334 AsmNodeOperands);
10335 InlineAsm::Flag Flag(AsmNodeOperands[CurOp]->getAsZExtVal());
10336 if (Flag.isRegDefKind() || Flag.isRegDefEarlyClobberKind()) {
10337 if (OpInfo.isIndirect) {
10338 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
10339 emitInlineAsmError(Call, "inline asm not supported yet: "
10340 "don't know how to handle tied "
10341 "indirect register inputs");
10342 return;
10343 }
10344
10346 MachineFunction &MF = DAG.getMachineFunction();
10347 MachineRegisterInfo &MRI = MF.getRegInfo();
10348 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10349 auto *R = cast<RegisterSDNode>(AsmNodeOperands[CurOp+1]);
10350 Register TiedReg = R->getReg();
10351 MVT RegVT = R->getSimpleValueType(0);
10352 const TargetRegisterClass *RC =
10353 TiedReg.isVirtual() ? MRI.getRegClass(TiedReg)
10354 : RegVT != MVT::Untyped ? TLI.getRegClassFor(RegVT)
10355 : TRI.getMinimalPhysRegClass(TiedReg);
10356 for (unsigned i = 0, e = Flag.getNumOperandRegisters(); i != e; ++i)
10357 Regs.push_back(MRI.createVirtualRegister(RC));
10358
10359 RegsForValue MatchedRegs(Regs, RegVT, InOperandVal.getValueType());
10360
10361 SDLoc dl = getCurSDLoc();
10362 // Use the produced MatchedRegs object to
10363 MatchedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue, &Call);
10364 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, true,
10365 OpInfo.getMatchedOperand(), dl, DAG,
10366 AsmNodeOperands);
10367 break;
10368 }
10369
10370 assert(Flag.isMemKind() && "Unknown matching constraint!");
10371 assert(Flag.getNumOperandRegisters() == 1 &&
10372 "Unexpected number of operands");
10373 // Add information to the INLINEASM node to know about this input.
10374 // See InlineAsm.h isUseOperandTiedToDef.
10375 Flag.clearMemConstraint();
10376 Flag.setMatchingOp(OpInfo.getMatchedOperand());
10377 AsmNodeOperands.push_back(DAG.getTargetConstant(
10378 Flag, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10379 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
10380 break;
10381 }
10382
10383 // Treat indirect 'X' constraint as memory.
10384 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
10385 OpInfo.isIndirect)
10386 OpInfo.ConstraintType = TargetLowering::C_Memory;
10387
10388 if (OpInfo.ConstraintType == TargetLowering::C_Immediate ||
10389 OpInfo.ConstraintType == TargetLowering::C_Other) {
10390 std::vector<SDValue> Ops;
10391 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
10392 Ops, DAG);
10393 if (Ops.empty()) {
10394 if (OpInfo.ConstraintType == TargetLowering::C_Immediate)
10395 if (isa<ConstantSDNode>(InOperandVal)) {
10396 emitInlineAsmError(Call, "value out of range for constraint '" +
10397 Twine(OpInfo.ConstraintCode) + "'");
10398 return;
10399 }
10400
10401 emitInlineAsmError(Call,
10402 "invalid operand for inline asm constraint '" +
10403 Twine(OpInfo.ConstraintCode) + "'");
10404 return;
10405 }
10406
10407 // Add information to the INLINEASM node to know about this input.
10408 InlineAsm::Flag ResOpType(InlineAsm::Kind::Imm, Ops.size());
10409 AsmNodeOperands.push_back(DAG.getTargetConstant(
10410 ResOpType, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10411 llvm::append_range(AsmNodeOperands, Ops);
10412 break;
10413 }
10414
10415 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10416 assert((OpInfo.isIndirect ||
10417 OpInfo.ConstraintType != TargetLowering::C_Memory) &&
10418 "Operand must be indirect to be a mem!");
10419 assert(InOperandVal.getValueType() ==
10420 TLI.getPointerTy(DAG.getDataLayout()) &&
10421 "Memory operands expect pointer values");
10422
10423 const InlineAsm::ConstraintCode ConstraintID =
10424 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10426 "Failed to convert memory constraint code to constraint id.");
10427
10428 // Add information to the INLINEASM node to know about this input.
10429 InlineAsm::Flag ResOpType(InlineAsm::Kind::Mem, 1);
10430 ResOpType.setMemConstraint(ConstraintID);
10431 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
10432 getCurSDLoc(),
10433 MVT::i32));
10434 AsmNodeOperands.push_back(InOperandVal);
10435 break;
10436 }
10437
10438 if (OpInfo.ConstraintType == TargetLowering::C_Address) {
10439 const InlineAsm::ConstraintCode ConstraintID =
10440 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10442 "Failed to convert memory constraint code to constraint id.");
10443
10444 InlineAsm::Flag ResOpType(InlineAsm::Kind::Mem, 1);
10445
10446 SDValue AsmOp = InOperandVal;
10447 if (isFunction(InOperandVal)) {
10448 auto *GA = cast<GlobalAddressSDNode>(InOperandVal);
10449 ResOpType = InlineAsm::Flag(InlineAsm::Kind::Func, 1);
10450 AsmOp = DAG.getTargetGlobalAddress(GA->getGlobal(), getCurSDLoc(),
10451 InOperandVal.getValueType(),
10452 GA->getOffset());
10453 }
10454
10455 // Add information to the INLINEASM node to know about this input.
10456 ResOpType.setMemConstraint(ConstraintID);
10457
10458 AsmNodeOperands.push_back(
10459 DAG.getTargetConstant(ResOpType, getCurSDLoc(), MVT::i32));
10460
10461 AsmNodeOperands.push_back(AsmOp);
10462 break;
10463 }
10464
10465 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
10466 OpInfo.ConstraintType != TargetLowering::C_Register) {
10467 emitInlineAsmError(Call, "unknown asm constraint '" +
10468 Twine(OpInfo.ConstraintCode) + "'");
10469 return;
10470 }
10471
10472 // TODO: Support this.
10473 if (OpInfo.isIndirect) {
10474 emitInlineAsmError(
10475 Call, "Don't know how to handle indirect register inputs yet "
10476 "for constraint '" +
10477 Twine(OpInfo.ConstraintCode) + "'");
10478 return;
10479 }
10480
10481 // Copy the input into the appropriate registers.
10482 if (OpInfo.AssignedRegs.Regs.empty()) {
10483 emitInlineAsmError(Call,
10484 "couldn't allocate input reg for constraint '" +
10485 Twine(OpInfo.ConstraintCode) + "'");
10486 return;
10487 }
10488
10489 if (DetectWriteToReservedRegister())
10490 return;
10491
10492 SDLoc dl = getCurSDLoc();
10493
10494 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue,
10495 &Call);
10496
10497 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, false,
10498 0, dl, DAG, AsmNodeOperands);
10499 break;
10500 }
10502 // Add the clobbered value to the operand list, so that the register
10503 // allocator is aware that the physreg got clobbered.
10504 if (!OpInfo.AssignedRegs.Regs.empty())
10506 false, 0, getCurSDLoc(), DAG,
10507 AsmNodeOperands);
10508 break;
10509 }
10510 }
10511
10512 // Finish up input operands. Set the input chain and add the flag last.
10513 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
10514 if (Glue.getNode()) AsmNodeOperands.push_back(Glue);
10515
10516 unsigned ISDOpc = IsCallBr ? ISD::INLINEASM_BR : ISD::INLINEASM;
10517 Chain = DAG.getNode(ISDOpc, getCurSDLoc(),
10518 DAG.getVTList(MVT::Other, MVT::Glue), AsmNodeOperands);
10519 Glue = Chain.getValue(1);
10520
10521 // Do additional work to generate outputs.
10522
10523 SmallVector<EVT, 1> ResultVTs;
10524 SmallVector<SDValue, 1> ResultValues;
10525 SmallVector<SDValue, 8> OutChains;
10526
10527 llvm::Type *CallResultType = Call.getType();
10528 ArrayRef<Type *> ResultTypes;
10529 if (StructType *StructResult = dyn_cast<StructType>(CallResultType))
10530 ResultTypes = StructResult->elements();
10531 else if (!CallResultType->isVoidTy())
10532 ResultTypes = ArrayRef(CallResultType);
10533
10534 auto CurResultType = ResultTypes.begin();
10535 auto handleRegAssign = [&](SDValue V) {
10536 assert(CurResultType != ResultTypes.end() && "Unexpected value");
10537 assert((*CurResultType)->isSized() && "Unexpected unsized type");
10538 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), *CurResultType);
10539 ++CurResultType;
10540 // If the type of the inline asm call site return value is different but has
10541 // same size as the type of the asm output bitcast it. One example of this
10542 // is for vectors with different width / number of elements. This can
10543 // happen for register classes that can contain multiple different value
10544 // types. The preg or vreg allocated may not have the same VT as was
10545 // expected.
10546 //
10547 // This can also happen for a return value that disagrees with the register
10548 // class it is put in, eg. a double in a general-purpose register on a
10549 // 32-bit machine.
10550 if (ResultVT != V.getValueType() &&
10551 ResultVT.getSizeInBits() == V.getValueSizeInBits())
10552 V = DAG.getNode(ISD::BITCAST, getCurSDLoc(), ResultVT, V);
10553 else if (ResultVT != V.getValueType() && ResultVT.isInteger() &&
10554 V.getValueType().isInteger()) {
10555 // If a result value was tied to an input value, the computed result
10556 // may have a wider width than the expected result. Extract the
10557 // relevant portion.
10558 V = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultVT, V);
10559 }
10560 assert(ResultVT == V.getValueType() && "Asm result value mismatch!");
10561 ResultVTs.push_back(ResultVT);
10562 ResultValues.push_back(V);
10563 };
10564
10565 // Deal with output operands.
10566 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10567 if (OpInfo.Type == InlineAsm::isOutput) {
10568 SDValue Val;
10569 // Skip trivial output operands.
10570 if (OpInfo.AssignedRegs.Regs.empty())
10571 continue;
10572
10573 switch (OpInfo.ConstraintType) {
10576 Val = OpInfo.AssignedRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
10577 Chain, &Glue, &Call);
10578 break;
10581 Val = TLI.LowerAsmOutputForConstraint(Chain, Glue, getCurSDLoc(),
10582 OpInfo, DAG);
10583 break;
10585 break; // Already handled.
10587 break; // Silence warning.
10589 assert(false && "Unexpected unknown constraint");
10590 }
10591
10592 // Indirect output manifest as stores. Record output chains.
10593 if (OpInfo.isIndirect) {
10594 const Value *Ptr = OpInfo.CallOperandVal;
10595 assert(Ptr && "Expected value CallOperandVal for indirect asm operand");
10596 SDValue Store = DAG.getStore(Chain, getCurSDLoc(), Val, getValue(Ptr),
10597 MachinePointerInfo(Ptr));
10598 OutChains.push_back(Store);
10599 } else {
10600 // generate CopyFromRegs to associated registers.
10601 assert(!Call.getType()->isVoidTy() && "Bad inline asm!");
10602 if (Val.getOpcode() == ISD::MERGE_VALUES) {
10603 for (const SDValue &V : Val->op_values())
10604 handleRegAssign(V);
10605 } else
10606 handleRegAssign(Val);
10607 }
10608 }
10609 }
10610
10611 // Set results.
10612 if (!ResultValues.empty()) {
10613 assert(CurResultType == ResultTypes.end() &&
10614 "Mismatch in number of ResultTypes");
10615 assert(ResultValues.size() == ResultTypes.size() &&
10616 "Mismatch in number of output operands in asm result");
10617
10619 DAG.getVTList(ResultVTs), ResultValues);
10620 setValue(&Call, V);
10621 }
10622
10623 // Collect store chains.
10624 if (!OutChains.empty())
10625 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, OutChains);
10626
10627 if (EmitEHLabels) {
10628 Chain = lowerEndEH(Chain, cast<InvokeInst>(&Call), EHPadBB, BeginLabel);
10629 }
10630
10631 // Only Update Root if inline assembly has a memory effect.
10632 if (ResultValues.empty() || HasSideEffect || !OutChains.empty() || IsCallBr ||
10633 EmitEHLabels)
10634 DAG.setRoot(Chain);
10635}
10636
10637void SelectionDAGBuilder::emitInlineAsmError(const CallBase &Call,
10638 const Twine &Message) {
10639 LLVMContext &Ctx = *DAG.getContext();
10640 Ctx.diagnose(DiagnosticInfoInlineAsm(Call, Message));
10641
10642 // Make sure we leave the DAG in a valid state
10643 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10644 SmallVector<EVT, 1> ValueVTs;
10645 ComputeValueVTs(TLI, DAG.getDataLayout(), Call.getType(), ValueVTs);
10646
10647 if (ValueVTs.empty())
10648 return;
10649
10651 for (const EVT &VT : ValueVTs)
10652 Ops.push_back(DAG.getUNDEF(VT));
10653
10654 setValue(&Call, DAG.getMergeValues(Ops, getCurSDLoc()));
10655}
10656
10657void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
10658 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(),
10659 MVT::Other, getRoot(),
10660 getValue(I.getArgOperand(0)),
10661 DAG.getSrcValue(I.getArgOperand(0))));
10662}
10663
10664void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
10665 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10666 const DataLayout &DL = DAG.getDataLayout();
10667 SDValue V = DAG.getVAArg(
10668 TLI.getMemValueType(DAG.getDataLayout(), I.getType()), getCurSDLoc(),
10669 getRoot(), getValue(I.getOperand(0)), DAG.getSrcValue(I.getOperand(0)),
10670 DL.getABITypeAlign(I.getType()).value());
10671 DAG.setRoot(V.getValue(1));
10672
10673 if (I.getType()->isPointerTy())
10674 V = DAG.getPtrExtOrTrunc(
10675 V, getCurSDLoc(), TLI.getValueType(DAG.getDataLayout(), I.getType()));
10676 setValue(&I, V);
10677}
10678
10679void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
10680 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(),
10681 MVT::Other, getRoot(),
10682 getValue(I.getArgOperand(0)),
10683 DAG.getSrcValue(I.getArgOperand(0))));
10684}
10685
10686void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
10687 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(),
10688 MVT::Other, getRoot(),
10689 getValue(I.getArgOperand(0)),
10690 getValue(I.getArgOperand(1)),
10691 DAG.getSrcValue(I.getArgOperand(0)),
10692 DAG.getSrcValue(I.getArgOperand(1))));
10693}
10694
10696 const Instruction &I,
10697 SDValue Op) {
10698 std::optional<ConstantRange> CR = getRange(I);
10699
10700 if (!CR || CR->isFullSet() || CR->isEmptySet() || CR->isUpperWrapped())
10701 return Op;
10702
10703 APInt Lo = CR->getUnsignedMin();
10704 if (!Lo.isMinValue())
10705 return Op;
10706
10707 APInt Hi = CR->getUnsignedMax();
10708 unsigned Bits = std::max(Hi.getActiveBits(),
10709 static_cast<unsigned>(IntegerType::MIN_INT_BITS));
10710
10711 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), Bits);
10712
10713 SDLoc SL = getCurSDLoc();
10714
10715 SDValue ZExt = DAG.getNode(ISD::AssertZext, SL, Op.getValueType(), Op,
10716 DAG.getValueType(SmallVT));
10717 unsigned NumVals = Op.getNode()->getNumValues();
10718 if (NumVals == 1)
10719 return ZExt;
10720
10722
10723 Ops.push_back(ZExt);
10724 for (unsigned I = 1; I != NumVals; ++I)
10725 Ops.push_back(Op.getValue(I));
10726
10727 return DAG.getMergeValues(Ops, SL);
10728}
10729
10731 SelectionDAG &DAG, const Instruction &I, SDValue Op) {
10732 FPClassTest Classes = getNoFPClass(I);
10733 if (Classes == fcNone)
10734 return Op;
10735
10736 SDLoc SL = getCurSDLoc();
10737 SDValue TestConst = DAG.getTargetConstant(Classes, SDLoc(), MVT::i32);
10738
10739 if (Op.getOpcode() != ISD::MERGE_VALUES) {
10740 return DAG.getNode(ISD::AssertNoFPClass, SL, Op.getValueType(), Op,
10741 TestConst);
10742 }
10743
10744 SmallVector<SDValue, 8> Ops(Op.getNumOperands());
10745 for (unsigned I = 0, E = Ops.size(); I != E; ++I) {
10746 SDValue MergeOp = Op.getOperand(I);
10747 Ops[I] = DAG.getNode(ISD::AssertNoFPClass, SL, MergeOp.getValueType(),
10748 MergeOp, TestConst);
10749 }
10750
10751 return DAG.getMergeValues(Ops, SL);
10752}
10753
10754/// Populate a CallLowerinInfo (into \p CLI) based on the properties of
10755/// the call being lowered.
10756///
10757/// This is a helper for lowering intrinsics that follow a target calling
10758/// convention or require stack pointer adjustment. Only a subset of the
10759/// intrinsic's operands need to participate in the calling convention.
10762 unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy,
10763 AttributeSet RetAttrs, bool IsPatchPoint) {
10765 Args.reserve(NumArgs);
10766
10767 // Populate the argument list.
10768 // Attributes for args start at offset 1, after the return attribute.
10769 for (unsigned ArgI = ArgIdx, ArgE = ArgIdx + NumArgs;
10770 ArgI != ArgE; ++ArgI) {
10771 const Value *V = Call->getOperand(ArgI);
10772
10773 assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic.");
10774
10775 TargetLowering::ArgListEntry Entry(getValue(V), V->getType());
10776 Entry.setAttributes(Call, ArgI);
10777 Args.push_back(Entry);
10778 }
10779
10781 .setChain(getRoot())
10782 .setCallee(Call->getCallingConv(), ReturnTy, Callee, std::move(Args),
10783 RetAttrs)
10784 .setDiscardResult(Call->use_empty())
10785 .setIsPatchPoint(IsPatchPoint)
10787 Call->countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0);
10788}
10789
10790/// Add a stack map intrinsic call's live variable operands to a stackmap
10791/// or patchpoint target node's operand list.
10792///
10793/// Constants are converted to TargetConstants purely as an optimization to
10794/// avoid constant materialization and register allocation.
10795///
10796/// FrameIndex operands are converted to TargetFrameIndex so that ISEL does not
10797/// generate addess computation nodes, and so FinalizeISel can convert the
10798/// TargetFrameIndex into a DirectMemRefOp StackMap location. This avoids
10799/// address materialization and register allocation, but may also be required
10800/// for correctness. If a StackMap (or PatchPoint) intrinsic directly uses an
10801/// alloca in the entry block, then the runtime may assume that the alloca's
10802/// StackMap location can be read immediately after compilation and that the
10803/// location is valid at any point during execution (this is similar to the
10804/// assumption made by the llvm.gcroot intrinsic). If the alloca's location were
10805/// only available in a register, then the runtime would need to trap when
10806/// execution reaches the StackMap in order to read the alloca's location.
10807static void addStackMapLiveVars(const CallBase &Call, unsigned StartIdx,
10809 SelectionDAGBuilder &Builder) {
10810 SelectionDAG &DAG = Builder.DAG;
10811 for (unsigned I = StartIdx; I < Call.arg_size(); I++) {
10812 SDValue Op = Builder.getValue(Call.getArgOperand(I));
10813
10814 // Things on the stack are pointer-typed, meaning that they are already
10815 // legal and can be emitted directly to target nodes.
10817 Ops.push_back(DAG.getTargetFrameIndex(FI->getIndex(), Op.getValueType()));
10818 } else {
10819 // Otherwise emit a target independent node to be legalised.
10820 Ops.push_back(Builder.getValue(Call.getArgOperand(I)));
10821 }
10822 }
10823}
10824
10825/// Lower llvm.experimental.stackmap.
10826void SelectionDAGBuilder::visitStackmap(const CallInst &CI) {
10827 // void @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>,
10828 // [live variables...])
10829
10830 assert(CI.getType()->isVoidTy() && "Stackmap cannot return a value.");
10831
10832 SDValue Chain, InGlue, Callee;
10834
10835 SDLoc DL = getCurSDLoc();
10837
10838 // The stackmap intrinsic only records the live variables (the arguments
10839 // passed to it) and emits NOPS (if requested). Unlike the patchpoint
10840 // intrinsic, this won't be lowered to a function call. This means we don't
10841 // have to worry about calling conventions and target specific lowering code.
10842 // Instead we perform the call lowering right here.
10843 //
10844 // chain, flag = CALLSEQ_START(chain, 0, 0)
10845 // chain, flag = STACKMAP(id, nbytes, ..., chain, flag)
10846 // chain, flag = CALLSEQ_END(chain, 0, 0, flag)
10847 //
10848 Chain = DAG.getCALLSEQ_START(getRoot(), 0, 0, DL);
10849 InGlue = Chain.getValue(1);
10850
10851 // Add the STACKMAP operands, starting with DAG house-keeping.
10852 Ops.push_back(Chain);
10853 Ops.push_back(InGlue);
10854
10855 // Add the <id>, <numShadowBytes> operands.
10856 //
10857 // These do not require legalisation, and can be emitted directly to target
10858 // constant nodes.
10860 assert(ID.getValueType() == MVT::i64);
10861 SDValue IDConst =
10862 DAG.getTargetConstant(ID->getAsZExtVal(), DL, ID.getValueType());
10863 Ops.push_back(IDConst);
10864
10865 SDValue Shad = getValue(CI.getArgOperand(1));
10866 assert(Shad.getValueType() == MVT::i32);
10867 SDValue ShadConst =
10868 DAG.getTargetConstant(Shad->getAsZExtVal(), DL, Shad.getValueType());
10869 Ops.push_back(ShadConst);
10870
10871 // Add the live variables.
10872 addStackMapLiveVars(CI, 2, DL, Ops, *this);
10873
10874 // Create the STACKMAP node.
10875 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
10876 Chain = DAG.getNode(ISD::STACKMAP, DL, NodeTys, Ops);
10877 InGlue = Chain.getValue(1);
10878
10879 Chain = DAG.getCALLSEQ_END(Chain, 0, 0, InGlue, DL);
10880
10881 // Stackmaps don't generate values, so nothing goes into the NodeMap.
10882
10883 // Set the root to the target-lowered call chain.
10884 DAG.setRoot(Chain);
10885
10886 // Inform the Frame Information that we have a stackmap in this function.
10887 FuncInfo.MF->getFrameInfo().setHasStackMap();
10888}
10889
10890/// Lower llvm.experimental.patchpoint directly to its target opcode.
10891void SelectionDAGBuilder::visitPatchpoint(const CallBase &CB,
10892 const BasicBlock *EHPadBB) {
10893 // <ty> @llvm.experimental.patchpoint.<ty>(i64 <id>,
10894 // i32 <numBytes>,
10895 // i8* <target>,
10896 // i32 <numArgs>,
10897 // [Args...],
10898 // [live variables...])
10899
10901 bool IsAnyRegCC = CC == CallingConv::AnyReg;
10902 bool HasDef = !CB.getType()->isVoidTy();
10903 SDLoc dl = getCurSDLoc();
10905
10906 // Handle immediate and symbolic callees.
10907 if (auto* ConstCallee = dyn_cast<ConstantSDNode>(Callee))
10908 Callee = DAG.getIntPtrConstant(ConstCallee->getZExtValue(), dl,
10909 /*isTarget=*/true);
10910 else if (auto* SymbolicCallee = dyn_cast<GlobalAddressSDNode>(Callee))
10911 Callee = DAG.getTargetGlobalAddress(SymbolicCallee->getGlobal(),
10912 SDLoc(SymbolicCallee),
10913 SymbolicCallee->getValueType(0));
10914
10915 // Get the real number of arguments participating in the call <numArgs>
10917 unsigned NumArgs = NArgVal->getAsZExtVal();
10918
10919 // Skip the four meta args: <id>, <numNopBytes>, <target>, <numArgs>
10920 // Intrinsics include all meta-operands up to but not including CC.
10921 unsigned NumMetaOpers = PatchPointOpers::CCPos;
10922 assert(CB.arg_size() >= NumMetaOpers + NumArgs &&
10923 "Not enough arguments provided to the patchpoint intrinsic");
10924
10925 // For AnyRegCC the arguments are lowered later on manually.
10926 unsigned NumCallArgs = IsAnyRegCC ? 0 : NumArgs;
10927 Type *ReturnTy =
10928 IsAnyRegCC ? Type::getVoidTy(*DAG.getContext()) : CB.getType();
10929
10930 TargetLowering::CallLoweringInfo CLI(DAG);
10931 populateCallLoweringInfo(CLI, &CB, NumMetaOpers, NumCallArgs, Callee,
10932 ReturnTy, CB.getAttributes().getRetAttrs(), true);
10933 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
10934
10935 SDNode *CallEnd = Result.second.getNode();
10936 if (CallEnd->getOpcode() == ISD::EH_LABEL)
10937 CallEnd = CallEnd->getOperand(0).getNode();
10938 if (HasDef && (CallEnd->getOpcode() == ISD::CopyFromReg))
10939 CallEnd = CallEnd->getOperand(0).getNode();
10940
10941 /// Get a call instruction from the call sequence chain.
10942 /// Tail calls are not allowed.
10943 assert(CallEnd->getOpcode() == ISD::CALLSEQ_END &&
10944 "Expected a callseq node.");
10945 SDNode *Call = CallEnd->getOperand(0).getNode();
10946 bool HasGlue = Call->getGluedNode();
10947
10948 // Replace the target specific call node with the patchable intrinsic.
10950
10951 // Push the chain.
10952 Ops.push_back(*(Call->op_begin()));
10953
10954 // Optionally, push the glue (if any).
10955 if (HasGlue)
10956 Ops.push_back(*(Call->op_end() - 1));
10957
10958 // Push the register mask info.
10959 if (HasGlue)
10960 Ops.push_back(*(Call->op_end() - 2));
10961 else
10962 Ops.push_back(*(Call->op_end() - 1));
10963
10964 // Add the <id> and <numBytes> constants.
10966 Ops.push_back(DAG.getTargetConstant(IDVal->getAsZExtVal(), dl, MVT::i64));
10968 Ops.push_back(DAG.getTargetConstant(NBytesVal->getAsZExtVal(), dl, MVT::i32));
10969
10970 // Add the callee.
10971 Ops.push_back(Callee);
10972
10973 // Adjust <numArgs> to account for any arguments that have been passed on the
10974 // stack instead.
10975 // Call Node: Chain, Target, {Args}, RegMask, [Glue]
10976 unsigned NumCallRegArgs = Call->getNumOperands() - (HasGlue ? 4 : 3);
10977 NumCallRegArgs = IsAnyRegCC ? NumArgs : NumCallRegArgs;
10978 Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, dl, MVT::i32));
10979
10980 // Add the calling convention
10981 Ops.push_back(DAG.getTargetConstant((unsigned)CC, dl, MVT::i32));
10982
10983 // Add the arguments we omitted previously. The register allocator should
10984 // place these in any free register.
10985 if (IsAnyRegCC)
10986 for (unsigned i = NumMetaOpers, e = NumMetaOpers + NumArgs; i != e; ++i)
10987 Ops.push_back(getValue(CB.getArgOperand(i)));
10988
10989 // Push the arguments from the call instruction.
10990 SDNode::op_iterator e = HasGlue ? Call->op_end()-2 : Call->op_end()-1;
10991 Ops.append(Call->op_begin() + 2, e);
10992
10993 // Push live variables for the stack map.
10994 addStackMapLiveVars(CB, NumMetaOpers + NumArgs, dl, Ops, *this);
10995
10996 SDVTList NodeTys;
10997 if (IsAnyRegCC && HasDef) {
10998 // Create the return types based on the intrinsic definition
10999 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11000 SmallVector<EVT, 3> ValueVTs;
11001 ComputeValueVTs(TLI, DAG.getDataLayout(), CB.getType(), ValueVTs);
11002 assert(ValueVTs.size() == 1 && "Expected only one return value type.");
11003
11004 // There is always a chain and a glue type at the end
11005 ValueVTs.push_back(MVT::Other);
11006 ValueVTs.push_back(MVT::Glue);
11007 NodeTys = DAG.getVTList(ValueVTs);
11008 } else
11009 NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
11010
11011 // Replace the target specific call node with a PATCHPOINT node.
11012 SDValue PPV = DAG.getNode(ISD::PATCHPOINT, dl, NodeTys, Ops);
11013
11014 // Update the NodeMap.
11015 if (HasDef) {
11016 if (IsAnyRegCC)
11017 setValue(&CB, SDValue(PPV.getNode(), 0));
11018 else
11019 setValue(&CB, Result.first);
11020 }
11021
11022 // Fixup the consumers of the intrinsic. The chain and glue may be used in the
11023 // call sequence. Furthermore the location of the chain and glue can change
11024 // when the AnyReg calling convention is used and the intrinsic returns a
11025 // value.
11026 if (IsAnyRegCC && HasDef) {
11027 SDValue From[] = {SDValue(Call, 0), SDValue(Call, 1)};
11028 SDValue To[] = {PPV.getValue(1), PPV.getValue(2)};
11029 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
11030 } else
11031 DAG.ReplaceAllUsesWith(Call, PPV.getNode());
11032 DAG.DeleteNode(Call);
11033
11034 // Inform the Frame Information that we have a patchpoint in this function.
11035 FuncInfo.MF->getFrameInfo().setHasPatchPoint();
11036}
11037
11038void SelectionDAGBuilder::visitVectorReduce(const CallInst &I,
11039 unsigned Intrinsic) {
11040 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11041 SDValue Op1 = getValue(I.getArgOperand(0));
11042 SDValue Op2;
11043 if (I.arg_size() > 1)
11044 Op2 = getValue(I.getArgOperand(1));
11045 SDLoc dl = getCurSDLoc();
11046 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
11047 SDValue Res;
11048 SDNodeFlags SDFlags;
11049 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
11050 SDFlags.copyFMF(*FPMO);
11051
11052 switch (Intrinsic) {
11053 case Intrinsic::vector_reduce_fadd:
11054 if (SDFlags.hasAllowReassociation())
11055 Res = DAG.getNode(ISD::FADD, dl, VT, Op1,
11056 DAG.getNode(ISD::VECREDUCE_FADD, dl, VT, Op2, SDFlags),
11057 SDFlags);
11058 else
11059 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FADD, dl, VT, Op1, Op2, SDFlags);
11060 break;
11061 case Intrinsic::vector_reduce_fmul:
11062 if (SDFlags.hasAllowReassociation())
11063 Res = DAG.getNode(ISD::FMUL, dl, VT, Op1,
11064 DAG.getNode(ISD::VECREDUCE_FMUL, dl, VT, Op2, SDFlags),
11065 SDFlags);
11066 else
11067 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FMUL, dl, VT, Op1, Op2, SDFlags);
11068 break;
11069 case Intrinsic::vector_reduce_add:
11070 Res = DAG.getNode(ISD::VECREDUCE_ADD, dl, VT, Op1);
11071 break;
11072 case Intrinsic::vector_reduce_mul:
11073 Res = DAG.getNode(ISD::VECREDUCE_MUL, dl, VT, Op1);
11074 break;
11075 case Intrinsic::vector_reduce_and:
11076 Res = DAG.getNode(ISD::VECREDUCE_AND, dl, VT, Op1);
11077 break;
11078 case Intrinsic::vector_reduce_or:
11079 Res = DAG.getNode(ISD::VECREDUCE_OR, dl, VT, Op1);
11080 break;
11081 case Intrinsic::vector_reduce_xor:
11082 Res = DAG.getNode(ISD::VECREDUCE_XOR, dl, VT, Op1);
11083 break;
11084 case Intrinsic::vector_reduce_smax:
11085 Res = DAG.getNode(ISD::VECREDUCE_SMAX, dl, VT, Op1);
11086 break;
11087 case Intrinsic::vector_reduce_smin:
11088 Res = DAG.getNode(ISD::VECREDUCE_SMIN, dl, VT, Op1);
11089 break;
11090 case Intrinsic::vector_reduce_umax:
11091 Res = DAG.getNode(ISD::VECREDUCE_UMAX, dl, VT, Op1);
11092 break;
11093 case Intrinsic::vector_reduce_umin:
11094 Res = DAG.getNode(ISD::VECREDUCE_UMIN, dl, VT, Op1);
11095 break;
11096 case Intrinsic::vector_reduce_fmax:
11097 Res = DAG.getNode(ISD::VECREDUCE_FMAX, dl, VT, Op1, SDFlags);
11098 break;
11099 case Intrinsic::vector_reduce_fmin:
11100 Res = DAG.getNode(ISD::VECREDUCE_FMIN, dl, VT, Op1, SDFlags);
11101 break;
11102 case Intrinsic::vector_reduce_fmaximum:
11103 Res = DAG.getNode(ISD::VECREDUCE_FMAXIMUM, dl, VT, Op1, SDFlags);
11104 break;
11105 case Intrinsic::vector_reduce_fminimum:
11106 Res = DAG.getNode(ISD::VECREDUCE_FMINIMUM, dl, VT, Op1, SDFlags);
11107 break;
11108 default:
11109 llvm_unreachable("Unhandled vector reduce intrinsic");
11110 }
11111 setValue(&I, Res);
11112}
11113
11114/// Returns an AttributeList representing the attributes applied to the return
11115/// value of the given call.
11118 if (CLI.RetSExt)
11119 Attrs.push_back(Attribute::SExt);
11120 if (CLI.RetZExt)
11121 Attrs.push_back(Attribute::ZExt);
11122 if (CLI.IsInReg)
11123 Attrs.push_back(Attribute::InReg);
11124
11125 return AttributeList::get(CLI.RetTy->getContext(), AttributeList::ReturnIndex,
11126 Attrs);
11127}
11128
11129/// TargetLowering::LowerCallTo - This is the default LowerCallTo
11130/// implementation, which just calls LowerCall.
11131/// FIXME: When all targets are
11132/// migrated to using LowerCall, this hook should be integrated into SDISel.
11133std::pair<SDValue, SDValue>
11135 LLVMContext &Context = CLI.RetTy->getContext();
11136
11137 // Handle the incoming return values from the call.
11138 CLI.Ins.clear();
11139 SmallVector<Type *, 4> RetOrigTys;
11141 auto &DL = CLI.DAG.getDataLayout();
11142 ComputeValueTypes(DL, CLI.OrigRetTy, RetOrigTys, &Offsets);
11143
11144 SmallVector<EVT, 4> RetVTs;
11145 if (CLI.RetTy != CLI.OrigRetTy) {
11146 assert(RetOrigTys.size() == 1 &&
11147 "Only supported for non-aggregate returns");
11148 RetVTs.push_back(getValueType(DL, CLI.RetTy));
11149 } else {
11150 for (Type *Ty : RetOrigTys)
11151 RetVTs.push_back(getValueType(DL, Ty));
11152 }
11153
11154 if (CLI.IsPostTypeLegalization) {
11155 // If we are lowering a libcall after legalization, split the return type.
11156 SmallVector<Type *, 4> OldRetOrigTys;
11157 SmallVector<EVT, 4> OldRetVTs;
11158 SmallVector<TypeSize, 4> OldOffsets;
11159 RetOrigTys.swap(OldRetOrigTys);
11160 RetVTs.swap(OldRetVTs);
11161 Offsets.swap(OldOffsets);
11162
11163 for (size_t i = 0, e = OldRetVTs.size(); i != e; ++i) {
11164 EVT RetVT = OldRetVTs[i];
11165 uint64_t Offset = OldOffsets[i];
11166 MVT RegisterVT = getRegisterType(Context, RetVT);
11167 unsigned NumRegs = getNumRegisters(Context, RetVT);
11168 unsigned RegisterVTByteSZ = RegisterVT.getSizeInBits() / 8;
11169 RetOrigTys.append(NumRegs, OldRetOrigTys[i]);
11170 RetVTs.append(NumRegs, RegisterVT);
11171 for (unsigned j = 0; j != NumRegs; ++j)
11172 Offsets.push_back(TypeSize::getFixed(Offset + j * RegisterVTByteSZ));
11173 }
11174 }
11175
11177 GetReturnInfo(CLI.CallConv, CLI.RetTy, getReturnAttrs(CLI), Outs, *this, DL);
11178
11179 bool CanLowerReturn =
11181 CLI.IsVarArg, Outs, Context, CLI.RetTy);
11182
11183 SDValue DemoteStackSlot;
11184 int DemoteStackIdx = -100;
11185 if (!CanLowerReturn) {
11186 // FIXME: equivalent assert?
11187 // assert(!CS.hasInAllocaArgument() &&
11188 // "sret demotion is incompatible with inalloca");
11189 uint64_t TySize = DL.getTypeAllocSize(CLI.RetTy);
11190 Align Alignment = DL.getPrefTypeAlign(CLI.RetTy);
11192 DemoteStackIdx =
11193 MF.getFrameInfo().CreateStackObject(TySize, Alignment, false);
11194 Type *StackSlotPtrType = PointerType::get(Context, DL.getAllocaAddrSpace());
11195
11196 DemoteStackSlot = CLI.DAG.getFrameIndex(DemoteStackIdx, getFrameIndexTy(DL));
11197 ArgListEntry Entry(DemoteStackSlot, StackSlotPtrType);
11198 Entry.IsSRet = true;
11199 Entry.Alignment = Alignment;
11200 CLI.getArgs().insert(CLI.getArgs().begin(), Entry);
11201 CLI.NumFixedArgs += 1;
11202 CLI.getArgs()[0].IndirectType = CLI.RetTy;
11203 CLI.RetTy = CLI.OrigRetTy = Type::getVoidTy(Context);
11204
11205 // sret demotion isn't compatible with tail-calls, since the sret argument
11206 // points into the callers stack frame.
11207 CLI.IsTailCall = false;
11208 } else {
11209 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11210 CLI.RetTy, CLI.CallConv, CLI.IsVarArg, DL);
11211 for (unsigned I = 0, E = RetVTs.size(); I != E; ++I) {
11212 ISD::ArgFlagsTy Flags;
11213 if (NeedsRegBlock) {
11214 Flags.setInConsecutiveRegs();
11215 if (I == RetVTs.size() - 1)
11216 Flags.setInConsecutiveRegsLast();
11217 }
11218 EVT VT = RetVTs[I];
11219 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11220 unsigned NumRegs =
11221 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11222 for (unsigned i = 0; i != NumRegs; ++i) {
11223 ISD::InputArg Ret(Flags, RegisterVT, VT, RetOrigTys[I],
11225 if (CLI.RetTy->isPointerTy()) {
11226 Ret.Flags.setPointer();
11227 Ret.Flags.setPointerAddrSpace(
11228 cast<PointerType>(CLI.RetTy)->getAddressSpace());
11229 }
11230 if (CLI.RetSExt)
11231 Ret.Flags.setSExt();
11232 if (CLI.RetZExt)
11233 Ret.Flags.setZExt();
11234 if (CLI.IsInReg)
11235 Ret.Flags.setInReg();
11236 CLI.Ins.push_back(Ret);
11237 }
11238 }
11239 }
11240
11241 // We push in swifterror return as the last element of CLI.Ins.
11242 ArgListTy &Args = CLI.getArgs();
11243 if (supportSwiftError()) {
11244 for (const ArgListEntry &Arg : Args) {
11245 if (Arg.IsSwiftError) {
11246 ISD::ArgFlagsTy Flags;
11247 Flags.setSwiftError();
11249 PointerType::getUnqual(Context),
11250 /*Used=*/true, ISD::InputArg::NoArgIndex, 0);
11251 CLI.Ins.push_back(Ret);
11252 }
11253 }
11254 }
11255
11256 // Handle all of the outgoing arguments.
11257 CLI.Outs.clear();
11258 CLI.OutVals.clear();
11259 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
11260 SmallVector<Type *, 4> OrigArgTys;
11261 ComputeValueTypes(DL, Args[i].OrigTy, OrigArgTys);
11262 // FIXME: Split arguments if CLI.IsPostTypeLegalization
11263 Type *FinalType = Args[i].Ty;
11264 if (Args[i].IsByVal)
11265 FinalType = Args[i].IndirectType;
11266 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11267 FinalType, CLI.CallConv, CLI.IsVarArg, DL);
11268 for (unsigned Value = 0, NumValues = OrigArgTys.size(); Value != NumValues;
11269 ++Value) {
11270 Type *OrigArgTy = OrigArgTys[Value];
11271 Type *ArgTy = OrigArgTy;
11272 if (Args[i].Ty != Args[i].OrigTy) {
11273 assert(Value == 0 && "Only supported for non-aggregate arguments");
11274 ArgTy = Args[i].Ty;
11275 }
11276
11277 EVT VT = getValueType(DL, ArgTy);
11278 SDValue Op = SDValue(Args[i].Node.getNode(),
11279 Args[i].Node.getResNo() + Value);
11280 ISD::ArgFlagsTy Flags;
11281
11282 // Certain targets (such as MIPS), may have a different ABI alignment
11283 // for a type depending on the context. Give the target a chance to
11284 // specify the alignment it wants.
11285 const Align OriginalAlignment(getABIAlignmentForCallingConv(ArgTy, DL));
11286 Flags.setOrigAlign(OriginalAlignment);
11287
11288 if (i >= CLI.NumFixedArgs)
11289 Flags.setVarArg();
11290 if (ArgTy->isPointerTy()) {
11291 Flags.setPointer();
11292 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11293 }
11294 if (Args[i].IsZExt)
11295 Flags.setZExt();
11296 if (Args[i].IsSExt)
11297 Flags.setSExt();
11298 if (Args[i].IsNoExt)
11299 Flags.setNoExt();
11300 if (Args[i].IsInReg) {
11301 // If we are using vectorcall calling convention, a structure that is
11302 // passed InReg - is surely an HVA
11304 isa<StructType>(FinalType)) {
11305 // The first value of a structure is marked
11306 if (0 == Value)
11307 Flags.setHvaStart();
11308 Flags.setHva();
11309 }
11310 // Set InReg Flag
11311 Flags.setInReg();
11312 }
11313 if (Args[i].IsSRet)
11314 Flags.setSRet();
11315 if (Args[i].IsSwiftSelf)
11316 Flags.setSwiftSelf();
11317 if (Args[i].IsSwiftAsync)
11318 Flags.setSwiftAsync();
11319 if (Args[i].IsSwiftError)
11320 Flags.setSwiftError();
11321 if (Args[i].IsCFGuardTarget)
11322 Flags.setCFGuardTarget();
11323 if (Args[i].IsByVal)
11324 Flags.setByVal();
11325 if (Args[i].IsByRef)
11326 Flags.setByRef();
11327 if (Args[i].IsPreallocated) {
11328 Flags.setPreallocated();
11329 // Set the byval flag for CCAssignFn callbacks that don't know about
11330 // preallocated. This way we can know how many bytes we should've
11331 // allocated and how many bytes a callee cleanup function will pop. If
11332 // we port preallocated to more targets, we'll have to add custom
11333 // preallocated handling in the various CC lowering callbacks.
11334 Flags.setByVal();
11335 }
11336 if (Args[i].IsInAlloca) {
11337 Flags.setInAlloca();
11338 // Set the byval flag for CCAssignFn callbacks that don't know about
11339 // inalloca. This way we can know how many bytes we should've allocated
11340 // and how many bytes a callee cleanup function will pop. If we port
11341 // inalloca to more targets, we'll have to add custom inalloca handling
11342 // in the various CC lowering callbacks.
11343 Flags.setByVal();
11344 }
11345 Align MemAlign;
11346 if (Args[i].IsByVal || Args[i].IsInAlloca || Args[i].IsPreallocated) {
11347 unsigned FrameSize = DL.getTypeAllocSize(Args[i].IndirectType);
11348 Flags.setByValSize(FrameSize);
11349
11350 // info is not there but there are cases it cannot get right.
11351 if (auto MA = Args[i].Alignment)
11352 MemAlign = *MA;
11353 else
11354 MemAlign = getByValTypeAlignment(Args[i].IndirectType, DL);
11355 } else if (auto MA = Args[i].Alignment) {
11356 MemAlign = *MA;
11357 } else {
11358 MemAlign = OriginalAlignment;
11359 }
11360 Flags.setMemAlign(MemAlign);
11361 if (Args[i].IsNest)
11362 Flags.setNest();
11363 if (NeedsRegBlock)
11364 Flags.setInConsecutiveRegs();
11365
11366 MVT PartVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11367 unsigned NumParts =
11368 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11369 SmallVector<SDValue, 4> Parts(NumParts);
11370 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
11371
11372 if (Args[i].IsSExt)
11373 ExtendKind = ISD::SIGN_EXTEND;
11374 else if (Args[i].IsZExt)
11375 ExtendKind = ISD::ZERO_EXTEND;
11376
11377 // Conservatively only handle 'returned' on non-vectors that can be lowered,
11378 // for now.
11379 if (Args[i].IsReturned && !Op.getValueType().isVector() &&
11381 assert((CLI.RetTy == Args[i].Ty ||
11382 (CLI.RetTy->isPointerTy() && Args[i].Ty->isPointerTy() &&
11384 Args[i].Ty->getPointerAddressSpace())) &&
11385 RetVTs.size() == NumValues && "unexpected use of 'returned'");
11386 // Before passing 'returned' to the target lowering code, ensure that
11387 // either the register MVT and the actual EVT are the same size or that
11388 // the return value and argument are extended in the same way; in these
11389 // cases it's safe to pass the argument register value unchanged as the
11390 // return register value (although it's at the target's option whether
11391 // to do so)
11392 // TODO: allow code generation to take advantage of partially preserved
11393 // registers rather than clobbering the entire register when the
11394 // parameter extension method is not compatible with the return
11395 // extension method
11396 if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
11397 (ExtendKind != ISD::ANY_EXTEND && CLI.RetSExt == Args[i].IsSExt &&
11398 CLI.RetZExt == Args[i].IsZExt))
11399 Flags.setReturned();
11400 }
11401
11402 getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts, PartVT, CLI.CB,
11403 CLI.CallConv, ExtendKind);
11404
11405 for (unsigned j = 0; j != NumParts; ++j) {
11406 // if it isn't first piece, alignment must be 1
11407 // For scalable vectors the scalable part is currently handled
11408 // by individual targets, so we just use the known minimum size here.
11409 ISD::OutputArg MyFlags(
11410 Flags, Parts[j].getValueType().getSimpleVT(), VT, OrigArgTy, i,
11411 j * Parts[j].getValueType().getStoreSize().getKnownMinValue());
11412 if (NumParts > 1 && j == 0)
11413 MyFlags.Flags.setSplit();
11414 else if (j != 0) {
11415 MyFlags.Flags.setOrigAlign(Align(1));
11416 if (j == NumParts - 1)
11417 MyFlags.Flags.setSplitEnd();
11418 }
11419
11420 CLI.Outs.push_back(MyFlags);
11421 CLI.OutVals.push_back(Parts[j]);
11422 }
11423
11424 if (NeedsRegBlock && Value == NumValues - 1)
11425 CLI.Outs[CLI.Outs.size() - 1].Flags.setInConsecutiveRegsLast();
11426 }
11427 }
11428
11430 CLI.Chain = LowerCall(CLI, InVals);
11431
11432 // Update CLI.InVals to use outside of this function.
11433 CLI.InVals = InVals;
11434
11435 // Verify that the target's LowerCall behaved as expected.
11436 assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
11437 "LowerCall didn't return a valid chain!");
11438 assert((!CLI.IsTailCall || InVals.empty()) &&
11439 "LowerCall emitted a return value for a tail call!");
11440 assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
11441 "LowerCall didn't emit the correct number of values!");
11442
11443 // For a tail call, the return value is merely live-out and there aren't
11444 // any nodes in the DAG representing it. Return a special value to
11445 // indicate that a tail call has been emitted and no more Instructions
11446 // should be processed in the current block.
11447 if (CLI.IsTailCall) {
11448 CLI.DAG.setRoot(CLI.Chain);
11449 return std::make_pair(SDValue(), SDValue());
11450 }
11451
11452#ifndef NDEBUG
11453 for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
11454 assert(InVals[i].getNode() && "LowerCall emitted a null value!");
11455 assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
11456 "LowerCall emitted a value with the wrong type!");
11457 }
11458#endif
11459
11460 SmallVector<SDValue, 4> ReturnValues;
11461 if (!CanLowerReturn) {
11462 // The instruction result is the result of loading from the
11463 // hidden sret parameter.
11464 MVT PtrVT = getPointerTy(DL, DL.getAllocaAddrSpace());
11465
11466 unsigned NumValues = RetVTs.size();
11467 ReturnValues.resize(NumValues);
11468 SmallVector<SDValue, 4> Chains(NumValues);
11469
11470 // An aggregate return value cannot wrap around the address space, so
11471 // offsets to its parts don't wrap either.
11473 Align HiddenSRetAlign = MF.getFrameInfo().getObjectAlign(DemoteStackIdx);
11474 for (unsigned i = 0; i < NumValues; ++i) {
11476 DemoteStackSlot, CLI.DAG.getConstant(Offsets[i], CLI.DL, PtrVT),
11478 SDValue L = CLI.DAG.getLoad(
11479 RetVTs[i], CLI.DL, CLI.Chain, Add,
11481 DemoteStackIdx, Offsets[i]),
11482 HiddenSRetAlign);
11483 ReturnValues[i] = L;
11484 Chains[i] = L.getValue(1);
11485 }
11486
11487 CLI.Chain = CLI.DAG.getNode(ISD::TokenFactor, CLI.DL, MVT::Other, Chains);
11488 } else {
11489 // Collect the legal value parts into potentially illegal values
11490 // that correspond to the original function's return values.
11491 std::optional<ISD::NodeType> AssertOp;
11492 if (CLI.RetSExt)
11493 AssertOp = ISD::AssertSext;
11494 else if (CLI.RetZExt)
11495 AssertOp = ISD::AssertZext;
11496 unsigned CurReg = 0;
11497 for (EVT VT : RetVTs) {
11498 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11499 unsigned NumRegs =
11500 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11501
11502 ReturnValues.push_back(getCopyFromParts(
11503 CLI.DAG, CLI.DL, &InVals[CurReg], NumRegs, RegisterVT, VT, nullptr,
11504 CLI.Chain, CLI.CallConv, AssertOp));
11505 CurReg += NumRegs;
11506 }
11507
11508 // For a function returning void, there is no return value. We can't create
11509 // such a node, so we just return a null return value in that case. In
11510 // that case, nothing will actually look at the value.
11511 if (ReturnValues.empty())
11512 return std::make_pair(SDValue(), CLI.Chain);
11513 }
11514
11515 SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
11516 CLI.DAG.getVTList(RetVTs), ReturnValues);
11517 return std::make_pair(Res, CLI.Chain);
11518}
11519
11520/// Places new result values for the node in Results (their number
11521/// and types must exactly match those of the original return values of
11522/// the node), or leaves Results empty, which indicates that the node is not
11523/// to be custom lowered after all.
11526 SelectionDAG &DAG) const {
11527 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
11528
11529 if (!Res.getNode())
11530 return;
11531
11532 // If the original node has one result, take the return value from
11533 // LowerOperation as is. It might not be result number 0.
11534 if (N->getNumValues() == 1) {
11535 Results.push_back(Res);
11536 return;
11537 }
11538
11539 // If the original node has multiple results, then the return node should
11540 // have the same number of results.
11541 assert((N->getNumValues() == Res->getNumValues()) &&
11542 "Lowering returned the wrong number of results!");
11543
11544 // Places new result values base on N result number.
11545 for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
11546 Results.push_back(Res.getValue(I));
11547}
11548
11550 llvm_unreachable("LowerOperation not implemented for this target!");
11551}
11552
11554 Register Reg,
11555 ISD::NodeType ExtendType) {
11557 assert((Op.getOpcode() != ISD::CopyFromReg ||
11558 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
11559 "Copy from a reg to the same reg!");
11560 assert(!Reg.isPhysical() && "Is a physreg");
11561
11562 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11563 // If this is an InlineAsm we have to match the registers required, not the
11564 // notional registers required by the type.
11565
11566 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg, V->getType(),
11567 std::nullopt); // This is not an ABI copy.
11568 SDValue Chain = DAG.getEntryNode();
11569
11570 if (ExtendType == ISD::ANY_EXTEND) {
11571 auto PreferredExtendIt = FuncInfo.PreferredExtendType.find(V);
11572 if (PreferredExtendIt != FuncInfo.PreferredExtendType.end())
11573 ExtendType = PreferredExtendIt->second;
11574 }
11575 RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, nullptr, V, ExtendType);
11576 PendingExports.push_back(Chain);
11577}
11578
11580
11581/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
11582/// entry block, return true. This includes arguments used by switches, since
11583/// the switch may expand into multiple basic blocks.
11584static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
11585 // With FastISel active, we may be splitting blocks, so force creation
11586 // of virtual registers for all non-dead arguments.
11587 if (FastISel)
11588 return A->use_empty();
11589
11590 const BasicBlock &Entry = A->getParent()->front();
11591 for (const User *U : A->users())
11592 if (cast<Instruction>(U)->getParent() != &Entry || isa<SwitchInst>(U))
11593 return false; // Use not in entry block.
11594
11595 return true;
11596}
11597
11599 DenseMap<const Argument *,
11600 std::pair<const AllocaInst *, const StoreInst *>>;
11601
11602/// Scan the entry block of the function in FuncInfo for arguments that look
11603/// like copies into a local alloca. Record any copied arguments in
11604/// ArgCopyElisionCandidates.
11605static void
11607 FunctionLoweringInfo *FuncInfo,
11608 ArgCopyElisionMapTy &ArgCopyElisionCandidates) {
11609 // Record the state of every static alloca used in the entry block. Argument
11610 // allocas are all used in the entry block, so we need approximately as many
11611 // entries as we have arguments.
11612 enum StaticAllocaInfo { Unknown, Clobbered, Elidable };
11614 unsigned NumArgs = FuncInfo->Fn->arg_size();
11615 StaticAllocas.reserve(NumArgs * 2);
11616
11617 auto GetInfoIfStaticAlloca = [&](const Value *V) -> StaticAllocaInfo * {
11618 if (!V)
11619 return nullptr;
11620 V = V->stripPointerCasts();
11621 const auto *AI = dyn_cast<AllocaInst>(V);
11622 if (!AI || !AI->isStaticAlloca() || !FuncInfo->StaticAllocaMap.count(AI))
11623 return nullptr;
11624 auto Iter = StaticAllocas.insert({AI, Unknown});
11625 return &Iter.first->second;
11626 };
11627
11628 // Look for stores of arguments to static allocas. Look through bitcasts and
11629 // GEPs to handle type coercions, as long as the alloca is fully initialized
11630 // by the store. Any non-store use of an alloca escapes it and any subsequent
11631 // unanalyzed store might write it.
11632 // FIXME: Handle structs initialized with multiple stores.
11633 for (const Instruction &I : FuncInfo->Fn->getEntryBlock()) {
11634 // Look for stores, and handle non-store uses conservatively.
11635 const auto *SI = dyn_cast<StoreInst>(&I);
11636 if (!SI) {
11637 // We will look through cast uses, so ignore them completely.
11638 if (I.isCast())
11639 continue;
11640 // Ignore debug info and pseudo op intrinsics, they don't escape or store
11641 // to allocas.
11642 if (I.isDebugOrPseudoInst())
11643 continue;
11644 // This is an unknown instruction. Assume it escapes or writes to all
11645 // static alloca operands.
11646 for (const Use &U : I.operands()) {
11647 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(U))
11648 *Info = StaticAllocaInfo::Clobbered;
11649 }
11650 continue;
11651 }
11652
11653 // If the stored value is a static alloca, mark it as escaped.
11654 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(SI->getValueOperand()))
11655 *Info = StaticAllocaInfo::Clobbered;
11656
11657 // Check if the destination is a static alloca.
11658 const Value *Dst = SI->getPointerOperand()->stripPointerCasts();
11659 StaticAllocaInfo *Info = GetInfoIfStaticAlloca(Dst);
11660 if (!Info)
11661 continue;
11662 const AllocaInst *AI = cast<AllocaInst>(Dst);
11663
11664 // Skip allocas that have been initialized or clobbered.
11665 if (*Info != StaticAllocaInfo::Unknown)
11666 continue;
11667
11668 // Check if the stored value is an argument, and that this store fully
11669 // initializes the alloca.
11670 // If the argument type has padding bits we can't directly forward a pointer
11671 // as the upper bits may contain garbage.
11672 // Don't elide copies from the same argument twice.
11673 const Value *Val = SI->getValueOperand()->stripPointerCasts();
11674 const auto *Arg = dyn_cast<Argument>(Val);
11675 if (!Arg || Arg->hasPassPointeeByValueCopyAttr() ||
11676 Arg->getType()->isEmptyTy() ||
11677 DL.getTypeStoreSize(Arg->getType()) !=
11678 DL.getTypeAllocSize(AI->getAllocatedType()) ||
11679 !DL.typeSizeEqualsStoreSize(Arg->getType()) ||
11680 ArgCopyElisionCandidates.count(Arg)) {
11681 *Info = StaticAllocaInfo::Clobbered;
11682 continue;
11683 }
11684
11685 LLVM_DEBUG(dbgs() << "Found argument copy elision candidate: " << *AI
11686 << '\n');
11687
11688 // Mark this alloca and store for argument copy elision.
11689 *Info = StaticAllocaInfo::Elidable;
11690 ArgCopyElisionCandidates.insert({Arg, {AI, SI}});
11691
11692 // Stop scanning if we've seen all arguments. This will happen early in -O0
11693 // builds, which is useful, because -O0 builds have large entry blocks and
11694 // many allocas.
11695 if (ArgCopyElisionCandidates.size() == NumArgs)
11696 break;
11697 }
11698}
11699
11700/// Try to elide argument copies from memory into a local alloca. Succeeds if
11701/// ArgVal is a load from a suitable fixed stack object.
11704 DenseMap<int, int> &ArgCopyElisionFrameIndexMap,
11705 SmallPtrSetImpl<const Instruction *> &ElidedArgCopyInstrs,
11706 ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg,
11707 ArrayRef<SDValue> ArgVals, bool &ArgHasUses) {
11708 // Check if this is a load from a fixed stack object.
11709 auto *LNode = dyn_cast<LoadSDNode>(ArgVals[0]);
11710 if (!LNode)
11711 return;
11712 auto *FINode = dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode());
11713 if (!FINode)
11714 return;
11715
11716 // Check that the fixed stack object is the right size and alignment.
11717 // Look at the alignment that the user wrote on the alloca instead of looking
11718 // at the stack object.
11719 auto ArgCopyIter = ArgCopyElisionCandidates.find(&Arg);
11720 assert(ArgCopyIter != ArgCopyElisionCandidates.end());
11721 const AllocaInst *AI = ArgCopyIter->second.first;
11722 int FixedIndex = FINode->getIndex();
11723 int &AllocaIndex = FuncInfo.StaticAllocaMap[AI];
11724 int OldIndex = AllocaIndex;
11725 MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo();
11726 if (MFI.getObjectSize(FixedIndex) != MFI.getObjectSize(OldIndex)) {
11727 LLVM_DEBUG(
11728 dbgs() << " argument copy elision failed due to bad fixed stack "
11729 "object size\n");
11730 return;
11731 }
11732 Align RequiredAlignment = AI->getAlign();
11733 if (MFI.getObjectAlign(FixedIndex) < RequiredAlignment) {
11734 LLVM_DEBUG(dbgs() << " argument copy elision failed: alignment of alloca "
11735 "greater than stack argument alignment ("
11736 << DebugStr(RequiredAlignment) << " vs "
11737 << DebugStr(MFI.getObjectAlign(FixedIndex)) << ")\n");
11738 return;
11739 }
11740
11741 // Perform the elision. Delete the old stack object and replace its only use
11742 // in the variable info map. Mark the stack object as mutable and aliased.
11743 LLVM_DEBUG({
11744 dbgs() << "Eliding argument copy from " << Arg << " to " << *AI << '\n'
11745 << " Replacing frame index " << OldIndex << " with " << FixedIndex
11746 << '\n';
11747 });
11748 MFI.RemoveStackObject(OldIndex);
11749 MFI.setIsImmutableObjectIndex(FixedIndex, false);
11750 MFI.setIsAliasedObjectIndex(FixedIndex, true);
11751 AllocaIndex = FixedIndex;
11752 ArgCopyElisionFrameIndexMap.insert({OldIndex, FixedIndex});
11753 for (SDValue ArgVal : ArgVals)
11754 Chains.push_back(ArgVal.getValue(1));
11755
11756 // Avoid emitting code for the store implementing the copy.
11757 const StoreInst *SI = ArgCopyIter->second.second;
11758 ElidedArgCopyInstrs.insert(SI);
11759
11760 // Check for uses of the argument again so that we can avoid exporting ArgVal
11761 // if it is't used by anything other than the store.
11762 for (const Value *U : Arg.users()) {
11763 if (U != SI) {
11764 ArgHasUses = true;
11765 break;
11766 }
11767 }
11768}
11769
11770void SelectionDAGISel::LowerArguments(const Function &F) {
11771 SelectionDAG &DAG = SDB->DAG;
11772 SDLoc dl = SDB->getCurSDLoc();
11773 const DataLayout &DL = DAG.getDataLayout();
11775
11776 // In Naked functions we aren't going to save any registers.
11777 if (F.hasFnAttribute(Attribute::Naked))
11778 return;
11779
11780 if (!FuncInfo->CanLowerReturn) {
11781 // Put in an sret pointer parameter before all the other parameters.
11782 MVT ValueVT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
11783
11784 ISD::ArgFlagsTy Flags;
11785 Flags.setSRet();
11786 MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVT);
11787 ISD::InputArg RetArg(Flags, RegisterVT, ValueVT, F.getReturnType(), true,
11789 Ins.push_back(RetArg);
11790 }
11791
11792 // Look for stores of arguments to static allocas. Mark such arguments with a
11793 // flag to ask the target to give us the memory location of that argument if
11794 // available.
11795 ArgCopyElisionMapTy ArgCopyElisionCandidates;
11797 ArgCopyElisionCandidates);
11798
11799 // Set up the incoming argument description vector.
11800 for (const Argument &Arg : F.args()) {
11801 unsigned ArgNo = Arg.getArgNo();
11803 ComputeValueTypes(DAG.getDataLayout(), Arg.getType(), Types);
11804 bool isArgValueUsed = !Arg.use_empty();
11805 unsigned PartBase = 0;
11806 Type *FinalType = Arg.getType();
11807 if (Arg.hasAttribute(Attribute::ByVal))
11808 FinalType = Arg.getParamByValType();
11809 bool NeedsRegBlock = TLI->functionArgumentNeedsConsecutiveRegisters(
11810 FinalType, F.getCallingConv(), F.isVarArg(), DL);
11811 for (unsigned Value = 0, NumValues = Types.size(); Value != NumValues;
11812 ++Value) {
11813 Type *ArgTy = Types[Value];
11814 EVT VT = TLI->getValueType(DL, ArgTy);
11815 ISD::ArgFlagsTy Flags;
11816
11817 if (ArgTy->isPointerTy()) {
11818 Flags.setPointer();
11819 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11820 }
11821 if (Arg.hasAttribute(Attribute::ZExt))
11822 Flags.setZExt();
11823 if (Arg.hasAttribute(Attribute::SExt))
11824 Flags.setSExt();
11825 if (Arg.hasAttribute(Attribute::InReg)) {
11826 // If we are using vectorcall calling convention, a structure that is
11827 // passed InReg - is surely an HVA
11828 if (F.getCallingConv() == CallingConv::X86_VectorCall &&
11829 isa<StructType>(Arg.getType())) {
11830 // The first value of a structure is marked
11831 if (0 == Value)
11832 Flags.setHvaStart();
11833 Flags.setHva();
11834 }
11835 // Set InReg Flag
11836 Flags.setInReg();
11837 }
11838 if (Arg.hasAttribute(Attribute::StructRet))
11839 Flags.setSRet();
11840 if (Arg.hasAttribute(Attribute::SwiftSelf))
11841 Flags.setSwiftSelf();
11842 if (Arg.hasAttribute(Attribute::SwiftAsync))
11843 Flags.setSwiftAsync();
11844 if (Arg.hasAttribute(Attribute::SwiftError))
11845 Flags.setSwiftError();
11846 if (Arg.hasAttribute(Attribute::ByVal))
11847 Flags.setByVal();
11848 if (Arg.hasAttribute(Attribute::ByRef))
11849 Flags.setByRef();
11850 if (Arg.hasAttribute(Attribute::InAlloca)) {
11851 Flags.setInAlloca();
11852 // Set the byval flag for CCAssignFn callbacks that don't know about
11853 // inalloca. This way we can know how many bytes we should've allocated
11854 // and how many bytes a callee cleanup function will pop. If we port
11855 // inalloca to more targets, we'll have to add custom inalloca handling
11856 // in the various CC lowering callbacks.
11857 Flags.setByVal();
11858 }
11859 if (Arg.hasAttribute(Attribute::Preallocated)) {
11860 Flags.setPreallocated();
11861 // Set the byval flag for CCAssignFn callbacks that don't know about
11862 // preallocated. This way we can know how many bytes we should've
11863 // allocated and how many bytes a callee cleanup function will pop. If
11864 // we port preallocated to more targets, we'll have to add custom
11865 // preallocated handling in the various CC lowering callbacks.
11866 Flags.setByVal();
11867 }
11868
11869 // Certain targets (such as MIPS), may have a different ABI alignment
11870 // for a type depending on the context. Give the target a chance to
11871 // specify the alignment it wants.
11872 const Align OriginalAlignment(
11873 TLI->getABIAlignmentForCallingConv(ArgTy, DL));
11874 Flags.setOrigAlign(OriginalAlignment);
11875
11876 Align MemAlign;
11877 Type *ArgMemTy = nullptr;
11878 if (Flags.isByVal() || Flags.isInAlloca() || Flags.isPreallocated() ||
11879 Flags.isByRef()) {
11880 if (!ArgMemTy)
11881 ArgMemTy = Arg.getPointeeInMemoryValueType();
11882
11883 uint64_t MemSize = DL.getTypeAllocSize(ArgMemTy);
11884
11885 // For in-memory arguments, size and alignment should be passed from FE.
11886 // BE will guess if this info is not there but there are cases it cannot
11887 // get right.
11888 if (auto ParamAlign = Arg.getParamStackAlign())
11889 MemAlign = *ParamAlign;
11890 else if ((ParamAlign = Arg.getParamAlign()))
11891 MemAlign = *ParamAlign;
11892 else
11893 MemAlign = TLI->getByValTypeAlignment(ArgMemTy, DL);
11894 if (Flags.isByRef())
11895 Flags.setByRefSize(MemSize);
11896 else
11897 Flags.setByValSize(MemSize);
11898 } else if (auto ParamAlign = Arg.getParamStackAlign()) {
11899 MemAlign = *ParamAlign;
11900 } else {
11901 MemAlign = OriginalAlignment;
11902 }
11903 Flags.setMemAlign(MemAlign);
11904
11905 if (Arg.hasAttribute(Attribute::Nest))
11906 Flags.setNest();
11907 if (NeedsRegBlock)
11908 Flags.setInConsecutiveRegs();
11909 if (ArgCopyElisionCandidates.count(&Arg))
11910 Flags.setCopyElisionCandidate();
11911 if (Arg.hasAttribute(Attribute::Returned))
11912 Flags.setReturned();
11913
11914 MVT RegisterVT = TLI->getRegisterTypeForCallingConv(
11915 *CurDAG->getContext(), F.getCallingConv(), VT);
11916 unsigned NumRegs = TLI->getNumRegistersForCallingConv(
11917 *CurDAG->getContext(), F.getCallingConv(), VT);
11918 for (unsigned i = 0; i != NumRegs; ++i) {
11919 // For scalable vectors, use the minimum size; individual targets
11920 // are responsible for handling scalable vector arguments and
11921 // return values.
11922 ISD::InputArg MyFlags(
11923 Flags, RegisterVT, VT, ArgTy, isArgValueUsed, ArgNo,
11924 PartBase + i * RegisterVT.getStoreSize().getKnownMinValue());
11925 if (NumRegs > 1 && i == 0)
11926 MyFlags.Flags.setSplit();
11927 // if it isn't first piece, alignment must be 1
11928 else if (i > 0) {
11929 MyFlags.Flags.setOrigAlign(Align(1));
11930 if (i == NumRegs - 1)
11931 MyFlags.Flags.setSplitEnd();
11932 }
11933 Ins.push_back(MyFlags);
11934 }
11935 if (NeedsRegBlock && Value == NumValues - 1)
11936 Ins[Ins.size() - 1].Flags.setInConsecutiveRegsLast();
11937 PartBase += VT.getStoreSize().getKnownMinValue();
11938 }
11939 }
11940
11941 // Call the target to set up the argument values.
11943 SDValue NewRoot = TLI->LowerFormalArguments(
11944 DAG.getRoot(), F.getCallingConv(), F.isVarArg(), Ins, dl, DAG, InVals);
11945
11946 // Verify that the target's LowerFormalArguments behaved as expected.
11947 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
11948 "LowerFormalArguments didn't return a valid chain!");
11949 assert(InVals.size() == Ins.size() &&
11950 "LowerFormalArguments didn't emit the correct number of values!");
11951 LLVM_DEBUG({
11952 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
11953 assert(InVals[i].getNode() &&
11954 "LowerFormalArguments emitted a null value!");
11955 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
11956 "LowerFormalArguments emitted a value with the wrong type!");
11957 }
11958 });
11959
11960 // Update the DAG with the new chain value resulting from argument lowering.
11961 DAG.setRoot(NewRoot);
11962
11963 // Set up the argument values.
11964 unsigned i = 0;
11965 if (!FuncInfo->CanLowerReturn) {
11966 // Create a virtual register for the sret pointer, and put in a copy
11967 // from the sret argument into it.
11968 MVT VT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
11969 MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
11970 std::optional<ISD::NodeType> AssertOp;
11971 SDValue ArgValue =
11972 getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT, VT, nullptr, NewRoot,
11973 F.getCallingConv(), AssertOp);
11974
11975 MachineFunction& MF = SDB->DAG.getMachineFunction();
11976 MachineRegisterInfo& RegInfo = MF.getRegInfo();
11977 Register SRetReg =
11978 RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
11979 FuncInfo->DemoteRegister = SRetReg;
11980 NewRoot =
11981 SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(), SRetReg, ArgValue);
11982 DAG.setRoot(NewRoot);
11983
11984 // i indexes lowered arguments. Bump it past the hidden sret argument.
11985 ++i;
11986 }
11987
11989 DenseMap<int, int> ArgCopyElisionFrameIndexMap;
11990 for (const Argument &Arg : F.args()) {
11991 SmallVector<SDValue, 4> ArgValues;
11992 SmallVector<EVT, 4> ValueVTs;
11993 ComputeValueVTs(*TLI, DAG.getDataLayout(), Arg.getType(), ValueVTs);
11994 unsigned NumValues = ValueVTs.size();
11995 if (NumValues == 0)
11996 continue;
11997
11998 bool ArgHasUses = !Arg.use_empty();
11999
12000 // Elide the copying store if the target loaded this argument from a
12001 // suitable fixed stack object.
12002 if (Ins[i].Flags.isCopyElisionCandidate()) {
12003 unsigned NumParts = 0;
12004 for (EVT VT : ValueVTs)
12005 NumParts += TLI->getNumRegistersForCallingConv(*CurDAG->getContext(),
12006 F.getCallingConv(), VT);
12007
12008 tryToElideArgumentCopy(*FuncInfo, Chains, ArgCopyElisionFrameIndexMap,
12009 ElidedArgCopyInstrs, ArgCopyElisionCandidates, Arg,
12010 ArrayRef(&InVals[i], NumParts), ArgHasUses);
12011 }
12012
12013 // If this argument is unused then remember its value. It is used to generate
12014 // debugging information.
12015 bool isSwiftErrorArg =
12016 TLI->supportSwiftError() &&
12017 Arg.hasAttribute(Attribute::SwiftError);
12018 if (!ArgHasUses && !isSwiftErrorArg) {
12019 SDB->setUnusedArgValue(&Arg, InVals[i]);
12020
12021 // Also remember any frame index for use in FastISel.
12022 if (FrameIndexSDNode *FI =
12024 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
12025 }
12026
12027 for (unsigned Val = 0; Val != NumValues; ++Val) {
12028 EVT VT = ValueVTs[Val];
12029 MVT PartVT = TLI->getRegisterTypeForCallingConv(*CurDAG->getContext(),
12030 F.getCallingConv(), VT);
12031 unsigned NumParts = TLI->getNumRegistersForCallingConv(
12032 *CurDAG->getContext(), F.getCallingConv(), VT);
12033
12034 // Even an apparent 'unused' swifterror argument needs to be returned. So
12035 // we do generate a copy for it that can be used on return from the
12036 // function.
12037 if (ArgHasUses || isSwiftErrorArg) {
12038 std::optional<ISD::NodeType> AssertOp;
12039 if (Arg.hasAttribute(Attribute::SExt))
12040 AssertOp = ISD::AssertSext;
12041 else if (Arg.hasAttribute(Attribute::ZExt))
12042 AssertOp = ISD::AssertZext;
12043
12044 SDValue OutVal =
12045 getCopyFromParts(DAG, dl, &InVals[i], NumParts, PartVT, VT, nullptr,
12046 NewRoot, F.getCallingConv(), AssertOp);
12047
12048 FPClassTest NoFPClass = Arg.getNoFPClass();
12049 if (NoFPClass != fcNone) {
12050 SDValue SDNoFPClass = DAG.getTargetConstant(
12051 static_cast<uint64_t>(NoFPClass), dl, MVT::i32);
12052 OutVal = DAG.getNode(ISD::AssertNoFPClass, dl, OutVal.getValueType(),
12053 OutVal, SDNoFPClass);
12054 }
12055 ArgValues.push_back(OutVal);
12056 }
12057
12058 i += NumParts;
12059 }
12060
12061 // We don't need to do anything else for unused arguments.
12062 if (ArgValues.empty())
12063 continue;
12064
12065 // Note down frame index.
12066 if (FrameIndexSDNode *FI =
12067 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
12068 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
12069
12070 SDValue Res = DAG.getMergeValues(ArrayRef(ArgValues.data(), NumValues),
12071 SDB->getCurSDLoc());
12072
12073 SDB->setValue(&Arg, Res);
12074 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) {
12075 // We want to associate the argument with the frame index, among
12076 // involved operands, that correspond to the lowest address. The
12077 // getCopyFromParts function, called earlier, is swapping the order of
12078 // the operands to BUILD_PAIR depending on endianness. The result of
12079 // that swapping is that the least significant bits of the argument will
12080 // be in the first operand of the BUILD_PAIR node, and the most
12081 // significant bits will be in the second operand.
12082 unsigned LowAddressOp = DAG.getDataLayout().isBigEndian() ? 1 : 0;
12083 if (LoadSDNode *LNode =
12084 dyn_cast<LoadSDNode>(Res.getOperand(LowAddressOp).getNode()))
12085 if (FrameIndexSDNode *FI =
12086 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
12087 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
12088 }
12089
12090 // Analyses past this point are naive and don't expect an assertion.
12091 if (Res.getOpcode() == ISD::AssertZext)
12092 Res = Res.getOperand(0);
12093
12094 // Update the SwiftErrorVRegDefMap.
12095 if (Res.getOpcode() == ISD::CopyFromReg && isSwiftErrorArg) {
12096 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
12097 if (Reg.isVirtual())
12098 SwiftError->setCurrentVReg(FuncInfo->MBB, SwiftError->getFunctionArg(),
12099 Reg);
12100 }
12101
12102 // If this argument is live outside of the entry block, insert a copy from
12103 // wherever we got it to the vreg that other BB's will reference it as.
12104 if (Res.getOpcode() == ISD::CopyFromReg) {
12105 // If we can, though, try to skip creating an unnecessary vreg.
12106 // FIXME: This isn't very clean... it would be nice to make this more
12107 // general.
12108 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
12109 if (Reg.isVirtual()) {
12110 FuncInfo->ValueMap[&Arg] = Reg;
12111 continue;
12112 }
12113 }
12114 if (!isOnlyUsedInEntryBlock(&Arg, TM.Options.EnableFastISel)) {
12115 FuncInfo->InitializeRegForValue(&Arg);
12116 SDB->CopyToExportRegsIfNeeded(&Arg);
12117 }
12118 }
12119
12120 if (!Chains.empty()) {
12121 Chains.push_back(NewRoot);
12122 NewRoot = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
12123 }
12124
12125 DAG.setRoot(NewRoot);
12126
12127 assert(i == InVals.size() && "Argument register count mismatch!");
12128
12129 // If any argument copy elisions occurred and we have debug info, update the
12130 // stale frame indices used in the dbg.declare variable info table.
12131 if (!ArgCopyElisionFrameIndexMap.empty()) {
12132 for (MachineFunction::VariableDbgInfo &VI :
12133 MF->getInStackSlotVariableDbgInfo()) {
12134 auto I = ArgCopyElisionFrameIndexMap.find(VI.getStackSlot());
12135 if (I != ArgCopyElisionFrameIndexMap.end())
12136 VI.updateStackSlot(I->second);
12137 }
12138 }
12139
12140 // Finally, if the target has anything special to do, allow it to do so.
12142}
12143
12144/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
12145/// ensure constants are generated when needed. Remember the virtual registers
12146/// that need to be added to the Machine PHI nodes as input. We cannot just
12147/// directly add them, because expansion might result in multiple MBB's for one
12148/// BB. As such, the start of the BB might correspond to a different MBB than
12149/// the end.
12150void
12151SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
12152 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12153
12154 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
12155
12156 // Check PHI nodes in successors that expect a value to be available from this
12157 // block.
12158 for (const BasicBlock *SuccBB : successors(LLVMBB->getTerminator())) {
12159 if (!isa<PHINode>(SuccBB->begin())) continue;
12160 MachineBasicBlock *SuccMBB = FuncInfo.getMBB(SuccBB);
12161
12162 // If this terminator has multiple identical successors (common for
12163 // switches), only handle each succ once.
12164 if (!SuccsHandled.insert(SuccMBB).second)
12165 continue;
12166
12168
12169 // At this point we know that there is a 1-1 correspondence between LLVM PHI
12170 // nodes and Machine PHI nodes, but the incoming operands have not been
12171 // emitted yet.
12172 for (const PHINode &PN : SuccBB->phis()) {
12173 // Ignore dead phi's.
12174 if (PN.use_empty())
12175 continue;
12176
12177 // Skip empty types
12178 if (PN.getType()->isEmptyTy())
12179 continue;
12180
12181 Register Reg;
12182 const Value *PHIOp = PN.getIncomingValueForBlock(LLVMBB);
12183
12184 if (const auto *C = dyn_cast<Constant>(PHIOp)) {
12185 Register &RegOut = ConstantsOut[C];
12186 if (!RegOut) {
12187 RegOut = FuncInfo.CreateRegs(&PN);
12188 // We need to zero/sign extend ConstantInt phi operands to match
12189 // assumptions in FunctionLoweringInfo::ComputePHILiveOutRegInfo.
12190 ISD::NodeType ExtendType = ISD::ANY_EXTEND;
12191 if (auto *CI = dyn_cast<ConstantInt>(C))
12192 ExtendType = TLI.signExtendConstant(CI) ? ISD::SIGN_EXTEND
12194 CopyValueToVirtualRegister(C, RegOut, ExtendType);
12195 }
12196 Reg = RegOut;
12197 } else {
12199 FuncInfo.ValueMap.find(PHIOp);
12200 if (I != FuncInfo.ValueMap.end())
12201 Reg = I->second;
12202 else {
12203 assert(isa<AllocaInst>(PHIOp) &&
12204 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
12205 "Didn't codegen value into a register!??");
12206 Reg = FuncInfo.CreateRegs(&PN);
12208 }
12209 }
12210
12211 // Remember that this register needs to added to the machine PHI node as
12212 // the input for this MBB.
12213 SmallVector<EVT, 4> ValueVTs;
12214 ComputeValueVTs(TLI, DAG.getDataLayout(), PN.getType(), ValueVTs);
12215 for (EVT VT : ValueVTs) {
12216 const unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
12217 for (unsigned i = 0; i != NumRegisters; ++i)
12218 FuncInfo.PHINodesToUpdate.emplace_back(&*MBBI++, Reg + i);
12219 Reg += NumRegisters;
12220 }
12221 }
12222 }
12223
12224 ConstantsOut.clear();
12225}
12226
12227MachineBasicBlock *SelectionDAGBuilder::NextBlock(MachineBasicBlock *MBB) {
12229 if (++I == FuncInfo.MF->end())
12230 return nullptr;
12231 return &*I;
12232}
12233
12234/// During lowering new call nodes can be created (such as memset, etc.).
12235/// Those will become new roots of the current DAG, but complications arise
12236/// when they are tail calls. In such cases, the call lowering will update
12237/// the root, but the builder still needs to know that a tail call has been
12238/// lowered in order to avoid generating an additional return.
12239void SelectionDAGBuilder::updateDAGForMaybeTailCall(SDValue MaybeTC) {
12240 // If the node is null, we do have a tail call.
12241 if (MaybeTC.getNode() != nullptr)
12242 DAG.setRoot(MaybeTC);
12243 else
12244 HasTailCall = true;
12245}
12246
12247void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond,
12248 MachineBasicBlock *SwitchMBB,
12249 MachineBasicBlock *DefaultMBB) {
12250 MachineFunction *CurMF = FuncInfo.MF;
12251 MachineBasicBlock *NextMBB = nullptr;
12253 if (++BBI != FuncInfo.MF->end())
12254 NextMBB = &*BBI;
12255
12256 unsigned Size = W.LastCluster - W.FirstCluster + 1;
12257
12258 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12259
12260 if (Size == 2 && W.MBB == SwitchMBB) {
12261 // If any two of the cases has the same destination, and if one value
12262 // is the same as the other, but has one bit unset that the other has set,
12263 // use bit manipulation to do two compares at once. For example:
12264 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
12265 // TODO: This could be extended to merge any 2 cases in switches with 3
12266 // cases.
12267 // TODO: Handle cases where W.CaseBB != SwitchBB.
12268 CaseCluster &Small = *W.FirstCluster;
12269 CaseCluster &Big = *W.LastCluster;
12270
12271 if (Small.Low == Small.High && Big.Low == Big.High &&
12272 Small.MBB == Big.MBB) {
12273 const APInt &SmallValue = Small.Low->getValue();
12274 const APInt &BigValue = Big.Low->getValue();
12275
12276 // Check that there is only one bit different.
12277 APInt CommonBit = BigValue ^ SmallValue;
12278 if (CommonBit.isPowerOf2()) {
12279 SDValue CondLHS = getValue(Cond);
12280 EVT VT = CondLHS.getValueType();
12281 SDLoc DL = getCurSDLoc();
12282
12283 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
12284 DAG.getConstant(CommonBit, DL, VT));
12285 SDValue Cond = DAG.getSetCC(
12286 DL, MVT::i1, Or, DAG.getConstant(BigValue | SmallValue, DL, VT),
12287 ISD::SETEQ);
12288
12289 // Update successor info.
12290 // Both Small and Big will jump to Small.BB, so we sum up the
12291 // probabilities.
12292 addSuccessorWithProb(SwitchMBB, Small.MBB, Small.Prob + Big.Prob);
12293 if (BPI)
12294 addSuccessorWithProb(
12295 SwitchMBB, DefaultMBB,
12296 // The default destination is the first successor in IR.
12297 BPI->getEdgeProbability(SwitchMBB->getBasicBlock(), (unsigned)0));
12298 else
12299 addSuccessorWithProb(SwitchMBB, DefaultMBB);
12300
12301 // Insert the true branch.
12302 SDValue BrCond =
12303 DAG.getNode(ISD::BRCOND, DL, MVT::Other, getControlRoot(), Cond,
12304 DAG.getBasicBlock(Small.MBB));
12305 // Insert the false branch.
12306 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
12307 DAG.getBasicBlock(DefaultMBB));
12308
12309 DAG.setRoot(BrCond);
12310 return;
12311 }
12312 }
12313 }
12314
12315 if (TM.getOptLevel() != CodeGenOptLevel::None) {
12316 // Here, we order cases by probability so the most likely case will be
12317 // checked first. However, two clusters can have the same probability in
12318 // which case their relative ordering is non-deterministic. So we use Low
12319 // as a tie-breaker as clusters are guaranteed to never overlap.
12320 llvm::sort(W.FirstCluster, W.LastCluster + 1,
12321 [](const CaseCluster &a, const CaseCluster &b) {
12322 return a.Prob != b.Prob ?
12323 a.Prob > b.Prob :
12324 a.Low->getValue().slt(b.Low->getValue());
12325 });
12326
12327 // Rearrange the case blocks so that the last one falls through if possible
12328 // without changing the order of probabilities.
12329 for (CaseClusterIt I = W.LastCluster; I > W.FirstCluster; ) {
12330 --I;
12331 if (I->Prob > W.LastCluster->Prob)
12332 break;
12333 if (I->Kind == CC_Range && I->MBB == NextMBB) {
12334 std::swap(*I, *W.LastCluster);
12335 break;
12336 }
12337 }
12338 }
12339
12340 // Compute total probability.
12341 BranchProbability DefaultProb = W.DefaultProb;
12342 BranchProbability UnhandledProbs = DefaultProb;
12343 for (CaseClusterIt I = W.FirstCluster; I <= W.LastCluster; ++I)
12344 UnhandledProbs += I->Prob;
12345
12346 MachineBasicBlock *CurMBB = W.MBB;
12347 for (CaseClusterIt I = W.FirstCluster, E = W.LastCluster; I <= E; ++I) {
12348 bool FallthroughUnreachable = false;
12349 MachineBasicBlock *Fallthrough;
12350 if (I == W.LastCluster) {
12351 // For the last cluster, fall through to the default destination.
12352 Fallthrough = DefaultMBB;
12353 FallthroughUnreachable = isa<UnreachableInst>(
12354 DefaultMBB->getBasicBlock()->getFirstNonPHIOrDbg());
12355 } else {
12356 Fallthrough = CurMF->CreateMachineBasicBlock(CurMBB->getBasicBlock());
12357 CurMF->insert(BBI, Fallthrough);
12358 // Put Cond in a virtual register to make it available from the new blocks.
12360 }
12361 UnhandledProbs -= I->Prob;
12362
12363 switch (I->Kind) {
12364 case CC_JumpTable: {
12365 // FIXME: Optimize away range check based on pivot comparisons.
12366 JumpTableHeader *JTH = &SL->JTCases[I->JTCasesIndex].first;
12367 SwitchCG::JumpTable *JT = &SL->JTCases[I->JTCasesIndex].second;
12368
12369 // The jump block hasn't been inserted yet; insert it here.
12370 MachineBasicBlock *JumpMBB = JT->MBB;
12371 CurMF->insert(BBI, JumpMBB);
12372
12373 auto JumpProb = I->Prob;
12374 auto FallthroughProb = UnhandledProbs;
12375
12376 // If the default statement is a target of the jump table, we evenly
12377 // distribute the default probability to successors of CurMBB. Also
12378 // update the probability on the edge from JumpMBB to Fallthrough.
12379 for (MachineBasicBlock::succ_iterator SI = JumpMBB->succ_begin(),
12380 SE = JumpMBB->succ_end();
12381 SI != SE; ++SI) {
12382 if (*SI == DefaultMBB) {
12383 JumpProb += DefaultProb / 2;
12384 FallthroughProb -= DefaultProb / 2;
12385 JumpMBB->setSuccProbability(SI, DefaultProb / 2);
12386 JumpMBB->normalizeSuccProbs();
12387 break;
12388 }
12389 }
12390
12391 // If the default clause is unreachable, propagate that knowledge into
12392 // JTH->FallthroughUnreachable which will use it to suppress the range
12393 // check.
12394 //
12395 // However, don't do this if we're doing branch target enforcement,
12396 // because a table branch _without_ a range check can be a tempting JOP
12397 // gadget - out-of-bounds inputs that are impossible in correct
12398 // execution become possible again if an attacker can influence the
12399 // control flow. So if an attacker doesn't already have a BTI bypass
12400 // available, we don't want them to be able to get one out of this
12401 // table branch.
12402 if (FallthroughUnreachable) {
12403 Function &CurFunc = CurMF->getFunction();
12404 if (!CurFunc.hasFnAttribute("branch-target-enforcement"))
12405 JTH->FallthroughUnreachable = true;
12406 }
12407
12408 if (!JTH->FallthroughUnreachable)
12409 addSuccessorWithProb(CurMBB, Fallthrough, FallthroughProb);
12410 addSuccessorWithProb(CurMBB, JumpMBB, JumpProb);
12411 CurMBB->normalizeSuccProbs();
12412
12413 // The jump table header will be inserted in our current block, do the
12414 // range check, and fall through to our fallthrough block.
12415 JTH->HeaderBB = CurMBB;
12416 JT->Default = Fallthrough; // FIXME: Move Default to JumpTableHeader.
12417
12418 // If we're in the right place, emit the jump table header right now.
12419 if (CurMBB == SwitchMBB) {
12420 visitJumpTableHeader(*JT, *JTH, SwitchMBB);
12421 JTH->Emitted = true;
12422 }
12423 break;
12424 }
12425 case CC_BitTests: {
12426 // FIXME: Optimize away range check based on pivot comparisons.
12427 BitTestBlock *BTB = &SL->BitTestCases[I->BTCasesIndex];
12428
12429 // The bit test blocks haven't been inserted yet; insert them here.
12430 for (BitTestCase &BTC : BTB->Cases)
12431 CurMF->insert(BBI, BTC.ThisBB);
12432
12433 // Fill in fields of the BitTestBlock.
12434 BTB->Parent = CurMBB;
12435 BTB->Default = Fallthrough;
12436
12437 BTB->DefaultProb = UnhandledProbs;
12438 // If the cases in bit test don't form a contiguous range, we evenly
12439 // distribute the probability on the edge to Fallthrough to two
12440 // successors of CurMBB.
12441 if (!BTB->ContiguousRange) {
12442 BTB->Prob += DefaultProb / 2;
12443 BTB->DefaultProb -= DefaultProb / 2;
12444 }
12445
12446 if (FallthroughUnreachable)
12447 BTB->FallthroughUnreachable = true;
12448
12449 // If we're in the right place, emit the bit test header right now.
12450 if (CurMBB == SwitchMBB) {
12451 visitBitTestHeader(*BTB, SwitchMBB);
12452 BTB->Emitted = true;
12453 }
12454 break;
12455 }
12456 case CC_Range: {
12457 const Value *RHS, *LHS, *MHS;
12458 ISD::CondCode CC;
12459 if (I->Low == I->High) {
12460 // Check Cond == I->Low.
12461 CC = ISD::SETEQ;
12462 LHS = Cond;
12463 RHS=I->Low;
12464 MHS = nullptr;
12465 } else {
12466 // Check I->Low <= Cond <= I->High.
12467 CC = ISD::SETLE;
12468 LHS = I->Low;
12469 MHS = Cond;
12470 RHS = I->High;
12471 }
12472
12473 // If Fallthrough is unreachable, fold away the comparison.
12474 if (FallthroughUnreachable)
12475 CC = ISD::SETTRUE;
12476
12477 // The false probability is the sum of all unhandled cases.
12478 CaseBlock CB(CC, LHS, RHS, MHS, I->MBB, Fallthrough, CurMBB,
12479 getCurSDLoc(), I->Prob, UnhandledProbs);
12480
12481 if (CurMBB == SwitchMBB)
12482 visitSwitchCase(CB, SwitchMBB);
12483 else
12484 SL->SwitchCases.push_back(CB);
12485
12486 break;
12487 }
12488 }
12489 CurMBB = Fallthrough;
12490 }
12491}
12492
12493void SelectionDAGBuilder::splitWorkItem(SwitchWorkList &WorkList,
12494 const SwitchWorkListItem &W,
12495 Value *Cond,
12496 MachineBasicBlock *SwitchMBB) {
12497 assert(W.FirstCluster->Low->getValue().slt(W.LastCluster->Low->getValue()) &&
12498 "Clusters not sorted?");
12499 assert(W.LastCluster - W.FirstCluster + 1 >= 2 && "Too small to split!");
12500
12501 auto [LastLeft, FirstRight, LeftProb, RightProb] =
12502 SL->computeSplitWorkItemInfo(W);
12503
12504 // Use the first element on the right as pivot since we will make less-than
12505 // comparisons against it.
12506 CaseClusterIt PivotCluster = FirstRight;
12507 assert(PivotCluster > W.FirstCluster);
12508 assert(PivotCluster <= W.LastCluster);
12509
12510 CaseClusterIt FirstLeft = W.FirstCluster;
12511 CaseClusterIt LastRight = W.LastCluster;
12512
12513 const ConstantInt *Pivot = PivotCluster->Low;
12514
12515 // New blocks will be inserted immediately after the current one.
12517 ++BBI;
12518
12519 // We will branch to the LHS if Value < Pivot. If LHS is a single cluster,
12520 // we can branch to its destination directly if it's squeezed exactly in
12521 // between the known lower bound and Pivot - 1.
12522 MachineBasicBlock *LeftMBB;
12523 if (FirstLeft == LastLeft && FirstLeft->Kind == CC_Range &&
12524 FirstLeft->Low == W.GE &&
12525 (FirstLeft->High->getValue() + 1LL) == Pivot->getValue()) {
12526 LeftMBB = FirstLeft->MBB;
12527 } else {
12528 LeftMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12529 FuncInfo.MF->insert(BBI, LeftMBB);
12530 WorkList.push_back(
12531 {LeftMBB, FirstLeft, LastLeft, W.GE, Pivot, W.DefaultProb / 2});
12532 // Put Cond in a virtual register to make it available from the new blocks.
12534 }
12535
12536 // Similarly, we will branch to the RHS if Value >= Pivot. If RHS is a
12537 // single cluster, RHS.Low == Pivot, and we can branch to its destination
12538 // directly if RHS.High equals the current upper bound.
12539 MachineBasicBlock *RightMBB;
12540 if (FirstRight == LastRight && FirstRight->Kind == CC_Range &&
12541 W.LT && (FirstRight->High->getValue() + 1ULL) == W.LT->getValue()) {
12542 RightMBB = FirstRight->MBB;
12543 } else {
12544 RightMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12545 FuncInfo.MF->insert(BBI, RightMBB);
12546 WorkList.push_back(
12547 {RightMBB, FirstRight, LastRight, Pivot, W.LT, W.DefaultProb / 2});
12548 // Put Cond in a virtual register to make it available from the new blocks.
12550 }
12551
12552 // Create the CaseBlock record that will be used to lower the branch.
12553 CaseBlock CB(ISD::SETLT, Cond, Pivot, nullptr, LeftMBB, RightMBB, W.MBB,
12554 getCurSDLoc(), LeftProb, RightProb);
12555
12556 if (W.MBB == SwitchMBB)
12557 visitSwitchCase(CB, SwitchMBB);
12558 else
12559 SL->SwitchCases.push_back(CB);
12560}
12561
12562// Scale CaseProb after peeling a case with the probablity of PeeledCaseProb
12563// from the swith statement.
12565 BranchProbability PeeledCaseProb) {
12566 if (PeeledCaseProb == BranchProbability::getOne())
12568 BranchProbability SwitchProb = PeeledCaseProb.getCompl();
12569
12570 uint32_t Numerator = CaseProb.getNumerator();
12571 uint32_t Denominator = SwitchProb.scale(CaseProb.getDenominator());
12572 return BranchProbability(Numerator, std::max(Numerator, Denominator));
12573}
12574
12575// Try to peel the top probability case if it exceeds the threshold.
12576// Return current MachineBasicBlock for the switch statement if the peeling
12577// does not occur.
12578// If the peeling is performed, return the newly created MachineBasicBlock
12579// for the peeled switch statement. Also update Clusters to remove the peeled
12580// case. PeeledCaseProb is the BranchProbability for the peeled case.
12581MachineBasicBlock *SelectionDAGBuilder::peelDominantCaseCluster(
12582 const SwitchInst &SI, CaseClusterVector &Clusters,
12583 BranchProbability &PeeledCaseProb) {
12584 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12585 // Don't perform if there is only one cluster or optimizing for size.
12586 if (SwitchPeelThreshold > 100 || !FuncInfo.BPI || Clusters.size() < 2 ||
12587 TM.getOptLevel() == CodeGenOptLevel::None ||
12588 SwitchMBB->getParent()->getFunction().hasMinSize())
12589 return SwitchMBB;
12590
12591 BranchProbability TopCaseProb = BranchProbability(SwitchPeelThreshold, 100);
12592 unsigned PeeledCaseIndex = 0;
12593 bool SwitchPeeled = false;
12594 for (unsigned Index = 0; Index < Clusters.size(); ++Index) {
12595 CaseCluster &CC = Clusters[Index];
12596 if (CC.Prob < TopCaseProb)
12597 continue;
12598 TopCaseProb = CC.Prob;
12599 PeeledCaseIndex = Index;
12600 SwitchPeeled = true;
12601 }
12602 if (!SwitchPeeled)
12603 return SwitchMBB;
12604
12605 LLVM_DEBUG(dbgs() << "Peeled one top case in switch stmt, prob: "
12606 << TopCaseProb << "\n");
12607
12608 // Record the MBB for the peeled switch statement.
12609 MachineFunction::iterator BBI(SwitchMBB);
12610 ++BBI;
12611 MachineBasicBlock *PeeledSwitchMBB =
12612 FuncInfo.MF->CreateMachineBasicBlock(SwitchMBB->getBasicBlock());
12613 FuncInfo.MF->insert(BBI, PeeledSwitchMBB);
12614
12615 ExportFromCurrentBlock(SI.getCondition());
12616 auto PeeledCaseIt = Clusters.begin() + PeeledCaseIndex;
12617 SwitchWorkListItem W = {SwitchMBB, PeeledCaseIt, PeeledCaseIt,
12618 nullptr, nullptr, TopCaseProb.getCompl()};
12619 lowerWorkItem(W, SI.getCondition(), SwitchMBB, PeeledSwitchMBB);
12620
12621 Clusters.erase(PeeledCaseIt);
12622 for (CaseCluster &CC : Clusters) {
12623 LLVM_DEBUG(
12624 dbgs() << "Scale the probablity for one cluster, before scaling: "
12625 << CC.Prob << "\n");
12626 CC.Prob = scaleCaseProbality(CC.Prob, TopCaseProb);
12627 LLVM_DEBUG(dbgs() << "After scaling: " << CC.Prob << "\n");
12628 }
12629 PeeledCaseProb = TopCaseProb;
12630 return PeeledSwitchMBB;
12631}
12632
12633void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
12634 // Extract cases from the switch.
12635 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12636 CaseClusterVector Clusters;
12637 Clusters.reserve(SI.getNumCases());
12638 for (auto I : SI.cases()) {
12639 MachineBasicBlock *Succ = FuncInfo.getMBB(I.getCaseSuccessor());
12640 const ConstantInt *CaseVal = I.getCaseValue();
12641 BranchProbability Prob =
12642 BPI ? BPI->getEdgeProbability(SI.getParent(), I.getSuccessorIndex())
12643 : BranchProbability(1, SI.getNumCases() + 1);
12644 Clusters.push_back(CaseCluster::range(CaseVal, CaseVal, Succ, Prob));
12645 }
12646
12647 MachineBasicBlock *DefaultMBB = FuncInfo.getMBB(SI.getDefaultDest());
12648
12649 // Cluster adjacent cases with the same destination. We do this at all
12650 // optimization levels because it's cheap to do and will make codegen faster
12651 // if there are many clusters.
12652 sortAndRangeify(Clusters);
12653
12654 // The branch probablity of the peeled case.
12655 BranchProbability PeeledCaseProb = BranchProbability::getZero();
12656 MachineBasicBlock *PeeledSwitchMBB =
12657 peelDominantCaseCluster(SI, Clusters, PeeledCaseProb);
12658
12659 // If there is only the default destination, jump there directly.
12660 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12661 if (Clusters.empty()) {
12662 assert(PeeledSwitchMBB == SwitchMBB);
12663 SwitchMBB->addSuccessor(DefaultMBB);
12664 if (DefaultMBB != NextBlock(SwitchMBB)) {
12665 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
12666 getControlRoot(), DAG.getBasicBlock(DefaultMBB)));
12667 }
12668 return;
12669 }
12670
12671 SL->findJumpTables(Clusters, &SI, getCurSDLoc(), DefaultMBB, DAG.getPSI(),
12672 DAG.getBFI());
12673 SL->findBitTestClusters(Clusters, &SI);
12674
12675 LLVM_DEBUG({
12676 dbgs() << "Case clusters: ";
12677 for (const CaseCluster &C : Clusters) {
12678 if (C.Kind == CC_JumpTable)
12679 dbgs() << "JT:";
12680 if (C.Kind == CC_BitTests)
12681 dbgs() << "BT:";
12682
12683 C.Low->getValue().print(dbgs(), true);
12684 if (C.Low != C.High) {
12685 dbgs() << '-';
12686 C.High->getValue().print(dbgs(), true);
12687 }
12688 dbgs() << ' ';
12689 }
12690 dbgs() << '\n';
12691 });
12692
12693 assert(!Clusters.empty());
12694 SwitchWorkList WorkList;
12695 CaseClusterIt First = Clusters.begin();
12696 CaseClusterIt Last = Clusters.end() - 1;
12697 auto DefaultProb = getEdgeProbability(PeeledSwitchMBB, DefaultMBB);
12698 // Scale the branchprobability for DefaultMBB if the peel occurs and
12699 // DefaultMBB is not replaced.
12700 if (PeeledCaseProb != BranchProbability::getZero() &&
12701 DefaultMBB == FuncInfo.getMBB(SI.getDefaultDest()))
12702 DefaultProb = scaleCaseProbality(DefaultProb, PeeledCaseProb);
12703 WorkList.push_back(
12704 {PeeledSwitchMBB, First, Last, nullptr, nullptr, DefaultProb});
12705
12706 while (!WorkList.empty()) {
12707 SwitchWorkListItem W = WorkList.pop_back_val();
12708 unsigned NumClusters = W.LastCluster - W.FirstCluster + 1;
12709
12710 if (NumClusters > 3 && TM.getOptLevel() != CodeGenOptLevel::None &&
12711 !DefaultMBB->getParent()->getFunction().hasMinSize()) {
12712 // For optimized builds, lower large range as a balanced binary tree.
12713 splitWorkItem(WorkList, W, SI.getCondition(), SwitchMBB);
12714 continue;
12715 }
12716
12717 lowerWorkItem(W, SI.getCondition(), SwitchMBB, DefaultMBB);
12718 }
12719}
12720
12721void SelectionDAGBuilder::visitStepVector(const CallInst &I) {
12722 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12723 auto DL = getCurSDLoc();
12724 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12725 setValue(&I, DAG.getStepVector(DL, ResultVT));
12726}
12727
12728void SelectionDAGBuilder::visitVectorReverse(const CallInst &I) {
12729 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12730 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12731
12732 SDLoc DL = getCurSDLoc();
12733 SDValue V = getValue(I.getOperand(0));
12734 assert(VT == V.getValueType() && "Malformed vector.reverse!");
12735
12736 if (VT.isScalableVector()) {
12737 setValue(&I, DAG.getNode(ISD::VECTOR_REVERSE, DL, VT, V));
12738 return;
12739 }
12740
12741 // Use VECTOR_SHUFFLE for the fixed-length vector
12742 // to maintain existing behavior.
12743 SmallVector<int, 8> Mask;
12744 unsigned NumElts = VT.getVectorMinNumElements();
12745 for (unsigned i = 0; i != NumElts; ++i)
12746 Mask.push_back(NumElts - 1 - i);
12747
12748 setValue(&I, DAG.getVectorShuffle(VT, DL, V, DAG.getUNDEF(VT), Mask));
12749}
12750
12751void SelectionDAGBuilder::visitVectorDeinterleave(const CallInst &I,
12752 unsigned Factor) {
12753 auto DL = getCurSDLoc();
12754 SDValue InVec = getValue(I.getOperand(0));
12755
12756 SmallVector<EVT, 4> ValueVTs;
12757 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
12758 ValueVTs);
12759
12760 EVT OutVT = ValueVTs[0];
12761 unsigned OutNumElts = OutVT.getVectorMinNumElements();
12762
12763 SmallVector<SDValue, 4> SubVecs(Factor);
12764 for (unsigned i = 0; i != Factor; ++i) {
12765 assert(ValueVTs[i] == OutVT && "Expected VTs to be the same");
12766 SubVecs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OutVT, InVec,
12767 DAG.getVectorIdxConstant(OutNumElts * i, DL));
12768 }
12769
12770 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12771 // from existing legalisation and combines.
12772 if (OutVT.isFixedLengthVector() && Factor == 2) {
12773 SDValue Even = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12774 createStrideMask(0, 2, OutNumElts));
12775 SDValue Odd = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12776 createStrideMask(1, 2, OutNumElts));
12777 SDValue Res = DAG.getMergeValues({Even, Odd}, getCurSDLoc());
12778 setValue(&I, Res);
12779 return;
12780 }
12781
12782 SDValue Res = DAG.getNode(ISD::VECTOR_DEINTERLEAVE, DL,
12783 DAG.getVTList(ValueVTs), SubVecs);
12784 setValue(&I, Res);
12785}
12786
12787void SelectionDAGBuilder::visitVectorInterleave(const CallInst &I,
12788 unsigned Factor) {
12789 auto DL = getCurSDLoc();
12790 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12791 EVT InVT = getValue(I.getOperand(0)).getValueType();
12792 EVT OutVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12793
12794 SmallVector<SDValue, 8> InVecs(Factor);
12795 for (unsigned i = 0; i < Factor; ++i) {
12796 InVecs[i] = getValue(I.getOperand(i));
12797 assert(InVecs[i].getValueType() == InVecs[0].getValueType() &&
12798 "Expected VTs to be the same");
12799 }
12800
12801 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12802 // from existing legalisation and combines.
12803 if (OutVT.isFixedLengthVector() && Factor == 2) {
12804 unsigned NumElts = InVT.getVectorMinNumElements();
12805 SDValue V = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, InVecs);
12806 setValue(&I, DAG.getVectorShuffle(OutVT, DL, V, DAG.getUNDEF(OutVT),
12807 createInterleaveMask(NumElts, 2)));
12808 return;
12809 }
12810
12811 SmallVector<EVT, 8> ValueVTs(Factor, InVT);
12812 SDValue Res =
12813 DAG.getNode(ISD::VECTOR_INTERLEAVE, DL, DAG.getVTList(ValueVTs), InVecs);
12814
12816 for (unsigned i = 0; i < Factor; ++i)
12817 Results[i] = Res.getValue(i);
12818
12819 Res = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, Results);
12820 setValue(&I, Res);
12821}
12822
12823void SelectionDAGBuilder::visitFreeze(const FreezeInst &I) {
12824 SmallVector<EVT, 4> ValueVTs;
12825 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
12826 ValueVTs);
12827 unsigned NumValues = ValueVTs.size();
12828 if (NumValues == 0) return;
12829
12830 SmallVector<SDValue, 4> Values(NumValues);
12831 SDValue Op = getValue(I.getOperand(0));
12832
12833 for (unsigned i = 0; i != NumValues; ++i)
12834 Values[i] = DAG.getNode(ISD::FREEZE, getCurSDLoc(), ValueVTs[i],
12835 SDValue(Op.getNode(), Op.getResNo() + i));
12836
12838 DAG.getVTList(ValueVTs), Values));
12839}
12840
12841void SelectionDAGBuilder::visitVectorSplice(const CallInst &I) {
12842 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12843 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12844
12845 SDLoc DL = getCurSDLoc();
12846 SDValue V1 = getValue(I.getOperand(0));
12847 SDValue V2 = getValue(I.getOperand(1));
12848 int64_t Imm = cast<ConstantInt>(I.getOperand(2))->getSExtValue();
12849
12850 // VECTOR_SHUFFLE doesn't support a scalable mask so use a dedicated node.
12851 if (VT.isScalableVector()) {
12852 setValue(
12853 &I, DAG.getNode(ISD::VECTOR_SPLICE, DL, VT, V1, V2,
12854 DAG.getSignedConstant(
12855 Imm, DL, TLI.getVectorIdxTy(DAG.getDataLayout()))));
12856 return;
12857 }
12858
12859 unsigned NumElts = VT.getVectorNumElements();
12860
12861 uint64_t Idx = (NumElts + Imm) % NumElts;
12862
12863 // Use VECTOR_SHUFFLE to maintain original behaviour for fixed-length vectors.
12864 SmallVector<int, 8> Mask;
12865 for (unsigned i = 0; i < NumElts; ++i)
12866 Mask.push_back(Idx + i);
12867 setValue(&I, DAG.getVectorShuffle(VT, DL, V1, V2, Mask));
12868}
12869
12870// Consider the following MIR after SelectionDAG, which produces output in
12871// phyregs in the first case or virtregs in the second case.
12872//
12873// INLINEASM_BR ..., implicit-def $ebx, ..., implicit-def $edx
12874// %5:gr32 = COPY $ebx
12875// %6:gr32 = COPY $edx
12876// %1:gr32 = COPY %6:gr32
12877// %0:gr32 = COPY %5:gr32
12878//
12879// INLINEASM_BR ..., def %5:gr32, ..., def %6:gr32
12880// %1:gr32 = COPY %6:gr32
12881// %0:gr32 = COPY %5:gr32
12882//
12883// Given %0, we'd like to return $ebx in the first case and %5 in the second.
12884// Given %1, we'd like to return $edx in the first case and %6 in the second.
12885//
12886// If a callbr has outputs, it will have a single mapping in FuncInfo.ValueMap
12887// to a single virtreg (such as %0). The remaining outputs monotonically
12888// increase in virtreg number from there. If a callbr has no outputs, then it
12889// should not have a corresponding callbr landingpad; in fact, the callbr
12890// landingpad would not even be able to refer to such a callbr.
12892 MachineInstr *MI = MRI.def_begin(Reg)->getParent();
12893 // There is definitely at least one copy.
12894 assert(MI->getOpcode() == TargetOpcode::COPY &&
12895 "start of copy chain MUST be COPY");
12896 Reg = MI->getOperand(1).getReg();
12897
12898 // If the copied register in the first copy must be virtual.
12899 assert(Reg.isVirtual() && "expected COPY of virtual register");
12900 MI = MRI.def_begin(Reg)->getParent();
12901
12902 // There may be an optional second copy.
12903 if (MI->getOpcode() == TargetOpcode::COPY) {
12904 assert(Reg.isVirtual() && "expected COPY of virtual register");
12905 Reg = MI->getOperand(1).getReg();
12906 assert(Reg.isPhysical() && "expected COPY of physical register");
12907 } else {
12908 // The start of the chain must be an INLINEASM_BR.
12909 assert(MI->getOpcode() == TargetOpcode::INLINEASM_BR &&
12910 "end of copy chain MUST be INLINEASM_BR");
12911 }
12912
12913 return Reg;
12914}
12915
12916// We must do this walk rather than the simpler
12917// setValue(&I, getCopyFromRegs(CBR, CBR->getType()));
12918// otherwise we will end up with copies of virtregs only valid along direct
12919// edges.
12920void SelectionDAGBuilder::visitCallBrLandingPad(const CallInst &I) {
12921 SmallVector<EVT, 8> ResultVTs;
12922 SmallVector<SDValue, 8> ResultValues;
12923 const auto *CBR =
12924 cast<CallBrInst>(I.getParent()->getUniquePredecessor()->getTerminator());
12925
12926 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12927 const TargetRegisterInfo *TRI = DAG.getSubtarget().getRegisterInfo();
12928 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
12929
12930 Register InitialDef = FuncInfo.ValueMap[CBR];
12931 SDValue Chain = DAG.getRoot();
12932
12933 // Re-parse the asm constraints string.
12934 TargetLowering::AsmOperandInfoVector TargetConstraints =
12935 TLI.ParseConstraints(DAG.getDataLayout(), TRI, *CBR);
12936 for (auto &T : TargetConstraints) {
12937 SDISelAsmOperandInfo OpInfo(T);
12938 if (OpInfo.Type != InlineAsm::isOutput)
12939 continue;
12940
12941 // Pencil in OpInfo.ConstraintType and OpInfo.ConstraintVT based on the
12942 // individual constraint.
12943 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
12944
12945 switch (OpInfo.ConstraintType) {
12948 // Fill in OpInfo.AssignedRegs.Regs.
12949 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, OpInfo);
12950
12951 // getRegistersForValue may produce 1 to many registers based on whether
12952 // the OpInfo.ConstraintVT is legal on the target or not.
12953 for (Register &Reg : OpInfo.AssignedRegs.Regs) {
12954 Register OriginalDef = FollowCopyChain(MRI, InitialDef++);
12955 if (OriginalDef.isPhysical())
12956 FuncInfo.MBB->addLiveIn(OriginalDef);
12957 // Update the assigned registers to use the original defs.
12958 Reg = OriginalDef;
12959 }
12960
12961 SDValue V = OpInfo.AssignedRegs.getCopyFromRegs(
12962 DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, CBR);
12963 ResultValues.push_back(V);
12964 ResultVTs.push_back(OpInfo.ConstraintVT);
12965 break;
12966 }
12968 SDValue Flag;
12969 SDValue V = TLI.LowerAsmOutputForConstraint(Chain, Flag, getCurSDLoc(),
12970 OpInfo, DAG);
12971 ++InitialDef;
12972 ResultValues.push_back(V);
12973 ResultVTs.push_back(OpInfo.ConstraintVT);
12974 break;
12975 }
12976 default:
12977 break;
12978 }
12979 }
12981 DAG.getVTList(ResultVTs), ResultValues);
12982 setValue(&I, V);
12983}
unsigned const MachineRegisterInfo * MRI
return SDValue()
static unsigned getIntrinsicID(const SDNode *N)
unsigned RegSize
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static msgpack::DocNode getNode(msgpack::DocNode DN, msgpack::Type Type, MCValue Val)
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
Function Alias Analysis Results
Atomic ordering constants.
This file contains the simple types necessary to represent the attributes associated with functions a...
static const Function * getParent(const Value *V)
This file implements the BitVector class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Analysis containing CSE Info
Definition CSEInfo.cpp:27
This file contains the declarations for the subclasses of Constant, which represent the different fla...
dxil translate DXIL Translate Metadata
static AttributeList getReturnAttrs(FastISel::CallLoweringInfo &CLI)
Returns an AttributeList representing the attributes applied to the return value of the given call.
Definition FastISel.cpp:942
#define Check(C,...)
static Value * getCondition(Instruction *I)
Hexagon Common GEP
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Module.h This file contains the declarations for the Module class.
static void getRegistersForValue(MachineFunction &MF, MachineIRBuilder &MIRBuilder, GISelAsmOperandInfo &OpInfo, GISelAsmOperandInfo &RefOpInfo)
Assign virtual/physical registers for the specified register operand.
This file defines an InstructionCost class that is used when calculating the cost of an instruction,...
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define RegName(no)
lazy value info
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Machine Check Debug Module
static bool isUndef(const MachineInstr &MI)
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static const Function * getCalledFunction(const Value *V)
This file provides utility analysis objects describing memory locations.
This file provides utility for Memory Model Relaxation Annotations (MMRAs).
This file contains the declarations for metadata subclasses.
Type::TypeID TypeID
#define T
#define T1
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static unsigned getAddressSpace(const Value *V, unsigned MaxLookup)
MachineInstr unsigned OpIdx
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t High
uint64_t IntrinsicInst * II
OptimizedStructLayoutField Field
#define P(N)
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
static Type * getValueType(Value *V)
Returns the type of the given value/instruction V.
This file contains some templates that are useful if you are working with the STL at all.
static bool hasOnlySelectUsers(const Value *Cond)
static SDValue getLoadStackGuard(SelectionDAG &DAG, const SDLoc &DL, SDValue &Chain)
Create a LOAD_STACK_GUARD node, and let it carry the target specific global variable if there exists ...
static bool getUniformBase(const Value *Ptr, SDValue &Base, SDValue &Index, SDValue &Scale, SelectionDAGBuilder *SDB, const BasicBlock *CurBB, uint64_t ElemSize)
static void failForInvalidBundles(const CallBase &I, StringRef Name, ArrayRef< uint32_t > AllowedBundles)
static void addStackMapLiveVars(const CallBase &Call, unsigned StartIdx, const SDLoc &DL, SmallVectorImpl< SDValue > &Ops, SelectionDAGBuilder &Builder)
Add a stack map intrinsic call's live variable operands to a stackmap or patchpoint target node's ope...
static const unsigned MaxParallelChains
static SDValue expandPow(const SDLoc &dl, SDValue LHS, SDValue RHS, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
visitPow - Lower a pow intrinsic.
static const CallBase * FindPreallocatedCall(const Value *PreallocatedSetup)
Given a @llvm.call.preallocated.setup, return the corresponding preallocated call.
static cl::opt< unsigned > SwitchPeelThreshold("switch-peel-threshold", cl::Hidden, cl::init(66), cl::desc("Set the case probability threshold for peeling the case from a " "switch statement. A value greater than 100 will void this " "optimization"))
static cl::opt< bool > InsertAssertAlign("insert-assert-align", cl::init(true), cl::desc("Insert the experimental `assertalign` node."), cl::ReallyHidden)
static unsigned getISDForVPIntrinsic(const VPIntrinsic &VPIntrin)
static bool handleDanglingVariadicDebugInfo(SelectionDAG &DAG, DILocalVariable *Variable, DebugLoc DL, unsigned Order, SmallVectorImpl< Value * > &Values, DIExpression *Expression)
static unsigned findMatchingInlineAsmOperand(unsigned OperandNo, const std::vector< SDValue > &AsmNodeOperands)
static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo, SDISelAsmOperandInfo &MatchingOpInfo, SelectionDAG &DAG)
Make sure that the output operand OpInfo and its corresponding input operand MatchingOpInfo have comp...
static void findUnwindDestinations(FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB, BranchProbability Prob, SmallVectorImpl< std::pair< MachineBasicBlock *, BranchProbability > > &UnwindDests)
When an invoke or a cleanupret unwinds to the next EH pad, there are many places it could ultimately ...
static unsigned FixedPointIntrinsicToOpcode(unsigned Intrinsic)
static BranchProbability scaleCaseProbality(BranchProbability CaseProb, BranchProbability PeeledCaseProb)
static SDValue expandExp2(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandExp2 - Lower an exp2 intrinsic.
static SDValue expandDivFix(unsigned Opcode, const SDLoc &DL, SDValue LHS, SDValue RHS, SDValue Scale, SelectionDAG &DAG, const TargetLowering &TLI)
static SDValue getF32Constant(SelectionDAG &DAG, unsigned Flt, const SDLoc &dl)
getF32Constant - Get 32-bit floating point constant.
static SDValue widenVectorToPartType(SelectionDAG &DAG, SDValue Val, const SDLoc &DL, EVT PartVT)
static SDValue expandLog10(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandLog10 - Lower a log10 intrinsic.
DenseMap< const Argument *, std::pair< const AllocaInst *, const StoreInst * > > ArgCopyElisionMapTy
static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &dl, SDValue Val, SDValue *Parts, unsigned NumParts, MVT PartVT, const Value *V, std::optional< CallingConv::ID > CallConv)
getCopyToPartsVector - Create a series of nodes that contain the specified value split into legal par...
static void getUnderlyingArgRegs(SmallVectorImpl< std::pair< Register, TypeSize > > &Regs, const SDValue &N)
static void getCopyToParts(SelectionDAG &DAG, const SDLoc &DL, SDValue Val, SDValue *Parts, unsigned NumParts, MVT PartVT, const Value *V, std::optional< CallingConv::ID > CallConv=std::nullopt, ISD::NodeType ExtendKind=ISD::ANY_EXTEND)
getCopyToParts - Create a series of nodes that contain the specified value split into legal parts.
static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT, SelectionDAGBuilder &Builder)
static SDValue expandLog2(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandLog2 - Lower a log2 intrinsic.
static SDValue getAddressForMemoryInput(SDValue Chain, const SDLoc &Location, SDISelAsmOperandInfo &OpInfo, SelectionDAG &DAG)
Get a direct memory input to behave well as an indirect operand.
static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel)
isOnlyUsedInEntryBlock - If the specified argument is only used in the entry block,...
static void diagnosePossiblyInvalidConstraint(LLVMContext &Ctx, const Value *V, const Twine &ErrMsg)
static bool collectInstructionDeps(SmallMapVector< const Instruction *, bool, 8 > *Deps, const Value *V, SmallMapVector< const Instruction *, bool, 8 > *Necessary=nullptr, unsigned Depth=0)
static void findArgumentCopyElisionCandidates(const DataLayout &DL, FunctionLoweringInfo *FuncInfo, ArgCopyElisionMapTy &ArgCopyElisionCandidates)
Scan the entry block of the function in FuncInfo for arguments that look like copies into a local all...
static bool isFunction(SDValue Op)
static SDValue GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI, const SDLoc &dl)
GetExponent - Get the exponent:
static Register FollowCopyChain(MachineRegisterInfo &MRI, Register Reg)
static SDValue ExpandPowI(const SDLoc &DL, SDValue LHS, SDValue RHS, SelectionDAG &DAG)
ExpandPowI - Expand a llvm.powi intrinsic.
static SDValue expandLog(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandLog - Lower a log intrinsic.
static SDValue getCopyFromParts(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts, MVT PartVT, EVT ValueVT, const Value *V, SDValue InChain, std::optional< CallingConv::ID > CC=std::nullopt, std::optional< ISD::NodeType > AssertOp=std::nullopt)
getCopyFromParts - Create a value that contains the specified legal parts combined into the value the...
static SDValue getLimitedPrecisionExp2(SDValue t0, const SDLoc &dl, SelectionDAG &DAG)
static SDValue GetSignificand(SelectionDAG &DAG, SDValue Op, const SDLoc &dl)
GetSignificand - Get the significand and build it into a floating-point number with exponent of 1:
static SDValue expandExp(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandExp - Lower an exp intrinsic.
static const MDNode * getRangeMetadata(const Instruction &I)
static cl::opt< unsigned, true > LimitFPPrecision("limit-float-precision", cl::desc("Generate low-precision inline sequences " "for some float libcalls"), cl::location(LimitFloatPrecision), cl::Hidden, cl::init(0))
static void tryToElideArgumentCopy(FunctionLoweringInfo &FuncInfo, SmallVectorImpl< SDValue > &Chains, DenseMap< int, int > &ArgCopyElisionFrameIndexMap, SmallPtrSetImpl< const Instruction * > &ElidedArgCopyInstrs, ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg, ArrayRef< SDValue > ArgVals, bool &ArgHasUses)
Try to elide argument copies from memory into a local alloca.
static unsigned LimitFloatPrecision
LimitFloatPrecision - Generate low-precision inline sequences for some float libcalls (6,...
static SDValue getCopyFromPartsVector(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts, MVT PartVT, EVT ValueVT, const Value *V, SDValue InChain, std::optional< CallingConv::ID > CC)
getCopyFromPartsVector - Create a value that contains the specified legal parts combined into the val...
static bool InBlock(const Value *V, const BasicBlock *BB)
static FPClassTest getNoFPClass(const Instruction &I)
static LLVM_ATTRIBUTE_ALWAYS_INLINE MVT::SimpleValueType getSimpleVT(const unsigned char *MatcherTable, unsigned &MatcherIndex)
getSimpleVT - Decode a value in MatcherTable, if it's a VBR encoded value, use GetVBR to decode it.
This file defines the SmallPtrSet class.
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:114
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
This pass exposes codegen information to IR-level passes.
Value * RHS
Value * LHS
static const fltSemantics & IEEEsingle()
Definition APFloat.h:296
Class for arbitrary precision integers.
Definition APInt.h:78
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:441
an instruction to allocate memory on the stack
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
LLVM_ABI bool hasAttribute(Attribute::AttrKind Kind) const
Check if an argument has a given attribute.
Definition Function.cpp:339
unsigned getArgNo() const
Return the index of this formal argument in its containing function.
Definition Argument.h:50
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:131
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
iterator begin() const
Definition ArrayRef.h:130
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
A cache of @llvm.assume calls within a function.
An instruction that atomically checks whether a specified value is in a memory location,...
an instruction that atomically reads a memory location, combines it with another value,...
@ Add
*p = old + v
@ FAdd
*p = old + v
@ USubCond
Subtract only if no unsigned overflow.
@ FMinimum
*p = minimum(old, v) minimum matches the behavior of llvm.minimum.
@ Min
*p = old <signed v ? old : v
@ Sub
*p = old - v
@ And
*p = old & v
@ Xor
*p = old ^ v
@ USubSat
*p = usub.sat(old, v) usub.sat matches the behavior of llvm.usub.sat.
@ FMaximum
*p = maximum(old, v) maximum matches the behavior of llvm.maximum.
@ FSub
*p = old - v
@ UIncWrap
Increment one up to a maximum value.
@ Max
*p = old >signed v ? old : v
@ UMin
*p = old <unsigned v ? old : v
@ FMin
*p = minnum(old, v) minnum matches the behavior of llvm.minnum.
@ UMax
*p = old >unsigned v ? old : v
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
@ UDecWrap
Decrement one until a minimum value or zero.
@ Nand
*p = ~(old & v)
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:361
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
LLVM_ABI bool isEntryBlock() const
Return true if this is the entry block of the containing function.
LLVM_ABI InstListType::const_iterator getFirstNonPHIOrDbg(bool SkipPseudoOp=true) const
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic,...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
This class represents a no-op cast from one type to another.
The address of a basic block.
Definition Constants.h:899
Conditional or Unconditional Branch instruction.
Analysis providing branch probability information.
LLVM_ABI BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
LLVM_ABI bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const
Test if an edge is hot relative to other out-edges of the Src.
static uint32_t getDenominator()
static BranchProbability getOne()
static BranchProbability getUnknown()
uint32_t getNumerator() const
LLVM_ABI uint64_t scale(uint64_t Num) const
Scale a large integer.
BranchProbability getCompl() const
static BranchProbability getZero()
static void normalizeProbabilities(ProbabilityIter Begin, ProbabilityIter End)
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
CallingConv::ID getCallingConv() const
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
LLVM_ABI bool isMustTailCall() const
Tests if this call site must be tail call optimized.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
unsigned countOperandBundlesOfType(StringRef Name) const
Return the number of operand bundles with the tag Name attached to this instruction.
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
bool isConvergent() const
Determine if the invoke is convergent.
FunctionType * getFunctionType() const
unsigned arg_size() const
AttributeList getAttributes() const
Return the attributes for this call.
LLVM_ABI bool isTailCall() const
Tests if this call site is marked as a tail call.
CallBr instruction, tracking function calls that may not return control but instead transfer it to a ...
This class represents a function call, abstracting a target machine's calling convention.
This class is the base class for the comparison instructions.
Definition InstrTypes.h:664
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition Constants.h:593
A constant value that is initialized with an expression using other constant values.
Definition Constants.h:1120
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:277
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition Constants.h:214
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:163
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:154
A signed pointer, in the ptrauth sense.
Definition Constants.h:1032
uint64_t getZExtValue() const
Constant Vector Declarations.
Definition Constants.h:517
This is an important base class in LLVM.
Definition Constant.h:43
This is the common base class for constrained floating point intrinsics.
LLVM_ABI std::optional< fp::ExceptionBehavior > getExceptionBehavior() const
LLVM_ABI unsigned getNonMetadataArgCount() const
DWARF expression.
LLVM_ABI bool isEntryValue() const
Check if the expression consists of exactly one entry value operand.
static bool fragmentsOverlap(const FragmentInfo &A, const FragmentInfo &B)
Check if fragments overlap between a pair of FragmentInfos.
static LLVM_ABI DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
static LLVM_ABI std::optional< FragmentInfo > getFragmentInfo(expr_op_iterator Start, expr_op_iterator End)
Retrieve the details of this fragment expression.
LLVM_ABI uint64_t getNumLocationOperands() const
Return the number of unique location operands referred to (via DW_OP_LLVM_arg) in this expression; th...
static LLVM_ABI std::optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
static LLVM_ABI const DIExpression * convertToUndefExpression(const DIExpression *Expr)
Removes all elements from Expr that do not apply to an undef debug value, which includes every operat...
static LLVM_ABI DIExpression * prepend(const DIExpression *Expr, uint8_t Flags, int64_t Offset=0)
Prepend DIExpr with a deref and offset operation and optionally turn it into a stack value or/and an ...
static LLVM_ABI DIExpression * prependOpcodes(const DIExpression *Expr, SmallVectorImpl< uint64_t > &Ops, bool StackValue=false, bool EntryValue=false)
Prepend DIExpr with the given opcodes and optionally turn it into a stack value.
Base class for variables.
LLVM_ABI std::optional< uint64_t > getSizeInBits() const
Determines the size of the variable's type.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
bool isBigEndian() const
Definition DataLayout.h:208
Records a position in IR for a source label (DILabel).
Base class for non-instruction debug metadata records that have positions within IR.
DebugLoc getDebugLoc() const
Record of a variable value-assignment, aka a non instruction representation of the dbg....
LLVM_ABI Value * getVariableLocationOp(unsigned OpIdx) const
DIExpression * getExpression() const
DILocalVariable * getVariable() const
LLVM_ABI iterator_range< location_op_iterator > location_ops() const
Get the locations corresponding to the variable referenced by the debug info intrinsic.
A debug info location.
Definition DebugLoc.h:124
LLVM_ABI DILocation * getInlinedAt() const
Definition DebugLoc.cpp:67
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition DenseMap.h:74
bool empty() const
Definition DenseMap.h:109
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:75
iterator end()
Definition DenseMap.h:81
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:233
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition DenseMap.h:114
Diagnostic information for inline asm reporting.
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition TypeSize.h:309
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:315
constexpr bool isScalar() const
Exactly one element.
Definition TypeSize.h:320
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Class representing an expression and its matching format.
This instruction extracts a struct member or array element value from an aggregate value.
This instruction compares its operands according to the predicate given to the constructor.
This is a fast-path instruction selection class that generates poor code and doesn't support illegal ...
Definition FastISel.h:66
bool allowReassoc() const
Flag queries.
Definition FMF.h:64
An instruction for ordering other memory operations.
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:802
This class represents a freeze function that returns random concrete value if an operand is either a ...
FunctionLoweringInfo - This contains information that is global to a function that is used when lower...
BranchProbabilityInfo * BPI
MachineBasicBlock * getMBB(const BasicBlock *BB) const
DenseMap< const AllocaInst *, int > StaticAllocaMap
StaticAllocaMap - Keep track of frame indices for fixed sized allocas in the entry block.
const LiveOutInfo * GetLiveOutRegInfo(Register Reg)
GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the register is a PHI destinat...
MachineBasicBlock * MBB
MBB - The current block.
Class to represent function types.
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Type * getParamType(unsigned i) const
Parameter type accessors.
Type * getReturnType() const
Data structure describing the variable locations in a function.
const BasicBlock & getEntryBlock() const
Definition Function.h:807
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition Function.h:209
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition Function.h:244
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:703
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const
check if an attributes is in the list of attributes.
Definition Function.cpp:742
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:270
Constant * getPersonalityFn() const
Get the personality function associated with this function.
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:352
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
Definition Function.h:249
size_t arg_size() const
Definition Function.h:899
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:730
Garbage collection metadata for a single function.
Definition GCMetadata.h:80
bool hasNoUnsignedSignedWrap() const
bool hasNoUnsignedWrap() const
bool isInBounds() const
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
bool hasDLLImportStorageClass() const
Module * getParent()
Get the module that this global value is contained inside of...
This instruction compares its operands according to the predicate given to the constructor.
Indirect Branch Instruction.
This instruction inserts a struct field of array element value into an aggregate value.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
LLVM_ABI FastMathFlags getFastMathFlags() const LLVM_READONLY
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
LLVM_ABI AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
@ MIN_INT_BITS
Minimum number of bits that can be specified.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Invoke instruction.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
The landingpad instruction holds all of the information necessary to generate correct exception handl...
A helper class to return the specified delimiter string after the first invocation of operator String...
An instruction for reading from memory.
static LocationSize precise(uint64_t Value)
static constexpr LocationSize beforeOrAfterPointer()
Any location before or after the base pointer (but still within the underlying object).
LLVM_ABI MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
LLVM_ABI MCSymbol * getOrCreateFrameAllocSymbol(const Twine &FuncName, unsigned Idx)
Gets a symbol that will be defined to the final stack offset of a local variable after codegen.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Metadata node.
Definition Metadata.h:1078
Machine Value Type.
@ INVALID_SIMPLE_VALUE_TYPE
uint64_t getScalarSizeInBits() const
unsigned getVectorNumElements() const
bool isVector() const
Return true if this is a vector value type.
bool isInteger() const
Return true if this is an integer or a vector integer type.
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
uint64_t getFixedSizeInBits() const
Return the size of the specified fixed width value type in bits.
ElementCount getVectorElementCount() const
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
bool bitsGE(MVT VT) const
Return true if this has no less bits than VT.
bool isScalarInteger() const
Return true if this is an integer, not including vectors.
static MVT getVectorVT(MVT VT, unsigned NumElements)
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
static MVT getIntegerVT(unsigned BitWidth)
void normalizeSuccProbs()
Normalize probabilities of all successors so that the sum of them becomes one.
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI void setSuccProbability(succ_iterator I, BranchProbability Prob)
Set successor probability of a given iterator.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
SmallVectorImpl< MachineBasicBlock * >::iterator succ_iterator
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void setIsEHContTarget(bool V=true)
Indicates if this is a target of Windows EH Continuation Guard.
void setIsEHFuncletEntry(bool V=true)
Indicates if this is the entry block of an EH funclet.
MachineInstrBundleIterator< MachineInstr > iterator
void setIsEHScopeEntry(bool V=true)
Indicates if this is the entry block of an EH scope, i.e., the block that that used to have a catchpa...
void setMachineBlockAddressTaken()
Set this block to indicate that its address is used as something other than the target of a terminato...
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
void setIsImmutableObjectIndex(int ObjectIdx, bool IsImmutable)
Marks the immutability of an object.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
bool hasOpaqueSPAdjustment() const
Returns true if the function contains opaque dynamic stack adjustments.
int getStackProtectorIndex() const
Return the index for the stack protector object.
void setIsAliasedObjectIndex(int ObjectIdx, bool IsAliased)
Set "maybe pointed to by an LLVM IR value" for an object.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
void RemoveStackObject(int ObjectIdx)
Remove or mark dead a statically sized stack object.
void setFunctionContextIndex(int I)
const WinEHFuncInfo * getWinEHFuncInfo() const
getWinEHFuncInfo - Return information about how the current function uses Windows exception handling.
bool useDebugInstrRef() const
Returns true if the function's variable locations are tracked with instruction referencing.
void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site)
Map the begin label for a call site.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MCContext & getContext() const
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void addCodeViewAnnotation(MCSymbol *Label, MDNode *MD)
Record annotations associated with a particular label.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
void setHasEHContTarget(bool V)
void addInvoke(MachineBasicBlock *LandingPad, MCSymbol *BeginLabel, MCSymbol *EndLabel)
Provide the begin and end labels of an invoke style call and associate it with a try landing pad bloc...
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const MachineInstrBuilder & addSym(MCSymbol *Sym, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addFrameIndex(int Idx) const
Representation of each machine instruction.
A description of a memory reference used in the backend.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MONonTemporal
The memory access is non-temporal.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
static MachineOperand CreateFI(int Idx)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI MCRegister getLiveInPhysReg(Register VReg) const
getLiveInPhysReg - If VReg is a live-in virtual register, return the corresponding live-in physical r...
An SDNode that represents everything that will be needed to construct a MachineInstr.
bool contains(const KeyT &Key) const
Definition MapVector.h:141
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
Definition MapVector.h:111
static MemoryLocation getAfter(const Value *Ptr, const AAMDNodes &AATags=AAMDNodes())
Return a location that may access any location after Ptr, while remaining within the underlying objec...
Metadata wrapper in the Value hierarchy.
Definition Metadata.h:183
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
Resume the propagation of an exception.
Return a value (possibly void), from a function.
Holds the information from a dbg_label node through SDISel.
static SDDbgOperand fromNode(SDNode *Node, unsigned ResNo)
static SDDbgOperand fromFrameIdx(unsigned FrameIdx)
static SDDbgOperand fromVReg(Register VReg)
static SDDbgOperand fromConst(const Value *Const)
Holds the information from a dbg_value node through SDISel.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
iterator_range< value_op_iterator > op_values() const
unsigned getIROrder() const
Return the node ordering.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
const SDValue & getOperand(unsigned Num) const
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
unsigned getResNo() const
get the index which selects a specific result in the SDNode
MVT getSimpleValueType() const
Return the simple ValueType of the referenced return value.
unsigned getOpcode() const
SelectionDAGBuilder - This is the common target-independent lowering implementation that is parameter...
SDValue getValue(const Value *V)
getValue - Return an SDValue for the given Value.
DenseMap< const Constant *, Register > ConstantsOut
void addDanglingDebugInfo(SmallVectorImpl< Value * > &Values, DILocalVariable *Var, DIExpression *Expr, bool IsVariadic, DebugLoc DL, unsigned Order)
Register a dbg_value which relies on a Value which we have not yet seen.
void visitDbgInfo(const Instruction &I)
void clearDanglingDebugInfo()
Clear the dangling debug information map.
void LowerCallTo(const CallBase &CB, SDValue Callee, bool IsTailCall, bool IsMustTailCall, const BasicBlock *EHPadBB=nullptr, const TargetLowering::PtrAuthInfo *PAI=nullptr)
void clear()
Clear out the current SelectionDAG and the associated state and prepare this SelectionDAGBuilder obje...
void visitBitTestHeader(SwitchCG::BitTestBlock &B, MachineBasicBlock *SwitchBB)
visitBitTestHeader - This function emits necessary code to produce value suitable for "bit tests"
void LowerStatepoint(const GCStatepointInst &I, const BasicBlock *EHPadBB=nullptr)
std::unique_ptr< SDAGSwitchLowering > SL
SDValue lowerRangeToAssertZExt(SelectionDAG &DAG, const Instruction &I, SDValue Op)
bool HasTailCall
This is set to true if a call in the current block has been translated as a tail call.
bool ShouldEmitAsBranches(const std::vector< SwitchCG::CaseBlock > &Cases)
If the set of cases should be emitted as a series of branches, return true.
void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB, MachineBasicBlock *FBB, MachineBasicBlock *CurBB, MachineBasicBlock *SwitchBB, BranchProbability TProb, BranchProbability FProb, bool InvertCond)
EmitBranchForMergedCondition - Helper method for FindMergedConditions.
void LowerDeoptimizeCall(const CallInst *CI)
void LowerCallSiteWithDeoptBundle(const CallBase *Call, SDValue Callee, const BasicBlock *EHPadBB)
SwiftErrorValueTracking & SwiftError
Information about the swifterror values used throughout the function.
SDValue getNonRegisterValue(const Value *V)
getNonRegisterValue - Return an SDValue for the given Value, but don't look in FuncInfo....
const TargetTransformInfo * TTI
DenseMap< MachineBasicBlock *, SmallVector< unsigned, 4 > > LPadToCallSiteMap
Map a landing pad to the call site indexes.
SDValue lowerNoFPClassToAssertNoFPClass(SelectionDAG &DAG, const Instruction &I, SDValue Op)
void handleDebugDeclare(Value *Address, DILocalVariable *Variable, DIExpression *Expression, DebugLoc DL)
bool shouldKeepJumpConditionsTogether(const FunctionLoweringInfo &FuncInfo, const BranchInst &I, Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs, TargetLoweringBase::CondMergingParams Params) const
StatepointLoweringState StatepointLowering
State used while lowering a statepoint sequence (gc_statepoint, gc_relocate, and gc_result).
void visitBitTestCase(SwitchCG::BitTestBlock &BB, MachineBasicBlock *NextMBB, BranchProbability BranchProbToNext, Register Reg, SwitchCG::BitTestCase &B, MachineBasicBlock *SwitchBB)
visitBitTestCase - this function produces one "bit test"
bool canTailCall(const CallBase &CB) const
void populateCallLoweringInfo(TargetLowering::CallLoweringInfo &CLI, const CallBase *Call, unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy, AttributeSet RetAttrs, bool IsPatchPoint)
Populate a CallLowerinInfo (into CLI) based on the properties of the call being lowered.
void CopyValueToVirtualRegister(const Value *V, Register Reg, ISD::NodeType ExtendType=ISD::ANY_EXTEND)
void salvageUnresolvedDbgValue(const Value *V, DanglingDebugInfo &DDI)
For the given dangling debuginfo record, perform last-ditch efforts to resolve the debuginfo to somet...
SmallVector< SDValue, 8 > PendingLoads
Loads are not emitted to the program immediately.
GCFunctionInfo * GFI
Garbage collection metadata for the function.
void init(GCFunctionInfo *gfi, BatchAAResults *BatchAA, AssumptionCache *AC, const TargetLibraryInfo *li, const TargetTransformInfo &TTI)
SDValue getRoot()
Similar to getMemoryRoot, but also flushes PendingConstrainedFP(Strict) items.
void ExportFromCurrentBlock(const Value *V)
ExportFromCurrentBlock - If this condition isn't known to be exported from the current basic block,...
void resolveOrClearDbgInfo()
Evict any dangling debug information, attempting to salvage it first.
std::pair< SDValue, SDValue > lowerInvokable(TargetLowering::CallLoweringInfo &CLI, const BasicBlock *EHPadBB=nullptr)
SDValue getMemoryRoot()
Return the current virtual root of the Selection DAG, flushing any PendingLoad items.
void resolveDanglingDebugInfo(const Value *V, SDValue Val)
If we saw an earlier dbg_value referring to V, generate the debug data structures now that we've seen...
void visit(const Instruction &I)
void dropDanglingDebugInfo(const DILocalVariable *Variable, const DIExpression *Expr)
If we have dangling debug info that describes Variable, or an overlapping part of variable considerin...
SDValue getCopyFromRegs(const Value *V, Type *Ty)
If there was virtual register allocated for the value V emit CopyFromReg of the specified type Ty.
void CopyToExportRegsIfNeeded(const Value *V)
CopyToExportRegsIfNeeded - If the given value has virtual registers created for it,...
void handleKillDebugValue(DILocalVariable *Var, DIExpression *Expr, DebugLoc DbgLoc, unsigned Order)
Create a record for a kill location debug intrinsic.
void visitJumpTable(SwitchCG::JumpTable &JT)
visitJumpTable - Emit JumpTable node in the current MBB
SDValue getFPOperationRoot(fp::ExceptionBehavior EB)
Return the current virtual root of the Selection DAG, flushing PendingConstrainedFP or PendingConstra...
void visitJumpTableHeader(SwitchCG::JumpTable &JT, SwitchCG::JumpTableHeader &JTH, MachineBasicBlock *SwitchBB)
visitJumpTableHeader - This function emits necessary code to produce index in the JumpTable from swit...
void LowerCallSiteWithPtrAuthBundle(const CallBase &CB, const BasicBlock *EHPadBB)
static const unsigned LowestSDNodeOrder
Lowest valid SDNodeOrder.
FunctionLoweringInfo & FuncInfo
Information about the function as a whole.
void setValue(const Value *V, SDValue NewN)
void FindMergedConditions(const Value *Cond, MachineBasicBlock *TBB, MachineBasicBlock *FBB, MachineBasicBlock *CurBB, MachineBasicBlock *SwitchBB, Instruction::BinaryOps Opc, BranchProbability TProb, BranchProbability FProb, bool InvertCond)
const TargetLibraryInfo * LibInfo
bool isExportableFromCurrentBlock(const Value *V, const BasicBlock *FromBB)
void visitSPDescriptorParent(StackProtectorDescriptor &SPD, MachineBasicBlock *ParentBB)
Codegen a new tail for a stack protector check ParentMBB which has had its tail spliced into a stack ...
bool handleDebugValue(ArrayRef< const Value * > Values, DILocalVariable *Var, DIExpression *Expr, DebugLoc DbgLoc, unsigned Order, bool IsVariadic)
For a given list of Values, attempt to create and record a SDDbgValue in the SelectionDAG.
SDValue getControlRoot()
Similar to getRoot, but instead of flushing all the PendingLoad items, flush all the PendingExports (...
void UpdateSplitBlock(MachineBasicBlock *First, MachineBasicBlock *Last)
When an MBB was split during scheduling, update the references that need to refer to the last resulti...
SDValue getValueImpl(const Value *V)
getValueImpl - Helper function for getValue and getNonRegisterValue.
void visitSwitchCase(SwitchCG::CaseBlock &CB, MachineBasicBlock *SwitchBB)
visitSwitchCase - Emits the necessary code to represent a single node in the binary search tree resul...
void visitSPDescriptorFailure(StackProtectorDescriptor &SPD)
Codegen the failure basic block for a stack protector check.
std::unique_ptr< FunctionLoweringInfo > FuncInfo
SmallPtrSet< const Instruction *, 4 > ElidedArgCopyInstrs
const TargetLowering * TLI
MachineRegisterInfo * RegInfo
std::unique_ptr< SwiftErrorValueTracking > SwiftError
virtual void emitFunctionEntryCode()
std::unique_ptr< SelectionDAGBuilder > SDB
virtual std::pair< SDValue, SDValue > EmitTargetCodeForStrnlen(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src, SDValue MaxLength, MachinePointerInfo SrcPtrInfo) const
virtual std::pair< SDValue, SDValue > EmitTargetCodeForStrlen(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src, const CallInst *CI) const
virtual std::pair< SDValue, SDValue > EmitTargetCodeForStrcpy(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Dest, SDValue Src, MachinePointerInfo DestPtrInfo, MachinePointerInfo SrcPtrInfo, bool isStpcpy) const
Emit target-specific code that performs a strcpy or stpcpy, in cases where that is faster than a libc...
virtual std::pair< SDValue, SDValue > EmitTargetCodeForMemchr(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Src, SDValue Char, SDValue Length, MachinePointerInfo SrcPtrInfo) const
Emit target-specific code that performs a memchr, in cases where that is faster than a libcall.
virtual std::pair< SDValue, SDValue > EmitTargetCodeForMemcmp(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, const CallInst *CI) const
Emit target-specific code that performs a memcmp/bcmp, in cases where that is faster than a libcall.
virtual std::pair< SDValue, SDValue > EmitTargetCodeForStrcmp(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, MachinePointerInfo Op1PtrInfo, MachinePointerInfo Op2PtrInfo) const
Emit target-specific code that performs a strcmp, in cases where that is faster than a libcall.
virtual SDValue EmitTargetCodeForSetTag(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Addr, SDValue Size, MachinePointerInfo DstPtrInfo, bool ZeroData) const
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
SDValue getExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT, unsigned Opcode)
Convert Op, which must be of integer type, to the integer type VT, by either any/sign/zero-extending ...
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
const TargetSubtargetInfo & getSubtarget() const
SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, Register Reg, SDValue N)
LLVM_ABI SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
LLVM_ABI SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
LLVM_ABI SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL)
LLVM_ABI MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
LLVM_ABI void ExtractVectorElements(SDValue Op, SmallVectorImpl< SDValue > &Args, unsigned Start=0, unsigned Count=0, EVT EltVT=EVT())
Append the extracted elements from Start to Count out of the vector Op in Args.
LLVM_ABI SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
LLVM_ABI Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
LLVM_ABI bool shouldOptForSize() const
const TargetLowering & getTargetLoweringInfo() const
static constexpr unsigned MaxRecursionDepth
LLVM_ABI void AddDbgValue(SDDbgValue *DB, bool isParameter)
Add a dbg_value SDNode.
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
LLVM_ABI SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
LLVM_ABI SDDbgValue * getDbgValueList(DIVariable *Var, DIExpression *Expr, ArrayRef< SDDbgOperand > Locs, ArrayRef< SDNode * > Dependencies, bool IsIndirect, const DebugLoc &DL, unsigned O, bool IsVariadic)
Creates a SDDbgValue node from a list of locations.
SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, Register Reg, EVT VT)
LLVM_ABI void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
const DataLayout & getDataLayout() const
SDValue getTargetFrameIndex(int FI, EVT VT)
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
LLVM_ABI SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
LLVM_ABI SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getBasicBlock(MachineBasicBlock *MBB)
LLVM_ABI SDValue getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either truncating it or perform...
LLVM_ABI SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either any-extending or truncat...
LLVM_ABI SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI SDValue getValueType(EVT)
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of float type, to the float type VT, by either extending or rounding (by tr...
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
LLVM_ABI SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
MachineFunction & getMachineFunction() const
LLVM_ABI SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
LLVM_ABI SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
LLVMContext * getContext() const
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void swap(SmallVectorImpl &RHS)
void resize(size_type N)
void push_back(const T &Elt)
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Encapsulates all of the information needed to generate a stack protector check, and signals to isel w...
MachineBasicBlock * getSuccessMBB()
MachineBasicBlock * getFailureMBB()
MachineBasicBlock * getParentMBB()
bool shouldEmitFunctionBasedCheckStackProtector() const
An instruction for storing to memory.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:140
Multiway switch.
Information about stack frame layout on the target.
virtual TargetStackID::Value getStackIDForScalableVectors() const
Returns the StackID that scalable vectors should be associated with.
Provides information about what library functions are available for the current target.
virtual Align getByValTypeAlignment(Type *Ty, const DataLayout &DL) const
Returns the desired alignment for ByVal or InAlloca aggregate function arguments in the caller parame...
virtual bool isFMAFasterThanFMulAndFAdd(const MachineFunction &MF, EVT) const
Return true if an FMA operation is faster than a pair of fmul and fadd instructions.
EVT getMemValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
EVT getValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Return the EVT corresponding to this LLVM type.
LegalizeAction
This enum indicates whether operations are valid for a target, and if not, what action should be used...
virtual bool useStackGuardXorFP() const
If this function returns true, stack protection checks should XOR the frame pointer (or whichever poi...
virtual const TargetRegisterClass * getRegClassFor(MVT VT, bool isDivergent=false) const
Return the register class that should be used for the specified value type.
virtual bool isLegalScaleForGatherScatter(uint64_t Scale, uint64_t ElemSize) const
virtual bool isSExtCheaperThanZExt(EVT FromTy, EVT ToTy) const
Return true if sign-extension from FromTy to ToTy is cheaper than zero-extension.
MVT getVectorIdxTy(const DataLayout &DL) const
Returns the type to be used for the index operand of: ISD::INSERT_VECTOR_ELT, ISD::EXTRACT_VECTOR_ELT...
virtual unsigned getNumRegistersForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const
Certain targets require unusual breakdowns of certain types.
virtual bool isZExtFree(Type *FromTy, Type *ToTy) const
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
virtual MVT getRegisterTypeForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const
Certain combinations of ABIs, Targets and features require that types are legal for some operations a...
virtual Value * getSDagStackGuard(const Module &M) const
Return the variable that's previously inserted by insertSSPDeclarations, if any, otherwise return nul...
virtual unsigned getNumRegisters(LLVMContext &Context, EVT VT, std::optional< MVT > RegisterVT=std::nullopt) const
Return the number of registers that this ValueType will eventually require.
unsigned getBitWidthForCttzElements(Type *RetTy, ElementCount EC, bool ZeroIsPoison, const ConstantRange *VScaleRange) const
Return the minimum number of bits required to hold the maximum possible number of trailing zero vecto...
virtual bool shouldExtendGSIndex(EVT VT, EVT &EltTy) const
Returns true if the index type for a masked gather/scatter requires extending.
virtual unsigned getVectorTypeBreakdownForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const
Certain targets such as MIPS require that some types such as vectors are always broken down into scal...
Function * getSSPStackGuardCheck(const Module &M) const
If the target has a standard stack protection check function that performs validation and error handl...
Register getStackPointerRegisterToSaveRestore() const
If a physical register, this specifies the register that llvm.savestack/llvm.restorestack should save...
LegalizeAction getFixedPointOperationAction(unsigned Op, EVT VT, unsigned Scale) const
Some fixed point operations may be natively supported by the target but only for specific scales.
MachineMemOperand::Flags getAtomicMemOperandFlags(const Instruction &AI, const DataLayout &DL) const
virtual bool getTgtMemIntrinsic(IntrinsicInfo &, const CallInst &, MachineFunction &, unsigned) const
Given an intrinsic, checks if on the target the intrinsic will need to map to a MemIntrinsicNode (tou...
virtual bool allowsMisalignedMemoryAccesses(EVT, unsigned AddrSpace=0, Align Alignment=Align(1), MachineMemOperand::Flags Flags=MachineMemOperand::MONone, unsigned *=nullptr) const
Determine if the target supports unaligned memory accesses.
bool isOperationCustom(unsigned Op, EVT VT) const
Return true if the operation uses custom lowering, regardless of whether the type is legal or not.
bool hasBigEndianPartOrdering(EVT VT, const DataLayout &DL) const
When splitting a value of the specified type into parts, does the Lo or Hi part come first?
EVT getShiftAmountTy(EVT LHSTy, const DataLayout &DL) const
Returns the type for the shift amount of a shift opcode.
virtual Align getABIAlignmentForCallingConv(Type *ArgTy, const DataLayout &DL) const
Certain targets have context sensitive alignment requirements, where one type has the alignment requi...
MachineMemOperand::Flags getVPIntrinsicMemOperandFlags(const VPIntrinsic &VPIntrin) const
virtual bool shouldExpandGetActiveLaneMask(EVT VT, EVT OpVT) const
Return true if the @llvm.get.active.lane.mask intrinsic should be expanded using generic code in Sele...
virtual EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const
Return the ValueType of the result of SETCC operations.
MachineMemOperand::Flags getLoadMemOperandFlags(const LoadInst &LI, const DataLayout &DL, AssumptionCache *AC=nullptr, const TargetLibraryInfo *LibInfo=nullptr) const
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
MVT getProgramPointerTy(const DataLayout &DL) const
Return the type for code pointers, which is determined by the program address space specified through...
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
bool isOperationLegal(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target.
virtual bool shouldExpandVectorMatch(EVT VT, unsigned SearchSize) const
Return true if the @llvm.experimental.vector.match intrinsic should be expanded for vector type ‘VT’ ...
virtual MVT getFenceOperandTy(const DataLayout &DL) const
Return the type for operands of fence.
virtual bool shouldExpandGetVectorLength(EVT CountVT, unsigned VF, bool IsScalable) const
bool isOperationLegalOrCustom(unsigned Op, EVT VT, bool LegalOnly=false) const
Return true if the specified operation is legal on this target or can be made legal with custom lower...
virtual MVT hasFastEqualityCompare(unsigned NumBits) const
Return the preferred operand type if the target has a quick way to compare integer values of the give...
MachineMemOperand::Flags getStoreMemOperandFlags(const StoreInst &SI, const DataLayout &DL) const
virtual bool shouldExpandCttzElements(EVT VT) const
Return true if the @llvm.experimental.cttz.elts intrinsic should be expanded using generic code in Se...
virtual bool signExtendConstant(const ConstantInt *C) const
Return true if this constant should be sign extended when promoting to a larger type.
LegalizeTypeAction getTypeAction(LLVMContext &Context, EVT VT) const
Return how we should legalize values of this type, either it is already legal (return 'Legal') or we ...
virtual Register getExceptionPointerRegister(const Constant *PersonalityFn) const
If a physical register, this returns the register that receives the exception address on entry to an ...
bool supportsUnalignedAtomics() const
Whether the target supports unaligned atomic operations.
std::vector< ArgListEntry > ArgListTy
bool isBeneficialToExpandPowI(int64_t Exponent, bool OptForSize) const
Return true if it is beneficial to expand an @llvm.powi.
MVT getFrameIndexTy(const DataLayout &DL) const
Return the type for frame index, which is determined by the alloca address space specified through th...
virtual Register getExceptionSelectorRegister(const Constant *PersonalityFn) const
If a physical register, this returns the register that receives the exception typeid on entry to a la...
virtual MVT getPointerMemTy(const DataLayout &DL, uint32_t AS=0) const
Return the in-memory pointer type for the given address space, defaults to the pointer type from the ...
MVT getRegisterType(MVT VT) const
Return the type of registers that this ValueType will eventually require.
unsigned getVectorTypeBreakdown(LLVMContext &Context, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const
Vector types are broken down into some number of legal first class types.
virtual MVT getVPExplicitVectorLengthTy() const
Returns the type to be used for the EVL/AVL operand of VP nodes: ISD::VP_ADD, ISD::VP_SUB,...
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual bool supportKCFIBundles() const
Return true if the target supports kcfi operand bundles.
virtual bool supportPtrAuthBundles() const
Return true if the target supports ptrauth operand bundles.
virtual bool supportSwiftError() const
Return true if the target supports swifterror attribute.
virtual SDValue visitMaskedLoad(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, MachineMemOperand *MMO, SDValue &NewLoad, SDValue Ptr, SDValue PassThru, SDValue Mask) const
virtual SDValue emitStackGuardXorFP(SelectionDAG &DAG, SDValue Val, const SDLoc &DL) const
virtual EVT getTypeForExtReturn(LLVMContext &Context, EVT VT, ISD::NodeType) const
Return the type that should be used to zero or sign extend a zeroext/signext integer return value.
std::pair< SDValue, SDValue > makeLibCall(SelectionDAG &DAG, RTLIB::Libcall LC, EVT RetVT, ArrayRef< SDValue > Ops, MakeLibCallOptions CallOptions, const SDLoc &dl, SDValue Chain=SDValue()) const
Returns a pair of (return value, chain).
virtual InlineAsm::ConstraintCode getInlineAsmMemConstraint(StringRef ConstraintCode) const
std::vector< AsmOperandInfo > AsmOperandInfoVector
SDValue expandIS_FPCLASS(EVT ResultVT, SDValue Op, FPClassTest Test, SDNodeFlags Flags, const SDLoc &DL, SelectionDAG &DAG) const
Expand check for floating point class.
virtual SDValue prepareVolatileOrAtomicLoad(SDValue Chain, const SDLoc &DL, SelectionDAG &DAG) const
This callback is used to prepare for a volatile or atomic load.
virtual ConstraintType getConstraintType(StringRef Constraint) const
Given a constraint, return the type of constraint it is for this target.
virtual bool splitValueIntoRegisterParts(SelectionDAG &DAG, const SDLoc &DL, SDValue Val, SDValue *Parts, unsigned NumParts, MVT PartVT, std::optional< CallingConv::ID > CC) const
Target-specific splitting of values into parts that fit a register storing a legal type.
virtual SDValue joinRegisterPartsIntoValue(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts, MVT PartVT, EVT ValueVT, std::optional< CallingConv::ID > CC) const
Target-specific combining of register parts into its original value.
virtual SDValue LowerCall(CallLoweringInfo &, SmallVectorImpl< SDValue > &) const
This hook must be implemented to lower calls into the specified DAG.
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
virtual SDValue LowerAsmOutputForConstraint(SDValue &Chain, SDValue &Glue, const SDLoc &DL, const AsmOperandInfo &OpInfo, SelectionDAG &DAG) const
virtual std::pair< unsigned, const TargetRegisterClass * > getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const
Given a physical register constraint (e.g.
virtual AsmOperandInfoVector ParseConstraints(const DataLayout &DL, const TargetRegisterInfo *TRI, const CallBase &Call) const
Split up the constraint string from the inline assembly value into the specific constraints and their...
virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const
This callback is invoked for operations that are unsupported by the target, which are registered to u...
virtual bool functionArgumentNeedsConsecutiveRegisters(Type *Ty, CallingConv::ID CallConv, bool isVarArg, const DataLayout &DL) const
For some targets, an LLVM struct type must be broken down into multiple simple types,...
virtual void ComputeConstraintToUse(AsmOperandInfo &OpInfo, SDValue Op, SelectionDAG *DAG=nullptr) const
Determines the constraint code and constraint type to use for the specific AsmOperandInfo,...
virtual void CollectTargetIntrinsicOperands(const CallInst &I, SmallVectorImpl< SDValue > &Ops, SelectionDAG &DAG) const
virtual SDValue visitMaskedStore(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, MachineMemOperand *MMO, SDValue Ptr, SDValue Val, SDValue Mask) const
virtual bool useLoadStackGuardNode(const Module &M) const
If this function returns true, SelectionDAGBuilder emits a LOAD_STACK_GUARD node when it is lowering ...
virtual void LowerAsmOperandForConstraint(SDValue Op, StringRef Constraint, std::vector< SDValue > &Ops, SelectionDAG &DAG) const
Lower the specified operand into the Ops vector.
virtual void LowerOperationWrapper(SDNode *N, SmallVectorImpl< SDValue > &Results, SelectionDAG &DAG) const
This callback is invoked by the type legalizer to legalize nodes with an illegal operand type but leg...
virtual bool isInlineAsmTargetBranch(const SmallVectorImpl< StringRef > &AsmStrs, unsigned OpNo) const
On x86, return true if the operand with index OpNo is a CALL or JUMP instruction, which can use eithe...
virtual MVT getJumpTableRegTy(const DataLayout &DL) const
virtual bool CanLowerReturn(CallingConv::ID, MachineFunction &, bool, const SmallVectorImpl< ISD::OutputArg > &, LLVMContext &, const Type *RetTy) const
This hook should be implemented to check whether the return values described by the Outs array can fi...
CodeGenOptLevel getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
unsigned NoTrapAfterNoreturn
Do not emit a trap instruction for 'unreachable' IR instructions behind noreturn calls,...
unsigned TrapUnreachable
Emit target-specific trap instruction for 'unreachable' IR instructions.
unsigned getID() const
Return the register class ID number.
iterator begin() const
begin/end - Return all of the registers in this class.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
@ TCK_Latency
The latency of instruction.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:343
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
LLVM_ABI bool isEmptyTy() const
Return true if this type is empty, that is, it has no elements or all of its elements are empty.
Definition Type.cpp:180
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:267
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:280
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:352
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:128
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:293
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:240
bool isTokenTy() const
Return true if this is 'token'.
Definition Type.h:234
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:300
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:225
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:139
This function has undefined behavior.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
op_iterator op_begin()
Definition User.h:284
Value * getOperand(unsigned i) const
Definition User.h:232
unsigned getNumOperands() const
Definition User.h:254
op_iterator op_end()
Definition User.h:286
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
LLVM_ABI CmpInst::Predicate getPredicate() const
This is the common base class for vector predication intrinsics.
static LLVM_ABI std::optional< unsigned > getVectorLengthParamPos(Intrinsic::ID IntrinsicID)
LLVM_ABI MaybeAlign getPointerAlignment() const
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition Value.cpp:701
bool use_empty() const
Definition Value.h:346
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.cpp:1099
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
Base class of all SIMD vector types.
Type * getElementType() const
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:230
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
const ParentTy * getParent() const
Definition ilist_node.h:34
A raw_ostream that writes to an std::string.
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ AnyReg
OBSOLETED - Used for stack based JavaScript calls.
Definition CallingConv.h:60
@ AMDGPU_CS_Chain
Used on AMDGPUs to give the middle-end more control over argument placement.
@ X86_VectorCall
MSVC calling convention that passes vectors and vector aggregates in SSE registers.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:256
@ CTLZ_ZERO_UNDEF
Definition ISDOpcodes.h:780
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:504
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition ISDOpcodes.h:45
@ LOOP_DEPENDENCE_RAW_MASK
@ EH_SJLJ_LONGJMP
OUTCHAIN = EH_SJLJ_LONGJMP(INCHAIN, buffer) This corresponds to the eh.sjlj.longjmp intrinsic.
Definition ISDOpcodes.h:163
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h:593
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:771
@ SMULFIX
RESULT = [US]MULFIX(LHS, RHS, SCALE) - Perform fixed point multiplication on 2 integers with the same...
Definition ISDOpcodes.h:387
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:259
@ SMULFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:393
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:841
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:511
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:215
@ EH_SJLJ_SETUP_DISPATCH
OUTCHAIN = EH_SJLJ_SETUP_DISPATCH(INCHAIN) The target initializes the dispatch table here.
Definition ISDOpcodes.h:167
@ GlobalAddress
Definition ISDOpcodes.h:88
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:868
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:577
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:410
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:744
@ FMULADD
FMULADD - Performs a * b + c, with, or without, intermediate rounding.
Definition ISDOpcodes.h:521
@ FPTRUNC_ROUND
FPTRUNC_ROUND - This corresponds to the fptrunc_round intrinsic.
Definition ISDOpcodes.h:508
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:249
@ SDIVFIX
RESULT = [US]DIVFIX(LHS, RHS, SCALE) - Perform fixed point division on 2 integers with the same width...
Definition ISDOpcodes.h:400
@ EH_RETURN
OUTCHAIN = EH_RETURN(INCHAIN, OFFSET, HANDLER) - This node represents 'eh_return' gcc dwarf builtin,...
Definition ISDOpcodes.h:151
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:832
@ ADDROFRETURNADDR
ADDROFRETURNADDR - Represents the llvm.addressofreturnaddress intrinsic.
Definition ISDOpcodes.h:117
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition ISDOpcodes.h:779
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:347
@ VECTOR_INTERLEAVE
VECTOR_INTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor to...
Definition ISDOpcodes.h:628
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:534
@ IS_FPCLASS
Performs a check of floating point class property, defined by IEEE-754.
Definition ISDOpcodes.h:541
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:369
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:784
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:242
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:669
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition ISDOpcodes.h:225
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:343
@ GET_ROUNDING
Returns current rounding mode: -1 Undefined 0 Round to 0 1 Round to nearest, ties to even 2 Round to ...
Definition ISDOpcodes.h:958
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:762
@ AssertNoFPClass
AssertNoFPClass - These nodes record if a register contains a float value that is known to be not som...
Definition ISDOpcodes.h:78
@ PtrAuthGlobalAddress
A ptrauth constant.
Definition ISDOpcodes.h:100
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:607
@ EntryToken
EntryToken - This is the marker used to indicate the start of a region.
Definition ISDOpcodes.h:48
@ READ_REGISTER
READ_REGISTER, WRITE_REGISTER - This node represents llvm.register on the DAG, which implements the n...
Definition ISDOpcodes.h:134
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:569
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:838
@ LOCAL_RECOVER
LOCAL_RECOVER - Represents the llvm.localrecover intrinsic.
Definition ISDOpcodes.h:130
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:379
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:351
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:724
@ VECTOR_REVERSE
VECTOR_REVERSE(VECTOR) - Returns a vector, of the same type as VECTOR, whose elements are shuffled us...
Definition ISDOpcodes.h:633
@ SDIVFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:406
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:793
@ EH_DWARF_CFA
EH_DWARF_CFA - This node represents the pointer to the DWARF Canonical Frame Address (CFA),...
Definition ISDOpcodes.h:145
@ FRAMEADDR
FRAMEADDR, RETURNADDR - These nodes represent llvm.frameaddress and llvm.returnaddress on the DAG.
Definition ISDOpcodes.h:110
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:493
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:914
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:736
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:200
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition ISDOpcodes.h:732
@ STRICT_FADD
Constrained versions of the binary floating point operators.
Definition ISDOpcodes.h:420
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition ISDOpcodes.h:236
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:558
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ VECTOR_SPLICE
VECTOR_SPLICE(VEC1, VEC2, IMM) - Returns a subvector of the same type as VEC1/VEC2 from CONCAT_VECTOR...
Definition ISDOpcodes.h:654
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:947
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:696
@ SPONENTRY
SPONENTRY - Represents the llvm.sponentry intrinsic.
Definition ISDOpcodes.h:122
@ FP_TO_SINT_SAT
FP_TO_[US]INT_SAT - Convert floating point value in operand 0 to a signed or unsigned scalar integer ...
Definition ISDOpcodes.h:933
@ EH_SJLJ_SETJMP
RESULT, OUTCHAIN = EH_SJLJ_SETJMP(INCHAIN, buffer) This corresponds to the eh.sjlj....
Definition ISDOpcodes.h:157
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:844
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:527
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:360
@ VECTOR_DEINTERLEAVE
VECTOR_DEINTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor ...
Definition ISDOpcodes.h:617
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:208
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:549
@ LOOP_DEPENDENCE_WAR_MASK
Set rounding mode.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
This namespace contains an enum with a value for every intrinsic/builtin function known by LLVM.
Flag
These should be considered private to the implementation of the MCInstrDesc class.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
bool match(Val *V, const Pattern &P)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElt(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
Offsets
Offsets in bytes from the start of the input buffer.
std::pair< JumpTableHeader, JumpTable > JumpTableBlock
void sortAndRangeify(CaseClusterVector &Clusters)
Sort Clusters and merge adjacent cases.
std::vector< CaseCluster > CaseClusterVector
@ CC_Range
A cluster of adjacent case labels with the same destination, or just one case.
@ CC_JumpTable
A cluster of cases suitable for jump table lowering.
@ CC_BitTests
A cluster of cases suitable for bit test lowering.
SmallVector< SwitchWorkListItem, 4 > SwitchWorkList
CaseClusterVector::iterator CaseClusterIt
initializer< Ty > init(const Ty &Val)
LocationClass< Ty > location(Ty &L)
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition Dwarf.h:149
ExceptionBehavior
Exception behavior used for floating point operations.
Definition FPEnv.h:39
@ ebStrict
This corresponds to "fpexcept.strict".
Definition FPEnv.h:42
@ ebMayTrap
This corresponds to "fpexcept.maytrap".
Definition FPEnv.h:41
@ ebIgnore
This corresponds to "fpexcept.ignore".
Definition FPEnv.h:40
constexpr float log2ef
Definition MathExtras.h:51
constexpr double e
constexpr float ln2f
Definition MathExtras.h:49
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
Definition Threading.h:280
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:344
@ Offset
Definition DWP.cpp:532
@ Length
Definition DWP.cpp:532
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition Analysis.cpp:241
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1725
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1655
LLVM_ABI void GetReturnInfo(CallingConv::ID CC, Type *ReturnType, AttributeList attr, SmallVectorImpl< ISD::OutputArg > &Outs, const TargetLowering &TLI, const DataLayout &DL)
Given an LLVM IR type and return type attributes, compute the return value EVTs and flags,...
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
LLVM_ABI bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, SmallVectorImpl< EVT > &ValueVTs, SmallVectorImpl< EVT > *MemVTs=nullptr, SmallVectorImpl< TypeSize > *Offsets=nullptr, TypeSize StartingOffset=TypeSize::getZero())
ComputeValueVTs - Given an LLVM IR type, compute a sequence of EVTs that represent all the individual...
Definition Analysis.cpp:119
LLVM_ABI SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ Done
Definition Threading.h:60
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:293
LLVM_ABI void diagnoseDontCall(const CallInst &CI)
auto successors(const MachineBasicBlock *BB)
bool isIntOrFPConstant(SDValue V)
Return true if V is either a integer or FP constant.
static ConstantRange getRange(Value *Op, SCCPSolver &Solver, const SmallPtrSetImpl< Value * > &InsertedValues)
Helper for getting ranges from Solver.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2136
Value * GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, const DataLayout &DL, bool AllowNonInbounds=true)
Analyze the specified pointer to see if it can be expressed as a base pointer plus a constant offset.
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:243
auto cast_or_null(const Y &Val)
Definition Casting.h:714
constexpr T alignDown(U Value, V Align, W Skew=0)
Returns the largest unsigned integer less than or equal to Value and is Skew mod Align.
Definition MathExtras.h:546
gep_type_iterator gep_type_end(const User *GEP)
constexpr int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition bit.h:154
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
detail::concat_range< ValueT, RangeTs... > concat(RangeTs &&...Ranges)
Returns a concatenated range across two or more ranges.
Definition STLExtras.h:1150
bool isScopedEHPersonality(EHPersonality Pers)
Returns true if this personality uses scope-style EH IR instructions: catchswitch,...
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:202
void ComputeValueTypes(const DataLayout &DL, Type *Ty, SmallVectorImpl< Type * > &Types, SmallVectorImpl< TypeSize > *Offsets=nullptr, TypeSize StartingOffset=TypeSize::getZero())
Given an LLVM IR type, compute non-aggregate subtypes.
Definition Analysis.cpp:72
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1732
LLVM_ABI llvm::SmallVector< int, 16 > createStrideMask(unsigned Start, unsigned Stride, unsigned VF)
Create a stride shuffle mask.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
@ SPF_FMAXNUM
Floating point minnum.
@ SPF_UMIN
Signed minimum.
@ SPF_UMAX
Signed maximum.
@ SPF_SMAX
Unsigned minimum.
@ SPF_FMINNUM
Unsigned maximum.
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
detail::zippy< detail::zip_first, T, U, Args... > zip_first(T &&t, U &&u, Args &&...args)
zip iterator that, for the sake of efficiency, assumes the first iteratee to be the shortest.
Definition STLExtras.h:852
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1622
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
generic_gep_type_iterator<> gep_type_iterator
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
auto succ_size(const MachineBasicBlock *BB)
bool hasSingleElement(ContainerTy &&C)
Returns true if the given container only contains a single element.
Definition STLExtras.h:300
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred)
getFCmpCondCode - Return the ISD condition code corresponding to the given LLVM IR floating-point con...
Definition Analysis.cpp:207
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI Value * salvageDebugInfoImpl(Instruction &I, uint64_t CurrentLocOps, SmallVectorImpl< uint64_t > &Ops, SmallVectorImpl< Value * > &AdditionalValues)
Definition Local.cpp:2274
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Global
Append to llvm.global_dtors.
AtomicOrdering
Atomic ordering for LLVM's memory model.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
bool isFuncletEHPersonality(EHPersonality Pers)
Returns true if this is a personality function that invokes handler funclets (which must return to it...
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
LLVM_ABI bool isAssignmentTrackingEnabled(const Module &M)
Return true if assignment tracking is enabled for module M.
LLVM_ABI llvm::SmallVector< int, 16 > createInterleaveMask(unsigned VF, unsigned NumVecs)
Create an interleave shuffle mask.
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
@ Or
Bitwise or logical OR of integers.
@ Mul
Product of integers.
@ And
Bitwise or logical AND of integers.
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
@ SPNB_RETURNS_NAN
NaN behavior not applicable.
@ SPNB_RETURNS_OTHER
Given one NaN input, returns the NaN.
@ SPNB_RETURNS_ANY
Given one NaN input, returns the non-NaN.
bool isInTailCallPosition(const CallBase &Call, const TargetMachine &TM, bool ReturnsFirstArg=false)
Test if the given instruction is in a position to be optimized with a tail-call.
Definition Analysis.cpp:543
DWARFExpression::Operation Op
ISD::CondCode getFCmpCodeWithoutNaN(ISD::CondCode CC)
getFCmpCodeWithoutNaN - Given an ISD condition code comparing floats, return the equivalent code if w...
Definition Analysis.cpp:229
ArrayRef(const T &OneElt) -> ArrayRef< T >
bool isAsynchronousEHPersonality(EHPersonality Pers)
Returns true if this personality function catches asynchronous exceptions.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI std::optional< RoundingMode > convertStrToRoundingMode(StringRef)
Returns a valid RoundingMode enumerator when given a string that is valid as input in constrained int...
Definition FPEnv.cpp:25
gep_type_iterator gep_type_begin(const User *GEP)
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition STLExtras.h:2120
GlobalValue * ExtractTypeInfo(Value *V)
ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
Definition Analysis.cpp:185
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1897
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
bool all_equal(std::initializer_list< T > Values)
Returns true if all Values in the initializer lists are equal or the list.
Definition STLExtras.h:2108
LLVM_ABI Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset, const DataLayout &DL)
Return the value that a load from C with offset Offset would produce if it is constant and determinab...
unsigned ComputeLinearIndex(Type *Ty, const unsigned *Indices, const unsigned *IndicesEnd, unsigned CurIndex=0)
Compute the linearized index of a member in a nested aggregate/struct/array.
Definition Analysis.cpp:33
T bit_floor(T Value)
Returns the largest integral power of two no greater than Value if Value is nonzero.
Definition bit.h:330
@ Default
The result values are uniform if and only if all operands are uniform.
Definition Uniformity.h:20
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:180
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:869
#define N
#define NC
Definition regutils.h:42
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
Extended Value Type.
Definition ValueTypes.h:35
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition ValueTypes.h:395
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition ValueTypes.h:74
uint64_t getScalarStoreSize() const
Definition ValueTypes.h:402
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition ValueTypes.h:284
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:300
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:147
ElementCount getVectorElementCount() const
Definition ValueTypes.h:350
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:373
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:359
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:385
static LLVM_ABI EVT getEVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:316
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:65
bool isRISCVVectorTuple() const
Return true if this is a vector value type.
Definition ValueTypes.h:179
uint64_t getFixedSizeInBits() const
Return the size of the specified fixed width value type in bits.
Definition ValueTypes.h:381
bool isFixedLengthVector() const
Definition ValueTypes.h:181
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:168
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:323
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition ValueTypes.h:292
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition ValueTypes.h:174
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:328
bool isScalarInteger() const
Return true if this is an integer, but not a vector.
Definition ValueTypes.h:157
EVT changeVectorElementType(EVT EltVT) const
Return a VT for a vector type whose attributes match ourselves with the exception of the element type...
Definition ValueTypes.h:102
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:336
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:152
InputArg - This struct carries flags and type information about a single incoming (formal) argument o...
static const unsigned NoArgIndex
Sentinel value for implicit machine-level input arguments.
OutputArg - This struct carries flags and a value for a single outgoing (actual) argument or outgoing...
ConstraintPrefix Type
Type - The basic type of the constraint: input/output/clobber/label.
Definition InlineAsm.h:128
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:248
This class contains a discriminated union of information about pointers in memory operands,...
static LLVM_ABI MachinePointerInfo getUnknownStack(MachineFunction &MF)
Stack memory without other information.
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
A lightweight accessor for an operand bundle meant to be passed around by value.
This struct represents the registers (physical or virtual) that a particular set of values is assigne...
SmallVector< std::pair< Register, TypeSize >, 4 > getRegsAndSizes() const
Return a list of registers and their sizes.
RegsForValue()=default
SmallVector< unsigned, 4 > RegCount
This list holds the number of registers for each value.
SmallVector< EVT, 4 > ValueVTs
The value types of the values, which may not be legal, and may need be promoted or synthesized from o...
SmallVector< Register, 4 > Regs
This list holds the registers assigned to the values.
void AddInlineAsmOperands(InlineAsm::Kind Code, bool HasMatching, unsigned MatchingIdx, const SDLoc &dl, SelectionDAG &DAG, std::vector< SDValue > &Ops) const
Add this value to the specified inlineasm node operand list.
SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo, const SDLoc &dl, SDValue &Chain, SDValue *Glue, const Value *V=nullptr) const
Emit a series of CopyFromReg nodes that copies from this value and returns the result as a ValueVTs v...
SmallVector< MVT, 4 > RegVTs
The value types of the registers.
void getCopyToRegs(SDValue Val, SelectionDAG &DAG, const SDLoc &dl, SDValue &Chain, SDValue *Glue, const Value *V=nullptr, ISD::NodeType PreferredExtendType=ISD::ANY_EXTEND) const
Emit a series of CopyToReg nodes that copies the specified value into the registers specified by this...
std::optional< CallingConv::ID > CallConv
Records if this value needs to be treated in an ABI dependant manner, different to normal type legali...
bool occupiesMultipleRegs() const
Check if the total RegCount is greater than one.
These are IR-level optimization flags that may be propagated to SDNodes.
void copyFMF(const FPMathOperator &FPMO)
Propagate the fast-math-flags from an IR FPMathOperator.
void setUnpredictable(bool b)
bool hasAllowReassociation() const
void setNoUnsignedWrap(bool b)
void setNoSignedWrap(bool b)
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
A MapVector that performs no allocations if smaller than a certain size.
Definition MapVector.h:257
This structure is used to communicate between SelectionDAGBuilder and SDISel for the code generation ...
SDLoc DL
The debug location of the instruction this CaseBlock was produced from.
static CaseCluster range(const ConstantInt *Low, const ConstantInt *High, MachineBasicBlock *MBB, BranchProbability Prob)
This contains information for each constraint that we are lowering.
TargetLowering::ConstraintType ConstraintType
Information about the constraint code, e.g.
This structure contains all information that is necessary for lowering calls.
CallLoweringInfo & setConvergent(bool Value=true)
CallLoweringInfo & setCFIType(const ConstantInt *Type)
SmallVector< ISD::InputArg, 32 > Ins
Type * OrigRetTy
Original unlegalized return type.
CallLoweringInfo & setDiscardResult(bool Value=true)
CallLoweringInfo & setIsPatchPoint(bool Value=true)
CallLoweringInfo & setDebugLoc(const SDLoc &dl)
CallLoweringInfo & setTailCall(bool Value=true)
CallLoweringInfo & setIsPreallocated(bool Value=true)
CallLoweringInfo & setConvergenceControlToken(SDValue Token)
SmallVector< ISD::OutputArg, 32 > Outs
Type * RetTy
Same as OrigRetTy, or partially legalized for soft float libcalls.
CallLoweringInfo & setChain(SDValue InChain)
CallLoweringInfo & setPtrAuth(PtrAuthInfo Value)
CallLoweringInfo & setCallee(CallingConv::ID CC, Type *ResultType, SDValue Target, ArgListTy &&ArgsList, AttributeSet ResultAttrs={})
This structure is used to pass arguments to makeLibCall function.
MakeLibCallOptions & setDiscardResult(bool Value=true)
This structure contains the information necessary for lowering pointer-authenticating indirect calls.
void addIPToStateRange(const InvokeInst *II, MCSymbol *InvokeBegin, MCSymbol *InvokeEnd)